blob: 09ac69d00bbf537e1185cfabf056812937d6877f [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
Harry Ramsey0f6bc412024-10-04 10:36:54 +01008#include "x509_internal.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
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000013#include "mbedtls/error.h"
Gilles Peskinecd4c0d72025-05-07 23:45:12 +020014#include "mbedtls/oid.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +020015#include "x509_oid.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020016
Gilles Peskine1819a912025-07-22 21:54:50 +020017#include <limits.h>
Rich Evans00ab4702015-02-06 13:43:58 +000018#include <string.h>
19
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +010020#include "mbedtls/platform.h"
21
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +010022#include "mbedtls/asn1.h"
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +010023
Hanno Beckerd2c90092018-10-08 14:32:55 +010024/* Structure linking OIDs for X.509 DN AttributeTypes to their
25 * string representations and default string encodings used by Mbed TLS. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020026typedef struct {
Gilles Peskine449bd832023-01-11 14:50:10 +010027 const char *name; /* String representation of AttributeType, e.g.
28 * "CN" or "emailAddress". */
29 size_t name_len; /* Length of 'name', without trailing 0 byte. */
30 const char *oid; /* String representation of OID of AttributeType,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +010031 * as per RFC 5280, Appendix A.1. encoded as per
32 * X.690 */
Gilles Peskine449bd832023-01-11 14:50:10 +010033 int default_tag; /* The default character encoding used for the
Hanno Beckerd2c90092018-10-08 14:32:55 +010034 * given attribute type, e.g.
Hanno Beckeree334a32018-10-24 12:33:07 +010035 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020036} x509_attr_descriptor_t;
37
Gilles Peskine449bd832023-01-11 14:50:10 +010038#define ADD_STRLEN(s) s, sizeof(s) - 1
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020039
Hanno Becker35b68542018-10-08 14:47:38 +010040/* X.509 DN attributes from RFC 5280, Appendix A.1. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020041static const x509_attr_descriptor_t x509_attrs[] =
42{
Gilles Peskine449bd832023-01-11 14:50:10 +010043 { ADD_STRLEN("CN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010044 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010045 { ADD_STRLEN("commonName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010046 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010047 { ADD_STRLEN("C"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010048 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010049 { ADD_STRLEN("countryName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010050 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010051 { ADD_STRLEN("O"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010052 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010053 { ADD_STRLEN("organizationName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010054 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010055 { ADD_STRLEN("L"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010056 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010057 { ADD_STRLEN("locality"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010058 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010059 { ADD_STRLEN("R"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010060 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010061 { ADD_STRLEN("OU"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010062 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010063 { ADD_STRLEN("organizationalUnitName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010064 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010065 { ADD_STRLEN("ST"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010066 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010067 { ADD_STRLEN("stateOrProvinceName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010068 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010069 { ADD_STRLEN("emailAddress"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010070 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010071 { ADD_STRLEN("serialNumber"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010072 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010073 { ADD_STRLEN("postalAddress"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010074 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010075 { ADD_STRLEN("postalCode"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010076 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010077 { ADD_STRLEN("dnQualifier"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010078 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010079 { ADD_STRLEN("title"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010080 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010081 { ADD_STRLEN("surName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010082 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010083 { ADD_STRLEN("SN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010084 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010085 { ADD_STRLEN("givenName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010086 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010087 { ADD_STRLEN("GN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010088 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010089 { ADD_STRLEN("initials"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010090 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010091 { ADD_STRLEN("pseudonym"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010092 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010093 { ADD_STRLEN("generationQualifier"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010094 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010095 { ADD_STRLEN("domainComponent"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010096 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010097 { ADD_STRLEN("DC"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010098 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
tdoec150f0d2018-05-18 12:12:45 +020099 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200100};
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102static 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 +0200103{
104 const x509_attr_descriptor_t *cur;
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 for (cur = x509_attrs; cur->name != NULL; cur++) {
107 if (cur->name_len == name_len &&
108 strncmp(cur->name, name, name_len) == 0) {
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200109 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 }
111 }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (cur->name == NULL) {
114 return NULL;
115 }
Hanno Beckerd2c90092018-10-08 14:32:55 +0100116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return cur;
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200118}
119
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100120static int hex_to_int(char c)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100121{
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100122 return ('0' <= c && c <= '9') ? (c - '0') :
123 ('a' <= c && c <= 'f') ? (c - 'a' + 10) :
124 ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
125}
126
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100127static int hexpair_to_int(const char *hexpair)
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100128{
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100129 int n1 = hex_to_int(*hexpair);
130 int n2 = hex_to_int(*(hexpair + 1));
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100131
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100132 if (n1 != -1 && n2 != -1) {
133 return (n1 << 4) | n2;
134 } else {
135 return -1;
136 }
137}
138
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100139static int parse_attribute_value_string(const char *s,
140 int len,
141 unsigned char *data,
142 size_t *data_len)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100143{
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100144 const char *c;
145 const char *end = s + len;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100146 unsigned char *d = data;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100147 int n;
Agathiyan Bragadeesha2423de2023-08-30 16:24:31 +0100148
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100149 for (c = s; c < end; c++) {
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100150 if (*c == '\\') {
151 c++;
152
Agathiyan Bragadeeshe9d1c8e2023-08-30 15:50:12 +0100153 /* Check for valid escaped characters as per RFC 4514 Section 3 */
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100154 if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100155 if (n == 0) {
Agathiyan Bragadeesh9caaa6d2023-08-14 15:38:39 +0100156 return MBEDTLS_ERR_X509_INVALID_NAME;
157 }
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100158 *(d++) = n;
159 c++;
Agathiyan Bragadeeshc34804d2023-09-08 11:32:19 +0100160 } else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
161 *(d++) = *c;
162 } else {
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100163 return MBEDTLS_ERR_X509_INVALID_NAME;
164 }
Agathiyan Bragadeesh706a1c32023-09-08 12:04:41 +0100165 } else {
Agathiyan Bragadeeshc34804d2023-09-08 11:32:19 +0100166 *(d++) = *c;
167 }
Agathiyan Bragadeesha2423de2023-08-30 16:24:31 +0100168
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100169 if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
170 return MBEDTLS_ERR_X509_INVALID_NAME;
171 }
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100172 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000173 *data_len = (size_t) (d - data);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100174 return 0;
175}
176
Gilles Peskine25665782023-09-21 14:03:52 +0200177/** Parse a hexstring containing a DER-encoded string.
178 *
179 * \param s A string of \p len bytes hexadecimal digits.
180 * \param len Number of bytes to read from \p s.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200181 * \param data Output buffer of size \p data_size.
Gilles Peskine25665782023-09-21 14:03:52 +0200182 * On success, it contains the payload that's DER-encoded
183 * in the input (content without the tag and length).
184 * If the DER tag is a string tag, the payload is guaranteed
185 * not to contain null bytes.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200186 * \param data_size Length of the \p data buffer.
Gilles Peskine25665782023-09-21 14:03:52 +0200187 * \param data_len On success, the length of the parsed string.
188 * It is guaranteed to be less than
189 * #MBEDTLS_X509_MAX_DN_NAME_SIZE.
190 * \param tag The ASN.1 tag that the payload in \p data is encoded in.
191 *
192 * \retval 0 on success.
193 * \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
194 * a valid hexstring,
195 * or if the decoded hexstring is not valid DER,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200196 * or if the payload does not fit in \p data,
197 * or if the payload is more than
198 * #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes,
Gilles Peskine25665782023-09-21 14:03:52 +0200199 * of if \p *tag is an ASN.1 string tag and the payload
200 * contains a null byte.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200201 * \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory.
Gilles Peskine25665782023-09-21 14:03:52 +0200202 */
203static int parse_attribute_value_hex_der_encoded(const char *s,
Gilles Peskine70777812023-09-21 16:50:40 +0200204 size_t len,
Gilles Peskine25665782023-09-21 14:03:52 +0200205 unsigned char *data,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200206 size_t data_size,
Gilles Peskine25665782023-09-21 14:03:52 +0200207 size_t *data_len,
208 int *tag)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100209{
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200210 /* Step 1: preliminary length checks. */
Gilles Peskine25665782023-09-21 14:03:52 +0200211 /* Each byte is encoded by exactly two hexadecimal digits. */
212 if (len % 2 != 0) {
213 /* Odd number of hex digits */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100214 return MBEDTLS_ERR_X509_INVALID_NAME;
215 }
Gilles Peskine25665782023-09-21 14:03:52 +0200216 size_t const der_length = len / 2;
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200217 if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) {
218 /* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE
219 * (after subtracting the ASN.1 tag and length). Reject this early
220 * to avoid allocating a large intermediate buffer. */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100221 return MBEDTLS_ERR_X509_INVALID_NAME;
222 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200223 if (der_length < 1) {
224 /* Avoid empty-buffer shenanigans. A valid DER encoding is never
225 * empty. */
226 return MBEDTLS_ERR_X509_INVALID_NAME;
227 }
228
229 /* Step 2: Decode the hex string into an intermediate buffer. */
230 unsigned char *der = mbedtls_calloc(1, der_length);
231 if (der == NULL) {
232 return MBEDTLS_ERR_X509_ALLOC_FAILED;
233 }
234 /* Beyond this point, der needs to be freed on exit. */
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) {
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200238 goto error;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100239 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200240 der[i] = c;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100241 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100242
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200243 /* Step 3: decode the DER. */
244 /* We've checked that der_length >= 1 above. */
245 *tag = der[0];
Dave Rodgman515af1d2023-10-13 14:40:14 +0100246 {
247 unsigned char *p = der + 1;
248 if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) {
249 goto error;
250 }
251 /* Now p points to the first byte of the payload inside der,
252 * and *data_len is the length of the payload. */
Gilles Peskine25665782023-09-21 14:03:52 +0200253
Dave Rodgman515af1d2023-10-13 14:40:14 +0100254 /* Step 4: payload validation */
255 if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
256 goto error;
257 }
258 /* Strings must not contain null bytes. */
259 if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
260 for (size_t i = 0; i < *data_len; i++) {
261 if (p[i] == 0) {
262 goto error;
263 }
Gilles Peskine25665782023-09-21 14:03:52 +0200264 }
265 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100266
Dave Rodgman515af1d2023-10-13 14:40:14 +0100267 /* Step 5: output the payload. */
268 if (*data_len > data_size) {
269 goto error;
270 }
271 memcpy(data, p, *data_len);
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200272 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200273 mbedtls_free(der);
274
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100275 return 0;
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200276
277error:
278 mbedtls_free(der);
279 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100280}
281
Sam Berry3da783b2024-09-13 15:09:24 +0100282static int oid_parse_number(unsigned int *num, const char **p, const char *bound)
283{
284 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
285
286 *num = 0;
287
288 while (*p < bound && **p >= '0' && **p <= '9') {
289 ret = 0;
290 if (*num > (UINT_MAX / 10)) {
291 return MBEDTLS_ERR_ASN1_INVALID_DATA;
292 }
293 *num *= 10;
294 *num += **p - '0';
295 (*p)++;
296 }
297 return ret;
298}
299
300static size_t oid_subidentifier_num_bytes(unsigned int value)
301{
302 size_t num_bytes = 0;
303
304 do {
305 value >>= 7;
306 num_bytes++;
307 } while (value != 0);
308
309 return num_bytes;
310}
311
312static int oid_subidentifier_encode_into(unsigned char **p,
313 unsigned char *bound,
314 unsigned int value)
315{
316 size_t num_bytes = oid_subidentifier_num_bytes(value);
317
318 if ((size_t) (bound - *p) < num_bytes) {
Gilles Peskine47f1d7b2025-05-07 21:04:51 +0200319 return PSA_ERROR_BUFFER_TOO_SMALL;
Sam Berry3da783b2024-09-13 15:09:24 +0100320 }
321 (*p)[num_bytes - 1] = (unsigned char) (value & 0x7f);
322 value >>= 7;
323
324 for (size_t i = 2; i <= num_bytes; i++) {
325 (*p)[num_bytes - i] = 0x80 | (unsigned char) (value & 0x7f);
326 value >>= 7;
327 }
328 *p += num_bytes;
329
330 return 0;
331}
332
Sam Berryc71abc32024-07-19 15:11:10 +0100333/* Return the OID for the given x.y.z.... style numeric string */
334int mbedtls_oid_from_numeric_string(mbedtls_asn1_buf *oid,
335 const char *oid_str, size_t size)
336{
337 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
338 const char *str_ptr = oid_str;
339 const char *str_bound = oid_str + size;
340 unsigned int val = 0;
341 unsigned int component1, component2;
342 size_t encoded_len;
343 unsigned char *resized_mem;
344
345 /* Count the number of dots to get a worst-case allocation size. */
346 size_t num_dots = 0;
347 for (size_t i = 0; i < size; i++) {
348 if (oid_str[i] == '.') {
349 num_dots++;
350 }
351 }
352 /* Allocate maximum possible required memory:
353 * There are (num_dots + 1) integer components, but the first 2 share the
354 * same subidentifier, so we only need num_dots subidentifiers maximum. */
355 if (num_dots == 0 || (num_dots > MBEDTLS_OID_MAX_COMPONENTS - 1)) {
356 return MBEDTLS_ERR_ASN1_INVALID_DATA;
357 }
358 /* Each byte can store 7 bits, calculate number of bytes for a
359 * subidentifier:
360 *
361 * bytes = ceil(subidentifer_size * 8 / 7)
362 */
363 size_t bytes_per_subidentifier = (((sizeof(unsigned int) * 8) - 1) / 7)
364 + 1;
365 size_t max_possible_bytes = num_dots * bytes_per_subidentifier;
366 oid->p = mbedtls_calloc(max_possible_bytes, 1);
367 if (oid->p == NULL) {
368 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
369 }
370 unsigned char *out_ptr = oid->p;
371 unsigned char *out_bound = oid->p + max_possible_bytes;
372
373 ret = oid_parse_number(&component1, &str_ptr, str_bound);
374 if (ret != 0) {
375 goto error;
376 }
377 if (component1 > 2) {
378 /* First component can't be > 2 */
379 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
380 goto error;
381 }
382 if (str_ptr >= str_bound || *str_ptr != '.') {
383 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
384 goto error;
385 }
386 str_ptr++;
387
388 ret = oid_parse_number(&component2, &str_ptr, str_bound);
389 if (ret != 0) {
390 goto error;
391 }
392 if ((component1 < 2) && (component2 > 39)) {
393 /* Root nodes 0 and 1 may have up to 40 children, numbered 0-39 */
394 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
395 goto error;
396 }
397 if (str_ptr < str_bound) {
398 if (*str_ptr == '.') {
399 str_ptr++;
400 } else {
401 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
402 goto error;
403 }
404 }
405
406 if (component2 > (UINT_MAX - (component1 * 40))) {
407 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
408 goto error;
409 }
410 ret = oid_subidentifier_encode_into(&out_ptr, out_bound,
411 (component1 * 40) + component2);
412 if (ret != 0) {
413 goto error;
414 }
415
416 while (str_ptr < str_bound) {
417 ret = oid_parse_number(&val, &str_ptr, str_bound);
418 if (ret != 0) {
419 goto error;
420 }
421 if (str_ptr < str_bound) {
422 if (*str_ptr == '.') {
423 str_ptr++;
424 } else {
425 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
426 goto error;
427 }
428 }
429
430 ret = oid_subidentifier_encode_into(&out_ptr, out_bound, val);
431 if (ret != 0) {
432 goto error;
433 }
434 }
435
436 encoded_len = (size_t) (out_ptr - oid->p);
437 resized_mem = mbedtls_calloc(encoded_len, 1);
438 if (resized_mem == NULL) {
439 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
440 goto error;
441 }
442 memcpy(resized_mem, oid->p, encoded_len);
443 mbedtls_free(oid->p);
444 oid->p = resized_mem;
445 oid->len = encoded_len;
446
447 oid->tag = MBEDTLS_ASN1_OID;
448
449 return 0;
450
451error:
452 mbedtls_free(oid->p);
453 oid->p = NULL;
454 oid->len = 0;
455 return ret;
456}
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459{
David Horstmann8fd98d62023-06-27 15:17:44 +0100460 int ret = MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100461 int parse_ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +0100462 const char *s = name, *c = s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 const char *end = s + strlen(s);
Agathiyan Bragadeeshba386ec2023-08-16 11:31:17 +0100464 mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 const x509_attr_descriptor_t *attr_descr = NULL;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100466 int in_attr_type = 1;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100467 int tag;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100468 int numericoid = 0;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100469 unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
470 size_t data_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Manuel Pégourié-Gonnard43a1e732025-05-05 16:41:52 +0200472 /* Ensure the output parameter is not already populated.
473 * (If it were, overwriting it would likely cause a memory leak.)
474 */
475 if (*head != NULL) {
476 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
477 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 while (c <= end) {
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100480 if (in_attr_type && *c == '=') {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000481 if ((attr_descr = x509_attr_descr_from_name(s, (size_t) (c - s))) == NULL) {
482 if ((mbedtls_oid_from_numeric_string(&oid, s, (size_t) (c - s))) != 0) {
Agathiyan Bragadeesh17984872023-08-11 12:42:03 +0100483 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100484 } else {
485 numericoid = 1;
486 }
487 } else {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100488 oid.len = strlen(attr_descr->oid);
489 oid.p = mbedtls_calloc(1, oid.len);
490 memcpy(oid.p, attr_descr->oid, oid.len);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100491 numericoid = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 }
493
494 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100495 in_attr_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496 }
497
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100498 if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
Agathiyan Bragadeesh457ac842023-08-23 11:35:26 +0100499 if (s == c) {
Agathiyan Bragadeesh4c7d7bf2023-08-23 11:28:30 +0100500 mbedtls_free(oid.p);
501 return MBEDTLS_ERR_X509_INVALID_NAME;
502 } else if (*s == '#') {
Gilles Peskine70777812023-09-21 16:50:40 +0200503 /* We know that c >= s (loop invariant) and c != s (in this
504 * else branch), hence c - s - 1 >= 0. */
505 parse_ret = parse_attribute_value_hex_der_encoded(
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000506 s + 1, (size_t) (c - s) - 1,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200507 data, sizeof(data), &data_len, &tag);
Gilles Peskine70777812023-09-21 16:50:40 +0200508 if (parse_ret != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100509 mbedtls_free(oid.p);
Gilles Peskine391dd7f2023-09-21 18:51:35 +0200510 return parse_ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100511 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100512 } else {
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100513 if (numericoid) {
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100514 mbedtls_free(oid.p);
515 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100516 } else {
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100517 if ((parse_ret =
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100518 parse_attribute_value_string(s, (int) (c - s), data,
519 &data_len)) != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100520 mbedtls_free(oid.p);
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100521 return parse_ret;
522 }
523 tag = attr_descr->default_tag;
524 }
525 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 mbedtls_asn1_named_data *cur =
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100528 mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 (unsigned char *) data,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100530 data_len);
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100531 mbedtls_free(oid.p);
532 oid.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (cur == NULL) {
534 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 }
536
Jaeden Amero23f954d2018-05-17 11:46:13 +0100537 // set tagType
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100538 cur->val.tag = tag;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 while (c < end && *(c + 1) == ' ') {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541 c++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
544 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100545 in_attr_type = 1;
David Horstmann8fd98d62023-06-27 15:17:44 +0100546
547 /* Successfully parsed one name, update ret to success */
548 ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549 }
550 c++;
551 }
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100552 if (oid.p != NULL) {
553 mbedtls_free(oid.p);
Agathiyan Bragadeesh55d93192023-08-15 15:05:03 +0100554 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556}
557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559 * to store the critical boolean for us
560 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
562 int critical, const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_asn1_named_data *cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565
Jonathan Winzig5caf20e2024-01-09 16:41:10 +0100566 if (val_len > (SIZE_MAX - 1)) {
Jonathan Winzig05c722b2024-01-09 15:20:03 +0100567 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
568 }
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
571 NULL, val_len + 1)) == NULL) {
572 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573 }
574
575 cur->val.p[0] = critical;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 memcpy(cur->val.p + 1, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579}
580
581/*
582 * RelativeDistinguishedName ::=
583 * SET OF AttributeTypeAndValue
584 *
585 * AttributeTypeAndValue ::= SEQUENCE {
586 * type AttributeType,
587 * value AttributeValue }
588 *
589 * AttributeType ::= OBJECT IDENTIFIER
590 *
591 * AttributeValue ::= ANY DEFINED BY AttributeType
592 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100593static int x509_write_name(unsigned char **p,
594 unsigned char *start,
595 mbedtls_asn1_named_data *cur_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596{
Janos Follath865b3eb2019-12-16 11:46:15 +0000597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598 size_t len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 const char *oid = (const char *) cur_name->oid.p;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100600 size_t oid_len = cur_name->oid.len;
601 const unsigned char *name = cur_name->val.p;
602 size_t name_len = cur_name->val.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603
Jaeden Amero23f954d2018-05-17 11:46:13 +0100604 // Write correct string tag and value
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
606 cur_name->val.tag,
607 (const char *) name,
608 name_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609 // Write OID
610 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
612 oid_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
615 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
616 MBEDTLS_ASN1_CONSTRUCTED |
617 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
620 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
621 MBEDTLS_ASN1_CONSTRUCTED |
622 MBEDTLS_ASN1_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625}
626
Gilles Peskine449bd832023-01-11 14:50:10 +0100627int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
628 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629{
Janos Follath865b3eb2019-12-16 11:46:15 +0000630 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_asn1_named_data *cur = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 while (cur != NULL) {
635 MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 cur = cur->next;
637 }
638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
640 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
641 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644}
645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
647 const char *oid, size_t oid_len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100648 unsigned char *sig, size_t size,
649 mbedtls_pk_type_t pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650{
Janos Follath865b3eb2019-12-16 11:46:15 +0000651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Marek Jansta8bde6492022-11-07 12:38:38 +0100652 int write_null_par;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653 size_t len = 0;
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (*p < start || (size_t) (*p - start) < size) {
656 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
657 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658
659 len = size;
660 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 memcpy(*p, sig, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if (*p - start < 1) {
664 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
665 }
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200666
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 *--(*p) = 0;
668 len += 1;
669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
671 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
673 // Write OID
674 //
Marek Jansta8bde6492022-11-07 12:38:38 +0100675 if (pk_alg == MBEDTLS_PK_ECDSA) {
676 /*
677 * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
678 * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
679 * https://www.rfc-editor.org/rfc/rfc5758#section-3.
680 */
681 write_null_par = 0;
682 } else {
683 write_null_par = 1;
684 }
685 MBEDTLS_ASN1_CHK_ADD(len,
686 mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
687 0, write_null_par));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690}
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692static int x509_write_extension(unsigned char **p, unsigned char *start,
693 mbedtls_asn1_named_data *ext)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694{
Janos Follath865b3eb2019-12-16 11:46:15 +0000695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696 size_t len = 0;
697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
699 ext->val.len - 1));
700 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
701 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (ext->val.p[0] != 0) {
704 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 }
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
708 ext->oid.len));
709 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
710 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
713 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
714 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717}
718
719/*
720 * Extension ::= SEQUENCE {
721 * extnID OBJECT IDENTIFIER,
722 * critical BOOLEAN DEFAULT FALSE,
723 * extnValue OCTET STRING
724 * -- contains the DER encoding of an ASN.1 value
725 * -- corresponding to the extension type identified
726 * -- by extnID
727 * }
728 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
730 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731{
Janos Follath865b3eb2019-12-16 11:46:15 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_asn1_named_data *cur_ext = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 while (cur_ext != NULL) {
737 MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 cur_ext = cur_ext->next;
739 }
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742}
743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744#endif /* MBEDTLS_X509_CREATE_C */