Skip to content

Add Mbed TLS 4.x support (PSA Crypto)#2502

Merged
yhirose merged 3 commits into
masterfrom
mbedtls-4-macos-minimal
Jul 21, 2026
Merged

Add Mbed TLS 4.x support (PSA Crypto)#2502
yhirose merged 3 commits into
masterfrom
mbedtls-4-macos-minimal

Conversation

@yhirose

@yhirose yhirose commented Jul 19, 2026

Copy link
Copy Markdown
Owner

Summary

Add Mbed TLS 4.x support to the Mbed TLS backend, verified on macOS (Homebrew's default mbedtls is now 4.x). The version is auto-detected via MBEDTLS_VERSION_MAJOR; 2.x/3.x are unaffected.

Backend changes (httplib.h, all version-gated)

  • Include psa/crypto.h and drop the headers removed in 4.x (ctr_drbg, entropy, md5, sha*).
  • Compute MD5/SHA-256/SHA-512 via PSA (psa_hash_compute); initialize PSA Crypto once with std::call_once.
  • Drop the explicit entropy/CTR-DRBG RNG (PSA provides the TLS RNG); skip the RNG-callback overloads of pk_parse_key/pk_check_pair on 4.x.
  • Retry on a TLS 1.3 NewSessionTicket (the 4.x default) in connect/read/write/is_peer_closed through a single mbedtls_is_session_ticket() helper, so online HTTPS works — including large redirected downloads where the ticket arrives mid-write.

Note: V4 implies V3, so 3.x-only paths check V3 && !V4.

Build systems (macOS)

  • CMake / meson: no change needed for linking — Homebrew's CMake config and pkg-config resolve 4.x (libtfpsacrypto) transitively.
  • Makefile: links libtfpsacrypto when present, else libmbedcrypto.

Tests

  • Generate the encrypted client key as both PBES2-AES (3.6+/4.x, OpenSSL, wolfSSL) and PBES1-3DES (Mbed TLS 2.28), selected by version — 4.x dropped DES and 2.28 lacks PBES2.
  • Add the IP-host cert generation to test/meson.build to match gen-certs.sh and test/CMakeLists.txt.

Test plan

  • macOS full test suite green against Homebrew Mbed TLS 4.2 (Makefile) and meson (731/731)
  • 3.6 still green (no regression); 2.x/3.x/4.x all compile under -std=c++11
  • Online HTTPS + large redirected download (write-path NewSessionTicket)
  • CI: existing mbedtls (2.28/3.x) jobs still green

🤖 Generated with Claude Code

yhirose added 2 commits July 18, 2026 23:12
Auto-detect Mbed TLS 4.x via MBEDTLS_VERSION_MAJOR and adapt the backend:
- Include psa/crypto.h and drop the headers removed in 4.x (ctr_drbg,
  entropy, md5, sha*), gated behind the version macro.
- Compute MD5/SHA-256/SHA-512 via PSA (psa_hash_compute) and initialize
  PSA Crypto once with std::call_once.
- Drop the explicit entropy/CTR-DRBG RNG (PSA provides the TLS RNG) and
  skip the RNG-callback overloads of pk_parse_key/pk_check_pair on 4.x.
- Retry on a TLS 1.3 NewSessionTicket (the 4.x default) in connect, read,
  write and is_peer_closed via a single mbedtls_is_session_ticket() helper,
  so online HTTPS works, including large redirected downloads where the
  ticket arrives mid-write.

Note V4 implies V3, so 3.x-only paths now check V3 && !V4.

Build systems (macOS): the CMake config and pkg-config shipped by Homebrew
resolve 4.x transitively, so CMakeLists.txt and meson.build need no change
for linking; the Makefile links libtfpsacrypto when present, else
libmbedcrypto.

Tests: generate the encrypted client key as both PBES2-AES (3.6+/4.x,
OpenSSL, wolfSSL) and PBES1-3DES (Mbed TLS 2.28) and pick by version, since
4.x dropped DES and 2.28 lacks PBES2. Also generate the IP-host certs in
test/meson.build to match gen-certs.sh and CMakeLists.txt.
Homebrew's default mbedtls is now 4.x, so switch the macOS build and CI job
to it (drop the mbedtls@3 pin). That leaves 3.x (Ubuntu 24.04 apt ships 2.28,
macOS now 4.x) uncovered, so add an ubuntu-26.04 job whose apt provides Mbed
TLS 3.6. Net coverage: 2.28 (ubuntu-latest), 3.6 (ubuntu-26.04), 4.2 (macOS).

ubuntu-26.04 is a public-preview runner image; fold it into the main ubuntu
matrix once ubuntu-latest moves to 26.04.
@daveisfera daveisfera mentioned this pull request Jul 20, 2026
4 tasks
Update README.md and the tour's TLS setup pages (en/ja) to note that
Mbed TLS 4.x is now auto-detected and that it renames libmbedcrypto
to libtfpsacrypto.
@yhirose
yhirose merged commit 54b3c1d into master Jul 21, 2026
45 checks passed
@yhirose
yhirose deleted the mbedtls-4-macos-minimal branch July 24, 2026 00:07
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.

1 participant