PolarSSL v1.3.7
x509_crt.c
Go to the documentation of this file.
1 /*
2  * X.509 certificate and private key decoding
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/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 
37 #if !defined(POLARSSL_CONFIG_FILE)
38 #include "polarssl/config.h"
39 #else
40 #include POLARSSL_CONFIG_FILE
41 #endif
42 
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 
51 #if defined(POLARSSL_PLATFORM_C)
52 #include "polarssl/platform.h"
53 #else
54 #define polarssl_malloc malloc
55 #define polarssl_free free
56 #endif
57 
58 #if defined(POLARSSL_THREADING_C)
59 #include "polarssl/threading.h"
60 #endif
61 
62 #include <string.h>
63 #include <stdlib.h>
64 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
65 #include <windows.h>
66 #else
67 #include <time.h>
68 #endif
69 
70 #if defined(EFIX64) || defined(EFI32)
71 #include <stdio.h>
72 #endif
73 
74 #if defined(POLARSSL_FS_IO)
75 #include <stdio.h>
76 #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
77 #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  */
86 static 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,
95  {
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 )
108  return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
109 
110  if( *p != end )
113 
114  return( 0 );
115 }
116 
117 /*
118  * Validity ::= SEQUENCE {
119  * notBefore Time,
120  * notAfter Time }
121  */
122 static 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 )
132  return( POLARSSL_ERR_X509_INVALID_DATE + ret );
133 
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 )
145 
146  return( 0 );
147 }
148 
149 /*
150  * X.509 v2/v3 unique identifier (not parsed)
151  */
152 static 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  {
167  return( 0 );
168 
169  return( ret );
170  }
171 
172  uid->p = *p;
173  *p += uid->len;
174 
175  return( 0 );
176 }
177 
178 static 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 )
196  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
197 
198  if( *p == end )
199  return 0;
200 
201  if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
202  {
204  ret = asn1_get_int( p, end, ca_istrue );
205 
206  if( ret != 0 )
207  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
208 
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 )
217  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
218 
219  if( *p != end )
222 
223  (*max_pathlen)++;
224 
225  return 0;
226 }
227 
228 static 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 )
236  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
237 
238  if( bs.len != 1 )
241 
242  /* Get actual bitstring */
243  *ns_cert_type = *bs.p;
244  return 0;
245 }
246 
247 static 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 )
255  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
256 
257  if( bs.len < 1 )
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  */
271 static 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 )
278  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
279 
280  /* Sequence length must be >= 1 */
281  if( ext_key_usage->buf.p == NULL )
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  */
314 static 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 )
327  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
328 
329  if( *p + len != end )
332 
333  while( *p < end )
334  {
335  if( ( end - *p ) < 1 )
338 
339  tag = **p;
340  (*p)++;
341  if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
342  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
343 
344  if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
347 
348  /* Skip everything but DNS name */
349  if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
350  {
351  *p += tag_len;
352  continue;
353  }
354 
355  /* Allocate and assign next pointer */
356  if( cur->buf.p != NULL )
357  {
359  sizeof( asn1_sequence ) );
360 
361  if( cur->next == NULL )
364 
365  memset( cur->next, 0, sizeof( asn1_sequence ) );
366  cur = cur->next;
367  }
368 
369  buf = &(cur->buf);
370  buf->tag = tag;
371  buf->p = *p;
372  buf->len = tag_len;
373  *p += buf->len;
374  }
375 
376  /* Set final sequence entry's next pointer to NULL */
377  cur->next = NULL;
378 
379  if( *p != end )
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  */
393 static int x509_get_crt_ext( unsigned char **p,
394  const unsigned char *end,
395  x509_crt *crt )
396 {
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  {
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 )
423  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
424 
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 )
431  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
432 
433  extn_oid.p = *p;
434  *p += extn_oid.len;
435 
436  if( ( end - *p ) < 1 )
439 
440  /* Get optional critical */
441  if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
443  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
444 
445  /* Data should be octet string type */
446  if( ( ret = asn1_get_tag( p, end_ext_data, &len,
447  ASN1_OCTET_STRING ) ) != 0 )
448  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
449 
450  end_ext_octet = *p + len;
451 
452  if( end_ext_octet != end_ext_data )
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 */
472  }
473 #endif
474  continue;
475  }
476 
477  crt->ext_types |= ext_type;
478 
479  switch( ext_type )
480  {
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 
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 
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:
518  }
519  }
520 
521  if( *p != end )
524 
525  return( 0 );
526 }
527 
528 /*
529  * Parse and fill a single X.509 certificate in DER format
530  */
531 static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
532  size_t buflen )
533 {
534  int ret;
535  size_t len;
536  unsigned char *p, *end, *crt_end;
537 
538  /*
539  * Check for valid input
540  */
541  if( crt == NULL || buf == NULL )
543 
544  p = (unsigned char *) polarssl_malloc( len = buflen );
545 
546  if( p == NULL )
548 
549  memcpy( p, buf, buflen );
550 
551  crt->raw.p = p;
552  crt->raw.len = len;
553  end = p + len;
554 
555  /*
556  * Certificate ::= SEQUENCE {
557  * tbsCertificate TBSCertificate,
558  * signatureAlgorithm AlgorithmIdentifier,
559  * signatureValue BIT STRING }
560  */
561  if( ( ret = asn1_get_tag( &p, end, &len,
562  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
563  {
564  x509_crt_free( crt );
566  }
567 
568  if( len > (size_t) ( end - p ) )
569  {
570  x509_crt_free( crt );
573  }
574  crt_end = p + len;
575 
576  /*
577  * TBSCertificate ::= SEQUENCE {
578  */
579  crt->tbs.p = p;
580 
581  if( ( ret = asn1_get_tag( &p, end, &len,
582  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
583  {
584  x509_crt_free( crt );
585  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
586  }
587 
588  end = p + len;
589  crt->tbs.len = end - crt->tbs.p;
590 
591  /*
592  * Version ::= INTEGER { v1(0), v2(1), v3(2) }
593  *
594  * CertificateSerialNumber ::= INTEGER
595  *
596  * signature AlgorithmIdentifier
597  */
598  if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
599  ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
600  ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
601  {
602  x509_crt_free( crt );
603  return( ret );
604  }
605 
606  crt->version++;
607 
608  if( crt->version > 3 )
609  {
610  x509_crt_free( crt );
612  }
613 
614  if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
615  &crt->sig_pk ) ) != 0 )
616  {
617  x509_crt_free( crt );
618  return( ret );
619  }
620 
621  /*
622  * issuer Name
623  */
624  crt->issuer_raw.p = p;
625 
626  if( ( ret = asn1_get_tag( &p, end, &len,
627  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
628  {
629  x509_crt_free( crt );
630  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
631  }
632 
633  if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
634  {
635  x509_crt_free( crt );
636  return( ret );
637  }
638 
639  crt->issuer_raw.len = p - crt->issuer_raw.p;
640 
641  /*
642  * Validity ::= SEQUENCE {
643  * notBefore Time,
644  * notAfter Time }
645  *
646  */
647  if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
648  &crt->valid_to ) ) != 0 )
649  {
650  x509_crt_free( crt );
651  return( ret );
652  }
653 
654  /*
655  * subject Name
656  */
657  crt->subject_raw.p = p;
658 
659  if( ( ret = asn1_get_tag( &p, end, &len,
660  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
661  {
662  x509_crt_free( crt );
663  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
664  }
665 
666  if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
667  {
668  x509_crt_free( crt );
669  return( ret );
670  }
671 
672  crt->subject_raw.len = p - crt->subject_raw.p;
673 
674  /*
675  * SubjectPublicKeyInfo
676  */
677  if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
678  {
679  x509_crt_free( crt );
680  return( ret );
681  }
682 
683  /*
684  * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
685  * -- If present, version shall be v2 or v3
686  * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
687  * -- If present, version shall be v2 or v3
688  * extensions [3] EXPLICIT Extensions OPTIONAL
689  * -- If present, version shall be v3
690  */
691  if( crt->version == 2 || crt->version == 3 )
692  {
693  ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
694  if( ret != 0 )
695  {
696  x509_crt_free( crt );
697  return( ret );
698  }
699  }
700 
701  if( crt->version == 2 || crt->version == 3 )
702  {
703  ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
704  if( ret != 0 )
705  {
706  x509_crt_free( crt );
707  return( ret );
708  }
709  }
710 
711 #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
712  if( crt->version == 3 )
713  {
714 #endif
715  ret = x509_get_crt_ext( &p, end, crt);
716  if( ret != 0 )
717  {
718  x509_crt_free( crt );
719  return( ret );
720  }
721 #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
722  }
723 #endif
724 
725  if( p != end )
726  {
727  x509_crt_free( crt );
730  }
731 
732  end = crt_end;
733 
734  /*
735  * }
736  * -- end of TBSCertificate
737  *
738  * signatureAlgorithm AlgorithmIdentifier,
739  * signatureValue BIT STRING
740  */
741  if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
742  {
743  x509_crt_free( crt );
744  return( ret );
745  }
746 
747  if( crt->sig_oid1.len != crt->sig_oid2.len ||
748  memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
749  {
750  x509_crt_free( crt );
752  }
753 
754  if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
755  {
756  x509_crt_free( crt );
757  return( ret );
758  }
759 
760  if( p != end )
761  {
762  x509_crt_free( crt );
765  }
766 
767  return( 0 );
768 }
769 
770 /*
771  * Parse one X.509 certificate in DER format from a buffer and add them to a
772  * chained list
773  */
774 int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
775  size_t buflen )
776 {
777  int ret;
778  x509_crt *crt = chain, *prev = NULL;
779 
780  /*
781  * Check for valid input
782  */
783  if( crt == NULL || buf == NULL )
785 
786  while( crt->version != 0 && crt->next != NULL )
787  {
788  prev = crt;
789  crt = crt->next;
790  }
791 
792  /*
793  * Add new certificate on the end of the chain if needed.
794  */
795  if ( crt->version != 0 && crt->next == NULL)
796  {
797  crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
798 
799  if( crt->next == NULL )
801 
802  prev = crt;
803  crt = crt->next;
804  x509_crt_init( crt );
805  }
806 
807  if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
808  {
809  if( prev )
810  prev->next = NULL;
811 
812  if( crt != chain )
813  polarssl_free( crt );
814 
815  return( ret );
816  }
817 
818  return( 0 );
819 }
820 
821 /*
822  * Parse one or more PEM certificates from a buffer and add them to the chained
823  * list
824  */
825 int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
826 {
827  int success = 0, first_error = 0, total_failed = 0;
828  int buf_format = X509_FORMAT_DER;
829 
830  /*
831  * Check for valid input
832  */
833  if( chain == NULL || buf == NULL )
835 
836  /*
837  * Determine buffer content. Buffer contains either one DER certificate or
838  * one or more PEM certificates.
839  */
840 #if defined(POLARSSL_PEM_PARSE_C)
841  if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
842  buf_format = X509_FORMAT_PEM;
843 #endif
844 
845  if( buf_format == X509_FORMAT_DER )
846  return x509_crt_parse_der( chain, buf, buflen );
847 
848 #if defined(POLARSSL_PEM_PARSE_C)
849  if( buf_format == X509_FORMAT_PEM )
850  {
851  int ret;
852  pem_context pem;
853 
854  while( buflen > 0 )
855  {
856  size_t use_len;
857  pem_init( &pem );
858 
859  ret = pem_read_buffer( &pem,
860  "-----BEGIN CERTIFICATE-----",
861  "-----END CERTIFICATE-----",
862  buf, NULL, 0, &use_len );
863 
864  if( ret == 0 )
865  {
866  /*
867  * Was PEM encoded
868  */
869  buflen -= use_len;
870  buf += use_len;
871  }
872  else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
873  {
874  return( ret );
875  }
877  {
878  pem_free( &pem );
879 
880  /*
881  * PEM header and footer were found
882  */
883  buflen -= use_len;
884  buf += use_len;
885 
886  if( first_error == 0 )
887  first_error = ret;
888 
889  continue;
890  }
891  else
892  break;
893 
894  ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
895 
896  pem_free( &pem );
897 
898  if( ret != 0 )
899  {
900  /*
901  * Quit parsing on a memory error
902  */
904  return( ret );
905 
906  if( first_error == 0 )
907  first_error = ret;
908 
909  total_failed++;
910  continue;
911  }
912 
913  success = 1;
914  }
915  }
916 #endif /* POLARSSL_PEM_PARSE_C */
917 
918  if( success )
919  return( total_failed );
920  else if( first_error )
921  return( first_error );
922  else
924 }
925 
926 #if defined(POLARSSL_FS_IO)
927 /*
928  * Load one or more certificates and add them to the chained list
929  */
930 int x509_crt_parse_file( x509_crt *chain, const char *path )
931 {
932  int ret;
933  size_t n;
934  unsigned char *buf;
935 
936  if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
937  return( ret );
938 
939  ret = x509_crt_parse( chain, buf, n );
940 
941  memset( buf, 0, n + 1 );
942  polarssl_free( buf );
943 
944  return( ret );
945 }
946 
947 #if defined(POLARSSL_THREADING_PTHREAD)
948 static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
949 #endif
950 
951 int x509_crt_parse_path( x509_crt *chain, const char *path )
952 {
953  int ret = 0;
954 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
955  int w_ret;
956  WCHAR szDir[MAX_PATH];
957  char filename[MAX_PATH];
958  char *p;
959  int len = (int) strlen( path );
960 
961  WIN32_FIND_DATAW file_data;
962  HANDLE hFind;
963 
964  if( len > MAX_PATH - 3 )
966 
967  memset( szDir, 0, sizeof(szDir) );
968  memset( filename, 0, MAX_PATH );
969  memcpy( filename, path, len );
970  filename[len++] = '\\';
971  p = filename + len;
972  filename[len++] = '*';
973 
974  w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
975  MAX_PATH - 3 );
976 
977  hFind = FindFirstFileW( szDir, &file_data );
978  if (hFind == INVALID_HANDLE_VALUE)
980 
981  len = MAX_PATH - len;
982  do
983  {
984  memset( p, 0, len );
985 
986  if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
987  continue;
988 
989  w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
990  lstrlenW(file_data.cFileName),
991  p, len - 1,
992  NULL, NULL );
993 
994  w_ret = x509_crt_parse_file( chain, filename );
995  if( w_ret < 0 )
996  ret++;
997  else
998  ret += w_ret;
999  }
1000  while( FindNextFileW( hFind, &file_data ) != 0 );
1001 
1002  if (GetLastError() != ERROR_NO_MORE_FILES)
1004 
1005  FindClose( hFind );
1006 #else /* _WIN32 */
1007  int t_ret;
1008  struct stat sb;
1009  struct dirent *entry;
1010  char entry_name[255];
1011  DIR *dir = opendir( path );
1012 
1013  if( dir == NULL)
1015 
1016 #if defined(POLARSSL_THREADING_PTHREAD)
1017  if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
1018  return( ret );
1019 #endif
1020 
1021  while( ( entry = readdir( dir ) ) != NULL )
1022  {
1023  snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
1024 
1025  if( stat( entry_name, &sb ) == -1 )
1026  {
1027  closedir( dir );
1029  goto cleanup;
1030  }
1031 
1032  if( !S_ISREG( sb.st_mode ) )
1033  continue;
1034 
1035  // Ignore parse errors
1036  //
1037  t_ret = x509_crt_parse_file( chain, entry_name );
1038  if( t_ret < 0 )
1039  ret++;
1040  else
1041  ret += t_ret;
1042  }
1043  closedir( dir );
1044 
1045 cleanup:
1046 #if defined(POLARSSL_THREADING_PTHREAD)
1047  if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
1049 #endif
1050 
1051 #endif /* _WIN32 */
1052 
1053  return( ret );
1054 }
1055 #endif /* POLARSSL_FS_IO */
1056 
1057 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1058  !defined(EFI32)
1059 #include <stdarg.h>
1060 
1061 #if !defined vsnprintf
1062 #define vsnprintf _vsnprintf
1063 #endif // vsnprintf
1064 
1065 /*
1066  * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1067  * Result value is not size of buffer needed, but -1 if no fit is possible.
1068  *
1069  * This fuction tries to 'fix' this by at least suggesting enlarging the
1070  * size by 20.
1071  */
1072 static int compat_snprintf(char *str, size_t size, const char *format, ...)
1073 {
1074  va_list ap;
1075  int res = -1;
1076 
1077  va_start( ap, format );
1078 
1079  res = vsnprintf( str, size, format, ap );
1080 
1081  va_end( ap );
1082 
1083  // No quick fix possible
1084  if ( res < 0 )
1085  return( (int) size + 20 );
1086 
1087  return res;
1088 }
1089 
1090 #define snprintf compat_snprintf
1091 #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
1092 
1093 #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1094 
1095 #define SAFE_SNPRINTF() \
1096 { \
1097  if( ret == -1 ) \
1098  return( -1 ); \
1099  \
1100  if ( (unsigned int) ret > n ) { \
1101  p[n - 1] = '\0'; \
1102  return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1103  } \
1104  \
1105  n -= (unsigned int) ret; \
1106  p += (unsigned int) ret; \
1107 }
1108 
1109 static int x509_info_subject_alt_name( char **buf, size_t *size,
1110  const x509_sequence *subject_alt_name )
1111 {
1112  size_t i;
1113  size_t n = *size;
1114  char *p = *buf;
1115  const x509_sequence *cur = subject_alt_name;
1116  const char *sep = "";
1117  size_t sep_len = 0;
1118 
1119  while( cur != NULL )
1120  {
1121  if( cur->buf.len + sep_len >= n )
1122  {
1123  *p = '\0';
1124  return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
1125  }
1126 
1127  n -= cur->buf.len + sep_len;
1128  for( i = 0; i < sep_len; i++ )
1129  *p++ = sep[i];
1130  for( i = 0; i < cur->buf.len; i++ )
1131  *p++ = cur->buf.p[i];
1132 
1133  sep = ", ";
1134  sep_len = 2;
1135 
1136  cur = cur->next;
1137  }
1138 
1139  *p = '\0';
1140 
1141  *size = n;
1142  *buf = p;
1143 
1144  return( 0 );
1145 }
1146 
1147 #define PRINT_ITEM(i) \
1148  { \
1149  ret = snprintf( p, n, "%s" i, sep ); \
1150  SAFE_SNPRINTF(); \
1151  sep = ", "; \
1152  }
1153 
1154 #define CERT_TYPE(type,name) \
1155  if( ns_cert_type & type ) \
1156  PRINT_ITEM( name );
1157 
1158 static int x509_info_cert_type( char **buf, size_t *size,
1159  unsigned char ns_cert_type )
1160 {
1161  int ret;
1162  size_t n = *size;
1163  char *p = *buf;
1164  const char *sep = "";
1165 
1166  CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
1167  CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
1168  CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
1169  CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
1170  CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
1171  CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
1172  CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
1173  CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
1174 
1175  *size = n;
1176  *buf = p;
1177 
1178  return( 0 );
1179 }
1180 
1181 #define KEY_USAGE(code,name) \
1182  if( key_usage & code ) \
1183  PRINT_ITEM( name );
1184 
1185 static int x509_info_key_usage( char **buf, size_t *size,
1186  unsigned char key_usage )
1187 {
1188  int ret;
1189  size_t n = *size;
1190  char *p = *buf;
1191  const char *sep = "";
1192 
1193  KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
1194  KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
1195  KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
1196  KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
1197  KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
1198  KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
1199  KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
1200 
1201  *size = n;
1202  *buf = p;
1203 
1204  return( 0 );
1205 }
1206 
1207 static int x509_info_ext_key_usage( char **buf, size_t *size,
1208  const x509_sequence *extended_key_usage )
1209 {
1210  int ret;
1211  const char *desc;
1212  size_t n = *size;
1213  char *p = *buf;
1214  const x509_sequence *cur = extended_key_usage;
1215  const char *sep = "";
1216 
1217  while( cur != NULL )
1218  {
1219  if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
1220  desc = "???";
1221 
1222  ret = snprintf( p, n, "%s%s", sep, desc );
1223  SAFE_SNPRINTF();
1224 
1225  sep = ", ";
1226 
1227  cur = cur->next;
1228  }
1229 
1230  *size = n;
1231  *buf = p;
1232 
1233  return( 0 );
1234 }
1235 
1236 /*
1237  * Return an informational string about the certificate.
1238  */
1239 #define BEFORE_COLON 18
1240 #define BC "18"
1241 int x509_crt_info( char *buf, size_t size, const char *prefix,
1242  const x509_crt *crt )
1243 {
1244  int ret;
1245  size_t n;
1246  char *p;
1247  const char *desc = NULL;
1248  char key_size_str[BEFORE_COLON];
1249 
1250  p = buf;
1251  n = size;
1252 
1253  ret = snprintf( p, n, "%scert. version : %d\n",
1254  prefix, crt->version );
1255  SAFE_SNPRINTF();
1256  ret = snprintf( p, n, "%sserial number : ",
1257  prefix );
1258  SAFE_SNPRINTF();
1259 
1260  ret = x509_serial_gets( p, n, &crt->serial);
1261  SAFE_SNPRINTF();
1262 
1263  ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1264  SAFE_SNPRINTF();
1265  ret = x509_dn_gets( p, n, &crt->issuer );
1266  SAFE_SNPRINTF();
1267 
1268  ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1269  SAFE_SNPRINTF();
1270  ret = x509_dn_gets( p, n, &crt->subject );
1271  SAFE_SNPRINTF();
1272 
1273  ret = snprintf( p, n, "\n%sissued on : " \
1274  "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1275  crt->valid_from.year, crt->valid_from.mon,
1276  crt->valid_from.day, crt->valid_from.hour,
1277  crt->valid_from.min, crt->valid_from.sec );
1278  SAFE_SNPRINTF();
1279 
1280  ret = snprintf( p, n, "\n%sexpires on : " \
1281  "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1282  crt->valid_to.year, crt->valid_to.mon,
1283  crt->valid_to.day, crt->valid_to.hour,
1284  crt->valid_to.min, crt->valid_to.sec );
1285  SAFE_SNPRINTF();
1286 
1287  ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1288  SAFE_SNPRINTF();
1289 
1290  ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1291  if( ret != 0 )
1292  ret = snprintf( p, n, "???" );
1293  else
1294  ret = snprintf( p, n, "%s", desc );
1295  SAFE_SNPRINTF();
1296 
1297  /* Key size */
1298  if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1299  pk_get_name( &crt->pk ) ) ) != 0 )
1300  {
1301  return( ret );
1302  }
1303 
1304  ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1305  (int) pk_get_size( &crt->pk ) );
1306  SAFE_SNPRINTF();
1307 
1308  /*
1309  * Optional extensions
1310  */
1311 
1312  if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
1313  {
1314  ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
1315  crt->ca_istrue ? "true" : "false" );
1316  SAFE_SNPRINTF();
1317 
1318  if( crt->max_pathlen > 0 )
1319  {
1320  ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
1321  SAFE_SNPRINTF();
1322  }
1323  }
1324 
1325  if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1326  {
1327  ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
1328  SAFE_SNPRINTF();
1329 
1330  if( ( ret = x509_info_subject_alt_name( &p, &n,
1331  &crt->subject_alt_names ) ) != 0 )
1332  return( ret );
1333  }
1334 
1335  if( crt->ext_types & EXT_NS_CERT_TYPE )
1336  {
1337  ret = snprintf( p, n, "\n%scert. type : ", prefix );
1338  SAFE_SNPRINTF();
1339 
1340  if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
1341  return( ret );
1342  }
1343 
1344  if( crt->ext_types & EXT_KEY_USAGE )
1345  {
1346  ret = snprintf( p, n, "\n%skey usage : ", prefix );
1347  SAFE_SNPRINTF();
1348 
1349  if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
1350  return( ret );
1351  }
1352 
1353  if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
1354  {
1355  ret = snprintf( p, n, "\n%sext key usage : ", prefix );
1356  SAFE_SNPRINTF();
1357 
1358  if( ( ret = x509_info_ext_key_usage( &p, &n,
1359  &crt->ext_key_usage ) ) != 0 )
1360  return( ret );
1361  }
1362 
1363  ret = snprintf( p, n, "\n" );
1364  SAFE_SNPRINTF();
1365 
1366  return( (int) ( size - n ) );
1367 }
1368 
1369 #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1370 int x509_crt_check_key_usage( const x509_crt *crt, int usage )
1371 {
1372  if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
1373  ( crt->key_usage & usage ) != usage )
1375 
1376  return( 0 );
1377 }
1378 #endif
1379 
1380 #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
1382  const char *usage_oid,
1383  size_t usage_len )
1384 {
1385  const x509_sequence *cur;
1386 
1387  /* Extension is not mandatory, absent means no restriction */
1388  if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
1389  return( 0 );
1390 
1391  /*
1392  * Look for the requested usage (or wildcard ANY) in our list
1393  */
1394  for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
1395  {
1396  const x509_buf *cur_oid = &cur->buf;
1397 
1398  if( cur_oid->len == usage_len &&
1399  memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
1400  {
1401  return( 0 );
1402  }
1403 
1404  if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
1405  return( 0 );
1406  }
1407 
1409 }
1410 #endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
1411 
1412 #if defined(POLARSSL_X509_CRL_PARSE_C)
1413 /*
1414  * Return 1 if the certificate is revoked, or 0 otherwise.
1415  */
1416 int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
1417 {
1418  const x509_crl_entry *cur = &crl->entry;
1419 
1420  while( cur != NULL && cur->serial.len != 0 )
1421  {
1422  if( crt->serial.len == cur->serial.len &&
1423  memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1424  {
1425  if( x509_time_expired( &cur->revocation_date ) )
1426  return( 1 );
1427  }
1428 
1429  cur = cur->next;
1430  }
1431 
1432  return( 0 );
1433 }
1434 
1435 /*
1436  * Check that the given certificate is valid according to the CRL.
1437  */
1438 static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
1439  x509_crl *crl_list)
1440 {
1441  int flags = 0;
1442  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1443  const md_info_t *md_info;
1444 
1445  if( ca == NULL )
1446  return( flags );
1447 
1448  /*
1449  * TODO: What happens if no CRL is present?
1450  * Suggestion: Revocation state should be unknown if no CRL is present.
1451  * For backwards compatibility this is not yet implemented.
1452  */
1453 
1454  while( crl_list != NULL )
1455  {
1456  if( crl_list->version == 0 ||
1457  crl_list->issuer_raw.len != ca->subject_raw.len ||
1458  memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1459  crl_list->issuer_raw.len ) != 0 )
1460  {
1461  crl_list = crl_list->next;
1462  continue;
1463  }
1464 
1465  /*
1466  * Check if the CA is configured to sign CRLs
1467  */
1468 #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1469  if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
1470  {
1471  flags |= BADCRL_NOT_TRUSTED;
1472  break;
1473  }
1474 #endif
1475 
1476  /*
1477  * Check if CRL is correctly signed by the trusted CA
1478  */
1479  md_info = md_info_from_type( crl_list->sig_md );
1480  if( md_info == NULL )
1481  {
1482  /*
1483  * Cannot check 'unknown' hash
1484  */
1485  flags |= BADCRL_NOT_TRUSTED;
1486  break;
1487  }
1488 
1489  md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1490 
1491  if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1492  pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1493  crl_list->sig.p, crl_list->sig.len ) != 0 )
1494  {
1495  flags |= BADCRL_NOT_TRUSTED;
1496  break;
1497  }
1498 
1499  /*
1500  * Check for validity of CRL (Do not drop out)
1501  */
1502  if( x509_time_expired( &crl_list->next_update ) )
1503  flags |= BADCRL_EXPIRED;
1504 
1505  if( x509_time_future( &crl_list->this_update ) )
1506  flags |= BADCRL_FUTURE;
1507 
1508  /*
1509  * Check if certificate is revoked
1510  */
1511  if( x509_crt_revoked(crt, crl_list) )
1512  {
1513  flags |= BADCERT_REVOKED;
1514  break;
1515  }
1516 
1517  crl_list = crl_list->next;
1518  }
1519  return flags;
1520 }
1521 #endif /* POLARSSL_X509_CRL_PARSE_C */
1522 
1523 // Equal == 0, inequal == 1
1524 static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1525 {
1526  size_t i;
1527  unsigned char diff;
1528  const unsigned char *n1 = s1, *n2 = s2;
1529 
1530  for( i = 0; i < len; i++ )
1531  {
1532  diff = n1[i] ^ n2[i];
1533 
1534  if( diff == 0 )
1535  continue;
1536 
1537  if( diff == 32 &&
1538  ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
1539  ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
1540  {
1541  continue;
1542  }
1543 
1544  return( 1 );
1545  }
1546 
1547  return( 0 );
1548 }
1549 
1550 static int x509_wildcard_verify( const char *cn, x509_buf *name )
1551 {
1552  size_t i;
1553  size_t cn_idx = 0;
1554 
1555  if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1556  return( 0 );
1557 
1558  for( i = 0; i < strlen( cn ); ++i )
1559  {
1560  if( cn[i] == '.' )
1561  {
1562  cn_idx = i;
1563  break;
1564  }
1565  }
1566 
1567  if( cn_idx == 0 )
1568  return( 0 );
1569 
1570  if( strlen( cn ) - cn_idx == name->len - 1 &&
1571  x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1572  {
1573  return( 1 );
1574  }
1575 
1576  return( 0 );
1577 }
1578 
1579 /*
1580  * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1581  * Return 0 if yes, -1 if not.
1582  */
1583 static int x509_crt_check_parent( const x509_crt *child,
1584  const x509_crt *parent )
1585 {
1586  if( parent->version == 0 ||
1587  parent->ca_istrue == 0 ||
1588  child->issuer_raw.len != parent->subject_raw.len ||
1589  memcmp( child->issuer_raw.p, parent->subject_raw.p,
1590  child->issuer_raw.len ) != 0 )
1591  {
1592  return( -1 );
1593  }
1594 
1595 #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
1596  if( x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
1597  return( -1 );
1598 #endif
1599 
1600  return( 0 );
1601 }
1602 
1603 static int x509_crt_verify_top(
1604  x509_crt *child, x509_crt *trust_ca,
1605  x509_crl *ca_crl, int path_cnt, int *flags,
1606  int (*f_vrfy)(void *, x509_crt *, int, int *),
1607  void *p_vrfy )
1608 {
1609  int ret;
1610  int ca_flags = 0, check_path_cnt = path_cnt + 1;
1611  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1612  const md_info_t *md_info;
1613 
1614  if( x509_time_expired( &child->valid_to ) )
1615  *flags |= BADCERT_EXPIRED;
1616 
1617  if( x509_time_future( &child->valid_from ) )
1618  *flags |= BADCERT_FUTURE;
1619 
1620  /*
1621  * Child is the top of the chain. Check against the trust_ca list.
1622  */
1623  *flags |= BADCERT_NOT_TRUSTED;
1624 
1625  md_info = md_info_from_type( child->sig_md );
1626  if( md_info == NULL )
1627  {
1628  /*
1629  * Cannot check 'unknown', no need to try any CA
1630  */
1631  trust_ca = NULL;
1632  }
1633  else
1634  md( md_info, child->tbs.p, child->tbs.len, hash );
1635 
1636  for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
1637  {
1638  if( x509_crt_check_parent( child, trust_ca ) != 0 )
1639  continue;
1640 
1641  /*
1642  * Reduce path_len to check against if top of the chain is
1643  * the same as the trusted CA
1644  */
1645  if( child->subject_raw.len == trust_ca->subject_raw.len &&
1646  memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1647  child->issuer_raw.len ) == 0 )
1648  {
1649  check_path_cnt--;
1650  }
1651 
1652  if( trust_ca->max_pathlen > 0 &&
1653  trust_ca->max_pathlen < check_path_cnt )
1654  {
1655  continue;
1656  }
1657 
1658  if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1659  pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1660  child->sig.p, child->sig.len ) != 0 )
1661  {
1662  continue;
1663  }
1664 
1665  /*
1666  * Top of chain is signed by a trusted CA
1667  */
1668  *flags &= ~BADCERT_NOT_TRUSTED;
1669  break;
1670  }
1671 
1672  /*
1673  * If top of chain is not the same as the trusted CA send a verify request
1674  * to the callback for any issues with validity and CRL presence for the
1675  * trusted CA certificate.
1676  */
1677  if( trust_ca != NULL &&
1678  ( child->subject_raw.len != trust_ca->subject_raw.len ||
1679  memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1680  child->issuer_raw.len ) != 0 ) )
1681  {
1682 #if defined(POLARSSL_X509_CRL_PARSE_C)
1683  /* Check trusted CA's CRL for the chain's top crt */
1684  *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
1685 #else
1686  ((void) ca_crl);
1687 #endif
1688 
1689  if( x509_time_expired( &trust_ca->valid_to ) )
1690  ca_flags |= BADCERT_EXPIRED;
1691 
1692  if( x509_time_future( &trust_ca->valid_from ) )
1693  ca_flags |= BADCERT_FUTURE;
1694 
1695  if( NULL != f_vrfy )
1696  {
1697  if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
1698  &ca_flags ) ) != 0 )
1699  {
1700  return( ret );
1701  }
1702  }
1703  }
1704 
1705  /* Call callback on top cert */
1706  if( NULL != f_vrfy )
1707  {
1708  if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1709  return( ret );
1710  }
1711 
1712  *flags |= ca_flags;
1713 
1714  return( 0 );
1715 }
1716 
1717 static int x509_crt_verify_child(
1718  x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
1719  x509_crl *ca_crl, int path_cnt, int *flags,
1720  int (*f_vrfy)(void *, x509_crt *, int, int *),
1721  void *p_vrfy )
1722 {
1723  int ret;
1724  int parent_flags = 0;
1725  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1726  x509_crt *grandparent;
1727  const md_info_t *md_info;
1728 
1729  if( x509_time_expired( &child->valid_to ) )
1730  *flags |= BADCERT_EXPIRED;
1731 
1732  if( x509_time_future( &child->valid_from ) )
1733  *flags |= BADCERT_FUTURE;
1734 
1735  md_info = md_info_from_type( child->sig_md );
1736  if( md_info == NULL )
1737  {
1738  /*
1739  * Cannot check 'unknown' hash
1740  */
1741  *flags |= BADCERT_NOT_TRUSTED;
1742  }
1743  else
1744  {
1745  md( md_info, child->tbs.p, child->tbs.len, hash );
1746 
1747  if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1748  pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1749  child->sig.p, child->sig.len ) != 0 )
1750  {
1751  *flags |= BADCERT_NOT_TRUSTED;
1752  }
1753  }
1754 
1755 #if defined(POLARSSL_X509_CRL_PARSE_C)
1756  /* Check trusted CA's CRL for the given crt */
1757  *flags |= x509_crt_verifycrl(child, parent, ca_crl);
1758 #endif
1759 
1760  /* Look for a grandparent upwards the chain */
1761  for( grandparent = parent->next;
1762  grandparent != NULL;
1763  grandparent = grandparent->next )
1764  {
1765  if( x509_crt_check_parent( parent, grandparent ) == 0 )
1766  break;
1767  }
1768 
1769  /* Is our parent part of the chain or at the top? */
1770  if( grandparent != NULL )
1771  {
1772  ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
1773  path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1774  if( ret != 0 )
1775  return( ret );
1776  }
1777  else
1778  {
1779  ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
1780  path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1781  if( ret != 0 )
1782  return( ret );
1783  }
1784 
1785  /* child is verified to be a child of the parent, call verify callback */
1786  if( NULL != f_vrfy )
1787  if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1788  return( ret );
1789 
1790  *flags |= parent_flags;
1791 
1792  return( 0 );
1793 }
1794 
1795 /*
1796  * Verify the certificate validity
1797  */
1798 int x509_crt_verify( x509_crt *crt,
1799  x509_crt *trust_ca,
1800  x509_crl *ca_crl,
1801  const char *cn, int *flags,
1802  int (*f_vrfy)(void *, x509_crt *, int, int *),
1803  void *p_vrfy )
1804 {
1805  size_t cn_len;
1806  int ret;
1807  int pathlen = 0;
1808  x509_crt *parent;
1809  x509_name *name;
1810  x509_sequence *cur = NULL;
1811 
1812  *flags = 0;
1813 
1814  if( cn != NULL )
1815  {
1816  name = &crt->subject;
1817  cn_len = strlen( cn );
1818 
1819  if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1820  {
1821  cur = &crt->subject_alt_names;
1822 
1823  while( cur != NULL )
1824  {
1825  if( cur->buf.len == cn_len &&
1826  x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1827  break;
1828 
1829  if( cur->buf.len > 2 &&
1830  memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1831  x509_wildcard_verify( cn, &cur->buf ) )
1832  break;
1833 
1834  cur = cur->next;
1835  }
1836 
1837  if( cur == NULL )
1838  *flags |= BADCERT_CN_MISMATCH;
1839  }
1840  else
1841  {
1842  while( name != NULL )
1843  {
1844  if( OID_CMP( OID_AT_CN, &name->oid ) )
1845  {
1846  if( name->val.len == cn_len &&
1847  x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1848  break;
1849 
1850  if( name->val.len > 2 &&
1851  memcmp( name->val.p, "*.", 2 ) == 0 &&
1852  x509_wildcard_verify( cn, &name->val ) )
1853  break;
1854  }
1855 
1856  name = name->next;
1857  }
1858 
1859  if( name == NULL )
1860  *flags |= BADCERT_CN_MISMATCH;
1861  }
1862  }
1863 
1864  /* Look for a parent upwards the chain */
1865  for( parent = crt->next; parent != NULL; parent = parent->next )
1866  {
1867  if( x509_crt_check_parent( crt, parent ) == 0 )
1868  break;
1869  }
1870 
1871  /* Are we part of the chain or at the top? */
1872  if( parent != NULL )
1873  {
1874  ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
1875  pathlen, flags, f_vrfy, p_vrfy );
1876  if( ret != 0 )
1877  return( ret );
1878  }
1879  else
1880  {
1881  ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
1882  pathlen, flags, f_vrfy, p_vrfy );
1883  if( ret != 0 )
1884  return( ret );
1885  }
1886 
1887  if( *flags != 0 )
1889 
1890  return( 0 );
1891 }
1892 
1893 /*
1894  * Initialize a certificate chain
1895  */
1896 void x509_crt_init( x509_crt *crt )
1897 {
1898  memset( crt, 0, sizeof(x509_crt) );
1899 }
1900 
1901 /*
1902  * Unallocate all certificate data
1903  */
1904 void x509_crt_free( x509_crt *crt )
1905 {
1906  x509_crt *cert_cur = crt;
1907  x509_crt *cert_prv;
1908  x509_name *name_cur;
1909  x509_name *name_prv;
1910  x509_sequence *seq_cur;
1911  x509_sequence *seq_prv;
1912 
1913  if( crt == NULL )
1914  return;
1915 
1916  do
1917  {
1918  pk_free( &cert_cur->pk );
1919 
1920  name_cur = cert_cur->issuer.next;
1921  while( name_cur != NULL )
1922  {
1923  name_prv = name_cur;
1924  name_cur = name_cur->next;
1925  memset( name_prv, 0, sizeof( x509_name ) );
1926  polarssl_free( name_prv );
1927  }
1928 
1929  name_cur = cert_cur->subject.next;
1930  while( name_cur != NULL )
1931  {
1932  name_prv = name_cur;
1933  name_cur = name_cur->next;
1934  memset( name_prv, 0, sizeof( x509_name ) );
1935  polarssl_free( name_prv );
1936  }
1937 
1938  seq_cur = cert_cur->ext_key_usage.next;
1939  while( seq_cur != NULL )
1940  {
1941  seq_prv = seq_cur;
1942  seq_cur = seq_cur->next;
1943  memset( seq_prv, 0, sizeof( x509_sequence ) );
1944  polarssl_free( seq_prv );
1945  }
1946 
1947  seq_cur = cert_cur->subject_alt_names.next;
1948  while( seq_cur != NULL )
1949  {
1950  seq_prv = seq_cur;
1951  seq_cur = seq_cur->next;
1952  memset( seq_prv, 0, sizeof( x509_sequence ) );
1953  polarssl_free( seq_prv );
1954  }
1955 
1956  if( cert_cur->raw.p != NULL )
1957  {
1958  memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1959  polarssl_free( cert_cur->raw.p );
1960  }
1961 
1962  cert_cur = cert_cur->next;
1963  }
1964  while( cert_cur != NULL );
1965 
1966  cert_cur = crt;
1967  do
1968  {
1969  cert_prv = cert_cur;
1970  cert_cur = cert_cur->next;
1971 
1972  memset( cert_prv, 0, sizeof( x509_crt ) );
1973  if( cert_prv != crt )
1974  polarssl_free( cert_prv );
1975  }
1976  while( cert_cur != NULL );
1977 }
1978 
1979 #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
Definition: x509_crl.h:78
asn1_buf buf
Buffer containing the given ASN.1 item.
Definition: asn1.h:144
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
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
#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
#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:97
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:133
int x509_get_alg_null(unsigned char **p, const unsigned char *end, x509_buf *alg)
Container for date and time (precision in seconds).
Definition: x509.h:181
#define polarssl_free
Definition: platform.h:91
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 two asn1_buf structures for the same OID.
Definition: asn1.h:104
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:131
#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
struct _x509_crt * next
Next certificate in the CA-chain.
Definition: x509_crt.h:97
int x509_crt_check_key_usage(const x509_crt *crt, int usage)
Check usage of certificate against keyUsage extension.
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:155
int hour
Definition: x509.h:184
int mon
Definition: x509.h:183
Container for a sequence of ASN.1 items.
Definition: asn1.h:142
#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:135
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:154
#define NS_CERT_TYPE_EMAIL
Definition: x509.h:108
int pk_verify(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.
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:124
int oid_get_sig_alg_desc(const asn1_buf *oid, const char **desc)
Translate SignatureAlgorithm OID into description.
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.
int pk_can_do(pk_context *ctx, pk_type_t type)
Tell if a context can do the operation given by type.
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
int x509_get_sig_alg(const x509_buf *sig_oid, md_type_t *md_alg, pk_type_t *pk_alg)
x509_buf sig_oid1
Signature algorithm, e.g.
Definition: x509_crt.h:64
int tag
ASN1 type, e.g.
Definition: asn1.h:122
#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
#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:152
Type-length-value structure that allows for ASN1 using DER.
Definition: asn1.h:120
pk_type_t sig_pk
&lt; Internal representation of the Public Key algorithm of the signature algorithm, e...
Definition: x509_crl.h:95
size_t len
ASN1 length, e.g.
Definition: asn1.h:123
#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:156
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
&lt; Internal representation of the Public Key algorithm of the signature algorithm, e...
Definition: x509_crt.h:95
#define polarssl_malloc
Definition: platform.h:90
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:145
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