From be895933f56708d0fd075123c4d932abb52449d4 Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Mon, 23 Nov 2026 18:54:11 -0800 Subject: [PATCH 1/2] Add Mbed TLS non-blocking sockets and read timeouts --- docs/mbedtls-compatibility.md | 15 +- src/mbedtls/error.c | 2 + src/mbedtls/net.c | 197 ++++++++- src/mbedtls/ssl.c | 70 +++- test/mbedtls_compat/ssl_test.c | 402 ++++++++++++++++++- wolfpsa/mbedtls_compat/mbedtls/net_sockets.h | 11 + wolfpsa/mbedtls_compat/mbedtls/ssl.h | 2 + 7 files changed, 660 insertions(+), 39 deletions(-) diff --git a/docs/mbedtls-compatibility.md b/docs/mbedtls-compatibility.md index 08f43812..e2fba695 100644 --- a/docs/mbedtls-compatibility.md +++ b/docs/mbedtls-compatibility.md @@ -56,8 +56,8 @@ TLS: - `mbedtls_ssl_config_*`: init, free, defaults - `mbedtls_ssl_conf_*`: authmode, ca_chain, own_cert, min_tls_version, - max_tls_version, ciphersuites, cert_profile, dbg, verify, endpoint, - transport, get_endpoint, set_user_data_p, set_user_data_n, + max_tls_version, ciphersuites, cert_profile, dbg, verify, read_timeout, + endpoint, transport, get_endpoint, set_user_data_p, set_user_data_n, get_user_data_p, get_user_data_n - `mbedtls_ssl_*`: init, free, setup, context_get_config, session_reset, set_bio, set_timer_cb, set_verify, set_hostname, set_session, get_session, @@ -100,7 +100,7 @@ Other: - `mbedtls_strerror` - `mbedtls_debug_set_threshold` - `mbedtls_net_*`: init, connect, bind, accept (TCP and UDP), close, free, - recv, send, usleep + recv, recv_timeout, send, set_block, set_nonblock, poll, usleep - `mbedtls_pem_*`: init, read_buffer, get_buffer, free - `mbedtls_timing_*`: get_timer, set_delay, get_delay, get_final_delay - `mbedtls_ms_time` and `mbedtls_time` @@ -151,9 +151,6 @@ TLS: - Renegotiation (`mbedtls_ssl_conf_renegotiation`, `mbedtls_ssl_renegotiate`) - A config without `mbedtls_ssl_config_defaults` -- Read timeouts (`mbedtls_ssl_conf_read_timeout`, - `mbedtls_net_recv_timeout`) and non-blocking sockets - (`mbedtls_net_set_nonblock`, `mbedtls_net_set_block`, `mbedtls_net_poll`) - Groups and signature algorithms (`mbedtls_ssl_conf_groups`, `mbedtls_ssl_conf_sig_algs`) - The server name callback (`mbedtls_ssl_conf_sni`) and the @@ -257,8 +254,6 @@ TLS: Mbed TLS leaves the connection as it was. - `mbedtls_ssl_ciphersuite_t` has the ID, name and TLS versions only, not the cipher, MAC, key exchange or flags. -- The timers of `mbedtls_ssl_set_timer_cb` are not used. Mbed TLS uses them - for DTLS and for `mbedtls_ssl_conf_read_timeout`. Certificate verification: @@ -357,6 +352,10 @@ Other: `mbedtls_md_hmac`, `mbedtls_md_file`, `mbedtls_md_list` and `mbedtls_md_info_from_string`. For HMAC, use `psa_mac_*`. - No global mutexes such as `mbedtls_threading_gmtime_mutex`. +- `mbedtls_net_set_block` and `mbedtls_net_set_nonblock` return + `MBEDTLS_ERR_NET_INVALID_CONTEXT` without a socket and + `MBEDTLS_ERR_NET_SOCKET_FAILED` when the system call fails. Mbed TLS + returns the system call's result, -1 on failure. - After a UDP `mbedtls_net_accept`, the new listening socket has the old one's address and, except on Windows, its blocking mode. Mbed TLS binds it to the accepted socket's address, which for a socket bound to every diff --git a/src/mbedtls/error.c b/src/mbedtls/error.c index af49815b..2fd21fb4 100644 --- a/src/mbedtls/error.c +++ b/src/mbedtls/error.c @@ -69,7 +69,9 @@ static const struct { MBED_ERR(NET, INVALID_CONTEXT, "The context is invalid, eg because it was free()ed"), MBED_ERR(NET, BIND_FAILED, "Binding of the socket failed"), + MBED_ERR(NET, POLL_FAILED, "Polling the net context failed"), MBED_ERR(NET, LISTEN_FAILED, "Could not listen on the socket"), + MBED_ERR(NET, BAD_INPUT_DATA, "Input invalid"), MBED_ERR(NET, ACCEPT_FAILED, "Could not accept the incoming connection"), MBED_ERR(NET, RECV_FAILED, "Reading information from the socket failed"), MBED_ERR(NET, SEND_FAILED, diff --git a/src/mbedtls/net.c b/src/mbedtls/net.c index 3d89c7e5..e8d0b8f8 100644 --- a/src/mbedtls/net.c +++ b/src/mbedtls/net.c @@ -49,6 +49,7 @@ #if defined(MBEDTLS_NET_C) #include +#include #include "mbedtls_compat_internal.h" #ifndef USE_WINDOWS_API #include @@ -57,6 +58,13 @@ #include #endif /* !USE_WINDOWS_API */ +/* A receive that does not block, where the system has one */ +#ifdef MSG_DONTWAIT + #define MBED_NET_DONTWAIT MSG_DONTWAIT +#else + #define MBED_NET_DONTWAIT 0 +#endif /* MSG_DONTWAIT */ + void mbedtls_net_init(mbedtls_net_context* ctx) { WOLFSSL_ENTER("mbedtls_net_init"); @@ -183,14 +191,20 @@ int mbedtls_net_bind(mbedtls_net_context* ctx, const char* bind_ip, return MbedNetOpen(ctx, bind_ip, port, proto, 1); } -/* The last socket call failed only because it would block */ -static int MbedNetWouldBlock(void) +/* The error of the last socket call */ +static int MbedNetError(void) { #ifdef USE_WINDOWS_API - int err = WSAGetLastError(); + return WSAGetLastError(); #else - int err = errno; + return errno; #endif /* USE_WINDOWS_API */ +} + +/* The last socket call failed only because it would block */ +static int MbedNetWouldBlock(void) +{ + int err = MbedNetError(); return err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN; } @@ -321,6 +335,135 @@ int mbedtls_net_accept(mbedtls_net_context* bind_ctx, return 0; } +/* 0 when ctx has a socket that select() takes */ +static int MbedNetSelectable(const mbedtls_net_context* ctx) +{ + if (ctx == NULL || ctx->fd < 0) { + return MBEDTLS_ERR_NET_INVALID_CONTEXT; + } +#ifndef USE_WINDOWS_API + /* FD_SET() writes past an fd_set for a larger descriptor */ + if (ctx->fd >= FD_SETSIZE) { + return MBEDTLS_ERR_NET_POLL_FAILED; + } +#endif /* !USE_WINDOWS_API */ + return 0; +} + +/* ms for a deadline. Without a clock, 0: each wait gets the whole timeout. */ +static mbedtls_ms_time_t MbedNetNow(void) +{ +#ifdef MBEDTLS_HAVE_TIME + return mbedtls_ms_time(); +#else + return 0; +#endif /* MBEDTLS_HAVE_TIME */ +} + +/* Events of rw ready by deadline, no limit if forever; <0 on error */ +static int MbedNetSelect(int fd, uint32_t rw, mbedtls_ms_time_t deadline, + int forever) +{ + fd_set readFds; + fd_set writeFds; + struct timeval tv; + mbedtls_ms_time_t left = deadline - MbedNetNow(); + uint32_t ms = (left > 0) ? (uint32_t)left : 0; + int ret; + + FD_ZERO(&readFds); + FD_ZERO(&writeFds); + if ((rw & MBEDTLS_NET_POLL_READ) != 0) { + FD_SET((SOCKET_T)fd, &readFds); + } + if ((rw & MBEDTLS_NET_POLL_WRITE) != 0) { + FD_SET((SOCKET_T)fd, &writeFds); + } + tv.tv_sec = ms / 1000; + tv.tv_usec = (ms % 1000) * 1000; + + /* Winsock takes no empty set */ + ret = select(fd + 1, + ((rw & MBEDTLS_NET_POLL_READ) != 0) ? &readFds : NULL, + ((rw & MBEDTLS_NET_POLL_WRITE) != 0) ? &writeFds : NULL, + NULL, forever ? NULL : &tv); + if (ret > 0) { + ret = 0; + if (FD_ISSET(fd, &readFds)) { + ret |= MBEDTLS_NET_POLL_READ; + } + if (FD_ISSET(fd, &writeFds)) { + ret |= MBEDTLS_NET_POLL_WRITE; + } + } + return ret; +} + +/* A timeout of UINT32_MAX waits until an event comes */ +int mbedtls_net_poll(mbedtls_net_context* ctx, uint32_t rw, uint32_t timeout) +{ + const mbedtls_ms_time_t deadline = MbedNetNow() + timeout; + int ret; + + WOLFSSL_ENTER("mbedtls_net_poll"); + + ret = MbedNetSelectable(ctx); + if (ret != 0) { + return ret; + } + if (rw > (MBEDTLS_NET_POLL_READ | MBEDTLS_NET_POLL_WRITE)) { + return MBEDTLS_ERR_NET_BAD_INPUT_DATA; + } + /* After a signal, the wait goes on to the same deadline */ + do { + ret = MbedNetSelect(ctx->fd, rw, deadline, timeout == UINT32_MAX); + } while (ret < 0 && MbedNetError() == SOCKET_EINTR); + + return (ret < 0) ? MBEDTLS_ERR_NET_POLL_FAILED : ret; +} + +static int MbedNetSetNonBlock(mbedtls_net_context* ctx, int nonBlock) +{ +#ifdef USE_WINDOWS_API + u_long on = (u_long)nonBlock; +#else + int flags; +#endif /* USE_WINDOWS_API */ + + if (ctx == NULL || ctx->fd < 0) { + return MBEDTLS_ERR_NET_INVALID_CONTEXT; + } +#ifdef USE_WINDOWS_API + if (ioctlsocket(ctx->fd, FIONBIO, &on) != 0) { + return MBEDTLS_ERR_NET_SOCKET_FAILED; + } +#else + flags = fcntl(ctx->fd, F_GETFL); + if (flags < 0) { + return MBEDTLS_ERR_NET_SOCKET_FAILED; + } + flags = nonBlock ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK); + if (fcntl(ctx->fd, F_SETFL, flags) != 0) { + return MBEDTLS_ERR_NET_SOCKET_FAILED; + } +#endif /* USE_WINDOWS_API */ + return 0; +} + +int mbedtls_net_set_block(mbedtls_net_context* ctx) +{ + WOLFSSL_ENTER("mbedtls_net_set_block"); + + return MbedNetSetNonBlock(ctx, 0); +} + +int mbedtls_net_set_nonblock(mbedtls_net_context* ctx) +{ + WOLFSSL_ENTER("mbedtls_net_set_nonblock"); + + return MbedNetSetNonBlock(ctx, 1); +} + void mbedtls_net_usleep(unsigned long usec) { WOLFSSL_ENTER("mbedtls_net_usleep"); @@ -357,23 +500,28 @@ void mbedtls_net_free(mbedtls_net_context* ctx) mbedtls_net_close(ctx); } -int mbedtls_net_recv(void* ctx, unsigned char* buf, size_t len) +static int MbedNetRecv(const mbedtls_net_context* net, unsigned char* buf, + size_t len, int flags) { - mbedtls_net_context* net = (mbedtls_net_context*)ctx; int ret; - WOLFSSL_ENTER("mbedtls_net_recv"); - if (net == NULL || net->fd < 0) { return MBEDTLS_ERR_NET_INVALID_CONTEXT; } - ret = wolfIO_Recv(net->fd, (char*)buf, MbedIoLen(len, INT_MAX), 0); + ret = wolfIO_Recv(net->fd, (char*)buf, MbedIoLen(len, INT_MAX), flags); if (ret >= 0) { return ret; } return MbedIoErr(ret, MBED_IO_RECV, 0); } +int mbedtls_net_recv(void* ctx, unsigned char* buf, size_t len) +{ + WOLFSSL_ENTER("mbedtls_net_recv"); + + return MbedNetRecv((const mbedtls_net_context*)ctx, buf, len, 0); +} + int mbedtls_net_send(void* ctx, const unsigned char* buf, size_t len) { mbedtls_net_context* net = (mbedtls_net_context*)ctx; @@ -392,6 +540,37 @@ int mbedtls_net_send(void* ctx, const unsigned char* buf, size_t len) return MbedIoErr(ret, MBED_IO_SEND, 0); } +/* Waits for data even on a non-blocking socket, without a limit for 0 */ +int mbedtls_net_recv_timeout(void* ctx, unsigned char* buf, size_t len, + uint32_t timeout) +{ + const mbedtls_net_context* net = (const mbedtls_net_context*)ctx; + const mbedtls_ms_time_t deadline = MbedNetNow() + timeout; + int ret; + + WOLFSSL_ENTER("mbedtls_net_recv_timeout"); + + ret = MbedNetSelectable(net); + if (ret != 0) { + return ret; + } + do { + ret = MbedNetSelect(net->fd, MBEDTLS_NET_POLL_READ, deadline, + timeout == 0); + if (ret == 0) { + return MBEDTLS_ERR_SSL_TIMEOUT; + } + if (ret < 0) { + return (MbedNetError() == SOCKET_EINTR) ? + MBEDTLS_ERR_SSL_WANT_READ : MBEDTLS_ERR_NET_RECV_FAILED; + } + /* Readable can still have no data, as on Linux: wait again */ + ret = MbedNetRecv(net, buf, len, MBED_NET_DONTWAIT); + } while (ret == MBEDTLS_ERR_SSL_WANT_READ); + + return ret; +} + #endif /* MBEDTLS_NET_C */ #endif /* WOLFSSL_PSA_ENGINE && WOLFPSA_MBEDTLS_COMPAT */ diff --git a/src/mbedtls/ssl.c b/src/mbedtls/ssl.c index becc8fe9..003ece3f 100644 --- a/src/mbedtls/ssl.c +++ b/src/mbedtls/ssl.c @@ -87,7 +87,8 @@ int MbedIoErr(int code, int dir, int toWolf) /* A receive of 0 bytes is EOF */ { WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_CONN_CLOSE), { 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), { MBEDTLS_ERR_SSL_TIMEOUT, MBEDTLS_ERR_SSL_TIMEOUT } }, { WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_ISR), { MBEDTLS_ERR_SSL_WANT_READ, MBEDTLS_ERR_SSL_WANT_WRITE } }, @@ -142,8 +143,11 @@ static int MbedSslCallErr(const mbedtls_ssl_context* ssl, int ret) int err = wolfSSL_get_error(ssl->ssl, ret); size_t i; - /* wolfSSL's error for any failed BIO call */ - if (err == WC_NO_ERR_TRACE(SOCKET_ERROR_E) && ssl->io_err != 0) { + /* The BIO's error behind a failed call, a reset, or a call to retry */ + if (ssl->io_err != 0 && (err == WC_NO_ERR_TRACE(SOCKET_ERROR_E) || + err == WC_NO_ERR_TRACE(SOCKET_PEER_CLOSED_E) || + err == WOLFSSL_ERROR_WANT_READ || + err == WOLFSSL_ERROR_WANT_WRITE)) { return ssl->io_err; } /* FATAL_ERROR is also wolfSSL's error after its own fatal alert */ @@ -218,14 +222,37 @@ static int MbedIoRet(mbedtls_ssl_context* s, int ret, int dir) return (ret > 0) ? ret : MbedIoErr(ret, dir, 1); } +/* As wolfSSL does with its own errors, drops a BIO error to retry */ +static void MbedIoStart(mbedtls_ssl_context* ssl) +{ + if (ssl->io_err == MBEDTLS_ERR_SSL_WANT_READ || + ssl->io_err == MBEDTLS_ERR_SSL_WANT_WRITE || + ssl->io_err == MBEDTLS_ERR_SSL_TIMEOUT) { + ssl->io_err = 0; + } +} + +/* As mbedtls_ssl_set_timer(): a delay of 0 stops the timer */ +static void MbedSetTimer(const mbedtls_ssl_context* ssl, uint32_t ms) +{ + if (ssl->f_set_timer != NULL) { + ssl->f_set_timer(ssl->p_timer, ms / 4, ms); + } +} + static int MbedRecvCb(WOLFSSL* ssl, char* buf, int sz, void* ctx) { mbedtls_ssl_context* s = (mbedtls_ssl_context*)ctx; int ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; (void)ssl; - if (s->f_recv_timeout != NULL) { - ret = s->f_recv_timeout(s->p_bio, (unsigned char*)buf, (size_t)sz, 0); + /* As in Mbed TLS, a timeout once the read timer expires (2) */ + if (s->f_get_timer != NULL && s->f_get_timer(s->p_timer) == 2) { + ret = MBEDTLS_ERR_SSL_TIMEOUT; + } + else if (s->f_recv_timeout != NULL) { + ret = s->f_recv_timeout(s->p_bio, (unsigned char*)buf, (size_t)sz, + s->conf->read_timeout); } else if (s->f_recv != NULL) { ret = s->f_recv(s->p_bio, (unsigned char*)buf, (size_t)sz); @@ -837,6 +864,15 @@ void mbedtls_ssl_conf_verify(mbedtls_ssl_config* conf, } } +void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config* conf, uint32_t timeout) +{ + WOLFSSL_ENTER("mbedtls_ssl_conf_read_timeout"); + + if (conf != NULL) { + conf->read_timeout = timeout; + } +} + #define MBED_SUITES_MAX (WOLFSSL_MAX_SUITE_SZ / 2) #define MBED_SUITE_NAME_SZ 64 @@ -1222,6 +1258,7 @@ int mbedtls_ssl_session_reset(mbedtls_ssl_context* ssl) } /* As in Mbed TLS, the session set before is not used again */ ssl->session_set = 0; + MbedSetTimer(ssl, 0); return MbedSslNew(ssl); } @@ -1239,7 +1276,7 @@ void mbedtls_ssl_set_bio(mbedtls_ssl_context* ssl, void* p_bio, } } -/* Stored for DTLS. As in Mbed TLS, cancels the timer running. */ +/* Read timeout timers. As in Mbed TLS, cancels the timer running. */ void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context* ssl, void* p_timer, mbedtls_ssl_set_timer_t* f_set_timer, mbedtls_ssl_get_timer_t* f_get_timer) { @@ -1249,9 +1286,7 @@ void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context* ssl, void* p_timer, ssl->p_timer = p_timer; ssl->f_set_timer = f_set_timer; ssl->f_get_timer = f_get_timer; - if (f_set_timer != NULL) { - f_set_timer(p_timer, 0, 0); - } + MbedSetTimer(ssl, 0); } } @@ -1333,6 +1368,7 @@ static int MbedSslStart(mbedtls_ssl_context* ssl) } if (ret == 0) { ssl->started = 1; + MbedIoStart(ssl); } return ret; } @@ -1460,15 +1496,27 @@ int mbedtls_ssl_check_pending(const mbedtls_ssl_context* ssl) int mbedtls_ssl_read(mbedtls_ssl_context* ssl, unsigned char* buf, size_t len) { + int buffered; int ret; WOLFSSL_ENTER("mbedtls_ssl_read"); - ret = MbedSslStart(ssl); + /* As in Mbed TLS, the handshake first, then the read timer */ + ret = mbedtls_ssl_is_handshake_over(ssl) ? MbedSslStart(ssl) : + mbedtls_ssl_handshake(ssl); if (ret != 0) { return ret; } + /* The timer runs only while waiting for a record */ + buffered = wolfSSL_pending(ssl->ssl) > 0; + if (!buffered && ssl->f_get_timer != NULL && + ssl->f_get_timer(ssl->p_timer) == -1) { + MbedSetTimer(ssl, ssl->conf->read_timeout); + } ret = wolfSSL_read(ssl->ssl, buf, MbedIoLen(len, INT_MAX)); + if (!buffered && (ret > 0 || wolfSSL_pending(ssl->ssl) > 0)) { + MbedSetTimer(ssl, 0); + } if (ret > 0) { return ret; } @@ -1495,6 +1543,7 @@ int mbedtls_ssl_write(mbedtls_ssl_context* ssl, ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) { return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; } + MbedIoStart(ssl); ret = wolfSSL_write(ssl->ssl, buf, MbedIoLen(len, max)); return (ret > 0) ? ret : MbedSslErr(ssl, ret); } @@ -1573,6 +1622,7 @@ int mbedtls_ssl_close_notify(mbedtls_ssl_context* ssl) if (ssl == NULL || ssl->ssl == NULL) { return MBEDTLS_ERR_SSL_BAD_INPUT_DATA; } + MbedIoStart(ssl); ret = wolfSSL_shutdown(ssl->ssl); if (ret == WOLFSSL_SUCCESS || ret == WC_NO_ERR_TRACE(WOLFSSL_SHUTDOWN_NOT_DONE)) { diff --git a/test/mbedtls_compat/ssl_test.c b/test/mbedtls_compat/ssl_test.c index 48ae9a7b..cd4a1992 100644 --- a/test/mbedtls_compat/ssl_test.c +++ b/test/mbedtls_compat/ssl_test.c @@ -30,7 +30,9 @@ #include #include #include +#include #include +#include #include #ifdef MBEDTLS_THREADING_C #include @@ -105,6 +107,14 @@ /* The version, config, field size and format bytes at the start of a saved * session */ #define SESSION_HDR_SZ 8 +/* A wait that times out, and one for data already sent, in ms */ +#define SHORT_WAIT_MS 10 +#define LONG_WAIT_MS 1000 +/* A record header and the start of its data */ +#define PARTIAL_RECORD_SZ 8 +/* A wait, and a signal during it, in ms */ +#define SIGNAL_WAIT_MS 300 +#define SIGNAL_AT_MS 200 /* Data in one direction. closed is set by the writer. */ typedef struct { @@ -123,6 +133,7 @@ typedef struct { int recvErr; /* When not -1, the bytes sends take before WANT_WRITE */ int sendRoom; + uint32_t timeout; /* the last one PipeRecvTimeout got */ } Peer; /* The CAs a peer trusts, and the certificates and key a server sends */ @@ -225,6 +236,16 @@ static int PipeRecv(void* ctx, unsigned char* buf, size_t len) return (int)len; } +/* PipeRecv, with MBEDTLS_ERR_SSL_TIMEOUT when there is no data */ +static int PipeRecvTimeout(void* ctx, unsigned char* buf, size_t len, + uint32_t timeout) +{ + int ret = PipeRecv(ctx, buf, len); + + ((Peer*)ctx)->timeout = timeout; + return (ret == MBEDTLS_ERR_SSL_WANT_READ) ? MBEDTLS_ERR_SSL_TIMEOUT : ret; +} + static void PeerInit(Peer* p, int endpoint) { const int second = (p == &cli2 || p == &srv2); @@ -279,10 +300,11 @@ static void PeerFree(Peer* p) mbedtls_ssl_config_free(&p->conf); } +/* The call is made again after these */ static int Waiting(int ret) { return ret == MBEDTLS_ERR_SSL_WANT_READ || - ret == MBEDTLS_ERR_SSL_WANT_WRITE; + ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_TIMEOUT; } /* The result of client. *srvRet gets server's. */ @@ -1369,6 +1391,95 @@ static int BioErrors(void) return ok; } +/* A read goes on after a timeout, between records and inside one */ +static int ReadTimeout(void) +{ + unsigned char buf[16]; + unsigned char rest[64]; + size_t restLen = 0; + int ok = Connect(MBEDTLS_SSL_VERIFY_NONE) == 0; + + if (ok) { + mbedtls_ssl_set_bio(&srv.ssl, &srv, PipeSend, NULL, PipeRecvTimeout); + ok = mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + MBEDTLS_ERR_SSL_TIMEOUT && srv.timeout == 0 && + mbedtls_ssl_write(&cli.ssl, (const unsigned char*)"hello", + 5) == 5 && + toServer.len > PARTIAL_RECORD_SZ && + toServer.len - PARTIAL_RECORD_SZ <= sizeof(rest); + } + if (ok) { + restLen = toServer.len - PARTIAL_RECORD_SZ; + memcpy(rest, toServer.buf + PARTIAL_RECORD_SZ, restLen); + toServer.len = PARTIAL_RECORD_SZ; + mbedtls_ssl_conf_read_timeout(&srv.conf, SHORT_WAIT_MS); + ok = mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + MBEDTLS_ERR_SSL_TIMEOUT && srv.timeout == SHORT_WAIT_MS; + memcpy(toServer.buf + toServer.len, rest, restLen); + toServer.len += restLen; + ok = ok && mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == 5 && + memcmp(buf, "hello", 5) == 0; + } + PeerFree(&cli); + PeerFree(&srv); + return ok; +} + +/* A read's BIO error is returned again only when it ended the connection */ +static int BioErrorKept(void) +{ + static const struct { + int err; + int next; /* the next read's result, with no BIO error */ + } errs[] = { + { MBEDTLS_ERR_SSL_TIMEOUT, MBEDTLS_ERR_SSL_WANT_READ }, + { MBEDTLS_ERR_NET_RECV_FAILED, MBEDTLS_ERR_NET_RECV_FAILED }, + { MBEDTLS_ERR_NET_CONN_RESET, MBEDTLS_ERR_NET_CONN_RESET } + }; + unsigned char buf[16]; + size_t i; + int ok = 1; + + for (i = 0; ok && i < sizeof(errs) / sizeof(errs[0]); i++) { + ok = Connect(MBEDTLS_SSL_VERIFY_NONE) == 0; + srv.recvErr = errs[i].err; + ok = ok && mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + errs[i].err; + srv.recvErr = 0; + ok = ok && mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + errs[i].next; + PeerFree(&cli); + PeerFree(&srv); + } + return ok; +} + +/* A write and a close_notify that the BIO asks to retry are sent once */ +static int SendRetry(void) +{ + unsigned char buf[16]; + int ok = Connect(MBEDTLS_SSL_VERIFY_NONE) == 0; + + cli.sendErr = MBEDTLS_ERR_SSL_WANT_WRITE; + ok = ok && mbedtls_ssl_write(&cli.ssl, (const unsigned char*)"hello", + 5) == MBEDTLS_ERR_SSL_WANT_WRITE; + cli.sendErr = 0; + ok = ok && mbedtls_ssl_write(&cli.ssl, (const unsigned char*)"hello", + 5) == 5; + cli.sendErr = MBEDTLS_ERR_SSL_WANT_WRITE; + ok = ok && mbedtls_ssl_close_notify(&cli.ssl) == + MBEDTLS_ERR_SSL_WANT_WRITE; + cli.sendErr = 0; + ok = ok && mbedtls_ssl_close_notify(&cli.ssl) == 0 && + mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == 5 && + memcmp(buf, "hello", 5) == 0 && + mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY; + PeerFree(&cli); + PeerFree(&srv); + return ok; +} + /* A length that does not fit in an int. As in Mbed TLS, a write sends at * most one record, so each write and read uses only RECORD_SZ bytes of buf. * A sanitizer report here means a write sent more than one record. */ @@ -1473,9 +1584,6 @@ static int OwnAlert(void) return ok; } -#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) || \ - defined(MBEDTLS_SSL_KEYING_MATERIAL_EXPORT) || \ - defined(MBEDTLS_SSL_PROTO_TLS1_3) static int Connected(void) { int s; @@ -2511,9 +2619,9 @@ static int SuitesThreads(void) } #endif /* MBEDTLS_THREADING_C */ -static int SetNonBlocking(int fd) +static int IsNonBlocking(int fd) { - return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) == 0; + return (fcntl(fd, F_GETFL) & O_NONBLOCK) != 0; } /* Waits at most a second for data, or a connection, on fd */ @@ -2534,7 +2642,7 @@ static int StillBound(int fd, int nonBlock, struct sockaddr_storage now; socklen_t nowLen = (socklen_t)sizeof(now); - return ((fcntl(fd, F_GETFL) & O_NONBLOCK) != 0) == nonBlock && + return IsNonBlocking(fd) == nonBlock && getsockname(fd, (struct sockaddr*)&now, &nowLen) == 0 && nowLen == addrLen && memcmp(&now, addr, addrLen) == 0; } @@ -2548,7 +2656,7 @@ static int AcceptBlock(int proto) mbedtls_net_init(&bound); mbedtls_net_init(&client); ok = mbedtls_net_bind(&bound, "127.0.0.1", "0", proto) == 0 && - SetNonBlocking(bound.fd) && + mbedtls_net_set_nonblock(&bound) == 0 && mbedtls_net_accept(&bound, &client, NULL, 0, NULL) == MBEDTLS_ERR_SSL_WANT_READ; mbedtls_net_free(&client); @@ -2583,6 +2691,17 @@ static int UdpEcho(mbedtls_net_context* accepted, mbedtls_net_context* conn, UdpRecvByte(conn, b); } +/* The address bound is bound to, and its port as text */ +static int BoundAddr(const mbedtls_net_context* bound, + struct sockaddr_storage* addr, socklen_t* addrLen, char* port, + socklen_t portSz) +{ + *addrLen = (socklen_t)sizeof(*addr); + return getsockname(bound->fd, (struct sockaddr*)addr, addrLen) == 0 && + getnameinfo((struct sockaddr*)addr, *addrLen, NULL, 0, port, portSz, + NI_NUMERICSERV) == 0; +} + /* Accepts two clients of host, with client_ip one byte short of ip's size, * then of its size */ static int AcceptIp(const char* bindHost, const char* host, @@ -2607,10 +2726,8 @@ static int AcceptIp(const char* bindHost, const char* host, mbedtls_net_init(&client[i]); } ok = mbedtls_net_bind(&bound, bindHost, "0", proto) == 0 && - (!nonBlock || SetNonBlocking(bound.fd)) && - getsockname(bound.fd, (struct sockaddr*)&addr, &addrLen) == 0 && - getnameinfo((struct sockaddr*)&addr, addrLen, NULL, 0, port, - sizeof(port), NI_NUMERICSERV) == 0; + (!nonBlock || mbedtls_net_set_nonblock(&bound) == 0) && + BoundAddr(&bound, &addr, &addrLen, port, sizeof(port)); for (i = 0; ok && i < 2; i++) { bufSz = ipLen - 1 + i; memset(buf, 0, sizeof(buf)); @@ -2683,6 +2800,191 @@ static int AcceptUdpBlocking(void) return AcceptLoopback(0, 0, MBEDTLS_NET_PROTO_UDP, 0); } +/* conn, connected over TCP on 127.0.0.1 to accepted */ +static int TcpPair(mbedtls_net_context* conn, mbedtls_net_context* accepted) +{ + mbedtls_net_context bound; + struct sockaddr_storage addr; + socklen_t addrLen; + char port[8]; + int ok; + + mbedtls_net_init(&bound); + mbedtls_net_init(conn); + mbedtls_net_init(accepted); + ok = mbedtls_net_bind(&bound, "127.0.0.1", "0", + MBEDTLS_NET_PROTO_TCP) == 0 && + BoundAddr(&bound, &addr, &addrLen, port, sizeof(port)) && + mbedtls_net_connect(conn, "127.0.0.1", port, + MBEDTLS_NET_PROTO_TCP) == 0 && + mbedtls_net_accept(&bound, accepted, NULL, 0, NULL) == 0; + mbedtls_net_free(&bound); + return ok; +} + +static int NetBlocking(void) +{ + mbedtls_net_context conn; + mbedtls_net_context accepted; + unsigned char b; + int ok = TcpPair(&conn, &accepted) && + mbedtls_net_set_nonblock(&conn) == 0 && IsNonBlocking(conn.fd) && + mbedtls_net_recv(&conn, &b, 1) == MBEDTLS_ERR_SSL_WANT_READ && + mbedtls_net_set_block(&conn) == 0 && !IsNonBlocking(conn.fd); + + mbedtls_net_free(&accepted); + mbedtls_net_free(&conn); + return ok; +} + +/* With a timeout of 0, the events ready now */ +static int NetPoll(void) +{ + const uint32_t rw = MBEDTLS_NET_POLL_READ | MBEDTLS_NET_POLL_WRITE; + mbedtls_net_context conn; + mbedtls_net_context accepted; + int ok = TcpPair(&conn, &accepted) && + mbedtls_net_poll(&conn, rw, 0) == MBEDTLS_NET_POLL_WRITE && + mbedtls_net_poll(&conn, MBEDTLS_NET_POLL_READ, SHORT_WAIT_MS) == 0 && + mbedtls_net_poll(&conn, MBEDTLS_NET_POLL_WRITE, UINT32_MAX) == + MBEDTLS_NET_POLL_WRITE && + mbedtls_net_send(&accepted, (const unsigned char*)"x", 1) == 1 && + mbedtls_net_poll(&conn, MBEDTLS_NET_POLL_READ, LONG_WAIT_MS) == + MBEDTLS_NET_POLL_READ && + mbedtls_net_poll(&conn, rw, 0) == (int)rw && + mbedtls_net_poll(&conn, rw + 1, 0) == MBEDTLS_ERR_NET_BAD_INPUT_DATA; + + mbedtls_net_free(&accepted); + mbedtls_net_free(&conn); + return ok; +} + +/* A timeout of 0 waits until data comes */ +static int NetRecvTimeout(void) +{ + mbedtls_net_context conn; + mbedtls_net_context accepted; + unsigned char b = 0; + int ok = TcpPair(&conn, &accepted) && + mbedtls_net_recv_timeout(&conn, &b, 1, SHORT_WAIT_MS) == + MBEDTLS_ERR_SSL_TIMEOUT && + mbedtls_net_set_nonblock(&conn) == 0 && + mbedtls_net_send(&accepted, (const unsigned char*)"x", 1) == 1 && + mbedtls_net_recv_timeout(&conn, &b, 1, LONG_WAIT_MS) == 1 && + b == 'x' && + mbedtls_net_send(&accepted, (const unsigned char*)"y", 1) == 1 && + mbedtls_net_recv_timeout(&conn, &b, 1, 0) == 1 && b == 'y'; + + mbedtls_net_free(&accepted); + ok = ok && mbedtls_net_recv_timeout(&conn, &b, 1, LONG_WAIT_MS) == 0; + mbedtls_net_free(&conn); + return ok; +} + +/* No socket, or one that select() does not take */ +static int NetBadContext(void) +{ + mbedtls_net_context net; + unsigned char b; + int ok; + + mbedtls_net_init(&net); + ok = mbedtls_net_set_block(&net) == MBEDTLS_ERR_NET_INVALID_CONTEXT && + mbedtls_net_set_nonblock(NULL) == MBEDTLS_ERR_NET_INVALID_CONTEXT && + mbedtls_net_poll(&net, MBEDTLS_NET_POLL_READ, 0) == + MBEDTLS_ERR_NET_INVALID_CONTEXT && + mbedtls_net_poll(NULL, MBEDTLS_NET_POLL_READ, 0) == + MBEDTLS_ERR_NET_INVALID_CONTEXT && + mbedtls_net_recv_timeout(&net, &b, 1, 0) == + MBEDTLS_ERR_NET_INVALID_CONTEXT && + mbedtls_net_recv_timeout(NULL, &b, 1, 0) == + MBEDTLS_ERR_NET_INVALID_CONTEXT; + /* At least FD_SETSIZE */ + net.fd = INT_MAX; + return ok && mbedtls_net_poll(&net, MBEDTLS_NET_POLL_READ, 0) == + MBEDTLS_ERR_NET_POLL_FAILED && + mbedtls_net_recv_timeout(&net, &b, 1, 0) == + MBEDTLS_ERR_NET_POLL_FAILED; +} + +#ifdef MBEDTLS_HAVE_TIME +static void OnAlarm(int sig) +{ + (void)sig; +} + +/* A signal does not make a poll longer, and ends a timed receive */ +static int NetInterrupted(void) +{ + struct sigaction onAlarm; + struct sigaction old; + struct itimerval alarmAt; + mbedtls_net_context conn; + mbedtls_net_context accepted; + mbedtls_ms_time_t start = 0; + unsigned char b; + int ok; + + memset(&onAlarm, 0, sizeof(onAlarm)); + onAlarm.sa_handler = OnAlarm; + memset(&alarmAt, 0, sizeof(alarmAt)); + alarmAt.it_value.tv_usec = SIGNAL_AT_MS * 1000; + ok = TcpPair(&conn, &accepted) && + sigaction(SIGALRM, &onAlarm, &old) == 0; + if (ok) { + start = mbedtls_ms_time(); + ok = setitimer(ITIMER_REAL, &alarmAt, NULL) == 0 && + mbedtls_net_poll(&conn, MBEDTLS_NET_POLL_READ, + SIGNAL_WAIT_MS) == 0 && + mbedtls_ms_time() - start < SIGNAL_WAIT_MS + SIGNAL_AT_MS && + setitimer(ITIMER_REAL, &alarmAt, NULL) == 0 && + mbedtls_net_recv_timeout(&conn, &b, 1, SIGNAL_WAIT_MS) == + MBEDTLS_ERR_SSL_WANT_READ; + (void)sigaction(SIGALRM, &old, NULL); + } + mbedtls_net_free(&accepted); + mbedtls_net_free(&conn); + return ok; +} +#endif /* MBEDTLS_HAVE_TIME */ + +/* The handshake and a read called again after a timeout, over TCP */ +static int ReadTimeoutTcp(void) +{ + mbedtls_net_context conn; + mbedtls_net_context accepted; + unsigned char buf[16]; + int s; + int ok = TcpPair(&conn, &accepted) && + mbedtls_net_set_nonblock(&accepted) == 0 && + PeerNew(&cli, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_VERIFY_REQUIRED, 1, + HOST_NAME) == 0 && + PeerNew(&srv, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_VERIFY_NONE, 0, + NULL) == 0; + + if (ok) { + mbedtls_ssl_conf_read_timeout(&cli.conf, SHORT_WAIT_MS); + mbedtls_ssl_set_bio(&cli.ssl, &conn, mbedtls_net_send, NULL, + mbedtls_net_recv_timeout); + mbedtls_ssl_set_bio(&srv.ssl, &accepted, mbedtls_net_send, + mbedtls_net_recv, NULL); + /* Before the server answers */ + ok = mbedtls_ssl_handshake(&cli.ssl) == MBEDTLS_ERR_SSL_TIMEOUT && + Handshake(&s) == 0 && s == 0 && + mbedtls_ssl_read(&cli.ssl, buf, sizeof(buf)) == + MBEDTLS_ERR_SSL_TIMEOUT && + mbedtls_ssl_write(&srv.ssl, (const unsigned char*)"hello", + 5) == 5 && + mbedtls_ssl_read(&cli.ssl, buf, sizeof(buf)) == 5 && + memcmp(buf, "hello", 5) == 0; + } + PeerFree(&cli); + PeerFree(&srv); + mbedtls_net_free(&accepted); + mbedtls_net_free(&conn); + return ok; +} + /* Each listed suite's name gives back its ID */ static int SuiteIdFromName(void) { @@ -2828,6 +3130,69 @@ static int TimerCb(void) mbedtls_ssl_free(&ssl); return ok; } + +/* The read timeout on the timer callbacks, for non-blocking I/O */ +static int TimerReadTimeout(void) +{ + mbedtls_timing_delay_context timer; + unsigned char buf[16]; + int ok = Connect(MBEDTLS_SSL_VERIFY_NONE) == 0; + + if (ok) { + mbedtls_ssl_conf_read_timeout(&srv.conf, SHORT_WAIT_MS); + mbedtls_ssl_set_timer_cb(&srv.ssl, &timer, mbedtls_timing_set_delay, + mbedtls_timing_get_delay); + ok = mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + MBEDTLS_ERR_SSL_WANT_READ && + mbedtls_timing_get_delay(&timer) >= 0; + mbedtls_net_usleep(2 * SHORT_WAIT_MS * 1000); + /* As in Mbed TLS, the timer stays expired until it is set again */ + ok = ok && mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + MBEDTLS_ERR_SSL_TIMEOUT && + mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + MBEDTLS_ERR_SSL_TIMEOUT; + mbedtls_ssl_set_timer_cb(&srv.ssl, &timer, mbedtls_timing_set_delay, + mbedtls_timing_get_delay); + ok = ok && mbedtls_ssl_write(&cli.ssl, (const unsigned char*)"hello", + 5) == 5 && + mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == 5 && + mbedtls_timing_get_delay(&timer) == -1 && + mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == + MBEDTLS_ERR_SSL_WANT_READ && + mbedtls_timing_get_delay(&timer) >= 0 && + mbedtls_ssl_session_reset(&srv.ssl) == 0 && + mbedtls_timing_get_delay(&timer) == -1; + } + PeerFree(&cli); + PeerFree(&srv); + return ok; +} + +/* A zero-length read that gets a record stops the timer, as Mbed TLS does */ +static int TimerBufferedRead(void) +{ + mbedtls_timing_delay_context timer; + unsigned char buf[16]; + int ok = Connect(MBEDTLS_SSL_VERIFY_NONE) == 0; + + if (ok) { + mbedtls_ssl_conf_read_timeout(&srv.conf, LONG_WAIT_MS); + mbedtls_ssl_set_timer_cb(&srv.ssl, &timer, mbedtls_timing_set_delay, + mbedtls_timing_get_delay); + ok = mbedtls_ssl_write(&cli.ssl, (const unsigned char*)"hello", + 5) == 5 && + mbedtls_ssl_read(&srv.ssl, buf, 0) == 0 && + mbedtls_ssl_get_bytes_avail(&srv.ssl) == 5 && + mbedtls_timing_get_delay(&timer) == -1 && + mbedtls_ssl_read(&srv.ssl, buf, 2) == 2 && + mbedtls_timing_get_delay(&timer) == -1 && + mbedtls_ssl_read(&srv.ssl, buf, sizeof(buf)) == 3 && + mbedtls_timing_get_delay(&timer) == -1; + } + PeerFree(&cli); + PeerFree(&srv); + return ok; +} #endif /* MBEDTLS_TIMING_C */ static const struct { @@ -2898,6 +3263,9 @@ static const struct { { "alerts after the handshake", SendAlertPeer }, { "alert after a failed verify", SendAlertVerifyFailed }, { "BIO errors", BioErrors }, + { "read after a timeout", ReadTimeout }, + { "BIO error kept only when it ends the connection", BioErrorKept }, + { "write and close_notify sent once after a retry", SendRetry }, { "length over INT_MAX", LargeLength }, { "record expansion", RecordExpansion }, #ifdef MBEDTLS_SSL_MAX_FRAGMENT_LENGTH @@ -2952,12 +3320,22 @@ static const struct { { "UDP accept, bound to every IPv4 address", AcceptUdpAnyIpv4 }, { "UDP accept, bound to every IPv6 address", AcceptUdpAnyIpv6 }, { "UDP accept, blocking listener", AcceptUdpBlocking }, + { "blocking and non-blocking socket", NetBlocking }, + { "poll", NetPoll }, + { "receive with a timeout", NetRecvTimeout }, + { "network calls without a usable socket", NetBadContext }, +#ifdef MBEDTLS_HAVE_TIME + { "poll and timed receive interrupted", NetInterrupted }, +#endif /* MBEDTLS_HAVE_TIME */ + { "read timeout over TCP", ReadTimeoutTcp }, { "cipher suite ID from its name", SuiteIdFromName }, { "cipher suite info", SuiteInfo }, { "handshake over", HandshakeOver }, { "debug threshold and callback ignored", DebugIgnored }, #ifdef MBEDTLS_TIMING_C { "timer callbacks", TimerCb }, + { "read timeout on the timer callbacks", TimerReadTimeout }, + { "read timer with buffered data", TimerBufferedRead }, #endif /* MBEDTLS_TIMING_C */ { "user data kept through session reset", UserData } }; diff --git a/wolfpsa/mbedtls_compat/mbedtls/net_sockets.h b/wolfpsa/mbedtls_compat/mbedtls/net_sockets.h index b4131ead..516a5496 100644 --- a/wolfpsa/mbedtls_compat/mbedtls/net_sockets.h +++ b/wolfpsa/mbedtls_compat/mbedtls/net_sockets.h @@ -25,6 +25,7 @@ #include "mbedtls/ssl.h" #include +#include #ifdef __cplusplus extern "C" { @@ -37,7 +38,9 @@ #define MBEDTLS_ERR_NET_CONNECT_FAILED -0x0044 #define MBEDTLS_ERR_NET_INVALID_CONTEXT -0x0045 #define MBEDTLS_ERR_NET_BIND_FAILED -0x0046 +#define MBEDTLS_ERR_NET_POLL_FAILED -0x0047 #define MBEDTLS_ERR_NET_LISTEN_FAILED -0x0048 +#define MBEDTLS_ERR_NET_BAD_INPUT_DATA -0x0049 #define MBEDTLS_ERR_NET_ACCEPT_FAILED -0x004A #define MBEDTLS_ERR_NET_RECV_FAILED -0x004C #define MBEDTLS_ERR_NET_SEND_FAILED -0x004E @@ -47,6 +50,9 @@ #define MBEDTLS_NET_LISTEN_BACKLOG 10 +#define MBEDTLS_NET_POLL_READ 1 +#define MBEDTLS_NET_POLL_WRITE 2 + typedef struct mbedtls_net_context { int fd; } mbedtls_net_context; @@ -59,11 +65,16 @@ int mbedtls_net_bind(mbedtls_net_context* ctx, const char* bind_ip, int mbedtls_net_accept(mbedtls_net_context* bind_ctx, mbedtls_net_context* client_ctx, void* client_ip, size_t buf_size, size_t* cip_len); +int mbedtls_net_poll(mbedtls_net_context* ctx, uint32_t rw, uint32_t timeout); +int mbedtls_net_set_block(mbedtls_net_context* ctx); +int mbedtls_net_set_nonblock(mbedtls_net_context* ctx); void mbedtls_net_usleep(unsigned long usec); void mbedtls_net_close(mbedtls_net_context* ctx); void mbedtls_net_free(mbedtls_net_context* ctx); int mbedtls_net_recv(void* ctx, unsigned char* buf, size_t len); int mbedtls_net_send(void* ctx, const unsigned char* buf, size_t len); +int mbedtls_net_recv_timeout(void* ctx, unsigned char* buf, size_t len, + uint32_t timeout); #ifdef __cplusplus } /* extern "C" */ diff --git a/wolfpsa/mbedtls_compat/mbedtls/ssl.h b/wolfpsa/mbedtls_compat/mbedtls/ssl.h index 90fb6662..57a9c9c6 100644 --- a/wolfpsa/mbedtls_compat/mbedtls/ssl.h +++ b/wolfpsa/mbedtls_compat/mbedtls/ssl.h @@ -272,6 +272,7 @@ typedef struct mbedtls_ssl_config { mbedtls_x509_crl* ca_crl; /* not supported, an error when set */ int (*f_vrfy)(void*, mbedtls_x509_crt*, int, uint32_t*); void* p_vrfy; + uint32_t read_timeout; /* ms, passed to f_recv_timeout */ mbedtls_ssl_user_data_t user_data; } mbedtls_ssl_config; @@ -348,6 +349,7 @@ void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config* conf, const mbedtls_x509_crt_profile* profile); void mbedtls_ssl_conf_verify(mbedtls_ssl_config* conf, int (*f_vrfy)(void*, mbedtls_x509_crt*, int, uint32_t*), void* p_vrfy); +void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config* conf, uint32_t timeout); static inline void mbedtls_ssl_conf_set_user_data_p(mbedtls_ssl_config* conf, void* p) From 9753f0a9964cbf093384a64d798f6dc8029de008 Mon Sep 17 00:00:00 2001 From: Paul Adelsbach Date: Tue, 24 Nov 2026 12:07:15 -0800 Subject: [PATCH 2/2] Resend DTLS flights on a handshake timeout, poll in the TCP test --- docs/mbedtls-compatibility.md | 3 +++ src/mbedtls/net.c | 4 ++-- src/mbedtls/ssl.c | 14 ++++++++++---- test/mbedtls_compat/ssl_test.c | 25 +++++++++++++++++++------ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/docs/mbedtls-compatibility.md b/docs/mbedtls-compatibility.md index e2fba695..ce6fb53b 100644 --- a/docs/mbedtls-compatibility.md +++ b/docs/mbedtls-compatibility.md @@ -254,6 +254,9 @@ TLS: Mbed TLS leaves the connection as it was. - `mbedtls_ssl_ciphersuite_t` has the ID, name and TLS versions only, not the cipher, MAC, key exchange or flags. +- The timers of `mbedtls_ssl_set_timer_cb` are used only for + `mbedtls_ssl_conf_read_timeout`. Mbed TLS also uses them for DTLS + retransmission. Certificate verification: diff --git a/src/mbedtls/net.c b/src/mbedtls/net.c index e8d0b8f8..8cb55dd8 100644 --- a/src/mbedtls/net.c +++ b/src/mbedtls/net.c @@ -257,7 +257,7 @@ static int MbedNetUdpAccept(mbedtls_net_context* bind_ctx, #ifndef USE_WINDOWS_API /* Keeps the blocking mode, which Winsock cannot read */ flags = fcntl(client_ctx->fd, F_GETFL); - if (flags < 0 || fcntl(fd, F_SETFL, flags & O_NONBLOCK) != 0) { + if (flags < 0 || fcntl(fd, F_SETFL, flags & O_NONBLOCK) < 0) { ret = MBEDTLS_ERR_NET_SOCKET_FAILED; } #endif /* !USE_WINDOWS_API */ @@ -443,7 +443,7 @@ static int MbedNetSetNonBlock(mbedtls_net_context* ctx, int nonBlock) return MBEDTLS_ERR_NET_SOCKET_FAILED; } flags = nonBlock ? (flags | O_NONBLOCK) : (flags & ~O_NONBLOCK); - if (fcntl(ctx->fd, F_SETFL, flags) != 0) { + if (fcntl(ctx->fd, F_SETFL, flags) < 0) { return MBEDTLS_ERR_NET_SOCKET_FAILED; } #endif /* USE_WINDOWS_API */ diff --git a/src/mbedtls/ssl.c b/src/mbedtls/ssl.c index 003ece3f..48889e22 100644 --- a/src/mbedtls/ssl.c +++ b/src/mbedtls/ssl.c @@ -87,8 +87,7 @@ int MbedIoErr(int code, int dir, int toWolf) /* A receive of 0 bytes is EOF */ { WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_CONN_CLOSE), { 0, MBEDTLS_ERR_NET_CONN_RESET } }, - /* As in Mbed TLS, the next call goes on after a timeout */ - { WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_WANT_READ), + { WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_TIMEOUT), { MBEDTLS_ERR_SSL_TIMEOUT, MBEDTLS_ERR_SSL_TIMEOUT } }, { WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_ISR), { MBEDTLS_ERR_SSL_WANT_READ, MBEDTLS_ERR_SSL_WANT_WRITE } }, @@ -245,7 +244,6 @@ static int MbedRecvCb(WOLFSSL* ssl, char* buf, int sz, void* ctx) mbedtls_ssl_context* s = (mbedtls_ssl_context*)ctx; int ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA; - (void)ssl; /* As in Mbed TLS, a timeout once the read timer expires (2) */ if (s->f_get_timer != NULL && s->f_get_timer(s->p_timer) == 2) { ret = MBEDTLS_ERR_SSL_TIMEOUT; @@ -257,7 +255,15 @@ static int MbedRecvCb(WOLFSSL* ssl, char* buf, int sz, void* ctx) else if (s->f_recv != NULL) { ret = s->f_recv(s->p_bio, (unsigned char*)buf, (size_t)sz); } - return MbedIoRet(s, ret, MBED_IO_RECV); + ret = MbedIoRet(s, ret, MBED_IO_RECV); + /* wolfSSL resends a DTLS flight on a timeout. Otherwise, as in Mbed TLS, + * the next call goes on. */ + if (ret == WC_NO_ERR_TRACE(WOLFSSL_CBIO_ERR_TIMEOUT) && + (s->conf->transport == MBEDTLS_SSL_TRANSPORT_STREAM || + wolfSSL_is_init_finished(ssl))) { + ret = WOLFSSL_CBIO_ERR_WANT_READ; + } + return ret; } static int MbedSendCb(WOLFSSL* ssl, char* buf, int sz, void* ctx) diff --git a/test/mbedtls_compat/ssl_test.c b/test/mbedtls_compat/ssl_test.c index cd4a1992..d0f26b6a 100644 --- a/test/mbedtls_compat/ssl_test.c +++ b/test/mbedtls_compat/ssl_test.c @@ -134,6 +134,7 @@ typedef struct { /* When not -1, the bytes sends take before WANT_WRITE */ int sendRoom; uint32_t timeout; /* the last one PipeRecvTimeout got */ + mbedtls_net_context* net; /* when set, polled before handshake steps */ } Peer; /* The CAs a peer trusts, and the certificates and key a server sends */ @@ -259,6 +260,7 @@ static void PeerInit(Peer* p, int endpoint) p->sendErr = 0; p->recvErr = 0; p->sendRoom = -1; + p->net = NULL; memset(p->tx, 0, sizeof(*p->tx)); mbedtls_ssl_set_bio(&p->ssl, p, PipeSend, PipeRecv, NULL); } @@ -307,6 +309,15 @@ static int Waiting(int ret) ret == MBEDTLS_ERR_SSL_WANT_WRITE || ret == MBEDTLS_ERR_SSL_TIMEOUT; } +/* A handshake step, after data reaches p's socket, if it has one */ +static int HandshakeStep(Peer* p) +{ + if (p->net != NULL) { + (void)mbedtls_net_poll(p->net, MBEDTLS_NET_POLL_READ, LONG_WAIT_MS); + } + return mbedtls_ssl_handshake(&p->ssl); +} + /* The result of client. *srvRet gets server's. */ static int HandshakeOf(Peer* client, Peer* server, int* srvRet) { @@ -316,10 +327,10 @@ static int HandshakeOf(Peer* client, Peer* server, int* srvRet) for (i = 0; i < MAX_STEPS && (Waiting(c) || Waiting(s)); i++) { if (Waiting(c)) { - c = mbedtls_ssl_handshake(&client->ssl); + c = HandshakeStep(client); } if (Waiting(s)) { - s = mbedtls_ssl_handshake(&server->ssl); + s = HandshakeStep(server); } } *srvRet = s; @@ -1338,11 +1349,10 @@ static int SendAlertPlain(void) return ok; } -/* The client's send or receive fails with err during the handshake. It sends - * before it receives. */ +/* The client's send or receive fails with err in its first handshake call. It + * sends before it receives. */ static int BioErrorHandshake(int send, int err) { - int s; int ok = PeerNew(&cli, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_VERIFY_REQUIRED, 1, HOST_NAME) == 0 && PeerNew(&srv, MBEDTLS_SSL_IS_SERVER, MBEDTLS_SSL_VERIFY_NONE, 0, @@ -1350,7 +1360,7 @@ static int BioErrorHandshake(int send, int err) if (ok) { *(send ? &cli.sendErr : &cli.recvErr) = err; - ok = Handshake(&s) == err; + ok = mbedtls_ssl_handshake(&cli.ssl) == err; } PeerFree(&cli); PeerFree(&srv); @@ -2968,6 +2978,7 @@ static int ReadTimeoutTcp(void) mbedtls_net_recv_timeout); mbedtls_ssl_set_bio(&srv.ssl, &accepted, mbedtls_net_send, mbedtls_net_recv, NULL); + srv.net = &accepted; /* Before the server answers */ ok = mbedtls_ssl_handshake(&cli.ssl) == MBEDTLS_ERR_SSL_TIMEOUT && Handshake(&s) == 0 && s == 0 && @@ -2975,6 +2986,8 @@ static int ReadTimeoutTcp(void) MBEDTLS_ERR_SSL_TIMEOUT && mbedtls_ssl_write(&srv.ssl, (const unsigned char*)"hello", 5) == 5 && + mbedtls_net_poll(&conn, MBEDTLS_NET_POLL_READ, LONG_WAIT_MS) == + MBEDTLS_NET_POLL_READ && mbedtls_ssl_read(&cli.ssl, buf, sizeof(buf)) == 5 && memcmp(buf, "hello", 5) == 0; }