11//
2- // SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
2+ // SPDX-FileCopyrightText: 2021-2026 Espressif Systems (Shanghai) CO LTD
33//
44// SPDX-License-Identifier: BSL-1.0
55//
66#pragma once
77
8+ #include " mbedtls/version.h"
89#include " mbedtls/ssl.h"
9- #include " mbedtls/entropy.h"
10- #include " mbedtls/ctr_drbg.h"
1110#include " mbedtls/error.h"
1211#include " mbedtls/esp_debug.h"
1312#include " esp_log.h"
1413
14+ // Determine mbedTLS major version (ESP-IDF may define MBEDTLS_MAJOR_VERSION via compile flags)
15+ #if defined(MBEDTLS_MAJOR_VERSION)
16+ #define ASIO_MBEDTLS_MAJOR MBEDTLS_MAJOR_VERSION
17+ #elif defined(MBEDTLS_VERSION_MAJOR)
18+ #define ASIO_MBEDTLS_MAJOR MBEDTLS_VERSION_MAJOR
19+ #elif defined(MBEDTLS_VERSION_NUMBER)
20+ #define ASIO_MBEDTLS_MAJOR ((MBEDTLS_VERSION_NUMBER >> 24 ) & 0xFF )
21+ #else
22+ #define ASIO_MBEDTLS_MAJOR 0
23+ #endif
24+
25+ #if ASIO_MBEDTLS_MAJOR >= 4
26+ // mbedTLS v4: PSA-backed RNG
27+ #include " psa/crypto.h"
28+ #else
29+ // mbedTLS v3: legacy entropy/CTR_DRBG
30+ #include " mbedtls/entropy.h"
31+ #include " mbedtls/ctr_drbg.h"
32+ #endif
33+
1534namespace asio {
1635namespace ssl {
1736namespace mbedtls {
@@ -223,15 +242,29 @@ class engine {
223242
224243 impl ()
225244 {
226- const unsigned char pers[] = " asio ssl" ;
227245 mbedtls_ssl_init (&ssl_);
228246 mbedtls_ssl_config_init (&conf_);
229- mbedtls_ctr_drbg_init (&ctr_drbg_);
230247#ifdef CONFIG_MBEDTLS_DEBUG
231248 mbedtls_esp_enable_debug_log (&conf_, CONFIG_MBEDTLS_DEBUG_LEVEL);
232249#endif
250+ #if ASIO_MBEDTLS_MAJOR >= 4
251+ // mbedTLS v4: Initialize PSA Crypto API
252+ psa_status_t status = psa_crypto_init ();
253+ if (status != PSA_SUCCESS) {
254+ print_error (" psa_crypto_init" , static_cast <int >(status));
255+ // Note: We continue anyway as psa_crypto_init() is idempotent
256+ }
257+ #else
258+ // mbedTLS v3: Initialize legacy entropy and CTR_DRBG
259+ const unsigned char pers[] = " asio ssl" ;
260+ mbedtls_ctr_drbg_init (&ctr_drbg_);
233261 mbedtls_entropy_init (&entropy_);
234- mbedtls_ctr_drbg_seed (&ctr_drbg_, mbedtls_entropy_func, &entropy_, pers, sizeof (pers));
262+ int ret_seed = mbedtls_ctr_drbg_seed (&ctr_drbg_, mbedtls_entropy_func, &entropy_, pers, sizeof (pers));
263+ if (ret_seed != 0 ) {
264+ print_error (" mbedtls_ctr_drbg_seed" , ret_seed);
265+ }
266+ rng_initialized_ = (ret_seed == 0 );
267+ #endif
235268 mbedtls_x509_crt_init (&public_cert_);
236269 mbedtls_pk_init (&pk_key_);
237270 mbedtls_x509_crt_init (&ca_cert_);
@@ -241,8 +274,15 @@ class engine {
241274 {
242275 mbedtls_ssl_free (&ssl_);
243276 mbedtls_ssl_config_free (&conf_);
244- mbedtls_ctr_drbg_free (&ctr_drbg_);
245- mbedtls_entropy_free (&entropy_);
277+ #if ASIO_MBEDTLS_MAJOR < 4
278+ // mbedTLS v3: Free legacy RNG resources
279+ if (rng_initialized_) {
280+ mbedtls_ctr_drbg_free (&ctr_drbg_);
281+ mbedtls_entropy_free (&entropy_);
282+ }
283+ #endif
284+ // Note: mbedTLS v4 does not call mbedtls_psa_crypto_free() here
285+ // to avoid breaking other code that may also be using PSA Crypto
246286 mbedtls_x509_crt_free (&ca_cert_);
247287 mbedtls_pk_free (&pk_key_);
248288 mbedtls_x509_crt_free (&public_cert_);
@@ -262,7 +302,10 @@ class engine {
262302 print_error (" mbedtls_ssl_config_defaults" , ret);
263303 return false ;
264304 }
305+ #if ASIO_MBEDTLS_MAJOR < 4
306+ // mbedTLS v3: TLS RNG must be configured explicitly
265307 mbedtls_ssl_conf_rng (&conf_, mbedtls_ctr_drbg_random, &ctr_drbg_);
308+ #endif
266309 mbedtls_ssl_conf_authmode (&conf_, mbedtls_verify_mode);
267310 if (ctx->cert_chain_ .size () > 0 && ctx->private_key_ .size () > 0 ) {
268311 ret = mbedtls_x509_crt_parse (&public_cert_, ctx->data (container::CERT), ctx->size (container::CERT));
@@ -271,7 +314,11 @@ class engine {
271314 return false ;
272315 }
273316 ret = mbedtls_pk_parse_key (&pk_key_, ctx->data (container::PRIVKEY), ctx->size (container::PRIVKEY),
274- nullptr , 0 , mbedtls_ctr_drbg_random, &ctr_drbg_);
317+ nullptr , 0
318+ #if ASIO_MBEDTLS_MAJOR < 4
319+ , mbedtls_ctr_drbg_random, &ctr_drbg_
320+ #endif
321+ );
275322 if (ret < 0 ) {
276323 print_error (" mbedtls_pk_parse_keyfile" , ret);
277324 return false ;
@@ -311,10 +358,13 @@ class engine {
311358 return true ;
312359 }
313360 mbedtls_ssl_context ssl_{};
314- mbedtls_entropy_context entropy_{};
315- mbedtls_ctr_drbg_context ctr_drbg_{};
316361 mbedtls_ssl_config conf_{};
317- mbedtls_x509_crt public_cert_{};
362+ #if ASIO_MBEDTLS_MAJOR < 4
363+ mbedtls_entropy_context entropy_ {};
364+ mbedtls_ctr_drbg_context ctr_drbg_{};
365+ bool rng_initialized_{false };
366+ #endif
367+ mbedtls_x509_crt public_cert_ {};
318368 mbedtls_pk_context pk_key_{};
319369 mbedtls_x509_crt ca_cert_{};
320370 };
0 commit comments