mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
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.
This commit is contained in:
committed by
GitHub
parent
bd87970aae
commit
e3c970210f
@ -60,12 +60,12 @@ public:
|
||||
|
||||
std::unique_ptr<WiFiClient> create() override
|
||||
{
|
||||
return std::unique_ptr<WiFiClient>(new WiFiClientSecure());
|
||||
return std::unique_ptr<WiFiClient>(new axTLS::WiFiClientSecure());
|
||||
}
|
||||
|
||||
bool verify(WiFiClient& client, const char* host) override
|
||||
{
|
||||
auto wcs = static_cast<WiFiClientSecure&>(client);
|
||||
auto wcs = static_cast<axTLS::WiFiClientSecure&>(client);
|
||||
return wcs.verify(_fingerprint.c_str(), host);
|
||||
}
|
||||
|
||||
@ -73,6 +73,34 @@ protected:
|
||||
String _fingerprint;
|
||||
};
|
||||
|
||||
class BearSSLTraits : public TransportTraits
|
||||
{
|
||||
public:
|
||||
BearSSLTraits(const uint8_t fingerprint[20])
|
||||
{
|
||||
memcpy(_fingerprint, fingerprint, sizeof(_fingerprint));
|
||||
}
|
||||
|
||||
std::unique_ptr<WiFiClient> create() override
|
||||
{
|
||||
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure();
|
||||
client->setFingerprint(_fingerprint);
|
||||
return std::unique_ptr<WiFiClient>(client);
|
||||
}
|
||||
|
||||
bool verify(WiFiClient& client, const char* host) override
|
||||
{
|
||||
// No-op. BearSSL will not connect if the fingerprint doesn't match.
|
||||
// So if you get to here you've already connected and it matched
|
||||
(void) client;
|
||||
(void) host;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected:
|
||||
uint8_t _fingerprint[20];
|
||||
};
|
||||
|
||||
/**
|
||||
* constructor
|
||||
*/
|
||||
@ -116,6 +144,24 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
|
||||
{
|
||||
_transportTraits.reset(nullptr);
|
||||
_port = 443;
|
||||
if (!beginInternal(url, "https")) {
|
||||
return false;
|
||||
}
|
||||
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
|
||||
DEBUG_HTTPCLIENT("[HTTP-Client][begin] BearSSL-httpsFingerprint:");
|
||||
for (size_t i=0; i < 20; i++) {
|
||||
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
|
||||
}
|
||||
DEBUG_HTTPCLIENT("\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* parsing the url for all needed parameters
|
||||
* @param url String
|
||||
@ -213,6 +259,23 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFinge
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20])
|
||||
{
|
||||
clear();
|
||||
_host = host;
|
||||
_port = port;
|
||||
_uri = uri;
|
||||
|
||||
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
|
||||
DEBUG_HTTPCLIENT("[HTTP-Client][begin] host: %s port: %d url: %s BearSSL-httpsFingerprint:", host.c_str(), port, uri.c_str());
|
||||
for (size_t i=0; i < 20; i++) {
|
||||
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
|
||||
}
|
||||
DEBUG_HTTPCLIENT("\n");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* end
|
||||
* called after the payload is handled
|
||||
|
@ -133,10 +133,15 @@ public:
|
||||
HTTPClient();
|
||||
~HTTPClient();
|
||||
|
||||
// Plain HTTP connection, unencrypted
|
||||
bool begin(String url);
|
||||
bool begin(String url, String httpsFingerprint);
|
||||
bool begin(String host, uint16_t port, String uri = "/");
|
||||
// Use axTLS for secure HTTPS connection
|
||||
bool begin(String url, String httpsFingerprint);
|
||||
bool begin(String host, uint16_t port, String uri, String httpsFingerprint);
|
||||
// Use BearSSL for secure HTTPS connection
|
||||
bool begin(String url, const uint8_t httpsFingerprint[20]);
|
||||
bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]);
|
||||
// deprecated, use the overload above instead
|
||||
bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((deprecated));
|
||||
|
||||
|
Reference in New Issue
Block a user