blob: 6d206a3cda15ee9dea7560417b0116a282ebdfea [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;
628
629 if( ( ret = x509_get_rsassa_pss_params( &crt->sig_params,
630 &crt->sig_md, &salt_len, &trailer_field ) ) != 0 )
631 return( ret );
632 }
633 else
634 {
635 /* Make sure parameters were absent or NULL */
636 if( ( crt->sig_params.tag != ASN1_NULL && crt->sig_params.tag != 0 ) ||
637 crt->sig_params.len != 0 )
638 return( POLARSSL_ERR_X509_INVALID_ALG );
639 }
640
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200641 /*
642 * issuer Name
643 */
644 crt->issuer_raw.p = p;
645
646 if( ( ret = asn1_get_tag( &p, end, &len,
647 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
648 {
649 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200650 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200651 }
652
653 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
654 {
655 x509_crt_free( crt );
656 return( ret );
657 }
658
659 crt->issuer_raw.len = p - crt->issuer_raw.p;
660
661 /*
662 * Validity ::= SEQUENCE {
663 * notBefore Time,
664 * notAfter Time }
665 *
666 */
667 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
668 &crt->valid_to ) ) != 0 )
669 {
670 x509_crt_free( crt );
671 return( ret );
672 }
673
674 /*
675 * subject Name
676 */
677 crt->subject_raw.p = p;
678
679 if( ( ret = asn1_get_tag( &p, end, &len,
680 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
681 {
682 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200683 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684 }
685
686 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
687 {
688 x509_crt_free( crt );
689 return( ret );
690 }
691
692 crt->subject_raw.len = p - crt->subject_raw.p;
693
694 /*
695 * SubjectPublicKeyInfo
696 */
Paul Bakkerda771152013-09-16 22:45:03 +0200697 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200698 {
699 x509_crt_free( crt );
700 return( ret );
701 }
702
703 /*
704 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
705 * -- If present, version shall be v2 or v3
706 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
707 * -- If present, version shall be v2 or v3
708 * extensions [3] EXPLICIT Extensions OPTIONAL
709 * -- If present, version shall be v3
710 */
711 if( crt->version == 2 || crt->version == 3 )
712 {
713 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
714 if( ret != 0 )
715 {
716 x509_crt_free( crt );
717 return( ret );
718 }
719 }
720
721 if( crt->version == 2 || crt->version == 3 )
722 {
723 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
724 if( ret != 0 )
725 {
726 x509_crt_free( crt );
727 return( ret );
728 }
729 }
730
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200731#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200732 if( crt->version == 3 )
733 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200734#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 ret = x509_get_crt_ext( &p, end, crt);
736 if( ret != 0 )
737 {
738 x509_crt_free( crt );
739 return( ret );
740 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200741#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200742 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200743#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744
745 if( p != end )
746 {
747 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200748 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
750 }
751
752 end = crt_end;
753
754 /*
755 * }
756 * -- end of TBSCertificate
757 *
758 * signatureAlgorithm AlgorithmIdentifier,
759 * signatureValue BIT STRING
760 */
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100761 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200762 {
763 x509_crt_free( crt );
764 return( ret );
765 }
766
767 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100768 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
769 crt->sig_params.len != sig_params.len ||
770 memcmp( crt->sig_params.p, sig_params.p, sig_params.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200771 {
772 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200773 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200774 }
775
776 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
777 {
778 x509_crt_free( crt );
779 return( ret );
780 }
781
782 if( p != end )
783 {
784 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200785 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
787 }
788
789 return( 0 );
790}
791
792/*
793 * Parse one X.509 certificate in DER format from a buffer and add them to a
794 * chained list
795 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200796int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200797 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200798{
799 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200800 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200801
802 /*
803 * Check for valid input
804 */
805 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200806 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200807
808 while( crt->version != 0 && crt->next != NULL )
809 {
810 prev = crt;
811 crt = crt->next;
812 }
813
814 /*
815 * Add new certificate on the end of the chain if needed.
816 */
817 if ( crt->version != 0 && crt->next == NULL)
818 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200819 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200820
821 if( crt->next == NULL )
822 return( POLARSSL_ERR_X509_MALLOC_FAILED );
823
824 prev = crt;
825 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200826 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200827 }
828
Paul Bakkerddf26b42013-09-18 13:46:23 +0200829 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 {
831 if( prev )
832 prev->next = NULL;
833
834 if( crt != chain )
835 polarssl_free( crt );
836
837 return( ret );
838 }
839
840 return( 0 );
841}
842
843/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200844 * Parse one or more PEM certificates from a buffer and add them to the chained
845 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200846 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200847int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200848{
849 int success = 0, first_error = 0, total_failed = 0;
850 int buf_format = X509_FORMAT_DER;
851
852 /*
853 * Check for valid input
854 */
855 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200856 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200857
858 /*
859 * Determine buffer content. Buffer contains either one DER certificate or
860 * one or more PEM certificates.
861 */
862#if defined(POLARSSL_PEM_PARSE_C)
863 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
864 buf_format = X509_FORMAT_PEM;
865#endif
866
867 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200868 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200869
870#if defined(POLARSSL_PEM_PARSE_C)
871 if( buf_format == X509_FORMAT_PEM )
872 {
873 int ret;
874 pem_context pem;
875
876 while( buflen > 0 )
877 {
878 size_t use_len;
879 pem_init( &pem );
880
881 ret = pem_read_buffer( &pem,
882 "-----BEGIN CERTIFICATE-----",
883 "-----END CERTIFICATE-----",
884 buf, NULL, 0, &use_len );
885
886 if( ret == 0 )
887 {
888 /*
889 * Was PEM encoded
890 */
891 buflen -= use_len;
892 buf += use_len;
893 }
894 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
895 {
896 return( ret );
897 }
898 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
899 {
900 pem_free( &pem );
901
902 /*
903 * PEM header and footer were found
904 */
905 buflen -= use_len;
906 buf += use_len;
907
908 if( first_error == 0 )
909 first_error = ret;
910
911 continue;
912 }
913 else
914 break;
915
Paul Bakkerddf26b42013-09-18 13:46:23 +0200916 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200917
918 pem_free( &pem );
919
920 if( ret != 0 )
921 {
922 /*
923 * Quit parsing on a memory error
924 */
925 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
926 return( ret );
927
928 if( first_error == 0 )
929 first_error = ret;
930
931 total_failed++;
932 continue;
933 }
934
935 success = 1;
936 }
937 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200938#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200939
940 if( success )
941 return( total_failed );
942 else if( first_error )
943 return( first_error );
944 else
945 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
946}
947
948#if defined(POLARSSL_FS_IO)
949/*
950 * Load one or more certificates and add them to the chained list
951 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200952int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200953{
954 int ret;
955 size_t n;
956 unsigned char *buf;
957
958 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
959 return( ret );
960
Paul Bakkerddf26b42013-09-18 13:46:23 +0200961 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200962
963 memset( buf, 0, n + 1 );
964 polarssl_free( buf );
965
966 return( ret );
967}
968
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100969#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100970static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
971#endif
972
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200973int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200974{
975 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100976#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200977 int w_ret;
978 WCHAR szDir[MAX_PATH];
979 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200980 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200981 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
Paul Bakker9af723c2014-05-01 13:03:14 +0200983 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200984 HANDLE hFind;
985
986 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200987 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200988
Paul Bakker9af723c2014-05-01 13:03:14 +0200989 memset( szDir, 0, sizeof(szDir) );
990 memset( filename, 0, MAX_PATH );
991 memcpy( filename, path, len );
992 filename[len++] = '\\';
993 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200994 filename[len++] = '*';
995
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200996 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
997 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200998
999 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +02001000 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1002
1003 len = MAX_PATH - len;
1004 do
1005 {
Paul Bakker9af723c2014-05-01 13:03:14 +02001006 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001007
1008 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
1009 continue;
1010
Paul Bakker9af723c2014-05-01 13:03:14 +02001011 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1012 lstrlenW(file_data.cFileName),
1013 p, len - 1,
1014 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001015
Paul Bakkerddf26b42013-09-18 13:46:23 +02001016 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001017 if( w_ret < 0 )
1018 ret++;
1019 else
1020 ret += w_ret;
1021 }
1022 while( FindNextFileW( hFind, &file_data ) != 0 );
1023
Paul Bakker4aa40d42013-10-11 10:49:24 +02001024 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001025 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1026
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001027 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001028#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001029 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001031 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 char entry_name[255];
1033 DIR *dir = opendir( path );
1034
1035 if( dir == NULL)
1036 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1037
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001038#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001039 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1040 return( ret );
1041#endif
1042
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001043 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001045 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001046
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001047 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001048 {
1049 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001050 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1051 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001052 }
1053
1054 if( !S_ISREG( sb.st_mode ) )
1055 continue;
1056
1057 // Ignore parse errors
1058 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001059 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001060 if( t_ret < 0 )
1061 ret++;
1062 else
1063 ret += t_ret;
1064 }
1065 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001066
1067cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001068#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001069 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1070 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1071#endif
1072
Paul Bakkerbe089b02013-10-14 15:51:50 +02001073#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001074
1075 return( ret );
1076}
1077#endif /* POLARSSL_FS_IO */
1078
Paul Bakker6edcd412013-10-29 15:22:54 +01001079#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1080 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001081#include <stdarg.h>
1082
1083#if !defined vsnprintf
1084#define vsnprintf _vsnprintf
1085#endif // vsnprintf
1086
1087/*
1088 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1089 * Result value is not size of buffer needed, but -1 if no fit is possible.
1090 *
1091 * This fuction tries to 'fix' this by at least suggesting enlarging the
1092 * size by 20.
1093 */
1094static int compat_snprintf(char *str, size_t size, const char *format, ...)
1095{
1096 va_list ap;
1097 int res = -1;
1098
1099 va_start( ap, format );
1100
1101 res = vsnprintf( str, size, format, ap );
1102
1103 va_end( ap );
1104
1105 // No quick fix possible
1106 if ( res < 0 )
1107 return( (int) size + 20 );
1108
1109 return res;
1110}
1111
1112#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001113#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001114
1115#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1116
1117#define SAFE_SNPRINTF() \
1118{ \
1119 if( ret == -1 ) \
1120 return( -1 ); \
1121 \
1122 if ( (unsigned int) ret > n ) { \
1123 p[n - 1] = '\0'; \
1124 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1125 } \
1126 \
1127 n -= (unsigned int) ret; \
1128 p += (unsigned int) ret; \
1129}
1130
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001131static int x509_info_subject_alt_name( char **buf, size_t *size,
1132 const x509_sequence *subject_alt_name )
1133{
1134 size_t i;
1135 size_t n = *size;
1136 char *p = *buf;
1137 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001138 const char *sep = "";
1139 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001140
1141 while( cur != NULL )
1142 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001143 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001144 {
1145 *p = '\0';
1146 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1147 }
1148
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001149 n -= cur->buf.len + sep_len;
1150 for( i = 0; i < sep_len; i++ )
1151 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001152 for( i = 0; i < cur->buf.len; i++ )
1153 *p++ = cur->buf.p[i];
1154
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001155 sep = ", ";
1156 sep_len = 2;
1157
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001158 cur = cur->next;
1159 }
1160
1161 *p = '\0';
1162
1163 *size = n;
1164 *buf = p;
1165
1166 return( 0 );
1167}
1168
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001169#define PRINT_ITEM(i) \
1170 { \
1171 ret = snprintf( p, n, "%s" i, sep ); \
1172 SAFE_SNPRINTF(); \
1173 sep = ", "; \
1174 }
1175
1176#define CERT_TYPE(type,name) \
1177 if( ns_cert_type & type ) \
1178 PRINT_ITEM( name );
1179
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001180static int x509_info_cert_type( char **buf, size_t *size,
1181 unsigned char ns_cert_type )
1182{
1183 int ret;
1184 size_t n = *size;
1185 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001186 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001187
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001188 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1189 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1190 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1191 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1192 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1193 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1194 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1195 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001196
1197 *size = n;
1198 *buf = p;
1199
1200 return( 0 );
1201}
1202
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001203#define KEY_USAGE(code,name) \
1204 if( key_usage & code ) \
1205 PRINT_ITEM( name );
1206
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001207static int x509_info_key_usage( char **buf, size_t *size,
1208 unsigned char key_usage )
1209{
1210 int ret;
1211 size_t n = *size;
1212 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001213 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001214
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001215 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1216 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1217 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1218 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1219 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1220 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1221 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001222
1223 *size = n;
1224 *buf = p;
1225
1226 return( 0 );
1227}
1228
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001229static int x509_info_ext_key_usage( char **buf, size_t *size,
1230 const x509_sequence *extended_key_usage )
1231{
1232 int ret;
1233 const char *desc;
1234 size_t n = *size;
1235 char *p = *buf;
1236 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001237 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001238
1239 while( cur != NULL )
1240 {
1241 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1242 desc = "???";
1243
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001244 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001245 SAFE_SNPRINTF();
1246
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001247 sep = ", ";
1248
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001249 cur = cur->next;
1250 }
1251
1252 *size = n;
1253 *buf = p;
1254
1255 return( 0 );
1256}
1257
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001258/*
1259 * Return an informational string about the certificate.
1260 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001261#define BEFORE_COLON 18
1262#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001263int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001264 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001265{
1266 int ret;
1267 size_t n;
1268 char *p;
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001269 const char *desc = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 char key_size_str[BEFORE_COLON];
1271
1272 p = buf;
1273 n = size;
1274
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001275 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001276 prefix, crt->version );
1277 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001278 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001279 prefix );
1280 SAFE_SNPRINTF();
1281
Paul Bakker86d0c192013-09-18 11:11:02 +02001282 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001283 SAFE_SNPRINTF();
1284
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001285 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001286 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001287 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001288 SAFE_SNPRINTF();
1289
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001290 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001291 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001292 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001293 SAFE_SNPRINTF();
1294
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001295 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001296 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1297 crt->valid_from.year, crt->valid_from.mon,
1298 crt->valid_from.day, crt->valid_from.hour,
1299 crt->valid_from.min, crt->valid_from.sec );
1300 SAFE_SNPRINTF();
1301
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001302 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001303 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1304 crt->valid_to.year, crt->valid_to.mon,
1305 crt->valid_to.day, crt->valid_to.hour,
1306 crt->valid_to.min, crt->valid_to.sec );
1307 SAFE_SNPRINTF();
1308
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001309 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001310 SAFE_SNPRINTF();
1311
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001312 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1313 if( ret != 0 )
1314 ret = snprintf( p, n, "???" );
1315 else
1316 ret = snprintf( p, n, "%s", desc );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001317 SAFE_SNPRINTF();
1318
Manuel Pégourié-Gonnardf346bab2014-01-23 16:24:44 +01001319 if( crt->sig_pk == POLARSSL_PK_RSASSA_PSS )
1320 {
1321 md_type_t md_alg;
1322 int salt_len, trailer_field;
1323
1324 if( ( ret = x509_get_rsassa_pss_params( &crt->sig_params,
1325 &md_alg, &salt_len, &trailer_field ) ) != 0 )
1326 return( ret );
1327
1328 // TODO: SHA1 harcoded twice (WIP)
1329 ret = snprintf( p, n, " (SHA1, MGF1-SHA1, %d, %d)",
1330 salt_len, trailer_field );
1331 SAFE_SNPRINTF();
1332 }
1333
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001334 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001335 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1336 pk_get_name( &crt->pk ) ) ) != 0 )
1337 {
1338 return( ret );
1339 }
1340
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001341 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001342 (int) pk_get_size( &crt->pk ) );
1343 SAFE_SNPRINTF();
1344
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001345 /*
1346 * Optional extensions
1347 */
1348
1349 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1350 {
1351 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1352 crt->ca_istrue ? "true" : "false" );
1353 SAFE_SNPRINTF();
1354
1355 if( crt->max_pathlen > 0 )
1356 {
1357 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1358 SAFE_SNPRINTF();
1359 }
1360 }
1361
1362 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1363 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001364 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001365 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001366
1367 if( ( ret = x509_info_subject_alt_name( &p, &n,
1368 &crt->subject_alt_names ) ) != 0 )
1369 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001370 }
1371
1372 if( crt->ext_types & EXT_NS_CERT_TYPE )
1373 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001374 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001375 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001376
1377 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1378 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001379 }
1380
1381 if( crt->ext_types & EXT_KEY_USAGE )
1382 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001383 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001384 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001385
1386 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1387 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001388 }
1389
1390 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1391 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001392 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001393 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001394
1395 if( ( ret = x509_info_ext_key_usage( &p, &n,
1396 &crt->ext_key_usage ) ) != 0 )
1397 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001398 }
1399
1400 ret = snprintf( p, n, "\n" );
1401 SAFE_SNPRINTF();
1402
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001403 return( (int) ( size - n ) );
1404}
1405
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001406#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1407int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1408{
1409 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1410 ( crt->key_usage & usage ) != usage )
1411 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1412
1413 return( 0 );
1414}
1415#endif
1416
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001417#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1418int x509_crt_check_extended_key_usage( const x509_crt *crt,
1419 const char *usage_oid,
1420 size_t usage_len )
1421{
1422 const x509_sequence *cur;
1423
1424 /* Extension is not mandatory, absent means no restriction */
1425 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1426 return( 0 );
1427
1428 /*
1429 * Look for the requested usage (or wildcard ANY) in our list
1430 */
1431 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1432 {
1433 const x509_buf *cur_oid = &cur->buf;
1434
1435 if( cur_oid->len == usage_len &&
1436 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1437 {
1438 return( 0 );
1439 }
1440
1441 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1442 return( 0 );
1443 }
1444
1445 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1446}
Paul Bakker9af723c2014-05-01 13:03:14 +02001447#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001448
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001449#if defined(POLARSSL_X509_CRL_PARSE_C)
1450/*
1451 * Return 1 if the certificate is revoked, or 0 otherwise.
1452 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001453int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001454{
1455 const x509_crl_entry *cur = &crl->entry;
1456
1457 while( cur != NULL && cur->serial.len != 0 )
1458 {
1459 if( crt->serial.len == cur->serial.len &&
1460 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1461 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001462 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001463 return( 1 );
1464 }
1465
1466 cur = cur->next;
1467 }
1468
1469 return( 0 );
1470}
1471
1472/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001473 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001474 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001475static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001476 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001477{
1478 int flags = 0;
1479 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1480 const md_info_t *md_info;
1481
1482 if( ca == NULL )
1483 return( flags );
1484
1485 /*
1486 * TODO: What happens if no CRL is present?
1487 * Suggestion: Revocation state should be unknown if no CRL is present.
1488 * For backwards compatibility this is not yet implemented.
1489 */
1490
1491 while( crl_list != NULL )
1492 {
1493 if( crl_list->version == 0 ||
1494 crl_list->issuer_raw.len != ca->subject_raw.len ||
1495 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1496 crl_list->issuer_raw.len ) != 0 )
1497 {
1498 crl_list = crl_list->next;
1499 continue;
1500 }
1501
1502 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001503 * Check if the CA is configured to sign CRLs
1504 */
1505#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1506 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1507 {
1508 flags |= BADCRL_NOT_TRUSTED;
1509 break;
1510 }
1511#endif
1512
1513 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514 * Check if CRL is correctly signed by the trusted CA
1515 */
1516 md_info = md_info_from_type( crl_list->sig_md );
1517 if( md_info == NULL )
1518 {
1519 /*
1520 * Cannot check 'unknown' hash
1521 */
1522 flags |= BADCRL_NOT_TRUSTED;
1523 break;
1524 }
1525
1526 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1527
1528 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1529 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1530 crl_list->sig.p, crl_list->sig.len ) != 0 )
1531 {
1532 flags |= BADCRL_NOT_TRUSTED;
1533 break;
1534 }
1535
1536 /*
1537 * Check for validity of CRL (Do not drop out)
1538 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001539 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001540 flags |= BADCRL_EXPIRED;
1541
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001542 if( x509_time_future( &crl_list->this_update ) )
1543 flags |= BADCRL_FUTURE;
1544
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001545 /*
1546 * Check if certificate is revoked
1547 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001548 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549 {
1550 flags |= BADCERT_REVOKED;
1551 break;
1552 }
1553
1554 crl_list = crl_list->next;
1555 }
1556 return flags;
1557}
1558#endif /* POLARSSL_X509_CRL_PARSE_C */
1559
1560// Equal == 0, inequal == 1
1561static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1562{
1563 size_t i;
1564 unsigned char diff;
1565 const unsigned char *n1 = s1, *n2 = s2;
1566
1567 for( i = 0; i < len; i++ )
1568 {
1569 diff = n1[i] ^ n2[i];
1570
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001571 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001572 continue;
1573
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001574 if( diff == 32 &&
1575 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1576 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1577 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001578 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001579 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001580
1581 return( 1 );
1582 }
1583
1584 return( 0 );
1585}
1586
1587static int x509_wildcard_verify( const char *cn, x509_buf *name )
1588{
1589 size_t i;
1590 size_t cn_idx = 0;
1591
1592 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1593 return( 0 );
1594
1595 for( i = 0; i < strlen( cn ); ++i )
1596 {
1597 if( cn[i] == '.' )
1598 {
1599 cn_idx = i;
1600 break;
1601 }
1602 }
1603
1604 if( cn_idx == 0 )
1605 return( 0 );
1606
1607 if( strlen( cn ) - cn_idx == name->len - 1 &&
1608 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1609 {
1610 return( 1 );
1611 }
1612
1613 return( 0 );
1614}
1615
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001616/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001617 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1618 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001619 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001620static int x509_crt_check_parent( const x509_crt *child,
1621 const x509_crt *parent )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001622{
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001623 if( parent->version == 0 ||
1624 parent->ca_istrue == 0 ||
1625 child->issuer_raw.len != parent->subject_raw.len ||
1626 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1627 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001628 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001629 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001630 }
1631
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001632#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1633 if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1634 return( -1 );
1635#endif
1636
1637 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001638}
1639
Paul Bakkerddf26b42013-09-18 13:46:23 +02001640static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001641 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001642 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001643 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001644 void *p_vrfy )
1645{
1646 int ret;
1647 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1648 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1649 const md_info_t *md_info;
1650
Paul Bakker86d0c192013-09-18 11:11:02 +02001651 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001652 *flags |= BADCERT_EXPIRED;
1653
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001654 if( x509_time_future( &child->valid_from ) )
1655 *flags |= BADCERT_FUTURE;
1656
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001657 /*
1658 * Child is the top of the chain. Check against the trust_ca list.
1659 */
1660 *flags |= BADCERT_NOT_TRUSTED;
1661
1662 md_info = md_info_from_type( child->sig_md );
1663 if( md_info == NULL )
1664 {
1665 /*
1666 * Cannot check 'unknown', no need to try any CA
1667 */
1668 trust_ca = NULL;
1669 }
1670 else
1671 md( md_info, child->tbs.p, child->tbs.len, hash );
1672
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001673 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001674 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001675 if( x509_crt_check_parent( child, trust_ca ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001676 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001677
1678 /*
1679 * Reduce path_len to check against if top of the chain is
1680 * the same as the trusted CA
1681 */
1682 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1683 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1684 child->issuer_raw.len ) == 0 )
1685 {
1686 check_path_cnt--;
1687 }
1688
1689 if( trust_ca->max_pathlen > 0 &&
1690 trust_ca->max_pathlen < check_path_cnt )
1691 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001692 continue;
1693 }
1694
1695 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1696 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1697 child->sig.p, child->sig.len ) != 0 )
1698 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001699 continue;
1700 }
1701
1702 /*
1703 * Top of chain is signed by a trusted CA
1704 */
1705 *flags &= ~BADCERT_NOT_TRUSTED;
1706 break;
1707 }
1708
1709 /*
1710 * If top of chain is not the same as the trusted CA send a verify request
1711 * to the callback for any issues with validity and CRL presence for the
1712 * trusted CA certificate.
1713 */
1714 if( trust_ca != NULL &&
1715 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1716 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1717 child->issuer_raw.len ) != 0 ) )
1718 {
1719#if defined(POLARSSL_X509_CRL_PARSE_C)
1720 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001721 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001722#else
1723 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001724#endif
1725
Paul Bakker86d0c192013-09-18 11:11:02 +02001726 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001727 ca_flags |= BADCERT_EXPIRED;
1728
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001729 if( x509_time_future( &trust_ca->valid_from ) )
1730 ca_flags |= BADCERT_FUTURE;
1731
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001732 if( NULL != f_vrfy )
1733 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001734 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1735 &ca_flags ) ) != 0 )
1736 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001737 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001738 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001739 }
1740 }
1741
1742 /* Call callback on top cert */
1743 if( NULL != f_vrfy )
1744 {
1745 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1746 return( ret );
1747 }
1748
1749 *flags |= ca_flags;
1750
1751 return( 0 );
1752}
1753
Paul Bakkerddf26b42013-09-18 13:46:23 +02001754static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001755 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001756 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001757 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001758 void *p_vrfy )
1759{
1760 int ret;
1761 int parent_flags = 0;
1762 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001763 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764 const md_info_t *md_info;
1765
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001766 if( x509_time_expired( &child->valid_to ) )
1767 *flags |= BADCERT_EXPIRED;
1768
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001769 if( x509_time_future( &child->valid_from ) )
1770 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001771
1772 md_info = md_info_from_type( child->sig_md );
1773 if( md_info == NULL )
1774 {
1775 /*
1776 * Cannot check 'unknown' hash
1777 */
1778 *flags |= BADCERT_NOT_TRUSTED;
1779 }
1780 else
1781 {
1782 md( md_info, child->tbs.p, child->tbs.len, hash );
1783
1784 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1785 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1786 child->sig.p, child->sig.len ) != 0 )
1787 {
1788 *flags |= BADCERT_NOT_TRUSTED;
1789 }
1790 }
1791
1792#if defined(POLARSSL_X509_CRL_PARSE_C)
1793 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001794 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001795#endif
1796
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001797 /* Look for a grandparent upwards the chain */
1798 for( grandparent = parent->next;
1799 grandparent != NULL;
1800 grandparent = grandparent->next )
1801 {
1802 if( x509_crt_check_parent( parent, grandparent ) == 0 )
1803 break;
1804 }
1805
1806 /* Is our parent part of the chain or at the top? */
1807 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001808 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001809 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1810 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001811 if( ret != 0 )
1812 return( ret );
1813 }
1814 else
1815 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001816 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1817 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001818 if( ret != 0 )
1819 return( ret );
1820 }
1821
1822 /* child is verified to be a child of the parent, call verify callback */
1823 if( NULL != f_vrfy )
1824 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1825 return( ret );
1826
1827 *flags |= parent_flags;
1828
1829 return( 0 );
1830}
1831
1832/*
1833 * Verify the certificate validity
1834 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001835int x509_crt_verify( x509_crt *crt,
1836 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001837 x509_crl *ca_crl,
1838 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001839 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001840 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001841{
1842 size_t cn_len;
1843 int ret;
1844 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001845 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001846 x509_name *name;
1847 x509_sequence *cur = NULL;
1848
1849 *flags = 0;
1850
1851 if( cn != NULL )
1852 {
1853 name = &crt->subject;
1854 cn_len = strlen( cn );
1855
1856 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1857 {
1858 cur = &crt->subject_alt_names;
1859
1860 while( cur != NULL )
1861 {
1862 if( cur->buf.len == cn_len &&
1863 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1864 break;
1865
1866 if( cur->buf.len > 2 &&
1867 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1868 x509_wildcard_verify( cn, &cur->buf ) )
1869 break;
1870
1871 cur = cur->next;
1872 }
1873
1874 if( cur == NULL )
1875 *flags |= BADCERT_CN_MISMATCH;
1876 }
1877 else
1878 {
1879 while( name != NULL )
1880 {
1881 if( OID_CMP( OID_AT_CN, &name->oid ) )
1882 {
1883 if( name->val.len == cn_len &&
1884 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1885 break;
1886
1887 if( name->val.len > 2 &&
1888 memcmp( name->val.p, "*.", 2 ) == 0 &&
1889 x509_wildcard_verify( cn, &name->val ) )
1890 break;
1891 }
1892
1893 name = name->next;
1894 }
1895
1896 if( name == NULL )
1897 *flags |= BADCERT_CN_MISMATCH;
1898 }
1899 }
1900
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001901 /* Look for a parent upwards the chain */
1902 for( parent = crt->next; parent != NULL; parent = parent->next )
1903 {
1904 if( x509_crt_check_parent( crt, parent ) == 0 )
1905 break;
1906 }
1907
1908 /* Are we part of the chain or at the top? */
1909 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001910 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001911 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1912 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001913 if( ret != 0 )
1914 return( ret );
1915 }
1916 else
1917 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001918 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1919 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001920 if( ret != 0 )
1921 return( ret );
1922 }
1923
1924 if( *flags != 0 )
1925 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1926
1927 return( 0 );
1928}
1929
1930/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001931 * Initialize a certificate chain
1932 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001933void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001934{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001935 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001936}
1937
1938/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001939 * Unallocate all certificate data
1940 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001941void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001942{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001943 x509_crt *cert_cur = crt;
1944 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001945 x509_name *name_cur;
1946 x509_name *name_prv;
1947 x509_sequence *seq_cur;
1948 x509_sequence *seq_prv;
1949
1950 if( crt == NULL )
1951 return;
1952
1953 do
1954 {
1955 pk_free( &cert_cur->pk );
1956
1957 name_cur = cert_cur->issuer.next;
1958 while( name_cur != NULL )
1959 {
1960 name_prv = name_cur;
1961 name_cur = name_cur->next;
1962 memset( name_prv, 0, sizeof( x509_name ) );
1963 polarssl_free( name_prv );
1964 }
1965
1966 name_cur = cert_cur->subject.next;
1967 while( name_cur != NULL )
1968 {
1969 name_prv = name_cur;
1970 name_cur = name_cur->next;
1971 memset( name_prv, 0, sizeof( x509_name ) );
1972 polarssl_free( name_prv );
1973 }
1974
1975 seq_cur = cert_cur->ext_key_usage.next;
1976 while( seq_cur != NULL )
1977 {
1978 seq_prv = seq_cur;
1979 seq_cur = seq_cur->next;
1980 memset( seq_prv, 0, sizeof( x509_sequence ) );
1981 polarssl_free( seq_prv );
1982 }
1983
1984 seq_cur = cert_cur->subject_alt_names.next;
1985 while( seq_cur != NULL )
1986 {
1987 seq_prv = seq_cur;
1988 seq_cur = seq_cur->next;
1989 memset( seq_prv, 0, sizeof( x509_sequence ) );
1990 polarssl_free( seq_prv );
1991 }
1992
1993 if( cert_cur->raw.p != NULL )
1994 {
1995 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1996 polarssl_free( cert_cur->raw.p );
1997 }
1998
1999 cert_cur = cert_cur->next;
2000 }
2001 while( cert_cur != NULL );
2002
2003 cert_cur = crt;
2004 do
2005 {
2006 cert_prv = cert_cur;
2007 cert_cur = cert_cur->next;
2008
Paul Bakkerc559c7a2013-09-18 14:13:26 +02002009 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02002010 if( cert_prv != crt )
2011 polarssl_free( cert_prv );
2012 }
2013 while( cert_cur != NULL );
2014}
2015
Paul Bakker9af723c2014-05-01 13:03:14 +02002016#endif /* POLARSSL_X509_CRT_PARSE_C */