Skip to content

Commit 62d16ce

Browse files
committed
Fix TLS for v5.4.1 update
Cert bundle code in lib/mbedtls_config was being used, and assumed the older ESP-IDF internal bundle format, which was changed in ESP-IDF v5.4. Added a wrapper around the ESP-IDF bundle routines, and stopped using the shared bundle code. Also fixed extraneous blank lines in mbedtls logging and removed an extraneous, unused, damaged .h.
1 parent 1942d5e commit 62d16ce

File tree

4 files changed

+45
-75
lines changed

4 files changed

+45
-75
lines changed

lib/mbedtls_config/mbedtls_config_port.h

Lines changed: 0 additions & 67 deletions
This file was deleted.

ports/espressif/Makefile

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ ifeq ($(DEBUG), 1)
175175
OPTIMIZATION_FLAGS ?= -Og
176176
CFLAGS += -DDEBUG
177177
endif
178-
# You may want to enable these flags to make setting breakpoints easier.
179-
# CFLAGS += -fno-inline -fno-ipa-sra
178+
# You may want to enable these flags to make setting breakpoints easier.
179+
# CFLAGS += -fno-inline -fno-ipa-sra
180180
else
181181
CFLAGS += -DNDEBUG
182-
# RISC-V is larger than xtensa
183-
# Use -Os for RISC-V when it overflows
182+
# RISC-V is larger than xtensa
183+
# Use -Os for RISC-V when it overflows
184184
ifeq ($(IDF_TARGET_ARCH),riscv)
185185
OPTIMIZATION_FLAGS ?= -Os
186186
else
@@ -384,7 +384,7 @@ SRC_C += \
384384
peripherals/$(IDF_TARGET)/pins.c
385385

386386
ifeq ($(CIRCUITPY_SSL),1)
387-
SRC_C += lib/mbedtls_config/crt_bundle.c
387+
SRC_C += common-hal/ssl/crt_bundle.c
388388
endif
389389

390390
SRC_C += $(wildcard common-hal/espidf/*.c)
@@ -608,8 +608,8 @@ ifneq ($(CIRCUITPY_BLEIO_NATIVE),0)
608608

609609
ESP_IDF_COMPONENTS_LINK += bt esp_phy esp_security
610610
ifeq ($(BLE_IMPL),esp32)
611-
# BLE will hang the ESP32 and trigger an interrupt watchdog without this undefined symbol at
612-
# link because a weak version of the interrupt that BLE uses will be linked incorrectly.
611+
# BLE will hang the ESP32 and trigger an interrupt watchdog without this undefined symbol at
612+
# link because a weak version of the interrupt that BLE uses will be linked incorrectly.
613613
REGISTRATION_FUNCTIONS += -u ld_include_hli_vectors_bt
614614
BINARY_BLOBS += esp-idf/components/bt/controller/lib_esp32/$(IDF_TARGET)/libbtdm_app.a
615615
endif
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2025 Dan Halbert for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
// In ESP-IDF v5.4, Espressif changed the format of the in-flash cert bundle, which
8+
// made lib/mbedtls_config/crt_bundle.c no longer work. Rather than update that,
9+
// just wrap those functions and use the ESP-IDF versions.
10+
11+
#include "py/mperrno.h"
12+
#include "mbedtls/x509_crt.h"
13+
#include "lib/mbedtls_config/crt_bundle.h"
14+
#include "esp_crt_bundle.h"
15+
16+
static int convert_esp_err(esp_err_t ret) {
17+
switch (ret) {
18+
case ESP_OK:
19+
return 0;
20+
default:
21+
// Right now esp_crt_bundle.c doesn't return very specific errors.
22+
case ESP_ERR_INVALID_ARG:
23+
return -MP_EINVAL;
24+
}
25+
}
26+
27+
int crt_bundle_attach(mbedtls_ssl_config *ssl_conf) {
28+
return convert_esp_err(esp_crt_bundle_attach(ssl_conf));
29+
}
30+
31+
void crt_bundle_detach(mbedtls_ssl_config *conf) {
32+
esp_crt_bundle_detach(conf);
33+
}
34+
35+
int crt_bundle_set(const uint8_t *x509_bundle, size_t bundle_size) {
36+
return convert_esp_err(esp_crt_bundle_set(x509_bundle, bundle_size));
37+
}

shared-module/ssl/SSLSocket.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
static void mbedtls_debug(void *ctx, int level, const char *file, int line, const char *str) {
3838
(void)ctx;
3939
(void)level;
40-
mp_printf(&mp_plat_print, "DBG:%s:%04d: %s\n", file, line, str);
40+
mp_printf(&mp_plat_print, "DBG:%s:%04d: %s", file, line, str);
4141
}
4242
#define DEBUG_PRINT(fmt, ...) mp_printf(&mp_plat_print, "DBG:%s:%04d: " fmt "\n", __FILE__, __LINE__,##__VA_ARGS__)
4343
#else

0 commit comments

Comments
 (0)