From 8c9a3da69553dadfd14dc2c51a1815082a4f726e Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Mon, 25 Oct 2021 18:35:50 +0530 Subject: [PATCH 1/3] mbedtls: disable Diffie-Hellman key exchange modes by default Using these ciphers can constitute a security risk if the server uses a weak prime for the key exchange. Footprint impact: Roughly 3K saved in text+rodata in default https_request example --- components/esp-tls/Kconfig | 4 ++-- components/mbedtls/Kconfig | 13 ++++++++++++- .../mbedtls/port/include/mbedtls/esp_config.h | 4 ++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/components/esp-tls/Kconfig b/components/esp-tls/Kconfig index 636860986e..1c6911c69d 100644 --- a/components/esp-tls/Kconfig +++ b/components/esp-tls/Kconfig @@ -66,8 +66,8 @@ menu "ESP-TLS" bool "Enable PSK verification" select MBEDTLS_PSK_MODES if ESP_TLS_USING_MBEDTLS select MBEDTLS_KEY_EXCHANGE_PSK if ESP_TLS_USING_MBEDTLS - select MBEDTLS_KEY_EXCHANGE_DHE_PSK if ESP_TLS_USING_MBEDTLS - select MBEDTLS_KEY_EXCHANGE_ECDHE_PSK if ESP_TLS_USING_MBEDTLS + select MBEDTLS_KEY_EXCHANGE_DHE_PSK if ESP_TLS_USING_MBEDTLS && MBEDTLS_DHM_C + select MBEDTLS_KEY_EXCHANGE_ECDHE_PSK if ESP_TLS_USING_MBEDTLS && MBEDTLS_ECDH_C select MBEDTLS_KEY_EXCHANGE_RSA_PSK if ESP_TLS_USING_MBEDTLS default n help diff --git a/components/mbedtls/Kconfig b/components/mbedtls/Kconfig index bc7430826b..978727db97 100644 --- a/components/mbedtls/Kconfig +++ b/components/mbedtls/Kconfig @@ -426,7 +426,7 @@ menu "mbedTLS" config MBEDTLS_KEY_EXCHANGE_DHE_PSK bool "Enable DHE-PSK based ciphersuite modes" - depends on MBEDTLS_PSK_MODES + depends on MBEDTLS_PSK_MODES && MBEDTLS_DHM_C default y help Enable to support Diffie-Hellman PSK (pre-shared-key) TLS authentication modes. @@ -454,6 +454,7 @@ menu "mbedTLS" config MBEDTLS_KEY_EXCHANGE_DHE_RSA bool "Enable DHE-RSA based ciphersuite modes" default y + depends on MBEDTLS_DHM_C help Enable to support ciphersuites with prefix TLS-DHE-RSA-WITH- @@ -716,6 +717,16 @@ menu "mbedTLS" bool "Elliptic Curve Ciphers" default y + config MBEDTLS_DHM_C + bool "Diffie-Hellman-Merkle key exchange (DHM)" + default n + help + Enable DHM. Needed to use DHE-xxx TLS ciphersuites. + + Note that the security of Diffie-Hellman key exchanges depends on + a suitable prime being used for the exchange. Please see detailed + warning text about this in file `mbedtls/dhm.h` file. + config MBEDTLS_ECDH_C bool "Elliptic Curve Diffie-Hellman (ECDH)" depends on MBEDTLS_ECP_C diff --git a/components/mbedtls/port/include/mbedtls/esp_config.h b/components/mbedtls/port/include/mbedtls/esp_config.h index f36ebf9bc7..24faec132a 100644 --- a/components/mbedtls/port/include/mbedtls/esp_config.h +++ b/components/mbedtls/port/include/mbedtls/esp_config.h @@ -1601,7 +1601,11 @@ * This module is used by the following key exchanges: * DHE-RSA, DHE-PSK */ +#ifdef CONFIG_MBEDTLS_DHM_C #define MBEDTLS_DHM_C +#else +#undef MBEDTLS_DHM_C +#endif /** * \def MBEDTLS_ECDH_C From b05666a2c256b10e19b66ca95a9829c3fd9e8c8a Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Tue, 26 Oct 2021 13:37:43 +0530 Subject: [PATCH 2/3] examples: blufi: enable MBEDTLS_DHM_C, as blufi has dependency on this --- examples/bluetooth/blufi/sdkconfig.defaults | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/bluetooth/blufi/sdkconfig.defaults b/examples/bluetooth/blufi/sdkconfig.defaults index cc1440f272..d62431b112 100644 --- a/examples/bluetooth/blufi/sdkconfig.defaults +++ b/examples/bluetooth/blufi/sdkconfig.defaults @@ -37,3 +37,4 @@ CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY=n CONFIG_BT_SMP_ENABLE=n CONFIG_BT_BLE_BLUFI_ENABLE=y CONFIG_MBEDTLS_HARDWARE_MPI=n +CONFIG_MBEDTLS_DHM_C=y From f7ed95349f6a95921a66b2376b08557fcf3425ee Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Tue, 26 Oct 2021 12:01:24 +0530 Subject: [PATCH 3/3] mbedtls: fix dynamic buffer feature build --- .../port/dynamic/esp_mbedtls_dynamic_impl.c | 20 +++++++------------ tools/ci/check_copyright_ignore.txt | 1 - 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/components/mbedtls/port/dynamic/esp_mbedtls_dynamic_impl.c b/components/mbedtls/port/dynamic/esp_mbedtls_dynamic_impl.c index 373264452b..ecf2dc3f45 100644 --- a/components/mbedtls/port/dynamic/esp_mbedtls_dynamic_impl.c +++ b/components/mbedtls/port/dynamic/esp_mbedtls_dynamic_impl.c @@ -1,16 +1,8 @@ -// Copyright 2020 Espressif Systems (Shanghai) PTE LTD -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// 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. +/* + * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ #include #include "esp_mbedtls_dynamic_impl.h" @@ -471,8 +463,10 @@ size_t esp_mbedtls_get_crt_size(mbedtls_x509_crt *cert, size_t *num) #ifdef CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA void esp_mbedtls_free_dhm(mbedtls_ssl_context *ssl) { +#ifdef CONFIG_MBEDTLS_DHM_C mbedtls_mpi_free((mbedtls_mpi *)&ssl->conf->dhm_P); mbedtls_mpi_free((mbedtls_mpi *)&ssl->conf->dhm_G); +#endif /* CONFIG_MBEDTLS_DHM_C */ } void esp_mbedtls_free_keycert(mbedtls_ssl_context *ssl) diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index cd75dfc3fb..1b2f5aea03 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -1561,7 +1561,6 @@ components/mbedtls/port/aes/esp_aes_common.c components/mbedtls/port/aes/esp_aes_gcm.c components/mbedtls/port/aes/esp_aes_xts.c components/mbedtls/port/crypto_shared_gdma/esp_crypto_shared_gdma.c -components/mbedtls/port/dynamic/esp_mbedtls_dynamic_impl.c components/mbedtls/port/dynamic/esp_mbedtls_dynamic_impl.h components/mbedtls/port/dynamic/esp_ssl_cli.c components/mbedtls/port/dynamic/esp_ssl_srv.c