Skip to content

Commit ee770da

Browse files
Merge pull request #460 from theupdateframework/lukpueh-fix-#412
Lukpueh fix #412
2 parents b3b5f63 + ef67163 commit ee770da

3 files changed

Lines changed: 12 additions & 240 deletions

File tree

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
'Topic :: Security',
106106
'Topic :: Software Development'
107107
],
108-
install_requires = ['iso8601', 'six', 'securesystemslib>=0.10.2'],
108+
install_requires = ['iso8601', 'six', 'securesystemslib>=0.10.5'],
109109
packages = find_packages(exclude=['tests']),
110110
scripts = [
111111
'tuf/scripts/basic_client.py',

‎tests/.coveragerc‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ branch = True
66
exclude_lines =
77
pragma: no cover
88
def check_crypto_libraries
9-
def _get_password
10-
def _prompt
119
def __str__
1210
if __name__ == .__main__.:
1311

‎tuf/repository_lib.py‎

Lines changed: 11 additions & 237 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import tuf.settings
5252

5353
import securesystemslib
54+
import securesystemslib.interface
5455
import iso8601
5556
import six
5657

@@ -260,45 +261,6 @@ def should_write():
260261

261262

262263

263-
def _prompt(message, result_type=str):
264-
"""
265-
Non-public function that prompts the user for input by logging 'message',
266-
converting the input to 'result_type', and returning the value to the
267-
caller.
268-
"""
269-
270-
return result_type(six.moves.input(message))
271-
272-
273-
274-
275-
276-
def _get_password(prompt='Password: ', confirm=False):
277-
"""
278-
Non-public function that returns the password entered by the user. If
279-
'confirm' is True, the user is asked to enter the previously entered
280-
password once again. If they match, the password is returned to the caller.
281-
"""
282-
283-
while True:
284-
# getpass() prompts the user for a password without echoing
285-
# the user input.
286-
password = getpass.getpass(prompt, sys.stderr)
287-
288-
if not confirm:
289-
return password
290-
password2 = getpass.getpass('Confirm: ', sys.stderr)
291-
292-
if password == password2:
293-
return password
294-
295-
else:
296-
print('Mismatch; try again.')
297-
298-
299-
300-
301-
302264
def _metadata_is_partially_loaded(rolename, signable, roleinfo, repository_name):
303265
"""
304266
Non-public function that determines whether 'rolename' is loaded with
@@ -854,50 +816,8 @@ def generate_and_write_rsa_keypair(filepath, bits=DEFAULT_RSA_KEY_BITS,
854816
None.
855817
"""
856818

857-
# Do the arguments have the correct format?
858-
# This check ensures arguments have the appropriate number of
859-
# objects and object types, and that all dict keys are properly named.
860-
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
861-
securesystemslib.formats.PATH_SCHEMA.check_match(filepath)
862-
863-
# Does 'bits' have the correct format?
864-
securesystemslib.formats.RSAKEYBITS_SCHEMA.check_match(bits)
865-
866-
# If the caller does not provide a password argument, prompt for one.
867-
if password is None: # pragma: no cover
868-
message = 'Enter a password for the RSA key file: '
869-
password = _get_password(message, confirm=True)
870-
871-
# Does 'password' have the correct format?
872-
securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)
873-
874-
# Generate public and private RSA keys, encrypted the private portion
875-
# and store them in PEM format.
876-
rsa_key = securesystemslib.keys.generate_rsa_key(bits)
877-
public = rsa_key['keyval']['public']
878-
private = rsa_key['keyval']['private']
879-
encrypted_pem = securesystemslib.keys.create_rsa_encrypted_pem(private, password)
880-
881-
# Write public key (i.e., 'public', which is in PEM format) to
882-
# '<filepath>.pub'. If the parent directory of filepath does not exist,
883-
# create it (and all its parent directories, if necessary).
884-
securesystemslib.util.ensure_parent_dir(filepath)
885-
886-
# Create a tempororary file, write the contents of the public key, and move
887-
# to final destination.
888-
file_object = securesystemslib.util.TempFile()
889-
file_object.write(public.encode('utf-8'))
890-
891-
# The temporary file is closed after the final move.
892-
file_object.move(filepath + '.pub')
893-
894-
# Write the private key in encrypted PEM format to '<filepath>'.
895-
# Unlike the public key file, the private key does not have a file
896-
# extension.
897-
file_object = securesystemslib.util.TempFile()
898-
file_object.write(encrypted_pem.encode('utf-8'))
899-
file_object.move(filepath)
900-
819+
securesystemslib.interface.generate_and_write_rsa_keypair(
820+
filepath, bits, password)
901821

902822

903823

