blob: 130983189f993d148f8e296030fe2a4764709c02 [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 Berry3da783b2024-09-13 15:09:24 +0100281static int oid_parse_number(unsigned int *num, const char **p, const char *bound)
282{
283 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
284
285 *num = 0;
286
287 while (*p < bound && **p >= '0' && **p <= '9') {
288 ret = 0;
289 if (*num > (UINT_MAX / 10)) {
290 return MBEDTLS_ERR_ASN1_INVALID_DATA;
291 }
292 *num *= 10;
293 *num += **p - '0';
294 (*p)++;
295 }
296 return ret;
297}
298
299static size_t oid_subidentifier_num_bytes(unsigned int value)
300{
301 size_t num_bytes = 0;
302
303 do {
304 value >>= 7;
305 num_bytes++;
306 } while (value != 0);
307
308 return num_bytes;
309}
310
311static int oid_subidentifier_encode_into(unsigned char **p,
312 unsigned char *bound,
313 unsigned int value)
314{
315 size_t num_bytes = oid_subidentifier_num_bytes(value);
316
317 if ((size_t) (bound - *p) < num_bytes) {
318 return MBEDTLS_ERR_OID_BUF_TOO_SMALL;
319 }
320 (*p)[num_bytes - 1] = (unsigned char) (value & 0x7f);
321 value >>= 7;
322
323 for (size_t i = 2; i <= num_bytes; i++) {
324 (*p)[num_bytes - i] = 0x80 | (unsigned char) (value & 0x7f);
325 value >>= 7;
326 }
327 *p += num_bytes;
328
329 return 0;
330}
331
Sam Berryc71abc32024-07-19 15:11:10 +0100332/* Return the OID for the given x.y.z.... style numeric string */
333int mbedtls_oid_from_numeric_string(mbedtls_asn1_buf *oid,
334 const char *oid_str, size_t size)
335{
336 int ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
337 const char *str_ptr = oid_str;
338 const char *str_bound = oid_str + size;
339 unsigned int val = 0;
340 unsigned int component1, component2;
341 size_t encoded_len;
342 unsigned char *resized_mem;
343
344 /* Count the number of dots to get a worst-case allocation size. */
345 size_t num_dots = 0;
346 for (size_t i = 0; i < size; i++) {
347 if (oid_str[i] == '.') {
348 num_dots++;
349 }
350 }
351 /* Allocate maximum possible required memory:
352 * There are (num_dots + 1) integer components, but the first 2 share the
353 * same subidentifier, so we only need num_dots subidentifiers maximum. */
354 if (num_dots == 0 || (num_dots > MBEDTLS_OID_MAX_COMPONENTS - 1)) {
355 return MBEDTLS_ERR_ASN1_INVALID_DATA;
356 }
357 /* Each byte can store 7 bits, calculate number of bytes for a
358 * subidentifier:
359 *
360 * bytes = ceil(subidentifer_size * 8 / 7)
361 */
362 size_t bytes_per_subidentifier = (((sizeof(unsigned int) * 8) - 1) / 7)
363 + 1;
364 size_t max_possible_bytes = num_dots * bytes_per_subidentifier;
365 oid->p = mbedtls_calloc(max_possible_bytes, 1);
366 if (oid->p == NULL) {
367 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
368 }
369 unsigned char *out_ptr = oid->p;
370 unsigned char *out_bound = oid->p + max_possible_bytes;
371
372 ret = oid_parse_number(&component1, &str_ptr, str_bound);
373 if (ret != 0) {
374 goto error;
375 }
376 if (component1 > 2) {
377 /* First component can't be > 2 */
378 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
379 goto error;
380 }
381 if (str_ptr >= str_bound || *str_ptr != '.') {
382 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
383 goto error;
384 }
385 str_ptr++;
386
387 ret = oid_parse_number(&component2, &str_ptr, str_bound);
388 if (ret != 0) {
389 goto error;
390 }
391 if ((component1 < 2) && (component2 > 39)) {
392 /* Root nodes 0 and 1 may have up to 40 children, numbered 0-39 */
393 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
394 goto error;
395 }
396 if (str_ptr < str_bound) {
397 if (*str_ptr == '.') {
398 str_ptr++;
399 } else {
400 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
401 goto error;
402 }
403 }
404
405 if (component2 > (UINT_MAX - (component1 * 40))) {
406 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
407 goto error;
408 }
409 ret = oid_subidentifier_encode_into(&out_ptr, out_bound,
410 (component1 * 40) + component2);
411 if (ret != 0) {
412 goto error;
413 }
414
415 while (str_ptr < str_bound) {
416 ret = oid_parse_number(&val, &str_ptr, str_bound);
417 if (ret != 0) {
418 goto error;
419 }
420 if (str_ptr < str_bound) {
421 if (*str_ptr == '.') {
422 str_ptr++;
423 } else {
424 ret = MBEDTLS_ERR_ASN1_INVALID_DATA;
425 goto error;
426 }
427 }
428
429 ret = oid_subidentifier_encode_into(&out_ptr, out_bound, val);
430 if (ret != 0) {
431 goto error;
432 }
433 }
434
435 encoded_len = (size_t) (out_ptr - oid->p);
436 resized_mem = mbedtls_calloc(encoded_len, 1);
437 if (resized_mem == NULL) {
438 ret = MBEDTLS_ERR_ASN1_ALLOC_FAILED;
439 goto error;
440 }
441 memcpy(resized_mem, oid->p, encoded_len);
442 mbedtls_free(oid->p);
443 oid->p = resized_mem;
444 oid->len = encoded_len;
445
446 oid->tag = MBEDTLS_ASN1_OID;
447
448 return 0;
449
450error:
451 mbedtls_free(oid->p);
452 oid->p = NULL;
453 oid->len = 0;
454 return ret;
455}
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458{
David Horstmann8fd98d62023-06-27 15:17:44 +0100459 int ret = MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100460 int parse_ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +0100461 const char *s = name, *c = s;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 const char *end = s + strlen(s);
Agathiyan Bragadeeshba386ec2023-08-16 11:31:17 +0100463 mbedtls_asn1_buf oid = { .p = NULL, .len = 0, .tag = MBEDTLS_ASN1_NULL };
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 const x509_attr_descriptor_t *attr_descr = NULL;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100465 int in_attr_type = 1;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100466 int tag;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100467 int numericoid = 0;
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100468 unsigned char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
469 size_t data_len = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
471 /* Clear existing chain if present */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 mbedtls_asn1_free_named_data_list(head);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 while (c <= end) {
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100475 if (in_attr_type && *c == '=') {
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000476 if ((attr_descr = x509_attr_descr_from_name(s, (size_t) (c - s))) == NULL) {
477 if ((mbedtls_oid_from_numeric_string(&oid, s, (size_t) (c - s))) != 0) {
Agathiyan Bragadeesh17984872023-08-11 12:42:03 +0100478 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100479 } else {
480 numericoid = 1;
481 }
482 } else {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100483 oid.len = strlen(attr_descr->oid);
484 oid.p = mbedtls_calloc(1, oid.len);
485 memcpy(oid.p, attr_descr->oid, oid.len);
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100486 numericoid = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487 }
488
489 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100490 in_attr_type = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 }
492
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100493 if (!in_attr_type && ((*c == ',' && *(c-1) != '\\') || c == end)) {
Agathiyan Bragadeesh457ac842023-08-23 11:35:26 +0100494 if (s == c) {
Agathiyan Bragadeesh4c7d7bf2023-08-23 11:28:30 +0100495 mbedtls_free(oid.p);
496 return MBEDTLS_ERR_X509_INVALID_NAME;
497 } else if (*s == '#') {
Gilles Peskine70777812023-09-21 16:50:40 +0200498 /* We know that c >= s (loop invariant) and c != s (in this
499 * else branch), hence c - s - 1 >= 0. */
500 parse_ret = parse_attribute_value_hex_der_encoded(
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000501 s + 1, (size_t) (c - s) - 1,
Gilles Peskine7f420fa2023-09-21 18:13:17 +0200502 data, sizeof(data), &data_len, &tag);
Gilles Peskine70777812023-09-21 16:50:40 +0200503 if (parse_ret != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100504 mbedtls_free(oid.p);
Gilles Peskine391dd7f2023-09-21 18:51:35 +0200505 return parse_ret;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100506 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100507 } else {
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100508 if (numericoid) {
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100509 mbedtls_free(oid.p);
510 return MBEDTLS_ERR_X509_INVALID_NAME;
Agathiyan Bragadeesh15df0122023-08-22 17:50:00 +0100511 } else {
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100512 if ((parse_ret =
Agathiyan Bragadeesheb558672023-08-14 16:31:11 +0100513 parse_attribute_value_string(s, (int) (c - s), data,
514 &data_len)) != 0) {
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100515 mbedtls_free(oid.p);
Agathiyan Bragadeesh957ca052023-08-11 14:58:14 +0100516 return parse_ret;
517 }
518 tag = attr_descr->default_tag;
519 }
520 }
Agathiyan Bragadeesh4606bf32023-08-22 17:29:18 +0100521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_asn1_named_data *cur =
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100523 mbedtls_asn1_store_named_data(head, (char *) oid.p, oid.len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 (unsigned char *) data,
Agathiyan Bragadeeshe119f3c2023-07-24 17:21:14 +0100525 data_len);
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100526 mbedtls_free(oid.p);
527 oid.p = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (cur == NULL) {
529 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200530 }
531
Jaeden Amero23f954d2018-05-17 11:46:13 +0100532 // set tagType
Agathiyan Bragadeesh6cbfae52023-07-27 14:34:11 +0100533 cur->val.tag = tag;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 while (c < end && *(c + 1) == ' ') {
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536 c++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538
539 s = c + 1;
Agathiyan Bragadeeshed88eef2023-08-10 13:51:38 +0100540 in_attr_type = 1;
David Horstmann8fd98d62023-06-27 15:17:44 +0100541
542 /* Successfully parsed one name, update ret to success */
543 ret = 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 }
545 c++;
546 }
Agathiyan Bragadeesh12b9d702023-08-15 17:42:33 +0100547 if (oid.p != NULL) {
548 mbedtls_free(oid.p);
Agathiyan Bragadeesh55d93192023-08-15 15:05:03 +0100549 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551}
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 * to store the critical boolean for us
555 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
557 int critical, const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_asn1_named_data *cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
Jonathan Winzig5caf20e2024-01-09 16:41:10 +0100561 if (val_len > (SIZE_MAX - 1)) {
Jonathan Winzig05c722b2024-01-09 15:20:03 +0100562 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
563 }
564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
566 NULL, val_len + 1)) == NULL) {
567 return MBEDTLS_ERR_X509_ALLOC_FAILED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568 }
569
570 cur->val.p[0] = critical;
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 memcpy(cur->val.p + 1, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574}
575
576/*
577 * RelativeDistinguishedName ::=
578 * SET OF AttributeTypeAndValue
579 *
580 * AttributeTypeAndValue ::= SEQUENCE {
581 * type AttributeType,
582 * value AttributeValue }
583 *
584 * AttributeType ::= OBJECT IDENTIFIER
585 *
586 * AttributeValue ::= ANY DEFINED BY AttributeType
587 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588static int x509_write_name(unsigned char **p,
589 unsigned char *start,
590 mbedtls_asn1_named_data *cur_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591{
Janos Follath865b3eb2019-12-16 11:46:15 +0000592 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593 size_t len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 const char *oid = (const char *) cur_name->oid.p;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100595 size_t oid_len = cur_name->oid.len;
596 const unsigned char *name = cur_name->val.p;
597 size_t name_len = cur_name->val.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200598
Jaeden Amero23f954d2018-05-17 11:46:13 +0100599 // Write correct string tag and value
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
601 cur_name->val.tag,
602 (const char *) name,
603 name_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604 // Write OID
605 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
607 oid_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
610 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
611 MBEDTLS_ASN1_CONSTRUCTED |
612 MBEDTLS_ASN1_SEQUENCE));
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_SET));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620}
621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
623 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624{
Janos Follath865b3eb2019-12-16 11:46:15 +0000625 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_asn1_named_data *cur = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 while (cur != NULL) {
630 MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 cur = cur->next;
632 }
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
635 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
636 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639}
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
642 const char *oid, size_t oid_len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100643 unsigned char *sig, size_t size,
644 mbedtls_pk_type_t pk_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645{
Janos Follath865b3eb2019-12-16 11:46:15 +0000646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Marek Jansta8bde6492022-11-07 12:38:38 +0100647 int write_null_par;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 size_t len = 0;
649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (*p < start || (size_t) (*p - start) < size) {
651 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
652 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200653
654 len = size;
655 (*p) -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 memcpy(*p, sig, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (*p - start < 1) {
659 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
660 }
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200661
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200662 *--(*p) = 0;
663 len += 1;
664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
666 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667
668 // Write OID
669 //
Marek Jansta8bde6492022-11-07 12:38:38 +0100670 if (pk_alg == MBEDTLS_PK_ECDSA) {
671 /*
672 * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
673 * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
674 * https://www.rfc-editor.org/rfc/rfc5758#section-3.
675 */
676 write_null_par = 0;
677 } else {
678 write_null_par = 1;
679 }
680 MBEDTLS_ASN1_CHK_ADD(len,
681 mbedtls_asn1_write_algorithm_identifier_ext(p, start, oid, oid_len,
682 0, write_null_par));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685}
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687static int x509_write_extension(unsigned char **p, unsigned char *start,
688 mbedtls_asn1_named_data *ext)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689{
Janos Follath865b3eb2019-12-16 11:46:15 +0000690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200691 size_t len = 0;
692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
694 ext->val.len - 1));
695 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
696 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (ext->val.p[0] != 0) {
699 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 }
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
703 ext->oid.len));
704 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
705 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
708 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
709 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712}
713
714/*
715 * Extension ::= SEQUENCE {
716 * extnID OBJECT IDENTIFIER,
717 * critical BOOLEAN DEFAULT FALSE,
718 * extnValue OCTET STRING
719 * -- contains the DER encoding of an ASN.1 value
720 * -- corresponding to the extension type identified
721 * -- by extnID
722 * }
723 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
725 mbedtls_asn1_named_data *first)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726{
Janos Follath865b3eb2019-12-16 11:46:15 +0000727 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_asn1_named_data *cur_ext = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 while (cur_ext != NULL) {
732 MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 cur_ext = cur_ext->next;
734 }
735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737}
738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739#endif /* MBEDTLS_X509_CREATE_C */