@@ -12,10 +12,43 @@ def parse_args():
1212 parser .add_argument ('-m' , '--mode' , help = 'Mode (header, sign)' )
1313 parser .add_argument ('-b' , '--bin' , help = 'Unsigned binary' )
1414 parser .add_argument ('-o' , '--out' , help = 'Output file' );
15+ parser .add_argument ('-l' , '--legacy' , help = 'Legacy output file' );
1516 parser .add_argument ('-p' , '--publickey' , help = 'Public key file' );
1617 parser .add_argument ('-s' , '--privatekey' , help = 'Private(secret) key file' );
1718 return parser .parse_args ()
1819
20+ def sign_and_write (data , priv_key , out_file ):
21+ """Signs the data (bytes) with the private key (file path)."""
22+ """Save the signed firmware to out_file (file path)."""
23+
24+ signcmd = [ 'openssl' , 'dgst' , '-sha256' , '-sign' , priv_key ]
25+ proc = subprocess .Popen (signcmd , stdout = subprocess .PIPE , stdin = subprocess .PIPE , stderr = subprocess .PIPE )
26+ signout , signerr = proc .communicate (input = data )
27+ if proc .returncode :
28+ sys .stderr .write ("OpenSSL returned an error signing the binary: " + str (proc .returncode ) + "\n STDERR: " + str (signerr ))
29+ else :
30+ with open (out_file , "wb" ) as out :
31+ out .write (data )
32+ out .write (signout )
33+ out .write (b'\x00 \x01 \x00 \x00 ' )
34+ sys .stderr .write ("Signed binary: " + out_file + "\n " )
35+
36+ def sign_and_write_legacy (data , priv_key , out_file ):
37+ """Signs the data (bytes) with the private key (file path)."""
38+ """Save the signed firmware to out_file (file path)."""
39+
40+ sha256 = hashlib .sha256 (data )
41+ signcmd = [ 'openssl' , 'rsautl' , '-sign' , '-inkey' , priv_key ]
42+ proc = subprocess .Popen (signcmd , stdout = subprocess .PIPE , stdin = subprocess .PIPE , stderr = subprocess .PIPE )
43+ signout , signerr = proc .communicate (input = sha256 .digest ())
44+ if proc .returncode :
45+ sys .stderr .write ("OpenSSL returned an error legacy signing the binary: " + str (proc .returncode ) + "\n STDERR: " + str (signerr ))
46+ else :
47+ with open (out_file , "wb" ) as out :
48+ out .write (data )
49+ out .write (signout )
50+ out .write (b'\x00 \x01 \x00 \x00 ' )
51+ sys .stderr .write ("Legacy signed binary: " + out_file + "\n " )
1952
2053def main ():
2154 args = parse_args ()
@@ -51,18 +84,12 @@ def main():
5184 try :
5285 with open (args .bin , "rb" ) as b :
5386 bin = b .read ()
54- sha256 = hashlib .sha256 (bin )
55- signcmd = [ 'openssl' , 'rsautl' , '-sign' , '-inkey' , args .privatekey ]
56- proc = subprocess .Popen (signcmd , stdout = subprocess .PIPE , stdin = subprocess .PIPE , stderr = subprocess .PIPE )
57- signout , signerr = proc .communicate (input = sha256 .digest ())
58- if proc .returncode :
59- sys .stderr .write ("OpenSSL returned an error signing the binary: " + str (proc .returncode ) + "\n STDERR: " + str (signerr ))
60- else :
61- with open (args .out , "wb" ) as out :
62- out .write (bin )
63- out .write (signout )
64- out .write (b'\x00 \x01 \x00 \x00 ' )
65- sys .stderr .write ("Signed binary: " + args .out + "\n " )
87+
88+ sign_and_write (bin , args .privatekey , args .out )
89+
90+ if args .legacy :
91+ sign_and_write_legacy (bin , args .privatekey , args .legacy )
92+
6693 except Exception as e :
6794 sys .stderr .write (str (e ))
6895 sys .stderr .write ("Not signing the generated binary\n " )
0 commit comments