Skip to content

Commit b30787a

Browse files
committed
Merge patch series "Enable https for wget"
Ilias Apalodimas <ilias.apalodimas@linaro.org> says: Hi all, This is a respin of [1] adding https support to wget. In short patch#1 enables the crypto algorithms we need in mbedTLS patches#2, #3 enable anf fix the lwIP part we need patch#4 is adding https:// parsing support in our wget patch#5 is making https:// the default for QEMU lwip defconfig so people can easily test and finaly patch#6 updates our documentation [1] https://lore.kernel.org/u-boot/20241024112449.1362319-1-ilias.apalodimas@linaro.org/ Link: https://lore.kernel.org/r/20241110083017.367565-1-ilias.apalodimas@linaro.org
2 parents 385af1b + 99649c6 commit b30787a

13 files changed

Lines changed: 277 additions & 39 deletions

File tree

cmd/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,25 @@ config CMD_WGET
21242124
wget is a simple command to download kernel, or other files,
21252125
from a http server over TCP.
21262126

2127+
config WGET_HTTPS
2128+
bool "wget https"
2129+
depends on CMD_WGET
2130+
depends on PROT_TCP_LWIP
2131+
depends on MBEDTLS_LIB
2132+
select SHA256
2133+
select RSA
2134+
select ASYMMETRIC_KEY_TYPE
2135+
select ASYMMETRIC_PUBLIC_KEY_SUBTYPE
2136+
select X509_CERTIFICATE_PARSER
2137+
select PKCS7_MESSAGE_PARSER
2138+
select MBEDTLS_LIB_CRYPTO
2139+
select MBEDTLS_LIB_TLS
2140+
select RSA_VERIFY_WITH_PKEY
2141+
select X509_CERTIFICATE_PARSER
2142+
select PKCS7_MESSAGE_PARSER
2143+
help
2144+
Enable TLS over http for wget.
2145+
21272146
endif # if CMD_NET
21282147

21292148
config CMD_PXE

configs/qemu_arm64_lwip_defconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ CONFIG_NET_LWIP=y
77
CONFIG_CMD_DNS=y
88
CONFIG_CMD_WGET=y
99
CONFIG_EFI_HTTP_BOOT=y
10+
CONFIG_WGET_HTTPS=y

doc/develop/uefi/uefi.rst

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ UEFI variables. Booting according to these variables is possible via::
681681
As of U-Boot v2020.10 UEFI variables cannot be set at runtime. The U-Boot
682682
command 'efidebug' can be used to set the variables.
683683

684-
UEFI HTTP Boot
685-
~~~~~~~~~~~~~~
684+
UEFI HTTP Boot using the legacy TCP stack
685+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
686686

687687
HTTP Boot provides the capability for system deployment and configuration
688688
over the network. HTTP Boot can be activated by specifying::
@@ -715,6 +715,47 @@ We need to preset the "httpserverip" environment variable to proceed the wget::
715715

716716
setenv httpserverip 192.168.1.1
717717

718+
UEFI HTTP(s) Boot using lwIP
719+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
720+
Similar to the above U-Boot can do EFI HTTP boot using lwIP. If we combine this
721+
with Mbed TLS we can also download from https://
722+
723+
HTTP(s) Boot can be activated by specifying::
724+
725+
CONFIG_EFI_HTTP_BOOT
726+
CONFIG_NET_LWIP
727+
CONFIG_WGET_HTTPS
728+
729+
For QEMU targets there's a Kconfig that supports this by default::
730+
731+
make qemu_arm64_lwip_defconfig
732+
733+
The commands and functionality are similar to the legacy stack, with the notable
734+
exception of not having to define an "httpserverip" if you are trying to resolve
735+
an IP. However, lwIP code doesn't yet support redirects::
736+
737+
=> efidebug boot add -u 1 netinst https://cdimage.debian.org/cdimage/weekly-builds/arm64/iso-cd/debian-testing-arm64-netinst.iso
738+
=> dhcp
739+
DHCP client bound to address 10.0.2.15 (3 ms)
740+
=> efidebug boot order 1
741+
=> bootefi bootmgr
742+
743+
HTTP server error 302
744+
Loading Boot0001 'netinst' failed
745+
EFI boot manager: Cannot load any image
746+
747+
If the url you specified isn't a redirect::
748+
749+
=> efidebug boot add -u 1 netinst https://download.rockylinux.org/pub/rocky/9/isos/aarch64/Rocky-9.4-aarch64-minimal.iso
750+
=> dhcp
751+
=> bootefi bootmgr
752+
#######################################
753+
754+
If the downloaded file extension is .iso or .img file, efibootmgr tries to
755+
mount the image and boot with the default file(e.g. EFI/BOOT/BOOTAA64.EFI).
756+
If the downloaded file is PE-COFF image, load the downloaded file and
757+
start it.
758+
718759
Executing the built in hello world application
719760
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
720761

