Update lib/mbedtls from 2.28.3 to 4.2.0 · niederr/circuitpython@152a5f2 · GitHub
Skip to content

Commit 152a5f2

Browse files
dhalbertclaude
andcommitted
Update lib/mbedtls from 2.28.3 to 4.2.0
Fixes TLS verification against servers addressed by IP: 2.28.3's x509_crt_check_san() matches only dNSName, so iPAddress fell through to "unrecognized type" and any such connection failed with MBEDTLS_ERR_X509_CERT_VERIFY_FAILED on raspberrypi. mbedtls 4.x moves crypto into the nested tf-psa-crypto submodule and splits the configuration in two: MBEDTLS_CONFIG_FILE now covers only TLS and X.509, while algorithms are requested with PSA_WANT_* from TF_PSA_CRYPTO_CONFIG_FILE. The legacy mbedtls_sha256_*() headers also became private. - tools/ci_fetch_deps.py initializes tf-psa-crypto and framework by name rather than with --recursive, which would also pull mldsa-native. - ports/raspberrypi/Makefile generates the PSA driver wrappers with tf-psa-crypto's jinja templates, adding jsonschema to requirements-dev.txt, and globs the source directories instead of listing files. - Randomness comes from the port TRNG via MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG, since 4.x dropped MBEDTLS_ENTROPY_HARDWARE_ALT. This also keeps entropy.c and ctr_drbg.c out of the build. - hashlib uses the public mbedtls_md_*() interface. hashlib.Hash gains a finaliser because mbedtls_md_setup() allocates its digest context. - Protocol versions and crypto are kept in line with espressif, which is already on 4.0.0: TLS 1.2 only, no P-521. ChaCha20-Poly1305 is enabled here and not there, because RP2 has no AES accelerator. - Removes the MBEDTLS_VERSION_MAJOR == 3 branches, which no port ever built. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent b609318 commit 152a5f2

26 files changed

Lines changed: 612 additions & 987 deletions

.gitmodules

Lines changed: 1 addition & 1 deletion

lib/mbedtls

Submodule mbedtls updated 1433 files

lib/mbedtls_config/crt_bundle.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@ static crt_bundle_t s_crt_bundle;
5656
static int crt_check_signature(mbedtls_x509_crt *child, const uint8_t *pub_key_buf, size_t pub_key_len);
5757

5858

59-
#if MBEDTLS_VERSION_MAJOR < 3
60-
#define MBEDTLS_PRIVATE(x) x
61-
#endif
62-
6359
static int crt_check_signature(mbedtls_x509_crt *child, const uint8_t *pub_key_buf, size_t pub_key_len) {
6460
int ret = 0;
6561
mbedtls_x509_crt parent;
@@ -74,33 +70,16 @@ static int crt_check_signature(mbedtls_x509_crt *child, const uint8_t *pub_key_b
7470
}
7571

7672

77-
#if MBEDTLS_VERSION_MAJOR < 4
78-
// Fast check to avoid expensive computations when not necessary
79-
if (!mbedtls_pk_can_do(&parent.pk, child->MBEDTLS_PRIVATE(sig_pk))) {
80-
LOGE(TAG, "Simple compare failed");
81-
ret = -1;
82-
goto cleanup;
83-
}
84-
#endif
85-
8673
md_info = mbedtls_md_info_from_type(child->MBEDTLS_PRIVATE(sig_md));
8774
if ((ret = mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash)) != 0) {
8875
LOGE(TAG, "Internal mbedTLS error %X", ret);
8976
goto cleanup;
9077
}
9178

