Skip to content

mbedtls: complete version 4 compatibility#3594

Open
stefan-sedlak wants to merge 4 commits into
warmcat:mainfrom
stefan-sedlak:main
Open

mbedtls: complete version 4 compatibility#3594
stefan-sedlak wants to merge 4 commits into
warmcat:mainfrom
stefan-sedlak:main

Conversation

@stefan-sedlak
Copy link
Copy Markdown

This completes mbedTLS 4.x compatibility while keeping the change surface
localized to the mbedTLS backend and include boundaries, so existing 2.x and
3.x support is preserved.

mbedTLS 4.x moved a number of low-level details behind TF-PSA private headers
and also needs PSA-backed initialization in standalone crypto paths that older
versions did not. This patch keeps those differences inside the mbedTLS
integration layer instead of spreading version-specific handling through
generic code.

The main pieces are:

  • add the missing 4.x header / feature probing and private include routing
  • add 4.x compatibility glue for key handling, x509 handling and crypto helpers
  • initialize PSA-backed crypto on standalone RSA / x509 paths where 4.x needs it
  • adapt 4.x HMAC setup and unsupported AES-XTS handling
  • add local 4.x RSA CRT completion support for imported private keys
  • keep the widened RSA completion trigger limited to 4.x so 2.x / 3.x retain
    their previous behaviour
  • make the COSE RSA sign path use the returned signature length and fail hard
    instead of serializing an empty signature on error

Validation was kept conservative:

  • mbedTLS 2.28.10: api-test-gencrypto, api-test-x509, crypto-cose-sign-7,
    crypto-cose-sign-8 all pass
  • mbedTLS 3.6.6: api-test-gencrypto, api-test-x509, crypto-cose-sign-7,
    crypto-cose-sign-8 all pass
  • mbedTLS 4.1.0: api-test-gencrypto, api-test-x509, crypto-cose-sign-7,
    crypto-cose-sign-8 all pass
  • full 4.1.0 ctest run completed with only the known external warmcat trust
    failures remaining:
    • http-client-warmcat
    • http-client-warmcat-h1
    • http-client-h3

Full 2.x / 3.x broad reruns were also attempted, but those runs were dominated
by unrelated external network / CA-environment instability, so the focused
cross-version regression slice above is the reliable validation signal for this
patch.

Review Note:

It was developed by GPT-5.4. I tried to review it as much as possible. If it is garbage I will keep it on my private repo.

lws-team and others added 3 commits May 29, 2026 16:36
Keep the compatibility handling localized to the mbedtls backend and include boundaries while preserving existing 2.x and 3.x behavior.

Add the missing 4.x private-header and PSA-backed glue for hashing, key handling, x509 paths and COSE RSA signing, and keep the broadened RSA key completion logic limited to 4.x so older versions retain their previous behavior.

Validated with api-test-gencrypto, api-test-x509, crypto-cose-sign-7 and crypto-cose-sign-8 on mbedTLS 2.28.10, 3.6.6 and 4.1.0.
Copilot AI review requested due to automatic review settings May 29, 2026 19:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds support for building libwebsockets against mbedTLS 4.x while keeping compatibility with 2.x/3.x. mbedTLS 4 removes the legacy ctr_drbg/entropy modules, relocates many headers under mbedtls/private/, and exposes PSA Crypto as the primary API; this PR introduces detection, header-path shims, RNG/key-parsing wrappers, and legacy-type stubs to bridge those differences.

Changes:

  • New CMake checks (LWS_HAVE_MBEDTLS_V4, LWS_HAVE_MBEDTLS_PRIVATE_*_H) plus conditional includes throughout the tree to pick the right header location for each mbedTLS major.
  • New lws_mbedtls_* wrapper helpers (PK parse, RNG conf, CSR/CRT write, RSA/EC keygen, global crypto init) plus inline shims for mbedtls_rsa_complete, ECDH context, and PSS sign/verify when building against mbedTLS 4.
  • Reworked lws_x509_public_to_jwk / lws_x509_jwk_privkey_pem to derive JWK elements via PSA export / ASN.1 parsing when the legacy RSA/ECP internals are unavailable.

Reviewed changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
cmake/lws_config.h.in Adds new LWS_HAVE_MBEDTLS_* config defines.
lib/tls/mbedtls/CMakeLists.txt Detects mbedTLS 4 and the TF-PSA-Crypto private headers; fails fast if missing.
include/libwebsockets.h Conditionally includes private mbedTLS headers and provides opaque stub types for removed entropy/drbg.
include/libwebsockets/lws-gen{aes,chacha,dtls,ec,rsa}.h Public headers updated to pick private headers / forward-declare types for mbedTLS 4.
lib/core/private-lib-core.h Provides inline stubs for removed mbedtls_entropy_* / mbedtls_ctr_drbg_* functions on mbedTLS 4.
lib/core/context.c Uses the v4 form of mbedtls_version_get_string.
lib/cose/cose_sign_alg.c Uses signer return length instead of precomputed bytes and marks alg failed on error.
lib/tls/lws-genchacha.c Picks private chacha/cipher headers under mbedTLS 4.
lib/tls/private-lib-tls.h Conditional include of private AES/GCM/ECP/ECDSA/RSA headers.
lib/tls/mbedtls/private-lib-tls-mbedtls.h Core compatibility layer: PSA init, RNG wrappers, PK/CSR/CRT helpers, ECDH/PSS/mbedtls_rsa_complete reimplementations.
lib/tls/mbedtls/mbedtls-tls.c Calls lws_mbedtls_global_crypto_init during SSL library init.
lib/tls/mbedtls/mbedtls-server.c Switches CSR creation to the new keygen / CSR-write wrappers.
lib/tls/mbedtls/mbedtls-extensions.c Local re-declaration of mbedtls_oid_descriptor_t because the type is no longer public.
lib/tls/mbedtls/mbedtls-x509.c New v4 paths for cert info, JWK from public/private keys via PSA + ASN.1.
lib/tls/mbedtls/lws-gen{aes,dtls,ec,hash,rsa}.c Re-route to new wrappers, adjust struct access for v4, compute key length from N.
lib/tls/mbedtls/wrapper/platform/ssl_pm.c Uses lws_mbedtls_ssl_conf_rng and lws_mbedtls_pk_parse_key.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/tls/mbedtls/private-lib-tls-mbedtls.h Outdated
Comment thread lib/tls/mbedtls/mbedtls-server.c
Comment thread include/libwebsockets/lws-genec.h Outdated
Comment thread lib/tls/mbedtls/mbedtls-x509.c
Comment thread lib/tls/mbedtls/lws-genrsa.c
Comment thread lib/tls/mbedtls/private-lib-tls-mbedtls.h Outdated
Comment thread lib/cose/cose_sign_alg.c
Comment thread include/libwebsockets.h Outdated
Comment thread lib/tls/mbedtls/lws-genaes.c
Tighten the 4.x RSA completion gate, fix the PSA ECDSA algorithm selection, harden RSA public-key JWK extraction for SPKI-wrapped DER, and reduce public mbedtls 4 type leakage by keeping legacy RNG shims out of umbrella public headers.

Also cache PSA initialization in the 4.x compatibility helpers and document the unsupported AES mode return.
@lws-team
Copy link
Copy Markdown
Member

I think it's good to support mbedtls4 but probably these patches aren't quite the way.

Let me take a look tomorrow and see if we can match what your patches do in a slightly better way.

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.

3 participants