Skip to content

Commit 514f18f

Browse files
jetmtrini
authored andcommitted
net: lwip: Update lwIP for mbedTLS > 3.0 support and enable https
The current code support mbedTLS 2.28. Since we are using a newer version in U-Boot, update the necessary accessors and the lwIP codebase to work with mbedTLS 3.6.0. It's worth noting that the patches are already sent to lwIP [0] While at it enable LWIP_ALTCP_TLS and enable TLS support in lwIP [0] lwip-tcpip/lwip#47 Signed-off-by: Javier Tia <[email protected]> Acked-by: Jerome Forissier <[email protected]> Signed-off-by: Ilias Apalodimas <[email protected]>
1 parent a564f50 commit 514f18f

File tree

4 files changed

+34
-24
lines changed

4 files changed

+34
-24
lines changed

lib/lwip/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ obj-y += \
5353
lwip/src/core/timeouts.o \
5454
lwip/src/core/udp.o \
5555
lwip/src/netif/ethernet.o
56+
57+
obj-$(CONFIG_MBEDTLS_LIB_TLS) += lwip/src/apps/altcp_tls/altcp_tls_mbedtls.o \
58+
lwip/src/apps/altcp_tls/altcp_tls_mbedtls_mem.o

lib/lwip/lwip/src/apps/altcp_tls/altcp_tls_mbedtls.c

+24-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Application layered TCP/TLS connection API (to be used from TCPIP thread)
44
*
55
* This file provides a TLS layer using mbedTLS
6-
*
6+
*
77
* This version is currently compatible with the 2.x.x branch (current LTS).
88
*/
99

@@ -70,7 +70,6 @@
7070
/* @todo: which includes are really needed? */
7171
#include "mbedtls/entropy.h"
7272
#include "mbedtls/ctr_drbg.h"
73-
#include "mbedtls/certs.h"
7473
#include "mbedtls/x509.h"
7574
#include "mbedtls/ssl.h"
7675
#include "mbedtls/net_sockets.h"
@@ -81,8 +80,6 @@
8180
#include "mbedtls/ssl_cache.h"
8281
#include "mbedtls/ssl_ticket.h"
8382

84-
#include "mbedtls/ssl_internal.h" /* to call mbedtls_flush_output after ERR_MEM */
85-
8683
#include <string.h>
8784

