Skip to content

Commit e3c9702

Browse files
Add BearSSL client and server, support true bidir, lower memory, modern SSL (#4273)
BearSSL (https://www.bearssl.org) is a TLS(SSL) library written by Thomas Pornin that is optimized for lower-memory embedded systems like the ESP8266. It supports a wide variety of modern ciphers and is unique in that it doesn't perform any memory allocations during operation (which is the unfortunate bane of the current axTLS). BearSSL is also absolutely focused on security and by default performs all its security checks on x.509 certificates during the connection phase (but if you want to be insecure and dangerous, that's possible too). While it does support unidirectional SSL buffers, like axTLS, as implemented the ESP8266 wrappers only support bidirectional buffers. These bidirectional buffers avoid deadlocks in protocols which don't have well separated receive and transmit periods. This patch adds several classes which allow connecting to TLS servers using this library in almost the same way as axTLS: BearSSL::WiFiClientSecure - WiFiClient that supports TLS BearSSL::WiFiServerSecure - WiFiServer supporting TLS and client certs It also introduces objects for PEM/DER encoded keys and certificates: BearSSLX509List - x.509 Certificate (list) for general use BearSSLPrivateKey - RSA or EC private key BearSSLPublicKey - RSA or EC public key (i.e. from a public website) Finally, it adds a Certificate Authority store object which lets BearSSL access a set of trusted CA certificates on SPIFFS to allow it to verify the identity of any remote site on the Internet, without requiring RAM except for the single matching certificate. CertStoreSPIFFSBearSSL - Certificate store utility Client certificates are supported for the BearSSL::WiFiClientSecure, and what's more the BearSSL::WiFiServerSecure can also *require* remote clients to have a trusted certificate signed by a specific CA (or yourself with self-signing CAs). Maximum Fragment Length Negotiation probing and usage are supported, but be aware that most sites on the Internet don't support it yet. When available, you can reduce the memory footprint of the SSL client or server dramatically (i.e. down to 2-8KB vs. the ~22KB required for a full 16K receive fragment and 512b send fragment). You can also manually set a smaller fragment size and guarantee at your protocol level all data will fit within it. Examples are included to show the usage of these new features. axTLS has been moved to its own namespace, "axtls". A default "using" clause allows existing apps to run using axTLS without any changes. The BearSSL::WiFi{client,server}Secure implements the axTLS client/server API which lets many end user applications take advantage of BearSSL with few or no changes. The BearSSL static library used presently is stored at https://github.com/earlephilhower/bearssl-esp8266 and can be built using the standard ESP8266 toolchain.
1 parent bd87970 commit e3c9702

File tree

70 files changed

+18433
-145
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+18433
-145
lines changed

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+65-2
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,47 @@ class TLSTraits : public TransportTraits
6060

6161
std::unique_ptr<WiFiClient> create() override
6262
{
63-
return std::unique_ptr<WiFiClient>(new WiFiClientSecure());
63+
return std::unique_ptr<WiFiClient>(new axTLS::WiFiClientSecure());
6464
}
6565

6666
bool verify(WiFiClient& client, const char* host) override
6767
{
68-
auto wcs = static_cast<WiFiClientSecure&>(client);
68+
auto wcs = static_cast<axTLS::WiFiClientSecure&>(client);
6969
return wcs.verify(_fingerprint.c_str(), host);
7070
}
7171

7272
protected:
7373
String _fingerprint;
7474
};
7575

76+
class BearSSLTraits : public TransportTraits
77+
{
78+
public:
79+
BearSSLTraits(const uint8_t fingerprint[20])
80+
{
81+
memcpy(_fingerprint, fingerprint, sizeof(_fingerprint));
82+
}
83+
84+
std::unique_ptr<WiFiClient> create() override
85+
{
86+
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure();
87+
client->setFingerprint(_fingerprint);
88+
return std::unique_ptr<WiFiClient>(client);
89+
}
90+
91+
bool verify(WiFiClient& client, const char* host) override
92+
{
93+
// No-op. BearSSL will not connect if the fingerprint doesn't match.
94+
// So if you get to here you've already connected and it matched
95+
(void) client;
96+
(void) host;
97+
return true;
98+
}
99+
100+
protected:
101+
uint8_t _fingerprint[20];
102+
};
103+
76104
/**
77105
* constructor
78106
*/
@@ -116,6 +144,24 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
116144
return true;
117145
}
118146

