Skip to content

Commit

Permalink
Make error messages more readable.
Browse files Browse the repository at this point in the history
  • Loading branch information
gareth-palmer committed Jul 15, 2024
1 parent 7106a24 commit af7b5fa
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
32 changes: 16 additions & 16 deletions enccnf
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def parse_enc_file(enc_file, tftp_certificate_file):
tlv_index += 3

if tlv_tag != HEADER_VERSION:
raise ProgramError(f'Tag is not HEADER_VERSION: {tlv_tag}')
raise ProgramError(f'Tag is not header version: {tlv_tag}')

(major, minor) = struct.unpack_from('B B', tlv_data, tlv_index)
tlv_index += tlv_length
Expand All @@ -86,16 +86,16 @@ def parse_enc_file(enc_file, tftp_certificate_file):
tlv_index += 3

if tlv_tag != HEADER_LENGTH:
raise ProgramError(f'Tag is not HEADER_LENGTH: {tlv_tag}')
raise ProgramError(f'Tag is not header length: {tlv_tag}')

(header_length,) = struct.unpack_from('> H', tlv_data, tlv_index)
tlv_index += tlv_length

print(f'Header Length: {header_length} bytes')

hash_algorithm = None
signature_index = 0
signature_length = 0
signature_index = None
signature_length = None

while tlv_index < header_length:
tlv_tag = tlv_data[tlv_index]
Expand Down Expand Up @@ -198,20 +198,20 @@ def parse_enc_file(enc_file, tftp_certificate_file):
print(f'Encryption Digest: {encryption_hash}')

else:
raise ProgramError(f'Unknown HEADER tag: {tlv_tag}')
raise ProgramError(f'Unknown header tag: {tlv_tag}')

tlv_index += tlv_length

print('')

if hash_algorithm is None:
raise ProgramError('Missing HEADER_HASH_ALGORITHM')
raise ProgramError('Missing header hash algorithm')

if hash_algorithm not in (HASH_SHA1, HASH_SHA512):
raise ProgramError(f'Unsupported HEADER_HASH_ALGORITHM: {hash_algorithm}')
raise ProgramError(f'Unsupported header hash algorithm: {hash_algorithm}')

if signature_index is None:
raise ProgramError('Missing HEADER_SIGNATURE')
raise ProgramError('Missing header signature')

if tftp_certificate_file is None:
print('No certificate specified, unable to check signature')
Expand Down Expand Up @@ -271,15 +271,15 @@ def remove_enc_file(enc_file, private_key_file):
tlv_index += 3

if tlv_tag != HEADER_VERSION:
raise ProgramError(f'Tag is not HEADER_VERSION: {tlv_tag}')
raise ProgramError(f'Tag is not header version: {tlv_tag}')

tlv_index += tlv_length

(tlv_tag, tlv_length) = struct.unpack_from('> B H', tlv_data, tlv_index)
tlv_index += 3

if tlv_tag != HEADER_LENGTH:
raise ProgramError(f'Tag is not HEADER_LENGTH: {tlv_tag}')
raise ProgramError(f'Tag is not header length: {tlv_tag}')

(header_length,) = struct.unpack_from('> H', tlv_data, tlv_index)
tlv_index += tlv_length
Expand Down Expand Up @@ -327,24 +327,24 @@ def remove_enc_file(enc_file, private_key_file):
encryption_hash = binascii.hexlify(encryption_hash).decode('utf-8')

else:
raise ProgramError(f'Unknown HEADER tag: {tlv_tag}')
raise ProgramError(f'Unknown header tag: {tlv_tag}')

tlv_index += tlv_length

if encryption_iv is None:
raise ProgramError('Missing HEADER_ENCRYPTION_IV')
raise ProgramError('Missing header encryption IV')

if encryption_key is None:
raise ProgramError('Missing HEADER_ENCRYPTION_KEY')
raise ProgramError('Missing header encryption key')

if encryption_hash_algorithm is None:
raise ProgramError(f'Missing HEADER_ENCRYPTION_HASH_ALGORITHM: {encryption_hash_algorithm}')
raise ProgramError(f'Missing header encryption hash algorithm: {encryption_hash_algorithm}')

if encryption_hash_algorithm not in (HASH_SHA1, HASH_SHA512):
raise ProgramError(f'Unsupported HEADER_HASH_ALGORITHM: {encryption_hash_algorithm}')
raise ProgramError(f'Unsupported header encryption hash algorithm: {encryption_hash_algorithm}')

