| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## WebAuthn - ~/lib/Web/Authn/Crypto.pm | ||
| 3 | ## Version v0.2.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::Crypto; | ||
| 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 ); | ||
| 21 | use Digest::SHA qw( sha1 sha256 sha384 sha512 ); | ||
| 22 | use MIME::Base64 qw( encode_base64 decode_base64 ); | ||
| 23 | use Scalar::Util (); | ||
| 24 | use Web::Authn::COSE; | ||
| 25 | use Web::Authn::Exception; | ||
| 26 | use Web::Authn::Parse; | ||
| 27 | our $VERSION = 'v0.2.0'; | ||
| 28 | }; | ||
| 29 | |||
| 30 | use strict; | ||
| 31 | use warnings; | ||
| 32 | |||
| 33 | my $_HAS_CRYPTX; | ||
| 34 | |||
| 35 | # Convert decoded COSE key hashref to a CryptX public key object. | ||
| 36 | sub cose_to_public_key | ||
| 37 | { | ||
| 38 | my $decoded = shift( @_ ); | ||
| 39 | require_cryptx(); | ||
| 40 | my $kty = $decoded->{kty}; | ||
| 41 | if( $kty == Web::Authn::COSE::KTY_EC2 ) | ||
| 42 | { | ||
| 43 | my $curve = Web::Authn::COSE::curve_name( $decoded->{crv} ) or | ||
| 44 | Web::Authn::Exception::UnsupportedAlgorithm->throw( 'unsupported EC curve' ); | ||
| 45 | # key2hash form: pub_x / pub_y are hex. Do not use the JWK form | ||
| 46 | # (kty/crv/x/y): CryptX would treat x/y as base64url and decode them again. | ||
| 47 | my $pk = Crypt::PK::ECC->new; | ||
| 48 | $pk->import_key({ | ||
| 49 | curve_name => $curve, | ||
| 50 | pub_x => unpack( 'H*', $decoded->{'x'} ), | ||
| 51 | pub_y => unpack( 'H*', $decoded->{'y'} ), | ||
| 52 | }); | ||
| 53 | return( $pk ); | ||
| 54 | } | ||
| 55 | if( $kty == Web::Authn::COSE::KTY_RSA ) | ||
| 56 | { | ||
| 57 | my $pk = Crypt::PK::RSA->new; | ||
| 58 | $pk->import_key({ | ||
| 59 | kty => 'RSA', | ||
| 60 | n => unpack( 'H*', $decoded->{n} ), | ||
| 61 | e => unpack( 'H*', $decoded->{e} ), | ||
| 62 | }); | ||
| 63 | return( $pk ); | ||
| 64 | } | ||
| 65 | if( $kty == Web::Authn::COSE::KTY_OKP ) | ||
| 66 | { | ||
| 67 | if( $decoded->{alg} != Web::Authn::COSE::EDDSA || | ||
| 68 | $decoded->{crv} != Web::Authn::COSE::CRV_ED25519 ) | ||
| 69 | { | ||
| 70 | Web::Authn::Exception::UnsupportedAlgorithm->throw( 'only Ed25519 OKP keys are supported' ); | ||
| 71 | } | ||
| 72 | return( Crypt::PK::Ed25519->new->import_key_raw($decoded->{'x'}, 'public' ) ); | ||
| 73 | } | ||
| 74 | Web::Authn::Exception::UnsupportedAlgorithm->throw( "unsupported COSE kty $kty" ); | ||
| 75 | } | ||
| 76 | |||
| 77 | sub export_spki_der | ||
| 78 | { | ||
| 79 | my $pk = shift( @_ ); | ||
| 80 | return( $pk->export_key_der( 'public' ) ); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub extract_extension_octet | ||
| 84 | { | ||
| 85 | my( $cert_der, $oid ) = @_; | ||
| 86 | my $exts = _extensions( $cert_der ) or return; | ||
| 87 | return( $exts->{$oid} ); | ||
| 88 | } | ||
| 89 | |||
| 90 | # Minimal X.509 walk: Certificate ::= SEQUENCE { tbsCertificate, signatureAlgorithm, signature } | ||
| 91 | # tbsCertificate ::= SEQUENCE { version?, serial, sig, issuer, validity, subject, subjectPublicKeyInfo, ... } | ||
| 92 | sub extract_spki_from_cert | ||
| 93 | { | ||
| 94 | my $der = Web::Authn::Parse::maybe_bytes( shift( @_ ) ); | ||
| 95 | my( $cert, $off ) = _asn1_read( $der, 0 ); | ||
| 96 | $cert->{tag} == 16 or | ||
| 97 | Web::Authn::Exception::InvalidCertificateChain->throw( 'cert is not a SEQUENCE' ); | ||
| 98 | my $tbs = _first_seq( $cert->{value} ); | ||
| 99 | my @els = @{$tbs->{value}}; | ||
| 100 | # We skip optional [0] version | ||
| 101 | my $i = 0; | ||
| 102 | $i++ if( $els[0]->{cls} == 2 && $els[0]->{tag} == 0 ); | ||
| 103 | # serial, signature, issuer, validity, subject, spki | ||
| 104 | $i += 5; | ||
| 105 | my $spki = $els[$i] or | ||
| 106 | Web::Authn::Exception::InvalidCertificateChain->throw( 'missing subjectPublicKeyInfo' ); | ||
| 107 | return( substr( $der, $spki->{start}, $spki->{len} ) ); | ||
| 108 | } | ||
| 109 | |||
| 110 | sub public_key_from_cert_der | ||
| 111 | { | ||
| 112 | my $cert_der = Web::Authn::Parse::maybe_bytes( shift( @_ ) ); | ||
| 113 | my $spki = extract_spki_from_cert( $cert_der ); | ||
| 114 | return( public_key_from_der_spki( $spki ) ); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub public_key_from_der_spki | ||
| 118 | { | ||
| 119 | my $spki = Web::Authn::Parse::maybe_bytes( shift( @_ ) ); | ||
| 120 | require_cryptx(); | ||
| 121 | my $pem = _der_to_pem( $spki, 'PUBLIC KEY' ); | ||
| 122 | local $@; | ||
| 123 | foreach my $class ( qw( Crypt::PK::ECC Crypt::PK::RSA Crypt::PK::Ed25519 ) ) | ||
| 124 | { | ||
| 125 | my $pk = eval{ $class->new( \$pem ) }; | ||
| 126 | return( $pk ) if( $pk ); | ||
| 127 | } | ||
| 128 | Web::Authn::Exception->throw( 'unable to load SubjectPublicKeyInfo' ); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub require_cryptx | ||
| 132 | { | ||
| 133 | _cryptx() or Web::Authn::Exception->throw( 'Web::Authn requires CryptX (Crypt::PK::ECC/RSA/Ed25519) for signature verification' ); | ||
| 134 | } | ||
| 135 | |||
| 136 | # In-process analogue of py_webauthn's OpenSSL.crypto.X509StoreContext. | ||
| 137 | # No openssl(1) subprocess. CryptX verifies each certificate signature; | ||
| 138 | # we parse issuer/subject/validity from DER ourselves. | ||
| 139 | sub validate_certificate_chain | ||
| 140 | { | ||
| 141 | my( %arg ) = @_; | ||
| 142 | my $x5c = $arg{x5c} || []; | ||
| 143 | my $pem_roots = $arg{pem_root_certs} || []; | ||
| 144 | unless( @$x5c ) | ||
| 145 | { | ||
| 146 | Web::Authn::Exception::InvalidCertificateChain->throw( 'empty x5c' ); | ||
| 147 | } | ||
| 148 | |||
| 149 | local $@; | ||
| 150 | my @chain; | ||
| 151 | foreach my $c ( @$x5c ) | ||
| 152 | { | ||
| 153 | my $parsed = eval{ _parse_certificate($c) }; | ||
| 154 | unless( $parsed ) | ||
| 155 | { | ||
| 156 | Web::Authn::Exception::InvalidCertificateChain->throw( 'malformed certificate in x5c' ); | ||
| 157 | } | ||
| 158 | push( @chain, $parsed ); | ||
| 159 | } | ||
| 160 | |||
| 161 | # Same short-circuit as py_webauthn: no roots means "do not pin". | ||
| 162 | return(1) unless( @$pem_roots ); | ||
| 163 | |||
| 164 | my @roots; | ||
| 165 | foreach my $blob ( @$pem_roots ) | ||
| 166 | { | ||
| 167 | $blob = $$blob if( ( Scalar::Util::reftype( $blob ) || '' ) eq 'SCALAR' ); | ||
| 168 | $blob = Web::Authn::Parse::_plain( $blob ); | ||
| 169 | foreach my $der ( _certs_from_blob( $blob ) ) | ||
| 170 | { | ||
| 171 | my $parsed = eval{ _parse_certificate( $der ) }; | ||
| 172 | unless( $parsed ) | ||
| 173 | { | ||
| 174 | Web::Authn::Exception::InvalidCertificateChain->throw( 'malformed root certificate' ); | ||
| 175 | } | ||
| 176 | push( @roots, $parsed ); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | unless( @roots ) | ||
| 180 | { | ||
| 181 | Web::Authn::Exception::InvalidCertificateChain->throw( 'no usable root certificates' ); | ||
| 182 | } | ||
| 183 | |||
| 184 | foreach my $i ( 0 .. $#chain - 1 ) | ||
| 185 | { | ||
| 186 | unless( _verify_issued_by( $chain[ $i ], $chain[ $i + 1 ] ) ) | ||
| 187 | { | ||
| 188 | Web::Authn::Exception::InvalidCertificateChain->throw( 'intermediate certificate chain signature failed' ); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | my $end = $chain[-1]; | ||
| 193 | foreach my $root ( @roots ) | ||
| 194 | { | ||
| 195 | return(1) if( _same_cert( $end, $root ) ); | ||
| 196 | return(1) if( _verify_issued_by( $end, $root ) ); | ||
| 197 | } | ||
| 198 | Web::Authn::Exception::InvalidCertificateChain->throw( 'Certificate chain could not be validated against supplied roots' ); | ||
| 199 | } | ||
| 200 | |||
| 201 | sub verify_signature | ||
| 202 | { | ||
| 203 | my( %arg ) = @_; | ||
| 204 | my $pk = $arg{public_key}; | ||
| 205 | my $alg = $arg{alg}; | ||
| 206 | my $sig = Web::Authn::Parse::maybe_bytes( $arg{signature} ); | ||
| 207 | my $data = Web::Authn::Parse::maybe_bytes( $arg{data} ); | ||
| 208 | unless( defined( $pk ) && defined( $sig ) && defined( $data ) ) | ||
| 209 | { | ||
| 210 | Web::Authn::Exception->throw( 'verify_signature missing arguments' ); | ||
| 211 | } | ||
| 212 | |||
| 213 | if( Web::Authn::COSE::is_ecdsa( $alg ) ) | ||
| 214 | { | ||
| 215 | my $hash = Web::Authn::COSE::alg_hash_name( $alg ); | ||
| 216 | # CryptX expects DER ECDSA signatures, which WebAuthn uses. | ||
| 217 | $pk->verify_message( $sig, $data, $hash ) or | ||
| 218 | Web::Authn::Exception->throw( 'ECDSA signature verification failed' ); | ||
| 219 | return(1); | ||
| 220 | } | ||
| 221 | if( $alg == Web::Authn::COSE::EDDSA ) | ||
| 222 | { | ||
| 223 | $pk->verify_message( $sig, $data ) or | ||
| 224 | Web::Authn::Exception->throw( 'EdDSA signature verification failed' ); | ||
| 225 | return(1); | ||
| 226 | } | ||
| 227 | if( Web::Authn::COSE::is_rsa_pkcs( $alg ) ) | ||
| 228 | { | ||
| 229 | my $hash = Web::Authn::COSE::alg_hash_name( $alg ); | ||
| 230 | $pk->verify_message( $sig, $data, $hash, 'v1.5' ) or | ||
| 231 | Web::Authn::Exception->throw( 'RSA PKCS1 signature verification failed' ); | ||
| 232 | return(1); | ||
| 233 | } | ||
| 234 | if( Web::Authn::COSE::is_rsa_pss( $alg ) ) | ||
| 235 | { | ||
| 236 | my $hash = Web::Authn::COSE::alg_hash_name( $alg ); | ||
| 237 | $pk->verify_message( $sig, $data, $hash, 'pss' ) or | ||
| 238 | Web::Authn::Exception->throw( 'RSA-PSS signature verification failed' ); | ||
| 239 | return(1); | ||
| 240 | } | ||
| 241 | Web::Authn::Exception::UnsupportedAlgorithm->throw( "unsupported signature alg $alg" ); | ||
| 242 | } | ||
| 243 | |||
| 244 | sub _alg_oid | ||
| 245 | { | ||
| 246 | my( $alg_node ) = @_; | ||
| 247 | return('') unless( $alg_node && $alg_node->{cons} ); | ||
| 248 | my $oid_node = $alg_node->{value}->[0]; | ||
| 249 | return( _decode_oid( $oid_node->{value} ) ); | ||
| 250 | } | ||
| 251 | |||
| 252 | # NOTE: tiny DER reader | ||
| 253 | sub _asn1_read | ||
| 254 | { | ||
| 255 | my( $buf, $off ) = @_; | ||
| 256 | unless( $off < length( $buf ) ) | ||
| 257 | { | ||
| 258 | Web::Authn::Exception::InvalidStructure->throw( 'truncated DER' ); | ||
| 259 | } | ||
| 260 | my $start = $off; | ||
| 261 | my $ib = unpack( 'C', substr( $buf, $off, 1 ) ); | ||
| 262 | $off++; | ||
| 263 | my $cls = ( $ib >> 6 ) & 3; | ||
| 264 | my $cons = ( $ib >> 5 ) & 1; | ||
| 265 | my $tag = $ib & 31; | ||
| 266 | if( $tag == 31 ) | ||
| 267 | { | ||
| 268 | $tag = 0; | ||
| 269 | while(1) | ||
| 270 | { | ||
| 271 | unless( $off < length( $buf ) ) | ||
| 272 | { | ||
| 273 | Web::Authn::Exception::InvalidStructure->throw( 'truncated DER tag' ); | ||
| 274 | } | ||
| 275 | my $b = unpack( 'C', substr( $buf, $off, 1 ) ); | ||
| 276 | $off++; | ||
| 277 | $tag = ( $tag << 7 ) | ( $b & 0x7f ); | ||
| 278 | last unless( $b & 0x80 ); | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | unless( $off < length( $buf ) ) | ||
| 283 | { | ||
| 284 | Web::Authn::Exception::InvalidStructure->throw( 'truncated DER len' ); | ||
| 285 | } | ||
| 286 | |||
| 287 | my $lb = unpack( 'C', substr( $buf, $off, 1 ) ); | ||
| 288 | $off++; | ||
| 289 | my $len; | ||
| 290 | if( $lb < 128 ) | ||
| 291 | { | ||
| 292 | $len = $lb; | ||
| 293 | } | ||
| 294 | else | ||
| 295 | { | ||
| 296 | my $n = $lb & 0x7f; | ||
| 297 | unless( $off + $n <= length( $buf ) ) | ||
| 298 | { | ||
| 299 | Web::Authn::Exception::InvalidStructure->throw( 'truncated DER len' ); | ||
| 300 | } | ||
| 301 | $len = 0; | ||
| 302 | for( 1 .. $n ) | ||
| 303 | { | ||
| 304 | $len = ( $len << 8 ) + unpack( 'C', substr( $buf, $off, 1 ) ); | ||
| 305 | $off++ | ||
| 306 | } | ||
| 307 | } | ||
| 308 | |||
| 309 | unless( $off + $len <= length( $buf ) ) | ||
| 310 | { | ||
| 311 | Web::Authn::Exception::InvalidStructure->throw( 'truncated DER value' ); | ||
| 312 | } | ||
| 313 | |||
| 314 | my $val = substr( $buf, $off, $len ); | ||
| 315 | my $end = $off + $len; | ||
| 316 | my $node = | ||
| 317 | { | ||
| 318 | cls => $cls, | ||
| 319 | tag => $tag, | ||
| 320 | cons => $cons, | ||
| 321 | value => $val, | ||
| 322 | start => $start, | ||
| 323 | len => $end - $start | ||
| 324 | }; | ||
| 325 | if( $cons ) | ||
| 326 | { | ||
| 327 | my @kids; | ||
| 328 | my $p = 0; | ||
| 329 | while( $p < length( $val ) ) | ||
| 330 | { | ||
| 331 | my( $kid, $no ) = _asn1_read( $val, $p ); | ||
| 332 | # Rewrite start relative to original buffer | ||
| 333 | $kid->{start} = $off + $kid->{start}; | ||
| 334 | push( @kids, $kid ); | ||
| 335 | $p = $no; | ||
| 336 | } | ||
| 337 | $node->{value} = \@kids; | ||
| 338 | } | ||
| 339 | return( $node, $end ); | ||
| 340 | } | ||
| 341 | |||
| 342 | sub _asn1_time | ||
| 343 | { | ||
| 344 | my $node = shift( @_ ); | ||
| 345 | unless( $node ) | ||
| 346 | { | ||
| 347 | Web::Authn::Exception::InvalidCertificateChain->throw( 'missing certificate time field' ); | ||
| 348 | } | ||
| 349 | my $s = defined( $node->{value} ) ? $node->{value} : ''; | ||
| 350 | $s =~ s/Z\z//; | ||
| 351 | if( $node->{tag} == 23 && $s =~ /^(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ ) | ||
| 352 | { | ||
| 353 | my $year = $1 < 50 ? 2000 + $1 : 1900 + $1; # RFC 5280 UTCTime | ||
| 354 | return( _timegm( $year, $2, $3, $4, $5, $6 ) ); | ||
| 355 | } | ||
| 356 | if( $node->{tag} == 24 && $s =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/ ) | ||
| 357 | { | ||
| 358 | return( _timegm( $1, $2, $3, $4, $5, $6 ) ); | ||
| 359 | } | ||
| 360 | my $tag = defined( $node->{tag} ) ? $node->{tag} : '?'; | ||
| 361 | Web::Authn::Exception::InvalidCertificateChain->throw( | ||
| 362 | sprintf( 'unparseable certificate time (tag %s, value %s)', $tag, unpack( 'H*', $s ) ) | ||
| 363 | ); | ||
| 364 | } | ||
| 365 | |||
| 366 | sub _certs_from_blob | ||
| 367 | { | ||
| 368 | my $blob = shift( @_ ); | ||
| 369 | return unless( defined( $blob ) && length( $blob ) ); | ||
| 370 | if( $blob =~ /-----BEGIN [A-Z0-9 ]*CERTIFICATE-----/ ) | ||
| 371 | { | ||
| 372 | my @out; | ||
| 373 | while( $blob =~ /-----BEGIN [A-Z0-9 ]*CERTIFICATE-----\s*([A-Za-z0-9\/+=\r\n]+)-----END [A-Z0-9 ]*CERTIFICATE-----/g ) | ||
| 374 | { | ||
| 375 | my $b64 = $1; | ||
| 376 | $b64 =~ s/\s+//g; | ||
| 377 | push( @out, decode_base64( $b64 ) ); | ||
| 378 | } | ||
| 379 | return( @out ); | ||
| 380 | } | ||
| 381 | return( $blob ); | ||
| 382 | } | ||
| 383 | |||
| 384 | sub _check_validity | ||
| 385 | { | ||
| 386 | my $cert = shift( @_ ); | ||
| 387 | my $now = time; | ||
| 388 | return(0) if( defined( $cert->{not_before} ) && $now < $cert->{not_before} ); | ||
| 389 | return(0) if( defined( $cert->{not_after} ) && $now > $cert->{not_after} ); | ||
| 390 | return(1); | ||
| 391 | } | ||
| 392 | |||
| 393 | sub _cryptx | ||
| 394 | { | ||
| 395 | return( $_HAS_CRYPTX ) if( defined( $_HAS_CRYPTX ) ); | ||
| 396 | local $@; | ||
| 397 | $_HAS_CRYPTX = eval | ||
| 398 | { | ||
| 399 | require Crypt::PK::ECC; | ||
| 400 | require Crypt::PK::RSA; | ||
| 401 | require Crypt::PK::Ed25519; | ||
| 402 | 1; | ||
| 403 | } ? 1 : 0; | ||
| 404 | return( $_HAS_CRYPTX ); | ||
| 405 | } | ||
| 406 | |||
| 407 | sub _decode_oid | ||
| 408 | { | ||
| 409 | my $raw = shift( @_ ); | ||
| 410 | return( '' ) unless( defined( $raw ) && length( $raw ) ); | ||
| 411 | my $first = unpack( 'C', substr( $raw, 0, 1 ) ); | ||
| 412 | my @ids = ( int( $first / 40 ), $first % 40 ); | ||
| 413 | my $acc = 0; | ||
| 414 | foreach my $i ( 1 .. length( $raw ) - 1 ) | ||
| 415 | { | ||
| 416 | my $b = unpack( 'C', substr( $raw, $i, 1 ) ); | ||
| 417 | $acc = ( $acc << 7 ) | ( $b & 0x7f ); | ||
| 418 | if( !( $b & 0x80 ) ) | ||
| 419 | { | ||
| 420 | push( @ids, $acc ); | ||
| 421 | $acc = 0; | ||
| 422 | } | ||
| 423 | } | ||
| 424 | return( join( '.', @ids ) ); | ||
| 425 | } | ||
| 426 | |||
| 427 | sub _der_to_pem | ||
| 428 | { | ||
| 429 | my( $der, $type ) = @_; | ||
| 430 | my $b64 = encode_base64( $der, '' ); | ||
| 431 | $b64 =~ s/(.{64})/$1\n/g; | ||
| 432 | $b64 .= "\n" unless( $b64 =~ /\n\z/ ); | ||
| 433 | return( "-----BEGIN $type-----\n$b64-----END $type-----\n" ); | ||
| 434 | } | ||
| 435 | |||
| 436 | sub _extensions | ||
| 437 | { | ||
| 438 | my $der = shift( @_ ); | ||
| 439 | my( $cert ) = _asn1_read( $der, 0 ); | ||
| 440 | my $tbs = _first_seq( $cert->{value} ); | ||
| 441 | my %oid_to_val; | ||
| 442 | foreach my $el ( @{$tbs->{value}} ) | ||
| 443 | { | ||
| 444 | next unless( $el->{cls} == 2 && $el->{tag} == 3 ); # [3] extensions | ||
| 445 | my( $exts_seq ) = _asn1_read( $el->{value}, 0 ); | ||
| 446 | foreach my $ext ( @{$exts_seq->{value}} ) | ||
| 447 | { | ||
| 448 | my @f = @{$ext->{value}}; | ||
| 449 | my $oid = _decode_oid( $f[0]->{value} ); | ||
| 450 | my $val = $f[-1]->{value}; # extnValue OCTET STRING | ||
| 451 | $oid_to_val{ $oid } = $val; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | return( \%oid_to_val ); | ||
| 455 | } | ||
| 456 | |||
| 457 | sub _first_seq | ||
| 458 | { | ||
| 459 | my $kids = shift( @_ ); | ||
| 460 | foreach my $k ( @$kids ) | ||
| 461 | { | ||
| 462 | return( $k ) if( $k->{tag} == 16 ); | ||
| 463 | } | ||
| 464 | return( $kids->[0] ); | ||
| 465 | } | ||
| 466 | |||
| 467 | # Certificate ::= SEQUENCE { tbsCertificate, signatureAlgorithm, signatureValue } | ||
| 468 | sub _parse_certificate | ||
| 469 | { | ||
| 470 | my $der = shift( @_ ); | ||
| 471 | my( $top ) = _asn1_read( $der, 0 ); | ||
| 472 | $top->{tag} == 16 or | ||
| 473 | Web::Authn::Exception::InvalidCertificateChain->throw( 'cert is not a SEQUENCE' ); | ||
| 474 | my @el = @{ $top->{value} }; | ||
| 475 | unless( @el >= 3 ) | ||
| 476 | { | ||
| 477 | Web::Authn::Exception::InvalidCertificateChain->throw( 'truncated certificate' ); | ||
| 478 | } | ||
| 479 | |||
| 480 | my $tbs_node = $el[0]; | ||
| 481 | my $alg_node = $el[1]; | ||
| 482 | my $sig_node = $el[2]; | ||
| 483 | my $tbs_der = substr( $der, $tbs_node->{start}, $tbs_node->{len} ); | ||
| 484 | |||
| 485 | my $oid = _alg_oid($alg_node); | ||
| 486 | my $sig_alg = _sigalg_from_oid( $oid ) or | ||
| 487 | Web::Authn::Exception::InvalidCertificateChain->throw( "unsupported certificate signature OID $oid" ); | ||
| 488 | |||
| 489 | my $sig = $sig_node->{value}; | ||
| 490 | # BIT STRING: first octet is unused-bit count | ||
| 491 | if( !$sig_node->{cons} && length( $sig ) ) | ||
| 492 | { | ||
| 493 | $sig = substr( $sig, 1 ); | ||
| 494 | } | ||
| 495 | |||
| 496 | my @tbs = @{$tbs_node->{value}}; | ||
| 497 | my $ti = 0; | ||
| 498 | $ti++ if( $tbs[0]->{cls} == 2 && $tbs[0]->{tag} == 0 ); # version | ||
| 499 | $ti++; # serial | ||
| 500 | $ti++; # inner signature alg | ||
| 501 | my $issuer_node = $tbs[ $ti++ ]; | ||
| 502 | my $validity_node = $tbs[ $ti++ ]; | ||
| 503 | my $subject_node = $tbs[ $ti++ ]; | ||
| 504 | |||
| 505 | my( $nb, $na ) = _validity_times( $validity_node ); | ||
| 506 | |||
| 507 | return({ | ||
| 508 | der => $der, | ||
| 509 | tbs => $tbs_der, | ||
| 510 | sig => $sig, | ||
| 511 | sig_alg => $sig_alg, | ||
| 512 | issuer => substr( $der, $issuer_node->{start}, $issuer_node->{len} ), | ||
| 513 | subject => substr( $der, $subject_node->{start}, $subject_node->{len} ), | ||
| 514 | not_before => $nb, | ||
| 515 | not_after => $na, | ||
| 516 | }); | ||
| 517 | } | ||
| 518 | |||
| 519 | sub _same_cert | ||
| 520 | { | ||
| 521 | my( $a, $b ) = @_; | ||
| 522 | return(0) unless( $a && $b ); | ||
| 523 | return(1) if( $a->{der} eq $b->{der} ); | ||
| 524 | return(1) if( $a->{tbs} eq $b->{tbs} ); | ||
| 525 | return(0); | ||
| 526 | } | ||
| 527 | |||
| 528 | sub _sigalg_from_oid | ||
| 529 | { | ||
| 530 | my $oid = shift( @_ ); | ||
| 531 | return( ['rsa', 'SHA1'] ) if( $oid eq '1.2.840.113549.1.1.5' ); | ||
| 532 | return( ['rsa', 'SHA224'] ) if( $oid eq '1.2.840.113549.1.1.14' ); | ||
| 533 | return( ['rsa', 'SHA256'] ) if( $oid eq '1.2.840.113549.1.1.11' ); | ||
| 534 | return( ['rsa', 'SHA384'] ) if( $oid eq '1.2.840.113549.1.1.12' ); | ||
| 535 | return( ['rsa', 'SHA512'] ) if( $oid eq '1.2.840.113549.1.1.13' ); | ||
| 536 | return( ['rsa-pss', 'SHA256'] ) if( $oid eq '1.2.840.113549.1.1.10' ); | ||
| 537 | return( ['ecdsa', 'SHA1'] ) if( $oid eq '1.2.840.10045.4.1' ); | ||
| 538 | return( ['ecdsa', 'SHA224'] ) if( $oid eq '1.2.840.10045.4.3.1' ); | ||
| 539 | return( ['ecdsa', 'SHA256'] ) if( $oid eq '1.2.840.10045.4.3.2' ); | ||
| 540 | return( ['ecdsa', 'SHA384'] ) if( $oid eq '1.2.840.10045.4.3.3' ); | ||
| 541 | return( ['ecdsa', 'SHA512'] ) if( $oid eq '1.2.840.10045.4.3.4' ); | ||
| 542 | return( ['ed25519', undef] ) if( $oid eq '1.3.101.112' ); | ||
| 543 | return; | ||
| 544 | } | ||
| 545 | |||
| 546 | sub _timegm | ||
| 547 | { | ||
| 548 | my( $Y, $M, $D, $h, $m, $s ) = @_; | ||
| 549 | $Y = int( $Y ); | ||
| 550 | $M = int( $M ); | ||
| 551 | $D = int( $D ); | ||
| 552 | $h = int( $h ); | ||
| 553 | $m = int( $m ); | ||
| 554 | $s = int( $s ); | ||
| 555 | unless( $Y >= 1950 && $Y <= 9999 && | ||
| 556 | $M >= 1 && $M <= 12 && | ||
| 557 | $D >= 1 && $D <= 31 && | ||
| 558 | $h >= 0 && $h <= 23 && | ||
| 559 | $m >= 0 && $m <= 59 && | ||
| 560 | $s >= 0 && $s <= 60 ) | ||
| 561 | { | ||
| 562 | Web::Authn::Exception::InvalidCertificateChain->throw( | ||
| 563 | sprintf( 'invalid certificate time %04d-%02d-%02dT%02d:%02d:%02dZ', $Y, $M, $D, $h, $m, $s ) | ||
| 564 | ); | ||
| 565 | } | ||
| 566 | require Time::Local; | ||
| 567 | my $epoch; | ||
| 568 | local $@; | ||
| 569 | if( Time::Local->can( 'timegm_modern' ) ) | ||
| 570 | { | ||
| 571 | $epoch = eval | ||
| 572 | { | ||
| 573 | Time::Local::timegm_modern( $s, $m, $h, $D, $M - 1, $Y ); | ||
| 574 | }; | ||
| 575 | } | ||
| 576 | else | ||
| 577 | { | ||
| 578 | $epoch = eval | ||
| 579 | { | ||
| 580 | Time::Local::timegm( $s, $m, $h, $D, $M - 1, $Y ); | ||
| 581 | }; | ||
| 582 | } | ||
| 583 | if( $@ || !defined( $epoch ) ) | ||
| 584 | { | ||
| 585 | my $why = $@ ? "$@" : 'undefined epoch'; | ||
| 586 | $why =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*\z//; | ||
| 587 | Web::Authn::Exception::InvalidCertificateChain->throw( | ||
| 588 | sprintf( 'invalid certificate time %04d-%02d-%02dT%02d:%02d:%02dZ: %s', $Y, $M, $D, $h, $m, $s, $why ) | ||
| 589 | ); | ||
| 590 | } | ||
| 591 | return( $epoch ); | ||
| 592 | } | ||
| 593 | |||
| 594 | sub _validity_times | ||
| 595 | { | ||
| 596 | my $node = shift( @_ ); | ||
| 597 | return unless( $node && $node->{cons} && @{$node->{value}} >= 2 ); | ||
| 598 | return( _asn1_time( $node->{value}->[0] ), _asn1_time( $node->{value}->[1] ) ); | ||
| 599 | } | ||
| 600 | |||
| 601 | # Child must name issuer as parent.subject and carry a valid signature by parent. | ||
| 602 | sub _verify_issued_by | ||
| 603 | { | ||
| 604 | my( $child, $parent ) = @_; | ||
| 605 | return(0) unless( $child && $parent ); | ||
| 606 | return(0) unless( $child->{issuer} eq $parent->{subject} ); | ||
| 607 | _check_validity( $child ) or return(0); | ||
| 608 | local $@; | ||
| 609 | my $pk = eval{ public_key_from_cert_der( $parent->{der} ) } or return(0); | ||
| 610 | my( $alg, $hash ) = @{$child->{sig_alg} || []}; | ||
| 611 | return(0) unless( $alg ); | ||
| 612 | my $rv = eval | ||
| 613 | { | ||
| 614 | if( $alg eq 'ed25519' ) | ||
| 615 | { | ||
| 616 | $pk->verify_message( $child->{sig}, $child->{tbs} ); | ||
| 617 | } | ||
| 618 | elsif( $alg eq 'rsa-pss' ) | ||
| 619 | { | ||
| 620 | $pk->verify_message( $child->{sig}, $child->{tbs}, $hash, 'pss' ); | ||
| 621 | } | ||
| 622 | else | ||
| 623 | { | ||
| 624 | $pk->verify_message( $child->{sig}, $child->{tbs}, $hash ); | ||
| 625 | } | ||
| 626 | }; | ||
| 627 | return( $rv ? 1 : 0 ); | ||
| 628 | } | ||
| 629 | |||
| 630 | 1; | ||
| 631 | # NOTE: POD | ||
| 632 | __END__ |