Debian Codegraph @

addons/auth_passkey/_vendor/webauthn/helpers/parse_authenticator_data.py

raw
1 from typing import Union
2
3 from .byteslike_to_bytes import byteslike_to_bytes
4 from .exceptions import InvalidAuthenticatorDataStructure
5 from .structs import AttestedCredentialData, AuthenticatorData, AuthenticatorDataFlags
6 from .parse_cbor import parse_cbor
7 from .encode_cbor import encode_cbor
8
9
10 def parse_authenticator_data(val: bytes) -> AuthenticatorData:
11 """
12 Turn `response.attestationObject.authData` into structured data
13 """
14 val = byteslike_to_bytes(val)
15
16 # Don't bother parsing if there aren't enough bytes for at least:
17 # - rpIdHash (32 bytes)
18 # - flags (1 byte)
19 # - signCount (4 bytes)
20 if len(val) < 37:
21 raise InvalidAuthenticatorDataStructure(
22 f"Authenticator data was {len(val)} bytes, expected at least 37 bytes"
23 )
24
25 pointer = 0
26
27 rp_id_hash = val[pointer:32]
28 pointer += 32
29
30 # Cast byte to ordinal so we can use bitwise operators on it
31 flags_bytes = ord(val[pointer : pointer + 1])
32 pointer += 1
33
34 sign_count = val[pointer : pointer + 4]
35 pointer += 4
36
37 # Parse flags
38 flags = AuthenticatorDataFlags(
39 up=flags_bytes & (1 << 0) != 0,
40 uv=flags_bytes & (1 << 2) != 0,
41 be=flags_bytes & (1 << 3) != 0,
42 bs=flags_bytes & (1 << 4) != 0,
43 at=flags_bytes & (1 << 6) != 0,
44 ed=flags_bytes & (1 << 7) != 0,
45 )
46
47 # The value to return
48 authenticator_data = AuthenticatorData(
49 rp_id_hash=rp_id_hash,
50 flags=flags,
51 sign_count=int.from_bytes(sign_count, "big"),
52 )
53
54 # Parse AttestedCredentialData if present
55 if flags.at is True:
56 aaguid = val[pointer : pointer + 16]
57 pointer += 16
58
59 credential_id_len = int.from_bytes(val[pointer : pointer + 2], "big")
60 pointer += 2
61
62 credential_id = val[pointer : pointer + credential_id_len]
63 pointer += credential_id_len
64
65 """
66 Some authenticators incorrectly compose authData when using EdDSA for their public keys.
67 A CBOR "Map of 3 items" (0xA3) should be "Map of 4 items" (0xA4), and if we manually adjust
68 the single byte there's a good chance the authData can be correctly parsed. Let's try to
69 detect when this happens and gracefully handle it.
70 """
71 # Decodes to `{1: "OKP", 3: -8, -1: "Ed25519"}` (it's missing key -2 a.k.a. COSEKey.X)
72 bad_eddsa_cbor = bytearray.fromhex("a301634f4b500327206745643235353139")
73 # If we find the bytes here then let's fix the bad data
74 if val[pointer : pointer + len(bad_eddsa_cbor)] == bad_eddsa_cbor:
75 # Make a mutable copy of the bytes...
76 _val = bytearray(val)
77 # ...Fix the bad byte...
78 _val[pointer] = 0xA4
79 # ...Then replace `val` with the fixed bytes
80 val = bytes(_val)
81
82 # Load the next CBOR-encoded value
83 credential_public_key = parse_cbor(val[pointer:])
84 credential_public_key_bytes = encode_cbor(credential_public_key)
85 pointer += len(credential_public_key_bytes)
86
87 attested_cred_data = AttestedCredentialData(
88 aaguid=aaguid,
89 credential_id=credential_id,
90 credential_public_key=credential_public_key_bytes,
91 )
92 authenticator_data.attested_credential_data = attested_cred_data
93
94 if flags.ed is True:
95 extension_object = parse_cbor(val[pointer:])
96 extension_bytes = encode_cbor(extension_object)
97 pointer += len(extension_bytes)
98 authenticator_data.extensions = extension_bytes
99
100 # We should have parsed all authenticator data by this point
101 if len(val) > pointer:
102 raise InvalidAuthenticatorDataStructure(
103 "Leftover bytes detected while parsing authenticator data"
104 )
105
106 return authenticator_data