Skip to content

Commit 877057d

Browse files
Merge branch 'fix/fix_timeout_issue_in_https_server' into 'master'
fix(esp-tls): Fixed the server session create API Closes IDFGH-14201 See merge request espressif/esp-idf!36519
2 parents 26a1b69 + d31654d commit 877057d

File tree

7 files changed

+31
-2
lines changed

7 files changed

+31
-2
lines changed

components/esp-tls/esp_tls.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ typedef struct esp_tls_cfg_server {
307307
bool use_secure_element; /*!< Enable this option to use secure element or
308308
atecc608a chip */
309309

310+
uint32_t tls_handshake_timeout_ms; /*!< TLS handshake timeout in milliseconds.
311+
Note: If this value is not set, by default the timeout is
312+
set to 10 seconds. If you wish that the session should wait
313+
indefinitely then please use a larger value e.g., INT32_MAX */
310314

311315
#if defined(CONFIG_ESP_TLS_SERVER_SESSION_TICKETS)
312316
esp_tls_server_session_ticket_ctx_t * ticket_ctx; /*!< Session ticket generation context.

components/esp-tls/esp_tls_errors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern "C" {
3232
#define ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT (ESP_ERR_ESP_TLS_BASE + 0x06) /*!< new connection in esp_tls_low_level_conn connection timeouted */
3333
#define ESP_ERR_ESP_TLS_SE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x07) /*< esp-tls use Secure Element returned failed */
3434
#define ESP_ERR_ESP_TLS_TCP_CLOSED_FIN (ESP_ERR_ESP_TLS_BASE + 0x08) /*< esp-tls's TPC transport connection has benn closed (in a clean way) */
35-
35+
#define ESP_ERR_ESP_TLS_SERVER_HANDSHAKE_TIMEOUT (ESP_ERR_ESP_TLS_BASE + 0x09) /*!< TLS handshake timeout */
3636
/* mbedtls specific error codes */
3737
#define ESP_ERR_MBEDTLS_CERT_PARTLY_OK (ESP_ERR_ESP_TLS_BASE + 0x10) /*!< mbedtls parse certificates was partly successful */
3838
#define ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED (ESP_ERR_ESP_TLS_BASE + 0x11) /*!< mbedtls api returned error */

components/esp-tls/esp_tls_mbedtls.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2019-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -16,6 +16,7 @@
1616
#include "esp_tls_mbedtls.h"
1717
#include "esp_tls_private.h"
1818
#include "esp_tls_error_capture_internal.h"
19+
#include "esp_tls_platform_port.h"
1920
#include <errno.h>
2021
#include "esp_log.h"
2122
#include "esp_check.h"
@@ -928,10 +929,24 @@ int esp_mbedtls_server_session_create(esp_tls_cfg_server_t *cfg, int sockfd, esp
928929
if ((ret = esp_mbedtls_server_session_init(cfg, sockfd, tls)) != 0) {
929930
return ret;
930931
}
932+
933+
uint64_t timeout_ms;
934+
if (cfg->tls_handshake_timeout_ms == 0) {
935+
timeout_ms = ESP_TLS_DEFAULT_SERVER_HANDSHAKE_TIMEOUT_MS;
936+
} else {
937+
timeout_ms = cfg->tls_handshake_timeout_ms;
938+
}
939+
uint64_t start_time = esp_tls_get_platform_time();
940+
931941
while ((ret = esp_mbedtls_server_session_continue_async(tls)) != 0) {
932942
if (ret != ESP_TLS_ERR_SSL_WANT_READ && ret != ESP_TLS_ERR_SSL_WANT_WRITE) {
933943
return ret;
934944
}
945+
uint64_t elapsed_time_us = esp_tls_get_platform_time() - start_time;
946+
if ((elapsed_time_us / 1000) > timeout_ms) {
947+
ESP_LOGD(TAG, "Server handshake timed out");
948+
return ESP_ERR_ESP_TLS_SERVER_HANDSHAKE_TIMEOUT;
949+
}
935950
}
936951
return ret;
937952
}

components/esp-tls/private_include/esp_tls_private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,5 @@ typedef esp_err_t (*set_server_config_func_ptr) (esp_tls_cfg_server_t *cfg, esp_
103103
typedef struct esp_tls_server_params {
104104
set_server_config_func_ptr set_server_cfg;
105105
} esp_tls_server_params_t;
106+
107+
#define ESP_TLS_DEFAULT_SERVER_HANDSHAKE_TIMEOUT_MS (10000) /*!< Default handshake timeout in milliseconds */

components/esp_common/src/esp_err_to_name.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,9 @@ static const esp_err_msg_t esp_err_msg_table[] = {
694694
# ifdef ESP_ERR_ESP_TLS_TCP_CLOSED_FIN
695695
ERR_TBL_IT(ESP_ERR_ESP_TLS_TCP_CLOSED_FIN), /* 32776 0x8008 */
696696
# endif
697+
# ifdef ESP_ERR_ESP_TLS_SERVER_HANDSHAKE_TIMEOUT
698+
ERR_TBL_IT(ESP_ERR_ESP_TLS_SERVER_HANDSHAKE_TIMEOUT), /* 32777 0x8009 TLS handshake timeout */
699+
# endif
697700
# ifdef ESP_ERR_MBEDTLS_CERT_PARTLY_OK
698701
ERR_TBL_IT(ESP_ERR_MBEDTLS_CERT_PARTLY_OK), /* 32784 0x8010 mbedtls parse certificates was partly successful */
699702
# endif

components/esp_https_server/include/esp_https_server.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ struct httpd_ssl_config {
132132
* Used for negotiating during the TLS handshake, first one the client supports is selected.
133133
* The data structure must live as long as the https server itself */
134134
const char** alpn_protos;
135+
136+
/** TLS handshake timeout in milliseconds, default timeout is 10 seconds if not set */
137+
uint32_t tls_handshake_timeout_ms;
135138
};
136139

137140
typedef struct httpd_ssl_config httpd_ssl_config_t;
@@ -192,6 +195,7 @@ typedef struct httpd_ssl_config httpd_ssl_config_t;
192195
.ssl_userdata = NULL, \
193196
.cert_select_cb = NULL, \
194197
.alpn_protos = NULL, \
198+
.tls_handshake_timeout_ms = 0 \
195199
}
196200

197201
/**

components/esp_https_server/src/https_server.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ static esp_err_t create_secure_context(const struct httpd_ssl_config *config, ht
277277

278278
cfg->userdata = config->ssl_userdata;
279279
cfg->alpn_protos = config->alpn_protos;
280+
cfg->tls_handshake_timeout_ms = config->tls_handshake_timeout_ms;
280281

281282
#if defined(CONFIG_ESP_HTTPS_SERVER_CERT_SELECT_HOOK)
282283
cfg->cert_select_cb = config->cert_select_cb;

0 commit comments

Comments
 (0)