|
| 1 | +/* |
| 2 | + SecureBearSSLUpdater - SSL encrypted, password-protected firmware update |
| 3 | +
|
| 4 | + This example starts a HTTPS server on the ESP8266 to allow firmware updates |
| 5 | + to be performed. All communication, including the username and password, |
| 6 | + is encrypted via SSL. Be sure to update the SSID and PASSWORD before running |
| 7 | + to allow connection to your WiFi network. |
| 8 | +
|
| 9 | + To upload through terminal you can use: |
| 10 | + curl -u admin:admin -F "[email protected]" esp8266-webupdate.local/firmware |
| 11 | +
|
| 12 | + Adapted by Earle F. Philhower, III, from the SecureWebUpdater.ino example. |
| 13 | + This example is released into the public domain. |
| 14 | +*/ |
| 15 | + |
| 16 | +#include <ESP8266WiFi.h> |
| 17 | +#include <WiFiClient.h> |
| 18 | +#include <ESP8266WebServerSecure.h> |
| 19 | +#include <ESP8266mDNS.h> |
| 20 | +#include <ESP8266HTTPUpdateServer.h> |
| 21 | + |
| 22 | +const char* host = "esp8266-webupdate"; |
| 23 | +const char* update_path = "/firmware"; |
| 24 | +const char* update_username = "admin"; |
| 25 | +const char* update_password = "admin"; |
| 26 | +const char* ssid = "........"; |
| 27 | +const char* password = "........"; |
| 28 | + |
| 29 | +BearSSL::ESP8266WebServerSecure httpServer(443); |
| 30 | +ESP8266HTTPUpdateServer httpUpdater; |
| 31 | + |
| 32 | +static const char serverCert[] PROGMEM = R"EOF( |
| 33 | +-----BEGIN CERTIFICATE----- |
| 34 | +MIIDSzCCAjMCCQD2ahcfZAwXxDANBgkqhkiG9w0BAQsFADCBiTELMAkGA1UEBhMC |
| 35 | +VVMxEzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDU9yYW5nZSBDb3VudHkx |
| 36 | +EDAOBgNVBAoMB1ByaXZhZG8xGjAYBgNVBAMMEXNlcnZlci56bGFiZWwuY29tMR8w |
| 37 | +HQYJKoZIhvcNAQkBFhBlYXJsZUB6bGFiZWwuY29tMB4XDTE4MDMwNjA1NDg0NFoX |
| 38 | +DTE5MDMwNjA1NDg0NFowRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3Rh |
| 39 | +dGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZI |
| 40 | +hvcNAQEBBQADggEPADCCAQoCggEBAPVKBwbZ+KDSl40YCDkP6y8Sv4iNGvEOZg8Y |
| 41 | +X7sGvf/xZH7UiCBWPFIRpNmDSaZ3yjsmFqm6sLiYSGSdrBCFqdt9NTp2r7hga6Sj |
| 42 | +oASSZY4B9pf+GblDy5m10KDx90BFKXdPMCLT+o76Nx9PpCvw13A848wHNG3bpBgI |
| 43 | +t+w/vJCX3bkRn8yEYAU6GdMbYe7v446hX3kY5UmgeJFr9xz1kq6AzYrMt/UHhNzO |
| 44 | +S+QckJaY0OGWvmTNspY3xCbbFtIDkCdBS8CZAw+itnofvnWWKQEXlt6otPh5njwy |
| 45 | ++O1t/Q+Z7OMDYQaH02IQx3188/kW3FzOY32knER1uzjmRO+jhA8CAwEAATANBgkq |
| 46 | +hkiG9w0BAQsFAAOCAQEAnDrROGRETB0woIcI1+acY1yRq4yAcH2/hdq2MoM+DCyM |
| 47 | +E8CJaOznGR9ND0ImWpTZqomHOUkOBpvu7u315blQZcLbL1LfHJGRTCHVhvVrcyEb |
| 48 | +fWTnRtAQdlirUm/obwXIitoz64VSbIVzcqqfg9C6ZREB9JbEX98/9Wp2gVY+31oC |
| 49 | +JfUvYadSYxh3nblvA4OL+iEZiW8NE3hbW6WPXxvS7Euge0uWMPc4uEcnsE0ZVG3m |
| 50 | ++TGimzSdeWDvGBRWZHXczC2zD4aoE5vrl+GD2i++c6yjL/otHfYyUpzUfbI2hMAA |
| 51 | +5tAF1D5vAAwA8nfPysumlLsIjohJZo4lgnhB++AlOg== |
| 52 | +-----END CERTIFICATE----- |
| 53 | +)EOF"; |
| 54 | + |
| 55 | +static const char serverKey[] PROGMEM = R"EOF( |
| 56 | +-----BEGIN RSA PRIVATE KEY----- |
| 57 | +MIIEpQIBAAKC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72 | +DMOxZBYgfeLY9exje00eZJNSj/csjJQqiRftrbvYY7m5njX1kM5K8x4HlynQTDkg |
| 73 | +rtKO0YZJxxmjRTbFGMegh1SLlFLRIMtehNhOgipRAoGBAPnEEpJGCS9GGLfaX0HW |
| 74 | +YqwiEK8Il12q57mqgsq7ag7NPwWOymHesxHV5mMh/Dw+NyBi4xAGWRh9mtrUmeqK |
| 75 | +iyICik773Gxo0RIqnPgd4jJWN3N3YWeynzulOIkJnSNx5BforOCTc3uCD2s2YB5X |
| 76 | +jx1LKoNQxLeLRN8cmpIWicf/AoGBANjRSsZTKwV9WWIDJoHyxav/vPb+8WYFp8lZ |
| 77 | +zaRxQbGM6nn4NiZI7OF62N3uhWB/1c7IqTK/bVHqFTuJCrCNcsgld3gLZ2QWYaMV |
| 78 | +kCPgaj1BjHw4AmB0+EcajfKilcqtSroJ6MfMJ6IclVOizkjbByeTsE4lxDmPCDSt |
| 79 | +/9MKanBxAoGAY9xo741Pn9WUxDyRplww606ccdNf/ksHWNc/Y2B5SPwxxSnIq8nO |
| 80 | +j01SmsCUYVFAgZVOTiiycakjYLzxlc6p8BxSVqy6LlJqn95N8OXoQ+bkwUux/ekg |
| 81 | +gz5JWYhbD6c38khSzJb0pNXCo3EuYAVa36kDM96k1BtWuhRS10Q1VXk= |
| 82 | +-----END RSA PRIVATE KEY----- |
| 83 | +)EOF"; |
| 84 | + |
| 85 | + |
| 86 | +void setup() |
| 87 | +{ |
| 88 | + |
| 89 | + Serial.begin(115200); |
| 90 | + Serial.println(); |
| 91 | + Serial.println("Booting Sketch..."); |
| 92 | + WiFi.mode(WIFI_AP_STA); |
| 93 | + WiFi.begin(ssid, password); |
| 94 | + |
| 95 | + while(WiFi.waitForConnectResult() != WL_CONNECTED){ |
| 96 | + WiFi.begin(ssid, password); |
| 97 | + Serial.println("WiFi failed, retrying."); |
| 98 | + } |
| 99 | + |
| 100 | + configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); |
| 101 | + |
| 102 | + MDNS.begin(host); |
| 103 | + |
| 104 | + httpServer.setRSACert(new BearSSLX509List(serverCert), new BearSSLPrivateKey(serverKey)); |
| 105 | + httpUpdater.setup(&httpServer, update_path, update_username, update_password); |
| 106 | + httpServer.begin(); |
| 107 | + |
| 108 | + MDNS.addService("https", "tcp", 443); |
| 109 | + Serial.printf("BearSSLUpdateServer ready!\nOpen https://%s.local%s in "\ |
| 110 | + "your browser and login with username '%s' and password "\ |
| 111 | + "'%s'\n", host, update_path, update_username, update_password); |
| 112 | +} |
| 113 | + |
| 114 | +void loop() |
| 115 | +{ |
| 116 | + httpServer.handleClient(); |
| 117 | +} |
0 commit comments