blob: c837c03d139512481660c40da80b080d1df8f79e [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é-Gonnard9a702252015-06-23 10:14:36 +0200374 size_t i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_x509_bitstring bs = { 0, 0, NULL };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
378 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
380 if( bs.len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
382 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
384 /* Get actual bitstring */
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +0200385 *key_usage = 0;
386 for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
387 {
388 *key_usage |= (unsigned int) bs.p[i] << (8*i);
389 }
390
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200391 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392}
393
394/*
395 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
396 *
397 * KeyPurposeId ::= OBJECT IDENTIFIER
398 */
399static int x509_get_ext_key_usage( unsigned char **p,
400 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_x509_sequence *ext_key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200402{
403 int ret;
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
406 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407
408 /* Sequence length must be >= 1 */
409 if( ext_key_usage->buf.p == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
411 MBEDTLS_ERR_ASN1_INVALID_LENGTH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200413 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414}
415
416/*
417 * SubjectAltName ::= GeneralNames
418 *
419 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
420 *
421 * GeneralName ::= CHOICE {
422 * otherName [0] OtherName,
423 * rfc822Name [1] IA5String,
424 * dNSName [2] IA5String,
425 * x400Address [3] ORAddress,
426 * directoryName [4] Name,
427 * ediPartyName [5] EDIPartyName,
428 * uniformResourceIdentifier [6] IA5String,
429 * iPAddress [7] OCTET STRING,
430 * registeredID [8] OBJECT IDENTIFIER }
431 *
432 * OtherName ::= SEQUENCE {
433 * type-id OBJECT IDENTIFIER,
434 * value [0] EXPLICIT ANY DEFINED BY type-id }
435 *
436 * EDIPartyName ::= SEQUENCE {
437 * nameAssigner [0] DirectoryString OPTIONAL,
438 * partyName [1] DirectoryString }
439 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000440 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 */
442static int x509_get_subject_alt_name( unsigned char **p,
443 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_x509_sequence *subject_alt_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445{
446 int ret;
447 size_t len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_asn1_buf *buf;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449 unsigned char tag;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_asn1_sequence *cur = subject_alt_name;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 /* Get main sequence tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
454 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
455 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
459 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 while( *p < end )
462 {
463 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
465 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466
467 tag = **p;
468 (*p)++;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
470 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
473 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
474 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200476 /* Skip everything but DNS name */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 {
479 *p += tag_len;
480 continue;
481 }
482
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200484 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200485 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100486 if( cur->next != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100488
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200489 cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490
491 if( cur->next == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200493 MBEDTLS_ERR_ASN1_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 cur = cur->next;
496 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200497
498 buf = &(cur->buf);
499 buf->tag = tag;
500 buf->p = *p;
501 buf->len = tag_len;
502 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503 }
504
505 /* Set final sequence entry's next pointer to NULL */
506 cur->next = NULL;
507
508 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
510 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
512 return( 0 );
513}
514
515/*
516 * X.509 v3 extensions
517 *
518 * TODO: Perform all of the basic constraints tests required by the RFC
519 * TODO: Set values for undetected extensions to a sane default?
520 *
521 */
522static int x509_get_crt_ext( unsigned char **p,
523 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525{
526 int ret;
527 size_t len;
528 unsigned char *end_ext_data, *end_ext_octet;
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533 return( 0 );
534
535 return( ret );
536 }
537
538 while( *p < end )
539 {
540 /*
541 * Extension ::= SEQUENCE {
542 * extnID OBJECT IDENTIFIER,
543 * critical BOOLEAN DEFAULT FALSE,
544 * extnValue OCTET STRING }
545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 mbedtls_x509_buf extn_oid = {0, 0, NULL};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547 int is_critical = 0; /* DEFAULT FALSE */
548 int ext_type = 0;
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
551 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
552 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553
554 end_ext_data = *p + len;
555
556 /* Get extension ID */
557 extn_oid.tag = **p;
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
560 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
562 extn_oid.p = *p;
563 *p += extn_oid.len;
564
565 if( ( end - *p ) < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
567 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
569 /* Get optional critical */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
571 ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
572 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573
574 /* Data should be octet string type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
576 MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
577 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
579 end_ext_octet = *p + len;
580
581 if( end_ext_octet != end_ext_data )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
583 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584
585 /*
586 * Detect supported extensions
587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589
590 if( ret != 0 )
591 {
592 /* No parser found, skip extension */
593 *p = end_ext_octet;
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595#if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200596 if( is_critical )
597 {
598 /* Data is marked as critical: fail */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
600 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601 }
602#endif
603 continue;
604 }
605
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100606 /* Forbid repeated extensions */
607 if( ( crt->ext_types & ext_type ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100609
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610 crt->ext_types |= ext_type;
611
612 switch( ext_type )
613 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100614 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 /* Parse basic constraints */
616 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
617 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200618 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200619 break;
620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 case MBEDTLS_X509_EXT_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622 /* Parse key usage */
623 if( ( ret = x509_get_key_usage( p, end_ext_octet,
624 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200625 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 break;
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 /* Parse extended key usage */
630 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
631 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200632 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 break;
634
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100635 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636 /* Parse subject alt name */
637 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
638 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200639 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 break;
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643 /* Parse netscape certificate type */
644 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
645 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200646 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200647 break;
648
649 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 }
652 }
653
654 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
656 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
658 return( 0 );
659}
660
661/*
662 * Parse and fill a single X.509 certificate in DER format
663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200665 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666{
667 int ret;
668 size_t len;
669 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
673 memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
674 memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675
676 /*
677 * Check for valid input
678 */
679 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200682 p = mbedtls_calloc( 1, len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683 if( p == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200684 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200685
686 memcpy( p, buf, buflen );
687
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 crt->raw.p = p;
689 crt->raw.len = len;
690 end = p + len;
691
692 /*
693 * Certificate ::= SEQUENCE {
694 * tbsCertificate TBSCertificate,
695 * signatureAlgorithm AlgorithmIdentifier,
696 * signatureValue BIT STRING }
697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
699 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_x509_crt_free( crt );
702 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200703 }
704
705 if( len > (size_t) ( end - p ) )
706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 mbedtls_x509_crt_free( crt );
708 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
709 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200710 }
711 crt_end = p + len;
712
713 /*
714 * TBSCertificate ::= SEQUENCE {
715 */
716 crt->tbs.p = p;
717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
719 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 mbedtls_x509_crt_free( crt );
722 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200723 }
724
725 end = p + len;
726 crt->tbs.len = end - crt->tbs.p;
727
728 /*
729 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
730 *
731 * CertificateSerialNumber ::= INTEGER
732 *
733 * signature AlgorithmIdentifier
734 */
735 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
737 ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200738 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741 return( ret );
742 }
743
744 crt->version++;
745
746 if( crt->version > 3 )
747 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_x509_crt_free( crt );
749 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750 }
751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200753 &crt->sig_md, &crt->sig_pk,
754 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 return( ret );
758 }
759
760 /*
761 * issuer Name
762 */
763 crt->issuer_raw.p = p;
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
766 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_x509_crt_free( crt );
769 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 }
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200775 return( ret );
776 }
777
778 crt->issuer_raw.len = p - crt->issuer_raw.p;
779
780 /*
781 * Validity ::= SEQUENCE {
782 * notBefore Time,
783 * notAfter Time }
784 *
785 */
786 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
787 &crt->valid_to ) ) != 0 )
788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790 return( ret );
791 }
792
793 /*
794 * subject Name
795 */
796 crt->subject_raw.p = p;
797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
799 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801 mbedtls_x509_crt_free( crt );
802 return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200803 }
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200806 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808 return( ret );
809 }
810
811 crt->subject_raw.len = p - crt->subject_raw.p;
812
813 /*
814 * SubjectPublicKeyInfo
815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200819 return( ret );
820 }
821
822 /*
823 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
824 * -- If present, version shall be v2 or v3
825 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
826 * -- If present, version shall be v2 or v3
827 * extensions [3] EXPLICIT Extensions OPTIONAL
828 * -- If present, version shall be v3
829 */
830 if( crt->version == 2 || crt->version == 3 )
831 {
832 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
833 if( ret != 0 )
834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836 return( ret );
837 }
838 }
839
840 if( crt->version == 2 || crt->version == 3 )
841 {
842 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
843 if( ret != 0 )
844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846 return( ret );
847 }
848 }
849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850#if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851 if( crt->version == 3 )
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200852#endif
Manuel Pégourié-Gonnarda252af72015-03-27 16:15:55 +0100853 {
Paul Bakker66d5d072014-06-17 16:39:18 +0200854 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200855 if( ret != 0 )
856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200858 return( ret );
859 }
860 }
861
862 if( p != end )
863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200864 mbedtls_x509_crt_free( crt );
865 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
866 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200867 }
868
869 end = crt_end;
870
871 /*
872 * }
873 * -- end of TBSCertificate
874 *
875 * signatureAlgorithm AlgorithmIdentifier,
876 * signatureValue BIT STRING
877 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200881 return( ret );
882 }
883
Manuel Pégourié-Gonnard1022fed2015-03-27 16:30:47 +0100884 if( crt->sig_oid.len != sig_oid2.len ||
885 memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200886 sig_params1.len != sig_params2.len ||
Manuel Pégourié-Gonnard159c5242015-04-30 11:15:22 +0200887 ( sig_params1.len != 0 &&
888 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_x509_crt_free( crt );
891 return( MBEDTLS_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200892 }
893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_x509_crt_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200897 return( ret );
898 }
899
900 if( p != end )
901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_x509_crt_free( crt );
903 return( MBEDTLS_ERR_X509_INVALID_FORMAT +
904 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200905 }
906
907 return( 0 );
908}
909
910/*
911 * Parse one X.509 certificate in DER format from a buffer and add them to a
912 * chained list
913 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200915 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200916{
917 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200919
920 /*
921 * Check for valid input
922 */
923 if( crt == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200925
926 while( crt->version != 0 && crt->next != NULL )
927 {
928 prev = crt;
929 crt = crt->next;
930 }
931
932 /*
933 * Add new certificate on the end of the chain if needed.
934 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200935 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200936 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200937 crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200938
939 if( crt->next == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200940 return( MBEDTLS_ERR_X509_ALLOC_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200941
942 prev = crt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200945 }
946
Paul Bakkerddf26b42013-09-18 13:46:23 +0200947 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200948 {
949 if( prev )
950 prev->next = NULL;
951
952 if( crt != chain )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_free( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954
955 return( ret );
956 }
957
958 return( 0 );
959}
960
961/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200962 * Parse one or more PEM certificates from a buffer and add them to the chained
963 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966{
967 int success = 0, first_error = 0, total_failed = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 int buf_format = MBEDTLS_X509_FORMAT_DER;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200969
970 /*
971 * Check for valid input
972 */
973 if( chain == NULL || buf == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975
976 /*
977 * Determine buffer content. Buffer contains either one DER certificate or
978 * one or more PEM certificates.
979 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +0200981 if( buflen != 0 && buf[buflen - 1] == '\0' &&
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200982 strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
983 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 buf_format = MBEDTLS_X509_FORMAT_PEM;
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200985 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986#endif
987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 if( buf_format == MBEDTLS_X509_FORMAT_DER )
989 return mbedtls_x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991#if defined(MBEDTLS_PEM_PARSE_C)
992 if( buf_format == MBEDTLS_X509_FORMAT_PEM )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200993 {
994 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200995 mbedtls_pem_context pem;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200997 /* 1 rather than 0 since the terminating NULL byte is counted in */
998 while( buflen > 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 {
1000 size_t use_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_pem_init( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001002
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001003 /* If we get there, we know the string is null-terminated */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 ret = mbedtls_pem_read_buffer( &pem,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005 "-----BEGIN CERTIFICATE-----",
1006 "-----END CERTIFICATE-----",
1007 buf, NULL, 0, &use_len );
1008
1009 if( ret == 0 )
1010 {
1011 /*
1012 * Was PEM encoded
1013 */
1014 buflen -= use_len;
1015 buf += use_len;
1016 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 {
1019 return( ret );
1020 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001024
1025 /*
1026 * PEM header and footer were found
1027 */
1028 buflen -= use_len;
1029 buf += use_len;
1030
1031 if( first_error == 0 )
1032 first_error = ret;
1033
Paul Bakker5a5fa922014-09-26 14:53:04 +02001034 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 continue;
1036 }
1037 else
1038 break;
1039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_pem_free( &pem );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043
1044 if( ret != 0 )
1045 {
1046 /*
1047 * Quit parsing on a memory error
1048 */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02001049 if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 return( ret );
1051
1052 if( first_error == 0 )
1053 first_error = ret;
1054
1055 total_failed++;
1056 continue;
1057 }
1058
1059 success = 1;
1060 }
1061 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063
1064 if( success )
1065 return( total_failed );
1066 else if( first_error )
1067 return( first_error );
1068 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070}
1071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#if defined(MBEDTLS_FS_IO)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001073/*
1074 * Load one or more certificates and add them to the chained list
1075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077{
1078 int ret;
1079 size_t n;
1080 unsigned char *buf;
1081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001083 return( ret );
1084
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 ret = mbedtls_x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001087 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_free( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001089
1090 return( ret );
1091}
1092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001094{
1095 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +01001096#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 int w_ret;
1098 WCHAR szDir[MAX_PATH];
1099 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +02001100 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001101 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102
Paul Bakker9af723c2014-05-01 13:03:14 +02001103 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104 HANDLE hFind;
1105
1106 if( len > MAX_PATH - 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108
Paul Bakker9af723c2014-05-01 13:03:14 +02001109 memset( szDir, 0, sizeof(szDir) );
1110 memset( filename, 0, MAX_PATH );
1111 memcpy( filename, path, len );
1112 filename[len++] = '\\';
1113 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114 filename[len++] = '*';
1115
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001116 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1117 MAX_PATH - 3 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001118 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120
1121 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +02001122 if( hFind == INVALID_HANDLE_VALUE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124
1125 len = MAX_PATH - len;
1126 do
1127 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001128 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001129
1130 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1131 continue;
1132
Paul Bakker9af723c2014-05-01 13:03:14 +02001133 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001134 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001135 p, len - 1,
1136 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001137 if( w_ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 w_ret = mbedtls_x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001141 if( w_ret < 0 )
1142 ret++;
1143 else
1144 ret += w_ret;
1145 }
1146 while( FindNextFileW( hFind, &file_data ) != 0 );
1147
Paul Bakker66d5d072014-06-17 16:39:18 +02001148 if( GetLastError() != ERROR_NO_MORE_FILES )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001150
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001151 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001152#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001153 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001154 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001155 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001156 char entry_name[255];
1157 DIR *dir = opendir( path );
1158
Paul Bakker66d5d072014-06-17 16:39:18 +02001159 if( dir == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001163 if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001164 {
1165 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001166 return( ret );
Manuel Pégourié-Gonnardf9b85d92015-06-22 18:39:57 +02001167 }
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001168#endif
1169
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001170 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 mbedtls_snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001173
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001174 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001175 {
1176 closedir( dir );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001178 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001179 }
1180
1181 if( !S_ISREG( sb.st_mode ) )
1182 continue;
1183
1184 // Ignore parse errors
1185 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001187 if( t_ret < 0 )
1188 ret++;
1189 else
1190 ret += t_ret;
1191 }
1192 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001193
1194cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#if defined(MBEDTLS_THREADING_PTHREAD)
Manuel Pégourié-Gonnard944cfe82015-05-27 20:07:18 +02001196 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001198#endif
1199
Paul Bakkerbe089b02013-10-14 15:51:50 +02001200#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001201
1202 return( ret );
1203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#endif /* MBEDTLS_FS_IO */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001205
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001206static int x509_info_subject_alt_name( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 const mbedtls_x509_sequence *subject_alt_name )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001208{
1209 size_t i;
1210 size_t n = *size;
1211 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 const mbedtls_x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001213 const char *sep = "";
1214 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001215
1216 while( cur != NULL )
1217 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001218 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001219 {
1220 *p = '\0';
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001221 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001222 }
1223
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001224 n -= cur->buf.len + sep_len;
1225 for( i = 0; i < sep_len; i++ )
1226 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001227 for( i = 0; i < cur->buf.len; i++ )
1228 *p++ = cur->buf.p[i];
1229
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001230 sep = ", ";
1231 sep_len = 2;
1232
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001233 cur = cur->next;
1234 }
1235
1236 *p = '\0';
1237
1238 *size = n;
1239 *buf = p;
1240
1241 return( 0 );
1242}
1243
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001244#define PRINT_ITEM(i) \
1245 { \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001247 MBEDTLS_X509_SAFE_SNPRINTF; \
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001248 sep = ", "; \
1249 }
1250
1251#define CERT_TYPE(type,name) \
1252 if( ns_cert_type & type ) \
1253 PRINT_ITEM( name );
1254
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001255static int x509_info_cert_type( char **buf, size_t *size,
1256 unsigned char ns_cert_type )
1257{
1258 int ret;
1259 size_t n = *size;
1260 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001261 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001264 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
1266 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1267 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001268 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
1269 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1270 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001271
1272 *size = n;
1273 *buf = p;
1274
1275 return( 0 );
1276}
1277
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001278#define KEY_USAGE(code,name) \
1279 if( key_usage & code ) \
1280 PRINT_ITEM( name );
1281
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001282static int x509_info_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001283 unsigned int key_usage )
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001284{
1285 int ret;
1286 size_t n = *size;
1287 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001288 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
1291 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001292 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1293 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1294 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
1296 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard9a702252015-06-23 10:14:36 +02001297 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
1298 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001299
1300 *size = n;
1301 *buf = p;
1302
1303 return( 0 );
1304}
1305
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001306static int x509_info_ext_key_usage( char **buf, size_t *size,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 const mbedtls_x509_sequence *extended_key_usage )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001308{
1309 int ret;
1310 const char *desc;
1311 size_t n = *size;
1312 char *p = *buf;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 const mbedtls_x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001314 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001315
1316 while( cur != NULL )
1317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001319 desc = "???";
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001322 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001323
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001324 sep = ", ";
1325
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001326 cur = cur->next;
1327 }
1328
1329 *size = n;
1330 *buf = p;
1331
1332 return( 0 );
1333}
1334
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335/*
1336 * Return an informational string about the certificate.
1337 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001338#define BEFORE_COLON 18
1339#define BC "18"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
1341 const mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342{
1343 int ret;
1344 size_t n;
1345 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001346 char key_size_str[BEFORE_COLON];
1347
1348 p = buf;
1349 n = size;
1350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351 ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001352 prefix, crt->version );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001353 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 ret = mbedtls_snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001355 prefix );
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_x509_serial_gets( p, n, &crt->serial );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001359 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001362 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001364 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001367 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001369 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1373 crt->valid_from.year, crt->valid_from.mon,
1374 crt->valid_from.day, crt->valid_from.hour,
1375 crt->valid_from.min, crt->valid_from.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001376 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1380 crt->valid_to.year, crt->valid_to.mon,
1381 crt->valid_to.day, crt->valid_to.hour,
1382 crt->valid_to.min, crt->valid_to.sec );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001383 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001386 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001389 crt->sig_md, crt->sig_opts );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001390 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001391
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001392 /* Key size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
1394 mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001395 {
1396 return( ret );
1397 }
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Manuel Pégourié-Gonnard097c7bb2015-06-18 16:43:38 +02001400 (int) mbedtls_pk_get_bitlen( &crt->pk ) );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001401 MBEDTLS_X509_SAFE_SNPRINTF;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001402
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001403 /*
1404 * Optional extensions
1405 */
1406
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001407 if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001410 crt->ca_istrue ? "true" : "false" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001411 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001412
1413 if( crt->max_pathlen > 0 )
1414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001416 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001417 }
1418 }
1419
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001420 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001423 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001424
1425 if( ( ret = x509_info_subject_alt_name( &p, &n,
1426 &crt->subject_alt_names ) ) != 0 )
1427 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001428 }
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001433 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001434
1435 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1436 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001437 }
1438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001442 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001443
1444 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1445 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001446 }
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001451 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001452
1453 if( ( ret = x509_info_ext_key_usage( &p, &n,
1454 &crt->ext_key_usage ) ) != 0 )
1455 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001456 }
1457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ret = mbedtls_snprintf( p, n, "\n" );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001459 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001460
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001461 return( (int) ( size - n ) );
1462}
1463
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001464struct x509_crt_verify_string {
1465 int code;
1466 const char *string;
1467};
1468
1469static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001470 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001471 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
1472 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
1473 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
1474 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
1475 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001476 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
1477 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
1478 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001479 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001480 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
1481 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
1482 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
1483 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001484 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
1485 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1486 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1487 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
1488 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1489 { 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 +01001490 { 0, NULL }
1491};
1492
1493int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001494 uint32_t flags )
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001495{
1496 int ret;
1497 const struct x509_crt_verify_string *cur;
1498 char *p = buf;
1499 size_t n = size;
1500
1501 for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
1502 {
1503 if( ( flags & cur->code ) == 0 )
1504 continue;
1505
1506 ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001507 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001508 flags ^= cur->code;
1509 }
1510
1511 if( flags != 0 )
1512 {
1513 ret = mbedtls_snprintf( p, n, "%sUnknown reason "
1514 "(this should not happen)\n", prefix );
Manuel Pégourié-Gonnard16853682015-06-22 11:12:02 +02001515 MBEDTLS_X509_SAFE_SNPRINTF;
Manuel Pégourié-Gonnardb5f48ad2015-04-20 10:38:13 +01001516 }
1517
1518 return( (int) ( size - n ) );
1519}
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001522int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
1523 unsigned int usage )
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001524{
Manuel Pégourié-Gonnard655a9642015-06-23 10:48:44 +02001525 unsigned int usage_must, usage_may;
1526 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
1527 | MBEDTLS_X509_KU_DECIPHER_ONLY;
1528
1529 if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
1530 return( 0 );
1531
1532 usage_must = usage & ~may_mask;
1533
1534 if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
1535 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
1536
1537 usage_may = usage & may_mask;
1538
1539 if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001541
1542 return( 0 );
1543}
1544#endif
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1547int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001548 const char *usage_oid,
1549 size_t usage_len )
1550{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 const mbedtls_x509_sequence *cur;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001552
1553 /* Extension is not mandatory, absent means no restriction */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001555 return( 0 );
1556
1557 /*
1558 * Look for the requested usage (or wildcard ANY) in our list
1559 */
1560 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 const mbedtls_x509_buf *cur_oid = &cur->buf;
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001563
1564 if( cur_oid->len == usage_len &&
1565 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1566 {
1567 return( 0 );
1568 }
1569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001571 return( 0 );
1572 }
1573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579/*
1580 * Return 1 if the certificate is revoked, or 0 otherwise.
1581 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001582int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001583{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 const mbedtls_x509_crl_entry *cur = &crl->entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001585
1586 while( cur != NULL && cur->serial.len != 0 )
1587 {
1588 if( crt->serial.len == cur->serial.len &&
1589 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1590 {
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001591 if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 return( 1 );
1593 }
1594
1595 cur = cur->next;
1596 }
1597
1598 return( 0 );
1599}
1600
1601/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001602 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001605 mbedtls_x509_crl *crl_list,
1606 const mbedtls_x509_crt_profile *profile )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001607{
1608 int flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1610 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611
1612 if( ca == NULL )
1613 return( flags );
1614
1615 /*
1616 * TODO: What happens if no CRL is present?
1617 * Suggestion: Revocation state should be unknown if no CRL is present.
1618 * For backwards compatibility this is not yet implemented.
1619 */
1620
1621 while( crl_list != NULL )
1622 {
1623 if( crl_list->version == 0 ||
1624 crl_list->issuer_raw.len != ca->subject_raw.len ||
1625 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1626 crl_list->issuer_raw.len ) != 0 )
1627 {
1628 crl_list = crl_list->next;
1629 continue;
1630 }
1631
1632 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001633 * Check if the CA is configured to sign CRLs
1634 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1636 if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001637 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001639 break;
1640 }
1641#endif
1642
1643 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 * Check if CRL is correctly signed by the trusted CA
1645 */
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001646 if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
1647 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
1648
1649 if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
1650 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
1651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 md_info = mbedtls_md_info_from_type( crl_list->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 if( md_info == NULL )
1654 {
1655 /*
1656 * Cannot check 'unknown' hash
1657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001659 break;
1660 }
1661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001663
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001664 if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
1665 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1668 crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001669 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001672 break;
1673 }
1674
1675 /*
1676 * Check for validity of CRL (Do not drop out)
1677 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001678 if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001681 if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001682 flags |= MBEDTLS_X509_BADCRL_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001683
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684 /*
1685 * Check if certificate is revoked
1686 */
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001687 if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 flags |= MBEDTLS_X509_BADCERT_REVOKED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001690 break;
1691 }
1692
1693 crl_list = crl_list->next;
1694 }
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001695
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001696 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698#endif /* MBEDTLS_X509_CRL_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001700/*
1701 * Like memcmp, but case-insensitive and always returns -1 if different
1702 */
1703static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001704{
1705 size_t i;
1706 unsigned char diff;
1707 const unsigned char *n1 = s1, *n2 = s2;
1708
1709 for( i = 0; i < len; i++ )
1710 {
1711 diff = n1[i] ^ n2[i];
1712
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001713 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714 continue;
1715
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001716 if( diff == 32 &&
1717 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1718 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1719 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001720 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001721 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001722
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001723 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001724 }
1725
1726 return( 0 );
1727}
1728
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001729/*
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001730 * Return 0 if name matches wildcard, -1 otherwise
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001731 */
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001732static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001733{
1734 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001735 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736
1737 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1738 return( 0 );
1739
Paul Bakker14b16c62014-05-28 11:33:54 +02001740 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741 {
1742 if( cn[i] == '.' )
1743 {
1744 cn_idx = i;
1745 break;
1746 }
1747 }
1748
1749 if( cn_idx == 0 )
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001750 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001751
Paul Bakker14b16c62014-05-28 11:33:54 +02001752 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001753 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001754 {
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001755 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 }
1757
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02001758 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001759}
1760
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001761/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001762 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1763 * variations (but not all).
1764 *
1765 * Return 0 if equal, -1 otherwise.
1766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001767static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001768{
1769 if( a->tag == b->tag &&
1770 a->len == b->len &&
1771 memcmp( a->p, b->p, b->len ) == 0 )
1772 {
1773 return( 0 );
1774 }
1775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776 if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
1777 ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001778 a->len == b->len &&
1779 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1780 {
1781 return( 0 );
1782 }
1783
1784 return( -1 );
1785}
1786
1787/*
1788 * Compare two X.509 Names (aka rdnSequence).
1789 *
1790 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1791 * we sometimes return unequal when the full algorithm would return equal,
1792 * but never the other way. (In particular, we don't do Unicode normalisation
1793 * or space folding.)
1794 *
1795 * Return 0 if equal, -1 otherwise.
1796 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001798{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001799 /* Avoid recursion, it might not be optimised by the compiler */
1800 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001801 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001802 if( a == NULL || b == NULL )
1803 return( -1 );
1804
1805 /* type */
1806 if( a->oid.tag != b->oid.tag ||
1807 a->oid.len != b->oid.len ||
1808 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1809 {
1810 return( -1 );
1811 }
1812
1813 /* value */
1814 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1815 return( -1 );
1816
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001817 /* structure of the list of sets */
1818 if( a->next_merged != b->next_merged )
1819 return( -1 );
1820
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001821 a = a->next;
1822 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001823 }
1824
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001825 /* a == NULL == b */
1826 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001827}
1828
1829/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001830 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1831 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001832 *
1833 * top means parent is a locally-trusted certificate
1834 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836static int x509_crt_check_parent( const mbedtls_x509_crt *child,
1837 const mbedtls_x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001838 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001839{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001840 int need_ca_bit;
1841
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001842 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001843 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001844 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001845
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001846 /* Parent must have the basicConstraints CA bit set as a general rule */
1847 need_ca_bit = 1;
1848
1849 /* Exception: v1/v2 certificates that are locally trusted. */
1850 if( top && parent->version < 3 )
1851 need_ca_bit = 0;
1852
1853 /* Exception: self-signed end-entity certs that are locally trusted. */
1854 if( top && bottom &&
1855 child->raw.len == parent->raw.len &&
1856 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1857 {
1858 need_ca_bit = 0;
1859 }
1860
1861 if( need_ca_bit && ! parent->ca_istrue )
1862 return( -1 );
1863
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001865 if( need_ca_bit &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001867 {
1868 return( -1 );
1869 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001870#endif
1871
1872 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001873}
1874
Paul Bakkerddf26b42013-09-18 13:46:23 +02001875static int x509_crt_verify_top(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001877 mbedtls_x509_crl *ca_crl,
1878 const mbedtls_x509_crt_profile *profile,
1879 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001880 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001881 void *p_vrfy )
1882{
1883 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02001884 uint32_t ca_flags = 0;
Manuel Pégourié-Gonnard0574bb02015-06-02 09:59:29 +01001885 int check_path_cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
1887 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001888
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001889 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001890 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001891
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001892 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001893 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001894
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02001895 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
1896 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
1897
1898 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
1899 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
1900
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001901 /*
1902 * Child is the top of the chain. Check against the trust_ca list.
1903 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001904 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001906 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001907 if( md_info == NULL )
1908 {
1909 /*
1910 * Cannot check 'unknown', no need to try any CA
1911 */
1912 trust_ca = NULL;
1913 }
1914 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001916
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001917 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001919 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001920 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001922 check_path_cnt = path_cnt + 1;
1923
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001924 /*
Nicholas Wilsonbc07c3a2015-05-13 10:40:30 +01001925 * Reduce check_path_cnt to check against if top of the chain is
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001926 * the same as the trusted CA
1927 */
1928 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1929 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1930 child->issuer_raw.len ) == 0 )
1931 {
1932 check_path_cnt--;
1933 }
1934
1935 if( trust_ca->max_pathlen > 0 &&
1936 trust_ca->max_pathlen < check_path_cnt )
1937 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001938 continue;
1939 }
1940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1942 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001943 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945 continue;
1946 }
1947
1948 /*
1949 * Top of chain is signed by a trusted CA
1950 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Manuel Pégourié-Gonnardfa67eba2015-06-27 14:41:38 +02001952
1953 if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
1954 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
1955
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001956 break;
1957 }
1958
1959 /*
1960 * If top of chain is not the same as the trusted CA send a verify request
1961 * to the callback for any issues with validity and CRL presence for the
1962 * trusted CA certificate.
1963 */
1964 if( trust_ca != NULL &&
1965 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1966 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1967 child->issuer_raw.len ) != 0 ) )
1968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001969#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001970 /* Check trusted CA's CRL for the chain's top crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02001971 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001972#else
1973 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001974#endif
1975
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001976 if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01001977 ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001978
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01001979 if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001980 ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001981
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001982 if( NULL != f_vrfy )
1983 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001984 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1985 &ca_flags ) ) != 0 )
1986 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001988 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989 }
1990 }
1991
1992 /* Call callback on top cert */
1993 if( NULL != f_vrfy )
1994 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001995 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001996 return( ret );
1997 }
1998
1999 *flags |= ca_flags;
2000
2001 return( 0 );
2002}
2003
Paul Bakkerddf26b42013-09-18 13:46:23 +02002004static int x509_crt_verify_child(
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002005 mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
2006 mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
2007 const mbedtls_x509_crt_profile *profile,
2008 int path_cnt, uint32_t *flags,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002009 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002010 void *p_vrfy )
2011{
2012 int ret;
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002013 uint32_t parent_flags = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002014 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2015 mbedtls_x509_crt *grandparent;
2016 const mbedtls_md_info_t *md_info;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002017
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002018 /* path_cnt is 0 for the first intermediate CA */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002019 if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002020 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002021 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2022 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01002023 }
2024
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002025 if( mbedtls_x509_time_is_past( &child->valid_to ) )
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002026 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02002027
Manuel Pégourié-Gonnardc730ed32015-06-02 10:38:50 +01002028 if( mbedtls_x509_time_is_future( &child->valid_from ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002029 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002030
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002031 if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
2032 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2033
2034 if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
2035 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002037 md_info = mbedtls_md_info_from_type( child->sig_md );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002038 if( md_info == NULL )
2039 {
2040 /*
2041 * Cannot check 'unknown' hash
2042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002043 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044 }
2045 else
2046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002047 mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002048
Manuel Pégourié-Gonnardcbb1f6e2015-06-15 16:17:55 +02002049 if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
2050 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002052 if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
2053 child->sig_md, hash, mbedtls_md_get_size( md_info ),
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02002054 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002056 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002057 }
2058 }
2059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002060#if defined(MBEDTLS_X509_CRL_PARSE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002061 /* Check trusted CA's CRL for the given crt */
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002062 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002063#endif
2064
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002065 /* Look for a grandparent upwards the chain */
2066 for( grandparent = parent->next;
2067 grandparent != NULL;
2068 grandparent = grandparent->next )
2069 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002070 if( x509_crt_check_parent( parent, grandparent,
2071 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002072 break;
2073 }
2074
2075 /* Is our parent part of the chain or at the top? */
2076 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002078 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002079 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 if( ret != 0 )
2081 return( ret );
2082 }
2083 else
2084 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002085 ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002086 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002087 if( ret != 0 )
2088 return( ret );
2089 }
2090
2091 /* child is verified to be a child of the parent, call verify callback */
2092 if( NULL != f_vrfy )
2093 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
2094 return( ret );
2095
2096 *flags |= parent_flags;
2097
2098 return( 0 );
2099}
2100
2101/*
2102 * Verify the certificate validity
2103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002104int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
2105 mbedtls_x509_crt *trust_ca,
2106 mbedtls_x509_crl *ca_crl,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02002107 const char *cn, uint32_t *flags,
2108 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02002109 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002110{
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002111 return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
Manuel Pégourié-Gonnard88db5da2015-06-15 14:34:59 +02002112 &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002113}
2114
2115
2116/*
2117 * Verify the certificate validity, with profile
2118 */
2119int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
2120 mbedtls_x509_crt *trust_ca,
2121 mbedtls_x509_crl *ca_crl,
2122 const mbedtls_x509_crt_profile *profile,
2123 const char *cn, uint32_t *flags,
2124 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2125 void *p_vrfy )
2126{
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002127 size_t cn_len;
2128 int ret;
2129 int pathlen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002130 mbedtls_x509_crt *parent;
2131 mbedtls_x509_name *name;
2132 mbedtls_x509_sequence *cur = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002133
Manuel Pégourié-Gonnarda83e4e22015-06-17 11:53:48 +02002134 if( profile == NULL )
2135 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
2136
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002137 *flags = 0;
2138
2139 if( cn != NULL )
2140 {
2141 name = &crt->subject;
2142 cn_len = strlen( cn );
2143
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01002144 if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002145 {
2146 cur = &crt->subject_alt_names;
2147
2148 while( cur != NULL )
2149 {
2150 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002151 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002152 break;
2153
2154 if( cur->buf.len > 2 &&
2155 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002156 x509_check_wildcard( cn, &cur->buf ) == 0 )
2157 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002158 break;
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002159 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002160
2161 cur = cur->next;
2162 }
2163
2164 if( cur == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002166 }
2167 else
2168 {
2169 while( name != NULL )
2170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002171 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002172 {
2173 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02002174 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002175 break;
2176
2177 if( name->val.len > 2 &&
2178 memcmp( name->val.p, "*.", 2 ) == 0 &&
Manuel Pégourié-Gonnardb80d16d2015-06-23 09:24:29 +02002179 x509_check_wildcard( cn, &name->val ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002180 break;
2181 }
2182
2183 name = name->next;
2184 }
2185
2186 if( name == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002187 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002188 }
2189 }
2190
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002191 /* Look for a parent upwards the chain */
2192 for( parent = crt->next; parent != NULL; parent = parent->next )
2193 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02002194 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02002195 break;
2196 }
2197
2198 /* Are we part of the chain or at the top? */
2199 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002200 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002201 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002202 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002203 if( ret != 0 )
2204 return( ret );
2205 }
2206 else
2207 {
Manuel Pégourié-Gonnard95051642015-06-15 10:39:46 +02002208 ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02002209 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002210 if( ret != 0 )
2211 return( ret );
2212 }
2213
2214 if( *flags != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002215 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002216
2217 return( 0 );
2218}
2219
2220/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002221 * Initialize a certificate chain
2222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002224{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225 memset( crt, 0, sizeof(mbedtls_x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002226}
2227
2228/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002229 * Unallocate all certificate data
2230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002231void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002232{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233 mbedtls_x509_crt *cert_cur = crt;
2234 mbedtls_x509_crt *cert_prv;
2235 mbedtls_x509_name *name_cur;
2236 mbedtls_x509_name *name_prv;
2237 mbedtls_x509_sequence *seq_cur;
2238 mbedtls_x509_sequence *seq_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002239
2240 if( crt == NULL )
2241 return;
2242
2243 do
2244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002245 mbedtls_pk_free( &cert_cur->pk );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002247#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2248 mbedtls_free( cert_cur->sig_opts );
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002249#endif
2250
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002251 name_cur = cert_cur->issuer.next;
2252 while( name_cur != NULL )
2253 {
2254 name_prv = name_cur;
2255 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2257 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002258 }
2259
2260 name_cur = cert_cur->subject.next;
2261 while( name_cur != NULL )
2262 {
2263 name_prv = name_cur;
2264 name_cur = name_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002265 mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
2266 mbedtls_free( name_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002267 }
2268
2269 seq_cur = cert_cur->ext_key_usage.next;
2270 while( seq_cur != NULL )
2271 {
2272 seq_prv = seq_cur;
2273 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2275 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002276 }
2277
2278 seq_cur = cert_cur->subject_alt_names.next;
2279 while( seq_cur != NULL )
2280 {
2281 seq_prv = seq_cur;
2282 seq_cur = seq_cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
2284 mbedtls_free( seq_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002285 }
2286
2287 if( cert_cur->raw.p != NULL )
2288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
2290 mbedtls_free( cert_cur->raw.p );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002291 }
2292
2293 cert_cur = cert_cur->next;
2294 }
2295 while( cert_cur != NULL );
2296
2297 cert_cur = crt;
2298 do
2299 {
2300 cert_prv = cert_cur;
2301 cert_cur = cert_cur->next;
2302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002304 if( cert_prv != crt )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 mbedtls_free( cert_prv );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002306 }
2307 while( cert_cur != NULL );
2308}
2309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#endif /* MBEDTLS_X509_CRT_PARSE_C */