2022-03-15 13:29:32 +01:00
|
|
|
/*
|
|
|
|
* X.509 base functions for creating certificates / CSRs
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2024-02-08 17:41:31 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2022-03-15 13:29:32 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CREATE_C)
|
|
|
|
|
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/asn1write.h"
|
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/* Structure linking OIDs for X.509 DN AttributeTypes to their
|
|
|
|
* string representations and default string encodings used by Mbed TLS. */
|
|
|
|
typedef struct {
|
2023-08-30 11:18:00 +02:00
|
|
|
const char *name; /* String representation of AttributeType, e.g.
|
|
|
|
* "CN" or "emailAddress". */
|
|
|
|
size_t name_len; /* Length of 'name', without trailing 0 byte. */
|
|
|
|
const char *oid; /* String representation of OID of AttributeType,
|
2022-03-15 13:29:32 +01:00
|
|
|
* as per RFC 5280, Appendix A.1. */
|
2023-08-30 11:18:00 +02:00
|
|
|
int default_tag; /* The default character encoding used for the
|
2022-03-15 13:29:32 +01:00
|
|
|
* given attribute type, e.g.
|
|
|
|
* MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
|
|
|
|
} x509_attr_descriptor_t;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
#define ADD_STRLEN(s) s, sizeof(s) - 1
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
/* X.509 DN attributes from RFC 5280, Appendix A.1. */
|
|
|
|
static const x509_attr_descriptor_t x509_attrs[] =
|
|
|
|
{
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("CN"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("commonName"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("C"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("countryName"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("O"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("organizationName"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("L"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("locality"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("R"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("OU"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("organizationalUnitName"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("ST"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("stateOrProvinceName"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("emailAddress"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("serialNumber"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("postalAddress"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("postalCode"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("dnQualifier"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("title"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("surName"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("SN"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("givenName"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("GN"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("initials"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("pseudonym"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("generationQualifier"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("domainComponent"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
|
2023-08-30 11:18:00 +02:00
|
|
|
{ ADD_STRLEN("DC"),
|
2022-03-15 13:29:32 +01:00
|
|
|
MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
|
|
|
|
{ NULL, 0, NULL, MBEDTLS_ASN1_NULL }
|
|
|
|
};
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name, size_t name_len)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
|
|
|
const x509_attr_descriptor_t *cur;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
for (cur = x509_attrs; cur->name != NULL; cur++) {
|
|
|
|
if (cur->name_len == name_len &&
|
|
|
|
strncmp(cur->name, name, name_len) == 0) {
|
2022-03-15 13:29:32 +01:00
|
|
|
break;
|
2023-08-30 11:18:00 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (cur->name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return cur;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
2023-08-30 11:18:00 +02:00
|
|
|
int ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
2022-03-15 13:29:32 +01:00
|
|
|
const char *s = name, *c = s;
|
2023-08-30 11:18:00 +02:00
|
|
|
const char *end = s + strlen(s);
|
2022-03-15 13:29:32 +01:00
|
|
|
const char *oid = NULL;
|
2023-08-30 11:18:00 +02:00
|
|
|
const x509_attr_descriptor_t *attr_descr = NULL;
|
2022-03-15 13:29:32 +01:00
|
|
|
int in_tag = 1;
|
|
|
|
char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
|
|
|
char *d = data;
|
|
|
|
|
|
|
|
/* Clear existing chain if present */
|
2023-08-30 11:18:00 +02:00
|
|
|
mbedtls_asn1_free_named_data_list(head);
|
|
|
|
|
|
|
|
while (c <= end) {
|
|
|
|
if (in_tag && *c == '=') {
|
|
|
|
if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) {
|
2022-03-15 13:29:32 +01:00
|
|
|
ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
oid = attr_descr->oid;
|
|
|
|
s = c + 1;
|
|
|
|
in_tag = 0;
|
|
|
|
d = data;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (!in_tag && *c == '\\' && c != end) {
|
2022-03-15 13:29:32 +01:00
|
|
|
c++;
|
|
|
|
|
|
|
|
/* Check for valid escaped characters */
|
2023-08-30 11:18:00 +02:00
|
|
|
if (c == end || *c != ',') {
|
2022-03-15 13:29:32 +01:00
|
|
|
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
goto exit;
|
|
|
|
}
|
2023-08-30 11:18:00 +02:00
|
|
|
} else if (!in_tag && (*c == ',' || c == end)) {
|
|
|
|
mbedtls_asn1_named_data *cur =
|
|
|
|
mbedtls_asn1_store_named_data(head, oid, strlen(oid),
|
|
|
|
(unsigned char *) data,
|
|
|
|
d - data);
|
|
|
|
|
|
|
|
if (cur == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// set tagType
|
|
|
|
cur->val.tag = attr_descr->default_tag;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
while (c < end && *(c + 1) == ' ') {
|
2022-03-15 13:29:32 +01:00
|
|
|
c++;
|
2023-08-30 11:18:00 +02:00
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
s = c + 1;
|
|
|
|
in_tag = 1;
|
2023-08-30 11:18:00 +02:00
|
|
|
|
|
|
|
/* Successfully parsed one name, update ret to success */
|
|
|
|
ret = 0;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (!in_tag && s != c + 1) {
|
2022-03-15 13:29:32 +01:00
|
|
|
*(d++) = *c;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
2022-03-15 13:29:32 +01:00
|
|
|
ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return ret;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
|
|
|
|
* to store the critical boolean for us
|
|
|
|
*/
|
2023-08-30 11:18:00 +02:00
|
|
|
int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
|
|
|
|
int critical, const unsigned char *val, size_t val_len)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
|
|
|
mbedtls_asn1_named_data *cur;
|
|
|
|
|
2024-02-08 17:41:31 +01:00
|
|
|
if (val_len > (SIZE_MAX - 1)) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
|
|
|
|
NULL, val_len + 1)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cur->val.p[0] = critical;
|
2023-08-30 11:18:00 +02:00
|
|
|
memcpy(cur->val.p + 1, val, val_len);
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return 0;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
2023-08-30 11:18:00 +02:00
|
|
|
static int x509_write_name(unsigned char **p,
|
|
|
|
unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *cur_name)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t len = 0;
|
2023-08-30 11:18:00 +02:00
|
|
|
const char *oid = (const char *) cur_name->oid.p;
|
2022-03-15 13:29:32 +01:00
|
|
|
size_t oid_len = cur_name->oid.len;
|
|
|
|
const unsigned char *name = cur_name->val.p;
|
|
|
|
size_t name_len = cur_name->val.len;
|
|
|
|
|
|
|
|
// Write correct string tag and value
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
|
|
|
|
cur_name->val.tag,
|
|
|
|
(const char *) name,
|
|
|
|
name_len));
|
2022-03-15 13:29:32 +01:00
|
|
|
// Write OID
|
|
|
|
//
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
|
|
|
|
oid_len));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SET));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return (int) len;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *first)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t len = 0;
|
|
|
|
mbedtls_asn1_named_data *cur = first;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
while (cur != NULL) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
|
2022-03-15 13:29:32 +01:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return (int) len;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
|
|
|
|
const char *oid, size_t oid_len,
|
2023-10-21 14:06:23 +02:00
|
|
|
unsigned char *sig, size_t size,
|
|
|
|
mbedtls_pk_type_t pk_alg)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-10-21 14:06:23 +02:00
|
|
|
int write_null_par;
|
2022-03-15 13:29:32 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (*p < start || (size_t) (*p - start) < size) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
len = size;
|
|
|
|
(*p) -= len;
|
2023-08-30 11:18:00 +02:00
|
|
|
memcpy(*p, sig, len);
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
*--(*p) = 0;
|
|
|
|
len += 1;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
|
|
|
// Write OID
|
|
|
|
//
|
2023-10-21 14:06:23 +02:00
|
|
|
if (pk_alg == MBEDTLS_PK_ECDSA) {
|
|
|
|
/*
|
|
|
|
* The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
|
|
|
|
* algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
|
|
|
|
* https://www.rfc-editor.org/rfc/rfc5758#section-3.
|
|
|
|
*/
|
|
|
|
write_null_par = 0;
|
|
|
|
} else {
|
|
|
|
write_null_par = 1;
|
|
|
|
}
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
|
|
|
|
0, write_null_par));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return (int) len;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
static int x509_write_extension(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *ext)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
|
|
|
|
ext->val.len - 1));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
if (ext->val.p[0] != 0) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
|
|
|
|
ext->oid.len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2022-03-15 13:29:32 +01:00
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return (int) len;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extension ::= SEQUENCE {
|
|
|
|
* extnID OBJECT IDENTIFIER,
|
|
|
|
* critical BOOLEAN DEFAULT FALSE,
|
|
|
|
* extnValue OCTET STRING
|
|
|
|
* -- contains the DER encoding of an ASN.1 value
|
|
|
|
* -- corresponding to the extension type identified
|
|
|
|
* -- by extnID
|
|
|
|
* }
|
|
|
|
*/
|
2023-08-30 11:18:00 +02:00
|
|
|
int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *first)
|
2022-03-15 13:29:32 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t len = 0;
|
|
|
|
mbedtls_asn1_named_data *cur_ext = first;
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
while (cur_ext != NULL) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
|
2022-03-15 13:29:32 +01:00
|
|
|
cur_ext = cur_ext->next;
|
|
|
|
}
|
|
|
|
|
2023-08-30 11:18:00 +02:00
|
|
|
return (int) len;
|
2022-03-15 13:29:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_X509_CREATE_C */
|