@@ -242,7 +242,8 @@ def generate_and_write_rsa_keypair(filepath=None, bits=DEFAULT_RSA_KEY_BITS,
242
242
243
243
244
244
def import_rsa_privatekey_from_file (filepath , password = None ,
245
- scheme = 'rsassa-pss-sha256' , prompt = False ):
245
+ scheme = 'rsassa-pss-sha256' , prompt = False ,
246
+ storage_backend = None ):
246
247
"""
247
248
<Purpose>
248
249
Import the PEM file in 'filepath' containing the private key.
@@ -272,6 +273,11 @@ def import_rsa_privatekey_from_file(filepath, password=None,
272
273
If True the user is prompted for a passphrase to decrypt 'filepath'.
273
274
Default is False.
274
275
276
+ storage_backend:
277
+ An object which implements
278
+ securesystemslib.storage.StorageBackendInterface. When no object is
279
+ passed a FilesystemBackend will be instantiated and used.
280
+
275
281
<Exceptions>
276
282
ValueError, if 'password' is passed and 'prompt' is True.
277
283
@@ -344,9 +350,13 @@ def import_rsa_privatekey_from_file(filepath, password=None,
344
350
logger .debug ('No password was given. Attempting to import an'
345
351
' unencrypted file.' )
346
352
353
+ if storage_backend is None :
354
+ storage_backend = securesystemslib .storage .FilesystemBackend ()
355
+
347
356
# Read the contents of 'filepath' that should be a PEM formatted private key.
348
- with open (filepath , 'rb' ) as file_object :
349
- pem_key = file_object .read ().decode ('utf-8' )
357
+ file_object = storage_backend .get (filepath )
358
+ pem_key = file_object .read ().decode ('utf-8' )
359
+ file_object .close ()
350
360
351
361
# Convert 'pem_key' to 'securesystemslib.formats.RSAKEY_SCHEMA' format.
352
362
# Raise 'securesystemslib.exceptions.CryptoError' if 'pem_key' is invalid.
@@ -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,10 +413,14 @@ 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 :
403
- rsa_pubkey_pem = file_object .read ().decode ('utf-8' )
421
+ file_object = storage_backend .get (filepath )
422
+ rsa_pubkey_pem = file_object .read ().decode ('utf-8' )
423
+ file_object .close ()
404
424
405
425
# Convert 'rsa_pubkey_pem' to 'securesystemslib.formats.RSAKEY_SCHEMA' format.
406
426
try :
@@ -587,7 +607,8 @@ def import_ed25519_publickey_from_file(filepath):
587
607
588
608
589
609
590
- def import_ed25519_privatekey_from_file (filepath , password = None , prompt = False ):
610
+ def import_ed25519_privatekey_from_file (filepath , password = None , prompt = False ,
611
+ storage_backend = None ):
591
612
"""
592
613
<Purpose>
593
614
Import the encrypted ed25519 key file in 'filepath', decrypt it, and return
@@ -610,6 +631,11 @@ def import_ed25519_privatekey_from_file(filepath, password=None, prompt=False):
610
631
If True the user is prompted for a passphrase to decrypt 'filepath'.
611
632
Default is False.
612
633
634
+ storage_backend:
635
+ An object which implements
636
+ securesystemslib.storage.StorageBackendInterface. When no object is
637
+ passed a FilesystemBackend will be instantiated and used.
638
+
613
639
<Exceptions>
614
640
securesystemslib.exceptions.FormatError, if the arguments are improperly
615
641
formatted or the imported key object contains an invalid key type (i.e.,
@@ -634,6 +660,9 @@ def import_ed25519_privatekey_from_file(filepath, password=None, prompt=False):
634
660
if password and prompt :
635
661
raise ValueError ("Passing 'password' and 'prompt' True is not allowed." )
636
662
663
+ if storage_backend is None :
664
+ storage_backend = securesystemslib .storage .FilesystemBackend ()
665
+
637
666
# If 'password' was passed check format and that it is not empty.
638
667
if password is not None :
639
668
securesystemslib .formats .PASSWORD_SCHEMA .check_match (password )
@@ -664,10 +693,11 @@ def import_ed25519_privatekey_from_file(filepath, password=None, prompt=False):
664
693
665
694
# Finally, regardless of password, try decrypting the key, if necessary.
666
695
# Otherwise, load it straight from the disk.
667
- with open (filepath , 'rb' ) as file_object :
668
- json_str = file_object .read ()
669
- return securesystemslib .keys .\
670
- import_ed25519key_from_private_json (json_str , password = password )
696
+ file_object = storage_backend .get (filepath )
697
+ json_str = file_object .read ()
698
+ file_object .close ()
699
+ return securesystemslib .keys .\
700
+ import_ed25519key_from_private_json (json_str , password = password )
671
701
672
702
673
703
@@ -832,7 +862,8 @@ def import_ecdsa_publickey_from_file(filepath):
832
862
833
863
834
864
835
- def import_ecdsa_privatekey_from_file (filepath , password = None ):
865
+ def import_ecdsa_privatekey_from_file (filepath , password = None ,
866
+ storage_backend = None ):
836
867
"""
837
868
<Purpose>
838
869
Import the encrypted ECDSA key file in 'filepath', decrypt it, and return
@@ -850,6 +881,11 @@ def import_ecdsa_privatekey_from_file(filepath, password=None):
850
881
encrypted key file 'filepath' must be decrypted before the ECDSA key
851
882
object can be returned.
852
883
884
+ storage_backend:
885
+ An object which implements
886
+ securesystemslib.storage.StorageBackendInterface. When no object is
887
+ passed a FilesystemBackend will be instantiated and used.
888
+
853
889
<Exceptions>
854
890
securesystemslib.exceptions.FormatError, if the arguments are improperly
855
891
formatted or the imported key object contains an invalid key type (i.e.,
@@ -886,12 +922,16 @@ def import_ecdsa_privatekey_from_file(filepath, password=None):
886
922
# Does 'password' have the correct format?
887
923
securesystemslib .formats .PASSWORD_SCHEMA .check_match (password )
888
924
925
+ if storage_backend is None :
926
+ storage_backend = securesystemslib .storage .FilesystemBackend ()
927
+
889
928
# Store the encrypted contents of 'filepath' prior to calling the decryption
890
929
# routine.
891
930
encrypted_key = None
892
931
893
- with open (filepath , 'rb' ) as file_object :
894
- encrypted_key = file_object .read ()
932
+ file_object = storage_backend .get (filepath )
933
+ encrypted_key = file_object .read ()
934
+ file_object .close ()
895
935
896
936
# Decrypt the loaded key file, calling the 'cryptography' library to generate
897
937
# the derived encryption key from 'password'. Raise
0 commit comments