Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions docs/mbedtls-compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -257,8 +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 not used. Mbed TLS uses them
for DTLS and for `mbedtls_ssl_conf_read_timeout`.
- 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:

Expand Down Expand Up @@ -357,6 +355,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
Expand Down
2 changes: 2 additions & 0 deletions src/mbedtls/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
199 changes: 189 additions & 10 deletions src/mbedtls/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#if defined(MBEDTLS_NET_C)

#include <limits.h>
#include <mbedtls/platform_time.h>
#include "mbedtls_compat_internal.h"
#ifndef USE_WINDOWS_API
#include <fcntl.h>
Expand All @@ -57,6 +58,13 @@
#include <sys/select.h>
#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");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -243,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 */
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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 */
Loading
Loading