Skip to content

Commit 5bfedaa

Browse files
committed
az keyvault key sign/verify: Fix --digest to accept base64 encoded string
1 parent b13672a commit 5bfedaa

File tree

3 files changed

+517
-304
lines changed

3 files changed

+517
-304
lines changed

src/azure-cli/azure/cli/command_modules/keyvault/custom.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# --------------------------------------------------------------------------------------------
55

66
# pylint: disable=too-many-lines
7+
import base64
78
import codecs
89
import hashlib
910
import json
@@ -1127,19 +1128,31 @@ def decrypt_key(cmd, client, algorithm, value, iv=None, tag=None, aad=None,
11271128

11281129

11291130
def sign_key(cmd, client, algorithm, digest, name=None, version=None):
1131+
if '256' in algorithm:
1132+
hash_func = hashlib.sha256
1133+
elif '384' in algorithm:
1134+
hash_func = hashlib.sha384
1135+
else:
1136+
hash_func = hashlib.sha512
11301137
SignatureAlgorithm = cmd.loader.get_sdk('SignatureAlgorithm', mod='crypto._enums',
11311138
resource_type=ResourceType.DATA_KEYVAULT_KEYS)
11321139
crypto_client = client.get_cryptography_client(name, key_version=version)
1133-
return crypto_client.sign(SignatureAlgorithm(algorithm), digest.encode('utf-8'))
1140+
return crypto_client.sign(SignatureAlgorithm(algorithm),
1141+
hash_func(base64.b64decode(digest.encode('utf-8'))).digest())
11341142

11351143

11361144
def verify_key(cmd, client, algorithm, digest, signature, name=None, version=None):
1137-
import base64
1145+
if '256' in algorithm:
1146+
hash_func = hashlib.sha256
1147+
elif '384' in algorithm:
1148+
hash_func = hashlib.sha384
1149+
else:
1150+
hash_func = hashlib.sha512
11381151
SignatureAlgorithm = cmd.loader.get_sdk('SignatureAlgorithm', mod='crypto._enums',
11391152
resource_type=ResourceType.DATA_KEYVAULT_KEYS)
11401153
crypto_client = client.get_cryptography_client(name, key_version=version)
11411154
return crypto_client.verify(SignatureAlgorithm(algorithm),
1142-
digest.encode('utf-8'),
1155+
hash_func(base64.b64decode(digest.encode('utf-8'))).digest(),
11431156
base64.b64decode(signature.encode('utf-8')))
11441157

11451158

@@ -1493,7 +1506,6 @@ def download_secret(client, file_path, name=None, encoding=None, version=''): #
14931506
f.write(secret_value)
14941507
else:
14951508
if encoding == 'base64':
1496-
import base64
14971509
decoded = base64.b64decode(secret_value)
14981510
elif encoding == 'hex':
14991511
import binascii
@@ -1558,7 +1570,6 @@ def download_certificate(client, file_path, certificate_name=None, encoding='PEM
15581570
if encoding == 'DER':
15591571
f.write(cert)
15601572
else:
1561-
import base64
15621573
encoded = base64.encodebytes(cert)
15631574
if isinstance(encoded, bytes):
15641575
encoded = encoded.decode("utf-8")

0 commit comments

Comments
 (0)