Skip to content

Fix mbedTLS 4.x compatibility - #579

Merged
bsergean merged 1 commit into
masterfrom
fix-mbedtls4-compat
May 21, 2026
Merged

bsergean merged 1 commit into
masterfrom
fix-mbedtls4-compat

Conversation

@bsergean

Copy link
Copy Markdown
Collaborator

Summary

  • mbedtls/ctr_drbg.h and mbedtls/entropy.h were removed in mbedTLS 4.0 (RNG is now handled by the PSA Crypto API), causing build failures when using mbedTLS 4.x (reported in fatal error: mbedtls/ctr_drbg.h: No such file or directory #577)
  • Guard all affected #includes, member variables, and call-sites with #if MBEDTLS_VERSION_MAJOR < 4
  • mbedtls_ssl_conf_rng was also removed in 4.0 — same guard applied
  • mbedtls_pk_parse_keyfile dropped its RNG parameters in 4.0 (same as the pre-3 signature); replace the IXWEBSOCKET_USE_MBED_TLS_MIN_VERSION_3 macro check with #if MBEDTLS_VERSION_MAJOR == 3
  • Replace runtime if (MBEDTLS_VERSION_MAJOR >= ...) checks for psa_crypto_init / mbedtls_psa_crypto_free with compile-time #if guards

Test plan

  • Build with mbedTLS 2.x — existing behaviour unchanged
  • Build with mbedTLS 3.x — existing behaviour unchanged
  • Build with mbedTLS 4.1.0 on MinGW / Windows — no longer fails on missing ctr_drbg.h
  • Build with mbedTLS 4.1.0 on Linux/macOS

Fixes #577

🤖 Generated with Claude Code

mbedTLS 4.0 removed ctr_drbg/entropy headers and APIs in favour of the
PSA Crypto RNG, and dropped the rng parameters from mbedtls_pk_parse_keyfile
and mbedtls_ssl_conf_rng. Guard all affected includes and call-sites with
MBEDTLS_VERSION_MAJOR < 4 preprocessor checks, and replace runtime version
if-checks with compile-time #if for psa_crypto_init / mbedtls_psa_crypto_free.

Fixes #577

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@bsergean
bsergean merged commit 998cf95 into master May 21, 2026
14 checks passed
bsergean pushed a commit that referenced this pull request Oct 14, 2026
…612) (#613)

* Add regression test for process-global PSA state surviving a socket close

Closing one SocketMbedTLS must not invalidate PSA crypto state (RNG, key
slots) used by other live sockets in the same process.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Release PSA crypto state in uninitNetSystem(), not per socket (fix #612)

SocketMbedTLS::close() called mbedtls_psa_crypto_free() for every socket.
That function is process-global: it wipes all PSA key slots and the
shared entropy/RNG state, so closing one TLS socket broke every other
live TLS socket in the process. With several concurrent TLS WebSocket
clients reconnecting on their own threads this showed up as
PSA_ERROR_BAD_STATE failures and heap-corruption aborts.

- close() no longer calls mbedtls_psa_crypto_free().
- uninitNetSystem() now calls mbedtls_psa_crypto_free() on all
  platforms when built with mbedTLS >= 3.6, mirroring the existing
  WSAStartup/WSACleanup pairing. It must be called only after all
  sockets are closed.
- initMBedTLS() is guarded by a per-socket flag so the ctor + init()
  double call no longer re-initialises live mbedTLS contexts (which
  re-inits mutexes under MBEDTLS_THREADING_C). close() clears the flag
  so a socket can be re-initialised.
- init() checks psa_crypto_init() and fails the connect with an error
  message instead of running TLS on uninitialised PSA state.

Affected releases: v11.4.6, v12.0.0, v12.0.1 and master (introduced in
#527, widened to mbedTLS 4.x in #579), only when built against
mbedTLS >= 3.6. v11.4.5 and earlier are not affected.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* Guard the PSA include for pre-3.6 mbedTLS and keep the PSA test binary non-empty

- IXNetSystem.cpp: include <psa/crypto.h> only for mbedTLS >= 3.6, so
  builds against mbedTLS 2.x (no psa/ headers) still compile.
- The PSA lifetime test compiles to a single passing placeholder case
  below mbedTLS 3.6 instead of an empty binary, which Catch2 reports as
  "no tests ran" (exit 2) and would fail ctest for a supported config.
- Document in usage.md that uninitNetSystem() now also releases
  process-global TLS (PSA) state and must follow the last socket close.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fatal error: mbedtls/ctr_drbg.h: No such file or directory

1 participant