Repository navigation
Add Mbed TLS non-blocking sockets and read timeouts - #79
Conversation
There was a problem hiding this comment.
🟡 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
| ret = MbedNetSelect(ctx->fd, rw, timeout, timeout == UINT32_MAX); | ||
| } while (ret < 0 && MbedNetError() == SOCKET_EINTR); |
There was a problem hiding this comment.
Fixed as described (roughly)
Frauschi
left a comment
There was a problem hiding this comment.
4 comments inline, nothing blocking.
| { 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), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 && |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Added 1000ms poll between steps
| return MBEDTLS_ERR_NET_SOCKET_FAILED; | ||
| } | ||
| flags = nonBlock ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK); | ||
| if (fcntl(ctx->fd, F_SETFL, flags) != 0) { |
There was a problem hiding this comment.
POSIX only promises a value other than -1 from F_SETFL on success; check < 0 like the F_GETFL call above.
| if (fcntl(ctx->fd, F_SETFL, flags) != 0) { | |
| if (fcntl(ctx->fd, F_SETFL, flags) < 0) { |
There was a problem hiding this comment.
Nice catch, done
| @@ -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. | |||
There was a problem hiding this comment.
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.

Adds the non-blocking socket and read timeout calls:
mbedtls_net_set_block,mbedtls_net_set_nonblock,mbedtls_net_pollandmbedtls_net_recv_timeout, withMBEDTLS_NET_POLL_READand_WRITE, and the error codesMBEDTLS_ERR_NET_POLL_FAILEDandMBEDTLS_ERR_NET_BAD_INPUT_DATAmbedtls_ssl_conf_read_timeout, applied through the receive callback with a timeout (such asmbedtls_net_recv_timeout) and, with non-blocking I/O, through thembedtls_ssl_set_timer_cbtimersA read or handshake that returns
MBEDTLS_ERR_SSL_TIMEOUTcan now be called again.