Skip to content

Commit 4e6942f

Browse files
committed
Explicitly encode data passed to securesystemslib.keys
securesystemslib PR #162 removed implicit encoding of data to bytes in securesystemslib.keys.[create_signature|verify_signature] Update to encode data where required. Signed-off-by: Joshua Lock <[email protected]>
1 parent 09f9f9b commit 4e6942f

File tree

4 files changed

+35
-22
lines changed

4 files changed

+35
-22
lines changed

tests/test_repository_lib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,9 @@ def test__remove_invalid_and_duplicate_signatures(self):
10601060
# Append the new valid, but duplicate PSS signature, and test that
10611061
# duplicates are removed. create_signature() generates a key for the
10621062
# key type of the first argument (i.e., root_rsa_key).
1063+
data = securesystemslib.formats.encode_canonical(root_signable['signed']).encode('utf-8')
10631064
new_pss_signature = securesystemslib.keys.create_signature(root_rsa_key,
1064-
root_signable['signed'])
1065+
data)
10651066
root_signable['signatures'].append(new_pss_signature)
10661067

10671068
expected_number_of_signatures = len(root_signable['signatures'])

tests/test_sig.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ def test_get_signature_status_no_role(self):
8282
# Should verify we are not adding a duplicate signature
8383
# when doing the following action. Here we know 'signable'
8484
# has only one signature so it's okay.
85+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
8586
signable['signatures'].append(securesystemslib.keys.create_signature(
86-
KEYS[0], signable['signed']))
87+
KEYS[0], signed))
8788

8889
tuf.keydb.add_key(KEYS[0])
8990

@@ -101,9 +102,10 @@ def test_get_signature_status_no_role(self):
101102

102103
def test_get_signature_status_bad_sig(self):
103104
signable = {'signed' : 'test', 'signatures' : []}
105+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
104106

105107
signable['signatures'].append(securesystemslib.keys.create_signature(
106-
KEYS[0], signable['signed']))
108+
KEYS[0], signed))
107109
signable['signed'] += 'signature no longer matches signed data'
108110

109111
tuf.keydb.add_key(KEYS[0])
@@ -133,9 +135,10 @@ def test_get_signature_status_bad_sig(self):
133135

134136
def test_get_signature_status_unknown_signing_scheme(self):
135137
signable = {'signed' : 'test', 'signatures' : []}
138+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
136139

137140
signable['signatures'].append(securesystemslib.keys.create_signature(
138-
KEYS[0], signable['signed']))
141+
KEYS[0], signed))
139142

140143
valid_scheme = KEYS[0]['scheme']
141144
KEYS[0]['scheme'] = 'unknown_signing_scheme'
@@ -168,9 +171,10 @@ def test_get_signature_status_unknown_signing_scheme(self):
168171

169172
def test_get_signature_status_single_key(self):
170173
signable = {'signed' : 'test', 'signatures' : []}
174+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
171175

172176
signable['signatures'].append(securesystemslib.keys.create_signature(
173-
KEYS[0], signable['signed']))
177+
KEYS[0], signed))
174178

175179
threshold = 1
176180

@@ -209,9 +213,10 @@ def test_get_signature_status_single_key(self):
209213

210214
def test_get_signature_status_below_threshold(self):
211215
signable = {'signed' : 'test', 'signatures' : []}
216+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
212217

213218
signable['signatures'].append(securesystemslib.keys.create_signature(
214-
KEYS[0], signable['signed']))
219+
KEYS[0], signed))
215220

216221
tuf.keydb.add_key(KEYS[0])
217222
threshold = 2
@@ -243,12 +248,13 @@ def test_get_signature_status_below_threshold(self):
243248

244249
def test_get_signature_status_below_threshold_unrecognized_sigs(self):
245250
signable = {'signed' : 'test', 'signatures' : []}
251+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
246252

247253
# Two keys sign it, but only one of them will be trusted.
248254
signable['signatures'].append(securesystemslib.keys.create_signature(
249-
KEYS[0], signable['signed']))
255+
KEYS[0], signed))
250256
signable['signatures'].append(securesystemslib.keys.create_signature(
251-
KEYS[2], signable['signed']))
257+
KEYS[2], signed))
252258

253259
tuf.keydb.add_key(KEYS[0])
254260
tuf.keydb.add_key(KEYS[1])
@@ -282,13 +288,13 @@ def test_get_signature_status_below_threshold_unrecognized_sigs(self):
282288

283289
def test_get_signature_status_below_threshold_unauthorized_sigs(self):
284290
signable = {'signed' : 'test', 'signatures' : []}
285-
291+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
286292
# Two keys sign it, but one of them is only trusted for a different
287293
# role.
288294
signable['signatures'].append(securesystemslib.keys.create_signature(
289-
KEYS[0], signable['signed']))
295+
KEYS[0], signed))
290296
signable['signatures'].append(securesystemslib.keys.create_signature(
291-
KEYS[1], signable['signed']))
297+
KEYS[1], signed))
292298

