Merge 2dd7e50b101b663271ef16ae1a04d804b649d546 into d8d5ea7cbc42b7800a3b030fb4341810074e7cb0

This commit is contained in:
Jeremy Levartovsky 2026-10-27 20:00:51 +00:00 committed by GitHub
commit 7487d7639d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 10 deletions

View File

@ -0,0 +1,6 @@
Bugfix
* Fix a stack buffer overflow in the cert_write and cert_req sample
programs when an IPv6 address is given in the san parameter. IPv6
addresses are now written correctly.
* Fix the cert_write and cert_req sample programs writing the last IP
address for every IP entry when the san parameter has more than one.

View File

@ -66,7 +66,7 @@ int main(void)
" DNS:value\n" \ " DNS:value\n" \
" URI:value\n" \ " URI:value\n" \
" RFC822:value\n" \ " RFC822:value\n" \
" IP:value (Only IPv4 is supported)\n" \ " IP:value (IPv4 or IPv6)\n" \
" DN:list of comma separated key=value pairs\n" \ " DN:list of comma separated key=value pairs\n" \
" key_usage=%%s default: (empty)\n" \ " key_usage=%%s default: (empty)\n" \
" Comma-separated-list of values:\n" \ " Comma-separated-list of values:\n" \
@ -152,9 +152,6 @@ int main(int argc, char *argv[])
char *p, *q, *r; char *p, *q, *r;
mbedtls_x509write_csr req; mbedtls_x509write_csr req;
mbedtls_x509_san_list *cur, *prev; mbedtls_x509_san_list *cur, *prev;
#if defined(MBEDTLS_X509_CRT_PARSE_C)
uint8_t ip[4] = { 0 };
#endif
/* /*
* Set to sane values * Set to sane values
*/ */
@ -260,15 +257,24 @@ usage:
cur->node.type = MBEDTLS_X509_SAN_DNS_NAME; cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
} else if (strcmp(q, "IP") == 0) { } else if (strcmp(q, "IP") == 0) {
size_t ip_addr_len = 0; size_t ip_addr_len = 0;
/* Each entry needs its own buffer, because the list is
* only written once all entries have been parsed.
* mbedtls_x509_crt_parse_cn_inet_pton() needs 16 bytes. */
unsigned char *ip = mbedtls_calloc(1, 16);
if (ip == NULL) {
mbedtls_printf("Not enough memory for subjectAltName list\n");
goto exit;
}
cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS; cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip); ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
if (ip_addr_len == 0) { if (ip_addr_len == 0) {
mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n", mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
subtype_value); subtype_value);
mbedtls_free(ip);
goto exit; goto exit;
} }
cur->node.san.unstructured_name.p = (unsigned char *) ip; cur->node.san.unstructured_name.p = ip;
cur->node.san.unstructured_name.len = sizeof(ip); cur->node.san.unstructured_name.len = ip_addr_len;
} else if (strcmp(q, "DN") == 0) { } else if (strcmp(q, "DN") == 0) {
cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME; cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
/* Work around an API mismatch between string_to_names() and /* Work around an API mismatch between string_to_names() and
@ -565,6 +571,9 @@ exit:
mbedtls_free(dn->val.p); mbedtls_free(dn->val.p);
mbedtls_asn1_free_named_data_list(&dn->next); mbedtls_asn1_free_named_data_list(&dn->next);
} }
if (cur->node.type == MBEDTLS_X509_SAN_IP_ADDRESS) {
mbedtls_free(cur->node.san.unstructured_name.p);
}
mbedtls_free(cur); mbedtls_free(cur);
cur = next; cur = next;
} }

View File

@ -135,7 +135,7 @@ int main(void)
" DNS:value\n" \ " DNS:value\n" \
" URI:value\n" \ " URI:value\n" \
" RFC822:value\n" \ " RFC822:value\n" \
" IP:value (Only IPv4 is supported)\n" \ " IP:value (IPv4 or IPv6)\n" \
" DN:list of comma separated key=value pairs\n" \ " DN:list of comma separated key=value pairs\n" \
" authority_identifier=%%s default: 1\n" \ " authority_identifier=%%s default: 1\n" \
" Possible values: 0, 1\n" \ " Possible values: 0, 1\n" \
@ -316,7 +316,6 @@ int main(int argc, char *argv[])
size_t serial_len; size_t serial_len;
mbedtls_asn1_sequence *ext_key_usage; mbedtls_asn1_sequence *ext_key_usage;
mbedtls_x509_san_list *cur, *prev; mbedtls_x509_san_list *cur, *prev;
uint8_t ip[4] = { 0 };
/* /*
* Set to sane values * Set to sane values
*/ */
@ -642,15 +641,24 @@ usage:
cur->node.type = MBEDTLS_X509_SAN_DNS_NAME; cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
} else if (strcmp(q, "IP") == 0) { } else if (strcmp(q, "IP") == 0) {
size_t ip_addr_len = 0; size_t ip_addr_len = 0;
/* Each entry needs its own buffer, because the list is
* only written once all entries have been parsed.
* mbedtls_x509_crt_parse_cn_inet_pton() needs 16 bytes. */
unsigned char *ip = mbedtls_calloc(1, 16);
if (ip == NULL) {
mbedtls_printf("Not enough memory for subjectAltName list\n");
goto exit;
}
cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS; cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip); ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
if (ip_addr_len == 0) { if (ip_addr_len == 0) {
mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n", mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
subtype_value); subtype_value);
mbedtls_free(ip);
goto exit; goto exit;
} }
cur->node.san.unstructured_name.p = (unsigned char *) ip; cur->node.san.unstructured_name.p = ip;
cur->node.san.unstructured_name.len = sizeof(ip); cur->node.san.unstructured_name.len = ip_addr_len;
} else if (strcmp(q, "DN") == 0) { } else if (strcmp(q, "DN") == 0) {
cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME; cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
/* Work around an API mismatch between string_to_names() and /* Work around an API mismatch between string_to_names() and
@ -1065,6 +1073,9 @@ exit:
mbedtls_free(dn->val.p); mbedtls_free(dn->val.p);
mbedtls_asn1_free_named_data_list(&dn->next); mbedtls_asn1_free_named_data_list(&dn->next);
} }
if (cur->node.type == MBEDTLS_X509_SAN_IP_ADDRESS) {
mbedtls_free(cur->node.san.unstructured_name.p);
}
mbedtls_free(cur); mbedtls_free(cur);
cur = next; cur = next;
} }