Skip to content

Commit

Permalink
More improvement of INFO field building.
Browse files Browse the repository at this point in the history
  • Loading branch information
gareth-palmer committed Jul 12, 2024
1 parent f4cad85 commit 7106a24
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions enccnf
Original file line number Diff line number Diff line change
Expand Up @@ -617,17 +617,17 @@ def build_enc_file(cnf_file, tftp_certificate_file, certificate_file, hash_algor
serial_number = certificate.serial_number
serial_number = serial_number.to_bytes((serial_number.bit_length() + 7) // 8, byteorder = 'big')

signer_info = 3 + len(signer_name) + 3 + len(serial_number) + 3 + len(issuer_name)
tlv_data += struct.pack('> B H', HEADER_SIGNER_INFO, signer_info)
signer_info = struct.pack('> B H', HEADER_SIGNER_NAME, len(signer_name))
signer_info += signer_name

tlv_data += struct.pack('> B H', HEADER_SIGNER_NAME, len(signer_name))
tlv_data += signer_name
signer_info += struct.pack('> B H', HEADER_SERIAL_NUMBER, len(serial_number))
signer_info += serial_number

tlv_data += struct.pack('> B H', HEADER_SERIAL_NUMBER, len(serial_number))
tlv_data += serial_number
signer_info += struct.pack('> B H', HEADER_ISSUER_NAME, len(issuer_name))
signer_info += issuer_name

tlv_data += struct.pack('> B H', HEADER_ISSUER_NAME, len(issuer_name))
tlv_data += issuer_name
tlv_data += struct.pack('> B H', HEADER_SIGNER_INFO, len(signer_info))
tlv_data += signer_info

tlv_data += struct.pack('> B H', HEADER_SIGNATURE_INFO, 15)
tlv_data += struct.pack('> B H B', HEADER_HASH_ALGORITHM, 1, HASH_SHA512 if hash_algorithm == 'sha512' else HASH_SHA1)
Expand All @@ -636,7 +636,9 @@ def build_enc_file(cnf_file, tftp_certificate_file, certificate_file, hash_algor
tlv_data += struct.pack('> B H B', HEADER_SIGNATURE_ALGORITHM, 1, 0)
tlv_data += struct.pack('> B H B', HEADER_SIGNATURE_MODULUS, 1, [64, 128, 256, 512].index(signature_length))

# Index where the signature will be inserted
signature_index = len(tlv_data)

filename = os.path.basename(sgn_file).encode('utf-8') + b'\x00'

tlv_data += struct.pack('> B H', HEADER_FILENAME, len(filename))
Expand All @@ -659,7 +661,8 @@ def build_enc_file(cnf_file, tftp_certificate_file, certificate_file, hash_algor
try:
with open(sgn_file, 'wb') as file:
file.write(tlv_data[:signature_index])
file.write(struct.pack('> B H', HEADER_SIGNATURE, len(signature)) + signature)
file.write(struct.pack('> B H', HEADER_SIGNATURE, len(signature)))
file.write(signature)
file.write(tlv_data[signature_index:])

except (PermissionError, IsADirectoryError) as error:
Expand Down

0 comments on commit 7106a24

Please sign in to comment.