Skip to content

Commit

Permalink
Fix possible null pointer exception error.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Aug 3, 2023
1 parent 28f1bce commit 1951e59
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 17 deletions.
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP_SSLClient

version=2.0.3
version=2.0.4

author=Mobizt

Expand Down
4 changes: 2 additions & 2 deletions src/ESP_SSLClient.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
44 changes: 39 additions & 5 deletions src/client/BSSL_SSL_Client.cpp
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/client/BSSL_SSL_Client.h
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions src/client/BSSL_TCP_Client.cpp
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/client/BSSL_TCP_Client.h
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 1951e59

Please sign in to comment.