Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,12 @@ jobs:
pip install --break-system-packages wheel
# requirements-dev.txt doesn't install on windows. (with msys2 python)
# instead, pick a subset for what we want to do
pip install --break-system-packages cascadetoml jinja2 typer click intelhex
# jsonschema is needed by mbedtls' generate_driver_wrappers.py. Pin it below 4.18:
# 4.18 and later depend on rpds-py, a Rust extension with no wheel for msys2 python
# (SOABI cpython-3xx-x86_64-cygwin), and maturin refuses to build it from source
# ("Unsupported platform: x86_64-cygwin"). 4.17.3 uses attrs and pyrsistent instead,
# both of which install without a Rust toolchain.
pip install --break-system-packages cascadetoml jinja2 typer click intelhex 'jsonschema<4.18'
# check that installed packages work....?
which python; python --version; python -c "import cascadetoml"
which python3; python3 --version; python3 -c "import cascadetoml"
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
branch = circuitpython9
[submodule "lib/mbedtls"]
path = lib/mbedtls
url = https://github.com/ARMmbed/mbedtls.git
url = https://github.com/Mbed-TLS/mbedtls.git
[submodule "frozen/Adafruit_CircuitPython_UC8151D"]
path = frozen/Adafruit_CircuitPython_UC8151D
url = https://github.com/adafruit/Adafruit_CircuitPython_UC8151D
Expand Down
2 changes: 1 addition & 1 deletion lib/mbedtls
Submodule mbedtls updated 1433 files
21 changes: 0 additions & 21 deletions lib/mbedtls_config/crt_bundle.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ static crt_bundle_t s_crt_bundle;
static int crt_check_signature(mbedtls_x509_crt *child, const uint8_t *pub_key_buf, size_t pub_key_len);


#if MBEDTLS_VERSION_MAJOR < 3
#define MBEDTLS_PRIVATE(x) x
#endif

static int crt_check_signature(mbedtls_x509_crt *child, const uint8_t *pub_key_buf, size_t pub_key_len) {
int ret = 0;
mbedtls_x509_crt parent;
Expand All @@ -74,33 +70,16 @@ static int crt_check_signature(mbedtls_x509_crt *child, const uint8_t *pub_key_b
}


#if MBEDTLS_VERSION_MAJOR < 4
// Fast check to avoid expensive computations when not necessary
if (!mbedtls_pk_can_do(&parent.pk, child->MBEDTLS_PRIVATE(sig_pk))) {
LOGE(TAG, "Simple compare failed");
ret = -1;
goto cleanup;
}
#endif

md_info = mbedtls_md_info_from_type(child->MBEDTLS_PRIVATE(sig_md));
if ((ret = mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash)) != 0) {
LOGE(TAG, "Internal mbedTLS error %X", ret);
goto cleanup;
}

