mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-08-01 10:06:53 +03:00
Merge pull request #7009 from mprse/csr_write_san
Added ability to include the SubjectAltName extension to a CSR - v.2
This commit is contained in:
@ -26,6 +26,7 @@
|
||||
|
||||
#if defined(MBEDTLS_X509_CSR_WRITE_C)
|
||||
|
||||
#include "mbedtls/x509.h"
|
||||
#include "mbedtls/x509_csr.h"
|
||||
#include "mbedtls/asn1write.h"
|
||||
#include "mbedtls/error.h"
|
||||
@ -85,6 +86,105 @@ int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
|
||||
critical, val, val_len);
|
||||
}
|
||||
|
||||
int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
|
||||
const mbedtls_x509_san_list *san_list)
|
||||
{
|
||||
int ret = 0;
|
||||
const mbedtls_x509_san_list *cur;
|
||||
unsigned char *buf;
|
||||
unsigned char *p;
|
||||
size_t len;
|
||||
size_t buflen = 0;
|
||||
|
||||
/* Determine the maximum size of the SubjectAltName list */
|
||||
for (cur = san_list; cur != NULL; cur = cur->next) {
|
||||
/* Calculate size of the required buffer */
|
||||
switch (cur->node.type) {
|
||||
case MBEDTLS_X509_SAN_DNS_NAME:
|
||||
case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
|
||||
case MBEDTLS_X509_SAN_IP_ADDRESS:
|
||||
/* length of value for each name entry,
|
||||
* maximum 4 bytes for the length field,
|
||||
* 1 byte for the tag/type.
|
||||
*/
|
||||
buflen += cur->node.san.unstructured_name.len + 4 + 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* Not supported - skip. */
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Add the extra length field and tag */
|
||||
buflen += 4 + 1;
|
||||
|
||||
/* Allocate buffer */
|
||||
buf = mbedtls_calloc(1, buflen);
|
||||
if (buf == NULL) {
|
||||
return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
|
||||
}
|
||||
|
||||
mbedtls_platform_zeroize(buf, buflen);
|
||||
p = buf + buflen;
|
||||
|
||||
/* Write ASN.1-based structure */
|
||||
cur = san_list;
|
||||
len = 0;
|
||||
while (cur != NULL) {
|
||||
switch (cur->node.type) {
|
||||
case MBEDTLS_X509_SAN_DNS_NAME:
|
||||
case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
|
||||
case MBEDTLS_X509_SAN_IP_ADDRESS:
|
||||
{
|
||||
const unsigned char *unstructured_name =
|
||||
(const unsigned char *) cur->node.san.unstructured_name.p;
|
||||
size_t unstructured_name_len = cur->node.san.unstructured_name.len;
|
||||
|
||||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
|
||||
mbedtls_asn1_write_raw_buffer(
|
||||
&p, buf,
|
||||
unstructured_name, unstructured_name_len));
|
||||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(
|
||||
&p, buf, unstructured_name_len));
|
||||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
|
||||
mbedtls_asn1_write_tag(
|
||||
&p, buf,
|
||||
MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
/* Skip unsupported names. */
|
||||
break;
|
||||
}
|
||||
cur = cur->next;
|
||||
}
|
||||
|
||||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
|
||||
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
|
||||
mbedtls_asn1_write_tag(&p, buf,
|
||||
MBEDTLS_ASN1_CONSTRUCTED |
|
||||
MBEDTLS_ASN1_SEQUENCE));
|
||||
|
||||
ret = mbedtls_x509write_csr_set_extension(
|
||||
ctx,
|
||||
MBEDTLS_OID_SUBJECT_ALT_NAME,
|
||||
MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
|
||||
0,
|
||||
buf + buflen - len,
|
||||
len);
|
||||
|
||||
/* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list
|
||||
* was incorrectly calculated and memory is corrupted. */
|
||||
if (p < buf) {
|
||||
ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
|
||||
}
|
||||
|
||||
cleanup:
|
||||
mbedtls_free(buf);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
|
||||
{
|
||||
unsigned char buf[4] = { 0 };
|
||||
|
Reference in New Issue
Block a user