Merge pull request #2735 from jareckib/master
[RRG-proxmark3.git] / common / mbedtls / x509_create.c
blob9cd33c789bec31706df98f569a38822735480fb2
1 /*
2 * X.509 base functions for creating certificates / CSRs
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 #include "common.h"
22 #if defined(MBEDTLS_X509_CREATE_C)
24 #include "mbedtls/x509.h"
25 #include "mbedtls/asn1write.h"
26 #include "mbedtls/error.h"
27 #include "mbedtls/oid.h"
29 #include <string.h>
31 /* Structure linking OIDs for X.509 DN AttributeTypes to their
32 * string representations and default string encodings used by Mbed TLS. */
33 typedef struct {
34 const char *name; /* String representation of AttributeType, e.g.
35 * "CN" or "emailAddress". */
36 size_t name_len; /* Length of 'name', without trailing 0 byte. */
37 const char *oid; /* String representation of OID of AttributeType,
38 * as per RFC 5280, Appendix A.1. */
39 int default_tag; /* The default character encoding used for the
40 * given attribute type, e.g.
41 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
42 } x509_attr_descriptor_t;
44 #define ADD_STRLEN( s ) s, sizeof( s ) - 1
46 /* X.509 DN attributes from RFC 5280, Appendix A.1. */
47 static const x509_attr_descriptor_t x509_attrs[] = {
49 ADD_STRLEN("CN"),
50 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING
53 ADD_STRLEN("commonName"),
54 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING
57 ADD_STRLEN("C"),
58 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING
61 ADD_STRLEN("countryName"),
62 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING
65 ADD_STRLEN("O"),
66 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING
69 ADD_STRLEN("organizationName"),
70 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING
73 ADD_STRLEN("L"),
74 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING
77 ADD_STRLEN("locality"),
78 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING
81 ADD_STRLEN("R"),
82 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING
85 ADD_STRLEN("OU"),
86 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING
89 ADD_STRLEN("organizationalUnitName"),
90 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING
93 ADD_STRLEN("ST"),
94 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING
97 ADD_STRLEN("stateOrProvinceName"),
98 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING
101 ADD_STRLEN("emailAddress"),
102 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING
105 ADD_STRLEN("serialNumber"),
106 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING
109 ADD_STRLEN("postalAddress"),
110 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING
113 ADD_STRLEN("postalCode"),
114 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING
117 ADD_STRLEN("dnQualifier"),
118 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING
121 ADD_STRLEN("title"),
122 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING
125 ADD_STRLEN("surName"),
126 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING
129 ADD_STRLEN("SN"),
130 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING
133 ADD_STRLEN("givenName"),
134 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING
137 ADD_STRLEN("GN"),
138 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING
141 ADD_STRLEN("initials"),
142 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING
145 ADD_STRLEN("pseudonym"),
146 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING
149 ADD_STRLEN("generationQualifier"),
150 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING
153 ADD_STRLEN("domainComponent"),
154 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING
157 ADD_STRLEN("DC"),
158 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING
160 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
163 static const x509_attr_descriptor_t *x509_attr_descr_from_name(const char *name, size_t name_len) {
164 const x509_attr_descriptor_t *cur;
166 for (cur = x509_attrs; cur->name != NULL; cur++)
167 if (cur->name_len == name_len &&
168 strncmp(cur->name, name, name_len) == 0)
169 break;
171 if (cur->name == NULL)
172 return (NULL);
174 return (cur);
177 int mbedtls_x509_string_to_names(mbedtls_asn1_named_data **head, const char *name) {
178 int ret = 0;
179 const char *s = name, *c = s;
180 const char *end = s + strlen(s);
181 const char *oid = NULL;
182 const x509_attr_descriptor_t *attr_descr = NULL;
183 int in_tag = 1;
184 char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
185 char *d = data;
187 /* Clear existing chain if present */
188 mbedtls_asn1_free_named_data_list(head);
190 while (c <= end) {
191 if (in_tag && *c == '=') {
192 if ((attr_descr = x509_attr_descr_from_name(s, c - s)) == NULL) {
193 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
194 goto exit;
197 oid = attr_descr->oid;
198 s = c + 1;
199 in_tag = 0;
200 d = data;
203 if (!in_tag && *c == '\\' && c != end) {
204 c++;
206 /* Check for valid escaped characters */
207 if (c == end || *c != ',') {
208 ret = MBEDTLS_ERR_X509_INVALID_NAME;
209 goto exit;
211 } else if (!in_tag && (*c == ',' || c == end)) {
212 mbedtls_asn1_named_data *cur =
213 mbedtls_asn1_store_named_data(head, oid, strlen(oid),
214 (unsigned char *) data,
215 d - data);
217 if (cur == NULL) {
218 return (MBEDTLS_ERR_X509_ALLOC_FAILED);
221 // set tagType
222 cur->val.tag = attr_descr->default_tag;
224 while (c < end && *(c + 1) == ' ')
225 c++;
227 s = c + 1;
228 in_tag = 1;
231 if (!in_tag && s != c + 1) {
232 *(d++) = *c;
234 if (d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE) {
235 ret = MBEDTLS_ERR_X509_INVALID_NAME;
236 goto exit;
240 c++;
243 exit:
245 return (ret);
248 /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
249 * to store the critical boolean for us
251 int mbedtls_x509_set_extension(mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
252 int critical, const unsigned char *val, size_t val_len) {
253 mbedtls_asn1_named_data *cur;
255 if ((cur = mbedtls_asn1_store_named_data(head, oid, oid_len,
256 NULL, val_len + 1)) == NULL) {
257 return (MBEDTLS_ERR_X509_ALLOC_FAILED);
260 cur->val.p[0] = critical;
261 memcpy(cur->val.p + 1, val, val_len);
263 return (0);
267 * RelativeDistinguishedName ::=
268 * SET OF AttributeTypeAndValue
270 * AttributeTypeAndValue ::= SEQUENCE {
271 * type AttributeType,
272 * value AttributeValue }
274 * AttributeType ::= OBJECT IDENTIFIER
276 * AttributeValue ::= ANY DEFINED BY AttributeType
278 static int x509_write_name(unsigned char **p, unsigned char *start, mbedtls_asn1_named_data *cur_name) {
279 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
280 size_t len = 0;
281 const char *oid = (const char *)cur_name->oid.p;
282 size_t oid_len = cur_name->oid.len;
283 const unsigned char *name = cur_name->val.p;
284 size_t name_len = cur_name->val.len;
286 // Write correct string tag and value
287 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tagged_string(p, start,
288 cur_name->val.tag,
289 (const char *) name,
290 name_len));
291 // Write OID
293 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_oid(p, start, oid,
294 oid_len));
296 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
297 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
298 MBEDTLS_ASN1_CONSTRUCTED |
299 MBEDTLS_ASN1_SEQUENCE));
301 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
302 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
303 MBEDTLS_ASN1_CONSTRUCTED |
304 MBEDTLS_ASN1_SET));
306 return ((int) len);
309 int mbedtls_x509_write_names(unsigned char **p, unsigned char *start,
310 mbedtls_asn1_named_data *first) {
311 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
312 size_t len = 0;
313 mbedtls_asn1_named_data *cur = first;
315 while (cur != NULL) {
316 MBEDTLS_ASN1_CHK_ADD(len, x509_write_name(p, start, cur));
317 cur = cur->next;
320 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
321 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
322 MBEDTLS_ASN1_SEQUENCE));
324 return ((int) len);
327 int mbedtls_x509_write_sig(unsigned char **p, unsigned char *start,
328 const char *oid, size_t oid_len,
329 unsigned char *sig, size_t size) {
330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
331 size_t len = 0;
333 if (*p < start || (size_t)(*p - start) < size)
334 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
336 len = size;
337 (*p) -= len;
338 memcpy(*p, sig, len);
340 if (*p - start < 1)
341 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL);
343 *--(*p) = 0;
344 len += 1;
346 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
347 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_BIT_STRING));
349 // Write OID
351 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_algorithm_identifier(p, start, oid,
352 oid_len, 0));
354 return ((int) len);
357 static int x509_write_extension(unsigned char **p, unsigned char *start,
358 mbedtls_asn1_named_data *ext) {
359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
360 size_t len = 0;
362 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->val.p + 1,
363 ext->val.len - 1));
364 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->val.len - 1));
365 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OCTET_STRING));
367 if (ext->val.p[0] != 0) {
368 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(p, start, 1));
371 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, ext->oid.p,
372 ext->oid.len));
373 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, ext->oid.len));
374 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_OID));
376 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
377 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, MBEDTLS_ASN1_CONSTRUCTED |
378 MBEDTLS_ASN1_SEQUENCE));
380 return ((int) len);
384 * Extension ::= SEQUENCE {
385 * extnID OBJECT IDENTIFIER,
386 * critical BOOLEAN DEFAULT FALSE,
387 * extnValue OCTET STRING
388 * -- contains the DER encoding of an ASN.1 value
389 * -- corresponding to the extension type identified
390 * -- by extnID
393 int mbedtls_x509_write_extensions(unsigned char **p, unsigned char *start,
394 mbedtls_asn1_named_data *first) {
395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
396 size_t len = 0;
397 mbedtls_asn1_named_data *cur_ext = first;
399 while (cur_ext != NULL) {
400 MBEDTLS_ASN1_CHK_ADD(len, x509_write_extension(p, start, cur_ext));
401 cur_ext = cur_ext->next;
404 return ((int) len);
407 #endif /* MBEDTLS_X509_CREATE_C */