Debian Codegraph @

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

raw
1 from typing import Union
2 from dataclasses import dataclass
3
4 import cbor2
5
6 from .cose import COSECRV, COSEKTY, COSEAlgorithmIdentifier, COSEKey
7 from .exceptions import InvalidPublicKeyStructure, UnsupportedPublicKeyType
8 from .parse_cbor import parse_cbor
9
10
11 @dataclass
12 class DecodedOKPPublicKey:
13 kty: COSEKTY
14 alg: COSEAlgorithmIdentifier
15 crv: COSECRV
16 x: bytes
17
18
19 @dataclass
20 class DecodedEC2PublicKey:
21 kty: COSEKTY
22 alg: COSEAlgorithmIdentifier
23 crv: COSECRV
24 x: bytes
25 y: bytes
26
27
28 @dataclass
29 class DecodedRSAPublicKey:
30 kty: COSEKTY
31 alg: COSEAlgorithmIdentifier
32 n: bytes
33 e: bytes
34
35
36 def decode_credential_public_key(
37 key: bytes,
38 ) -> Union[DecodedOKPPublicKey, DecodedEC2PublicKey, DecodedRSAPublicKey]:
39 """
40 Decode a CBOR-encoded public key and turn it into a data structure.
41
42 Supports OKP, EC2, and RSA public keys
43 """
44 # Occassionally we might be given a public key in an "uncompressed" format,
45 # typically from older U2F security keys. As per the FIDO spec this is indicated by
46 # a leading 0x04 "uncompressed point compression method" format byte. In that case
47 # we need to fill in some blanks to turn it into a full EC2 key for signature
48 # verification
49 #
50 # See https://fidoalliance.org/specs/fido-v2.0-id-20180227/fido-registry-v2.0-id-20180227.html#public-key-representation-formats
51 if key[0] == 0x04:
52 return DecodedEC2PublicKey(
53 kty=COSEKTY.EC2,
54 alg=COSEAlgorithmIdentifier.ECDSA_SHA_256,
55 crv=COSECRV.P256,
56 x=key[1:33],
57 y=key[33:65],
58 )
59
60 decoded_key: dict = parse_cbor(key)
61
62 kty = decoded_key[COSEKey.KTY]
63 alg = decoded_key[COSEKey.ALG]
64
65 if not kty:
66 raise InvalidPublicKeyStructure("Credential public key missing kty")
67 if not alg:
68 raise InvalidPublicKeyStructure("Credential public key missing alg")
69
70 if kty == COSEKTY.OKP:
71 crv = decoded_key[COSEKey.CRV]
72 x = decoded_key[COSEKey.X]
73
74 if not crv:
75 raise InvalidPublicKeyStructure("OKP credential public key missing crv")
76 if not x:
77 raise InvalidPublicKeyStructure("OKP credential public key missing x")
78
79 return DecodedOKPPublicKey(
80 kty=kty,
81 alg=alg,
82 crv=crv,
83 x=x,
84 )
85 elif kty == COSEKTY.EC2:
86 crv = decoded_key[COSEKey.CRV]
87 x = decoded_key[COSEKey.X]
88 y = decoded_key[COSEKey.Y]
89
90 if not crv:
91 raise InvalidPublicKeyStructure("EC2 credential public key missing crv")
92 if not x:
93 raise InvalidPublicKeyStructure("EC2 credential public key missing x")
94 if not y:
95 raise InvalidPublicKeyStructure("EC2 credential public key missing y")
96
97 return DecodedEC2PublicKey(
98 kty=kty,
99 alg=alg,
100 crv=crv,
101 x=x,
102 y=y,
103 )
104 elif kty == COSEKTY.RSA:
105 n = decoded_key[COSEKey.N]
106 e = decoded_key[COSEKey.E]
107
108 if not n:
109 raise InvalidPublicKeyStructure("RSA credential public key missing n")
110 if not e:
111 raise InvalidPublicKeyStructure("RSA credential public key missing e")
112
113 return DecodedRSAPublicKey(
114 kty=kty,
115 alg=alg,
116 n=n,
117 e=e,
118 )
119
120 raise UnsupportedPublicKeyType(f'Unsupported credential public key type "{kty}"')