92-
#if MBEDTLS_VERSION_MAJOR >= 4
9379
if ((ret = mbedtls_pk_verify_ext(
9480
child->MBEDTLS_PRIVATE(sig_pk), &parent.pk,
9581
child->MBEDTLS_PRIVATE(sig_md), hash, mbedtls_md_get_size(md_info),
9682
child->MBEDTLS_PRIVATE(sig).p, child->MBEDTLS_PRIVATE(sig).len)) != 0) {
97-
#else
98-
if ((ret = mbedtls_pk_verify_ext(
99-
child->MBEDTLS_PRIVATE(sig_pk), child->MBEDTLS_PRIVATE(sig_opts), &parent.pk,
100-
child->MBEDTLS_PRIVATE(sig_md), hash, mbedtls_md_get_size(md_info),
101-
child->MBEDTLS_PRIVATE(sig).p, child->MBEDTLS_PRIVATE(sig).len)) != 0) {
102-
#endif
103-
10483
LOGE(TAG, "PK verify failed with error %X", ret);
10584
goto cleanup;
10685
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2026 Dan Halbert for Adafruit Industries
4+
//
5+
// SPDX-License-Identifier: MIT
6+
7+
// Platform glue for ports that build the PSA crypto core for hashlib but not for ssl.
8+
// The ssl equivalent is mbedtls_port.c; the two are mutually exclusive, because
9+
// CIRCUITPY_HASHLIB_MBEDTLS_ONLY means hashlib without ssl.
10+
11+
#include <py/mpconfig.h>
12+
13+
#if CIRCUITPY_HASHLIB_MBEDTLS_ONLY
14+
15+
#include "psa/crypto.h"
16+
17+
#include "shared-bindings/os/__init__.h"
18+
19+
// Required by MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG. psa_crypto_init() initializes the RNG
20+
// subsystem even in a build that only ever hashes, so this has to exist.
21+
psa_status_t mbedtls_psa_external_get_random(
22+
mbedtls_psa_external_random_context_t *context,
23+
uint8_t *output, size_t output_size, size_t *output_length) {
24+
(void)context;
25+
if (!common_hal_os_urandom(output, output_size)) {
26+
return PSA_ERROR_INSUFFICIENT_ENTROPY;
27+
}
28+
*output_length = output_size;
29+
return PSA_SUCCESS;
30+
}
31+
32+
#endif
Lines changed: 92 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,102 @@
1-
/*
2-
* This file is part of the MicroPython project, http://micropython.org/
3-
*
4-
* The MIT License (MIT)
5-
*
6-
* Copyright (c) 2018-2019 Damien P. George
7-
*
8-
* Permission is hereby granted, free of charge, to any person obtaining a copy
9-
* of this software and associated documentation files (the "Software"), to deal
10-
* in the Software without restriction, including without limitation the rights
11-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12-
* copies of the Software, and to permit persons to whom the Software is
13-
* furnished to do so, subject to the following conditions:
14-
*
15-
* The above copyright notice and this permission notice shall be included in
16-
* all copies or substantial portions of the Software.
17-
*
18-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24-
* THE SOFTWARE.
25-
*/
26-
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
27-
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H
28-
29-
// If you want to debug MBEDTLS uncomment the following and
30-
// Pass 3 to mbedtls_debug_set_threshold in socket_new
1+
// This file is part of the CircuitPython project: https://circuitpython.org
2+
//
3+
// SPDX-FileCopyrightText: Copyright (c) 2018-2019 Damien P. George
4+
// SPDX-FileCopyrightText: Copyright (c) 2026 Dan Halbert for Adafruit Industries
5+
//
6+
// SPDX-License-Identifier: MIT
7+
8+
// mbedtls TLS and X.509 configuration, selected with MBEDTLS_CONFIG_FILE.
9+
//
10+
// As of mbedtls 4.0 this file covers only TLS and X.509. Everything cryptographic --
11+
// algorithms, key types, the platform hooks and the RNG -- is configured in
12+
// tf_psa_crypto_config.h next to this file, and selected with
13+
// TF_PSA_CRYPTO_CONFIG_FILE.
14+
15+
#pragma once
16+
17+
// If you want to debug mbedtls, uncomment the following. SSLSocket.c raises the debug
18+
// threshold to 4 when it is set.
3119
// #define MBEDTLS_DEBUG_C
3220

33-
// Set mbedtls configuration
34-
#define MBEDTLS_PLATFORM_MEMORY
35-
#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
36-
#define MBEDTLS_DEPRECATED_REMOVED
37-
#define MBEDTLS_ENTROPY_HARDWARE_ALT
38-
#define MBEDTLS_AES_ROM_TABLES
39-
#define MBEDTLS_CIPHER_MODE_CBC
40-
#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
41-
#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
42-
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
43-
#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
44-
#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
45-
#define MBEDTLS_ECP_DP_SECP192K1_ENABLED
46-
#define MBEDTLS_ECP_DP_SECP224K1_ENABLED
47-
#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
48-
#define MBEDTLS_ECP_DP_BP256R1_ENABLED
49-
#define MBEDTLS_ECP_DP_BP384R1_ENABLED
50-
#define MBEDTLS_ECP_DP_BP512R1_ENABLED
51-
#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
52-
#define MBEDTLS_ECP_NIST_OPTIM
53-
#define MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
54-
#define MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
55-
#define MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
56-
#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
57-
#define MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
58-
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
59-
#define MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
60-
#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
61-
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
62-
#define MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
63-
#define MBEDTLS_NO_PLATFORM_ENTROPY
64-
#define MBEDTLS_PKCS1_V15
65-
#define MBEDTLS_SHA256_SMALLER
66-
#define MBEDTLS_SSL_PROTO_TLS1
67-
#define MBEDTLS_SSL_PROTO_TLS1_1
21+
// Protocol versions
22+
23+
// TLS 1.0 and 1.1 were removed in mbedtls 3.0, and were obsolete long before that.
24+
//
25+
// TLS 1.3 is available in 4.x but is turned off, to match espressif
26+
// It also cost 25648 bytes on Pico W, which has only ~50 KB of firmware
27+
// space left. Enabling it here would also require enabling
28+
// MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED, and PSA_WANT_ALG_HKDF* in
29+
// tf_psa_crypto_config.h for the 1.3 key schedule.
6830
#define MBEDTLS_SSL_PROTO_TLS1_2
31+
32+
// DTLS is deliberately off: common_hal_ssl_sslcontext_wrap_socket() rejects anything
33+
// that is not SOCKETPOOL_SOCK_STREAM, so it could never be reached.
34+
35+
#define MBEDTLS_SSL_CLI_C
36+
#define MBEDTLS_SSL_SRV_C
37+
#define MBEDTLS_SSL_TLS_C
38+
39+
// Key exchanges. Without at least one of these there are no TLS 1.2 ciphersuites at
40+
// all, the ClientHello offers nothing, and the server answers with a fatal
41+
// handshake_failure alert. These two are what espressif enables
42+
// (CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_{RSA,ECDSA}) and cover the public web. The PSK
43+
// and ECJPAKE exchanges that 4.x also still offers are not reachable from the ssl
44+
// module, and the static-RSA and DHE exchanges the mbedtls 2.28 config enabled are
45+
// gone from 4.x upstream.
46+
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
47+
#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
48+
49+
// Extensions
50+
6951
#define MBEDTLS_SSL_SERVER_NAME_INDICATION
52+
#define MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
53+
#define MBEDTLS_SSL_ENCRYPT_THEN_MAC
54+
#define MBEDTLS_SSL_EXTENDED_MASTER_SECRET
55+
56+
// Buffers
7057

71-
// Use a smaller output buffer to reduce size of SSL context
58+
// Accept a full-size record inbound, since we do not control what the peer sends,
59+
// but use a smaller outbound buffer to reduce the SSL context size.
7260
#define MBEDTLS_SSL_MAX_CONTENT_LEN (16384)
7361
#define MBEDTLS_SSL_IN_CONTENT_LEN (MBEDTLS_SSL_MAX_CONTENT_LEN)
7462
#define MBEDTLS_SSL_OUT_CONTENT_LEN (4096)
7563

76-
// Enable mbedtls modules
77-
#define MBEDTLS_AES_C
78-
#define MBEDTLS_ASN1_PARSE_C
79-
#define MBEDTLS_ASN1_WRITE_C
80-
#define MBEDTLS_BASE64_C
81-
#define MBEDTLS_BIGNUM_C
82-
#define MBEDTLS_CIPHER_C
83-
#define MBEDTLS_CTR_DRBG_C
84-
#define MBEDTLS_ECDH_C
85-
#define MBEDTLS_ECDSA_C
86-
#define MBEDTLS_ECP_C
87-
#define MBEDTLS_ENTROPY_C
88-
#define MBEDTLS_ERROR_C
89-
#define MBEDTLS_GCM_C
90-
#define MBEDTLS_MD_C
91-
#define MBEDTLS_MD5_C
92-
#define MBEDTLS_OID_C
93-
#define MBEDTLS_PKCS5_C
94-
#define MBEDTLS_PEM_PARSE_C
95-
#define MBEDTLS_PK_C
96-
#define MBEDTLS_PK_HAVE_ECC_KEYS
97-
#define MBEDTLS_PK_PARSE_C
98-
#define MBEDTLS_PLATFORM_C
99-
#define MBEDTLS_RSA_C
100-
#define MBEDTLS_SHA1_C
101-
#define MBEDTLS_SHA256_C
102-
#define MBEDTLS_SHA512_C
103-
#define MBEDTLS_SSL_CLI_C
104-
#define MBEDTLS_SSL_PROTO_DTLS
105-
#define MBEDTLS_SSL_SRV_C
106-
#define MBEDTLS_SSL_TLS_C
107-
#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE
108-
#define MBEDTLS_X509_CRT_PARSE_C
64+
// X.509
65+
10966
#define MBEDTLS_X509_USE_C
110-
#define MBEDTLS_HAVE_TIME
111-
#define MBEDTLS_DHM_C // needed by DHE_PSK
112-
#undef MBEDTLS_HAVE_TIME_DATE
113-
114-
// Memory allocation hooks
115-
#include <stdlib.h>
116-
#include <stdio.h>
117-
void *m_tracked_calloc(size_t nmemb, size_t size);
118-
void m_tracked_free(void *ptr);
119-
#define MBEDTLS_PLATFORM_STD_CALLOC m_tracked_calloc
120-
#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free
121-
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf
122-
123-
// Time hook
124-
#include <time.h>
125-
time_t rp2_rtctime_seconds(time_t *timer);
126-
#define MBEDTLS_PLATFORM_TIME_MACRO rp2_rtctime_seconds
127-
128-
#include "mbedtls/check_config.h"
129-
130-
#endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */
67+
#define MBEDTLS_X509_CRT_PARSE_C
68+
#define MBEDTLS_X509_RSASSA_PSS_SUPPORT
69+
70+
// MBEDTLS_HAVE_TIME_DATE is deliberately left off, so certificate notBefore/notAfter
71+
// are not checked (see the BADCERT_EXPIRED/BADCERT_FUTURE tests in x509_crt.c, which
72+
// are compiled out without it). CircuitPython does not know the wall clock time unless
73+
// the program sets it explicitly, which often does not happen; with an unset clock,
74+
// checking the dates would reject valid certificates rather than catch expired ones.
75+
// espressif does not set CONFIG_MBEDTLS_HAVE_TIME_DATE either.
76+
//
77+
// Nothing else we enable consumes time -- no session tickets, no context
78+
// serialization, no DTLS, no TLS 1.3 -- so MBEDTLS_HAVE_TIME is off as well, and
79+
// mbedtls_port.c needs neither a wall clock nor mbedtls_ms_time().
80+
81+
// Error strings
82+
83+
// SSLSocket.c keys off MBEDTLS_ERROR_C to decide whether to put a message on the
84+
// OSError it raises. mbedtls's own error.c is not built; lib/mbedtls_errors supplies
85+
// a smaller mbedtls_strerror() instead.
86+
#define MBEDTLS_ERROR_C
87+
88+
// Sanity checks
89+
//
90+
// mbedtls's own mbedtls_check_config.h only validates the prerequisites of options
91+
// that are enabled, so it says nothing when a whole category is missing. This is where
92+
// TF_PSA_CRYPTO_CONFIG_FILE replacing psa/crypto_config.h rather than overlaying it
93+
// bites: every default we rely on has to be restated here, and forgetting one produces
94+
// a build that compiles and links but cannot complete a handshake.
95+
#if !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
96+
!defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
97+
!defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) && \
98+
!defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
99+
!defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
100+
#error "No MBEDTLS_KEY_EXCHANGE_* enabled: ciphersuite_definitions[] would be empty, " \
101+
"so the ClientHello would offer nothing and every TLS 1.2 handshake would fail."
102+
#endif

lib/mbedtls_config/mbedtls_port.c

Lines changed: 20 additions & 44 deletions

0 commit comments

Comments
 (0)