blob: 8ed3468c75c7230fab9826eb682ad85414b7760e [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ITU-T X.509 standard defines a certificate format for PKI.
24 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020025 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028 *
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
31 */
32
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020040
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/x509_crt.h"
42#include "mbedtls/oid.h"
Rich Evans00ab4702015-02-06 13:43:58 +000043
44#include <stdio.h>
45#include <string.h>
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pem.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
Rich Evans00ab4702015-02-06 13:43:58 +000054#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020056#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_snprintf snprintf
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#endif
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_THREADING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000061#include "mbedtls/threading.h"
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010062#endif
63
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#include <windows.h>
66#else
67#include <time.h>
68#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_FS_IO)
Rich Evans00ab4702015-02-06 13:43:58 +000071#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020072#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#include <sys/types.h>
74#include <sys/stat.h>
75#include <dirent.h>
Rich Evans00ab4702015-02-06 13:43:58 +000076#endif /* !_WIN32 || EFIX64 || EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#endif
78
Paul Bakker34617722014-06-13 17:20:13 +020079/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020081 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
82}
83
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084/*
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020085 * Default profile
86 */
87static const mbedtls_md_type_t x509_prof_default_mds[] =
88{
89 MBEDTLS_MD_SHA1,
90 MBEDTLS_MD_RIPEMD160,
91 MBEDTLS_MD_SHA224,
92 MBEDTLS_MD_SHA256,
93 MBEDTLS_MD_SHA384,
94 MBEDTLS_MD_SHA512,
95 MBEDTLS_MD_NONE
96};
97
98static const mbedtls_pk_type_t x509_prof_default_pks[] =
99{
100 MBEDTLS_PK_RSA,
101 MBEDTLS_PK_ECDSA,
102 MBEDTLS_PK_NONE
103};
104
105#if defined(MBEDTLS_ECP_C)
106static const mbedtls_ecp_group_id x509_prof_default_curves[] =
107{
108 MBEDTLS_ECP_DP_SECP192R1,
109 MBEDTLS_ECP_DP_SECP224R1,
110 MBEDTLS_ECP_DP_SECP256R1,
111 MBEDTLS_ECP_DP_SECP384R1,
112 MBEDTLS_ECP_DP_SECP521R1,
113 MBEDTLS_ECP_DP_BP256R1,
114 MBEDTLS_ECP_DP_BP384R1,
115 MBEDTLS_ECP_DP_BP512R1,
116 MBEDTLS_ECP_DP_SECP192K1,
117 MBEDTLS_ECP_DP_SECP224K1,
118 MBEDTLS_ECP_DP_SECP256K1,
119};
120#else
121static const mbedtls_ecp_group_id *x509_prof_default_curves = NULL;
122#endif
123
124const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
125{
126 x509_prof_default_mds,
127 x509_prof_default_pks,
128 x509_prof_default_curves,
129 2048,
130};
131
132/*
133 * Next-default profile
134 */
135static const mbedtls_md_type_t x509_prof_next_mds[] =
136{
137 MBEDTLS_MD_SHA256,
138 MBEDTLS_MD_SHA384,
139 MBEDTLS_MD_SHA512,
140 MBEDTLS_MD_NONE
141};
142
143#if defined(MBEDTLS_ECP_C)
144static const mbedtls_ecp_group_id x509_prof_next_curves[] =
145{
146 MBEDTLS_ECP_DP_SECP256R1,
147 MBEDTLS_ECP_DP_SECP384R1,
148 MBEDTLS_ECP_DP_SECP521R1,
149 MBEDTLS_ECP_DP_BP256R1,
150 MBEDTLS_ECP_DP_BP384R1,
151 MBEDTLS_ECP_DP_BP512R1,
152 MBEDTLS_ECP_DP_SECP256K1,
153};
154#else
155static const mbedtls_ecp_group_id *x509_prof_next_curves = NULL;
156#endif
157
158const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
159{
160 x509_prof_next_mds,
161 x509_prof_default_pks,
162 x509_prof_next_curves,
163 2048,
164};
165
166/*
167 * NSA Suite B Profile
168 */
169static const mbedtls_md_type_t x509_prof_suiteb_mds[] =
170{
171 MBEDTLS_MD_SHA256,
172 MBEDTLS_MD_SHA384,
173 MBEDTLS_MD_NONE
174};
175
176static const mbedtls_pk_type_t x509_prof_suiteb_pks[] =
177{
178 MBEDTLS_PK_ECDSA,
179 MBEDTLS_PK_NONE
180};
181
182#if defined(MBEDTLS_ECP_C)
183static const mbedtls_ecp_group_id x509_prof_suiteb_curves[] =
184{
185 MBEDTLS_ECP_DP_SECP256R1,
186 MBEDTLS_ECP_DP_SECP384R1,
187};
188#else
189static const mbedtls_ecp_group_id *x509_prof_suiteb_curves = NULL;
190#endif
191
192const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
193{
194 x509_prof_suiteb_mds,
195 x509_prof_suiteb_pks,
196 x509_prof_suiteb_curves,
197 2048,
198};
199
200/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
202 */
203static int x509_get_version( unsigned char **p,
204 const unsigned char *end,
205 int *ver )
206{
207 int ret;
208 size_t len;
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
211 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214 {
215 *ver = 0;
216 return( 0 );
217 }
218
219 return( ret );
220 }
221
222 end = *p + len;
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
225 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200226
227 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 return( MBEDTLS_ERR_X509_INVALID_VERSION +
229 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230
231 return( 0 );
232}
233
234/*
235 * Validity ::= SEQUENCE {
236 * notBefore Time,
237 * notAfter Time }
238 */
239static int x509_get_dates( unsigned char **p,
240 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_x509_time *from,
242 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243{
244 int ret;
245 size_t len;
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
248 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
249 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200250
251 end = *p + len;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254 return( ret );
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257 return( ret );
258
259 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 return( MBEDTLS_ERR_X509_INVALID_DATE +
261 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262
263 return( 0 );
264}
265
266/*
267 * X.509 v2/v3 unique identifier (not parsed)
268 */
269static int x509_get_uid( unsigned char **p,
270 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272{
273 int ret;
274
275 if( *p == end )
276 return( 0 );
277
278 uid->tag = **p;
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
281 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 return( 0 );
285
286 return( ret );
287 }
288
289 uid->p = *p;
290 *p += uid->len;
291
292 return( 0 );
293}
294
295static int x509_get_basic_constraints( unsigned char **p,
296 const unsigned char *end,
297 int *ca_istrue,
298 int *max_pathlen )
299{
300 int ret;
301 size_t len;
302
303 /*
304 * BasicConstraints ::= SEQUENCE {
305 * cA BOOLEAN DEFAULT FALSE,
306 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
307 */
308 *ca_istrue = 0; /* DEFAULT FALSE */
309 *max_pathlen = 0; /* endless */
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
312 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
313 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314
315 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200316 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
321 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322
323 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325
326 if( *ca_istrue != 0 )
327 *ca_istrue = 1;
328 }
329
330 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200331 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
334 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335
336 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
338 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340 (*max_pathlen)++;
341
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200342 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343}
344
345static int x509_get_ns_cert_type( unsigned char **p,
346 const unsigned char *end,
347 unsigned char *ns_cert_type)
348{
349 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
353 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354
355 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
357 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358
359 /* Get actual bitstring */
360 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200361 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200362}
363
364static int x509_get_key_usage( unsigned char **p,
365 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100366 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367{
368 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
372 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200373
374 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
376 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
378 /* Get actual bitstring */
379 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200380 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381}
382
383/*
384 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
385 *
386 * KeyPurposeId ::= OBJECT IDENTIFIER
387 */
388static int x509_get_ext_key_usage( unsigned char **p,
389 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391{
392 int ret;
393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
395 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396
397 /* Sequence length must be >= 1 */
398 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
400 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200402 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403}
404
405/*
406 * SubjectAltName ::= GeneralNames
407 *
408 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
409 *
410 * GeneralName ::= CHOICE {
411 * otherName [0] OtherName,
412 * rfc822Name [1] IA5String,
413 * dNSName [2] IA5String,
414 * x400Address [3] ORAddress,
415 * directoryName [4] Name,
416 * ediPartyName [5] EDIPartyName,
417 * uniformResourceIdentifier [6] IA5String,
418 * iPAddress [7] OCTET STRING,
419 * registeredID [8] OBJECT IDENTIFIER }
420 *
421 * OtherName ::= SEQUENCE {
422 * type-id OBJECT IDENTIFIER,
423 * value [0] EXPLICIT ANY DEFINED BY type-id }
424 *
425 * EDIPartyName ::= SEQUENCE {
426 * nameAssigner [0] DirectoryString OPTIONAL,
427 * partyName [1] DirectoryString }
428 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000429 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 */
431static int x509_get_subject_alt_name( unsigned char **p,
432 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434{
435 int ret;
436 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
441 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
443 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
444 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
448 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 while( *p < end )
451 {
452 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
454 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
456 tag = **p;
457 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
459 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
462 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
463 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200465 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467 {
468 *p += tag_len;
469 continue;
470 }
471
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200473 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100475 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100477
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200478 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479
480 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200482 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 cur = cur->next;
485 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200486
487 buf = &(cur->buf);
488 buf->tag = tag;
489 buf->p = *p;
490 buf->len = tag_len;
491 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492 }
493
494 /* Set final sequence entry's next pointer to NULL */
495 cur->next = NULL;
496
497 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
499 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200500
501 return( 0 );
502}
503
504/*
505 * X.509 v3 extensions
506 *
507 * TODO: Perform all of the basic constraints tests required by the RFC
508 * TODO: Set values for undetected extensions to a sane default?
509 *
510 */
511static int x509_get_crt_ext( unsigned char **p,
512 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514{
515 int ret;
516 size_t len;
517 unsigned char *end_ext_data, *end_ext_octet;
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 return( 0 );
523
524 return( ret );
525 }
526
527 while( *p < end )
528 {
529 /*
530 * Extension ::= SEQUENCE {
531 * extnID OBJECT IDENTIFIER,
532 * critical BOOLEAN DEFAULT FALSE,
533 * extnValue OCTET STRING }
534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536 int is_critical = 0; /* DEFAULT FALSE */
537 int ext_type = 0;
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
540 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
541 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542
543 end_ext_data = *p + len;
544
545 /* Get extension ID */
546 extn_oid.tag = **p;
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
549 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550
551 extn_oid.p = *p;
552 *p += extn_oid.len;
553
554 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
556 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
558 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
560 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
561 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
565 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
566 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 end_ext_octet = *p + len;
569
570 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
572 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573
574 /*
575 * Detect supported extensions
576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 if( ret != 0 )
580 {
581 /* No parser found, skip extension */
582 *p = end_ext_octet;
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585 if( is_critical )
586 {
587 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
589 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590 }
591#endif
592 continue;
593 }
594
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100595 /* Forbid repeated extensions */
596 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100598
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599 crt->ext_types |= ext_type;
600
601 switch( ext_type )
602 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100603 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604 /* Parse basic constraints */
605 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
606 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200607 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608 break;
609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 /* Parse key usage */
612 if( ( ret = x509_get_key_usage( p, end_ext_octet,
613 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200614 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 break;
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618 /* Parse extended key usage */
619 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
620 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200621 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 break;
623
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100624 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 /* Parse subject alt name */
626 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
627 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200628 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 break;
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632 /* Parse netscape certificate type */
633 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
634 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200635 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 break;
637
638 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 }
641 }
642
643 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
645 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646
647 return( 0 );
648}
649
650/*
651 * Parse and fill a single X.509 certificate in DER format
652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200654 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655{
656 int ret;
657 size_t len;
658 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
662 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
663 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664
665 /*
666 * Check for valid input
667 */
668 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200671 p = mbedtls_calloc( 1, len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200673 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674
675 memcpy( p, buf, buflen );
676
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 crt->raw.p = p;
678 crt->raw.len = len;
679 end = p + len;
680
681 /*
682 * Certificate ::= SEQUENCE {
683 * tbsCertificate TBSCertificate,
684 * signatureAlgorithm AlgorithmIdentifier,
685 * signatureValue BIT STRING }
686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
688 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_x509_crt_free( crt );
691 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692 }
693
694 if( len > (size_t) ( end - p ) )
695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 mbedtls_x509_crt_free( crt );
697 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
698 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699 }
700 crt_end = p + len;
701
702 /*
703 * TBSCertificate ::= SEQUENCE {
704 */
705 crt->tbs.p = p;
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
708 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200709 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_x509_crt_free( crt );
711 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200712 }
713
714 end = p + len;
715 crt->tbs.len = end - crt->tbs.p;
716
717 /*
718 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
719 *
720 * CertificateSerialNumber ::= INTEGER
721 *
722 * signature AlgorithmIdentifier
723 */
724 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
726 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200727 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730 return( ret );
731 }
732
733 crt->version++;
734
735 if( crt->version > 3 )
736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 mbedtls_x509_crt_free( crt );
738 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 }
740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200742 &crt->sig_md, &crt->sig_pk,
743 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 return( ret );
747 }
748
749 /*
750 * issuer Name
751 */
752 crt->issuer_raw.p = p;
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
755 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_x509_crt_free( crt );
758 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 }
760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 return( ret );
765 }
766
767 crt->issuer_raw.len = p - crt->issuer_raw.p;
768
769 /*
770 * Validity ::= SEQUENCE {
771 * notBefore Time,
772 * notAfter Time }
773 *
774 */
775 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
776 &crt->valid_to ) ) != 0 )
777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 return( ret );
780 }
781
782 /*
783 * subject Name
784 */
785 crt->subject_raw.p = p;
786
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
788 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_x509_crt_free( crt );
791 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 }
793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797 return( ret );
798 }
799
800 crt->subject_raw.len = p - crt->subject_raw.p;
801
802 /*
803 * SubjectPublicKeyInfo
804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808 return( ret );
809 }
810
811 /*
812 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
813 * -- If present, version shall be v2 or v3
814 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
815 * -- If present, version shall be v2 or v3
816 * extensions [3] EXPLICIT Extensions OPTIONAL
817 * -- If present, version shall be v3
818 */
819 if( crt->version == 2 || crt->version == 3 )
820 {
821 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
822 if( ret != 0 )
823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825 return( ret );
826 }
827 }
828
829 if( crt->version == 2 || crt->version == 3 )
830 {
831 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
832 if( ret != 0 )
833 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200835 return( ret );
836 }
837 }
838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200841#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100842 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200843 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200844 if( ret != 0 )
845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847 return( ret );
848 }
849 }
850
851 if( p != end )
852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_x509_crt_free( crt );
854 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
855 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200856 }
857
858 end = crt_end;
859
860 /*
861 * }
862 * -- end of TBSCertificate
863 *
864 * signatureAlgorithm AlgorithmIdentifier,
865 * signatureValue BIT STRING
866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200870 return( ret );
871 }
872
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100873 if( crt->sig_oid.len != sig_oid2.len ||
874 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200875 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200876 ( sig_params1.len != 0 &&
877 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_x509_crt_free( crt );
880 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 }
882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 return( ret );
887 }
888
889 if( p != end )
890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_x509_crt_free( crt );
892 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
893 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200894 }
895
896 return( 0 );
897}
898
899/*
900 * Parse one X.509 certificate in DER format from a buffer and add them to a
901 * chained list
902 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200904 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905{
906 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
909 /*
910 * Check for valid input
911 */
912 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200914
915 while( crt->version != 0 && crt->next != NULL )
916 {
917 prev = crt;
918 crt = crt->next;
919 }
920
921 /*
922 * Add new certificate on the end of the chain if needed.
923 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200924 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200926 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200927
928 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200929 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
931 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200933 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 }
935
Paul Bakkerddf26b42013-09-18 13:46:23 +0200936 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937 {
938 if( prev )
939 prev->next = NULL;
940
941 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943
944 return( ret );
945 }
946
947 return( 0 );
948}
949
950/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200951 * Parse one or more PEM certificates from a buffer and add them to the chained
952 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955{
956 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
959 /*
960 * Check for valid input
961 */
962 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964
965 /*
966 * Determine buffer content. Buffer contains either one DER certificate or
967 * one or more PEM certificates.
968 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200970 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200971 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200974 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975#endif
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 if( buf_format == MBEDTLS_X509_FORMAT_DER )
978 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_PEM_PARSE_C)
981 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 {
983 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200986 /* 1 rather than 0 since the terminating NULL byte is counted in */
987 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988 {
989 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200992 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 "-----BEGIN CERTIFICATE-----",
995 "-----END CERTIFICATE-----",
996 buf, NULL, 0, &use_len );
997
998 if( ret == 0 )
999 {
1000 /*
1001 * Was PEM encoded
1002 */
1003 buflen -= use_len;
1004 buf += use_len;
1005 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 {
1008 return( ret );
1009 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013
1014 /*
1015 * PEM header and footer were found
1016 */
1017 buflen -= use_len;
1018 buf += use_len;
1019
1020 if( first_error == 0 )
1021 first_error = ret;
1022
Paul Bakker5a5fa922014-09-26 14:53:04 +02001023 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024 continue;
1025 }
1026 else
1027 break;
1028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032
1033 if( ret != 0 )
1034 {
1035 /*
1036 * Quit parsing on a memory error
1037 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001038 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 return( ret );
1040
1041 if( first_error == 0 )
1042 first_error = ret;
1043
1044 total_failed++;
1045 continue;
1046 }
1047
1048 success = 1;
1049 }
1050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052
1053 if( success )
1054 return( total_failed );
1055 else if( first_error )
1056 return( first_error );
1057 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059}
1060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001062/*
1063 * Load one or more certificates and add them to the chained list
1064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001066{
1067 int ret;
1068 size_t n;
1069 unsigned char *buf;
1070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072 return( ret );
1073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001075
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001076 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001078
1079 return( ret );
1080}
1081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083{
1084 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001085#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 int w_ret;
1087 WCHAR szDir[MAX_PATH];
1088 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001089 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001090 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091
Paul Bakker9af723c2014-05-01 13:03:14 +02001092 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093 HANDLE hFind;
1094
1095 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097
Paul Bakker9af723c2014-05-01 13:03:14 +02001098 memset( szDir, 0, sizeof(szDir) );
1099 memset( filename, 0, MAX_PATH );
1100 memcpy( filename, path, len );
1101 filename[len++] = '\\';
1102 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 filename[len++] = '*';
1104
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001105 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1106 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001107 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109
1110 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001111 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001113
1114 len = MAX_PATH - len;
1115 do
1116 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001117 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118
1119 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1120 continue;
1121
Paul Bakker9af723c2014-05-01 13:03:14 +02001122 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001123 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001124 p, len - 1,
1125 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001126 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001130 if( w_ret < 0 )
1131 ret++;
1132 else
1133 ret += w_ret;
1134 }
1135 while( FindNextFileW( hFind, &file_data ) != 0 );
1136
Paul Bakker66d5d072014-06-17 16:39:18 +02001137 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001141#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001142 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001143 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001144 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145 char entry_name[255];
1146 DIR *dir = opendir( path );
1147
Paul Bakker66d5d072014-06-17 16:39:18 +02001148 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001152 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001153 return( ret );
1154#endif
1155
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001156 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001159
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001160 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161 {
1162 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001164 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001165 }
1166
1167 if( !S_ISREG( sb.st_mode ) )
1168 continue;
1169
1170 // Ignore parse errors
1171 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 if( t_ret < 0 )
1174 ret++;
1175 else
1176 ret += t_ret;
1177 }
1178 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001179
1180cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001182 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001184#endif
1185
Paul Bakkerbe089b02013-10-14 15:51:50 +02001186#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187
1188 return( ret );
1189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001191
Paul Bakker6edcd412013-10-29 15:22:54 +01001192#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1193 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001194#include <stdarg.h>
1195
1196#if !defined vsnprintf
1197#define vsnprintf _vsnprintf
1198#endif // vsnprintf
1199
1200/*
1201 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1202 * Result value is not size of buffer needed, but -1 if no fit is possible.
1203 *
1204 * This fuction tries to 'fix' this by at least suggesting enlarging the
1205 * size by 20.
1206 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001207static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001208{
1209 va_list ap;
1210 int res = -1;
1211
1212 va_start( ap, format );
1213
1214 res = vsnprintf( str, size, format, ap );
1215
1216 va_end( ap );
1217
1218 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001219 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001220 return( (int) size + 20 );
1221
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001222 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001223}
1224
1225#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001226#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001227
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001228#define ERR_BUF_TOO_SMALL -2
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001229
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001230#define SAFE_SNPRINTF() \
1231{ \
1232 if( ret == -1 ) \
1233 return( -1 ); \
1234 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001235 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001236 p[n - 1] = '\0'; \
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001237 return( ERR_BUF_TOO_SMALL ); \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001238 } \
1239 \
1240 n -= (unsigned int) ret; \
1241 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242}
1243
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001244static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001246{
1247 size_t i;
1248 size_t n = *size;
1249 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001251 const char *sep = "";
1252 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001253
1254 while( cur != NULL )
1255 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001256 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001257 {
1258 *p = '\0';
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001259 return( ERR_BUF_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001260 }
1261
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001262 n -= cur->buf.len + sep_len;
1263 for( i = 0; i < sep_len; i++ )
1264 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001265 for( i = 0; i < cur->buf.len; i++ )
1266 *p++ = cur->buf.p[i];
1267
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001268 sep = ", ";
1269 sep_len = 2;
1270
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001271 cur = cur->next;
1272 }
1273
1274 *p = '\0';
1275
1276 *size = n;
1277 *buf = p;
1278
1279 return( 0 );
1280}
1281
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001282#define PRINT_ITEM(i) \
1283 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001285 SAFE_SNPRINTF(); \
1286 sep = ", "; \
1287 }
1288
1289#define CERT_TYPE(type,name) \
1290 if( ns_cert_type & type ) \
1291 PRINT_ITEM( name );
1292
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001293static int x509_info_cert_type( char **buf, size_t *size,
1294 unsigned char ns_cert_type )
1295{
1296 int ret;
1297 size_t n = *size;
1298 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001299 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001302 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1304 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1305 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001306 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1307 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1308 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001309
1310 *size = n;
1311 *buf = p;
1312
1313 return( 0 );
1314}
1315
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001316#define KEY_USAGE(code,name) \
1317 if( key_usage & code ) \
1318 PRINT_ITEM( name );
1319
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001320static int x509_info_key_usage( char **buf, size_t *size,
1321 unsigned char key_usage )
1322{
1323 int ret;
1324 size_t n = *size;
1325 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001326 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1329 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001330 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1331 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1332 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1334 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001335
1336 *size = n;
1337 *buf = p;
1338
1339 return( 0 );
1340}
1341
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001342static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001344{
1345 int ret;
1346 const char *desc;
1347 size_t n = *size;
1348 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001350 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001351
1352 while( cur != NULL )
1353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001355 desc = "???";
1356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001358 SAFE_SNPRINTF();
1359
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001360 sep = ", ";
1361
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001362 cur = cur->next;
1363 }
1364
1365 *size = n;
1366 *buf = p;
1367
1368 return( 0 );
1369}
1370
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371/*
1372 * Return an informational string about the certificate.
1373 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001374#define BEFORE_COLON 18
1375#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1377 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001378{
1379 int ret;
1380 size_t n;
1381 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 char key_size_str[BEFORE_COLON];
1383
1384 p = buf;
1385 n = size;
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001388 prefix, crt->version );
1389 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391 prefix );
1392 SAFE_SNPRINTF();
1393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395 SAFE_SNPRINTF();
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001398 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001400 SAFE_SNPRINTF();
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001405 SAFE_SNPRINTF();
1406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001408 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1409 crt->valid_from.year, crt->valid_from.mon,
1410 crt->valid_from.day, crt->valid_from.hour,
1411 crt->valid_from.min, crt->valid_from.sec );
1412 SAFE_SNPRINTF();
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001415 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1416 crt->valid_to.year, crt->valid_to.mon,
1417 crt->valid_to.day, crt->valid_to.hour,
1418 crt->valid_to.min, crt->valid_to.sec );
1419 SAFE_SNPRINTF();
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001422 SAFE_SNPRINTF();
1423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001425 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426 SAFE_SNPRINTF();
1427
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001428 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1430 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431 {
1432 return( ret );
1433 }
1434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1436 (int) mbedtls_pk_get_size( &crt->pk ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001437 SAFE_SNPRINTF();
1438
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001439 /*
1440 * Optional extensions
1441 */
1442
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001443 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001446 crt->ca_istrue ? "true" : "false" );
1447 SAFE_SNPRINTF();
1448
1449 if( crt->max_pathlen > 0 )
1450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001452 SAFE_SNPRINTF();
1453 }
1454 }
1455
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001456 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001459 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001460
1461 if( ( ret = x509_info_subject_alt_name( &p, &n,
1462 &crt->subject_alt_names ) ) != 0 )
1463 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001464 }
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001469 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001470
1471 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1472 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001473 }
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001478 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001479
1480 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1481 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001482 }
1483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001487 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001488
1489 if( ( ret = x509_info_ext_key_usage( &p, &n,
1490 &crt->ext_key_usage ) ) != 0 )
1491 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001492 }
1493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001495 SAFE_SNPRINTF();
1496
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001497 return( (int) ( size - n ) );
1498}
1499
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001500struct x509_crt_verify_string {
1501 int code;
1502 const char *string;
1503};
1504
1505static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001506 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001507 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1508 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1509 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1510 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1511 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001512 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1513 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1514 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001515 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001516 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1517 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1518 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1519 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001520 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1521 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1522 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1523 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1524 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1525 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001526 { 0, NULL }
1527};
1528
1529int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001530 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001531{
1532 int ret;
1533 const struct x509_crt_verify_string *cur;
1534 char *p = buf;
1535 size_t n = size;
1536
1537 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1538 {
1539 if( ( flags & cur->code ) == 0 )
1540 continue;
1541
1542 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
1543 SAFE_SNPRINTF();
1544 flags ^= cur->code;
1545 }
1546
1547 if( flags != 0 )
1548 {
1549 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1550 "(this should not happen)\n", prefix );
1551 SAFE_SNPRINTF();
1552 }
1553
1554 return( (int) ( size - n ) );
1555}
1556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1558int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt, unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001559{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) != 0 &&
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001561 ( crt->key_usage & usage ) != usage )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001563
1564 return( 0 );
1565}
1566#endif
1567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1569int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001570 const char *usage_oid,
1571 size_t usage_len )
1572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001574
1575 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001577 return( 0 );
1578
1579 /*
1580 * Look for the requested usage (or wildcard ANY) in our list
1581 */
1582 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1583 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001585
1586 if( cur_oid->len == usage_len &&
1587 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1588 {
1589 return( 0 );
1590 }
1591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001593 return( 0 );
1594 }
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001601/*
1602 * Return 1 if the certificate is revoked, or 0 otherwise.
1603 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001604int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001605{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607
1608 while( cur != NULL && cur->serial.len != 0 )
1609 {
1610 if( crt->serial.len == cur->serial.len &&
1611 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1612 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001613 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001614 return( 1 );
1615 }
1616
1617 cur = cur->next;
1618 }
1619
1620 return( 0 );
1621}
1622
1623/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001624 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001627 mbedtls_x509_crl *crl_list,
1628 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001629{
1630 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1632 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633
1634 if( ca == NULL )
1635 return( flags );
1636
1637 /*
1638 * TODO: What happens if no CRL is present?
1639 * Suggestion: Revocation state should be unknown if no CRL is present.
1640 * For backwards compatibility this is not yet implemented.
1641 */
1642
1643 while( crl_list != NULL )
1644 {
1645 if( crl_list->version == 0 ||
1646 crl_list->issuer_raw.len != ca->subject_raw.len ||
1647 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1648 crl_list->issuer_raw.len ) != 0 )
1649 {
1650 crl_list = crl_list->next;
1651 continue;
1652 }
1653
1654 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001655 * Check if the CA is configured to sign CRLs
1656 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1658 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001661 break;
1662 }
1663#endif
1664
1665 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666 * Check if CRL is correctly signed by the trusted CA
1667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001669 if( md_info == NULL )
1670 {
1671 /*
1672 * Cannot check 'unknown' hash
1673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675 break;
1676 }
1677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001679
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001680 (void) profile; /* WIP:TODO: check profile */
1681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1683 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001684 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001687 break;
1688 }
1689
1690 /*
1691 * Check for validity of CRL (Do not drop out)
1692 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001693 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001696 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001697 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001698
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699 /*
1700 * Check if certificate is revoked
1701 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001702 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001703 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001705 break;
1706 }
1707
1708 crl_list = crl_list->next;
1709 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001710 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001714/*
1715 * Like memcmp, but case-insensitive and always returns -1 if different
1716 */
1717static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718{
1719 size_t i;
1720 unsigned char diff;
1721 const unsigned char *n1 = s1, *n2 = s2;
1722
1723 for( i = 0; i < len; i++ )
1724 {
1725 diff = n1[i] ^ n2[i];
1726
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001727 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728 continue;
1729
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001730 if( diff == 32 &&
1731 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1732 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1733 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001735 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001737 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001738 }
1739
1740 return( 0 );
1741}
1742
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001743/*
1744 * Return 1 if match, 0 if not
1745 * TODO: inverted return value!
1746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747static int x509_wildcard_verify( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001748{
1749 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001750 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751
1752 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1753 return( 0 );
1754
Paul Bakker14b16c62014-05-28 11:33:54 +02001755 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 {
1757 if( cn[i] == '.' )
1758 {
1759 cn_idx = i;
1760 break;
1761 }
1762 }
1763
1764 if( cn_idx == 0 )
1765 return( 0 );
1766
Paul Bakker14b16c62014-05-28 11:33:54 +02001767 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001768 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001769 {
1770 return( 1 );
1771 }
1772
1773 return( 0 );
1774}
1775
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001776/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001777 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1778 * variations (but not all).
1779 *
1780 * Return 0 if equal, -1 otherwise.
1781 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001782static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001783{
1784 if( a->tag == b->tag &&
1785 a->len == b->len &&
1786 memcmp( a->p, b->p, b->len ) == 0 )
1787 {
1788 return( 0 );
1789 }
1790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1792 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001793 a->len == b->len &&
1794 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1795 {
1796 return( 0 );
1797 }
1798
1799 return( -1 );
1800}
1801
1802/*
1803 * Compare two X.509 Names (aka rdnSequence).
1804 *
1805 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1806 * we sometimes return unequal when the full algorithm would return equal,
1807 * but never the other way. (In particular, we don't do Unicode normalisation
1808 * or space folding.)
1809 *
1810 * Return 0 if equal, -1 otherwise.
1811 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001813{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001814 /* Avoid recursion, it might not be optimised by the compiler */
1815 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001816 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001817 if( a == NULL || b == NULL )
1818 return( -1 );
1819
1820 /* type */
1821 if( a->oid.tag != b->oid.tag ||
1822 a->oid.len != b->oid.len ||
1823 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1824 {
1825 return( -1 );
1826 }
1827
1828 /* value */
1829 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1830 return( -1 );
1831
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001832 /* structure of the list of sets */
1833 if( a->next_merged != b->next_merged )
1834 return( -1 );
1835
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001836 a = a->next;
1837 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001838 }
1839
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001840 /* a == NULL == b */
1841 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001842}
1843
1844/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001845 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1846 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001847 *
1848 * top means parent is a locally-trusted certificate
1849 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001851static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1852 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001853 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001854{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001855 int need_ca_bit;
1856
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001857 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001858 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001859 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001860
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001861 /* Parent must have the basicConstraints CA bit set as a general rule */
1862 need_ca_bit = 1;
1863
1864 /* Exception: v1/v2 certificates that are locally trusted. */
1865 if( top && parent->version < 3 )
1866 need_ca_bit = 0;
1867
1868 /* Exception: self-signed end-entity certs that are locally trusted. */
1869 if( top && bottom &&
1870 child->raw.len == parent->raw.len &&
1871 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1872 {
1873 need_ca_bit = 0;
1874 }
1875
1876 if( need_ca_bit && ! parent->ca_istrue )
1877 return( -1 );
1878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001880 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001882 {
1883 return( -1 );
1884 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001885#endif
1886
1887 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001888}
1889
Paul Bakkerddf26b42013-09-18 13:46:23 +02001890static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001891 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001892 mbedtls_x509_crl *ca_crl,
1893 const mbedtls_x509_crt_profile *profile,
1894 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001895 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001896 void *p_vrfy )
1897{
1898 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001899 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001900 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1902 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001903
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001904 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001905 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001906
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001907 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001909
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910 /*
1911 * Child is the top of the chain. Check against the trust_ca list.
1912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001916 if( md_info == NULL )
1917 {
1918 /*
1919 * Cannot check 'unknown', no need to try any CA
1920 */
1921 trust_ca = NULL;
1922 }
1923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001926 (void) profile; /* WIP:TODO: check profile */
1927
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001928 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001930 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001932
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001933 check_path_cnt = path_cnt + 1;
1934
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001935 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001936 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001937 * the same as the trusted CA
1938 */
1939 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1940 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1941 child->issuer_raw.len ) == 0 )
1942 {
1943 check_path_cnt--;
1944 }
1945
1946 if( trust_ca->max_pathlen > 0 &&
1947 trust_ca->max_pathlen < check_path_cnt )
1948 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001949 continue;
1950 }
1951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1953 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001954 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001955 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001956 continue;
1957 }
1958
1959 /*
1960 * Top of chain is signed by a trusted CA
1961 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001962 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963 break;
1964 }
1965
1966 /*
1967 * If top of chain is not the same as the trusted CA send a verify request
1968 * to the callback for any issues with validity and CRL presence for the
1969 * trusted CA certificate.
1970 */
1971 if( trust_ca != NULL &&
1972 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1973 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1974 child->issuer_raw.len ) != 0 ) )
1975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001977 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001978 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001979#else
1980 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001981#endif
1982
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001983 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001984 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001985
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001986 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001987 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001988
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989 if( NULL != f_vrfy )
1990 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001991 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1992 &ca_flags ) ) != 0 )
1993 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001994 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001995 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996 }
1997 }
1998
1999 /* Call callback on top cert */
2000 if( NULL != f_vrfy )
2001 {
Paul Bakker66d5d072014-06-17 16:39:18 +02002002 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002003 return( ret );
2004 }
2005
2006 *flags |= ca_flags;
2007
2008 return( 0 );
2009}
2010
Paul Bakkerddf26b42013-09-18 13:46:23 +02002011static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002012 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2013 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2014 const mbedtls_x509_crt_profile *profile,
2015 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002016 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002017 void *p_vrfy )
2018{
2019 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002020 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2022 mbedtls_x509_crt *grandparent;
2023 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002025 (void) profile; /* WIP */
2026
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002027 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002028 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002030 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2031 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002032 }
2033
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002034 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002035 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002036
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002037 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002038 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002040 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041 if( md_info == NULL )
2042 {
2043 /*
2044 * Cannot check 'unknown' hash
2045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002046 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002047 }
2048 else
2049 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002050 (void) profile; /* WIP:TODO: check profile */
2051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002054 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2055 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002056 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002058 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059 }
2060 }
2061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002062#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002064 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065#endif
2066
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002067 /* Look for a grandparent upwards the chain */
2068 for( grandparent = parent->next;
2069 grandparent != NULL;
2070 grandparent = grandparent->next )
2071 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002072 if( x509_crt_check_parent( parent, grandparent,
2073 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002074 break;
2075 }
2076
2077 /* Is our parent part of the chain or at the top? */
2078 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002080 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002081 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002082 if( ret != 0 )
2083 return( ret );
2084 }
2085 else
2086 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002087 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002088 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089 if( ret != 0 )
2090 return( ret );
2091 }
2092
2093 /* child is verified to be a child of the parent, call verify callback */
2094 if( NULL != f_vrfy )
2095 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2096 return( ret );
2097
2098 *flags |= parent_flags;
2099
2100 return( 0 );
2101}
2102
2103/*
2104 * Verify the certificate validity
2105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002106int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2107 mbedtls_x509_crt *trust_ca,
2108 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002109 const char *cn, uint32_t *flags,
2110 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002111 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002113 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002114 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002115}
2116
2117
2118/*
2119 * Verify the certificate validity, with profile
2120 */
2121int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2122 mbedtls_x509_crt *trust_ca,
2123 mbedtls_x509_crl *ca_crl,
2124 const mbedtls_x509_crt_profile *profile,
2125 const char *cn, uint32_t *flags,
2126 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2127 void *p_vrfy )
2128{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002129 size_t cn_len;
2130 int ret;
2131 int pathlen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002132 mbedtls_x509_crt *parent;
2133 mbedtls_x509_name *name;
2134 mbedtls_x509_sequence *cur = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002135
2136 *flags = 0;
2137
2138 if( cn != NULL )
2139 {
2140 name = &crt->subject;
2141 cn_len = strlen( cn );
2142
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002143 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002144 {
2145 cur = &crt->subject_alt_names;
2146
2147 while( cur != NULL )
2148 {
2149 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002150 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151 break;
2152
2153 if( cur->buf.len > 2 &&
2154 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
2155 x509_wildcard_verify( cn, &cur->buf ) )
2156 break;
2157
2158 cur = cur->next;
2159 }
2160
2161 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002163 }
2164 else
2165 {
2166 while( name != NULL )
2167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002169 {
2170 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002171 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002172 break;
2173
2174 if( name->val.len > 2 &&
2175 memcmp( name->val.p, "*.", 2 ) == 0 &&
2176 x509_wildcard_verify( cn, &name->val ) )
2177 break;
2178 }
2179
2180 name = name->next;
2181 }
2182
2183 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002184 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002185 }
2186 }
2187
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002188 /* Look for a parent upwards the chain */
2189 for( parent = crt->next; parent != NULL; parent = parent->next )
2190 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002191 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002192 break;
2193 }
2194
2195 /* Are we part of the chain or at the top? */
2196 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002197 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002198 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002199 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002200 if( ret != 0 )
2201 return( ret );
2202 }
2203 else
2204 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002205 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002206 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002207 if( ret != 0 )
2208 return( ret );
2209 }
2210
2211 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002213
2214 return( 0 );
2215}
2216
2217/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002218 * Initialize a certificate chain
2219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002223}
2224
2225/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002226 * Unallocate all certificate data
2227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002228void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230 mbedtls_x509_crt *cert_cur = crt;
2231 mbedtls_x509_crt *cert_prv;
2232 mbedtls_x509_name *name_cur;
2233 mbedtls_x509_name *name_prv;
2234 mbedtls_x509_sequence *seq_cur;
2235 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002236
2237 if( crt == NULL )
2238 return;
2239
2240 do
2241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2245 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002246#endif
2247
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002248 name_cur = cert_cur->issuer.next;
2249 while( name_cur != NULL )
2250 {
2251 name_prv = name_cur;
2252 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2254 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002255 }
2256
2257 name_cur = cert_cur->subject.next;
2258 while( name_cur != NULL )
2259 {
2260 name_prv = name_cur;
2261 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2263 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002264 }
2265
2266 seq_cur = cert_cur->ext_key_usage.next;
2267 while( seq_cur != NULL )
2268 {
2269 seq_prv = seq_cur;
2270 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2272 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002273 }
2274
2275 seq_cur = cert_cur->subject_alt_names.next;
2276 while( seq_cur != NULL )
2277 {
2278 seq_prv = seq_cur;
2279 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2281 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002282 }
2283
2284 if( cert_cur->raw.p != NULL )
2285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2287 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002288 }
2289
2290 cert_cur = cert_cur->next;
2291 }
2292 while( cert_cur != NULL );
2293
2294 cert_cur = crt;
2295 do
2296 {
2297 cert_prv = cert_cur;
2298 cert_cur = cert_cur->next;
2299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002300 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002301 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002303 }
2304 while( cert_cur != NULL );
2305}
2306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002307#endif /* MBEDTLS_X509_CRT_PARSE_C */