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.
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"
31 /* Structure linking OIDs for X.509 DN AttributeTypes to their
32 * string representations and default string encodings used by Mbed TLS. */
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
[] = {
50 MBEDTLS_OID_AT_CN
, MBEDTLS_ASN1_UTF8_STRING
53 ADD_STRLEN("commonName"),
54 MBEDTLS_OID_AT_CN
, MBEDTLS_ASN1_UTF8_STRING
58 MBEDTLS_OID_AT_COUNTRY
, MBEDTLS_ASN1_PRINTABLE_STRING
61 ADD_STRLEN("countryName"),
62 MBEDTLS_OID_AT_COUNTRY
, MBEDTLS_ASN1_PRINTABLE_STRING
66 MBEDTLS_OID_AT_ORGANIZATION
, MBEDTLS_ASN1_UTF8_STRING
69 ADD_STRLEN("organizationName"),
70 MBEDTLS_OID_AT_ORGANIZATION
, MBEDTLS_ASN1_UTF8_STRING
74 MBEDTLS_OID_AT_LOCALITY
, MBEDTLS_ASN1_UTF8_STRING
77 ADD_STRLEN("locality"),
78 MBEDTLS_OID_AT_LOCALITY
, MBEDTLS_ASN1_UTF8_STRING
82 MBEDTLS_OID_PKCS9_EMAIL
, MBEDTLS_ASN1_IA5_STRING
86 MBEDTLS_OID_AT_ORG_UNIT
, MBEDTLS_ASN1_UTF8_STRING
89 ADD_STRLEN("organizationalUnitName"),
90 MBEDTLS_OID_AT_ORG_UNIT
, MBEDTLS_ASN1_UTF8_STRING
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
122 MBEDTLS_OID_AT_TITLE
, MBEDTLS_ASN1_UTF8_STRING
125 ADD_STRLEN("surName"),
126 MBEDTLS_OID_AT_SUR_NAME
, MBEDTLS_ASN1_UTF8_STRING
130 MBEDTLS_OID_AT_SUR_NAME
, MBEDTLS_ASN1_UTF8_STRING
133 ADD_STRLEN("givenName"),
134 MBEDTLS_OID_AT_GIVEN_NAME
, MBEDTLS_ASN1_UTF8_STRING
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
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)
171 if (cur
->name
== NULL
)
177 int mbedtls_x509_string_to_names(mbedtls_asn1_named_data
**head
, const char *name
) {
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
;
184 char data
[MBEDTLS_X509_MAX_DN_NAME_SIZE
];
187 /* Clear existing chain if present */
188 mbedtls_asn1_free_named_data_list(head
);
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
;
197 oid
= attr_descr
->oid
;
203 if (!in_tag
&& *c
== '\\' && c
!= end
) {
206 /* Check for valid escaped characters */
207 if (c
== end
|| *c
!= ',') {
208 ret
= MBEDTLS_ERR_X509_INVALID_NAME
;
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
,
218 return (MBEDTLS_ERR_X509_ALLOC_FAILED
);
222 cur
->val
.tag
= attr_descr
->default_tag
;
224 while (c
< end
&& *(c
+ 1) == ' ')
231 if (!in_tag
&& s
!= c
+ 1) {
234 if (d
- data
== MBEDTLS_X509_MAX_DN_NAME_SIZE
) {
235 ret
= MBEDTLS_ERR_X509_INVALID_NAME
;
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
);
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
;
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
,
293 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_oid(p
, start
, oid
,
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
|
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
;
313 mbedtls_asn1_named_data
*cur
= first
;
315 while (cur
!= NULL
) {
316 MBEDTLS_ASN1_CHK_ADD(len
, x509_write_name(p
, start
, cur
));
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
));
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
;
333 if (*p
< start
|| (size_t)(*p
- start
) < size
)
334 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
338 memcpy(*p
, sig
, len
);
341 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
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
));
351 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_algorithm_identifier(p
, start
, oid
,
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
;
362 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_raw_buffer(p
, start
, ext
->val
.p
+ 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
,
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
));
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
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
;
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
;
407 #endif /* MBEDTLS_X509_CREATE_C */