blob: 03cdda807909f677ece242e98a60a0f086c9a8c6 [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
901 continue;
902 }
903 else
904 break;
905
Paul Bakkerddf26b42013-09-18 13:46:23 +0200906 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200907
908 pem_free( &pem );
909
910 if( ret != 0 )
911 {
912 /*
913 * Quit parsing on a memory error
914 */
915 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
916 return( ret );
917
918 if( first_error == 0 )
919 first_error = ret;
920
921 total_failed++;
922 continue;
923 }
924
925 success = 1;
926 }
927 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200928#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200929
930 if( success )
931 return( total_failed );
932 else if( first_error )
933 return( first_error );
934 else
935 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
936}
937
938#if defined(POLARSSL_FS_IO)
939/*
940 * Load one or more certificates and add them to the chained list
941 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200942int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200943{
944 int ret;
945 size_t n;
946 unsigned char *buf;
947
Paul Bakker66d5d072014-06-17 16:39:18 +0200948 if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200949 return( ret );
950
Paul Bakkerddf26b42013-09-18 13:46:23 +0200951 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200952
Paul Bakker34617722014-06-13 17:20:13 +0200953 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200954 polarssl_free( buf );
955
956 return( ret );
957}
958
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100959#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100960static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
961#endif
962
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200963int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200964{
965 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100966#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967 int w_ret;
968 WCHAR szDir[MAX_PATH];
969 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200970 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200971 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972
Paul Bakker9af723c2014-05-01 13:03:14 +0200973 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974 HANDLE hFind;
975
976 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200977 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978
Paul Bakker9af723c2014-05-01 13:03:14 +0200979 memset( szDir, 0, sizeof(szDir) );
980 memset( filename, 0, MAX_PATH );
981 memcpy( filename, path, len );
982 filename[len++] = '\\';
983 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 filename[len++] = '*';
985
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200986 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
987 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
989 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200990 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
992
993 len = MAX_PATH - len;
994 do
995 {
Paul Bakker9af723c2014-05-01 13:03:14 +0200996 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997
998 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
999 continue;
1000
Paul Bakker9af723c2014-05-01 13:03:14 +02001001 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001002 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001003 p, len - 1,
1004 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005
Paul Bakkerddf26b42013-09-18 13:46:23 +02001006 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007 if( w_ret < 0 )
1008 ret++;
1009 else
1010 ret += w_ret;
1011 }
1012 while( FindNextFileW( hFind, &file_data ) != 0 );
1013
Paul Bakker66d5d072014-06-17 16:39:18 +02001014 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1016
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001018#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001019 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001021 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022 char entry_name[255];
1023 DIR *dir = opendir( path );
1024
Paul Bakker66d5d072014-06-17 16:39:18 +02001025 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001026 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1027
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001028#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001029 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1030 return( ret );
1031#endif
1032
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001033 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001035 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001037 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001038 {
1039 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001040 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1041 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 }
1043
1044 if( !S_ISREG( sb.st_mode ) )
1045 continue;
1046
1047 // Ignore parse errors
1048 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001049 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 if( t_ret < 0 )
1051 ret++;
1052 else
1053 ret += t_ret;
1054 }
1055 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001056
1057cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001058#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001059 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1060 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1061#endif
1062
Paul Bakkerbe089b02013-10-14 15:51:50 +02001063#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001064
1065 return( ret );
1066}
1067#endif /* POLARSSL_FS_IO */
1068
Paul Bakker6edcd412013-10-29 15:22:54 +01001069#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1070 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001071#include <stdarg.h>
1072
1073#if !defined vsnprintf
1074#define vsnprintf _vsnprintf
1075#endif // vsnprintf
1076
1077/*
1078 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1079 * Result value is not size of buffer needed, but -1 if no fit is possible.
1080 *
1081 * This fuction tries to 'fix' this by at least suggesting enlarging the
1082 * size by 20.
1083 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001084static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001085{
1086 va_list ap;
1087 int res = -1;
1088
1089 va_start( ap, format );
1090
1091 res = vsnprintf( str, size, format, ap );
1092
1093 va_end( ap );
1094
1095 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001096 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001097 return( (int) size + 20 );
1098
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001099 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001100}
1101
1102#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001103#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001104
1105#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1106
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001107#define SAFE_SNPRINTF() \
1108{ \
1109 if( ret == -1 ) \
1110 return( -1 ); \
1111 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001112 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001113 p[n - 1] = '\0'; \
1114 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1115 } \
1116 \
1117 n -= (unsigned int) ret; \
1118 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001119}
1120
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001121static int x509_info_subject_alt_name( char **buf, size_t *size,
1122 const x509_sequence *subject_alt_name )
1123{
1124 size_t i;
1125 size_t n = *size;
1126 char *p = *buf;
1127 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001128 const char *sep = "";
1129 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001130
1131 while( cur != NULL )
1132 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001133 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001134 {
1135 *p = '\0';
1136 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1137 }
1138
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001139 n -= cur->buf.len + sep_len;
1140 for( i = 0; i < sep_len; i++ )
1141 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001142 for( i = 0; i < cur->buf.len; i++ )
1143 *p++ = cur->buf.p[i];
1144
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001145 sep = ", ";
1146 sep_len = 2;
1147
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001148 cur = cur->next;
1149 }
1150
1151 *p = '\0';
1152
1153 *size = n;
1154 *buf = p;
1155
1156 return( 0 );
1157}
1158
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001159#define PRINT_ITEM(i) \
1160 { \
1161 ret = snprintf( p, n, "%s" i, sep ); \
1162 SAFE_SNPRINTF(); \
1163 sep = ", "; \
1164 }
1165
1166#define CERT_TYPE(type,name) \
1167 if( ns_cert_type & type ) \
1168 PRINT_ITEM( name );
1169
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001170static int x509_info_cert_type( char **buf, size_t *size,
1171 unsigned char ns_cert_type )
1172{
1173 int ret;
1174 size_t n = *size;
1175 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001176 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001177
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001178 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1179 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1180 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1181 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1182 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1183 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1184 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1185 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001186
1187 *size = n;
1188 *buf = p;
1189
1190 return( 0 );
1191}
1192
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001193#define KEY_USAGE(code,name) \
1194 if( key_usage & code ) \
1195 PRINT_ITEM( name );
1196
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001197static int x509_info_key_usage( char **buf, size_t *size,
1198 unsigned char key_usage )
1199{
1200 int ret;
1201 size_t n = *size;
1202 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001203 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001204
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001205 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1206 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1207 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1208 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1209 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1210 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1211 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001212
1213 *size = n;
1214 *buf = p;
1215
1216 return( 0 );
1217}
1218
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001219static int x509_info_ext_key_usage( char **buf, size_t *size,
1220 const x509_sequence *extended_key_usage )
1221{
1222 int ret;
1223 const char *desc;
1224 size_t n = *size;
1225 char *p = *buf;
1226 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001227 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001228
1229 while( cur != NULL )
1230 {
1231 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1232 desc = "???";
1233
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001234 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001235 SAFE_SNPRINTF();
1236
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001237 sep = ", ";
1238
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001239 cur = cur->next;
1240 }
1241
1242 *size = n;
1243 *buf = p;
1244
1245 return( 0 );
1246}
1247
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001248/*
1249 * Return an informational string about the certificate.
1250 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001251#define BEFORE_COLON 18
1252#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001253int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001254 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001255{
1256 int ret;
1257 size_t n;
1258 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001259 char key_size_str[BEFORE_COLON];
1260
1261 p = buf;
1262 n = size;
1263
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001264 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265 prefix, crt->version );
1266 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001267 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268 prefix );
1269 SAFE_SNPRINTF();
1270
Paul Bakker66d5d072014-06-17 16:39:18 +02001271 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 SAFE_SNPRINTF();
1273
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001274 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001276 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001277 SAFE_SNPRINTF();
1278
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001279 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001281 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 SAFE_SNPRINTF();
1283
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001284 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1286 crt->valid_from.year, crt->valid_from.mon,
1287 crt->valid_from.day, crt->valid_from.hour,
1288 crt->valid_from.min, crt->valid_from.sec );
1289 SAFE_SNPRINTF();
1290
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001291 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001292 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1293 crt->valid_to.year, crt->valid_to.mon,
1294 crt->valid_to.day, crt->valid_to.hour,
1295 crt->valid_to.min, crt->valid_to.sec );
1296 SAFE_SNPRINTF();
1297
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001298 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 SAFE_SNPRINTF();
1300
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001301 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001302 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 SAFE_SNPRINTF();
1304
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001305 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1307 pk_get_name( &crt->pk ) ) ) != 0 )
1308 {
1309 return( ret );
1310 }
1311
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001312 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 (int) pk_get_size( &crt->pk ) );
1314 SAFE_SNPRINTF();
1315
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001316 /*
1317 * Optional extensions
1318 */
1319
1320 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1321 {
1322 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1323 crt->ca_istrue ? "true" : "false" );
1324 SAFE_SNPRINTF();
1325
1326 if( crt->max_pathlen > 0 )
1327 {
1328 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1329 SAFE_SNPRINTF();
1330 }
1331 }
1332
1333 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1334 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001335 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001336 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001337
1338 if( ( ret = x509_info_subject_alt_name( &p, &n,
1339 &crt->subject_alt_names ) ) != 0 )
1340 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001341 }
1342
1343 if( crt->ext_types & EXT_NS_CERT_TYPE )
1344 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001345 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001346 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001347
1348 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1349 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001350 }
1351
1352 if( crt->ext_types & EXT_KEY_USAGE )
1353 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001354 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001355 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001356
1357 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1358 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001359 }
1360
1361 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1362 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001363 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001364 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001365
1366 if( ( ret = x509_info_ext_key_usage( &p, &n,
1367 &crt->ext_key_usage ) ) != 0 )
1368 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001369 }
1370
1371 ret = snprintf( p, n, "\n" );
1372 SAFE_SNPRINTF();
1373
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001374 return( (int) ( size - n ) );
1375}
1376
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001377#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1378int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1379{
1380 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1381 ( crt->key_usage & usage ) != usage )
1382 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1383
1384 return( 0 );
1385}
1386#endif
1387
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001388#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1389int x509_crt_check_extended_key_usage( const x509_crt *crt,
1390 const char *usage_oid,
1391 size_t usage_len )
1392{
1393 const x509_sequence *cur;
1394
1395 /* Extension is not mandatory, absent means no restriction */
1396 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1397 return( 0 );
1398
1399 /*
1400 * Look for the requested usage (or wildcard ANY) in our list
1401 */
1402 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1403 {
1404 const x509_buf *cur_oid = &cur->buf;
1405
1406 if( cur_oid->len == usage_len &&
1407 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1408 {
1409 return( 0 );
1410 }
1411
1412 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1413 return( 0 );
1414 }
1415
1416 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1417}
Paul Bakker9af723c2014-05-01 13:03:14 +02001418#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001419
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001420#if defined(POLARSSL_X509_CRL_PARSE_C)
1421/*
1422 * Return 1 if the certificate is revoked, or 0 otherwise.
1423 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001424int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425{
1426 const x509_crl_entry *cur = &crl->entry;
1427
1428 while( cur != NULL && cur->serial.len != 0 )
1429 {
1430 if( crt->serial.len == cur->serial.len &&
1431 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1432 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001433 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001434 return( 1 );
1435 }
1436
1437 cur = cur->next;
1438 }
1439
1440 return( 0 );
1441}
1442
1443/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001444 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001445 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001446static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001447 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001448{
1449 int flags = 0;
1450 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1451 const md_info_t *md_info;
1452
1453 if( ca == NULL )
1454 return( flags );
1455
1456 /*
1457 * TODO: What happens if no CRL is present?
1458 * Suggestion: Revocation state should be unknown if no CRL is present.
1459 * For backwards compatibility this is not yet implemented.
1460 */
1461
1462 while( crl_list != NULL )
1463 {
1464 if( crl_list->version == 0 ||
1465 crl_list->issuer_raw.len != ca->subject_raw.len ||
1466 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1467 crl_list->issuer_raw.len ) != 0 )
1468 {
1469 crl_list = crl_list->next;
1470 continue;
1471 }
1472
1473 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001474 * Check if the CA is configured to sign CRLs
1475 */
1476#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1477 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1478 {
1479 flags |= BADCRL_NOT_TRUSTED;
1480 break;
1481 }
1482#endif
1483
1484 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485 * Check if CRL is correctly signed by the trusted CA
1486 */
1487 md_info = md_info_from_type( crl_list->sig_md );
1488 if( md_info == NULL )
1489 {
1490 /*
1491 * Cannot check 'unknown' hash
1492 */
1493 flags |= BADCRL_NOT_TRUSTED;
1494 break;
1495 }
1496
1497 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1498
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001499 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1500 crl_list->sig_md, hash, md_info->size,
1501 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001502 {
1503 flags |= BADCRL_NOT_TRUSTED;
1504 break;
1505 }
1506
1507 /*
1508 * Check for validity of CRL (Do not drop out)
1509 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001510 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001511 flags |= BADCRL_EXPIRED;
1512
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001513 if( x509_time_future( &crl_list->this_update ) )
1514 flags |= BADCRL_FUTURE;
1515
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 /*
1517 * Check if certificate is revoked
1518 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001519 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001520 {
1521 flags |= BADCERT_REVOKED;
1522 break;
1523 }
1524
1525 crl_list = crl_list->next;
1526 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001527 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528}
1529#endif /* POLARSSL_X509_CRL_PARSE_C */
1530
1531// Equal == 0, inequal == 1
1532static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1533{
1534 size_t i;
1535 unsigned char diff;
1536 const unsigned char *n1 = s1, *n2 = s2;
1537
1538 for( i = 0; i < len; i++ )
1539 {
1540 diff = n1[i] ^ n2[i];
1541
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001542 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543 continue;
1544
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001545 if( diff == 32 &&
1546 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1547 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1548 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001550 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551
1552 return( 1 );
1553 }
1554
1555 return( 0 );
1556}
1557
1558static int x509_wildcard_verify( const char *cn, x509_buf *name )
1559{
1560 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001561 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001562
1563 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1564 return( 0 );
1565
Paul Bakker14b16c62014-05-28 11:33:54 +02001566 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001567 {
1568 if( cn[i] == '.' )
1569 {
1570 cn_idx = i;
1571 break;
1572 }
1573 }
1574
1575 if( cn_idx == 0 )
1576 return( 0 );
1577
Paul Bakker14b16c62014-05-28 11:33:54 +02001578 if( cn_len - cn_idx == name->len - 1 &&
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001579 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1580 {
1581 return( 1 );
1582 }
1583
1584 return( 0 );
1585}
1586
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001587/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001588 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1589 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001590 *
1591 * top means parent is a locally-trusted certificate
1592 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001593 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001594static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001595 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001596 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001597{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001598 int need_ca_bit;
1599
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001600 /* Parent must be the issuer */
1601 if( child->issuer_raw.len != parent->subject_raw.len ||
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001602 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1603 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001604 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001605 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001606 }
1607
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001608 /* Parent must have the basicConstraints CA bit set as a general rule */
1609 need_ca_bit = 1;
1610
1611 /* Exception: v1/v2 certificates that are locally trusted. */
1612 if( top && parent->version < 3 )
1613 need_ca_bit = 0;
1614
1615 /* Exception: self-signed end-entity certs that are locally trusted. */
1616 if( top && bottom &&
1617 child->raw.len == parent->raw.len &&
1618 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1619 {
1620 need_ca_bit = 0;
1621 }
1622
1623 if( need_ca_bit && ! parent->ca_istrue )
1624 return( -1 );
1625
1626#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1627 if( need_ca_bit &&
1628 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001629 {
1630 return( -1 );
1631 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001632#endif
1633
1634 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001635}
1636
Paul Bakkerddf26b42013-09-18 13:46:23 +02001637static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001638 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001639 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001640 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001641 void *p_vrfy )
1642{
1643 int ret;
1644 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1645 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1646 const md_info_t *md_info;
1647
Paul Bakker86d0c192013-09-18 11:11:02 +02001648 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001649 *flags |= BADCERT_EXPIRED;
1650
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001651 if( x509_time_future( &child->valid_from ) )
1652 *flags |= BADCERT_FUTURE;
1653
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001654 /*
1655 * Child is the top of the chain. Check against the trust_ca list.
1656 */
1657 *flags |= BADCERT_NOT_TRUSTED;
1658
1659 md_info = md_info_from_type( child->sig_md );
1660 if( md_info == NULL )
1661 {
1662 /*
1663 * Cannot check 'unknown', no need to try any CA
1664 */
1665 trust_ca = NULL;
1666 }
1667 else
1668 md( md_info, child->tbs.p, child->tbs.len, hash );
1669
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001670 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001672 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001673 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674
1675 /*
1676 * Reduce path_len to check against if top of the chain is
1677 * the same as the trusted CA
1678 */
1679 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1680 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1681 child->issuer_raw.len ) == 0 )
1682 {
1683 check_path_cnt--;
1684 }
1685
1686 if( trust_ca->max_pathlen > 0 &&
1687 trust_ca->max_pathlen < check_path_cnt )
1688 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 continue;
1690 }
1691
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001692 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1693 child->sig_md, hash, md_info->size,
1694 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001695 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696 continue;
1697 }
1698
1699 /*
1700 * Top of chain is signed by a trusted CA
1701 */
1702 *flags &= ~BADCERT_NOT_TRUSTED;
1703 break;
1704 }
1705
1706 /*
1707 * If top of chain is not the same as the trusted CA send a verify request
1708 * to the callback for any issues with validity and CRL presence for the
1709 * trusted CA certificate.
1710 */
1711 if( trust_ca != NULL &&
1712 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1713 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1714 child->issuer_raw.len ) != 0 ) )
1715 {
1716#if defined(POLARSSL_X509_CRL_PARSE_C)
1717 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001718 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001719#else
1720 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721#endif
1722
Paul Bakker86d0c192013-09-18 11:11:02 +02001723 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001724 ca_flags |= BADCERT_EXPIRED;
1725
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001726 if( x509_time_future( &trust_ca->valid_from ) )
1727 ca_flags |= BADCERT_FUTURE;
1728
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001729 if( NULL != f_vrfy )
1730 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001731 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1732 &ca_flags ) ) != 0 )
1733 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001734 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001735 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001736 }
1737 }
1738
1739 /* Call callback on top cert */
1740 if( NULL != f_vrfy )
1741 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001742 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001743 return( ret );
1744 }
1745
1746 *flags |= ca_flags;
1747
1748 return( 0 );
1749}
1750
Paul Bakkerddf26b42013-09-18 13:46:23 +02001751static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001752 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001753 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001754 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001755 void *p_vrfy )
1756{
1757 int ret;
1758 int parent_flags = 0;
1759 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001760 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001761 const md_info_t *md_info;
1762
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001763 if( x509_time_expired( &child->valid_to ) )
1764 *flags |= BADCERT_EXPIRED;
1765
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001766 if( x509_time_future( &child->valid_from ) )
1767 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001768
1769 md_info = md_info_from_type( child->sig_md );
1770 if( md_info == NULL )
1771 {
1772 /*
1773 * Cannot check 'unknown' hash
1774 */
1775 *flags |= BADCERT_NOT_TRUSTED;
1776 }
1777 else
1778 {
1779 md( md_info, child->tbs.p, child->tbs.len, hash );
1780
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001781 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1782 child->sig_md, hash, md_info->size,
1783 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001784 {
1785 *flags |= BADCERT_NOT_TRUSTED;
1786 }
1787 }
1788
1789#if defined(POLARSSL_X509_CRL_PARSE_C)
1790 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001791 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001792#endif
1793
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001794 /* Look for a grandparent upwards the chain */
1795 for( grandparent = parent->next;
1796 grandparent != NULL;
1797 grandparent = grandparent->next )
1798 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001799 if( x509_crt_check_parent( parent, grandparent,
1800 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001801 break;
1802 }
1803
1804 /* Is our parent part of the chain or at the top? */
1805 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001806 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001807 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1808 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809 if( ret != 0 )
1810 return( ret );
1811 }
1812 else
1813 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001814 ret = x509_crt_verify_top( parent, 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
1820 /* child is verified to be a child of the parent, call verify callback */
1821 if( NULL != f_vrfy )
1822 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1823 return( ret );
1824
1825 *flags |= parent_flags;
1826
1827 return( 0 );
1828}
1829
1830/*
1831 * Verify the certificate validity
1832 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001833int x509_crt_verify( x509_crt *crt,
1834 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001835 x509_crl *ca_crl,
1836 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001837 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001838 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001839{
1840 size_t cn_len;
1841 int ret;
1842 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001843 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001844 x509_name *name;
1845 x509_sequence *cur = NULL;
1846
1847 *flags = 0;
1848
1849 if( cn != NULL )
1850 {
1851 name = &crt->subject;
1852 cn_len = strlen( cn );
1853
1854 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1855 {
1856 cur = &crt->subject_alt_names;
1857
1858 while( cur != NULL )
1859 {
1860 if( cur->buf.len == cn_len &&
1861 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1862 break;
1863
1864 if( cur->buf.len > 2 &&
1865 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1866 x509_wildcard_verify( cn, &cur->buf ) )
1867 break;
1868
1869 cur = cur->next;
1870 }
1871
1872 if( cur == NULL )
1873 *flags |= BADCERT_CN_MISMATCH;
1874 }
1875 else
1876 {
1877 while( name != NULL )
1878 {
1879 if( OID_CMP( OID_AT_CN, &name->oid ) )
1880 {
1881 if( name->val.len == cn_len &&
1882 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1883 break;
1884
1885 if( name->val.len > 2 &&
1886 memcmp( name->val.p, "*.", 2 ) == 0 &&
1887 x509_wildcard_verify( cn, &name->val ) )
1888 break;
1889 }
1890
1891 name = name->next;
1892 }
1893
1894 if( name == NULL )
1895 *flags |= BADCERT_CN_MISMATCH;
1896 }
1897 }
1898
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001899 /* Look for a parent upwards the chain */
1900 for( parent = crt->next; parent != NULL; parent = parent->next )
1901 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001902 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001903 break;
1904 }
1905
1906 /* Are we part of the chain or at the top? */
1907 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001908 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001909 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1910 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001911 if( ret != 0 )
1912 return( ret );
1913 }
1914 else
1915 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001916 ret = x509_crt_verify_top( crt, 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
1922 if( *flags != 0 )
1923 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1924
1925 return( 0 );
1926}
1927
1928/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001929 * Initialize a certificate chain
1930 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001931void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001932{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001933 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001934}
1935
1936/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001937 * Unallocate all certificate data
1938 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001939void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001940{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001941 x509_crt *cert_cur = crt;
1942 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001943 x509_name *name_cur;
1944 x509_name *name_prv;
1945 x509_sequence *seq_cur;
1946 x509_sequence *seq_prv;
1947
1948 if( crt == NULL )
1949 return;
1950
1951 do
1952 {
1953 pk_free( &cert_cur->pk );
1954
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02001955#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001956 polarssl_free( cert_cur->sig_opts );
1957#endif
1958
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001959 name_cur = cert_cur->issuer.next;
1960 while( name_cur != NULL )
1961 {
1962 name_prv = name_cur;
1963 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001964 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001965 polarssl_free( name_prv );
1966 }
1967
1968 name_cur = cert_cur->subject.next;
1969 while( name_cur != NULL )
1970 {
1971 name_prv = name_cur;
1972 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001973 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001974 polarssl_free( name_prv );
1975 }
1976
1977 seq_cur = cert_cur->ext_key_usage.next;
1978 while( seq_cur != NULL )
1979 {
1980 seq_prv = seq_cur;
1981 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001982 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001983 polarssl_free( seq_prv );
1984 }
1985
1986 seq_cur = cert_cur->subject_alt_names.next;
1987 while( seq_cur != NULL )
1988 {
1989 seq_prv = seq_cur;
1990 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02001991 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001992 polarssl_free( seq_prv );
1993 }
1994
1995 if( cert_cur->raw.p != NULL )
1996 {
Paul Bakker34617722014-06-13 17:20:13 +02001997 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001998 polarssl_free( cert_cur->raw.p );
1999 }
2000
2001 cert_cur = cert_cur->next;
2002 }
2003 while( cert_cur != NULL );
2004
2005 cert_cur = crt;
2006 do
2007 {
2008 cert_prv = cert_cur;
2009 cert_cur = cert_cur->next;
2010
Paul Bakker34617722014-06-13 17:20:13 +02002011 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002012 if( cert_prv != crt )
2013 polarssl_free( cert_prv );
2014 }
2015 while( cert_cur != NULL );
2016}
2017
Paul Bakker9af723c2014-05-01 13:03:14 +02002018#endif /* POLARSSL_X509_CRT_PARSE_C */