2828 Encoding ,
2929 PublicFormat ,
3030 )
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+
3145except ImportError :
3246 AZURE_IMPORT_ERROR = (
3347 "Signing with Azure Key Vault requires azure-identity, "
@@ -66,27 +80,28 @@ class AzureSigner(Signer):
6680
6781 SCHEME = "azurekms"
6882
69- def __init__ (self , az_key_uri : str , public_key : Key ):
83+ def __init__ (self , az_key_uri : str , public_key : SSlibKey ):
7084 if AZURE_IMPORT_ERROR :
7185 raise UnsupportedLibraryError (AZURE_IMPORT_ERROR )
7286
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"
8192 )
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 ()
86101 self ._public_key = public_key
87102
88103 @property
89- def public_key (self ) -> Key :
104+ def public_key (self ) -> SSlibKey :
90105 return self ._public_key
91106
92107 @staticmethod
@@ -128,53 +143,12 @@ def _create_crypto_client(
128143 )
129144 raise e
130145
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-
168146 @staticmethod
169147 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" )
178152
179153 @classmethod
180154 def from_priv_key_uri (
@@ -183,6 +157,9 @@ def from_priv_key_uri(
183157 public_key : Key ,
184158 secrets_handler : SecretsHandler | None = None ,
185159 ) -> AzureSigner :
160+ if not isinstance (public_key , SSlibKey ):
161+ raise ValueError (f"Expected SSlibKey for { priv_key_uri } " )
162+
186163 uri = parse .urlparse (priv_key_uri )
187164
188165 if uri .scheme != cls .SCHEME :
@@ -192,7 +169,7 @@ def from_priv_key_uri(
192169 return cls (az_key_uri , public_key )
193170
194171 @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 ]:
196173 """Load key and signer details from KMS
197174
198175 Returns the private key uri and the public key. This method should only
0 commit comments