Skip to content

Commit 5185214

Browse files
jetmttefke
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]> Signed-off-by: Ilias Apalodimas <[email protected]>
1 parent 3aa381f commit 5185214

File tree

2 files changed

+16
-23
lines changed

2 files changed

+16
-23
lines changed

components/network/lwip/src/apps/altcp_tls/altcp_tls_mbedtls.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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"
@@ -80,8 +79,6 @@
8079
#include "mbedtls/ssl_cache.h"
8180
#include "mbedtls/ssl_ticket.h"
8281

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

8784
#ifndef ALTCP_MBEDTLS_ENTROPY_PTR
@@ -133,11 +130,13 @@ static int altcp_mbedtls_bio_send(void *ctx, const unsigned char *dataptr, size_
133130

134131

135132
static void
136-
altcp_mbedtls_flush_output(altcp_mbedtls_state_t* state)
133+
altcp_mbedtls_flush_output(altcp_mbedtls_state_t *state)
137134
{
138-
int flushed = mbedtls_ssl_flush_output(&state->ssl_context);
139-
if (flushed) {
140-
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_ssl_flush_output failed: %d\n", flushed));
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+
}
141140
}
142141
}
143142

@@ -532,7 +531,7 @@ altcp_mbedtls_lower_sent(void *arg, struct altcp_pcb *inner_conn, u16_t len)
532531
LWIP_ASSERT("state", state != NULL);
533532
LWIP_ASSERT("pcb mismatch", conn->inner_conn == inner_conn);
534533
/* calculate TLS overhead part to not send it to application */
535-
overhead = state->overhead_bytes_adjust + state->ssl_context.out_left;
534+
overhead = state->overhead_bytes_adjust + state->ssl_context.MBEDTLS_PRIVATE(out_left);
536535
if ((unsigned)overhead > len) {
537536
overhead = len;
538537
}
@@ -692,7 +691,7 @@ altcp_tls_set_session(struct altcp_pcb *conn, struct altcp_tls_session *session)
692691
if (session && conn && conn->state) {
693692
altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state;
694693
int ret = -1;
695-
if (session->data.start)
694+
if (session->data.MBEDTLS_PRIVATE(start))
696695
ret = mbedtls_ssl_set_session(&state->ssl_context, &session->data);
697696
return ret < 0 ? ERR_VAL : ERR_OK;
698697
}
@@ -786,7 +785,7 @@ altcp_tls_create_config(int is_server, u8_t cert_count, u8_t pkey_count, int hav
786785
struct altcp_tls_config *conf;
787786
mbedtls_x509_crt *mem;
788787

789-
if (TCP_WND < MBEDTLS_SSL_MAX_CONTENT_LEN) {
788+
if (TCP_WND < MBEDTLS_SSL_IN_CONTENT_LEN || TCP_WND < MBEDTLS_SSL_OUT_CONTENT_LEN) {
790789
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG|LWIP_DBG_LEVEL_SERIOUS,
791790
("altcp_tls: TCP_WND is smaller than the RX decrypion buffer, connection RX might stall!\n"));
792791
}
@@ -910,7 +909,7 @@ err_t altcp_tls_config_server_add_privkey_cert(struct altcp_tls_config *config,
910909
return ERR_VAL;
911910
}
912911

913-
ret = mbedtls_pk_parse_key(pkey, (const unsigned char *) privkey, privkey_len, privkey_pass, privkey_pass_len);
912+
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);
914913
if (ret != 0) {
915914
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_pk_parse_public_key failed: %d\n", ret));
916915
mbedtls_x509_crt_free(srvcert);
@@ -1013,7 +1012,7 @@ altcp_tls_create_config_client_2wayauth(const u8_t *ca, size_t ca_len, const u8_
10131012
}
10141013

10151014
mbedtls_pk_init(conf->pkey);
1016-
ret = mbedtls_pk_parse_key(conf->pkey, privkey, privkey_len, privkey_pass, privkey_pass_len);
1015+
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);
10171016
if (ret != 0) {
10181017
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_pk_parse_key failed: %d 0x%x\n", ret, -1*ret));
10191018
altcp_tls_free_config(conf);
@@ -1199,7 +1198,7 @@ altcp_mbedtls_sndbuf(struct altcp_pcb *conn)
11991198
size_t ret;
12001199
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
12011200
/* @todo: adjust ssl_added to real value related to negotiated cipher */
1202-
size_t max_frag_len = mbedtls_ssl_get_max_frag_len(&state->ssl_context);
1201+
size_t max_frag_len = mbedtls_ssl_get_max_in_record_payload(&state->ssl_context);
12031202
max_len = LWIP_MIN(max_frag_len, max_len);
12041203
#endif
12051204
/* Adjust sndbuf of inner_conn with what added by SSL */
@@ -1242,9 +1241,9 @@ altcp_mbedtls_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t
12421241
/* HACK: if there is something left to send, try to flush it and only
12431242
allow sending more if this succeeded (this is a hack because neither
12441243
returning 0 nor MBEDTLS_ERR_SSL_WANT_WRITE worked for me) */
1245-
if (state->ssl_context.out_left) {
1244+
if (state->ssl_context.MBEDTLS_PRIVATE(out_left)) {
12461245
altcp_mbedtls_flush_output(state);
1247-
if (state->ssl_context.out_left) {
1246+
if (state->ssl_context.MBEDTLS_PRIVATE(out_left)) {
12481247
return ERR_MEM;
12491248
}
12501249
}
@@ -1294,6 +1293,8 @@ altcp_mbedtls_bio_send(void *ctx, const unsigned char *dataptr, size_t size)
12941293
while (size_left) {
12951294
u16_t write_len = (u16_t)LWIP_MIN(size_left, 0xFFFF);
12961295
err_t err = altcp_write(conn->inner_conn, (const void *)dataptr, write_len, apiflags);
1296+
/* try to send data... */
1297+
altcp_output(conn->inner_conn);
12971298
if (err == ERR_OK) {
12981299
written += write_len;
12991300
size_left -= write_len;

components/network/lwip/src/core/tcp_out.c

Lines changed: 0 additions & 8 deletions
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;

0 commit comments

Comments
 (0)