#if MBEDTLS_VERSION_MAJOR >= 4
if ((ret = mbedtls_pk_verify_ext(
child->MBEDTLS_PRIVATE(sig_pk), &parent.pk,
child->MBEDTLS_PRIVATE(sig_md), hash, mbedtls_md_get_size(md_info),
child->MBEDTLS_PRIVATE(sig).p, child->MBEDTLS_PRIVATE(sig).len)) != 0) {
#else
if ((ret = mbedtls_pk_verify_ext(
child->MBEDTLS_PRIVATE(sig_pk), child->MBEDTLS_PRIVATE(sig_opts), &parent.pk,
child->MBEDTLS_PRIVATE(sig_md), hash, mbedtls_md_get_size(md_info),
child->MBEDTLS_PRIVATE(sig).p, child->MBEDTLS_PRIVATE(sig).len)) != 0) {
#endif

LOGE(TAG, "PK verify failed with error %X", ret);
goto cleanup;
}
Expand Down
32 changes: 32 additions & 0 deletions lib/mbedtls_config/hashlib_psa_port.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2026 Dan Halbert for Adafruit Industries
//
// SPDX-License-Identifier: MIT

// Platform glue for ports that build the PSA crypto core for hashlib but not for ssl.
// The ssl equivalent is mbedtls_port.c; the two are mutually exclusive, because
// CIRCUITPY_HASHLIB_MBEDTLS_ONLY means hashlib without ssl.

#include <py/mpconfig.h>

#if CIRCUITPY_HASHLIB_MBEDTLS_ONLY

#include "psa/crypto.h"

#include "shared-bindings/os/__init__.h"

// Required by MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG. psa_crypto_init() initializes the RNG
// subsystem even in a build that only ever hashes, so this has to exist.
psa_status_t mbedtls_psa_external_get_random(
mbedtls_psa_external_random_context_t *context,
uint8_t *output, size_t output_size, size_t *output_length) {
(void)context;
if (!common_hal_os_urandom(output, output_size)) {
return PSA_ERROR_INSUFFICIENT_ENTROPY;
}
*output_length = output_size;
return PSA_SUCCESS;
}

#endif
212 changes: 92 additions & 120 deletions lib/mbedtls_config/mbedtls_config.h
Original file line number Diff line number Diff line change
@@ -1,130 +1,102 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018-2019 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef MICROPY_INCLUDED_MBEDTLS_CONFIG_H
#define MICROPY_INCLUDED_MBEDTLS_CONFIG_H

// If you want to debug MBEDTLS uncomment the following and
// Pass 3 to mbedtls_debug_set_threshold in socket_new
// This file is part of the CircuitPython project: https://circuitpython.org
//
// SPDX-FileCopyrightText: Copyright (c) 2018-2019 Damien P. George
// SPDX-FileCopyrightText: Copyright (c) 2026 Dan Halbert for Adafruit Industries
//
// SPDX-License-Identifier: MIT

// mbedtls TLS and X.509 configuration, selected with MBEDTLS_CONFIG_FILE.
//
// As of mbedtls 4.0 this file covers only TLS and X.509. Everything cryptographic --
// algorithms, key types, the platform hooks and the RNG -- is configured in
// tf_psa_crypto_config.h next to this file, and selected with
// TF_PSA_CRYPTO_CONFIG_FILE.

#pragma once

// If you want to debug mbedtls, uncomment the following. SSLSocket.c raises the debug
// threshold to 4 when it is set.
// #define MBEDTLS_DEBUG_C

// Set mbedtls configuration
#define MBEDTLS_PLATFORM_MEMORY
#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
#define MBEDTLS_DEPRECATED_REMOVED
#define MBEDTLS_ENTROPY_HARDWARE_ALT
#define MBEDTLS_AES_ROM_TABLES
#define MBEDTLS_CIPHER_MODE_CBC
#define MBEDTLS_ECP_DP_SECP192R1_ENABLED
#define MBEDTLS_ECP_DP_SECP224R1_ENABLED
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
#define MBEDTLS_ECP_DP_SECP384R1_ENABLED
#define MBEDTLS_ECP_DP_SECP521R1_ENABLED
#define MBEDTLS_ECP_DP_SECP192K1_ENABLED
#define MBEDTLS_ECP_DP_SECP224K1_ENABLED
#define MBEDTLS_ECP_DP_SECP256K1_ENABLED
#define MBEDTLS_ECP_DP_BP256R1_ENABLED
#define MBEDTLS_ECP_DP_BP384R1_ENABLED
#define MBEDTLS_ECP_DP_BP512R1_ENABLED
#define MBEDTLS_ECP_DP_CURVE25519_ENABLED
#define MBEDTLS_ECP_NIST_OPTIM
#define MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED
#define MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
#define MBEDTLS_KEY_EXCHANGE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED
#define MBEDTLS_NO_PLATFORM_ENTROPY
#define MBEDTLS_PKCS1_V15
#define MBEDTLS_SHA256_SMALLER
#define MBEDTLS_SSL_PROTO_TLS1
#define MBEDTLS_SSL_PROTO_TLS1_1
// Protocol versions

// TLS 1.0 and 1.1 were removed in mbedtls 3.0, and were obsolete long before that.
//
// TLS 1.3 is available in 4.x but is turned off, to match espressif
// It also cost 25648 bytes on Pico W, which has only ~50 KB of firmware
// space left. Enabling it here would also require enabling
// MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED, and PSA_WANT_ALG_HKDF* in
// tf_psa_crypto_config.h for the 1.3 key schedule.
#define MBEDTLS_SSL_PROTO_TLS1_2

// DTLS is deliberately off: common_hal_ssl_sslcontext_wrap_socket() rejects anything
// that is not SOCKETPOOL_SOCK_STREAM, so it could never be reached.

#define MBEDTLS_SSL_CLI_C
#define MBEDTLS_SSL_SRV_C
#define MBEDTLS_SSL_TLS_C

// Key exchanges. Without at least one of these there are no TLS 1.2 ciphersuites at
// all, the ClientHello offers nothing, and the server answers with a fatal
// handshake_failure alert. These two are what espressif enables
// (CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_{RSA,ECDSA}) and cover the public web. The PSK
// and ECJPAKE exchanges that 4.x also still offers are not reachable from the ssl
// module, and the static-RSA and DHE exchanges the mbedtls 2.28 config enabled are
// gone from 4.x upstream.
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED

// Extensions

#define MBEDTLS_SSL_SERVER_NAME_INDICATION
#define MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
#define MBEDTLS_SSL_ENCRYPT_THEN_MAC
#define MBEDTLS_SSL_EXTENDED_MASTER_SECRET

// Buffers

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

// Enable mbedtls modules
#define MBEDTLS_AES_C
#define MBEDTLS_ASN1_PARSE_C
#define MBEDTLS_ASN1_WRITE_C
#define MBEDTLS_BASE64_C
#define MBEDTLS_BIGNUM_C
#define MBEDTLS_CIPHER_C
#define MBEDTLS_CTR_DRBG_C
#define MBEDTLS_ECDH_C
#define MBEDTLS_ECDSA_C
#define MBEDTLS_ECP_C
#define MBEDTLS_ENTROPY_C
#define MBEDTLS_ERROR_C
#define MBEDTLS_GCM_C
#define MBEDTLS_MD_C
#define MBEDTLS_MD5_C
#define MBEDTLS_OID_C
#define MBEDTLS_PKCS5_C
#define MBEDTLS_PEM_PARSE_C
#define MBEDTLS_PK_C
#define MBEDTLS_PK_HAVE_ECC_KEYS
#define MBEDTLS_PK_PARSE_C
#define MBEDTLS_PLATFORM_C
#define MBEDTLS_RSA_C
#define MBEDTLS_SHA1_C
#define MBEDTLS_SHA256_C
#define MBEDTLS_SHA512_C
#define MBEDTLS_SSL_CLI_C
#define MBEDTLS_SSL_PROTO_DTLS
#define MBEDTLS_SSL_SRV_C
#define MBEDTLS_SSL_TLS_C
#define MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE
#define MBEDTLS_X509_CRT_PARSE_C
// X.509

#define MBEDTLS_X509_USE_C
#define MBEDTLS_HAVE_TIME
#define MBEDTLS_DHM_C // needed by DHE_PSK
#undef MBEDTLS_HAVE_TIME_DATE

// Memory allocation hooks
#include <stdlib.h>
#include <stdio.h>
void *m_tracked_calloc(size_t nmemb, size_t size);
void m_tracked_free(void *ptr);
#define MBEDTLS_PLATFORM_STD_CALLOC m_tracked_calloc
#define MBEDTLS_PLATFORM_STD_FREE m_tracked_free
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf

// Time hook
#include <time.h>
time_t rp2_rtctime_seconds(time_t *timer);
#define MBEDTLS_PLATFORM_TIME_MACRO rp2_rtctime_seconds

#include "mbedtls/check_config.h"

#endif /* MICROPY_INCLUDED_MBEDTLS_CONFIG_H */
#define MBEDTLS_X509_CRT_PARSE_C
#define MBEDTLS_X509_RSASSA_PSS_SUPPORT

// MBEDTLS_HAVE_TIME_DATE is deliberately left off, so certificate notBefore/notAfter
// are not checked (see the BADCERT_EXPIRED/BADCERT_FUTURE tests in x509_crt.c, which
// are compiled out without it). CircuitPython does not know the wall clock time unless
// the program sets it explicitly, which often does not happen; with an unset clock,
// checking the dates would reject valid certificates rather than catch expired ones.
// espressif does not set CONFIG_MBEDTLS_HAVE_TIME_DATE either.
//
// Nothing else we enable consumes time -- no session tickets, no context
// serialization, no DTLS, no TLS 1.3 -- so MBEDTLS_HAVE_TIME is off as well, and
// mbedtls_port.c needs neither a wall clock nor mbedtls_ms_time().

// Error strings

// SSLSocket.c keys off MBEDTLS_ERROR_C to decide whether to put a message on the
// OSError it raises. mbedtls's own error.c is not built; lib/mbedtls_errors supplies
// a smaller mbedtls_strerror() instead.
#define MBEDTLS_ERROR_C

// Sanity checks
//
// mbedtls's own mbedtls_check_config.h only validates the prerequisites of options
// that are enabled, so it says nothing when a whole category is missing. This is where
// TF_PSA_CRYPTO_CONFIG_FILE replacing psa/crypto_config.h rather than overlaying it
// bites: every default we rely on has to be restated here, and forgetting one produces
// a build that compiles and links but cannot complete a handshake.
#if !defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) && \
!defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) && \
!defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) && \
!defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
!defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
#error "No MBEDTLS_KEY_EXCHANGE_* enabled: ciphersuite_definitions[] would be empty, " \
"so the ClientHello would offer nothing and every TLS 1.2 handshake would fail."
#endif
Loading
Loading