Skip to content
Prev Previous commit
Next Next commit
Add SSL session caching to HTTPS server examples
  • Loading branch information
ZakCodes committed Dec 17, 2020
commit fd5662be5d4e4d55f8ad165d553619cdcc6c5348
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const char* ssid = STASSID;
const char* password = STAPSK;

BearSSL::ESP8266WebServerSecure server(443);
BearSSL::ServerSessions serverCache(500);

static const char serverCert[] PROGMEM = R"EOF(
-----BEGIN CERTIFICATE-----
Expand Down Expand Up @@ -132,6 +133,9 @@ void setup(void){

server.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));

// Cache SSL sessions to accelerate the TLS handshake.
server.getServer().setCache(&serverCache);

server.on("/", handleRoot);

server.on("/inline", [](){
Expand Down
22 changes: 22 additions & 0 deletions libraries/ESP8266WiFi/examples/BearSSL_Server/BearSSL_Server.ino
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,23 @@ GBEnkz4KpKv7TkHoW+j7F5EMcLcSrUIpyw==

#endif

#define CACHE_SIZE 500 // Size of the cache for SSL sessions.
// Each SSL session requires 100 bytes,
// so 500 is enough for 5 sessions.
#define USE_CACHE // Enable SSL session caching.
// Caching SSL sessions shortens the length of the SSL handshake.
// You can see the performance improvement by looking at the
// Network tab of the developper tools of your browser.
//#define DYNAMIC_CACHE // Whether to dynamically allocate the cache.

#if defined(USE_CACHE) && defined(DYNAMIC_CACHE)
// Dynamically allocated cache.
BearSSL::ServerSessions serverCache(STORE, CACHE_SIZE);
#elif defined(USE_CACHE)
// Statically allocated cache.
uint8_t store[CACHE_SIZE];
BearSSL::ServerSessions serverCache(CACHE_SIZE);
#endif

void setup() {
Serial.begin(115200);
Expand Down Expand Up @@ -169,6 +186,11 @@ void setup() {
server.setECCert(serverCertList, BR_KEYTYPE_KEYX|BR_KEYTYPE_SIGN, serverPrivKey);
#endif

// Set the server's cache
#if defined(USE_CACHE)
server.setCache(&serverCache);
#endif

// Actually start accepting connections
server.begin();
}
Expand Down