lib/lwip/Makefile

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

Lines changed: 31 additions & 18 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"
@@ -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
@@ -109,6 +106,7 @@ struct altcp_tls_config {
109106
u8_t pkey_count;
110107
u8_t pkey_max;
111108
mbedtls_x509_crt *ca;
109+
char host[256];
112110
#if defined(MBEDTLS_SSL_CACHE_C) && ALTCP_MBEDTLS_USE_SESSION_CACHE
113111
/** Inter-connection cache for fast connection startup */
114112
struct mbedtls_ssl_cache_context cache;
@@ -132,6 +130,16 @@ static err_t altcp_mbedtls_lower_recv_process(struct altcp_pcb *conn, altcp_mbed
132130
static err_t altcp_mbedtls_handle_rx_appldata(struct altcp_pcb *conn, altcp_mbedtls_state_t *state);
133131
static int altcp_mbedtls_bio_send(void *ctx, const unsigned char *dataptr, size_t size);
134132

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

136144
/* callback functions from inner/lower connection: */
137145

@@ -524,14 +532,14 @@ altcp_mbedtls_lower_sent(void *arg, struct altcp_pcb *inner_conn, u16_t len)
524532
LWIP_ASSERT("state", state != NULL);
525533
LWIP_ASSERT("pcb mismatch", conn->inner_conn == inner_conn);
526534
/* calculate TLS overhead part to not send it to application */
527-
overhead = state->overhead_bytes_adjust + state->ssl_context.out_left;
535+
overhead = state->overhead_bytes_adjust + state->ssl_context.MBEDTLS_PRIVATE(out_left);
528536
if ((unsigned)overhead > len) {
529537
overhead = len;
530538
}
531539
/* remove ACKed bytes from overhead adjust counter */
532540
state->overhead_bytes_adjust -= len;
533541
/* try to send more if we failed before (may increase overhead adjust counter) */
534-
mbedtls_ssl_flush_output(&state->ssl_context);
542+
altcp_mbedtls_flush_output(state);
535543
/* remove calculated overhead from ACKed bytes len */
536544
app_len = len - (u16_t)overhead;
537545
/* update application write counter and inform application */
@@ -559,7 +567,7 @@ altcp_mbedtls_lower_poll(void *arg, struct altcp_pcb *inner_conn)
559567
if (conn->state) {
560568
altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state;
561569
/* try to send more if we failed before */
562-
mbedtls_ssl_flush_output(&state->ssl_context);
570+
altcp_mbedtls_flush_output(state);
563571
if (altcp_mbedtls_handle_rx_appldata(conn, state) == ERR_ABRT) {
564572
return ERR_ABRT;
565573
}
@@ -635,6 +643,7 @@ altcp_mbedtls_setup(void *conf, struct altcp_pcb *conn, struct altcp_pcb *inner_
635643
/* tell mbedtls about our I/O functions */
636644
mbedtls_ssl_set_bio(&state->ssl_context, conn, altcp_mbedtls_bio_send, altcp_mbedtls_bio_recv, NULL);
637645

646+
mbedtls_ssl_set_hostname(&state->ssl_context, config->host);
638647
altcp_mbedtls_setup_callbacks(conn, inner_conn);
639648
conn->inner_conn = inner_conn;
640649
conn->fns = &altcp_mbedtls_functions;
@@ -683,7 +692,7 @@ altcp_tls_set_session(struct altcp_pcb *conn, struct altcp_tls_session *session)
683692
if (session && conn && conn->state) {
684693
altcp_mbedtls_state_t *state = (altcp_mbedtls_state_t *)conn->state;
685694
int ret = -1;
686-
if (session->data.start)
695+
if (session->data.MBEDTLS_PRIVATE(start))
687696
ret = mbedtls_ssl_set_session(&state->ssl_context, &session->data);
688697
return ret < 0 ? ERR_VAL : ERR_OK;
689698
}
@@ -776,7 +785,7 @@ altcp_tls_create_config(int is_server, u8_t cert_count, u8_t pkey_count, int hav
776785
struct altcp_tls_config *conf;
777786
mbedtls_x509_crt *mem;
778787

779-
if (TCP_WND < MBEDTLS_SSL_MAX_CONTENT_LEN) {
788+
if (TCP_WND < MBEDTLS_SSL_IN_CONTENT_LEN || TCP_WND < MBEDTLS_SSL_OUT_CONTENT_LEN) {
780789
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG|LWIP_DBG_LEVEL_SERIOUS,
781790
("altcp_tls: TCP_WND is smaller than the RX decrypion buffer, connection RX might stall!\n"));
782791
}
@@ -900,7 +909,7 @@ err_t altcp_tls_config_server_add_privkey_cert(struct altcp_tls_config *config,
900909
return ERR_VAL;
901910
}
902911

903-
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);
904913
if (ret != 0) {
905914
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_pk_parse_public_key failed: %d\n", ret));
906915
mbedtls_x509_crt_free(srvcert);
@@ -944,7 +953,7 @@ altcp_tls_create_config_server_privkey_cert(const u8_t *privkey, size_t privkey_
944953
}
945954

946955
static struct altcp_tls_config *
947-
altcp_tls_create_config_client_common(const u8_t *ca, size_t ca_len, int is_2wayauth)
956+
altcp_tls_create_config_client_common(const u8_t *ca, size_t ca_len, int is_2wayauth, char *host)
948957
{
949958
int ret;
950959
struct altcp_tls_config *conf = altcp_tls_create_config(0, (is_2wayauth) ? 1 : 0, (is_2wayauth) ? 1 : 0, ca != NULL);
@@ -966,13 +975,15 @@ altcp_tls_create_config_client_common(const u8_t *ca, size_t ca_len, int is_2way
966975

967976
mbedtls_ssl_conf_ca_chain(&conf->conf, conf->ca, NULL);
968977
}
978+
strlcpy(conf->host, host, sizeof(conf->host));
979+
969980
return conf;
970981
}
971982

972983
struct altcp_tls_config *
973-
altcp_tls_create_config_client(const u8_t *ca, size_t ca_len)
984+
altcp_tls_create_config_client(const u8_t *ca, size_t ca_len, char *host)
974985
{
975-
return altcp_tls_create_config_client_common(ca, ca_len, 0);
986+
return altcp_tls_create_config_client_common(ca, ca_len, 0, host);
976987
}
977988

978989
struct altcp_tls_config *
@@ -988,7 +999,7 @@ altcp_tls_create_config_client_2wayauth(const u8_t *ca, size_t ca_len, const u8_
988999
return NULL;
9891000
}
9901001

991-
conf = altcp_tls_create_config_client_common(ca, ca_len, 1);
1002+
conf = altcp_tls_create_config_client_common(ca, ca_len, 1, NULL);
9921003
if (conf == NULL) {
9931004
return NULL;
9941005
}
@@ -1003,7 +1014,7 @@ altcp_tls_create_config_client_2wayauth(const u8_t *ca, size_t ca_len, const u8_
10031014
}
10041015

10051016
mbedtls_pk_init(conf->pkey);
1006-
ret = mbedtls_pk_parse_key(conf->pkey, privkey, privkey_len, privkey_pass, privkey_pass_len);
1017+
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);
10071018
if (ret != 0) {
10081019
LWIP_DEBUGF(ALTCP_MBEDTLS_DEBUG, ("mbedtls_pk_parse_key failed: %d 0x%x\n", ret, -1*ret));
10091020
altcp_tls_free_config(conf);
@@ -1189,7 +1200,7 @@ altcp_mbedtls_sndbuf(struct altcp_pcb *conn)
11891200
size_t ret;
11901201
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
11911202
/* @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);
1203+
size_t max_frag_len = mbedtls_ssl_get_max_in_record_payload(&state->ssl_context);
11931204
max_len = LWIP_MIN(max_frag_len, max_len);
11941205
#endif
11951206
/* Adjust sndbuf of inner_conn with what added by SSL */
@@ -1232,9 +1243,9 @@ altcp_mbedtls_write(struct altcp_pcb *conn, const void *dataptr, u16_t len, u8_t
12321243
/* HACK: if there is something left to send, try to flush it and only
12331244
allow sending more if this succeeded (this is a hack because neither
12341245
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) {
1246+
if (state->ssl_context.MBEDTLS_PRIVATE(out_left)) {
1247+
altcp_mbedtls_flush_output(state);
1248+
if (state->ssl_context.MBEDTLS_PRIVATE(out_left)) {
12381249
return ERR_MEM;
12391250
}
12401251
}
@@ -1284,6 +1295,8 @@ altcp_mbedtls_bio_send(void *ctx, const unsigned char *dataptr, size_t size)
12841295
while (size_left) {
12851296
u16_t write_len = (u16_t)LWIP_MIN(size_left, 0xFFFF);
12861297
err_t err = altcp_write(conn->inner_conn, (const void *)dataptr, write_len, apiflags);
1298+
/* try to send data... */
1299+
altcp_output(conn->inner_conn);
12871300
if (err == ERR_OK) {
12881301
written += write_len;
12891302
size_left -= write_len;

lib/lwip/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;

lib/lwip/lwip/src/include/lwip/altcp_tls.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ struct altcp_tls_config *altcp_tls_create_config_server_privkey_cert(const u8_t
9292
/** @ingroup altcp_tls
9393
* Create an ALTCP_TLS client configuration handle
9494
*/
95-
struct altcp_tls_config *altcp_tls_create_config_client(const u8_t *cert, size_t cert_len);
95+
struct altcp_tls_config *altcp_tls_create_config_client(const u8_t *cert, size_t cert_len, char *host);
9696

9797
/** @ingroup altcp_tls
9898
* Create an ALTCP_TLS client configuration handle with two-way server/client authentication

lib/lwip/u-boot/lwipopts.h

Lines changed: 6 additions & 0 deletions
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 */

lib/mbedtls/Kconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,16 @@ endif # SPL
430430

431431
endif # MBEDTLS_LIB_X509
432432

433+
config MBEDTLS_LIB_TLS
434+
bool "MbedTLS TLS library"
435+
depends on RSA_PUBLIC_KEY_PARSER_MBEDTLS
436+
depends on X509_CERTIFICATE_PARSER_MBEDTLS
437+
depends on ASYMMETRIC_PUBLIC_KEY_MBEDTLS
438+
depends on ASN1_DECODER_MBEDTLS
439+
depends on ASYMMETRIC_PUBLIC_KEY_MBEDTLS
440+
depends on MBEDTLS_LIB_CRYPTO
441+
help
442+
Enable MbedTLS TLS library. Required for HTTPs support
443+
in wget
444+
433445
endif # MBEDTLS_LIB

lib/mbedtls/Makefile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mbedtls_lib_crypto-y := \
2626
$(MBEDTLS_LIB_DIR)/platform_util.o \
2727
$(MBEDTLS_LIB_DIR)/constant_time.o \
2828
$(MBEDTLS_LIB_DIR)/md.o
29+
2930
mbedtls_lib_crypto-$(CONFIG_$(SPL_)MD5_MBEDTLS) += $(MBEDTLS_LIB_DIR)/md5.o
3031
mbedtls_lib_crypto-$(CONFIG_$(SPL_)SHA1_MBEDTLS) += $(MBEDTLS_LIB_DIR)/sha1.o
3132
mbedtls_lib_crypto-$(CONFIG_$(SPL_)SHA256_MBEDTLS) += \
@@ -54,3 +55,33 @@ mbedtls_lib_x509-$(CONFIG_$(SPL_)X509_CERTIFICATE_PARSER_MBEDTLS) += \
5455
$(MBEDTLS_LIB_DIR)/x509_crt.o
5556
mbedtls_lib_x509-$(CONFIG_$(SPL_)PKCS7_MESSAGE_PARSER_MBEDTLS) += \
5657
$(MBEDTLS_LIB_DIR)/pkcs7.o
58+
59+
#mbedTLS TLS support
60+
obj-$(CONFIG_MBEDTLS_LIB_TLS) += mbedtls_lib_tls.o
61+
mbedtls_lib_tls-y := \
62+
$(MBEDTLS_LIB_DIR)/mps_reader.o \
63+
$(MBEDTLS_LIB_DIR)/mps_trace.o \
64+
$(MBEDTLS_LIB_DIR)/net_sockets.o \
65+
$(MBEDTLS_LIB_DIR)/pk_ecc.o \
66+
$(MBEDTLS_LIB_DIR)/ssl_cache.o \
67+
$(MBEDTLS_LIB_DIR)/ssl_ciphersuites.o \
68+
$(MBEDTLS_LIB_DIR)/ssl_client.o \
69+
$(MBEDTLS_LIB_DIR)/ssl_cookie.o \
70+
$(MBEDTLS_LIB_DIR)/ssl_debug_helpers_generated.o \
71+
$(MBEDTLS_LIB_DIR)/ssl_msg.o \
72+
$(MBEDTLS_LIB_DIR)/ssl_ticket.o \
73+
$(MBEDTLS_LIB_DIR)/ssl_tls.o \
74+
$(MBEDTLS_LIB_DIR)/ssl_tls12_client.o \
75+
$(MBEDTLS_LIB_DIR)/hmac_drbg.o \
76+
$(MBEDTLS_LIB_DIR)/ctr_drbg.o \
77+
$(MBEDTLS_LIB_DIR)/entropy.o \
78+
$(MBEDTLS_LIB_DIR)/entropy_poll.o \
79+
$(MBEDTLS_LIB_DIR)/aes.o \
80+
$(MBEDTLS_LIB_DIR)/cipher.o \
81+
$(MBEDTLS_LIB_DIR)/cipher_wrap.o \
82+
$(MBEDTLS_LIB_DIR)/ecdh.o \
83+
$(MBEDTLS_LIB_DIR)/ecdsa.o \
84+
$(MBEDTLS_LIB_DIR)/ecp.o \
85+
$(MBEDTLS_LIB_DIR)/ecp_curves.o \
86+
$(MBEDTLS_LIB_DIR)/ecp_curves_new.o \
87+
$(MBEDTLS_LIB_DIR)/gcm.o \

0 commit comments

Comments
 (0)