28
28
Encoding ,
29
29
PublicFormat ,
30
30
)
31
+
32
+ KEYTYPES_AND_SCHEMES = {
33
+ KeyCurveName .p_256 : ("ecdsa" , "ecdsa-sha2-nistp256" ),
34
+ KeyCurveName .p_384 : ("ecdsa" , "ecdsa-sha2-nistp384" ),
35
+ KeyCurveName .p_521 : ("ecdsa" , "ecdsa-sha2-nistp521" ),
36
+ }
37
+
38
+ SIGNATURE_ALGORITHMS = {
39
+ "ecdsa-sha2-nistp256" : SignatureAlgorithm .es256 ,
40
+ "ecdsa-sha2-nistp384" : SignatureAlgorithm .es384 ,
41
+ "ecdsa-sha2-nistp521" : SignatureAlgorithm .es512 ,
42
+ }
43
+
44
+
31
45
except ImportError :
32
46
AZURE_IMPORT_ERROR = (
33
47
"Signing with Azure Key Vault requires azure-identity, "
@@ -66,27 +80,28 @@ class AzureSigner(Signer):
66
80
67
81
SCHEME = "azurekms"
68
82
69
- def __init__ (self , az_key_uri : str , public_key : Key ):
83
+ def __init__ (self , az_key_uri : str , public_key : SSlibKey ):
70
84
if AZURE_IMPORT_ERROR :
71
85
raise UnsupportedLibraryError (AZURE_IMPORT_ERROR )
72
86
73
- try :
74
- cred = DefaultAzureCredential ()
75
- self .crypto_client = CryptographyClient (
76
- az_key_uri ,
77
- credential = cred ,
78
- )
79
- self .signature_algorithm = self ._get_signature_algorithm (
80
- public_key ,
87
+ if (public_key .keytype , public_key .scheme ) not in KEYTYPES_AND_SCHEMES .values ():
88
+ logger .info ("only EC keys are supported for now" )
89
+ raise UnsupportedKeyType (
90
+ "Supplied key must be an EC key on curve "
91
+ "nistp256, nistp384, or nistp521"
81
92
)
82
- self .hash_algorithm = self ._get_hash_algorithm (public_key )
83
- except UnsupportedKeyType as e :
84
- logger .info ("Key %s has unsupported key type or unsupported elliptic curve" )
85
- raise e
93
+
94
+ cred = DefaultAzureCredential ()
95
+ self .crypto_client = CryptographyClient (
96
+ az_key_uri ,
97
+ credential = cred ,
98
+ )
99
+ self .signature_algorithm = SIGNATURE_ALGORITHMS [public_key .scheme ]
100
+ self .hash_algorithm = public_key .get_hash_algorithm_name ()
86
101
self ._public_key = public_key
87
102
88
103
@property
89
- def public_key (self ) -> Key :
104
+ def public_key (self ) -> SSlibKey :
90
105
return self ._public_key
91
106
92
107
@staticmethod
@@ -128,53 +143,12 @@ def _create_crypto_client(
128
143
)
129
144
raise e
130
145
131
- @staticmethod
132
- def _get_signature_algorithm (public_key : Key ) -> SignatureAlgorithm :
133
- """Return SignatureAlgorithm after parsing the public key"""
134
- if public_key .keytype != "ecdsa" :
135
- logger .info ("only EC keys are supported for now" )
136
- raise UnsupportedKeyType ("Supplied key must be an EC key" )
137
- # Format is "ecdsa-sha2-nistp256"
138
- comps = public_key .scheme .split ("-" )
139
- if len (comps ) != 3 : # noqa: PLR2004
140
- raise UnsupportedKeyType ("Invalid scheme found" )
141
-
142
- if comps [2 ] == "nistp256" :
143
- return SignatureAlgorithm .es256
144
- if comps [2 ] == "nistp384" :
145
- return SignatureAlgorithm .es384
146
- if comps [2 ] == "nistp521" :
147
- return SignatureAlgorithm .es512
148
-
149
- raise UnsupportedKeyType ("Unsupported curve supplied by key" )
150
-
151
- @staticmethod
152
- def _get_hash_algorithm (public_key : Key ) -> str :
153
- """Return the hash algorithm used by the public key"""
154
- # Format is "ecdsa-sha2-nistp256"
155
- comps = public_key .scheme .split ("-" )
156
- if len (comps ) != 3 : # noqa: PLR2004
157
- raise UnsupportedKeyType ("Invalid scheme found" )
158
-
159
- if comps [2 ] == "nistp256" :
160
- return "sha256"
161
- if comps [2 ] == "nistp384" :
162
- return "sha384"
163
- if comps [2 ] == "nistp521" :
164
- return "sha512"
165
-
166
- raise UnsupportedKeyType ("Unsupported curve supplied by key" )
167
-
168
146
@staticmethod
169
147
def _get_keytype_and_scheme (crv : str ) -> tuple [str , str ]:
170
- if crv == KeyCurveName .p_256 :
171
- return "ecdsa" , "ecdsa-sha2-nistp256"
172
- if crv == KeyCurveName .p_384 :
173
- return "ecdsa" , "ecdsa-sha2-nistp384"
174
- if crv == KeyCurveName .p_521 :
175
- return "ecdsa" , "ecdsa-sha2-nistp521"
176
-
177
- raise UnsupportedKeyType ("Unsupported curve supplied by key" )
148
+ try :
149
+ return KEYTYPES_AND_SCHEMES [crv ]
150
+ except KeyError :
151
+ raise UnsupportedKeyType ("Unsupported curve supplied by key" )
178
152
179
153
@classmethod
180
154
def from_priv_key_uri (
@@ -183,6 +157,9 @@ def from_priv_key_uri(
183
157
public_key : Key ,
184
158
secrets_handler : SecretsHandler | None = None ,
185
159
) -> AzureSigner :
160
+ if not isinstance (public_key , SSlibKey ):
161
+ raise ValueError (f"Expected SSlibKey for { priv_key_uri } " )
162
+
186
163
uri = parse .urlparse (priv_key_uri )
187
164
188
165
if uri .scheme != cls .SCHEME :
@@ -192,7 +169,7 @@ def from_priv_key_uri(
192
169
return cls (az_key_uri , public_key )
193
170
194
171
@classmethod
195
- def import_ (cls , az_vault_name : str , az_key_name : str ) -> tuple [str , Key ]:
172
+ def import_ (cls , az_vault_name : str , az_key_name : str ) -> tuple [str , SSlibKey ]:
196
173
"""Load key and signer details from KMS
197
174
198
175
Returns the private key uri and the public key. This method should only
0 commit comments