147+
148+
bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
149+
{
150+
_transportTraits.reset(nullptr);
151+
_port = 443;
152+
if (!beginInternal(url, "https")) {
153+
return false;
154+
}
155+
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
156+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] BearSSL-httpsFingerprint:");
157+
for (size_t i=0; i < 20; i++) {
158+
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
159+
}
160+
DEBUG_HTTPCLIENT("\n");
161+
return true;
162+
}
163+
164+
119165
/**
120166
* parsing the url for all needed parameters
121167
* @param url String
@@ -213,6 +259,23 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFinge
213259
return true;
214260
}
215261

262+
bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20])
263+
{
264+
clear();
265+
_host = host;
266+
_port = port;
267+
_uri = uri;
268+
269+
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
270+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s BearSSL-httpsFingerprint:", host.c_str(), port, uri.c_str());
271+
for (size_t i=0; i < 20; i++) {
272+
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
273+
}
274+
DEBUG_HTTPCLIENT("\n");
275+
return true;
276+
}
277+
278+
216279
/**
217280
* end
218281
* called after the payload is handled

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,15 @@ class HTTPClient
133133
HTTPClient();
134134
~HTTPClient();
135135

136+
// Plain HTTP connection, unencrypted
136137
bool begin(String url);
137-
bool begin(String url, String httpsFingerprint);
138138
bool begin(String host, uint16_t port, String uri = "/");
139+
// Use axTLS for secure HTTPS connection
140+
bool begin(String url, String httpsFingerprint);
139141
bool begin(String host, uint16_t port, String uri, String httpsFingerprint);
142+
// Use BearSSL for secure HTTPS connection
143+
bool begin(String url, const uint8_t httpsFingerprint[20]);
144+
bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]);
140145
// deprecated, use the overload above instead
141146
bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((deprecated));
142147

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
MIIEpQIBAAKCAQEA9UoHBtn4oNKXjRgIOQ/rLxK/iI0a8Q5mDxhfuwa9//FkftSI
58+
IFY8UhGk2YNJpnfKOyYWqbqwuJhIZJ2sEIWp2301OnavuGBrpKOgBJJljgH2l/4Z
59+
uUPLmbXQoPH3QEUpd08wItP6jvo3H0+kK/DXcDzjzAc0bdukGAi37D+8kJfduRGf
60+
zIRgBToZ0xth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1LKoNQxLeLRN8cmpIWicf/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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
HelloServerBearSSL - Simple HTTPS server example
3+
4+
This example demonstrates a basic ESP8266WebServerSecure HTTPS server
5+
that can serve "/" and "/inline" and generate detailed 404 (not found)
6+
HTTP respoinses. Be sure to update the SSID and PASSWORD before running
7+
to allow connection to your WiFi network.
8+
9+
Adapted by Earle F. Philhower, III, from the HelloServer.ino example.
10+
This example is released into the public domain.
11+
*/
12+
#include <ESP8266WiFi.h>
13+
#include <WiFiClient.h>
14+
#include <ESP8266WebServerSecure.h>
15+
#include <ESP8266mDNS.h>
16+
17+
const char* ssid = "....";
18+
const char* password = "....";
19+
20+
BearSSL::ESP8266WebServerSecure server(443);
21+
22+
static const char serverCert[] PROGMEM = R"EOF(
23+
-----BEGIN CERTIFICATE-----
24+
MIIDSzCCAjMCCQD2ahcfZAwXxDANBgkqhkiG9w0BAQsFADCBiTELMAkGA1UEBhMC
25+
VVMxEzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcMDU9yYW5nZSBDb3VudHkx
26+
EDAOBgNVBAoMB1ByaXZhZG8xGjAYBgNVBAMMEXNlcnZlci56bGFiZWwuY29tMR8w
27+
HQYJKoZIhvcNAQkBFhBlYXJsZUB6bGFiZWwuY29tMB4XDTE4MDMwNjA1NDg0NFoX
28+
DTE5MDMwNjA1NDg0NFowRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3Rh
29+
dGUxITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZI
30+
hvcNAQEBBQADggEPADCCAQoCggEBAPVKBwbZ+KDSl40YCDkP6y8Sv4iNGvEOZg8Y
31+
X7sGvf/xZH7UiCBWPFIRpNmDSaZ3yjsmFqm6sLiYSGSdrBCFqdt9NTp2r7hga6Sj
32+
oASSZY4B9pf+GblDy5m10KDx90BFKXdPMCLT+o76Nx9PpCvw13A848wHNG3bpBgI
33+
t+w/vJCX3bkRn8yEYAU6GdMbYe7v446hX3kY5UmgeJFr9xz1kq6AzYrMt/UHhNzO
34+
S+QckJaY0OGWvmTNspY3xCbbFtIDkCdBS8CZAw+itnofvnWWKQEXlt6otPh5njwy
35+
+O1t/Q+Z7OMDYQaH02IQx3188/kW3FzOY32knER1uzjmRO+jhA8CAwEAATANBgkq
36+
hkiG9w0BAQsFAAOCAQEAnDrROGRETB0woIcI1+acY1yRq4yAcH2/hdq2MoM+DCyM
37+
E8CJaOznGR9ND0ImWpTZqomHOUkOBpvu7u315blQZcLbL1LfHJGRTCHVhvVrcyEb
38+
fWTnRtAQdlirUm/obwXIitoz64VSbIVzcqqfg9C6ZREB9JbEX98/9Wp2gVY+31oC
39+
JfUvYadSYxh3nblvA4OL+iEZiW8NE3hbW6WPXxvS7Euge0uWMPc4uEcnsE0ZVG3m
40+
+TGimzSdeWDvGBRWZHXczC2zD4aoE5vrl+GD2i++c6yjL/otHfYyUpzUfbI2hMAA
41+
5tAF1D5vAAwA8nfPysumlLsIjohJZo4lgnhB++AlOg==
42+
-----END CERTIFICATE-----
43+
)EOF";
44+
45+
static const char serverKey[] PROGMEM = R"EOF(
46+
-----BEGIN RSA PRIVATE KEY-----
47+
MIIEpQIBAAKCAQEA9UoHBtn4oNKXjRgIOQ/rLxK/iI0a8Q5mDxhfuwa9//FkftSI
48+
IFY8UhGk2YNJpnfKOyYWqbqwuJhIZJ2sEIWp2301OnavuGBrpKOgBJJljgH2l/4Z
49+
uUPLmbXQoPH3QEUpd08wItP6jvo3H0+kK/DXcDzjzAc0bdukGAi37D+8kJfduRGf
50+
zIRgBToZ0xth7u/jjqFfeRjlSaB4kWv3HPWSroDNisy39QeE3M5L5ByQlpjQ4Za+
51+
ZM2yljfEJtsW0gOQJ0FLwJkDD6K2eh++dZYpAReW3qi0+HmePDL47W39D5ns4wNh
52+
BofTYhDHfXzz+RbcXM5jfaScRHW7OOZE76OEDwIDAQABAoIBAQDKov5NFbNFQNR8
53+
djcM1O7Is6dRaqiwLeH4ZH1pZ3d9QnFwKanPdQ5eCj9yhfhJMrr5xEyCqT0nMn7T
54+
yEIGYDXjontfsf8WxWkH2TjvrfWBrHOIOx4LJEvFzyLsYxiMmtZXvy6YByD+Dw2M
55+
q2GH/24rRdI2kl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hbD6c38khSzJb0pNXCo3EuYAVa36kDM96k1BtWuhRS10Q1VXk=
72+
-----END RSA PRIVATE KEY-----
73+
)EOF";
74+
75+
76+
const int led = 13;
77+
78+
void handleRoot() {
79+
digitalWrite(led, 1);
80+
server.send(200, "text/plain", "Hello from esp8266 over HTTPS!");
81+
digitalWrite(led, 0);
82+
}
83+
84+
void handleNotFound(){
85+
digitalWrite(led, 1);
86+
String message = "File Not Found\n\n";
87+
message += "URI: ";
88+
message += server.uri();
89+
message += "\nMethod: ";
90+
message += (server.method() == HTTP_GET)?"GET":"POST";
91+
message += "\nArguments: ";
92+
message += server.args();
93+
message += "\n";
94+
for (uint8_t i=0; i<server.args(); i++){
95+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
96+
}
97+
server.send(404, "text/plain", message);
98+
digitalWrite(led, 0);
99+
}
100+
101+
void setup(void){
102+
pinMode(led, OUTPUT);
103+
digitalWrite(led, 0);
104+
Serial.begin(115200);
105+
WiFi.begin(ssid, password);
106+
Serial.println("");
107+
108+
// Wait for connection
109+
while (WiFi.status() != WL_CONNECTED) {
110+
delay(500);
111+
Serial.print(".");
112+
}
113+
114+
configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov");
115+
116+
Serial.println("");
117+
Serial.print("Connected to ");
118+
Serial.println(ssid);
119+
Serial.print("IP address: ");
120+
Serial.println(WiFi.localIP());
121+
122+
if (MDNS.begin("esp8266")) {
123+
Serial.println("MDNS responder started");
124+
}
125+
126+
server.setRSACert(new BearSSLX509List(serverCert), new BearSSLPrivateKey(serverKey));
127+
128+
server.on("/", handleRoot);
129+
130+
server.on("/inline", [](){
131+
server.send(200, "text/plain", "this works as well");
132+
});
133+
134+
server.onNotFound(handleNotFound);
135+
136+
server.begin();
137+
Serial.println("HTTPS server started");
138+
}
139+
140+
void loop(void){
141+
server.handleClient();
142+
}

0 commit comments

Comments
 (0)