Skip to content

Commit ef26c5f

Browse files
committed
More error checks in WiFiClientSecure
1 parent 119512d commit ef26c5f

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

+38-1
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,19 @@ class SSLContext {
130130

131131
protected:
132132
int _readAll() {
133+
if (!_ssl)
134+
return 0;
135+
133136
uint8_t* data;
134137
int rc = ssl_read(_ssl, &data);
135-
if (rc <= 0)
138+
if (rc <= 0) {
139+
if (rc < SSL_OK && rc != SSL_CLOSE_NOTIFY && rc != SSL_ERROR_CONN_LOST) {
140+
ssl_free(_ssl);
141+
_ssl = nullptr;
142+
}
136143
return 0;
144+
}
145+
137146

138147
if (rc > _rxbuf->room()) {
139148
DEBUGV("WiFiClientSecure rx overflow");
@@ -219,6 +228,9 @@ int WiFiClientSecure::_connectSSL() {
219228
}
220229

221230
size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {
231+
if (!_ssl)
232+
return 0;
233+
222234
int rc = ssl_write(*_ssl, buf, size);
223235
if (rc >= 0)
224236
return rc;
@@ -227,21 +239,43 @@ size_t WiFiClientSecure::write(const uint8_t *buf, size_t size) {
227239
}
228240

229241
int WiFiClientSecure::read(uint8_t *buf, size_t size) {
242+
if (!_ssl)
243+
return 0;
244+
230245
return _ssl->read(buf, size);
231246
}
232247

233248
int WiFiClientSecure::read() {
249+
if (!_ssl)
250+
return -1;
251+
234252
return _ssl->read();
235253
}
236254

237255
int WiFiClientSecure::peek() {
256+
if (!_ssl)
257+
return -1;
258+
238259
return _ssl->peek();
239260
}
240261

241262
int WiFiClientSecure::available() {
263+
if (!_ssl)
264+
return 0;
265+
242266
return _ssl->available();
243267
}
244268

269+
uint8_t WiFiClientSecure::connected() {
270+
if (_client->state() == ESTABLISHED)
271+
return 1;
272+
273+
if (!_ssl)
274+
return 0;
275+
276+
return _ssl->available() > 0;
277+
}
278+
245279
void WiFiClientSecure::stop() {
246280
if (_ssl) {
247281
_ssl->unref();
@@ -264,6 +298,9 @@ static bool parseHexNibble(char pb, uint8_t* res) {
264298
}
265299

266300
bool WiFiClientSecure::verify(const char* fp, const char* url) {
301+
if (!_ssl)
302+
return false;
303+
267304
uint8_t sha1[20];
268305
int len = strlen(fp);
269306
int pos = 0;

hardware/esp8266com/esp8266/libraries/ESP8266WiFi/src/WiFiClientSecure.h

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class WiFiClientSecure : public WiFiClient {
4040

4141
bool verify(const char* fingerprint, const char* url);
4242

43+
uint8_t connected() override;
4344
size_t write(const uint8_t *buf, size_t size) override;
4445
int read(uint8_t *buf, size_t size) override;
4546
int available() override;

0 commit comments

Comments
 (0)