Debian Codegraph @

addons/auth_passkey/_vendor/webauthn/registration/formats/fido_u2f.py

raw symbols SCIP
1 import hashlib
2 from typing import List
3
4 from cryptography import x509
5 from cryptography.exceptions import InvalidSignature
6 from cryptography.hazmat.backends import default_backend
7 from cryptography.hazmat.primitives.asymmetric.ec import (
8 SECP256R1,
9 EllipticCurvePublicKey,
10 )
11
12 from ....webauthn.helpers import (
13 aaguid_to_string,
14 validate_certificate_chain,
15 verify_signature,
16 )
17 from ....webauthn.helpers.cose import COSEAlgorithmIdentifier
18 from ....webauthn.helpers.decode_credential_public_key import (
19 DecodedEC2PublicKey,
20 decode_credential_public_key,
21 )
22 from ....webauthn.helpers.exceptions import (
23 InvalidCertificateChain,
24 InvalidRegistrationResponse,
25 )
26 from ....webauthn.helpers.structs import AttestationStatement
27
28
29 def verify_fido_u2f(
30 *,
31 attestation_statement: AttestationStatement,
32 client_data_json: bytes,
33 rp_id_hash: bytes,
34 credential_id: bytes,
35 credential_public_key: bytes,
36 aaguid: bytes,
37 pem_root_certs_bytes: List[bytes],
38 ) -> bool:
39 """Verify a "fido-u2f" attestation statement
40
41 See https://www.w3.org/TR/webauthn-2/#sctn-fido-u2f-attestation
42 """
43 if not attestation_statement.sig:
44 raise InvalidRegistrationResponse("Attestation statement was missing signature (FIDO-U2F)")
45
46 if not attestation_statement.x5c:
47 raise InvalidRegistrationResponse(
48 "Attestation statement was missing certificate (FIDO-U2F)"
49 )
50
51 if len(attestation_statement.x5c) > 1:
52 raise InvalidRegistrationResponse(
53 "Attestation statement contained too many certificates (FIDO-U2F)"
54 )
55
56 # Validate the certificate chain
57 try:
58 validate_certificate_chain(
59 x5c=attestation_statement.x5c,
60 pem_root_certs_bytes=pem_root_certs_bytes,
61 )
62 except InvalidCertificateChain as err:
63 raise InvalidRegistrationResponse(f"{err} (FIDO-U2F)")
64
65 # FIDO spec requires AAGUID in U2F attestations to be all zeroes
66 # See https://fidoalliance.org/specs/fido-v2.1-rd-20191217/fido-client-to-authenticator-protocol-v2.1-rd-20191217.html#u2f-authenticatorMakeCredential-interoperability
67 actual_aaguid = aaguid_to_string(aaguid)
68 expected_aaguid = "00000000-0000-0000-0000-000000000000"
69 if actual_aaguid != expected_aaguid:
70 raise InvalidRegistrationResponse(
71 f"AAGUID {actual_aaguid} was not expected {expected_aaguid} (FIDO-U2F)"
72 )
73
74 # Get the public key from the leaf certificate
75 leaf_cert_bytes = attestation_statement.x5c[0]
76 leaf_cert = x509.load_der_x509_certificate(leaf_cert_bytes, default_backend())
77 leaf_cert_pub_key = leaf_cert.public_key()
78
79 # We need the cert's x and y points so make sure they exist
80 if not isinstance(leaf_cert_pub_key, EllipticCurvePublicKey):
81 raise InvalidRegistrationResponse("Leaf cert was not an EC2 certificate (FIDO-U2F)")
82
83 if not isinstance(leaf_cert_pub_key.curve, SECP256R1):
84 raise InvalidRegistrationResponse("Leaf cert did not use P-256 curve (FIDO-U2F)")
85
86 decoded_public_key = decode_credential_public_key(credential_public_key)
87 if not isinstance(decoded_public_key, DecodedEC2PublicKey):
88 raise InvalidRegistrationResponse("Credential public key was not EC2 (FIDO-U2F)")
89
90 # Convert the public key to "Raw ANSI X9.62 public key format"
91 public_key_u2f = b"".join(
92 [
93 bytes([0x04]),
94 decoded_public_key.x,
95 decoded_public_key.y,
96 ]
97 )
98
99 # Generate a hash of client_data_json
100 client_data_hash = hashlib.sha256()
101 client_data_hash.update(client_data_json)
102 client_data_hash_bytes = client_data_hash.digest()
103
104 # Prepare the signature base (called "verificationData" in the WebAuthn spec)
105 verification_data = b"".join(
106 [
107 bytes([0x00]),
108 rp_id_hash,
109 client_data_hash_bytes,
110 credential_id,
111 public_key_u2f,
112 ]
113 )
114
115 try:
116 verify_signature(
117 public_key=leaf_cert_pub_key,
118 signature_alg=COSEAlgorithmIdentifier.ECDSA_SHA_256,
119 signature=attestation_statement.sig,
120 data=verification_data,
121 )
122 except InvalidSignature:
123 raise InvalidRegistrationResponse(
124 "Could not verify attestation statement signature (FIDO-U2F)"
125 )
126
127 # If we make it to here we're all good
128 return True