blob: be62d142ab126fb63463e46f96066b61249a6aad [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 *
Manuel Pégourié-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
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 *
Manuel Pégourié-Gonnardb4fe3cb2015-01-22 16:11:05 +0000316 * NOTE: we only parse and use dNSName at this point.
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317 */
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 {
Manuel Pégourié-Gonnardb1340602014-11-11 23:11:16 +0100362 if( cur->next != NULL )
363 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
364
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 cur->next = (asn1_sequence *) polarssl_malloc(
366 sizeof( asn1_sequence ) );
367
368 if( cur->next == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200369 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 POLARSSL_ERR_ASN1_MALLOC_FAILED );
371
372 memset( cur->next, 0, sizeof( asn1_sequence ) );
373 cur = cur->next;
374 }
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200375
376 buf = &(cur->buf);
377 buf->tag = tag;
378 buf->p = *p;
379 buf->len = tag_len;
380 *p += buf->len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381 }
382
383 /* Set final sequence entry's next pointer to NULL */
384 cur->next = NULL;
385
386 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200387 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
389
390 return( 0 );
391}
392
393/*
394 * X.509 v3 extensions
395 *
396 * TODO: Perform all of the basic constraints tests required by the RFC
397 * TODO: Set values for undetected extensions to a sane default?
398 *
399 */
400static int x509_get_crt_ext( unsigned char **p,
401 const unsigned char *end,
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200402 x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403{
404 int ret;
405 size_t len;
406 unsigned char *end_ext_data, *end_ext_octet;
407
408 if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
409 {
410 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
411 return( 0 );
412
413 return( ret );
414 }
415
416 while( *p < end )
417 {
418 /*
419 * Extension ::= SEQUENCE {
420 * extnID OBJECT IDENTIFIER,
421 * critical BOOLEAN DEFAULT FALSE,
422 * extnValue OCTET STRING }
423 */
424 x509_buf extn_oid = {0, 0, NULL};
425 int is_critical = 0; /* DEFAULT FALSE */
426 int ext_type = 0;
427
428 if( ( ret = asn1_get_tag( p, end, &len,
429 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200430 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200431
432 end_ext_data = *p + len;
433
434 /* Get extension ID */
435 extn_oid.tag = **p;
436
437 if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200438 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
440 extn_oid.p = *p;
441 *p += extn_oid.len;
442
443 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200444 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445 POLARSSL_ERR_ASN1_OUT_OF_DATA );
446
447 /* Get optional critical */
448 if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
449 ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
Paul Bakker51876562013-09-17 14:36:05 +0200450 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451
452 /* Data should be octet string type */
453 if( ( ret = asn1_get_tag( p, end_ext_data, &len,
454 ASN1_OCTET_STRING ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200455 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200456
457 end_ext_octet = *p + len;
458
459 if( end_ext_octet != end_ext_data )
Paul Bakker51876562013-09-17 14:36:05 +0200460 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200461 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
462
463 /*
464 * Detect supported extensions
465 */
466 ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
467
468 if( ret != 0 )
469 {
470 /* No parser found, skip extension */
471 *p = end_ext_octet;
472
473#if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
474 if( is_critical )
475 {
476 /* Data is marked as critical: fail */
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200477 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200478 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
479 }
480#endif
481 continue;
482 }
483
Manuel Pégourié-Gonnard8a5e3d42014-11-12 17:47:28 +0100484 /* Forbid repeated extensions */
485 if( ( crt->ext_types & ext_type ) != 0 )
486 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS );
487
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 crt->ext_types |= ext_type;
489
490 switch( ext_type )
491 {
492 case EXT_BASIC_CONSTRAINTS:
493 /* Parse basic constraints */
494 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
495 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200496 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497 break;
498
499 case EXT_KEY_USAGE:
500 /* Parse key usage */
501 if( ( ret = x509_get_key_usage( p, end_ext_octet,
502 &crt->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_EXTENDED_KEY_USAGE:
507 /* Parse extended key usage */
508 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
509 &crt->ext_key_usage ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200510 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511 break;
512
513 case EXT_SUBJECT_ALT_NAME:
514 /* Parse subject alt name */
515 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
516 &crt->subject_alt_names ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200517 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 break;
519
520 case EXT_NS_CERT_TYPE:
521 /* Parse netscape certificate type */
522 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
523 &crt->ns_cert_type ) ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200524 return( ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525 break;
526
527 default:
528 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
529 }
530 }
531
532 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200533 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200534 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
535
536 return( 0 );
537}
538
539/*
540 * Parse and fill a single X.509 certificate in DER format
541 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200542static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200543 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544{
545 int ret;
546 size_t len;
547 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200548 x509_buf sig_params1, sig_params2;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100549
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200550 memset( &sig_params1, 0, sizeof( x509_buf ) );
551 memset( &sig_params2, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200552
553 /*
554 * Check for valid input
555 */
556 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200557 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 p = (unsigned char *) polarssl_malloc( len = buflen );
560
561 if( p == NULL )
562 return( POLARSSL_ERR_X509_MALLOC_FAILED );
563
564 memcpy( p, buf, buflen );
565
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566 crt->raw.p = p;
567 crt->raw.len = len;
568 end = p + len;
569
570 /*
571 * Certificate ::= SEQUENCE {
572 * tbsCertificate TBSCertificate,
573 * signatureAlgorithm AlgorithmIdentifier,
574 * signatureValue BIT STRING }
575 */
576 if( ( ret = asn1_get_tag( &p, end, &len,
577 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
578 {
579 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200580 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581 }
582
583 if( len > (size_t) ( end - p ) )
584 {
585 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200586 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
588 }
589 crt_end = p + len;
590
591 /*
592 * TBSCertificate ::= SEQUENCE {
593 */
594 crt->tbs.p = p;
595
596 if( ( ret = asn1_get_tag( &p, end, &len,
597 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
598 {
599 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200600 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601 }
602
603 end = p + len;
604 crt->tbs.len = end - crt->tbs.p;
605
606 /*
607 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
608 *
609 * CertificateSerialNumber ::= INTEGER
610 *
611 * signature AlgorithmIdentifier
612 */
613 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
614 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100615 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200616 &sig_params1 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617 {
618 x509_crt_free( crt );
619 return( ret );
620 }
621
622 crt->version++;
623
624 if( crt->version > 3 )
625 {
626 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200627 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200628 }
629
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200630 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200631 &crt->sig_md, &crt->sig_pk,
632 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633 {
634 x509_crt_free( crt );
635 return( ret );
636 }
637
638 /*
639 * issuer Name
640 */
641 crt->issuer_raw.p = p;
642
643 if( ( ret = asn1_get_tag( &p, end, &len,
644 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
645 {
646 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200647 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648 }
649
650 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
651 {
652 x509_crt_free( crt );
653 return( ret );
654 }
655
656 crt->issuer_raw.len = p - crt->issuer_raw.p;
657
658 /*
659 * Validity ::= SEQUENCE {
660 * notBefore Time,
661 * notAfter Time }
662 *
663 */
664 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
665 &crt->valid_to ) ) != 0 )
666 {
667 x509_crt_free( crt );
668 return( ret );
669 }
670
671 /*
672 * subject Name
673 */
674 crt->subject_raw.p = p;
675
676 if( ( ret = asn1_get_tag( &p, end, &len,
677 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
678 {
679 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200680 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 }
682
683 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
684 {
685 x509_crt_free( crt );
686 return( ret );
687 }
688
689 crt->subject_raw.len = p - crt->subject_raw.p;
690
691 /*
692 * SubjectPublicKeyInfo
693 */
Paul Bakkerda771152013-09-16 22:45:03 +0200694 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200695 {
696 x509_crt_free( crt );
697 return( ret );
698 }
699
700 /*
701 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
702 * -- If present, version shall be v2 or v3
703 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
704 * -- If present, version shall be v2 or v3
705 * extensions [3] EXPLICIT Extensions OPTIONAL
706 * -- If present, version shall be v3
707 */
708 if( crt->version == 2 || crt->version == 3 )
709 {
710 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
711 if( ret != 0 )
712 {
713 x509_crt_free( crt );
714 return( ret );
715 }
716 }
717
718 if( crt->version == 2 || crt->version == 3 )
719 {
720 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
721 if( ret != 0 )
722 {
723 x509_crt_free( crt );
724 return( ret );
725 }
726 }
727
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200728#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729 if( crt->version == 3 )
730 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200731#endif
Paul Bakker66d5d072014-06-17 16:39:18 +0200732 ret = x509_get_crt_ext( &p, end, crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 if( ret != 0 )
734 {
735 x509_crt_free( crt );
736 return( ret );
737 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200738#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200739 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200740#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200741
742 if( p != end )
743 {
744 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200745 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
747 }
748
749 end = crt_end;
750
751 /*
752 * }
753 * -- end of TBSCertificate
754 *
755 * signatureAlgorithm AlgorithmIdentifier,
756 * signatureValue BIT STRING
757 */
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200758 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759 {
760 x509_crt_free( crt );
761 return( ret );
762 }
763
764 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnarddddbb1d2014-06-05 17:02:24 +0200765 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
766 sig_params1.len != sig_params2.len ||
Paul Bakker66d5d072014-06-17 16:39:18 +0200767 memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768 {
769 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200770 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 }
772
773 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
774 {
775 x509_crt_free( crt );
776 return( ret );
777 }
778
779 if( p != end )
780 {
781 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200782 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200783 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
784 }
785
786 return( 0 );
787}
788
789/*
790 * Parse one X.509 certificate in DER format from a buffer and add them to a
791 * chained list
792 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200793int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200794 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795{
796 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200797 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798
799 /*
800 * Check for valid input
801 */
802 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200803 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804
805 while( crt->version != 0 && crt->next != NULL )
806 {
807 prev = crt;
808 crt = crt->next;
809 }
810
811 /*
812 * Add new certificate on the end of the chain if needed.
813 */
Paul Bakker66d5d072014-06-17 16:39:18 +0200814 if( crt->version != 0 && crt->next == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200815 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200816 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200817
818 if( crt->next == NULL )
819 return( POLARSSL_ERR_X509_MALLOC_FAILED );
820
821 prev = crt;
Manuel Pégourié-Gonnarde5b0fc12014-11-12 22:27:42 +0100822 x509_crt_init( crt->next );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 crt = crt->next;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200824 }
825
Paul Bakkerddf26b42013-09-18 13:46:23 +0200826 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 {
828 if( prev )
829 prev->next = NULL;
830
831 if( crt != chain )
832 polarssl_free( crt );
833
834 return( ret );
835 }
836
837 return( 0 );
838}
839
840/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200841 * Parse one or more PEM certificates from a buffer and add them to the chained
842 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200843 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200844int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200845{
846 int success = 0, first_error = 0, total_failed = 0;
847 int buf_format = X509_FORMAT_DER;
848
849 /*
850 * Check for valid input
851 */
852 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200853 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200854
855 /*
856 * Determine buffer content. Buffer contains either one DER certificate or
857 * one or more PEM certificates.
858 */
859#if defined(POLARSSL_PEM_PARSE_C)
860 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
861 buf_format = X509_FORMAT_PEM;
862#endif
863
864 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200865 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200866
867#if defined(POLARSSL_PEM_PARSE_C)
868 if( buf_format == X509_FORMAT_PEM )
869 {
870 int ret;
871 pem_context pem;
872
873 while( buflen > 0 )
874 {
875 size_t use_len;
876 pem_init( &pem );
877
878 ret = pem_read_buffer( &pem,
879 "-----BEGIN CERTIFICATE-----",
880 "-----END CERTIFICATE-----",
881 buf, NULL, 0, &use_len );
882
883 if( ret == 0 )
884 {
885 /*
886 * Was PEM encoded
887 */
888 buflen -= use_len;
889 buf += use_len;
890 }
891 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
892 {
893 return( ret );
894 }
895 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
896 {
897 pem_free( &pem );
898
899 /*
900 * PEM header and footer were found
901 */
902 buflen -= use_len;
903 buf += use_len;
904
905 if( first_error == 0 )
906 first_error = ret;
907
Paul Bakker5a5fa922014-09-26 14:53:04 +0200908 total_failed++;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200909 continue;
910 }
911 else
912 break;
913
Paul Bakkerddf26b42013-09-18 13:46:23 +0200914 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200915
916 pem_free( &pem );
917
918 if( ret != 0 )
919 {
920 /*
921 * Quit parsing on a memory error
922 */
923 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
924 return( ret );
925
926 if( first_error == 0 )
927 first_error = ret;
928
929 total_failed++;
930 continue;
931 }
932
933 success = 1;
934 }
935 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200936#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937
938 if( success )
939 return( total_failed );
940 else if( first_error )
941 return( first_error );
942 else
943 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
944}
945
946#if defined(POLARSSL_FS_IO)
947/*
948 * Load one or more certificates and add them to the chained list
949 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200950int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200951{
952 int ret;
953 size_t n;
954 unsigned char *buf;
955
Manuel Pégourié-Gonnard9439f932014-11-21 09:49:43 +0100956 if( ( ret = pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200957 return( ret );
958
Paul Bakkerddf26b42013-09-18 13:46:23 +0200959 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200960
Paul Bakker34617722014-06-13 17:20:13 +0200961 polarssl_zeroize( buf, n + 1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962 polarssl_free( buf );
963
964 return( ret );
965}
966
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100967#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100968static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
969#endif
970
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200971int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972{
973 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100974#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975 int w_ret;
976 WCHAR szDir[MAX_PATH];
977 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200978 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200979 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980
Paul Bakker9af723c2014-05-01 13:03:14 +0200981 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982 HANDLE hFind;
983
984 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200985 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200986
Paul Bakker9af723c2014-05-01 13:03:14 +0200987 memset( szDir, 0, sizeof(szDir) );
988 memset( filename, 0, MAX_PATH );
989 memcpy( filename, path, len );
990 filename[len++] = '\\';
991 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200992 filename[len++] = '*';
993
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200994 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
995 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200996
997 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker66d5d072014-06-17 16:39:18 +0200998 if( hFind == INVALID_HANDLE_VALUE )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1000
1001 len = MAX_PATH - len;
1002 do
1003 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001004 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001005
1006 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1007 continue;
1008
Paul Bakker9af723c2014-05-01 13:03:14 +02001009 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
Paul Bakker66d5d072014-06-17 16:39:18 +02001010 lstrlenW( file_data.cFileName ),
Paul Bakker9af723c2014-05-01 13:03:14 +02001011 p, len - 1,
1012 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001013
Paul Bakkerddf26b42013-09-18 13:46:23 +02001014 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015 if( w_ret < 0 )
1016 ret++;
1017 else
1018 ret += w_ret;
1019 }
1020 while( FindNextFileW( hFind, &file_data ) != 0 );
1021
Paul Bakker66d5d072014-06-17 16:39:18 +02001022 if( GetLastError() != ERROR_NO_MORE_FILES )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1024
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001026#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001027 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001029 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 char entry_name[255];
1031 DIR *dir = opendir( path );
1032
Paul Bakker66d5d072014-06-17 16:39:18 +02001033 if( dir == NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001034 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1035
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001036#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001037 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1038 return( ret );
1039#endif
1040
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001041 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001042 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001043 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001045 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046 {
1047 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001048 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1049 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001050 }
1051
1052 if( !S_ISREG( sb.st_mode ) )
1053 continue;
1054
1055 // Ignore parse errors
1056 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001057 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058 if( t_ret < 0 )
1059 ret++;
1060 else
1061 ret += t_ret;
1062 }
1063 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001064
1065cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001066#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001067 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1068 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1069#endif
1070
Paul Bakkerbe089b02013-10-14 15:51:50 +02001071#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001072
1073 return( ret );
1074}
1075#endif /* POLARSSL_FS_IO */
1076
Paul Bakker6edcd412013-10-29 15:22:54 +01001077#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1078 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001079#include <stdarg.h>
1080
1081#if !defined vsnprintf
1082#define vsnprintf _vsnprintf
1083#endif // vsnprintf
1084
1085/*
1086 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1087 * Result value is not size of buffer needed, but -1 if no fit is possible.
1088 *
1089 * This fuction tries to 'fix' this by at least suggesting enlarging the
1090 * size by 20.
1091 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001092static int compat_snprintf( char *str, size_t size, const char *format, ... )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001093{
1094 va_list ap;
1095 int res = -1;
1096
1097 va_start( ap, format );
1098
1099 res = vsnprintf( str, size, format, ap );
1100
1101 va_end( ap );
1102
1103 // No quick fix possible
Paul Bakker66d5d072014-06-17 16:39:18 +02001104 if( res < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001105 return( (int) size + 20 );
1106
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001107 return( res );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001108}
1109
1110#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001111#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001112
1113#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1114
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001115#define SAFE_SNPRINTF() \
1116{ \
1117 if( ret == -1 ) \
1118 return( -1 ); \
1119 \
Paul Bakker66d5d072014-06-17 16:39:18 +02001120 if( (unsigned int) ret > n ) { \
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001121 p[n - 1] = '\0'; \
1122 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1123 } \
1124 \
1125 n -= (unsigned int) ret; \
1126 p += (unsigned int) ret; \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001127}
1128
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001129static int x509_info_subject_alt_name( char **buf, size_t *size,
1130 const x509_sequence *subject_alt_name )
1131{
1132 size_t i;
1133 size_t n = *size;
1134 char *p = *buf;
1135 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001136 const char *sep = "";
1137 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001138
1139 while( cur != NULL )
1140 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001141 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001142 {
1143 *p = '\0';
1144 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1145 }
1146
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001147 n -= cur->buf.len + sep_len;
1148 for( i = 0; i < sep_len; i++ )
1149 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001150 for( i = 0; i < cur->buf.len; i++ )
1151 *p++ = cur->buf.p[i];
1152
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001153 sep = ", ";
1154 sep_len = 2;
1155
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001156 cur = cur->next;
1157 }
1158
1159 *p = '\0';
1160
1161 *size = n;
1162 *buf = p;
1163
1164 return( 0 );
1165}
1166
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001167#define PRINT_ITEM(i) \
1168 { \
1169 ret = snprintf( p, n, "%s" i, sep ); \
1170 SAFE_SNPRINTF(); \
1171 sep = ", "; \
1172 }
1173
1174#define CERT_TYPE(type,name) \
1175 if( ns_cert_type & type ) \
1176 PRINT_ITEM( name );
1177
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001178static int x509_info_cert_type( char **buf, size_t *size,
1179 unsigned char ns_cert_type )
1180{
1181 int ret;
1182 size_t n = *size;
1183 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001184 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001185
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001186 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1187 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1188 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1189 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1190 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1191 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1192 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1193 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001194
1195 *size = n;
1196 *buf = p;
1197
1198 return( 0 );
1199}
1200
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001201#define KEY_USAGE(code,name) \
1202 if( key_usage & code ) \
1203 PRINT_ITEM( name );
1204
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001205static int x509_info_key_usage( char **buf, size_t *size,
1206 unsigned char key_usage )
1207{
1208 int ret;
1209 size_t n = *size;
1210 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001211 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001212
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001213 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1214 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1215 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1216 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1217 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1218 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1219 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001220
1221 *size = n;
1222 *buf = p;
1223
1224 return( 0 );
1225}
1226
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001227static int x509_info_ext_key_usage( char **buf, size_t *size,
1228 const x509_sequence *extended_key_usage )
1229{
1230 int ret;
1231 const char *desc;
1232 size_t n = *size;
1233 char *p = *buf;
1234 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001235 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001236
1237 while( cur != NULL )
1238 {
1239 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1240 desc = "???";
1241
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001242 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001243 SAFE_SNPRINTF();
1244
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001245 sep = ", ";
1246
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001247 cur = cur->next;
1248 }
1249
1250 *size = n;
1251 *buf = p;
1252
1253 return( 0 );
1254}
1255
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001256/*
1257 * Return an informational string about the certificate.
1258 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001259#define BEFORE_COLON 18
1260#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001261int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001262 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263{
1264 int ret;
1265 size_t n;
1266 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 char key_size_str[BEFORE_COLON];
1268
1269 p = buf;
1270 n = size;
1271
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001272 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 prefix, crt->version );
1274 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001275 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 prefix );
1277 SAFE_SNPRINTF();
1278
Paul Bakker66d5d072014-06-17 16:39:18 +02001279 ret = x509_serial_gets( p, n, &crt->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 SAFE_SNPRINTF();
1281
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001282 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001284 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001285 SAFE_SNPRINTF();
1286
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001287 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001289 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001290 SAFE_SNPRINTF();
1291
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001292 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1294 crt->valid_from.year, crt->valid_from.mon,
1295 crt->valid_from.day, crt->valid_from.hour,
1296 crt->valid_from.min, crt->valid_from.sec );
1297 SAFE_SNPRINTF();
1298
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001299 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001300 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1301 crt->valid_to.year, crt->valid_to.mon,
1302 crt->valid_to.day, crt->valid_to.hour,
1303 crt->valid_to.min, crt->valid_to.sec );
1304 SAFE_SNPRINTF();
1305
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001306 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001307 SAFE_SNPRINTF();
1308
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001309 ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
Manuel Pégourié-Gonnardbf696d02014-06-05 17:07:30 +02001310 crt->sig_md, crt->sig_opts );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 SAFE_SNPRINTF();
1312
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001313 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001314 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1315 pk_get_name( &crt->pk ) ) ) != 0 )
1316 {
1317 return( ret );
1318 }
1319
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001320 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001321 (int) pk_get_size( &crt->pk ) );
1322 SAFE_SNPRINTF();
1323
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001324 /*
1325 * Optional extensions
1326 */
1327
1328 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1329 {
1330 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1331 crt->ca_istrue ? "true" : "false" );
1332 SAFE_SNPRINTF();
1333
1334 if( crt->max_pathlen > 0 )
1335 {
1336 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1337 SAFE_SNPRINTF();
1338 }
1339 }
1340
1341 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1342 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001343 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001344 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001345
1346 if( ( ret = x509_info_subject_alt_name( &p, &n,
1347 &crt->subject_alt_names ) ) != 0 )
1348 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001349 }
1350
1351 if( crt->ext_types & EXT_NS_CERT_TYPE )
1352 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001353 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001354 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001355
1356 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1357 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001358 }
1359
1360 if( crt->ext_types & EXT_KEY_USAGE )
1361 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001362 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001363 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001364
1365 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1366 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001367 }
1368
1369 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1370 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001371 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001372 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001373
1374 if( ( ret = x509_info_ext_key_usage( &p, &n,
1375 &crt->ext_key_usage ) ) != 0 )
1376 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001377 }
1378
1379 ret = snprintf( p, n, "\n" );
1380 SAFE_SNPRINTF();
1381
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001382 return( (int) ( size - n ) );
1383}
1384
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001385#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1386int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1387{
1388 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1389 ( crt->key_usage & usage ) != usage )
1390 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1391
1392 return( 0 );
1393}
1394#endif
1395
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001396#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1397int x509_crt_check_extended_key_usage( const x509_crt *crt,
1398 const char *usage_oid,
1399 size_t usage_len )
1400{
1401 const x509_sequence *cur;
1402
1403 /* Extension is not mandatory, absent means no restriction */
1404 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1405 return( 0 );
1406
1407 /*
1408 * Look for the requested usage (or wildcard ANY) in our list
1409 */
1410 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1411 {
1412 const x509_buf *cur_oid = &cur->buf;
1413
1414 if( cur_oid->len == usage_len &&
1415 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1416 {
1417 return( 0 );
1418 }
1419
1420 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1421 return( 0 );
1422 }
1423
1424 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1425}
Paul Bakker9af723c2014-05-01 13:03:14 +02001426#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001427
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001428#if defined(POLARSSL_X509_CRL_PARSE_C)
1429/*
1430 * Return 1 if the certificate is revoked, or 0 otherwise.
1431 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001432int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001433{
1434 const x509_crl_entry *cur = &crl->entry;
1435
1436 while( cur != NULL && cur->serial.len != 0 )
1437 {
1438 if( crt->serial.len == cur->serial.len &&
1439 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1440 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001441 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001442 return( 1 );
1443 }
1444
1445 cur = cur->next;
1446 }
1447
1448 return( 0 );
1449}
1450
1451/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001452 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001453 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001454static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001455 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001456{
1457 int flags = 0;
1458 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1459 const md_info_t *md_info;
1460
1461 if( ca == NULL )
1462 return( flags );
1463
1464 /*
1465 * TODO: What happens if no CRL is present?
1466 * Suggestion: Revocation state should be unknown if no CRL is present.
1467 * For backwards compatibility this is not yet implemented.
1468 */
1469
1470 while( crl_list != NULL )
1471 {
1472 if( crl_list->version == 0 ||
1473 crl_list->issuer_raw.len != ca->subject_raw.len ||
1474 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1475 crl_list->issuer_raw.len ) != 0 )
1476 {
1477 crl_list = crl_list->next;
1478 continue;
1479 }
1480
1481 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001482 * Check if the CA is configured to sign CRLs
1483 */
1484#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1485 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1486 {
1487 flags |= BADCRL_NOT_TRUSTED;
1488 break;
1489 }
1490#endif
1491
1492 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001493 * Check if CRL is correctly signed by the trusted CA
1494 */
1495 md_info = md_info_from_type( crl_list->sig_md );
1496 if( md_info == NULL )
1497 {
1498 /*
1499 * Cannot check 'unknown' hash
1500 */
1501 flags |= BADCRL_NOT_TRUSTED;
1502 break;
1503 }
1504
1505 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1506
Manuel Pégourié-Gonnard53882022014-06-05 17:53:52 +02001507 if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1508 crl_list->sig_md, hash, md_info->size,
1509 crl_list->sig.p, crl_list->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001510 {
1511 flags |= BADCRL_NOT_TRUSTED;
1512 break;
1513 }
1514
1515 /*
1516 * Check for validity of CRL (Do not drop out)
1517 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001518 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001519 flags |= BADCRL_EXPIRED;
1520
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001521 if( x509_time_future( &crl_list->this_update ) )
1522 flags |= BADCRL_FUTURE;
1523
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001524 /*
1525 * Check if certificate is revoked
1526 */
Paul Bakker66d5d072014-06-17 16:39:18 +02001527 if( x509_crt_revoked( crt, crl_list ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001528 {
1529 flags |= BADCERT_REVOKED;
1530 break;
1531 }
1532
1533 crl_list = crl_list->next;
1534 }
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001535 return( flags );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001536}
1537#endif /* POLARSSL_X509_CRL_PARSE_C */
1538
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001539/*
1540 * Like memcmp, but case-insensitive and always returns -1 if different
1541 */
1542static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001543{
1544 size_t i;
1545 unsigned char diff;
1546 const unsigned char *n1 = s1, *n2 = s2;
1547
1548 for( i = 0; i < len; i++ )
1549 {
1550 diff = n1[i] ^ n2[i];
1551
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001552 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553 continue;
1554
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001555 if( diff == 32 &&
1556 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1557 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1558 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001560 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001561
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001562 return( -1 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001563 }
1564
1565 return( 0 );
1566}
1567
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001568/*
1569 * Return 1 if match, 0 if not
1570 * TODO: inverted return value!
1571 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572static int x509_wildcard_verify( const char *cn, x509_buf *name )
1573{
1574 size_t i;
Paul Bakker14b16c62014-05-28 11:33:54 +02001575 size_t cn_idx = 0, cn_len = strlen( cn );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001576
1577 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1578 return( 0 );
1579
Paul Bakker14b16c62014-05-28 11:33:54 +02001580 for( i = 0; i < cn_len; ++i )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001581 {
1582 if( cn[i] == '.' )
1583 {
1584 cn_idx = i;
1585 break;
1586 }
1587 }
1588
1589 if( cn_idx == 0 )
1590 return( 0 );
1591
Paul Bakker14b16c62014-05-28 11:33:54 +02001592 if( cn_len - cn_idx == name->len - 1 &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001593 x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001594 {
1595 return( 1 );
1596 }
1597
1598 return( 0 );
1599}
1600
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001601/*
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001602 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1603 * variations (but not all).
1604 *
1605 * Return 0 if equal, -1 otherwise.
1606 */
1607static int x509_string_cmp( const x509_buf *a, const x509_buf *b )
1608{
1609 if( a->tag == b->tag &&
1610 a->len == b->len &&
1611 memcmp( a->p, b->p, b->len ) == 0 )
1612 {
1613 return( 0 );
1614 }
1615
1616 if( ( a->tag == ASN1_UTF8_STRING || a->tag == ASN1_PRINTABLE_STRING ) &&
1617 ( b->tag == ASN1_UTF8_STRING || b->tag == ASN1_PRINTABLE_STRING ) &&
1618 a->len == b->len &&
1619 x509_memcasecmp( a->p, b->p, b->len ) == 0 )
1620 {
1621 return( 0 );
1622 }
1623
1624 return( -1 );
1625}
1626
1627/*
1628 * Compare two X.509 Names (aka rdnSequence).
1629 *
1630 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1631 * we sometimes return unequal when the full algorithm would return equal,
1632 * but never the other way. (In particular, we don't do Unicode normalisation
1633 * or space folding.)
1634 *
1635 * Return 0 if equal, -1 otherwise.
1636 */
1637static int x509_name_cmp( const x509_name *a, const x509_name *b )
1638{
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001639 /* Avoid recursion, it might not be optimised by the compiler */
1640 while( a != NULL || b != NULL )
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001641 {
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001642 if( a == NULL || b == NULL )
1643 return( -1 );
1644
1645 /* type */
1646 if( a->oid.tag != b->oid.tag ||
1647 a->oid.len != b->oid.len ||
1648 memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
1649 {
1650 return( -1 );
1651 }
1652
1653 /* value */
1654 if( x509_string_cmp( &a->val, &b->val ) != 0 )
1655 return( -1 );
1656
1657 a = a->next;
1658 b = b->next;
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001659 }
1660
Manuel Pégourié-Gonnardf631bbc2014-11-12 18:35:31 +01001661 /* a == NULL == b */
1662 return( 0 );
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001663}
1664
1665/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001666 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1667 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001668 *
1669 * top means parent is a locally-trusted certificate
1670 * bottom means child is the end entity cert
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001671 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001672static int x509_crt_check_parent( const x509_crt *child,
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001673 const x509_crt *parent,
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001674 int top, int bottom )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001675{
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001676 int need_ca_bit;
1677
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001678 /* Parent must be the issuer */
Manuel Pégourié-Gonnardef9a6ae2014-10-17 12:25:12 +02001679 if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001680 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001681
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001682 /* Parent must have the basicConstraints CA bit set as a general rule */
1683 need_ca_bit = 1;
1684
1685 /* Exception: v1/v2 certificates that are locally trusted. */
1686 if( top && parent->version < 3 )
1687 need_ca_bit = 0;
1688
1689 /* Exception: self-signed end-entity certs that are locally trusted. */
1690 if( top && bottom &&
1691 child->raw.len == parent->raw.len &&
1692 memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1693 {
1694 need_ca_bit = 0;
1695 }
1696
1697 if( need_ca_bit && ! parent->ca_istrue )
1698 return( -1 );
1699
1700#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1701 if( need_ca_bit &&
1702 x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
Manuel Pégourié-Gonnardc4eff162014-06-19 12:18:08 +02001703 {
1704 return( -1 );
1705 }
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001706#endif
1707
1708 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001709}
1710
Paul Bakkerddf26b42013-09-18 13:46:23 +02001711static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001712 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001713 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001714 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001715 void *p_vrfy )
1716{
1717 int ret;
1718 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1719 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1720 const md_info_t *md_info;
1721
Paul Bakker86d0c192013-09-18 11:11:02 +02001722 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001723 *flags |= BADCERT_EXPIRED;
1724
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001725 if( x509_time_future( &child->valid_from ) )
1726 *flags |= BADCERT_FUTURE;
1727
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001728 /*
1729 * Child is the top of the chain. Check against the trust_ca list.
1730 */
1731 *flags |= BADCERT_NOT_TRUSTED;
1732
1733 md_info = md_info_from_type( child->sig_md );
1734 if( md_info == NULL )
1735 {
1736 /*
1737 * Cannot check 'unknown', no need to try any CA
1738 */
1739 trust_ca = NULL;
1740 }
1741 else
1742 md( md_info, child->tbs.p, child->tbs.len, hash );
1743
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001744 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001746 if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001748
1749 /*
1750 * Reduce path_len to check against if top of the chain is
1751 * the same as the trusted CA
1752 */
1753 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1754 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1755 child->issuer_raw.len ) == 0 )
1756 {
1757 check_path_cnt--;
1758 }
1759
1760 if( trust_ca->max_pathlen > 0 &&
1761 trust_ca->max_pathlen < check_path_cnt )
1762 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001763 continue;
1764 }
1765
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001766 if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1767 child->sig_md, hash, md_info->size,
1768 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001769 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001770 continue;
1771 }
1772
1773 /*
1774 * Top of chain is signed by a trusted CA
1775 */
1776 *flags &= ~BADCERT_NOT_TRUSTED;
1777 break;
1778 }
1779
1780 /*
1781 * If top of chain is not the same as the trusted CA send a verify request
1782 * to the callback for any issues with validity and CRL presence for the
1783 * trusted CA certificate.
1784 */
1785 if( trust_ca != NULL &&
1786 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1787 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1788 child->issuer_raw.len ) != 0 ) )
1789 {
1790#if defined(POLARSSL_X509_CRL_PARSE_C)
1791 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001792 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001793#else
1794 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795#endif
1796
Paul Bakker86d0c192013-09-18 11:11:02 +02001797 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001798 ca_flags |= BADCERT_EXPIRED;
1799
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001800 if( x509_time_future( &trust_ca->valid_from ) )
1801 ca_flags |= BADCERT_FUTURE;
1802
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803 if( NULL != f_vrfy )
1804 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001805 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1806 &ca_flags ) ) != 0 )
1807 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001809 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810 }
1811 }
1812
1813 /* Call callback on top cert */
1814 if( NULL != f_vrfy )
1815 {
Paul Bakker66d5d072014-06-17 16:39:18 +02001816 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817 return( ret );
1818 }
1819
1820 *flags |= ca_flags;
1821
1822 return( 0 );
1823}
1824
Paul Bakkerddf26b42013-09-18 13:46:23 +02001825static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001826 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001828 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001829 void *p_vrfy )
1830{
1831 int ret;
1832 int parent_flags = 0;
1833 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001834 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001835 const md_info_t *md_info;
1836
Manuel Pégourié-Gonnardfd6c85c2014-11-20 16:34:20 +01001837 /* path_cnt is 0 for the first intermediate CA */
1838 if( 1 + path_cnt > POLARSSL_X509_MAX_INTERMEDIATE_CA )
1839 {
1840 *flags |= BADCERT_NOT_TRUSTED;
1841 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1842 }
1843
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001844 if( x509_time_expired( &child->valid_to ) )
1845 *flags |= BADCERT_EXPIRED;
1846
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001847 if( x509_time_future( &child->valid_from ) )
1848 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849
1850 md_info = md_info_from_type( child->sig_md );
1851 if( md_info == NULL )
1852 {
1853 /*
1854 * Cannot check 'unknown' hash
1855 */
1856 *flags |= BADCERT_NOT_TRUSTED;
1857 }
1858 else
1859 {
1860 md( md_info, child->tbs.p, child->tbs.len, hash );
1861
Manuel Pégourié-Gonnard46db4b02014-06-05 16:34:18 +02001862 if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1863 child->sig_md, hash, md_info->size,
1864 child->sig.p, child->sig.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001865 {
1866 *flags |= BADCERT_NOT_TRUSTED;
1867 }
1868 }
1869
1870#if defined(POLARSSL_X509_CRL_PARSE_C)
1871 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001872 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001873#endif
1874
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001875 /* Look for a grandparent upwards the chain */
1876 for( grandparent = parent->next;
1877 grandparent != NULL;
1878 grandparent = grandparent->next )
1879 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001880 if( x509_crt_check_parent( parent, grandparent,
1881 0, path_cnt == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001882 break;
1883 }
1884
1885 /* Is our parent part of the chain or at the top? */
1886 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001887 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001888 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1889 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001890 if( ret != 0 )
1891 return( ret );
1892 }
1893 else
1894 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001895 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1896 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001897 if( ret != 0 )
1898 return( ret );
1899 }
1900
1901 /* child is verified to be a child of the parent, call verify callback */
1902 if( NULL != f_vrfy )
1903 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1904 return( ret );
1905
1906 *flags |= parent_flags;
1907
1908 return( 0 );
1909}
1910
1911/*
1912 * Verify the certificate validity
1913 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001914int x509_crt_verify( x509_crt *crt,
1915 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001916 x509_crl *ca_crl,
1917 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001918 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001919 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001920{
1921 size_t cn_len;
1922 int ret;
1923 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001924 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001925 x509_name *name;
1926 x509_sequence *cur = NULL;
1927
1928 *flags = 0;
1929
1930 if( cn != NULL )
1931 {
1932 name = &crt->subject;
1933 cn_len = strlen( cn );
1934
1935 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1936 {
1937 cur = &crt->subject_alt_names;
1938
1939 while( cur != NULL )
1940 {
1941 if( cur->buf.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001942 x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001943 break;
1944
1945 if( cur->buf.len > 2 &&
1946 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1947 x509_wildcard_verify( cn, &cur->buf ) )
1948 break;
1949
1950 cur = cur->next;
1951 }
1952
1953 if( cur == NULL )
1954 *flags |= BADCERT_CN_MISMATCH;
1955 }
1956 else
1957 {
1958 while( name != NULL )
1959 {
1960 if( OID_CMP( OID_AT_CN, &name->oid ) )
1961 {
1962 if( name->val.len == cn_len &&
Manuel Pégourié-Gonnard88421242014-10-17 11:36:18 +02001963 x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001964 break;
1965
1966 if( name->val.len > 2 &&
1967 memcmp( name->val.p, "*.", 2 ) == 0 &&
1968 x509_wildcard_verify( cn, &name->val ) )
1969 break;
1970 }
1971
1972 name = name->next;
1973 }
1974
1975 if( name == NULL )
1976 *flags |= BADCERT_CN_MISMATCH;
1977 }
1978 }
1979
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001980 /* Look for a parent upwards the chain */
1981 for( parent = crt->next; parent != NULL; parent = parent->next )
1982 {
Manuel Pégourié-Gonnardd249b7a2014-06-24 11:49:16 +02001983 if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001984 break;
1985 }
1986
1987 /* Are we part of the chain or at the top? */
1988 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001989 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001990 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1991 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001992 if( ret != 0 )
1993 return( ret );
1994 }
1995 else
1996 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001997 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1998 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001999 if( ret != 0 )
2000 return( ret );
2001 }
2002
2003 if( *flags != 0 )
2004 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
2005
2006 return( 0 );
2007}
2008
2009/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02002010 * Initialize a certificate chain
2011 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002012void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02002013{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002014 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02002015}
2016
2017/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002018 * Unallocate all certificate data
2019 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002020void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002021{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002022 x509_crt *cert_cur = crt;
2023 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002024 x509_name *name_cur;
2025 x509_name *name_prv;
2026 x509_sequence *seq_cur;
2027 x509_sequence *seq_prv;
2028
2029 if( crt == NULL )
2030 return;
2031
2032 do
2033 {
2034 pk_free( &cert_cur->pk );
2035
Manuel Pégourié-Gonnardd1539b12014-06-06 16:42:37 +02002036#if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02002037 polarssl_free( cert_cur->sig_opts );
2038#endif
2039
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002040 name_cur = cert_cur->issuer.next;
2041 while( name_cur != NULL )
2042 {
2043 name_prv = name_cur;
2044 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002045 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002046 polarssl_free( name_prv );
2047 }
2048
2049 name_cur = cert_cur->subject.next;
2050 while( name_cur != NULL )
2051 {
2052 name_prv = name_cur;
2053 name_cur = name_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002054 polarssl_zeroize( name_prv, sizeof( x509_name ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002055 polarssl_free( name_prv );
2056 }
2057
2058 seq_cur = cert_cur->ext_key_usage.next;
2059 while( seq_cur != NULL )
2060 {
2061 seq_prv = seq_cur;
2062 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002063 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002064 polarssl_free( seq_prv );
2065 }
2066
2067 seq_cur = cert_cur->subject_alt_names.next;
2068 while( seq_cur != NULL )
2069 {
2070 seq_prv = seq_cur;
2071 seq_cur = seq_cur->next;
Paul Bakker34617722014-06-13 17:20:13 +02002072 polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002073 polarssl_free( seq_prv );
2074 }
2075
2076 if( cert_cur->raw.p != NULL )
2077 {
Paul Bakker34617722014-06-13 17:20:13 +02002078 polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002079 polarssl_free( cert_cur->raw.p );
2080 }
2081
2082 cert_cur = cert_cur->next;
2083 }
2084 while( cert_cur != NULL );
2085
2086 cert_cur = crt;
2087 do
2088 {
2089 cert_prv = cert_cur;
2090 cert_cur = cert_cur->next;
2091
Paul Bakker34617722014-06-13 17:20:13 +02002092 polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002093 if( cert_prv != crt )
2094 polarssl_free( cert_prv );
2095 }
2096 while( cert_cur != NULL );
2097}
2098
Paul Bakker9af723c2014-05-01 13:03:14 +02002099#endif /* POLARSSL_X509_CRT_PARSE_C */