@@ -937,35 +857,8 @@ def import_rsa_privatekey_from_file(filepath, password=None):
937857
An RSA key object, conformant to 'securesystemslib.RSAKEY_SCHEMA'.
938858
"""
939859

940-
# Does 'filepath' have the correct format?
941-
# Ensure the arguments have the appropriate number of objects and object
942-
# types, and that all dict keys are properly named.
943-
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
944-
securesystemslib.formats.PATH_SCHEMA.check_match(filepath)
945-
946-
# If the caller does not provide a password argument, prompt for one.
947-
# Password confirmation disabled here, which should ideally happen only
948-
# when creating encrypted key files (i.e., improve usability).
949-
if password is None: # pragma: no cover
950-
message = 'Enter a password for the encrypted RSA file: '
951-
password = _get_password(message, confirm=False)
952-
953-
# Does 'password' have the correct format?
954-
securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)
955-
956-
encrypted_pem = None
957-
958-
# Read the contents of 'filepath' that should be an encrypted PEM.
959-
with open(filepath, 'rb') as file_object:
960-
encrypted_pem = file_object.read().decode('utf-8')
961-
962-
# Convert 'encrypted_pem' to 'securesystemslib.RSAKEY_SCHEMA' format.
963-
# Raise 'securesystemslib.exceptions.CryptoError' if 'encrypted_pem' is
964-
# invalid.
965-
rsa_key = securesystemslib.keys.import_rsakey_from_private_pem(encrypted_pem,
966-
password)
967-
968-
return rsa_key
860+
return securesystemslib.interface.import_rsa_privatekey_from_file(
861+
filepath, password)
969862

970863

971864

@@ -1000,26 +893,7 @@ def import_rsa_publickey_from_file(filepath):
1000893
An RSA key object conformant to 'securesystemslib.RSAKEY_SCHEMA'.
1001894
"""
1002895

1003-
# Does 'filepath' have the correct format?
1004-
# Ensure the arguments have the appropriate number of objects and object
1005-
# types, and that all dict keys are properly named.
1006-
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
1007-
securesystemslib.formats.PATH_SCHEMA.check_match(filepath)
1008-
1009-
# Read the contents of the key file that should be in PEM format and contains
1010-
# the public portion of the RSA key.
1011-
with open(filepath, 'rb') as file_object:
1012-
rsa_pubkey_pem = file_object.read().decode('utf-8')
1013-
1014-
# Convert 'rsa_pubkey_pem' to 'securesystemslib.RSAKEY_SCHEMA' format.
1015-
try:
1016-
rsakey_dict = securesystemslib.keys.import_rsakey_from_public_pem(rsa_pubkey_pem)
1017-
1018-
except securesystemslib.exceptions.FormatError as e:
1019-
raise securesystemslib.exceptions.Error('Cannot import improperly formatted'
1020-
' PEM file.' + repr(str(e)))
1021-
1022-
return rsakey_dict
896+
return securesystemslib.interface.import_rsa_publickey_from_file(filepath)
1023897

1024898

1025899

@@ -1065,53 +939,8 @@ def generate_and_write_ed25519_keypair(filepath, password=None):
1065939
None.
1066940
"""
1067941

1068-
# Does 'filepath' have the correct format?
1069-
# Ensure the arguments have the appropriate number of objects and object
1070-
# types, and that all dict keys are properly named.
1071-
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
1072-
securesystemslib.formats.PATH_SCHEMA.check_match(filepath)
1073-
1074-
# If the caller does not provide a password argument, prompt for one.
1075-
if password is None: # pragma: no cover
1076-
message = 'Enter a password for the Ed25519 key: '
1077-
password = _get_password(message, confirm=True)
1078-
1079-
# Does 'password' have the correct format?
1080-
securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)
1081-
1082-
# Generate a new ED25519 key object and encrypt it. The cryptography library
1083-
# used is determined by the user, or by default (set in
1084-
# 'settings.ED25519_CRYPTO_LIBRARY'). Raise
1085-
# 'securesystemslib.exceptions.CryptoError' or
1086-
# 'securesystemslib.exceptions.UnsupportedLibraryError', if 'ed25519_key'
1087-
# cannot be encrypted.
1088-
ed25519_key = securesystemslib.keys.generate_ed25519_key()
1089-
encrypted_key = securesystemslib.keys.encrypt_key(ed25519_key, password)
1090-
1091-
# ed25519 public key file contents in metadata format (i.e., does not include
1092-
# the keyid portion).
1093-
keytype = ed25519_key['keytype']
1094-
keyval = ed25519_key['keyval']
1095-
ed25519key_metadata_format = \
1096-
securesystemslib.keys.format_keyval_to_metadata(keytype, keyval, private=False)
1097-
1098-
# Write the public key, conformant to 'securesystemslib.KEY_SCHEMA', to
1099-
# '<filepath>.pub'.
1100-
securesystemslib.util.ensure_parent_dir(filepath)
1101-
1102-
# Create a tempororary file, write the contents of the public key, and move
1103-
# to final destination.
1104-
file_object = securesystemslib.util.TempFile()
1105-
file_object.write(json.dumps(ed25519key_metadata_format).encode('utf-8'))
1106-
1107-
# The temporary file is closed after the final move.
1108-
file_object.move(filepath + '.pub')
1109-
1110-
# Write the encrypted key string, conformant to
1111-
# 'securesystemslib.ENCRYPTEDKEY_SCHEMA', to '<filepath>'.
1112-
file_object = securesystemslib.util.TempFile()
1113-
file_object.write(encrypted_key.encode('utf-8'))
1114-
file_object.move(filepath)
942+
securesystemslib.interface.generate_and_write_ed25519_keypair(
943+
filepath, password)
1115944

1116945

1117946

@@ -1142,26 +971,7 @@ def import_ed25519_publickey_from_file(filepath):
1142971
'securesystemslib.ED25519KEY_SCHEMA'.
1143972
"""
1144973

