Skip to content

Commit bfe4110

Browse files
committed
WiFiClientSecure: initialize ssl_ctx when loading certificate
Fixes #2470
1 parent f211014 commit bfe4110

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

+19-13
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class SSLContext
9494
ssl_ext_set_host_name(ext, hostName);
9595
ssl_ext_set_max_fragment_size(ext, 4096);
9696
s_io_ctx = ctx;
97+
if (_ssl) {
98+
ssl_free(_ssl);
99+
}
97100
_ssl = ssl_client_new(_ssl_ctx, 0, nullptr, 0, ext);
98101
uint32_t t = millis();
99102

@@ -239,7 +242,7 @@ class SSLContext
239242
}
240243
return 0;
241244
}
242-
DEBUGV(":wcs ra %d", rc);
245+
DEBUGV(":wcs ra %d\r\n", rc);
243246
_read_ptr = data;
244247
_available = rc;
245248
return _available;
@@ -311,13 +314,10 @@ int WiFiClientSecure::connect(const char* name, uint16_t port)
311314

312315
int WiFiClientSecure::_connectSSL(const char* hostName)
313316
{
314-
if (_ssl) {
315-
_ssl->unref();
316-
_ssl = nullptr;
317+
if (!_ssl) {
318+
_ssl = new SSLContext;
319+
_ssl->ref();
317320
}
318-
319-
_ssl = new SSLContext;
320-
_ssl->ref();
321321
_ssl->connect(_client, hostName, 5000);
322322

323323
auto status = ssl_handshake_status(*_ssl);
@@ -553,47 +553,53 @@ bool WiFiClientSecure::verifyCertChain(const char* domain_name)
553553
bool WiFiClientSecure::setCACert(const uint8_t* pk, size_t size)
554554
{
555555
if (!_ssl) {
556-
return false;
556+
_ssl = new SSLContext;
557+
_ssl->ref();
557558
}
558559
return _ssl->loadObject(SSL_OBJ_X509_CACERT, pk, size);
559560
}
560561

561562
bool WiFiClientSecure::setCertificate(const uint8_t* pk, size_t size)
562563
{
563564
if (!_ssl) {
564-
return false;
565+
_ssl = new SSLContext;
566+
_ssl->ref();
565567
}
566568
return _ssl->loadObject(SSL_OBJ_X509_CERT, pk, size);
567569
}
568570

569571
bool WiFiClientSecure::setPrivateKey(const uint8_t* pk, size_t size)
570572
{
571573
if (!_ssl) {
572-
return false;
574+
_ssl = new SSLContext;
575+
_ssl->ref();
573576
}
574577
return _ssl->loadObject(SSL_OBJ_RSA_KEY, pk, size);
575578
}
576579

577580
bool WiFiClientSecure::loadCACert(Stream& stream, size_t size)
578581
{
579582
if (!_ssl) {
580-
return false;
583+
_ssl = new SSLContext;
584+
_ssl->ref();
581585
}
582586
return _ssl->loadObject(SSL_OBJ_X509_CACERT, stream, size);
583587
}
584588

585589
bool WiFiClientSecure::loadCertificate(Stream& stream, size_t size)
586590
{
587591
if (!_ssl) {
588-
return false;
592+
_ssl = new SSLContext;
593+
_ssl->ref();
589594
}
590595
return _ssl->loadObject(SSL_OBJ_X509_CERT, stream, size);
591596
}
592597

593598
bool WiFiClientSecure::loadPrivateKey(Stream& stream, size_t size)
594599
{
595600
if (!_ssl) {
596-
return false;
601+
_ssl = new SSLContext;
602+
_ssl->ref();
597603
}
598604
return _ssl->loadObject(SSL_OBJ_RSA_KEY, stream, size);
599605
}

0 commit comments

Comments
 (0)