| 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 |
|
| 67 |
try: |
| 68 |
|
| 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 |
|
| 80 |
attestation_dict = parse_cbor(attestation_object) |
| 81 |
authenticator_data_bytes = attestation_dict["authData"] |
| 82 |
|
| 83 |
|
| 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 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 115 |
|
| 116 |
attestation_cert_pub_key_bytes = attestation_cert_pub_key.public_bytes( |
| 117 |
Encoding.DER, |
| 118 |
PublicFormat.SubjectPublicKeyInfo, |
| 119 |
) |
| 120 |
|
| 121 |
|
| 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 |
|
| 135 |
|
| 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 |
|
| 148 |
|
| 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 |
|
| 154 |
|
| 155 |
software_enforced: AuthorizationList = parsed_ext["softwareEnforced"] |
| 156 |
tee_enforced: AuthorizationList = parsed_ext["teeEnforced"] |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 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 |
|
| 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 |