mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
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.
120 lines
3.9 KiB
C++
120 lines
3.9 KiB
C++
/*
|
|
WiFiServerBearSSL.cpp - SSL server for esp8266, mostly compatible
|
|
with Arduino WiFi shield library
|
|
|
|
Copyright (c) 2018 Earle F. Philhower, III
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#define LWIP_INTERNAL
|
|
|
|
extern "C" {
|
|
#include "osapi.h"
|
|
#include "ets_sys.h"
|
|
}
|
|
|
|
#include "debug.h"
|
|
#include "ESP8266WiFi.h"
|
|
#include "WiFiClient.h"
|
|
#include "WiFiServer.h"
|
|
#include "lwip/opt.h"
|
|
#include "lwip/tcp.h"
|
|
#include "lwip/inet.h"
|
|
#include "include/ClientContext.h"
|
|
#include "WiFiServerSecureBearSSL.h"
|
|
|
|
namespace BearSSL {
|
|
|
|
// Only need to call the standard server constructor
|
|
WiFiServerSecure::WiFiServerSecure(IPAddress addr, uint16_t port) : WiFiServer(addr, port) {
|
|
}
|
|
|
|
// Only need to call the standard server constructor
|
|
WiFiServerSecure::WiFiServerSecure(uint16_t port) : WiFiServer(port) {
|
|
}
|
|
|
|
// Destructor only checks if we need to delete compatibilty cert/key
|
|
WiFiServerSecure::~WiFiServerSecure() {
|
|
if (_deleteChainAndKey) {
|
|
delete _chain;
|
|
delete _sk;
|
|
}
|
|
}
|
|
|
|
// Specify a RSA-signed certificate and key for the server. Only copies the pointer, the
|
|
// caller needs to preserve this chain and key for the life of the object.
|
|
void WiFiServerSecure::setRSACert(const BearSSLX509List *chain, const BearSSLPrivateKey *sk) {
|
|
_chain = chain;
|
|
_sk = sk;
|
|
}
|
|
|
|
// Specify a EC-signed certificate and key for the server. Only copies the pointer, the
|
|
// caller needs to preserve this chain and key for the life of the object.
|
|
void WiFiServerSecure::setECCert(const BearSSLX509List *chain, unsigned cert_issuer_key_type, const BearSSLPrivateKey *sk) {
|
|
_chain = chain;
|
|
_cert_issuer_key_type = cert_issuer_key_type;
|
|
_sk = sk;
|
|
}
|
|
|
|
// Return a client if there's an available connection waiting. If one is returned,
|
|
// then any validation (i.e. client cert checking) will have succeeded.
|
|
WiFiClientSecure WiFiServerSecure::available(uint8_t* status) {
|
|
(void) status; // Unused
|
|
if (_unclaimed) {
|
|
if (_sk && _sk->isRSA()) {
|
|
WiFiClientSecure result(_unclaimed, _chain, _sk, _iobuf_in_size, _iobuf_out_size, _client_CA_ta);
|
|
_unclaimed = _unclaimed->next();
|
|
result.setNoDelay(_noDelay);
|
|
DEBUGV("WS:av\r\n");
|
|
return result;
|
|
} else if (_sk && _sk->isEC()) {
|
|
WiFiClientSecure result(_unclaimed, _chain, _cert_issuer_key_type, _sk, _iobuf_in_size, _iobuf_out_size, _client_CA_ta);
|
|
_unclaimed = _unclaimed->next();
|
|
result.setNoDelay(_noDelay);
|
|
DEBUGV("WS:av\r\n");
|
|
return result;
|
|
} else {
|
|
// No key was defined, so we can't actually accept and attempt accept() and SSL handshake.
|
|
DEBUGV("WS:nokey\r\n");
|
|
}
|
|
}
|
|
|
|
// Something weird, return a no-op object
|
|
optimistic_yield(1000);
|
|
return WiFiClientSecure();
|
|
}
|
|
|
|
|
|
void WiFiServerSecure::setServerKeyAndCert(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen) {
|
|
BearSSLX509List *chain = new BearSSLX509List(cert, certLen);
|
|
BearSSLPrivateKey *sk = new BearSSLPrivateKey(key, keyLen);
|
|
if (!chain || !key) {
|
|
// OOM, fail gracefully
|
|
delete chain;
|
|
delete sk;
|
|
return;
|
|
}
|
|
_deleteChainAndKey = true;
|
|
setRSACert(chain, sk);
|
|
}
|
|
|
|
void WiFiServerSecure::setServerKeyAndCert_P(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen) {
|
|
setServerKeyAndCert(key, keyLen, cert, certLen);
|
|
}
|
|
|
|
|
|
};
|