Debian Codegraph @

addons/auth_passkey/_vendor/webauthn/registration/formats/android_key.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.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 verify_signature,
21 )
22 from ....webauthn.helpers.asn1.android_key import (
23 AuthorizationList,
24 KeyDescription,
25 KeyOrigin,
26 KeyPurpose,
27 )
28 from ....webauthn.helpers.exceptions import (
29 InvalidCertificateChain,
30 InvalidRegistrationResponse,
31 )
32 from ....webauthn.helpers.known_root_certs import (
33 google_hardware_attestation_root_1,
34 google_hardware_attestation_root_2,
35 )
36 from ....webauthn.helpers.structs import AttestationStatement
37
38
39 def verify_android_key(
40 *,
41 attestation_statement: AttestationStatement,
42 attestation_object: bytes,
43 client_data_json: bytes,
44 credential_public_key: bytes,
45 pem_root_certs_bytes: List[bytes],
46 ) -> bool:
47 """Verify an "android-key" attestation statement
48
49 See https://www.w3.org/TR/webauthn-2/#sctn-android-key-attestation
50
51 Also referenced: https://source.android.com/security/keystore/attestation
52 """
53 if not attestation_statement.sig:
54 raise InvalidRegistrationResponse(
55 "Attestation statement was missing signature (Android Key)"
56 )
57
58 if not attestation_statement.alg:
59 raise InvalidRegistrationResponse(
60 "Attestation statement was missing algorithm (Android Key)"
61 )
62
63 if not attestation_statement.x5c:
64 raise InvalidRegistrationResponse("Attestation statement was missing x5c (Android Key)")
65
66 # Validate certificate chain
67 try:
68 # Include known root certificates for this attestation format
69 pem_root_certs_bytes.append(google_hardware_attestation_root_1)
70 pem_root_certs_bytes.append(google_hardware_attestation_root_2)
71
72 validate_certificate_chain(
73 x5c=attestation_statement.x5c,
74 pem_root_certs_bytes=pem_root_certs_bytes,
75 )
76 except InvalidCertificateChain as err:
77 raise InvalidRegistrationResponse(f"{err} (Android Key)")
78
79 # Extract attStmt bytes from attestation_object
80 attestation_dict = parse_cbor(attestation_object)
81 authenticator_data_bytes = attestation_dict["authData"]
82
83 # Generate a hash of client_data_json
84 client_data_hash = hashlib.sha256()
85 client_data_hash.update(client_data_json)
86 client_data_hash_bytes = client_data_hash.digest()
87
88 verification_data = b"".join(
89 [
90 authenticator_data_bytes,
91 client_data_hash_bytes,
92 ]
93 )
94
95 # Verify that sig is a valid signature over the concatenation of authenticatorData
96 # and clientDataHash using the public key in the first certificate in x5c with the
97 # algorithm specified in alg.
98 attestation_cert_bytes = attestation_statement.x5c[0]
99 attestation_cert = x509.load_der_x509_certificate(attestation_cert_bytes, default_backend())
100 attestation_cert_pub_key = attestation_cert.public_key()
101
102 try:
103 verify_signature(
104 public_key=attestation_cert_pub_key,
105 signature_alg=attestation_statement.alg,
106 signature=attestation_statement.sig,
107 data=verification_data,
108 )
109 except InvalidSignature:
110 raise InvalidRegistrationResponse(
111 "Could not verify attestation statement signature (Android Key)"
112 )
113
114 # Verify that the public key in the first certificate in x5c matches the
115 # credentialPublicKey in the attestedCredentialData in authenticatorData.
116 attestation_cert_pub_key_bytes = attestation_cert_pub_key.public_bytes(
117 Encoding.DER,
118 PublicFormat.SubjectPublicKeyInfo,
119 )
120 # Convert our raw public key bytes into the same format cryptography generates for
121 # the cert subject key
122 decoded_pub_key = decode_credential_public_key(credential_public_key)
123 pub_key_crypto = decoded_public_key_to_cryptography(decoded_pub_key)
124 pub_key_crypto_bytes = pub_key_crypto.public_bytes(
125 Encoding.DER,
126 PublicFormat.SubjectPublicKeyInfo,
127 )
128
129 if attestation_cert_pub_key_bytes != pub_key_crypto_bytes:
130 raise InvalidRegistrationResponse(
131 "Certificate public key did not match credential public key (Android Key)"
132 )
133
134 # Verify that the attestationChallenge field in the attestation certificate
135 # extension data is identical to clientDataHash.
136 ext_key_description_oid = "1.3.6.1.4.1.11129.2.1.17"
137 try:
138 cert_extensions = attestation_cert.extensions
139 ext_key_description: Extension = cert_extensions.get_extension_for_oid(
140 ObjectIdentifier(ext_key_description_oid)
141 )
142 except ExtensionNotFound:
143 raise InvalidRegistrationResponse(
144 f"Certificate missing extension {ext_key_description_oid} (Android Key)"
145 )
146
147 # Peel apart the Extension into an UnrecognizedExtension, then the bytes we actually
148 # want
149 ext_value_wrapper: UnrecognizedExtension = ext_key_description.value
150 ext_value: bytes = ext_value_wrapper.value
151 parsed_ext = KeyDescription.load(ext_value)
152
153 # Verify the following using the appropriate authorization list from the attestation
154 # certificate extension data:
155 software_enforced: AuthorizationList = parsed_ext["softwareEnforced"]
156 tee_enforced: AuthorizationList = parsed_ext["teeEnforced"]
157
158 # The AuthorizationList.allApplications field is not present on either authorization
159 # list (softwareEnforced nor teeEnforced), since PublicKeyCredential MUST be scoped
160 # to the RP ID.
161 if software_enforced["allApplications"].native is not None:
162 raise InvalidRegistrationResponse(
163 "allApplications field was present in softwareEnforced (Android Key)"
164 )
165
166 if tee_enforced["allApplications"].native is not None:
167 raise InvalidRegistrationResponse(
168 "allApplications field was present in teeEnforced (Android Key)"
169 )
170
171 # The value in the AuthorizationList.origin field is equal to KM_ORIGIN_GENERATED.
172 origin = tee_enforced["origin"].native
173 if origin != KeyOrigin.GENERATED:
174 raise InvalidRegistrationResponse(
175 f"teeEnforced.origin {origin} was not {KeyOrigin.GENERATED}"
176 )
177
178 # The value in the AuthorizationList.purpose field is equal to KM_PURPOSE_SIGN.
179 purpose = tee_enforced["purpose"].native
180 if purpose != [KeyPurpose.SIGN]:
181 raise InvalidRegistrationResponse(
182 f"teeEnforced.purpose {purpose} was not [{KeyPurpose.SIGN}]"
183 )
184
185 return True