PolarSSL v1.3.8
x509_crt.c
Go to the documentation of this file.
1 /*
2  * X.509 certificate parsing and verification
3  *
4  * Copyright (C) 2006-2014, Brainspark B.V.
5  *
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/rfc5280.txt (Certificates and CRLs)
29  * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
30  * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
31  *
32  * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
33  * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
34  */
35 
36 #if !defined(POLARSSL_CONFIG_FILE)
37 #include "polarssl/config.h"
38 #else
39 #include POLARSSL_CONFIG_FILE
40 #endif
41 
42 #if defined(POLARSSL_X509_CRT_PARSE_C)
43 
44 #include "polarssl/x509_crt.h"
45 #include "polarssl/oid.h"
46 #if defined(POLARSSL_PEM_PARSE_C)
47 #include "polarssl/pem.h"
48 #endif
49 
50 #if defined(POLARSSL_PLATFORM_C)
51 #include "polarssl/platform.h"
52 #else
53 #define polarssl_malloc malloc
54 #define polarssl_free free
55 #endif
56 
57 #if defined(POLARSSL_THREADING_C)
58 #include "polarssl/threading.h"
59 #endif
60 
61 #include <string.h>
62 #include <stdlib.h>
63 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
64 #include <windows.h>
65 #else
66 #include <time.h>
67 #endif
68 
69 #if defined(EFIX64) || defined(EFI32)
70 #include <stdio.h>
71 #endif
72 
73 #if defined(POLARSSL_FS_IO)
74 #include <stdio.h>
75 #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
76 #include <sys/types.h>
77 #include <sys/stat.h>
78 #include <dirent.h>
79 #endif
80 #endif
81 
82 /* Implementation that should never be optimized out by the compiler */
83 static void polarssl_zeroize( void *v, size_t n ) {
84  volatile unsigned char *p = v; while( n-- ) *p++ = 0;
85 }
86 
87 /*
88  * Version ::= INTEGER { v1(0), v2(1), v3(2) }
89  */
90 static int x509_get_version( unsigned char **p,
91  const unsigned char *end,
92  int *ver )
93 {
94  int ret;
95  size_t len;
96 
97  if( ( ret = asn1_get_tag( p, end, &len,
99  {
101  {
102  *ver = 0;
103  return( 0 );
104  }
105 
106  return( ret );
107  }
108 
109  end = *p + len;
110 
111  if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
112  return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
113 
114  if( *p != end )
117 
118  return( 0 );
119 }
120 
121 /*
122  * Validity ::= SEQUENCE {
123  * notBefore Time,
124  * notAfter Time }
125  */
126 static int x509_get_dates( unsigned char **p,
127  const unsigned char *end,
128  x509_time *from,
129  x509_time *to )
130 {
131  int ret;
132  size_t len;
133 
134  if( ( ret = asn1_get_tag( p, end, &len,
135  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
136  return( POLARSSL_ERR_X509_INVALID_DATE + ret );
137 
138  end = *p + len;
139 
140  if( ( ret = x509_get_time( p, end, from ) ) != 0 )
141  return( ret );
142 
143  if( ( ret = x509_get_time( p, end, to ) ) != 0 )
144  return( ret );
145 
146  if( *p != end )
149 
150  return( 0 );
151 }
152 
153 /*
154  * X.509 v2/v3 unique identifier (not parsed)
155  */
156 static int x509_get_uid( unsigned char **p,
157  const unsigned char *end,
158  x509_buf *uid, int n )
159 {
160  int ret;
161 
162  if( *p == end )
163  return( 0 );
164 
165  uid->tag = **p;
166 
167  if( ( ret = asn1_get_tag( p, end, &uid->len,
168  ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
169  {
171  return( 0 );
172 
173  return( ret );
174  }
175 
176  uid->p = *p;
177  *p += uid->len;
178 
179  return( 0 );
180 }
181 
182 static int x509_get_basic_constraints( unsigned char **p,
183  const unsigned char *end,
184  int *ca_istrue,
185  int *max_pathlen )
186 {
187  int ret;
188  size_t len;
189 
190  /*
191  * BasicConstraints ::= SEQUENCE {
192  * cA BOOLEAN DEFAULT FALSE,
193  * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
194  */
195  *ca_istrue = 0; /* DEFAULT FALSE */
196  *max_pathlen = 0; /* endless */
197 
198  if( ( ret = asn1_get_tag( p, end, &len,
199  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
200  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
201 
202  if( *p == end )
203  return( 0 );
204 
205  if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
206  {
208  ret = asn1_get_int( p, end, ca_istrue );
209 
210  if( ret != 0 )
211  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
212 
213  if( *ca_istrue != 0 )
214  *ca_istrue = 1;
215  }
216 
217  if( *p == end )
218  return( 0 );
219 
220  if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
221  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
222 
223  if( *p != end )
226 
227  (*max_pathlen)++;
228 
229  return( 0 );
230 }
231 
232 static int x509_get_ns_cert_type( unsigned char **p,
233  const unsigned char *end,
234  unsigned char *ns_cert_type)
235 {
236  int ret;
237  x509_bitstring bs = { 0, 0, NULL };
238 
239  if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
240  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
241 
242  if( bs.len != 1 )
245 
246  /* Get actual bitstring */
247  *ns_cert_type = *bs.p;
248  return( 0 );
249 }
250 
251 static int x509_get_key_usage( unsigned char **p,
252  const unsigned char *end,
253  unsigned char *key_usage)
254 {
255  int ret;
256  x509_bitstring bs = { 0, 0, NULL };
257 
258  if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
259  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
260 
261  if( bs.len < 1 )
264 
265  /* Get actual bitstring */
266  *key_usage = *bs.p;
267  return( 0 );
268 }
269 
270 /*
271  * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
272  *
273  * KeyPurposeId ::= OBJECT IDENTIFIER
274  */
275 static int x509_get_ext_key_usage( unsigned char **p,
276  const unsigned char *end,
277  x509_sequence *ext_key_usage)
278 {
279  int ret;
280 
281  if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
282  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
283 
284  /* Sequence length must be >= 1 */
285  if( ext_key_usage->buf.p == NULL )
288 
289  return( 0 );
290 }
291 
292 /*
293  * SubjectAltName ::= GeneralNames
294  *
295  * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
296  *
297  * GeneralName ::= CHOICE {
298  * otherName [0] OtherName,
299  * rfc822Name [1] IA5String,
300  * dNSName [2] IA5String,
301  * x400Address [3] ORAddress,
302  * directoryName [4] Name,
303  * ediPartyName [5] EDIPartyName,
304  * uniformResourceIdentifier [6] IA5String,
305  * iPAddress [7] OCTET STRING,
306  * registeredID [8] OBJECT IDENTIFIER }
307  *
308  * OtherName ::= SEQUENCE {
309  * type-id OBJECT IDENTIFIER,
310  * value [0] EXPLICIT ANY DEFINED BY type-id }
311  *
312  * EDIPartyName ::= SEQUENCE {
313  * nameAssigner [0] DirectoryString OPTIONAL,
314  * partyName [1] DirectoryString }
315  *
316  * NOTE: PolarSSL only parses and uses dNSName at this point.
317  */
318 static int x509_get_subject_alt_name( unsigned char **p,
319  const unsigned char *end,
320  x509_sequence *subject_alt_name )
321 {
322  int ret;
323  size_t len, tag_len;
324  asn1_buf *buf;
325  unsigned char tag;
326  asn1_sequence *cur = subject_alt_name;
327 
328  /* Get main sequence tag */
329  if( ( ret = asn1_get_tag( p, end, &len,
330  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
331  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
332 
333  if( *p + len != end )
336 
337  while( *p < end )
338  {
339  if( ( end - *p ) < 1 )
342 
343  tag = **p;
344  (*p)++;
345  if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
346  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
347 
348  if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
351 
352  /* Skip everything but DNS name */
353  if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
354  {
355  *p += tag_len;
356  continue;
357  }
358 
359  /* Allocate and assign next pointer */
360  if( cur->buf.p != NULL )
361  {
363  sizeof( asn1_sequence ) );
364 
365  if( cur->next == NULL )
368 
369  memset( cur->next, 0, sizeof( asn1_sequence ) );
370  cur = cur->next;
371  }
372 
373  buf = &(cur->buf);
374  buf->tag = tag;
375  buf->p = *p;
376  buf->len = tag_len;
377  *p += buf->len;
378  }
379 
380  /* Set final sequence entry's next pointer to NULL */
381  cur->next = NULL;
382 
383  if( *p != end )
386 
387  return( 0 );
388 }
389 
390 /*
391  * X.509 v3 extensions
392  *
393  * TODO: Perform all of the basic constraints tests required by the RFC
394  * TODO: Set values for undetected extensions to a sane default?
395  *
396  */
397 static int x509_get_crt_ext( unsigned char **p,
398  const unsigned char *end,
399  x509_crt *crt )
400 {
401  int ret;
402  size_t len;
403  unsigned char *end_ext_data, *end_ext_octet;
404 
405  if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
406  {
408  return( 0 );
409 
410  return( ret );
411  }
412 
413  while( *p < end )
414  {
415  /*
416  * Extension ::= SEQUENCE {
417  * extnID OBJECT IDENTIFIER,
418  * critical BOOLEAN DEFAULT FALSE,
419  * extnValue OCTET STRING }
420  */
421  x509_buf extn_oid = {0, 0, NULL};
422  int is_critical = 0; /* DEFAULT FALSE */
423  int ext_type = 0;
424 
425  if( ( ret = asn1_get_tag( p, end, &len,
426  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
427  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
428 
429  end_ext_data = *p + len;
430 
431  /* Get extension ID */
432  extn_oid.tag = **p;
433 
434  if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
435  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
436 
437  extn_oid.p = *p;
438  *p += extn_oid.len;
439 
440  if( ( end - *p ) < 1 )
443 
444  /* Get optional critical */
445  if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
447  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
448 
449  /* Data should be octet string type */
450  if( ( ret = asn1_get_tag( p, end_ext_data, &len,
451  ASN1_OCTET_STRING ) ) != 0 )
452  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
453 
454  end_ext_octet = *p + len;
455 
456  if( end_ext_octet != end_ext_data )
459 
460  /*
461  * Detect supported extensions
462  */
463  ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
464 
465  if( ret != 0 )
466  {
467  /* No parser found, skip extension */
468  *p = end_ext_octet;
469 
470 #if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
471  if( is_critical )
472  {
473  /* Data is marked as critical: fail */
476  }
477 #endif
478  continue;
479  }
480 
481  crt->ext_types |= ext_type;
482 
483  switch( ext_type )
484  {
486  /* Parse basic constraints */
487  if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
488  &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
489  return( ret );
490  break;
491 
492  case EXT_KEY_USAGE:
493  /* Parse key usage */
494  if( ( ret = x509_get_key_usage( p, end_ext_octet,
495  &crt->key_usage ) ) != 0 )
496  return( ret );
497  break;
498 
500  /* Parse extended key usage */
501  if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
502  &crt->ext_key_usage ) ) != 0 )
503  return( ret );
504  break;
505 
507  /* Parse subject alt name */
508  if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
509  &crt->subject_alt_names ) ) != 0 )
510  return( ret );
511  break;
512 
513  case EXT_NS_CERT_TYPE:
514  /* Parse netscape certificate type */
515  if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
516  &crt->ns_cert_type ) ) != 0 )
517  return( ret );
518  break;
519 
520  default:
522  }
523  }
524 
525  if( *p != end )
528 
529  return( 0 );
530 }
531 
532 /*
533  * Parse and fill a single X.509 certificate in DER format
534  */
535 static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
536  size_t buflen )
537 {
538  int ret;
539  size_t len;
540  unsigned char *p, *end, *crt_end;
541  x509_buf sig_params1, sig_params2;
542 
543  memset( &sig_params1, 0, sizeof( x509_buf ) );
544  memset( &sig_params2, 0, sizeof( x509_buf ) );
545 
546  /*
547  * Check for valid input
548  */
549  if( crt == NULL || buf == NULL )
551 
552  p = (unsigned char *) polarssl_malloc( len = buflen );
553 
554  if( p == NULL )
556 
557  memcpy( p, buf, buflen );
558 
559  crt->raw.p = p;
560  crt->raw.len = len;
561  end = p + len;
562 
563  /*
564  * Certificate ::= SEQUENCE {
565  * tbsCertificate TBSCertificate,
566  * signatureAlgorithm AlgorithmIdentifier,
567  * signatureValue BIT STRING }
568  */
569  if( ( ret = asn1_get_tag( &p, end, &len,
570  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
571  {
572  x509_crt_free( crt );
574  }
575 
576  if( len > (size_t) ( end - p ) )
577  {
578  x509_crt_free( crt );
581  }
582  crt_end = p + len;
583 
584  /*
585  * TBSCertificate ::= SEQUENCE {
586  */
587  crt->tbs.p = p;
588 
589  if( ( ret = asn1_get_tag( &p, end, &len,
590  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
591  {
592  x509_crt_free( crt );
593  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
594  }
595 
596  end = p + len;
597  crt->tbs.len = end - crt->tbs.p;
598 
599  /*
600  * Version ::= INTEGER { v1(0), v2(1), v3(2) }
601  *
602  * CertificateSerialNumber ::= INTEGER
603  *
604  * signature AlgorithmIdentifier
605  */
606  if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
607  ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
608  ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
609  &sig_params1 ) ) != 0 )
610  {
611  x509_crt_free( crt );
612  return( ret );
613  }
614 
615  crt->version++;
616 
617  if( crt->version > 3 )
618  {
619  x509_crt_free( crt );
621  }
622 
623  if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
624  &crt->sig_md, &crt->sig_pk,
625  &crt->sig_opts ) ) != 0 )
626  {
627  x509_crt_free( crt );
628  return( ret );
629  }
630 
631  /*
632  * issuer Name
633  */
634  crt->issuer_raw.p = p;
635 
636  if( ( ret = asn1_get_tag( &p, end, &len,
637  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
638  {
639  x509_crt_free( crt );
640  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
641  }
642 
643  if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
644  {
645  x509_crt_free( crt );
646  return( ret );
647  }
648 
649  crt->issuer_raw.len = p - crt->issuer_raw.p;
650 
651  /*
652  * Validity ::= SEQUENCE {
653  * notBefore Time,
654  * notAfter Time }
655  *
656  */
657  if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
658  &crt->valid_to ) ) != 0 )
659  {
660  x509_crt_free( crt );
661  return( ret );
662  }
663 
664  /*
665  * subject Name
666  */
667  crt->subject_raw.p = p;
668 
669  if( ( ret = asn1_get_tag( &p, end, &len,
670  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
671  {
672  x509_crt_free( crt );
673  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
674  }
675 
676  if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
677  {
678  x509_crt_free( crt );
679  return( ret );
680  }
681 
682  crt->subject_raw.len = p - crt->subject_raw.p;
683 
684  /*
685  * SubjectPublicKeyInfo
686  */
687  if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
688  {
689  x509_crt_free( crt );
690  return( ret );
691  }
692 
693  /*
694  * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
695  * -- If present, version shall be v2 or v3
696  * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
697  * -- If present, version shall be v2 or v3
698  * extensions [3] EXPLICIT Extensions OPTIONAL
699  * -- If present, version shall be v3
700  */
701  if( crt->version == 2 || crt->version == 3 )
702  {
703  ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
704  if( ret != 0 )
705  {
706  x509_crt_free( crt );
707  return( ret );
708  }
709  }
710 
711  if( crt->version == 2 || crt->version == 3 )
712  {
713  ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
714  if( ret != 0 )
715  {
716  x509_crt_free( crt );
717  return( ret );
718  }
719  }
720 
721 #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
722  if( crt->version == 3 )
723  {
724 #endif
725  ret = x509_get_crt_ext( &p, end, crt );
726  if( ret != 0 )
727  {
728  x509_crt_free( crt );
729  return( ret );
730  }
731 #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
732  }
733 #endif
734 
735  if( p != end )
736  {
737  x509_crt_free( crt );
740  }
741 
742  end = crt_end;
743 
744  /*
745  * }
746  * -- end of TBSCertificate
747  *
748  * signatureAlgorithm AlgorithmIdentifier,
749  * signatureValue BIT STRING
750  */
751  if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
752  {
753  x509_crt_free( crt );
754  return( ret );
755  }
756 
757  if( crt->sig_oid1.len != crt->sig_oid2.len ||
758  memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
759  sig_params1.len != sig_params2.len ||
760  memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
761  {
762  x509_crt_free( crt );
764  }
765 
766  if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
767  {
768  x509_crt_free( crt );
769  return( ret );
770  }
771 
772  if( p != end )
773  {
774  x509_crt_free( crt );
777  }
778 
779  return( 0 );
780 }
781 
782 /*
783  * Parse one X.509 certificate in DER format from a buffer and add them to a
784  * chained list
785  */
786 int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
787  size_t buflen )
788 {
789  int ret;
790  x509_crt *crt = chain, *prev = NULL;
791 
792  /*
793  * Check for valid input
794  */
795  if( crt == NULL || buf == NULL )
797 
798  while( crt->version != 0 && crt->next != NULL )
799  {
800  prev = crt;
801  crt = crt->next;
802  }
803 
804  /*
805  * Add new certificate on the end of the chain if needed.
806  */
807  if( crt->version != 0 && crt->next == NULL )
808  {
809  crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
810 
811  if( crt->next == NULL )
813 
814  prev = crt;
815  crt = crt->next;
816  x509_crt_init( crt );
817  }
818 
819  if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
820  {
821  if( prev )
822  prev->next = NULL;
823 
824  if( crt != chain )
825  polarssl_free( crt );
826 
827  return( ret );
828  }
829 
830  return( 0 );
831 }
832 
833 /*
834  * Parse one or more PEM certificates from a buffer and add them to the chained
835  * list
836  */
837 int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
838 {
839  int success = 0, first_error = 0, total_failed = 0;
840  int buf_format = X509_FORMAT_DER;
841 
842  /*
843  * Check for valid input
844  */
845  if( chain == NULL || buf == NULL )
847 
848  /*
849  * Determine buffer content. Buffer contains either one DER certificate or
850  * one or more PEM certificates.
851  */
852 #if defined(POLARSSL_PEM_PARSE_C)
853  if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
854  buf_format = X509_FORMAT_PEM;
855 #endif
856 
857  if( buf_format == X509_FORMAT_DER )
858  return x509_crt_parse_der( chain, buf, buflen );
859 
860 #if defined(POLARSSL_PEM_PARSE_C)
861  if( buf_format == X509_FORMAT_PEM )
862  {
863  int ret;
864  pem_context pem;
865 
866  while( buflen > 0 )
867  {
868  size_t use_len;
869  pem_init( &pem );
870 
871  ret = pem_read_buffer( &pem,
872  "-----BEGIN CERTIFICATE-----",
873  "-----END CERTIFICATE-----",
874  buf, NULL, 0, &use_len );
875 
876  if( ret == 0 )
877  {
878  /*
879  * Was PEM encoded
880  */
881  buflen -= use_len;
882  buf += use_len;
883  }
884  else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
885  {
886  return( ret );
887  }
889  {
890  pem_free( &pem );
891 
892  /*
893  * PEM header and footer were found
894  */
895  buflen -= use_len;
896  buf += use_len;
897 
898  if( first_error == 0 )
899  first_error = ret;
900 
901  continue;
902  }
903  else
904  break;
905 
906  ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
907 
908  pem_free( &pem );
909 
910  if( ret != 0 )
911  {
912  /*
913  * Quit parsing on a memory error
914  */
916  return( ret );
917 
918  if( first_error == 0 )
919  first_error = ret;
920 
921  total_failed++;
922  continue;
923  }
924 
925  success = 1;
926  }
927  }
928 #endif /* POLARSSL_PEM_PARSE_C */
929 
930  if( success )
931  return( total_failed );
932  else if( first_error )
933  return( first_error );
934  else
936 }
937 
938 #if defined(POLARSSL_FS_IO)
939 /*
940  * Load one or more certificates and add them to the chained list
941  */
942 int x509_crt_parse_file( x509_crt *chain, const char *path )
943 {
944  int ret;
945  size_t n;
946  unsigned char *buf;
947 
948  if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
949  return( ret );
950 
951  ret = x509_crt_parse( chain, buf, n );
952 
953  polarssl_zeroize( buf, n + 1 );
954  polarssl_free( buf );
955 
956  return( ret );
957 }
958 
959 #if defined(POLARSSL_THREADING_PTHREAD)
960 static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
961 #endif
962 
963 int x509_crt_parse_path( x509_crt *chain, const char *path )
964 {
965  int ret = 0;
966 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
967  int w_ret;
968  WCHAR szDir[MAX_PATH];
969  char filename[MAX_PATH];
970  char *p;
971  int len = (int) strlen( path );
972 
973  WIN32_FIND_DATAW file_data;
974  HANDLE hFind;
975 
976  if( len > MAX_PATH - 3 )
978 
979  memset( szDir, 0, sizeof(szDir) );
980  memset( filename, 0, MAX_PATH );
981  memcpy( filename, path, len );
982  filename[len++] = '\\';
983  p = filename + len;
984  filename[len++] = '*';
985 
986  w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
987  MAX_PATH - 3 );
988 
989  hFind = FindFirstFileW( szDir, &file_data );
990  if( hFind == INVALID_HANDLE_VALUE )
992 
993  len = MAX_PATH - len;
994  do
995  {
996  memset( p, 0, len );
997 
998  if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
999  continue;
1000 
1001  w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
1002  lstrlenW( file_data.cFileName ),
1003  p, len - 1,
1004  NULL, NULL );
1005 
1006  w_ret = x509_crt_parse_file( chain, filename );
1007  if( w_ret < 0 )
1008  ret++;
1009  else
1010  ret += w_ret;
1011  }
1012  while( FindNextFileW( hFind, &file_data ) != 0 );
1013 
1014  if( GetLastError() != ERROR_NO_MORE_FILES )
1016 
1017  FindClose( hFind );
1018 #else /* _WIN32 */
1019  int t_ret;
1020  struct stat sb;
1021  struct dirent *entry;
1022  char entry_name[255];
1023  DIR *dir = opendir( path );
1024 
1025  if( dir == NULL )
1027 
1028 #if defined(POLARSSL_THREADING_PTHREAD)
1029  if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1030  return( ret );
1031 #endif
1032 
1033  while( ( entry = readdir( dir ) ) != NULL )
1034  {
1035  snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
1036 
1037  if( stat( entry_name, &sb ) == -1 )
1038  {
1039  closedir( dir );
1041  goto cleanup;
1042  }
1043 
1044  if( !S_ISREG( sb.st_mode ) )
1045  continue;
1046 
1047  // Ignore parse errors
1048  //
1049  t_ret = x509_crt_parse_file( chain, entry_name );
1050  if( t_ret < 0 )
1051  ret++;
1052  else
1053  ret += t_ret;
1054  }
1055  closedir( dir );
1056 
1057 cleanup:
1058 #if defined(POLARSSL_THREADING_PTHREAD)
1059  if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1061 #endif
1062 
1063 #endif /* _WIN32 */
1064 
1065  return( ret );
1066 }
1067 #endif /* POLARSSL_FS_IO */
1068 
1069 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1070  !defined(EFI32)
1071 #include <stdarg.h>
1072 
1073 #if !defined vsnprintf
1074 #define vsnprintf _vsnprintf
1075 #endif // vsnprintf
1076 
1077 /*
1078  * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1079  * Result value is not size of buffer needed, but -1 if no fit is possible.
1080  *
1081  * This fuction tries to 'fix' this by at least suggesting enlarging the
1082  * size by 20.
1083  */
1084 static int compat_snprintf( char *str, size_t size, const char *format, ... )
1085 {
1086  va_list ap;
1087  int res = -1;
1088 
1089  va_start( ap, format );
1090 
1091  res = vsnprintf( str, size, format, ap );
1092 
1093  va_end( ap );
1094 
1095  // No quick fix possible
1096  if( res < 0 )
1097  return( (int) size + 20 );
1098 
1099  return( res );
1100 }
1101 
1102 #define snprintf compat_snprintf
1103 #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
1104 
1105 #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1106 
1107 #define SAFE_SNPRINTF() \
1108 { \
1109  if( ret == -1 ) \
1110  return( -1 ); \
1111  \
1112  if( (unsigned int) ret > n ) { \
1113  p[n - 1] = '\0'; \
1114  return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
1115  } \
1116  \
1117  n -= (unsigned int) ret; \
1118  p += (unsigned int) ret; \
1119 }
1120 
1121 static int x509_info_subject_alt_name( char **buf, size_t *size,
1122  const x509_sequence *subject_alt_name )
1123 {
1124  size_t i;
1125  size_t n = *size;
1126  char *p = *buf;
1127  const x509_sequence *cur = subject_alt_name;
1128  const char *sep = "";
1129  size_t sep_len = 0;
1130 
1131  while( cur != NULL )
1132  {
1133  if( cur->buf.len + sep_len >= n )
1134  {
1135  *p = '\0';
1136  return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1137  }
1138 
1139  n -= cur->buf.len + sep_len;
1140  for( i = 0; i < sep_len; i++ )
1141  *p++ = sep[i];
1142  for( i = 0; i < cur->buf.len; i++ )
1143  *p++ = cur->buf.p[i];
1144 
1145  sep = ", ";
1146  sep_len = 2;
1147 
1148  cur = cur->next;
1149  }
1150 
1151  *p = '\0';
1152 
1153  *size = n;
1154  *buf = p;
1155 
1156  return( 0 );
1157 }
1158 
1159 #define PRINT_ITEM(i) \
1160  { \
1161  ret = snprintf( p, n, "%s" i, sep ); \
1162  SAFE_SNPRINTF(); \
1163  sep = ", "; \
1164  }
1165 
1166 #define CERT_TYPE(type,name) \
1167  if( ns_cert_type & type ) \
1168  PRINT_ITEM( name );
1169 
1170 static int x509_info_cert_type( char **buf, size_t *size,
1171  unsigned char ns_cert_type )
1172 {
1173  int ret;
1174  size_t n = *size;
1175  char *p = *buf;
1176  const char *sep = "";
1177 
1178  CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1179  CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1180  CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1181  CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1182  CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1183  CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1184  CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1185  CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
1186 
1187  *size = n;
1188  *buf = p;
1189 
1190  return( 0 );
1191 }
1192 
1193 #define KEY_USAGE(code,name) \
1194  if( key_usage & code ) \
1195  PRINT_ITEM( name );
1196 
1197 static int x509_info_key_usage( char **buf, size_t *size,
1198  unsigned char key_usage )
1199 {
1200  int ret;
1201  size_t n = *size;
1202  char *p = *buf;
1203  const char *sep = "";
1204 
1205  KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1206  KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1207  KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1208  KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1209  KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1210  KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1211  KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
1212 
1213  *size = n;
1214  *buf = p;
1215 
1216  return( 0 );
1217 }
1218 
1219 static int x509_info_ext_key_usage( char **buf, size_t *size,
1220  const x509_sequence *extended_key_usage )
1221 {
1222  int ret;
1223  const char *desc;
1224  size_t n = *size;
1225  char *p = *buf;
1226  const x509_sequence *cur = extended_key_usage;
1227  const char *sep = "";
1228 
1229  while( cur != NULL )
1230  {
1231  if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1232  desc = "???";
1233 
1234  ret = snprintf( p, n, "%s%s", sep, desc );
1235  SAFE_SNPRINTF();
1236 
1237  sep = ", ";
1238 
1239  cur = cur->next;
1240  }
1241 
1242  *size = n;
1243  *buf = p;
1244 
1245  return( 0 );
1246 }
1247 
1248 /*
1249  * Return an informational string about the certificate.
1250  */
1251 #define BEFORE_COLON 18
1252 #define BC "18"
1253 int x509_crt_info( char *buf, size_t size, const char *prefix,
1254  const x509_crt *crt )
1255 {
1256  int ret;
1257  size_t n;
1258  char *p;
1259  char key_size_str[BEFORE_COLON];
1260 
1261  p = buf;
1262  n = size;
1263 
1264  ret = snprintf( p, n, "%scert. version : %d\n",
1265  prefix, crt->version );
1266  SAFE_SNPRINTF();
1267  ret = snprintf( p, n, "%sserial number : ",
1268  prefix );
1269  SAFE_SNPRINTF();
1270 
1271  ret = x509_serial_gets( p, n, &crt->serial );
1272  SAFE_SNPRINTF();
1273 
1274  ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1275  SAFE_SNPRINTF();
1276  ret = x509_dn_gets( p, n, &crt->issuer );
1277  SAFE_SNPRINTF();
1278 
1279  ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1280  SAFE_SNPRINTF();
1281  ret = x509_dn_gets( p, n, &crt->subject );
1282  SAFE_SNPRINTF();
1283 
1284  ret = snprintf( p, n, "\n%sissued on : " \
1285  "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1286  crt->valid_from.year, crt->valid_from.mon,
1287  crt->valid_from.day, crt->valid_from.hour,
1288  crt->valid_from.min, crt->valid_from.sec );
1289  SAFE_SNPRINTF();
1290 
1291  ret = snprintf( p, n, "\n%sexpires on : " \
1292  "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1293  crt->valid_to.year, crt->valid_to.mon,
1294  crt->valid_to.day, crt->valid_to.hour,
1295  crt->valid_to.min, crt->valid_to.sec );
1296  SAFE_SNPRINTF();
1297 
1298  ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1299  SAFE_SNPRINTF();
1300 
1301  ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
1302  crt->sig_md, crt->sig_opts );
1303  SAFE_SNPRINTF();
1304 
1305  /* Key size */
1306  if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1307  pk_get_name( &crt->pk ) ) ) != 0 )
1308  {
1309  return( ret );
1310  }
1311 
1312  ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1313  (int) pk_get_size( &crt->pk ) );
1314  SAFE_SNPRINTF();
1315 
1316  /*
1317  * Optional extensions
1318  */
1319 
1320  if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1321  {
1322  ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1323  crt->ca_istrue ? "true" : "false" );
1324  SAFE_SNPRINTF();
1325 
1326  if( crt->max_pathlen > 0 )
1327  {
1328  ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1329  SAFE_SNPRINTF();
1330  }
1331  }
1332 
1333  if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1334  {
1335  ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
1336  SAFE_SNPRINTF();
1337 
1338  if( ( ret = x509_info_subject_alt_name( &p, &n,
1339  &crt->subject_alt_names ) ) != 0 )
1340  return( ret );
1341  }
1342 
1343  if( crt->ext_types & EXT_NS_CERT_TYPE )
1344  {
1345  ret = snprintf( p, n, "\n%scert. type : ", prefix );
1346  SAFE_SNPRINTF();
1347 
1348  if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1349  return( ret );
1350  }
1351 
1352  if( crt->ext_types & EXT_KEY_USAGE )
1353  {
1354  ret = snprintf( p, n, "\n%skey usage : ", prefix );
1355  SAFE_SNPRINTF();
1356 
1357  if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1358  return( ret );
1359  }
1360 
1361  if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1362  {
1363  ret = snprintf( p, n, "\n%sext key usage : ", prefix );
1364  SAFE_SNPRINTF();
1365 
1366  if( ( ret = x509_info_ext_key_usage( &p, &n,
1367  &crt->ext_key_usage ) ) != 0 )
1368  return( ret );
1369  }
1370 
1371  ret = snprintf( p, n, "\n" );
1372  SAFE_SNPRINTF();
1373 
1374  return( (int) ( size - n ) );
1375 }
1376 
1377 #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1378 int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1379 {
1380  if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1381  ( crt->key_usage & usage ) != usage )
1383 
1384  return( 0 );
1385 }
1386 #endif
1387 
1388 #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1390  const char *usage_oid,
1391  size_t usage_len )
1392 {
1393  const x509_sequence *cur;
1394 
1395  /* Extension is not mandatory, absent means no restriction */
1396  if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1397  return( 0 );
1398 
1399  /*
1400  * Look for the requested usage (or wildcard ANY) in our list
1401  */
1402  for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1403  {
1404  const x509_buf *cur_oid = &cur->buf;
1405 
1406  if( cur_oid->len == usage_len &&
1407  memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1408  {
1409  return( 0 );
1410  }
1411 
1412  if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1413  return( 0 );
1414  }
1415 
1417 }
1418 #endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
1419 
1420 #if defined(POLARSSL_X509_CRL_PARSE_C)
1421 /*
1422  * Return 1 if the certificate is revoked, or 0 otherwise.
1423  */
1424 int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
1425 {
1426  const x509_crl_entry *cur = &crl->entry;
1427 
1428  while( cur != NULL && cur->serial.len != 0 )
1429  {
1430  if( crt->serial.len == cur->serial.len &&
1431  memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1432  {
1433  if( x509_time_expired( &cur->revocation_date ) )
1434  return( 1 );
1435  }
1436 
1437  cur = cur->next;
1438  }
1439 
1440  return( 0 );
1441 }
1442 
1443 /*
1444  * Check that the given certificate is valid according to the CRL.
1445  */
1446 static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
1447  x509_crl *crl_list)
1448 {
1449  int flags = 0;
1450  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1451  const md_info_t *md_info;
1452 
1453  if( ca == NULL )
1454  return( flags );
1455 
1456  /*
1457  * TODO: What happens if no CRL is present?
1458  * Suggestion: Revocation state should be unknown if no CRL is present.
1459  * For backwards compatibility this is not yet implemented.
1460  */
1461 
1462  while( crl_list != NULL )
1463  {
1464  if( crl_list->version == 0 ||
1465  crl_list->issuer_raw.len != ca->subject_raw.len ||
1466  memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1467  crl_list->issuer_raw.len ) != 0 )
1468  {
1469  crl_list = crl_list->next;
1470  continue;
1471  }
1472 
1473  /*
1474  * Check if the CA is configured to sign CRLs
1475  */
1476 #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1477  if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1478  {
1479  flags |= BADCRL_NOT_TRUSTED;
1480  break;
1481  }
1482 #endif
1483 
1484  /*
1485  * Check if CRL is correctly signed by the trusted CA
1486  */
1487  md_info = md_info_from_type( crl_list->sig_md );
1488  if( md_info == NULL )
1489  {
1490  /*
1491  * Cannot check 'unknown' hash
1492  */
1493  flags |= BADCRL_NOT_TRUSTED;
1494  break;
1495  }
1496 
1497  md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1498 
1499  if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
1500  crl_list->sig_md, hash, md_info->size,
1501  crl_list->sig.p, crl_list->sig.len ) != 0 )
1502  {
1503  flags |= BADCRL_NOT_TRUSTED;
1504  break;
1505  }
1506 
1507  /*
1508  * Check for validity of CRL (Do not drop out)
1509  */
1510  if( x509_time_expired( &crl_list->next_update ) )
1511  flags |= BADCRL_EXPIRED;
1512 
1513  if( x509_time_future( &crl_list->this_update ) )
1514  flags |= BADCRL_FUTURE;
1515 
1516  /*
1517  * Check if certificate is revoked
1518  */
1519  if( x509_crt_revoked( crt, crl_list ) )
1520  {
1521  flags |= BADCERT_REVOKED;
1522  break;
1523  }
1524 
1525  crl_list = crl_list->next;
1526  }
1527  return( flags );
1528 }
1529 #endif /* POLARSSL_X509_CRL_PARSE_C */
1530 
1531 // Equal == 0, inequal == 1
1532 static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1533 {
1534  size_t i;
1535  unsigned char diff;
1536  const unsigned char *n1 = s1, *n2 = s2;
1537 
1538  for( i = 0; i < len; i++ )
1539  {
1540  diff = n1[i] ^ n2[i];
1541 
1542  if( diff == 0 )
1543  continue;
1544 
1545  if( diff == 32 &&
1546  ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1547  ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1548  {
1549  continue;
1550  }
1551 
1552  return( 1 );
1553  }
1554 
1555  return( 0 );
1556 }
1557 
1558 static int x509_wildcard_verify( const char *cn, x509_buf *name )
1559 {
1560  size_t i;
1561  size_t cn_idx = 0, cn_len = strlen( cn );
1562 
1563  if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1564  return( 0 );
1565 
1566  for( i = 0; i < cn_len; ++i )
1567  {
1568  if( cn[i] == '.' )
1569  {
1570  cn_idx = i;
1571  break;
1572  }
1573  }
1574 
1575  if( cn_idx == 0 )
1576  return( 0 );
1577 
1578  if( cn_len - cn_idx == name->len - 1 &&
1579  x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1580  {
1581  return( 1 );
1582  }
1583 
1584  return( 0 );
1585 }
1586 
1587 /*
1588  * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1589  * Return 0 if yes, -1 if not.
1590  *
1591  * top means parent is a locally-trusted certificate
1592  * bottom means child is the end entity cert
1593  */
1594 static int x509_crt_check_parent( const x509_crt *child,
1595  const x509_crt *parent,
1596  int top, int bottom )
1597 {
1598  int need_ca_bit;
1599 
1600  /* Parent must be the issuer */
1601  if( child->issuer_raw.len != parent->subject_raw.len ||
1602  memcmp( child->issuer_raw.p, parent->subject_raw.p,
1603  child->issuer_raw.len ) != 0 )
1604  {
1605  return( -1 );
1606  }
1607 
1608  /* Parent must have the basicConstraints CA bit set as a general rule */
1609  need_ca_bit = 1;
1610 
1611  /* Exception: v1/v2 certificates that are locally trusted. */
1612  if( top && parent->version < 3 )
1613  need_ca_bit = 0;
1614 
1615  /* Exception: self-signed end-entity certs that are locally trusted. */
1616  if( top && bottom &&
1617  child->raw.len == parent->raw.len &&
1618  memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
1619  {
1620  need_ca_bit = 0;
1621  }
1622 
1623  if( need_ca_bit && ! parent->ca_istrue )
1624  return( -1 );
1625 
1626 #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1627  if( need_ca_bit &&
1628  x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1629  {
1630  return( -1 );
1631  }
1632 #endif
1633 
1634  return( 0 );
1635 }
1636 
1637 static int x509_crt_verify_top(
1638  x509_crt *child, x509_crt *trust_ca,
1639  x509_crl *ca_crl, int path_cnt, int *flags,
1640  int (*f_vrfy)(void *, x509_crt *, int, int *),
1641  void *p_vrfy )
1642 {
1643  int ret;
1644  int ca_flags = 0, check_path_cnt = path_cnt + 1;
1645  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1646  const md_info_t *md_info;
1647 
1648  if( x509_time_expired( &child->valid_to ) )
1649  *flags |= BADCERT_EXPIRED;
1650 
1651  if( x509_time_future( &child->valid_from ) )
1652  *flags |= BADCERT_FUTURE;
1653 
1654  /*
1655  * Child is the top of the chain. Check against the trust_ca list.
1656  */
1657  *flags |= BADCERT_NOT_TRUSTED;
1658 
1659  md_info = md_info_from_type( child->sig_md );
1660  if( md_info == NULL )
1661  {
1662  /*
1663  * Cannot check 'unknown', no need to try any CA
1664  */
1665  trust_ca = NULL;
1666  }
1667  else
1668  md( md_info, child->tbs.p, child->tbs.len, hash );
1669 
1670  for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
1671  {
1672  if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
1673  continue;
1674 
1675  /*
1676  * Reduce path_len to check against if top of the chain is
1677  * the same as the trusted CA
1678  */
1679  if( child->subject_raw.len == trust_ca->subject_raw.len &&
1680  memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1681  child->issuer_raw.len ) == 0 )
1682  {
1683  check_path_cnt--;
1684  }
1685 
1686  if( trust_ca->max_pathlen > 0 &&
1687  trust_ca->max_pathlen < check_path_cnt )
1688  {
1689  continue;
1690  }
1691 
1692  if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
1693  child->sig_md, hash, md_info->size,
1694  child->sig.p, child->sig.len ) != 0 )
1695  {
1696  continue;
1697  }
1698 
1699  /*
1700  * Top of chain is signed by a trusted CA
1701  */
1702  *flags &= ~BADCERT_NOT_TRUSTED;
1703  break;
1704  }
1705 
1706  /*
1707  * If top of chain is not the same as the trusted CA send a verify request
1708  * to the callback for any issues with validity and CRL presence for the
1709  * trusted CA certificate.
1710  */
1711  if( trust_ca != NULL &&
1712  ( child->subject_raw.len != trust_ca->subject_raw.len ||
1713  memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1714  child->issuer_raw.len ) != 0 ) )
1715  {
1716 #if defined(POLARSSL_X509_CRL_PARSE_C)
1717  /* Check trusted CA's CRL for the chain's top crt */
1718  *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
1719 #else
1720  ((void) ca_crl);
1721 #endif
1722 
1723  if( x509_time_expired( &trust_ca->valid_to ) )
1724  ca_flags |= BADCERT_EXPIRED;
1725 
1726  if( x509_time_future( &trust_ca->valid_from ) )
1727  ca_flags |= BADCERT_FUTURE;
1728 
1729  if( NULL != f_vrfy )
1730  {
1731  if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1732  &ca_flags ) ) != 0 )
1733  {
1734  return( ret );
1735  }
1736  }
1737  }
1738 
1739  /* Call callback on top cert */
1740  if( NULL != f_vrfy )
1741  {
1742  if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1743  return( ret );
1744  }
1745 
1746  *flags |= ca_flags;
1747 
1748  return( 0 );
1749 }
1750 
1751 static int x509_crt_verify_child(
1752  x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
1753  x509_crl *ca_crl, int path_cnt, int *flags,
1754  int (*f_vrfy)(void *, x509_crt *, int, int *),
1755  void *p_vrfy )
1756 {
1757  int ret;
1758  int parent_flags = 0;
1759  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1760  x509_crt *grandparent;
1761  const md_info_t *md_info;
1762 
1763  if( x509_time_expired( &child->valid_to ) )
1764  *flags |= BADCERT_EXPIRED;
1765 
1766  if( x509_time_future( &child->valid_from ) )
1767  *flags |= BADCERT_FUTURE;
1768 
1769  md_info = md_info_from_type( child->sig_md );
1770  if( md_info == NULL )
1771  {
1772  /*
1773  * Cannot check 'unknown' hash
1774  */
1775  *flags |= BADCERT_NOT_TRUSTED;
1776  }
1777  else
1778  {
1779  md( md_info, child->tbs.p, child->tbs.len, hash );
1780 
1781  if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
1782  child->sig_md, hash, md_info->size,
1783  child->sig.p, child->sig.len ) != 0 )
1784  {
1785  *flags |= BADCERT_NOT_TRUSTED;
1786  }
1787  }
1788 
1789 #if defined(POLARSSL_X509_CRL_PARSE_C)
1790  /* Check trusted CA's CRL for the given crt */
1791  *flags |= x509_crt_verifycrl(child, parent, ca_crl);
1792 #endif
1793 
1794  /* Look for a grandparent upwards the chain */
1795  for( grandparent = parent->next;
1796  grandparent != NULL;
1797  grandparent = grandparent->next )
1798  {
1799  if( x509_crt_check_parent( parent, grandparent,
1800  0, path_cnt == 0 ) == 0 )
1801  break;
1802  }
1803 
1804  /* Is our parent part of the chain or at the top? */
1805  if( grandparent != NULL )
1806  {
1807  ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1808  path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1809  if( ret != 0 )
1810  return( ret );
1811  }
1812  else
1813  {
1814  ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1815  path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1816  if( ret != 0 )
1817  return( ret );
1818  }
1819 
1820  /* child is verified to be a child of the parent, call verify callback */
1821  if( NULL != f_vrfy )
1822  if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1823  return( ret );
1824 
1825  *flags |= parent_flags;
1826 
1827  return( 0 );
1828 }
1829 
1830 /*
1831  * Verify the certificate validity
1832  */
1833 int x509_crt_verify( x509_crt *crt,
1834  x509_crt *trust_ca,
1835  x509_crl *ca_crl,
1836  const char *cn, int *flags,
1837  int (*f_vrfy)(void *, x509_crt *, int, int *),
1838  void *p_vrfy )
1839 {
1840  size_t cn_len;
1841  int ret;
1842  int pathlen = 0;
1843  x509_crt *parent;
1844  x509_name *name;
1845  x509_sequence *cur = NULL;
1846 
1847  *flags = 0;
1848 
1849  if( cn != NULL )
1850  {
1851  name = &crt->subject;
1852  cn_len = strlen( cn );
1853 
1854  if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1855  {
1856  cur = &crt->subject_alt_names;
1857 
1858  while( cur != NULL )
1859  {
1860  if( cur->buf.len == cn_len &&
1861  x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1862  break;
1863 
1864  if( cur->buf.len > 2 &&
1865  memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1866  x509_wildcard_verify( cn, &cur->buf ) )
1867  break;
1868 
1869  cur = cur->next;
1870  }
1871 
1872  if( cur == NULL )
1873  *flags |= BADCERT_CN_MISMATCH;
1874  }
1875  else
1876  {
1877  while( name != NULL )
1878  {
1879  if( OID_CMP( OID_AT_CN, &name->oid ) )
1880  {
1881  if( name->val.len == cn_len &&
1882  x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1883  break;
1884 
1885  if( name->val.len > 2 &&
1886  memcmp( name->val.p, "*.", 2 ) == 0 &&
1887  x509_wildcard_verify( cn, &name->val ) )
1888  break;
1889  }
1890 
1891  name = name->next;
1892  }
1893 
1894  if( name == NULL )
1895  *flags |= BADCERT_CN_MISMATCH;
1896  }
1897  }
1898 
1899  /* Look for a parent upwards the chain */
1900  for( parent = crt->next; parent != NULL; parent = parent->next )
1901  {
1902  if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
1903  break;
1904  }
1905 
1906  /* Are we part of the chain or at the top? */
1907  if( parent != NULL )
1908  {
1909  ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1910  pathlen, flags, f_vrfy, p_vrfy );
1911  if( ret != 0 )
1912  return( ret );
1913  }
1914  else
1915  {
1916  ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1917  pathlen, flags, f_vrfy, p_vrfy );
1918  if( ret != 0 )
1919  return( ret );
1920  }
1921 
1922  if( *flags != 0 )
1924 
1925  return( 0 );
1926 }
1927 
1928 /*
1929  * Initialize a certificate chain
1930  */
1931 void x509_crt_init( x509_crt *crt )
1932 {
1933  memset( crt, 0, sizeof(x509_crt) );
1934 }
1935 
1936 /*
1937  * Unallocate all certificate data
1938  */
1939 void x509_crt_free( x509_crt *crt )
1940 {
1941  x509_crt *cert_cur = crt;
1942  x509_crt *cert_prv;
1943  x509_name *name_cur;
1944  x509_name *name_prv;
1945  x509_sequence *seq_cur;
1946  x509_sequence *seq_prv;
1947 
1948  if( crt == NULL )
1949  return;
1950 
1951  do
1952  {
1953  pk_free( &cert_cur->pk );
1954 
1955 #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
1956  polarssl_free( cert_cur->sig_opts );
1957 #endif
1958 
1959  name_cur = cert_cur->issuer.next;
1960  while( name_cur != NULL )
1961  {
1962  name_prv = name_cur;
1963  name_cur = name_cur->next;
1964  polarssl_zeroize( name_prv, sizeof( x509_name ) );
1965  polarssl_free( name_prv );
1966  }
1967 
1968  name_cur = cert_cur->subject.next;
1969  while( name_cur != NULL )
1970  {
1971  name_prv = name_cur;
1972  name_cur = name_cur->next;
1973  polarssl_zeroize( name_prv, sizeof( x509_name ) );
1974  polarssl_free( name_prv );
1975  }
1976 
1977  seq_cur = cert_cur->ext_key_usage.next;
1978  while( seq_cur != NULL )
1979  {
1980  seq_prv = seq_cur;
1981  seq_cur = seq_cur->next;
1982  polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
1983  polarssl_free( seq_prv );
1984  }
1985 
1986  seq_cur = cert_cur->subject_alt_names.next;
1987  while( seq_cur != NULL )
1988  {
1989  seq_prv = seq_cur;
1990  seq_cur = seq_cur->next;
1991  polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
1992  polarssl_free( seq_prv );
1993  }
1994 
1995  if( cert_cur->raw.p != NULL )
1996  {
1997  polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
1998  polarssl_free( cert_cur->raw.p );
1999  }
2000 
2001  cert_cur = cert_cur->next;
2002  }
2003  while( cert_cur != NULL );
2004 
2005  cert_cur = crt;
2006  do
2007  {
2008  cert_prv = cert_cur;
2009  cert_cur = cert_cur->next;
2010 
2011  polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
2012  if( cert_prv != crt )
2013  polarssl_free( cert_prv );
2014  }
2015  while( cert_cur != NULL );
2016 }
2017 
2018 #endif /* POLARSSL_X509_CRT_PARSE_C */
int md(const md_info_t *md_info, const unsigned char *input, size_t ilen, unsigned char *output)
Output = message_digest( input buffer )
x509_buf sig
Definition: x509_crl.h:93
int x509_time_expired(const x509_time *time)
Check a given x509_time against the system time and check if it is not expired.
int asn1_get_sequence_of(unsigned char **p, const unsigned char *end, asn1_sequence *cur, int tag)
Parses and splits an ASN.1 &quot;SEQUENCE OF &lt;tag&gt;&quot; Updated the pointer to immediately behind the full seq...
x509_sequence subject_alt_names
Optional list of Subject Alternative Names (Only dNSName supported).
Definition: x509_crt.h:80
#define X509_FORMAT_DER
Definition: x509.h:143
#define KU_NON_REPUDIATION
Definition: x509.h:94
#define ASN1_OID
Definition: asn1.h:80
#define EXT_KEY_USAGE
Definition: x509.h:123
int(* polarssl_mutex_lock)(threading_mutex_t *mutex)
int sec
Time.
Definition: x509.h:184
int x509_get_name(unsigned char **p, const unsigned char *end, x509_name *cur)
#define POLARSSL_ERR_X509_INVALID_DATE
The date tag or value is invalid.
Definition: x509.h:59
int version
CRL version (1=v1, 2=v2)
Definition: x509_crl.h:78
asn1_buf buf
Buffer containing the given ASN.1 item.
Definition: asn1.h:148
int x509_get_serial(unsigned char **p, const unsigned char *end, x509_buf *serial)
x509_buf raw
The raw certificate data (DER).
Definition: x509_crt.h:59
x509_time next_update
Definition: x509_crl.h:86
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
Definition: asn1.h:57
int ext_types
Bit string containing detected and parsed extensions.
Definition: x509_crt.h:82
Certificate revocation list entry.
Definition: x509_crl.h:55
#define polarssl_malloc
size_t pk_get_size(const pk_context *ctx)
Get the size in bits of the underlying key.
#define OID_ANY_EXTENDED_KEY_USAGE
anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 }
Definition: oid.h:173
void * sig_opts
Signature options to be passed to pk_verify_ext(), e.g.
Definition: x509_crt.h:96
#define polarssl_free
#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT
Format not recognized as DER or PEM.
Definition: x509.h:66
#define EXT_BASIC_CONSTRAINTS
Definition: x509.h:129
unsigned char ns_cert_type
Optional Netscape certificate type extension value: See the values in x509.h.
Definition: x509_crt.h:90
int x509_get_sig_alg(const x509_buf *sig_oid, const x509_buf *sig_params, md_type_t *md_alg, pk_type_t *pk_alg, void **sig_opts)
#define KU_DATA_ENCIPHERMENT
Definition: x509.h:96
x509_buf issuer_raw
The raw issuer data (DER).
Definition: x509_crt.h:66
#define POLARSSL_ERR_X509_INVALID_FORMAT
The CRT/CRL/CSR format is invalid, e.g.
Definition: x509.h:54
int x509_key_size_helper(char *buf, size_t size, const char *name)
#define NS_CERT_TYPE_OBJECT_SIGNING
Definition: x509.h:109
struct _x509_crl * next
Definition: x509_crl.h:98
int asn1_get_int(unsigned char **p, const unsigned char *end, int *val)
Retrieve an integer ASN.1 tag and its value.
size_t len
ASN1 length, e.g.
Definition: asn1.h:137
void * sig_opts
Signature options to be passed to pk_verify_ext(), e.g.
Definition: x509_crl.h:96
Container for date and time (precision in seconds).
Definition: x509.h:181
int x509_crt_parse(x509_crt *chain, const unsigned char *buf, size_t buflen)
Parse one or more certificates and add them to the chained list.
#define ASN1_SEQUENCE
Definition: asn1.h:82
x509_buf sig_oid2
Signature algorithm.
Definition: x509_crt.h:92
int oid_get_x509_ext_type(const asn1_buf *oid, int *ext_type)
Translate an X.509 extension OID into local values.
void x509_crt_free(x509_crt *crt)
Unallocate all certificate data.
Configuration options (set of defines)
#define OID_CMP(oid_str, oid_buf)
Compares an asn1_buf structure to a reference OID.
Definition: asn1.h:108
x509_buf tbs
The raw certificate body (DER).
Definition: x509_crt.h:60
x509_buf serial
Unique id for certificate issued by a specific CA.
Definition: x509_crt.h:63
md_type_t sig_md
Internal representation of the MD algorithm of the signature algorithm, e.g.
Definition: x509_crt.h:94
#define NS_CERT_TYPE_OBJECT_SIGNING_CA
Definition: x509.h:113
int ca_istrue
Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise...
Definition: x509_crt.h:83
#define ASN1_CONSTRUCTED
Definition: asn1.h:92
int x509_crt_parse_der(x509_crt *chain, const unsigned char *buf, size_t buflen)
Parse a single DER formatted certificate and add it to the chained list.
PolarSSL Platform abstraction layer.
int max_pathlen
Optional Basic Constraint extension value: The maximum path length to the root certificate.
Definition: x509_crt.h:84
int x509_get_sig(unsigned char **p, const unsigned char *end, x509_buf *sig)
const char * pk_get_name(const pk_context *ctx)
Access the type name.
#define BADCRL_NOT_TRUSTED
CRL is not correctly signed by the trusted CA.
Definition: x509.h:80
#define POLARSSL_ERR_ASN1_INVALID_LENGTH
Error when trying to determine the length or invalid length.
Definition: asn1.h:56
Container for ASN1 bit strings.
Definition: asn1.h:135
#define POLARSSL_ERR_X509_UNKNOWN_VERSION
CRT/CRL/CSR has an unsupported version number.
Definition: x509.h:62
Object Identifier (OID) database.
x509_buf serial
Definition: x509_crl.h:59
#define OID_AT_CN
id-at-commonName AttributeType:= {id-at 3}
Definition: oid.h:111
int x509_get_alg(unsigned char **p, const unsigned char *end, x509_buf *alg, x509_buf *params)
struct _x509_crt * next
Next certificate in the CA-chain.
Definition: x509_crt.h:98
int x509_crt_check_key_usage(const x509_crt *crt, int usage)
Check usage of certificate against keyUsage extension.
int pk_verify_ext(pk_type_t type, const void *options, pk_context *ctx, md_type_t md_alg, const unsigned char *hash, size_t hash_len, const unsigned char *sig, size_t sig_len)
Verify signature, with options.
x509_crl_entry entry
The CRL entries containing the certificate revocation times for this CA.
Definition: x509_crl.h:88
asn1_buf val
The named value.
Definition: asn1.h:159
int hour
Definition: x509.h:184
int mon
Definition: x509.h:183
Container for a sequence of ASN.1 items.
Definition: asn1.h:146
#define NS_CERT_TYPE_RESERVED
Definition: x509.h:110
const md_info_t * md_info_from_type(md_type_t md_type)
Returns the message digest information associated with the given digest type.
int x509_get_time(unsigned char **p, const unsigned char *end, x509_time *time)
#define BADCERT_EXPIRED
The certificate validity has expired.
Definition: x509.h:76
#define BADCERT_FUTURE
The certificate validity starts in the future.
Definition: x509.h:85
unsigned char * p
Raw ASN1 data for the bit string.
Definition: asn1.h:139
Threading abstraction layer.
#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED
Certificate verification failed, e.g.
Definition: x509.h:65
Container for an X.509 certificate.
Definition: x509_crt.h:57
Privacy Enhanced Mail (PEM) decoding.
x509_time valid_from
Start time of certificate validity.
Definition: x509_crt.h:72
int x509_dn_gets(char *buf, size_t size, const x509_name *dn)
Store the certificate DN in printable form into buf; no more than size characters will be written...
asn1_buf oid
The object identifier.
Definition: asn1.h:158
#define NS_CERT_TYPE_EMAIL
Definition: x509.h:108
x509_sequence ext_key_usage
Optional list of extended key usage OIDs.
Definition: x509_crt.h:88
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
unsigned char * p
ASN1 data, e.g.
Definition: asn1.h:128
x509_name subject
The parsed subject data (named information object).
Definition: x509_crt.h:70
#define EXT_NS_CERT_TYPE
Definition: x509.h:137
int x509_crt_verify(x509_crt *crt, x509_crt *trust_ca, x509_crl *ca_crl, const char *cn, int *flags, int(*f_vrfy)(void *, x509_crt *, int, int *), void *p_vrfy)
Verify the certificate signature.
x509_buf tbs
The raw certificate body (DER).
Definition: x509_crl.h:76
int asn1_get_bool(unsigned char **p, const unsigned char *end, int *val)
Retrieve a boolean ASN.1 tag and its value.
x509_time valid_to
End time of certificate validity.
Definition: x509_crt.h:73
struct _x509_crl_entry * next
Definition: x509_crl.h:65
md_type_t sig_md
Internal representation of the MD algorithm of the signature algorithm, e.g.
Definition: x509_crl.h:94
X.509 certificate parsing and writing.
#define NS_CERT_TYPE_SSL_CA
Definition: x509.h:111
int day
Date.
Definition: x509.h:183
x509_buf sig_oid1
Signature algorithm, e.g.
Definition: x509_crt.h:64
int tag
ASN1 type, e.g.
Definition: asn1.h:126
#define KU_KEY_AGREEMENT
Definition: x509.h:97
#define POLARSSL_ERR_ASN1_OUT_OF_DATA
Out of data when parsing an ASN1 data structure.
Definition: asn1.h:54
int x509_sig_alg_gets(char *buf, size_t size, const x509_buf *sig_oid, pk_type_t pk_alg, md_type_t md_alg, const void *sig_opts)
#define EXT_EXTENDED_KEY_USAGE
Definition: x509.h:132
int pk_parse_subpubkey(unsigned char **p, const unsigned char *end, pk_context *pk)
Parse a SubjectPublicKeyInfo DER structure.
int x509_load_file(const char *path, unsigned char **buf, size_t *n)
#define POLARSSL_ERR_ASN1_MALLOC_FAILED
Memory allocation failed.
Definition: asn1.h:59
#define ASN1_CONTEXT_SPECIFIC
Definition: asn1.h:93
#define BADCERT_NOT_TRUSTED
The certificate is not correctly signed by the trusted CA.
Definition: x509.h:79
#define POLARSSL_ERR_X509_FILE_IO_ERROR
Read/write of file failed.
Definition: x509.h:69
int x509_crt_revoked(const x509_crt *crt, const x509_crl *crl)
Verify the certificate revocation status.
x509_time this_update
Definition: x509_crl.h:85
Container for a sequence or list of &#39;named&#39; ASN.1 data items.
Definition: asn1.h:156
Type-length-value structure that allows for ASN1 using DER.
Definition: asn1.h:124
pk_type_t sig_pk
Internal representation of the Public Key algorithm of the signature algorithm, e.g.
Definition: x509_crl.h:95
size_t len
ASN1 length, e.g.
Definition: asn1.h:127
#define BADCRL_FUTURE
The CRL is from the future.
Definition: x509.h:86
x509_name issuer
The parsed issuer data (named information object).
Definition: x509_crt.h:69
#define X509_FORMAT_PEM
Definition: x509.h:144
void pk_free(pk_context *ctx)
Free a pk_context.
#define POLARSSL_MD_MAX_SIZE
Definition: md.h:67
int(* polarssl_mutex_unlock)(threading_mutex_t *mutex)
int year
Definition: x509.h:183
#define NS_CERT_TYPE_EMAIL_CA
Definition: x509.h:112
#define BADCERT_REVOKED
The certificate has been revoked (is on a CRL).
Definition: x509.h:77
#define BADCRL_EXPIRED
CRL is expired.
Definition: x509.h:81
int asn1_get_len(unsigned char **p, const unsigned char *end, size_t *len)
Get the length of an ASN.1 element.
#define POLARSSL_ERR_X509_FEATURE_UNAVAILABLE
Unavailable feature, e.g.
Definition: x509.h:52
int asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag.
#define KU_DIGITAL_SIGNATURE
Definition: x509.h:93
int x509_get_ext(unsigned char **p, const unsigned char *end, x509_buf *ext, int tag)
#define NS_CERT_TYPE_SSL_SERVER
Definition: x509.h:107
#define POLARSSL_ERR_X509_INVALID_VERSION
The CRT/CRL/CSR version element is invalid.
Definition: x509.h:55
Certificate revocation list structure.
Definition: x509_crl.h:73
int asn1_get_bitstring(unsigned char **p, const unsigned char *end, asn1_bitstring *bs)
Retrieve a bitstring ASN.1 tag and its value.
pk_context pk
Container for the public key context.
Definition: x509_crt.h:75
#define POLARSSL_ERR_THREADING_MUTEX_ERROR
Locking / unlocking / free failed with error code.
Definition: threading.h:44
int min
Definition: x509.h:184
struct _asn1_named_data * next
The next entry in the sequence.
Definition: asn1.h:160
int size
Output length of the digest function.
Definition: md.h:82
x509_buf issuer_id
Optional X.509 v2/v3 issuer unique identifier.
Definition: x509_crt.h:77
#define POLARSSL_ERR_X509_INVALID_EXTENSIONS
The extension tag or value is invalid.
Definition: x509.h:61
#define ASN1_OCTET_STRING
Definition: asn1.h:78
int x509_time_future(const x509_time *time)
Check a given x509_time against the system time and check if it is not from the future.
int x509_crt_parse_path(x509_crt *chain, const char *path)
Load one or more certificate files from a path and add them to the chained list.
x509_buf v3_ext
Optional X.509 v3 extensions.
Definition: x509_crt.h:79
#define POLARSSL_ERR_X509_BAD_INPUT_DATA
Input invalid.
Definition: x509.h:67
x509_buf subject_id
Optional X.509 v2/v3 subject unique identifier.
Definition: x509_crt.h:78
#define KU_KEY_CERT_SIGN
Definition: x509.h:98
#define BADCERT_CN_MISMATCH
The certificate Common Name (CN) does not match with the expected CN.
Definition: x509.h:78
int version
The X.509 version.
Definition: x509_crt.h:62
#define POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT
No PEM header or footer found.
Definition: pem.h:38
pk_type_t sig_pk
Internal representation of the Public Key algorithm of the signature algorithm, e.g.
Definition: x509_crt.h:95
x509_time revocation_date
Definition: x509_crl.h:61
x509_buf issuer_raw
The raw issuer data (DER).
Definition: x509_crl.h:81
int x509_crt_info(char *buf, size_t size, const char *prefix, const x509_crt *crt)
Returns an informational string about the certificate.
#define NS_CERT_TYPE_SSL_CLIENT
Definition: x509.h:106
#define POLARSSL_ERR_X509_MALLOC_FAILED
Allocation of memory failed.
Definition: x509.h:68
#define POLARSSL_ERR_PEM_BAD_INPUT_DATA
Bad input parameters to function.
Definition: pem.h:46
unsigned char key_usage
Optional key usage extension value: See the values in x509.h.
Definition: x509_crt.h:86
x509_buf subject_raw
The raw subject data (DER).
Definition: x509_crt.h:67
int x509_crt_check_extended_key_usage(const x509_crt *crt, const char *usage_oid, size_t usage_len)
Check usage of certificate against extentedJeyUsage.
Message digest information.
Definition: md.h:74
int oid_get_extended_key_usage(const asn1_buf *oid, const char **desc)
Translate Extended Key Usage OID into description.
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
Definition: asn1.h:55
#define KU_KEY_ENCIPHERMENT
Definition: x509.h:95
int x509_serial_gets(char *buf, size_t size, const x509_buf *serial)
Store the certificate serial in printable form into buf; no more than size characters will be written...
#define POLARSSL_ERR_X509_SIG_MISMATCH
Signature algorithms do not match.
Definition: x509.h:64
struct _asn1_sequence * next
The next entry in the sequence.
Definition: asn1.h:149
int x509_crt_parse_file(x509_crt *chain, const char *path)
Load one or more certificates and add them to the chained list.
x509_buf sig
Signature: hash of the tbs part signed with the private key.
Definition: x509_crt.h:93
#define EXT_SUBJECT_ALT_NAME
Definition: x509.h:126
#define KU_CRL_SIGN
Definition: x509.h:99