1# Copyright (c) 2023 Lucas Dietrich <ld.adecy@gmail.com> 2# SPDX-License-Identifier: Apache-2.0 3 4import glob 5import os 6 7 8def bin2array(name, fin, fout): 9 with open(fin, 'rb') as f: 10 data = f.read() 11 12 data += b'\0' # add null terminator 13 14 with open(fout, 'w') as f: 15 f.write("#include <stdint.h>\n") 16 f.write(f"const uint8_t {name}[] = {{") 17 for i in range(0, len(data), 16): 18 f.write("\n\t") 19 f.write(", ".join(f"0x{b:02x}" for b in data[i:i+16])) 20 f.write(",") 21 f.write("\n};\n") 22 f.write(f"const uint32_t {name}_len = sizeof({name});\n") 23 24 print( 25 f"[{name.center(13, ' ')}]: {os.path.relpath(fin)} -> {os.path.relpath(fout)}") 26 27 28if __name__ == "__main__": 29 creds_dir = os.path.dirname(os.path.realpath(__file__)) 30 31 creds = glob.glob(f"{creds_dir}/*.pem.*") 32 33 cert_found, key_found = False, False 34 35 for cred in creds: 36 if cred.endswith('-certificate.pem.crt'): 37 bin2array("public_cert", cred, os.path.join(creds_dir, "cert.c")) 38 cert_found = True 39 elif cred.endswith('-private.pem.key'): 40 bin2array("private_key", cred, os.path.join(creds_dir, "key.c")) 41 key_found = True 42 43 if not cert_found: 44 print("No certificate found !") 45 if not key_found: 46 print("No private key found !") 47 48 bin2array("ca_cert", os.path.join(creds_dir, "AmazonRootCA1.pem"), 49 os.path.join(creds_dir, "ca.c")) 50