43
43
44
44
import securesystemslib .formats
45
45
import securesystemslib .settings
46
+ import securesystemslib .storage
46
47
import securesystemslib .util
47
48
import securesystemslib .keys
48
49
@@ -242,7 +243,8 @@ def generate_and_write_rsa_keypair(filepath=None, bits=DEFAULT_RSA_KEY_BITS,
242
243
243
244
244
245
def import_rsa_privatekey_from_file (filepath , password = None ,
245
- scheme = 'rsassa-pss-sha256' , prompt = False ):
246
+ scheme = 'rsassa-pss-sha256' , prompt = False ,
247
+ storage_backend = None ):
246
248
"""
247
249
<Purpose>
248
250
Import the PEM file in 'filepath' containing the private key.
@@ -272,6 +274,11 @@ def import_rsa_privatekey_from_file(filepath, password=None,
272
274
If True the user is prompted for a passphrase to decrypt 'filepath'.
273
275
Default is False.
274
276
277
+ storage_backend:
278
+ An object which implements
279
+ securesystemslib.storage.StorageBackendInterface. When no object is
280
+ passed a FilesystemBackend will be instantiated and used.
281
+
275
282
<Exceptions>
276
283
ValueError, if 'password' is passed and 'prompt' is True.
277
284
@@ -344,8 +351,11 @@ def import_rsa_privatekey_from_file(filepath, password=None,
344
351
logger .debug ('No password was given. Attempting to import an'
345
352
' unencrypted file.' )
346
353
354
+ if storage_backend is None :
355
+ storage_backend = securesystemslib .storage .FilesystemBackend ()
356
+
347
357
# Read the contents of 'filepath' that should be a PEM formatted private key.
348
- with open (filepath , 'rb' ) as file_object :
358
+ with storage_backend . get (filepath ) as file_object :
349
359
pem_key = file_object .read ().decode ('utf-8' )
350
360
351
361
# Convert 'pem_key' to 'securesystemslib.formats.RSAKEY_SCHEMA' format.
@@ -360,7 +370,8 @@ def import_rsa_privatekey_from_file(filepath, password=None,
360
370
361
371
362
372
363
- def import_rsa_publickey_from_file (filepath , scheme = 'rsassa-pss-sha256' ):
373
+ def import_rsa_publickey_from_file (filepath , scheme = 'rsassa-pss-sha256' ,
374
+ storage_backend = None ):
364
375
"""
365
376
<Purpose>
366
377
Import the RSA key stored in 'filepath'. The key object returned is in the
@@ -374,6 +385,11 @@ def import_rsa_publickey_from_file(filepath, scheme='rsassa-pss-sha256'):
374
385
scheme:
375
386
The signature scheme used by the imported key.
376
387
388
+ storage_backend:
389
+ An object which implements
390
+ securesystemslib.storage.StorageBackendInterface. When no object is
391
+ passed a FilesystemBackend will be instantiated and used.
392
+
377
393
<Exceptions>
378
394
securesystemslib.exceptions.FormatError, if 'filepath' is improperly
379
395
formatted.
@@ -397,9 +413,12 @@ def import_rsa_publickey_from_file(filepath, scheme='rsassa-pss-sha256'):
397
413
# Is 'scheme' properly formatted?
398
414
securesystemslib .formats .RSA_SCHEME_SCHEMA .check_match (scheme )
399
415
416
+ if storage_backend is None :
417
+ storage_backend = securesystemslib .storage .FilesystemBackend ()
418
+
400
419
# Read the contents of the key file that should be in PEM format and contains
401
420
# the public portion of the RSA key.
402
- with open (filepath , 'rb' ) as file_object :
421
+ with storage_backend . get (filepath ) as file_object :
403
422
rsa_pubkey_pem = file_object .read ().decode ('utf-8' )
404
423
405
424
# Convert 'rsa_pubkey_pem' to 'securesystemslib.formats.RSAKEY_SCHEMA' format.
@@ -587,7 +606,8 @@ def import_ed25519_publickey_from_file(filepath):
587
606
588
607
589
608
590
- def import_ed25519_privatekey_from_file (filepath , password = None , prompt = False ):
609
+ def import_ed25519_privatekey_from_file (filepath , password = None , prompt = False ,
610
+ storage_backend = None ):
591
611
"""
592
612
<Purpose>
593
613
Import the encrypted ed25519 key file in 'filepath', decrypt it, and return
@@ -610,6 +630,11 @@ def import_ed25519_privatekey_from_file(filepath, password=None, prompt=False):
610
630
If True the user is prompted for a passphrase to decrypt 'filepath'.
611
631
Default is False.
612
632
633
+ storage_backend:
634
+ An object which implements
635
+ securesystemslib.storage.StorageBackendInterface. When no object is
636
+ passed a FilesystemBackend will be instantiated and used.
637
+
613
638
<Exceptions>
614
639
securesystemslib.exceptions.FormatError, if the arguments are improperly
615
640
formatted or the imported key object contains an invalid key type (i.e.,
@@ -634,6 +659,9 @@ def import_ed25519_privatekey_from_file(filepath, password=None, prompt=False):
634
659
if password and prompt :
635
660
raise ValueError ("Passing 'password' and 'prompt' True is not allowed." )
636
661
662
+ if storage_backend is None :
663
+ storage_backend = securesystemslib .storage .FilesystemBackend ()
664
+
637
665
# If 'password' was passed check format and that it is not empty.
638
666
if password is not None :
639
667
securesystemslib .formats .PASSWORD_SCHEMA .check_match (password )
@@ -663,11 +691,12 @@ def import_ed25519_privatekey_from_file(filepath, password=None, prompt=False):
663
691
password = None
664
692
665
693
# Finally, regardless of password, try decrypting the key, if necessary.
666
- # Otherwise, load it straight from the disk .
667
- with open (filepath , 'rb' ) as file_object :
694
+ # Otherwise, load it straight from storage .
695
+ with storage_backend . get (filepath ) as file_object :
668
696
json_str = file_object .read ()
669
- return securesystemslib .keys .\
670
- import_ed25519key_from_private_json (json_str , password = password )
697
+
698
+ return securesystemslib .keys .\
699
+ import_ed25519key_from_private_json (json_str , password = password )
671
700
672
701
673
702
@@ -832,7 +861,8 @@ def import_ecdsa_publickey_from_file(filepath):
832
861
833
862
834
863
835
- def import_ecdsa_privatekey_from_file (filepath , password = None ):
864
+ def import_ecdsa_privatekey_from_file (filepath , password = None ,
865
+ storage_backend = None ):
836
866
"""
837
867
<Purpose>
838
868
Import the encrypted ECDSA key file in 'filepath', decrypt it, and return
@@ -850,6 +880,11 @@ def import_ecdsa_privatekey_from_file(filepath, password=None):
850
880
encrypted key file 'filepath' must be decrypted before the ECDSA key
851
881
object can be returned.
852
882
883
+ storage_backend:
884
+ An object which implements
885
+ securesystemslib.storage.StorageBackendInterface. When no object is
886
+ passed a FilesystemBackend will be instantiated and used.
887
+
853
888
<Exceptions>
854
889
securesystemslib.exceptions.FormatError, if the arguments are improperly
855
890
formatted or the imported key object contains an invalid key type (i.e.,
@@ -886,11 +921,14 @@ def import_ecdsa_privatekey_from_file(filepath, password=None):
886
921
# Does 'password' have the correct format?
887
922
securesystemslib .formats .PASSWORD_SCHEMA .check_match (password )
888
923
924
+ if storage_backend is None :
925
+ storage_backend = securesystemslib .storage .FilesystemBackend ()
926
+
889
927
# Store the encrypted contents of 'filepath' prior to calling the decryption
890
928
# routine.
891
929
encrypted_key = None
892
930
893
- with open (filepath , 'rb' ) as file_object :
931
+ with storage_backend . get (filepath ) as file_object :
894
932
encrypted_key = file_object .read ()
895
933
896
934
# Decrypt the loaded key file, calling the 'cryptography' library to generate
0 commit comments