@@ -96,14 +96,15 @@ text without prepended symbols is the output of a command.
9696# following function creates an RSA key pair, where the private key is saved to
9797# "root_key" and the public key to "root_key.pub" (both saved to the current
9898# working directory).
99- >> > generate_and_write_rsa_keypair(" root_key " , bits = 2048 , password = " password " )
99+ >> > generate_and_write_rsa_keypair(password = " password " , filepath = " root_key " , bits = 2048 )
100100
101101# If the key length is unspecified, it defaults to 3072 bits. A length of less
102- # than 2048 bits raises an exception. A password may be supplied as an
103- # argument, otherwise a user prompt is presented. If an empty password
104- # is entered, the private key is saved unencrypted.
105- >> > generate_and_write_rsa_keypair(" root_key2" )
106- Enter a password for the RSA key (/ path/ to/ root_key2):
102+ # than 2048 bits raises an exception. A similar function is available to supply
103+ # a password on the prompt. If an empty password is entered, the private key
104+ # is saved unencrypted.
105+ >> > generate_and_write_rsa_keypair_with_prompt(filepath = " root_key2" )
106+ enter password to encrypt private key file ' /path/to/root_key2'
107+ (leave empty if key should not be encrypted):
107108Confirm:
108109```
109110The following four key files should now exist:
@@ -117,8 +118,9 @@ If a filepath is not given, the KEYID of the generated key is used as the
117118filename. The key files are written to the current working directory.
118119``` python
119120# Continuing from the previous section . . .
120- >> > generate_and_write_rsa_keypair()
121- Enter a password for the encrypted RSA key (/ path/ to/ b5b8de8aeda674bce948fbe82cab07e309d6775fc0ec299199d16746dc2bd54c):
121+ >> > generate_and_write_rsa_keypair_with_prompt()
122+ enter password to encrypt private key file ' /path/to/KEYID'
123+ (leave empty if key should not be encrypted):
122124Confirm:
123125```
124126
@@ -132,36 +134,27 @@ Confirm:
132134# Import an existing private key. Importing a private key requires a password,
133135# whereas importing a public key does not.
134136>> > private_root_key = import_rsa_privatekey_from_file(" root_key" )
135- Enter a password for the encrypted RSA key (/ path/ to/ root_key):
136- ```
137-
138- `import_rsa_privatekey_from_file()` raises a
139- `securesystemslib.exceptions.CryptoError` exception if the key / password is
140- invalid:
141-
142- ```
143- securesystemslib.exceptions.CryptoError: RSA (public, private) tuple cannot be
144- generated from the encrypted PEM string: Bad decrypt. Incorrect password?
137+ enter password to decrypt private key file ' /path/to/root_key'
138+ (leave empty if key not encrypted):
145139```
146140
147141### Create and Import Ed25519 Keys ###
148142``` Python
149143# Continuing from the previous section . . .
150144
151- # Generate and write an Ed25519 key pair. A 'password' argument may be
152- # supplied, otherwise a prompt is presented. The private key is saved
153- # encrypted if a non-empty password is given, and unencrypted if the password
154- # is empty.
155- >> > generate_and_write_ed25519_keypair(' ed25519_key' )
156- Enter a password for the Ed25519 key (/ path/ to/ ed25519_key):
145+ # The same generation and import functions as for rsa keys exist for ed25519
146+ >> > generate_and_write_ed25519_keypair_with_prompt(filepath = ' ed25519_key' )
147+ enter password to encrypt private key file ' /path/to/ed25519_key'
148+ (leave empty if key should not be encrypted):
157149Confirm:
158150
159151# Import the ed25519 public key just created . . .
160152>> > public_ed25519_key = import_ed25519_publickey_from_file(' ed25519_key.pub' )
161153
162154# and its corresponding private key.
163155>> > private_ed25519_key = import_ed25519_privatekey_from_file(' ed25519_key' )
164- Enter a password for the encrypted Ed25519 key (/ path/ to/ ed25519_key):
156+ enter password to decrypt private key file ' /path/to/ed25519_key'
157+ (leave empty if key should not be encrypted):
165158```
166159
167160Note: Methods are also available to generate and write keys from memory.
@@ -259,26 +252,20 @@ secure manner.
259252>> > import datetime
260253
261254# Generate keys for the remaining top-level roles. The root keys have been set above.
262- # The password argument may be omitted if a password prompt is needed.
263- >> > generate_and_write_rsa_keypair(' targets_key' , password = ' password' )
264- >> > generate_and_write_rsa_keypair(' snapshot_key' , password = ' password' )
265- >> > generate_and_write_rsa_keypair(' timestamp_key' , password = ' password' )
255+ >> > generate_and_write_rsa_keypair(password = ' password' , filepath = ' targets_key' )
256+ >> > generate_and_write_rsa_keypair(password = ' password' , filepath = ' snapshot_key' )
257+ >> > generate_and_write_rsa_keypair(password = ' password' , filepath = ' timestamp_key' )
266258
267259# Add the verification keys of the remaining top-level roles.
268260
269261>> > repository.targets.add_verification_key(import_rsa_publickey_from_file(' targets_key.pub' ))
270262>> > repository.snapshot.add_verification_key(import_rsa_publickey_from_file(' snapshot_key.pub' ))
271263>> > repository.timestamp.add_verification_key(import_rsa_publickey_from_file(' timestamp_key.pub' ))
272264
273- # Import the signing keys of the remaining top-level roles. Prompt for passwords.
274- >> > private_targets_key = import_rsa_privatekey_from_file(' targets_key' )
275- Enter a password for the encrypted RSA key (/ path/ to/ targets_key):
276-
277- >> > private_snapshot_key = import_rsa_privatekey_from_file(' snapshot_key' )
278- Enter a password for the encrypted RSA key (/ path/ to/ snapshot_key):
279-
280- >> > private_timestamp_key = import_rsa_privatekey_from_file(' timestamp_key' )
281- Enter a password for the encrypted RSA key (/ path/ to/ timestamp_key):
265+ # Import the signing keys of the remaining top-level roles.
266+ >> > private_targets_key = import_rsa_privatekey_from_file(' targets_key' , password = ' password' )
267+ >> > private_snapshot_key = import_rsa_privatekey_from_file(' snapshot_key' , password = ' password' )
268+ >> > private_timestamp_key = import_rsa_privatekey_from_file(' timestamp_key' , password = ' password' )
282269
283270# Load the signing keys of the remaining roles so that valid signatures are
284271# generated when repository.writeall() is called.
@@ -390,18 +377,21 @@ metadata. `snapshot.json` keys must be loaded and its metadata signed because
390377# The private key of the updated targets metadata must be re-loaded before it
391378# can be signed and written (Note the load_repository() call above).
392379>> > private_targets_key = import_rsa_privatekey_from_file(' targets_key' )
393- Enter a password for the encrypted RSA key (/ path/ to/ targets_key):
380+ enter password to decrypt private key file ' /path/to/targets_key'
381+ (leave empty if key not encrypted):
394382
395383>> > repository.targets.load_signing_key(private_targets_key)
396384
397385# Due to the load_repository() and new versions of metadata, we must also load
398386# the private keys of Snapshot and Timestamp to generate a valid set of metadata.
399387>> > private_snapshot_key = import_rsa_privatekey_from_file(' snapshot_key' )
400- Enter a password for the encrypted RSA key (/ path/ to/ snapshot_key):
388+ enter password to decrypt private key file ' /path/to/snapshot_key'
389+ (leave empty if key not encrypted):
401390>> > repository.snapshot.load_signing_key(private_snapshot_key)
402391
403392>> > private_timestamp_key = import_rsa_privatekey_from_file(' timestamp_key' )
404- Enter a password for the encrypted RSA key (/ path/ to/ timestamp_key):
393+ enter password to decrypt private key file ' /path/to/timestamp_key'
394+ (leave empty if key not encrypted):
405395>> > repository.timestamp.load_signing_key(private_timestamp_key)
406396
407397# Mark roles for metadata update (see #964, #958)
@@ -451,7 +441,7 @@ threshold, it needs to be added to `root.json`, e.g. via
451441>> > from securesystemslib.formats import encode_canonical
452442>> > from securesystemslib.keys import create_signature
453443>> > private_ed25519_key = import_ed25519_privatekey_from_file(' ed25519_key' )
454- Enter a password for the encrypted Ed25519 key ( / path/ to/ ed25519_key):
444+ enter password to decrypt private key file ' /path/to/ed25519_key'
455445>> > signature = create_signature(
456446... private_ed25519_key, encode_canonical(signable_content).encode())
457447```
@@ -489,7 +479,7 @@ targets and generate signed metadata.
489479# Continuing from the previous section . . .
490480
491481# Generate a key for a new delegated role named "unclaimed".
492- >> > generate_and_write_rsa_keypair(' unclaimed_key ' , bits = 2048 , password = ' password ' )
482+ >> > generate_and_write_rsa_keypair(password = ' password ' , filepath = ' unclaimed_key ' , bits = 2048 )
493483>> > public_unclaimed_key = import_rsa_publickey_from_file(' unclaimed_key.pub' )
494484
495485# Make a delegation (delegate trust of 'myproject/*.txt' files) from "targets"
@@ -502,8 +492,7 @@ targets and generate signed metadata.
502492
503493# Load the private key of "unclaimed" so that unclaimed's metadata can be
504494# signed, and valid metadata created.
505- >> > private_unclaimed_key = import_rsa_privatekey_from_file(' unclaimed_key' )
506- Enter a password for the encrypted RSA key (/ path/ to/ unclaimed_key):
495+ >> > private_unclaimed_key = import_rsa_privatekey_from_file(' unclaimed_key' , password = ' password' )
507496
508497>> > repository.targets(" unclaimed" ).load_signing_key(private_unclaimed_key)
509498
0 commit comments