blob: 056bbaa786343393a0d27336076fe407c2f1c886 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * X.509 base functions for creating certificates / CSRs
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
19
Jerome Forissier79013242021-07-28 10:24:04 +020020#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020021
22#if defined(MBEDTLS_X509_CREATE_C)
23
24#include "mbedtls/x509.h"
25#include "mbedtls/asn1write.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020026#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020027#include "mbedtls/oid.h"
28
29#include <string.h>
30
Jens Wiklander3d3b0592019-03-20 15:30:29 +010031/* Structure linking OIDs for X.509 DN AttributeTypes to their
32 * string representations and default string encodings used by Mbed TLS. */
Jens Wiklander817466c2018-05-22 13:49:31 +020033typedef struct {
Jens Wiklander3d3b0592019-03-20 15:30:29 +010034 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. */
Jens Wiklander817466c2018-05-22 13:49:31 +020042} x509_attr_descriptor_t;
43
44#define ADD_STRLEN( s ) s, sizeof( s ) - 1
45
Jens Wiklander3d3b0592019-03-20 15:30:29 +010046/* X.509 DN attributes from RFC 5280, Appendix A.1. */
Jens Wiklander817466c2018-05-22 13:49:31 +020047static const x509_attr_descriptor_t x509_attrs[] =
48{
Jens Wiklander3d3b0592019-03-20 15:30:29 +010049 { ADD_STRLEN( "CN" ),
50 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
51 { ADD_STRLEN( "commonName" ),
52 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
53 { ADD_STRLEN( "C" ),
54 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
55 { ADD_STRLEN( "countryName" ),
56 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
57 { ADD_STRLEN( "O" ),
58 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
59 { ADD_STRLEN( "organizationName" ),
60 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
61 { ADD_STRLEN( "L" ),
62 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
63 { ADD_STRLEN( "locality" ),
64 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
65 { ADD_STRLEN( "R" ),
66 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
67 { ADD_STRLEN( "OU" ),
68 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
69 { ADD_STRLEN( "organizationalUnitName" ),
70 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
71 { ADD_STRLEN( "ST" ),
72 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
73 { ADD_STRLEN( "stateOrProvinceName" ),
74 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
75 { ADD_STRLEN( "emailAddress" ),
76 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
77 { ADD_STRLEN( "serialNumber" ),
78 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
79 { ADD_STRLEN( "postalAddress" ),
80 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
81 { ADD_STRLEN( "postalCode" ),
82 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
83 { ADD_STRLEN( "dnQualifier" ),
84 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
85 { ADD_STRLEN( "title" ),
86 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
87 { ADD_STRLEN( "surName" ),
88 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
89 { ADD_STRLEN( "SN" ),
90 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
91 { ADD_STRLEN( "givenName" ),
92 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
93 { ADD_STRLEN( "GN" ),
94 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
95 { ADD_STRLEN( "initials" ),
96 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
97 { ADD_STRLEN( "pseudonym" ),
98 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
99 { ADD_STRLEN( "generationQualifier" ),
100 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
101 { ADD_STRLEN( "domainComponent" ),
102 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
103 { ADD_STRLEN( "DC" ),
104 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
105 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
Jens Wiklander817466c2018-05-22 13:49:31 +0200106};
107
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100108static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
Jens Wiklander817466c2018-05-22 13:49:31 +0200109{
110 const x509_attr_descriptor_t *cur;
111
112 for( cur = x509_attrs; cur->name != NULL; cur++ )
113 if( cur->name_len == name_len &&
114 strncmp( cur->name, name, name_len ) == 0 )
115 break;
116
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100117 if ( cur->name == NULL )
118 return( NULL );
119
120 return( cur );
Jens Wiklander817466c2018-05-22 13:49:31 +0200121}
122
123int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
124{
125 int ret = 0;
126 const char *s = name, *c = s;
127 const char *end = s + strlen( s );
128 const char *oid = NULL;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100129 const x509_attr_descriptor_t* attr_descr = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200130 int in_tag = 1;
131 char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
132 char *d = data;
133
134 /* Clear existing chain if present */
135 mbedtls_asn1_free_named_data_list( head );
136
137 while( c <= end )
138 {
139 if( in_tag && *c == '=' )
140 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100141 if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200142 {
143 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
144 goto exit;
145 }
146
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100147 oid = attr_descr->oid;
Jens Wiklander817466c2018-05-22 13:49:31 +0200148 s = c + 1;
149 in_tag = 0;
150 d = data;
151 }
152
153 if( !in_tag && *c == '\\' && c != end )
154 {
155 c++;
156
157 /* Check for valid escaped characters */
158 if( c == end || *c != ',' )
159 {
160 ret = MBEDTLS_ERR_X509_INVALID_NAME;
161 goto exit;
162 }
163 }
164 else if( !in_tag && ( *c == ',' || c == end ) )
165 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100166 mbedtls_asn1_named_data* cur =
167 mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
168 (unsigned char *) data,
169 d - data );
170
171 if(cur == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200172 {
173 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
174 }
175
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100176 // set tagType
177 cur->val.tag = attr_descr->default_tag;
178
Jens Wiklander817466c2018-05-22 13:49:31 +0200179 while( c < end && *(c + 1) == ' ' )
180 c++;
181
182 s = c + 1;
183 in_tag = 1;
184 }
185
186 if( !in_tag && s != c + 1 )
187 {
188 *(d++) = *c;
189
190 if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
191 {
192 ret = MBEDTLS_ERR_X509_INVALID_NAME;
193 goto exit;
194 }
195 }
196
197 c++;
198 }
199
200exit:
201
202 return( ret );
203}
204
205/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
206 * to store the critical boolean for us
207 */
208int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
209 int critical, const unsigned char *val, size_t val_len )
210{
211 mbedtls_asn1_named_data *cur;
212
213 if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
214 NULL, val_len + 1 ) ) == NULL )
215 {
216 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
217 }
218
219 cur->val.p[0] = critical;
220 memcpy( cur->val.p + 1, val, val_len );
221
222 return( 0 );
223}
224
225/*
226 * RelativeDistinguishedName ::=
227 * SET OF AttributeTypeAndValue
228 *
229 * AttributeTypeAndValue ::= SEQUENCE {
230 * type AttributeType,
231 * value AttributeValue }
232 *
233 * AttributeType ::= OBJECT IDENTIFIER
234 *
235 * AttributeValue ::= ANY DEFINED BY AttributeType
236 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100237static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
Jens Wiklander817466c2018-05-22 13:49:31 +0200238{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200240 size_t len = 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100241 const char *oid = (const char*)cur_name->oid.p;
242 size_t oid_len = cur_name->oid.len;
243 const unsigned char *name = cur_name->val.p;
244 size_t name_len = cur_name->val.len;
Jens Wiklander817466c2018-05-22 13:49:31 +0200245
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100246 // Write correct string tag and value
247 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
248 cur_name->val.tag,
249 (const char *) name,
250 name_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200251 // Write OID
252 //
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100253 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
254 oid_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200255
256 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100257 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
258 MBEDTLS_ASN1_CONSTRUCTED |
259 MBEDTLS_ASN1_SEQUENCE ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200260
261 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
263 MBEDTLS_ASN1_CONSTRUCTED |
Jens Wiklander817466c2018-05-22 13:49:31 +0200264 MBEDTLS_ASN1_SET ) );
265
266 return( (int) len );
267}
268
269int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100270 mbedtls_asn1_named_data *first )
Jens Wiklander817466c2018-05-22 13:49:31 +0200271{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200273 size_t len = 0;
274 mbedtls_asn1_named_data *cur = first;
275
276 while( cur != NULL )
277 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100278 MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200279 cur = cur->next;
280 }
281
282 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
283 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
284 MBEDTLS_ASN1_SEQUENCE ) );
285
286 return( (int) len );
287}
288
289int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
290 const char *oid, size_t oid_len,
291 unsigned char *sig, size_t size )
292{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200294 size_t len = 0;
295
296 if( *p < start || (size_t)( *p - start ) < size )
297 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
298
299 len = size;
300 (*p) -= len;
301 memcpy( *p, sig, len );
302
303 if( *p - start < 1 )
304 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
305
306 *--(*p) = 0;
307 len += 1;
308
309 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
310 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
311
312 // Write OID
313 //
314 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
315 oid_len, 0 ) );
316
317 return( (int) len );
318}
319
320static int x509_write_extension( unsigned char **p, unsigned char *start,
321 mbedtls_asn1_named_data *ext )
322{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200323 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200324 size_t len = 0;
325
326 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
327 ext->val.len - 1 ) );
328 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
329 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
330
331 if( ext->val.p[0] != 0 )
332 {
333 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
334 }
335
336 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
337 ext->oid.len ) );
338 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
339 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
340
341 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
342 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
343 MBEDTLS_ASN1_SEQUENCE ) );
344
345 return( (int) len );
346}
347
348/*
349 * Extension ::= SEQUENCE {
350 * extnID OBJECT IDENTIFIER,
351 * critical BOOLEAN DEFAULT FALSE,
352 * extnValue OCTET STRING
353 * -- contains the DER encoding of an ASN.1 value
354 * -- corresponding to the extension type identified
355 * -- by extnID
356 * }
357 */
358int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
359 mbedtls_asn1_named_data *first )
360{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200362 size_t len = 0;
363 mbedtls_asn1_named_data *cur_ext = first;
364
365 while( cur_ext != NULL )
366 {
367 MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
368 cur_ext = cur_ext->next;
369 }
370
371 return( (int) len );
372}
373
374#endif /* MBEDTLS_X509_CREATE_C */