blob: 2b3c092c1fd229c269feaa7c25d461f78f848eec [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 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificate format for PKI.
27 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020028 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
29 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
30 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020031 *
32 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
34 */
35
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020036#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020038#else
39#include POLARSSL_CONFIG_FILE
40#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
42#if defined(POLARSSL_X509_CRT_PARSE_C)
43
44#include "polarssl/x509_crt.h"
45#include "polarssl/oid.h"
46#if defined(POLARSSL_PEM_PARSE_C)
47#include "polarssl/pem.h"
48#endif
49
Paul Bakker7dc4c442014-02-01 22:50:26 +010050#if defined(POLARSSL_PLATFORM_C)
51#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052#else
53#define polarssl_malloc malloc
54#define polarssl_free free
55#endif
56
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010057#if defined(POLARSSL_THREADING_C)
58#include "polarssl/threading.h"
59#endif
60
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <string.h>
62#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010063#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020064#include <windows.h>
65#else
66#include <time.h>
67#endif
68
Paul Bakkerfa6a6202013-10-28 18:48:30 +010069#if defined(EFIX64) || defined(EFI32)
70#include <stdio.h>
71#endif
72
Paul Bakker7c6b2c32013-09-16 13:49:26 +020073#if defined(POLARSSL_FS_IO)
74#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020075#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076#include <sys/types.h>
77#include <sys/stat.h>
78#include <dirent.h>
79#endif
80#endif
81
Paul Bakker34617722014-06-13 17:20:13 +020082/* Implementation that should never be optimized out by the compiler */
83static void polarssl_zeroize( void *v, size_t n ) {
84 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
85}
86
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087/*
88 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
89 */
90static int x509_get_version( unsigned char **p,
91 const unsigned char *end,
92 int *ver )
93{
94 int ret;
95 size_t len;
96
97 if( ( ret = asn1_get_tag( p, end, &len,
98 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
99 {
100 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
101 {
102 *ver = 0;
103 return( 0 );
104 }
105
106 return( ret );
107 }
108
109 end = *p + len;
110
111 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200112 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200113
114 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200115 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
117
118 return( 0 );
119}
120
121/*
122 * Validity ::= SEQUENCE {
123 * notBefore Time,
124 * notAfter Time }
125 */
126static int x509_get_dates( unsigned char **p,
127 const unsigned char *end,
128 x509_time *from,
129 x509_time *to )
130{
131 int ret;
132 size_t len;
133
134 if( ( ret = asn1_get_tag( p, end, &len,
135 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200136 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200137
138 end = *p + len;
139
140 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
141 return( ret );
142
143 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
144 return( ret );
145
146 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200147 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200148 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
149
150 return( 0 );
151}
152
153/*
154 * X.509 v2/v3 unique identifier (not parsed)
155 */
156static int x509_get_uid( unsigned char **p,
157 const unsigned char *end,
158 x509_buf *uid, int n )
159{
160 int ret;
161
162 if( *p == end )
163 return( 0 );
164
165 uid->tag = **p;
166
167 if( ( ret = asn1_get_tag( p, end, &uid->len,
168 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
169 {
170 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
171 return( 0 );
172
173 return( ret );
174 }
175
176 uid->p = *p;
177 *p += uid->len;
178
179 return( 0 );
180}
181
182static int x509_get_basic_constraints( unsigned char **p,
183 const unsigned char *end,
184 int *ca_istrue,
185 int *max_pathlen )
186{
187 int ret;
188 size_t len;
189
190 /*
191 * BasicConstraints ::= SEQUENCE {
192 * cA BOOLEAN DEFAULT FALSE,
193 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
194 */
195 *ca_istrue = 0; /* DEFAULT FALSE */
196 *max_pathlen = 0; /* endless */
197
198 if( ( ret = asn1_get_tag( p, end, &len,
199 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200200 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200201
202 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200203 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200204
205 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
206 {
207 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
208 ret = asn1_get_int( p, end, ca_istrue );
209
210 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200211 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200212
213 if( *ca_istrue != 0 )
214 *ca_istrue = 1;
215 }
216
217 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200218 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219
220 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200221 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200222
223 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200224 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
226
227 (*max_pathlen)++;
228
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200229 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230}
231
232static int x509_get_ns_cert_type( unsigned char **p,
233 const unsigned char *end,
234 unsigned char *ns_cert_type)
235{
236 int ret;
237 x509_bitstring bs = { 0, 0, NULL };
238
239 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200240 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200241
242 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200243 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244 POLARSSL_ERR_ASN1_INVALID_LENGTH );
245
246 /* Get actual bitstring */
247 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200248 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249}
250
251static int x509_get_key_usage( unsigned char **p,
252 const unsigned char *end,
253 unsigned char *key_usage)
254{
255 int ret;
256 x509_bitstring bs = { 0, 0, NULL };
257
258 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200259 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260
261 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200262 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 POLARSSL_ERR_ASN1_INVALID_LENGTH );
264
265 /* Get actual bitstring */
266 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200267 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268}
269
270/*
271 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
272 *
273 * KeyPurposeId ::= OBJECT IDENTIFIER
274 */
275static int x509_get_ext_key_usage( unsigned char **p,
276 const unsigned char *end,
277 x509_sequence *ext_key_usage)
278{
279 int ret;
280
281 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200282 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283
284 /* Sequence length must be >= 1 */
285 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200286 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287 POLARSSL_ERR_ASN1_INVALID_LENGTH );
288
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200289 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290}
291
292/*
293 * SubjectAltName ::= GeneralNames
294 *
295 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
296 *
297 * GeneralName ::= CHOICE {
298 * otherName [0] OtherName,
299 * rfc822Name [1] IA5String,
300 * dNSName [2] IA5String,
301 * x400Address [3] ORAddress,
302 * directoryName [4] Name,
303 * ediPartyName [5] EDIPartyName,
304 * uniformResourceIdentifier [6] IA5String,
305 * iPAddress [7] OCTET STRING,
306 * registeredID [8] OBJECT IDENTIFIER }
307 *
308 * OtherName ::= SEQUENCE {
309 * type-id OBJECT IDENTIFIER,
310 * value [0] EXPLICIT ANY DEFINED BY type-id }
311 *
312 * EDIPartyName ::= SEQUENCE {
313 * nameAssigner [0] DirectoryString OPTIONAL,
314 * partyName [1] DirectoryString }
315 *
316 * NOTE: PolarSSL only parses and uses dNSName at this point.
317 */
318static int x509_get_subject_alt_name( unsigned char **p,
319 const unsigned char *end,
320 x509_sequence *subject_alt_name )
321{
322 int ret;
323 size_t len, tag_len;
324 asn1_buf *buf;
325 unsigned char tag;
326 asn1_sequence *cur = subject_alt_name;
327
328 /* Get main sequence tag */
329 if( ( ret = asn1_get_tag( p, end, &len,
330 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200331 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332
333 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200334 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
336
337 while( *p < end )
338 {
339 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200340 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341 POLARSSL_ERR_ASN1_OUT_OF_DATA );
342
343 tag = **p;
344 (*p)++;
345 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200346 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347
348 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200349 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
351
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200352 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
354 {
355 *p += tag_len;
356 continue;
357 }
358
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200360 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 {
362 cur->next = (asn1_sequence *) polarssl_malloc(
363 sizeof( asn1_sequence ) );
364
365 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200366 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367 POLARSSL_ERR_ASN1_MALLOC_FAILED );
368
369 memset( cur->next, 0, sizeof( asn1_sequence ) );
370 cur = cur->next;
371 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200372
373 buf = &(cur->buf);
374 buf->tag = tag;
375 buf->p = *p;
376 buf->len = tag_len;
377 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 }
379
380 /* Set final sequence entry's next pointer to NULL */
381 cur->next = NULL;
382
383 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200384 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
386
387 return( 0 );
388}
389
390/*
391 * X.509 v3 extensions
392 *
393 * TODO: Perform all of the basic constraints tests required by the RFC
394 * TODO: Set values for undetected extensions to a sane default?
395 *
396 */
397static int x509_get_crt_ext( unsigned char **p,
398 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200399 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400{
401 int ret;
402 size_t len;
403 unsigned char *end_ext_data, *end_ext_octet;
404
405 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
406 {
407 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
408 return( 0 );
409
410 return( ret );
411 }
412
413 while( *p < end )
414 {
415 /*
416 * Extension ::= SEQUENCE {
417 * extnID OBJECT IDENTIFIER,
418 * critical BOOLEAN DEFAULT FALSE,
419 * extnValue OCTET STRING }
420 */
421 x509_buf extn_oid = {0, 0, NULL};
422 int is_critical = 0; /* DEFAULT FALSE */
423 int ext_type = 0;
424
425 if( ( ret = asn1_get_tag( p, end, &len,
426 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200427 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
429 end_ext_data = *p + len;
430
431 /* Get extension ID */
432 extn_oid.tag = **p;
433
434 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200435 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200436
437 extn_oid.p = *p;
438 *p += extn_oid.len;
439
440 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200441 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200442 POLARSSL_ERR_ASN1_OUT_OF_DATA );
443
444 /* Get optional critical */
445 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
446 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200447 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
449 /* Data should be octet string type */
450 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
451 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200452 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200453
454 end_ext_octet = *p + len;
455
456 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200457 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200458 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
459
460 /*
461 * Detect supported extensions
462 */
463 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
464
465 if( ret != 0 )
466 {
467 /* No parser found, skip extension */
468 *p = end_ext_octet;
469
470#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
471 if( is_critical )
472 {
473 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200474 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
476 }
477#endif
478 continue;
479 }
480
481 crt->ext_types |= ext_type;
482
483 switch( ext_type )
484 {
485 case EXT_BASIC_CONSTRAINTS:
486 /* Parse basic constraints */
487 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
488 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200489 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200490 break;
491
492 case EXT_KEY_USAGE:
493 /* Parse key usage */
494 if( ( ret = x509_get_key_usage( p, end_ext_octet,
495 &crt->key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200496 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 break;
498
499 case EXT_EXTENDED_KEY_USAGE:
500 /* Parse extended key usage */
501 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
502 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200503 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504 break;
505
506 case EXT_SUBJECT_ALT_NAME:
507 /* Parse subject alt name */
508 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
509 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200510 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 break;
512
513 case EXT_NS_CERT_TYPE:
514 /* Parse netscape certificate type */
515 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
516 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200517 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 break;
519
520 default:
521 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
522 }
523 }
524
525 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200526 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200527 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
528
529 return( 0 );
530}
531
532/*
533 * Parse and fill a single X.509 certificate in DER format
534 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200535static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200536 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537{
538 int ret;
539 size_t len;
540 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200541 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100542
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200543 memset( &sig_params1, 0, sizeof( x509_buf ) );
544 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545
546 /*
547 * Check for valid input
548 */
549 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200550 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200551
552 p = (unsigned char *) polarssl_malloc( len = buflen );
553
554 if( p == NULL )
555 return( POLARSSL_ERR_X509_MALLOC_FAILED );
556
557 memcpy( p, buf, buflen );
558
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559 crt->raw.p = p;
560 crt->raw.len = len;
561 end = p + len;
562
563 /*
564 * Certificate ::= SEQUENCE {
565 * tbsCertificate TBSCertificate,
566 * signatureAlgorithm AlgorithmIdentifier,
567 * signatureValue BIT STRING }
568 */
569 if( ( ret = asn1_get_tag( &p, end, &len,
570 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
571 {
572 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200573 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200574 }
575
576 if( len > (size_t) ( end - p ) )
577 {
578 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200579 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
581 }
582 crt_end = p + len;
583
584 /*
585 * TBSCertificate ::= SEQUENCE {
586 */
587 crt->tbs.p = p;
588
589 if( ( ret = asn1_get_tag( &p, end, &len,
590 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
591 {
592 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200593 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200594 }
595
596 end = p + len;
597 crt->tbs.len = end - crt->tbs.p;
598
599 /*
600 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
601 *
602 * CertificateSerialNumber ::= INTEGER
603 *
604 * signature AlgorithmIdentifier
605 */
606 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
607 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100608 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200609 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610 {
611 x509_crt_free( crt );
612 return( ret );
613 }
614
615 crt->version++;
616
617 if( crt->version > 3 )
618 {
619 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200620 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 }
622
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200623 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200624 &crt->sig_md, &crt->sig_pk,
625 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200626 {
627 x509_crt_free( crt );
628 return( ret );
629 }
630
631 /*
632 * issuer Name
633 */
634 crt->issuer_raw.p = p;
635
636 if( ( ret = asn1_get_tag( &p, end, &len,
637 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
638 {
639 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200640 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 }
642
643 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
644 {
645 x509_crt_free( crt );
646 return( ret );
647 }
648
649 crt->issuer_raw.len = p - crt->issuer_raw.p;
650
651 /*
652 * Validity ::= SEQUENCE {
653 * notBefore Time,
654 * notAfter Time }
655 *
656 */
657 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
658 &crt->valid_to ) ) != 0 )
659 {
660 x509_crt_free( crt );
661 return( ret );
662 }
663
664 /*
665 * subject Name
666 */
667 crt->subject_raw.p = p;
668
669 if( ( ret = asn1_get_tag( &p, end, &len,
670 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
671 {
672 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200673 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200674 }
675
676 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
677 {
678 x509_crt_free( crt );
679 return( ret );
680 }
681
682 crt->subject_raw.len = p - crt->subject_raw.p;
683
684 /*
685 * SubjectPublicKeyInfo
686 */
Paul Bakkerda771152013-09-16 22:45:03 +0200687 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200688 {
689 x509_crt_free( crt );
690 return( ret );
691 }
692
693 /*
694 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
695 * -- If present, version shall be v2 or v3
696 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
697 * -- If present, version shall be v2 or v3
698 * extensions [3] EXPLICIT Extensions OPTIONAL
699 * -- If present, version shall be v3
700 */
701 if( crt->version == 2 || crt->version == 3 )
702 {
703 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
704 if( ret != 0 )
705 {
706 x509_crt_free( crt );
707 return( ret );
708 }
709 }
710
711 if( crt->version == 2 || crt->version == 3 )
712 {
713 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
714 if( ret != 0 )
715 {
716 x509_crt_free( crt );
717 return( ret );
718 }
719 }
720
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200721#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200722 if( crt->version == 3 )
723 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200724#endif
Paul Bakker66d5d072014-06-17 16:39:18 +0200725 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 if( ret != 0 )
727 {
728 x509_crt_free( crt );
729 return( ret );
730 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200731#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200733#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734
735 if( p != end )
736 {
737 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200738 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
740 }
741
742 end = crt_end;
743
744 /*
745 * }
746 * -- end of TBSCertificate
747 *
748 * signatureAlgorithm AlgorithmIdentifier,
749 * signatureValue BIT STRING
750 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200751 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 {
753 x509_crt_free( crt );
754 return( ret );
755 }
756
757 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200758 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
759 sig_params1.len != sig_params2.len ||
Paul Bakker66d5d072014-06-17 16:39:18 +0200760 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761 {
762 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200763 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 }
765
766 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
767 {
768 x509_crt_free( crt );
769 return( ret );
770 }
771
772 if( p != end )
773 {
774 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200775 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200776 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
777 }
778
779 return( 0 );
780}
781
782/*
783 * Parse one X.509 certificate in DER format from a buffer and add them to a
784 * chained list
785 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200786int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200787 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200788{
789 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200790 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791
792 /*
793 * Check for valid input
794 */
795 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200796 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797
798 while( crt->version != 0 && crt->next != NULL )
799 {
800 prev = crt;
801 crt = crt->next;
802 }
803
804 /*
805 * Add new certificate on the end of the chain if needed.
806 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200807 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200808 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200809 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810
811 if( crt->next == NULL )
812 return( POLARSSL_ERR_X509_MALLOC_FAILED );
813
814 prev = crt;
815 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200816 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817 }
818
Paul Bakkerddf26b42013-09-18 13:46:23 +0200819 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 {
821 if( prev )
822 prev->next = NULL;
823
824 if( crt != chain )
825 polarssl_free( crt );
826
827 return( ret );
828 }
829
830 return( 0 );
831}
832
833/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200834 * Parse one or more PEM certificates from a buffer and add them to the chained
835 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200836 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200837int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200838{
839 int success = 0, first_error = 0, total_failed = 0;
840 int buf_format = X509_FORMAT_DER;
841
842 /*
843 * Check for valid input
844 */
845 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200846 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200847
848 /*
849 * Determine buffer content. Buffer contains either one DER certificate or
850 * one or more PEM certificates.
851 */
852#if defined(POLARSSL_PEM_PARSE_C)
853 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
854 buf_format = X509_FORMAT_PEM;
855#endif
856
857 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200858 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200859
860#if defined(POLARSSL_PEM_PARSE_C)
861 if( buf_format == X509_FORMAT_PEM )
862 {
863 int ret;
864 pem_context pem;
865
866 while( buflen > 0 )
867 {
868 size_t use_len;
869 pem_init( &pem );
870
871 ret = pem_read_buffer( &pem,
872 "-----BEGIN CERTIFICATE-----",
873 "-----END CERTIFICATE-----",
874 buf, NULL, 0, &use_len );
875
876 if( ret == 0 )
877 {
878 /*
879 * Was PEM encoded
880 */
881 buflen -= use_len;
882 buf += use_len;
883 }
884 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
885 {
886 return( ret );
887 }
888 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
889 {
890 pem_free( &pem );
891
892 /*
893 * PEM header and footer were found
894 */
895 buflen -= use_len;
896 buf += use_len;
897
898 if( first_error == 0 )
899 first_error = ret;
900
Paul Bakker5a5fa922014-09-26 14:53:04 +0200901 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200902 continue;
903 }
904 else
905 break;
906
Paul Bakkerddf26b42013-09-18 13:46:23 +0200907 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200908
909 pem_free( &pem );
910
911 if( ret != 0 )
912 {
913 /*
914 * Quit parsing on a memory error
915 */
916 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
917 return( ret );
918
919 if( first_error == 0 )
920 first_error = ret;
921
922 total_failed++;
923 continue;
924 }
925
926 success = 1;
927 }
928 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200929#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200930
931 if( success )
932 return( total_failed );
933 else if( first_error )
934 return( first_error );
935 else
936 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
937}
938
939#if defined(POLARSSL_FS_IO)
940/*
941 * Load one or more certificates and add them to the chained list
942 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200943int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200944{
945 int ret;
946 size_t n;
947 unsigned char *buf;
948
Paul Bakker66d5d072014-06-17 16:39:18 +0200949 if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200950 return( ret );
951
Paul Bakkerddf26b42013-09-18 13:46:23 +0200952 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953
Paul Bakker34617722014-06-13 17:20:13 +0200954 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955 polarssl_free( buf );
956
957 return( ret );
958}
959
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100960#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100961static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
962#endif
963
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200964int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965{
966 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100967#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 int w_ret;
969 WCHAR szDir[MAX_PATH];
970 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200971 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200972 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200973
Paul Bakker9af723c2014-05-01 13:03:14 +0200974 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975 HANDLE hFind;
976
977 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200978 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200979
Paul Bakker9af723c2014-05-01 13:03:14 +0200980 memset( szDir, 0, sizeof(szDir) );
981 memset( filename, 0, MAX_PATH );
982 memcpy( filename, path, len );
983 filename[len++] = '\\';
984 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 filename[len++] = '*';
986
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200987 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
988 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200989
990 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200991 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
993
994 len = MAX_PATH - len;
995 do
996 {
Paul Bakker9af723c2014-05-01 13:03:14 +0200997 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
999 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1000 continue;
1001
Paul Bakker9af723c2014-05-01 13:03:14 +02001002 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001003 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001004 p, len - 1,
1005 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001006
Paul Bakkerddf26b42013-09-18 13:46:23 +02001007 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008 if( w_ret < 0 )
1009 ret++;
1010 else
1011 ret += w_ret;
1012 }
1013 while( FindNextFileW( hFind, &file_data ) != 0 );
1014
Paul Bakker66d5d072014-06-17 16:39:18 +02001015 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1017
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001019#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001020 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001021 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001022 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 char entry_name[255];
1024 DIR *dir = opendir( path );
1025
Paul Bakker66d5d072014-06-17 16:39:18 +02001026 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1028
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001029#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001030 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1031 return( ret );
1032#endif
1033
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001034 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001036 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001038 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 {
1040 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001041 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1042 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 }
1044
1045 if( !S_ISREG( sb.st_mode ) )
1046 continue;
1047
1048 // Ignore parse errors
1049 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001050 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 if( t_ret < 0 )
1052 ret++;
1053 else
1054 ret += t_ret;
1055 }
1056 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001057
1058cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001059#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001060 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1061 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1062#endif
1063
Paul Bakkerbe089b02013-10-14 15:51:50 +02001064#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065
1066 return( ret );
1067}
1068#endif /* POLARSSL_FS_IO */
1069
Paul Bakker6edcd412013-10-29 15:22:54 +01001070#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1071 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072#include <stdarg.h>
1073
1074#if !defined vsnprintf
1075#define vsnprintf _vsnprintf
1076#endif // vsnprintf
1077
1078/*
1079 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1080 * Result value is not size of buffer needed, but -1 if no fit is possible.
1081 *
1082 * This fuction tries to 'fix' this by at least suggesting enlarging the
1083 * size by 20.
1084 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001085static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001086{
1087 va_list ap;
1088 int res = -1;
1089
1090 va_start( ap, format );
1091
1092 res = vsnprintf( str, size, format, ap );
1093
1094 va_end( ap );
1095
1096 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001097 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098 return( (int) size + 20 );
1099
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001100 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001101}
1102
1103#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001104#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105
1106#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1107
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001108#define SAFE_SNPRINTF() \
1109{ \
1110 if( ret == -1 ) \
1111 return( -1 ); \
1112 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001113 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001114 p[n - 1] = '\0'; \
1115 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1116 } \
1117 \
1118 n -= (unsigned int) ret; \
1119 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001120}
1121
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001122static int x509_info_subject_alt_name( char **buf, size_t *size,
1123 const x509_sequence *subject_alt_name )
1124{
1125 size_t i;
1126 size_t n = *size;
1127 char *p = *buf;
1128 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001129 const char *sep = "";
1130 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001131
1132 while( cur != NULL )
1133 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001134 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001135 {
1136 *p = '\0';
1137 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1138 }
1139
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001140 n -= cur->buf.len + sep_len;
1141 for( i = 0; i < sep_len; i++ )
1142 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001143 for( i = 0; i < cur->buf.len; i++ )
1144 *p++ = cur->buf.p[i];
1145
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001146 sep = ", ";
1147 sep_len = 2;
1148
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001149 cur = cur->next;
1150 }
1151
1152 *p = '\0';
1153
1154 *size = n;
1155 *buf = p;
1156
1157 return( 0 );
1158}
1159
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001160#define PRINT_ITEM(i) \
1161 { \
1162 ret = snprintf( p, n, "%s" i, sep ); \
1163 SAFE_SNPRINTF(); \
1164 sep = ", "; \
1165 }
1166
1167#define CERT_TYPE(type,name) \
1168 if( ns_cert_type & type ) \
1169 PRINT_ITEM( name );
1170
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001171static int x509_info_cert_type( char **buf, size_t *size,
1172 unsigned char ns_cert_type )
1173{
1174 int ret;
1175 size_t n = *size;
1176 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001177 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001178
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001179 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1180 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1181 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1182 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1183 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1184 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1185 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1186 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001187
1188 *size = n;
1189 *buf = p;
1190
1191 return( 0 );
1192}
1193
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001194#define KEY_USAGE(code,name) \
1195 if( key_usage & code ) \
1196 PRINT_ITEM( name );
1197
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001198static int x509_info_key_usage( char **buf, size_t *size,
1199 unsigned char key_usage )
1200{
1201 int ret;
1202 size_t n = *size;
1203 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001204 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001205
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001206 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1207 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1208 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1209 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1210 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1211 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1212 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001213
1214 *size = n;
1215 *buf = p;
1216
1217 return( 0 );
1218}
1219
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001220static int x509_info_ext_key_usage( char **buf, size_t *size,
1221 const x509_sequence *extended_key_usage )
1222{
1223 int ret;
1224 const char *desc;
1225 size_t n = *size;
1226 char *p = *buf;
1227 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001228 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001229
1230 while( cur != NULL )
1231 {
1232 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1233 desc = "???";
1234
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001235 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001236 SAFE_SNPRINTF();
1237
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001238 sep = ", ";
1239
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001240 cur = cur->next;
1241 }
1242
1243 *size = n;
1244 *buf = p;
1245
1246 return( 0 );
1247}
1248
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249/*
1250 * Return an informational string about the certificate.
1251 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001252#define BEFORE_COLON 18
1253#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001254int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001255 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256{
1257 int ret;
1258 size_t n;
1259 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260 char key_size_str[BEFORE_COLON];
1261
1262 p = buf;
1263 n = size;
1264
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001265 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001266 prefix, crt->version );
1267 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001268 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001269 prefix );
1270 SAFE_SNPRINTF();
1271
Paul Bakker66d5d072014-06-17 16:39:18 +02001272 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 SAFE_SNPRINTF();
1274
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001275 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001277 ret = x509_dn_gets( p, n, &crt->issuer );
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%ssubject 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->subject );
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%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1287 crt->valid_from.year, crt->valid_from.mon,
1288 crt->valid_from.day, crt->valid_from.hour,
1289 crt->valid_from.min, crt->valid_from.sec );
1290 SAFE_SNPRINTF();
1291
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001292 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1294 crt->valid_to.year, crt->valid_to.mon,
1295 crt->valid_to.day, crt->valid_to.hour,
1296 crt->valid_to.min, crt->valid_to.sec );
1297 SAFE_SNPRINTF();
1298
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001299 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 SAFE_SNPRINTF();
1301
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001302 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001303 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 SAFE_SNPRINTF();
1305
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001306 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1308 pk_get_name( &crt->pk ) ) ) != 0 )
1309 {
1310 return( ret );
1311 }
1312
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001313 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 (int) pk_get_size( &crt->pk ) );
1315 SAFE_SNPRINTF();
1316
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001317 /*
1318 * Optional extensions
1319 */
1320
1321 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1322 {
1323 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1324 crt->ca_istrue ? "true" : "false" );
1325 SAFE_SNPRINTF();
1326
1327 if( crt->max_pathlen > 0 )
1328 {
1329 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1330 SAFE_SNPRINTF();
1331 }
1332 }
1333
1334 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1335 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001336 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001337 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001338
1339 if( ( ret = x509_info_subject_alt_name( &p, &n,
1340 &crt->subject_alt_names ) ) != 0 )
1341 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001342 }
1343
1344 if( crt->ext_types & EXT_NS_CERT_TYPE )
1345 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001346 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001347 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001348
1349 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1350 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001351 }
1352
1353 if( crt->ext_types & EXT_KEY_USAGE )
1354 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001355 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001356 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001357
1358 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1359 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001360 }
1361
1362 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1363 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001364 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001365 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001366
1367 if( ( ret = x509_info_ext_key_usage( &p, &n,
1368 &crt->ext_key_usage ) ) != 0 )
1369 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001370 }
1371
1372 ret = snprintf( p, n, "\n" );
1373 SAFE_SNPRINTF();
1374
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001375 return( (int) ( size - n ) );
1376}
1377
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001378#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1379int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1380{
1381 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1382 ( crt->key_usage & usage ) != usage )
1383 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1384
1385 return( 0 );
1386}
1387#endif
1388
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001389#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1390int x509_crt_check_extended_key_usage( const x509_crt *crt,
1391 const char *usage_oid,
1392 size_t usage_len )
1393{
1394 const x509_sequence *cur;
1395
1396 /* Extension is not mandatory, absent means no restriction */
1397 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1398 return( 0 );
1399
1400 /*
1401 * Look for the requested usage (or wildcard ANY) in our list
1402 */
1403 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1404 {
1405 const x509_buf *cur_oid = &cur->buf;
1406
1407 if( cur_oid->len == usage_len &&
1408 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1409 {
1410 return( 0 );
1411 }
1412
1413 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1414 return( 0 );
1415 }
1416
1417 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1418}
Paul Bakker9af723c2014-05-01 13:03:14 +02001419#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001420
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001421#if defined(POLARSSL_X509_CRL_PARSE_C)
1422/*
1423 * Return 1 if the certificate is revoked, or 0 otherwise.
1424 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001425int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001426{
1427 const x509_crl_entry *cur = &crl->entry;
1428
1429 while( cur != NULL && cur->serial.len != 0 )
1430 {
1431 if( crt->serial.len == cur->serial.len &&
1432 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1433 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001434 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001435 return( 1 );
1436 }
1437
1438 cur = cur->next;
1439 }
1440
1441 return( 0 );
1442}
1443
1444/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001445 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001447static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001448 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449{
1450 int flags = 0;
1451 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1452 const md_info_t *md_info;
1453
1454 if( ca == NULL )
1455 return( flags );
1456
1457 /*
1458 * TODO: What happens if no CRL is present?
1459 * Suggestion: Revocation state should be unknown if no CRL is present.
1460 * For backwards compatibility this is not yet implemented.
1461 */
1462
1463 while( crl_list != NULL )
1464 {
1465 if( crl_list->version == 0 ||
1466 crl_list->issuer_raw.len != ca->subject_raw.len ||
1467 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1468 crl_list->issuer_raw.len ) != 0 )
1469 {
1470 crl_list = crl_list->next;
1471 continue;
1472 }
1473
1474 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001475 * Check if the CA is configured to sign CRLs
1476 */
1477#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1478 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1479 {
1480 flags |= BADCRL_NOT_TRUSTED;
1481 break;
1482 }
1483#endif
1484
1485 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001486 * Check if CRL is correctly signed by the trusted CA
1487 */
1488 md_info = md_info_from_type( crl_list->sig_md );
1489 if( md_info == NULL )
1490 {
1491 /*
1492 * Cannot check 'unknown' hash
1493 */
1494 flags |= BADCRL_NOT_TRUSTED;
1495 break;
1496 }
1497
1498 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1499
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001500 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1501 crl_list->sig_md, hash, md_info->size,
1502 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001503 {
1504 flags |= BADCRL_NOT_TRUSTED;
1505 break;
1506 }
1507
1508 /*
1509 * Check for validity of CRL (Do not drop out)
1510 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001511 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001512 flags |= BADCRL_EXPIRED;
1513
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001514 if( x509_time_future( &crl_list->this_update ) )
1515 flags |= BADCRL_FUTURE;
1516
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001517 /*
1518 * Check if certificate is revoked
1519 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001520 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521 {
1522 flags |= BADCERT_REVOKED;
1523 break;
1524 }
1525
1526 crl_list = crl_list->next;
1527 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001528 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001529}
1530#endif /* POLARSSL_X509_CRL_PARSE_C */
1531
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001532/*
1533 * Like memcmp, but case-insensitive and always returns -1 if different
1534 */
1535static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536{
1537 size_t i;
1538 unsigned char diff;
1539 const unsigned char *n1 = s1, *n2 = s2;
1540
1541 for( i = 0; i < len; i++ )
1542 {
1543 diff = n1[i] ^ n2[i];
1544
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001545 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001546 continue;
1547
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001548 if( diff == 32 &&
1549 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1550 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1551 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001552 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001553 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001554
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001555 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556 }
1557
1558 return( 0 );
1559}
1560
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001561/*
1562 * Return 1 if match, 0 if not
1563 * TODO: inverted return value!
1564 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001565static int x509_wildcard_verify( const char *cn, x509_buf *name )
1566{
1567 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001568 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569
1570 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1571 return( 0 );
1572
Paul Bakker14b16c62014-05-28 11:33:54 +02001573 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001574 {
1575 if( cn[i] == '.' )
1576 {
1577 cn_idx = i;
1578 break;
1579 }
1580 }
1581
1582 if( cn_idx == 0 )
1583 return( 0 );
1584
Paul Bakker14b16c62014-05-28 11:33:54 +02001585 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001586 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001587 {
1588 return( 1 );
1589 }
1590
1591 return( 0 );
1592}
1593
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001594/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001595 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1596 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001597 *
1598 * top means parent is a locally-trusted certificate
1599 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001600 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001601static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001602 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001603 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001604{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001605 int need_ca_bit;
1606
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001607 /* Parent must be the issuer */
1608 if( child->issuer_raw.len != parent->subject_raw.len ||
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001609 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1610 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001611 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001612 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001613 }
1614
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001615 /* Parent must have the basicConstraints CA bit set as a general rule */
1616 need_ca_bit = 1;
1617
1618 /* Exception: v1/v2 certificates that are locally trusted. */
1619 if( top && parent->version < 3 )
1620 need_ca_bit = 0;
1621
1622 /* Exception: self-signed end-entity certs that are locally trusted. */
1623 if( top && bottom &&
1624 child->raw.len == parent->raw.len &&
1625 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1626 {
1627 need_ca_bit = 0;
1628 }
1629
1630 if( need_ca_bit && ! parent->ca_istrue )
1631 return( -1 );
1632
1633#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1634 if( need_ca_bit &&
1635 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001636 {
1637 return( -1 );
1638 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001639#endif
1640
1641 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001642}
1643
Paul Bakkerddf26b42013-09-18 13:46:23 +02001644static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001645 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001647 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001648 void *p_vrfy )
1649{
1650 int ret;
1651 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1652 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1653 const md_info_t *md_info;
1654
Paul Bakker86d0c192013-09-18 11:11:02 +02001655 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656 *flags |= BADCERT_EXPIRED;
1657
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001658 if( x509_time_future( &child->valid_from ) )
1659 *flags |= BADCERT_FUTURE;
1660
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 /*
1662 * Child is the top of the chain. Check against the trust_ca list.
1663 */
1664 *flags |= BADCERT_NOT_TRUSTED;
1665
1666 md_info = md_info_from_type( child->sig_md );
1667 if( md_info == NULL )
1668 {
1669 /*
1670 * Cannot check 'unknown', no need to try any CA
1671 */
1672 trust_ca = NULL;
1673 }
1674 else
1675 md( md_info, child->tbs.p, child->tbs.len, hash );
1676
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001677 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001678 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001679 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001680 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001681
1682 /*
1683 * Reduce path_len to check against if top of the chain is
1684 * the same as the trusted CA
1685 */
1686 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1687 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1688 child->issuer_raw.len ) == 0 )
1689 {
1690 check_path_cnt--;
1691 }
1692
1693 if( trust_ca->max_pathlen > 0 &&
1694 trust_ca->max_pathlen < check_path_cnt )
1695 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696 continue;
1697 }
1698
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001699 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1700 child->sig_md, hash, md_info->size,
1701 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001702 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001703 continue;
1704 }
1705
1706 /*
1707 * Top of chain is signed by a trusted CA
1708 */
1709 *flags &= ~BADCERT_NOT_TRUSTED;
1710 break;
1711 }
1712
1713 /*
1714 * If top of chain is not the same as the trusted CA send a verify request
1715 * to the callback for any issues with validity and CRL presence for the
1716 * trusted CA certificate.
1717 */
1718 if( trust_ca != NULL &&
1719 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1720 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1721 child->issuer_raw.len ) != 0 ) )
1722 {
1723#if defined(POLARSSL_X509_CRL_PARSE_C)
1724 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001725 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001726#else
1727 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728#endif
1729
Paul Bakker86d0c192013-09-18 11:11:02 +02001730 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001731 ca_flags |= BADCERT_EXPIRED;
1732
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001733 if( x509_time_future( &trust_ca->valid_from ) )
1734 ca_flags |= BADCERT_FUTURE;
1735
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 if( NULL != f_vrfy )
1737 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001738 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1739 &ca_flags ) ) != 0 )
1740 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001741 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001742 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743 }
1744 }
1745
1746 /* Call callback on top cert */
1747 if( NULL != f_vrfy )
1748 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001749 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750 return( ret );
1751 }
1752
1753 *flags |= ca_flags;
1754
1755 return( 0 );
1756}
1757
Paul Bakkerddf26b42013-09-18 13:46:23 +02001758static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001759 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001760 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001761 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001762 void *p_vrfy )
1763{
1764 int ret;
1765 int parent_flags = 0;
1766 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001767 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768 const md_info_t *md_info;
1769
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001770 if( x509_time_expired( &child->valid_to ) )
1771 *flags |= BADCERT_EXPIRED;
1772
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001773 if( x509_time_future( &child->valid_from ) )
1774 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001775
1776 md_info = md_info_from_type( child->sig_md );
1777 if( md_info == NULL )
1778 {
1779 /*
1780 * Cannot check 'unknown' hash
1781 */
1782 *flags |= BADCERT_NOT_TRUSTED;
1783 }
1784 else
1785 {
1786 md( md_info, child->tbs.p, child->tbs.len, hash );
1787
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001788 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1789 child->sig_md, hash, md_info->size,
1790 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001791 {
1792 *flags |= BADCERT_NOT_TRUSTED;
1793 }
1794 }
1795
1796#if defined(POLARSSL_X509_CRL_PARSE_C)
1797 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001798 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799#endif
1800
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001801 /* Look for a grandparent upwards the chain */
1802 for( grandparent = parent->next;
1803 grandparent != NULL;
1804 grandparent = grandparent->next )
1805 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001806 if( x509_crt_check_parent( parent, grandparent,
1807 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001808 break;
1809 }
1810
1811 /* Is our parent part of the chain or at the top? */
1812 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001813 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001814 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1815 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816 if( ret != 0 )
1817 return( ret );
1818 }
1819 else
1820 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001821 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1822 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001823 if( ret != 0 )
1824 return( ret );
1825 }
1826
1827 /* child is verified to be a child of the parent, call verify callback */
1828 if( NULL != f_vrfy )
1829 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1830 return( ret );
1831
1832 *flags |= parent_flags;
1833
1834 return( 0 );
1835}
1836
1837/*
1838 * Verify the certificate validity
1839 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001840int x509_crt_verify( x509_crt *crt,
1841 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001842 x509_crl *ca_crl,
1843 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001844 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001845 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846{
1847 size_t cn_len;
1848 int ret;
1849 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001850 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001851 x509_name *name;
1852 x509_sequence *cur = NULL;
1853
1854 *flags = 0;
1855
1856 if( cn != NULL )
1857 {
1858 name = &crt->subject;
1859 cn_len = strlen( cn );
1860
1861 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1862 {
1863 cur = &crt->subject_alt_names;
1864
1865 while( cur != NULL )
1866 {
1867 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001868 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001869 break;
1870
1871 if( cur->buf.len > 2 &&
1872 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1873 x509_wildcard_verify( cn, &cur->buf ) )
1874 break;
1875
1876 cur = cur->next;
1877 }
1878
1879 if( cur == NULL )
1880 *flags |= BADCERT_CN_MISMATCH;
1881 }
1882 else
1883 {
1884 while( name != NULL )
1885 {
1886 if( OID_CMP( OID_AT_CN, &name->oid ) )
1887 {
1888 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001889 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001890 break;
1891
1892 if( name->val.len > 2 &&
1893 memcmp( name->val.p, "*.", 2 ) == 0 &&
1894 x509_wildcard_verify( cn, &name->val ) )
1895 break;
1896 }
1897
1898 name = name->next;
1899 }
1900
1901 if( name == NULL )
1902 *flags |= BADCERT_CN_MISMATCH;
1903 }
1904 }
1905
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001906 /* Look for a parent upwards the chain */
1907 for( parent = crt->next; parent != NULL; parent = parent->next )
1908 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001909 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001910 break;
1911 }
1912
1913 /* Are we part of the chain or at the top? */
1914 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001915 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001916 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1917 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918 if( ret != 0 )
1919 return( ret );
1920 }
1921 else
1922 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001923 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1924 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925 if( ret != 0 )
1926 return( ret );
1927 }
1928
1929 if( *flags != 0 )
1930 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1931
1932 return( 0 );
1933}
1934
1935/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001936 * Initialize a certificate chain
1937 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001938void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001939{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001940 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001941}
1942
1943/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944 * Unallocate all certificate data
1945 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001946void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001947{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001948 x509_crt *cert_cur = crt;
1949 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001950 x509_name *name_cur;
1951 x509_name *name_prv;
1952 x509_sequence *seq_cur;
1953 x509_sequence *seq_prv;
1954
1955 if( crt == NULL )
1956 return;
1957
1958 do
1959 {
1960 pk_free( &cert_cur->pk );
1961
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02001962#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001963 polarssl_free( cert_cur->sig_opts );
1964#endif
1965
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001966 name_cur = cert_cur->issuer.next;
1967 while( name_cur != NULL )
1968 {
1969 name_prv = name_cur;
1970 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001971 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001972 polarssl_free( name_prv );
1973 }
1974
1975 name_cur = cert_cur->subject.next;
1976 while( name_cur != NULL )
1977 {
1978 name_prv = name_cur;
1979 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001980 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001981 polarssl_free( name_prv );
1982 }
1983
1984 seq_cur = cert_cur->ext_key_usage.next;
1985 while( seq_cur != NULL )
1986 {
1987 seq_prv = seq_cur;
1988 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001989 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990 polarssl_free( seq_prv );
1991 }
1992
1993 seq_cur = cert_cur->subject_alt_names.next;
1994 while( seq_cur != NULL )
1995 {
1996 seq_prv = seq_cur;
1997 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001998 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001999 polarssl_free( seq_prv );
2000 }
2001
2002 if( cert_cur->raw.p != NULL )
2003 {
Paul Bakker34617722014-06-13 17:20:13 +02002004 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002005 polarssl_free( cert_cur->raw.p );
2006 }
2007
2008 cert_cur = cert_cur->next;
2009 }
2010 while( cert_cur != NULL );
2011
2012 cert_cur = crt;
2013 do
2014 {
2015 cert_prv = cert_cur;
2016 cert_cur = cert_cur->next;
2017
Paul Bakker34617722014-06-13 17:20:13 +02002018 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019 if( cert_prv != crt )
2020 polarssl_free( cert_prv );
2021 }
2022 while( cert_cur != NULL );
2023}
2024
Paul Bakker9af723c2014-05-01 13:03:14 +02002025#endif /* POLARSSL_X509_CRT_PARSE_C */