#!/usr/bin/env python3 # -*- coding: utf-8 -*- # import argparse import hashlib import os import struct import subprocess import sys def parse_args(): parser = argparse.ArgumentParser(description='Binary signing tool') parser.add_argument('-m', '--mode', help='Mode (header, sign)') parser.add_argument('-b', '--bin', help='Unsigned binary') parser.add_argument('-o', '--out', help='Output file'); parser.add_argument('-l', '--legacy', help='Legacy output file'); parser.add_argument('-p', '--publickey', help='Public key file'); parser.add_argument('-s', '--privatekey', help='Private(secret) key file'); return parser.parse_args() def sign_and_write(data, priv_key, out_file): """Signs the data (bytes) with the private key (file path).""" """Save the signed firmware to out_file (file path).""" signcmd = [ 'openssl', 'dgst', '-sha256', '-sign', priv_key ] proc = subprocess.Popen(signcmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) signout, signerr = proc.communicate(input=data) if proc.returncode: sys.stderr.write("OpenSSL returned an error signing the binary: " + str(proc.returncode) + "\nSTDERR: " + str(signerr)) else: with open(out_file, "wb") as out: out.write(data) out.write(signout) out.write(struct.pack("