From 1951e591f573a8a2141bdab43d88f96b9c441cfe Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 4 Aug 2023 02:10:45 +0700 Subject: [PATCH] Fix possible null pointer exception error. --- library.json | 2 +- library.properties | 2 +- src/ESP_SSLClient.h | 4 ++-- src/client/BSSL_SSL_Client.cpp | 44 ++++++++++++++++++++++++++++++---- src/client/BSSL_SSL_Client.h | 10 ++++++-- src/client/BSSL_TCP_Client.cpp | 8 ++++--- src/client/BSSL_TCP_Client.h | 6 ++--- 7 files changed, 59 insertions(+), 17 deletions(-) diff --git a/library.json b/library.json index d0c78d2..278ce9a 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP_SSLClient", - "version": "2.0.3", + "version": "2.0.4", "keywords": "communication, REST, esp32, esp8266, arduino", "description": "This library provided the Secure Layer Networking (SSL/TLS) TCP Client for ESP8266, ESP32 and Raspberry Pi RP2040, Teensy, SAMD, AVR and other Arduino devices that support external networking interfaces e.g., WiFiClient, EthernetClient and GSMClient.", "repository": { diff --git a/library.properties b/library.properties index 213b475..402b3d4 100644 --- a/library.properties +++ b/library.properties @@ -1,6 +1,6 @@ name=ESP_SSLClient -version=2.0.3 +version=2.0.4 author=Mobizt diff --git a/src/ESP_SSLClient.h b/src/ESP_SSLClient.h index 9f7bc59..2260448 100644 --- a/src/ESP_SSLClient.h +++ b/src/ESP_SSLClient.h @@ -1,8 +1,8 @@ /** * - * The ESP SSL Client Class, ESP_SSLClient.h v2.0.3 + * The ESP SSL Client Class, ESP_SSLClient.h v2.0.4 * - * Created August 3, 2023 + * Created August 4, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) diff --git a/src/client/BSSL_SSL_Client.cpp b/src/client/BSSL_SSL_Client.cpp index 61cffec..f281a1d 100644 --- a/src/client/BSSL_SSL_Client.cpp +++ b/src/client/BSSL_SSL_Client.cpp @@ -1,7 +1,7 @@ /** - * BSSL_SSL_Client library v1.0.1 for Arduino devices. + * BSSL_SSL_Client library v1.0.2 for Arduino devices. * - * Created August 3, 2003 + * Created August 4, 2003 * * This work contains codes based on WiFiClientSecure from Earle F. Philhower and SSLClient from OSU OPEnS Lab. * @@ -231,6 +231,9 @@ int BSSL_SSL_Client::read() int BSSL_SSL_Client::read(uint8_t *buf, size_t size) { + if (!_basic_client) + return 0; + if (!_secure) return _basic_client->read(buf, size); @@ -250,6 +253,9 @@ int BSSL_SSL_Client::read(uint8_t *buf, size_t size) size_t BSSL_SSL_Client::write(const uint8_t *buf, size_t size) { + if (!_basic_client) + return 0; + if (!_secure) return _basic_client->write(buf, size); @@ -367,6 +373,26 @@ int BSSL_SSL_Client::peek() return -1; } +size_t BSSL_SSL_Client::peekBytes(uint8_t *buffer, size_t length) +{ + if (!_basic_client || !_secure) + return 0; + + size_t to_copy = 0; + if (!_sc) + return 0; + + unsigned long _startMillis = millis(); + while ((available() < (int)length) && ((millis() - _startMillis) < 5000)) + { + yield(); + } + + to_copy = _recvapp_len < length ? _recvapp_len : length; + memcpy(buffer, _recvapp_buf, to_copy); + return to_copy; +} + // Don't validate the chain, just accept whatever is given. VERY INSECURE! void BSSL_SSL_Client::setInsecure() { @@ -442,7 +468,7 @@ void BSSL_SSL_Client::setHandshakeTimeout(unsigned int timeoutMs) { _handshake_t void BSSL_SSL_Client::flush() { - if (!_secure) + if (!_secure && _basic_client) { _basic_client->flush(); return; @@ -509,6 +535,8 @@ int BSSL_SSL_Client::availableForWrite() return 0; } +void BSSL_SSL_Client::setSession(BearSSL_Session *session) { _session = session; }; + // Assume a given public key, don't validate or use cert info at all void BSSL_SSL_Client::setKnownKey(const PublicKey *pk, unsigned usages) { @@ -966,6 +994,11 @@ bool BSSL_SSL_Client::probeMaxFragmentLength(const String &host, uint16_t port, return BSSL_SSL_Client::probeMaxFragmentLength(host.c_str(), port, len); } +size_t BSSL_SSL_Client::peekAvailable() +{ + return available(); +} + // return a pointer to available data buffer (size = peekAvailable()) // semantic forbids any kind of read() before calling peekConsume() const char *BSSL_SSL_Client::peekBuffer() @@ -1585,9 +1618,10 @@ int BSSL_SSL_Client::mRunUntil(const unsigned target, unsigned long timeout) br_ssl_engine_recvrec_buf(_eng, &len); if (lastLen != len) { + lastLen = len; #if defined(ESP_SSLCLIENT_ENABLE_DEBUG) String s = PSTR("Expected bytes count: "); - s += lastLen = len; + s += len; esp_ssl_debug_print(s.c_str(), _debug_level, esp_ssl_debug_info, __func__); #endif } @@ -2018,7 +2052,7 @@ bool BSSL_SSL_Client::mInstallClientX509Validator() #endif bssl::br_x509_minimal_install_hashes(_x509_minimal.get()); -#if (defined(ESP32) || defined(ESP8266) || defined(ARDUINO_ARCH_RP2040)) && !defined(ARDUINO_NANO_RP2040_CONNECT) +#if (defined(ESP32) || defined(ESP8266) || defined(ARDUINO_ARCH_RP2040)) && !defined(ARDUINO_NANO_RP2040_CONNECT) if (_now < ESP_SSLCLIENT_VALID_TIMESTAMP) _now = time(nullptr); #endif diff --git a/src/client/BSSL_SSL_Client.h b/src/client/BSSL_SSL_Client.h index 88e1a68..3a9cbb3 100644 --- a/src/client/BSSL_SSL_Client.h +++ b/src/client/BSSL_SSL_Client.h @@ -1,7 +1,7 @@ /** - * BSSL_SSL_Client library v1.0.1 for Arduino devices. + * BSSL_SSL_Client library v1.0.2 for Arduino devices. * - * Created August 3, 2003 + * Created August 4, 2003 * * This work contains codes based on WiFiClientSecure from Earle F. Philhower and SSLClient from OSU OPEnS Lab. * @@ -119,6 +119,8 @@ class BSSL_SSL_Client : public Client int peek() override; + size_t peekBytes(uint8_t *buffer, size_t length); + void setInsecure(); void enableSSL(bool enable); @@ -141,6 +143,8 @@ class BSSL_SSL_Client : public Client int availableForWrite(); + void setSession(BearSSL_Session *session); + void setKnownKey(const PublicKey *pk, unsigned usages = BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN); bool setFingerprint(const uint8_t fingerprint[20]); @@ -177,6 +181,8 @@ class BSSL_SSL_Client : public Client bool probeMaxFragmentLength(const String &host, uint16_t port, uint16_t len); + size_t peekAvailable(); + const char *peekBuffer(); void peekConsume(size_t consume); diff --git a/src/client/BSSL_TCP_Client.cpp b/src/client/BSSL_TCP_Client.cpp index 341109b..4a4bb77 100644 --- a/src/client/BSSL_TCP_Client.cpp +++ b/src/client/BSSL_TCP_Client.cpp @@ -1,7 +1,7 @@ /** - * BSSL_TCP_Client v2.0.3 for Arduino devices. + * BSSL_TCP_Client v2.0.4 for Arduino devices. * - * Created August 3, 2023 + * Created August 4, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -299,6 +299,8 @@ void BSSL_TCP_Client::setBufferSizes(int recv, int xmit) int BSSL_TCP_Client::availableForWrite() { return _ssl_client.availableForWrite(); }; +void BSSL_TCP_Client::setSession(BearSSL_Session *session) {_ssl_client.setSession(session);}; + void BSSL_TCP_Client::setKnownKey(const PublicKey *pk, unsigned usages) { _ssl_client.setKnownKey(pk, usages); @@ -383,7 +385,7 @@ bool BSSL_TCP_Client::probeMaxFragmentLength(const String &host, uint16_t port, bool BSSL_TCP_Client::hasPeekBufferAPI() const { return true; } // return number of byte accessible by peekBuffer() -size_t BSSL_TCP_Client::peekAvailable() { return _ssl_client.available(); } +size_t BSSL_TCP_Client::peekAvailable() { return _ssl_client.peekAvailable(); } // return a pointer to available data buffer (size = peekAvailable()) // semantic forbids any kind of read() before calling peekConsume() diff --git a/src/client/BSSL_TCP_Client.h b/src/client/BSSL_TCP_Client.h index 3574d5e..3c90547 100644 --- a/src/client/BSSL_TCP_Client.h +++ b/src/client/BSSL_TCP_Client.h @@ -1,7 +1,7 @@ /** - * BSSL_TCP_Client v2.0.3 for Arduino devices. + * BSSL_TCP_Client v2.0.4 for Arduino devices. * - * Created August 3, 2023 + * Created August 4, 2023 * * The MIT License (MIT) * Copyright (c) 2023 K. Suwatchai (Mobizt) @@ -307,7 +307,7 @@ class BSSL_TCP_Client : public Client int availableForWrite(); - // void setSession(BearSSL_Session *session) {}; + void setSession(BearSSL_Session *session); void setKnownKey(const PublicKey *pk, unsigned usages = BR_KEYTYPE_KEYX | BR_KEYTYPE_SIGN);