if encryption_hash is None:
raise ProgramError('Missing HEADER_ENCRYPTION_HASH')
raise ProgramError('Missing header encryption hash')

xml = tlv_data[header_length:]

Expand Down
20 changes: 10 additions & 10 deletions sgnfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def parse_sgn_file(sgn_file, tftp_certificate_file):
tlv_index += 3

if tlv_tag != HEADER_VERSION:
raise ProgramError(f'Tag is not HEADER_VERSION: {tlv_tag}')
raise ProgramError(f'Tag is not header version: {tlv_tag}')

(major, minor) = struct.unpack_from('B B', tlv_data, tlv_index)
tlv_index += tlv_length
Expand All @@ -72,16 +72,16 @@ def parse_sgn_file(sgn_file, tftp_certificate_file):
tlv_index += 3

if tlv_tag != HEADER_LENGTH:
raise ProgramError(f'Tag is not HEADER_LENGTH: {tlv_tag}')
raise ProgramError(f'Tag is not header length: {tlv_tag}')

(header_length,) = struct.unpack_from('> H', tlv_data, tlv_index)
tlv_index += tlv_length

print(f'Header Length: {header_length} bytes')

hash_algorithm = None
signature_index = 0
signature_length = 0
signature_index = None
signature_length = None

while tlv_index < header_length:
tlv_tag = tlv_data[tlv_index]
Expand Down Expand Up @@ -150,20 +150,20 @@ def parse_sgn_file(sgn_file, tftp_certificate_file):
print(timestamp.strftime('Timestamp: %Y-%m-%d %H:%M:%S'))

else:
raise ProgramError(f'Unknown HEADER tag: {tlv_tag}')
raise ProgramError(f'Unknown header tag: {tlv_tag}')

tlv_index += tlv_length

print('')

if hash_algorithm is None:
raise ProgramError('Missing HEADER_HASH_ALGORITHM')
raise ProgramError('Missing header hash algorithm')

if hash_algorithm not in (HASH_SHA1, HASH_SHA512):
raise ProgramError(f'Unsupported HEADER_HASH_ALGORITHM: {hash_algorithm}')
raise ProgramError(f'Unsupported header hash algorithm: {hash_algorithm}')

if signature_index is None:
raise ProgramError('Missing HEADER_SIGNATURE')
raise ProgramError('Missing header signature')

if tftp_certificate_file is None:
print('No certificate specified, unable to check signature')
Expand Down Expand Up @@ -208,7 +208,7 @@ def remove_sgn_file(sgn_file):
tlv_index += 3

if tlv_tag != HEADER_VERSION:
raise ProgramError(f'Tag is not HEADER_VERSION: {tlv_tag}')
raise ProgramError(f'Tag is not header version: {tlv_tag}')

# Skip version
tlv_index += tlv_length
Expand All @@ -217,7 +217,7 @@ def remove_sgn_file(sgn_file):
tlv_index += 3

if tlv_tag != HEADER_LENGTH:
raise ProgramError(f'Tag is not HEADER_LENGTH: {tlv_tag}')
raise ProgramError(f'Tag is not header length: {tlv_tag}')

(header_length,) = struct.unpack_from('> H', tlv_data, tlv_index)
tlv_index += tlv_length
Expand Down
4 changes: 2 additions & 2 deletions stripsgn
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def strip_sgn_file(sgn_file):
tlv_index += 3

if tlv_tag != HEADER_VERSION:
raise ProgramError(f'Tag is not HEADER_VERSION: {tlv_tag}')
raise ProgramError(f'Tag is not header version: {tlv_tag}')

# Skip version
tlv_index += tlv_length
Expand All @@ -43,7 +43,7 @@ def strip_sgn_file(sgn_file):
tlv_index += 3

if tlv_tag != HEADER_LENGTH:
raise ProgramError(f'Tag is not HEADER_LENGTH: {tlv_tag}')
raise ProgramError(f'Tag is not header length: {tlv_tag}')

(header_length,) = struct.unpack_from('> H', tlv_data, tlv_index)
tlv_index += tlv_length
Expand Down
33 changes: 16 additions & 17 deletions tlvfile
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def parse_tlv_file(tlv_file):
tlv_index += 3

if tlv_tag != HEADER_VERSION:
raise ProgramError(f'Tag is not HEADER_VERSION: {tlv_tag}')
raise ProgramError(f'Tag is not header version: {tlv_tag}')

