Debian Codegraph @

addons/auth_passkey/_vendor/webauthn/registration/formats/packed.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
8 from ....webauthn.helpers import (
9 decode_credential_public_key,
10 decoded_public_key_to_cryptography,
11 parse_cbor,
12 validate_certificate_chain,
13 verify_signature,
14 )
15 from ....webauthn.helpers.exceptions import (
16 InvalidCertificateChain,
17 InvalidRegistrationResponse,
18 )
19 from ....webauthn.helpers.structs import AttestationStatement
20
21
22 def verify_packed(
23 *,
24 attestation_statement: AttestationStatement,
25 attestation_object: bytes,
26 client_data_json: bytes,
27 credential_public_key: bytes,
28 pem_root_certs_bytes: List[bytes],
29 ) -> bool:
30 """Verify a "packed" attestation statement
31
32 See https://www.w3.org/TR/webauthn-2/#sctn-packed-attestation
33 """
34 if not attestation_statement.sig:
35 raise InvalidRegistrationResponse("Attestation statement was missing signature (Packed)")
36
37 if not attestation_statement.alg:
38 raise InvalidRegistrationResponse("Attestation statement was missing algorithm (Packed)")
39
40 # Extract attStmt bytes from attestation_object
41 attestation_dict = parse_cbor(attestation_object)
42 authenticator_data_bytes = attestation_dict["authData"]
43
44 # Generate a hash of client_data_json
45 client_data_hash = hashlib.sha256()
46 client_data_hash.update(client_data_json)
47 client_data_hash_bytes = client_data_hash.digest()
48
49 verification_data = b"".join(
50 [
51 authenticator_data_bytes,
52 client_data_hash_bytes,
53 ]
54 )
55
56 if attestation_statement.x5c:
57 # Validate the certificate chain
58 try:
59 validate_certificate_chain(
60 x5c=attestation_statement.x5c,
61 pem_root_certs_bytes=pem_root_certs_bytes,
62 )
63 except InvalidCertificateChain as err:
64 raise InvalidRegistrationResponse(f"{err} (Packed)")
65
66 attestation_cert_bytes = attestation_statement.x5c[0]
67 attestation_cert = x509.load_der_x509_certificate(
68 attestation_cert_bytes, default_backend()
69 )
70 attestation_cert_pub_key = attestation_cert.public_key()
71
72 try:
73 verify_signature(
74 public_key=attestation_cert_pub_key,
75 signature_alg=attestation_statement.alg,
76 signature=attestation_statement.sig,
77 data=verification_data,
78 )
79 except InvalidSignature:
80 raise InvalidRegistrationResponse(
81 "Could not verify attestation statement signature (Packed)"
82 )
83 else:
84 # Self Attestation
85 decoded_pub_key = decode_credential_public_key(credential_public_key)
86
87 if decoded_pub_key.alg != attestation_statement.alg:
88 raise InvalidRegistrationResponse(
89 f"Credential public key alg {decoded_pub_key.alg} did not equal attestation statement alg {attestation_statement.alg}"
90 )
91
92 public_key = decoded_public_key_to_cryptography(decoded_pub_key)
93
94 try:
95 verify_signature(
96 public_key=public_key,
97 signature_alg=attestation_statement.alg,
98 signature=attestation_statement.sig,
99 data=verification_data,
100 )
101 except InvalidSignature:
102 raise InvalidRegistrationResponse(
103 "Could not verify attestation statement signature (Packed|Self)"
104 )
105
106 return True