Skip to content

Commit 0ae91ae

Browse files
Earle F. Philhower, IIIEarle F. Philhower, III
Earle F. Philhower, III
authored and
Earle F. Philhower, III
committed
Automatically include validation in updater
1 parent b9344f6 commit 0ae91ae

File tree

6 files changed

+75
-6
lines changed

6 files changed

+75
-6
lines changed

cores/esp8266/Updater.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
//#define DEBUG_UPDATER Serial
88

9+
#include <Updater_Signing.h>
10+
#ifndef ARDUINO_SIGNING
11+
#define ARDUINO_SIGNING 0
12+
#endif
13+
14+
#if ARDUINO_SIGNING
15+
#include "../../libraries/ESP8266WiFi/src/BearSSLHelpers.h"
16+
static BearSSLPublicKey signPubKey(signing_pubkey);
17+
static BearSSLHashSHA256 hash;
18+
static BearSSLSigningVerifier sign(&signPubKey);
19+
#endif
20+
921
extern "C" {
1022
#include "c_types.h"
1123
#include "spi_flash.h"
@@ -26,6 +38,9 @@ UpdaterClass::UpdaterClass()
2638
, _hash(nullptr)
2739
, _verify(nullptr)
2840
{
41+
#if ARDUINO_SIGNING
42+
installSignature(&hash, &sign);
43+
#endif
2944
}
3045

3146
void UpdaterClass::_reset() {

cores/esp8266/Updater_Signing.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file will be overridden when automatic signing is used.
2+
// By default, no signing.
3+
#define ARDUINO_SIGNING 0

libraries/ESP8266httpUpdate/examples/httpUpdateSigned/httpUpdateSigned.ino

+15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@
1818

1919
ESP8266WiFiMulti WiFiMulti;
2020

21+
#define MANUAL_SIGNING 0
22+
23+
// This example is now configured to use the automated signing support
24+
// present in the Arduino IDE by having a "private.key" and "public.key"
25+
// in the sketch folder. You can also programmatically enable signing
26+
// using the method shown here.
27+
2128
// This key is taken from the server public certificate in BearSSL examples
2229
// You should make your own private/public key pair and guard the private
2330
// key (never upload it to the 8266).
@@ -32,9 +39,11 @@ cuWNgTnDQd6KUzV0E4it2fNG+cHN4kEvofN6gHx8IbOrXwFttlpAH/o7bcfCnUVh
3239
TQIDAQAB
3340
-----END PUBLIC KEY-----
3441
)EOF";
42+
#if MANUAL_SIGNING
3543
BearSSLPublicKey *signPubKey = nullptr;
3644
BearSSLHashSHA256 *hash;
3745
BearSSLSigningVerifier *sign;
46+
#endif
3847

3948
void setup() {
4049

@@ -54,9 +63,11 @@ void setup() {
5463
WiFi.mode(WIFI_STA);
5564
WiFiMulti.addAP("SSID", "PASS");
5665

66+
#if MANUAL_SIGNING
5767
signPubKey = new BearSSLPublicKey(pubkey);
5868
hash = new BearSSLHashSHA256();
5969
sign = new BearSSLSigningVerifier(signPubKey);
70+
#endif
6071
}
6172

6273

@@ -66,8 +77,12 @@ void loop() {
6677

6778
WiFiClient client;
6879

80+
#if MANUAL_SIGNING
6981
// Ensure all updates are signed appropriately. W/o this call, all will be accepted.
7082
Update.installSignature(hash, sign);
83+
#endif
84+
// If the key files are present in the build directory, signing will be
85+
// enabled using them automatically
7186

7287
ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW);
7388

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIIEogIBAAKCAQEAu1Pt7yEk/xI+6cozLj5Bu4xV8gXDXcHS0rSJFfl4wBTk4UXp
3+
aJRaLfR1k0juEEa5LBRZaoA0iLj2e6kfCibONx0VVoWmeqN2HBc3zkA1eqCksI0Q
4+
Uudzto4KhKHp0odiZ2zo6c/2Tn1zqD/m3OLoSjVTbsJmGuwx8RGMBXozpg/uL0hH
5+
flihX+HND4Xfw92QXv7SaPBhgvM9xyRxn0/w3J2nNjtuPuVN5vcQkd8ncMexVfy9
6+
AWp+HSA5AT5N8CJ/EeIsdDMY1US28bUePzj1WIo75bZHKZNFw/iXe2xoPpm74qri
7+
MNSlW2craFP2K3KYnI28vJeUU6t9I6LS9zt2zQIDAQABAoIBAE5GpuDKb8Qp4qIc
8+
fMBxAVSWMn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JAoGAbehNOUwEzmUKkfxf+279kBkgabcQ3NTaeSx0QOnI9KWHFGLYLQk9cMSu
25+
maQJ1TYpbIoP1njzJ4bI2tynhwEuSMEhh4afP6U5H10NJX4PqSd0Rqc1vSJYcszr
26+
5mUWil8FfbCBZ8jod2NQ55KYMVY5CphCqaK/s2bw2pvIR3uqJGg=
27+
-----END RSA PRIVATE KEY-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAu1Pt7yEk/xI+6cozLj5B
3+
u4xV8gXDXcHS0rSJFfl4wBTk4UXpaJRaLfR1k0juEEa5LBRZaoA0iLj2e6kfCibO
4+
Nx0VVoWmeqN2HBc3zkA1eqCksI0QUudzto4KhKHp0odiZ2zo6c/2Tn1zqD/m3OLo
5+
SjVTbsJmGuwx8RGMBXozpg/uL0hHflihX+HND4Xfw92QXv7SaPBhgvM9xyRxn0/w
6+
3J2nNjtuPuVN5vcQkd8ncMexVfy9AWp+HSA5AT5N8CJ/EeIsdDMY1US28bUePzj1
7+
WIo75bZHKZNFw/iXe2xoPpm74qriMNSlW2craFP2K3KYnI28vJeUU6t9I6LS9zt2
8+
zQIDAQAB
9+
-----END PUBLIC KEY-----

tools/signing.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ def main():
3030
val += "0x%02x, \n" % ord(i)
3131
val = val[:-3]
3232
val +="\n};\n"
33-
print "Enabling binary signing\n"
33+
sys.stderr.write("Enabling binary signing\n")
3434
except:
35-
print "Not enabling binary signing\n"
35+
sys.stderr.write("Not enabling binary signing\n")
3636
val += "#define ARDUINO_SIGNING 0\n"
3737
with open(args.out, "w") as f:
3838
f.write(val)
@@ -43,20 +43,20 @@ def main():
4343
with open(args.bin, "rb") as b:
4444
bin = b.read()
4545
sha256 = hashlib.sha256(bin)
46-
print "Binary SHA256 = " + sha256.hexdigest()
46+
sys.stderr.write("Signing SHA256 = " + sha256.hexdigest() + "\n");
4747
signcmd = [ 'openssl', 'rsautl', '-sign', '-inkey', args.privatekey ]
4848
proc = subprocess.Popen(signcmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
4949
signout = proc.communicate(input=sha256.digest())[0]
5050
with open(args.out, "wb") as out:
5151
out.write(bin)
5252
out.write(signout)
5353
out.write(b'\x00\x01\x00\x00')
54-
print "Signed binary: " + args.out
54+
sys.stderr.write("Signed binary: " + args.out + "\n")
5555
except:
56-
print "Not signing the generated binary\n"
56+
sys.stderr.write("Not signing the generated binary\n")
5757
return 0
5858
else:
59-
print "ERROR: Mode not specified as header or sign\n"
59+
sys.stderr.write("ERROR: Mode not specified as header or sign\n")
6060

6161
if __name__ == '__main__':
6262
sys.exit(main())

0 commit comments

Comments
 (0)