blob: 0dbd679a9396772d9e9dc087c77b8eaa2a8cb88c [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 base functions for creating certificates / CSRs
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker7c6b2c32013-09-16 13:49:26 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_X509_CREATE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/x509.h"
56#include "mbedtls/asn1write.h"
57#include "mbedtls/oid.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
Rich Evans00ab4702015-02-06 13:43:58 +000059#include <string.h>
60
Hanno Beckerd2c90092018-10-08 14:32:55 +010061/* Structure linking OIDs for X.509 DN AttributeTypes to their
62 * string representations and default string encodings used by Mbed TLS. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020063typedef struct {
Hanno Beckerd2c90092018-10-08 14:32:55 +010064 const char *name; /* String representation of AttributeType, e.g.
65 * "CN" or "emailAddress". */
Hanno Beckeree334a32018-10-24 12:33:07 +010066 size_t name_len; /* Length of 'name', without trailing 0 byte. */
Hanno Beckerd2c90092018-10-08 14:32:55 +010067 const char *oid; /* String representation of OID of AttributeType,
68 * as per RFC 5280, Appendix A.1. */
Hanno Beckerd355e692018-10-08 14:42:47 +010069 int default_tag; /* The default character encoding used for the
Hanno Beckerd2c90092018-10-08 14:32:55 +010070 * given attribute type, e.g.
Hanno Beckeree334a32018-10-24 12:33:07 +010071 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020072} x509_attr_descriptor_t;
73
74#define ADD_STRLEN( s ) s, sizeof( s ) - 1
75
Hanno Becker35b68542018-10-08 14:47:38 +010076/* X.509 DN attributes from RFC 5280, Appendix A.1. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020077static const x509_attr_descriptor_t x509_attrs[] =
78{
Hanno Becker1624e2e2018-10-08 14:52:20 +010079 { ADD_STRLEN( "CN" ),
80 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
81 { ADD_STRLEN( "commonName" ),
82 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
83 { ADD_STRLEN( "C" ),
84 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
85 { ADD_STRLEN( "countryName" ),
86 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
87 { ADD_STRLEN( "O" ),
88 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
89 { ADD_STRLEN( "organizationName" ),
90 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
91 { ADD_STRLEN( "L" ),
92 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
93 { ADD_STRLEN( "locality" ),
94 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
95 { ADD_STRLEN( "R" ),
96 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
97 { ADD_STRLEN( "OU" ),
98 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
99 { ADD_STRLEN( "organizationalUnitName" ),
100 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
101 { ADD_STRLEN( "ST" ),
102 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
103 { ADD_STRLEN( "stateOrProvinceName" ),
104 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
105 { ADD_STRLEN( "emailAddress" ),
106 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
107 { ADD_STRLEN( "serialNumber" ),
108 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
109 { ADD_STRLEN( "postalAddress" ),
110 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
111 { ADD_STRLEN( "postalCode" ),
112 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
113 { ADD_STRLEN( "dnQualifier" ),
114 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
115 { ADD_STRLEN( "title" ),
116 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
117 { ADD_STRLEN( "surName" ),
118 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
119 { ADD_STRLEN( "SN" ),
120 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
121 { ADD_STRLEN( "givenName" ),
122 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
123 { ADD_STRLEN( "GN" ),
124 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
125 { ADD_STRLEN( "initials" ),
126 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
127 { ADD_STRLEN( "pseudonym" ),
128 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
129 { ADD_STRLEN( "generationQualifier" ),
130 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
131 { ADD_STRLEN( "domainComponent" ),
132 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
133 { ADD_STRLEN( "DC" ),
134 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
tdoec150f0d2018-05-18 12:12:45 +0200135 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200136};
137
thomas-deeeba6c9b2018-09-19 09:10:37 +0200138static const x509_attr_descriptor_t *x509_attr_descr_from_name( const char *name, size_t name_len )
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200139{
140 const x509_attr_descriptor_t *cur;
141
142 for( cur = x509_attrs; cur->name != NULL; cur++ )
143 if( cur->name_len == name_len &&
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200144 strncmp( cur->name, name, name_len ) == 0 )
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200145 break;
146
tdoe020c8232018-05-18 13:09:12 +0200147 if ( cur->name == NULL )
148 return( NULL );
Hanno Beckerd2c90092018-10-08 14:32:55 +0100149
Jaeden Amero23f954d2018-05-17 11:46:13 +0100150 return( cur );
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200151}
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154{
155 int ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +0100156 const char *s = name, *c = s;
157 const char *end = s + strlen( s );
Paul Bakkerfcc17212013-10-11 09:36:52 +0200158 const char *oid = NULL;
thomas-deeeba6c9b2018-09-19 09:10:37 +0200159 const x509_attr_descriptor_t* attr_descr = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160 int in_tag = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200162 char *d = data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163
164 /* Clear existing chain if present */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_asn1_free_named_data_list( head );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200166
167 while( c <= end )
168 {
169 if( in_tag && *c == '=' )
170 {
thomas-deeeba6c9b2018-09-19 09:10:37 +0200171 if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174 goto exit;
175 }
176
thomas-deeeba6c9b2018-09-19 09:10:37 +0200177 oid = attr_descr->oid;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178 s = c + 1;
179 in_tag = 0;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200180 d = data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181 }
182
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200183 if( !in_tag && *c == '\\' && c != end )
184 {
185 c++;
186
187 /* Check for valid escaped characters */
188 if( c == end || *c != ',' )
189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 ret = MBEDTLS_ERR_X509_INVALID_NAME;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200191 goto exit;
192 }
193 }
194 else if( !in_tag && ( *c == ',' || c == end ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195 {
Hanno Beckercec1c262018-10-24 12:31:45 +0100196 mbedtls_asn1_named_data* cur =
197 mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
198 (unsigned char *) data,
199 d - data );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100200
201 if(cur == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200203 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204 }
205
Jaeden Amero23f954d2018-05-17 11:46:13 +0100206 // set tagType
Hanno Beckerd355e692018-10-08 14:42:47 +0100207 cur->val.tag = attr_descr->default_tag;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100208
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209 while( c < end && *(c + 1) == ' ' )
210 c++;
211
212 s = c + 1;
213 in_tag = 1;
214 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200215
216 if( !in_tag && s != c + 1 )
217 {
218 *(d++) = *c;
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 ret = MBEDTLS_ERR_X509_INVALID_NAME;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200223 goto exit;
224 }
225 }
226
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227 c++;
228 }
229
230exit:
231
232 return( ret );
233}
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200236 * to store the critical boolean for us
237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239 int critical, const unsigned char *val, size_t val_len )
240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_asn1_named_data *cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244 NULL, val_len + 1 ) ) == NULL )
245 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200246 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247 }
248
249 cur->val.p[0] = critical;
250 memcpy( cur->val.p + 1, val, val_len );
251
252 return( 0 );
253}
254
255/*
256 * RelativeDistinguishedName ::=
257 * SET OF AttributeTypeAndValue
258 *
259 * AttributeTypeAndValue ::= SEQUENCE {
260 * type AttributeType,
261 * value AttributeValue }
262 *
263 * AttributeType ::= OBJECT IDENTIFIER
264 *
265 * AttributeValue ::= ANY DEFINED BY AttributeType
266 */
Jaeden Amero23f954d2018-05-17 11:46:13 +0100267static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268{
269 int ret;
270 size_t len = 0;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100271 const char *oid = (const char*)cur_name->oid.p;
272 size_t oid_len = cur_name->oid.len;
273 const unsigned char *name = cur_name->val.p;
274 size_t name_len = cur_name->val.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200275
Jaeden Amero23f954d2018-05-17 11:46:13 +0100276 // Write correct string tag and value
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100277 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
278 cur_name->val.tag,
279 (const char *) name,
280 name_len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281 // Write OID
282 //
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100283 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
284 oid_len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100287 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
288 MBEDTLS_ASN1_CONSTRUCTED |
289 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100292 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
293 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 MBEDTLS_ASN1_SET ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200295
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200296 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297}
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100300 mbedtls_asn1_named_data *first )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301{
302 int ret;
303 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_asn1_named_data *cur = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305
306 while( cur != NULL )
307 {
Jaeden Amero23f954d2018-05-17 11:46:13 +0100308 MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309 cur = cur->next;
310 }
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
313 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
314 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200316 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317}
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320 const char *oid, size_t oid_len,
321 unsigned char *sig, size_t size )
322{
323 int ret;
324 size_t len = 0;
325
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200326 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328
329 len = size;
330 (*p) -= len;
331 memcpy( *p, sig, len );
332
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200333 if( *p - start < 1 )
334 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
335
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 *--(*p) = 0;
337 len += 1;
338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
340 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
342 // Write OID
343 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345 oid_len, 0 ) );
346
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200347 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348}
349
350static int x509_write_extension( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_asn1_named_data *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352{
353 int ret;
354 size_t len = 0;
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 ext->val.len - 1 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
359 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 if( ext->val.p[0] != 0 )
362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 }
365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367 ext->oid.len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
369 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
372 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
373 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200375 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376}
377
378/*
379 * Extension ::= SEQUENCE {
380 * extnID OBJECT IDENTIFIER,
381 * critical BOOLEAN DEFAULT FALSE,
382 * extnValue OCTET STRING
383 * -- contains the DER encoding of an ASN.1 value
384 * -- corresponding to the extension type identified
385 * -- by extnID
386 * }
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
389 mbedtls_asn1_named_data *first )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390{
391 int ret;
392 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_asn1_named_data *cur_ext = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394
395 while( cur_ext != NULL )
396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 cur_ext = cur_ext->next;
399 }
400
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200401 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402}
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#endif /* MBEDTLS_X509_CREATE_C */