PolarSSL v1.3.2
x509_crt.c
Go to the documentation of this file.
1 /*
2  * X.509 certificate and private key decoding
3  *
4  * Copyright (C) 2006-2013, 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 #include "polarssl/config.h"
38 
39 #if defined(POLARSSL_X509_CRT_PARSE_C)
40 
41 #include "polarssl/x509_crt.h"
42 #include "polarssl/oid.h"
43 #if defined(POLARSSL_PEM_PARSE_C)
44 #include "polarssl/pem.h"
45 #endif
46 
47 #if defined(POLARSSL_MEMORY_C)
48 #include "polarssl/memory.h"
49 #else
50 #define polarssl_malloc malloc
51 #define polarssl_free free
52 #endif
53 
54 #include <string.h>
55 #include <stdlib.h>
56 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
57 #include <windows.h>
58 #else
59 #include <time.h>
60 #endif
61 
62 #if defined(EFIX64) || defined(EFI32)
63 #include <stdio.h>
64 #endif
65 
66 #if defined(POLARSSL_FS_IO)
67 #include <stdio.h>
68 #if !defined(_WIN32)
69 #include <sys/types.h>
70 #include <sys/stat.h>
71 #include <dirent.h>
72 #endif
73 #endif
74 
75 /*
76  * Version ::= INTEGER { v1(0), v2(1), v3(2) }
77  */
78 static int x509_get_version( unsigned char **p,
79  const unsigned char *end,
80  int *ver )
81 {
82  int ret;
83  size_t len;
84 
85  if( ( ret = asn1_get_tag( p, end, &len,
87  {
89  {
90  *ver = 0;
91  return( 0 );
92  }
93 
94  return( ret );
95  }
96 
97  end = *p + len;
98 
99  if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
100  return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
101 
102  if( *p != end )
105 
106  return( 0 );
107 }
108 
109 /*
110  * Validity ::= SEQUENCE {
111  * notBefore Time,
112  * notAfter Time }
113  */
114 static int x509_get_dates( unsigned char **p,
115  const unsigned char *end,
116  x509_time *from,
117  x509_time *to )
118 {
119  int ret;
120  size_t len;
121 
122  if( ( ret = asn1_get_tag( p, end, &len,
123  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
124  return( POLARSSL_ERR_X509_INVALID_DATE + ret );
125 
126  end = *p + len;
127 
128  if( ( ret = x509_get_time( p, end, from ) ) != 0 )
129  return( ret );
130 
131  if( ( ret = x509_get_time( p, end, to ) ) != 0 )
132  return( ret );
133 
134  if( *p != end )
137 
138  return( 0 );
139 }
140 
141 /*
142  * X.509 v2/v3 unique identifier (not parsed)
143  */
144 static int x509_get_uid( unsigned char **p,
145  const unsigned char *end,
146  x509_buf *uid, int n )
147 {
148  int ret;
149 
150  if( *p == end )
151  return( 0 );
152 
153  uid->tag = **p;
154 
155  if( ( ret = asn1_get_tag( p, end, &uid->len,
156  ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
157  {
159  return( 0 );
160 
161  return( ret );
162  }
163 
164  uid->p = *p;
165  *p += uid->len;
166 
167  return( 0 );
168 }
169 
170 static int x509_get_basic_constraints( unsigned char **p,
171  const unsigned char *end,
172  int *ca_istrue,
173  int *max_pathlen )
174 {
175  int ret;
176  size_t len;
177 
178  /*
179  * BasicConstraints ::= SEQUENCE {
180  * cA BOOLEAN DEFAULT FALSE,
181  * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
182  */
183  *ca_istrue = 0; /* DEFAULT FALSE */
184  *max_pathlen = 0; /* endless */
185 
186  if( ( ret = asn1_get_tag( p, end, &len,
187  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
188  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
189 
190  if( *p == end )
191  return 0;
192 
193  if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
194  {
196  ret = asn1_get_int( p, end, ca_istrue );
197 
198  if( ret != 0 )
199  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
200 
201  if( *ca_istrue != 0 )
202  *ca_istrue = 1;
203  }
204 
205  if( *p == end )
206  return 0;
207 
208  if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
209  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
210 
211  if( *p != end )
214 
215  (*max_pathlen)++;
216 
217  return 0;
218 }
219 
220 static int x509_get_ns_cert_type( unsigned char **p,
221  const unsigned char *end,
222  unsigned char *ns_cert_type)
223 {
224  int ret;
225  x509_bitstring bs = { 0, 0, NULL };
226 
227  if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
228  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
229 
230  if( bs.len != 1 )
233 
234  /* Get actual bitstring */
235  *ns_cert_type = *bs.p;
236  return 0;
237 }
238 
239 static int x509_get_key_usage( unsigned char **p,
240  const unsigned char *end,
241  unsigned char *key_usage)
242 {
243  int ret;
244  x509_bitstring bs = { 0, 0, NULL };
245 
246  if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
247  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
248 
249  if( bs.len < 1 )
252 
253  /* Get actual bitstring */
254  *key_usage = *bs.p;
255  return 0;
256 }
257 
258 /*
259  * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
260  *
261  * KeyPurposeId ::= OBJECT IDENTIFIER
262  */
263 static int x509_get_ext_key_usage( unsigned char **p,
264  const unsigned char *end,
265  x509_sequence *ext_key_usage)
266 {
267  int ret;
268 
269  if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
270  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
271 
272  /* Sequence length must be >= 1 */
273  if( ext_key_usage->buf.p == NULL )
276 
277  return 0;
278 }
279 
280 /*
281  * SubjectAltName ::= GeneralNames
282  *
283  * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
284  *
285  * GeneralName ::= CHOICE {
286  * otherName [0] OtherName,
287  * rfc822Name [1] IA5String,
288  * dNSName [2] IA5String,
289  * x400Address [3] ORAddress,
290  * directoryName [4] Name,
291  * ediPartyName [5] EDIPartyName,
292  * uniformResourceIdentifier [6] IA5String,
293  * iPAddress [7] OCTET STRING,
294  * registeredID [8] OBJECT IDENTIFIER }
295  *
296  * OtherName ::= SEQUENCE {
297  * type-id OBJECT IDENTIFIER,
298  * value [0] EXPLICIT ANY DEFINED BY type-id }
299  *
300  * EDIPartyName ::= SEQUENCE {
301  * nameAssigner [0] DirectoryString OPTIONAL,
302  * partyName [1] DirectoryString }
303  *
304  * NOTE: PolarSSL only parses and uses dNSName at this point.
305  */
306 static int x509_get_subject_alt_name( unsigned char **p,
307  const unsigned char *end,
308  x509_sequence *subject_alt_name )
309 {
310  int ret;
311  size_t len, tag_len;
312  asn1_buf *buf;
313  unsigned char tag;
314  asn1_sequence *cur = subject_alt_name;
315 
316  /* Get main sequence tag */
317  if( ( ret = asn1_get_tag( p, end, &len,
318  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
319  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
320 
321  if( *p + len != end )
324 
325  while( *p < end )
326  {
327  if( ( end - *p ) < 1 )
330 
331  tag = **p;
332  (*p)++;
333  if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
334  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
335 
336  if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
339 
340  if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
341  {
342  *p += tag_len;
343  continue;
344  }
345 
346  buf = &(cur->buf);
347  buf->tag = tag;
348  buf->p = *p;
349  buf->len = tag_len;
350  *p += buf->len;
351 
352  /* Allocate and assign next pointer */
353  if (*p < end)
354  {
356  sizeof( asn1_sequence ) );
357 
358  if( cur->next == NULL )
361 
362  memset( cur->next, 0, sizeof( asn1_sequence ) );
363  cur = cur->next;
364  }
365  }
366 
367  /* Set final sequence entry's next pointer to NULL */
368  cur->next = NULL;
369 
370  if( *p != end )
373 
374  return( 0 );
375 }
376 
377 /*
378  * X.509 v3 extensions
379  *
380  * TODO: Perform all of the basic constraints tests required by the RFC
381  * TODO: Set values for undetected extensions to a sane default?
382  *
383  */
384 static int x509_get_crt_ext( unsigned char **p,
385  const unsigned char *end,
386  x509_crt *crt )
387 {
388  int ret;
389  size_t len;
390  unsigned char *end_ext_data, *end_ext_octet;
391 
392  if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
393  {
395  return( 0 );
396 
397  return( ret );
398  }
399 
400  while( *p < end )
401  {
402  /*
403  * Extension ::= SEQUENCE {
404  * extnID OBJECT IDENTIFIER,
405  * critical BOOLEAN DEFAULT FALSE,
406  * extnValue OCTET STRING }
407  */
408  x509_buf extn_oid = {0, 0, NULL};
409  int is_critical = 0; /* DEFAULT FALSE */
410  int ext_type = 0;
411 
412  if( ( ret = asn1_get_tag( p, end, &len,
413  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
414  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
415 
416  end_ext_data = *p + len;
417 
418  /* Get extension ID */
419  extn_oid.tag = **p;
420 
421  if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
422  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
423 
424  extn_oid.p = *p;
425  *p += extn_oid.len;
426 
427  if( ( end - *p ) < 1 )
430 
431  /* Get optional critical */
432  if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
434  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
435 
436  /* Data should be octet string type */
437  if( ( ret = asn1_get_tag( p, end_ext_data, &len,
438  ASN1_OCTET_STRING ) ) != 0 )
439  return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
440 
441  end_ext_octet = *p + len;
442 
443  if( end_ext_octet != end_ext_data )
446 
447  /*
448  * Detect supported extensions
449  */
450  ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
451 
452  if( ret != 0 )
453  {
454  /* No parser found, skip extension */
455  *p = end_ext_octet;
456 
457 #if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
458  if( is_critical )
459  {
460  /* Data is marked as critical: fail */
463  }
464 #endif
465  continue;
466  }
467 
468  crt->ext_types |= ext_type;
469 
470  switch( ext_type )
471  {
473  /* Parse basic constraints */
474  if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
475  &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
476  return ( ret );
477  break;
478 
479  case EXT_KEY_USAGE:
480  /* Parse key usage */
481  if( ( ret = x509_get_key_usage( p, end_ext_octet,
482  &crt->key_usage ) ) != 0 )
483  return ( ret );
484  break;
485 
487  /* Parse extended key usage */
488  if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
489  &crt->ext_key_usage ) ) != 0 )
490  return ( ret );
491  break;
492 
494  /* Parse subject alt name */
495  if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
496  &crt->subject_alt_names ) ) != 0 )
497  return ( ret );
498  break;
499 
500  case EXT_NS_CERT_TYPE:
501  /* Parse netscape certificate type */
502  if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
503  &crt->ns_cert_type ) ) != 0 )
504  return ( ret );
505  break;
506 
507  default:
509  }
510  }
511 
512  if( *p != end )
515 
516  return( 0 );
517 }
518 
519 /*
520  * Parse and fill a single X.509 certificate in DER format
521  */
522 static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
523  size_t buflen )
524 {
525  int ret;
526  size_t len;
527  unsigned char *p, *end, *crt_end;
528 
529  /*
530  * Check for valid input
531  */
532  if( crt == NULL || buf == NULL )
534 
535  p = (unsigned char *) polarssl_malloc( len = buflen );
536 
537  if( p == NULL )
539 
540  memcpy( p, buf, buflen );
541 
542  buflen = 0;
543 
544  crt->raw.p = p;
545  crt->raw.len = len;
546  end = p + len;
547 
548  /*
549  * Certificate ::= SEQUENCE {
550  * tbsCertificate TBSCertificate,
551  * signatureAlgorithm AlgorithmIdentifier,
552  * signatureValue BIT STRING }
553  */
554  if( ( ret = asn1_get_tag( &p, end, &len,
555  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
556  {
557  x509_crt_free( crt );
559  }
560 
561  if( len > (size_t) ( end - p ) )
562  {
563  x509_crt_free( crt );
566  }
567  crt_end = p + len;
568 
569  /*
570  * TBSCertificate ::= SEQUENCE {
571  */
572  crt->tbs.p = p;
573 
574  if( ( ret = asn1_get_tag( &p, end, &len,
575  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
576  {
577  x509_crt_free( crt );
578  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
579  }
580 
581  end = p + len;
582  crt->tbs.len = end - crt->tbs.p;
583 
584  /*
585  * Version ::= INTEGER { v1(0), v2(1), v3(2) }
586  *
587  * CertificateSerialNumber ::= INTEGER
588  *
589  * signature AlgorithmIdentifier
590  */
591  if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
592  ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
593  ( ret = x509_get_alg_null( &p, end, &crt->sig_oid1 ) ) != 0 )
594  {
595  x509_crt_free( crt );
596  return( ret );
597  }
598 
599  crt->version++;
600 
601  if( crt->version > 3 )
602  {
603  x509_crt_free( crt );
605  }
606 
607  if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &crt->sig_md,
608  &crt->sig_pk ) ) != 0 )
609  {
610  x509_crt_free( crt );
611  return( ret );
612  }
613 
614  /*
615  * issuer Name
616  */
617  crt->issuer_raw.p = p;
618 
619  if( ( ret = asn1_get_tag( &p, end, &len,
620  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
621  {
622  x509_crt_free( crt );
623  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
624  }
625 
626  if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
627  {
628  x509_crt_free( crt );
629  return( ret );
630  }
631 
632  crt->issuer_raw.len = p - crt->issuer_raw.p;
633 
634  /*
635  * Validity ::= SEQUENCE {
636  * notBefore Time,
637  * notAfter Time }
638  *
639  */
640  if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
641  &crt->valid_to ) ) != 0 )
642  {
643  x509_crt_free( crt );
644  return( ret );
645  }
646 
647  /*
648  * subject Name
649  */
650  crt->subject_raw.p = p;
651 
652  if( ( ret = asn1_get_tag( &p, end, &len,
653  ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
654  {
655  x509_crt_free( crt );
656  return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
657  }
658 
659  if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
660  {
661  x509_crt_free( crt );
662  return( ret );
663  }
664 
665  crt->subject_raw.len = p - crt->subject_raw.p;
666 
667  /*
668  * SubjectPublicKeyInfo
669  */
670  if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
671  {
672  x509_crt_free( crt );
673  return( ret );
674  }
675 
676  /*
677  * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
678  * -- If present, version shall be v2 or v3
679  * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
680  * -- If present, version shall be v2 or v3
681  * extensions [3] EXPLICIT Extensions OPTIONAL
682  * -- If present, version shall be v3
683  */
684  if( crt->version == 2 || crt->version == 3 )
685  {
686  ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
687  if( ret != 0 )
688  {
689  x509_crt_free( crt );
690  return( ret );
691  }
692  }
693 
694  if( crt->version == 2 || crt->version == 3 )
695  {
696  ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
697  if( ret != 0 )
698  {
699  x509_crt_free( crt );
700  return( ret );
701  }
702  }
703 
704 #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
705  if( crt->version == 3 )
706  {
707 #endif
708  ret = x509_get_crt_ext( &p, end, crt);
709  if( ret != 0 )
710  {
711  x509_crt_free( crt );
712  return( ret );
713  }
714 #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
715  }
716 #endif
717 
718  if( p != end )
719  {
720  x509_crt_free( crt );
723  }
724 
725  end = crt_end;
726 
727  /*
728  * }
729  * -- end of TBSCertificate
730  *
731  * signatureAlgorithm AlgorithmIdentifier,
732  * signatureValue BIT STRING
733  */
734  if( ( ret = x509_get_alg_null( &p, end, &crt->sig_oid2 ) ) != 0 )
735  {
736  x509_crt_free( crt );
737  return( ret );
738  }
739 
740  if( crt->sig_oid1.len != crt->sig_oid2.len ||
741  memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 )
742  {
743  x509_crt_free( crt );
745  }
746 
747  if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
748  {
749  x509_crt_free( crt );
750  return( ret );
751  }
752 
753  if( p != end )
754  {
755  x509_crt_free( crt );
758  }
759 
760  return( 0 );
761 }
762 
763 /*
764  * Parse one X.509 certificate in DER format from a buffer and add them to a
765  * chained list
766  */
767 int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
768  size_t buflen )
769 {
770  int ret;
771  x509_crt *crt = chain, *prev = NULL;
772 
773  /*
774  * Check for valid input
775  */
776  if( crt == NULL || buf == NULL )
778 
779  while( crt->version != 0 && crt->next != NULL )
780  {
781  prev = crt;
782  crt = crt->next;
783  }
784 
785  /*
786  * Add new certificate on the end of the chain if needed.
787  */
788  if ( crt->version != 0 && crt->next == NULL)
789  {
790  crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
791 
792  if( crt->next == NULL )
794 
795  prev = crt;
796  crt = crt->next;
797  x509_crt_init( crt );
798  }
799 
800  if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
801  {
802  if( prev )
803  prev->next = NULL;
804 
805  if( crt != chain )
806  polarssl_free( crt );
807 
808  return( ret );
809  }
810 
811  return( 0 );
812 }
813 
814 /*
815  * Parse one or more PEM certificates from a buffer and add them to the chained list
816  */
817 int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
818 {
819  int success = 0, first_error = 0, total_failed = 0;
820  int buf_format = X509_FORMAT_DER;
821 
822  /*
823  * Check for valid input
824  */
825  if( chain == NULL || buf == NULL )
827 
828  /*
829  * Determine buffer content. Buffer contains either one DER certificate or
830  * one or more PEM certificates.
831  */
832 #if defined(POLARSSL_PEM_PARSE_C)
833  if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
834  buf_format = X509_FORMAT_PEM;
835 #endif
836 
837  if( buf_format == X509_FORMAT_DER )
838  return x509_crt_parse_der( chain, buf, buflen );
839 
840 #if defined(POLARSSL_PEM_PARSE_C)
841  if( buf_format == X509_FORMAT_PEM )
842  {
843  int ret;
844  pem_context pem;
845 
846  while( buflen > 0 )
847  {
848  size_t use_len;
849  pem_init( &pem );
850 
851  ret = pem_read_buffer( &pem,
852  "-----BEGIN CERTIFICATE-----",
853  "-----END CERTIFICATE-----",
854  buf, NULL, 0, &use_len );
855 
856  if( ret == 0 )
857  {
858  /*
859  * Was PEM encoded
860  */
861  buflen -= use_len;
862  buf += use_len;
863  }
864  else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
865  {
866  return( ret );
867  }
869  {
870  pem_free( &pem );
871 
872  /*
873  * PEM header and footer were found
874  */
875  buflen -= use_len;
876  buf += use_len;
877 
878  if( first_error == 0 )
879  first_error = ret;
880 
881  continue;
882  }
883  else
884  break;
885 
886  ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
887 
888  pem_free( &pem );
889 
890  if( ret != 0 )
891  {
892  /*
893  * Quit parsing on a memory error
894  */
896  return( ret );
897 
898  if( first_error == 0 )
899  first_error = ret;
900 
901  total_failed++;
902  continue;
903  }
904 
905  success = 1;
906  }
907  }
908 #endif
909 
910  if( success )
911  return( total_failed );
912  else if( first_error )
913  return( first_error );
914  else
916 }
917 
918 #if defined(POLARSSL_FS_IO)
919 /*
920  * Load one or more certificates and add them to the chained list
921  */
922 int x509_crt_parse_file( x509_crt *chain, const char *path )
923 {
924  int ret;
925  size_t n;
926  unsigned char *buf;
927 
928  if ( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
929  return( ret );
930 
931  ret = x509_crt_parse( chain, buf, n );
932 
933  memset( buf, 0, n + 1 );
934  polarssl_free( buf );
935 
936  return( ret );
937 }
938 
939 int x509_crt_parse_path( x509_crt *chain, const char *path )
940 {
941  int ret = 0;
942 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
943  int w_ret;
944  WCHAR szDir[MAX_PATH];
945  char filename[MAX_PATH];
946  char *p;
947  int len = (int) strlen( path );
948 
949  WIN32_FIND_DATAW file_data;
950  HANDLE hFind;
951 
952  if( len > MAX_PATH - 3 )
954 
955  memset( szDir, 0, sizeof(szDir) );
956  memset( filename, 0, MAX_PATH );
957  memcpy( filename, path, len );
958  filename[len++] = '\\';
959  p = filename + len;
960  filename[len++] = '*';
961 
962  w_ret = MultiByteToWideChar( CP_ACP, 0, path, len, szDir, MAX_PATH - 3 );
963 
964  hFind = FindFirstFileW( szDir, &file_data );
965  if (hFind == INVALID_HANDLE_VALUE)
967 
968  len = MAX_PATH - len;
969  do
970  {
971  memset( p, 0, len );
972 
973  if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
974  continue;
975 
976  w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
977  lstrlenW(file_data.cFileName),
978  p, len - 1,
979  NULL, NULL );
980 
981  w_ret = x509_crt_parse_file( chain, filename );
982  if( w_ret < 0 )
983  ret++;
984  else
985  ret += w_ret;
986  }
987  while( FindNextFileW( hFind, &file_data ) != 0 );
988 
989  if (GetLastError() != ERROR_NO_MORE_FILES)
991 
992  FindClose( hFind );
993 #else /* _WIN32 */
994 #if defined(POLARSSL_HAVE_READDIR_R)
995  int t_ret, i;
996  struct stat sb;
997  struct dirent entry, *result = NULL;
998  char entry_name[255];
999  DIR *dir = opendir( path );
1000 
1001  if( dir == NULL)
1003 
1004  while( ( t_ret = readdir_r( dir, &entry, &result ) ) == 0 )
1005  {
1006  if( result == NULL )
1007  break;
1008 
1009  snprintf( entry_name, sizeof(entry_name), "%s/%s", path, entry.d_name );
1010 
1011  i = stat( entry_name, &sb );
1012 
1013  if( i == -1 )
1014  {
1015  closedir( dir );
1017  }
1018 
1019  if( !S_ISREG( sb.st_mode ) )
1020  continue;
1021 
1022  // Ignore parse errors
1023  //
1024  t_ret = x509_crt_parse_file( chain, entry_name );
1025  if( t_ret < 0 )
1026  ret++;
1027  else
1028  ret += t_ret;
1029  }
1030  closedir( dir );
1031 #else /* POLARSSL_HAVE_READDIR_R */
1032  ((void) chain);
1033  ((void) path);
1035 #endif /* POLARSSL_HAVE_READDIR_R */
1036 #endif /* _WIN32 */
1037 
1038  return( ret );
1039 }
1040 #endif /* POLARSSL_FS_IO */
1041 
1042 #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
1043  !defined(EFI32)
1044 #include <stdarg.h>
1045 
1046 #if !defined vsnprintf
1047 #define vsnprintf _vsnprintf
1048 #endif // vsnprintf
1049 
1050 /*
1051  * Windows _snprintf and _vsnprintf are not compatible to linux versions.
1052  * Result value is not size of buffer needed, but -1 if no fit is possible.
1053  *
1054  * This fuction tries to 'fix' this by at least suggesting enlarging the
1055  * size by 20.
1056  */
1057 static int compat_snprintf(char *str, size_t size, const char *format, ...)
1058 {
1059  va_list ap;
1060  int res = -1;
1061 
1062  va_start( ap, format );
1063 
1064  res = vsnprintf( str, size, format, ap );
1065 
1066  va_end( ap );
1067 
1068  // No quick fix possible
1069  if ( res < 0 )
1070  return( (int) size + 20 );
1071 
1072  return res;
1073 }
1074 
1075 #define snprintf compat_snprintf
1076 #endif
1077 
1078 #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
1079 
1080 #define SAFE_SNPRINTF() \
1081 { \
1082  if( ret == -1 ) \
1083  return( -1 ); \
1084  \
1085  if ( (unsigned int) ret > n ) { \
1086  p[n - 1] = '\0'; \
1087  return POLARSSL_ERR_DEBUG_BUF_TOO_SMALL;\
1088  } \
1089  \
1090  n -= (unsigned int) ret; \
1091  p += (unsigned int) ret; \
1092 }
1093 
1094 /*
1095  * Return an informational string about the certificate.
1096  */
1097 #define BEFORE_COLON 14
1098 #define BC "14"
1099 int x509_crt_info( char *buf, size_t size, const char *prefix,
1100  const x509_crt *crt )
1101 {
1102  int ret;
1103  size_t n;
1104  char *p;
1105  const char *desc = NULL;
1106  char key_size_str[BEFORE_COLON];
1107 
1108  p = buf;
1109  n = size;
1110 
1111  ret = snprintf( p, n, "%scert. version : %d\n",
1112  prefix, crt->version );
1113  SAFE_SNPRINTF();
1114  ret = snprintf( p, n, "%sserial number : ",
1115  prefix );
1116  SAFE_SNPRINTF();
1117 
1118  ret = x509_serial_gets( p, n, &crt->serial);
1119  SAFE_SNPRINTF();
1120 
1121  ret = snprintf( p, n, "\n%sissuer name : ", prefix );
1122  SAFE_SNPRINTF();
1123  ret = x509_dn_gets( p, n, &crt->issuer );
1124  SAFE_SNPRINTF();
1125 
1126  ret = snprintf( p, n, "\n%ssubject name : ", prefix );
1127  SAFE_SNPRINTF();
1128  ret = x509_dn_gets( p, n, &crt->subject );
1129  SAFE_SNPRINTF();
1130 
1131  ret = snprintf( p, n, "\n%sissued on : " \
1132  "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1133  crt->valid_from.year, crt->valid_from.mon,
1134  crt->valid_from.day, crt->valid_from.hour,
1135  crt->valid_from.min, crt->valid_from.sec );
1136  SAFE_SNPRINTF();
1137 
1138  ret = snprintf( p, n, "\n%sexpires on : " \
1139  "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1140  crt->valid_to.year, crt->valid_to.mon,
1141  crt->valid_to.day, crt->valid_to.hour,
1142  crt->valid_to.min, crt->valid_to.sec );
1143  SAFE_SNPRINTF();
1144 
1145  ret = snprintf( p, n, "\n%ssigned using : ", prefix );
1146  SAFE_SNPRINTF();
1147 
1148  ret = oid_get_sig_alg_desc( &crt->sig_oid1, &desc );
1149  if( ret != 0 )
1150  ret = snprintf( p, n, "???" );
1151  else
1152  ret = snprintf( p, n, "%s", desc );
1153  SAFE_SNPRINTF();
1154 
1155  if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
1156  pk_get_name( &crt->pk ) ) ) != 0 )
1157  {
1158  return( ret );
1159  }
1160 
1161  ret = snprintf( p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
1162  (int) pk_get_size( &crt->pk ) );
1163  SAFE_SNPRINTF();
1164 
1165  return( (int) ( size - n ) );
1166 }
1167 
1168 #if defined(POLARSSL_X509_CRL_PARSE_C)
1169 /*
1170  * Return 1 if the certificate is revoked, or 0 otherwise.
1171  */
1172 int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
1173 {
1174  const x509_crl_entry *cur = &crl->entry;
1175 
1176  while( cur != NULL && cur->serial.len != 0 )
1177  {
1178  if( crt->serial.len == cur->serial.len &&
1179  memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
1180  {
1181  if( x509_time_expired( &cur->revocation_date ) )
1182  return( 1 );
1183  }
1184 
1185  cur = cur->next;
1186  }
1187 
1188  return( 0 );
1189 }
1190 
1191 /*
1192  * Check that the given certificate is valid according to the CRL.
1193  */
1194 static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
1195  x509_crl *crl_list)
1196 {
1197  int flags = 0;
1198  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1199  const md_info_t *md_info;
1200 
1201  if( ca == NULL )
1202  return( flags );
1203 
1204  /*
1205  * TODO: What happens if no CRL is present?
1206  * Suggestion: Revocation state should be unknown if no CRL is present.
1207  * For backwards compatibility this is not yet implemented.
1208  */
1209 
1210  while( crl_list != NULL )
1211  {
1212  if( crl_list->version == 0 ||
1213  crl_list->issuer_raw.len != ca->subject_raw.len ||
1214  memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
1215  crl_list->issuer_raw.len ) != 0 )
1216  {
1217  crl_list = crl_list->next;
1218  continue;
1219  }
1220 
1221  /*
1222  * Check if CRL is correctly signed by the trusted CA
1223  */
1224  md_info = md_info_from_type( crl_list->sig_md );
1225  if( md_info == NULL )
1226  {
1227  /*
1228  * Cannot check 'unknown' hash
1229  */
1230  flags |= BADCRL_NOT_TRUSTED;
1231  break;
1232  }
1233 
1234  md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
1235 
1236  if( pk_can_do( &ca->pk, crl_list->sig_pk ) == 0 ||
1237  pk_verify( &ca->pk, crl_list->sig_md, hash, md_info->size,
1238  crl_list->sig.p, crl_list->sig.len ) != 0 )
1239  {
1240  flags |= BADCRL_NOT_TRUSTED;
1241  break;
1242  }
1243 
1244  /*
1245  * Check for validity of CRL (Do not drop out)
1246  */
1247  if( x509_time_expired( &crl_list->next_update ) )
1248  flags |= BADCRL_EXPIRED;
1249 
1250  /*
1251  * Check if certificate is revoked
1252  */
1253  if( x509_crt_revoked(crt, crl_list) )
1254  {
1255  flags |= BADCERT_REVOKED;
1256  break;
1257  }
1258 
1259  crl_list = crl_list->next;
1260  }
1261  return flags;
1262 }
1263 #endif /* POLARSSL_X509_CRL_PARSE_C */
1264 
1265 // Equal == 0, inequal == 1
1266 static int x509_name_cmp( const void *s1, const void *s2, size_t len )
1267 {
1268  size_t i;
1269  unsigned char diff;
1270  const unsigned char *n1 = s1, *n2 = s2;
1271 
1272  for( i = 0; i < len; i++ )
1273  {
1274  diff = n1[i] ^ n2[i];
1275 
1276  if( ( n1[i] >= 'a' || n1[i] <= 'z' ) && ( diff == 0 || diff == 32 ) )
1277  continue;
1278 
1279  if( ( n1[i] >= 'A' || n1[i] <= 'Z' ) && ( diff == 0 || diff == 32 ) )
1280  continue;
1281 
1282  return( 1 );
1283  }
1284 
1285  return( 0 );
1286 }
1287 
1288 static int x509_wildcard_verify( const char *cn, x509_buf *name )
1289 {
1290  size_t i;
1291  size_t cn_idx = 0;
1292 
1293  if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
1294  return( 0 );
1295 
1296  for( i = 0; i < strlen( cn ); ++i )
1297  {
1298  if( cn[i] == '.' )
1299  {
1300  cn_idx = i;
1301  break;
1302  }
1303  }
1304 
1305  if( cn_idx == 0 )
1306  return( 0 );
1307 
1308  if( strlen( cn ) - cn_idx == name->len - 1 &&
1309  x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
1310  {
1311  return( 1 );
1312  }
1313 
1314  return( 0 );
1315 }
1316 
1317 static int x509_crt_verify_top(
1318  x509_crt *child, x509_crt *trust_ca,
1319  x509_crl *ca_crl, int path_cnt, int *flags,
1320  int (*f_vrfy)(void *, x509_crt *, int, int *),
1321  void *p_vrfy )
1322 {
1323  int ret;
1324  int ca_flags = 0, check_path_cnt = path_cnt + 1;
1325  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1326  const md_info_t *md_info;
1327 
1328  if( x509_time_expired( &child->valid_to ) )
1329  *flags |= BADCERT_EXPIRED;
1330 
1331  /*
1332  * Child is the top of the chain. Check against the trust_ca list.
1333  */
1334  *flags |= BADCERT_NOT_TRUSTED;
1335 
1336  md_info = md_info_from_type( child->sig_md );
1337  if( md_info == NULL )
1338  {
1339  /*
1340  * Cannot check 'unknown', no need to try any CA
1341  */
1342  trust_ca = NULL;
1343  }
1344  else
1345  md( md_info, child->tbs.p, child->tbs.len, hash );
1346 
1347  while( trust_ca != NULL )
1348  {
1349  if( trust_ca->version == 0 ||
1350  child->issuer_raw.len != trust_ca->subject_raw.len ||
1351  memcmp( child->issuer_raw.p, trust_ca->subject_raw.p,
1352  child->issuer_raw.len ) != 0 )
1353  {
1354  trust_ca = trust_ca->next;
1355  continue;
1356  }
1357 
1358  /*
1359  * Reduce path_len to check against if top of the chain is
1360  * the same as the trusted CA
1361  */
1362  if( child->subject_raw.len == trust_ca->subject_raw.len &&
1363  memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1364  child->issuer_raw.len ) == 0 )
1365  {
1366  check_path_cnt--;
1367  }
1368 
1369  if( trust_ca->max_pathlen > 0 &&
1370  trust_ca->max_pathlen < check_path_cnt )
1371  {
1372  trust_ca = trust_ca->next;
1373  continue;
1374  }
1375 
1376  if( pk_can_do( &trust_ca->pk, child->sig_pk ) == 0 ||
1377  pk_verify( &trust_ca->pk, child->sig_md, hash, md_info->size,
1378  child->sig.p, child->sig.len ) != 0 )
1379  {
1380  trust_ca = trust_ca->next;
1381  continue;
1382  }
1383 
1384  /*
1385  * Top of chain is signed by a trusted CA
1386  */
1387  *flags &= ~BADCERT_NOT_TRUSTED;
1388  break;
1389  }
1390 
1391  /*
1392  * If top of chain is not the same as the trusted CA send a verify request
1393  * to the callback for any issues with validity and CRL presence for the
1394  * trusted CA certificate.
1395  */
1396  if( trust_ca != NULL &&
1397  ( child->subject_raw.len != trust_ca->subject_raw.len ||
1398  memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
1399  child->issuer_raw.len ) != 0 ) )
1400  {
1401 #if defined(POLARSSL_X509_CRL_PARSE_C)
1402  /* Check trusted CA's CRL for the chain's top crt */
1403  *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
1404 #else
1405  ((void) ca_crl);
1406 #endif
1407 
1408  if( x509_time_expired( &trust_ca->valid_to ) )
1409  ca_flags |= BADCERT_EXPIRED;
1410 
1411  if( NULL != f_vrfy )
1412  {
1413  if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1, &ca_flags ) ) != 0 )
1414  return( ret );
1415  }
1416  }
1417 
1418  /* Call callback on top cert */
1419  if( NULL != f_vrfy )
1420  {
1421  if( ( ret = f_vrfy(p_vrfy, child, path_cnt, flags ) ) != 0 )
1422  return( ret );
1423  }
1424 
1425  *flags |= ca_flags;
1426 
1427  return( 0 );
1428 }
1429 
1430 static int x509_crt_verify_child(
1431  x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
1432  x509_crl *ca_crl, int path_cnt, int *flags,
1433  int (*f_vrfy)(void *, x509_crt *, int, int *),
1434  void *p_vrfy )
1435 {
1436  int ret;
1437  int parent_flags = 0;
1438  unsigned char hash[POLARSSL_MD_MAX_SIZE];
1439  x509_crt *grandparent;
1440  const md_info_t *md_info;
1441 
1442  if( x509_time_expired( &child->valid_to ) )
1443  *flags |= BADCERT_EXPIRED;
1444 
1445  md_info = md_info_from_type( child->sig_md );
1446  if( md_info == NULL )
1447  {
1448  /*
1449  * Cannot check 'unknown' hash
1450  */
1451  *flags |= BADCERT_NOT_TRUSTED;
1452  }
1453  else
1454  {
1455  md( md_info, child->tbs.p, child->tbs.len, hash );
1456 
1457  if( pk_can_do( &parent->pk, child->sig_pk ) == 0 ||
1458  pk_verify( &parent->pk, child->sig_md, hash, md_info->size,
1459  child->sig.p, child->sig.len ) != 0 )
1460  {
1461  *flags |= BADCERT_NOT_TRUSTED;
1462  }
1463  }
1464 
1465 #if defined(POLARSSL_X509_CRL_PARSE_C)
1466  /* Check trusted CA's CRL for the given crt */
1467  *flags |= x509_crt_verifycrl(child, parent, ca_crl);
1468 #endif
1469 
1470  grandparent = parent->next;
1471 
1472  while( grandparent != NULL )
1473  {
1474  if( grandparent->version == 0 ||
1475  grandparent->ca_istrue == 0 ||
1476  parent->issuer_raw.len != grandparent->subject_raw.len ||
1477  memcmp( parent->issuer_raw.p, grandparent->subject_raw.p,
1478  parent->issuer_raw.len ) != 0 )
1479  {
1480  grandparent = grandparent->next;
1481  continue;
1482  }
1483  break;
1484  }
1485 
1486  if( grandparent != NULL )
1487  {
1488  /*
1489  * Part of the chain
1490  */
1491  ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1492  if( ret != 0 )
1493  return( ret );
1494  }
1495  else
1496  {
1497  ret = x509_crt_verify_top( parent, trust_ca, ca_crl, path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
1498  if( ret != 0 )
1499  return( ret );
1500  }
1501 
1502  /* child is verified to be a child of the parent, call verify callback */
1503  if( NULL != f_vrfy )
1504  if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
1505  return( ret );
1506 
1507  *flags |= parent_flags;
1508 
1509  return( 0 );
1510 }
1511 
1512 /*
1513  * Verify the certificate validity
1514  */
1515 int x509_crt_verify( x509_crt *crt,
1516  x509_crt *trust_ca,
1517  x509_crl *ca_crl,
1518  const char *cn, int *flags,
1519  int (*f_vrfy)(void *, x509_crt *, int, int *),
1520  void *p_vrfy )
1521 {
1522  size_t cn_len;
1523  int ret;
1524  int pathlen = 0;
1525  x509_crt *parent;
1526  x509_name *name;
1527  x509_sequence *cur = NULL;
1528 
1529  *flags = 0;
1530 
1531  if( cn != NULL )
1532  {
1533  name = &crt->subject;
1534  cn_len = strlen( cn );
1535 
1536  if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
1537  {
1538  cur = &crt->subject_alt_names;
1539 
1540  while( cur != NULL )
1541  {
1542  if( cur->buf.len == cn_len &&
1543  x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
1544  break;
1545 
1546  if( cur->buf.len > 2 &&
1547  memcmp( cur->buf.p, "*.", 2 ) == 0 &&
1548  x509_wildcard_verify( cn, &cur->buf ) )
1549  break;
1550 
1551  cur = cur->next;
1552  }
1553 
1554  if( cur == NULL )
1555  *flags |= BADCERT_CN_MISMATCH;
1556  }
1557  else
1558  {
1559  while( name != NULL )
1560  {
1561  if( OID_CMP( OID_AT_CN, &name->oid ) )
1562  {
1563  if( name->val.len == cn_len &&
1564  x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
1565  break;
1566 
1567  if( name->val.len > 2 &&
1568  memcmp( name->val.p, "*.", 2 ) == 0 &&
1569  x509_wildcard_verify( cn, &name->val ) )
1570  break;
1571  }
1572 
1573  name = name->next;
1574  }
1575 
1576  if( name == NULL )
1577  *flags |= BADCERT_CN_MISMATCH;
1578  }
1579  }
1580 
1581  /*
1582  * Iterate upwards in the given cert chain, to find our crt parent.
1583  * Ignore any upper cert with CA != TRUE.
1584  */
1585  parent = crt->next;
1586 
1587  while( parent != NULL && parent->version != 0 )
1588  {
1589  if( parent->ca_istrue == 0 ||
1590  crt->issuer_raw.len != parent->subject_raw.len ||
1591  memcmp( crt->issuer_raw.p, parent->subject_raw.p,
1592  crt->issuer_raw.len ) != 0 )
1593  {
1594  parent = parent->next;
1595  continue;
1596  }
1597  break;
1598  }
1599 
1600  if( parent != NULL )
1601  {
1602  /*
1603  * Part of the chain
1604  */
1605  ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
1606  if( ret != 0 )
1607  return( ret );
1608  }
1609  else
1610  {
1611  ret = x509_crt_verify_top( crt, trust_ca, ca_crl, pathlen, flags, f_vrfy, p_vrfy );
1612  if( ret != 0 )
1613  return( ret );
1614  }
1615 
1616  if( *flags != 0 )
1618 
1619  return( 0 );
1620 }
1621 
1622 /*
1623  * Initialize a certificate chain
1624  */
1625 void x509_crt_init( x509_crt *crt )
1626 {
1627  memset( crt, 0, sizeof(x509_crt) );
1628 }
1629 
1630 /*
1631  * Unallocate all certificate data
1632  */
1633 void x509_crt_free( x509_crt *crt )
1634 {
1635  x509_crt *cert_cur = crt;
1636  x509_crt *cert_prv;
1637  x509_name *name_cur;
1638  x509_name *name_prv;
1639  x509_sequence *seq_cur;
1640  x509_sequence *seq_prv;
1641 
1642  if( crt == NULL )
1643  return;
1644 
1645  do
1646  {
1647  pk_free( &cert_cur->pk );
1648 
1649  name_cur = cert_cur->issuer.next;
1650  while( name_cur != NULL )
1651  {
1652  name_prv = name_cur;
1653  name_cur = name_cur->next;
1654  memset( name_prv, 0, sizeof( x509_name ) );
1655  polarssl_free( name_prv );
1656  }
1657 
1658  name_cur = cert_cur->subject.next;
1659  while( name_cur != NULL )
1660  {
1661  name_prv = name_cur;
1662  name_cur = name_cur->next;
1663  memset( name_prv, 0, sizeof( x509_name ) );
1664  polarssl_free( name_prv );
1665  }
1666 
1667  seq_cur = cert_cur->ext_key_usage.next;
1668  while( seq_cur != NULL )
1669  {
1670  seq_prv = seq_cur;
1671  seq_cur = seq_cur->next;
1672  memset( seq_prv, 0, sizeof( x509_sequence ) );
1673  polarssl_free( seq_prv );
1674  }
1675 
1676  seq_cur = cert_cur->subject_alt_names.next;
1677  while( seq_cur != NULL )
1678  {
1679  seq_prv = seq_cur;
1680  seq_cur = seq_cur->next;
1681  memset( seq_prv, 0, sizeof( x509_sequence ) );
1682  polarssl_free( seq_prv );
1683  }
1684 
1685  if( cert_cur->raw.p != NULL )
1686  {
1687  memset( cert_cur->raw.p, 0, cert_cur->raw.len );
1688  polarssl_free( cert_cur->raw.p );
1689  }
1690 
1691  cert_cur = cert_cur->next;
1692  }
1693  while( cert_cur != NULL );
1694 
1695  cert_cur = crt;
1696  do
1697  {
1698  cert_prv = cert_cur;
1699  cert_cur = cert_cur->next;
1700 
1701  memset( cert_prv, 0, sizeof( x509_crt ) );
1702  if( cert_prv != crt )
1703  polarssl_free( cert_prv );
1704  }
1705  while( cert_cur != NULL );
1706 }
1707 
1708 #endif
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:89
int x509_time_expired(const x509_time *time)
Check a given x509_time against the system time and check if it is valid.
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:76
#define X509_FORMAT_DER
Definition: x509.h:134
Memory allocation layer.
#define ASN1_OID
Definition: asn1.h:76
#define EXT_KEY_USAGE
Definition: x509.h:114
void *(* polarssl_malloc)(size_t len)
int sec
Time.
Definition: x509.h:175
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:55
int version
Definition: x509_crl.h:74
asn1_buf buf
Buffer containing the given ASN.1 item.
Definition: asn1.h:140
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:55
x509_time next_update
Definition: x509_crl.h:82
#define POLARSSL_ERR_ASN1_LENGTH_MISMATCH
Actual length differs from expected length.
Definition: asn1.h:53
int ext_types
Bit string containing detected and parsed extensions.
Definition: x509_crt.h:78
Certificate revocation list entry.
Definition: x509_crl.h:51
size_t pk_get_size(const pk_context *ctx)
Get the size in bits of the underlying key.
#define POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT
Format not recognized as DER or PEM.
Definition: x509.h:62
#define EXT_BASIC_CONSTRAINTS
Definition: x509.h:120
unsigned char ns_cert_type
Optional Netscape certificate type extension value: See the values below.
Definition: x509_crt.h:86
x509_buf issuer_raw
The raw issuer data (DER).
Definition: x509_crt.h:62
#define POLARSSL_ERR_X509_INVALID_FORMAT
The CRT/CRL/CSR format is invalid, e.g.
Definition: x509.h:50
int x509_key_size_helper(char *buf, size_t size, const char *name)
struct _x509_crl * next
Definition: x509_crl.h:93
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:129
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:172
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:78
x509_buf sig_oid2
Signature algorithm.
Definition: x509_crt.h:88
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:100
x509_buf tbs
The raw certificate body (DER).
Definition: x509_crt.h:56
x509_buf serial
Unique id for certificate issued by a specific CA.
Definition: x509_crt.h:59
md_type_t sig_md
Internal representation of the MD algorithm of the signature algorithm, e.g.
Definition: x509_crt.h:90
int ca_istrue
Optional Basic Constraint extension value: 1 if this certificate belongs to a CA, 0 otherwise...
Definition: x509_crt.h:79
#define ASN1_CONSTRUCTED
Definition: asn1.h:88
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.
int max_pathlen
Optional Basic Constraint extension value: The maximum path length to the root certificate.
Definition: x509_crt.h:80
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:76
#define POLARSSL_ERR_ASN1_INVALID_LENGTH
Error when trying to determine the length or invalid length.
Definition: asn1.h:52
Container for ASN1 bit strings.
Definition: asn1.h:127
#define POLARSSL_ERR_X509_UNKNOWN_VERSION
CRT/CRL/CSR has an unsupported version number.
Definition: x509.h:58
Object Identifier (OID) database.
x509_buf serial
Definition: x509_crl.h:55
#define OID_AT_CN
id-at-commonName AttributeType:= {id-at 3}
Definition: oid.h:106
struct _x509_crt * next
Next certificate in the CA-chain.
Definition: x509_crt.h:93
x509_crl_entry entry
The CRL entries containing the certificate revocation times for this CA.
Definition: x509_crl.h:84
asn1_buf val
The named value.
Definition: asn1.h:151
int hour
Definition: x509.h:175
int mon
Definition: x509.h:174
Container for a sequence of ASN.1 items.
Definition: asn1.h:138
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:72
unsigned char * p
Raw ASN1 data for the bit string.
Definition: asn1.h:131
#define POLARSSL_ERR_X509_CERT_VERIFY_FAILED
Certificate verification failed, e.g.
Definition: x509.h:61
Container for an X.509 certificate.
Definition: x509_crt.h:53
Privacy Enhanced Mail (PEM) decoding.
x509_time valid_from
Start time of certificate validity.
Definition: x509_crt.h:68
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:150
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:84
void x509_crt_init(x509_crt *crt)
Initialize a certificate (chain)
void(* polarssl_free)(void *ptr)
unsigned char * p
ASN1 data, e.g.
Definition: asn1.h:120
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:66
#define EXT_NS_CERT_TYPE
Definition: x509.h:128
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:72
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:69
struct _x509_crl_entry * next
Definition: x509_crl.h:61
md_type_t sig_md
Internal representation of the MD algorithm of the signature algorithm, e.g.
Definition: x509_crl.h:90
X.509 certificate parsing and writing.
int day
Date.
Definition: x509.h:174
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:60
int tag
ASN1 type, e.g.
Definition: asn1.h:118
#define POLARSSL_ERR_ASN1_OUT_OF_DATA
Out of data when parsing an ASN1 data structure.
Definition: asn1.h:50
#define EXT_EXTENDED_KEY_USAGE
Definition: x509.h:123
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:55
#define ASN1_CONTEXT_SPECIFIC
Definition: asn1.h:89
#define BADCERT_NOT_TRUSTED
The certificate is not correctly signed by the trusted CA.
Definition: x509.h:75
#define POLARSSL_ERR_X509_FILE_IO_ERROR
Read/write of file failed.
Definition: x509.h:65
int x509_crt_revoked(const x509_crt *crt, const x509_crl *crl)
Verify the certificate revocation status.
Container for a sequence or list of &#39;named&#39; ASN.1 data items.
Definition: asn1.h:148
Type-length-value structure that allows for ASN1 using DER.
Definition: asn1.h:116
pk_type_t sig_pk
&lt; Internal representation of the Public Key algorithm of the signature algorithm, e...
Definition: x509_crl.h:91
size_t len
ASN1 length, e.g.
Definition: asn1.h:119
x509_name issuer
The parsed issuer data (named information object).
Definition: x509_crt.h:65
#define X509_FORMAT_PEM
Definition: x509.h:135
void pk_free(pk_context *ctx)
Free a pk_context.
#define POLARSSL_MD_MAX_SIZE
Definition: md.h:66
int year
Definition: x509.h:174
#define BADCERT_REVOKED
The certificate has been revoked (is on a CRL).
Definition: x509.h:73
#define BADCRL_EXPIRED
CRL is expired.
Definition: x509.h:77
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:48
int asn1_get_tag(unsigned char **p, const unsigned char *end, size_t *len, int tag)
Get the tag and length of the tag.
int x509_get_ext(unsigned char **p, const unsigned char *end, x509_buf *ext, int tag)
#define POLARSSL_ERR_X509_INVALID_VERSION
The CRT/CRL/CSR version element is invalid.
Definition: x509.h:51
Certificate revocation list structure.
Definition: x509_crl.h:69
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:71
int min
Definition: x509.h:175
struct _asn1_named_data * next
The next entry in the sequence.
Definition: asn1.h:152
int size
Output length of the digest function.
Definition: md.h:81
x509_buf issuer_id
Optional X.509 v2/v3 issuer unique identifier.
Definition: x509_crt.h:73
#define POLARSSL_ERR_X509_INVALID_EXTENSIONS
The extension tag or value is invalid.
Definition: x509.h:57
#define ASN1_OCTET_STRING
Definition: asn1.h:74
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:75
#define POLARSSL_ERR_X509_BAD_INPUT_DATA
Input invalid.
Definition: x509.h:63
x509_buf subject_id
Optional X.509 v2/v3 subject unique identifier.
Definition: x509_crt.h:74
#define BADCERT_CN_MISMATCH
The certificate Common Name (CN) does not match with the expected CN.
Definition: x509.h:74
int version
The X.509 version.
Definition: x509_crt.h:58
#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:91
x509_time revocation_date
Definition: x509_crl.h:57
x509_buf issuer_raw
The raw issuer data (DER).
Definition: x509_crl.h:77
int x509_crt_info(char *buf, size_t size, const char *prefix, const x509_crt *crt)
Returns an informational string about the certificate.
#define POLARSSL_ERR_X509_MALLOC_FAILED
Allocation of memory failed.
Definition: x509.h:64
#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 below.
Definition: x509_crt.h:82
x509_buf subject_raw
The raw subject data (DER).
Definition: x509_crt.h:63
Message digest information.
Definition: md.h:73
#define POLARSSL_ERR_ASN1_UNEXPECTED_TAG
ASN1 tag was of an unexpected value.
Definition: asn1.h:51
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:60
struct _asn1_sequence * next
The next entry in the sequence.
Definition: asn1.h:141
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:89
#define EXT_SUBJECT_ALT_NAME
Definition: x509.h:117