Skip to content

Commit d7bc328

Browse files
Merge pull request #239 from vladimir-v-diaz/develop
Review and update Pull Request #238.
2 parents 377f722 + 4a61392 commit d7bc328

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

tests/test_keys.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ def test_format_keyval_to_metadata(self):
9898
self.assertRaises(tuf.FormatError, KEYS.format_keyval_to_metadata,
9999
keytype, keyvalue)
100100
keyvalue['public'] = public
101+
102+
103+
104+
def test_format_rsakey_from_pem(self):
105+
pem = self.rsakey_dict['keyval']['public']
106+
rsa_key = KEYS.format_rsakey_from_pem(pem)
107+
108+
# Check if the format of the object returned by this function corresponds
109+
# to 'tuf.formats.RSAKEY_SCHEMA' format.
110+
self.assertTrue(tuf.formats.RSAKEY_SCHEMA.matches(rsa_key))
111+
112+
# Verify whitespace is stripped.
113+
self.assertEqual(rsa_key, KEYS.format_rsakey_from_pem(pem + '\n'))
114+
115+
# Supplying a 'bad_pem' argument.
116+
self.assertRaises(tuf.FormatError, KEYS.format_rsakey_from_pem, 'bad_pem')
117+
118+
# Supplying an improperly formatted PEM.
119+
# Strip the PEM header and footer.
120+
pem_header = '-----BEGIN PUBLIC KEY-----'
121+
pem_footer= '-----END PUBLIC KEY-----'
122+
self.assertRaises(tuf.FormatError, KEYS.format_rsakey_from_pem,
123+
pem[:len(pem_footer)])
124+
self.assertRaises(tuf.FormatError, KEYS.format_rsakey_from_pem,
125+
pem[len(pem_header):])
101126

102127

103128

tuf/keys.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ def format_rsakey_from_pem(pem):
977977
978978
{'keytype': 'rsa',
979979
'keyid': keyid,
980-
'keyval': {'public': '-----BEGIN RSA PUBLIC KEY----- ...',
980+
'keyval': {'public': '-----BEGIN PUBLIC KEY----- ...',
981981
'private': ''}}
982982
983983
The public portion of the RSA key is a string in PEM format.
@@ -997,7 +997,9 @@ def format_rsakey_from_pem(pem):
997997
tuf.FormatError, if 'pem' is improperly formatted.
998998
999999
<Side Effects>
1000-
None.
1000+
Only the public portion of the PEM is extracted. Leading or trailing
1001+
whitespace is not included in the PEM string stored in the rsakey object
1002+
returned.
10011003
10021004
<Returns>
10031005
A dictionary containing the RSA keys and other identifying information.
@@ -1010,16 +1012,43 @@ def format_rsakey_from_pem(pem):
10101012
# Raise 'tuf.FormatError' if the check fails.
10111013
tuf.formats.PEMRSA_SCHEMA.check_match(pem)
10121014

1013-
# Ensure the PEM string starts with the required number of dashes. Although
1014-
# a simple validation of 'pem' is performed here, a fully valid PEM string is
1015-
# needed to successfully verify signatures.
1016-
if not pem.startswith('-----'):
1017-
raise tuf.FormatError('The PEM string argument is improperly formatted.')
1015+
# Ensure the PEM string has a valid header and footer. Although a simple
1016+
# validation of 'pem' is performed here, a fully valid PEM string is
1017+
# needed to later successfully verify signatures.
1018+
pem_header = '-----BEGIN PUBLIC KEY-----'
1019+
pem_footer = '-----END PUBLIC KEY-----'
1020+
header_start = 0
1021+
footer_start = 0
1022+
1023+
# Raise error message if the expected header or footer is not found in 'pem'.
1024+
try:
1025+
header_start = pem.index(pem_header)
10181026

1027+
except ValueError:
1028+
message = \
1029+
'Required PEM header ' + repr(pem_header) + '\n not found in PEM' + \
1030+
' string: ' + repr(pem)
1031+
raise tuf.FormatError(message)
1032+
1033+
try:
1034+
# Search for 'pem_footer' after the PEM header.
1035+
footer_start = pem.index(pem_footer, header_start + len(pem_header))
1036+
1037+
except ValueError:
1038+
message = \
1039+
'Required PEM footer ' + repr(pem_footer) + '\n not found in PEM' + \
1040+
' string ' + repr(pem)
1041+
raise tuf.FormatError(message)
1042+
1043+
# Extract only the public portion of 'pem'. Leading or trailing whitespace
1044+
# is not included.
1045+
public_pem = pem[header_start:footer_start + len(pem_footer)]
1046+
1047+
10191048
# Begin building the RSA key dictionary.
10201049
rsakey_dict = {}
10211050
keytype = 'rsa'
1022-
public = pem
1051+
public = public_pem
10231052

10241053
# Generate the keyid of the RSA key. 'key_value' corresponds to the
10251054
# 'keyval' entry of the 'RSAKEY_SCHEMA' dictionary. The private key

0 commit comments

Comments
 (0)