Skip to content

Commit 0baedd3

Browse files
committed
Expect bytes in create_ and verify_signature; do not serialize
The caller knows what their data is and how to serialize it (including whether or not to canonicalize it in some fashion); ssl.keys is too low level to be doing this. (I talked this over with Lukas and it serves his interests for in-toto as well.) Updated testing. Did not touch code style, even though I'd love to.... Signed-off-by: Sebastien Awwad <[email protected]>
1 parent 23008b4 commit 0baedd3

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

securesystemslib/keys.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,10 @@ def create_signature(key_dict, data):
662662
The public and private keys are strings in PEM format.
663663
664664
data:
665-
Data object used by create_signature() to generate the signature.
665+
Data to be signed. This should be a bytes object; data should be
666+
encoded/serialized before it is passed here. The same value can be be
667+
passed into securesystemslib.verify_signature() (along with the public
668+
key) to later verify the signature.
666669
667670
<Exceptions>
668671
securesystemslib.exceptions.FormatError, if 'key_dict' is improperly
@@ -702,17 +705,11 @@ def create_signature(key_dict, data):
702705
keyid = key_dict['keyid']
703706
sig = None
704707

705-
# Convert 'data' to canonical JSON format so that repeatable signatures are
706-
# generated across different platforms and Python key dictionaries. The
707-
# resulting 'data' is a string encoded in UTF-8 and compatible with the input
708-
# expected by the cryptography functions called below.
709-
data = securesystemslib.formats.encode_canonical(data)
710-
711708
if keytype == 'rsa':
712709
if scheme == 'rsassa-pss-sha256':
713710
private = private.replace('\r\n', '\n')
714-
sig, scheme = securesystemslib.pyca_crypto_keys.create_rsa_signature(private,
715-
data.encode('utf-8'), scheme)
711+
sig, scheme = securesystemslib.pyca_crypto_keys.create_rsa_signature(
712+
private, data, scheme)
716713

717714
else:
718715
raise securesystemslib.exceptions.UnsupportedAlgorithmError('Unsupported'
@@ -721,12 +718,12 @@ def create_signature(key_dict, data):
721718
elif keytype == 'ed25519':
722719
public = binascii.unhexlify(public.encode('utf-8'))
723720
private = binascii.unhexlify(private.encode('utf-8'))
724-
sig, scheme = securesystemslib.ed25519_keys.create_signature(public,
725-
private, data.encode('utf-8'), scheme)
721+
sig, scheme = securesystemslib.ed25519_keys.create_signature(
722+
public, private, data, scheme)
726723

727724
elif keytype == 'ecdsa-sha2-nistp256':
728-
sig, scheme = securesystemslib.ecdsa_keys.create_signature(public, private,
729-
data.encode('utf-8'), scheme)
725+
sig, scheme = securesystemslib.ecdsa_keys.create_signature(
726+
public, private, data, scheme)
730727

731728
# 'securesystemslib.formats.ANYKEY_SCHEMA' should have detected invalid key
732729
# types. This is a defensive check against an invalid key type.
@@ -795,8 +792,10 @@ def verify_signature(key_dict, signature, data):
795792
Conformant to 'securesystemslib.formats.SIGNATURE_SCHEMA'.
796793
797794
data:
798-
Data object used by securesystemslib.rsa_key.create_signature() to
799-
generate 'signature'. 'data' is needed here to verify the signature.
795+
Data that the signature is expected to be over. This should be a bytes
796+
object; data should be encoded/serialized before it is passed here.)
797+
This is the same value that can be passed into
798+
securesystemslib.create_signature() in order to create the signature.
800799
801800
<Exceptions>
802801
securesystemslib.exceptions.FormatError, raised if either 'key_dict' or
@@ -846,11 +845,6 @@ def verify_signature(key_dict, signature, data):
846845
scheme = key_dict['scheme']
847846
valid_signature = False
848847

849-
# Convert 'data' to canonical JSON format so that repeatable signatures are
850-
# generated across different platforms and Python key dictionaries. The
851-
# resulting 'data' is a string encoded in UTF-8 and compatible with the input
852-
# expected by the cryptography functions called below.
853-
data = securesystemslib.formats.encode_canonical(data).encode('utf-8')
854848

855849
if keytype == 'rsa':
856850
if scheme == 'rsassa-pss-sha256':

tests/test_keys.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
KEYS = securesystemslib.keys
4040
FORMAT_ERROR_MSG = 'securesystemslib.exceptions.FormatError was raised!' + \
4141
' Check object\'s format.'
42-
DATA = 'SOME DATA REQUIRING AUTHENTICITY.'
42+
DATA_STR = 'SOME DATA REQUIRING AUTHENTICITY.'
43+
DATA = securesystemslib.formats.encode_canonical(DATA_STR).encode('utf-8')
4344

4445

4546

@@ -332,7 +333,8 @@ def test_verify_signature(self):
332333
# 'rsa_signature'. Function should return 'False'.
333334

334335
# Modifying 'DATA'.
335-
_DATA = '1111' + DATA + '1111'
336+
_DATA_STR = '1111' + DATA_STR + '1111'
337+
_DATA = securesystemslib.formats.encode_canonical(_DATA_STR).encode('utf-8')
336338

337339
# Verifying the 'signature' of modified '_DATA'.
338340
verified = KEYS.verify_signature(self.rsakey_dict, rsa_signature, _DATA)

0 commit comments

Comments
 (0)