blob: 4fddf6524412cedfb49864d6e528b80cb654d8a0 [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/*
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200147 * Check md_alg against profile
148 * Return 0 if md_alg acceptable for this profile, -1 otherwise
149 */
150static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
151 mbedtls_md_type_t md_alg )
152{
153 if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
154 return( 0 );
155
156 return( -1 );
157}
158
159/*
160 * Check pk_alg against profile
161 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
162 */
163static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
164 mbedtls_pk_type_t pk_alg )
165{
166 if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
167 return( 0 );
168
169 return( -1 );
170}
171
172/*
173 * Check key against profile
174 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
175 */
176static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
177 mbedtls_pk_type_t pk_alg,
178 const mbedtls_pk_context *pk )
179{
180#if defined(MBEDTLS_RSA_C)
181 if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
182 {
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +0200183 if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +0200184 return( 0 );
185
186 return( -1 );
187 }
188#endif
189
190#if defined(MBEDTLS_ECDSA_C)
191 if( pk_alg == MBEDTLS_PK_ECDSA )
192 {
193 mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
194
195 if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
196 return( 0 );
197
198 return( -1 );
199 }
200#endif
201
202 return( -1 );
203}
204
205/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
207 */
208static int x509_get_version( unsigned char **p,
209 const unsigned char *end,
210 int *ver )
211{
212 int ret;
213 size_t len;
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
216 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219 {
220 *ver = 0;
221 return( 0 );
222 }
223
224 return( ret );
225 }
226
227 end = *p + len;
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
230 return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200231
232 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 return( MBEDTLS_ERR_X509_INVALID_VERSION +
234 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
236 return( 0 );
237}
238
239/*
240 * Validity ::= SEQUENCE {
241 * notBefore Time,
242 * notAfter Time }
243 */
244static int x509_get_dates( unsigned char **p,
245 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_x509_time *from,
247 mbedtls_x509_time *to )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248{
249 int ret;
250 size_t len;
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
253 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
254 return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200255
256 end = *p + len;
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259 return( ret );
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262 return( ret );
263
264 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 return( MBEDTLS_ERR_X509_INVALID_DATE +
266 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200267
268 return( 0 );
269}
270
271/*
272 * X.509 v2/v3 unique identifier (not parsed)
273 */
274static int x509_get_uid( unsigned char **p,
275 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_x509_buf *uid, int n )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277{
278 int ret;
279
280 if( *p == end )
281 return( 0 );
282
283 uid->tag = **p;
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
286 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289 return( 0 );
290
291 return( ret );
292 }
293
294 uid->p = *p;
295 *p += uid->len;
296
297 return( 0 );
298}
299
300static int x509_get_basic_constraints( unsigned char **p,
301 const unsigned char *end,
302 int *ca_istrue,
303 int *max_pathlen )
304{
305 int ret;
306 size_t len;
307
308 /*
309 * BasicConstraints ::= SEQUENCE {
310 * cA BOOLEAN DEFAULT FALSE,
311 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
312 */
313 *ca_istrue = 0; /* DEFAULT FALSE */
314 *max_pathlen = 0; /* endless */
315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
317 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
318 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319
320 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200321 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
326 ret = mbedtls_asn1_get_int( p, end, ca_istrue );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327
328 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330
331 if( *ca_istrue != 0 )
332 *ca_istrue = 1;
333 }
334
335 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200336 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
339 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200340
341 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
343 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
345 (*max_pathlen)++;
346
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200347 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348}
349
350static int x509_get_ns_cert_type( unsigned char **p,
351 const unsigned char *end,
352 unsigned char *ns_cert_type)
353{
354 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
358 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359
360 if( bs.len != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
362 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363
364 /* Get actual bitstring */
365 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200366 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367}
368
369static int x509_get_key_usage( unsigned char **p,
370 const unsigned char *end,
Manuel Pégourié-Gonnard1d0ca1a2015-03-27 16:50:00 +0100371 unsigned int *key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372{
373 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
377 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378
379 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
381 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382
383 /* Get actual bitstring */
384 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200385 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386}
387
388/*
389 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
390 *
391 * KeyPurposeId ::= OBJECT IDENTIFIER
392 */
393static int x509_get_ext_key_usage( unsigned char **p,
394 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396{
397 int ret;
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
400 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401
402 /* Sequence length must be >= 1 */
403 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
405 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200407 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408}
409
410/*
411 * SubjectAltName ::= GeneralNames
412 *
413 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
414 *
415 * GeneralName ::= CHOICE {
416 * otherName [0] OtherName,
417 * rfc822Name [1] IA5String,
418 * dNSName [2] IA5String,
419 * x400Address [3] ORAddress,
420 * directoryName [4] Name,
421 * ediPartyName [5] EDIPartyName,
422 * uniformResourceIdentifier [6] IA5String,
423 * iPAddress [7] OCTET STRING,
424 * registeredID [8] OBJECT IDENTIFIER }
425 *
426 * OtherName ::= SEQUENCE {
427 * type-id OBJECT IDENTIFIER,
428 * value [0] EXPLICIT ANY DEFINED BY type-id }
429 *
430 * EDIPartyName ::= SEQUENCE {
431 * nameAssigner [0] DirectoryString OPTIONAL,
432 * partyName [1] DirectoryString }
433 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000434 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435 */
436static int x509_get_subject_alt_name( unsigned char **p,
437 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439{
440 int ret;
441 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
446 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
448 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
449 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450
451 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
453 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 while( *p < end )
456 {
457 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
459 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 tag = **p;
462 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
464 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
467 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
468 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200469
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200470 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200472 {
473 *p += tag_len;
474 continue;
475 }
476
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200477 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200478 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100480 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100482
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200483 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484
485 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200487 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489 cur = cur->next;
490 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200491
492 buf = &(cur->buf);
493 buf->tag = tag;
494 buf->p = *p;
495 buf->len = tag_len;
496 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 }
498
499 /* Set final sequence entry's next pointer to NULL */
500 cur->next = NULL;
501
502 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
504 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
506 return( 0 );
507}
508
509/*
510 * X.509 v3 extensions
511 *
512 * TODO: Perform all of the basic constraints tests required by the RFC
513 * TODO: Set values for undetected extensions to a sane default?
514 *
515 */
516static int x509_get_crt_ext( unsigned char **p,
517 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519{
520 int ret;
521 size_t len;
522 unsigned char *end_ext_data, *end_ext_octet;
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527 return( 0 );
528
529 return( ret );
530 }
531
532 while( *p < end )
533 {
534 /*
535 * Extension ::= SEQUENCE {
536 * extnID OBJECT IDENTIFIER,
537 * critical BOOLEAN DEFAULT FALSE,
538 * extnValue OCTET STRING }
539 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541 int is_critical = 0; /* DEFAULT FALSE */
542 int ext_type = 0;
543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
545 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
546 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547
548 end_ext_data = *p + len;
549
550 /* Get extension ID */
551 extn_oid.tag = **p;
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
554 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
556 extn_oid.p = *p;
557 *p += extn_oid.len;
558
559 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
561 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
565 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
566 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
570 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
571 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
573 end_ext_octet = *p + len;
574
575 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
577 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 /*
580 * Detect supported extensions
581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583
584 if( ret != 0 )
585 {
586 /* No parser found, skip extension */
587 *p = end_ext_octet;
588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590 if( is_critical )
591 {
592 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
594 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595 }
596#endif
597 continue;
598 }
599
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100600 /* Forbid repeated extensions */
601 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100603
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604 crt->ext_types |= ext_type;
605
606 switch( ext_type )
607 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100608 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609 /* Parse basic constraints */
610 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
611 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200612 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613 break;
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616 /* Parse key usage */
617 if( ( ret = x509_get_key_usage( p, end_ext_octet,
618 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200619 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 break;
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 /* Parse extended key usage */
624 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
625 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200626 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200627 break;
628
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100629 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 /* Parse subject alt name */
631 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
632 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200633 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634 break;
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637 /* Parse netscape certificate type */
638 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
639 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200640 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 break;
642
643 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200645 }
646 }
647
648 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
650 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651
652 return( 0 );
653}
654
655/*
656 * Parse and fill a single X.509 certificate in DER format
657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200659 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200660{
661 int ret;
662 size_t len;
663 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
667 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
668 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200669
670 /*
671 * Check for valid input
672 */
673 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200676 p = mbedtls_calloc( 1, len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200677 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200678 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679
680 memcpy( p, buf, buflen );
681
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 crt->raw.p = p;
683 crt->raw.len = len;
684 end = p + len;
685
686 /*
687 * Certificate ::= SEQUENCE {
688 * tbsCertificate TBSCertificate,
689 * signatureAlgorithm AlgorithmIdentifier,
690 * signatureValue BIT STRING }
691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
693 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 mbedtls_x509_crt_free( crt );
696 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200697 }
698
699 if( len > (size_t) ( end - p ) )
700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_x509_crt_free( crt );
702 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
703 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704 }
705 crt_end = p + len;
706
707 /*
708 * TBSCertificate ::= SEQUENCE {
709 */
710 crt->tbs.p = p;
711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
713 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 mbedtls_x509_crt_free( crt );
716 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200717 }
718
719 end = p + len;
720 crt->tbs.len = end - crt->tbs.p;
721
722 /*
723 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
724 *
725 * CertificateSerialNumber ::= INTEGER
726 *
727 * signature AlgorithmIdentifier
728 */
729 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
731 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200732 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 return( ret );
736 }
737
738 crt->version++;
739
740 if( crt->version > 3 )
741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_x509_crt_free( crt );
743 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744 }
745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200747 &crt->sig_md, &crt->sig_pk,
748 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 return( ret );
752 }
753
754 /*
755 * issuer Name
756 */
757 crt->issuer_raw.p = p;
758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
760 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_x509_crt_free( crt );
763 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 }
765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 return( ret );
770 }
771
772 crt->issuer_raw.len = p - crt->issuer_raw.p;
773
774 /*
775 * Validity ::= SEQUENCE {
776 * notBefore Time,
777 * notAfter Time }
778 *
779 */
780 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
781 &crt->valid_to ) ) != 0 )
782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784 return( ret );
785 }
786
787 /*
788 * subject Name
789 */
790 crt->subject_raw.p = p;
791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
793 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795 mbedtls_x509_crt_free( crt );
796 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797 }
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802 return( ret );
803 }
804
805 crt->subject_raw.len = p - crt->subject_raw.p;
806
807 /*
808 * SubjectPublicKeyInfo
809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 return( ret );
814 }
815
816 /*
817 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
818 * -- If present, version shall be v2 or v3
819 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
820 * -- If present, version shall be v2 or v3
821 * extensions [3] EXPLICIT Extensions OPTIONAL
822 * -- If present, version shall be v3
823 */
824 if( crt->version == 2 || crt->version == 3 )
825 {
826 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
827 if( ret != 0 )
828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 return( ret );
831 }
832 }
833
834 if( crt->version == 2 || crt->version == 3 )
835 {
836 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
837 if( ret != 0 )
838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200840 return( ret );
841 }
842 }
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200846#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100847 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200848 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849 if( ret != 0 )
850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852 return( ret );
853 }
854 }
855
856 if( p != end )
857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 mbedtls_x509_crt_free( crt );
859 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
860 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200861 }
862
863 end = crt_end;
864
865 /*
866 * }
867 * -- end of TBSCertificate
868 *
869 * signatureAlgorithm AlgorithmIdentifier,
870 * signatureValue BIT STRING
871 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200873 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200875 return( ret );
876 }
877
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100878 if( crt->sig_oid.len != sig_oid2.len ||
879 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200880 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200881 ( sig_params1.len != 0 &&
882 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_x509_crt_free( crt );
885 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200886 }
887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200891 return( ret );
892 }
893
894 if( p != end )
895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_x509_crt_free( crt );
897 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
898 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200899 }
900
901 return( 0 );
902}
903
904/*
905 * Parse one X.509 certificate in DER format from a buffer and add them to a
906 * chained list
907 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200908int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200909 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910{
911 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200912 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913
914 /*
915 * Check for valid input
916 */
917 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
920 while( crt->version != 0 && crt->next != NULL )
921 {
922 prev = crt;
923 crt = crt->next;
924 }
925
926 /*
927 * Add new certificate on the end of the chain if needed.
928 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200929 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200931 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
933 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200934 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939 }
940
Paul Bakkerddf26b42013-09-18 13:46:23 +0200941 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942 {
943 if( prev )
944 prev->next = NULL;
945
946 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948
949 return( ret );
950 }
951
952 return( 0 );
953}
954
955/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200956 * Parse one or more PEM certificates from a buffer and add them to the chained
957 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960{
961 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200963
964 /*
965 * Check for valid input
966 */
967 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969
970 /*
971 * Determine buffer content. Buffer contains either one DER certificate or
972 * one or more PEM certificates.
973 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200975 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200976 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200979 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980#endif
981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 if( buf_format == MBEDTLS_X509_FORMAT_DER )
983 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_PEM_PARSE_C)
986 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 {
988 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200991 /* 1 rather than 0 since the terminating NULL byte is counted in */
992 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 {
994 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200997 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 "-----BEGIN CERTIFICATE-----",
1000 "-----END CERTIFICATE-----",
1001 buf, NULL, 0, &use_len );
1002
1003 if( ret == 0 )
1004 {
1005 /*
1006 * Was PEM encoded
1007 */
1008 buflen -= use_len;
1009 buf += use_len;
1010 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 {
1013 return( ret );
1014 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018
1019 /*
1020 * PEM header and footer were found
1021 */
1022 buflen -= use_len;
1023 buf += use_len;
1024
1025 if( first_error == 0 )
1026 first_error = ret;
1027
Paul Bakker5a5fa922014-09-26 14:53:04 +02001028 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001029 continue;
1030 }
1031 else
1032 break;
1033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
1038 if( ret != 0 )
1039 {
1040 /*
1041 * Quit parsing on a memory error
1042 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001043 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 return( ret );
1045
1046 if( first_error == 0 )
1047 first_error = ret;
1048
1049 total_failed++;
1050 continue;
1051 }
1052
1053 success = 1;
1054 }
1055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001057
1058 if( success )
1059 return( total_failed );
1060 else if( first_error )
1061 return( first_error );
1062 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064}
1065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067/*
1068 * Load one or more certificates and add them to the chained list
1069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071{
1072 int ret;
1073 size_t n;
1074 unsigned char *buf;
1075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077 return( ret );
1078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001080
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001081 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083
1084 return( ret );
1085}
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001088{
1089 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001090#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091 int w_ret;
1092 WCHAR szDir[MAX_PATH];
1093 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001094 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001095 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001096
Paul Bakker9af723c2014-05-01 13:03:14 +02001097 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 HANDLE hFind;
1099
1100 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
Paul Bakker9af723c2014-05-01 13:03:14 +02001103 memset( szDir, 0, sizeof(szDir) );
1104 memset( filename, 0, MAX_PATH );
1105 memcpy( filename, path, len );
1106 filename[len++] = '\\';
1107 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108 filename[len++] = '*';
1109
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001110 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1111 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001112 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114
1115 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001116 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001118
1119 len = MAX_PATH - len;
1120 do
1121 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001122 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001123
1124 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1125 continue;
1126
Paul Bakker9af723c2014-05-01 13:03:14 +02001127 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001128 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001129 p, len - 1,
1130 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001131 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001135 if( w_ret < 0 )
1136 ret++;
1137 else
1138 ret += w_ret;
1139 }
1140 while( FindNextFileW( hFind, &file_data ) != 0 );
1141
Paul Bakker66d5d072014-06-17 16:39:18 +02001142 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001144
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001145 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001146#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001147 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001148 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001149 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150 char entry_name[255];
1151 DIR *dir = opendir( path );
1152
Paul Bakker66d5d072014-06-17 16:39:18 +02001153 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001157 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001158 {
1159 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001160 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001161 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001162#endif
1163
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001164 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001167
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001168 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001169 {
1170 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001172 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173 }
1174
1175 if( !S_ISREG( sb.st_mode ) )
1176 continue;
1177
1178 // Ignore parse errors
1179 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001181 if( t_ret < 0 )
1182 ret++;
1183 else
1184 ret += t_ret;
1185 }
1186 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001187
1188cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001190 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001192#endif
1193
Paul Bakkerbe089b02013-10-14 15:51:50 +02001194#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001195
1196 return( ret );
1197}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001199
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001200static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001202{
1203 size_t i;
1204 size_t n = *size;
1205 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001207 const char *sep = "";
1208 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001209
1210 while( cur != NULL )
1211 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001212 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001213 {
1214 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001215 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001216 }
1217
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001218 n -= cur->buf.len + sep_len;
1219 for( i = 0; i < sep_len; i++ )
1220 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001221 for( i = 0; i < cur->buf.len; i++ )
1222 *p++ = cur->buf.p[i];
1223
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001224 sep = ", ";
1225 sep_len = 2;
1226
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001227 cur = cur->next;
1228 }
1229
1230 *p = '\0';
1231
1232 *size = n;
1233 *buf = p;
1234
1235 return( 0 );
1236}
1237
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001238#define PRINT_ITEM(i) \
1239 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001241 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001242 sep = ", "; \
1243 }
1244
1245#define CERT_TYPE(type,name) \
1246 if( ns_cert_type & type ) \
1247 PRINT_ITEM( name );
1248
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001249static int x509_info_cert_type( char **buf, size_t *size,
1250 unsigned char ns_cert_type )
1251{
1252 int ret;
1253 size_t n = *size;
1254 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001255 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001258 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1260 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1261 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001262 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1263 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1264 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001265
1266 *size = n;
1267 *buf = p;
1268
1269 return( 0 );
1270}
1271
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001272#define KEY_USAGE(code,name) \
1273 if( key_usage & code ) \
1274 PRINT_ITEM( name );
1275
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001276static int x509_info_key_usage( char **buf, size_t *size,
1277 unsigned char key_usage )
1278{
1279 int ret;
1280 size_t n = *size;
1281 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001282 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1285 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001286 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1287 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1288 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1290 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001291
1292 *size = n;
1293 *buf = p;
1294
1295 return( 0 );
1296}
1297
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001298static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001300{
1301 int ret;
1302 const char *desc;
1303 size_t n = *size;
1304 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001306 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001307
1308 while( cur != NULL )
1309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001311 desc = "???";
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001314 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001315
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001316 sep = ", ";
1317
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001318 cur = cur->next;
1319 }
1320
1321 *size = n;
1322 *buf = p;
1323
1324 return( 0 );
1325}
1326
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001327/*
1328 * Return an informational string about the certificate.
1329 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001330#define BEFORE_COLON 18
1331#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1333 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001334{
1335 int ret;
1336 size_t n;
1337 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001338 char key_size_str[BEFORE_COLON];
1339
1340 p = buf;
1341 n = size;
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001344 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001345 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001347 prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001348 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001351 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001354 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001356 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001359 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001361 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001364 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1365 crt->valid_from.year, crt->valid_from.mon,
1366 crt->valid_from.day, crt->valid_from.hour,
1367 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001368 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001371 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1372 crt->valid_to.year, crt->valid_to.mon,
1373 crt->valid_to.day, crt->valid_to.hour,
1374 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001375 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001378 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001381 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001382 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001383
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001384 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1386 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387 {
1388 return( ret );
1389 }
1390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001392 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001393 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001394
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001395 /*
1396 * Optional extensions
1397 */
1398
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001399 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001402 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001403 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001404
1405 if( crt->max_pathlen > 0 )
1406 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001408 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001409 }
1410 }
1411
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001412 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
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%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001415 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001416
1417 if( ( ret = x509_info_subject_alt_name( &p, &n,
1418 &crt->subject_alt_names ) ) != 0 )
1419 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001420 }
1421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001425 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001426
1427 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1428 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001429 }
1430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001434 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001435
1436 if( ( ret = x509_info_key_usage( &p, &n, crt->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 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001441 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001443 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001444
1445 if( ( ret = x509_info_ext_key_usage( &p, &n,
1446 &crt->ext_key_usage ) ) != 0 )
1447 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001448 }
1449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001451 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001452
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 return( (int) ( size - n ) );
1454}
1455
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001456struct x509_crt_verify_string {
1457 int code;
1458 const char *string;
1459};
1460
1461static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001462 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001463 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1464 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1465 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1466 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1467 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001468 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1469 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1470 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001471 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001472 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1473 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1474 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1475 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001476 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1477 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1478 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1479 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1480 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1481 { 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 +01001482 { 0, NULL }
1483};
1484
1485int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001486 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001487{
1488 int ret;
1489 const struct x509_crt_verify_string *cur;
1490 char *p = buf;
1491 size_t n = size;
1492
1493 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1494 {
1495 if( ( flags & cur->code ) == 0 )
1496 continue;
1497
1498 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001499 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001500 flags ^= cur->code;
1501 }
1502
1503 if( flags != 0 )
1504 {
1505 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1506 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001507 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001508 }
1509
1510 return( (int) ( size - n ) );
1511}
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1514int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt, unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001515{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001516 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) != 0 &&
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001517 ( crt->key_usage & usage ) != usage )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001519
1520 return( 0 );
1521}
1522#endif
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1525int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001526 const char *usage_oid,
1527 size_t usage_len )
1528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001530
1531 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001533 return( 0 );
1534
1535 /*
1536 * Look for the requested usage (or wildcard ANY) in our list
1537 */
1538 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001541
1542 if( cur_oid->len == usage_len &&
1543 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1544 {
1545 return( 0 );
1546 }
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001549 return( 0 );
1550 }
1551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557/*
1558 * Return 1 if the certificate is revoked, or 0 otherwise.
1559 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001560int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563
1564 while( cur != NULL && cur->serial.len != 0 )
1565 {
1566 if( crt->serial.len == cur->serial.len &&
1567 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1568 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001569 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570 return( 1 );
1571 }
1572
1573 cur = cur->next;
1574 }
1575
1576 return( 0 );
1577}
1578
1579/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001580 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001583 mbedtls_x509_crl *crl_list,
1584 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585{
1586 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1588 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001589
1590 if( ca == NULL )
1591 return( flags );
1592
1593 /*
1594 * TODO: What happens if no CRL is present?
1595 * Suggestion: Revocation state should be unknown if no CRL is present.
1596 * For backwards compatibility this is not yet implemented.
1597 */
1598
1599 while( crl_list != NULL )
1600 {
1601 if( crl_list->version == 0 ||
1602 crl_list->issuer_raw.len != ca->subject_raw.len ||
1603 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1604 crl_list->issuer_raw.len ) != 0 )
1605 {
1606 crl_list = crl_list->next;
1607 continue;
1608 }
1609
1610 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001611 * Check if the CA is configured to sign CRLs
1612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1614 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001617 break;
1618 }
1619#endif
1620
1621 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001622 * Check if CRL is correctly signed by the trusted CA
1623 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001624 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1625 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1626
1627 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1628 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 if( md_info == NULL )
1632 {
1633 /*
1634 * Cannot check 'unknown' hash
1635 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001637 break;
1638 }
1639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001642 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1643 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001645 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1646 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001647 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650 break;
1651 }
1652
1653 /*
1654 * Check for validity of CRL (Do not drop out)
1655 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001656 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001658
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001659 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001660 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001661
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001662 /*
1663 * Check if certificate is revoked
1664 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001665 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 break;
1669 }
1670
1671 crl_list = crl_list->next;
1672 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001673
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001674 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001678/*
1679 * Like memcmp, but case-insensitive and always returns -1 if different
1680 */
1681static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682{
1683 size_t i;
1684 unsigned char diff;
1685 const unsigned char *n1 = s1, *n2 = s2;
1686
1687 for( i = 0; i < len; i++ )
1688 {
1689 diff = n1[i] ^ n2[i];
1690
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001691 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692 continue;
1693
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001694 if( diff == 32 &&
1695 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1696 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1697 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001698 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001699 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001701 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702 }
1703
1704 return( 0 );
1705}
1706
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001707/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001708 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001709 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001710static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711{
1712 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001713 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714
1715 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1716 return( 0 );
1717
Paul Bakker14b16c62014-05-28 11:33:54 +02001718 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001719 {
1720 if( cn[i] == '.' )
1721 {
1722 cn_idx = i;
1723 break;
1724 }
1725 }
1726
1727 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001728 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001729
Paul Bakker14b16c62014-05-28 11:33:54 +02001730 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001731 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001733 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734 }
1735
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001736 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001737}
1738
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001739/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001740 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1741 * variations (but not all).
1742 *
1743 * Return 0 if equal, -1 otherwise.
1744 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001746{
1747 if( a->tag == b->tag &&
1748 a->len == b->len &&
1749 memcmp( a->p, b->p, b->len ) == 0 )
1750 {
1751 return( 0 );
1752 }
1753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1755 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001756 a->len == b->len &&
1757 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1758 {
1759 return( 0 );
1760 }
1761
1762 return( -1 );
1763}
1764
1765/*
1766 * Compare two X.509 Names (aka rdnSequence).
1767 *
1768 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1769 * we sometimes return unequal when the full algorithm would return equal,
1770 * but never the other way. (In particular, we don't do Unicode normalisation
1771 * or space folding.)
1772 *
1773 * Return 0 if equal, -1 otherwise.
1774 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001775static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001776{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001777 /* Avoid recursion, it might not be optimised by the compiler */
1778 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001779 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001780 if( a == NULL || b == NULL )
1781 return( -1 );
1782
1783 /* type */
1784 if( a->oid.tag != b->oid.tag ||
1785 a->oid.len != b->oid.len ||
1786 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1787 {
1788 return( -1 );
1789 }
1790
1791 /* value */
1792 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1793 return( -1 );
1794
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001795 /* structure of the list of sets */
1796 if( a->next_merged != b->next_merged )
1797 return( -1 );
1798
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001799 a = a->next;
1800 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001801 }
1802
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001803 /* a == NULL == b */
1804 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001805}
1806
1807/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001808 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1809 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001810 *
1811 * top means parent is a locally-trusted certificate
1812 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1815 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001816 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001817{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001818 int need_ca_bit;
1819
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001820 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001821 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001822 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001823
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001824 /* Parent must have the basicConstraints CA bit set as a general rule */
1825 need_ca_bit = 1;
1826
1827 /* Exception: v1/v2 certificates that are locally trusted. */
1828 if( top && parent->version < 3 )
1829 need_ca_bit = 0;
1830
1831 /* Exception: self-signed end-entity certs that are locally trusted. */
1832 if( top && bottom &&
1833 child->raw.len == parent->raw.len &&
1834 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1835 {
1836 need_ca_bit = 0;
1837 }
1838
1839 if( need_ca_bit && ! parent->ca_istrue )
1840 return( -1 );
1841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001843 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001844 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001845 {
1846 return( -1 );
1847 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001848#endif
1849
1850 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001851}
1852
Paul Bakkerddf26b42013-09-18 13:46:23 +02001853static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001855 mbedtls_x509_crl *ca_crl,
1856 const mbedtls_x509_crt_profile *profile,
1857 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001858 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001859 void *p_vrfy )
1860{
1861 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001862 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001863 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1865 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001866
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001867 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001868 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001869
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001870 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001872
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001873 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1874 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1875
1876 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1877 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1878
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001879 /*
1880 * Child is the top of the chain. Check against the trust_ca list.
1881 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001884 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001885 if( md_info == NULL )
1886 {
1887 /*
1888 * Cannot check 'unknown', no need to try any CA
1889 */
1890 trust_ca = NULL;
1891 }
1892 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001894
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001895 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001896 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001897 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001898 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001899
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001900 check_path_cnt = path_cnt + 1;
1901
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001902 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001903 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001904 * the same as the trusted CA
1905 */
1906 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1907 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1908 child->issuer_raw.len ) == 0 )
1909 {
1910 check_path_cnt--;
1911 }
1912
1913 if( trust_ca->max_pathlen > 0 &&
1914 trust_ca->max_pathlen < check_path_cnt )
1915 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001916 continue;
1917 }
1918
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001919 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
1920 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
1921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1923 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001924 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001926 continue;
1927 }
1928
1929 /*
1930 * Top of chain is signed by a trusted CA
1931 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001932 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001933 break;
1934 }
1935
1936 /*
1937 * If top of chain is not the same as the trusted CA send a verify request
1938 * to the callback for any issues with validity and CRL presence for the
1939 * trusted CA certificate.
1940 */
1941 if( trust_ca != NULL &&
1942 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1943 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1944 child->issuer_raw.len ) != 0 ) )
1945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001947 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001948 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001949#else
1950 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951#endif
1952
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001953 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001954 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001955
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001956 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001958
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001959 if( NULL != f_vrfy )
1960 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001961 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1962 &ca_flags ) ) != 0 )
1963 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001964 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001965 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966 }
1967 }
1968
1969 /* Call callback on top cert */
1970 if( NULL != f_vrfy )
1971 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001972 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001973 return( ret );
1974 }
1975
1976 *flags |= ca_flags;
1977
1978 return( 0 );
1979}
1980
Paul Bakkerddf26b42013-09-18 13:46:23 +02001981static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001982 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
1983 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
1984 const mbedtls_x509_crt_profile *profile,
1985 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001986 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987 void *p_vrfy )
1988{
1989 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001990 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001991 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1992 mbedtls_x509_crt *grandparent;
1993 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001994
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001995 (void) profile; /* WIP */
1996
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001997 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001998 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002000 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2001 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002002 }
2003
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002004 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002005 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002006
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002007 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002008 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002009
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002010 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2011 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2012
2013 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2014 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002016 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002017 if( md_info == NULL )
2018 {
2019 /*
2020 * Cannot check 'unknown' hash
2021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002022 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023 }
2024 else
2025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002026 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002027
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002028 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2029 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002031 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2032 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002033 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002035 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002036 }
2037 }
2038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002039#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002040 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002041 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002042#endif
2043
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002044 /* Look for a grandparent upwards the chain */
2045 for( grandparent = parent->next;
2046 grandparent != NULL;
2047 grandparent = grandparent->next )
2048 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002049 if( x509_crt_check_parent( parent, grandparent,
2050 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002051 break;
2052 }
2053
2054 /* Is our parent part of the chain or at the top? */
2055 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002057 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002058 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002059 if( ret != 0 )
2060 return( ret );
2061 }
2062 else
2063 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002064 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002065 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002066 if( ret != 0 )
2067 return( ret );
2068 }
2069
2070 /* child is verified to be a child of the parent, call verify callback */
2071 if( NULL != f_vrfy )
2072 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2073 return( ret );
2074
2075 *flags |= parent_flags;
2076
2077 return( 0 );
2078}
2079
2080/*
2081 * Verify the certificate validity
2082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002083int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2084 mbedtls_x509_crt *trust_ca,
2085 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002086 const char *cn, uint32_t *flags,
2087 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002088 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002089{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002090 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002091 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002092}
2093
2094
2095/*
2096 * Verify the certificate validity, with profile
2097 */
2098int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2099 mbedtls_x509_crt *trust_ca,
2100 mbedtls_x509_crl *ca_crl,
2101 const mbedtls_x509_crt_profile *profile,
2102 const char *cn, uint32_t *flags,
2103 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2104 void *p_vrfy )
2105{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002106 size_t cn_len;
2107 int ret;
2108 int pathlen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002109 mbedtls_x509_crt *parent;
2110 mbedtls_x509_name *name;
2111 mbedtls_x509_sequence *cur = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002112
Manuel Pégourié-Gonnarda83e4e22015-06-17 11:53:48 +02002113 if( profile == NULL )
2114 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2115
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002116 *flags = 0;
2117
2118 if( cn != NULL )
2119 {
2120 name = &crt->subject;
2121 cn_len = strlen( cn );
2122
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002123 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002124 {
2125 cur = &crt->subject_alt_names;
2126
2127 while( cur != NULL )
2128 {
2129 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002130 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002131 break;
2132
2133 if( cur->buf.len > 2 &&
2134 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002135 x509_check_wildcard( cn, &cur->buf ) == 0 )
2136 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002137 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002138 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002139
2140 cur = cur->next;
2141 }
2142
2143 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002145 }
2146 else
2147 {
2148 while( name != NULL )
2149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002150 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002151 {
2152 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002153 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002154 break;
2155
2156 if( name->val.len > 2 &&
2157 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002158 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002159 break;
2160 }
2161
2162 name = name->next;
2163 }
2164
2165 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002167 }
2168 }
2169
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002170 /* Look for a parent upwards the chain */
2171 for( parent = crt->next; parent != NULL; parent = parent->next )
2172 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002173 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002174 break;
2175 }
2176
2177 /* Are we part of the chain or at the top? */
2178 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002179 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002180 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002181 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002182 if( ret != 0 )
2183 return( ret );
2184 }
2185 else
2186 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002187 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002188 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002189 if( ret != 0 )
2190 return( ret );
2191 }
2192
2193 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002194 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002195
2196 return( 0 );
2197}
2198
2199/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002200 * Initialize a certificate chain
2201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002202void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002204 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002205}
2206
2207/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002208 * Unallocate all certificate data
2209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002211{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 mbedtls_x509_crt *cert_cur = crt;
2213 mbedtls_x509_crt *cert_prv;
2214 mbedtls_x509_name *name_cur;
2215 mbedtls_x509_name *name_prv;
2216 mbedtls_x509_sequence *seq_cur;
2217 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002218
2219 if( crt == NULL )
2220 return;
2221
2222 do
2223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002224 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2227 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002228#endif
2229
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002230 name_cur = cert_cur->issuer.next;
2231 while( name_cur != NULL )
2232 {
2233 name_prv = name_cur;
2234 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2236 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002237 }
2238
2239 name_cur = cert_cur->subject.next;
2240 while( name_cur != NULL )
2241 {
2242 name_prv = name_cur;
2243 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002244 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2245 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002246 }
2247
2248 seq_cur = cert_cur->ext_key_usage.next;
2249 while( seq_cur != NULL )
2250 {
2251 seq_prv = seq_cur;
2252 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2254 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002255 }
2256
2257 seq_cur = cert_cur->subject_alt_names.next;
2258 while( seq_cur != NULL )
2259 {
2260 seq_prv = seq_cur;
2261 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002262 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2263 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002264 }
2265
2266 if( cert_cur->raw.p != NULL )
2267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002268 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2269 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002270 }
2271
2272 cert_cur = cert_cur->next;
2273 }
2274 while( cert_cur != NULL );
2275
2276 cert_cur = crt;
2277 do
2278 {
2279 cert_prv = cert_cur;
2280 cert_cur = cert_cur->next;
2281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002282 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002283 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002284 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002285 }
2286 while( cert_cur != NULL );
2287}
2288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289#endif /* MBEDTLS_X509_CRT_PARSE_C */