blob: b9f226b076ea1ee9ba66fd1363e7385c2693ccc9 [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
625 /*
626 * issuer Name
627 */
628 crt->issuer_raw.p = p;
629
630 if( ( ret = asn1_get_tag( &p, end, &len,
631 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
632 {
633 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200634 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 }
636
637 if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
638 {
639 x509_crt_free( crt );
640 return( ret );
641 }
642
643 crt->issuer_raw.len = p - crt->issuer_raw.p;
644
645 /*
646 * Validity ::= SEQUENCE {
647 * notBefore Time,
648 * notAfter Time }
649 *
650 */
651 if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
652 &crt->valid_to ) ) != 0 )
653 {
654 x509_crt_free( crt );
655 return( ret );
656 }
657
658 /*
659 * subject Name
660 */
661 crt->subject_raw.p = p;
662
663 if( ( ret = asn1_get_tag( &p, end, &len,
664 ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
665 {
666 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200667 return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668 }
669
670 if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
671 {
672 x509_crt_free( crt );
673 return( ret );
674 }
675
676 crt->subject_raw.len = p - crt->subject_raw.p;
677
678 /*
679 * SubjectPublicKeyInfo
680 */
Paul Bakkerda771152013-09-16 22:45:03 +0200681 if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682 {
683 x509_crt_free( crt );
684 return( ret );
685 }
686
687 /*
688 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
689 * -- If present, version shall be v2 or v3
690 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
691 * -- If present, version shall be v2 or v3
692 * extensions [3] EXPLICIT Extensions OPTIONAL
693 * -- If present, version shall be v3
694 */
695 if( crt->version == 2 || crt->version == 3 )
696 {
697 ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
698 if( ret != 0 )
699 {
700 x509_crt_free( crt );
701 return( ret );
702 }
703 }
704
705 if( crt->version == 2 || crt->version == 3 )
706 {
707 ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
708 if( ret != 0 )
709 {
710 x509_crt_free( crt );
711 return( ret );
712 }
713 }
714
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200715#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200716 if( crt->version == 3 )
717 {
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200718#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200719 ret = x509_get_crt_ext( &p, end, crt);
720 if( ret != 0 )
721 {
722 x509_crt_free( crt );
723 return( ret );
724 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200725#if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200726 }
Paul Bakkerc27c4e22013-09-23 15:01:36 +0200727#endif
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200728
729 if( p != end )
730 {
731 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200732 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200733 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
734 }
735
736 end = crt_end;
737
738 /*
739 * }
740 * -- end of TBSCertificate
741 *
742 * signatureAlgorithm AlgorithmIdentifier,
743 * signatureValue BIT STRING
744 */
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100745 if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 {
747 x509_crt_free( crt );
748 return( ret );
749 }
750
751 if( crt->sig_oid1.len != crt->sig_oid2.len ||
Manuel Pégourié-Gonnard59a75d52014-01-22 10:12:57 +0100752 memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
753 crt->sig_params.len != sig_params.len ||
754 memcmp( crt->sig_params.p, sig_params.p, sig_params.len ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200755 {
756 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200757 return( POLARSSL_ERR_X509_SIG_MISMATCH );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200758 }
759
760 if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
761 {
762 x509_crt_free( crt );
763 return( ret );
764 }
765
766 if( p != end )
767 {
768 x509_crt_free( crt );
Paul Bakker51876562013-09-17 14:36:05 +0200769 return( POLARSSL_ERR_X509_INVALID_FORMAT +
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200770 POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
771 }
772
773 return( 0 );
774}
775
776/*
777 * Parse one X.509 certificate in DER format from a buffer and add them to a
778 * chained list
779 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200780int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
Paul Bakkerddf26b42013-09-18 13:46:23 +0200781 size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782{
783 int ret;
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200784 x509_crt *crt = chain, *prev = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785
786 /*
787 * Check for valid input
788 */
789 if( crt == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200790 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791
792 while( crt->version != 0 && crt->next != NULL )
793 {
794 prev = crt;
795 crt = crt->next;
796 }
797
798 /*
799 * Add new certificate on the end of the chain if needed.
800 */
801 if ( crt->version != 0 && crt->next == NULL)
802 {
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200803 crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200804
805 if( crt->next == NULL )
806 return( POLARSSL_ERR_X509_MALLOC_FAILED );
807
808 prev = crt;
809 crt = crt->next;
Paul Bakker369d2eb2013-09-18 11:58:25 +0200810 x509_crt_init( crt );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200811 }
812
Paul Bakkerddf26b42013-09-18 13:46:23 +0200813 if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200814 {
815 if( prev )
816 prev->next = NULL;
817
818 if( crt != chain )
819 polarssl_free( crt );
820
821 return( ret );
822 }
823
824 return( 0 );
825}
826
827/*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200828 * Parse one or more PEM certificates from a buffer and add them to the chained
829 * list
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200830 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200831int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200832{
833 int success = 0, first_error = 0, total_failed = 0;
834 int buf_format = X509_FORMAT_DER;
835
836 /*
837 * Check for valid input
838 */
839 if( chain == NULL || buf == NULL )
Paul Bakker51876562013-09-17 14:36:05 +0200840 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200841
842 /*
843 * Determine buffer content. Buffer contains either one DER certificate or
844 * one or more PEM certificates.
845 */
846#if defined(POLARSSL_PEM_PARSE_C)
847 if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
848 buf_format = X509_FORMAT_PEM;
849#endif
850
851 if( buf_format == X509_FORMAT_DER )
Paul Bakkerddf26b42013-09-18 13:46:23 +0200852 return x509_crt_parse_der( chain, buf, buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200853
854#if defined(POLARSSL_PEM_PARSE_C)
855 if( buf_format == X509_FORMAT_PEM )
856 {
857 int ret;
858 pem_context pem;
859
860 while( buflen > 0 )
861 {
862 size_t use_len;
863 pem_init( &pem );
864
865 ret = pem_read_buffer( &pem,
866 "-----BEGIN CERTIFICATE-----",
867 "-----END CERTIFICATE-----",
868 buf, NULL, 0, &use_len );
869
870 if( ret == 0 )
871 {
872 /*
873 * Was PEM encoded
874 */
875 buflen -= use_len;
876 buf += use_len;
877 }
878 else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
879 {
880 return( ret );
881 }
882 else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
883 {
884 pem_free( &pem );
885
886 /*
887 * PEM header and footer were found
888 */
889 buflen -= use_len;
890 buf += use_len;
891
892 if( first_error == 0 )
893 first_error = ret;
894
895 continue;
896 }
897 else
898 break;
899
Paul Bakkerddf26b42013-09-18 13:46:23 +0200900 ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200901
902 pem_free( &pem );
903
904 if( ret != 0 )
905 {
906 /*
907 * Quit parsing on a memory error
908 */
909 if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
910 return( ret );
911
912 if( first_error == 0 )
913 first_error = ret;
914
915 total_failed++;
916 continue;
917 }
918
919 success = 1;
920 }
921 }
Paul Bakker9af723c2014-05-01 13:03:14 +0200922#endif /* POLARSSL_PEM_PARSE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200923
924 if( success )
925 return( total_failed );
926 else if( first_error )
927 return( first_error );
928 else
929 return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
930}
931
932#if defined(POLARSSL_FS_IO)
933/*
934 * Load one or more certificates and add them to the chained list
935 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200936int x509_crt_parse_file( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200937{
938 int ret;
939 size_t n;
940 unsigned char *buf;
941
942 if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
943 return( ret );
944
Paul Bakkerddf26b42013-09-18 13:46:23 +0200945 ret = x509_crt_parse( chain, buf, n );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200946
947 memset( buf, 0, n + 1 );
948 polarssl_free( buf );
949
950 return( ret );
951}
952
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +0100953#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +0100954static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
955#endif
956
Paul Bakkerc559c7a2013-09-18 14:13:26 +0200957int x509_crt_parse_path( x509_crt *chain, const char *path )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200958{
959 int ret = 0;
Paul Bakkerfa6a6202013-10-28 18:48:30 +0100960#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200961 int w_ret;
962 WCHAR szDir[MAX_PATH];
963 char filename[MAX_PATH];
Paul Bakker9af723c2014-05-01 13:03:14 +0200964 char *p;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200965 int len = (int) strlen( path );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200966
Paul Bakker9af723c2014-05-01 13:03:14 +0200967 WIN32_FIND_DATAW file_data;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200968 HANDLE hFind;
969
970 if( len > MAX_PATH - 3 )
Paul Bakker3cf63ed2013-09-23 15:10:16 +0200971 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200972
Paul Bakker9af723c2014-05-01 13:03:14 +0200973 memset( szDir, 0, sizeof(szDir) );
974 memset( filename, 0, MAX_PATH );
975 memcpy( filename, path, len );
976 filename[len++] = '\\';
977 p = filename + len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200978 filename[len++] = '*';
979
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200980 w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
981 MAX_PATH - 3 );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200982
983 hFind = FindFirstFileW( szDir, &file_data );
Paul Bakker4aa40d42013-10-11 10:49:24 +0200984 if (hFind == INVALID_HANDLE_VALUE)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200985 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
986
987 len = MAX_PATH - len;
988 do
989 {
Paul Bakker9af723c2014-05-01 13:03:14 +0200990 memset( p, 0, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200991
992 if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
993 continue;
994
Paul Bakker9af723c2014-05-01 13:03:14 +0200995 w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
996 lstrlenW(file_data.cFileName),
997 p, len - 1,
998 NULL, NULL );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200999
Paul Bakkerddf26b42013-09-18 13:46:23 +02001000 w_ret = x509_crt_parse_file( chain, filename );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001001 if( w_ret < 0 )
1002 ret++;
1003 else
1004 ret += w_ret;
1005 }
1006 while( FindNextFileW( hFind, &file_data ) != 0 );
1007
Paul Bakker4aa40d42013-10-11 10:49:24 +02001008 if (GetLastError() != ERROR_NO_MORE_FILES)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001009 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1010
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001011 FindClose( hFind );
Paul Bakkerbe089b02013-10-14 15:51:50 +02001012#else /* _WIN32 */
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001013 int t_ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001014 struct stat sb;
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001015 struct dirent *entry;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001016 char entry_name[255];
1017 DIR *dir = opendir( path );
1018
1019 if( dir == NULL)
1020 return( POLARSSL_ERR_X509_FILE_IO_ERROR );
1021
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001022#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001023 if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1024 return( ret );
1025#endif
1026
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001027 while( ( entry = readdir( dir ) ) != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001028 {
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001029 snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001030
Manuel Pégourié-Gonnard964bf9b2013-11-26 16:47:11 +01001031 if( stat( entry_name, &sb ) == -1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001032 {
1033 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001034 ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
1035 goto cleanup;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001036 }
1037
1038 if( !S_ISREG( sb.st_mode ) )
1039 continue;
1040
1041 // Ignore parse errors
1042 //
Paul Bakkerddf26b42013-09-18 13:46:23 +02001043 t_ret = x509_crt_parse_file( chain, entry_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001044 if( t_ret < 0 )
1045 ret++;
1046 else
1047 ret += t_ret;
1048 }
1049 closedir( dir );
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001050
1051cleanup:
Manuel Pégourié-Gonnard6df09572014-02-12 09:29:05 +01001052#if defined(POLARSSL_THREADING_PTHREAD)
Manuel Pégourié-Gonnard5ad68e42013-11-28 17:11:54 +01001053 if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1054 ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
1055#endif
1056
Paul Bakkerbe089b02013-10-14 15:51:50 +02001057#endif /* _WIN32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001058
1059 return( ret );
1060}
1061#endif /* POLARSSL_FS_IO */
1062
Paul Bakker6edcd412013-10-29 15:22:54 +01001063#if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1064 !defined(EFI32)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001065#include <stdarg.h>
1066
1067#if !defined vsnprintf
1068#define vsnprintf _vsnprintf
1069#endif // vsnprintf
1070
1071/*
1072 * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1073 * Result value is not size of buffer needed, but -1 if no fit is possible.
1074 *
1075 * This fuction tries to 'fix' this by at least suggesting enlarging the
1076 * size by 20.
1077 */
1078static int compat_snprintf(char *str, size_t size, const char *format, ...)
1079{
1080 va_list ap;
1081 int res = -1;
1082
1083 va_start( ap, format );
1084
1085 res = vsnprintf( str, size, format, ap );
1086
1087 va_end( ap );
1088
1089 // No quick fix possible
1090 if ( res < 0 )
1091 return( (int) size + 20 );
1092
1093 return res;
1094}
1095
1096#define snprintf compat_snprintf
Paul Bakker9af723c2014-05-01 13:03:14 +02001097#endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001098
1099#define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1100
1101#define SAFE_SNPRINTF() \
1102{ \
1103 if( ret == -1 ) \
1104 return( -1 ); \
1105 \
1106 if ( (unsigned int) ret > n ) { \
1107 p[n - 1] = '\0'; \
1108 return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1109 } \
1110 \
1111 n -= (unsigned int) ret; \
1112 p += (unsigned int) ret; \
1113}
1114
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001115static int x509_info_subject_alt_name( char **buf, size_t *size,
1116 const x509_sequence *subject_alt_name )
1117{
1118 size_t i;
1119 size_t n = *size;
1120 char *p = *buf;
1121 const x509_sequence *cur = subject_alt_name;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001122 const char *sep = "";
1123 size_t sep_len = 0;
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001124
1125 while( cur != NULL )
1126 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001127 if( cur->buf.len + sep_len >= n )
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001128 {
1129 *p = '\0';
1130 return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1131 }
1132
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001133 n -= cur->buf.len + sep_len;
1134 for( i = 0; i < sep_len; i++ )
1135 *p++ = sep[i];
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001136 for( i = 0; i < cur->buf.len; i++ )
1137 *p++ = cur->buf.p[i];
1138
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001139 sep = ", ";
1140 sep_len = 2;
1141
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001142 cur = cur->next;
1143 }
1144
1145 *p = '\0';
1146
1147 *size = n;
1148 *buf = p;
1149
1150 return( 0 );
1151}
1152
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001153#define PRINT_ITEM(i) \
1154 { \
1155 ret = snprintf( p, n, "%s" i, sep ); \
1156 SAFE_SNPRINTF(); \
1157 sep = ", "; \
1158 }
1159
1160#define CERT_TYPE(type,name) \
1161 if( ns_cert_type & type ) \
1162 PRINT_ITEM( name );
1163
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001164static int x509_info_cert_type( char **buf, size_t *size,
1165 unsigned char ns_cert_type )
1166{
1167 int ret;
1168 size_t n = *size;
1169 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001170 const char *sep = "";
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001171
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001172 CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1173 CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1174 CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1175 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1176 CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1177 CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1178 CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1179 CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001180
1181 *size = n;
1182 *buf = p;
1183
1184 return( 0 );
1185}
1186
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001187#define KEY_USAGE(code,name) \
1188 if( key_usage & code ) \
1189 PRINT_ITEM( name );
1190
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001191static int x509_info_key_usage( char **buf, size_t *size,
1192 unsigned char key_usage )
1193{
1194 int ret;
1195 size_t n = *size;
1196 char *p = *buf;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001197 const char *sep = "";
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001198
Manuel Pégourié-Gonnard0db29b02014-04-01 18:12:24 +02001199 KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1200 KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1201 KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1202 KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1203 KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1204 KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1205 KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001206
1207 *size = n;
1208 *buf = p;
1209
1210 return( 0 );
1211}
1212
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001213static int x509_info_ext_key_usage( char **buf, size_t *size,
1214 const x509_sequence *extended_key_usage )
1215{
1216 int ret;
1217 const char *desc;
1218 size_t n = *size;
1219 char *p = *buf;
1220 const x509_sequence *cur = extended_key_usage;
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001221 const char *sep = "";
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001222
1223 while( cur != NULL )
1224 {
1225 if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1226 desc = "???";
1227
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001228 ret = snprintf( p, n, "%s%s", sep, desc );
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001229 SAFE_SNPRINTF();
1230
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001231 sep = ", ";
1232
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001233 cur = cur->next;
1234 }
1235
1236 *size = n;
1237 *buf = p;
1238
1239 return( 0 );
1240}
1241
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001242/*
1243 * Return an informational string about the certificate.
1244 */
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001245#define BEFORE_COLON 18
1246#define BC "18"
Paul Bakkerddf26b42013-09-18 13:46:23 +02001247int x509_crt_info( char *buf, size_t size, const char *prefix,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001248 const x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001249{
1250 int ret;
1251 size_t n;
1252 char *p;
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001253 const char *desc = NULL;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001254 char key_size_str[BEFORE_COLON];
1255
1256 p = buf;
1257 n = size;
1258
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001259 ret = snprintf( p, n, "%scert. version : %d\n",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001260 prefix, crt->version );
1261 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001262 ret = snprintf( p, n, "%sserial number : ",
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001263 prefix );
1264 SAFE_SNPRINTF();
1265
Paul Bakker86d0c192013-09-18 11:11:02 +02001266 ret = x509_serial_gets( p, n, &crt->serial);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001267 SAFE_SNPRINTF();
1268
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001269 ret = snprintf( p, n, "\n%sissuer name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001270 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001271 ret = x509_dn_gets( p, n, &crt->issuer );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001272 SAFE_SNPRINTF();
1273
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001274 ret = snprintf( p, n, "\n%ssubject name : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001275 SAFE_SNPRINTF();
Paul Bakker86d0c192013-09-18 11:11:02 +02001276 ret = x509_dn_gets( p, n, &crt->subject );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001277 SAFE_SNPRINTF();
1278
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001279 ret = snprintf( p, n, "\n%sissued on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001280 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1281 crt->valid_from.year, crt->valid_from.mon,
1282 crt->valid_from.day, crt->valid_from.hour,
1283 crt->valid_from.min, crt->valid_from.sec );
1284 SAFE_SNPRINTF();
1285
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001286 ret = snprintf( p, n, "\n%sexpires on : " \
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001287 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1288 crt->valid_to.year, crt->valid_to.mon,
1289 crt->valid_to.day, crt->valid_to.hour,
1290 crt->valid_to.min, crt->valid_to.sec );
1291 SAFE_SNPRINTF();
1292
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001293 ret = snprintf( p, n, "\n%ssigned using : ", prefix );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001294 SAFE_SNPRINTF();
1295
Manuel Pégourié-Gonnardc9093082014-02-12 09:39:59 +01001296 ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1297 if( ret != 0 )
1298 ret = snprintf( p, n, "???" );
1299 else
1300 ret = snprintf( p, n, "%s", desc );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001301 SAFE_SNPRINTF();
1302
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001303 /* Key size */
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001304 if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1305 pk_get_name( &crt->pk ) ) ) != 0 )
1306 {
1307 return( ret );
1308 }
1309
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001310 ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001311 (int) pk_get_size( &crt->pk ) );
1312 SAFE_SNPRINTF();
1313
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001314 /*
1315 * Optional extensions
1316 */
1317
1318 if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1319 {
1320 ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1321 crt->ca_istrue ? "true" : "false" );
1322 SAFE_SNPRINTF();
1323
1324 if( crt->max_pathlen > 0 )
1325 {
1326 ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1327 SAFE_SNPRINTF();
1328 }
1329 }
1330
1331 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1332 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001333 ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001334 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardbce2b302014-04-01 13:43:28 +02001335
1336 if( ( ret = x509_info_subject_alt_name( &p, &n,
1337 &crt->subject_alt_names ) ) != 0 )
1338 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001339 }
1340
1341 if( crt->ext_types & EXT_NS_CERT_TYPE )
1342 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001343 ret = snprintf( p, n, "\n%scert. type : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001344 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard919f8f52014-04-01 13:01:11 +02001345
1346 if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1347 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001348 }
1349
1350 if( crt->ext_types & EXT_KEY_USAGE )
1351 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001352 ret = snprintf( p, n, "\n%skey usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001353 SAFE_SNPRINTF();
Manuel Pégourié-Gonnard65c2ddc2014-04-01 14:12:11 +02001354
1355 if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1356 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001357 }
1358
1359 if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1360 {
Manuel Pégourié-Gonnard7b30cfc2014-04-01 18:00:07 +02001361 ret = snprintf( p, n, "\n%sext key usage : ", prefix );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001362 SAFE_SNPRINTF();
Manuel Pégourié-Gonnardf6f4ab42014-04-01 17:32:44 +02001363
1364 if( ( ret = x509_info_ext_key_usage( &p, &n,
1365 &crt->ext_key_usage ) ) != 0 )
1366 return( ret );
Manuel Pégourié-Gonnardb28487d2014-04-01 12:19:09 +02001367 }
1368
1369 ret = snprintf( p, n, "\n" );
1370 SAFE_SNPRINTF();
1371
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001372 return( (int) ( size - n ) );
1373}
1374
Manuel Pégourié-Gonnard603116c2014-04-09 09:50:03 +02001375#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1376int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1377{
1378 if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1379 ( crt->key_usage & usage ) != usage )
1380 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1381
1382 return( 0 );
1383}
1384#endif
1385
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001386#if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1387int x509_crt_check_extended_key_usage( const x509_crt *crt,
1388 const char *usage_oid,
1389 size_t usage_len )
1390{
1391 const x509_sequence *cur;
1392
1393 /* Extension is not mandatory, absent means no restriction */
1394 if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1395 return( 0 );
1396
1397 /*
1398 * Look for the requested usage (or wildcard ANY) in our list
1399 */
1400 for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1401 {
1402 const x509_buf *cur_oid = &cur->buf;
1403
1404 if( cur_oid->len == usage_len &&
1405 memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1406 {
1407 return( 0 );
1408 }
1409
1410 if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1411 return( 0 );
1412 }
1413
1414 return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
1415}
Paul Bakker9af723c2014-05-01 13:03:14 +02001416#endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard7afb8a02014-04-10 17:53:56 +02001417
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001418#if defined(POLARSSL_X509_CRL_PARSE_C)
1419/*
1420 * Return 1 if the certificate is revoked, or 0 otherwise.
1421 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001422int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001423{
1424 const x509_crl_entry *cur = &crl->entry;
1425
1426 while( cur != NULL && cur->serial.len != 0 )
1427 {
1428 if( crt->serial.len == cur->serial.len &&
1429 memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1430 {
Paul Bakker86d0c192013-09-18 11:11:02 +02001431 if( x509_time_expired( &cur->revocation_date ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001432 return( 1 );
1433 }
1434
1435 cur = cur->next;
1436 }
1437
1438 return( 0 );
1439}
1440
1441/*
Paul Bakker60b1d102013-10-29 10:02:51 +01001442 * Check that the given certificate is valid according to the CRL.
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001443 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001444static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001445 x509_crl *crl_list)
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001446{
1447 int flags = 0;
1448 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1449 const md_info_t *md_info;
1450
1451 if( ca == NULL )
1452 return( flags );
1453
1454 /*
1455 * TODO: What happens if no CRL is present?
1456 * Suggestion: Revocation state should be unknown if no CRL is present.
1457 * For backwards compatibility this is not yet implemented.
1458 */
1459
1460 while( crl_list != NULL )
1461 {
1462 if( crl_list->version == 0 ||
1463 crl_list->issuer_raw.len != ca->subject_raw.len ||
1464 memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1465 crl_list->issuer_raw.len ) != 0 )
1466 {
1467 crl_list = crl_list->next;
1468 continue;
1469 }
1470
1471 /*
Manuel Pégourié-Gonnard99d4f192014-04-08 15:10:07 +02001472 * Check if the CA is configured to sign CRLs
1473 */
1474#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1475 if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1476 {
1477 flags |= BADCRL_NOT_TRUSTED;
1478 break;
1479 }
1480#endif
1481
1482 /*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001483 * Check if CRL is correctly signed by the trusted CA
1484 */
1485 md_info = md_info_from_type( crl_list->sig_md );
1486 if( md_info == NULL )
1487 {
1488 /*
1489 * Cannot check 'unknown' hash
1490 */
1491 flags |= BADCRL_NOT_TRUSTED;
1492 break;
1493 }
1494
1495 md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1496
1497 if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1498 pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1499 crl_list->sig.p, crl_list->sig.len ) != 0 )
1500 {
1501 flags |= BADCRL_NOT_TRUSTED;
1502 break;
1503 }
1504
1505 /*
1506 * Check for validity of CRL (Do not drop out)
1507 */
Paul Bakker86d0c192013-09-18 11:11:02 +02001508 if( x509_time_expired( &crl_list->next_update ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001509 flags |= BADCRL_EXPIRED;
1510
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001511 if( x509_time_future( &crl_list->this_update ) )
1512 flags |= BADCRL_FUTURE;
1513
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001514 /*
1515 * Check if certificate is revoked
1516 */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001517 if( x509_crt_revoked(crt, crl_list) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001518 {
1519 flags |= BADCERT_REVOKED;
1520 break;
1521 }
1522
1523 crl_list = crl_list->next;
1524 }
1525 return flags;
1526}
1527#endif /* POLARSSL_X509_CRL_PARSE_C */
1528
1529// Equal == 0, inequal == 1
1530static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1531{
1532 size_t i;
1533 unsigned char diff;
1534 const unsigned char *n1 = s1, *n2 = s2;
1535
1536 for( i = 0; i < len; i++ )
1537 {
1538 diff = n1[i] ^ n2[i];
1539
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001540 if( diff == 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001541 continue;
1542
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001543 if( diff == 32 &&
1544 ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1545 ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1546 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001547 continue;
Paul Bakkerf2b4d862013-11-20 17:23:53 +01001548 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001549
1550 return( 1 );
1551 }
1552
1553 return( 0 );
1554}
1555
1556static int x509_wildcard_verify( const char *cn, x509_buf *name )
1557{
1558 size_t i;
1559 size_t cn_idx = 0;
1560
1561 if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1562 return( 0 );
1563
1564 for( i = 0; i < strlen( cn ); ++i )
1565 {
1566 if( cn[i] == '.' )
1567 {
1568 cn_idx = i;
1569 break;
1570 }
1571 }
1572
1573 if( cn_idx == 0 )
1574 return( 0 );
1575
1576 if( strlen( cn ) - cn_idx == name->len - 1 &&
1577 x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1578 {
1579 return( 1 );
1580 }
1581
1582 return( 0 );
1583}
1584
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001585/*
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001586 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1587 * Return 0 if yes, -1 if not.
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001588 */
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001589static int x509_crt_check_parent( const x509_crt *child,
1590 const x509_crt *parent )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001591{
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001592 if( parent->version == 0 ||
1593 parent->ca_istrue == 0 ||
1594 child->issuer_raw.len != parent->subject_raw.len ||
1595 memcmp( child->issuer_raw.p, parent->subject_raw.p,
1596 child->issuer_raw.len ) != 0 )
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001597 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001598 return( -1 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001599 }
1600
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001601#if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1602 if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1603 return( -1 );
1604#endif
1605
1606 return( 0 );
Manuel Pégourié-Gonnard3fed0b32014-04-08 13:18:01 +02001607}
1608
Paul Bakkerddf26b42013-09-18 13:46:23 +02001609static int x509_crt_verify_top(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001610 x509_crt *child, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001611 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001612 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001613 void *p_vrfy )
1614{
1615 int ret;
1616 int ca_flags = 0, check_path_cnt = path_cnt + 1;
1617 unsigned char hash[POLARSSL_MD_MAX_SIZE];
1618 const md_info_t *md_info;
1619
Paul Bakker86d0c192013-09-18 11:11:02 +02001620 if( x509_time_expired( &child->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001621 *flags |= BADCERT_EXPIRED;
1622
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001623 if( x509_time_future( &child->valid_from ) )
1624 *flags |= BADCERT_FUTURE;
1625
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001626 /*
1627 * Child is the top of the chain. Check against the trust_ca list.
1628 */
1629 *flags |= BADCERT_NOT_TRUSTED;
1630
1631 md_info = md_info_from_type( child->sig_md );
1632 if( md_info == NULL )
1633 {
1634 /*
1635 * Cannot check 'unknown', no need to try any CA
1636 */
1637 trust_ca = NULL;
1638 }
1639 else
1640 md( md_info, child->tbs.p, child->tbs.len, hash );
1641
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001642 for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001643 {
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001644 if( x509_crt_check_parent( child, trust_ca ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001645 continue;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001646
1647 /*
1648 * Reduce path_len to check against if top of the chain is
1649 * the same as the trusted CA
1650 */
1651 if( child->subject_raw.len == trust_ca->subject_raw.len &&
1652 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1653 child->issuer_raw.len ) == 0 )
1654 {
1655 check_path_cnt--;
1656 }
1657
1658 if( trust_ca->max_pathlen > 0 &&
1659 trust_ca->max_pathlen < check_path_cnt )
1660 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001661 continue;
1662 }
1663
1664 if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1665 pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1666 child->sig.p, child->sig.len ) != 0 )
1667 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001668 continue;
1669 }
1670
1671 /*
1672 * Top of chain is signed by a trusted CA
1673 */
1674 *flags &= ~BADCERT_NOT_TRUSTED;
1675 break;
1676 }
1677
1678 /*
1679 * If top of chain is not the same as the trusted CA send a verify request
1680 * to the callback for any issues with validity and CRL presence for the
1681 * trusted CA certificate.
1682 */
1683 if( trust_ca != NULL &&
1684 ( child->subject_raw.len != trust_ca->subject_raw.len ||
1685 memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1686 child->issuer_raw.len ) != 0 ) )
1687 {
1688#if defined(POLARSSL_X509_CRL_PARSE_C)
1689 /* Check trusted CA's CRL for the chain's top crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001690 *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
Manuel Pégourié-Gonnardcbf3ef32013-09-23 12:20:02 +02001691#else
1692 ((void) ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001693#endif
1694
Paul Bakker86d0c192013-09-18 11:11:02 +02001695 if( x509_time_expired( &trust_ca->valid_to ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001696 ca_flags |= BADCERT_EXPIRED;
1697
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001698 if( x509_time_future( &trust_ca->valid_from ) )
1699 ca_flags |= BADCERT_FUTURE;
1700
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001701 if( NULL != f_vrfy )
1702 {
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001703 if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1704 &ca_flags ) ) != 0 )
1705 {
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001706 return( ret );
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001707 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001708 }
1709 }
1710
1711 /* Call callback on top cert */
1712 if( NULL != f_vrfy )
1713 {
1714 if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1715 return( ret );
1716 }
1717
1718 *flags |= ca_flags;
1719
1720 return( 0 );
1721}
1722
Paul Bakkerddf26b42013-09-18 13:46:23 +02001723static int x509_crt_verify_child(
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001724 x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001725 x509_crl *ca_crl, int path_cnt, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001726 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001727 void *p_vrfy )
1728{
1729 int ret;
1730 int parent_flags = 0;
1731 unsigned char hash[POLARSSL_MD_MAX_SIZE];
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001732 x509_crt *grandparent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001733 const md_info_t *md_info;
1734
Manuel Pégourié-Gonnard8c045ef2014-04-08 11:55:03 +02001735 if( x509_time_expired( &child->valid_to ) )
1736 *flags |= BADCERT_EXPIRED;
1737
Manuel Pégourié-Gonnard95337652014-03-10 13:15:18 +01001738 if( x509_time_future( &child->valid_from ) )
1739 *flags |= BADCERT_FUTURE;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001740
1741 md_info = md_info_from_type( child->sig_md );
1742 if( md_info == NULL )
1743 {
1744 /*
1745 * Cannot check 'unknown' hash
1746 */
1747 *flags |= BADCERT_NOT_TRUSTED;
1748 }
1749 else
1750 {
1751 md( md_info, child->tbs.p, child->tbs.len, hash );
1752
1753 if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1754 pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1755 child->sig.p, child->sig.len ) != 0 )
1756 {
1757 *flags |= BADCERT_NOT_TRUSTED;
1758 }
1759 }
1760
1761#if defined(POLARSSL_X509_CRL_PARSE_C)
1762 /* Check trusted CA's CRL for the given crt */
Paul Bakkerddf26b42013-09-18 13:46:23 +02001763 *flags |= x509_crt_verifycrl(child, parent, ca_crl);
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001764#endif
1765
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001766 /* Look for a grandparent upwards the chain */
1767 for( grandparent = parent->next;
1768 grandparent != NULL;
1769 grandparent = grandparent->next )
1770 {
1771 if( x509_crt_check_parent( parent, grandparent ) == 0 )
1772 break;
1773 }
1774
1775 /* Is our parent part of the chain or at the top? */
1776 if( grandparent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001777 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001778 ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1779 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001780 if( ret != 0 )
1781 return( ret );
1782 }
1783 else
1784 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001785 ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1786 path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001787 if( ret != 0 )
1788 return( ret );
1789 }
1790
1791 /* child is verified to be a child of the parent, call verify callback */
1792 if( NULL != f_vrfy )
1793 if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1794 return( ret );
1795
1796 *flags |= parent_flags;
1797
1798 return( 0 );
1799}
1800
1801/*
1802 * Verify the certificate validity
1803 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001804int x509_crt_verify( x509_crt *crt,
1805 x509_crt *trust_ca,
Paul Bakkerddf26b42013-09-18 13:46:23 +02001806 x509_crl *ca_crl,
1807 const char *cn, int *flags,
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001808 int (*f_vrfy)(void *, x509_crt *, int, int *),
Paul Bakkerddf26b42013-09-18 13:46:23 +02001809 void *p_vrfy )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001810{
1811 size_t cn_len;
1812 int ret;
1813 int pathlen = 0;
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001814 x509_crt *parent;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001815 x509_name *name;
1816 x509_sequence *cur = NULL;
1817
1818 *flags = 0;
1819
1820 if( cn != NULL )
1821 {
1822 name = &crt->subject;
1823 cn_len = strlen( cn );
1824
1825 if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1826 {
1827 cur = &crt->subject_alt_names;
1828
1829 while( cur != NULL )
1830 {
1831 if( cur->buf.len == cn_len &&
1832 x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1833 break;
1834
1835 if( cur->buf.len > 2 &&
1836 memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1837 x509_wildcard_verify( cn, &cur->buf ) )
1838 break;
1839
1840 cur = cur->next;
1841 }
1842
1843 if( cur == NULL )
1844 *flags |= BADCERT_CN_MISMATCH;
1845 }
1846 else
1847 {
1848 while( name != NULL )
1849 {
1850 if( OID_CMP( OID_AT_CN, &name->oid ) )
1851 {
1852 if( name->val.len == cn_len &&
1853 x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1854 break;
1855
1856 if( name->val.len > 2 &&
1857 memcmp( name->val.p, "*.", 2 ) == 0 &&
1858 x509_wildcard_verify( cn, &name->val ) )
1859 break;
1860 }
1861
1862 name = name->next;
1863 }
1864
1865 if( name == NULL )
1866 *flags |= BADCERT_CN_MISMATCH;
1867 }
1868 }
1869
Manuel Pégourié-Gonnard312010e2014-04-09 14:30:11 +02001870 /* Look for a parent upwards the chain */
1871 for( parent = crt->next; parent != NULL; parent = parent->next )
1872 {
1873 if( x509_crt_check_parent( crt, parent ) == 0 )
1874 break;
1875 }
1876
1877 /* Are we part of the chain or at the top? */
1878 if( parent != NULL )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001879 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001880 ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1881 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001882 if( ret != 0 )
1883 return( ret );
1884 }
1885 else
1886 {
Manuel Pégourié-Gonnard490047c2014-04-09 14:30:45 +02001887 ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1888 pathlen, flags, f_vrfy, p_vrfy );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001889 if( ret != 0 )
1890 return( ret );
1891 }
1892
1893 if( *flags != 0 )
1894 return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
1895
1896 return( 0 );
1897}
1898
1899/*
Paul Bakker369d2eb2013-09-18 11:58:25 +02001900 * Initialize a certificate chain
1901 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001902void x509_crt_init( x509_crt *crt )
Paul Bakker369d2eb2013-09-18 11:58:25 +02001903{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001904 memset( crt, 0, sizeof(x509_crt) );
Paul Bakker369d2eb2013-09-18 11:58:25 +02001905}
1906
1907/*
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001908 * Unallocate all certificate data
1909 */
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001910void x509_crt_free( x509_crt *crt )
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001911{
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001912 x509_crt *cert_cur = crt;
1913 x509_crt *cert_prv;
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001914 x509_name *name_cur;
1915 x509_name *name_prv;
1916 x509_sequence *seq_cur;
1917 x509_sequence *seq_prv;
1918
1919 if( crt == NULL )
1920 return;
1921
1922 do
1923 {
1924 pk_free( &cert_cur->pk );
1925
1926 name_cur = cert_cur->issuer.next;
1927 while( name_cur != NULL )
1928 {
1929 name_prv = name_cur;
1930 name_cur = name_cur->next;
1931 memset( name_prv, 0, sizeof( x509_name ) );
1932 polarssl_free( name_prv );
1933 }
1934
1935 name_cur = cert_cur->subject.next;
1936 while( name_cur != NULL )
1937 {
1938 name_prv = name_cur;
1939 name_cur = name_cur->next;
1940 memset( name_prv, 0, sizeof( x509_name ) );
1941 polarssl_free( name_prv );
1942 }
1943
1944 seq_cur = cert_cur->ext_key_usage.next;
1945 while( seq_cur != NULL )
1946 {
1947 seq_prv = seq_cur;
1948 seq_cur = seq_cur->next;
1949 memset( seq_prv, 0, sizeof( x509_sequence ) );
1950 polarssl_free( seq_prv );
1951 }
1952
1953 seq_cur = cert_cur->subject_alt_names.next;
1954 while( seq_cur != NULL )
1955 {
1956 seq_prv = seq_cur;
1957 seq_cur = seq_cur->next;
1958 memset( seq_prv, 0, sizeof( x509_sequence ) );
1959 polarssl_free( seq_prv );
1960 }
1961
1962 if( cert_cur->raw.p != NULL )
1963 {
1964 memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1965 polarssl_free( cert_cur->raw.p );
1966 }
1967
1968 cert_cur = cert_cur->next;
1969 }
1970 while( cert_cur != NULL );
1971
1972 cert_cur = crt;
1973 do
1974 {
1975 cert_prv = cert_cur;
1976 cert_cur = cert_cur->next;
1977
Paul Bakkerc559c7a2013-09-18 14:13:26 +02001978 memset( cert_prv, 0, sizeof( x509_crt ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001979 if( cert_prv != crt )
1980 polarssl_free( cert_prv );
1981 }
1982 while( cert_cur != NULL );
1983}
1984
Paul Bakker9af723c2014-05-01 13:03:14 +02001985#endif /* POLARSSL_X509_CRT_PARSE_C */