Skip to content

Commit 46257c0

Browse files
a-c-sreedhar-reddyme-no-dev
authored andcommitted
handshake in ssl_client.cpp (#2044)
* issue #2041 * handshake timeout * seconds to milliseconds
1 parent 0640964 commit 46257c0

File tree

4 files changed

+14
-3
lines changed

4 files changed

+14
-3
lines changed

Diff for: libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ WiFiClientSecure::WiFiClientSecure()
3535
sslclient = new sslclient_context;
3636
ssl_init(sslclient);
3737
sslclient->socket = -1;
38-
38+
sslclient->handshake_timeout = 120000;
3939
_CA_cert = NULL;
4040
_cert = NULL;
4141
_private_key = NULL;
@@ -50,6 +50,7 @@ WiFiClientSecure::WiFiClientSecure(int sock)
5050
sslclient = new sslclient_context;
5151
ssl_init(sslclient);
5252
sslclient->socket = sock;
53+
sslclient->handshake_timeout = 120000;
5354

5455
if (sock >= 0) {
5556
_connected = true;
@@ -285,3 +286,8 @@ int WiFiClientSecure::lastError(char *buf, const size_t size)
285286
snprintf(buf, size, "%s", error_buf);
286287
return _lastError;
287288
}
289+
290+
void WiFiClientSecure::setHandshakeTimeout(unsigned long handshake_timeout)
291+
{
292+
sslclient->handshake_timeout = handshake_timeout * 1000;
293+
}

Diff for: libraries/WiFiClientSecure/src/WiFiClientSecure.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class WiFiClientSecure : public WiFiClient
6262
bool loadCertificate(Stream& stream, size_t size);
6363
bool loadPrivateKey(Stream& stream, size_t size);
6464
bool verify(const char* fingerprint, const char* domain_name);
65+
void setHandshakeTimeout(unsigned long handshake_timeout);
6566

6667
operator bool()
6768
{

Diff for: libraries/WiFiClientSecure/src/ssl_client.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,14 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
158158
mbedtls_ssl_set_bio(&ssl_client->ssl_ctx, &ssl_client->socket, mbedtls_net_send, mbedtls_net_recv, NULL );
159159

160160
log_v("Performing the SSL/TLS handshake...");
161-
161+
unsigned long handshake_start_time=millis();
162162
while ((ret = mbedtls_ssl_handshake(&ssl_client->ssl_ctx)) != 0) {
163163
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
164164
return handle_error(ret);
165165
}
166-
vTaskDelay(10 / portTICK_PERIOD_MS);
166+
if((millis()-handshake_start_time)>ssl_client->handshake_timeout)
167+
return -1;
168+
vTaskDelay(10 / portTICK_PERIOD_MS);
167169
}
168170

169171

Diff for: libraries/WiFiClientSecure/src/ssl_client.h

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ typedef struct sslclient_context {
2323
mbedtls_x509_crt ca_cert;
2424
mbedtls_x509_crt client_cert;
2525
mbedtls_pk_context client_key;
26+
27+
unsigned long handshake_timeout;
2628
} sslclient_context;
2729

2830

0 commit comments

Comments
 (0)