mirror of
https://github.com/espressif/esp-idf
synced 2025-03-09 17:19:09 -04:00
fix(esp-tls): Use TLS 1.2 and TLS 1.3 simultaneously
This commit fixes the issue with TLS 1.2 connection when TLS 1.3 is enabled in config.
This commit is contained in:
parent
1c3c89eb43
commit
90d0689331
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2017-2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -80,6 +80,16 @@ typedef enum esp_tls_addr_family {
|
|||||||
ESP_TLS_AF_INET6, /**< IPv6 address family. */
|
ESP_TLS_AF_INET6, /**< IPv6 address family. */
|
||||||
} esp_tls_addr_family_t;
|
} esp_tls_addr_family_t;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @brief ESP-TLS TLS Protocol version
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
ESP_TLS_VER_ANY = 0, /* No preference */
|
||||||
|
ESP_TLS_VER_TLS_1_2 = 0x1, /* (D)TLS 1.2 */
|
||||||
|
ESP_TLS_VER_TLS_1_3 = 0x2, /* (D)TLS 1.3 */
|
||||||
|
ESP_TLS_VER_TLS_MAX, /* to indicate max */
|
||||||
|
} esp_tls_proto_ver_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief ESP-TLS configuration parameters
|
* @brief ESP-TLS configuration parameters
|
||||||
*
|
*
|
||||||
@ -193,6 +203,7 @@ typedef struct esp_tls_cfg {
|
|||||||
#endif /* CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS */
|
#endif /* CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS */
|
||||||
|
|
||||||
esp_tls_addr_family_t addr_family; /*!< The address family to use when connecting to a host. */
|
esp_tls_addr_family_t addr_family; /*!< The address family to use when connecting to a host. */
|
||||||
|
esp_tls_proto_ver_t tls_version; /*!< TLS protocol version of the connection, e.g., TLS 1.2, TLS 1.3 (default - no preference) */
|
||||||
} esp_tls_cfg_t;
|
} esp_tls_cfg_t;
|
||||||
|
|
||||||
#ifdef CONFIG_ESP_TLS_SERVER
|
#ifdef CONFIG_ESP_TLS_SERVER
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2019-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -93,6 +93,24 @@ esp_err_t esp_create_mbedtls_handle(const char *hostname, size_t hostlen, const
|
|||||||
ESP_LOGE(TAG, "Failed to set client configurations, returned [0x%04X] (%s)", esp_ret, esp_err_to_name(esp_ret));
|
ESP_LOGE(TAG, "Failed to set client configurations, returned [0x%04X] (%s)", esp_ret, esp_err_to_name(esp_ret));
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
const esp_tls_proto_ver_t tls_ver = ((esp_tls_cfg_t *)cfg)->tls_version;
|
||||||
|
if (tls_ver == ESP_TLS_VER_TLS_1_3) {
|
||||||
|
#if CONFIG_MBEDTLS_SSL_PROTO_TLS1_3
|
||||||
|
ESP_LOGD(TAG, "Setting TLS version to 0x%4x", MBEDTLS_SSL_VERSION_TLS1_3);
|
||||||
|
mbedtls_ssl_conf_min_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_3);
|
||||||
|
mbedtls_ssl_conf_max_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_3);
|
||||||
|
#else
|
||||||
|
ESP_LOGW(TAG, "TLS 1.3 is not enabled in config, continuing with default TLS protocol");
|
||||||
|
#endif
|
||||||
|
} else if (tls_ver == ESP_TLS_VER_TLS_1_2) {
|
||||||
|
ESP_LOGD(TAG, "Setting TLS version to 0x%4x", MBEDTLS_SSL_VERSION_TLS1_2);
|
||||||
|
mbedtls_ssl_conf_min_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_2);
|
||||||
|
mbedtls_ssl_conf_max_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_2);
|
||||||
|
} else if (tls_ver != ESP_TLS_VER_ANY) {
|
||||||
|
ESP_LOGE(TAG, "Unsupported protocol version");
|
||||||
|
esp_ret = ESP_ERR_INVALID_ARG;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
} else if (tls->role == ESP_TLS_SERVER) {
|
} else if (tls->role == ESP_TLS_SERVER) {
|
||||||
#ifdef CONFIG_ESP_TLS_SERVER
|
#ifdef CONFIG_ESP_TLS_SERVER
|
||||||
esp_ret = set_server_config((esp_tls_cfg_server_t *) cfg, tls);
|
esp_ret = set_server_config((esp_tls_cfg_server_t *) cfg, tls);
|
||||||
@ -121,11 +139,6 @@ esp_err_t esp_create_mbedtls_handle(const char *hostname, size_t hostlen, const
|
|||||||
mbedtls_esp_enable_debug_log(&tls->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
|
mbedtls_esp_enable_debug_log(&tls->conf, CONFIG_MBEDTLS_DEBUG_LEVEL);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_MBEDTLS_SSL_PROTO_TLS1_3
|
|
||||||
mbedtls_ssl_conf_min_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_3);
|
|
||||||
mbedtls_ssl_conf_max_tls_version(&tls->conf, MBEDTLS_SSL_VERSION_TLS1_3);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if ((ret = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) {
|
if ((ret = mbedtls_ssl_setup(&tls->ssl, &tls->conf)) != 0) {
|
||||||
ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%04X", -ret);
|
ESP_LOGE(TAG, "mbedtls_ssl_setup returned -0x%04X", -ret);
|
||||||
mbedtls_print_error_msg(ret);
|
mbedtls_print_error_msg(ret);
|
||||||
@ -229,15 +242,17 @@ ssize_t esp_mbedtls_read(esp_tls_t *tls, char *data, size_t datalen)
|
|||||||
{
|
{
|
||||||
|
|
||||||
ssize_t ret = mbedtls_ssl_read(&tls->ssl, (unsigned char *)data, datalen);
|
ssize_t ret = mbedtls_ssl_read(&tls->ssl, (unsigned char *)data, datalen);
|
||||||
#if CONFIG_MBEDTLS_SSL_PROTO_TLS1_3 && CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS
|
#if CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS
|
||||||
// If a post-handshake message is received, connection state is changed to `MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET`
|
// If a post-handshake message is received, connection state is changed to `MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET`
|
||||||
// Call mbedtls_ssl_read() till state is `MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET` or return code is `MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET`
|
// Call mbedtls_ssl_read() till state is `MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET` or return code is `MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET`
|
||||||
// to process session tickets in TLS 1.3 connection
|
// to process session tickets in TLS 1.3 connection
|
||||||
while (ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET || tls->ssl.MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET) {
|
if (mbedtls_ssl_get_version_number(&tls->ssl) == MBEDTLS_SSL_VERSION_TLS1_3) {
|
||||||
ESP_LOGD(TAG, "got session ticket in TLS 1.3 connection, retry read");
|
while (ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET || tls->ssl.MBEDTLS_PRIVATE(state) == MBEDTLS_SSL_TLS1_3_NEW_SESSION_TICKET) {
|
||||||
ret = mbedtls_ssl_read(&tls->ssl, (unsigned char *)data, datalen);
|
ESP_LOGD(TAG, "got session ticket in TLS 1.3 connection, retry read");
|
||||||
|
ret = mbedtls_ssl_read(&tls->ssl, (unsigned char *)data, datalen);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#endif // CONFIG_MBEDTLS_SSL_PROTO_TLS1_3 && CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS
|
#endif // CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS
|
||||||
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include "esp_system.h"
|
#include "esp_system.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_assert.h"
|
||||||
#include "esp_check.h"
|
#include "esp_check.h"
|
||||||
#include "http_parser.h"
|
#include "http_parser.h"
|
||||||
#include "http_header.h"
|
#include "http_header.h"
|
||||||
@ -19,6 +20,8 @@
|
|||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
#include "esp_http_client.h"
|
#include "esp_http_client.h"
|
||||||
#include "errno.h"
|
#include "errno.h"
|
||||||
|
#include "esp_random.h"
|
||||||
|
#include "esp_tls.h"
|
||||||
|
|
||||||
#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
|
#ifdef CONFIG_ESP_HTTP_CLIENT_ENABLE_HTTPS
|
||||||
#include "esp_transport_ssl.h"
|
#include "esp_transport_ssl.h"
|
||||||
@ -26,6 +29,9 @@
|
|||||||
|
|
||||||
static const char *TAG = "HTTP_CLIENT";
|
static const char *TAG = "HTTP_CLIENT";
|
||||||
|
|
||||||
|
ESP_STATIC_ASSERT((int)ESP_HTTP_CLIENT_TLS_VER_ANY == (int)ESP_TLS_VER_ANY, "Enum mismatch in esp_http_client and esp-tls");
|
||||||
|
ESP_STATIC_ASSERT((int)ESP_HTTP_CLIENT_TLS_VER_MAX <= (int)ESP_TLS_VER_TLS_MAX, "HTTP client supported TLS is not supported in esp-tls");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP Buffer
|
* HTTP Buffer
|
||||||
*/
|
*/
|
||||||
@ -661,6 +667,7 @@ esp_http_client_handle_t esp_http_client_init(const esp_http_client_config_t *co
|
|||||||
esp_transport_ssl_set_client_cert_data_der(ssl, config->client_cert_pem, config->client_cert_len);
|
esp_transport_ssl_set_client_cert_data_der(ssl, config->client_cert_pem, config->client_cert_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
esp_transport_ssl_set_tls_version(ssl, config->tls_version);
|
||||||
|
|
||||||
#if CONFIG_ESP_TLS_USE_DS_PERIPHERAL
|
#if CONFIG_ESP_TLS_USE_DS_PERIPHERAL
|
||||||
if (config->ds_data != NULL) {
|
if (config->ds_data != NULL) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
*/
|
*/
|
||||||
@ -60,6 +60,16 @@ typedef enum {
|
|||||||
HTTP_TRANSPORT_OVER_SSL, /*!< Transport over ssl */
|
HTTP_TRANSPORT_OVER_SSL, /*!< Transport over ssl */
|
||||||
} esp_http_client_transport_t;
|
} esp_http_client_transport_t;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* @brief TLS Protocol version
|
||||||
|
*/
|
||||||
|
typedef enum {
|
||||||
|
ESP_HTTP_CLIENT_TLS_VER_ANY = 0, /* No preference */
|
||||||
|
ESP_HTTP_CLIENT_TLS_VER_TLS_1_2 = 0x1, /* (D)TLS 1.2 */
|
||||||
|
ESP_HTTP_CLIENT_TLS_VER_TLS_1_3 = 0x2, /* (D)TLS 1.3 */
|
||||||
|
ESP_HTTP_CLIENT_TLS_VER_MAX, /* to indicate max */
|
||||||
|
} esp_http_client_proto_ver_t;
|
||||||
|
|
||||||
typedef esp_err_t (*http_event_handle_cb)(esp_http_client_event_t *evt);
|
typedef esp_err_t (*http_event_handle_cb)(esp_http_client_event_t *evt);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -115,6 +125,7 @@ typedef struct {
|
|||||||
size_t client_key_len; /*!< Length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem */
|
size_t client_key_len; /*!< Length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem */
|
||||||
const char *client_key_password; /*!< Client key decryption password string */
|
const char *client_key_password; /*!< Client key decryption password string */
|
||||||
size_t client_key_password_len; /*!< String length of the password pointed to by client_key_password */
|
size_t client_key_password_len; /*!< String length of the password pointed to by client_key_password */
|
||||||
|
esp_http_client_proto_ver_t tls_version; /*!< TLS protocol version of the connection, e.g., TLS 1.2, TLS 1.3 (default - no preference) */
|
||||||
const char *user_agent; /*!< The User Agent string to send with HTTP requests */
|
const char *user_agent; /*!< The User Agent string to send with HTTP requests */
|
||||||
esp_http_client_method_t method; /*!< HTTP Method */
|
esp_http_client_method_t method; /*!< HTTP Method */
|
||||||
int timeout_ms; /*!< Network timeout in milliseconds */
|
int timeout_ms; /*!< Network timeout in milliseconds */
|
||||||
|
@ -1,16 +1,8 @@
|
|||||||
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
|
/*
|
||||||
//
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
*
|
||||||
// you may not use this file except in compliance with the License.
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
// You may obtain a copy of the License at
|
*/
|
||||||
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
#ifndef _ESP_TRANSPORT_SSL_H_
|
#ifndef _ESP_TRANSPORT_SSL_H_
|
||||||
#define _ESP_TRANSPORT_SSL_H_
|
#define _ESP_TRANSPORT_SSL_H_
|
||||||
@ -69,6 +61,14 @@ void esp_transport_ssl_crt_bundle_attach(esp_transport_handle_t t, esp_err_t ((*
|
|||||||
*/
|
*/
|
||||||
void esp_transport_ssl_enable_global_ca_store(esp_transport_handle_t t);
|
void esp_transport_ssl_enable_global_ca_store(esp_transport_handle_t t);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Set TLS protocol version for ESP-TLS connection
|
||||||
|
*
|
||||||
|
* @param t ssl transport
|
||||||
|
* @param[in] tls_version TLS version
|
||||||
|
*/
|
||||||
|
void esp_transport_ssl_set_tls_version(esp_transport_handle_t t, esp_tls_proto_ver_t tls_version);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set SSL client certificate data for mutual authentication (as PEM format).
|
* @brief Set SSL client certificate data for mutual authentication (as PEM format).
|
||||||
* Note that, this function stores the pointer to data, rather than making a copy.
|
* Note that, this function stores the pointer to data, rather than making a copy.
|
||||||
|
@ -342,6 +342,12 @@ void esp_transport_ssl_enable_global_ca_store(esp_transport_handle_t t)
|
|||||||
ssl->cfg.use_global_ca_store = true;
|
ssl->cfg.use_global_ca_store = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void esp_transport_ssl_set_tls_version(esp_transport_handle_t t, esp_tls_proto_ver_t tls_version)
|
||||||
|
{
|
||||||
|
GET_SSL_FROM_TRANSPORT_OR_RETURN(ssl, t);
|
||||||
|
ssl->cfg.tls_version = tls_version;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef CONFIG_ESP_TLS_PSK_VERIFICATION
|
#ifdef CONFIG_ESP_TLS_PSK_VERIFICATION
|
||||||
void esp_transport_ssl_set_psk_key_hint(esp_transport_handle_t t, const psk_hint_key_t *psk_hint_key)
|
void esp_transport_ssl_set_psk_key_hint(esp_transport_handle_t t, const psk_hint_key_t *psk_hint_key)
|
||||||
{
|
{
|
||||||
|
@ -1197,7 +1197,6 @@ components/spi_flash/test/test_mmap.c
|
|||||||
components/spi_flash/test/test_out_of_bounds_write.c
|
components/spi_flash/test/test_out_of_bounds_write.c
|
||||||
components/spi_flash/test/test_partition_ext.c
|
components/spi_flash/test/test_partition_ext.c
|
||||||
components/spi_flash/test/test_spi_flash.c
|
components/spi_flash/test/test_spi_flash.c
|
||||||
components/tcp_transport/include/esp_transport_ssl.h
|
|
||||||
components/tcp_transport/include/esp_transport_ws.h
|
components/tcp_transport/include/esp_transport_ws.h
|
||||||
components/tcp_transport/test/tcp_transport_fixtures.h
|
components/tcp_transport/test/tcp_transport_fixtures.h
|
||||||
components/tcp_transport/test/test_transport_basic.c
|
components/tcp_transport/test/test_transport_basic.c
|
||||||
|
Loading…
x
Reference in New Issue
Block a user