293299
tuf.keydb.add_key(KEYS[0])
294300
tuf.keydb.add_key(KEYS[1])
@@ -334,9 +340,10 @@ def test_get_signature_status_below_threshold_unauthorized_sigs(self):
334340

335341
def test_check_signatures_no_role(self):
336342
signable = {'signed' : 'test', 'signatures' : []}
343+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
337344

338345
signable['signatures'].append(securesystemslib.keys.create_signature(
339-
KEYS[0], signable['signed']))
346+
KEYS[0], signed))
340347

341348
tuf.keydb.add_key(KEYS[0])
342349

@@ -353,8 +360,10 @@ def test_check_signatures_no_role(self):
353360

354361
def test_verify_single_key(self):
355362
signable = {'signed' : 'test', 'signatures' : []}
363+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
364+
356365
signable['signatures'].append(securesystemslib.keys.create_signature(
357-
KEYS[0], signable['signed']))
366+
KEYS[0], signed))
358367

359368
tuf.keydb.add_key(KEYS[0])
360369
threshold = 1
@@ -377,12 +386,13 @@ def test_verify_single_key(self):
377386

378387
def test_verify_unrecognized_sig(self):
379388
signable = {'signed' : 'test', 'signatures' : []}
389+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
380390

381391
# Two keys sign it, but only one of them will be trusted.
382392
signable['signatures'].append(securesystemslib.keys.create_signature(
383-
KEYS[0], signable['signed']))
393+
KEYS[0], signed))
384394
signable['signatures'].append(securesystemslib.keys.create_signature(
385-
KEYS[2], signable['signed']))
395+
KEYS[2], signed))
386396

387397
tuf.keydb.add_key(KEYS[0])
388398
tuf.keydb.add_key(KEYS[1])
@@ -408,9 +418,10 @@ def test_verify_unrecognized_sig(self):
408418

409419
def test_generate_rsa_signature(self):
410420
signable = {'signed' : 'test', 'signatures' : []}
421+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
411422

412423
signable['signatures'].append(securesystemslib.keys.create_signature(
413-
KEYS[0], signable['signed']))
424+
KEYS[0], signed))
414425

415426
self.assertEqual(1, len(signable['signatures']))
416427
signature = signable['signatures'][0]
@@ -420,7 +431,7 @@ def test_generate_rsa_signature(self):
420431
self.assertTrue(securesystemslib.formats.SIGNATURE_SCHEMA.matches(returned_signature))
421432

422433
signable['signatures'].append(securesystemslib.keys.create_signature(
423-
KEYS[1], signable['signed']))
434+
KEYS[1], signed))
424435

425436
self.assertEqual(2, len(signable['signatures']))
426437
signature = signable['signatures'][1]
@@ -431,9 +442,10 @@ def test_generate_rsa_signature(self):
431442
def test_may_need_new_keys(self):
432443
# One untrusted key in 'signable'.
433444
signable = {'signed' : 'test', 'signatures' : []}
445+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
434446

435447
signable['signatures'].append(securesystemslib.keys.create_signature(
436-
KEYS[0], signable['signed']))
448+
KEYS[0], signed))
437449

438450
tuf.keydb.add_key(KEYS[1])
439451
threshold = 1

tuf/repository_lib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def _remove_invalid_and_duplicate_signatures(signable, repository_name):
360360
signature_keyids = []
361361

362362
for signature in signable['signatures']:
363-
signed = signable['signed']
363+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
364364
keyid = signature['keyid']
365365
key = None
366366

@@ -1769,7 +1769,7 @@ def sign_metadata(metadata_object, keyids, filename, repository_name):
17691769
# Generate the signature using the appropriate signing method.
17701770
if key['keytype'] in SUPPORTED_KEY_TYPES:
17711771
if 'private' in key['keyval']:
1772-
signed = signable['signed']
1772+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
17731773
try:
17741774
signature = securesystemslib.keys.create_signature(key, signed)
17751775
signable['signatures'].append(signature)

tuf/sig.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def get_signature_status(signable, role=None, repository_name='default',
157157

158158
# Extract the relevant fields from 'signable' that will allow us to identify
159159
# the different classes of keys (i.e., good_sigs, bad_sigs, etc.).
160-
signed = signable['signed']
160+
signed = securesystemslib.formats.encode_canonical(signable['signed']).encode('utf-8')
161161
signatures = signable['signatures']
162162

163163
# Iterate the signatures and enumerate the signature_status fields.
@@ -390,7 +390,7 @@ def generate_rsa_signature(signed, rsakey_dict):
390390

391391
# We need 'signed' in canonical JSON format to generate
392392
# the 'method' and 'sig' fields of the signature.
393-
signed = securesystemslib.formats.encode_canonical(signed)
393+
signed = securesystemslib.formats.encode_canonical(signed).encode('utf-8')
394394

395395
# Generate the RSA signature.
396396
# Raises securesystemslib.exceptions.FormatError and TypeError.

0 commit comments

Comments
 (0)