blob: 75de91f6c8545da0d26211f460353c1e31118281 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 base functions for creating certificates / CSRs
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_X509_CREATE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/x509.h"
58#include "mbedtls/asn1write.h"
59#include "mbedtls/oid.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <string.h>
62
Hanno Beckerd2c90092018-10-08 14:32:55 +010063/* Structure linking OIDs for X.509 DN AttributeTypes to their
64 * string representations and default string encodings used by Mbed TLS. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020065typedef struct {
Hanno Beckerd2c90092018-10-08 14:32:55 +010066 const char *name; /* String representation of AttributeType, e.g.
67 * "CN" or "emailAddress". */
Hanno Beckeree334a32018-10-24 12:33:07 +010068 size_t name_len; /* Length of 'name', without trailing 0 byte. */
Hanno Beckerd2c90092018-10-08 14:32:55 +010069 const char *oid; /* String representation of OID of AttributeType,
70 * as per RFC 5280, Appendix A.1. */
Hanno Beckerd355e692018-10-08 14:42:47 +010071 int default_tag; /* The default character encoding used for the
Hanno Beckerd2c90092018-10-08 14:32:55 +010072 * given attribute type, e.g.
Hanno Beckeree334a32018-10-24 12:33:07 +010073 * MBEDTLS_ASN1_UTF8_STRING for UTF-8. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020074} x509_attr_descriptor_t;
75
76#define ADD_STRLEN( s ) s, sizeof( s ) - 1
77
Hanno Becker35b68542018-10-08 14:47:38 +010078/* X.509 DN attributes from RFC 5280, Appendix A.1. */
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +020079static const x509_attr_descriptor_t x509_attrs[] =
80{
Hanno Becker1624e2e2018-10-08 14:52:20 +010081 { ADD_STRLEN( "CN" ),
82 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
83 { ADD_STRLEN( "commonName" ),
84 MBEDTLS_OID_AT_CN, MBEDTLS_ASN1_UTF8_STRING },
85 { ADD_STRLEN( "C" ),
86 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
87 { ADD_STRLEN( "countryName" ),
88 MBEDTLS_OID_AT_COUNTRY, MBEDTLS_ASN1_PRINTABLE_STRING },
89 { ADD_STRLEN( "O" ),
90 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
91 { ADD_STRLEN( "organizationName" ),
92 MBEDTLS_OID_AT_ORGANIZATION, MBEDTLS_ASN1_UTF8_STRING },
93 { ADD_STRLEN( "L" ),
94 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
95 { ADD_STRLEN( "locality" ),
96 MBEDTLS_OID_AT_LOCALITY, MBEDTLS_ASN1_UTF8_STRING },
97 { ADD_STRLEN( "R" ),
98 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
99 { ADD_STRLEN( "OU" ),
100 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
101 { ADD_STRLEN( "organizationalUnitName" ),
102 MBEDTLS_OID_AT_ORG_UNIT, MBEDTLS_ASN1_UTF8_STRING },
103 { ADD_STRLEN( "ST" ),
104 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
105 { ADD_STRLEN( "stateOrProvinceName" ),
106 MBEDTLS_OID_AT_STATE, MBEDTLS_ASN1_UTF8_STRING },
107 { ADD_STRLEN( "emailAddress" ),
108 MBEDTLS_OID_PKCS9_EMAIL, MBEDTLS_ASN1_IA5_STRING },
109 { ADD_STRLEN( "serialNumber" ),
110 MBEDTLS_OID_AT_SERIAL_NUMBER, MBEDTLS_ASN1_PRINTABLE_STRING },
111 { ADD_STRLEN( "postalAddress" ),
112 MBEDTLS_OID_AT_POSTAL_ADDRESS, MBEDTLS_ASN1_PRINTABLE_STRING },
113 { ADD_STRLEN( "postalCode" ),
114 MBEDTLS_OID_AT_POSTAL_CODE, MBEDTLS_ASN1_PRINTABLE_STRING },
115 { ADD_STRLEN( "dnQualifier" ),
116 MBEDTLS_OID_AT_DN_QUALIFIER, MBEDTLS_ASN1_PRINTABLE_STRING },
117 { ADD_STRLEN( "title" ),
118 MBEDTLS_OID_AT_TITLE, MBEDTLS_ASN1_UTF8_STRING },
119 { ADD_STRLEN( "surName" ),
120 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
121 { ADD_STRLEN( "SN" ),
122 MBEDTLS_OID_AT_SUR_NAME, MBEDTLS_ASN1_UTF8_STRING },
123 { ADD_STRLEN( "givenName" ),
124 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
125 { ADD_STRLEN( "GN" ),
126 MBEDTLS_OID_AT_GIVEN_NAME, MBEDTLS_ASN1_UTF8_STRING },
127 { ADD_STRLEN( "initials" ),
128 MBEDTLS_OID_AT_INITIALS, MBEDTLS_ASN1_UTF8_STRING },
129 { ADD_STRLEN( "pseudonym" ),
130 MBEDTLS_OID_AT_PSEUDONYM, MBEDTLS_ASN1_UTF8_STRING },
131 { ADD_STRLEN( "generationQualifier" ),
132 MBEDTLS_OID_AT_GENERATION_QUALIFIER, MBEDTLS_ASN1_UTF8_STRING },
133 { ADD_STRLEN( "domainComponent" ),
134 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
135 { ADD_STRLEN( "DC" ),
136 MBEDTLS_OID_DOMAIN_COMPONENT, MBEDTLS_ASN1_IA5_STRING },
tdoec150f0d2018-05-18 12:12:45 +0200137 { NULL, 0, NULL, MBEDTLS_ASN1_NULL }
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200138};
139
thomas-deeeba6c9b2018-09-19 09:10:37 +0200140static 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 +0200141{
142 const x509_attr_descriptor_t *cur;
143
144 for( cur = x509_attrs; cur->name != NULL; cur++ )
145 if( cur->name_len == name_len &&
Manuel Pégourié-Gonnardcb46fd82015-05-28 17:06:07 +0200146 strncmp( cur->name, name, name_len ) == 0 )
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200147 break;
148
tdoe020c8232018-05-18 13:09:12 +0200149 if ( cur->name == NULL )
150 return( NULL );
Hanno Beckerd2c90092018-10-08 14:32:55 +0100151
Jaeden Amero23f954d2018-05-17 11:46:13 +0100152 return( cur );
Manuel Pégourié-Gonnardf3e5c222014-06-12 11:06:36 +0200153}
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_x509_string_to_names( mbedtls_asn1_named_data **head, const char *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200156{
157 int ret = 0;
Paul Bakker50dc8502013-10-28 21:19:10 +0100158 const char *s = name, *c = s;
159 const char *end = s + strlen( s );
Paul Bakkerfcc17212013-10-11 09:36:52 +0200160 const char *oid = NULL;
thomas-deeeba6c9b2018-09-19 09:10:37 +0200161 const x509_attr_descriptor_t* attr_descr = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162 int in_tag = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 char data[MBEDTLS_X509_MAX_DN_NAME_SIZE];
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200164 char *d = data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200165
166 /* Clear existing chain if present */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 mbedtls_asn1_free_named_data_list( head );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168
169 while( c <= end )
170 {
171 if( in_tag && *c == '=' )
172 {
thomas-deeeba6c9b2018-09-19 09:10:37 +0200173 if( ( attr_descr = x509_attr_descr_from_name( s, c - s ) ) == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 ret = MBEDTLS_ERR_X509_UNKNOWN_OID;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176 goto exit;
177 }
178
thomas-deeeba6c9b2018-09-19 09:10:37 +0200179 oid = attr_descr->oid;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180 s = c + 1;
181 in_tag = 0;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200182 d = data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200183 }
184
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200185 if( !in_tag && *c == '\\' && c != end )
186 {
187 c++;
188
189 /* Check for valid escaped characters */
190 if( c == end || *c != ',' )
191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 ret = MBEDTLS_ERR_X509_INVALID_NAME;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200193 goto exit;
194 }
195 }
196 else if( !in_tag && ( *c == ',' || c == end ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197 {
Hanno Beckercec1c262018-10-24 12:31:45 +0100198 mbedtls_asn1_named_data* cur =
199 mbedtls_asn1_store_named_data( head, oid, strlen( oid ),
200 (unsigned char *) data,
201 d - data );
Jaeden Amero23f954d2018-05-17 11:46:13 +0100202
203 if(cur == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200205 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206 }
207
Jaeden Amero23f954d2018-05-17 11:46:13 +0100208 // set tagType
Hanno Beckerd355e692018-10-08 14:42:47 +0100209 cur->val.tag = attr_descr->default_tag;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100210
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211 while( c < end && *(c + 1) == ' ' )
212 c++;
213
214 s = c + 1;
215 in_tag = 1;
216 }
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200217
218 if( !in_tag && s != c + 1 )
219 {
220 *(d++) = *c;
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( d - data == MBEDTLS_X509_MAX_DN_NAME_SIZE )
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 ret = MBEDTLS_ERR_X509_INVALID_NAME;
Paul Bakker8dcb2d72014-08-08 12:22:30 +0200225 goto exit;
226 }
227 }
228
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200229 c++;
230 }
231
232exit:
233
234 return( ret );
235}
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237/* The first byte of the value in the mbedtls_asn1_named_data structure is reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238 * to store the critical boolean for us
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_x509_set_extension( mbedtls_asn1_named_data **head, const char *oid, size_t oid_len,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241 int critical, const unsigned char *val, size_t val_len )
242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_asn1_named_data *cur;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( cur = mbedtls_asn1_store_named_data( head, oid, oid_len,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246 NULL, val_len + 1 ) ) == NULL )
247 {
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200248 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249 }
250
251 cur->val.p[0] = critical;
252 memcpy( cur->val.p + 1, val, val_len );
253
254 return( 0 );
255}
256
257/*
258 * RelativeDistinguishedName ::=
259 * SET OF AttributeTypeAndValue
260 *
261 * AttributeTypeAndValue ::= SEQUENCE {
262 * type AttributeType,
263 * value AttributeValue }
264 *
265 * AttributeType ::= OBJECT IDENTIFIER
266 *
267 * AttributeValue ::= ANY DEFINED BY AttributeType
268 */
Jaeden Amero23f954d2018-05-17 11:46:13 +0100269static int x509_write_name( unsigned char **p, unsigned char *start, mbedtls_asn1_named_data* cur_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270{
271 int ret;
272 size_t len = 0;
Jaeden Amero23f954d2018-05-17 11:46:13 +0100273 const char *oid = (const char*)cur_name->oid.p;
274 size_t oid_len = cur_name->oid.len;
275 const unsigned char *name = cur_name->val.p;
276 size_t name_len = cur_name->val.len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277
Jaeden Amero23f954d2018-05-17 11:46:13 +0100278 // Write correct string tag and value
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100279 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tagged_string( p, start,
280 cur_name->val.tag,
281 (const char *) name,
282 name_len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 // Write OID
284 //
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100285 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid,
286 oid_len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100289 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
290 MBEDTLS_ASN1_CONSTRUCTED |
291 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100294 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
295 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 MBEDTLS_ASN1_SET ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200298 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299}
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_x509_write_names( unsigned char **p, unsigned char *start,
Hanno Beckercfc47ba2018-10-08 14:45:42 +0100302 mbedtls_asn1_named_data *first )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303{
304 int ret;
305 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_asn1_named_data *cur = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307
308 while( cur != NULL )
309 {
Jaeden Amero23f954d2018-05-17 11:46:13 +0100310 MBEDTLS_ASN1_CHK_ADD( len, x509_write_name( p, start, cur ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 cur = cur->next;
312 }
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
315 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
316 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200318 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319}
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321int mbedtls_x509_write_sig( unsigned char **p, unsigned char *start,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322 const char *oid, size_t oid_len,
323 unsigned char *sig, size_t size )
324{
325 int ret;
326 size_t len = 0;
327
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200328 if( *p < start || (size_t)( *p - start ) < size )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330
331 len = size;
332 (*p) -= len;
333 memcpy( *p, sig, len );
334
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200335 if( *p - start < 1 )
336 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
337
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338 *--(*p) = 0;
339 len += 1;
340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 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_BIT_STRING ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 // Write OID
345 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( p, start, oid,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 oid_len, 0 ) );
348
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200349 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350}
351
352static int x509_write_extension( unsigned char **p, unsigned char *start,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 mbedtls_asn1_named_data *ext )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354{
355 int ret;
356 size_t len = 0;
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->val.p + 1,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 ext->val.len - 1 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->val.len - 1 ) );
361 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362
363 if( ext->val.p[0] != 0 )
364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( p, start, 1 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200366 }
367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, ext->oid.p,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 ext->oid.len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, ext->oid.len ) );
371 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_OID ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
374 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
375 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200377 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378}
379
380/*
381 * Extension ::= SEQUENCE {
382 * extnID OBJECT IDENTIFIER,
383 * critical BOOLEAN DEFAULT FALSE,
384 * extnValue OCTET STRING
385 * -- contains the DER encoding of an ASN.1 value
386 * -- corresponding to the extension type identified
387 * -- by extnID
388 * }
389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390int mbedtls_x509_write_extensions( unsigned char **p, unsigned char *start,
391 mbedtls_asn1_named_data *first )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392{
393 int ret;
394 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_asn1_named_data *cur_ext = first;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396
397 while( cur_ext != NULL )
398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 MBEDTLS_ASN1_CHK_ADD( len, x509_write_extension( p, start, cur_ext ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400 cur_ext = cur_ext->next;
401 }
402
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200403 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404}
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#endif /* MBEDTLS_X509_CREATE_C */