1717from datetime import datetime , timedelta
1818from typing import Any , ClassVar , Dict
1919
20+ from securesystemslib import exceptions as sslib_exceptions
2021from securesystemslib import hash as sslib_hash
2122from securesystemslib .interface import (
2223 import_ed25519_privatekey_from_file ,
2324 import_ed25519_publickey_from_file ,
2425)
2526from securesystemslib .keys import generate_ed25519_key
26- from securesystemslib .signer import Signature , SSlibSigner
27+ from securesystemslib .signer import SSlibKey , SSlibSigner
2728
2829from tests import utils
2930from tuf .api import exceptions
3435 Key ,
3536 Metadata ,
3637 Root ,
38+ Signature ,
3739 Snapshot ,
3840 SuccinctRoles ,
3941 TargetFile ,
@@ -187,8 +189,8 @@ def test_to_from_bytes(self) -> None:
187189 self .assertEqual (metadata_obj_2 .to_bytes (), obj_bytes )
188190
189191 def test_sign_verify (self ) -> None :
190- root_path = os .path .join (self .repo_dir , "metadata" , "root.json " )
191- root = Metadata [Root ].from_file (root_path ).signed
192+ path = os .path .join (self .repo_dir , "metadata" )
193+ root = Metadata [Root ].from_file (os . path . join ( path , "root.json" ) ).signed
192194
193195 # Locate the public keys we need from root
194196 targets_keyid = next (iter (root .roles [Targets .type ].keyids ))
@@ -199,41 +201,37 @@ def test_sign_verify(self) -> None:
199201 timestamp_key = root .keys [timestamp_keyid ]
200202
201203 # Load sample metadata (targets) and assert ...
202- path = os .path .join (self .repo_dir , "metadata" , "targets.json" )
203- md_obj = Metadata .from_file (path )
204+ md_obj = Metadata .from_file (os .path .join (path , "targets.json" ))
205+ sig = md_obj .signatures [targets_keyid ]
206+ data = CanonicalJSONSerializer ().serialize (md_obj .signed )
204207
205208 # ... it has a single existing signature,
206209 self .assertEqual (len (md_obj .signatures ), 1 )
207210 # ... which is valid for the correct key.
208- targets_key .verify_signature (md_obj )
209- with self .assertRaises (exceptions .UnsignedMetadataError ):
210- snapshot_key .verify_signature (md_obj )
211-
212- # Test verifying with explicitly set serializer
213- targets_key .verify_signature (md_obj , CanonicalJSONSerializer ())
214- with self .assertRaises (exceptions .UnsignedMetadataError ):
215- targets_key .verify_signature (md_obj , JSONSerializer ()) # type: ignore[arg-type]
211+ targets_key .verify_signature (sig , data )
212+ with self .assertRaises (sslib_exceptions .VerificationError ):
213+ snapshot_key .verify_signature (sig , data )
216214
217215 sslib_signer = SSlibSigner (self .keystore [Snapshot .type ])
218216 # Append a new signature with the unrelated key and assert that ...
219- sig = md_obj .sign (sslib_signer , append = True )
217+ snapshot_sig = md_obj .sign (sslib_signer , append = True )
220218 # ... there are now two signatures, and
221219 self .assertEqual (len (md_obj .signatures ), 2 )
222220 # ... both are valid for the corresponding keys.
223- targets_key .verify_signature (md_obj )
224- snapshot_key .verify_signature (md_obj )
221+ targets_key .verify_signature (sig , data )
222+ snapshot_key .verify_signature (snapshot_sig , data )
225223 # ... the returned (appended) signature is for snapshot key
226- self .assertEqual (sig .keyid , snapshot_keyid )
224+ self .assertEqual (snapshot_sig .keyid , snapshot_keyid )
227225
228226 sslib_signer = SSlibSigner (self .keystore [Timestamp .type ])
229227 # Create and assign (don't append) a new signature and assert that ...
230- md_obj .sign (sslib_signer , append = False )
228+ ts_sig = md_obj .sign (sslib_signer , append = False )
231229 # ... there now is only one signature,
232230 self .assertEqual (len (md_obj .signatures ), 1 )
233231 # ... valid for that key.
234- timestamp_key .verify_signature (md_obj )
235- with self .assertRaises (exceptions . UnsignedMetadataError ):
236- targets_key .verify_signature (md_obj )
232+ timestamp_key .verify_signature (ts_sig , data )
233+ with self .assertRaises (sslib_exceptions . VerificationError ):
234+ targets_key .verify_signature (ts_sig , data )
237235
238236 def test_sign_failures (self ) -> None :
239237 # Test throwing UnsignedMetadataError because of signing problems
@@ -248,7 +246,7 @@ def test_sign_failures(self) -> None:
248246 with self .assertRaises (exceptions .UnsignedMetadataError ):
249247 md .sign (sslib_signer )
250248
251- def test_verify_failures (self ) -> None :
249+ def test_key_verify_failures (self ) -> None :
252250 root_path = os .path .join (self .repo_dir , "metadata" , "root.json" )
253251 root = Metadata [Root ].from_file (root_path ).signed
254252
@@ -259,36 +257,36 @@ def test_verify_failures(self) -> None:
259257 # Load sample metadata (timestamp)
260258 path = os .path .join (self .repo_dir , "metadata" , "timestamp.json" )
261259 md_obj = Metadata .from_file (path )
260+ sig = md_obj .signatures [timestamp_keyid ]
261+ data = CanonicalJSONSerializer ().serialize (md_obj .signed )
262262
263263 # Test failure on unknown scheme (securesystemslib
264264 # UnsupportedAlgorithmError)
265265 scheme = timestamp_key .scheme
266266 timestamp_key .scheme = "foo"
267- with self .assertRaises (exceptions . UnsignedMetadataError ):
268- timestamp_key .verify_signature (md_obj )
267+ with self .assertRaises (sslib_exceptions . VerificationError ):
268+ timestamp_key .verify_signature (sig , data )
269269 timestamp_key .scheme = scheme
270270
271271 # Test failure on broken public key data (securesystemslib
272272 # CryptoError)
273273 public = timestamp_key .keyval ["public" ]
274274 timestamp_key .keyval ["public" ] = "ffff"
275- with self .assertRaises (exceptions . UnsignedMetadataError ):
276- timestamp_key .verify_signature (md_obj )
275+ with self .assertRaises (sslib_exceptions . VerificationError ):
276+ timestamp_key .verify_signature (sig , data )
277277 timestamp_key .keyval ["public" ] = public
278278
279279 # Test failure with invalid signature (securesystemslib
280280 # FormatError)
281- sig = md_obj .signatures [timestamp_keyid ]
282- correct_sig = sig .signature
283- sig .signature = "foo"
284- with self .assertRaises (exceptions .UnsignedMetadataError ):
285- timestamp_key .verify_signature (md_obj )
281+ incorrect_sig = copy (sig )
282+ incorrect_sig .signature = "foo"
283+ with self .assertRaises (sslib_exceptions .VerificationError ):
284+ timestamp_key .verify_signature (incorrect_sig , data )
286285
287286 # Test failure with valid but incorrect signature
288- sig .signature = "ff" * 64
289- with self .assertRaises (exceptions .UnsignedMetadataError ):
290- timestamp_key .verify_signature (md_obj )
291- sig .signature = correct_sig
287+ incorrect_sig .signature = "ff" * 64
288+ with self .assertRaises (sslib_exceptions .UnverifiedSignatureError ):
289+ timestamp_key .verify_signature (incorrect_sig , data )
292290
293291 def test_metadata_signed_is_expired (self ) -> None :
294292 # Use of Snapshot is arbitrary, we're just testing the base class
@@ -355,6 +353,15 @@ def test_metadata_verify_delegate(self) -> None:
355353 root .verify_delegate (Snapshot .type , snapshot )
356354 snapshot .signed .expires = expires
357355
356+ # verify fails if sslib verify fails with VerificationError
357+ # (in this case signature is malformed)
358+ keyid = next (iter (root .signed .roles [Snapshot .type ].keyids ))
359+ good_sig = snapshot .signatures [keyid ].signature
360+ snapshot .signatures [keyid ].signature = "foo"
361+ with self .assertRaises (exceptions .UnsignedMetadataError ):
362+ root .verify_delegate (Snapshot .type , snapshot )
363+ snapshot .signatures [keyid ].signature = good_sig
364+
358365 # verify fails if roles keys do not sign the metadata
359366 with self .assertRaises (exceptions .UnsignedMetadataError ):
360367 root .verify_delegate (Timestamp .type , snapshot )
@@ -382,14 +389,9 @@ def test_key_class(self) -> None:
382389 # Test if from_securesystemslib_key removes the private key from keyval
383390 # of a securesystemslib key dictionary.
384391 sslib_key = generate_ed25519_key ()
385- key = Key .from_securesystemslib_key (sslib_key )
392+ key = SSlibKey .from_securesystemslib_key (sslib_key )
386393 self .assertFalse ("private" in key .keyval .keys ())
387394
388- # Test raising ValueError with non-existent keytype
389- sslib_key ["keytype" ] = "bad keytype"
390- with self .assertRaises (ValueError ):
391- Key .from_securesystemslib_key (sslib_key )
392-
393395 def test_root_add_key_and_revoke_key (self ) -> None :
394396 root_path = os .path .join (self .repo_dir , "metadata" , "root.json" )
395397 root = Metadata [Root ].from_file (root_path )
@@ -399,7 +401,7 @@ def test_root_add_key_and_revoke_key(self) -> None:
399401 os .path .join (self .keystore_dir , "root_key2.pub" )
400402 )
401403 keyid = root_key2 ["keyid" ]
402- key_metadata = Key (
404+ key_metadata = SSlibKey (
403405 keyid ,
404406 root_key2 ["keytype" ],
405407 root_key2 ["scheme" ],
@@ -412,7 +414,7 @@ def test_root_add_key_and_revoke_key(self) -> None:
412414
413415 # Assert that add_key with old argument order will raise an error
414416 with self .assertRaises (ValueError ):
415- root .signed .add_key (Root .type , key_metadata ) # type: ignore
417+ root .signed .add_key (Root .type , key_metadata )
416418
417419 # Add new root key
418420 root .signed .add_key (key_metadata , Root .type )
@@ -513,7 +515,7 @@ def test_targets_key_api(self) -> None:
513515
514516 # Assert that add_key with old argument order will raise an error
515517 with self .assertRaises (ValueError ):
516- targets .add_key ("role1" , key ) # type: ignore
518+ targets .add_key ("role1" , key )
517519
518520 # Assert that delegated role "role1" does not contain the new key
519521 self .assertNotIn (key .keyid , targets .delegations .roles ["role1" ].keyids )
0 commit comments