Repository navigation
Add Mbed TLS 4.x TLS, net and PEM API - #54
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Session resumption on supported wolfSSL 5.9.4 can bypass required client-certificate authentication, leaving a critical security issue unresolved.
3 open issues
What changed in this PR
Extends wolfPSA’s Mbed TLS 4.x compatibility layer beyond the X.509 work in #53, adding wolfSSL-backed TLS, socket networking, and PEM decoding.
Changes:
- Adds compatibility APIs, headers, and shared-library exports.
- Integrates TLS sources and feature detection into the optional
MBEDTLS_COMPAT=1build. - Adds parsing, TLS, memory-tracking, and interoperability tests, with CI and usage documentation.
| File | Description |
|---|---|
wolfpsa/mbedtls_compat/mbedtls/x509.h |
Defines X.509 types, flags, and errors. |
wolfpsa/mbedtls_compat/mbedtls/x509_crt.h |
Declares certificate parsing APIs and profiles. |
wolfpsa/mbedtls_compat/mbedtls/version.h |
Exposes compatibility version macros. |
wolfpsa/mbedtls_compat/mbedtls/timing.h |
Adds a placeholder timing header. |
wolfpsa/mbedtls_compat/mbedtls/ssl.h |
Declares TLS configuration, connection, and session APIs. |
wolfpsa/mbedtls_compat/mbedtls/ssl_ciphersuites.h |
Defines cipher suite identifiers. |
wolfpsa/mbedtls_compat/mbedtls/psa_util.h |
Adds a PSA utility compatibility header. |
wolfpsa/mbedtls_compat/mbedtls/pem.h |
Declares PEM decoding APIs. |
wolfpsa/mbedtls_compat/mbedtls/net_sockets.h |
Declares socket networking APIs. |
wolfpsa/mbedtls_compat/mbedtls/error.h |
Adds a placeholder error header. |
wolfpsa/mbedtls_compat/mbedtls/debug.h |
Exposes the TLS debug configuration declaration. |
wolfpsa/mbedtls_compat/mbedtls/build_info.h |
Defines API version and feature information. |
wolfpsa/mbedtls_compat/mbedtls/asn1.h |
Defines the ASN.1 buffer type. |
wolfpsa.map |
Exports compatibility API symbols. |
user_settings.h |
Enables wolfSSL TLS settings for compatibility builds. |
tools/psa_want_probe.c |
Detects supported compatibility features. |
tools/gen-crypto-config.py |
Updates comments to mention Mbed TLS options. |
test/psa_server/psa_track_memory_test.c |
Tests compatibility allocation and initialization behavior. |
test/mbedtls_compat/x509_crt_test.c |
Tests certificate parsing and failure cases. |
test/mbedtls_compat/ssl_test.c |
Tests TLS verification, I/O, configuration, and sessions. |
test/mbedtls_compat/programs.sh |
Runs client/server interoperability scenarios. |
test/mbedtls_compat/pk_test.c |
Tests private-key parsing compatibility. |
test/mbedtls_compat/pem_test.c |
Tests PEM decoding and invalid input. |
test/mbedtls_compat/Makefile |
Builds compatibility tests and external sample programs. |
test/mbedtls_compat/client-tls-mbedtls.c |
Adds a migration-guide client with nonblocking mode. |
test/Makefile |
Enables compatibility APIs in memory-tracking tests. |
src/psa_tf_psa.c |
Shares error mapping and file-loading helpers. |
src/psa_tf_psa_internal.h |
Declares shared internal helpers. |
src/mbedtls/x509.c |
Implements certificate parsing through wolfCrypt. |
src/mbedtls/ssl.c |
Implements TLS APIs through wolfSSL. |
src/mbedtls/pem.c |
Implements unencrypted PEM decoding. |
src/mbedtls/net.c |
Implements socket networking wrappers. |
src/mbedtls/mbedtls_compat_internal.h |
Declares shared I/O conversion helpers. |
README.md |
Documents setup, supported APIs, limitations, and tests. |
Makefile |
Integrates compatibility sources and test targets. |
.github/workflows/mbedtls-compat.yml |
Adds compatibility and build-variant CI coverage. |
Review effort: Balanced
Frauschi
left a comment
There was a problem hiding this comment.
A failed mbedtls_ssl_set_hostname currently lets the handshake skip the host-name check; the rest are smaller.
| } | ||
| wolfSSL_SESSION_free(session); | ||
| if (ret == 0) { | ||
| ssl->conf_changes = ssl->conf->changes; |
There was a problem hiding this comment.
If MbedSslNew() fails after wolfSSL_new(), the half-built WOLFSSL stays with conf_changes still current, so after setup mbedtls_ssl_set_hostname(&ssl, "router") fails but the next handshake succeeds with no host-name check. Free ssl->ssl and set it to NULL on any failure here.
| #include "mbedtls_compat_internal.h" | ||
| #include "psa_tf_psa_internal.h" | ||
|
|
||
| #if LIBWOLFSSL_VERSION_HEX < 0x05009004 |
There was a problem hiding this comment.
wolfSSL master and v5.9.4-stable both report 0x05009004, so this lets 5.9.4 through, and the test-psa-api (v5.9.4-stable) job already builds this layer for psa_track_memory_test. 5.9.4 lacks wolfSSL 7460ecc (client auth on resumed sessions), so this needs a check only master passes.
There was a problem hiding this comment.
Removed the check. Newest wolfPSA requires newest wolfSSL for now. Updated test-psa-api to run with MBEDTLS_COMPAT=0
| conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2; | ||
| conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3; | ||
| conf->ciphersuites = NULL; | ||
| conf->ctx = wolfSSL_CTX_new(methods[i].method()); |
There was a problem hiding this comment.
wolfSSL_CTX_new() calls wolfSSL_Init() on first use, which takes a second wolfCrypt_Init() reference that nothing releases, so mbedtls_psa_crypto_free() no longer cleans up wolfCrypt. Pair it with wolfSSL_Cleanup() in wolfPSA_CryptoCleanup() when the TLS layer is built.
There was a problem hiding this comment.
Fixed for both MBEDTLS_SSL_TLS_C defined and not
| if (ssl == NULL || ssl->ssl == NULL) { | ||
| return NULL; | ||
| } | ||
| return wolfSSL_get_cipher(ssl->ssl); |
There was a problem hiding this comment.
This returns the IANA name, so mbedtls_ssl_get_ciphersuite_id(mbedtls_ssl_get_ciphersuite(&ssl)) is 0, where Mbed TLS returns its own name. Return mbedtls_ssl_get_ciphersuite_name(wolfSSL_get_current_cipher_suite(ssl->ssl)) instead.
There was a problem hiding this comment.
Fixed as suggested
| MBEDTLS_ERR_SSL_BAD_INPUT_DATA); | ||
| XFREE(chain, NULL, DYNAMIC_TYPE_TMP_BUFFER); | ||
| if (ret == 0) { | ||
| ret = MbedRet(wolfSSL_CTX_use_PrivateKey_buffer(conf->ctx, |
There was a problem hiding this comment.
If this key load fails, the CTX keeps the new chain with the old key, where Mbed TLS leaves the config unchanged; validate the key before replacing the chain. Server setup without an own cert also reports ALLOC_FAILED from the NULL wolfSSL_new(), which hides the real cause.
| if (hostname == NULL) { | ||
| return 0; | ||
| } | ||
| ret = MbedRet(wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, hostname, |
There was a problem hiding this comment.
RFC 6066 does not allow an IP literal in server_name, and some servers reject such a ClientHello, even though Mbed TLS sends it too. Skip wolfSSL_UseSNI() when MbedIpText() matches and keep the IP SAN check.
| static void MbedConfCAs(mbedtls_ssl_config* conf) | ||
| { | ||
| const mbedtls_x509_crt* crt; | ||
| int ret = (conf->ca_crl == NULL) ? 0 : MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE; |
There was a problem hiding this comment.
conf->err is cleared only by config_defaults, so after conf_ca_chain(conf, ca, crl) a corrective conf_ca_chain(conf, ca, NULL) still fails every later setup and handshake. Clear the CRL error when the CA chain is set again.
| int ok = Connect(MBEDTLS_SSL_VERIFY_NONE) == 0; | ||
|
|
||
| for (i = 0; ok && i < sizeof(lens) / sizeof(lens[0]); i++) { | ||
| ok = mbedtls_ssl_write(&cli.ssl, buf, lens[i]) == RECORD_SZ && |
There was a problem hiding this comment.
buf has RECORD_SZ bytes but write and read get lengths up to UINT_MAX + 6, so this stays in bounds only because each call moves one record. Name that assumption in a comment so a later change does not read an ASan report here as a library bug.
| if (ssl == NULL) { | ||
| return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; | ||
| } | ||
| ret = wolfSSL_shutdown(ssl->ssl); |
There was a problem hiding this comment.
With ssl->ssl == NULL (setup never run or failed) this returns MBEDTLS_ERR_SSL_INTERNAL_ERROR from wolfSSL_shutdown(NULL); return MBEDTLS_ERR_SSL_BAD_INPUT_DATA as the other entry points do.
There was a problem hiding this comment.
Updated to return BAD_INPUT_DATA
| return 0; | ||
| } | ||
| /* wolfSSL cannot remove a name check, so a new name needs a new WOLFSSL */ | ||
| return MbedSslNew(ssl); |
There was a problem hiding this comment.
Setup followed by set_hostname, the usual order, runs wolfSSL_new() and its RNG seeding twice per connection. Validate the name here and defer the rebuild to MbedSslStart(), as conf changes already are.
There was a problem hiding this comment.
Fixed with new flag hostname_changed
ae90169 to
b6f5692
Compare
| { | ||
| int ret = 0; | ||
|
|
||
| if (ssl == NULL || ssl->conf == NULL || ssl->ssl == NULL) { |
There was a problem hiding this comment.
After a handshake fails on a conf error, MbedSslNew() leaves ssl->ssl NULL, so fixing the conf and calling the handshake again returns BAD_INPUT_DATA until mbedtls_ssl_session_reset(). Either rebuild in MbedSslStart() when ssl->ssl is NULL or say in the README that a reset is needed.


Follow up to #53 and builds upon it. Adds networking API and PEM decode