2 * X.509 base functions for creating certificates / CSRs
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_X509_CREATE_C)
30 #include "mbedtls/x509.h"
31 #include "mbedtls/asn1write.h"
32 #include "mbedtls/oid.h"
36 /* Structure linking OIDs for X.509 DN AttributeTypes to their
37 * string representations and default string encodings used by Mbed TLS. */
39 const char *name; /* String representation of AttributeType, e.g.
40 * "CN" or "emailAddress". */
41 size_t name_len; /* Length of 'name', without trailing 0 byte. */
42 const char *oid; /* String representation of OID of AttributeType,
43 * as per RFC 5280, Appendix A.1. */
44 int default_tag; /* The default character encoding used for the
45 * given attribute type, e.g.
46 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
47 } x509_attr_descriptor_t;
49 #define ADD_STRLEN( s ) s, sizeof( s ) - 1
51 /* X.509 DN attributes from RFC 5280, Appendix A.1. */
52 static const x509_attr_descriptor_t x509_attrs[] =
55 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
56 { ADD_STRLEN( "commonName" ),
57 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
59 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
60 { ADD_STRLEN( "countryName" ),
61 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
63 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
64 { ADD_STRLEN( "organizationName" ),
65 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
67 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
68 { ADD_STRLEN( "locality" ),
69 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
71 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
73 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
74 { ADD_STRLEN( "organizationalUnitName" ),
75 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
77 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
78 { ADD_STRLEN( "stateOrProvinceName" ),
79 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
80 { ADD_STRLEN( "emailAddress" ),
81 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
82 { ADD_STRLEN( "serialNumber" ),
83 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
84 { ADD_STRLEN( "postalAddress" ),
85 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
86 { ADD_STRLEN( "postalCode" ),
87 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
88 { ADD_STRLEN( "dnQualifier" ),
89 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
90 { ADD_STRLEN( "title" ),
91 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
92 { ADD_STRLEN( "surName" ),
93 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
95 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
96 { ADD_STRLEN( "givenName" ),
97 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
99 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
100 { ADD_STRLEN( "initials" ),
101 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
102 { ADD_STRLEN( "pseudonym" ),
103 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
104 { ADD_STRLEN( "generationQualifier" ),
105 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
106 { ADD_STRLEN( "domainComponent" ),
107 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
108 { ADD_STRLEN( "DC" ),
109 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
110 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
113 static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
115 const x509_attr_descriptor_t *cur;
117 for( cur = x509_attrs; cur->name != NULL; cur++ )
118 if( cur->name_len == name_len &&
119 strncmp( cur->name, name, name_len ) == 0 )
122 if ( cur->name == NULL )
128 int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
131 const char *s = name, *c = s;
132 const char *end = s + strlen( s );
133 const char *oid = NULL;
134 const x509_attr_descriptor_t* attr_descr = NULL;
136 char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
139 /* Clear existing chain if present */
140 mbedtls_asn1_free_named_data_list( head );
144 if( in_tag && *c == '=' )
146 if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
148 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
152 oid = attr_descr->oid;
158 if( !in_tag && *c == '\\' && c != end )
162 /* Check for valid escaped characters */
163 if( c == end || *c != ',' )
165 ret = MBEDTLS_ERR_X509_INVALID_NAME;
169 else if( !in_tag && ( *c == ',' || c == end ) )
171 mbedtls_asn1_named_data* cur =
172 mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
173 (unsigned char *) data,
178 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
182 cur->val.tag = attr_descr->default_tag;
184 while( c < end && *(c + 1) == ' ' )
191 if( !in_tag && s != c + 1 )
195 if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
197 ret = MBEDTLS_ERR_X509_INVALID_NAME;
210 /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
211 * to store the critical boolean for us
213 int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
214 int critical, const unsigned char *val, size_t val_len )
216 mbedtls_asn1_named_data *cur;
218 if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
219 NULL, val_len + 1 ) ) == NULL )
221 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
224 cur->val.p[0] = critical;
225 memcpy( cur->val.p + 1, val, val_len );
231 * RelativeDistinguishedName ::=
232 * SET OF AttributeTypeAndValue
234 * AttributeTypeAndValue ::= SEQUENCE {
235 * type AttributeType,
236 * value AttributeValue }
238 * AttributeType ::= OBJECT IDENTIFIER
240 * AttributeValue ::= ANY DEFINED BY AttributeType
242 static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
246 const char *oid = (const char*)cur_name->oid.p;
247 size_t oid_len = cur_name->oid.len;
248 const unsigned char *name = cur_name->val.p;
249 size_t name_len = cur_name->val.len;
251 // Write correct string tag and value
252 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
261 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
263 MBEDTLS_ASN1_CONSTRUCTED |
264 MBEDTLS_ASN1_SEQUENCE ) );
266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
267 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
268 MBEDTLS_ASN1_CONSTRUCTED |
269 MBEDTLS_ASN1_SET ) );
274 int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
275 mbedtls_asn1_named_data *first )
279 mbedtls_asn1_named_data *cur = first;
283 MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
287 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
289 MBEDTLS_ASN1_SEQUENCE ) );
294 int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
295 const char *oid, size_t oid_len,
296 unsigned char *sig, size_t size )
301 if( *p < start || (size_t)( *p - start ) < size )
302 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
306 memcpy( *p, sig, len );
309 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
314 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
315 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
319 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
325 static int x509_write_extension( unsigned char **p, unsigned char *start,
326 mbedtls_asn1_named_data *ext )
331 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
332 ext->val.len - 1 ) );
333 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
334 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
336 if( ext->val.p[0] != 0 )
338 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
341 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
343 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
344 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
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_CONSTRUCTED |
348 MBEDTLS_ASN1_SEQUENCE ) );
354 * Extension ::= SEQUENCE {
355 * extnID OBJECT IDENTIFIER,
356 * critical BOOLEAN DEFAULT FALSE,
357 * extnValue OCTET STRING
358 * -- contains the DER encoding of an ASN.1 value
359 * -- corresponding to the extension type identified
363 int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
364 mbedtls_asn1_named_data *first )
368 mbedtls_asn1_named_data *cur_ext = first;
370 while( cur_ext != NULL )
372 MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
373 cur_ext = cur_ext->next;
379 #endif /* MBEDTLS_X509_CREATE_C */