blob: fd3aad16b5ce874b872bf0fc379e04f4106d4fa7 [file] [log] [blame]
Edison Aic6672fd2018-02-28 15:01:47 +08001// SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02002/*
3 * X.509 base functions for creating certificates / CSRs
4 *
5 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_X509_CREATE_C)
29
30#include "mbedtls/x509.h"
31#include "mbedtls/asn1write.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020032#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020033#include "mbedtls/oid.h"
34
35#include <string.h>
36
Jens Wiklander3d3b0592019-03-20 15:30:29 +010037/* Structure linking OIDs for X.509 DN AttributeTypes to their
38 * string representations and default string encodings used by Mbed TLS. */
Jens Wiklander817466c2018-05-22 13:49:31 +020039typedef struct {
Jens Wiklander3d3b0592019-03-20 15:30:29 +010040 const char *name; /* String representation of AttributeType, e.g.
41 * "CN" or "emailAddress". */
42 size_t name_len; /* Length of 'name', without trailing 0 byte. */
43 const char *oid; /* String representation of OID of AttributeType,
44 * as per RFC 5280, Appendix A.1. */
45 int default_tag; /* The default character encoding used for the
46 * given attribute type, e.g.
47 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
Jens Wiklander817466c2018-05-22 13:49:31 +020048} x509_attr_descriptor_t;
49
50#define ADD_STRLEN( s ) s, sizeof( s ) - 1
51
Jens Wiklander3d3b0592019-03-20 15:30:29 +010052/* X.509 DN attributes from RFC 5280, Appendix A.1. */
Jens Wiklander817466c2018-05-22 13:49:31 +020053static const x509_attr_descriptor_t x509_attrs[] =
54{
Jens Wiklander3d3b0592019-03-20 15:30:29 +010055 { ADD_STRLEN( "CN" ),
56 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
57 { ADD_STRLEN( "commonName" ),
58 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
59 { ADD_STRLEN( "C" ),
60 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
61 { ADD_STRLEN( "countryName" ),
62 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
63 { ADD_STRLEN( "O" ),
64 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
65 { ADD_STRLEN( "organizationName" ),
66 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
67 { ADD_STRLEN( "L" ),
68 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
69 { ADD_STRLEN( "locality" ),
70 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
71 { ADD_STRLEN( "R" ),
72 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
73 { ADD_STRLEN( "OU" ),
74 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
75 { ADD_STRLEN( "organizationalUnitName" ),
76 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
77 { ADD_STRLEN( "ST" ),
78 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
79 { ADD_STRLEN( "stateOrProvinceName" ),
80 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
81 { ADD_STRLEN( "emailAddress" ),
82 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
83 { ADD_STRLEN( "serialNumber" ),
84 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
85 { ADD_STRLEN( "postalAddress" ),
86 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
87 { ADD_STRLEN( "postalCode" ),
88 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
89 { ADD_STRLEN( "dnQualifier" ),
90 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
91 { ADD_STRLEN( "title" ),
92 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
93 { ADD_STRLEN( "surName" ),
94 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
95 { ADD_STRLEN( "SN" ),
96 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
97 { ADD_STRLEN( "givenName" ),
98 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
99 { ADD_STRLEN( "GN" ),
100 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
101 { ADD_STRLEN( "initials" ),
102 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
103 { ADD_STRLEN( "pseudonym" ),
104 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
105 { ADD_STRLEN( "generationQualifier" ),
106 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
107 { ADD_STRLEN( "domainComponent" ),
108 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
109 { ADD_STRLEN( "DC" ),
110 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
111 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
Jens Wiklander817466c2018-05-22 13:49:31 +0200112};
113
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100114static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
Jens Wiklander817466c2018-05-22 13:49:31 +0200115{
116 const x509_attr_descriptor_t *cur;
117
118 for( cur = x509_attrs; cur->name != NULL; cur++ )
119 if( cur->name_len == name_len &&
120 strncmp( cur->name, name, name_len ) == 0 )
121 break;
122
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100123 if ( cur->name == NULL )
124 return( NULL );
125
126 return( cur );
Jens Wiklander817466c2018-05-22 13:49:31 +0200127}
128
129int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
130{
131 int ret = 0;
132 const char *s = name, *c = s;
133 const char *end = s + strlen( s );
134 const char *oid = NULL;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100135 const x509_attr_descriptor_t* attr_descr = NULL;
Jens Wiklander817466c2018-05-22 13:49:31 +0200136 int in_tag = 1;
137 char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
138 char *d = data;
139
140 /* Clear existing chain if present */
141 mbedtls_asn1_free_named_data_list( head );
142
143 while( c <= end )
144 {
145 if( in_tag && *c == '=' )
146 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100147 if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200148 {
149 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
150 goto exit;
151 }
152
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100153 oid = attr_descr->oid;
Jens Wiklander817466c2018-05-22 13:49:31 +0200154 s = c + 1;
155 in_tag = 0;
156 d = data;
157 }
158
159 if( !in_tag && *c == '\\' && c != end )
160 {
161 c++;
162
163 /* Check for valid escaped characters */
164 if( c == end || *c != ',' )
165 {
166 ret = MBEDTLS_ERR_X509_INVALID_NAME;
167 goto exit;
168 }
169 }
170 else if( !in_tag && ( *c == ',' || c == end ) )
171 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100172 mbedtls_asn1_named_data* cur =
173 mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
174 (unsigned char *) data,
175 d - data );
176
177 if(cur == NULL )
Jens Wiklander817466c2018-05-22 13:49:31 +0200178 {
179 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
180 }
181
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100182 // set tagType
183 cur->val.tag = attr_descr->default_tag;
184
Jens Wiklander817466c2018-05-22 13:49:31 +0200185 while( c < end && *(c + 1) == ' ' )
186 c++;
187
188 s = c + 1;
189 in_tag = 1;
190 }
191
192 if( !in_tag && s != c + 1 )
193 {
194 *(d++) = *c;
195
196 if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
197 {
198 ret = MBEDTLS_ERR_X509_INVALID_NAME;
199 goto exit;
200 }
201 }
202
203 c++;
204 }
205
206exit:
207
208 return( ret );
209}
210
211/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
212 * to store the critical boolean for us
213 */
214int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
215 int critical, const unsigned char *val, size_t val_len )
216{
217 mbedtls_asn1_named_data *cur;
218
219 if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
220 NULL, val_len + 1 ) ) == NULL )
221 {
222 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
223 }
224
225 cur->val.p[0] = critical;
226 memcpy( cur->val.p + 1, val, val_len );
227
228 return( 0 );
229}
230
231/*
232 * RelativeDistinguishedName ::=
233 * SET OF AttributeTypeAndValue
234 *
235 * AttributeTypeAndValue ::= SEQUENCE {
236 * type AttributeType,
237 * value AttributeValue }
238 *
239 * AttributeType ::= OBJECT IDENTIFIER
240 *
241 * AttributeValue ::= ANY DEFINED BY AttributeType
242 */
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100243static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
Jens Wiklander817466c2018-05-22 13:49:31 +0200244{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200246 size_t len = 0;
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100247 const char *oid = (const char*)cur_name->oid.p;
248 size_t oid_len = cur_name->oid.len;
249 const unsigned char *name = cur_name->val.p;
250 size_t name_len = cur_name->val.len;
Jens Wiklander817466c2018-05-22 13:49:31 +0200251
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100252 // Write correct string tag and value
253 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
254 cur_name->val.tag,
255 (const char *) name,
256 name_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200257 // Write OID
258 //
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100259 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
260 oid_len ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200261
262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100263 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
264 MBEDTLS_ASN1_CONSTRUCTED |
265 MBEDTLS_ASN1_SEQUENCE ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200266
267 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100268 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
269 MBEDTLS_ASN1_CONSTRUCTED |
Jens Wiklander817466c2018-05-22 13:49:31 +0200270 MBEDTLS_ASN1_SET ) );
271
272 return( (int) len );
273}
274
275int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100276 mbedtls_asn1_named_data *first )
Jens Wiklander817466c2018-05-22 13:49:31 +0200277{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200279 size_t len = 0;
280 mbedtls_asn1_named_data *cur = first;
281
282 while( cur != NULL )
283 {
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100284 MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
Jens Wiklander817466c2018-05-22 13:49:31 +0200285 cur = cur->next;
286 }
287
288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
289 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
290 MBEDTLS_ASN1_SEQUENCE ) );
291
292 return( (int) len );
293}
294
295int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
296 const char *oid, size_t oid_len,
297 unsigned char *sig, size_t size )
298{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200300 size_t len = 0;
301
302 if( *p < start || (size_t)( *p - start ) < size )
303 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
304
305 len = size;
306 (*p) -= len;
307 memcpy( *p, sig, len );
308
309 if( *p - start < 1 )
310 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
311
312 *--(*p) = 0;
313 len += 1;
314
315 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
316 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
317
318 // Write OID
319 //
320 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
321 oid_len, 0 ) );
322
323 return( (int) len );
324}
325
326static int x509_write_extension( unsigned char **p, unsigned char *start,
327 mbedtls_asn1_named_data *ext )
328{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200329 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200330 size_t len = 0;
331
332 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
333 ext->val.len - 1 ) );
334 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
335 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
336
337 if( ext->val.p[0] != 0 )
338 {
339 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
340 }
341
342 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
343 ext->oid.len ) );
344 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
345 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
346
347 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
348 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
349 MBEDTLS_ASN1_SEQUENCE ) );
350
351 return( (int) len );
352}
353
354/*
355 * Extension ::= SEQUENCE {
356 * extnID OBJECT IDENTIFIER,
357 * critical BOOLEAN DEFAULT FALSE,
358 * extnValue OCTET STRING
359 * -- contains the DER encoding of an ASN.1 value
360 * -- corresponding to the extension type identified
361 * -- by extnID
362 * }
363 */
364int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
365 mbedtls_asn1_named_data *first )
366{
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200368 size_t len = 0;
369 mbedtls_asn1_named_data *cur_ext = first;
370
371 while( cur_ext != NULL )
372 {
373 MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
374 cur_ext = cur_ext->next;
375 }
376
377 return( (int) len );
378}
379
380#endif /* MBEDTLS_X509_CREATE_C */