2525import tempfile
2626from datetime import datetime , timedelta , timezone
2727from pathlib import Path
28- from typing import Any , Dict
28+ from typing import Dict
2929
30- from securesystemslib .keys import generate_ed25519_key
31- from securesystemslib .signer import SSlibKey , SSlibSigner
30+ from securesystemslib .signer import CryptoSigner , Signer
3231
3332from tuf .api .metadata import (
3433 SPECIFICATION_VERSION ,
@@ -89,7 +88,7 @@ def _in(days: float) -> datetime:
8988# Define containers for role objects and cryptographic keys created below. This
9089# allows us to sign and write metadata in a batch more easily.
9190roles : Dict [str , Metadata ] = {}
92- keys : Dict [str , Dict [ str , Any ] ] = {}
91+ signers : Dict [str , Signer ] = {}
9392
9493
9594# Targets (integrity)
@@ -157,10 +156,8 @@ def _in(days: float) -> datetime:
157156# See https://github.com/secure-systems-lab/securesystemslib for more details
158157# about key handling, and don't forget to password-encrypt your private keys!
159158for name in ["targets" , "snapshot" , "timestamp" , "root" ]:
160- keys [name ] = generate_ed25519_key ()
161- roles ["root" ].signed .add_key (
162- SSlibKey .from_securesystemslib_key (keys [name ]), name
163- )
159+ signers [name ] = CryptoSigner .generate_ecdsa ()
160+ roles ["root" ].signed .add_key (signers [name ].public_key , name )
164161
165162# NOTE: We only need the public part to populate root, so it is possible to use
166163# out-of-band mechanisms to generate key pairs and only expose the public part
@@ -173,10 +170,8 @@ def _in(days: float) -> datetime:
173170# threshold of multiple keys to sign root metadata. For this example we
174171# generate another root key (you can pretend it's out-of-band) and increase the
175172# required signature threshold.
176- another_root_key = generate_ed25519_key ()
177- roles ["root" ].signed .add_key (
178- SSlibKey .from_securesystemslib_key (another_root_key ), "root"
179- )
173+ another_root_signer = CryptoSigner .generate_ecdsa ()
174+ roles ["root" ].signed .add_key (another_root_signer .public_key , "root" )
180175roles ["root" ].signed .roles ["root" ].threshold = 2
181176
182177
@@ -185,9 +180,7 @@ def _in(days: float) -> datetime:
185180# In this example we have access to all top-level signing keys, so we can use
186181# them to create and add a signature for each role metadata.
187182for name in ["targets" , "snapshot" , "timestamp" , "root" ]:
188- key = keys [roles [name ].signed .type ]
189- signer = SSlibSigner (key )
190- roles [name ].sign (signer )
183+ roles [name ].sign (signers [name ])
191184
192185
193186# Persist metadata (consistent snapshot)
@@ -227,9 +220,9 @@ def _in(days: float) -> datetime:
227220# file, sign it, and write it back to the same file, and this can be repeated
228221# until the threshold is satisfied.
229222root_path = os .path .join (TMP_DIR , "1.root.json" )
230- roles [ " root" ] .from_file (root_path )
231- roles [ " root" ] .sign (SSlibSigner ( another_root_key ) , append = True )
232- roles [ " root" ] .to_file (root_path , serializer = PRETTY )
223+ root = Metadata .from_file (root_path )
224+ root .sign (another_root_signer , append = True )
225+ root .to_file (root_path , serializer = PRETTY )
233226
234227
235228# Targets delegation
@@ -243,7 +236,7 @@ def _in(days: float) -> datetime:
243236# In this example the top-level targets role trusts a new "python-scripts"
244237# targets role to provide integrity for any target file that ends with ".py".
245238delegatee_name = "python-scripts"
246- keys [delegatee_name ] = generate_ed25519_key ()
239+ signers [delegatee_name ] = CryptoSigner . generate_ecdsa ()
247240
248241# Delegatee
249242# ---------
@@ -271,16 +264,13 @@ def _in(days: float) -> datetime:
271264# delegatee is responsible for, e.g. a list of path patterns. For details about
272265# all configuration parameters see
273266# https://theupdateframework.github.io/specification/latest/#delegations
267+ delegatee_key = signers [delegatee_name ].public_key
274268roles ["targets" ].signed .delegations = Delegations (
275- keys = {
276- keys [delegatee_name ]["keyid" ]: SSlibKey .from_securesystemslib_key (
277- keys [delegatee_name ]
278- )
279- },
269+ keys = {delegatee_key .keyid : delegatee_key },
280270 roles = {
281271 delegatee_name : DelegatedRole (
282272 name = delegatee_name ,
283- keyids = [keys [ delegatee_name ][ " keyid" ] ],
273+ keyids = [delegatee_key . keyid ],
284274 threshold = 1 ,
285275 terminating = True ,
286276 paths = ["*.py" ],
@@ -319,8 +309,7 @@ def _in(days: float) -> datetime:
319309
320310# Sign and write metadata for all changed roles, i.e. all but root
321311for role_name in ["targets" , "python-scripts" , "snapshot" , "timestamp" ]:
322- signer = SSlibSigner (keys [role_name ])
323- roles [role_name ].sign (signer )
312+ roles [role_name ].sign (signers [role_name ])
324313
325314 # Prefix all but timestamp with version number (see consistent snapshot)
326315 filename = f"{ role_name } .json"
@@ -343,17 +332,15 @@ def _in(days: float) -> datetime:
343332# In this example we will replace a root key, and sign a new version of root
344333# with the threshold of old and new keys. Since one of the previous root keys
345334# remains in place, it can be used to count towards the old and new threshold.
346- new_root_key = generate_ed25519_key ()
335+ new_root_signer = CryptoSigner . generate_ecdsa ()
347336
348- roles ["root" ].signed .revoke_key (keys ["root" ]["keyid" ], "root" )
349- roles ["root" ].signed .add_key (
350- SSlibKey .from_securesystemslib_key (new_root_key ), "root"
351- )
337+ roles ["root" ].signed .revoke_key (signers ["root" ].public_key .keyid , "root" )
338+ roles ["root" ].signed .add_key (new_root_signer .public_key , "root" )
352339roles ["root" ].signed .version += 1
353340
354341roles ["root" ].signatures .clear ()
355- for key in [keys ["root" ], another_root_key , new_root_key ]:
356- roles ["root" ].sign (SSlibSigner ( key ) , append = True )
342+ for signer in [signers ["root" ], another_root_signer , new_root_signer ]:
343+ roles ["root" ].sign (signer , append = True )
357344
358345roles ["root" ].to_file (
359346 os .path .join (TMP_DIR , f"{ roles ['root' ].signed .version } .root.json" ),
0 commit comments