Debian Codegraph @

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

raw symbols SCIP
1 import hashlib
2 from typing import List
3
4 import cbor2
5 from cryptography import x509
6 from cryptography.hazmat.backends import default_backend
7 from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat
8 from cryptography.x509 import (
9 Extension,
10 ExtensionNotFound,
11 ObjectIdentifier,
12 UnrecognizedExtension,
13 )
14
15 from ....webauthn.helpers import (
16 decode_credential_public_key,
17 decoded_public_key_to_cryptography,
18 parse_cbor,
19 validate_certificate_chain,
20 )
21 from ....webauthn.helpers.exceptions import (
22 InvalidCertificateChain,
23 InvalidRegistrationResponse,
24 )
25 from ....webauthn.helpers.known_root_certs import apple_webauthn_root_ca
26 from ....webauthn.helpers.structs import AttestationStatement
27
28
29 def verify_apple(
30 *,
31 attestation_statement: AttestationStatement,
32 attestation_object: bytes,
33 client_data_json: bytes,
34 credential_public_key: bytes,
35 pem_root_certs_bytes: List[bytes],
36 ) -> bool:
37 """
38 https://www.w3.org/TR/webauthn-2/#sctn-apple-anonymous-attestation
39 """
40
41 if not attestation_statement.x5c:
42 raise InvalidRegistrationResponse("Attestation statement was missing x5c (Apple)")
43
44 # Validate the certificate chain
45 try:
46 # Include known root certificates for this attestation format
47 pem_root_certs_bytes.append(apple_webauthn_root_ca)
48
49 validate_certificate_chain(
50 x5c=attestation_statement.x5c,
51 pem_root_certs_bytes=pem_root_certs_bytes,
52 )
53 except InvalidCertificateChain as err:
54 raise InvalidRegistrationResponse(f"{err} (Apple)")
55
56 # Concatenate authenticatorData and clientDataHash to form nonceToHash.
57 attestation_dict = parse_cbor(attestation_object)
58 authenticator_data_bytes = attestation_dict["authData"]
59
60 client_data_hash = hashlib.sha256()
61 client_data_hash.update(client_data_json)
62 client_data_hash_bytes = client_data_hash.digest()
63
64 nonce_to_hash = b"".join(
65 [
66 authenticator_data_bytes,
67 client_data_hash_bytes,
68 ]
69 )
70
71 # Perform SHA-256 hash of nonceToHash to produce nonce.
72 nonce = hashlib.sha256()
73 nonce.update(nonce_to_hash)
74 nonce_bytes = nonce.digest()
75
76 # Verify that nonce equals the value of the extension with
77 # OID 1.2.840.113635.100.8.2 in credCert.
78 attestation_cert_bytes = attestation_statement.x5c[0]
79 attestation_cert = x509.load_der_x509_certificate(attestation_cert_bytes, default_backend())
80 cert_extensions = attestation_cert.extensions
81
82 # Still no documented name for this OID...
83 ext_1_2_840_113635_100_8_2_oid = "1.2.840.113635.100.8.2"
84 try:
85 ext_1_2_840_113635_100_8_2: Extension = cert_extensions.get_extension_for_oid(
86 ObjectIdentifier(ext_1_2_840_113635_100_8_2_oid)
87 )
88 except ExtensionNotFound:
89 raise InvalidRegistrationResponse(
90 f"Certificate missing extension {ext_1_2_840_113635_100_8_2_oid} (Apple)"
91 )
92
93 # Peel apart the Extension into an UnrecognizedExtension, then the bytes we actually
94 # want
95 ext_value_wrapper: UnrecognizedExtension = ext_1_2_840_113635_100_8_2.value
96 # Ignore the first six ASN.1 structure bytes that define the nonce as an
97 # OCTET STRING. Should trim off '0$\xa1"\x04'
98 ext_value: bytes = ext_value_wrapper.value[6:]
99
100 if ext_value != nonce_bytes:
101 raise InvalidRegistrationResponse("Certificate nonce was not expected value (Apple)")
102
103 # Verify that the credential public key equals the Subject Public Key of credCert.
104 attestation_cert_pub_key = attestation_cert.public_key()
105 attestation_cert_pub_key_bytes = attestation_cert_pub_key.public_bytes(
106 Encoding.DER,
107 PublicFormat.SubjectPublicKeyInfo,
108 )
109 # Convert our raw public key bytes into the same format cryptography generates for
110 # the cert subject key
111 decoded_pub_key = decode_credential_public_key(credential_public_key)
112 pub_key_crypto = decoded_public_key_to_cryptography(decoded_pub_key)
113 pub_key_crypto_bytes = pub_key_crypto.public_bytes(
114 Encoding.DER,
115 PublicFormat.SubjectPublicKeyInfo,
116 )
117
118 if attestation_cert_pub_key_bytes != pub_key_crypto_bytes:
119 raise InvalidRegistrationResponse(
120 "Certificate public key did not match credential public key (Apple)"
121 )
122
123 return True