Skip to content

Add MbedTLS example - #653

Open
padelsbach wants to merge 1 commit into
wolfSSL:masterfrom
padelsbach:mbedtls-compat
Open

padelsbach wants to merge 1 commit into
wolfSSL:masterfrom
padelsbach:mbedtls-compat

Conversation

@padelsbach

@padelsbach padelsbach commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Add the example from the docs repo to the examples repo

Copilot AI balanced review requested due to automatic review settings November 3, 2026 21:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Non-blocking TLS I/O mishandles retryable results and partial writes, while CI dependency fetching fails for SHA inputs and uses unpinned or potentially mismatched commits.

4 open issues
What changed in this PR

Adds an Mbed TLS 4.x client example using wolfPSA’s compatibility layer over wolfSSL, integrated with the repository’s example build and CI infrastructure.

Changes:

  • Adds a TLS client with certificate verification and a non-blocking socket option.
  • Adds build instructions and checks for TLS 1.2, TLS 1.3, and expected error cases.
  • Registers the example in CI and adds dependency-fetch configuration.
File Description
README.md Introduces and links the compatibility example.
mbedtls-compat/​README.md Documents building, running, testing, and compatibility limitations.
mbedtls-compat/​Makefile Builds the client and test servers and exposes check targets.
mbedtls-compat/​client-tls-mbedtls.c Implements the client using Mbed TLS APIs.
mbedtls-compat/​check.sh Checks protocol versions, socket modes, and error results.
.gitignore Excludes generated example binaries.
.github/​workflows/​examples.yml Passes the wolfSSL ref to dependency-fetch steps.
.github/​examples-manifest.yml Registers the example and its dependency builds.

Review effort: Balanced

Comment thread .github/examples-manifest.yml Resolved
Comment thread .github/examples-manifest.yml Resolved
Comment thread mbedtls-compat/client-tls-mbedtls.c Resolved
Comment thread mbedtls-compat/client-tls-mbedtls.c Resolved
@padelsbach
padelsbach marked this pull request as ready for review November 17, 2026 02:26
@padelsbach
padelsbach requested a review from Frauschi November 17, 2026 18:09

@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.

Builds and passes make check locally; a few comments on the test script and the client.

Comment thread mbedtls-compat/check.sh
while IFS='|' read -r name server opts expect; do
[ -z "$name" ] && continue
"$BIN_DIR/$server" >/dev/null 2>&1 &
srv=$!

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.

If an earlier server still holds port 11111 (e.g. from the README steps), this server fails to bind and the client tests the old one, so the TLS 1.2 cases pass against a TLS 1.3 server. Check kill -0 $srv after the client runs and fail the case if the server is gone.

ret = mbedtls_ssl_read(&ssl, buf, sizeof(buf) - 1);
} while (tls_want(ret) ||
ret == MBEDTLS_ERR_SSL_RECEIVED_NEW_SESSION_TICKET);
if (ret > 0) {

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.

mbedtls_ssl_read() returns 0 when the peer closes without sending data, so this reports success with no response read. Treat ret == 0 as a failure; checking the server's reply in check.sh would also catch it.

}
mbedtls_ssl_set_bio(&ssl, &net, mbedtls_net_send, mbedtls_net_recv, NULL);

while ((ret = mbedtls_ssl_handshake(&ssl)) != 0) {

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 -N these loops retry WANT_READ/WANT_WRITE immediately and spin a core. Since this is meant to be copied, wait on the fd with poll() (POLLIN/POLLOUT) before retrying.

Comment thread mbedtls-compat/README.md

```
make check-server-tls13
./check-server-tls13 &

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.

The client can run before the server is listening, and the server keeps running afterwards and holds port 11111. Add a short sleep before the client and a kill %1 at the end.

Comment thread mbedtls-compat/check.sh
run_client() {
i=0
while :; do
out=$($WRAP "$BIN_DIR/client-tls-mbedtls" $opts 127.0.0.1 2>&1)

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.

Comparing out by exact string breaks with any WRAP that prints (valgrind banner, sanitizer reports): no retry happens and every case fails. Match only the last line of out instead.

}
/* tls_get() retries on MBEDTLS_ERR_SSL_WANT_READ/WANT_WRITE */
if (nonBlock &&
fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFL) | O_NONBLOCK) != 0) {

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.

If F_GETFL fails this sets -1 | O_NONBLOCK, and F_SETFL only guarantees -1 on failure. Store the flags, check them for -1, and compare the F_SETFL result against -1.

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