blob: 85e4ef9d1e183fd126da1501dd732cee4fc5bf31 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate and private key decoding
3 *
Paul Bakker7dc4c442014-02-01 22:50:26 +01004 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25/*
26 * The ITU-T X.509 standard defines a certificate format for PKI.
27 *
28 * http://www.ietf.org/rfc/rfc3279.txt
29 * http://www.ietf.org/rfc/rfc3280.txt
30 *
31 * ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-1v2.asc
32 *
33 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
34 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
35 */
36
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020037#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020039#else
40#include POLARSSL_CONFIG_FILE
41#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
43#if defined(POLARSSL_X509_CRT_PARSE_C)
44
45#include "polarssl/x509_crt.h"
46#include "polarssl/oid.h"
47#if defined(POLARSSL_PEM_PARSE_C)
48#include "polarssl/pem.h"
49#endif
50
Paul Bakker7dc4c442014-02-01 22:50:26 +010051#if defined(POLARSSL_PLATFORM_C)
52#include "polarssl/platform.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053#else
54#define polarssl_malloc malloc
55#define polarssl_free free
56#endif
57
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +010058#if defined(POLARSSL_THREADING_C)
59#include "polarssl/threading.h"
60#endif
61
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062#include <string.h>
63#include <stdlib.h>
Paul Bakkerfa6a6202013-10-28 18:48:30 +010064#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065#include <windows.h>
66#else
67#include <time.h>
68#endif
69
Paul Bakkerfa6a6202013-10-28 18:48:30 +010070#if defined(EFIX64) || defined(EFI32)
71#include <stdio.h>
72#endif
73
Paul Bakker7c6b2c32013-09-16 13:49:26 +020074#if defined(POLARSSL_FS_IO)
75#include <stdio.h>
Paul Bakker5ff3f912014-04-04 15:08:20 +020076#if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077#include <sys/types.h>
78#include <sys/stat.h>
79#include <dirent.h>
80#endif
81#endif
82
83/*
84 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
85 */
86static int x509_get_version( unsigned char **p,
87 const unsigned char *end,
88 int *ver )
89{
90 int ret;
91 size_t len;
92
93 if( ( ret = asn1_get_tag( p, end, &len,
94 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
95 {
96 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
97 {
98 *ver = 0;
99 return( 0 );
100 }
101
102 return( ret );
103 }
104
105 end = *p + len;
106
107 if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200108 return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
110 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200111 return( POLARSSL_ERR_X509_INVALID_VERSION +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
113
114 return( 0 );
115}
116
117/*
118 * Validity ::= SEQUENCE {
119 * notBefore Time,
120 * notAfter Time }
121 */
122static int x509_get_dates( unsigned char **p,
123 const unsigned char *end,
124 x509_time *from,
125 x509_time *to )
126{
127 int ret;
128 size_t len;
129
130 if( ( ret = asn1_get_tag( p, end, &len,
131 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200132 return( POLARSSL_ERR_X509_INVALID_DATE + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200133
134 end = *p + len;
135
136 if( ( ret = x509_get_time( p, end, from ) ) != 0 )
137 return( ret );
138
139 if( ( ret = x509_get_time( p, end, to ) ) != 0 )
140 return( ret );
141
142 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200143 return( POLARSSL_ERR_X509_INVALID_DATE +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
145
146 return( 0 );
147}
148
149/*
150 * X.509 v2/v3 unique identifier (not parsed)
151 */
152static int x509_get_uid( unsigned char **p,
153 const unsigned char *end,
154 x509_buf *uid, int n )
155{
156 int ret;
157
158 if( *p == end )
159 return( 0 );
160
161 uid->tag = **p;
162
163 if( ( ret = asn1_get_tag( p, end, &uid->len,
164 ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
165 {
166 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
167 return( 0 );
168
169 return( ret );
170 }
171
172 uid->p = *p;
173 *p += uid->len;
174
175 return( 0 );
176}
177
178static int x509_get_basic_constraints( unsigned char **p,
179 const unsigned char *end,
180 int *ca_istrue,
181 int *max_pathlen )
182{
183 int ret;
184 size_t len;
185
186 /*
187 * BasicConstraints ::= SEQUENCE {
188 * cA BOOLEAN DEFAULT FALSE,
189 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
190 */
191 *ca_istrue = 0; /* DEFAULT FALSE */
192 *max_pathlen = 0; /* endless */
193
194 if( ( ret = asn1_get_tag( p, end, &len,
195 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200196 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200197
198 if( *p == end )
199 return 0;
200
201 if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
202 {
203 if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
204 ret = asn1_get_int( p, end, ca_istrue );
205
206 if( ret != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200207 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
209 if( *ca_istrue != 0 )
210 *ca_istrue = 1;
211 }
212
213 if( *p == end )
214 return 0;
215
216 if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200217 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200218
219 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200220 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
222
223 (*max_pathlen)++;
224
225 return 0;
226}
227
228static int x509_get_ns_cert_type( unsigned char **p,
229 const unsigned char *end,
230 unsigned char *ns_cert_type)
231{
232 int ret;
233 x509_bitstring bs = { 0, 0, NULL };
234
235 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200236 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237
238 if( bs.len != 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200239 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240 POLARSSL_ERR_ASN1_INVALID_LENGTH );
241
242 /* Get actual bitstring */
243 *ns_cert_type = *bs.p;
244 return 0;
245}
246
247static int x509_get_key_usage( unsigned char **p,
248 const unsigned char *end,
249 unsigned char *key_usage)
250{
251 int ret;
252 x509_bitstring bs = { 0, 0, NULL };
253
254 if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200255 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256
257 if( bs.len < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200258 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259 POLARSSL_ERR_ASN1_INVALID_LENGTH );
260
261 /* Get actual bitstring */
262 *key_usage = *bs.p;
263 return 0;
264}
265
266/*
267 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
268 *
269 * KeyPurposeId ::= OBJECT IDENTIFIER
270 */
271static int x509_get_ext_key_usage( unsigned char **p,
272 const unsigned char *end,
273 x509_sequence *ext_key_usage)
274{
275 int ret;
276
277 if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200278 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279
280 /* Sequence length must be >= 1 */
281 if( ext_key_usage->buf.p == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200282 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 POLARSSL_ERR_ASN1_INVALID_LENGTH );
284
285 return 0;
286}
287
288/*
289 * SubjectAltName ::= GeneralNames
290 *
291 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
292 *
293 * GeneralName ::= CHOICE {
294 * otherName [0] OtherName,
295 * rfc822Name [1] IA5String,
296 * dNSName [2] IA5String,
297 * x400Address [3] ORAddress,
298 * directoryName [4] Name,
299 * ediPartyName [5] EDIPartyName,
300 * uniformResourceIdentifier [6] IA5String,
301 * iPAddress [7] OCTET STRING,
302 * registeredID [8] OBJECT IDENTIFIER }
303 *
304 * OtherName ::= SEQUENCE {
305 * type-id OBJECT IDENTIFIER,
306 * value [0] EXPLICIT ANY DEFINED BY type-id }
307 *
308 * EDIPartyName ::= SEQUENCE {
309 * nameAssigner [0] DirectoryString OPTIONAL,
310 * partyName [1] DirectoryString }
311 *
312 * NOTE: PolarSSL only parses and uses dNSName at this point.
313 */
314static int x509_get_subject_alt_name( unsigned char **p,
315 const unsigned char *end,
316 x509_sequence *subject_alt_name )
317{
318 int ret;
319 size_t len, tag_len;
320 asn1_buf *buf;
321 unsigned char tag;
322 asn1_sequence *cur = subject_alt_name;
323
324 /* Get main sequence tag */
325 if( ( ret = asn1_get_tag( p, end, &len,
326 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200327 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328
329 if( *p + len != end )
Paul Bakker51876562013-09-17 14:36:05 +0200330 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
332
333 while( *p < end )
334 {
335 if( ( end - *p ) < 1 )
Paul Bakker51876562013-09-17 14:36:05 +0200336 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337 POLARSSL_ERR_ASN1_OUT_OF_DATA );
338
339 tag = **p;
340 (*p)++;
341 if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
Paul Bakker51876562013-09-17 14:36:05 +0200342 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343
344 if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
Paul Bakker51876562013-09-17 14:36:05 +0200345 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346 POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
347
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200348 /* Skip everything but DNS name */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200349 if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
350 {
351 *p += tag_len;
352 continue;
353 }
354
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355 /* Allocate and assign next pointer */
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +0200356 if( cur->buf.p != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 {
358 cur->next = (asn1_sequence *) polarssl_malloc(
359 sizeof( asn1_sequence ) );
360
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 Bakker51876562013-09-17 14:36:05 +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
477 crt->ext_types |= ext_type;
478
479 switch( ext_type )
480 {
481 case EXT_BASIC_CONSTRAINTS:
482 /* Parse basic constraints */
483 if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
484 &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
485 return ( ret );
486 break;
487
488 case EXT_KEY_USAGE:
489 /* Parse key usage */
490 if( ( ret = x509_get_key_usage( p, end_ext_octet,
491 &crt->key_usage ) ) != 0 )
492 return ( ret );
493 break;
494
495 case EXT_EXTENDED_KEY_USAGE:
496 /* Parse extended key usage */
497 if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
498 &crt->ext_key_usage ) ) != 0 )
499 return ( ret );
500 break;
501
502 case EXT_SUBJECT_ALT_NAME:
503 /* Parse subject alt name */
504 if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
505 &crt->subject_alt_names ) ) != 0 )
506 return ( ret );
507 break;
508
509 case EXT_NS_CERT_TYPE:
510 /* Parse netscape certificate type */
511 if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
512 &crt->ns_cert_type ) ) != 0 )
513 return ( ret );
514 break;
515
516 default:
517 return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
518 }
519 }
520
521 if( *p != end )
Paul Bakker51876562013-09-17 14:36:05 +0200522 return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
524
525 return( 0 );
526}
527
528/*
529 * Parse and fill a single X.509 certificate in DER format
530 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200531static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200532 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533{
534 int ret;
535 size_t len;
536 unsigned char *p, *end, *crt_end;
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100537 x509_buf sig_params;
538
539 memset( &sig_params, 0, sizeof( x509_buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
541 /*
542 * Check for valid input
543 */
544 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200545 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546
547 p = (unsigned char *) polarssl_malloc( len = buflen );
548
549 if( p == NULL )
550 return( POLARSSL_ERR_X509_MALLOC_FAILED );
551
552 memcpy( p, buf, buflen );
553
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554 crt->raw.p = p;
555 crt->raw.len = len;
556 end = p + len;
557
558 /*
559 * Certificate ::= SEQUENCE {
560 * tbsCertificate TBSCertificate,
561 * signatureAlgorithm AlgorithmIdentifier,
562 * signatureValue BIT STRING }
563 */
564 if( ( ret = asn1_get_tag( &p, end, &len,
565 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
566 {
567 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200568 return( POLARSSL_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200569 }
570
571 if( len > (size_t) ( end - p ) )
572 {
573 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200574 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
576 }
577 crt_end = p + len;
578
579 /*
580 * TBSCertificate ::= SEQUENCE {
581 */
582 crt->tbs.p = p;
583
584 if( ( ret = asn1_get_tag( &p, end, &len,
585 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
586 {
587 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200588 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200589 }
590
591 end = p + len;
592 crt->tbs.len = end - crt->tbs.p;
593
594 /*
595 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
596 *
597 * CertificateSerialNumber ::= INTEGER
598 *
599 * signature AlgorithmIdentifier
600 */
601 if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
602 ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100603 ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
604 &crt->sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605 {
606 x509_crt_free( crt );
607 return( ret );
608 }
609
610 crt->version++;
611
612 if( crt->version > 3 )
613 {
614 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200615 return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200616 }
617
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +0100618 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
619 &crt->sig_pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620 {
621 x509_crt_free( crt );
622 return( ret );
623 }
624
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100625 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
626 {
627 int salt_len, trailer_field;
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100628 md_type_t mgf_md;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100629
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +0100630 /* Make sure params are valid */
631 ret = x509_get_rsassa_pss_params( &crt->sig_params,
632 &crt->sig_md, &mgf_md, &salt_len, &trailer_field );
633 if( ret != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100634 return( ret );
635 }
636 else
637 {
638 /* Make sure parameters were absent or NULL */
639 if( ( crt->sig_params.tag != ASN1_NULL && crt->sig_params.tag != 0 ) ||
640 crt->sig_params.len != 0 )
641 return( POLARSSL_ERR_X509_INVALID_ALG );
642 }
643
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644 /*
645 * issuer Name
646 */
647 crt->issuer_raw.p = p;
648
649 if( ( ret = asn1_get_tag( &p, end, &len,
650 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
651 {
652 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200653 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654 }
655
656 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
657 {
658 x509_crt_free( crt );
659 return( ret );
660 }
661
662 crt->issuer_raw.len = p - crt->issuer_raw.p;
663
664 /*
665 * Validity ::= SEQUENCE {
666 * notBefore Time,
667 * notAfter Time }
668 *
669 */
670 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
671 &crt->valid_to ) ) != 0 )
672 {
673 x509_crt_free( crt );
674 return( ret );
675 }
676
677 /*
678 * subject Name
679 */
680 crt->subject_raw.p = p;
681
682 if( ( ret = asn1_get_tag( &p, end, &len,
683 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
684 {
685 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200686 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 }
688
689 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
690 {
691 x509_crt_free( crt );
692 return( ret );
693 }
694
695 crt->subject_raw.len = p - crt->subject_raw.p;
696
697 /*
698 * SubjectPublicKeyInfo
699 */
Paul Bakkerda771152013-09-16 22:45:03 +0200700 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701 {
702 x509_crt_free( crt );
703 return( ret );
704 }
705
706 /*
707 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
708 * -- If present, version shall be v2 or v3
709 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
710 * -- If present, version shall be v2 or v3
711 * extensions [3] EXPLICIT Extensions OPTIONAL
712 * -- If present, version shall be v3
713 */
714 if( crt->version == 2 || crt->version == 3 )
715 {
716 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
717 if( ret != 0 )
718 {
719 x509_crt_free( crt );
720 return( ret );
721 }
722 }
723
724 if( crt->version == 2 || crt->version == 3 )
725 {
726 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
727 if( ret != 0 )
728 {
729 x509_crt_free( crt );
730 return( ret );
731 }
732 }
733
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200734#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 if( crt->version == 3 )
736 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200737#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 ret = x509_get_crt_ext( &p, end, crt);
739 if( ret != 0 )
740 {
741 x509_crt_free( crt );
742 return( ret );
743 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200744#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200746#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200747
748 if( p != end )
749 {
750 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200751 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200752 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
753 }
754
755 end = crt_end;
756
757 /*
758 * }
759 * -- end of TBSCertificate
760 *
761 * signatureAlgorithm AlgorithmIdentifier,
762 * signatureValue BIT STRING
763 */
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100764 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765 {
766 x509_crt_free( crt );
767 return( ret );
768 }
769
770 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100771 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
772 crt->sig_params.len != sig_params.len ||
773 memcmp( crt->sig_params.p, sig_params.p, sig_params.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 {
775 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200776 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777 }
778
779 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
780 {
781 x509_crt_free( crt );
782 return( ret );
783 }
784
785 if( p != end )
786 {
787 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200788 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200789 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
790 }
791
792 return( 0 );
793}
794
795/*
796 * Parse one X.509 certificate in DER format from a buffer and add them to a
797 * chained list
798 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200799int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200800 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801{
802 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200803 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804
805 /*
806 * Check for valid input
807 */
808 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200809 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200810
811 while( crt->version != 0 && crt->next != NULL )
812 {
813 prev = crt;
814 crt = crt->next;
815 }
816
817 /*
818 * Add new certificate on the end of the chain if needed.
819 */
820 if ( crt->version != 0 && crt->next == NULL)
821 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200822 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823
824 if( crt->next == NULL )
825 return( POLARSSL_ERR_X509_MALLOC_FAILED );
826
827 prev = crt;
828 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200829 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 }
831
Paul Bakkerddf26b42013-09-18 13:46:23 +0200832 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200833 {
834 if( prev )
835 prev->next = NULL;
836
837 if( crt != chain )
838 polarssl_free( crt );
839
840 return( ret );
841 }
842
843 return( 0 );
844}
845
846/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200847 * Parse one or more PEM certificates from a buffer and add them to the chained
848 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200849 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200850int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200851{
852 int success = 0, first_error = 0, total_failed = 0;
853 int buf_format = X509_FORMAT_DER;
854
855 /*
856 * Check for valid input
857 */
858 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200859 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200860
861 /*
862 * Determine buffer content. Buffer contains either one DER certificate or
863 * one or more PEM certificates.
864 */
865#if defined(POLARSSL_PEM_PARSE_C)
866 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
867 buf_format = X509_FORMAT_PEM;
868#endif
869
870 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200871 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200872
873#if defined(POLARSSL_PEM_PARSE_C)
874 if( buf_format == X509_FORMAT_PEM )
875 {
876 int ret;
877 pem_context pem;
878
879 while( buflen > 0 )
880 {
881 size_t use_len;
882 pem_init( &pem );
883
884 ret = pem_read_buffer( &pem,
885 "-----BEGIN CERTIFICATE-----",
886 "-----END CERTIFICATE-----",
887 buf, NULL, 0, &use_len );
888
889 if( ret == 0 )
890 {
891 /*
892 * Was PEM encoded
893 */
894 buflen -= use_len;
895 buf += use_len;
896 }
897 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
898 {
899 return( ret );
900 }
901 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
902 {
903 pem_free( &pem );
904
905 /*
906 * PEM header and footer were found
907 */
908 buflen -= use_len;
909 buf += use_len;
910
911 if( first_error == 0 )
912 first_error = ret;
913
914 continue;
915 }
916 else
917 break;
918
Paul Bakkerddf26b42013-09-18 13:46:23 +0200919 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200920
921 pem_free( &pem );
922
923 if( ret != 0 )
924 {
925 /*
926 * Quit parsing on a memory error
927 */
928 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
929 return( ret );
930
931 if( first_error == 0 )
932 first_error = ret;
933
934 total_failed++;
935 continue;
936 }
937
938 success = 1;
939 }
940 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200941#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200942
943 if( success )
944 return( total_failed );
945 else if( first_error )
946 return( first_error );
947 else
948 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
949}
950
951#if defined(POLARSSL_FS_IO)
952/*
953 * Load one or more certificates and add them to the chained list
954 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200955int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200956{
957 int ret;
958 size_t n;
959 unsigned char *buf;
960
961 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
962 return( ret );
963
Paul Bakkerddf26b42013-09-18 13:46:23 +0200964 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200965
966 memset( buf, 0, n + 1 );
967 polarssl_free( buf );
968
969 return( ret );
970}
971
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100972#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100973static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
974#endif
975
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200976int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977{
978 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100979#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200980 int w_ret;
981 WCHAR szDir[MAX_PATH];
982 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200983 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200984 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985
Paul Bakker9af723c2014-05-01 13:03:14 +0200986 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 HANDLE hFind;
988
989 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200990 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
Paul Bakker9af723c2014-05-01 13:03:14 +0200992 memset( szDir, 0, sizeof(szDir) );
993 memset( filename, 0, MAX_PATH );
994 memcpy( filename, path, len );
995 filename[len++] = '\\';
996 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200997 filename[len++] = '*';
998
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200999 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
1000 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001
1002 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +02001003 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001004 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1005
1006 len = MAX_PATH - len;
1007 do
1008 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001009 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010
1011 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1012 continue;
1013
Paul Bakker9af723c2014-05-01 13:03:14 +02001014 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1015 lstrlenW(file_data.cFileName),
1016 p, len - 1,
1017 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018
Paul Bakkerddf26b42013-09-18 13:46:23 +02001019 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 if( w_ret < 0 )
1021 ret++;
1022 else
1023 ret += w_ret;
1024 }
1025 while( FindNextFileW( hFind, &file_data ) != 0 );
1026
Paul Bakker4aa40d42013-10-11 10:49:24 +02001027 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1029
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001031#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001032 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001033 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001034 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001035 char entry_name[255];
1036 DIR *dir = opendir( path );
1037
1038 if( dir == NULL)
1039 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1040
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001041#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001042 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1043 return( ret );
1044#endif
1045
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001046 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001047 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001048 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001049
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001050 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001051 {
1052 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001053 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1054 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001055 }
1056
1057 if( !S_ISREG( sb.st_mode ) )
1058 continue;
1059
1060 // Ignore parse errors
1061 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001062 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001063 if( t_ret < 0 )
1064 ret++;
1065 else
1066 ret += t_ret;
1067 }
1068 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001069
1070cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001071#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001072 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1073 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1074#endif
1075
Paul Bakkerbe089b02013-10-14 15:51:50 +02001076#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001077
1078 return( ret );
1079}
1080#endif /* POLARSSL_FS_IO */
1081
Paul Bakker6edcd412013-10-29 15:22:54 +01001082#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1083 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001084#include <stdarg.h>
1085
1086#if !defined vsnprintf
1087#define vsnprintf _vsnprintf
1088#endif // vsnprintf
1089
1090/*
1091 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1092 * Result value is not size of buffer needed, but -1 if no fit is possible.
1093 *
1094 * This fuction tries to 'fix' this by at least suggesting enlarging the
1095 * size by 20.
1096 */
1097static int compat_snprintf(char *str, size_t size, const char *format, ...)
1098{
1099 va_list ap;
1100 int res = -1;
1101
1102 va_start( ap, format );
1103
1104 res = vsnprintf( str, size, format, ap );
1105
1106 va_end( ap );
1107
1108 // No quick fix possible
1109 if ( res < 0 )
1110 return( (int) size + 20 );
1111
1112 return res;
1113}
1114
1115#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001116#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001117
1118#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1119
1120#define SAFE_SNPRINTF() \
1121{ \
1122 if( ret == -1 ) \
1123 return( -1 ); \
1124 \
1125 if ( (unsigned int) ret > n ) { \
1126 p[n - 1] = '\0'; \
1127 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1128 } \
1129 \
1130 n -= (unsigned int) ret; \
1131 p += (unsigned int) ret; \
1132}
1133
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001134static int x509_info_subject_alt_name( char **buf, size_t *size,
1135 const x509_sequence *subject_alt_name )
1136{
1137 size_t i;
1138 size_t n = *size;
1139 char *p = *buf;
1140 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001141 const char *sep = "";
1142 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001143
1144 while( cur != NULL )
1145 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001146 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001147 {
1148 *p = '\0';
1149 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1150 }
1151
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001152 n -= cur->buf.len + sep_len;
1153 for( i = 0; i < sep_len; i++ )
1154 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001155 for( i = 0; i < cur->buf.len; i++ )
1156 *p++ = cur->buf.p[i];
1157
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001158 sep = ", ";
1159 sep_len = 2;
1160
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001161 cur = cur->next;
1162 }
1163
1164 *p = '\0';
1165
1166 *size = n;
1167 *buf = p;
1168
1169 return( 0 );
1170}
1171
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001172#define PRINT_ITEM(i) \
1173 { \
1174 ret = snprintf( p, n, "%s" i, sep ); \
1175 SAFE_SNPRINTF(); \
1176 sep = ", "; \
1177 }
1178
1179#define CERT_TYPE(type,name) \
1180 if( ns_cert_type & type ) \
1181 PRINT_ITEM( name );
1182
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001183static int x509_info_cert_type( char **buf, size_t *size,
1184 unsigned char ns_cert_type )
1185{
1186 int ret;
1187 size_t n = *size;
1188 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001189 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001190
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001191 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1192 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1193 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1194 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1195 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1196 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1197 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1198 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001199
1200 *size = n;
1201 *buf = p;
1202
1203 return( 0 );
1204}
1205
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001206#define KEY_USAGE(code,name) \
1207 if( key_usage & code ) \
1208 PRINT_ITEM( name );
1209
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001210static int x509_info_key_usage( char **buf, size_t *size,
1211 unsigned char key_usage )
1212{
1213 int ret;
1214 size_t n = *size;
1215 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001216 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001217
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001218 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1219 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1220 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1221 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1222 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1223 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1224 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001225
1226 *size = n;
1227 *buf = p;
1228
1229 return( 0 );
1230}
1231
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001232static int x509_info_ext_key_usage( char **buf, size_t *size,
1233 const x509_sequence *extended_key_usage )
1234{
1235 int ret;
1236 const char *desc;
1237 size_t n = *size;
1238 char *p = *buf;
1239 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001240 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001241
1242 while( cur != NULL )
1243 {
1244 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1245 desc = "???";
1246
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001247 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001248 SAFE_SNPRINTF();
1249
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001250 sep = ", ";
1251
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001252 cur = cur->next;
1253 }
1254
1255 *size = n;
1256 *buf = p;
1257
1258 return( 0 );
1259}
1260
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001261/*
1262 * Return an informational string about the certificate.
1263 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001264#define BEFORE_COLON 18
1265#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001266int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001267 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268{
1269 int ret;
1270 size_t n;
1271 char *p;
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001272 const char *desc = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001273 char key_size_str[BEFORE_COLON];
1274
1275 p = buf;
1276 n = size;
1277
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001278 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 prefix, crt->version );
1280 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001281 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001282 prefix );
1283 SAFE_SNPRINTF();
1284
Paul Bakker86d0c192013-09-18 11:11:02 +02001285 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 SAFE_SNPRINTF();
1287
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001288 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001289 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001290 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 SAFE_SNPRINTF();
1292
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001293 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001295 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 SAFE_SNPRINTF();
1297
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001298 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001299 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1300 crt->valid_from.year, crt->valid_from.mon,
1301 crt->valid_from.day, crt->valid_from.hour,
1302 crt->valid_from.min, crt->valid_from.sec );
1303 SAFE_SNPRINTF();
1304
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001305 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001306 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1307 crt->valid_to.year, crt->valid_to.mon,
1308 crt->valid_to.day, crt->valid_to.hour,
1309 crt->valid_to.min, crt->valid_to.sec );
1310 SAFE_SNPRINTF();
1311
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001312 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001313 SAFE_SNPRINTF();
1314
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001315 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1316 if( ret != 0 )
1317 ret = snprintf( p, n, "???" );
1318 else
1319 ret = snprintf( p, n, "%s", desc );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001320 SAFE_SNPRINTF();
1321
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001322 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
1323 {
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +01001324 md_type_t md_alg, mgf_md;
1325 const md_info_t *md_info, *mgf_md_info;
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001326 int salt_len, trailer_field;
1327
1328 if( ( ret = x509_get_rsassa_pss_params( &crt->sig_params,
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +01001329 &md_alg, &mgf_md, &salt_len, &trailer_field ) ) != 0 )
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001330 return( ret );
1331
Manuel Pégourié-Gonnarde76b7502014-01-23 19:15:29 +01001332 md_info = md_info_from_type( md_alg );
1333 mgf_md_info = md_info_from_type( mgf_md );
1334
1335 ret = snprintf( p, n, " (%s, MGF1-%s, 0x%02X, %d)",
1336 md_info ? md_info->name : "???",
1337 mgf_md_info ? mgf_md_info->name : "???",
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001338 salt_len, trailer_field );
1339 SAFE_SNPRINTF();
1340 }
1341
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001342 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001343 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1344 pk_get_name( &crt->pk ) ) ) != 0 )
1345 {
1346 return( ret );
1347 }
1348
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001349 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001350 (int) pk_get_size( &crt->pk ) );
1351 SAFE_SNPRINTF();
1352
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001353 /*
1354 * Optional extensions
1355 */
1356
1357 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1358 {
1359 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1360 crt->ca_istrue ? "true" : "false" );
1361 SAFE_SNPRINTF();
1362
1363 if( crt->max_pathlen > 0 )
1364 {
1365 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1366 SAFE_SNPRINTF();
1367 }
1368 }
1369
1370 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1371 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001372 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001373 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001374
1375 if( ( ret = x509_info_subject_alt_name( &p, &n,
1376 &crt->subject_alt_names ) ) != 0 )
1377 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001378 }
1379
1380 if( crt->ext_types & EXT_NS_CERT_TYPE )
1381 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001382 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001383 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001384
1385 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1386 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001387 }
1388
1389 if( crt->ext_types & EXT_KEY_USAGE )
1390 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001391 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001392 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001393
1394 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1395 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001396 }
1397
1398 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1399 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001400 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001401 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001402
1403 if( ( ret = x509_info_ext_key_usage( &p, &n,
1404 &crt->ext_key_usage ) ) != 0 )
1405 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001406 }
1407
1408 ret = snprintf( p, n, "\n" );
1409 SAFE_SNPRINTF();
1410
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001411 return( (int) ( size - n ) );
1412}
1413
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001414#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1415int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1416{
1417 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1418 ( crt->key_usage & usage ) != usage )
1419 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1420
1421 return( 0 );
1422}
1423#endif
1424
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001425#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1426int x509_crt_check_extended_key_usage( const x509_crt *crt,
1427 const char *usage_oid,
1428 size_t usage_len )
1429{
1430 const x509_sequence *cur;
1431
1432 /* Extension is not mandatory, absent means no restriction */
1433 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1434 return( 0 );
1435
1436 /*
1437 * Look for the requested usage (or wildcard ANY) in our list
1438 */
1439 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1440 {
1441 const x509_buf *cur_oid = &cur->buf;
1442
1443 if( cur_oid->len == usage_len &&
1444 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1445 {
1446 return( 0 );
1447 }
1448
1449 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1450 return( 0 );
1451 }
1452
1453 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1454}
Paul Bakker9af723c2014-05-01 13:03:14 +02001455#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001456
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001457#if defined(POLARSSL_X509_CRL_PARSE_C)
1458/*
1459 * Return 1 if the certificate is revoked, or 0 otherwise.
1460 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001461int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001462{
1463 const x509_crl_entry *cur = &crl->entry;
1464
1465 while( cur != NULL && cur->serial.len != 0 )
1466 {
1467 if( crt->serial.len == cur->serial.len &&
1468 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1469 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001470 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001471 return( 1 );
1472 }
1473
1474 cur = cur->next;
1475 }
1476
1477 return( 0 );
1478}
1479
1480/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001481 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001482 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001483static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001484 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001485{
1486 int flags = 0;
1487 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1488 const md_info_t *md_info;
1489
1490 if( ca == NULL )
1491 return( flags );
1492
1493 /*
1494 * TODO: What happens if no CRL is present?
1495 * Suggestion: Revocation state should be unknown if no CRL is present.
1496 * For backwards compatibility this is not yet implemented.
1497 */
1498
1499 while( crl_list != NULL )
1500 {
1501 if( crl_list->version == 0 ||
1502 crl_list->issuer_raw.len != ca->subject_raw.len ||
1503 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1504 crl_list->issuer_raw.len ) != 0 )
1505 {
1506 crl_list = crl_list->next;
1507 continue;
1508 }
1509
1510 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001511 * Check if the CA is configured to sign CRLs
1512 */
1513#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1514 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1515 {
1516 flags |= BADCRL_NOT_TRUSTED;
1517 break;
1518 }
1519#endif
1520
1521 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001522 * Check if CRL is correctly signed by the trusted CA
1523 */
1524 md_info = md_info_from_type( crl_list->sig_md );
1525 if( md_info == NULL )
1526 {
1527 /*
1528 * Cannot check 'unknown' hash
1529 */
1530 flags |= BADCRL_NOT_TRUSTED;
1531 break;
1532 }
1533
1534 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1535
1536 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1537 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1538 crl_list->sig.p, crl_list->sig.len ) != 0 )
1539 {
1540 flags |= BADCRL_NOT_TRUSTED;
1541 break;
1542 }
1543
1544 /*
1545 * Check for validity of CRL (Do not drop out)
1546 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001547 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001548 flags |= BADCRL_EXPIRED;
1549
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001550 if( x509_time_future( &crl_list->this_update ) )
1551 flags |= BADCRL_FUTURE;
1552
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001553 /*
1554 * Check if certificate is revoked
1555 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001556 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557 {
1558 flags |= BADCERT_REVOKED;
1559 break;
1560 }
1561
1562 crl_list = crl_list->next;
1563 }
1564 return flags;
1565}
1566#endif /* POLARSSL_X509_CRL_PARSE_C */
1567
1568// Equal == 0, inequal == 1
1569static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1570{
1571 size_t i;
1572 unsigned char diff;
1573 const unsigned char *n1 = s1, *n2 = s2;
1574
1575 for( i = 0; i < len; i++ )
1576 {
1577 diff = n1[i] ^ n2[i];
1578
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001579 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580 continue;
1581
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001582 if( diff == 32 &&
1583 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1584 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1585 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001586 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001587 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001588
1589 return( 1 );
1590 }
1591
1592 return( 0 );
1593}
1594
1595static int x509_wildcard_verify( const char *cn, x509_buf *name )
1596{
1597 size_t i;
1598 size_t cn_idx = 0;
1599
1600 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1601 return( 0 );
1602
1603 for( i = 0; i < strlen( cn ); ++i )
1604 {
1605 if( cn[i] == '.' )
1606 {
1607 cn_idx = i;
1608 break;
1609 }
1610 }
1611
1612 if( cn_idx == 0 )
1613 return( 0 );
1614
1615 if( strlen( cn ) - cn_idx == name->len - 1 &&
1616 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1617 {
1618 return( 1 );
1619 }
1620
1621 return( 0 );
1622}
1623
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001624/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001625 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1626 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001627 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001628static int x509_crt_check_parent( const x509_crt *child,
1629 const x509_crt *parent )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001630{
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001631 if( parent->version == 0 ||
1632 parent->ca_istrue == 0 ||
1633 child->issuer_raw.len != parent->subject_raw.len ||
1634 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1635 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001636 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001637 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001638 }
1639
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001640#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1641 if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1642 return( -1 );
1643#endif
1644
1645 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001646}
1647
Paul Bakkerddf26b42013-09-18 13:46:23 +02001648static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001649 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001650 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001651 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 void *p_vrfy )
1653{
1654 int ret;
1655 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1656 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1657 const md_info_t *md_info;
1658
Paul Bakker86d0c192013-09-18 11:11:02 +02001659 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001660 *flags |= BADCERT_EXPIRED;
1661
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001662 if( x509_time_future( &child->valid_from ) )
1663 *flags |= BADCERT_FUTURE;
1664
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001665 /*
1666 * Child is the top of the chain. Check against the trust_ca list.
1667 */
1668 *flags |= BADCERT_NOT_TRUSTED;
1669
1670 md_info = md_info_from_type( child->sig_md );
1671 if( md_info == NULL )
1672 {
1673 /*
1674 * Cannot check 'unknown', no need to try any CA
1675 */
1676 trust_ca = NULL;
1677 }
1678 else
1679 md( md_info, child->tbs.p, child->tbs.len, hash );
1680
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001681 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001682 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001683 if( x509_crt_check_parent( child, trust_ca ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001684 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001685
1686 /*
1687 * Reduce path_len to check against if top of the chain is
1688 * the same as the trusted CA
1689 */
1690 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1691 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1692 child->issuer_raw.len ) == 0 )
1693 {
1694 check_path_cnt--;
1695 }
1696
1697 if( trust_ca->max_pathlen > 0 &&
1698 trust_ca->max_pathlen < check_path_cnt )
1699 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001700 continue;
1701 }
1702
1703 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1704 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1705 child->sig.p, child->sig.len ) != 0 )
1706 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001707 continue;
1708 }
1709
1710 /*
1711 * Top of chain is signed by a trusted CA
1712 */
1713 *flags &= ~BADCERT_NOT_TRUSTED;
1714 break;
1715 }
1716
1717 /*
1718 * If top of chain is not the same as the trusted CA send a verify request
1719 * to the callback for any issues with validity and CRL presence for the
1720 * trusted CA certificate.
1721 */
1722 if( trust_ca != NULL &&
1723 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1724 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1725 child->issuer_raw.len ) != 0 ) )
1726 {
1727#if defined(POLARSSL_X509_CRL_PARSE_C)
1728 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001729 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001730#else
1731 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732#endif
1733
Paul Bakker86d0c192013-09-18 11:11:02 +02001734 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001735 ca_flags |= BADCERT_EXPIRED;
1736
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001737 if( x509_time_future( &trust_ca->valid_from ) )
1738 ca_flags |= BADCERT_FUTURE;
1739
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740 if( NULL != f_vrfy )
1741 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001742 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1743 &ca_flags ) ) != 0 )
1744 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001745 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001746 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001747 }
1748 }
1749
1750 /* Call callback on top cert */
1751 if( NULL != f_vrfy )
1752 {
1753 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1754 return( ret );
1755 }
1756
1757 *flags |= ca_flags;
1758
1759 return( 0 );
1760}
1761
Paul Bakkerddf26b42013-09-18 13:46:23 +02001762static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001763 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001765 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001766 void *p_vrfy )
1767{
1768 int ret;
1769 int parent_flags = 0;
1770 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001771 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001772 const md_info_t *md_info;
1773
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001774 if( x509_time_expired( &child->valid_to ) )
1775 *flags |= BADCERT_EXPIRED;
1776
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001777 if( x509_time_future( &child->valid_from ) )
1778 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001779
1780 md_info = md_info_from_type( child->sig_md );
1781 if( md_info == NULL )
1782 {
1783 /*
1784 * Cannot check 'unknown' hash
1785 */
1786 *flags |= BADCERT_NOT_TRUSTED;
1787 }
1788 else
1789 {
1790 md( md_info, child->tbs.p, child->tbs.len, hash );
1791
1792 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1793 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1794 child->sig.p, child->sig.len ) != 0 )
1795 {
1796 *flags |= BADCERT_NOT_TRUSTED;
1797 }
1798 }
1799
1800#if defined(POLARSSL_X509_CRL_PARSE_C)
1801 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001802 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001803#endif
1804
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001805 /* Look for a grandparent upwards the chain */
1806 for( grandparent = parent->next;
1807 grandparent != NULL;
1808 grandparent = grandparent->next )
1809 {
1810 if( x509_crt_check_parent( parent, grandparent ) == 0 )
1811 break;
1812 }
1813
1814 /* Is our parent part of the chain or at the top? */
1815 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001816 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001817 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1818 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001819 if( ret != 0 )
1820 return( ret );
1821 }
1822 else
1823 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001824 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1825 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001826 if( ret != 0 )
1827 return( ret );
1828 }
1829
1830 /* child is verified to be a child of the parent, call verify callback */
1831 if( NULL != f_vrfy )
1832 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1833 return( ret );
1834
1835 *flags |= parent_flags;
1836
1837 return( 0 );
1838}
1839
1840/*
1841 * Verify the certificate validity
1842 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001843int x509_crt_verify( x509_crt *crt,
1844 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001845 x509_crl *ca_crl,
1846 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001847 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001848 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001849{
1850 size_t cn_len;
1851 int ret;
1852 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001853 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001854 x509_name *name;
1855 x509_sequence *cur = NULL;
1856
1857 *flags = 0;
1858
1859 if( cn != NULL )
1860 {
1861 name = &crt->subject;
1862 cn_len = strlen( cn );
1863
1864 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1865 {
1866 cur = &crt->subject_alt_names;
1867
1868 while( cur != NULL )
1869 {
1870 if( cur->buf.len == cn_len &&
1871 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1872 break;
1873
1874 if( cur->buf.len > 2 &&
1875 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1876 x509_wildcard_verify( cn, &cur->buf ) )
1877 break;
1878
1879 cur = cur->next;
1880 }
1881
1882 if( cur == NULL )
1883 *flags |= BADCERT_CN_MISMATCH;
1884 }
1885 else
1886 {
1887 while( name != NULL )
1888 {
1889 if( OID_CMP( OID_AT_CN, &name->oid ) )
1890 {
1891 if( name->val.len == cn_len &&
1892 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1893 break;
1894
1895 if( name->val.len > 2 &&
1896 memcmp( name->val.p, "*.", 2 ) == 0 &&
1897 x509_wildcard_verify( cn, &name->val ) )
1898 break;
1899 }
1900
1901 name = name->next;
1902 }
1903
1904 if( name == NULL )
1905 *flags |= BADCERT_CN_MISMATCH;
1906 }
1907 }
1908
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001909 /* Look for a parent upwards the chain */
1910 for( parent = crt->next; parent != NULL; parent = parent->next )
1911 {
1912 if( x509_crt_check_parent( crt, parent ) == 0 )
1913 break;
1914 }
1915
1916 /* Are we part of the chain or at the top? */
1917 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001918 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001919 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1920 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001921 if( ret != 0 )
1922 return( ret );
1923 }
1924 else
1925 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001926 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1927 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001928 if( ret != 0 )
1929 return( ret );
1930 }
1931
1932 if( *flags != 0 )
1933 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1934
1935 return( 0 );
1936}
1937
1938/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001939 * Initialize a certificate chain
1940 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001941void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001942{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001943 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001944}
1945
1946/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001947 * Unallocate all certificate data
1948 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001949void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001950{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001951 x509_crt *cert_cur = crt;
1952 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001953 x509_name *name_cur;
1954 x509_name *name_prv;
1955 x509_sequence *seq_cur;
1956 x509_sequence *seq_prv;
1957
1958 if( crt == NULL )
1959 return;
1960
1961 do
1962 {
1963 pk_free( &cert_cur->pk );
1964
1965 name_cur = cert_cur->issuer.next;
1966 while( name_cur != NULL )
1967 {
1968 name_prv = name_cur;
1969 name_cur = name_cur->next;
1970 memset( name_prv, 0, sizeof( x509_name ) );
1971 polarssl_free( name_prv );
1972 }
1973
1974 name_cur = cert_cur->subject.next;
1975 while( name_cur != NULL )
1976 {
1977 name_prv = name_cur;
1978 name_cur = name_cur->next;
1979 memset( name_prv, 0, sizeof( x509_name ) );
1980 polarssl_free( name_prv );
1981 }
1982
1983 seq_cur = cert_cur->ext_key_usage.next;
1984 while( seq_cur != NULL )
1985 {
1986 seq_prv = seq_cur;
1987 seq_cur = seq_cur->next;
1988 memset( seq_prv, 0, sizeof( x509_sequence ) );
1989 polarssl_free( seq_prv );
1990 }
1991
1992 seq_cur = cert_cur->subject_alt_names.next;
1993 while( seq_cur != NULL )
1994 {
1995 seq_prv = seq_cur;
1996 seq_cur = seq_cur->next;
1997 memset( seq_prv, 0, sizeof( x509_sequence ) );
1998 polarssl_free( seq_prv );
1999 }
2000
2001 if( cert_cur->raw.p != NULL )
2002 {
2003 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
2004 polarssl_free( cert_cur->raw.p );
2005 }
2006
2007 cert_cur = cert_cur->next;
2008 }
2009 while( cert_cur != NULL );
2010
2011 cert_cur = crt;
2012 do
2013 {
2014 cert_prv = cert_cur;
2015 cert_cur = cert_cur->next;
2016
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002017 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002018 if( cert_prv != crt )
2019 polarssl_free( cert_prv );
2020 }
2021 while( cert_cur != NULL );
2022}
2023
Paul Bakker9af723c2014-05-01 13:03:14 +02002024#endif /* POLARSSL_X509_CRT_PARSE_C */