blob: 296836943f0f5317e6b7e46cadda949758adefae [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 base functions for creating certificates / CSRs
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_X509_CREATE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/x509.h"
25#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000026#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/oid.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +010031#include "mbedtls/platform.h"
32
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +010033#include "mbedtls/asn1.h"
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +010034
Hanno Beckerd2c90092018-10-08 14:32:55 +010035/* Structure linking OIDs for X.509 DN AttributeTypes to their
36 * string representations and default string encodings used by Mbed TLS. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020037typedef struct {
Gilles Peskine449bd832023-01-11 14:50:10 +010038 const char *name; /* String representation of AttributeType, e.g.
39 * "CN" or "emailAddress". */
40 size_t name_len; /* Length of 'name', without trailing 0 byte. */
41 const char *oid; /* String representation of OID of AttributeType,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +010042 * as per RFC 5280, Appendix A.1. encoded as per
43 * X.690 */
Gilles Peskine449bd832023-01-11 14:50:10 +010044 int default_tag; /* The default character encoding used for the
Hanno Beckerd2c90092018-10-08 14:32:55 +010045 * given attribute type, e.g.
Hanno Beckeree334a32018-10-24 12:33:07 +010046 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020047} x509_attr_descriptor_t;
48
Gilles Peskine449bd832023-01-11 14:50:10 +010049#define ADD_STRLEN(s) s, sizeof(s) - 1
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020050
Hanno Becker35b68542018-10-08 14:47:38 +010051/* X.509 DN attributes from RFC 5280, Appendix A.1. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020052static const x509_attr_descriptor_t x509_attrs[] =
53{
Gilles Peskine449bd832023-01-11 14:50:10 +010054 { ADD_STRLEN("CN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010055 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010056 { ADD_STRLEN("commonName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010057 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010058 { ADD_STRLEN("C"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010059 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010060 { ADD_STRLEN("countryName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010061 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010062 { ADD_STRLEN("O"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010063 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010064 { ADD_STRLEN("organizationName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010065 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010066 { ADD_STRLEN("L"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010067 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010068 { ADD_STRLEN("locality"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010069 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010070 { ADD_STRLEN("R"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010071 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010072 { ADD_STRLEN("OU"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010073 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010074 { ADD_STRLEN("organizationalUnitName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010075 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010076 { ADD_STRLEN("ST"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010077 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010078 { ADD_STRLEN("stateOrProvinceName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010079 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010080 { ADD_STRLEN("emailAddress"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010081 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010082 { ADD_STRLEN("serialNumber"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010083 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010084 { ADD_STRLEN("postalAddress"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010085 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010086 { ADD_STRLEN("postalCode"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010087 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010088 { ADD_STRLEN("dnQualifier"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010089 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010090 { ADD_STRLEN("title"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010091 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010092 { ADD_STRLEN("surName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010093 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010094 { ADD_STRLEN("SN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010095 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010096 { ADD_STRLEN("givenName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010097 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010098 { ADD_STRLEN("GN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010099 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 { ADD_STRLEN("initials"),
Hanno Becker1624e2e2018-10-08 14:52:20 +0100101 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 { ADD_STRLEN("pseudonym"),
Hanno Becker1624e2e2018-10-08 14:52:20 +0100103 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 { ADD_STRLEN("generationQualifier"),
Hanno Becker1624e2e2018-10-08 14:52:20 +0100105 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 { ADD_STRLEN("domainComponent"),
Hanno Becker1624e2e2018-10-08 14:52:20 +0100107 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 { ADD_STRLEN("DC"),
Hanno Becker1624e2e2018-10-08 14:52:20 +0100109 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
tdoec150f0d2018-05-18 12:12:45 +0200110 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200111};
112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name, size_t name_len)
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200114{
115 const x509_attr_descriptor_t *cur;
116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 for (cur = x509_attrs; cur->name != NULL; cur++) {
118 if (cur->name_len == name_len &&
119 strncmp(cur->name, name, name_len) == 0) {
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200120 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 }
122 }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if (cur->name == NULL) {
125 return NULL;
126 }
Hanno Beckerd2c90092018-10-08 14:32:55 +0100127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 return cur;
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200129}
130
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100131static int hex_to_int(char c)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100132{
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100133 return ('0' <= c && c <= '9') ? (c - '0') :
134 ('a' <= c && c <= 'f') ? (c - 'a' + 10) :
135 ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
136}
137
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100138static int hexpair_to_int(const char *hexpair)
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100139{
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100140 int n1 = hex_to_int(*hexpair);
141 int n2 = hex_to_int(*(hexpair + 1));
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100142
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100143 if (n1 != -1 && n2 != -1) {
144 return (n1 << 4) | n2;
145 } else {
146 return -1;
147 }
148}
149
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100150static int parse_attribute_value_string(const char *s,
151 int len,
152 unsigned char *data,
153 size_t *data_len)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100154{
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100155 const char *c;
156 const char *end = s + len;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100157 unsigned char *d = data;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100158 int n;
Agathiyan Bragadeesha2423de2023-08-30 16:24:31 +0100159
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100160 for (c = s; c < end; c++) {
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100161 if (*c == '\\') {
162 c++;
163
Agathiyan Bragadeeshe9d1c8e2023-08-30 15:50:12 +0100164 /* Check for valid escaped characters as per RFC 4514 Section 3 */
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100165 if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100166 if (n == 0) {
Agathiyan Bragadeesh9caaa6d2023-08-14 15:38:39 +0100167 return MBEDTLS_ERR_X509_INVALID_NAME;
168 }
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100169 *(d++) = n;
170 c++;
Agathiyan Bragadeeshc34804d2023-09-08 11:32:19 +0100171 } else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
172 *(d++) = *c;
173 } else {
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100174 return MBEDTLS_ERR_X509_INVALID_NAME;
175 }
Agathiyan Bragadeesh706a1c32023-09-08 12:04:41 +0100176 } else {
Agathiyan Bragadeeshc34804d2023-09-08 11:32:19 +0100177 *(d++) = *c;
178 }
Agathiyan Bragadeesha2423de2023-08-30 16:24:31 +0100179
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100180 if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
181 return MBEDTLS_ERR_X509_INVALID_NAME;
182 }
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100183 }
184 *data_len = d - data;
185 return 0;
186}
187
Gilles Peskine25665782023-09-21 14:03:52 +0200188/** Parse a hexstring containing a DER-encoded string.
189 *
190 * \param s A string of \p len bytes hexadecimal digits.
191 * \param len Number of bytes to read from \p s.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200192 * \param data Output buffer of size \p data_size.
Gilles Peskine25665782023-09-21 14:03:52 +0200193 * On success, it contains the payload that's DER-encoded
194 * in the input (content without the tag and length).
195 * If the DER tag is a string tag, the payload is guaranteed
196 * not to contain null bytes.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200197 * \param data_size Length of the \p data buffer.
Gilles Peskine25665782023-09-21 14:03:52 +0200198 * \param data_len On success, the length of the parsed string.
199 * It is guaranteed to be less than
200 * #MBEDTLS_X509_MAX_DN_NAME_SIZE.
201 * \param tag The ASN.1 tag that the payload in \p data is encoded in.
202 *
203 * \retval 0 on success.
204 * \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
205 * a valid hexstring,
206 * or if the decoded hexstring is not valid DER,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200207 * or if the payload does not fit in \p data,
208 * or if the payload is more than
209 * #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes,
Gilles Peskine25665782023-09-21 14:03:52 +0200210 * of if \p *tag is an ASN.1 string tag and the payload
211 * contains a null byte.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200212 * \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory.
Gilles Peskine25665782023-09-21 14:03:52 +0200213 */
214static int parse_attribute_value_hex_der_encoded(const char *s,
Gilles Peskine70777812023-09-21 16:50:40 +0200215 size_t len,
Gilles Peskine25665782023-09-21 14:03:52 +0200216 unsigned char *data,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200217 size_t data_size,
Gilles Peskine25665782023-09-21 14:03:52 +0200218 size_t *data_len,
219 int *tag)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100220{
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200221 /* Step 1: preliminary length checks. */
Gilles Peskine25665782023-09-21 14:03:52 +0200222 /* Each byte is encoded by exactly two hexadecimal digits. */
223 if (len % 2 != 0) {
224 /* Odd number of hex digits */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100225 return MBEDTLS_ERR_X509_INVALID_NAME;
226 }
Gilles Peskine25665782023-09-21 14:03:52 +0200227 size_t const der_length = len / 2;
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200228 if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) {
229 /* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE
230 * (after subtracting the ASN.1 tag and length). Reject this early
231 * to avoid allocating a large intermediate buffer. */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100232 return MBEDTLS_ERR_X509_INVALID_NAME;
233 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200234 if (der_length < 1) {
235 /* Avoid empty-buffer shenanigans. A valid DER encoding is never
236 * empty. */
237 return MBEDTLS_ERR_X509_INVALID_NAME;
238 }
239
240 /* Step 2: Decode the hex string into an intermediate buffer. */
241 unsigned char *der = mbedtls_calloc(1, der_length);
242 if (der == NULL) {
243 return MBEDTLS_ERR_X509_ALLOC_FAILED;
244 }
245 /* Beyond this point, der needs to be freed on exit. */
Gilles Peskine25665782023-09-21 14:03:52 +0200246 for (size_t i = 0; i < der_length; i++) {
247 int c = hexpair_to_int(s + 2 * i);
248 if (c < 0) {
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200249 goto error;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100250 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200251 der[i] = c;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100252 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100253
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200254 /* Step 3: decode the DER. */
255 /* We've checked that der_length >= 1 above. */
256 *tag = der[0];
257 unsigned char *p = der + 1;
258 if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) {
259 goto error;
Gilles Peskine25665782023-09-21 14:03:52 +0200260 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200261 /* Now p points to the first byte of the payload inside der,
262 * and *data_len is the length of the payload. */
Gilles Peskine25665782023-09-21 14:03:52 +0200263
264 /* Step 4: payload validation */
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200265 if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
266 goto error;
267 }
268 /* Strings must not contain null bytes. */
Gilles Peskine25665782023-09-21 14:03:52 +0200269 if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
270 for (size_t i = 0; i < *data_len; i++) {
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200271 if (p[i] == 0) {
272 goto error;
Gilles Peskine25665782023-09-21 14:03:52 +0200273 }
274 }
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100275 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100276
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200277 /* Step 5: output the payload. */
278 if (*data_len > data_size) {
279 goto error;
280 }
281 memcpy(data, p, *data_len);
282 mbedtls_free(der);
283
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100284 return 0;
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200285
286error:
287 mbedtls_free(der);
288 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100289}
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292{
David Horstmann8fd98d62023-06-27 15:17:44 +0100293 int ret = MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100294 int parse_ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +0100295 const char *s = name, *c = s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 const char *end = s + strlen(s);
Agathiyan Bragadeeshba386ec2023-08-16 11:31:17 +0100297 mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 const x509_attr_descriptor_t *attr_descr = NULL;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100299 int in_attr_type = 1;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100300 int tag;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100301 int numericoid = 0;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100302 unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
303 size_t data_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304
305 /* Clear existing chain if present */
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 mbedtls_asn1_free_named_data_list(head);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 while (c <= end) {
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100309 if (in_attr_type && *c == '=') {
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100311 if ((mbedtls_oid_from_numeric_string(&oid, s, c - s)) != 0) {
Agathiyan Bragadeesh17984872023-08-11 12:42:03 +0100312 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100313 } else {
314 numericoid = 1;
315 }
316 } else {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100317 oid.len = strlen(attr_descr->oid);
318 oid.p = mbedtls_calloc(1, oid.len);
319 memcpy(oid.p, attr_descr->oid, oid.len);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100320 numericoid = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321 }
322
323 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100324 in_attr_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325 }
326
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100327 if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
Agathiyan Bragadeesh457ac842023-08-23 11:35:26 +0100328 if (s == c) {
Agathiyan Bragadeesh4c7d7bf2023-08-23 11:28:30 +0100329 mbedtls_free(oid.p);
330 return MBEDTLS_ERR_X509_INVALID_NAME;
331 } else if (*s == '#') {
Gilles Peskine70777812023-09-21 16:50:40 +0200332 /* We know that c >= s (loop invariant) and c != s (in this
333 * else branch), hence c - s - 1 >= 0. */
334 parse_ret = parse_attribute_value_hex_der_encoded(
335 s + 1, c - s - 1,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200336 data, sizeof(data), &data_len, &tag);
Gilles Peskine70777812023-09-21 16:50:40 +0200337 if (parse_ret != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100338 mbedtls_free(oid.p);
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100339 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100340 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100341 } else {
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100342 if (numericoid) {
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100343 mbedtls_free(oid.p);
344 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100345 } else {
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100346 if ((parse_ret =
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100347 parse_attribute_value_string(s, (int) (c - s), data,
348 &data_len)) != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100349 mbedtls_free(oid.p);
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100350 return parse_ret;
351 }
352 tag = attr_descr->default_tag;
353 }
354 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 mbedtls_asn1_named_data *cur =
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100357 mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 (unsigned char *) data,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100359 data_len);
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100360 mbedtls_free(oid.p);
361 oid.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (cur == NULL) {
363 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 }
365
Jaeden Amero23f954d2018-05-17 11:46:13 +0100366 // set tagType
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100367 cur->val.tag = tag;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 while (c < end && *(c + 1) == ' ') {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 c++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
373 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100374 in_attr_type = 1;
David Horstmann8fd98d62023-06-27 15:17:44 +0100375
376 /* Successfully parsed one name, update ret to success */
377 ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 }
379 c++;
380 }
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100381 if (oid.p != NULL) {
382 mbedtls_free(oid.p);
Agathiyan Bragadeesh55d93192023-08-15 15:05:03 +0100383 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385}
386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 * to store the critical boolean for us
389 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
391 int critical, const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_asn1_named_data *cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
396 NULL, val_len + 1)) == NULL) {
397 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 }
399
400 cur->val.p[0] = critical;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 memcpy(cur->val.p + 1, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404}
405
406/*
407 * RelativeDistinguishedName ::=
408 * SET OF AttributeTypeAndValue
409 *
410 * AttributeTypeAndValue ::= SEQUENCE {
411 * type AttributeType,
412 * value AttributeValue }
413 *
414 * AttributeType ::= OBJECT IDENTIFIER
415 *
416 * AttributeValue ::= ANY DEFINED BY AttributeType
417 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418static int x509_write_name(unsigned char **p,
419 unsigned char *start,
420 mbedtls_asn1_named_data *cur_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421{
Janos Follath865b3eb2019-12-16 11:46:15 +0000422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 size_t len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 const char *oid = (const char *) cur_name->oid.p;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100425 size_t oid_len = cur_name->oid.len;
426 const unsigned char *name = cur_name->val.p;
427 size_t name_len = cur_name->val.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
Jaeden Amero23f954d2018-05-17 11:46:13 +0100429 // Write correct string tag and value
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
431 cur_name->val.tag,
432 (const char *) name,
433 name_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434 // Write OID
435 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
437 oid_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
440 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
441 MBEDTLS_ASN1_CONSTRUCTED |
442 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
445 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
446 MBEDTLS_ASN1_CONSTRUCTED |
447 MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450}
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
453 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454{
Janos Follath865b3eb2019-12-16 11:46:15 +0000455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_asn1_named_data *cur = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 while (cur != NULL) {
460 MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 cur = cur->next;
462 }
463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
465 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
466 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469}
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
472 const char *oid, size_t oid_len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100473 unsigned char *sig, size_t size,
474 mbedtls_pk_type_t pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475{
Janos Follath865b3eb2019-12-16 11:46:15 +0000476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Marek Jansta8bde6492022-11-07 12:38:38 +0100477 int write_null_par;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 size_t len = 0;
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (*p < start || (size_t) (*p - start) < size) {
481 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
482 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483
484 len = size;
485 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 memcpy(*p, sig, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (*p - start < 1) {
489 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
490 }
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200491
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 *--(*p) = 0;
493 len += 1;
494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
496 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 // Write OID
499 //
Marek Jansta8bde6492022-11-07 12:38:38 +0100500 if (pk_alg == MBEDTLS_PK_ECDSA) {
501 /*
502 * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
503 * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
504 * https://www.rfc-editor.org/rfc/rfc5758#section-3.
505 */
506 write_null_par = 0;
507 } else {
508 write_null_par = 1;
509 }
510 MBEDTLS_ASN1_CHK_ADD(len,
511 mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
512 0, write_null_par));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515}
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517static int x509_write_extension(unsigned char **p, unsigned char *start,
518 mbedtls_asn1_named_data *ext)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519{
Janos Follath865b3eb2019-12-16 11:46:15 +0000520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 size_t len = 0;
522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
524 ext->val.len - 1));
525 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
526 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (ext->val.p[0] != 0) {
529 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 }
531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
533 ext->oid.len));
534 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
535 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
538 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
539 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542}
543
544/*
545 * Extension ::= SEQUENCE {
546 * extnID OBJECT IDENTIFIER,
547 * critical BOOLEAN DEFAULT FALSE,
548 * extnValue OCTET STRING
549 * -- contains the DER encoding of an ASN.1 value
550 * -- corresponding to the extension type identified
551 * -- by extnID
552 * }
553 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100554int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
555 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556{
Janos Follath865b3eb2019-12-16 11:46:15 +0000557 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_asn1_named_data *cur_ext = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 while (cur_ext != NULL) {
562 MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563 cur_ext = cur_ext->next;
564 }
565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567}
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_X509_CREATE_C */