@@ -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