From 54d2eb96a11edf9db7cd005db63382ecebdeeba5 Mon Sep 17 00:00:00 2001 From: Josef Schlehofer Date: Sun, 4 Oct 2026 14:51:36 +0200 Subject: [PATCH] px5g-mbedtls: fix the subjectAltName extension The subjectAltName support added in commit efca89daef ("px5g-mbedtls: add subjectAltName and extendedKeyUsage to SSL certs") has these bugs: - Every IP entry is written with a length of 16 bytes. An IPv4 address ends up as a zero padded IPv6 address, so 192.168.1.1 becomes C0A8:101:: and clients reject the certificate for that address. - All IP entries share one buffer, so each of them carries the address given last. - An invalid IP address is not rejected but written as :: or as the address of an earlier IP entry. - The list is linked after every option instead of only after a new entry. If any other option follows the last subjectAltName, that entry points to itself. Adding the extension then loops until the size counter overflows, which never ends on 64-bit targets and drops the whole extension on 32-bit targets. - An unknown entry type only prints a warning. It stays in the list with type 0, which mbedtls cannot write, so the whole extension is dropped while px5g still exits with 0. - Without any subjectAltName, the extension is written with an empty list, which RFC 5280 does not allow. Chrome rejects such a certificate with ERR_CERT_INVALID and, unlike for other self-signed certificates, does not offer to proceed. Use the length returned by mbedtls_x509_crt_parse_cn_inet_pton(), allocate a buffer for each IP entry and link only new entries. Reject invalid IP addresses and unknown entry types with an error. Add the extension only if there are entries and fail with an error if mbedtls refuses it. Signed-off-by: Josef Schlehofer Link: https://github.com/openwrt/openwrt/pull/25604 Signed-off-by: Robert Marko --- package/utils/px5g-mbedtls/Makefile | 2 +- package/utils/px5g-mbedtls/px5g-mbedtls.c | 40 +++++++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/package/utils/px5g-mbedtls/Makefile b/package/utils/px5g-mbedtls/Makefile index 6addbbab122..4bed2bbf8a9 100644 --- a/package/utils/px5g-mbedtls/Makefile +++ b/package/utils/px5g-mbedtls/Makefile @@ -8,7 +8,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=px5g-mbedtls -PKG_RELEASE:=11 +PKG_RELEASE:=12 PKG_LICENSE:=LGPL-2.1 PKG_BUILD_FLAGS:=no-mips16 diff --git a/package/utils/px5g-mbedtls/px5g-mbedtls.c b/package/utils/px5g-mbedtls/px5g-mbedtls.c index b1c809f8346..dde3b320a4f 100644 --- a/package/utils/px5g-mbedtls/px5g-mbedtls.c +++ b/package/utils/px5g-mbedtls/px5g-mbedtls.c @@ -207,7 +207,8 @@ int selfsigned(char **arg) */ mbedtls_asn1_sequence *eku = NULL, *ext_key_usage = NULL; char *sanval, *santype; - uint8_t ipaddr[16] = { 0 }; + uint8_t *ipaddr; + size_t iplen; char *subject = ""; unsigned int ksize = 512; @@ -310,28 +311,35 @@ int selfsigned(char **arg) san_cur->node.san.unstructured_name.p = (unsigned char *) sanval; san_cur->node.san.unstructured_name.len = strlen(sanval); } else if (!strncmp(santype, "IP:", strlen("IP:"))) { + ipaddr = calloc(1, 16); + iplen = mbedtls_x509_crt_parse_cn_inet_pton(sanval, ipaddr); + if (!iplen) { + fprintf(stderr, "error: invalid IP address: %s\n", sanval); + return 1; + } san_cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS; - mbedtls_x509_crt_parse_cn_inet_pton(sanval, ipaddr); - san_cur->node.san.unstructured_name.p = (unsigned char *) ipaddr; - san_cur->node.san.unstructured_name.len = sizeof(ipaddr); + san_cur->node.san.unstructured_name.p = ipaddr; + san_cur->node.san.unstructured_name.len = iplen; } else if (!strncmp(santype, "URI:", strlen("URI:"))) { san_cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER; san_cur->node.san.unstructured_name.p = (unsigned char *) sanval; san_cur->node.san.unstructured_name.len = strlen(sanval); + } else { + fprintf(stderr, "error: invalid subjectAltName type: %s\n", santype); + return 1; } - else fprintf(stderr, "No match to subjectAltName content type.\n"); + + //append the new entry to our san_list linked list + if (san_prev == NULL) { + san_list = san_cur; + } else { + san_prev->next = san_cur; + } + san_prev = san_cur; arg++; } } arg++; - - //set the pointers in our san_list linked list - if (san_prev == NULL) { - san_list = san_cur; - } else { - san_prev->next = san_cur; - } - san_prev = san_cur; } gen_key(&key, rsa, ksize, exp, curve, pem); @@ -358,7 +366,11 @@ int selfsigned(char **arg) mbedtls_x509write_crt_set_basic_constraints(&cert, 0, -1); mbedtls_x509write_crt_set_subject_key_identifier(&cert); mbedtls_x509write_crt_set_authority_key_identifier(&cert); - mbedtls_x509write_crt_set_subject_alternative_name(&cert, san_list); + //an empty subjectAltName is not valid, add it only if names were given + if (san_list && mbedtls_x509write_crt_set_subject_alternative_name(&cert, san_list)) { + fprintf(stderr, "error: failed to add subjectAltName\n"); + return 1; + } mbedtls_x509write_crt_set_ext_key_usage(&cert, ext_key_usage); _urandom(NULL, (void *) buf, 8);