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