blob: b6895bf0a27325f6f6ca6a535904044e59ac507a [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.
192 * \param data Output buffer of size #MBEDTLS_X509_MAX_DN_NAME_SIZE.
193 * 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.
197 * \param data_len On success, the length of the parsed string.
198 * It is guaranteed to be less than
199 * #MBEDTLS_X509_MAX_DN_NAME_SIZE.
200 * \param tag The ASN.1 tag that the payload in \p data is encoded in.
201 *
202 * \retval 0 on success.
203 * \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
204 * a valid hexstring,
205 * or if the decoded hexstring is not valid DER,
206 * or if the decoded hexstring does not fit in \p data,
207 * of if \p *tag is an ASN.1 string tag and the payload
208 * contains a null byte.
209 */
210static int parse_attribute_value_hex_der_encoded(const char *s,
211 int len,
212 unsigned char *data,
213 size_t *data_len,
214 int *tag)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100215{
Gilles Peskine25665782023-09-21 14:03:52 +0200216 /* Step 1: Decode the hex string.
217 * We use data as an intermediate buffer. This limits the ultimate payload
218 * to slightly less than MBEDTLS_X509_MAX_DN_NAME_SIZE bytes due to the
219 * overhead of the DER tag+length. */
220 /* Each byte is encoded by exactly two hexadecimal digits. */
221 if (len % 2 != 0) {
222 /* Odd number of hex digits */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100223 return MBEDTLS_ERR_X509_INVALID_NAME;
224 }
Gilles Peskine25665782023-09-21 14:03:52 +0200225 size_t const der_length = len / 2;
226 /* Here we treat MBEDTLS_X509_MAX_DN_NAME_SIZE as the maximum length of
227 * the DER encoding. This is convenient, but seems wrong: should it
228 * be the length of the payload (which would require a few more bytes
229 * in the intermediate buffer)? In practice the hex-encoded data is
230 * expected to be much shorter anyway. */
231 if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
232 /* Not enough room in data */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100233 return MBEDTLS_ERR_X509_INVALID_NAME;
234 }
Gilles Peskine25665782023-09-21 14:03:52 +0200235 for (size_t i = 0; i < der_length; i++) {
236 int c = hexpair_to_int(s + 2 * i);
237 if (c < 0) {
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100238 return MBEDTLS_ERR_X509_INVALID_NAME;
239 }
Gilles Peskine25665782023-09-21 14:03:52 +0200240 data[i] = c;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100241 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100242
Gilles Peskine25665782023-09-21 14:03:52 +0200243 /* Step 2: decode the DER. */
244 if (der_length < 1) {
245 return MBEDTLS_ERR_X509_INVALID_NAME;
246 }
247 *tag = data[0];
248 unsigned char *p = data + 1;
249 if (mbedtls_asn1_get_len(&p, data + der_length, data_len) != 0) {
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100250 return MBEDTLS_ERR_X509_INVALID_NAME;
251 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100252
Gilles Peskine25665782023-09-21 14:03:52 +0200253 /* Step 3: extract the payload. */
254 /* Now p points to the first byte of the payload inside data.
255 * Shift the content of data to move the payload to the beginning. */
256 memmove(data, p, *data_len);
257
258 /* Step 4: payload validation */
259 if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
260 for (size_t i = 0; i < *data_len; i++) {
261 if (data[i] == 0) {
262 return MBEDTLS_ERR_X509_INVALID_NAME;
263 }
264 }
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100265 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100266
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100267 return 0;
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100268}
269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271{
David Horstmann8fd98d62023-06-27 15:17:44 +0100272 int ret = MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100273 int parse_ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +0100274 const char *s = name, *c = s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 const char *end = s + strlen(s);
Agathiyan Bragadeeshba386ec2023-08-16 11:31:17 +0100276 mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 const x509_attr_descriptor_t *attr_descr = NULL;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100278 int in_attr_type = 1;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100279 int tag;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100280 int numericoid = 0;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100281 unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
282 size_t data_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283
284 /* Clear existing chain if present */
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_asn1_free_named_data_list(head);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 while (c <= end) {
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100288 if (in_attr_type && *c == '=') {
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100290 if ((mbedtls_oid_from_numeric_string(&oid, s, c - s)) != 0) {
Agathiyan Bragadeesh17984872023-08-11 12:42:03 +0100291 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100292 } else {
293 numericoid = 1;
294 }
295 } else {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100296 oid.len = strlen(attr_descr->oid);
297 oid.p = mbedtls_calloc(1, oid.len);
298 memcpy(oid.p, attr_descr->oid, oid.len);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100299 numericoid = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 }
301
302 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100303 in_attr_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304 }
305
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100306 if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
Agathiyan Bragadeesh457ac842023-08-23 11:35:26 +0100307 if (s == c) {
Agathiyan Bragadeesh4c7d7bf2023-08-23 11:28:30 +0100308 mbedtls_free(oid.p);
309 return MBEDTLS_ERR_X509_INVALID_NAME;
310 } else if (*s == '#') {
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100311 if ((parse_ret =
Gilles Peskine25665782023-09-21 14:03:52 +0200312 parse_attribute_value_hex_der_encoded(s + 1, (int) (c - s - 1),
313 data, &data_len,
314 &tag)) != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100315 mbedtls_free(oid.p);
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100316 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100317 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100318 } else {
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100319 if (numericoid) {
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100320 mbedtls_free(oid.p);
321 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100322 } else {
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100323 if ((parse_ret =
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100324 parse_attribute_value_string(s, (int) (c - s), data,
325 &data_len)) != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100326 mbedtls_free(oid.p);
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100327 return parse_ret;
328 }
329 tag = attr_descr->default_tag;
330 }
331 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 mbedtls_asn1_named_data *cur =
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100334 mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 (unsigned char *) data,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100336 data_len);
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100337 mbedtls_free(oid.p);
338 oid.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (cur == NULL) {
340 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341 }
342
Jaeden Amero23f954d2018-05-17 11:46:13 +0100343 // set tagType
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100344 cur->val.tag = tag;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 while (c < end && *(c + 1) == ' ') {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 c++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349
350 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100351 in_attr_type = 1;
David Horstmann8fd98d62023-06-27 15:17:44 +0100352
353 /* Successfully parsed one name, update ret to success */
354 ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 }
356 c++;
357 }
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100358 if (oid.p != NULL) {
359 mbedtls_free(oid.p);
Agathiyan Bragadeesh55d93192023-08-15 15:05:03 +0100360 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362}
363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 * to store the critical boolean for us
366 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100367int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
368 int critical, const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_asn1_named_data *cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
373 NULL, val_len + 1)) == NULL) {
374 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375 }
376
377 cur->val.p[0] = critical;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 memcpy(cur->val.p + 1, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381}
382
383/*
384 * RelativeDistinguishedName ::=
385 * SET OF AttributeTypeAndValue
386 *
387 * AttributeTypeAndValue ::= SEQUENCE {
388 * type AttributeType,
389 * value AttributeValue }
390 *
391 * AttributeType ::= OBJECT IDENTIFIER
392 *
393 * AttributeValue ::= ANY DEFINED BY AttributeType
394 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395static int x509_write_name(unsigned char **p,
396 unsigned char *start,
397 mbedtls_asn1_named_data *cur_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398{
Janos Follath865b3eb2019-12-16 11:46:15 +0000399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 size_t len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 const char *oid = (const char *) cur_name->oid.p;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100402 size_t oid_len = cur_name->oid.len;
403 const unsigned char *name = cur_name->val.p;
404 size_t name_len = cur_name->val.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405
Jaeden Amero23f954d2018-05-17 11:46:13 +0100406 // Write correct string tag and value
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
408 cur_name->val.tag,
409 (const char *) name,
410 name_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 // Write OID
412 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
414 oid_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
417 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
418 MBEDTLS_ASN1_CONSTRUCTED |
419 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
422 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
423 MBEDTLS_ASN1_CONSTRUCTED |
424 MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427}
428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
430 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431{
Janos Follath865b3eb2019-12-16 11:46:15 +0000432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200433 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_asn1_named_data *cur = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 while (cur != NULL) {
437 MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 cur = cur->next;
439 }
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
442 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
443 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446}
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
449 const char *oid, size_t oid_len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100450 unsigned char *sig, size_t size,
451 mbedtls_pk_type_t pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200452{
Janos Follath865b3eb2019-12-16 11:46:15 +0000453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Marek Jansta8bde6492022-11-07 12:38:38 +0100454 int write_null_par;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455 size_t len = 0;
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (*p < start || (size_t) (*p - start) < size) {
458 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
459 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 len = size;
462 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 memcpy(*p, sig, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (*p - start < 1) {
466 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
467 }
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200468
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469 *--(*p) = 0;
470 len += 1;
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
473 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
475 // Write OID
476 //
Marek Jansta8bde6492022-11-07 12:38:38 +0100477 if (pk_alg == MBEDTLS_PK_ECDSA) {
478 /*
479 * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
480 * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
481 * https://www.rfc-editor.org/rfc/rfc5758#section-3.
482 */
483 write_null_par = 0;
484 } else {
485 write_null_par = 1;
486 }
487 MBEDTLS_ASN1_CHK_ADD(len,
488 mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
489 0, write_null_par));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492}
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494static int x509_write_extension(unsigned char **p, unsigned char *start,
495 mbedtls_asn1_named_data *ext)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496{
Janos Follath865b3eb2019-12-16 11:46:15 +0000497 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200498 size_t len = 0;
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
501 ext->val.len - 1));
502 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
503 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (ext->val.p[0] != 0) {
506 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200507 }
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
510 ext->oid.len));
511 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
512 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
515 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
516 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519}
520
521/*
522 * Extension ::= SEQUENCE {
523 * extnID OBJECT IDENTIFIER,
524 * critical BOOLEAN DEFAULT FALSE,
525 * extnValue OCTET STRING
526 * -- contains the DER encoding of an ASN.1 value
527 * -- corresponding to the extension type identified
528 * -- by extnID
529 * }
530 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100531int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
532 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533{
Janos Follath865b3eb2019-12-16 11:46:15 +0000534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 mbedtls_asn1_named_data *cur_ext = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 while (cur_ext != NULL) {
539 MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540 cur_ext = cur_ext->next;
541 }
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544}
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_X509_CREATE_C */