blob: 3327e68862d65d3e4a793c361141445b419827bc [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +02002 * X.509 certificate parsing and verification
Paul Bakker7c6b2c32013-09-16 13:49:26 +02003 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 *
Paul Bakker7c6b2c32013-09-16 13:49:26 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22/*
23 * The ITU-T X.509 standard defines a certificate format for PKI.
24 *
Manuel Pégourié-Gonnard1c082f32014-06-12 22:34:55 +020025 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
26 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
27 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020028 *
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
30 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
31 */
32
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020033#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020034#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020035#else
36#include POLARSSL_CONFIG_FILE
37#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038
39#if defined(POLARSSL_X509_CRT_PARSE_C)
40
41#include "polarssl/x509_crt.h"
42#include "polarssl/oid.h"
43#if defined(POLARSSL_PEM_PARSE_C)
44#include "polarssl/pem.h"
45#endif
46
Paul Bakker7dc4c442014-02-01 22:50:26 +010047#if defined(POLARSSL_PLATFORM_C)
48#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049#else
50#define polarssl_malloc malloc
51#define polarssl_free free
52#endif
53
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010054#if defined(POLARSSL_THREADING_C)
55#include "polarssl/threading.h"
56#endif
57
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058#include <string.h>
59#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020061#include <windows.h>
62#else
63#include <time.h>
64#endif
65
Paul Bakkerfa6a6202013-10-28 18:48:30 +010066#include <stdio.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010067
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068#if defined(POLARSSL_FS_IO)
Paul Bakker5ff3f912014-04-04 15:08:20 +020069#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070#include <sys/types.h>
71#include <sys/stat.h>
72#include <dirent.h>
73#endif
74#endif
75
Paul Bakker34617722014-06-13 17:20:13 +020076/* Implementation that should never be optimized out by the compiler */
77static void polarssl_zeroize( void *v, size_t n ) {
78 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
79}
80
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081/*
82 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
83 */
84static int x509_get_version( unsigned char **p,
85 const unsigned char *end,
86 int *ver )
87{
88 int ret;
89 size_t len;
90
91 if( ( ret = asn1_get_tag( p, end, &len,
92 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
93 {
94 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
95 {
96 *ver = 0;
97 return( 0 );
98 }
99
100 return( ret );
101 }
102
103 end = *p + len;
104
105 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200106 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
108 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200109 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
111
112 return( 0 );
113}
114
115/*
116 * Validity ::= SEQUENCE {
117 * notBefore Time,
118 * notAfter Time }
119 */
120static int x509_get_dates( unsigned char **p,
121 const unsigned char *end,
122 x509_time *from,
123 x509_time *to )
124{
125 int ret;
126 size_t len;
127
128 if( ( ret = asn1_get_tag( p, end, &len,
129 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200130 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200131
132 end = *p + len;
133
134 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
135 return( ret );
136
137 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
138 return( ret );
139
140 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200141 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
143
144 return( 0 );
145}
146
147/*
148 * X.509 v2/v3 unique identifier (not parsed)
149 */
150static int x509_get_uid( unsigned char **p,
151 const unsigned char *end,
152 x509_buf *uid, int n )
153{
154 int ret;
155
156 if( *p == end )
157 return( 0 );
158
159 uid->tag = **p;
160
161 if( ( ret = asn1_get_tag( p, end, &uid->len,
162 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
163 {
164 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
165 return( 0 );
166
167 return( ret );
168 }
169
170 uid->p = *p;
171 *p += uid->len;
172
173 return( 0 );
174}
175
176static int x509_get_basic_constraints( unsigned char **p,
177 const unsigned char *end,
178 int *ca_istrue,
179 int *max_pathlen )
180{
181 int ret;
182 size_t len;
183
184 /*
185 * BasicConstraints ::= SEQUENCE {
186 * cA BOOLEAN DEFAULT FALSE,
187 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
188 */
189 *ca_istrue = 0; /* DEFAULT FALSE */
190 *max_pathlen = 0; /* endless */
191
192 if( ( ret = asn1_get_tag( p, end, &len,
193 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200194 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200195
196 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200197 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200198
199 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
200 {
201 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
202 ret = asn1_get_int( p, end, ca_istrue );
203
204 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200205 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206
207 if( *ca_istrue != 0 )
208 *ca_istrue = 1;
209 }
210
211 if( *p == end )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200212 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200213
214 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200215 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216
217 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200218 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
220
221 (*max_pathlen)++;
222
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200223 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200224}
225
226static int x509_get_ns_cert_type( unsigned char **p,
227 const unsigned char *end,
228 unsigned char *ns_cert_type)
229{
230 int ret;
231 x509_bitstring bs = { 0, 0, NULL };
232
233 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200234 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235
236 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200237 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200238 POLARSSL_ERR_ASN1_INVALID_LENGTH );
239
240 /* Get actual bitstring */
241 *ns_cert_type = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200242 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243}
244
245static int x509_get_key_usage( unsigned char **p,
246 const unsigned char *end,
247 unsigned char *key_usage)
248{
249 int ret;
250 x509_bitstring bs = { 0, 0, NULL };
251
252 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200253 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254
255 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200256 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200257 POLARSSL_ERR_ASN1_INVALID_LENGTH );
258
259 /* Get actual bitstring */
260 *key_usage = *bs.p;
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200261 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262}
263
264/*
265 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
266 *
267 * KeyPurposeId ::= OBJECT IDENTIFIER
268 */
269static int x509_get_ext_key_usage( unsigned char **p,
270 const unsigned char *end,
271 x509_sequence *ext_key_usage)
272{
273 int ret;
274
275 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200276 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277
278 /* Sequence length must be >= 1 */
279 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200280 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281 POLARSSL_ERR_ASN1_INVALID_LENGTH );
282
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200283 return( 0 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284}
285
286/*
287 * SubjectAltName ::= GeneralNames
288 *
289 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
290 *
291 * GeneralName ::= CHOICE {
292 * otherName [0] OtherName,
293 * rfc822Name [1] IA5String,
294 * dNSName [2] IA5String,
295 * x400Address [3] ORAddress,
296 * directoryName [4] Name,
297 * ediPartyName [5] EDIPartyName,
298 * uniformResourceIdentifier [6] IA5String,
299 * iPAddress [7] OCTET STRING,
300 * registeredID [8] OBJECT IDENTIFIER }
301 *
302 * OtherName ::= SEQUENCE {
303 * type-id OBJECT IDENTIFIER,
304 * value [0] EXPLICIT ANY DEFINED BY type-id }
305 *
306 * EDIPartyName ::= SEQUENCE {
307 * nameAssigner [0] DirectoryString OPTIONAL,
308 * partyName [1] DirectoryString }
309 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000310 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 */
312static int x509_get_subject_alt_name( unsigned char **p,
313 const unsigned char *end,
314 x509_sequence *subject_alt_name )
315{
316 int ret;
317 size_t len, tag_len;
318 asn1_buf *buf;
319 unsigned char tag;
320 asn1_sequence *cur = subject_alt_name;
321
322 /* Get main sequence tag */
323 if( ( ret = asn1_get_tag( p, end, &len,
324 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200325 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326
327 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200328 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
330
331 while( *p < end )
332 {
333 if( ( end - *p ) < 1 )
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_OUT_OF_DATA );
336
337 tag = **p;
338 (*p)++;
339 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200340 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341
342 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200343 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
345
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200346 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
348 {
349 *p += tag_len;
350 continue;
351 }
352
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200354 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100356 if( cur->next != NULL )
357 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
358
Mansour Moufidbd1d44e2015-02-15 17:46:32 -0500359 cur->next = polarssl_malloc( sizeof( asn1_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360
361 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200362 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 POLARSSL_ERR_ASN1_MALLOC_FAILED );
364
365 memset( cur->next, 0, sizeof( asn1_sequence ) );
366 cur = cur->next;
367 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200368
369 buf = &(cur->buf);
370 buf->tag = tag;
371 buf->p = *p;
372 buf->len = tag_len;
373 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200374 }
375
376 /* Set final sequence entry's next pointer to NULL */
377 cur->next = NULL;
378
379 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200380 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
382
383 return( 0 );
384}
385
386/*
387 * X.509 v3 extensions
388 *
389 * TODO: Perform all of the basic constraints tests required by the RFC
390 * TODO: Set values for undetected extensions to a sane default?
391 *
392 */
393static int x509_get_crt_ext( unsigned char **p,
394 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200395 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396{
397 int ret;
398 size_t len;
399 unsigned char *end_ext_data, *end_ext_octet;
400
401 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
402 {
403 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
404 return( 0 );
405
406 return( ret );
407 }
408
409 while( *p < end )
410 {
411 /*
412 * Extension ::= SEQUENCE {
413 * extnID OBJECT IDENTIFIER,
414 * critical BOOLEAN DEFAULT FALSE,
415 * extnValue OCTET STRING }
416 */
417 x509_buf extn_oid = {0, 0, NULL};
418 int is_critical = 0; /* DEFAULT FALSE */
419 int ext_type = 0;
420
421 if( ( ret = asn1_get_tag( p, end, &len,
422 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200423 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
425 end_ext_data = *p + len;
426
427 /* Get extension ID */
428 extn_oid.tag = **p;
429
430 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200431 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 extn_oid.p = *p;
434 *p += extn_oid.len;
435
436 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200437 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 POLARSSL_ERR_ASN1_OUT_OF_DATA );
439
440 /* Get optional critical */
441 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
442 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200443 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 /* Data should be octet string type */
446 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
447 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200448 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449
450 end_ext_octet = *p + len;
451
452 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200453 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
455
456 /*
457 * Detect supported extensions
458 */
459 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
460
461 if( ret != 0 )
462 {
463 /* No parser found, skip extension */
464 *p = end_ext_octet;
465
466#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
467 if( is_critical )
468 {
469 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200470 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
472 }
473#endif
474 continue;
475 }
476
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100477 /* Forbid repeated extensions */
478 if( ( crt->ext_types & ext_type ) != 0 )
479 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
480
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481 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
Mansour Moufid369e6c22015-02-15 17:35:38 -0500552 p = polarssl_malloc( len = buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200553
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 {
Mansour Moufid369e6c22015-02-15 17:35:38 -0500809 crt->next = 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;
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100815 x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200816 crt = crt->next;
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
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100949 if( ( ret = pk_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 );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +0000989 if( w_ret == 0 )
990 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
992 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200993 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
995
996 len = MAX_PATH - len;
997 do
998 {
Paul Bakker9af723c2014-05-01 13:03:14 +0200999 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000
1001 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1002 continue;
1003
Paul Bakker9af723c2014-05-01 13:03:14 +02001004 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001005 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001006 p, len - 1,
1007 NULL, NULL );
Manuel Pégourié-Gonnardacdb9b92015-01-23 17:50:34 +00001008 if( w_ret == 0 )
1009 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
Paul Bakkerddf26b42013-09-18 13:46:23 +02001011 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001012 if( w_ret < 0 )
1013 ret++;
1014 else
1015 ret += w_ret;
1016 }
1017 while( FindNextFileW( hFind, &file_data ) != 0 );
1018
Paul Bakker66d5d072014-06-17 16:39:18 +02001019 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1021
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001022 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001023#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001024 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001026 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 char entry_name[255];
1028 DIR *dir = opendir( path );
1029
Paul Bakker66d5d072014-06-17 16:39:18 +02001030 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001031 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1032
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001033#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001034 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1035 return( ret );
1036#endif
1037
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001038 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001040 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001042 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001043 {
1044 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001045 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1046 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047 }
1048
1049 if( !S_ISREG( sb.st_mode ) )
1050 continue;
1051
1052 // Ignore parse errors
1053 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001054 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 if( t_ret < 0 )
1056 ret++;
1057 else
1058 ret += t_ret;
1059 }
1060 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001061
1062cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001063#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001064 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1065 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1066#endif
1067
Paul Bakkerbe089b02013-10-14 15:51:50 +02001068#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001069
1070 return( ret );
1071}
1072#endif /* POLARSSL_FS_IO */
1073
Paul Bakker6edcd412013-10-29 15:22:54 +01001074#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1075 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001076#include <stdarg.h>
1077
1078#if !defined vsnprintf
1079#define vsnprintf _vsnprintf
1080#endif // vsnprintf
1081
1082/*
1083 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1084 * Result value is not size of buffer needed, but -1 if no fit is possible.
1085 *
1086 * This fuction tries to 'fix' this by at least suggesting enlarging the
1087 * size by 20.
1088 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001089static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001090{
1091 va_list ap;
1092 int res = -1;
1093
1094 va_start( ap, format );
1095
1096 res = vsnprintf( str, size, format, ap );
1097
1098 va_end( ap );
1099
1100 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001101 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001102 return( (int) size + 20 );
1103
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001104 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105}
1106
1107#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001108#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001109
1110#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1111
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001112#define SAFE_SNPRINTF() \
1113{ \
1114 if( ret == -1 ) \
1115 return( -1 ); \
1116 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001117 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001118 p[n - 1] = '\0'; \
1119 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1120 } \
1121 \
1122 n -= (unsigned int) ret; \
1123 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001124}
1125
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001126static int x509_info_subject_alt_name( char **buf, size_t *size,
1127 const x509_sequence *subject_alt_name )
1128{
1129 size_t i;
1130 size_t n = *size;
1131 char *p = *buf;
1132 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001133 const char *sep = "";
1134 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001135
1136 while( cur != NULL )
1137 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001138 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001139 {
1140 *p = '\0';
1141 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1142 }
1143
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001144 n -= cur->buf.len + sep_len;
1145 for( i = 0; i < sep_len; i++ )
1146 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001147 for( i = 0; i < cur->buf.len; i++ )
1148 *p++ = cur->buf.p[i];
1149
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001150 sep = ", ";
1151 sep_len = 2;
1152
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001153 cur = cur->next;
1154 }
1155
1156 *p = '\0';
1157
1158 *size = n;
1159 *buf = p;
1160
1161 return( 0 );
1162}
1163
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001164#define PRINT_ITEM(i) \
1165 { \
1166 ret = snprintf( p, n, "%s" i, sep ); \
1167 SAFE_SNPRINTF(); \
1168 sep = ", "; \
1169 }
1170
1171#define CERT_TYPE(type,name) \
1172 if( ns_cert_type & type ) \
1173 PRINT_ITEM( name );
1174
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001175static int x509_info_cert_type( char **buf, size_t *size,
1176 unsigned char ns_cert_type )
1177{
1178 int ret;
1179 size_t n = *size;
1180 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001181 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001182
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001183 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1184 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1185 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1186 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1187 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1188 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1189 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1190 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001191
1192 *size = n;
1193 *buf = p;
1194
1195 return( 0 );
1196}
1197
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001198#define KEY_USAGE(code,name) \
1199 if( key_usage & code ) \
1200 PRINT_ITEM( name );
1201
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001202static int x509_info_key_usage( char **buf, size_t *size,
1203 unsigned char key_usage )
1204{
1205 int ret;
1206 size_t n = *size;
1207 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001208 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001209
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001210 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1211 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1212 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1213 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1214 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1215 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1216 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001217
1218 *size = n;
1219 *buf = p;
1220
1221 return( 0 );
1222}
1223
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001224static int x509_info_ext_key_usage( char **buf, size_t *size,
1225 const x509_sequence *extended_key_usage )
1226{
1227 int ret;
1228 const char *desc;
1229 size_t n = *size;
1230 char *p = *buf;
1231 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001232 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001233
1234 while( cur != NULL )
1235 {
1236 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1237 desc = "???";
1238
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001239 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001240 SAFE_SNPRINTF();
1241
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001242 sep = ", ";
1243
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001244 cur = cur->next;
1245 }
1246
1247 *size = n;
1248 *buf = p;
1249
1250 return( 0 );
1251}
1252
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001253/*
1254 * Return an informational string about the certificate.
1255 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001256#define BEFORE_COLON 18
1257#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001258int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001259 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260{
1261 int ret;
1262 size_t n;
1263 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001264 char key_size_str[BEFORE_COLON];
1265
1266 p = buf;
1267 n = size;
1268
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001269 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 prefix, crt->version );
1271 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001272 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 prefix );
1274 SAFE_SNPRINTF();
1275
Paul Bakker66d5d072014-06-17 16:39:18 +02001276 ret = x509_serial_gets( p, n, &crt->serial );
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%sissuer 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->issuer );
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%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001286 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001287 SAFE_SNPRINTF();
1288
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001289 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1291 crt->valid_from.year, crt->valid_from.mon,
1292 crt->valid_from.day, crt->valid_from.hour,
1293 crt->valid_from.min, crt->valid_from.sec );
1294 SAFE_SNPRINTF();
1295
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001296 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001297 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1298 crt->valid_to.year, crt->valid_to.mon,
1299 crt->valid_to.day, crt->valid_to.hour,
1300 crt->valid_to.min, crt->valid_to.sec );
1301 SAFE_SNPRINTF();
1302
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001303 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 SAFE_SNPRINTF();
1305
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001306 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001307 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001308 SAFE_SNPRINTF();
1309
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001310 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1312 pk_get_name( &crt->pk ) ) ) != 0 )
1313 {
1314 return( ret );
1315 }
1316
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001317 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001318 (int) pk_get_size( &crt->pk ) );
1319 SAFE_SNPRINTF();
1320
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001321 /*
1322 * Optional extensions
1323 */
1324
1325 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1326 {
1327 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1328 crt->ca_istrue ? "true" : "false" );
1329 SAFE_SNPRINTF();
1330
1331 if( crt->max_pathlen > 0 )
1332 {
1333 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1334 SAFE_SNPRINTF();
1335 }
1336 }
1337
1338 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1339 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001340 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001341 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001342
1343 if( ( ret = x509_info_subject_alt_name( &p, &n,
1344 &crt->subject_alt_names ) ) != 0 )
1345 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001346 }
1347
1348 if( crt->ext_types & EXT_NS_CERT_TYPE )
1349 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001350 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001351 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001352
1353 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1354 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001355 }
1356
1357 if( crt->ext_types & EXT_KEY_USAGE )
1358 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001359 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001360 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001361
1362 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1363 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001364 }
1365
1366 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1367 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001368 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001369 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001370
1371 if( ( ret = x509_info_ext_key_usage( &p, &n,
1372 &crt->ext_key_usage ) ) != 0 )
1373 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001374 }
1375
1376 ret = snprintf( p, n, "\n" );
1377 SAFE_SNPRINTF();
1378
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001379 return( (int) ( size - n ) );
1380}
1381
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001382#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1383int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1384{
1385 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1386 ( crt->key_usage & usage ) != usage )
1387 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1388
1389 return( 0 );
1390}
1391#endif
1392
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001393#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1394int x509_crt_check_extended_key_usage( const x509_crt *crt,
1395 const char *usage_oid,
1396 size_t usage_len )
1397{
1398 const x509_sequence *cur;
1399
1400 /* Extension is not mandatory, absent means no restriction */
1401 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1402 return( 0 );
1403
1404 /*
1405 * Look for the requested usage (or wildcard ANY) in our list
1406 */
1407 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1408 {
1409 const x509_buf *cur_oid = &cur->buf;
1410
1411 if( cur_oid->len == usage_len &&
1412 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1413 {
1414 return( 0 );
1415 }
1416
1417 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1418 return( 0 );
1419 }
1420
1421 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1422}
Paul Bakker9af723c2014-05-01 13:03:14 +02001423#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001424
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001425#if defined(POLARSSL_X509_CRL_PARSE_C)
1426/*
1427 * Return 1 if the certificate is revoked, or 0 otherwise.
1428 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001429int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001430{
1431 const x509_crl_entry *cur = &crl->entry;
1432
1433 while( cur != NULL && cur->serial.len != 0 )
1434 {
1435 if( crt->serial.len == cur->serial.len &&
1436 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1437 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001438 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001439 return( 1 );
1440 }
1441
1442 cur = cur->next;
1443 }
1444
1445 return( 0 );
1446}
1447
1448/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001449 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001450 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001451static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001452 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453{
1454 int flags = 0;
1455 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1456 const md_info_t *md_info;
1457
1458 if( ca == NULL )
1459 return( flags );
1460
1461 /*
1462 * TODO: What happens if no CRL is present?
1463 * Suggestion: Revocation state should be unknown if no CRL is present.
1464 * For backwards compatibility this is not yet implemented.
1465 */
1466
1467 while( crl_list != NULL )
1468 {
1469 if( crl_list->version == 0 ||
1470 crl_list->issuer_raw.len != ca->subject_raw.len ||
1471 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1472 crl_list->issuer_raw.len ) != 0 )
1473 {
1474 crl_list = crl_list->next;
1475 continue;
1476 }
1477
1478 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001479 * Check if the CA is configured to sign CRLs
1480 */
1481#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1482 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1483 {
1484 flags |= BADCRL_NOT_TRUSTED;
1485 break;
1486 }
1487#endif
1488
1489 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001490 * Check if CRL is correctly signed by the trusted CA
1491 */
1492 md_info = md_info_from_type( crl_list->sig_md );
1493 if( md_info == NULL )
1494 {
1495 /*
1496 * Cannot check 'unknown' hash
1497 */
1498 flags |= BADCRL_NOT_TRUSTED;
1499 break;
1500 }
1501
1502 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1503
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001504 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1505 crl_list->sig_md, hash, md_info->size,
1506 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001507 {
1508 flags |= BADCRL_NOT_TRUSTED;
1509 break;
1510 }
1511
1512 /*
1513 * Check for validity of CRL (Do not drop out)
1514 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001515 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001516 flags |= BADCRL_EXPIRED;
1517
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001518 if( x509_time_future( &crl_list->this_update ) )
1519 flags |= BADCRL_FUTURE;
1520
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001521 /*
1522 * Check if certificate is revoked
1523 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001524 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001525 {
1526 flags |= BADCERT_REVOKED;
1527 break;
1528 }
1529
1530 crl_list = crl_list->next;
1531 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001532 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001533}
1534#endif /* POLARSSL_X509_CRL_PARSE_C */
1535
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001536/*
1537 * Like memcmp, but case-insensitive and always returns -1 if different
1538 */
1539static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540{
1541 size_t i;
1542 unsigned char diff;
1543 const unsigned char *n1 = s1, *n2 = s2;
1544
1545 for( i = 0; i < len; i++ )
1546 {
1547 diff = n1[i] ^ n2[i];
1548
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001549 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001550 continue;
1551
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001552 if( diff == 32 &&
1553 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1554 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1555 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001556 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001557 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001558
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001559 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001560 }
1561
1562 return( 0 );
1563}
1564
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001565/*
1566 * Return 1 if match, 0 if not
1567 * TODO: inverted return value!
1568 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001569static int x509_wildcard_verify( const char *cn, x509_buf *name )
1570{
1571 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001572 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001573
1574 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1575 return( 0 );
1576
Paul Bakker14b16c62014-05-28 11:33:54 +02001577 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578 {
1579 if( cn[i] == '.' )
1580 {
1581 cn_idx = i;
1582 break;
1583 }
1584 }
1585
1586 if( cn_idx == 0 )
1587 return( 0 );
1588
Paul Bakker14b16c62014-05-28 11:33:54 +02001589 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001590 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001591 {
1592 return( 1 );
1593 }
1594
1595 return( 0 );
1596}
1597
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001598/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001599 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1600 * variations (but not all).
1601 *
1602 * Return 0 if equal, -1 otherwise.
1603 */
1604static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1605{
1606 if( a->tag == b->tag &&
1607 a->len == b->len &&
1608 memcmp( a->p, b->p, b->len ) == 0 )
1609 {
1610 return( 0 );
1611 }
1612
1613 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1614 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1615 a->len == b->len &&
1616 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1617 {
1618 return( 0 );
1619 }
1620
1621 return( -1 );
1622}
1623
1624/*
1625 * Compare two X.509 Names (aka rdnSequence).
1626 *
1627 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1628 * we sometimes return unequal when the full algorithm would return equal,
1629 * but never the other way. (In particular, we don't do Unicode normalisation
1630 * or space folding.)
1631 *
1632 * Return 0 if equal, -1 otherwise.
1633 */
1634static int x509_name_cmp( const x509_name *a, const x509_name *b )
1635{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001636 /* Avoid recursion, it might not be optimised by the compiler */
1637 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001638 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001639 if( a == NULL || b == NULL )
1640 return( -1 );
1641
1642 /* type */
1643 if( a->oid.tag != b->oid.tag ||
1644 a->oid.len != b->oid.len ||
1645 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1646 {
1647 return( -1 );
1648 }
1649
1650 /* value */
1651 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1652 return( -1 );
1653
Manuel Pégourié-Gonnard555fbf82015-02-04 17:11:55 +00001654 /* structure of the list of sets */
1655 if( a->next_merged != b->next_merged )
1656 return( -1 );
1657
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001658 a = a->next;
1659 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001660 }
1661
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001662 /* a == NULL == b */
1663 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001664}
1665
1666/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001667 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1668 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001669 *
1670 * top means parent is a locally-trusted certificate
1671 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001672 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001673static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001674 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001675 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001676{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001677 int need_ca_bit;
1678
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001679 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001680 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001681 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001682
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001683 /* Parent must have the basicConstraints CA bit set as a general rule */
1684 need_ca_bit = 1;
1685
1686 /* Exception: v1/v2 certificates that are locally trusted. */
1687 if( top && parent->version < 3 )
1688 need_ca_bit = 0;
1689
1690 /* Exception: self-signed end-entity certs that are locally trusted. */
1691 if( top && bottom &&
1692 child->raw.len == parent->raw.len &&
1693 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1694 {
1695 need_ca_bit = 0;
1696 }
1697
1698 if( need_ca_bit && ! parent->ca_istrue )
1699 return( -1 );
1700
1701#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1702 if( need_ca_bit &&
1703 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001704 {
1705 return( -1 );
1706 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001707#endif
1708
1709 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001710}
1711
Paul Bakkerddf26b42013-09-18 13:46:23 +02001712static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001713 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001714 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001715 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001716 void *p_vrfy )
1717{
1718 int ret;
1719 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1720 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1721 const md_info_t *md_info;
1722
Paul Bakker86d0c192013-09-18 11:11:02 +02001723 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001724 *flags |= BADCERT_EXPIRED;
1725
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001726 if( x509_time_future( &child->valid_from ) )
1727 *flags |= BADCERT_FUTURE;
1728
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001729 /*
1730 * Child is the top of the chain. Check against the trust_ca list.
1731 */
1732 *flags |= BADCERT_NOT_TRUSTED;
1733
1734 md_info = md_info_from_type( child->sig_md );
1735 if( md_info == NULL )
1736 {
1737 /*
1738 * Cannot check 'unknown', no need to try any CA
1739 */
1740 trust_ca = NULL;
1741 }
1742 else
1743 md( md_info, child->tbs.p, child->tbs.len, hash );
1744
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001745 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001746 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001747 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001748 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001749
1750 /*
1751 * Reduce path_len to check against if top of the chain is
1752 * the same as the trusted CA
1753 */
1754 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1755 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1756 child->issuer_raw.len ) == 0 )
1757 {
1758 check_path_cnt--;
1759 }
1760
1761 if( trust_ca->max_pathlen > 0 &&
1762 trust_ca->max_pathlen < check_path_cnt )
1763 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764 continue;
1765 }
1766
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001767 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1768 child->sig_md, hash, md_info->size,
1769 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001771 continue;
1772 }
1773
1774 /*
1775 * Top of chain is signed by a trusted CA
1776 */
1777 *flags &= ~BADCERT_NOT_TRUSTED;
1778 break;
1779 }
1780
1781 /*
1782 * If top of chain is not the same as the trusted CA send a verify request
1783 * to the callback for any issues with validity and CRL presence for the
1784 * trusted CA certificate.
1785 */
1786 if( trust_ca != NULL &&
1787 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1788 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1789 child->issuer_raw.len ) != 0 ) )
1790 {
1791#if defined(POLARSSL_X509_CRL_PARSE_C)
1792 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001793 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001794#else
1795 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001796#endif
1797
Paul Bakker86d0c192013-09-18 11:11:02 +02001798 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001799 ca_flags |= BADCERT_EXPIRED;
1800
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001801 if( x509_time_future( &trust_ca->valid_from ) )
1802 ca_flags |= BADCERT_FUTURE;
1803
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804 if( NULL != f_vrfy )
1805 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001806 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1807 &ca_flags ) ) != 0 )
1808 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001809 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001810 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811 }
1812 }
1813
1814 /* Call callback on top cert */
1815 if( NULL != f_vrfy )
1816 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001817 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818 return( ret );
1819 }
1820
1821 *flags |= ca_flags;
1822
1823 return( 0 );
1824}
1825
Paul Bakkerddf26b42013-09-18 13:46:23 +02001826static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001827 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001828 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001829 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001830 void *p_vrfy )
1831{
1832 int ret;
1833 int parent_flags = 0;
1834 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001835 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001836 const md_info_t *md_info;
1837
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001838 /* path_cnt is 0 for the first intermediate CA */
1839 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
1840 {
1841 *flags |= BADCERT_NOT_TRUSTED;
1842 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1843 }
1844
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001845 if( x509_time_expired( &child->valid_to ) )
1846 *flags |= BADCERT_EXPIRED;
1847
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001848 if( x509_time_future( &child->valid_from ) )
1849 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001850
1851 md_info = md_info_from_type( child->sig_md );
1852 if( md_info == NULL )
1853 {
1854 /*
1855 * Cannot check 'unknown' hash
1856 */
1857 *flags |= BADCERT_NOT_TRUSTED;
1858 }
1859 else
1860 {
1861 md( md_info, child->tbs.p, child->tbs.len, hash );
1862
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001863 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1864 child->sig_md, hash, md_info->size,
1865 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001866 {
1867 *flags |= BADCERT_NOT_TRUSTED;
1868 }
1869 }
1870
1871#if defined(POLARSSL_X509_CRL_PARSE_C)
1872 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001873 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001874#endif
1875
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001876 /* Look for a grandparent upwards the chain */
1877 for( grandparent = parent->next;
1878 grandparent != NULL;
1879 grandparent = grandparent->next )
1880 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001881 if( x509_crt_check_parent( parent, grandparent,
1882 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001883 break;
1884 }
1885
1886 /* Is our parent part of the chain or at the top? */
1887 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001888 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001889 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1890 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001891 if( ret != 0 )
1892 return( ret );
1893 }
1894 else
1895 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001896 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1897 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001898 if( ret != 0 )
1899 return( ret );
1900 }
1901
1902 /* child is verified to be a child of the parent, call verify callback */
1903 if( NULL != f_vrfy )
1904 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1905 return( ret );
1906
1907 *flags |= parent_flags;
1908
1909 return( 0 );
1910}
1911
1912/*
1913 * Verify the certificate validity
1914 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001915int x509_crt_verify( x509_crt *crt,
1916 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001917 x509_crl *ca_crl,
1918 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001919 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001920 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921{
1922 size_t cn_len;
1923 int ret;
1924 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001925 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001926 x509_name *name;
1927 x509_sequence *cur = NULL;
1928
1929 *flags = 0;
1930
1931 if( cn != NULL )
1932 {
1933 name = &crt->subject;
1934 cn_len = strlen( cn );
1935
1936 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1937 {
1938 cur = &crt->subject_alt_names;
1939
1940 while( cur != NULL )
1941 {
1942 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001943 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001944 break;
1945
1946 if( cur->buf.len > 2 &&
1947 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1948 x509_wildcard_verify( cn, &cur->buf ) )
1949 break;
1950
1951 cur = cur->next;
1952 }
1953
1954 if( cur == NULL )
1955 *flags |= BADCERT_CN_MISMATCH;
1956 }
1957 else
1958 {
1959 while( name != NULL )
1960 {
1961 if( OID_CMP( OID_AT_CN, &name->oid ) )
1962 {
1963 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001964 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001965 break;
1966
1967 if( name->val.len > 2 &&
1968 memcmp( name->val.p, "*.", 2 ) == 0 &&
1969 x509_wildcard_verify( cn, &name->val ) )
1970 break;
1971 }
1972
1973 name = name->next;
1974 }
1975
1976 if( name == NULL )
1977 *flags |= BADCERT_CN_MISMATCH;
1978 }
1979 }
1980
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001981 /* Look for a parent upwards the chain */
1982 for( parent = crt->next; parent != NULL; parent = parent->next )
1983 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001984 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001985 break;
1986 }
1987
1988 /* Are we part of the chain or at the top? */
1989 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001990 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001991 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1992 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001993 if( ret != 0 )
1994 return( ret );
1995 }
1996 else
1997 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001998 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1999 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002000 if( ret != 0 )
2001 return( ret );
2002 }
2003
2004 if( *flags != 0 )
2005 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
2006
2007 return( 0 );
2008}
2009
2010/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002011 * Initialize a certificate chain
2012 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002013void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002014{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002015 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002016}
2017
2018/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002019 * Unallocate all certificate data
2020 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002021void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002022{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002023 x509_crt *cert_cur = crt;
2024 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002025 x509_name *name_cur;
2026 x509_name *name_prv;
2027 x509_sequence *seq_cur;
2028 x509_sequence *seq_prv;
2029
2030 if( crt == NULL )
2031 return;
2032
2033 do
2034 {
2035 pk_free( &cert_cur->pk );
2036
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002037#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002038 polarssl_free( cert_cur->sig_opts );
2039#endif
2040
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002041 name_cur = cert_cur->issuer.next;
2042 while( name_cur != NULL )
2043 {
2044 name_prv = name_cur;
2045 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002046 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002047 polarssl_free( name_prv );
2048 }
2049
2050 name_cur = cert_cur->subject.next;
2051 while( name_cur != NULL )
2052 {
2053 name_prv = name_cur;
2054 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002055 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002056 polarssl_free( name_prv );
2057 }
2058
2059 seq_cur = cert_cur->ext_key_usage.next;
2060 while( seq_cur != NULL )
2061 {
2062 seq_prv = seq_cur;
2063 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002064 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002065 polarssl_free( seq_prv );
2066 }
2067
2068 seq_cur = cert_cur->subject_alt_names.next;
2069 while( seq_cur != NULL )
2070 {
2071 seq_prv = seq_cur;
2072 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002073 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002074 polarssl_free( seq_prv );
2075 }
2076
2077 if( cert_cur->raw.p != NULL )
2078 {
Paul Bakker34617722014-06-13 17:20:13 +02002079 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002080 polarssl_free( cert_cur->raw.p );
2081 }
2082
2083 cert_cur = cert_cur->next;
2084 }
2085 while( cert_cur != NULL );
2086
2087 cert_cur = crt;
2088 do
2089 {
2090 cert_prv = cert_cur;
2091 cert_cur = cert_cur->next;
2092
Paul Bakker34617722014-06-13 17:20:13 +02002093 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002094 if( cert_prv != crt )
2095 polarssl_free( cert_prv );
2096 }
2097 while( cert_cur != NULL );
2098}
2099
Paul Bakker9af723c2014-05-01 13:03:14 +02002100#endif /* POLARSSL_X509_CRT_PARSE_C */