2019-09-03 13:19:48 -03:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import nacl.encoding
|
|
|
|
import nacl.signing
|
|
|
|
import nacl.hash
|
|
|
|
import struct
|
|
|
|
import binascii
|
|
|
|
import json
|
|
|
|
import time
|
|
|
|
import argparse
|
|
|
|
from pathlib import Path
|
|
|
|
import sys
|
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
def make_public_key_h_file(signing_key,key_name):
|
2019-09-03 13:19:48 -03:00
|
|
|
"""
|
|
|
|
This file generate the public key header file
|
|
|
|
to be included into the bootloader build.
|
|
|
|
"""
|
2020-10-21 05:52:44 -03:00
|
|
|
public_key_c='\n'
|
2019-09-03 13:19:48 -03:00
|
|
|
for i,c in enumerate(signing_key.verify_key.encode(encoder=nacl.encoding.RawEncoder)):
|
|
|
|
public_key_c+= hex(c)
|
|
|
|
public_key_c+= ', '
|
|
|
|
if((i+1)%8==0):
|
|
|
|
public_key_c+= '\n'
|
2020-10-21 05:52:44 -03:00
|
|
|
with open(key_name+'.pub' ,mode='w') as f:
|
2019-09-03 13:19:48 -03:00
|
|
|
f.write("//Public key to verify signed binaries")
|
|
|
|
f.write(public_key_c)
|
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
def make_key_file(signing_key, key_name):
|
2019-09-03 13:19:48 -03:00
|
|
|
"""
|
|
|
|
Writes the key.json file.
|
|
|
|
Attention do not override your existing key files.
|
|
|
|
Do not publish your private key!!
|
|
|
|
"""
|
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
key_file = Path(key_name+'.json')
|
2019-09-03 13:19:48 -03:00
|
|
|
if key_file.is_file():
|
2020-10-21 05:52:44 -03:00
|
|
|
print("ATTENTION: key.json already exists, are you sure you want to overwrite it?")
|
2019-09-03 13:19:48 -03:00
|
|
|
print("Remove file and run script again.")
|
|
|
|
print("Script aborted!")
|
2020-10-21 05:52:44 -03:00
|
|
|
sys.exit(1)
|
2019-09-03 13:19:48 -03:00
|
|
|
|
|
|
|
keys={}
|
|
|
|
keys["date"] = time.asctime()
|
|
|
|
keys["public"] = (signing_key.verify_key.encode(encoder=nacl.encoding.HexEncoder)).decode()
|
|
|
|
keys["private"] = binascii.hexlify(signing_key._seed).decode()
|
2020-10-21 05:52:44 -03:00
|
|
|
#print (keys)
|
|
|
|
with open(key_name+'.json', "w") as write_file:
|
2019-09-03 13:19:48 -03:00
|
|
|
json.dump(keys, write_file)
|
2020-10-21 05:52:44 -03:00
|
|
|
return keys
|
2019-09-03 13:19:48 -03:00
|
|
|
|
|
|
|
def ed25519_sign(private_key, signee_bin):
|
|
|
|
"""
|
2020-10-21 05:52:44 -03:00
|
|
|
This function creates the signature. It takes the private key and the binary file
|
|
|
|
and returns the tuple (signature, public key)
|
2019-09-03 13:19:48 -03:00
|
|
|
"""
|
|
|
|
|
|
|
|
signing_key = nacl.signing.SigningKey(private_key, encoder=nacl.encoding.HexEncoder)
|
|
|
|
|
|
|
|
# Sign a message with the signing key
|
2020-10-21 05:52:44 -03:00
|
|
|
signed = signing_key.sign(signee_bin,encoder=nacl.encoding.RawEncoder)
|
2019-09-03 13:19:48 -03:00
|
|
|
|
|
|
|
# Obtain the verify key for a given signing key
|
|
|
|
verify_key = signing_key.verify_key
|
|
|
|
|
|
|
|
# Serialize the verify key to send it to a third party
|
|
|
|
verify_key_hex = verify_key.encode(encoder=nacl.encoding.HexEncoder)
|
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
return signed.signature, verify_key_hex
|
2019-09-03 13:19:48 -03:00
|
|
|
|
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
def sign(bin_file_path, key_file_path=None, generated_key_file=None):
|
2019-09-03 13:19:48 -03:00
|
|
|
"""
|
|
|
|
reads the binary file and the key file.
|
|
|
|
If the key file does not exist, it generates a
|
|
|
|
new key file.
|
|
|
|
"""
|
|
|
|
|
|
|
|
with open(bin_file_path,mode='rb') as f:
|
|
|
|
signee_bin = f.read()
|
2020-10-21 05:52:44 -03:00
|
|
|
# Align to 4 bytes. Signature always starts at
|
|
|
|
# 4 byte aligned address, but the signee size
|
|
|
|
# might not be aligned
|
2021-05-28 10:13:51 -03:00
|
|
|
if len(signee_bin)%4 != 0:
|
|
|
|
signee_bin += bytearray(b'\xff')*(4-len(signee_bin)%4)
|
2019-09-03 13:19:48 -03:00
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
try:
|
|
|
|
with open(key_file_path,mode='r') as f:
|
|
|
|
keys = json.load(f)
|
|
|
|
#print(keys)
|
|
|
|
except:
|
|
|
|
print('ERROR: Key file',key_file_path,'not found')
|
|
|
|
sys.exit(1)
|
2019-09-03 13:19:48 -03:00
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
signature, public_key = ed25519_sign(keys["private"], signee_bin)
|
2019-09-03 13:19:48 -03:00
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
# Do a sanity check. This type of signature is always 64 bytes long
|
|
|
|
assert len(signature) == 64
|
2019-09-03 13:19:48 -03:00
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
# Print out the signing information
|
|
|
|
print("Binary \"%s\" signed."%bin_file_path)
|
|
|
|
print("Signature:",binascii.hexlify(signature))
|
|
|
|
print("Public key:",binascii.hexlify(public_key))
|
2019-09-03 13:19:48 -03:00
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
return signee_bin + signature, public_key
|
|
|
|
|
|
|
|
def generate_key(key_file):
|
2019-09-03 13:19:48 -03:00
|
|
|
"""
|
2020-10-21 05:52:44 -03:00
|
|
|
Generate two files:
|
|
|
|
"key_file.pub" containing the public key in C-format to be included in the bootloader build
|
|
|
|
"key_file.json, containt both private and public key.
|
2019-09-03 13:19:48 -03:00
|
|
|
Do not leak or loose the key file. This is mandatory for signing
|
|
|
|
all future binaries you want to deploy!
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Generate a new random signing key
|
|
|
|
signing_key = nacl.signing.SigningKey.generate()
|
|
|
|
# Serialize the verify key to send it to a third party
|
|
|
|
verify_key_hex = signing_key.verify_key.encode(encoder=nacl.encoding.HexEncoder)
|
|
|
|
print("public key :",verify_key_hex)
|
|
|
|
|
|
|
|
private_key_hex=binascii.hexlify(signing_key._seed)
|
|
|
|
print("private key :",private_key_hex)
|
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
keys = make_key_file(signing_key,key_file)
|
|
|
|
make_public_key_h_file(signing_key,key_file)
|
|
|
|
return keys
|
2019-09-03 13:19:48 -03:00
|
|
|
|
|
|
|
if(__name__ == "__main__"):
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="""CLI tool to calculate and add signature to px4. bin files\n
|
|
|
|
if given it takes an existing key file, else it generate new keys""",
|
|
|
|
epilog="Output: SignedBin.bin and a key.json file")
|
2020-10-21 05:52:44 -03:00
|
|
|
parser.add_argument("signee", help=".bin file to add signature", nargs='?', default=None)
|
|
|
|
parser.add_argument("signed", help="signed output .bin", nargs='?', default=None)
|
|
|
|
|
2021-05-28 09:36:37 -03:00
|
|
|
parser.add_argument("--key", help="key.json file", default="Tools/test_keys/test_keys.json")
|
2020-10-21 05:52:44 -03:00
|
|
|
parser.add_argument("--rdct", help="binary R&D certificate file", default=None)
|
|
|
|
parser.add_argument("--genkey", help="new generated key", default=None)
|
2019-09-03 13:19:48 -03:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2020-10-21 05:52:44 -03:00
|
|
|
# Only generate a key pair, don't sign
|
|
|
|
if args.genkey:
|
|
|
|
# Only create a key file, don't sign
|
|
|
|
generate_key(args.genkey)
|
|
|
|
print('New key file generated:',args.genkey)
|
|
|
|
sys.exit(0);
|
|
|
|
|
|
|
|
# Check that both signee and signed exist
|
|
|
|
if not args.signee or not args.signed:
|
|
|
|
print("ERROR: Must either provide file names for both signee and signed")
|
|
|
|
print(" or --genkey [key] to generate a new key pair")
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Issue a warning when signing with testing key
|
2021-05-28 09:36:37 -03:00
|
|
|
if args.key=='Tools/test_keys/test_keys.json':
|
2020-10-21 05:52:44 -03:00
|
|
|
print("WARNING: Signing with PX4 test key")
|
|
|
|
|
|
|
|
# Sign the binary
|
|
|
|
signed, public_key = sign(args.signee, args.key, args.genkey)
|
|
|
|
|
|
|
|
with open(args.signed, mode='wb') as fs:
|
|
|
|
# Write signed binary
|
|
|
|
fs.write(signed)
|
|
|
|
|
|
|
|
# Append rdcert if given
|
|
|
|
try:
|
|
|
|
with open(args.rdct ,mode='rb') as f:
|
|
|
|
with open(args.signed, mode='ab') as fs:
|
|
|
|
fs.write(f.read())
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|