blob: 617b733af526bcabe0dfaeadcc58cafb68b3770d [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,
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100604 &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é-Gonnardcf975a32014-01-24 19:28:43 +0100618 if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params,
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +0200619 &crt->sig_md, &crt->sig_pk,
620 &crt->sig_opts ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 {
622 x509_crt_free( crt );
623 return( ret );
624 }
625
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100626#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnardcf975a32014-01-24 19:28:43 +0100627 memcpy( &crt->sig_params, &sig_params, sizeof( x509_buf ) );
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100628#endif
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +0100629
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 /*
631 * issuer Name
632 */
633 crt->issuer_raw.p = p;
634
635 if( ( ret = asn1_get_tag( &p, end, &len,
636 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
637 {
638 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200639 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200640 }
641
642 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
643 {
644 x509_crt_free( crt );
645 return( ret );
646 }
647
648 crt->issuer_raw.len = p - crt->issuer_raw.p;
649
650 /*
651 * Validity ::= SEQUENCE {
652 * notBefore Time,
653 * notAfter Time }
654 *
655 */
656 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
657 &crt->valid_to ) ) != 0 )
658 {
659 x509_crt_free( crt );
660 return( ret );
661 }
662
663 /*
664 * subject Name
665 */
666 crt->subject_raw.p = p;
667
668 if( ( ret = asn1_get_tag( &p, end, &len,
669 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
670 {
671 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200672 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200673 }
674
675 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
676 {
677 x509_crt_free( crt );
678 return( ret );
679 }
680
681 crt->subject_raw.len = p - crt->subject_raw.p;
682
683 /*
684 * SubjectPublicKeyInfo
685 */
Paul Bakkerda771152013-09-16 22:45:03 +0200686 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200687 {
688 x509_crt_free( crt );
689 return( ret );
690 }
691
692 /*
693 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
694 * -- If present, version shall be v2 or v3
695 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
696 * -- If present, version shall be v2 or v3
697 * extensions [3] EXPLICIT Extensions OPTIONAL
698 * -- If present, version shall be v3
699 */
700 if( crt->version == 2 || crt->version == 3 )
701 {
702 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
703 if( ret != 0 )
704 {
705 x509_crt_free( crt );
706 return( ret );
707 }
708 }
709
710 if( crt->version == 2 || crt->version == 3 )
711 {
712 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
713 if( ret != 0 )
714 {
715 x509_crt_free( crt );
716 return( ret );
717 }
718 }
719
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200720#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200721 if( crt->version == 3 )
722 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200723#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200724 ret = x509_get_crt_ext( &p, end, crt);
725 if( ret != 0 )
726 {
727 x509_crt_free( crt );
728 return( ret );
729 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200730#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200732#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733
734 if( p != end )
735 {
736 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200737 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200738 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
739 }
740
741 end = crt_end;
742
743 /*
744 * }
745 * -- end of TBSCertificate
746 *
747 * signatureAlgorithm AlgorithmIdentifier,
748 * signatureValue BIT STRING
749 */
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100750 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200751 {
752 x509_crt_free( crt );
753 return( ret );
754 }
755
756 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100757 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0
758#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
759 ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100760 crt->sig_params.len != sig_params.len ||
Manuel Pégourié-Gonnard9df5c962014-01-24 14:37:29 +0100761 memcmp( crt->sig_params.p, sig_params.p, sig_params.len ) != 0
762#endif
763 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200764 {
765 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200766 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767 }
768
769 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
770 {
771 x509_crt_free( crt );
772 return( ret );
773 }
774
775 if( p != end )
776 {
777 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200778 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
780 }
781
782 return( 0 );
783}
784
785/*
786 * Parse one X.509 certificate in DER format from a buffer and add them to a
787 * chained list
788 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200789int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200790 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791{
792 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200793 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794
795 /*
796 * Check for valid input
797 */
798 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200799 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200800
801 while( crt->version != 0 && crt->next != NULL )
802 {
803 prev = crt;
804 crt = crt->next;
805 }
806
807 /*
808 * Add new certificate on the end of the chain if needed.
809 */
810 if ( crt->version != 0 && crt->next == NULL)
811 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200812 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200813
814 if( crt->next == NULL )
815 return( POLARSSL_ERR_X509_MALLOC_FAILED );
816
817 prev = crt;
818 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200819 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820 }
821
Paul Bakkerddf26b42013-09-18 13:46:23 +0200822 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200823 {
824 if( prev )
825 prev->next = NULL;
826
827 if( crt != chain )
828 polarssl_free( crt );
829
830 return( ret );
831 }
832
833 return( 0 );
834}
835
836/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200837 * Parse one or more PEM certificates from a buffer and add them to the chained
838 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200839 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200840int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841{
842 int success = 0, first_error = 0, total_failed = 0;
843 int buf_format = X509_FORMAT_DER;
844
845 /*
846 * Check for valid input
847 */
848 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200849 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200850
851 /*
852 * Determine buffer content. Buffer contains either one DER certificate or
853 * one or more PEM certificates.
854 */
855#if defined(POLARSSL_PEM_PARSE_C)
856 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
857 buf_format = X509_FORMAT_PEM;
858#endif
859
860 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200861 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200862
863#if defined(POLARSSL_PEM_PARSE_C)
864 if( buf_format == X509_FORMAT_PEM )
865 {
866 int ret;
867 pem_context pem;
868
869 while( buflen > 0 )
870 {
871 size_t use_len;
872 pem_init( &pem );
873
874 ret = pem_read_buffer( &pem,
875 "-----BEGIN CERTIFICATE-----",
876 "-----END CERTIFICATE-----",
877 buf, NULL, 0, &use_len );
878
879 if( ret == 0 )
880 {
881 /*
882 * Was PEM encoded
883 */
884 buflen -= use_len;
885 buf += use_len;
886 }
887 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
888 {
889 return( ret );
890 }
891 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
892 {
893 pem_free( &pem );
894
895 /*
896 * PEM header and footer were found
897 */
898 buflen -= use_len;
899 buf += use_len;
900
901 if( first_error == 0 )
902 first_error = ret;
903
904 continue;
905 }
906 else
907 break;
908
Paul Bakkerddf26b42013-09-18 13:46:23 +0200909 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200910
911 pem_free( &pem );
912
913 if( ret != 0 )
914 {
915 /*
916 * Quit parsing on a memory error
917 */
918 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
919 return( ret );
920
921 if( first_error == 0 )
922 first_error = ret;
923
924 total_failed++;
925 continue;
926 }
927
928 success = 1;
929 }
930 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200931#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200932
933 if( success )
934 return( total_failed );
935 else if( first_error )
936 return( first_error );
937 else
938 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
939}
940
941#if defined(POLARSSL_FS_IO)
942/*
943 * Load one or more certificates and add them to the chained list
944 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200945int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946{
947 int ret;
948 size_t n;
949 unsigned char *buf;
950
951 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
952 return( ret );
953
Paul Bakkerddf26b42013-09-18 13:46:23 +0200954 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200955
956 memset( buf, 0, n + 1 );
957 polarssl_free( buf );
958
959 return( ret );
960}
961
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100962#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100963static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
964#endif
965
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200966int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200967{
968 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100969#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200970 int w_ret;
971 WCHAR szDir[MAX_PATH];
972 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200973 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200974 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200975
Paul Bakker9af723c2014-05-01 13:03:14 +0200976 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 HANDLE hFind;
978
979 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200980 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200981
Paul Bakker9af723c2014-05-01 13:03:14 +0200982 memset( szDir, 0, sizeof(szDir) );
983 memset( filename, 0, MAX_PATH );
984 memcpy( filename, path, len );
985 filename[len++] = '\\';
986 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200987 filename[len++] = '*';
988
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200989 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
990 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
992 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200993 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
995
996 len = MAX_PATH - len;
997 do
998 {
Paul Bakker9af723c2014-05-01 13:03:14 +0200999 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001000
1001 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1002 continue;
1003
Paul Bakker9af723c2014-05-01 13:03:14 +02001004 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1005 lstrlenW(file_data.cFileName),
1006 p, len - 1,
1007 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001008
Paul Bakkerddf26b42013-09-18 13:46:23 +02001009 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001010 if( w_ret < 0 )
1011 ret++;
1012 else
1013 ret += w_ret;
1014 }
1015 while( FindNextFileW( hFind, &file_data ) != 0 );
1016
Paul Bakker4aa40d42013-10-11 10:49:24 +02001017 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001018 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1019
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001020 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001021#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001022 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001023 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001024 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 char entry_name[255];
1026 DIR *dir = opendir( path );
1027
1028 if( dir == NULL)
1029 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1030
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001031#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001032 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1033 return( ret );
1034#endif
1035
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001036 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001037 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001038 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001039
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001040 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001041 {
1042 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001043 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1044 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001045 }
1046
1047 if( !S_ISREG( sb.st_mode ) )
1048 continue;
1049
1050 // Ignore parse errors
1051 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001052 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001053 if( t_ret < 0 )
1054 ret++;
1055 else
1056 ret += t_ret;
1057 }
1058 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001059
1060cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001061#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001062 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1063 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1064#endif
1065
Paul Bakkerbe089b02013-10-14 15:51:50 +02001066#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001067
1068 return( ret );
1069}
1070#endif /* POLARSSL_FS_IO */
1071
Paul Bakker6edcd412013-10-29 15:22:54 +01001072#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1073 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074#include <stdarg.h>
1075
1076#if !defined vsnprintf
1077#define vsnprintf _vsnprintf
1078#endif // vsnprintf
1079
1080/*
1081 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1082 * Result value is not size of buffer needed, but -1 if no fit is possible.
1083 *
1084 * This fuction tries to 'fix' this by at least suggesting enlarging the
1085 * size by 20.
1086 */
1087static int compat_snprintf(char *str, size_t size, const char *format, ...)
1088{
1089 va_list ap;
1090 int res = -1;
1091
1092 va_start( ap, format );
1093
1094 res = vsnprintf( str, size, format, ap );
1095
1096 va_end( ap );
1097
1098 // No quick fix possible
1099 if ( res < 0 )
1100 return( (int) size + 20 );
1101
1102 return res;
1103}
1104
1105#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001106#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001107
1108#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1109
1110#define SAFE_SNPRINTF() \
1111{ \
1112 if( ret == -1 ) \
1113 return( -1 ); \
1114 \
1115 if ( (unsigned int) ret > n ) { \
1116 p[n - 1] = '\0'; \
1117 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1118 } \
1119 \
1120 n -= (unsigned int) ret; \
1121 p += (unsigned int) ret; \
1122}
1123
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001124static int x509_info_subject_alt_name( char **buf, size_t *size,
1125 const x509_sequence *subject_alt_name )
1126{
1127 size_t i;
1128 size_t n = *size;
1129 char *p = *buf;
1130 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001131 const char *sep = "";
1132 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001133
1134 while( cur != NULL )
1135 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001136 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001137 {
1138 *p = '\0';
1139 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1140 }
1141
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001142 n -= cur->buf.len + sep_len;
1143 for( i = 0; i < sep_len; i++ )
1144 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001145 for( i = 0; i < cur->buf.len; i++ )
1146 *p++ = cur->buf.p[i];
1147
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001148 sep = ", ";
1149 sep_len = 2;
1150
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001151 cur = cur->next;
1152 }
1153
1154 *p = '\0';
1155
1156 *size = n;
1157 *buf = p;
1158
1159 return( 0 );
1160}
1161
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001162#define PRINT_ITEM(i) \
1163 { \
1164 ret = snprintf( p, n, "%s" i, sep ); \
1165 SAFE_SNPRINTF(); \
1166 sep = ", "; \
1167 }
1168
1169#define CERT_TYPE(type,name) \
1170 if( ns_cert_type & type ) \
1171 PRINT_ITEM( name );
1172
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001173static int x509_info_cert_type( char **buf, size_t *size,
1174 unsigned char ns_cert_type )
1175{
1176 int ret;
1177 size_t n = *size;
1178 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001179 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001180
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001181 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1182 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1183 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1184 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1185 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1186 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1187 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1188 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001189
1190 *size = n;
1191 *buf = p;
1192
1193 return( 0 );
1194}
1195
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001196#define KEY_USAGE(code,name) \
1197 if( key_usage & code ) \
1198 PRINT_ITEM( name );
1199
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001200static int x509_info_key_usage( char **buf, size_t *size,
1201 unsigned char key_usage )
1202{
1203 int ret;
1204 size_t n = *size;
1205 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001206 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001207
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001208 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1209 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1210 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1211 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1212 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1213 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1214 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001215
1216 *size = n;
1217 *buf = p;
1218
1219 return( 0 );
1220}
1221
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001222static int x509_info_ext_key_usage( char **buf, size_t *size,
1223 const x509_sequence *extended_key_usage )
1224{
1225 int ret;
1226 const char *desc;
1227 size_t n = *size;
1228 char *p = *buf;
1229 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001230 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001231
1232 while( cur != NULL )
1233 {
1234 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1235 desc = "???";
1236
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001237 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001238 SAFE_SNPRINTF();
1239
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001240 sep = ", ";
1241
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001242 cur = cur->next;
1243 }
1244
1245 *size = n;
1246 *buf = p;
1247
1248 return( 0 );
1249}
1250
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001251/*
1252 * Return an informational string about the certificate.
1253 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001254#define BEFORE_COLON 18
1255#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001256int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001257 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258{
1259 int ret;
1260 size_t n;
1261 char *p;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001262 char key_size_str[BEFORE_COLON];
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001263#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001264 const void *sig_opts = crt->sig_opts;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001265#else
Manuel Pégourié-Gonnard91136032014-06-05 15:41:39 +02001266 const void *sig_opts = NULL;
Manuel Pégourié-Gonnardcac31ee2014-01-25 11:50:59 +01001267#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001268
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 Bakker86d0c192013-09-18 11:11:02 +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,
1310 crt->sig_md, 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
1507 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1508 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1509 crl_list->sig.p, crl_list->sig.len ) != 0 )
1510 {
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 Bakkerddf26b42013-09-18 13:46:23 +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 }
1535 return flags;
1536}
1537#endif /* POLARSSL_X509_CRL_PARSE_C */
1538
1539// Equal == 0, inequal == 1
1540static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1541{
1542 size_t i;
1543 unsigned char diff;
1544 const unsigned char *n1 = s1, *n2 = s2;
1545
1546 for( i = 0; i < len; i++ )
1547 {
1548 diff = n1[i] ^ n2[i];
1549
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001550 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001551 continue;
1552
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001553 if( diff == 32 &&
1554 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1555 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1556 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001557 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001558 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001559
1560 return( 1 );
1561 }
1562
1563 return( 0 );
1564}
1565
1566static int x509_wildcard_verify( const char *cn, x509_buf *name )
1567{
1568 size_t i;
1569 size_t cn_idx = 0;
1570
1571 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1572 return( 0 );
1573
1574 for( i = 0; i < strlen( cn ); ++i )
1575 {
1576 if( cn[i] == '.' )
1577 {
1578 cn_idx = i;
1579 break;
1580 }
1581 }
1582
1583 if( cn_idx == 0 )
1584 return( 0 );
1585
1586 if( strlen( cn ) - cn_idx == name->len - 1 &&
1587 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1588 {
1589 return( 1 );
1590 }
1591
1592 return( 0 );
1593}
1594
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001595/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001596 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1597 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001598 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001599static int x509_crt_check_parent( const x509_crt *child,
1600 const x509_crt *parent )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001601{
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001602 if( parent->version == 0 ||
1603 parent->ca_istrue == 0 ||
1604 child->issuer_raw.len != parent->subject_raw.len ||
1605 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1606 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001607 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001608 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001609 }
1610
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001611#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1612 if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1613 return( -1 );
1614#endif
1615
1616 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001617}
1618
Paul Bakkerddf26b42013-09-18 13:46:23 +02001619static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001620 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001622 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001623 void *p_vrfy )
1624{
1625 int ret;
1626 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1627 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1628 const md_info_t *md_info;
1629
Paul Bakker86d0c192013-09-18 11:11:02 +02001630 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001631 *flags |= BADCERT_EXPIRED;
1632
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001633 if( x509_time_future( &child->valid_from ) )
1634 *flags |= BADCERT_FUTURE;
1635
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001636 /*
1637 * Child is the top of the chain. Check against the trust_ca list.
1638 */
1639 *flags |= BADCERT_NOT_TRUSTED;
1640
1641 md_info = md_info_from_type( child->sig_md );
1642 if( md_info == NULL )
1643 {
1644 /*
1645 * Cannot check 'unknown', no need to try any CA
1646 */
1647 trust_ca = NULL;
1648 }
1649 else
1650 md( md_info, child->tbs.p, child->tbs.len, hash );
1651
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001652 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001653 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001654 if( x509_crt_check_parent( child, trust_ca ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001655 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001656
1657 /*
1658 * Reduce path_len to check against if top of the chain is
1659 * the same as the trusted CA
1660 */
1661 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1662 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1663 child->issuer_raw.len ) == 0 )
1664 {
1665 check_path_cnt--;
1666 }
1667
1668 if( trust_ca->max_pathlen > 0 &&
1669 trust_ca->max_pathlen < check_path_cnt )
1670 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001671 continue;
1672 }
1673
Manuel Pégourié-Gonnard920e1cd2014-06-02 18:11:07 +02001674#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
1675 if( child->sig_pk == POLARSSL_PK_RSASSA_PSS )
1676 {
1677 if( pk_can_do( &trust_ca->pk, POLARSSL_PK_RSA ) == 0 ||
1678 rsa_rsassa_pss_verify( pk_rsa( trust_ca->pk ),
1679 NULL, NULL, RSA_PUBLIC,
1680 child->sig_md,
1681 md_info->size, hash,
1682 child->sig.p ) != 0 )
1683 {
1684 continue;
1685 }
1686 }
1687 else
1688#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001689 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1690 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1691 child->sig.p, child->sig.len ) != 0 )
1692 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001693 continue;
1694 }
1695
1696 /*
1697 * Top of chain is signed by a trusted CA
1698 */
1699 *flags &= ~BADCERT_NOT_TRUSTED;
1700 break;
1701 }
1702
1703 /*
1704 * If top of chain is not the same as the trusted CA send a verify request
1705 * to the callback for any issues with validity and CRL presence for the
1706 * trusted CA certificate.
1707 */
1708 if( trust_ca != NULL &&
1709 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1710 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1711 child->issuer_raw.len ) != 0 ) )
1712 {
1713#if defined(POLARSSL_X509_CRL_PARSE_C)
1714 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001715 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001716#else
1717 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001718#endif
1719
Paul Bakker86d0c192013-09-18 11:11:02 +02001720 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001721 ca_flags |= BADCERT_EXPIRED;
1722
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001723 if( x509_time_future( &trust_ca->valid_from ) )
1724 ca_flags |= BADCERT_FUTURE;
1725
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001726 if( NULL != f_vrfy )
1727 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001728 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1729 &ca_flags ) ) != 0 )
1730 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001731 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001732 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001733 }
1734 }
1735
1736 /* Call callback on top cert */
1737 if( NULL != f_vrfy )
1738 {
1739 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1740 return( ret );
1741 }
1742
1743 *flags |= ca_flags;
1744
1745 return( 0 );
1746}
1747
Paul Bakkerddf26b42013-09-18 13:46:23 +02001748static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001749 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001750 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001751 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001752 void *p_vrfy )
1753{
1754 int ret;
1755 int parent_flags = 0;
1756 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001757 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758 const md_info_t *md_info;
1759
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001760 if( x509_time_expired( &child->valid_to ) )
1761 *flags |= BADCERT_EXPIRED;
1762
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001763 if( x509_time_future( &child->valid_from ) )
1764 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001765
1766 md_info = md_info_from_type( child->sig_md );
1767 if( md_info == NULL )
1768 {
1769 /*
1770 * Cannot check 'unknown' hash
1771 */
1772 *flags |= BADCERT_NOT_TRUSTED;
1773 }
1774 else
1775 {
1776 md( md_info, child->tbs.p, child->tbs.len, hash );
1777
Manuel Pégourié-Gonnard920e1cd2014-06-02 18:11:07 +02001778#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
1779 if( child->sig_pk == POLARSSL_PK_RSASSA_PSS )
1780 {
1781 if( pk_can_do( &parent->pk, POLARSSL_PK_RSA ) == 0 ||
1782 rsa_rsassa_pss_verify( pk_rsa( parent->pk ),
1783 NULL, NULL, RSA_PUBLIC,
1784 child->sig_md,
1785 md_info->size, hash,
1786 child->sig.p ) != 0 )
1787 {
1788 *flags |= BADCERT_NOT_TRUSTED;
1789 }
1790 }
1791 else
1792#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001793 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1794 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1795 child->sig.p, child->sig.len ) != 0 )
1796 {
1797 *flags |= BADCERT_NOT_TRUSTED;
1798 }
1799 }
1800
1801#if defined(POLARSSL_X509_CRL_PARSE_C)
1802 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001803 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001804#endif
1805
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001806 /* Look for a grandparent upwards the chain */
1807 for( grandparent = parent->next;
1808 grandparent != NULL;
1809 grandparent = grandparent->next )
1810 {
1811 if( x509_crt_check_parent( parent, grandparent ) == 0 )
1812 break;
1813 }
1814
1815 /* Is our parent part of the chain or at the top? */
1816 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001817 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001818 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1819 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001820 if( ret != 0 )
1821 return( ret );
1822 }
1823 else
1824 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001825 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1826 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001827 if( ret != 0 )
1828 return( ret );
1829 }
1830
1831 /* child is verified to be a child of the parent, call verify callback */
1832 if( NULL != f_vrfy )
1833 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1834 return( ret );
1835
1836 *flags |= parent_flags;
1837
1838 return( 0 );
1839}
1840
1841/*
1842 * Verify the certificate validity
1843 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001844int x509_crt_verify( x509_crt *crt,
1845 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001846 x509_crl *ca_crl,
1847 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001848 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001849 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001850{
1851 size_t cn_len;
1852 int ret;
1853 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001854 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001855 x509_name *name;
1856 x509_sequence *cur = NULL;
1857
1858 *flags = 0;
1859
1860 if( cn != NULL )
1861 {
1862 name = &crt->subject;
1863 cn_len = strlen( cn );
1864
1865 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1866 {
1867 cur = &crt->subject_alt_names;
1868
1869 while( cur != NULL )
1870 {
1871 if( cur->buf.len == cn_len &&
1872 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1873 break;
1874
1875 if( cur->buf.len > 2 &&
1876 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1877 x509_wildcard_verify( cn, &cur->buf ) )
1878 break;
1879
1880 cur = cur->next;
1881 }
1882
1883 if( cur == NULL )
1884 *flags |= BADCERT_CN_MISMATCH;
1885 }
1886 else
1887 {
1888 while( name != NULL )
1889 {
1890 if( OID_CMP( OID_AT_CN, &name->oid ) )
1891 {
1892 if( name->val.len == cn_len &&
1893 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1894 break;
1895
1896 if( name->val.len > 2 &&
1897 memcmp( name->val.p, "*.", 2 ) == 0 &&
1898 x509_wildcard_verify( cn, &name->val ) )
1899 break;
1900 }
1901
1902 name = name->next;
1903 }
1904
1905 if( name == NULL )
1906 *flags |= BADCERT_CN_MISMATCH;
1907 }
1908 }
1909
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001910 /* Look for a parent upwards the chain */
1911 for( parent = crt->next; parent != NULL; parent = parent->next )
1912 {
1913 if( x509_crt_check_parent( crt, parent ) == 0 )
1914 break;
1915 }
1916
1917 /* Are we part of the chain or at the top? */
1918 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001919 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001920 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1921 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001922 if( ret != 0 )
1923 return( ret );
1924 }
1925 else
1926 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001927 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1928 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001929 if( ret != 0 )
1930 return( ret );
1931 }
1932
1933 if( *flags != 0 )
1934 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1935
1936 return( 0 );
1937}
1938
1939/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001940 * Initialize a certificate chain
1941 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001942void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001943{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001944 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001945}
1946
1947/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001948 * Unallocate all certificate data
1949 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001950void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001951{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001952 x509_crt *cert_cur = crt;
1953 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001954 x509_name *name_cur;
1955 x509_name *name_prv;
1956 x509_sequence *seq_cur;
1957 x509_sequence *seq_prv;
1958
1959 if( crt == NULL )
1960 return;
1961
1962 do
1963 {
1964 pk_free( &cert_cur->pk );
1965
Manuel Pégourié-Gonnardf75f2f72014-06-05 15:14:28 +02001966#if defined(POLARSSL_RSASSA_PSS_CERTIFICATES)
1967 polarssl_free( cert_cur->sig_opts );
1968#endif
1969
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001970 name_cur = cert_cur->issuer.next;
1971 while( name_cur != NULL )
1972 {
1973 name_prv = name_cur;
1974 name_cur = name_cur->next;
1975 memset( name_prv, 0, sizeof( x509_name ) );
1976 polarssl_free( name_prv );
1977 }
1978
1979 name_cur = cert_cur->subject.next;
1980 while( name_cur != NULL )
1981 {
1982 name_prv = name_cur;
1983 name_cur = name_cur->next;
1984 memset( name_prv, 0, sizeof( x509_name ) );
1985 polarssl_free( name_prv );
1986 }
1987
1988 seq_cur = cert_cur->ext_key_usage.next;
1989 while( seq_cur != NULL )
1990 {
1991 seq_prv = seq_cur;
1992 seq_cur = seq_cur->next;
1993 memset( seq_prv, 0, sizeof( x509_sequence ) );
1994 polarssl_free( seq_prv );
1995 }
1996
1997 seq_cur = cert_cur->subject_alt_names.next;
1998 while( seq_cur != NULL )
1999 {
2000 seq_prv = seq_cur;
2001 seq_cur = seq_cur->next;
2002 memset( seq_prv, 0, sizeof( x509_sequence ) );
2003 polarssl_free( seq_prv );
2004 }
2005
2006 if( cert_cur->raw.p != NULL )
2007 {
2008 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
2009 polarssl_free( cert_cur->raw.p );
2010 }
2011
2012 cert_cur = cert_cur->next;
2013 }
2014 while( cert_cur != NULL );
2015
2016 cert_cur = crt;
2017 do
2018 {
2019 cert_prv = cert_cur;
2020 cert_cur = cert_cur->next;
2021
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002022 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002023 if( cert_prv != crt )
2024 polarssl_free( cert_prv );
2025 }
2026 while( cert_cur != NULL );
2027}
2028
Paul Bakker9af723c2014-05-01 13:03:14 +02002029#endif /* POLARSSL_X509_CRT_PARSE_C */