blob: fa254fec505308e94b68ff081485490d3b87e120 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_X509_CREATE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020011
Valerio Setti25b282e2024-01-17 10:55:32 +010012#include "x509_internal.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000013#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000014#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/oid.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020016
Rich Evans00ab4702015-02-06 13:43:58 +000017#include <string.h>
18
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +010019#include "mbedtls/platform.h"
20
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +010021#include "mbedtls/asn1.h"
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +010022
Hanno Beckerd2c90092018-10-08 14:32:55 +010023/* Structure linking OIDs for X.509 DN AttributeTypes to their
24 * string representations and default string encodings used by Mbed TLS. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020025typedef struct {
Gilles Peskine449bd832023-01-11 14:50:10 +010026 const char *name; /* String representation of AttributeType, e.g.
27 * "CN" or "emailAddress". */
28 size_t name_len; /* Length of 'name', without trailing 0 byte. */
29 const char *oid; /* String representation of OID of AttributeType,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +010030 * as per RFC 5280, Appendix A.1. encoded as per
31 * X.690 */
Gilles Peskine449bd832023-01-11 14:50:10 +010032 int default_tag; /* The default character encoding used for the
Hanno Beckerd2c90092018-10-08 14:32:55 +010033 * given attribute type, e.g.
Hanno Beckeree334a32018-10-24 12:33:07 +010034 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020035} x509_attr_descriptor_t;
36
Gilles Peskine449bd832023-01-11 14:50:10 +010037#define ADD_STRLEN(s) s, sizeof(s) - 1
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020038
Hanno Becker35b68542018-10-08 14:47:38 +010039/* X.509 DN attributes from RFC 5280, Appendix A.1. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020040static const x509_attr_descriptor_t x509_attrs[] =
41{
Gilles Peskine449bd832023-01-11 14:50:10 +010042 { ADD_STRLEN("CN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010043 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010044 { ADD_STRLEN("commonName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010045 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010046 { ADD_STRLEN("C"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010047 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010048 { ADD_STRLEN("countryName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010049 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010050 { ADD_STRLEN("O"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010051 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010052 { ADD_STRLEN("organizationName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010053 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010054 { ADD_STRLEN("L"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010055 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010056 { ADD_STRLEN("locality"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010057 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010058 { ADD_STRLEN("R"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010059 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010060 { ADD_STRLEN("OU"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010061 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010062 { ADD_STRLEN("organizationalUnitName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010063 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010064 { ADD_STRLEN("ST"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010065 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010066 { ADD_STRLEN("stateOrProvinceName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010067 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010068 { ADD_STRLEN("emailAddress"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010069 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010070 { ADD_STRLEN("serialNumber"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010071 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010072 { ADD_STRLEN("postalAddress"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010073 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010074 { ADD_STRLEN("postalCode"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010075 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010076 { ADD_STRLEN("dnQualifier"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010077 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010078 { ADD_STRLEN("title"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010079 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010080 { ADD_STRLEN("surName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010081 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010082 { ADD_STRLEN("SN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010083 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010084 { ADD_STRLEN("givenName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010085 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010086 { ADD_STRLEN("GN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010087 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010088 { ADD_STRLEN("initials"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010089 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010090 { ADD_STRLEN("pseudonym"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010091 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010092 { ADD_STRLEN("generationQualifier"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010093 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010094 { ADD_STRLEN("domainComponent"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010095 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010096 { ADD_STRLEN("DC"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010097 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
tdoec150f0d2018-05-18 12:12:45 +020098 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020099};
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101static 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 +0200102{
103 const x509_attr_descriptor_t *cur;
104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 for (cur = x509_attrs; cur->name != NULL; cur++) {
106 if (cur->name_len == name_len &&
107 strncmp(cur->name, name, name_len) == 0) {
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200108 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 }
110 }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (cur->name == NULL) {
113 return NULL;
114 }
Hanno Beckerd2c90092018-10-08 14:32:55 +0100115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 return cur;
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200117}
118
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100119static int hex_to_int(char c)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100120{
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100121 return ('0' <= c && c <= '9') ? (c - '0') :
122 ('a' <= c && c <= 'f') ? (c - 'a' + 10) :
123 ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
124}
125
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100126static int hexpair_to_int(const char *hexpair)
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100127{
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100128 int n1 = hex_to_int(*hexpair);
129 int n2 = hex_to_int(*(hexpair + 1));
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100130
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100131 if (n1 != -1 && n2 != -1) {
132 return (n1 << 4) | n2;
133 } else {
134 return -1;
135 }
136}
137
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100138static int parse_attribute_value_string(const char *s,
139 int len,
140 unsigned char *data,
141 size_t *data_len)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100142{
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100143 const char *c;
144 const char *end = s + len;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100145 unsigned char *d = data;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100146 int n;
Agathiyan Bragadeesha2423de2023-08-30 16:24:31 +0100147
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100148 for (c = s; c < end; c++) {
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100149 if (*c == '\\') {
150 c++;
151
Agathiyan Bragadeeshe9d1c8e2023-08-30 15:50:12 +0100152 /* Check for valid escaped characters as per RFC 4514 Section 3 */
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100153 if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100154 if (n == 0) {
Agathiyan Bragadeesh9caaa6d2023-08-14 15:38:39 +0100155 return MBEDTLS_ERR_X509_INVALID_NAME;
156 }
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100157 *(d++) = n;
158 c++;
Agathiyan Bragadeeshc34804d2023-09-08 11:32:19 +0100159 } else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
160 *(d++) = *c;
161 } else {
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100162 return MBEDTLS_ERR_X509_INVALID_NAME;
163 }
Agathiyan Bragadeesh706a1c32023-09-08 12:04:41 +0100164 } else {
Agathiyan Bragadeeshc34804d2023-09-08 11:32:19 +0100165 *(d++) = *c;
166 }
Agathiyan Bragadeesha2423de2023-08-30 16:24:31 +0100167
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100168 if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
169 return MBEDTLS_ERR_X509_INVALID_NAME;
170 }
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100171 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000172 *data_len = (size_t) (d - data);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100173 return 0;
174}
175
Gilles Peskine25665782023-09-21 14:03:52 +0200176/** Parse a hexstring containing a DER-encoded string.
177 *
178 * \param s A string of \p len bytes hexadecimal digits.
179 * \param len Number of bytes to read from \p s.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200180 * \param data Output buffer of size \p data_size.
Gilles Peskine25665782023-09-21 14:03:52 +0200181 * On success, it contains the payload that's DER-encoded
182 * in the input (content without the tag and length).
183 * If the DER tag is a string tag, the payload is guaranteed
184 * not to contain null bytes.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200185 * \param data_size Length of the \p data buffer.
Gilles Peskine25665782023-09-21 14:03:52 +0200186 * \param data_len On success, the length of the parsed string.
187 * It is guaranteed to be less than
188 * #MBEDTLS_X509_MAX_DN_NAME_SIZE.
189 * \param tag The ASN.1 tag that the payload in \p data is encoded in.
190 *
191 * \retval 0 on success.
192 * \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
193 * a valid hexstring,
194 * or if the decoded hexstring is not valid DER,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200195 * or if the payload does not fit in \p data,
196 * or if the payload is more than
197 * #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes,
Gilles Peskine25665782023-09-21 14:03:52 +0200198 * of if \p *tag is an ASN.1 string tag and the payload
199 * contains a null byte.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200200 * \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory.
Gilles Peskine25665782023-09-21 14:03:52 +0200201 */
202static int parse_attribute_value_hex_der_encoded(const char *s,
Gilles Peskine70777812023-09-21 16:50:40 +0200203 size_t len,
Gilles Peskine25665782023-09-21 14:03:52 +0200204 unsigned char *data,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200205 size_t data_size,
Gilles Peskine25665782023-09-21 14:03:52 +0200206 size_t *data_len,
207 int *tag)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100208{
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200209 /* Step 1: preliminary length checks. */
Gilles Peskine25665782023-09-21 14:03:52 +0200210 /* Each byte is encoded by exactly two hexadecimal digits. */
211 if (len % 2 != 0) {
212 /* Odd number of hex digits */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100213 return MBEDTLS_ERR_X509_INVALID_NAME;
214 }
Gilles Peskine25665782023-09-21 14:03:52 +0200215 size_t const der_length = len / 2;
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200216 if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) {
217 /* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE
218 * (after subtracting the ASN.1 tag and length). Reject this early
219 * to avoid allocating a large intermediate buffer. */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100220 return MBEDTLS_ERR_X509_INVALID_NAME;
221 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200222 if (der_length < 1) {
223 /* Avoid empty-buffer shenanigans. A valid DER encoding is never
224 * empty. */
225 return MBEDTLS_ERR_X509_INVALID_NAME;
226 }
227
228 /* Step 2: Decode the hex string into an intermediate buffer. */
229 unsigned char *der = mbedtls_calloc(1, der_length);
230 if (der == NULL) {
231 return MBEDTLS_ERR_X509_ALLOC_FAILED;
232 }
233 /* Beyond this point, der needs to be freed on exit. */
Gilles Peskine25665782023-09-21 14:03:52 +0200234 for (size_t i = 0; i < der_length; i++) {
235 int c = hexpair_to_int(s + 2 * i);
236 if (c < 0) {
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200237 goto error;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100238 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200239 der[i] = c;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100240 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100241
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200242 /* Step 3: decode the DER. */
243 /* We've checked that der_length >= 1 above. */
244 *tag = der[0];
Dave Rodgman515af1d2023-10-13 14:40:14 +0100245 {
246 unsigned char *p = der + 1;
247 if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) {
248 goto error;
249 }
250 /* Now p points to the first byte of the payload inside der,
251 * and *data_len is the length of the payload. */
Gilles Peskine25665782023-09-21 14:03:52 +0200252
Dave Rodgman515af1d2023-10-13 14:40:14 +0100253 /* Step 4: payload validation */
254 if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
255 goto error;
256 }
257 /* Strings must not contain null bytes. */
258 if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
259 for (size_t i = 0; i < *data_len; i++) {
260 if (p[i] == 0) {
261 goto error;
262 }
Gilles Peskine25665782023-09-21 14:03:52 +0200263 }
264 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100265
Dave Rodgman515af1d2023-10-13 14:40:14 +0100266 /* Step 5: output the payload. */
267 if (*data_len > data_size) {
268 goto error;
269 }
270 memcpy(data, p, *data_len);
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200271 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200272 mbedtls_free(der);
273
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100274 return 0;
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200275
276error:
277 mbedtls_free(der);
278 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100279}
280
Sam Berryc71abc32024-07-19 15:11:10 +0100281#if defined(MBEDTLS_OID_C)
282
Sam Berry3da783b2024-09-13 15:09:24 +0100283static int oid_parse_number(unsigned int *num, const char **p, const char *bound)
284{
285 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
286
287 *num = 0;
288
289 while (*p < bound && **p >= '0' && **p <= '9') {
290 ret = 0;
291 if (*num > (UINT_MAX / 10)) {
292 return MBEDTLS_ERR_ASN1_INVALID_DATA;
293 }
294 *num *= 10;
295 *num += **p - '0';
296 (*p)++;
297 }
298 return ret;
299}
300
301static size_t oid_subidentifier_num_bytes(unsigned int value)
302{
303 size_t num_bytes = 0;
304
305 do {
306 value >>= 7;
307 num_bytes++;
308 } while (value != 0);
309
310 return num_bytes;
311}
312
313static int oid_subidentifier_encode_into(unsigned char **p,
314 unsigned char *bound,
315 unsigned int value)
316{
317 size_t num_bytes = oid_subidentifier_num_bytes(value);
318
319 if ((size_t) (bound - *p) < num_bytes) {
320 return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
321 }
322 (*p)[num_bytes - 1] = (unsigned char) (value & 0x7f);
323 value >>= 7;
324
325 for (size_t i = 2; i <= num_bytes; i++) {
326 (*p)[num_bytes - i] = 0x80 | (unsigned char) (value & 0x7f);
327 value >>= 7;
328 }
329 *p += num_bytes;
330
331 return 0;
332}
333
Sam Berryc71abc32024-07-19 15:11:10 +0100334/* Return the OID for the given x.y.z.... style numeric string */
335int mbedtls_oid_from_numeric_string(mbedtls_asn1_buf *oid,
336 const char *oid_str, size_t size)
337{
338 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
339 const char *str_ptr = oid_str;
340 const char *str_bound = oid_str + size;
341 unsigned int val = 0;
342 unsigned int component1, component2;
343 size_t encoded_len;
344 unsigned char *resized_mem;
345
346 /* Count the number of dots to get a worst-case allocation size. */
347 size_t num_dots = 0;
348 for (size_t i = 0; i < size; i++) {
349 if (oid_str[i] == '.') {
350 num_dots++;
351 }
352 }
353 /* Allocate maximum possible required memory:
354 * There are (num_dots + 1) integer components, but the first 2 share the
355 * same subidentifier, so we only need num_dots subidentifiers maximum. */
356 if (num_dots == 0 || (num_dots > MBEDTLS_OID_MAX_COMPONENTS - 1)) {
357 return MBEDTLS_ERR_ASN1_INVALID_DATA;
358 }
359 /* Each byte can store 7 bits, calculate number of bytes for a
360 * subidentifier:
361 *
362 * bytes = ceil(subidentifer_size * 8 / 7)
363 */
364 size_t bytes_per_subidentifier = (((sizeof(unsigned int) * 8) - 1) / 7)
365 + 1;
366 size_t max_possible_bytes = num_dots * bytes_per_subidentifier;
367 oid->p = mbedtls_calloc(max_possible_bytes, 1);
368 if (oid->p == NULL) {
369 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
370 }
371 unsigned char *out_ptr = oid->p;
372 unsigned char *out_bound = oid->p + max_possible_bytes;
373
374 ret = oid_parse_number(&component1, &str_ptr, str_bound);
375 if (ret != 0) {
376 goto error;
377 }
378 if (component1 > 2) {
379 /* First component can't be > 2 */
380 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
381 goto error;
382 }
383 if (str_ptr >= str_bound || *str_ptr != '.') {
384 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
385 goto error;
386 }
387 str_ptr++;
388
389 ret = oid_parse_number(&component2, &str_ptr, str_bound);
390 if (ret != 0) {
391 goto error;
392 }
393 if ((component1 < 2) && (component2 > 39)) {
394 /* Root nodes 0 and 1 may have up to 40 children, numbered 0-39 */
395 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
396 goto error;
397 }
398 if (str_ptr < str_bound) {
399 if (*str_ptr == '.') {
400 str_ptr++;
401 } else {
402 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
403 goto error;
404 }
405 }
406
407 if (component2 > (UINT_MAX - (component1 * 40))) {
408 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
409 goto error;
410 }
411 ret = oid_subidentifier_encode_into(&out_ptr, out_bound,
412 (component1 * 40) + component2);
413 if (ret != 0) {
414 goto error;
415 }
416
417 while (str_ptr < str_bound) {
418 ret = oid_parse_number(&val, &str_ptr, str_bound);
419 if (ret != 0) {
420 goto error;
421 }
422 if (str_ptr < str_bound) {
423 if (*str_ptr == '.') {
424 str_ptr++;
425 } else {
426 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
427 goto error;
428 }
429 }
430
431 ret = oid_subidentifier_encode_into(&out_ptr, out_bound, val);
432 if (ret != 0) {
433 goto error;
434 }
435 }
436
437 encoded_len = (size_t) (out_ptr - oid->p);
438 resized_mem = mbedtls_calloc(encoded_len, 1);
439 if (resized_mem == NULL) {
440 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
441 goto error;
442 }
443 memcpy(resized_mem, oid->p, encoded_len);
444 mbedtls_free(oid->p);
445 oid->p = resized_mem;
446 oid->len = encoded_len;
447
448 oid->tag = MBEDTLS_ASN1_OID;
449
450 return 0;
451
452error:
453 mbedtls_free(oid->p);
454 oid->p = NULL;
455 oid->len = 0;
456 return ret;
457}
458
459#endif /* MBEDTLS_OID_C */
460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200462{
David Horstmann8fd98d62023-06-27 15:17:44 +0100463 int ret = MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100464 int parse_ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +0100465 const char *s = name, *c = s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 const char *end = s + strlen(s);
Agathiyan Bragadeeshba386ec2023-08-16 11:31:17 +0100467 mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 const x509_attr_descriptor_t *attr_descr = NULL;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100469 int in_attr_type = 1;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100470 int tag;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100471 int numericoid = 0;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100472 unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
473 size_t data_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
475 /* Clear existing chain if present */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_asn1_free_named_data_list(head);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 while (c <= end) {
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100479 if (in_attr_type && *c == '=') {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000480 if ((attr_descr = x509_attr_descr_from_name(s, (size_t) (c - s))) == NULL) {
481 if ((mbedtls_oid_from_numeric_string(&oid, s, (size_t) (c - s))) != 0) {
Agathiyan Bragadeesh17984872023-08-11 12:42:03 +0100482 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100483 } else {
484 numericoid = 1;
485 }
486 } else {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100487 oid.len = strlen(attr_descr->oid);
488 oid.p = mbedtls_calloc(1, oid.len);
489 memcpy(oid.p, attr_descr->oid, oid.len);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100490 numericoid = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 }
492
493 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100494 in_attr_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 }
496
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100497 if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
Agathiyan Bragadeesh457ac842023-08-23 11:35:26 +0100498 if (s == c) {
Agathiyan Bragadeesh4c7d7bf2023-08-23 11:28:30 +0100499 mbedtls_free(oid.p);
500 return MBEDTLS_ERR_X509_INVALID_NAME;
501 } else if (*s == '#') {
Gilles Peskine70777812023-09-21 16:50:40 +0200502 /* We know that c >= s (loop invariant) and c != s (in this
503 * else branch), hence c - s - 1 >= 0. */
504 parse_ret = parse_attribute_value_hex_der_encoded(
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000505 s + 1, (size_t) (c - s) - 1,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200506 data, sizeof(data), &data_len, &tag);
Gilles Peskine70777812023-09-21 16:50:40 +0200507 if (parse_ret != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100508 mbedtls_free(oid.p);
Gilles Peskine391dd7f2023-09-21 18:51:35 +0200509 return parse_ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100510 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100511 } else {
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100512 if (numericoid) {
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100513 mbedtls_free(oid.p);
514 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100515 } else {
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100516 if ((parse_ret =
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100517 parse_attribute_value_string(s, (int) (c - s), data,
518 &data_len)) != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100519 mbedtls_free(oid.p);
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100520 return parse_ret;
521 }
522 tag = attr_descr->default_tag;
523 }
524 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 mbedtls_asn1_named_data *cur =
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100527 mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 (unsigned char *) data,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100529 data_len);
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100530 mbedtls_free(oid.p);
531 oid.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (cur == NULL) {
533 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 }
535
Jaeden Amero23f954d2018-05-17 11:46:13 +0100536 // set tagType
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100537 cur->val.tag = tag;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 while (c < end && *(c + 1) == ' ') {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540 c++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542
543 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100544 in_attr_type = 1;
David Horstmann8fd98d62023-06-27 15:17:44 +0100545
546 /* Successfully parsed one name, update ret to success */
547 ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 }
549 c++;
550 }
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100551 if (oid.p != NULL) {
552 mbedtls_free(oid.p);
Agathiyan Bragadeesh55d93192023-08-15 15:05:03 +0100553 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555}
556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 * to store the critical boolean for us
559 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
561 int critical, const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_asn1_named_data *cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564
Jonathan Winzig5caf20e2024-01-09 16:41:10 +0100565 if (val_len > (SIZE_MAX - 1)) {
Jonathan Winzig05c722b2024-01-09 15:20:03 +0100566 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
567 }
568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
570 NULL, val_len + 1)) == NULL) {
571 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572 }
573
574 cur->val.p[0] = critical;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 memcpy(cur->val.p + 1, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578}
579
580/*
581 * RelativeDistinguishedName ::=
582 * SET OF AttributeTypeAndValue
583 *
584 * AttributeTypeAndValue ::= SEQUENCE {
585 * type AttributeType,
586 * value AttributeValue }
587 *
588 * AttributeType ::= OBJECT IDENTIFIER
589 *
590 * AttributeValue ::= ANY DEFINED BY AttributeType
591 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592static int x509_write_name(unsigned char **p,
593 unsigned char *start,
594 mbedtls_asn1_named_data *cur_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595{
Janos Follath865b3eb2019-12-16 11:46:15 +0000596 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597 size_t len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 const char *oid = (const char *) cur_name->oid.p;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100599 size_t oid_len = cur_name->oid.len;
600 const unsigned char *name = cur_name->val.p;
601 size_t name_len = cur_name->val.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602
Jaeden Amero23f954d2018-05-17 11:46:13 +0100603 // Write correct string tag and value
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
605 cur_name->val.tag,
606 (const char *) name,
607 name_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608 // Write OID
609 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
611 oid_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
614 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
615 MBEDTLS_ASN1_CONSTRUCTED |
616 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
619 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
620 MBEDTLS_ASN1_CONSTRUCTED |
621 MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624}
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
627 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628{
Janos Follath865b3eb2019-12-16 11:46:15 +0000629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 mbedtls_asn1_named_data *cur = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 while (cur != NULL) {
634 MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 cur = cur->next;
636 }
637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
639 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
640 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643}
644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
646 const char *oid, size_t oid_len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100647 unsigned char *sig, size_t size,
648 mbedtls_pk_type_t pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649{
Janos Follath865b3eb2019-12-16 11:46:15 +0000650 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Marek Jansta8bde6492022-11-07 12:38:38 +0100651 int write_null_par;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652 size_t len = 0;
653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (*p < start || (size_t) (*p - start) < size) {
655 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
656 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
658 len = size;
659 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 memcpy(*p, sig, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (*p - start < 1) {
663 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
664 }
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200665
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 *--(*p) = 0;
667 len += 1;
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
670 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
672 // Write OID
673 //
Marek Jansta8bde6492022-11-07 12:38:38 +0100674 if (pk_alg == MBEDTLS_PK_ECDSA) {
675 /*
676 * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
677 * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
678 * https://www.rfc-editor.org/rfc/rfc5758#section-3.
679 */
680 write_null_par = 0;
681 } else {
682 write_null_par = 1;
683 }
684 MBEDTLS_ASN1_CHK_ADD(len,
685 mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
686 0, write_null_par));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689}
690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691static int x509_write_extension(unsigned char **p, unsigned char *start,
692 mbedtls_asn1_named_data *ext)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693{
Janos Follath865b3eb2019-12-16 11:46:15 +0000694 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 size_t len = 0;
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
698 ext->val.len - 1));
699 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
700 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (ext->val.p[0] != 0) {
703 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 }
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
707 ext->oid.len));
708 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
709 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
712 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
713 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716}
717
718/*
719 * Extension ::= SEQUENCE {
720 * extnID OBJECT IDENTIFIER,
721 * critical BOOLEAN DEFAULT FALSE,
722 * extnValue OCTET STRING
723 * -- contains the DER encoding of an ASN.1 value
724 * -- corresponding to the extension type identified
725 * -- by extnID
726 * }
727 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
729 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730{
Janos Follath865b3eb2019-12-16 11:46:15 +0000731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 mbedtls_asn1_named_data *cur_ext = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 while (cur_ext != NULL) {
736 MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 cur_ext = cur_ext->next;
738 }
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741}
742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743#endif /* MBEDTLS_X509_CREATE_C */