(major, minor) = struct.unpack_from('B B', tlv_data, tlv_index)
tlv_index += tlv_length
Expand All @@ -94,17 +94,18 @@ def parse_tlv_file(tlv_file):
tlv_index += 3

if tlv_tag != HEADER_LENGTH:
raise ProgramError(f'Tag is not HEADER_LENGTH: {tlv_tag}')
raise ProgramError(f'Tag is not header length: {tlv_tag}')

(header_length,) = struct.unpack_from('> H', tlv_data, tlv_index)
tlv_index += tlv_length

print(f'Header Length: {header_length} bytes')

sast_serial_number = None
sast_certificate = None
hash_algorithm = None
signature_index = 0
signature_length = 0
signature_index = None
signature_length = None

while tlv_index < header_length:
tlv_tag = tlv_data[tlv_index]
Expand Down Expand Up @@ -180,25 +181,23 @@ def parse_tlv_file(tlv_file):
print(timestamp.strftime('Timestamp: %Y-%m-%d %H:%M:%S'))

else:
raise ProgramError(f'Unknown HEADER tag: {tlv_tag}')
raise ProgramError(f'Unknown header tag: {tlv_tag}')

tlv_index += tlv_length

print('')

if sast_serial_number is None:
raise ProgramError('Missing HEADER_SERIAL_NUMBER')
raise ProgramError('Missing header serial number')

if hash_algorithm is None:
raise ProgramError('Missing HEADER_HASH_ALGORITHM')
raise ProgramError('Missing header hash algorithm')

if hash_algorithm not in (HASH_SHA1, HASH_SHA512):
raise ProgramError(f'Unsupported HEADER_HASH_ALGORITHM: {hash_algorithm}')
raise ProgramError(f'Unsupported header hash algorithm: {hash_algorithm}')

if signature_index is None:
raise ProgramError('Missing HEADER_SIGNATURE')

sast_certificate = None
raise ProgramError('Missing header signature')

while tlv_index < len(tlv_data):
record_index = tlv_index
Expand All @@ -207,7 +206,7 @@ def parse_tlv_file(tlv_file):
tlv_index += 3

if tlv_tag != RECORD_LENGTH:
raise ProgramError(f'Tag is not RECORD_LENGTH: {tlv_tag}')
raise ProgramError(f'Tag is not record length: {tlv_tag}')

(record_length,) = struct.unpack_from('> H', tlv_data, tlv_index)
tlv_index += tlv_length
Expand Down Expand Up @@ -295,7 +294,7 @@ def parse_tlv_file(tlv_file):
elif isinstance(public_key, ec.EllipticCurvePublicKey):
print('Key Algorithm: EC')
else:
raise ProgramError('Unsupported RECORD_CERTIFICATE type')
raise ProgramError('Unsupported record certificate type')

certificate_hash = certificate.fingerprint(hashes.SHA1())
certificate_hash = binascii.hexlify(certificate_hash).decode('utf-8')
Expand All @@ -312,7 +311,7 @@ def parse_tlv_file(tlv_file):
pass

else:
raise ProgramError(f'Unknown RECORD tag: {tlv_tag}')
raise ProgramError(f'Unknown record tag: {tlv_tag}')

tlv_index += tlv_length

Expand All @@ -322,7 +321,7 @@ def parse_tlv_file(tlv_file):
print('')

if sast_certificate is None:
raise ProgramError('Missing RECORD with ROLE_SAST')
raise ProgramError('Missing record with role SAST')

public_key = sast_certificate.public_key()

Expand Down Expand Up @@ -474,7 +473,7 @@ def build_tlv_file(tlv_file, sast_certificate_file, version, hash_algorithm, fil
elif role == 'TVS':
tlv_data += struct.pack('> H', ROLE_TVS)
else:
raise ProgramError(f'Unsupported RECORD_ROLE: {role}')
raise ProgramError(f'Unsupported record role: {role}')

issuer_name = ''

Expand All @@ -495,7 +494,7 @@ def build_tlv_file(tlv_file, sast_certificate_file, version, hash_algorithm, fil
elif isinstance(public_key, ec.EllipticCurvePublicKey):
public_key = public_key.public_bytes(serialization.Encoding.X962, serialization.PublicFormat.UncompressedPoint)
else:
raise ProgramError('Unsupported certificate PUBLIC_KEY type')
raise ProgramError('Unsupported certificate public-key type')

tlv_data += struct.pack('> B H', RECORD_PUBLIC_KEY, len(public_key))
tlv_data += public_key
Expand Down

0 comments on commit af7b5fa

Please sign in to comment.