xref: /optee_os/lib/libmbedtls/mbedtls/library/x509_create.c (revision 11fa71b9ddb429088f325cfda430183003ccd1db)
1 // SPDX-License-Identifier: Apache-2.0
2 /*
3  *  X.509 base functions for creating certificates / CSRs
4  *
5  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
6  *
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"
32 #include "mbedtls/error.h"
33 #include "mbedtls/oid.h"
34 
35 #include <string.h>
36 
37 /* Structure linking OIDs for X.509 DN AttributeTypes to their
38  * string representations and default string encodings used by Mbed TLS. */
39 typedef struct {
40    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. */
48 } x509_attr_descriptor_t;
49 
50 #define ADD_STRLEN( s )     s, sizeof( s ) - 1
51 
52 /* X.509 DN attributes from RFC 5280, Appendix A.1. */
53 static const x509_attr_descriptor_t x509_attrs[] =
54 {
55     { 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 }
112 };
113 
114 static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
115 {
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 
123     if ( cur->name == NULL )
124         return( NULL );
125 
126     return( cur );
127 }
128 
129 int 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;
135     const x509_attr_descriptor_t* attr_descr = NULL;
136     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         {
147             if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
148             {
149                 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
150                 goto exit;
151             }
152 
153             oid = attr_descr->oid;
154             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         {
172             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 )
178             {
179                 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
180             }
181 
182             // set tagType
183             cur->val.tag = attr_descr->default_tag;
184 
185             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 
206 exit:
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  */
214 int 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  */
243 static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
244 {
245     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
246     size_t len = 0;
247     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;
251 
252     // 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 ) );
257     // Write OID
258     //
259     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
260                                                        oid_len ) );
261 
262     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
263     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
264                                                     MBEDTLS_ASN1_CONSTRUCTED |
265                                                     MBEDTLS_ASN1_SEQUENCE ) );
266 
267     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
268     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
269                                                  MBEDTLS_ASN1_CONSTRUCTED |
270                                                  MBEDTLS_ASN1_SET ) );
271 
272     return( (int) len );
273 }
274 
275 int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
276                               mbedtls_asn1_named_data *first )
277 {
278     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
279     size_t len = 0;
280     mbedtls_asn1_named_data *cur = first;
281 
282     while( cur != NULL )
283     {
284         MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
285         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 
295 int 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 {
299     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
300     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 
326 static int x509_write_extension( unsigned char **p, unsigned char *start,
327                                  mbedtls_asn1_named_data *ext )
328 {
329     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
330     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  */
364 int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
365                            mbedtls_asn1_named_data *first )
366 {
367     int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
368     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 */
381