1
- /* *
2
- httpUpdate.ino
3
-
4
- Created on: 27.11.2015
5
-
1
+ /*
2
+ httpUpdateSigned.ino - Earle F. Philhower, III
3
+ Released into the Public Domain
4
+
5
+ Shows how to use a public key extracted from your private certificate to
6
+ only allow updates that you have signed to be applied over HTTP. Remote
7
+ updates will require your private key to sign them, but of course
8
+ **ANYONE WITH PHYSICAL ACCESS CAN UPDATE THE 8266 VIA THE SERIAL PORT**.
6
9
*/
7
10
8
11
#include < Arduino.h>
13
16
#include < ESP8266HTTPClient.h>
14
17
#include < ESP8266httpUpdate.h>
15
18
16
- #define USE_SERIAL Serial
17
-
18
19
ESP8266WiFiMulti WiFiMulti;
19
20
21
+ // This key is taken from the server public certificate in BearSSL examples
22
+ // You should make your own private/public key pair and guard the private
23
+ // key (never upload it to the 8266).
20
24
const char pubkey[] PROGMEM = R"EOF(
21
25
-----BEGIN PUBLIC KEY-----
22
26
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyW5a4OO7xd6pRDTETO7h
@@ -30,20 +34,20 @@ TQIDAQAB
30
34
)EOF" ;
31
35
BearSSLPublicKey *signPubKey = nullptr ;
32
36
BearSSLHashSHA256 *hash;
33
- BearSSLVerifier *sign;
37
+ BearSSLSigningVerifier *sign;
34
38
35
39
void setup () {
36
40
37
- USE_SERIAL .begin (115200 );
38
- // USE_SERIAL .setDebugOutput(true);
41
+ Serial .begin (115200 );
42
+ // Serial .setDebugOutput(true);
39
43
40
- USE_SERIAL .println ();
41
- USE_SERIAL .println ();
42
- USE_SERIAL .println ();
44
+ Serial .println ();
45
+ Serial .println ();
46
+ Serial .println ();
43
47
44
48
for (uint8_t t = 4 ; t > 0 ; t--) {
45
- USE_SERIAL .printf (" [SETUP] WAIT %d...\n " , t);
46
- USE_SERIAL .flush ();
49
+ Serial .printf (" [SETUP] WAIT %d...\n " , t);
50
+ Serial .flush ();
47
51
delay (1000 );
48
52
}
49
53
@@ -52,7 +56,7 @@ void setup() {
52
56
53
57
signPubKey = new BearSSLPublicKey (pubkey);
54
58
hash = new BearSSLHashSHA256 ();
55
- sign = new BearSSLVerifier (signPubKey);
59
+ sign = new BearSSLSigningVerifier (signPubKey);
56
60
}
57
61
58
62
@@ -62,32 +66,24 @@ void loop() {
62
66
63
67
WiFiClient client;
64
68
65
-
69
+ // Ensure all updates are signed appropriately. W/o this call, all will be accepted.
66
70
Update.installSignature (hash, sign);
67
71
68
- // The line below is optional. It can be used to blink the LED on the board during flashing
69
- // The LED will be on during download of one buffer of data from the network. The LED will
70
- // be off during writing that buffer to flash
71
- // On a good connection the LED should flash regularly. On a bad connection the LED will be
72
- // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second
73
- // value is used to put the LED on. If the LED is on with HIGH, that value should be passed
74
72
ESPhttpUpdate.setLedPin (LED_BUILTIN, LOW);
75
73
76
74
t_httpUpdate_return ret = ESPhttpUpdate.update (client, " http://192.168.1.8/esp8266.bin" );
77
- // Or:
78
- // t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 80, "file.bin");
79
75
80
76
switch (ret) {
81
77
case HTTP_UPDATE_FAILED:
82
- USE_SERIAL .printf (" HTTP_UPDATE_FAILED Error (%d): %s\n " , ESPhttpUpdate.getLastError (), ESPhttpUpdate.getLastErrorString ().c_str ());
78
+ Serial .printf (" HTTP_UPDATE_FAILED Error (%d): %s\n " , ESPhttpUpdate.getLastError (), ESPhttpUpdate.getLastErrorString ().c_str ());
83
79
break ;
84
80
85
81
case HTTP_UPDATE_NO_UPDATES:
86
- USE_SERIAL .println (" HTTP_UPDATE_NO_UPDATES" );
82
+ Serial .println (" HTTP_UPDATE_NO_UPDATES" );
87
83
break ;
88
84
89
85
case HTTP_UPDATE_OK:
90
- USE_SERIAL .println (" HTTP_UPDATE_OK" );
86
+ Serial .println (" HTTP_UPDATE_OK" );
91
87
break ;
92
88
}
93
89
}
0 commit comments