2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* X.509 base functions for creating certificates / CSRs
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CREATE_C)
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/asn1write.h"
|
2019-12-18 16:07:04 +01:00
|
|
|
#include "mbedtls/error.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/oid.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2023-07-24 18:21:14 +02:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2023-07-27 15:34:11 +02:00
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
|
2018-10-08 15:32:55 +02:00
|
|
|
/* Structure linking OIDs for X.509 DN AttributeTypes to their
|
|
|
|
* string representations and default string encodings used by Mbed TLS. */
|
2014-06-12 11:06:36 +02:00
|
|
|
typedef struct {
|
2023-01-11 14:50:10 +01: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,
|
2023-07-24 18:21:14 +02:00
|
|
|
* as per RFC 5280, Appendix A.1. encoded as per
|
|
|
|
* X.690 */
|
2023-01-11 14:50:10 +01:00
|
|
|
int default_tag; /* The default character encoding used for the
|
2018-10-08 15:32:55 +02:00
|
|
|
* given attribute type, e.g.
|
2018-10-24 13:33:07 +02:00
|
|
|
* MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
|
2014-06-12 11:06:36 +02:00
|
|
|
} x509_attr_descriptor_t;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
#define ADD_STRLEN(s) s, sizeof(s) - 1
|
2014-06-12 11:06:36 +02:00
|
|
|
|
2018-10-08 15:47:38 +02:00
|
|
|
/* X.509 DN attributes from RFC 5280, Appendix A.1. */
|
2014-06-12 11:06:36 +02:00
|
|
|
static const x509_attr_descriptor_t x509_attrs[] =
|
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("CN"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("commonName"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("C"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("countryName"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("O"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("organizationName"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("L"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("locality"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("R"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("OU"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("organizationalUnitName"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("ST"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("stateOrProvinceName"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("emailAddress"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("serialNumber"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("postalAddress"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("postalCode"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("dnQualifier"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("title"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("surName"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("SN"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("givenName"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("GN"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("initials"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("pseudonym"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("generationQualifier"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("domainComponent"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
|
2023-01-11 14:50:10 +01:00
|
|
|
{ ADD_STRLEN("DC"),
|
2018-10-08 15:52:20 +02:00
|
|
|
MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
|
2018-05-18 12:12:45 +02:00
|
|
|
{ NULL, 0, NULL, MBEDTLS_ASN1_NULL }
|
2014-06-12 11:06:36 +02:00
|
|
|
};
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name, size_t name_len)
|
2014-06-12 11:06:36 +02:00
|
|
|
{
|
|
|
|
const x509_attr_descriptor_t *cur;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (cur = x509_attrs; cur->name != NULL; cur++) {
|
|
|
|
if (cur->name_len == name_len &&
|
|
|
|
strncmp(cur->name, name, name_len) == 0) {
|
2014-06-12 11:06:36 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-12 11:06:36 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cur->name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-08 15:32:55 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return cur;
|
2014-06-12 11:06:36 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 18:21:14 +02:00
|
|
|
static int hex_to_int(char c)
|
2023-07-21 16:47:47 +02:00
|
|
|
{
|
2023-07-24 18:21:14 +02:00
|
|
|
return ('0' <= c && c <= '9') ? (c - '0') :
|
|
|
|
('a' <= c && c <= 'f') ? (c - 'a' + 10) :
|
|
|
|
('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
|
|
|
|
}
|
|
|
|
|
2023-08-30 17:04:16 +02:00
|
|
|
static int hexpair_to_int(const char *hexpair)
|
2023-07-24 18:21:14 +02:00
|
|
|
{
|
2023-08-30 17:04:16 +02:00
|
|
|
int n1 = hex_to_int(*hexpair);
|
|
|
|
int n2 = hex_to_int(*(hexpair + 1));
|
2023-08-30 17:12:57 +02:00
|
|
|
|
2023-07-24 18:21:14 +02:00
|
|
|
if (n1 != -1 && n2 != -1) {
|
|
|
|
return (n1 << 4) | n2;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 12:10:52 +02:00
|
|
|
static int parse_attribute_value_string(const char *s,
|
|
|
|
int len,
|
|
|
|
unsigned char *data,
|
|
|
|
size_t *data_len)
|
2023-07-26 12:55:31 +02:00
|
|
|
{
|
2023-08-30 17:12:57 +02:00
|
|
|
const char *c;
|
|
|
|
const char *end = s + len;
|
2023-07-27 15:34:11 +02:00
|
|
|
unsigned char *d = data;
|
2023-07-24 18:21:14 +02:00
|
|
|
int n;
|
2023-08-30 17:24:31 +02:00
|
|
|
|
2023-08-30 17:12:57 +02:00
|
|
|
for (c = s; c < end; c++) {
|
2023-07-24 18:21:14 +02:00
|
|
|
if (*c == '\\') {
|
|
|
|
c++;
|
|
|
|
|
2023-08-30 16:50:12 +02:00
|
|
|
/* Check for valid escaped characters as per RFC 4514 Section 3 */
|
2023-08-30 17:04:16 +02:00
|
|
|
if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
|
2023-08-14 17:31:11 +02:00
|
|
|
if (n == 0) {
|
2023-08-14 16:38:39 +02:00
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
}
|
2023-07-24 18:21:14 +02:00
|
|
|
*(d++) = n;
|
|
|
|
c++;
|
2023-09-08 12:32:19 +02:00
|
|
|
} else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
|
|
|
|
*(d++) = *c;
|
|
|
|
} else {
|
2023-07-24 18:21:14 +02:00
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
}
|
2023-09-08 13:04:41 +02:00
|
|
|
} else {
|
2023-09-08 12:32:19 +02:00
|
|
|
*(d++) = *c;
|
|
|
|
}
|
2023-08-30 17:24:31 +02:00
|
|
|
|
2023-07-24 18:21:14 +02:00
|
|
|
if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*data_len = d - data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-09-21 14:03:52 +02:00
|
|
|
/** Parse a hexstring containing a DER-encoded string.
|
|
|
|
*
|
|
|
|
* \param s A string of \p len bytes hexadecimal digits.
|
|
|
|
* \param len Number of bytes to read from \p s.
|
2023-09-21 18:13:17 +02:00
|
|
|
* \param data Output buffer of size \p data_size.
|
2023-09-21 14:03:52 +02:00
|
|
|
* On success, it contains the payload that's DER-encoded
|
|
|
|
* in the input (content without the tag and length).
|
|
|
|
* If the DER tag is a string tag, the payload is guaranteed
|
|
|
|
* not to contain null bytes.
|
2023-09-21 18:13:17 +02:00
|
|
|
* \param data_size Length of the \p data buffer.
|
2023-09-21 14:03:52 +02:00
|
|
|
* \param data_len On success, the length of the parsed string.
|
|
|
|
* It is guaranteed to be less than
|
|
|
|
* #MBEDTLS_X509_MAX_DN_NAME_SIZE.
|
|
|
|
* \param tag The ASN.1 tag that the payload in \p data is encoded in.
|
|
|
|
*
|
|
|
|
* \retval 0 on success.
|
|
|
|
* \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
|
|
|
|
* a valid hexstring,
|
|
|
|
* or if the decoded hexstring is not valid DER,
|
2023-09-21 18:13:17 +02:00
|
|
|
* or if the payload does not fit in \p data,
|
|
|
|
* or if the payload is more than
|
|
|
|
* #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes,
|
2023-09-21 14:03:52 +02:00
|
|
|
* of if \p *tag is an ASN.1 string tag and the payload
|
|
|
|
* contains a null byte.
|
2023-09-21 18:13:17 +02:00
|
|
|
* \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory.
|
2023-09-21 14:03:52 +02:00
|
|
|
*/
|
|
|
|
static int parse_attribute_value_hex_der_encoded(const char *s,
|
2023-09-21 16:50:40 +02:00
|
|
|
size_t len,
|
2023-09-21 14:03:52 +02:00
|
|
|
unsigned char *data,
|
2023-09-21 18:13:17 +02:00
|
|
|
size_t data_size,
|
2023-09-21 14:03:52 +02:00
|
|
|
size_t *data_len,
|
|
|
|
int *tag)
|
2023-07-26 12:55:31 +02:00
|
|
|
{
|
2023-09-21 18:13:17 +02:00
|
|
|
/* Step 1: preliminary length checks. */
|
2023-09-21 14:03:52 +02:00
|
|
|
/* Each byte is encoded by exactly two hexadecimal digits. */
|
|
|
|
if (len % 2 != 0) {
|
|
|
|
/* Odd number of hex digits */
|
2023-08-01 12:10:52 +02:00
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
}
|
2023-09-21 14:03:52 +02:00
|
|
|
size_t const der_length = len / 2;
|
2023-09-21 18:13:17 +02:00
|
|
|
if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) {
|
|
|
|
/* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE
|
|
|
|
* (after subtracting the ASN.1 tag and length). Reject this early
|
|
|
|
* to avoid allocating a large intermediate buffer. */
|
2023-08-01 12:10:52 +02:00
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
}
|
2023-09-21 18:13:17 +02:00
|
|
|
if (der_length < 1) {
|
|
|
|
/* Avoid empty-buffer shenanigans. A valid DER encoding is never
|
|
|
|
* empty. */
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 2: Decode the hex string into an intermediate buffer. */
|
|
|
|
unsigned char *der = mbedtls_calloc(1, der_length);
|
|
|
|
if (der == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
/* Beyond this point, der needs to be freed on exit. */
|
2023-09-21 14:03:52 +02:00
|
|
|
for (size_t i = 0; i < der_length; i++) {
|
|
|
|
int c = hexpair_to_int(s + 2 * i);
|
|
|
|
if (c < 0) {
|
2023-09-21 18:13:17 +02:00
|
|
|
goto error;
|
2023-08-14 17:32:22 +02:00
|
|
|
}
|
2023-09-21 18:13:17 +02:00
|
|
|
der[i] = c;
|
2023-07-26 12:55:31 +02:00
|
|
|
}
|
2023-07-27 15:34:11 +02:00
|
|
|
|
2023-09-21 18:13:17 +02:00
|
|
|
/* Step 3: decode the DER. */
|
|
|
|
/* We've checked that der_length >= 1 above. */
|
|
|
|
*tag = der[0];
|
2023-10-13 15:40:14 +02:00
|
|
|
{
|
|
|
|
unsigned char *p = der + 1;
|
|
|
|
if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* Now p points to the first byte of the payload inside der,
|
|
|
|
* and *data_len is the length of the payload. */
|
2023-09-21 14:03:52 +02:00
|
|
|
|
2023-10-13 15:40:14 +02:00
|
|
|
/* Step 4: payload validation */
|
|
|
|
if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
/* Strings must not contain null bytes. */
|
|
|
|
if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
|
|
|
|
for (size_t i = 0; i < *data_len; i++) {
|
|
|
|
if (p[i] == 0) {
|
|
|
|
goto error;
|
|
|
|
}
|
2023-09-21 14:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
2023-07-27 15:34:11 +02:00
|
|
|
|
2023-10-13 15:40:14 +02:00
|
|
|
/* Step 5: output the payload. */
|
|
|
|
if (*data_len > data_size) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
memcpy(data, p, *data_len);
|
2023-09-21 18:13:17 +02:00
|
|
|
}
|
|
|
|
mbedtls_free(der);
|
|
|
|
|
2023-07-24 18:21:14 +02:00
|
|
|
return 0;
|
2023-09-21 18:13:17 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
mbedtls_free(der);
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
2023-07-21 16:47:47 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2023-06-27 16:17:44 +02:00
|
|
|
int ret = MBEDTLS_ERR_X509_INVALID_NAME;
|
2023-07-24 18:21:14 +02:00
|
|
|
int parse_ret = 0;
|
2013-10-28 21:19:10 +01:00
|
|
|
const char *s = name, *c = s;
|
2023-01-11 14:50:10 +01:00
|
|
|
const char *end = s + strlen(s);
|
2023-08-16 12:31:17 +02:00
|
|
|
mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
|
2023-01-11 14:50:10 +01:00
|
|
|
const x509_attr_descriptor_t *attr_descr = NULL;
|
2023-08-10 14:51:38 +02:00
|
|
|
int in_attr_type = 1;
|
2023-07-27 15:34:11 +02:00
|
|
|
int tag;
|
2023-07-24 18:21:14 +02:00
|
|
|
int numericoid = 0;
|
2023-07-27 15:34:11 +02:00
|
|
|
unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
|
|
|
|
size_t data_len = 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/* Clear existing chain if present */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_free_named_data_list(head);
|
|
|
|
|
|
|
|
while (c <= end) {
|
2023-08-10 14:51:38 +02:00
|
|
|
if (in_attr_type && *c == '=') {
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) {
|
2023-08-15 18:42:33 +02:00
|
|
|
if ((mbedtls_oid_from_numeric_string(&oid, s, c - s)) != 0) {
|
2023-08-11 13:42:03 +02:00
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
2023-07-24 18:21:14 +02:00
|
|
|
} else {
|
|
|
|
numericoid = 1;
|
|
|
|
}
|
|
|
|
} else {
|
2023-08-15 18:42:33 +02:00
|
|
|
oid.len = strlen(attr_descr->oid);
|
|
|
|
oid.p = mbedtls_calloc(1, oid.len);
|
|
|
|
memcpy(oid.p, attr_descr->oid, oid.len);
|
2023-07-24 18:21:14 +02:00
|
|
|
numericoid = 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
s = c + 1;
|
2023-08-10 14:51:38 +02:00
|
|
|
in_attr_type = 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-08-10 14:51:38 +02:00
|
|
|
if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
|
2023-08-23 12:35:26 +02:00
|
|
|
if (s == c) {
|
2023-08-23 12:28:30 +02:00
|
|
|
mbedtls_free(oid.p);
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
|
|
|
} else if (*s == '#') {
|
2023-09-21 16:50:40 +02:00
|
|
|
/* We know that c >= s (loop invariant) and c != s (in this
|
|
|
|
* else branch), hence c - s - 1 >= 0. */
|
|
|
|
parse_ret = parse_attribute_value_hex_der_encoded(
|
|
|
|
s + 1, c - s - 1,
|
2023-09-21 18:13:17 +02:00
|
|
|
data, sizeof(data), &data_len, &tag);
|
2023-09-21 16:50:40 +02:00
|
|
|
if (parse_ret != 0) {
|
2023-08-15 18:42:33 +02:00
|
|
|
mbedtls_free(oid.p);
|
2023-09-21 18:51:35 +02:00
|
|
|
return parse_ret;
|
2023-08-22 18:50:00 +02:00
|
|
|
}
|
2023-08-22 18:29:18 +02:00
|
|
|
} else {
|
2023-08-22 18:50:00 +02:00
|
|
|
if (numericoid) {
|
2023-08-22 18:29:18 +02:00
|
|
|
mbedtls_free(oid.p);
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
2023-08-22 18:50:00 +02:00
|
|
|
} else {
|
2023-08-11 15:58:14 +02:00
|
|
|
if ((parse_ret =
|
2023-08-14 17:31:11 +02:00
|
|
|
parse_attribute_value_string(s, (int) (c - s), data,
|
|
|
|
&data_len)) != 0) {
|
2023-08-15 18:42:33 +02:00
|
|
|
mbedtls_free(oid.p);
|
2023-08-11 15:58:14 +02:00
|
|
|
return parse_ret;
|
|
|
|
}
|
|
|
|
tag = attr_descr->default_tag;
|
|
|
|
}
|
|
|
|
}
|
2023-08-22 18:29:18 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_named_data *cur =
|
2023-08-15 18:42:33 +02:00
|
|
|
mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
|
2023-01-11 14:50:10 +01:00
|
|
|
(unsigned char *) data,
|
2023-07-24 18:21:14 +02:00
|
|
|
data_len);
|
2023-08-15 18:42:33 +02:00
|
|
|
mbedtls_free(oid.p);
|
|
|
|
oid.p = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cur == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 12:46:13 +02:00
|
|
|
// set tagType
|
2023-07-27 15:34:11 +02:00
|
|
|
cur->val.tag = tag;
|
2018-05-17 12:46:13 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (c < end && *(c + 1) == ' ') {
|
2013-09-16 13:49:26 +02:00
|
|
|
c++;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
s = c + 1;
|
2023-08-10 14:51:38 +02:00
|
|
|
in_attr_type = 1;
|
2023-06-27 16:17:44 +02:00
|
|
|
|
|
|
|
/* Successfully parsed one name, update ret to success */
|
|
|
|
ret = 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
c++;
|
|
|
|
}
|
2023-08-15 18:42:33 +02:00
|
|
|
if (oid.p != NULL) {
|
|
|
|
mbedtls_free(oid.p);
|
2023-08-15 16:05:03 +02:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
|
2013-09-16 13:49:26 +02:00
|
|
|
* to store the critical boolean for us
|
|
|
|
*/
|
2023-01-11 14:50:10 +01: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)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *cur;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
|
|
|
|
NULL, val_len + 1)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cur->val.p[0] = critical;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(cur->val.p + 1, val, val_len);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int x509_write_name(unsigned char **p,
|
|
|
|
unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *cur_name)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 13:49:26 +02:00
|
|
|
size_t len = 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
const char *oid = (const char *) cur_name->oid.p;
|
2018-05-17 12:46:13 +02: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-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
|
|
|
|
cur_name->val.tag,
|
|
|
|
(const char *) name,
|
|
|
|
name_len));
|
2013-09-16 13:49:26 +02:00
|
|
|
// Write OID
|
|
|
|
//
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
|
|
|
|
oid_len));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) len;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *first)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 13:49:26 +02:00
|
|
|
size_t len = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *cur = first;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur != NULL) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
|
2013-09-16 13:49:26 +02:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) len;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
|
|
|
|
const char *oid, size_t oid_len,
|
2022-11-07 12:38:38 +01:00
|
|
|
unsigned char *sig, size_t size,
|
|
|
|
mbedtls_pk_type_t pk_alg)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-11-07 12:38:38 +01:00
|
|
|
int write_null_par;
|
2013-09-16 13:49:26 +02:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p < start || (size_t) (*p - start) < size) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
len = size;
|
|
|
|
(*p) -= len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(*p, sig, len);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p - start < 1) {
|
|
|
|
return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
|
|
|
|
}
|
2015-10-21 12:23:09 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
*--(*p) = 0;
|
|
|
|
len += 1;
|
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
// Write OID
|
|
|
|
//
|
2022-11-07 12:38:38 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) len;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int x509_write_extension(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *ext)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 13:49:26 +02:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ext->val.p[0] != 0) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) len;
|
2013-09-16 13:49:26 +02: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-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
|
|
|
|
mbedtls_asn1_named_data *first)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 13:49:26 +02:00
|
|
|
size_t len = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_named_data *cur_ext = first;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (cur_ext != NULL) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
|
2013-09-16 13:49:26 +02:00
|
|
|
cur_ext = cur_ext->next;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) len;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CREATE_C */
|