| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## WebAuthn - ~/lib/Web/Authn/Attestation.pm | ||
| 3 | ## Version v0.1.0 | ||
| 4 | ## Copyright(c) 2026 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2026/09/09 | ||
| 7 | ## Modified 2026/09/11 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## This program is free software; you can redistribute it and/or modify it | ||
| 11 | ## under the same terms as Perl itself. | ||
| 12 | ##---------------------------------------------------------------------------- | ||
| 13 | package Web::Authn::Attestation; | ||
| 14 | BEGIN | ||
| 15 | { | ||
| 16 | use v5.16.0; | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'Web::Authn' ); | ||
| 20 | use vars qw( $VERSION $JSON_CLASS ); | ||
| 21 | use Digest::SHA qw( sha256 ); | ||
| 22 | use MIME::Base64 qw( decode_base64 encode_base64 ); | ||
| 23 | use Web::Authn::CBOR; | ||
| 24 | use Web::Authn::COSE; | ||
| 25 | use Web::Authn::Crypto; | ||
| 26 | use Web::Authn::Exception; | ||
| 27 | use Web::Authn::Parse; | ||
| 28 | # JSON backend detection: prefer Cpanel::JSON::XS (fastest, most rigorous), fall back | ||
| 29 | # to JSON::XS, then JSON::PP (core since Perl 5.14). | ||
| 30 | our $JSON_CLASS; | ||
| 31 | local $@; | ||
| 32 | if( eval{ require Cpanel::JSON::XS; 1 } ) | ||
| 33 | { | ||
| 34 | $JSON_CLASS = 'Cpanel::JSON::XS'; | ||
| 35 | } | ||
| 36 | elsif( eval{ require JSON::XS; 1 } ) | ||
| 37 | { | ||
| 38 | $JSON_CLASS = 'JSON::XS'; | ||
| 39 | } | ||
| 40 | else | ||
| 41 | { | ||
| 42 | require JSON::PP; | ||
| 43 | $JSON_CLASS = 'JSON::PP'; | ||
| 44 | } | ||
| 45 | our $VERSION = 'v0.1.0'; | ||
| 46 | }; | ||
| 47 | |||
| 48 | use strict; | ||
| 49 | use warnings; | ||
| 50 | |||
| 51 | sub sha384 { Digest::SHA::sha384( $_[0] ) } | ||
| 52 | |||
| 53 | sub sha512 { Digest::SHA::sha512( $_[0] ) } | ||
| 54 | |||
| 55 | sub verify | ||
| 56 | { | ||
| 57 | my( %arg ) = @_; | ||
| 58 | my $fmt = Web::Authn::Parse::_plain( $arg{fmt} ); | ||
| 59 | return( verify_none( %arg ) ) if( $fmt eq 'none' ); | ||
| 60 | return( verify_packed( %arg ) ) if( $fmt eq 'packed' ); | ||
| 61 | return( verify_fido_u2f( %arg ) ) if( $fmt eq 'fido-u2f' ); | ||
| 62 | return( verify_apple( %arg ) ) if( $fmt eq 'apple' ); | ||
| 63 | return( verify_tpm( %arg ) ) if( $fmt eq 'tpm' ); | ||
| 64 | return( verify_android_safetynet( %arg ) ) if( $fmt eq 'android-safetynet' ); | ||
| 65 | return( verify_android_key( %arg ) ) if( $fmt eq 'android-key' ); | ||
| 66 | Web::Authn::Exception::InvalidRegistration->throw( qq{Unsupported attestation type "$fmt"} ); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub verify_android_key | ||
| 70 | { | ||
| 71 | my( %arg ) = @_; | ||
| 72 | my $stmt = $arg{att_stmt}; | ||
| 73 | unless( $stmt->{sig} && $stmt->{x5c} ) | ||
| 74 | { | ||
| 75 | Web::Authn::Exception::InvalidRegistration->throw( 'android-key attestation missing sig/x5c' ); | ||
| 76 | } | ||
| 77 | Web::Authn::Crypto::validate_certificate_chain( | ||
| 78 | x5c => $stmt->{x5c}, | ||
| 79 | pem_root_certs => $arg{pem_root_certs} || [], | ||
| 80 | ); | ||
| 81 | my $verification_data = $arg{auth_raw} . sha256( $arg{client_data_json} ); | ||
| 82 | my $pk = Web::Authn::Crypto::public_key_from_cert_der( $stmt->{x5c}->[0] ); | ||
| 83 | local $@; | ||
| 84 | eval | ||
| 85 | { | ||
| 86 | Web::Authn::Crypto::verify_signature( | ||
| 87 | public_key => $pk, | ||
| 88 | alg => $stmt->{alg} || Web::Authn::COSE::ECDSA_SHA_256, | ||
| 89 | signature => $stmt->{sig}, | ||
| 90 | data => $verification_data, | ||
| 91 | ); | ||
| 92 | 1; | ||
| 93 | } or Web::Authn::Exception::InvalidRegistration->throw( 'Could not verify android-key attestation signature' ); | ||
| 94 | return(1); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub verify_android_safetynet | ||
| 98 | { | ||
| 99 | my( %arg ) = @_; | ||
| 100 | my $stmt = $arg{att_stmt}; | ||
| 101 | my $jwt = Web::Authn::Parse::_plain( $stmt->{response} ); | ||
| 102 | unless( defined( $jwt ) ) | ||
| 103 | { | ||
| 104 | Web::Authn::Exception::InvalidRegistration->throw( 'SafetyNet attestation missing response' ); | ||
| 105 | } | ||
| 106 | my( $h64, $p64, $s64 ) = split( /\./, $jwt, 3 ); | ||
| 107 | unless( $h64 && $p64 && defined( $s64 ) ) | ||
| 108 | { | ||
| 109 | Web::Authn::Exception::InvalidRegistration->throw( 'SafetyNet response is not a JWT' ); | ||
| 110 | } | ||
| 111 | my $header = $JSON_CLASS->new->utf8->decode(_b64u( $h64 ) ); | ||
| 112 | my $payload = $JSON_CLASS->new->utf8->decode(_b64u( $p64 ) ); | ||
| 113 | my $sig = _b64u( $s64 ); | ||
| 114 | my $x5c = $header->{x5c} || []; | ||
| 115 | unless( @$x5c ) | ||
| 116 | { | ||
| 117 | Web::Authn::Exception::InvalidRegistration->throw( 'SafetyNet JWT missing x5c' ); | ||
| 118 | } | ||
| 119 | my $leaf = decode_base64( $x5c->[0] ); | ||
| 120 | my $pk = Web::Authn::Crypto::public_key_from_cert_der( $leaf ); | ||
| 121 | my $signing_input = "$h64.$p64"; | ||
| 122 | local $@; | ||
| 123 | eval | ||
| 124 | { | ||
| 125 | Web::Authn::Crypto::verify_signature( | ||
| 126 | public_key => $pk, | ||
| 127 | alg => Web::Authn::COSE::RSASSA_PKCS1_SHA_256, | ||
| 128 | signature => $sig, | ||
| 129 | data => $signing_input, | ||
| 130 | ); | ||
| 131 | 1; | ||
| 132 | } or Web::Authn::Exception::InvalidRegistration->throw( 'SafetyNet JWT signature invalid' ); | ||
| 133 | |||
| 134 | unless( $payload->{ctsProfileMatch} ) | ||
| 135 | { | ||
| 136 | Web::Authn::Exception::InvalidRegistration->throw( 'SafetyNet ctsProfileMatch was false' ); | ||
| 137 | } | ||
| 138 | |||
| 139 | my $nonce_expected = encode_base64( sha256( $arg{auth_raw} . sha256( $arg{client_data_json} ) ), '' ); | ||
| 140 | # SafetyNet nonce is base64 of sha256(attestationObject || clientDataHash) — some impls use authData. | ||
| 141 | # Accept either the provided nonce matching hash of attestation object + clientDataHash | ||
| 142 | # or authData + clientDataHash. | ||
| 143 | my $nonce = $payload->{nonce} || ''; | ||
| 144 | my $alt = encode_base64( sha256( ( $arg{attestation_object} || $arg{auth_raw} ) . sha256( $arg{client_data_json} ) ), '' ); | ||
| 145 | unless( $nonce eq $nonce_expected || $nonce eq $alt ) | ||
| 146 | { | ||
| 147 | Web::Authn::Exception::InvalidRegistration->throw( 'SafetyNet nonce mismatch' ); | ||
| 148 | } | ||
| 149 | return(1); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub verify_apple | ||
| 153 | { | ||
| 154 | my( %arg ) = @_; | ||
| 155 | my $stmt = $arg{att_stmt}; | ||
| 156 | $stmt->{x5c} && @{$stmt->{x5c}} | ||
| 157 | or Web::Authn::Exception::InvalidRegistration->throw( 'Attestation statement was missing x5c (Apple)' ); | ||
| 158 | |||
| 159 | my $roots = [ @{$arg{pem_root_certs} || []} ]; | ||
| 160 | if( my $built_in = _apple_root() ) | ||
| 161 | { | ||
| 162 | push( @$roots, $built_in ); | ||
| 163 | } | ||
| 164 | Web::Authn::Crypto::validate_certificate_chain( x5c => $stmt->{x5c}, pem_root_certs => $roots ); | ||
| 165 | |||
| 166 | my $nonce_to_hash = $arg{auth_raw} . sha256( $arg{client_data_json} ); | ||
| 167 | my $nonce = sha256( $nonce_to_hash ); | ||
| 168 | my $ext = Web::Authn::Crypto::extract_extension_octet( $stmt->{x5c}->[0], '1.2.840.113635.100.8.2' ); | ||
| 169 | unless( defined( $ext ) ) | ||
| 170 | { | ||
| 171 | Web::Authn::Exception::InvalidRegistration->throw( 'Certificate missing extension 1.2.840.113635.100.8.2 (Apple)' ); | ||
| 172 | } | ||
| 173 | # Apple wraps nonce in an ASN.1 structure; take the last 32 bytes if longer. | ||
| 174 | my $ext_nonce = length( $ext ) > 32 ? substr( $ext, -32 ) : $ext; | ||
| 175 | unless( $ext_nonce eq $nonce ) | ||
| 176 | { | ||
| 177 | Web::Authn::Exception::InvalidRegistration->throw( 'Certificate nonce was not expected value (Apple)' ); | ||
| 178 | } | ||
| 179 | |||
| 180 | my $cert_pk = Web::Authn::Crypto::public_key_from_cert_der( $stmt->{x5c}->[0] ); | ||
| 181 | my $cred_pk = Web::Authn::Crypto::cose_to_public_key( | ||
| 182 | Web::Authn::Parse::decode_credential_public_key( $arg{credential_public_key} ) | ||
| 183 | ); | ||
| 184 | my $a = Web::Authn::Crypto::export_spki_der( $cert_pk ); | ||
| 185 | my $b = Web::Authn::Crypto::export_spki_der( $cred_pk ); | ||
| 186 | unless( $a eq $b ) | ||
| 187 | { | ||
| 188 | Web::Authn::Exception::InvalidRegistration->throw( 'Certificate public key did not match credential public key (Apple)' ); | ||
| 189 | } | ||
| 190 | return(1); | ||
| 191 | } | ||
| 192 | |||
| 193 | sub verify_fido_u2f | ||
| 194 | { | ||
| 195 | my( %arg ) = @_; | ||
| 196 | my $stmt = $arg{att_stmt}; | ||
| 197 | unless( $stmt->{sig} ) | ||
| 198 | { | ||
| 199 | Web::Authn::Exception::InvalidRegistration->throw( 'Attestation statement was missing signature (FIDO-U2F)' ); | ||
| 200 | } | ||
| 201 | unless( $stmt->{x5c} && @{$stmt->{x5c}} ) | ||
| 202 | { | ||
| 203 | Web::Authn::Exception::InvalidRegistration->throw('Attestation statement was missing certificate (FIDO-U2F)'); | ||
| 204 | } | ||
| 205 | unless( @{$stmt->{x5c}} == 1 ) | ||
| 206 | { | ||
| 207 | Web::Authn::Exception::InvalidRegistration->throw('Attestation statement contained too many certificates (FIDO-U2F)'); | ||
| 208 | } | ||
| 209 | |||
| 210 | Web::Authn::Crypto::validate_certificate_chain( | ||
| 211 | x5c => $stmt->{x5c}, | ||
| 212 | pem_root_certs => $arg{pem_root_certs} || [], | ||
| 213 | ); | ||
| 214 | |||
| 215 | my $aaguid = Web::Authn::Parse::aaguid_to_string( $arg{aaguid} ); | ||
| 216 | unless( $aaguid eq '00000000-0000-0000-0000-000000000000' ) | ||
| 217 | { | ||
| 218 | Web::Authn::Exception::InvalidRegistration->throw( "AAGUID $aaguid was not expected 00000000-0000-0000-0000-000000000000 (FIDO-U2F)" ); | ||
| 219 | } | ||
| 220 | |||
| 221 | my $decoded = Web::Authn::Parse::decode_credential_public_key( $arg{credential_public_key} ); | ||
| 222 | unless( $decoded->{kty} == Web::Authn::COSE::KTY_EC2 ) | ||
| 223 | { | ||
| 224 | Web::Authn::Exception::InvalidRegistration->throw( 'Credential public key was not EC2 (FIDO-U2F)' ); | ||
| 225 | } | ||
| 226 | |||
| 227 | my $public_key_u2f = pack( 'C', 0x04 ) . $decoded->{'x'} . $decoded->{'y'}; | ||
| 228 | my $client_hash = sha256( $arg{client_data_json} ); | ||
| 229 | my $verification_data = pack( 'C', 0x00 ) . $arg{rp_id_hash} . $client_hash . $arg{credential_id} . $public_key_u2f; | ||
| 230 | |||
| 231 | my $pk = Web::Authn::Crypto::public_key_from_cert_der( $stmt->{x5c}->[0] ); | ||
| 232 | local $@; | ||
| 233 | eval | ||
| 234 | { | ||
| 235 | Web::Authn::Crypto::verify_signature( | ||
| 236 | public_key => $pk, | ||
| 237 | alg => Web::Authn::COSE::ECDSA_SHA_256, | ||
| 238 | signature => $stmt->{sig}, | ||
| 239 | data => $verification_data, | ||
| 240 | ); | ||
| 241 | 1; | ||
| 242 | } or Web::Authn::Exception::InvalidRegistration->throw( 'Could not verify attestation statement signature (FIDO-U2F)' ); | ||
| 243 | return(1); | ||
| 244 | } | ||
| 245 | |||
| 246 | sub verify_none | ||
| 247 | { | ||
| 248 | my( %arg ) = @_; | ||
| 249 | my $stmt = $arg{att_stmt} || {}; | ||
| 250 | my $any = grep{ defined( $stmt->{ $_ } ) } keys( %$stmt ); | ||
| 251 | if( $any ) | ||
| 252 | { | ||
| 253 | Web::Authn::Exception::InvalidRegistration->throw( 'None attestation had unexpected attestation statement' ); | ||
| 254 | } | ||
| 255 | return(1); | ||
| 256 | } | ||
| 257 | |||
| 258 | sub verify_packed | ||
| 259 | { | ||
| 260 | my( %arg ) = @_; | ||
| 261 | my $stmt = $arg{att_stmt}; | ||
| 262 | unless( $stmt->{sig} ) | ||
| 263 | { | ||
| 264 | Web::Authn::Exception::InvalidRegistration->throw('Attestation statement was missing signature (Packed)'); | ||
| 265 | } | ||
| 266 | unless( defined( $stmt->{alg} ) ) | ||
| 267 | { | ||
| 268 | Web::Authn::Exception::InvalidRegistration->throw('Attestation statement was missing algorithm (Packed)'); | ||
| 269 | } | ||
| 270 | |||
| 271 | my $auth_raw = $arg{auth_raw}; | ||
| 272 | my $client_hash = sha256( $arg{client_data_json} ); | ||
| 273 | my $verification_data = $auth_raw . $client_hash; | ||
| 274 | |||
| 275 | if( $stmt->{x5c} && @{$stmt->{x5c}} ) | ||
| 276 | { | ||
| 277 | Web::Authn::Crypto::validate_certificate_chain( | ||
| 278 | x5c => $stmt->{x5c}, | ||
| 279 | pem_root_certs => $arg{pem_root_certs} || [], | ||
| 280 | ); | ||
| 281 | my $pk = Web::Authn::Crypto::public_key_from_cert_der( $stmt->{x5c}[0] ); | ||
| 282 | local $@; | ||
| 283 | eval | ||
| 284 | { | ||
| 285 | Web::Authn::Crypto::verify_signature( | ||
| 286 | public_key => $pk, | ||
| 287 | alg => $stmt->{alg}, | ||
| 288 | signature => $stmt->{sig}, | ||
| 289 | data => $verification_data, | ||
| 290 | ); | ||
| 291 | 1; | ||
| 292 | } or Web::Authn::Exception::InvalidRegistration->throw( 'Could not verify attestation statement signature (Packed)' ); | ||
| 293 | } | ||
| 294 | else | ||
| 295 | { | ||
| 296 | my $decoded = Web::Authn::Parse::decode_credential_public_key( $arg{credential_public_key} ); | ||
| 297 | if( $decoded->{alg} != $stmt->{alg} ) | ||
| 298 | { | ||
| 299 | Web::Authn::Exception::InvalidRegistration->throw( "Credential public key alg $decoded->{alg} did not equal attestation statement alg $stmt->{alg}" ); | ||
| 300 | } | ||
| 301 | my $pk = Web::Authn::Crypto::cose_to_public_key( $decoded ); | ||
| 302 | local $@; | ||
| 303 | eval | ||
| 304 | { | ||
| 305 | Web::Authn::Crypto::verify_signature( | ||
| 306 | public_key => $pk, | ||
| 307 | alg => $stmt->{alg}, | ||
| 308 | signature => $stmt->{sig}, | ||
| 309 | data => $verification_data, | ||
| 310 | ); | ||
| 311 | 1; | ||
| 312 | } or Web::Authn::Exception::InvalidRegistration->throw( 'Could not verify attestation statement signature (Packed|Self)' ); | ||
| 313 | } | ||
| 314 | return(1); | ||
| 315 | } | ||
| 316 | |||
| 317 | sub verify_tpm | ||
| 318 | { | ||
| 319 | my( %arg ) = @_; | ||
| 320 | my $stmt = $arg{att_stmt}; | ||
| 321 | unless( $stmt->{sig} && | ||
| 322 | $stmt->{x5c} && | ||
| 323 | $stmt->{cert_info} && | ||
| 324 | $stmt->{pub_area} ) | ||
| 325 | { | ||
| 326 | Web::Authn::Exception::InvalidRegistration->throw( 'TPM attestation statement missing fields' ); | ||
| 327 | } | ||
| 328 | |||
| 329 | Web::Authn::Crypto::validate_certificate_chain( | ||
| 330 | x5c => $stmt->{x5c}, | ||
| 331 | pem_root_certs => $arg{pem_root_certs} || [], | ||
| 332 | ); | ||
| 333 | |||
| 334 | my $auth_raw = $arg{auth_raw}; | ||
| 335 | my $att_to_be_signed = $auth_raw . sha256( $arg{client_data_json} ); | ||
| 336 | my $pk = Web::Authn::Crypto::public_key_from_cert_der( $stmt->{x5c}->[0] ); | ||
| 337 | local $@; | ||
| 338 | eval | ||
| 339 | { | ||
| 340 | Web::Authn::Crypto::verify_signature( | ||
| 341 | public_key => $pk, | ||
| 342 | alg => $stmt->{alg} || Web::Authn::COSE::RSASSA_PKCS1_SHA_256, | ||
| 343 | signature => $stmt->{sig}, | ||
| 344 | data => $stmt->{cert_info}, | ||
| 345 | ); | ||
| 346 | 1; | ||
| 347 | } or Web::Authn::Exception::InvalidRegistration->throw( 'Could not verify TPM certInfo signature' ); | ||
| 348 | # extraData in TPMS_ATTEST should be a hash of attToBeSigned. We check it appears. | ||
| 349 | unless( index( $stmt->{cert_info}, sha256( $att_to_be_signed ) ) >= 0 or | ||
| 350 | index( $stmt->{cert_info}, sha384( $att_to_be_signed ) ) >= 0 or | ||
| 351 | index( $stmt->{cert_info}, sha512( $att_to_be_signed ) ) >= 0 ) | ||
| 352 | { | ||
| 353 | Web::Authn::Exception::InvalidRegistration->throw( 'TPM certInfo extraData did not contain hash of attToBeSigned' ); | ||
| 354 | } | ||
| 355 | return(1); | ||
| 356 | } | ||
| 357 | |||
| 358 | # Apple WebAuthn Root CA is published by Apple. Callers should pass pem_root_certs | ||
| 359 | # for production verification. We do not embed the full CA blob here. | ||
| 360 | sub _apple_root { return } | ||
| 361 | |||
| 362 | sub _b64u | ||
| 363 | { | ||
| 364 | my $s = Web::Authn::Parse::_plain( shift( @_ ) ); | ||
| 365 | $s =~ tr{-_}{+/}; | ||
| 366 | my $pad = ( 4 - length( $s ) % 4 ) % 4; | ||
| 367 | $s .= '=' x $pad; | ||
| 368 | return( decode_base64( $s ) ); | ||
| 369 | } | ||
| 370 | |||
| 371 | 1; | ||
| 372 | # NOTE: POD | ||
| 373 | __END__ |