Skip to content

Add Mbed TLS 4.x verify callbacks and VERIFY_OPTIONAL - #56

Open
padelsbach wants to merge 5 commits into
wolfSSL:masterfrom
padelsbach:mbedtls-verify
Open

padelsbach wants to merge 5 commits into
wolfSSL:masterfrom
padelsbach:mbedtls-verify

Conversation

@padelsbach

@padelsbach padelsbach commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Follow up to #54 and should be merged after it. Also requires wolfSSL/wolfssl#11110.

Adds mbedtls_ssl_conf_verify and mbedtls_ssl_set_verify. The verify callback is called for each cert in the chain, top down (CA to leaf), with the flags of every failed check, before the handshake's result is decided.

CI points to PR 11110 until it merges.

Copilot AI balanced review requested due to automatic review settings November 13, 2026 22:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The default TLS bounds request protocols without checking whether they are compiled in, causing mbedtls_ssl_setup() to fail when TLS 1.3 is disabled.

1 open issue
What changed in this PR

This PR expands wolfPSA’s Mbed TLS 4.x compatibility layer with wolfSSL-backed TLS and networking APIs, PEM decoding, verification callbacks, and VERIFY_OPTIONAL.

Changes:

  • Adds TLS configuration, connections, sessions, and certificate-verification handling.
  • Integrates TLS sources, initialization, feature detection, and shared-library exports.
  • Adds compatibility tests, sample-program interoperability checks, CI updates, and documentation.
File Description
wolfpsa/​mbedtls_compat/​mbedtls/​version.h Exposes existing version macros through a compatibility header.
wolfpsa/​mbedtls_compat/​mbedtls/​timing.h Adds a placeholder header for unsupported timing APIs.
wolfpsa/​mbedtls_compat/​mbedtls/​ssl.h Declares TLS types, constants, and 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 APIs and error codes.
wolfpsa/​mbedtls_compat/​mbedtls/​error.h Adds a placeholder error header.
wolfpsa/​mbedtls_compat/​mbedtls/​debug.h Exposes the TLS debug configuration declaration.
wolfpsa.map Exports TLS, networking, and PEM symbols.
user_settings.h Enables wolfSSL TLS and verification callback settings.
tools/​psa_want_probe.c Detects available compatibility features and TLS versions.
test/​psa_server/​psa_track_memory_test.c Tests PEM/TLS allocation and initialization cleanup.
test/​mbedtls_compat/​ssl_test.c Tests TLS behavior, verification callbacks, and sessions.
test/​mbedtls_compat/​programs.sh Runs sample-program interoperability checks.
test/​mbedtls_compat/​pem_test.c Tests PEM decoding and error handling.
test/​mbedtls_compat/​Makefile Builds compatibility tests and interoperability peers.
test/​mbedtls_compat/​client-tls-mbedtls.c Adds a migration-guide client with nonblocking retries.
test/​Makefile Makes tracker-build compatibility support configurable.
src/​psa_crypto.c Shares PSA initialization and cleanup with wolfSSL TLS.
src/​mbedtls/​x509.c Makes certificate decoding available internally to TLS.
src/​mbedtls/​ssl.c Implements TLS APIs and verification callback handling.
src/​mbedtls/​pem.c Implements unencrypted PEM decoding.
src/​mbedtls/​net.c Implements socket operations and error translation.
src/​mbedtls/​mbedtls_compat_internal.h Declares shared certificate and I/O helpers.
README.md Documents supported APIs, limitations, and tests.
Makefile Builds bundled TLS sources and adds test targets.
.github/​workflows/​test-psa-api.yml Disables compatibility in the stable wolfSSL test variant.
.github/​workflows/​mbedtls-compat.yml Updates dependencies, upstream reference, and build coverage.

Review effort: Balanced

Comment thread src/mbedtls/ssl.c Resolved

@Frauschi Frauschi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A few notes; the get_session and net_accept ones are #54 code I missed there.

Comment thread src/mbedtls/ssl.c
WOLFSSL_ENTER("mbedtls_ssl_get_session");

if (ssl == NULL || ssl->ssl == NULL || session == NULL ||
session->session != NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mbed TLS frees dst and copies over it, and ssl_client2's reconnect loop exports into the same saved_session twice; here the second call fails with BAD_INPUT_DATA. Free the old session instead of rejecting it.

Comment thread src/mbedtls/net.c
return MbedNetWouldBlock() ? MBEDTLS_ERR_SSL_WANT_READ :
MBEDTLS_ERR_NET_ACCEPT_FAILED;
}
if (client_ip == NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No test or program passes a client_ip buffer, so the address copy, *cip_len and BUFFER_TOO_SMALL paths never run; an IPv4 and an IPv6 case in ssl_test.c would cover them.

Comment thread src/mbedtls/ssl.c
} map[] = {
{ WC_NO_ERR_TRACE(ASN_AFTER_DATE_E), MBEDTLS_X509_BADCERT_EXPIRED },
{ WC_NO_ERR_TRACE(ASN_BEFORE_DATE_E), MBEDTLS_X509_BADCERT_FUTURE },
{ WC_NO_ERR_TRACE(ASN_NO_SIGNER_E), MBEDTLS_X509_BADCERT_NOT_TRUSTED },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With OPENSSL_ALL or WOLFSSL_QT, an untrusted self-signed cert fails with ASN_SELF_SIGNED_E, which lands on BADCERT_OTHER, so a callback that clears NOT_TRUSTED no longer accepts it. Map it to NOT_TRUSTED too.

Comment thread src/mbedtls/ssl.c
ret = MbedChainNew(s, store->totalCerts);
}
if (ret == 0 && (s->chain_len != store->totalCerts || depth < 0 ||
depth >= s->chain_len)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With OPENSSL_ALL, wolfSSL master's DoVerifyCallbackIssuer() reports the trust anchor at error_depth == totalCerts, which fails here as FATAL_ERROR; against master plus #11110 that breaks 27 ssl tests. Handle that call as the CA's instead.

Comment thread src/mbedtls/ssl.c
if (!preverify) {
s->verify_flags |= MbedVerifyFlag(store->error);
}
return preverify || s->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

On a client, an empty server Certificate arrives here as NO_PEER_CERT after wolfSSL already sent decode_error, and OPTIONAL lets the handshake continue past that alert. Only accept a missing certificate on the server side.

return ClientVerify(&chainCerts, HOST_NAME, 0, 0);
}

/* The handshake ends at the first failed check, the one flag reported */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This comment still describes the old flow; the handshake now checks the whole chain before it fails.

Comment thread src/mbedtls/ssl.c
if (!conf->ca_loaded[i]) {
continue;
}
ret = MbedCertDecode(issuer, crt->raw.p, (word32)crt->raw.len);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Every handshake with a verify callback reparses each loaded CA with wc_ParseCert until one matches, which adds up with a large bundle. Keeping each CA's subject hash and SKID from MbedConfCAs would make this a compare.

Comment thread src/mbedtls/ssl.c
if (conf != NULL) {
conf->f_vrfy = f_vrfy;
conf->p_vrfy = p_vrfy;
conf->changes++;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

f_vrfy is read when the callback fires, so this bump only makes every context rebuild its WOLFSSL on the next handshake; mbedtls_ssl_set_verify already skips it.

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.

4 participants