From 75f01dc35a670abd66de07cd7101a56104a50e7f Mon Sep 17 00:00:00 2001 From: Dave <47106837+sislakd@users.noreply.github.com> Date: Wed, 15 May 2019 18:55:06 +0200 Subject: [PATCH] Drop X509 after connection, avoid hang on TLS broken (#6065) * Drop X509 context after successful server verification to save heap space After completing handshake in BSSL, server is already verified and X509 context is no longer needed. Depending on verification method it save more or less heap space. * Bugfix: Report not connected if there is no ready data and TLS connection is broken Added the change for reporting not connected if TLS session is broken and there is no more buffered decrypted data. TLS can be broken if message authentication (MAC) cannot be verified. BearSSL enters BR_SSL_CLOSED state when processing invalid encrypted application data fragment. In such situation the current implementation get stuck forever unless user has own timeout mechanism build on top of WiFiClientSecureBearSSL. This change introduce fail fast via connected() returning false. Further it imply return -1 from read methods indicating broken channel upon which user should perform reconnect if needed. Fixes #6005 --- libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp b/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp index 8d1eee72e..7ae53e6b9 100644 --- a/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp +++ b/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp @@ -255,7 +255,7 @@ bool WiFiClientSecure::_clientConnected() { } uint8_t WiFiClientSecure::connected() { - if (available() || (_clientConnected() && _handshake_done)) { + if (available() || (_clientConnected() && _handshake_done && (br_ssl_engine_current_state(_eng) != BR_SSL_CLOSED))) { return true; } return false; @@ -1003,6 +1003,12 @@ bool WiFiClientSecure::_connectSSL(const char* hostName) { DEBUG_BSSL("Connected!\n"); } #endif + + // Session is already validated here, there is no need to keep following + _x509_minimal = nullptr; + _x509_insecure = nullptr; + _x509_knownkey = nullptr; + return ret; }