8885
#ifndef ALTCP_MBEDTLS_ENTROPY_PTR
@@ -132,6 +129,16 @@ static err_t altcp_mbedtls_lower_recv_process(struct altcp_pcb *conn, altcp_mbed
132129
static err_t altcp_mbedtls_handle_rx_appldata(struct altcp_pcb *conn, altcp_mbedtls_state_t *state);
133130
static int altcp_mbedtls_bio_send(void *ctx, const unsigned char *dataptr, size_t size);
134131

132+
static void
133+
altcp_mbedtls_flush_output(altcp_mbedtls_state_t *state)
134+
{
135+
if (state->ssl_context.MBEDTLS_PRIVATE(out_left) != 0) {
136+
int flushed = mbedtls_ssl_send_alert_message(&state->ssl_context, 0, 0);
137+
if (flushed) {
138+
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_ssl_send_alert_message failed: %d\n", flushed));
139+
}
140+
}
141+
}
135142

136143
/* callback functions from inner/lower connection: */
137144

@@ -524,14 +531,14 @@ altcp_mbedtls_lower_sent(void *arg, struct altcp_pcb *inner_conn, u16_t len)
524531
LWIP_ASSERT("state", state != NULL);
525532
LWIP_ASSERT("pcb mismatch", conn->inner_conn == inner_conn);
526533
/* calculate TLS overhead part to not send it to application */
527-
overhead = state->overhead_bytes_adjust + state->ssl_context.out_left;
534+
overhead = state->overhead_bytes_adjust + state->ssl_context.MBEDTLS_PRIVATE(out_left);
528535
if ((unsigned)overhead > len) {
529536
overhead = len;
530537
}
531538
/* remove ACKed bytes from overhead adjust counter */
532539
state->overhead_bytes_adjust -= len;
533540
/* try to send more if we failed before (may increase overhead adjust counter) */
534-
mbedtls_ssl_flush_output(&state->ssl_context);
541+
altcp_mbedtls_flush_output(state);
535542
/* remove calculated overhead from ACKed bytes len */
536543
app_len = len - (u16_t)overhead;
537544
/* update application write counter and inform application */
@@ -559,7 +566,7 @@ altcp_mbedtls_lower_poll(void *arg, struct altcp_pcb *inner_conn)
559566
if (conn->state) {
560567
altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state;
561568
/* try to send more if we failed before */
562-
mbedtls_ssl_flush_output(&state->ssl_context);
569+
altcp_mbedtls_flush_output(state);
563570
if (altcp_mbedtls_handle_rx_appldata(conn, state) == ERR_ABRT) {
564571
return ERR_ABRT;
565572
}
@@ -683,7 +690,7 @@ altcp_tls_set_session(struct altcp_pcb *conn, struct altcp_tls_session *session)
683690
if (session && conn && conn->state) {
684691
altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state;
685692
int ret = -1;
686-
if (session->data.start)
693+
if (session->data.MBEDTLS_PRIVATE(start))
687694
ret = mbedtls_ssl_set_session(&state->ssl_context, &session->data);
688695
return ret < 0 ? ERR_VAL : ERR_OK;
689696
}
@@ -776,7 +783,7 @@ altcp_tls_create_config(int is_server, u8_t cert_count, u8_t pkey_count, int hav
776783
struct altcp_tls_config *conf;
777784
mbedtls_x509_crt *mem;
778785

779-
if (TCP_WND < MBEDTLS_SSL_MAX_CONTENT_LEN) {
786+
if (TCP_WND < MBEDTLS_SSL_IN_CONTENT_LEN || TCP_WND < MBEDTLS_SSL_OUT_CONTENT_LEN) {
780787
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG|LWIP_DBG_LEVEL_SERIOUS,
781788
("altcp_tls: TCP_WND is smaller than the RX decrypion buffer, connection RX might stall!\n"));
782789
}
@@ -900,7 +907,7 @@ err_t altcp_tls_config_server_add_privkey_cert(struct altcp_tls_config *config,
900907
return ERR_VAL;
901908
}
902909

903-
ret = mbedtls_pk_parse_key(pkey, (const unsigned char *) privkey, privkey_len, privkey_pass, privkey_pass_len);
910+
ret = mbedtls_pk_parse_key(pkey, (const unsigned char *) privkey, privkey_len, privkey_pass, privkey_pass_len, mbedtls_ctr_drbg_random, &altcp_tls_entropy_rng->ctr_drbg);
904911
if (ret != 0) {
905912
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_pk_parse_public_key failed: %d\n", ret));
906913
mbedtls_x509_crt_free(srvcert);
@@ -1003,7 +1010,7 @@ altcp_tls_create_config_client_2wayauth(const u8_t *ca, size_t ca_len, const u8_
10031010
}
10041011

10051012
mbedtls_pk_init(conf->pkey);
1006-
ret = mbedtls_pk_parse_key(conf->pkey, privkey, privkey_len, privkey_pass, privkey_pass_len);
1013+
ret = mbedtls_pk_parse_key(conf->pkey, privkey, privkey_len, privkey_pass, privkey_pass_len, mbedtls_ctr_drbg_random, &altcp_tls_entropy_rng->ctr_drbg);
10071014
if (ret != 0) {
10081015
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_pk_parse_key failed: %d 0x%x\n", ret, -1*ret));
10091016
altcp_tls_free_config(conf);
@@ -1189,7 +1196,7 @@ altcp_mbedtls_sndbuf(struct altcp_pcb *conn)
11891196
size_t ret;
11901197
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
11911198
/* @todo: adjust ssl_added to real value related to negotiated cipher */
1192-
size_t max_frag_len = mbedtls_ssl_get_max_frag_len(&state->ssl_context);
1199+
size_t max_frag_len = mbedtls_ssl_get_max_in_record_payload(&state->ssl_context);
11931200
max_len = LWIP_MIN(max_frag_len, max_len);
11941201
#endif
11951202
/* Adjust sndbuf of inner_conn with what added by SSL */
@@ -1232,9 +1239,9 @@ altcp_mbedtls_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t
12321239
/* HACK: if there is something left to send, try to flush it and only
12331240
allow sending more if this succeeded (this is a hack because neither
12341241
returning 0 nor MBEDTLS_ERR_SSL_WANT_WRITE worked for me) */
1235-
if (state->ssl_context.out_left) {
1236-
mbedtls_ssl_flush_output(&state->ssl_context);
1237-
if (state->ssl_context.out_left) {
1242+
if (state->ssl_context.MBEDTLS_PRIVATE(out_left)) {
1243+
altcp_mbedtls_flush_output(state);
1244+
if (state->ssl_context.MBEDTLS_PRIVATE(out_left)) {
12381245
return ERR_MEM;
12391246
}
12401247
}
@@ -1284,6 +1291,8 @@ altcp_mbedtls_bio_send(void *ctx, const unsigned char *dataptr, size_t size)
12841291
while (size_left) {
12851292
u16_t write_len = (u16_t)LWIP_MIN(size_left, 0xFFFF);
12861293
err_t err = altcp_write(conn->inner_conn, (const void *)dataptr, write_len, apiflags);
1294+
/* try to send data... */
1295+
altcp_output(conn->inner_conn);
12871296
if (err == ERR_OK) {
12881297
written += write_len;
12891298
size_left -= write_len;

lib/lwip/lwip/src/core/tcp_out.c

+1-9
Original file line numberDiff line numberDiff line change
@@ -1255,14 +1255,6 @@ tcp_output(struct tcp_pcb *pcb)
12551255
LWIP_ASSERT("don't call tcp_output for listen-pcbs",
12561256
pcb->state != LISTEN);
12571257

1258-
/* First, check if we are invoked by the TCP input processing
1259-
code. If so, we do not output anything. Instead, we rely on the
1260-
input processing code to call us when input processing is done
1261-
with. */
1262-
if (tcp_input_pcb == pcb) {
1263-
return ERR_OK;
1264-
}
1265-
12661258
wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
12671259

12681260
seg = pcb->unsent;
@@ -2036,7 +2028,7 @@ tcp_rst(const struct tcp_pcb *pcb, u32_t seqno, u32_t ackno,
20362028
u16_t local_port, u16_t remote_port)
20372029
{
20382030
struct pbuf *p;
2039-
2031+
20402032
p = tcp_rst_common(pcb, seqno, ackno, local_ip, remote_ip, local_port, remote_port);
20412033
if (p != NULL) {
20422034
tcp_output_control_segment(pcb, p, local_ip, remote_ip);

lib/lwip/u-boot/lwipopts.h

+6
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,10 @@
154154
#define MEMP_MEM_INIT 1
155155
#define MEM_LIBC_MALLOC 1
156156

157+
#if defined(CONFIG_MBEDTLS_LIB_TLS)
158+
#define LWIP_ALTCP 1
159+
#define LWIP_ALTCP_TLS 1
160+
#define LWIP_ALTCP_TLS_MBEDTLS 1
161+
#endif
162+
157163
#endif /* LWIP_UBOOT_LWIPOPTS_H */

0 commit comments

Comments
 (0)