1145-
# Does 'filepath' have the correct format?
1146-
# Ensure the arguments have the appropriate number of objects and object
1147-
# types, and that all dict keys are properly named.
1148-
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
1149-
securesystemslib.formats.PATH_SCHEMA.check_match(filepath)
1150-
1151-
# ED25519 key objects are saved in json and metadata format. Return the
1152-
# loaded key object in securesystemslib.ED25519KEY_SCHEMA' format that
1153-
# also includes the keyid.
1154-
ed25519_key_metadata = securesystemslib.util.load_json_file(filepath)
1155-
ed25519_key, junk = securesystemslib.keys.format_metadata_to_key(ed25519_key_metadata)
1156-
1157-
# Raise an exception if an unexpected key type is imported. Redundant
1158-
# validation of 'keytype'. 'securesystemslib.keys.format_metadata_to_key()'
1159-
# should have fully validated 'ed25519_key_metadata'.
1160-
if ed25519_key['keytype'] != 'ed25519': # pragma: no cover
1161-
message = 'Invalid key type loaded: ' + repr(ed25519_key['keytype'])
1162-
raise securesystemslib.exceptions.FormatError(message)
1163-
1164-
return ed25519_key
974+
return securesystemslib.interface.import_ed25519_publickey_from_file(filepath)
1165975

1166976

1167977

@@ -1208,44 +1018,8 @@ def import_ed25519_privatekey_from_file(filepath, password=None):
12081018
An ed25519 key object of the form: 'securesystemslib.ED25519KEY_SCHEMA'.
12091019
"""
12101020

1211-
# Does 'filepath' have the correct format?
1212-
# Ensure the arguments have the appropriate number of objects and object
1213-
# types, and that all dict keys are properly named.
1214-
# Raise 'securesystemslib.exceptions.FormatError' if there is a mismatch.
1215-
securesystemslib.formats.PATH_SCHEMA.check_match(filepath)
1216-
1217-
# If the caller does not provide a password argument, prompt for one.
1218-
# Password confirmation disabled here, which should ideally happen only
1219-
# when creating encrypted key files (i.e., improve usability).
1220-
if password is None: # pragma: no cover
1221-
message = 'Enter a password for the encrypted Ed25519 key: '
1222-
password = _get_password(message, confirm=False)
1223-
1224-
# Does 'password' have the correct format?
1225-
securesystemslib.formats.PASSWORD_SCHEMA.check_match(password)
1226-
1227-
# Store the encrypted contents of 'filepath' prior to calling the decryption
1228-
# routine.
1229-
encrypted_key = None
1230-
1231-
with open(filepath, 'rb') as file_object:
1232-
encrypted_key = file_object.read()
1233-
1234-
# Decrypt the loaded key file, calling the appropriate cryptography library
1235-
# (i.e., set by the user) and generating the derived encryption key from
1236-
# 'password'. Raise 'securesystemslib.exceptions.CryptoError' or
1237-
# 'securesystemslib.exceptions.UnsupportedLibraryError' if the decryption
1238-
# fails.
1239-
key_object = securesystemslib.keys.decrypt_key(encrypted_key.decode('utf-8'),
1240-
password)
1241-
1242-
# Raise an exception if an unexpected key type is imported.
1243-
if key_object['keytype'] != 'ed25519':
1244-
message = 'Invalid key type loaded: ' + repr(key_object['keytype'])
1245-
raise securesystemslib.exceptions.FormatError(message)
1246-
1247-
return key_object
1248-
1021+
return securesystemslib.interface.import_ed25519_privatekey_from_file(
1022+
filepath, password)
12491023

12501024

12511025

0 commit comments

Comments
 (0)