blob: 48ac080cbeb656b3ec009923810b75da06616210 [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"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/oid.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020015
Rich Evans00ab4702015-02-06 13:43:58 +000016#include <string.h>
17
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +010018#include "mbedtls/platform.h"
19
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +010020#include "mbedtls/asn1.h"
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +010021
Hanno Beckerd2c90092018-10-08 14:32:55 +010022/* Structure linking OIDs for X.509 DN AttributeTypes to their
23 * string representations and default string encodings used by Mbed TLS. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020024typedef struct {
Gilles Peskine449bd832023-01-11 14:50:10 +010025 const char *name; /* String representation of AttributeType, e.g.
26 * "CN" or "emailAddress". */
27 size_t name_len; /* Length of 'name', without trailing 0 byte. */
28 const char *oid; /* String representation of OID of AttributeType,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +010029 * as per RFC 5280, Appendix A.1. encoded as per
30 * X.690 */
Gilles Peskine449bd832023-01-11 14:50:10 +010031 int default_tag; /* The default character encoding used for the
Hanno Beckerd2c90092018-10-08 14:32:55 +010032 * given attribute type, e.g.
Hanno Beckeree334a32018-10-24 12:33:07 +010033 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020034} x509_attr_descriptor_t;
35
Gilles Peskine449bd832023-01-11 14:50:10 +010036#define ADD_STRLEN(s) s, sizeof(s) - 1
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020037
Hanno Becker35b68542018-10-08 14:47:38 +010038/* X.509 DN attributes from RFC 5280, Appendix A.1. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020039static const x509_attr_descriptor_t x509_attrs[] =
40{
Gilles Peskine449bd832023-01-11 14:50:10 +010041 { ADD_STRLEN("CN"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010042 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010043 { ADD_STRLEN("commonName"),
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("C"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010046 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010047 { ADD_STRLEN("countryName"),
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("O"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010050 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010051 { ADD_STRLEN("organizationName"),
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("L"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010054 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010055 { ADD_STRLEN("locality"),
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("R"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010058 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010059 { ADD_STRLEN("OU"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010060 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010061 { ADD_STRLEN("organizationalUnitName"),
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("ST"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010064 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010065 { ADD_STRLEN("stateOrProvinceName"),
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("emailAddress"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010068 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010069 { ADD_STRLEN("serialNumber"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010070 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010071 { ADD_STRLEN("postalAddress"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010072 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010073 { ADD_STRLEN("postalCode"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010074 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010075 { ADD_STRLEN("dnQualifier"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010076 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010077 { ADD_STRLEN("title"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010078 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010079 { ADD_STRLEN("surName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010080 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010081 { ADD_STRLEN("SN"),
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("givenName"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010084 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010085 { ADD_STRLEN("GN"),
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("initials"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010088 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010089 { ADD_STRLEN("pseudonym"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010090 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010091 { ADD_STRLEN("generationQualifier"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010092 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010093 { ADD_STRLEN("domainComponent"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010094 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
Gilles Peskine449bd832023-01-11 14:50:10 +010095 { ADD_STRLEN("DC"),
Hanno Becker1624e2e2018-10-08 14:52:20 +010096 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
tdoec150f0d2018-05-18 12:12:45 +020097 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020098};
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100static 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 +0200101{
102 const x509_attr_descriptor_t *cur;
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 for (cur = x509_attrs; cur->name != NULL; cur++) {
105 if (cur->name_len == name_len &&
106 strncmp(cur->name, name, name_len) == 0) {
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200107 break;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 }
109 }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 if (cur->name == NULL) {
112 return NULL;
113 }
Hanno Beckerd2c90092018-10-08 14:32:55 +0100114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return cur;
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200116}
117
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100118static int hex_to_int(char c)
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100119{
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100120 return ('0' <= c && c <= '9') ? (c - '0') :
121 ('a' <= c && c <= 'f') ? (c - 'a' + 10) :
122 ('A' <= c && c <= 'F') ? (c - 'A' + 10) : -1;
123}
124
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100125static int hexpair_to_int(const char *hexpair)
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100126{
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100127 int n1 = hex_to_int(*hexpair);
128 int n2 = hex_to_int(*(hexpair + 1));
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100129
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100130 if (n1 != -1 && n2 != -1) {
131 return (n1 << 4) | n2;
132 } else {
133 return -1;
134 }
135}
136
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100137static int parse_attribute_value_string(const char *s,
138 int len,
139 unsigned char *data,
140 size_t *data_len)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100141{
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100142 const char *c;
143 const char *end = s + len;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100144 unsigned char *d = data;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100145 int n;
Agathiyan Bragadeesha2423de2023-08-30 16:24:31 +0100146
Agathiyan Bragadeeshde02ee22023-08-30 16:12:57 +0100147 for (c = s; c < end; c++) {
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100148 if (*c == '\\') {
149 c++;
150
Agathiyan Bragadeeshe9d1c8e2023-08-30 15:50:12 +0100151 /* Check for valid escaped characters as per RFC 4514 Section 3 */
Agathiyan Bragadeesh1aece472023-08-30 16:04:16 +0100152 if (c + 1 < end && (n = hexpair_to_int(c)) != -1) {
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100153 if (n == 0) {
Agathiyan Bragadeesh9caaa6d2023-08-14 15:38:39 +0100154 return MBEDTLS_ERR_X509_INVALID_NAME;
155 }
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100156 *(d++) = n;
157 c++;
Agathiyan Bragadeeshc34804d2023-09-08 11:32:19 +0100158 } else if (c < end && strchr(" ,=+<>#;\"\\", *c)) {
159 *(d++) = *c;
160 } else {
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100161 return MBEDTLS_ERR_X509_INVALID_NAME;
162 }
Agathiyan Bragadeesh706a1c32023-09-08 12:04:41 +0100163 } else {
Agathiyan Bragadeeshc34804d2023-09-08 11:32:19 +0100164 *(d++) = *c;
165 }
Agathiyan Bragadeesha2423de2023-08-30 16:24:31 +0100166
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100167 if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
168 return MBEDTLS_ERR_X509_INVALID_NAME;
169 }
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100170 }
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000171 *data_len = (size_t) (d - data);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100172 return 0;
173}
174
Gilles Peskine25665782023-09-21 14:03:52 +0200175/** Parse a hexstring containing a DER-encoded string.
176 *
177 * \param s A string of \p len bytes hexadecimal digits.
178 * \param len Number of bytes to read from \p s.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200179 * \param data Output buffer of size \p data_size.
Gilles Peskine25665782023-09-21 14:03:52 +0200180 * On success, it contains the payload that's DER-encoded
181 * in the input (content without the tag and length).
182 * If the DER tag is a string tag, the payload is guaranteed
183 * not to contain null bytes.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200184 * \param data_size Length of the \p data buffer.
Gilles Peskine25665782023-09-21 14:03:52 +0200185 * \param data_len On success, the length of the parsed string.
186 * It is guaranteed to be less than
187 * #MBEDTLS_X509_MAX_DN_NAME_SIZE.
188 * \param tag The ASN.1 tag that the payload in \p data is encoded in.
189 *
190 * \retval 0 on success.
191 * \retval #MBEDTLS_ERR_X509_INVALID_NAME if \p s does not contain
192 * a valid hexstring,
193 * or if the decoded hexstring is not valid DER,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200194 * or if the payload does not fit in \p data,
195 * or if the payload is more than
196 * #MBEDTLS_X509_MAX_DN_NAME_SIZE bytes,
Gilles Peskine25665782023-09-21 14:03:52 +0200197 * of if \p *tag is an ASN.1 string tag and the payload
198 * contains a null byte.
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200199 * \retval #MBEDTLS_ERR_X509_ALLOC_FAILED on low memory.
Gilles Peskine25665782023-09-21 14:03:52 +0200200 */
201static int parse_attribute_value_hex_der_encoded(const char *s,
Gilles Peskine70777812023-09-21 16:50:40 +0200202 size_t len,
Gilles Peskine25665782023-09-21 14:03:52 +0200203 unsigned char *data,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200204 size_t data_size,
Gilles Peskine25665782023-09-21 14:03:52 +0200205 size_t *data_len,
206 int *tag)
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100207{
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200208 /* Step 1: preliminary length checks. */
Gilles Peskine25665782023-09-21 14:03:52 +0200209 /* Each byte is encoded by exactly two hexadecimal digits. */
210 if (len % 2 != 0) {
211 /* Odd number of hex digits */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100212 return MBEDTLS_ERR_X509_INVALID_NAME;
213 }
Gilles Peskine25665782023-09-21 14:03:52 +0200214 size_t const der_length = len / 2;
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200215 if (der_length > MBEDTLS_X509_MAX_DN_NAME_SIZE + 4) {
216 /* The payload would be more than MBEDTLS_X509_MAX_DN_NAME_SIZE
217 * (after subtracting the ASN.1 tag and length). Reject this early
218 * to avoid allocating a large intermediate buffer. */
Agathiyan Bragadeesh4987c8f2023-08-01 11:10:52 +0100219 return MBEDTLS_ERR_X509_INVALID_NAME;
220 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200221 if (der_length < 1) {
222 /* Avoid empty-buffer shenanigans. A valid DER encoding is never
223 * empty. */
224 return MBEDTLS_ERR_X509_INVALID_NAME;
225 }
226
227 /* Step 2: Decode the hex string into an intermediate buffer. */
228 unsigned char *der = mbedtls_calloc(1, der_length);
229 if (der == NULL) {
230 return MBEDTLS_ERR_X509_ALLOC_FAILED;
231 }
232 /* Beyond this point, der needs to be freed on exit. */
Gilles Peskine25665782023-09-21 14:03:52 +0200233 for (size_t i = 0; i < der_length; i++) {
234 int c = hexpair_to_int(s + 2 * i);
235 if (c < 0) {
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200236 goto error;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100237 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200238 der[i] = c;
Agathiyan Bragadeeshb73778d2023-07-26 11:55:31 +0100239 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100240
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200241 /* Step 3: decode the DER. */
242 /* We've checked that der_length >= 1 above. */
243 *tag = der[0];
Dave Rodgman515af1d2023-10-13 14:40:14 +0100244 {
245 unsigned char *p = der + 1;
246 if (mbedtls_asn1_get_len(&p, der + der_length, data_len) != 0) {
247 goto error;
248 }
249 /* Now p points to the first byte of the payload inside der,
250 * and *data_len is the length of the payload. */
Gilles Peskine25665782023-09-21 14:03:52 +0200251
Dave Rodgman515af1d2023-10-13 14:40:14 +0100252 /* Step 4: payload validation */
253 if (*data_len > MBEDTLS_X509_MAX_DN_NAME_SIZE) {
254 goto error;
255 }
256 /* Strings must not contain null bytes. */
257 if (MBEDTLS_ASN1_IS_STRING_TAG(*tag)) {
258 for (size_t i = 0; i < *data_len; i++) {
259 if (p[i] == 0) {
260 goto error;
261 }
Gilles Peskine25665782023-09-21 14:03:52 +0200262 }
263 }
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100264
Dave Rodgman515af1d2023-10-13 14:40:14 +0100265 /* Step 5: output the payload. */
266 if (*data_len > data_size) {
267 goto error;
268 }
269 memcpy(data, p, *data_len);
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200270 }
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200271 mbedtls_free(der);
272
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100273 return 0;
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200274
275error:
276 mbedtls_free(der);
277 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshef2decb2023-07-21 15:47:47 +0100278}
279
Sam Berry3da783b2024-09-13 15:09:24 +0100280static int oid_parse_number(unsigned int *num, const char **p, const char *bound)
281{
282 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
283
284 *num = 0;
285
286 while (*p < bound && **p >= '0' && **p <= '9') {
287 ret = 0;
288 if (*num > (UINT_MAX / 10)) {
289 return MBEDTLS_ERR_ASN1_INVALID_DATA;
290 }
291 *num *= 10;
292 *num += **p - '0';
293 (*p)++;
294 }
295 return ret;
296}
297
298static size_t oid_subidentifier_num_bytes(unsigned int value)
299{
300 size_t num_bytes = 0;
301
302 do {
303 value >>= 7;
304 num_bytes++;
305 } while (value != 0);
306
307 return num_bytes;
308}
309
310static int oid_subidentifier_encode_into(unsigned char **p,
311 unsigned char *bound,
312 unsigned int value)
313{
314 size_t num_bytes = oid_subidentifier_num_bytes(value);
315
316 if ((size_t) (bound - *p) < num_bytes) {
317 return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
318 }
319 (*p)[num_bytes - 1] = (unsigned char) (value & 0x7f);
320 value >>= 7;
321
322 for (size_t i = 2; i <= num_bytes; i++) {
323 (*p)[num_bytes - i] = 0x80 | (unsigned char) (value & 0x7f);
324 value >>= 7;
325 }
326 *p += num_bytes;
327
328 return 0;
329}
330
Sam Berryc71abc32024-07-19 15:11:10 +0100331/* Return the OID for the given x.y.z.... style numeric string */
332int mbedtls_oid_from_numeric_string(mbedtls_asn1_buf *oid,
333 const char *oid_str, size_t size)
334{
335 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
336 const char *str_ptr = oid_str;
337 const char *str_bound = oid_str + size;
338 unsigned int val = 0;
339 unsigned int component1, component2;
340 size_t encoded_len;
341 unsigned char *resized_mem;
342
343 /* Count the number of dots to get a worst-case allocation size. */
344 size_t num_dots = 0;
345 for (size_t i = 0; i < size; i++) {
346 if (oid_str[i] == '.') {
347 num_dots++;
348 }
349 }
350 /* Allocate maximum possible required memory:
351 * There are (num_dots + 1) integer components, but the first 2 share the
352 * same subidentifier, so we only need num_dots subidentifiers maximum. */
353 if (num_dots == 0 || (num_dots > MBEDTLS_OID_MAX_COMPONENTS - 1)) {
354 return MBEDTLS_ERR_ASN1_INVALID_DATA;
355 }
356 /* Each byte can store 7 bits, calculate number of bytes for a
357 * subidentifier:
358 *
359 * bytes = ceil(subidentifer_size * 8 / 7)
360 */
361 size_t bytes_per_subidentifier = (((sizeof(unsigned int) * 8) - 1) / 7)
362 + 1;
363 size_t max_possible_bytes = num_dots * bytes_per_subidentifier;
364 oid->p = mbedtls_calloc(max_possible_bytes, 1);
365 if (oid->p == NULL) {
366 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
367 }
368 unsigned char *out_ptr = oid->p;
369 unsigned char *out_bound = oid->p + max_possible_bytes;
370
371 ret = oid_parse_number(&component1, &str_ptr, str_bound);
372 if (ret != 0) {
373 goto error;
374 }
375 if (component1 > 2) {
376 /* First component can't be > 2 */
377 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
378 goto error;
379 }
380 if (str_ptr >= str_bound || *str_ptr != '.') {
381 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
382 goto error;
383 }
384 str_ptr++;
385
386 ret = oid_parse_number(&component2, &str_ptr, str_bound);
387 if (ret != 0) {
388 goto error;
389 }
390 if ((component1 < 2) && (component2 > 39)) {
391 /* Root nodes 0 and 1 may have up to 40 children, numbered 0-39 */
392 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
393 goto error;
394 }
395 if (str_ptr < str_bound) {
396 if (*str_ptr == '.') {
397 str_ptr++;
398 } else {
399 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
400 goto error;
401 }
402 }
403
404 if (component2 > (UINT_MAX - (component1 * 40))) {
405 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
406 goto error;
407 }
408 ret = oid_subidentifier_encode_into(&out_ptr, out_bound,
409 (component1 * 40) + component2);
410 if (ret != 0) {
411 goto error;
412 }
413
414 while (str_ptr < str_bound) {
415 ret = oid_parse_number(&val, &str_ptr, str_bound);
416 if (ret != 0) {
417 goto error;
418 }
419 if (str_ptr < str_bound) {
420 if (*str_ptr == '.') {
421 str_ptr++;
422 } else {
423 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
424 goto error;
425 }
426 }
427
428 ret = oid_subidentifier_encode_into(&out_ptr, out_bound, val);
429 if (ret != 0) {
430 goto error;
431 }
432 }
433
434 encoded_len = (size_t) (out_ptr - oid->p);
435 resized_mem = mbedtls_calloc(encoded_len, 1);
436 if (resized_mem == NULL) {
437 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
438 goto error;
439 }
440 memcpy(resized_mem, oid->p, encoded_len);
441 mbedtls_free(oid->p);
442 oid->p = resized_mem;
443 oid->len = encoded_len;
444
445 oid->tag = MBEDTLS_ASN1_OID;
446
447 return 0;
448
449error:
450 mbedtls_free(oid->p);
451 oid->p = NULL;
452 oid->len = 0;
453 return ret;
454}
455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200457{
David Horstmann8fd98d62023-06-27 15:17:44 +0100458 int ret = MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100459 int parse_ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +0100460 const char *s = name, *c = s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 const char *end = s + strlen(s);
Agathiyan Bragadeeshba386ec2023-08-16 11:31:17 +0100462 mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 const x509_attr_descriptor_t *attr_descr = NULL;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100464 int in_attr_type = 1;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100465 int tag;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100466 int numericoid = 0;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100467 unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
468 size_t data_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
470 /* Clear existing chain if present */
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 mbedtls_asn1_free_named_data_list(head);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 while (c <= end) {
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100474 if (in_attr_type && *c == '=') {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000475 if ((attr_descr = x509_attr_descr_from_name(s, (size_t) (c - s))) == NULL) {
476 if ((mbedtls_oid_from_numeric_string(&oid, s, (size_t) (c - s))) != 0) {
Agathiyan Bragadeesh17984872023-08-11 12:42:03 +0100477 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100478 } else {
479 numericoid = 1;
480 }
481 } else {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100482 oid.len = strlen(attr_descr->oid);
483 oid.p = mbedtls_calloc(1, oid.len);
484 memcpy(oid.p, attr_descr->oid, oid.len);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100485 numericoid = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486 }
487
488 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100489 in_attr_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 }
491
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100492 if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
Agathiyan Bragadeesh457ac842023-08-23 11:35:26 +0100493 if (s == c) {
Agathiyan Bragadeesh4c7d7bf2023-08-23 11:28:30 +0100494 mbedtls_free(oid.p);
495 return MBEDTLS_ERR_X509_INVALID_NAME;
496 } else if (*s == '#') {
Gilles Peskine70777812023-09-21 16:50:40 +0200497 /* We know that c >= s (loop invariant) and c != s (in this
498 * else branch), hence c - s - 1 >= 0. */
499 parse_ret = parse_attribute_value_hex_der_encoded(
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000500 s + 1, (size_t) (c - s) - 1,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200501 data, sizeof(data), &data_len, &tag);
Gilles Peskine70777812023-09-21 16:50:40 +0200502 if (parse_ret != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100503 mbedtls_free(oid.p);
Gilles Peskine391dd7f2023-09-21 18:51:35 +0200504 return parse_ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100505 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100506 } else {
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100507 if (numericoid) {
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100508 mbedtls_free(oid.p);
509 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100510 } else {
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100511 if ((parse_ret =
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100512 parse_attribute_value_string(s, (int) (c - s), data,
513 &data_len)) != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100514 mbedtls_free(oid.p);
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100515 return parse_ret;
516 }
517 tag = attr_descr->default_tag;
518 }
519 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 mbedtls_asn1_named_data *cur =
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100522 mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 (unsigned char *) data,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100524 data_len);
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100525 mbedtls_free(oid.p);
526 oid.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (cur == NULL) {
528 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200529 }
530
Jaeden Amero23f954d2018-05-17 11:46:13 +0100531 // set tagType
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100532 cur->val.tag = tag;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 while (c < end && *(c + 1) == ' ') {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200535 c++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100539 in_attr_type = 1;
David Horstmann8fd98d62023-06-27 15:17:44 +0100540
541 /* Successfully parsed one name, update ret to success */
542 ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543 }
544 c++;
545 }
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100546 if (oid.p != NULL) {
547 mbedtls_free(oid.p);
Agathiyan Bragadeesh55d93192023-08-15 15:05:03 +0100548 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550}
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553 * to store the critical boolean for us
554 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
556 int critical, const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 mbedtls_asn1_named_data *cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559
Jonathan Winzig5caf20e2024-01-09 16:41:10 +0100560 if (val_len > (SIZE_MAX - 1)) {
Jonathan Winzig05c722b2024-01-09 15:20:03 +0100561 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
562 }
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
565 NULL, val_len + 1)) == NULL) {
566 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567 }
568
569 cur->val.p[0] = critical;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 memcpy(cur->val.p + 1, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573}
574
575/*
576 * RelativeDistinguishedName ::=
577 * SET OF AttributeTypeAndValue
578 *
579 * AttributeTypeAndValue ::= SEQUENCE {
580 * type AttributeType,
581 * value AttributeValue }
582 *
583 * AttributeType ::= OBJECT IDENTIFIER
584 *
585 * AttributeValue ::= ANY DEFINED BY AttributeType
586 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100587static int x509_write_name(unsigned char **p,
588 unsigned char *start,
589 mbedtls_asn1_named_data *cur_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590{
Janos Follath865b3eb2019-12-16 11:46:15 +0000591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592 size_t len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 const char *oid = (const char *) cur_name->oid.p;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100594 size_t oid_len = cur_name->oid.len;
595 const unsigned char *name = cur_name->val.p;
596 size_t name_len = cur_name->val.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200597
Jaeden Amero23f954d2018-05-17 11:46:13 +0100598 // Write correct string tag and value
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
600 cur_name->val.tag,
601 (const char *) name,
602 name_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200603 // Write OID
604 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
606 oid_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
609 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
610 MBEDTLS_ASN1_CONSTRUCTED |
611 MBEDTLS_ASN1_SEQUENCE));
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_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619}
620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
622 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623{
Janos Follath865b3eb2019-12-16 11:46:15 +0000624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_asn1_named_data *cur = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 while (cur != NULL) {
629 MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 cur = cur->next;
631 }
632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
634 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
635 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638}
639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
641 const char *oid, size_t oid_len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100642 unsigned char *sig, size_t size,
643 mbedtls_pk_type_t pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644{
Janos Follath865b3eb2019-12-16 11:46:15 +0000645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Marek Jansta8bde6492022-11-07 12:38:38 +0100646 int write_null_par;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 size_t len = 0;
648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if (*p < start || (size_t) (*p - start) < size) {
650 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
651 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200652
653 len = size;
654 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 memcpy(*p, sig, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (*p - start < 1) {
658 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
659 }
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200660
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 *--(*p) = 0;
662 len += 1;
663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
665 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666
667 // Write OID
668 //
Marek Jansta8bde6492022-11-07 12:38:38 +0100669 if (pk_alg == MBEDTLS_PK_ECDSA) {
670 /*
671 * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
672 * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
673 * https://www.rfc-editor.org/rfc/rfc5758#section-3.
674 */
675 write_null_par = 0;
676 } else {
677 write_null_par = 1;
678 }
679 MBEDTLS_ASN1_CHK_ADD(len,
680 mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
681 0, write_null_par));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684}
685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686static int x509_write_extension(unsigned char **p, unsigned char *start,
687 mbedtls_asn1_named_data *ext)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688{
Janos Follath865b3eb2019-12-16 11:46:15 +0000689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690 size_t len = 0;
691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
693 ext->val.len - 1));
694 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
695 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (ext->val.p[0] != 0) {
698 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699 }
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
702 ext->oid.len));
703 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
704 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
707 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
708 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200711}
712
713/*
714 * Extension ::= SEQUENCE {
715 * extnID OBJECT IDENTIFIER,
716 * critical BOOLEAN DEFAULT FALSE,
717 * extnValue OCTET STRING
718 * -- contains the DER encoding of an ASN.1 value
719 * -- corresponding to the extension type identified
720 * -- by extnID
721 * }
722 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
724 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725{
Janos Follath865b3eb2019-12-16 11:46:15 +0000726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 mbedtls_asn1_named_data *cur_ext = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 while (cur_ext != NULL) {
731 MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 cur_ext = cur_ext->next;
733 }
734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736}
737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738#endif /* MBEDTLS_X509_CREATE_C */