@@ -12,10 +12,43 @@ def parse_args():
12
12
parser .add_argument ('-m' , '--mode' , help = 'Mode (header, sign)' )
13
13
parser .add_argument ('-b' , '--bin' , help = 'Unsigned binary' )
14
14
parser .add_argument ('-o' , '--out' , help = 'Output file' );
15
+ parser .add_argument ('-l' , '--legacy' , help = 'Legacy output file' );
15
16
parser .add_argument ('-p' , '--publickey' , help = 'Public key file' );
16
17
parser .add_argument ('-s' , '--privatekey' , help = 'Private(secret) key file' );
17
18
return parser .parse_args ()
18
19
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 " )
19
52
20
53
def main ():
21
54
args = parse_args ()
@@ -51,18 +84,12 @@ def main():
51
84
try :
52
85
with open (args .bin , "rb" ) as b :
53
86
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
+
66
93
except Exception as e :
67
94
sys .stderr .write (str (e ))
68
95
sys .stderr .write ("Not signing the generated binary\n " )
0 commit comments