blob: 2e2d97098d724390daf8b83b4f579ce8a81a3d1c [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 */
23/*
24 * The ITU-T X.509 standard defines a certificate format for PKI.
25 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020026 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
27 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
28 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020029 *
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
31 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
32 */
33
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020035#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#else
37#include POLARSSL_CONFIG_FILE
38#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039
40#if defined(POLARSSL_X509_CRT_PARSE_C)
41
42#include "polarssl/x509_crt.h"
43#include "polarssl/oid.h"
44#if defined(POLARSSL_PEM_PARSE_C)
45#include "polarssl/pem.h"
46#endif
47
Paul Bakker7dc4c442014-02-01 22:50:26 +010048#if defined(POLARSSL_PLATFORM_C)
49#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020050#else
51#define polarssl_malloc malloc
52#define polarssl_free free
53#endif
54
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010055#if defined(POLARSSL_THREADING_C)
56#include "polarssl/threading.h"
57#endif
58
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059#include <string.h>
60#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010061#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <windows.h>
63#else
64#include <time.h>
65#endif
66
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067#if defined(EFIX64) || defined(EFI32)
68#include <stdio.h>
69#endif
70
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071#if defined(POLARSSL_FS_IO)
72#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020073#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#include <sys/types.h>
75#include <sys/stat.h>
76#include <dirent.h>
77#endif
78#endif
79
Paul Bakker34617722014-06-13 17:20:13 +020080/* Implementation that should never be optimized out by the compiler */
81static void polarssl_zeroize( void *v, size_t n ) {
82 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
83}
84
Paul Bakker7c6b2c32013-09-16 13:49:26 +020085/*
86 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
87 */
88static int x509_get_version( unsigned char **p,
89 const unsigned char *end,
90 int *ver )
91{
92 int ret;
93 size_t len;
94
95 if( ( ret = asn1_get_tag( p, end, &len,
96 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
97 {
98 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
99 {
100 *ver = 0;
101 return( 0 );
102 }
103
104 return( ret );
105 }
106
107 end = *p + len;
108
109 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200110 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111
112 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200113 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200114 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
115
116 return( 0 );
117}
118
119/*
120 * Validity ::= SEQUENCE {
121 * notBefore Time,
122 * notAfter Time }
123 */
124static int x509_get_dates( unsigned char **p,
125 const unsigned char *end,
126 x509_time *from,
127 x509_time *to )
128{
129 int ret;
130 size_t len;
131
132 if( ( ret = asn1_get_tag( p, end, &len,
133 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200134 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135
136 end = *p + len;
137
138 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
139 return( ret );
140
141 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
142 return( ret );
143
144 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200145 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200146 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
147
148 return( 0 );
149}
150
151/*
152 * X.509 v2/v3 unique identifier (not parsed)
153 */
154static int x509_get_uid( unsigned char **p,
155 const unsigned char *end,
156 x509_buf *uid, int n )
157{
158 int ret;
159
160 if( *p == end )
161 return( 0 );
162
163 uid->tag = **p;
164
165 if( ( ret = asn1_get_tag( p, end, &uid->len,
166 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
167 {
168 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
169 return( 0 );
170
171 return( ret );
172 }
173
174 uid->p = *p;
175 *p += uid->len;
176
177 return( 0 );
178}
179
180static int x509_get_basic_constraints( unsigned char **p,
181 const unsigned char *end,
182 int *ca_istrue,
183 int *max_pathlen )
184{
185 int ret;
186 size_t len;
187
188 /*
189 * BasicConstraints ::= SEQUENCE {
190 * cA BOOLEAN DEFAULT FALSE,
191 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
192 */
193 *ca_istrue = 0; /* DEFAULT FALSE */
194 *max_pathlen = 0; /* endless */
195
196 if( ( ret = asn1_get_tag( p, end, &len,
197 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200198 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199
200 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200201 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202
203 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
204 {
205 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
206 ret = asn1_get_int( p, end, ca_istrue );
207
208 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200209 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210
211 if( *ca_istrue != 0 )
212 *ca_istrue = 1;
213 }
214
215 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200216 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200217
218 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200219 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220
221 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200222 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
224
225 (*max_pathlen)++;
226
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200227 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200228}
229
230static int x509_get_ns_cert_type( unsigned char **p,
231 const unsigned char *end,
232 unsigned char *ns_cert_type)
233{
234 int ret;
235 x509_bitstring bs = { 0, 0, NULL };
236
237 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200238 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200239
240 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200241 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200242 POLARSSL_ERR_ASN1_INVALID_LENGTH );
243
244 /* Get actual bitstring */
245 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200246 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247}
248
249static int x509_get_key_usage( unsigned char **p,
250 const unsigned char *end,
251 unsigned char *key_usage)
252{
253 int ret;
254 x509_bitstring bs = { 0, 0, NULL };
255
256 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200257 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258
259 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200260 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261 POLARSSL_ERR_ASN1_INVALID_LENGTH );
262
263 /* Get actual bitstring */
264 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200265 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266}
267
268/*
269 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
270 *
271 * KeyPurposeId ::= OBJECT IDENTIFIER
272 */
273static int x509_get_ext_key_usage( unsigned char **p,
274 const unsigned char *end,
275 x509_sequence *ext_key_usage)
276{
277 int ret;
278
279 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200280 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281
282 /* Sequence length must be >= 1 */
283 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200284 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285 POLARSSL_ERR_ASN1_INVALID_LENGTH );
286
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200287 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288}
289
290/*
291 * SubjectAltName ::= GeneralNames
292 *
293 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
294 *
295 * GeneralName ::= CHOICE {
296 * otherName [0] OtherName,
297 * rfc822Name [1] IA5String,
298 * dNSName [2] IA5String,
299 * x400Address [3] ORAddress,
300 * directoryName [4] Name,
301 * ediPartyName [5] EDIPartyName,
302 * uniformResourceIdentifier [6] IA5String,
303 * iPAddress [7] OCTET STRING,
304 * registeredID [8] OBJECT IDENTIFIER }
305 *
306 * OtherName ::= SEQUENCE {
307 * type-id OBJECT IDENTIFIER,
308 * value [0] EXPLICIT ANY DEFINED BY type-id }
309 *
310 * EDIPartyName ::= SEQUENCE {
311 * nameAssigner [0] DirectoryString OPTIONAL,
312 * partyName [1] DirectoryString }
313 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000314 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315 */
316static int x509_get_subject_alt_name( unsigned char **p,
317 const unsigned char *end,
318 x509_sequence *subject_alt_name )
319{
320 int ret;
321 size_t len, tag_len;
322 asn1_buf *buf;
323 unsigned char tag;
324 asn1_sequence *cur = subject_alt_name;
325
326 /* Get main sequence tag */
327 if( ( ret = asn1_get_tag( p, end, &len,
328 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200329 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200330
331 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200332 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
334
335 while( *p < end )
336 {
337 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200338 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339 POLARSSL_ERR_ASN1_OUT_OF_DATA );
340
341 tag = **p;
342 (*p)++;
343 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200344 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
346 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200347 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
349
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200350 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
352 {
353 *p += tag_len;
354 continue;
355 }
356
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200358 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100360 if( cur->next != NULL )
361 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
362
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 cur->next = (asn1_sequence *) polarssl_malloc(
364 sizeof( asn1_sequence ) );
365
366 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200367 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368 POLARSSL_ERR_ASN1_MALLOC_FAILED );
369
370 memset( cur->next, 0, sizeof( asn1_sequence ) );
371 cur = cur->next;
372 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200373
374 buf = &(cur->buf);
375 buf->tag = tag;
376 buf->p = *p;
377 buf->len = tag_len;
378 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 }
380
381 /* Set final sequence entry's next pointer to NULL */
382 cur->next = NULL;
383
384 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200385 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
387
388 return( 0 );
389}
390
391/*
392 * X.509 v3 extensions
393 *
394 * TODO: Perform all of the basic constraints tests required by the RFC
395 * TODO: Set values for undetected extensions to a sane default?
396 *
397 */
398static int x509_get_crt_ext( unsigned char **p,
399 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200400 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401{
402 int ret;
403 size_t len;
404 unsigned char *end_ext_data, *end_ext_octet;
405
406 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
407 {
408 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
409 return( 0 );
410
411 return( ret );
412 }
413
414 while( *p < end )
415 {
416 /*
417 * Extension ::= SEQUENCE {
418 * extnID OBJECT IDENTIFIER,
419 * critical BOOLEAN DEFAULT FALSE,
420 * extnValue OCTET STRING }
421 */
422 x509_buf extn_oid = {0, 0, NULL};
423 int is_critical = 0; /* DEFAULT FALSE */
424 int ext_type = 0;
425
426 if( ( ret = asn1_get_tag( p, end, &len,
427 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200428 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
430 end_ext_data = *p + len;
431
432 /* Get extension ID */
433 extn_oid.tag = **p;
434
435 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200436 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
438 extn_oid.p = *p;
439 *p += extn_oid.len;
440
441 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200442 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443 POLARSSL_ERR_ASN1_OUT_OF_DATA );
444
445 /* Get optional critical */
446 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
447 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200448 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 /* Data should be octet string type */
451 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
452 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200453 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 end_ext_octet = *p + len;
456
457 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200458 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
460
461 /*
462 * Detect supported extensions
463 */
464 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
465
466 if( ret != 0 )
467 {
468 /* No parser found, skip extension */
469 *p = end_ext_octet;
470
471#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
472 if( is_critical )
473 {
474 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200475 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
477 }
478#endif
479 continue;
480 }
481
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100482 /* Forbid repeated extensions */
483 if( ( crt->ext_types & ext_type ) != 0 )
484 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
485
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200486 crt->ext_types |= ext_type;
487
488 switch( ext_type )
489 {
490 case EXT_BASIC_CONSTRAINTS:
491 /* Parse basic constraints */
492 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
493 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200494 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 break;
496
497 case EXT_KEY_USAGE:
498 /* Parse key usage */
499 if( ( ret = x509_get_key_usage( p, end_ext_octet,
500 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200501 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502 break;
503
504 case EXT_EXTENDED_KEY_USAGE:
505 /* Parse extended key usage */
506 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
507 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200508 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509 break;
510
511 case EXT_SUBJECT_ALT_NAME:
512 /* Parse subject alt name */
513 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
514 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200515 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516 break;
517
518 case EXT_NS_CERT_TYPE:
519 /* Parse netscape certificate type */
520 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
521 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200522 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 break;
524
525 default:
526 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
527 }
528 }
529
530 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200531 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
533
534 return( 0 );
535}
536
537/*
538 * Parse and fill a single X.509 certificate in DER format
539 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200540static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200541 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542{
543 int ret;
544 size_t len;
545 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200546 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100547
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200548 memset( &sig_params1, 0, sizeof( x509_buf ) );
549 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550
551 /*
552 * Check for valid input
553 */
554 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200555 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556
557 p = (unsigned char *) polarssl_malloc( len = buflen );
558
559 if( p == NULL )
560 return( POLARSSL_ERR_X509_MALLOC_FAILED );
561
562 memcpy( p, buf, buflen );
563
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564 crt->raw.p = p;
565 crt->raw.len = len;
566 end = p + len;
567
568 /*
569 * Certificate ::= SEQUENCE {
570 * tbsCertificate TBSCertificate,
571 * signatureAlgorithm AlgorithmIdentifier,
572 * signatureValue BIT STRING }
573 */
574 if( ( ret = asn1_get_tag( &p, end, &len,
575 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
576 {
577 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200578 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579 }
580
581 if( len > (size_t) ( end - p ) )
582 {
583 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200584 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
586 }
587 crt_end = p + len;
588
589 /*
590 * TBSCertificate ::= SEQUENCE {
591 */
592 crt->tbs.p = p;
593
594 if( ( ret = asn1_get_tag( &p, end, &len,
595 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
596 {
597 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200598 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200599 }
600
601 end = p + len;
602 crt->tbs.len = end - crt->tbs.p;
603
604 /*
605 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
606 *
607 * CertificateSerialNumber ::= INTEGER
608 *
609 * signature AlgorithmIdentifier
610 */
611 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
612 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100613 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200614 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200615 {
616 x509_crt_free( crt );
617 return( ret );
618 }
619
620 crt->version++;
621
622 if( crt->version > 3 )
623 {
624 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200625 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 }
627
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200628 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200629 &crt->sig_md, &crt->sig_pk,
630 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631 {
632 x509_crt_free( crt );
633 return( ret );
634 }
635
636 /*
637 * issuer Name
638 */
639 crt->issuer_raw.p = p;
640
641 if( ( ret = asn1_get_tag( &p, end, &len,
642 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
643 {
644 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200645 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200646 }
647
648 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
649 {
650 x509_crt_free( crt );
651 return( ret );
652 }
653
654 crt->issuer_raw.len = p - crt->issuer_raw.p;
655
656 /*
657 * Validity ::= SEQUENCE {
658 * notBefore Time,
659 * notAfter Time }
660 *
661 */
662 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
663 &crt->valid_to ) ) != 0 )
664 {
665 x509_crt_free( crt );
666 return( ret );
667 }
668
669 /*
670 * subject Name
671 */
672 crt->subject_raw.p = p;
673
674 if( ( ret = asn1_get_tag( &p, end, &len,
675 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
676 {
677 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200678 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200679 }
680
681 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
682 {
683 x509_crt_free( crt );
684 return( ret );
685 }
686
687 crt->subject_raw.len = p - crt->subject_raw.p;
688
689 /*
690 * SubjectPublicKeyInfo
691 */
Paul Bakkerda771152013-09-16 22:45:03 +0200692 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200693 {
694 x509_crt_free( crt );
695 return( ret );
696 }
697
698 /*
699 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
700 * -- If present, version shall be v2 or v3
701 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
702 * -- If present, version shall be v2 or v3
703 * extensions [3] EXPLICIT Extensions OPTIONAL
704 * -- If present, version shall be v3
705 */
706 if( crt->version == 2 || crt->version == 3 )
707 {
708 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
709 if( ret != 0 )
710 {
711 x509_crt_free( crt );
712 return( ret );
713 }
714 }
715
716 if( crt->version == 2 || crt->version == 3 )
717 {
718 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
719 if( ret != 0 )
720 {
721 x509_crt_free( crt );
722 return( ret );
723 }
724 }
725
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200726#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200727 if( crt->version == 3 )
728 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200729#endif
Paul Bakker66d5d072014-06-17 16:39:18 +0200730 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731 if( ret != 0 )
732 {
733 x509_crt_free( crt );
734 return( ret );
735 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200736#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200737 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200738#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739
740 if( p != end )
741 {
742 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200743 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
745 }
746
747 end = crt_end;
748
749 /*
750 * }
751 * -- end of TBSCertificate
752 *
753 * signatureAlgorithm AlgorithmIdentifier,
754 * signatureValue BIT STRING
755 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200756 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200757 {
758 x509_crt_free( crt );
759 return( ret );
760 }
761
762 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200763 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
764 sig_params1.len != sig_params2.len ||
Paul Bakker66d5d072014-06-17 16:39:18 +0200765 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766 {
767 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200768 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769 }
770
771 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
772 {
773 x509_crt_free( crt );
774 return( ret );
775 }
776
777 if( p != end )
778 {
779 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200780 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
782 }
783
784 return( 0 );
785}
786
787/*
788 * Parse one X.509 certificate in DER format from a buffer and add them to a
789 * chained list
790 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200791int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200792 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793{
794 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200795 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796
797 /*
798 * Check for valid input
799 */
800 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200801 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200802
803 while( crt->version != 0 && crt->next != NULL )
804 {
805 prev = crt;
806 crt = crt->next;
807 }
808
809 /*
810 * Add new certificate on the end of the chain if needed.
811 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200812 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200814 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815
816 if( crt->next == NULL )
817 return( POLARSSL_ERR_X509_MALLOC_FAILED );
818
819 prev = crt;
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100820 x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200821 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200822 }
823
Paul Bakkerddf26b42013-09-18 13:46:23 +0200824 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200825 {
826 if( prev )
827 prev->next = NULL;
828
829 if( crt != chain )
830 polarssl_free( crt );
831
832 return( ret );
833 }
834
835 return( 0 );
836}
837
838/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200839 * Parse one or more PEM certificates from a buffer and add them to the chained
840 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200842int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843{
844 int success = 0, first_error = 0, total_failed = 0;
845 int buf_format = X509_FORMAT_DER;
846
847 /*
848 * Check for valid input
849 */
850 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200851 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200852
853 /*
854 * Determine buffer content. Buffer contains either one DER certificate or
855 * one or more PEM certificates.
856 */
857#if defined(POLARSSL_PEM_PARSE_C)
858 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
859 buf_format = X509_FORMAT_PEM;
860#endif
861
862 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200863 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200864
865#if defined(POLARSSL_PEM_PARSE_C)
866 if( buf_format == X509_FORMAT_PEM )
867 {
868 int ret;
869 pem_context pem;
870
871 while( buflen > 0 )
872 {
873 size_t use_len;
874 pem_init( &pem );
875
876 ret = pem_read_buffer( &pem,
877 "-----BEGIN CERTIFICATE-----",
878 "-----END CERTIFICATE-----",
879 buf, NULL, 0, &use_len );
880
881 if( ret == 0 )
882 {
883 /*
884 * Was PEM encoded
885 */
886 buflen -= use_len;
887 buf += use_len;
888 }
889 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
890 {
891 return( ret );
892 }
893 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
894 {
895 pem_free( &pem );
896
897 /*
898 * PEM header and footer were found
899 */
900 buflen -= use_len;
901 buf += use_len;
902
903 if( first_error == 0 )
904 first_error = ret;
905
Paul Bakker5a5fa922014-09-26 14:53:04 +0200906 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907 continue;
908 }
909 else
910 break;
911
Paul Bakkerddf26b42013-09-18 13:46:23 +0200912 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200913
914 pem_free( &pem );
915
916 if( ret != 0 )
917 {
918 /*
919 * Quit parsing on a memory error
920 */
921 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
922 return( ret );
923
924 if( first_error == 0 )
925 first_error = ret;
926
927 total_failed++;
928 continue;
929 }
930
931 success = 1;
932 }
933 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200934#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200935
936 if( success )
937 return( total_failed );
938 else if( first_error )
939 return( first_error );
940 else
941 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
942}
943
944#if defined(POLARSSL_FS_IO)
945/*
946 * Load one or more certificates and add them to the chained list
947 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200948int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949{
950 int ret;
951 size_t n;
952 unsigned char *buf;
953
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100954 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 return( ret );
956
Paul Bakkerddf26b42013-09-18 13:46:23 +0200957 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958
Paul Bakker34617722014-06-13 17:20:13 +0200959 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960 polarssl_free( buf );
961
962 return( ret );
963}
964
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100965#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100966static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
967#endif
968
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200969int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970{
971 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100972#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973 int w_ret;
974 WCHAR szDir[MAX_PATH];
975 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200976 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200977 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
Paul Bakker9af723c2014-05-01 13:03:14 +0200979 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 HANDLE hFind;
981
982 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200983 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984
Paul Bakker9af723c2014-05-01 13:03:14 +0200985 memset( szDir, 0, sizeof(szDir) );
986 memset( filename, 0, MAX_PATH );
987 memcpy( filename, path, len );
988 filename[len++] = '\\';
989 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200990 filename[len++] = '*';
991
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200992 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
993 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994
995 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200996 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
998
999 len = MAX_PATH - len;
1000 do
1001 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001002 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001003
1004 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1005 continue;
1006
Paul Bakker9af723c2014-05-01 13:03:14 +02001007 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001008 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001009 p, len - 1,
1010 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011
Paul Bakkerddf26b42013-09-18 13:46:23 +02001012 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013 if( w_ret < 0 )
1014 ret++;
1015 else
1016 ret += w_ret;
1017 }
1018 while( FindNextFileW( hFind, &file_data ) != 0 );
1019
Paul Bakker66d5d072014-06-17 16:39:18 +02001020 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1022
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001024#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001025 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001027 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 char entry_name[255];
1029 DIR *dir = opendir( path );
1030
Paul Bakker66d5d072014-06-17 16:39:18 +02001031 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1033
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001034#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001035 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1036 return( ret );
1037#endif
1038
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001039 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001040 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001041 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001043 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 {
1045 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001046 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1047 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 }
1049
1050 if( !S_ISREG( sb.st_mode ) )
1051 continue;
1052
1053 // Ignore parse errors
1054 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001055 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001056 if( t_ret < 0 )
1057 ret++;
1058 else
1059 ret += t_ret;
1060 }
1061 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001062
1063cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001064#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001065 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1066 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1067#endif
1068
Paul Bakkerbe089b02013-10-14 15:51:50 +02001069#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001070
1071 return( ret );
1072}
1073#endif /* POLARSSL_FS_IO */
1074
Paul Bakker6edcd412013-10-29 15:22:54 +01001075#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1076 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077#include <stdarg.h>
1078
1079#if !defined vsnprintf
1080#define vsnprintf _vsnprintf
1081#endif // vsnprintf
1082
1083/*
1084 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1085 * Result value is not size of buffer needed, but -1 if no fit is possible.
1086 *
1087 * This fuction tries to 'fix' this by at least suggesting enlarging the
1088 * size by 20.
1089 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001090static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001091{
1092 va_list ap;
1093 int res = -1;
1094
1095 va_start( ap, format );
1096
1097 res = vsnprintf( str, size, format, ap );
1098
1099 va_end( ap );
1100
1101 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001102 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001103 return( (int) size + 20 );
1104
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001105 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001106}
1107
1108#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001109#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001110
1111#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1112
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001113#define SAFE_SNPRINTF() \
1114{ \
1115 if( ret == -1 ) \
1116 return( -1 ); \
1117 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001118 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001119 p[n - 1] = '\0'; \
1120 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1121 } \
1122 \
1123 n -= (unsigned int) ret; \
1124 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001125}
1126
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001127static int x509_info_subject_alt_name( char **buf, size_t *size,
1128 const x509_sequence *subject_alt_name )
1129{
1130 size_t i;
1131 size_t n = *size;
1132 char *p = *buf;
1133 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001134 const char *sep = "";
1135 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001136
1137 while( cur != NULL )
1138 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001139 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001140 {
1141 *p = '\0';
1142 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1143 }
1144
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001145 n -= cur->buf.len + sep_len;
1146 for( i = 0; i < sep_len; i++ )
1147 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001148 for( i = 0; i < cur->buf.len; i++ )
1149 *p++ = cur->buf.p[i];
1150
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001151 sep = ", ";
1152 sep_len = 2;
1153
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001154 cur = cur->next;
1155 }
1156
1157 *p = '\0';
1158
1159 *size = n;
1160 *buf = p;
1161
1162 return( 0 );
1163}
1164
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001165#define PRINT_ITEM(i) \
1166 { \
1167 ret = snprintf( p, n, "%s" i, sep ); \
1168 SAFE_SNPRINTF(); \
1169 sep = ", "; \
1170 }
1171
1172#define CERT_TYPE(type,name) \
1173 if( ns_cert_type & type ) \
1174 PRINT_ITEM( name );
1175
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001176static int x509_info_cert_type( char **buf, size_t *size,
1177 unsigned char ns_cert_type )
1178{
1179 int ret;
1180 size_t n = *size;
1181 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001182 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001183
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001184 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1185 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1186 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1187 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1188 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1189 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1190 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1191 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001192
1193 *size = n;
1194 *buf = p;
1195
1196 return( 0 );
1197}
1198
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001199#define KEY_USAGE(code,name) \
1200 if( key_usage & code ) \
1201 PRINT_ITEM( name );
1202
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001203static int x509_info_key_usage( char **buf, size_t *size,
1204 unsigned char key_usage )
1205{
1206 int ret;
1207 size_t n = *size;
1208 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001209 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001210
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001211 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1212 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1213 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1214 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1215 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1216 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1217 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001218
1219 *size = n;
1220 *buf = p;
1221
1222 return( 0 );
1223}
1224
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001225static int x509_info_ext_key_usage( char **buf, size_t *size,
1226 const x509_sequence *extended_key_usage )
1227{
1228 int ret;
1229 const char *desc;
1230 size_t n = *size;
1231 char *p = *buf;
1232 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001233 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001234
1235 while( cur != NULL )
1236 {
1237 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1238 desc = "???";
1239
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001240 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001241 SAFE_SNPRINTF();
1242
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001243 sep = ", ";
1244
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001245 cur = cur->next;
1246 }
1247
1248 *size = n;
1249 *buf = p;
1250
1251 return( 0 );
1252}
1253
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254/*
1255 * Return an informational string about the certificate.
1256 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001257#define BEFORE_COLON 18
1258#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001259int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001260 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261{
1262 int ret;
1263 size_t n;
1264 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265 char key_size_str[BEFORE_COLON];
1266
1267 p = buf;
1268 n = size;
1269
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001270 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001271 prefix, crt->version );
1272 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001273 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001274 prefix );
1275 SAFE_SNPRINTF();
1276
Paul Bakker66d5d072014-06-17 16:39:18 +02001277 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001278 SAFE_SNPRINTF();
1279
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001280 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001281 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001282 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 SAFE_SNPRINTF();
1284
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001285 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001287 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 SAFE_SNPRINTF();
1289
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001290 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1292 crt->valid_from.year, crt->valid_from.mon,
1293 crt->valid_from.day, crt->valid_from.hour,
1294 crt->valid_from.min, crt->valid_from.sec );
1295 SAFE_SNPRINTF();
1296
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001297 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001298 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1299 crt->valid_to.year, crt->valid_to.mon,
1300 crt->valid_to.day, crt->valid_to.hour,
1301 crt->valid_to.min, crt->valid_to.sec );
1302 SAFE_SNPRINTF();
1303
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001304 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001305 SAFE_SNPRINTF();
1306
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001307 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001308 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001309 SAFE_SNPRINTF();
1310
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001311 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001312 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1313 pk_get_name( &crt->pk ) ) ) != 0 )
1314 {
1315 return( ret );
1316 }
1317
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001318 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001319 (int) pk_get_size( &crt->pk ) );
1320 SAFE_SNPRINTF();
1321
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001322 /*
1323 * Optional extensions
1324 */
1325
1326 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1327 {
1328 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1329 crt->ca_istrue ? "true" : "false" );
1330 SAFE_SNPRINTF();
1331
1332 if( crt->max_pathlen > 0 )
1333 {
1334 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1335 SAFE_SNPRINTF();
1336 }
1337 }
1338
1339 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1340 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001341 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001342 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001343
1344 if( ( ret = x509_info_subject_alt_name( &p, &n,
1345 &crt->subject_alt_names ) ) != 0 )
1346 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001347 }
1348
1349 if( crt->ext_types & EXT_NS_CERT_TYPE )
1350 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001351 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001352 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001353
1354 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1355 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001356 }
1357
1358 if( crt->ext_types & EXT_KEY_USAGE )
1359 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001360 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001361 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001362
1363 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1364 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001365 }
1366
1367 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1368 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001369 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001370 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001371
1372 if( ( ret = x509_info_ext_key_usage( &p, &n,
1373 &crt->ext_key_usage ) ) != 0 )
1374 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001375 }
1376
1377 ret = snprintf( p, n, "\n" );
1378 SAFE_SNPRINTF();
1379
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001380 return( (int) ( size - n ) );
1381}
1382
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001383#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1384int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1385{
1386 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1387 ( crt->key_usage & usage ) != usage )
1388 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1389
1390 return( 0 );
1391}
1392#endif
1393
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001394#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1395int x509_crt_check_extended_key_usage( const x509_crt *crt,
1396 const char *usage_oid,
1397 size_t usage_len )
1398{
1399 const x509_sequence *cur;
1400
1401 /* Extension is not mandatory, absent means no restriction */
1402 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1403 return( 0 );
1404
1405 /*
1406 * Look for the requested usage (or wildcard ANY) in our list
1407 */
1408 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1409 {
1410 const x509_buf *cur_oid = &cur->buf;
1411
1412 if( cur_oid->len == usage_len &&
1413 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1414 {
1415 return( 0 );
1416 }
1417
1418 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1419 return( 0 );
1420 }
1421
1422 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1423}
Paul Bakker9af723c2014-05-01 13:03:14 +02001424#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001425
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426#if defined(POLARSSL_X509_CRL_PARSE_C)
1427/*
1428 * Return 1 if the certificate is revoked, or 0 otherwise.
1429 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001430int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001431{
1432 const x509_crl_entry *cur = &crl->entry;
1433
1434 while( cur != NULL && cur->serial.len != 0 )
1435 {
1436 if( crt->serial.len == cur->serial.len &&
1437 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1438 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001439 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001440 return( 1 );
1441 }
1442
1443 cur = cur->next;
1444 }
1445
1446 return( 0 );
1447}
1448
1449/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001450 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001451 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001452static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001453 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454{
1455 int flags = 0;
1456 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1457 const md_info_t *md_info;
1458
1459 if( ca == NULL )
1460 return( flags );
1461
1462 /*
1463 * TODO: What happens if no CRL is present?
1464 * Suggestion: Revocation state should be unknown if no CRL is present.
1465 * For backwards compatibility this is not yet implemented.
1466 */
1467
1468 while( crl_list != NULL )
1469 {
1470 if( crl_list->version == 0 ||
1471 crl_list->issuer_raw.len != ca->subject_raw.len ||
1472 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1473 crl_list->issuer_raw.len ) != 0 )
1474 {
1475 crl_list = crl_list->next;
1476 continue;
1477 }
1478
1479 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001480 * Check if the CA is configured to sign CRLs
1481 */
1482#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1483 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1484 {
1485 flags |= BADCRL_NOT_TRUSTED;
1486 break;
1487 }
1488#endif
1489
1490 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001491 * Check if CRL is correctly signed by the trusted CA
1492 */
1493 md_info = md_info_from_type( crl_list->sig_md );
1494 if( md_info == NULL )
1495 {
1496 /*
1497 * Cannot check 'unknown' hash
1498 */
1499 flags |= BADCRL_NOT_TRUSTED;
1500 break;
1501 }
1502
1503 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1504
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001505 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1506 crl_list->sig_md, hash, md_info->size,
1507 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001508 {
1509 flags |= BADCRL_NOT_TRUSTED;
1510 break;
1511 }
1512
1513 /*
1514 * Check for validity of CRL (Do not drop out)
1515 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001516 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517 flags |= BADCRL_EXPIRED;
1518
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001519 if( x509_time_future( &crl_list->this_update ) )
1520 flags |= BADCRL_FUTURE;
1521
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522 /*
1523 * Check if certificate is revoked
1524 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001525 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001526 {
1527 flags |= BADCERT_REVOKED;
1528 break;
1529 }
1530
1531 crl_list = crl_list->next;
1532 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001533 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001534}
1535#endif /* POLARSSL_X509_CRL_PARSE_C */
1536
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001537/*
1538 * Like memcmp, but case-insensitive and always returns -1 if different
1539 */
1540static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541{
1542 size_t i;
1543 unsigned char diff;
1544 const unsigned char *n1 = s1, *n2 = s2;
1545
1546 for( i = 0; i < len; i++ )
1547 {
1548 diff = n1[i] ^ n2[i];
1549
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001550 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 continue;
1552
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001553 if( diff == 32 &&
1554 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1555 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1556 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001558 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001560 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561 }
1562
1563 return( 0 );
1564}
1565
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001566/*
1567 * Return 1 if match, 0 if not
1568 * TODO: inverted return value!
1569 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001570static int x509_wildcard_verify( const char *cn, x509_buf *name )
1571{
1572 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001573 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574
1575 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1576 return( 0 );
1577
Paul Bakker14b16c62014-05-28 11:33:54 +02001578 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 {
1580 if( cn[i] == '.' )
1581 {
1582 cn_idx = i;
1583 break;
1584 }
1585 }
1586
1587 if( cn_idx == 0 )
1588 return( 0 );
1589
Paul Bakker14b16c62014-05-28 11:33:54 +02001590 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001591 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001592 {
1593 return( 1 );
1594 }
1595
1596 return( 0 );
1597}
1598
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001599/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001600 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1601 * variations (but not all).
1602 *
1603 * Return 0 if equal, -1 otherwise.
1604 */
1605static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1606{
1607 if( a->tag == b->tag &&
1608 a->len == b->len &&
1609 memcmp( a->p, b->p, b->len ) == 0 )
1610 {
1611 return( 0 );
1612 }
1613
1614 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1615 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1616 a->len == b->len &&
1617 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1618 {
1619 return( 0 );
1620 }
1621
1622 return( -1 );
1623}
1624
1625/*
1626 * Compare two X.509 Names (aka rdnSequence).
1627 *
1628 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1629 * we sometimes return unequal when the full algorithm would return equal,
1630 * but never the other way. (In particular, we don't do Unicode normalisation
1631 * or space folding.)
1632 *
1633 * Return 0 if equal, -1 otherwise.
1634 */
1635static int x509_name_cmp( const x509_name *a, const x509_name *b )
1636{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001637 /* Avoid recursion, it might not be optimised by the compiler */
1638 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001639 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001640 if( a == NULL || b == NULL )
1641 return( -1 );
1642
1643 /* type */
1644 if( a->oid.tag != b->oid.tag ||
1645 a->oid.len != b->oid.len ||
1646 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1647 {
1648 return( -1 );
1649 }
1650
1651 /* value */
1652 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1653 return( -1 );
1654
1655 a = a->next;
1656 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001657 }
1658
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001659 /* a == NULL == b */
1660 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001661}
1662
1663/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001664 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1665 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001666 *
1667 * top means parent is a locally-trusted certificate
1668 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001669 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001670static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001671 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001672 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001673{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001674 int need_ca_bit;
1675
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001676 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001677 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001678 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001679
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001680 /* Parent must have the basicConstraints CA bit set as a general rule */
1681 need_ca_bit = 1;
1682
1683 /* Exception: v1/v2 certificates that are locally trusted. */
1684 if( top && parent->version < 3 )
1685 need_ca_bit = 0;
1686
1687 /* Exception: self-signed end-entity certs that are locally trusted. */
1688 if( top && bottom &&
1689 child->raw.len == parent->raw.len &&
1690 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1691 {
1692 need_ca_bit = 0;
1693 }
1694
1695 if( need_ca_bit && ! parent->ca_istrue )
1696 return( -1 );
1697
1698#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1699 if( need_ca_bit &&
1700 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001701 {
1702 return( -1 );
1703 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001704#endif
1705
1706 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001707}
1708
Paul Bakkerddf26b42013-09-18 13:46:23 +02001709static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001710 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001711 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001712 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713 void *p_vrfy )
1714{
1715 int ret;
1716 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1717 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1718 const md_info_t *md_info;
1719
Paul Bakker86d0c192013-09-18 11:11:02 +02001720 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721 *flags |= BADCERT_EXPIRED;
1722
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001723 if( x509_time_future( &child->valid_from ) )
1724 *flags |= BADCERT_FUTURE;
1725
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726 /*
1727 * Child is the top of the chain. Check against the trust_ca list.
1728 */
1729 *flags |= BADCERT_NOT_TRUSTED;
1730
1731 md_info = md_info_from_type( child->sig_md );
1732 if( md_info == NULL )
1733 {
1734 /*
1735 * Cannot check 'unknown', no need to try any CA
1736 */
1737 trust_ca = NULL;
1738 }
1739 else
1740 md( md_info, child->tbs.p, child->tbs.len, hash );
1741
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001742 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001744 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001746
1747 /*
1748 * Reduce path_len to check against if top of the chain is
1749 * the same as the trusted CA
1750 */
1751 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1752 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1753 child->issuer_raw.len ) == 0 )
1754 {
1755 check_path_cnt--;
1756 }
1757
1758 if( trust_ca->max_pathlen > 0 &&
1759 trust_ca->max_pathlen < check_path_cnt )
1760 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761 continue;
1762 }
1763
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001764 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1765 child->sig_md, hash, md_info->size,
1766 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001767 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 continue;
1769 }
1770
1771 /*
1772 * Top of chain is signed by a trusted CA
1773 */
1774 *flags &= ~BADCERT_NOT_TRUSTED;
1775 break;
1776 }
1777
1778 /*
1779 * If top of chain is not the same as the trusted CA send a verify request
1780 * to the callback for any issues with validity and CRL presence for the
1781 * trusted CA certificate.
1782 */
1783 if( trust_ca != NULL &&
1784 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1785 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1786 child->issuer_raw.len ) != 0 ) )
1787 {
1788#if defined(POLARSSL_X509_CRL_PARSE_C)
1789 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001790 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001791#else
1792 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793#endif
1794
Paul Bakker86d0c192013-09-18 11:11:02 +02001795 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796 ca_flags |= BADCERT_EXPIRED;
1797
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001798 if( x509_time_future( &trust_ca->valid_from ) )
1799 ca_flags |= BADCERT_FUTURE;
1800
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001801 if( NULL != f_vrfy )
1802 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001803 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1804 &ca_flags ) ) != 0 )
1805 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001807 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808 }
1809 }
1810
1811 /* Call callback on top cert */
1812 if( NULL != f_vrfy )
1813 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001814 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815 return( ret );
1816 }
1817
1818 *flags |= ca_flags;
1819
1820 return( 0 );
1821}
1822
Paul Bakkerddf26b42013-09-18 13:46:23 +02001823static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001824 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001825 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001826 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827 void *p_vrfy )
1828{
1829 int ret;
1830 int parent_flags = 0;
1831 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001832 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001833 const md_info_t *md_info;
1834
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001835 /* path_cnt is 0 for the first intermediate CA */
1836 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
1837 {
1838 *flags |= BADCERT_NOT_TRUSTED;
1839 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1840 }
1841
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001842 if( x509_time_expired( &child->valid_to ) )
1843 *flags |= BADCERT_EXPIRED;
1844
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001845 if( x509_time_future( &child->valid_from ) )
1846 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001847
1848 md_info = md_info_from_type( child->sig_md );
1849 if( md_info == NULL )
1850 {
1851 /*
1852 * Cannot check 'unknown' hash
1853 */
1854 *flags |= BADCERT_NOT_TRUSTED;
1855 }
1856 else
1857 {
1858 md( md_info, child->tbs.p, child->tbs.len, hash );
1859
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001860 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1861 child->sig_md, hash, md_info->size,
1862 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001863 {
1864 *flags |= BADCERT_NOT_TRUSTED;
1865 }
1866 }
1867
1868#if defined(POLARSSL_X509_CRL_PARSE_C)
1869 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001870 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001871#endif
1872
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001873 /* Look for a grandparent upwards the chain */
1874 for( grandparent = parent->next;
1875 grandparent != NULL;
1876 grandparent = grandparent->next )
1877 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001878 if( x509_crt_check_parent( parent, grandparent,
1879 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001880 break;
1881 }
1882
1883 /* Is our parent part of the chain or at the top? */
1884 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001885 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001886 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1887 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001888 if( ret != 0 )
1889 return( ret );
1890 }
1891 else
1892 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001893 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1894 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001895 if( ret != 0 )
1896 return( ret );
1897 }
1898
1899 /* child is verified to be a child of the parent, call verify callback */
1900 if( NULL != f_vrfy )
1901 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1902 return( ret );
1903
1904 *flags |= parent_flags;
1905
1906 return( 0 );
1907}
1908
1909/*
1910 * Verify the certificate validity
1911 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001912int x509_crt_verify( x509_crt *crt,
1913 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001914 x509_crl *ca_crl,
1915 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001916 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001917 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918{
1919 size_t cn_len;
1920 int ret;
1921 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001922 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001923 x509_name *name;
1924 x509_sequence *cur = NULL;
1925
1926 *flags = 0;
1927
1928 if( cn != NULL )
1929 {
1930 name = &crt->subject;
1931 cn_len = strlen( cn );
1932
1933 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1934 {
1935 cur = &crt->subject_alt_names;
1936
1937 while( cur != NULL )
1938 {
1939 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001940 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001941 break;
1942
1943 if( cur->buf.len > 2 &&
1944 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1945 x509_wildcard_verify( cn, &cur->buf ) )
1946 break;
1947
1948 cur = cur->next;
1949 }
1950
1951 if( cur == NULL )
1952 *flags |= BADCERT_CN_MISMATCH;
1953 }
1954 else
1955 {
1956 while( name != NULL )
1957 {
1958 if( OID_CMP( OID_AT_CN, &name->oid ) )
1959 {
1960 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001961 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001962 break;
1963
1964 if( name->val.len > 2 &&
1965 memcmp( name->val.p, "*.", 2 ) == 0 &&
1966 x509_wildcard_verify( cn, &name->val ) )
1967 break;
1968 }
1969
1970 name = name->next;
1971 }
1972
1973 if( name == NULL )
1974 *flags |= BADCERT_CN_MISMATCH;
1975 }
1976 }
1977
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001978 /* Look for a parent upwards the chain */
1979 for( parent = crt->next; parent != NULL; parent = parent->next )
1980 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001981 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001982 break;
1983 }
1984
1985 /* Are we part of the chain or at the top? */
1986 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001987 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001988 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1989 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990 if( ret != 0 )
1991 return( ret );
1992 }
1993 else
1994 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001995 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1996 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001997 if( ret != 0 )
1998 return( ret );
1999 }
2000
2001 if( *flags != 0 )
2002 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
2003
2004 return( 0 );
2005}
2006
2007/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002008 * Initialize a certificate chain
2009 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002010void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002011{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002012 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002013}
2014
2015/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002016 * Unallocate all certificate data
2017 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002018void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002020 x509_crt *cert_cur = crt;
2021 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022 x509_name *name_cur;
2023 x509_name *name_prv;
2024 x509_sequence *seq_cur;
2025 x509_sequence *seq_prv;
2026
2027 if( crt == NULL )
2028 return;
2029
2030 do
2031 {
2032 pk_free( &cert_cur->pk );
2033
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002034#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002035 polarssl_free( cert_cur->sig_opts );
2036#endif
2037
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002038 name_cur = cert_cur->issuer.next;
2039 while( name_cur != NULL )
2040 {
2041 name_prv = name_cur;
2042 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002043 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002044 polarssl_free( name_prv );
2045 }
2046
2047 name_cur = cert_cur->subject.next;
2048 while( name_cur != NULL )
2049 {
2050 name_prv = name_cur;
2051 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002052 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002053 polarssl_free( name_prv );
2054 }
2055
2056 seq_cur = cert_cur->ext_key_usage.next;
2057 while( seq_cur != NULL )
2058 {
2059 seq_prv = seq_cur;
2060 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002061 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002062 polarssl_free( seq_prv );
2063 }
2064
2065 seq_cur = cert_cur->subject_alt_names.next;
2066 while( seq_cur != NULL )
2067 {
2068 seq_prv = seq_cur;
2069 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002070 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002071 polarssl_free( seq_prv );
2072 }
2073
2074 if( cert_cur->raw.p != NULL )
2075 {
Paul Bakker34617722014-06-13 17:20:13 +02002076 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002077 polarssl_free( cert_cur->raw.p );
2078 }
2079
2080 cert_cur = cert_cur->next;
2081 }
2082 while( cert_cur != NULL );
2083
2084 cert_cur = crt;
2085 do
2086 {
2087 cert_prv = cert_cur;
2088 cert_cur = cert_cur->next;
2089
Paul Bakker34617722014-06-13 17:20:13 +02002090 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002091 if( cert_prv != crt )
2092 polarssl_free( cert_prv );
2093 }
2094 while( cert_cur != NULL );
2095}
2096
Paul Bakker9af723c2014-05-01 13:03:14 +02002097#endif /* POLARSSL_X509_CRT_PARSE_C */