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
28 changes: 25 additions & 3 deletions ports/common/src/tedge_sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ struct conn {
bool used;
bool tls;
int fd;
/* The socket's errno behind the last failed TLS read or write, so a
* failure can say what went wrong underneath. */
int bio_errno;
mbedtls_ssl_context ssl;
mbedtls_ssl_config conf;
mbedtls_x509_crt ca;
Expand All @@ -69,6 +72,16 @@ struct conn {
static struct conn conns[MAX_CONNS];
TEDGE_PORT_MUTEX_DEFINE(conns_lock);

/* A TLS read or write failed for good: say how, since the caller only sees
* -ECONNRESET. A reset from the peer shows as errno 104 underneath; an
* mbedTLS code without an errno is TLS's own (out of memory is -0x7f00). */
static void tls_failed(int handle, struct conn *c, const char *what, int ret)
{
TEDGE_LOG_WRN("TLS: connection %d: %s failed (-0x%04x, errno %d)",
handle, what, (unsigned int)-ret, c->bio_errno);
c->bio_errno = 0;
}

static int64_t now_ms(void)
{
return tedge_port_uptime_ms();
Expand All @@ -92,6 +105,7 @@ static int conn_alloc(int fd, bool tls)
conns[i].used = true;
conns[i].tls = tls;
conns[i].fd = fd;
conns[i].bio_errno = 0;
if (tls) {
/* Before anything can fail, so conn_free()
* always frees initialised state. */
Expand Down Expand Up @@ -294,6 +308,7 @@ static int bio_send(void *ctx, const unsigned char *buf, size_t len)
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
return MBEDTLS_ERR_SSL_WANT_WRITE;
}
c->bio_errno = errno;
return (errno == EPIPE || errno == ECONNRESET) ? MBEDTLS_ERR_NET_CONN_RESET
: MBEDTLS_ERR_NET_SEND_FAILED;
}
Expand All @@ -309,6 +324,7 @@ static int bio_recv(void *ctx, unsigned char *buf, size_t len)
if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
return MBEDTLS_ERR_SSL_WANT_READ;
}
c->bio_errno = errno;
return (errno == ECONNRESET) ? MBEDTLS_ERR_NET_CONN_RESET
: MBEDTLS_ERR_NET_RECV_FAILED;
}
Expand Down Expand Up @@ -538,6 +554,7 @@ int tedge_port_send(int conn, const void *buf, size_t len)
if (ret == MBEDTLS_ERR_SSL_WANT_READ) {
want = POLLIN;
} else if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
tls_failed(conn, c, "write", ret);
return -ECONNRESET;
}
} else {
Expand Down Expand Up @@ -572,15 +589,20 @@ int tedge_port_recv(int conn, void *buf, size_t len, uint32_t timeout_ms)

if (c->tls) {
ret = mbedtls_ssl_read(&c->ssl, buf, len);
if (ret >= 0) {
return ret; /* 0: the peer closed */
if (ret > 0) {
return ret;
}
if (ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
if (ret == 0 || ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
TEDGE_LOG_DBG("TLS: connection %d: the peer closed "
"it%s", conn,
ret == 0 ? " without close_notify"
: "");
return 0;
}
if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) {
want = POLLOUT;
} else if (ret != MBEDTLS_ERR_SSL_WANT_READ) {
tls_failed(conn, c, "read", ret);
return -ECONNRESET;
}
} else {
Expand Down
4 changes: 3 additions & 1 deletion ports/nuttx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ if(CONFIG_TINY_EDGE)
TEDGE_POSIX_THREAD_STACKS
TEDGE_POSIX_MAC_IOCTL
TEDGE_POSIX_NO_EXEC
# mbedTLS's Unix code paths; threading comes from the user config.
# mbedTLS's Unix code paths; threading and the memory settings come
# from the user configs.
unix
TF_PSA_CRYPTO_USER_CONFIG_FILE="tedge_mbedtls_user_config.h"
MBEDTLS_USER_CONFIG_FILE="tedge_mbedtls_tls_config.h"
COMPILE_FLAGS
-std=gnu11)
endif()
4 changes: 3 additions & 1 deletion ports/nuttx/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ CFLAGS += ${DEFINE_PREFIX}TEDGE_POSIX_MAC_IOCTL
# only with CONFIG_LIBC_EXECFUNCS.
CFLAGS += ${DEFINE_PREFIX}TEDGE_POSIX_NO_EXEC
# mbedTLS's Unix code paths (time, entropy from /dev/urandom, pthreads),
# as nuttx-apps builds mbedTLS 3; threading is added by the user config.
# as nuttx-apps builds mbedTLS 3; the two user configs (include/) add
# threading and the memory settings.
CFLAGS += ${DEFINE_PREFIX}unix
CFLAGS += ${DEFINE_PREFIX}TF_PSA_CRYPTO_USER_CONFIG_FILE=\"tedge_mbedtls_user_config.h\"
CFLAGS += ${DEFINE_PREFIX}MBEDTLS_USER_CONFIG_FILE=\"tedge_mbedtls_tls_config.h\"
CFLAGS += -std=gnu11

include $(APPDIR)/Application.mk
14 changes: 14 additions & 0 deletions ports/nuttx/include/tedge_mbedtls_tls_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* SPDX-License-Identifier: Apache-2.0
*
* mbedTLS 4 TLS settings on top of its defaults (MBEDTLS_USER_CONFIG_FILE).
*/

/* The client only sends small records (MQTT publishes, HTTP request heads,
* upload chunks, WebSocket frames); a larger write is split into several
* records. Only received records need the 16 KB the server may use. This
* saves 12 KB of heap for each TLS session. */
#undef MBEDTLS_SSL_OUT_CONTENT_LEN
#define MBEDTLS_SSL_OUT_CONTENT_LEN 4096

/* A TLS client only: the server role's handshake is never used. */
#undef MBEDTLS_SSL_SRV_C
15 changes: 13 additions & 2 deletions ports/nuttx/include/tedge_mbedtls_user_config.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
/* SPDX-License-Identifier: Apache-2.0
*
* mbedTLS 4 (TF-PSA-Crypto) settings on top of its defaults, as the POSIX
* port sets them: the client's threads share the PSA key store.
* mbedTLS 4 crypto (TF-PSA-Crypto) settings on top of its defaults
* (TF_PSA_CRYPTO_USER_CONFIG_FILE).
*/

/* The client's threads share the PSA key store, as on the POSIX port. */
#define MBEDTLS_THREADING_C
#define MBEDTLS_THREADING_PTHREAD

/* Without this, every PSA call copies its input and its output into
* buffers of its own on the heap: for each TLS record that is encrypted or
* decrypted, up to two 16 KB copies. With a firmware download and the MQTT
* session both busy, those copies ran the heap out, and TLS failed with
* PSA_ERROR_INSUFFICIENT_MEMORY on both connections (thin-edge/tiny-edge#32).
* The copies only protect buffers shared with an untrusted party; here every
* buffer belongs to the one application, as on ESP-IDF, whose mbedTLS
* configuration sets this too. */
#define MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS
Loading