xref: /reactos/dll/3rdparty/mbedtls/x509_create.c (revision cbda039f)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  *  X.509 base functions for creating certificates / CSRs
3c2c66affSColin Finck  *
4218e2596SThomas Faber  *  Copyright The Mbed TLS Contributors
5e57126f5SThomas Faber  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6e57126f5SThomas Faber  *
7e57126f5SThomas Faber  *  This file is provided under the Apache License 2.0, or the
8e57126f5SThomas Faber  *  GNU General Public License v2.0 or later.
9e57126f5SThomas Faber  *
10e57126f5SThomas Faber  *  **********
11e57126f5SThomas Faber  *  Apache License 2.0:
12e57126f5SThomas Faber  *
13e57126f5SThomas Faber  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14e57126f5SThomas Faber  *  not use this file except in compliance with the License.
15e57126f5SThomas Faber  *  You may obtain a copy of the License at
16e57126f5SThomas Faber  *
17e57126f5SThomas Faber  *  http://www.apache.org/licenses/LICENSE-2.0
18e57126f5SThomas Faber  *
19e57126f5SThomas Faber  *  Unless required by applicable law or agreed to in writing, software
20e57126f5SThomas Faber  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21e57126f5SThomas Faber  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22e57126f5SThomas Faber  *  See the License for the specific language governing permissions and
23e57126f5SThomas Faber  *  limitations under the License.
24e57126f5SThomas Faber  *
25e57126f5SThomas Faber  *  **********
26e57126f5SThomas Faber  *
27e57126f5SThomas Faber  *  **********
28e57126f5SThomas Faber  *  GNU General Public License v2.0 or later:
29c2c66affSColin Finck  *
30c2c66affSColin Finck  *  This program is free software; you can redistribute it and/or modify
31c2c66affSColin Finck  *  it under the terms of the GNU General Public License as published by
32c2c66affSColin Finck  *  the Free Software Foundation; either version 2 of the License, or
33c2c66affSColin Finck  *  (at your option) any later version.
34c2c66affSColin Finck  *
35c2c66affSColin Finck  *  This program is distributed in the hope that it will be useful,
36c2c66affSColin Finck  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37c2c66affSColin Finck  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38c2c66affSColin Finck  *  GNU General Public License for more details.
39c2c66affSColin Finck  *
40c2c66affSColin Finck  *  You should have received a copy of the GNU General Public License along
41c2c66affSColin Finck  *  with this program; if not, write to the Free Software Foundation, Inc.,
42c2c66affSColin Finck  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43c2c66affSColin Finck  *
44e57126f5SThomas Faber  *  **********
45c2c66affSColin Finck  */
46c2c66affSColin Finck 
47c2c66affSColin Finck #if !defined(MBEDTLS_CONFIG_FILE)
48c2c66affSColin Finck #include "mbedtls/config.h"
49c2c66affSColin Finck #else
50c2c66affSColin Finck #include MBEDTLS_CONFIG_FILE
51c2c66affSColin Finck #endif
52c2c66affSColin Finck 
53c2c66affSColin Finck #if defined(MBEDTLS_X509_CREATE_C)
54c2c66affSColin Finck 
55c2c66affSColin Finck #include "mbedtls/x509.h"
56c2c66affSColin Finck #include "mbedtls/asn1write.h"
57c2c66affSColin Finck #include "mbedtls/oid.h"
58c2c66affSColin Finck 
59c2c66affSColin Finck #include <string.h>
60c2c66affSColin Finck 
61*cbda039fSThomas Faber /* Structure linking OIDs for X.509 DN AttributeTypes to their
62*cbda039fSThomas Faber  * string representations and default string encodings used by Mbed TLS. */
63c2c66affSColin Finck typedef struct {
64*cbda039fSThomas Faber    const char *name; /* String representation of AttributeType, e.g.
65*cbda039fSThomas Faber                       * "CN" or "emailAddress". */
66*cbda039fSThomas Faber    size_t name_len;  /* Length of 'name', without trailing 0 byte. */
67*cbda039fSThomas Faber    const char *oid;  /* String representation of OID of AttributeType,
68*cbda039fSThomas Faber                       * as per RFC 5280, Appendix A.1. */
69*cbda039fSThomas Faber    int default_tag;  /* The default character encoding used for the
70*cbda039fSThomas Faber                       * given attribute type, e.g.
71*cbda039fSThomas Faber                       * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
72c2c66affSColin Finck } x509_attr_descriptor_t;
73c2c66affSColin Finck 
74c2c66affSColin Finck #define ADD_STRLEN( s )     s, sizeof( s ) - 1
75c2c66affSColin Finck 
76*cbda039fSThomas Faber /* X.509 DN attributes from RFC 5280, Appendix A.1. */
77c2c66affSColin Finck static const x509_attr_descriptor_t x509_attrs[] =
78c2c66affSColin Finck {
79*cbda039fSThomas Faber     { ADD_STRLEN( "CN" ),
80*cbda039fSThomas Faber       MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
81*cbda039fSThomas Faber     { ADD_STRLEN( "commonName" ),
82*cbda039fSThomas Faber       MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
83*cbda039fSThomas Faber     { ADD_STRLEN( "C" ),
84*cbda039fSThomas Faber       MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
85*cbda039fSThomas Faber     { ADD_STRLEN( "countryName" ),
86*cbda039fSThomas Faber       MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
87*cbda039fSThomas Faber     { ADD_STRLEN( "O" ),
88*cbda039fSThomas Faber       MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
89*cbda039fSThomas Faber     { ADD_STRLEN( "organizationName" ),
90*cbda039fSThomas Faber       MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
91*cbda039fSThomas Faber     { ADD_STRLEN( "L" ),
92*cbda039fSThomas Faber       MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
93*cbda039fSThomas Faber     { ADD_STRLEN( "locality" ),
94*cbda039fSThomas Faber       MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
95*cbda039fSThomas Faber     { ADD_STRLEN( "R" ),
96*cbda039fSThomas Faber       MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
97*cbda039fSThomas Faber     { ADD_STRLEN( "OU" ),
98*cbda039fSThomas Faber       MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
99*cbda039fSThomas Faber     { ADD_STRLEN( "organizationalUnitName" ),
100*cbda039fSThomas Faber       MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
101*cbda039fSThomas Faber     { ADD_STRLEN( "ST" ),
102*cbda039fSThomas Faber       MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
103*cbda039fSThomas Faber     { ADD_STRLEN( "stateOrProvinceName" ),
104*cbda039fSThomas Faber       MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
105*cbda039fSThomas Faber     { ADD_STRLEN( "emailAddress" ),
106*cbda039fSThomas Faber       MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
107*cbda039fSThomas Faber     { ADD_STRLEN( "serialNumber" ),
108*cbda039fSThomas Faber       MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
109*cbda039fSThomas Faber     { ADD_STRLEN( "postalAddress" ),
110*cbda039fSThomas Faber       MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
111*cbda039fSThomas Faber     { ADD_STRLEN( "postalCode" ),
112*cbda039fSThomas Faber       MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
113*cbda039fSThomas Faber     { ADD_STRLEN( "dnQualifier" ),
114*cbda039fSThomas Faber       MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
115*cbda039fSThomas Faber     { ADD_STRLEN( "title" ),
116*cbda039fSThomas Faber       MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
117*cbda039fSThomas Faber     { ADD_STRLEN( "surName" ),
118*cbda039fSThomas Faber       MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
119*cbda039fSThomas Faber     { ADD_STRLEN( "SN" ),
120*cbda039fSThomas Faber       MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
121*cbda039fSThomas Faber     { ADD_STRLEN( "givenName" ),
122*cbda039fSThomas Faber       MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
123*cbda039fSThomas Faber     { ADD_STRLEN( "GN" ),
124*cbda039fSThomas Faber       MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
125*cbda039fSThomas Faber     { ADD_STRLEN( "initials" ),
126*cbda039fSThomas Faber       MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
127*cbda039fSThomas Faber     { ADD_STRLEN( "pseudonym" ),
128*cbda039fSThomas Faber       MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
129*cbda039fSThomas Faber     { ADD_STRLEN( "generationQualifier" ),
130*cbda039fSThomas Faber       MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
131*cbda039fSThomas Faber     { ADD_STRLEN( "domainComponent" ),
132*cbda039fSThomas Faber       MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
133*cbda039fSThomas Faber     { ADD_STRLEN( "DC" ),
134*cbda039fSThomas Faber       MBEDTLS_OID_DOMAIN_COMPONENT,   MBEDTLS_ASN1_IA5_STRING },
135*cbda039fSThomas Faber     { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
136c2c66affSColin Finck };
137c2c66affSColin Finck 
x509_attr_descr_from_name(const char * name,size_t name_len)138*cbda039fSThomas Faber static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
139c2c66affSColin Finck {
140c2c66affSColin Finck     const x509_attr_descriptor_t *cur;
141c2c66affSColin Finck 
142c2c66affSColin Finck     for( cur = x509_attrs; cur->name != NULL; cur++ )
143c2c66affSColin Finck         if( cur->name_len == name_len &&
144c2c66affSColin Finck             strncmp( cur->name, name, name_len ) == 0 )
145c2c66affSColin Finck             break;
146c2c66affSColin Finck 
147*cbda039fSThomas Faber     if ( cur->name == NULL )
148*cbda039fSThomas Faber         return( NULL );
149*cbda039fSThomas Faber 
150*cbda039fSThomas Faber     return( cur );
151c2c66affSColin Finck }
152c2c66affSColin Finck 
mbedtls_x509_string_to_names(mbedtls_asn1_named_data ** head,const char * name)153c2c66affSColin Finck int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
154c2c66affSColin Finck {
155c2c66affSColin Finck     int ret = 0;
156c2c66affSColin Finck     const char *s = name, *c = s;
157c2c66affSColin Finck     const char *end = s + strlen( s );
158c2c66affSColin Finck     const char *oid = NULL;
159*cbda039fSThomas Faber     const x509_attr_descriptor_t* attr_descr = NULL;
160c2c66affSColin Finck     int in_tag = 1;
161c2c66affSColin Finck     char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
162c2c66affSColin Finck     char *d = data;
163c2c66affSColin Finck 
164c2c66affSColin Finck     /* Clear existing chain if present */
165c2c66affSColin Finck     mbedtls_asn1_free_named_data_list( head );
166c2c66affSColin Finck 
167c2c66affSColin Finck     while( c <= end )
168c2c66affSColin Finck     {
169c2c66affSColin Finck         if( in_tag && *c == '=' )
170c2c66affSColin Finck         {
171*cbda039fSThomas Faber             if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
172c2c66affSColin Finck             {
173c2c66affSColin Finck                 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
174c2c66affSColin Finck                 goto exit;
175c2c66affSColin Finck             }
176c2c66affSColin Finck 
177*cbda039fSThomas Faber             oid = attr_descr->oid;
178c2c66affSColin Finck             s = c + 1;
179c2c66affSColin Finck             in_tag = 0;
180c2c66affSColin Finck             d = data;
181c2c66affSColin Finck         }
182c2c66affSColin Finck 
183c2c66affSColin Finck         if( !in_tag && *c == '\\' && c != end )
184c2c66affSColin Finck         {
185c2c66affSColin Finck             c++;
186c2c66affSColin Finck 
187c2c66affSColin Finck             /* Check for valid escaped characters */
188c2c66affSColin Finck             if( c == end || *c != ',' )
189c2c66affSColin Finck             {
190c2c66affSColin Finck                 ret = MBEDTLS_ERR_X509_INVALID_NAME;
191c2c66affSColin Finck                 goto exit;
192c2c66affSColin Finck             }
193c2c66affSColin Finck         }
194c2c66affSColin Finck         else if( !in_tag && ( *c == ',' || c == end ) )
195c2c66affSColin Finck         {
196*cbda039fSThomas Faber             mbedtls_asn1_named_data* cur =
197*cbda039fSThomas Faber                 mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
198c2c66affSColin Finck                                                (unsigned char *) data,
199*cbda039fSThomas Faber                                                d - data );
200*cbda039fSThomas Faber 
201*cbda039fSThomas Faber             if(cur == NULL )
202c2c66affSColin Finck             {
203c2c66affSColin Finck                 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
204c2c66affSColin Finck             }
205c2c66affSColin Finck 
206*cbda039fSThomas Faber             // set tagType
207*cbda039fSThomas Faber             cur->val.tag = attr_descr->default_tag;
208*cbda039fSThomas Faber 
209c2c66affSColin Finck             while( c < end && *(c + 1) == ' ' )
210c2c66affSColin Finck                 c++;
211c2c66affSColin Finck 
212c2c66affSColin Finck             s = c + 1;
213c2c66affSColin Finck             in_tag = 1;
214c2c66affSColin Finck         }
215c2c66affSColin Finck 
216c2c66affSColin Finck         if( !in_tag && s != c + 1 )
217c2c66affSColin Finck         {
218c2c66affSColin Finck             *(d++) = *c;
219c2c66affSColin Finck 
220c2c66affSColin Finck             if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
221c2c66affSColin Finck             {
222c2c66affSColin Finck                 ret = MBEDTLS_ERR_X509_INVALID_NAME;
223c2c66affSColin Finck                 goto exit;
224c2c66affSColin Finck             }
225c2c66affSColin Finck         }
226c2c66affSColin Finck 
227c2c66affSColin Finck         c++;
228c2c66affSColin Finck     }
229c2c66affSColin Finck 
230c2c66affSColin Finck exit:
231c2c66affSColin Finck 
232c2c66affSColin Finck     return( ret );
233c2c66affSColin Finck }
234c2c66affSColin Finck 
235c2c66affSColin Finck /* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
236c2c66affSColin Finck  * to store the critical boolean for us
237c2c66affSColin Finck  */
mbedtls_x509_set_extension(mbedtls_asn1_named_data ** head,const char * oid,size_t oid_len,int critical,const unsigned char * val,size_t val_len)238c2c66affSColin Finck int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
239c2c66affSColin Finck                         int critical, const unsigned char *val, size_t val_len )
240c2c66affSColin Finck {
241c2c66affSColin Finck     mbedtls_asn1_named_data *cur;
242c2c66affSColin Finck 
243c2c66affSColin Finck     if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
244c2c66affSColin Finck                                        NULL, val_len + 1 ) ) == NULL )
245c2c66affSColin Finck     {
246c2c66affSColin Finck         return( MBEDTLS_ERR_X509_ALLOC_FAILED );
247c2c66affSColin Finck     }
248c2c66affSColin Finck 
249c2c66affSColin Finck     cur->val.p[0] = critical;
250c2c66affSColin Finck     memcpy( cur->val.p + 1, val, val_len );
251c2c66affSColin Finck 
252c2c66affSColin Finck     return( 0 );
253c2c66affSColin Finck }
254c2c66affSColin Finck 
255c2c66affSColin Finck /*
256c2c66affSColin Finck  *  RelativeDistinguishedName ::=
257c2c66affSColin Finck  *    SET OF AttributeTypeAndValue
258c2c66affSColin Finck  *
259c2c66affSColin Finck  *  AttributeTypeAndValue ::= SEQUENCE {
260c2c66affSColin Finck  *    type     AttributeType,
261c2c66affSColin Finck  *    value    AttributeValue }
262c2c66affSColin Finck  *
263c2c66affSColin Finck  *  AttributeType ::= OBJECT IDENTIFIER
264c2c66affSColin Finck  *
265c2c66affSColin Finck  *  AttributeValue ::= ANY DEFINED BY AttributeType
266c2c66affSColin Finck  */
x509_write_name(unsigned char ** p,unsigned char * start,mbedtls_asn1_named_data * cur_name)267*cbda039fSThomas Faber static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
268c2c66affSColin Finck {
269c2c66affSColin Finck     int ret;
270c2c66affSColin Finck     size_t len = 0;
271*cbda039fSThomas Faber     const char *oid             = (const char*)cur_name->oid.p;
272*cbda039fSThomas Faber     size_t oid_len              = cur_name->oid.len;
273*cbda039fSThomas Faber     const unsigned char *name   = cur_name->val.p;
274*cbda039fSThomas Faber     size_t name_len             = cur_name->val.len;
275c2c66affSColin Finck 
276*cbda039fSThomas Faber     // Write correct string tag and value
277*cbda039fSThomas Faber     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
278*cbda039fSThomas Faber                                                        cur_name->val.tag,
279c2c66affSColin Finck                                                        (const char *) name,
280c2c66affSColin Finck                                                        name_len ) );
281c2c66affSColin Finck     // Write OID
282c2c66affSColin Finck     //
283*cbda039fSThomas Faber     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
284*cbda039fSThomas Faber                                                        oid_len ) );
285c2c66affSColin Finck 
286c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
287*cbda039fSThomas Faber     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
288*cbda039fSThomas Faber                                                     MBEDTLS_ASN1_CONSTRUCTED |
289c2c66affSColin Finck                                                     MBEDTLS_ASN1_SEQUENCE ) );
290c2c66affSColin Finck 
291c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
292*cbda039fSThomas Faber     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
293*cbda039fSThomas Faber                                                  MBEDTLS_ASN1_CONSTRUCTED |
294c2c66affSColin Finck                                                  MBEDTLS_ASN1_SET ) );
295c2c66affSColin Finck 
296c2c66affSColin Finck     return( (int) len );
297c2c66affSColin Finck }
298c2c66affSColin Finck 
mbedtls_x509_write_names(unsigned char ** p,unsigned char * start,mbedtls_asn1_named_data * first)299c2c66affSColin Finck int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
300c2c66affSColin Finck                               mbedtls_asn1_named_data *first )
301c2c66affSColin Finck {
302c2c66affSColin Finck     int ret;
303c2c66affSColin Finck     size_t len = 0;
304c2c66affSColin Finck     mbedtls_asn1_named_data *cur = first;
305c2c66affSColin Finck 
306c2c66affSColin Finck     while( cur != NULL )
307c2c66affSColin Finck     {
308*cbda039fSThomas Faber         MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
309c2c66affSColin Finck         cur = cur->next;
310c2c66affSColin Finck     }
311c2c66affSColin Finck 
312c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
313c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
314c2c66affSColin Finck                                                  MBEDTLS_ASN1_SEQUENCE ) );
315c2c66affSColin Finck 
316c2c66affSColin Finck     return( (int) len );
317c2c66affSColin Finck }
318c2c66affSColin Finck 
mbedtls_x509_write_sig(unsigned char ** p,unsigned char * start,const char * oid,size_t oid_len,unsigned char * sig,size_t size)319c2c66affSColin Finck int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
320c2c66affSColin Finck                     const char *oid, size_t oid_len,
321c2c66affSColin Finck                     unsigned char *sig, size_t size )
322c2c66affSColin Finck {
323c2c66affSColin Finck     int ret;
324c2c66affSColin Finck     size_t len = 0;
325c2c66affSColin Finck 
326c2c66affSColin Finck     if( *p < start || (size_t)( *p - start ) < size )
327c2c66affSColin Finck         return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
328c2c66affSColin Finck 
329c2c66affSColin Finck     len = size;
330c2c66affSColin Finck     (*p) -= len;
331c2c66affSColin Finck     memcpy( *p, sig, len );
332c2c66affSColin Finck 
333c2c66affSColin Finck     if( *p - start < 1 )
334c2c66affSColin Finck         return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
335c2c66affSColin Finck 
336c2c66affSColin Finck     *--(*p) = 0;
337c2c66affSColin Finck     len += 1;
338c2c66affSColin Finck 
339c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
340c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
341c2c66affSColin Finck 
342c2c66affSColin Finck     // Write OID
343c2c66affSColin Finck     //
344c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
345c2c66affSColin Finck                                                         oid_len, 0 ) );
346c2c66affSColin Finck 
347c2c66affSColin Finck     return( (int) len );
348c2c66affSColin Finck }
349c2c66affSColin Finck 
x509_write_extension(unsigned char ** p,unsigned char * start,mbedtls_asn1_named_data * ext)350c2c66affSColin Finck static int x509_write_extension( unsigned char **p, unsigned char *start,
351c2c66affSColin Finck                                  mbedtls_asn1_named_data *ext )
352c2c66affSColin Finck {
353c2c66affSColin Finck     int ret;
354c2c66affSColin Finck     size_t len = 0;
355c2c66affSColin Finck 
356c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
357c2c66affSColin Finck                                               ext->val.len - 1 ) );
358c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
359c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
360c2c66affSColin Finck 
361c2c66affSColin Finck     if( ext->val.p[0] != 0 )
362c2c66affSColin Finck     {
363c2c66affSColin Finck         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
364c2c66affSColin Finck     }
365c2c66affSColin Finck 
366c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
367c2c66affSColin Finck                                               ext->oid.len ) );
368c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
369c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
370c2c66affSColin Finck 
371c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
372c2c66affSColin Finck     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
373c2c66affSColin Finck                                                  MBEDTLS_ASN1_SEQUENCE ) );
374c2c66affSColin Finck 
375c2c66affSColin Finck     return( (int) len );
376c2c66affSColin Finck }
377c2c66affSColin Finck 
378c2c66affSColin Finck /*
379c2c66affSColin Finck  * Extension  ::=  SEQUENCE  {
380c2c66affSColin Finck  *     extnID      OBJECT IDENTIFIER,
381c2c66affSColin Finck  *     critical    BOOLEAN DEFAULT FALSE,
382c2c66affSColin Finck  *     extnValue   OCTET STRING
383c2c66affSColin Finck  *                 -- contains the DER encoding of an ASN.1 value
384c2c66affSColin Finck  *                 -- corresponding to the extension type identified
385c2c66affSColin Finck  *                 -- by extnID
386c2c66affSColin Finck  *     }
387c2c66affSColin Finck  */
mbedtls_x509_write_extensions(unsigned char ** p,unsigned char * start,mbedtls_asn1_named_data * first)388c2c66affSColin Finck int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
389c2c66affSColin Finck                            mbedtls_asn1_named_data *first )
390c2c66affSColin Finck {
391c2c66affSColin Finck     int ret;
392c2c66affSColin Finck     size_t len = 0;
393c2c66affSColin Finck     mbedtls_asn1_named_data *cur_ext = first;
394c2c66affSColin Finck 
395c2c66affSColin Finck     while( cur_ext != NULL )
396c2c66affSColin Finck     {
397c2c66affSColin Finck         MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
398c2c66affSColin Finck         cur_ext = cur_ext->next;
399c2c66affSColin Finck     }
400c2c66affSColin Finck 
401c2c66affSColin Finck     return( (int) len );
402c2c66affSColin Finck }
403c2c66affSColin Finck 
404c2c66affSColin Finck #endif /* MBEDTLS_X509_CREATE_C */
405