blob: 8d58b9d44a12c1eaf7e8766c2dc00ffeb9e7e452 [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 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020087const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
88{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +020089 /* Hashes from SHA-1 and above */
90 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
91 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
92 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
93 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
94 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
95 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
96 0xFFFFFFF, /* Any PK alg */
97 0xFFFFFFF, /* Any curve */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +020098 2048,
99};
100
101/*
102 * Next-default profile
103 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200104const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
105{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200106 /* Hashes from SHA-256 and above */
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
109 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
110 0xFFFFFFF, /* Any PK alg */
111#if defined(MBEDTLS_ECP_C)
112 /* Curves at or above 128-bit security level */
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
119 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
120#else
121 0,
122#endif
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200123 2048,
124};
125
126/*
127 * NSA Suite B Profile
128 */
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200129const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
130{
Manuel Pégourié-Gonnardf8ea8562015-06-15 15:33:19 +0200131 /* Only SHA-256 and 384 */
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
133 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
134 /* Only ECDSA */
135 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ),
136#if defined(MBEDTLS_ECP_C)
137 /* Only NIST P-256 and P-384 */
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
139 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
140#else
141 0,
142#endif
143 0,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +0200144};
145
146/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
148 */
149static int x509_get_version( unsigned char **p,
150 const unsigned char *end,
151 int *ver )
152{
153 int ret;
154 size_t len;
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
157 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160 {
161 *ver = 0;
162 return( 0 );
163 }
164
165 return( ret );
166 }
167
168 end = *p + len;
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
171 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172
173 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 return( MBEDTLS_ERR_X509_INVALID_VERSION +
175 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176
177 return( 0 );
178}
179
180/*
181 * Validity ::= SEQUENCE {
182 * notBefore Time,
183 * notAfter Time }
184 */
185static int x509_get_dates( unsigned char **p,
186 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_x509_time *from,
188 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200189{
190 int ret;
191 size_t len;
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
194 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
195 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200196
197 end = *p + len;
198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200200 return( ret );
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203 return( ret );
204
205 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( MBEDTLS_ERR_X509_INVALID_DATE +
207 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
209 return( 0 );
210}
211
212/*
213 * X.509 v2/v3 unique identifier (not parsed)
214 */
215static int x509_get_uid( unsigned char **p,
216 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218{
219 int ret;
220
221 if( *p == end )
222 return( 0 );
223
224 uid->tag = **p;
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
227 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230 return( 0 );
231
232 return( ret );
233 }
234
235 uid->p = *p;
236 *p += uid->len;
237
238 return( 0 );
239}
240
241static int x509_get_basic_constraints( unsigned char **p,
242 const unsigned char *end,
243 int *ca_istrue,
244 int *max_pathlen )
245{
246 int ret;
247 size_t len;
248
249 /*
250 * BasicConstraints ::= SEQUENCE {
251 * cA BOOLEAN DEFAULT FALSE,
252 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
253 */
254 *ca_istrue = 0; /* DEFAULT FALSE */
255 *max_pathlen = 0; /* endless */
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
258 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
259 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260
261 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200262 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
267 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268
269 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200271
272 if( *ca_istrue != 0 )
273 *ca_istrue = 1;
274 }
275
276 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200277 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
280 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281
282 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
284 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285
286 (*max_pathlen)++;
287
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200288 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289}
290
291static int x509_get_ns_cert_type( unsigned char **p,
292 const unsigned char *end,
293 unsigned char *ns_cert_type)
294{
295 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
299 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300
301 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
303 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304
305 /* Get actual bitstring */
306 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200307 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308}
309
310static int x509_get_key_usage( unsigned char **p,
311 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100312 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313{
314 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
318 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319
320 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
322 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323
324 /* Get actual bitstring */
325 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200326 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327}
328
329/*
330 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
331 *
332 * KeyPurposeId ::= OBJECT IDENTIFIER
333 */
334static int x509_get_ext_key_usage( unsigned char **p,
335 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337{
338 int ret;
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
341 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342
343 /* Sequence length must be >= 1 */
344 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
346 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200348 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349}
350
351/*
352 * SubjectAltName ::= GeneralNames
353 *
354 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
355 *
356 * GeneralName ::= CHOICE {
357 * otherName [0] OtherName,
358 * rfc822Name [1] IA5String,
359 * dNSName [2] IA5String,
360 * x400Address [3] ORAddress,
361 * directoryName [4] Name,
362 * ediPartyName [5] EDIPartyName,
363 * uniformResourceIdentifier [6] IA5String,
364 * iPAddress [7] OCTET STRING,
365 * registeredID [8] OBJECT IDENTIFIER }
366 *
367 * OtherName ::= SEQUENCE {
368 * type-id OBJECT IDENTIFIER,
369 * value [0] EXPLICIT ANY DEFINED BY type-id }
370 *
371 * EDIPartyName ::= SEQUENCE {
372 * nameAssigner [0] DirectoryString OPTIONAL,
373 * partyName [1] DirectoryString }
374 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000375 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376 */
377static int x509_get_subject_alt_name( unsigned char **p,
378 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200380{
381 int ret;
382 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200384 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
387 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
389 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
390 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
392 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
394 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395
396 while( *p < end )
397 {
398 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
400 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401
402 tag = **p;
403 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
405 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
408 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
409 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200411 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 {
414 *p += tag_len;
415 continue;
416 }
417
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200419 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200420 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100421 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100423
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200424 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200425
426 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200428 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200430 cur = cur->next;
431 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200432
433 buf = &(cur->buf);
434 buf->tag = tag;
435 buf->p = *p;
436 buf->len = tag_len;
437 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 }
439
440 /* Set final sequence entry's next pointer to NULL */
441 cur->next = NULL;
442
443 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
445 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446
447 return( 0 );
448}
449
450/*
451 * X.509 v3 extensions
452 *
453 * TODO: Perform all of the basic constraints tests required by the RFC
454 * TODO: Set values for undetected extensions to a sane default?
455 *
456 */
457static int x509_get_crt_ext( unsigned char **p,
458 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460{
461 int ret;
462 size_t len;
463 unsigned char *end_ext_data, *end_ext_octet;
464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468 return( 0 );
469
470 return( ret );
471 }
472
473 while( *p < end )
474 {
475 /*
476 * Extension ::= SEQUENCE {
477 * extnID OBJECT IDENTIFIER,
478 * critical BOOLEAN DEFAULT FALSE,
479 * extnValue OCTET STRING }
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482 int is_critical = 0; /* DEFAULT FALSE */
483 int ext_type = 0;
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
486 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
487 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
489 end_ext_data = *p + len;
490
491 /* Get extension ID */
492 extn_oid.tag = **p;
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
495 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
497 extn_oid.p = *p;
498 *p += extn_oid.len;
499
500 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
502 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
504 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
506 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
507 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
509 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
511 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
512 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
514 end_ext_octet = *p + len;
515
516 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
518 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
520 /*
521 * Detect supported extensions
522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
525 if( ret != 0 )
526 {
527 /* No parser found, skip extension */
528 *p = end_ext_octet;
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 if( is_critical )
532 {
533 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
535 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536 }
537#endif
538 continue;
539 }
540
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100541 /* Forbid repeated extensions */
542 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100544
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545 crt->ext_types |= ext_type;
546
547 switch( ext_type )
548 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100549 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550 /* Parse basic constraints */
551 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
552 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200553 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 break;
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557 /* Parse key usage */
558 if( ( ret = x509_get_key_usage( p, end_ext_octet,
559 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200560 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561 break;
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564 /* Parse extended key usage */
565 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
566 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200567 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568 break;
569
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100570 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200571 /* Parse subject alt name */
572 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
573 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200574 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 break;
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578 /* Parse netscape certificate type */
579 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
580 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200581 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582 break;
583
584 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586 }
587 }
588
589 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
591 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592
593 return( 0 );
594}
595
596/*
597 * Parse and fill a single X.509 certificate in DER format
598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200600 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601{
602 int ret;
603 size_t len;
604 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
608 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
609 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610
611 /*
612 * Check for valid input
613 */
614 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200617 p = mbedtls_calloc( 1, len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200619 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620
621 memcpy( p, buf, buflen );
622
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 crt->raw.p = p;
624 crt->raw.len = len;
625 end = p + len;
626
627 /*
628 * Certificate ::= SEQUENCE {
629 * tbsCertificate TBSCertificate,
630 * signatureAlgorithm AlgorithmIdentifier,
631 * signatureValue BIT STRING }
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
634 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_x509_crt_free( crt );
637 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200638 }
639
640 if( len > (size_t) ( end - p ) )
641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_x509_crt_free( crt );
643 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
644 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 }
646 crt_end = p + len;
647
648 /*
649 * TBSCertificate ::= SEQUENCE {
650 */
651 crt->tbs.p = p;
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
654 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_x509_crt_free( crt );
657 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658 }
659
660 end = p + len;
661 crt->tbs.len = end - crt->tbs.p;
662
663 /*
664 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
665 *
666 * CertificateSerialNumber ::= INTEGER
667 *
668 * signature AlgorithmIdentifier
669 */
670 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
672 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200673 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200676 return( ret );
677 }
678
679 crt->version++;
680
681 if( crt->version > 3 )
682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_x509_crt_free( crt );
684 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685 }
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200688 &crt->sig_md, &crt->sig_pk,
689 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200692 return( ret );
693 }
694
695 /*
696 * issuer Name
697 */
698 crt->issuer_raw.p = p;
699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
701 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200703 mbedtls_x509_crt_free( crt );
704 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705 }
706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 return( ret );
711 }
712
713 crt->issuer_raw.len = p - crt->issuer_raw.p;
714
715 /*
716 * Validity ::= SEQUENCE {
717 * notBefore Time,
718 * notAfter Time }
719 *
720 */
721 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
722 &crt->valid_to ) ) != 0 )
723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200725 return( ret );
726 }
727
728 /*
729 * subject Name
730 */
731 crt->subject_raw.p = p;
732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
734 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_x509_crt_free( crt );
737 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 }
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200743 return( ret );
744 }
745
746 crt->subject_raw.len = p - crt->subject_raw.p;
747
748 /*
749 * SubjectPublicKeyInfo
750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200754 return( ret );
755 }
756
757 /*
758 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
759 * -- If present, version shall be v2 or v3
760 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
761 * -- If present, version shall be v2 or v3
762 * extensions [3] EXPLICIT Extensions OPTIONAL
763 * -- If present, version shall be v3
764 */
765 if( crt->version == 2 || crt->version == 3 )
766 {
767 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
768 if( ret != 0 )
769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 return( ret );
772 }
773 }
774
775 if( crt->version == 2 || crt->version == 3 )
776 {
777 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
778 if( ret != 0 )
779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 return( ret );
782 }
783 }
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200787#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100788 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200789 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790 if( ret != 0 )
791 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793 return( ret );
794 }
795 }
796
797 if( p != end )
798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_x509_crt_free( crt );
800 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
801 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802 }
803
804 end = crt_end;
805
806 /*
807 * }
808 * -- end of TBSCertificate
809 *
810 * signatureAlgorithm AlgorithmIdentifier,
811 * signatureValue BIT STRING
812 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 return( ret );
817 }
818
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100819 if( crt->sig_oid.len != sig_oid2.len ||
820 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200821 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200822 ( sig_params1.len != 0 &&
823 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_x509_crt_free( crt );
826 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 }
828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832 return( ret );
833 }
834
835 if( p != end )
836 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200837 mbedtls_x509_crt_free( crt );
838 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
839 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 }
841
842 return( 0 );
843}
844
845/*
846 * Parse one X.509 certificate in DER format from a buffer and add them to a
847 * chained list
848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200850 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851{
852 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854
855 /*
856 * Check for valid input
857 */
858 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860
861 while( crt->version != 0 && crt->next != NULL )
862 {
863 prev = crt;
864 crt = crt->next;
865 }
866
867 /*
868 * Add new certificate on the end of the chain if needed.
869 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200870 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200871 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200872 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873
874 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200875 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200876
877 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200880 }
881
Paul Bakkerddf26b42013-09-18 13:46:23 +0200882 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 {
884 if( prev )
885 prev->next = NULL;
886
887 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889
890 return( ret );
891 }
892
893 return( 0 );
894}
895
896/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200897 * Parse one or more PEM certificates from a buffer and add them to the chained
898 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901{
902 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200904
905 /*
906 * Check for valid input
907 */
908 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910
911 /*
912 * Determine buffer content. Buffer contains either one DER certificate or
913 * one or more PEM certificates.
914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200916 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200917 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200920 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200921#endif
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( buf_format == MBEDTLS_X509_FORMAT_DER )
924 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926#if defined(MBEDTLS_PEM_PARSE_C)
927 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200928 {
929 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200931
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200932 /* 1 rather than 0 since the terminating NULL byte is counted in */
933 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200934 {
935 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200938 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200940 "-----BEGIN CERTIFICATE-----",
941 "-----END CERTIFICATE-----",
942 buf, NULL, 0, &use_len );
943
944 if( ret == 0 )
945 {
946 /*
947 * Was PEM encoded
948 */
949 buflen -= use_len;
950 buf += use_len;
951 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953 {
954 return( ret );
955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200959
960 /*
961 * PEM header and footer were found
962 */
963 buflen -= use_len;
964 buf += use_len;
965
966 if( first_error == 0 )
967 first_error = ret;
968
Paul Bakker5a5fa922014-09-26 14:53:04 +0200969 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 continue;
971 }
972 else
973 break;
974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
979 if( ret != 0 )
980 {
981 /*
982 * Quit parsing on a memory error
983 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200984 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 return( ret );
986
987 if( first_error == 0 )
988 first_error = ret;
989
990 total_failed++;
991 continue;
992 }
993
994 success = 1;
995 }
996 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
999 if( success )
1000 return( total_failed );
1001 else if( first_error )
1002 return( first_error );
1003 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005}
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008/*
1009 * Load one or more certificates and add them to the chained list
1010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012{
1013 int ret;
1014 size_t n;
1015 unsigned char *buf;
1016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 return( ret );
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001022 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024
1025 return( ret );
1026}
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029{
1030 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001031#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 int w_ret;
1033 WCHAR szDir[MAX_PATH];
1034 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001035 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001036 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
Paul Bakker9af723c2014-05-01 13:03:14 +02001038 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 HANDLE hFind;
1040
1041 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043
Paul Bakker9af723c2014-05-01 13:03:14 +02001044 memset( szDir, 0, sizeof(szDir) );
1045 memset( filename, 0, MAX_PATH );
1046 memcpy( filename, path, len );
1047 filename[len++] = '\\';
1048 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001049 filename[len++] = '*';
1050
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001051 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1052 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001053 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055
1056 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001057 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001059
1060 len = MAX_PATH - len;
1061 do
1062 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001063 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064
1065 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1066 continue;
1067
Paul Bakker9af723c2014-05-01 13:03:14 +02001068 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001069 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001070 p, len - 1,
1071 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001072 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076 if( w_ret < 0 )
1077 ret++;
1078 else
1079 ret += w_ret;
1080 }
1081 while( FindNextFileW( hFind, &file_data ) != 0 );
1082
Paul Bakker66d5d072014-06-17 16:39:18 +02001083 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001087#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001088 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001090 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 char entry_name[255];
1092 DIR *dir = opendir( path );
1093
Paul Bakker66d5d072014-06-17 16:39:18 +02001094 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001098 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001099 return( ret );
1100#endif
1101
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001102 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001106 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107 {
1108 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001110 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001111 }
1112
1113 if( !S_ISREG( sb.st_mode ) )
1114 continue;
1115
1116 // Ignore parse errors
1117 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119 if( t_ret < 0 )
1120 ret++;
1121 else
1122 ret += t_ret;
1123 }
1124 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001125
1126cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001128 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001130#endif
1131
Paul Bakkerbe089b02013-10-14 15:51:50 +02001132#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133
1134 return( ret );
1135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001137
Paul Bakker6edcd412013-10-29 15:22:54 +01001138#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1139 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001140#include <stdarg.h>
1141
1142#if !defined vsnprintf
1143#define vsnprintf _vsnprintf
1144#endif // vsnprintf
1145
1146/*
1147 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1148 * Result value is not size of buffer needed, but -1 if no fit is possible.
1149 *
1150 * This fuction tries to 'fix' this by at least suggesting enlarging the
1151 * size by 20.
1152 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001153static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154{
1155 va_list ap;
1156 int res = -1;
1157
1158 va_start( ap, format );
1159
1160 res = vsnprintf( str, size, format, ap );
1161
1162 va_end( ap );
1163
1164 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001165 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001166 return( (int) size + 20 );
1167
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001168 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169}
1170
1171#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001172#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001174#define ERR_BUF_TOO_SMALL -2
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001176#define SAFE_SNPRINTF() \
1177{ \
1178 if( ret == -1 ) \
1179 return( -1 ); \
1180 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001181 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001182 p[n - 1] = '\0'; \
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001183 return( ERR_BUF_TOO_SMALL ); \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001184 } \
1185 \
1186 n -= (unsigned int) ret; \
1187 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001188}
1189
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001190static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001192{
1193 size_t i;
1194 size_t n = *size;
1195 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001197 const char *sep = "";
1198 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001199
1200 while( cur != NULL )
1201 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001202 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001203 {
1204 *p = '\0';
Manuel Pégourié-Gonnard8408a942015-04-09 12:14:31 +02001205 return( ERR_BUF_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001206 }
1207
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001208 n -= cur->buf.len + sep_len;
1209 for( i = 0; i < sep_len; i++ )
1210 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001211 for( i = 0; i < cur->buf.len; i++ )
1212 *p++ = cur->buf.p[i];
1213
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001214 sep = ", ";
1215 sep_len = 2;
1216
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001217 cur = cur->next;
1218 }
1219
1220 *p = '\0';
1221
1222 *size = n;
1223 *buf = p;
1224
1225 return( 0 );
1226}
1227
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001228#define PRINT_ITEM(i) \
1229 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001231 SAFE_SNPRINTF(); \
1232 sep = ", "; \
1233 }
1234
1235#define CERT_TYPE(type,name) \
1236 if( ns_cert_type & type ) \
1237 PRINT_ITEM( name );
1238
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001239static int x509_info_cert_type( char **buf, size_t *size,
1240 unsigned char ns_cert_type )
1241{
1242 int ret;
1243 size_t n = *size;
1244 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001245 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001248 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1250 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1251 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001252 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1253 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1254 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001255
1256 *size = n;
1257 *buf = p;
1258
1259 return( 0 );
1260}
1261
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001262#define KEY_USAGE(code,name) \
1263 if( key_usage & code ) \
1264 PRINT_ITEM( name );
1265
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001266static int x509_info_key_usage( char **buf, size_t *size,
1267 unsigned char key_usage )
1268{
1269 int ret;
1270 size_t n = *size;
1271 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001272 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1275 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001276 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1277 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1278 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1280 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001281
1282 *size = n;
1283 *buf = p;
1284
1285 return( 0 );
1286}
1287
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001288static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001290{
1291 int ret;
1292 const char *desc;
1293 size_t n = *size;
1294 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001296 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001297
1298 while( cur != NULL )
1299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001301 desc = "???";
1302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001304 SAFE_SNPRINTF();
1305
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001306 sep = ", ";
1307
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001308 cur = cur->next;
1309 }
1310
1311 *size = n;
1312 *buf = p;
1313
1314 return( 0 );
1315}
1316
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317/*
1318 * Return an informational string about the certificate.
1319 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001320#define BEFORE_COLON 18
1321#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1323 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001324{
1325 int ret;
1326 size_t n;
1327 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001328 char key_size_str[BEFORE_COLON];
1329
1330 p = buf;
1331 n = size;
1332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334 prefix, crt->version );
1335 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001337 prefix );
1338 SAFE_SNPRINTF();
1339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001341 SAFE_SNPRINTF();
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 SAFE_SNPRINTF();
1347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001351 SAFE_SNPRINTF();
1352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001354 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1355 crt->valid_from.year, crt->valid_from.mon,
1356 crt->valid_from.day, crt->valid_from.hour,
1357 crt->valid_from.min, crt->valid_from.sec );
1358 SAFE_SNPRINTF();
1359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001361 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1362 crt->valid_to.year, crt->valid_to.mon,
1363 crt->valid_to.day, crt->valid_to.hour,
1364 crt->valid_to.min, crt->valid_to.sec );
1365 SAFE_SNPRINTF();
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001368 SAFE_SNPRINTF();
1369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001371 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 SAFE_SNPRINTF();
1373
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001374 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1376 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377 {
1378 return( ret );
1379 }
1380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1382 (int) mbedtls_pk_get_size( &crt->pk ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383 SAFE_SNPRINTF();
1384
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001385 /*
1386 * Optional extensions
1387 */
1388
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001389 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001392 crt->ca_istrue ? "true" : "false" );
1393 SAFE_SNPRINTF();
1394
1395 if( crt->max_pathlen > 0 )
1396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001398 SAFE_SNPRINTF();
1399 }
1400 }
1401
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001402 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001403 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001405 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001406
1407 if( ( ret = x509_info_subject_alt_name( &p, &n,
1408 &crt->subject_alt_names ) ) != 0 )
1409 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001410 }
1411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001415 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001416
1417 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1418 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001419 }
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001422 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001424 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001425
1426 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1427 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001428 }
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001433 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001434
1435 if( ( ret = x509_info_ext_key_usage( &p, &n,
1436 &crt->ext_key_usage ) ) != 0 )
1437 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001438 }
1439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001441 SAFE_SNPRINTF();
1442
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443 return( (int) ( size - n ) );
1444}
1445
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001446struct x509_crt_verify_string {
1447 int code;
1448 const char *string;
1449};
1450
1451static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001452 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001453 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1454 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1455 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1456 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1457 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001458 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1459 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1460 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001461 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001462 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1463 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1464 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1465 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001466 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1467 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1468 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1469 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1470 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1471 { 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 +01001472 { 0, NULL }
1473};
1474
1475int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001476 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001477{
1478 int ret;
1479 const struct x509_crt_verify_string *cur;
1480 char *p = buf;
1481 size_t n = size;
1482
1483 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1484 {
1485 if( ( flags & cur->code ) == 0 )
1486 continue;
1487
1488 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
1489 SAFE_SNPRINTF();
1490 flags ^= cur->code;
1491 }
1492
1493 if( flags != 0 )
1494 {
1495 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1496 "(this should not happen)\n", prefix );
1497 SAFE_SNPRINTF();
1498 }
1499
1500 return( (int) ( size - n ) );
1501}
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1504int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt, unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001505{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) != 0 &&
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001507 ( crt->key_usage & usage ) != usage )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001509
1510 return( 0 );
1511}
1512#endif
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1515int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001516 const char *usage_oid,
1517 size_t usage_len )
1518{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001520
1521 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001523 return( 0 );
1524
1525 /*
1526 * Look for the requested usage (or wildcard ANY) in our list
1527 */
1528 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001531
1532 if( cur_oid->len == usage_len &&
1533 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1534 {
1535 return( 0 );
1536 }
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001539 return( 0 );
1540 }
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547/*
1548 * Return 1 if the certificate is revoked, or 0 otherwise.
1549 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001550int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553
1554 while( cur != NULL && cur->serial.len != 0 )
1555 {
1556 if( crt->serial.len == cur->serial.len &&
1557 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1558 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001559 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560 return( 1 );
1561 }
1562
1563 cur = cur->next;
1564 }
1565
1566 return( 0 );
1567}
1568
1569/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001570 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001573 mbedtls_x509_crl *crl_list,
1574 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001575{
1576 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1578 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579
1580 if( ca == NULL )
1581 return( flags );
1582
1583 /*
1584 * TODO: What happens if no CRL is present?
1585 * Suggestion: Revocation state should be unknown if no CRL is present.
1586 * For backwards compatibility this is not yet implemented.
1587 */
1588
1589 while( crl_list != NULL )
1590 {
1591 if( crl_list->version == 0 ||
1592 crl_list->issuer_raw.len != ca->subject_raw.len ||
1593 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1594 crl_list->issuer_raw.len ) != 0 )
1595 {
1596 crl_list = crl_list->next;
1597 continue;
1598 }
1599
1600 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001601 * Check if the CA is configured to sign CRLs
1602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1604 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001607 break;
1608 }
1609#endif
1610
1611 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001612 * Check if CRL is correctly signed by the trusted CA
1613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001615 if( md_info == NULL )
1616 {
1617 /*
1618 * Cannot check 'unknown' hash
1619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621 break;
1622 }
1623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001625
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001626 (void) profile; /* WIP:TODO: check profile */
1627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1629 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001630 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001633 break;
1634 }
1635
1636 /*
1637 * Check for validity of CRL (Do not drop out)
1638 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001639 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001642 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001643 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001644
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 /*
1646 * Check if certificate is revoked
1647 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001648 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001651 break;
1652 }
1653
1654 crl_list = crl_list->next;
1655 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001656 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001660/*
1661 * Like memcmp, but case-insensitive and always returns -1 if different
1662 */
1663static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001664{
1665 size_t i;
1666 unsigned char diff;
1667 const unsigned char *n1 = s1, *n2 = s2;
1668
1669 for( i = 0; i < len; i++ )
1670 {
1671 diff = n1[i] ^ n2[i];
1672
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001673 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674 continue;
1675
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001676 if( diff == 32 &&
1677 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1678 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1679 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001681 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001683 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684 }
1685
1686 return( 0 );
1687}
1688
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001689/*
1690 * Return 1 if match, 0 if not
1691 * TODO: inverted return value!
1692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001693static int x509_wildcard_verify( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001694{
1695 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001696 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697
1698 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1699 return( 0 );
1700
Paul Bakker14b16c62014-05-28 11:33:54 +02001701 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702 {
1703 if( cn[i] == '.' )
1704 {
1705 cn_idx = i;
1706 break;
1707 }
1708 }
1709
1710 if( cn_idx == 0 )
1711 return( 0 );
1712
Paul Bakker14b16c62014-05-28 11:33:54 +02001713 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001714 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715 {
1716 return( 1 );
1717 }
1718
1719 return( 0 );
1720}
1721
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001722/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001723 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1724 * variations (but not all).
1725 *
1726 * Return 0 if equal, -1 otherwise.
1727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001728static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001729{
1730 if( a->tag == b->tag &&
1731 a->len == b->len &&
1732 memcmp( a->p, b->p, b->len ) == 0 )
1733 {
1734 return( 0 );
1735 }
1736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1738 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001739 a->len == b->len &&
1740 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1741 {
1742 return( 0 );
1743 }
1744
1745 return( -1 );
1746}
1747
1748/*
1749 * Compare two X.509 Names (aka rdnSequence).
1750 *
1751 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1752 * we sometimes return unequal when the full algorithm would return equal,
1753 * but never the other way. (In particular, we don't do Unicode normalisation
1754 * or space folding.)
1755 *
1756 * Return 0 if equal, -1 otherwise.
1757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001759{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001760 /* Avoid recursion, it might not be optimised by the compiler */
1761 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001762 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001763 if( a == NULL || b == NULL )
1764 return( -1 );
1765
1766 /* type */
1767 if( a->oid.tag != b->oid.tag ||
1768 a->oid.len != b->oid.len ||
1769 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1770 {
1771 return( -1 );
1772 }
1773
1774 /* value */
1775 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1776 return( -1 );
1777
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001778 /* structure of the list of sets */
1779 if( a->next_merged != b->next_merged )
1780 return( -1 );
1781
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001782 a = a->next;
1783 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001784 }
1785
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001786 /* a == NULL == b */
1787 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001788}
1789
1790/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001791 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1792 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001793 *
1794 * top means parent is a locally-trusted certificate
1795 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1798 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001799 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001800{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001801 int need_ca_bit;
1802
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001803 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001804 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001805 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001806
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001807 /* Parent must have the basicConstraints CA bit set as a general rule */
1808 need_ca_bit = 1;
1809
1810 /* Exception: v1/v2 certificates that are locally trusted. */
1811 if( top && parent->version < 3 )
1812 need_ca_bit = 0;
1813
1814 /* Exception: self-signed end-entity certs that are locally trusted. */
1815 if( top && bottom &&
1816 child->raw.len == parent->raw.len &&
1817 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1818 {
1819 need_ca_bit = 0;
1820 }
1821
1822 if( need_ca_bit && ! parent->ca_istrue )
1823 return( -1 );
1824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001826 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001827 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001828 {
1829 return( -1 );
1830 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001831#endif
1832
1833 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001834}
1835
Paul Bakkerddf26b42013-09-18 13:46:23 +02001836static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001838 mbedtls_x509_crl *ca_crl,
1839 const mbedtls_x509_crt_profile *profile,
1840 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001841 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001842 void *p_vrfy )
1843{
1844 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001845 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001846 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1848 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001850 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001851 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001852
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001853 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001855
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001856 /*
1857 * Child is the top of the chain. Check against the trust_ca list.
1858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001862 if( md_info == NULL )
1863 {
1864 /*
1865 * Cannot check 'unknown', no need to try any CA
1866 */
1867 trust_ca = NULL;
1868 }
1869 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001870 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001871
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001872 (void) profile; /* WIP:TODO: check profile */
1873
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001874 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001875 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001876 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001877 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001878
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001879 check_path_cnt = path_cnt + 1;
1880
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001881 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001882 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001883 * the same as the trusted CA
1884 */
1885 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1886 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1887 child->issuer_raw.len ) == 0 )
1888 {
1889 check_path_cnt--;
1890 }
1891
1892 if( trust_ca->max_pathlen > 0 &&
1893 trust_ca->max_pathlen < check_path_cnt )
1894 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001895 continue;
1896 }
1897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1899 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001900 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001901 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001902 continue;
1903 }
1904
1905 /*
1906 * Top of chain is signed by a trusted CA
1907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001908 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001909 break;
1910 }
1911
1912 /*
1913 * If top of chain is not the same as the trusted CA send a verify request
1914 * to the callback for any issues with validity and CRL presence for the
1915 * trusted CA certificate.
1916 */
1917 if( trust_ca != NULL &&
1918 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1919 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1920 child->issuer_raw.len ) != 0 ) )
1921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001923 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001924 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001925#else
1926 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001927#endif
1928
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001929 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001930 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001931
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001932 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001933 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001934
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001935 if( NULL != f_vrfy )
1936 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001937 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1938 &ca_flags ) ) != 0 )
1939 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001940 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001941 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001942 }
1943 }
1944
1945 /* Call callback on top cert */
1946 if( NULL != f_vrfy )
1947 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001948 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001949 return( ret );
1950 }
1951
1952 *flags |= ca_flags;
1953
1954 return( 0 );
1955}
1956
Paul Bakkerddf26b42013-09-18 13:46:23 +02001957static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001958 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
1959 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
1960 const mbedtls_x509_crt_profile *profile,
1961 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001962 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001963 void *p_vrfy )
1964{
1965 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001966 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1968 mbedtls_x509_crt *grandparent;
1969 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001970
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001971 (void) profile; /* WIP */
1972
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001973 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001974 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001976 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
1977 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001978 }
1979
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001980 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001981 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001982
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001983 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001984 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001986 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987 if( md_info == NULL )
1988 {
1989 /*
1990 * Cannot check 'unknown' hash
1991 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001992 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993 }
1994 else
1995 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001996 (void) profile; /* WIP:TODO: check profile */
1997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2001 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002002 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002004 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005 }
2006 }
2007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002010 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002011#endif
2012
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002013 /* Look for a grandparent upwards the chain */
2014 for( grandparent = parent->next;
2015 grandparent != NULL;
2016 grandparent = grandparent->next )
2017 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002018 if( x509_crt_check_parent( parent, grandparent,
2019 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002020 break;
2021 }
2022
2023 /* Is our parent part of the chain or at the top? */
2024 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002025 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002026 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002027 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002028 if( ret != 0 )
2029 return( ret );
2030 }
2031 else
2032 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002033 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002034 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002035 if( ret != 0 )
2036 return( ret );
2037 }
2038
2039 /* child is verified to be a child of the parent, call verify callback */
2040 if( NULL != f_vrfy )
2041 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2042 return( ret );
2043
2044 *flags |= parent_flags;
2045
2046 return( 0 );
2047}
2048
2049/*
2050 * Verify the certificate validity
2051 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2053 mbedtls_x509_crt *trust_ca,
2054 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002055 const char *cn, uint32_t *flags,
2056 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002057 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002058{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002059 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002060 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002061}
2062
2063
2064/*
2065 * Verify the certificate validity, with profile
2066 */
2067int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2068 mbedtls_x509_crt *trust_ca,
2069 mbedtls_x509_crl *ca_crl,
2070 const mbedtls_x509_crt_profile *profile,
2071 const char *cn, uint32_t *flags,
2072 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2073 void *p_vrfy )
2074{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002075 size_t cn_len;
2076 int ret;
2077 int pathlen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002078 mbedtls_x509_crt *parent;
2079 mbedtls_x509_name *name;
2080 mbedtls_x509_sequence *cur = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002081
2082 *flags = 0;
2083
2084 if( cn != NULL )
2085 {
2086 name = &crt->subject;
2087 cn_len = strlen( cn );
2088
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002089 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002090 {
2091 cur = &crt->subject_alt_names;
2092
2093 while( cur != NULL )
2094 {
2095 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002096 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002097 break;
2098
2099 if( cur->buf.len > 2 &&
2100 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
2101 x509_wildcard_verify( cn, &cur->buf ) )
2102 break;
2103
2104 cur = cur->next;
2105 }
2106
2107 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002108 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002109 }
2110 else
2111 {
2112 while( name != NULL )
2113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002115 {
2116 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002117 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002118 break;
2119
2120 if( name->val.len > 2 &&
2121 memcmp( name->val.p, "*.", 2 ) == 0 &&
2122 x509_wildcard_verify( cn, &name->val ) )
2123 break;
2124 }
2125
2126 name = name->next;
2127 }
2128
2129 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131 }
2132 }
2133
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002134 /* Look for a parent upwards the chain */
2135 for( parent = crt->next; parent != NULL; parent = parent->next )
2136 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002137 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002138 break;
2139 }
2140
2141 /* Are we part of the chain or at the top? */
2142 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002143 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002144 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002145 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002146 if( ret != 0 )
2147 return( ret );
2148 }
2149 else
2150 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002151 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002152 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002153 if( ret != 0 )
2154 return( ret );
2155 }
2156
2157 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002158 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002159
2160 return( 0 );
2161}
2162
2163/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002164 * Initialize a certificate chain
2165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002167{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002168 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002169}
2170
2171/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002172 * Unallocate all certificate data
2173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002174void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002176 mbedtls_x509_crt *cert_cur = crt;
2177 mbedtls_x509_crt *cert_prv;
2178 mbedtls_x509_name *name_cur;
2179 mbedtls_x509_name *name_prv;
2180 mbedtls_x509_sequence *seq_cur;
2181 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002182
2183 if( crt == NULL )
2184 return;
2185
2186 do
2187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002188 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002190#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2191 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002192#endif
2193
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002194 name_cur = cert_cur->issuer.next;
2195 while( name_cur != NULL )
2196 {
2197 name_prv = name_cur;
2198 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2200 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002201 }
2202
2203 name_cur = cert_cur->subject.next;
2204 while( name_cur != NULL )
2205 {
2206 name_prv = name_cur;
2207 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002208 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2209 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002210 }
2211
2212 seq_cur = cert_cur->ext_key_usage.next;
2213 while( seq_cur != NULL )
2214 {
2215 seq_prv = seq_cur;
2216 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002217 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2218 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002219 }
2220
2221 seq_cur = cert_cur->subject_alt_names.next;
2222 while( seq_cur != NULL )
2223 {
2224 seq_prv = seq_cur;
2225 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2227 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002228 }
2229
2230 if( cert_cur->raw.p != NULL )
2231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002232 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2233 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002234 }
2235
2236 cert_cur = cert_cur->next;
2237 }
2238 while( cert_cur != NULL );
2239
2240 cert_cur = crt;
2241 do
2242 {
2243 cert_prv = cert_cur;
2244 cert_cur = cert_cur->next;
2245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002246 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002247 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002248 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002249 }
2250 while( cert_cur != NULL );
2251}
2252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253#endif /* MBEDTLS_X509_CRT_PARSE_C */