Skip to content

Add Mbed TLS non-blocking sockets and read timeouts - #79

Merged
Frauschi merged 2 commits into
wolfSSL:masterfrom
padelsbach:nonblock
Nov 24, 2026
Merged

Frauschi merged 2 commits into
wolfSSL:masterfrom
padelsbach:nonblock

Conversation

@padelsbach

@padelsbach padelsbach commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds the non-blocking socket and read timeout calls:

  • mbedtls_net_set_block, mbedtls_net_set_nonblock, mbedtls_net_poll and mbedtls_net_recv_timeout, with MBEDTLS_NET_POLL_READ and _WRITE, and the error codes MBEDTLS_ERR_NET_POLL_FAILED and MBEDTLS_ERR_NET_BAD_INPUT_DATA
  • mbedtls_ssl_conf_read_timeout, applied through the receive callback with a timeout (such as mbedtls_net_recv_timeout) and, with non-blocking I/O, through the mbedtls_ssl_set_timer_cb timers

A read or handshake that returns MBEDTLS_ERR_SSL_TIMEOUT can now be called again.

Copilot AI balanced review requested due to automatic review settings November 24, 2026 02:44

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 EINTR retry loop resets the original timeout, allowing signals to extend finite polls beyond their requested duration or prevent them from timing out.

1 open issue
What changed in this PR

Extends wolfPSA’s Mbed TLS compatibility layer with non-blocking socket controls, polling, and configurable read timeouts.

Changes:

  • Adds socket blocking-mode controls, polling, timed receives, and associated error codes.
  • Integrates TLS read timeouts with receive callbacks and timers, allowing reads and handshakes to resume after timeout errors.
  • Adds regression tests and updates compatibility documentation.
File Description
wolfpsa/​mbedtls_compat/​mbedtls/​ssl.h Declares read-timeout configuration and storage.
wolfpsa/​mbedtls_compat/​mbedtls/​net_sockets.h Declares new socket APIs, flags, and error codes.
test/​mbedtls_compat/​ssl_test.c Tests socket modes, polling, timeout recovery, and timers.
src/​mbedtls/​ssl.c Implements read-timeout configuration, timer integration, and retryable timeout handling.
src/​mbedtls/​net.c Implements blocking-mode controls, polling, and timed receives.
src/​mbedtls/​error.c Adds messages for the new network errors.
docs/​mbedtls-compatibility.md Documents supported APIs and blocking-mode error differences.

Review effort: Balanced

Comment thread src/mbedtls/net.c Outdated
Comment on lines +393 to +394
ret = MbedNetSelect(ctx->fd, rw, timeout, timeout == UINT32_MAX);
} while (ret < 0 && MbedNetError() == SOCKET_EINTR);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed as described (roughly)

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

4 comments inline, nothing blocking.

Comment thread src/mbedtls/ssl.c Outdated
{ 0, MBEDTLS_ERR_NET_CONN_RESET } },
{ WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_TIMEOUT),
/* As in Mbed TLS, the next call goes on after a timeout */
{ WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_WANT_READ),

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 WOLFSSL_DTLS, a datagram handshake timeout now reaches wolfSSL as WANT_READ, so it never retransmits; wolfSSL only resends on WOLFSSL_CBIO_ERR_TIMEOUT. Keep WOLFSSL_CBIO_ERR_TIMEOUT for DTLS, and limit this row to receive, since wolfSSL treats it as fatal on send.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good find, added mapping for ERR_TIMEOUT into WANT_READ

mbedtls_net_recv, NULL);
/* Before the server answers */
ok = mbedtls_ssl_handshake(&cli.ssl) == MBEDTLS_ERR_SSL_TIMEOUT &&
Handshake(&s) == 0 && s == 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.

HandshakeOf() gives the server 20 non-blocking calls with no wait, so over real TCP it can stop at WANT_READ before the client's Finished arrives; it fails most runs on a loaded Mac. Poll the socket between steps, which also stops the BioErrorHandshake TIMEOUT case passing by exhaustion.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added 1000ms poll between steps

Comment thread src/mbedtls/net.c Outdated
return MBEDTLS_ERR_NET_SOCKET_FAILED;
}
flags = nonBlock ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK);
if (fcntl(ctx->fd, F_SETFL, flags) != 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.

POSIX only promises a value other than -1 from F_SETFL on success; check < 0 like the F_GETFL call above.

Suggested change
if (fcntl(ctx->fd, F_SETFL, flags) != 0) {
if (fcntl(ctx->fd, F_SETFL, flags) < 0) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, done

Comment thread docs/mbedtls-compatibility.md Outdated
@@ -214,8 +211,6 @@ TLS:
uses the extension in TLS 1.2 only.
- `mbedtls_ssl_conf_dbg` and `mbedtls_ssl_conf_cert_profile` are accepted
and ignored.

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 timers still don't drive DTLS retransmission, so keep a shorter note saying mbedtls_ssl_set_timer_cb timers are used only for the read timeout.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

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

All four fixed; the DTLS resend branch can't run until DTLS handshakes work (a DATAGRAM config fails with VERSION_ERROR today), so it'll need a test then. Needs a rebase: it conflicts with master after #76-#78.

@padelsbach
padelsbach requested a review from Frauschi November 24, 2026 21:34
@Frauschi
Frauschi merged commit d96dfb2 into wolfSSL:master Nov 24, 2026
109 checks passed
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