2 * X.509 certificate parsing and verification
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 * The ITU-T X.509 standard defines a certificate format for PKI.
24 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
25 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
26 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
28 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
29 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
32 #if !defined(MBEDTLS_CONFIG_FILE)
33 #include "mbedtls/config.h"
35 #include MBEDTLS_CONFIG_FILE
38 #if defined(MBEDTLS_X509_CRT_PARSE_C)
40 #include "mbedtls/x509_crt.h"
41 #include "mbedtls/oid.h"
46 #if defined(MBEDTLS_PEM_PARSE_C)
47 #include "mbedtls/pem.h"
50 #if defined(MBEDTLS_PLATFORM_C)
51 #include "mbedtls/platform.h"
54 #define mbedtls_free free
55 #define mbedtls_calloc calloc
56 #define mbedtls_snprintf snprintf
59 #if defined(MBEDTLS_THREADING_C)
60 #include "mbedtls/threading.h"
63 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
69 #if defined(MBEDTLS_FS_IO)
71 #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
72 #include <sys/types.h>
75 #endif /* !_WIN32 || EFIX64 || EFI32 */
78 /* Implementation that should never be optimized out by the compiler */
79 static void mbedtls_zeroize( void *v
, size_t n
) {
80 volatile unsigned char *p
= v
; while( n
-- ) *p
++ = 0;
86 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default
=
88 /* Hashes from SHA-1 and above */
89 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1
) |
90 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160
) |
91 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224
) |
92 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256
) |
93 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384
) |
94 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512
),
95 0xFFFFFFF, /* Any PK alg */
96 0xFFFFFFF, /* Any curve */
101 * Next-default profile
103 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next
=
105 /* Hashes from SHA-256 and above */
106 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256
) |
107 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384
) |
108 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512
),
109 0xFFFFFFF, /* Any PK alg */
110 #if defined(MBEDTLS_ECP_C)
111 /* Curves at or above 128-bit security level */
112 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1
) |
113 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1
) |
114 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1
) |
115 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1
) |
116 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1
) |
117 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1
) |
118 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1
),
126 * NSA Suite B Profile
128 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb
=
130 /* Only SHA-256 and 384 */
131 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256
) |
132 MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384
),
134 MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA
),
135 #if defined(MBEDTLS_ECP_C)
136 /* Only NIST P-256 and P-384 */
137 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1
) |
138 MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1
),
146 * Check md_alg against profile
147 * Return 0 if md_alg acceptable for this profile, -1 otherwise
149 static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile
*profile
,
150 mbedtls_md_type_t md_alg
)
152 if( ( profile
->allowed_mds
& MBEDTLS_X509_ID_FLAG( md_alg
) ) != 0 )
159 * Check pk_alg against profile
160 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
162 static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile
*profile
,
163 mbedtls_pk_type_t pk_alg
)
165 if( ( profile
->allowed_pks
& MBEDTLS_X509_ID_FLAG( pk_alg
) ) != 0 )
172 * Check key against profile
173 * Return 0 if pk_alg acceptable for this profile, -1 otherwise
175 static int x509_profile_check_key( const mbedtls_x509_crt_profile
*profile
,
176 mbedtls_pk_type_t pk_alg
,
177 const mbedtls_pk_context
*pk
)
179 #if defined(MBEDTLS_RSA_C)
180 if( pk_alg
== MBEDTLS_PK_RSA
|| pk_alg
== MBEDTLS_PK_RSASSA_PSS
)
182 if( mbedtls_pk_get_bitlen( pk
) >= profile
->rsa_min_bitlen
)
189 #if defined(MBEDTLS_ECP_C)
190 if( pk_alg
== MBEDTLS_PK_ECDSA
||
191 pk_alg
== MBEDTLS_PK_ECKEY
||
192 pk_alg
== MBEDTLS_PK_ECKEY_DH
)
194 mbedtls_ecp_group_id gid
= mbedtls_pk_ec( *pk
)->grp
.id
;
196 if( ( profile
->allowed_curves
& MBEDTLS_X509_ID_FLAG( gid
) ) != 0 )
207 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
209 static int x509_get_version( unsigned char **p
,
210 const unsigned char *end
,
216 if( ( ret
= mbedtls_asn1_get_tag( p
, end
, &len
,
217 MBEDTLS_ASN1_CONTEXT_SPECIFIC
| MBEDTLS_ASN1_CONSTRUCTED
| 0 ) ) != 0 )
219 if( ret
== MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
)
230 if( ( ret
= mbedtls_asn1_get_int( p
, end
, ver
) ) != 0 )
231 return( MBEDTLS_ERR_X509_INVALID_VERSION
+ ret
);
234 return( MBEDTLS_ERR_X509_INVALID_VERSION
+
235 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
241 * Validity ::= SEQUENCE {
245 static int x509_get_dates( unsigned char **p
,
246 const unsigned char *end
,
247 mbedtls_x509_time
*from
,
248 mbedtls_x509_time
*to
)
253 if( ( ret
= mbedtls_asn1_get_tag( p
, end
, &len
,
254 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
255 return( MBEDTLS_ERR_X509_INVALID_DATE
+ ret
);
259 if( ( ret
= mbedtls_x509_get_time( p
, end
, from
) ) != 0 )
262 if( ( ret
= mbedtls_x509_get_time( p
, end
, to
) ) != 0 )
266 return( MBEDTLS_ERR_X509_INVALID_DATE
+
267 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
273 * X.509 v2/v3 unique identifier (not parsed)
275 static int x509_get_uid( unsigned char **p
,
276 const unsigned char *end
,
277 mbedtls_x509_buf
*uid
, int n
)
286 if( ( ret
= mbedtls_asn1_get_tag( p
, end
, &uid
->len
,
287 MBEDTLS_ASN1_CONTEXT_SPECIFIC
| MBEDTLS_ASN1_CONSTRUCTED
| n
) ) != 0 )
289 if( ret
== MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
)
301 static int x509_get_basic_constraints( unsigned char **p
,
302 const unsigned char *end
,
310 * BasicConstraints ::= SEQUENCE {
311 * cA BOOLEAN DEFAULT FALSE,
312 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
314 *ca_istrue
= 0; /* DEFAULT FALSE */
315 *max_pathlen
= 0; /* endless */
317 if( ( ret
= mbedtls_asn1_get_tag( p
, end
, &len
,
318 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
319 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
324 if( ( ret
= mbedtls_asn1_get_bool( p
, end
, ca_istrue
) ) != 0 )
326 if( ret
== MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
)
327 ret
= mbedtls_asn1_get_int( p
, end
, ca_istrue
);
330 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
332 if( *ca_istrue
!= 0 )
339 if( ( ret
= mbedtls_asn1_get_int( p
, end
, max_pathlen
) ) != 0 )
340 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
343 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
344 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
351 static int x509_get_ns_cert_type( unsigned char **p
,
352 const unsigned char *end
,
353 unsigned char *ns_cert_type
)
356 mbedtls_x509_bitstring bs
= { 0, 0, NULL
};
358 if( ( ret
= mbedtls_asn1_get_bitstring( p
, end
, &bs
) ) != 0 )
359 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
362 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
363 MBEDTLS_ERR_ASN1_INVALID_LENGTH
);
365 /* Get actual bitstring */
366 *ns_cert_type
= *bs
.p
;
370 static int x509_get_key_usage( unsigned char **p
,
371 const unsigned char *end
,
372 unsigned int *key_usage
)
376 mbedtls_x509_bitstring bs
= { 0, 0, NULL
};
378 if( ( ret
= mbedtls_asn1_get_bitstring( p
, end
, &bs
) ) != 0 )
379 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
382 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
383 MBEDTLS_ERR_ASN1_INVALID_LENGTH
);
385 /* Get actual bitstring */
387 for( i
= 0; i
< bs
.len
&& i
< sizeof( unsigned int ); i
++ )
389 *key_usage
|= (unsigned int) bs
.p
[i
] << (8*i
);
396 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
398 * KeyPurposeId ::= OBJECT IDENTIFIER
400 static int x509_get_ext_key_usage( unsigned char **p
,
401 const unsigned char *end
,
402 mbedtls_x509_sequence
*ext_key_usage
)
406 if( ( ret
= mbedtls_asn1_get_sequence_of( p
, end
, ext_key_usage
, MBEDTLS_ASN1_OID
) ) != 0 )
407 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
409 /* Sequence length must be >= 1 */
410 if( ext_key_usage
->buf
.p
== NULL
)
411 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
412 MBEDTLS_ERR_ASN1_INVALID_LENGTH
);
418 * SubjectAltName ::= GeneralNames
420 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
422 * GeneralName ::= CHOICE {
423 * otherName [0] OtherName,
424 * rfc822Name [1] IA5String,
425 * dNSName [2] IA5String,
426 * x400Address [3] ORAddress,
427 * directoryName [4] Name,
428 * ediPartyName [5] EDIPartyName,
429 * uniformResourceIdentifier [6] IA5String,
430 * iPAddress [7] OCTET STRING,
431 * registeredID [8] OBJECT IDENTIFIER }
433 * OtherName ::= SEQUENCE {
434 * type-id OBJECT IDENTIFIER,
435 * value [0] EXPLICIT ANY DEFINED BY type-id }
437 * EDIPartyName ::= SEQUENCE {
438 * nameAssigner [0] DirectoryString OPTIONAL,
439 * partyName [1] DirectoryString }
441 * NOTE: we only parse and use dNSName at this point.
443 static int x509_get_subject_alt_name( unsigned char **p
,
444 const unsigned char *end
,
445 mbedtls_x509_sequence
*subject_alt_name
)
449 mbedtls_asn1_buf
*buf
;
451 mbedtls_asn1_sequence
*cur
= subject_alt_name
;
453 /* Get main sequence tag */
454 if( ( ret
= mbedtls_asn1_get_tag( p
, end
, &len
,
455 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
456 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
458 if( *p
+ len
!= end
)
459 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
460 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
464 if( ( end
- *p
) < 1 )
465 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
466 MBEDTLS_ERR_ASN1_OUT_OF_DATA
);
470 if( ( ret
= mbedtls_asn1_get_len( p
, end
, &tag_len
) ) != 0 )
471 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
473 if( ( tag
& MBEDTLS_ASN1_CONTEXT_SPECIFIC
) != MBEDTLS_ASN1_CONTEXT_SPECIFIC
)
474 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
475 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
);
477 /* Skip everything but DNS name */
478 if( tag
!= ( MBEDTLS_ASN1_CONTEXT_SPECIFIC
| 2 ) )
484 /* Allocate and assign next pointer */
485 if( cur
->buf
.p
!= NULL
)
487 if( cur
->next
!= NULL
)
488 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
);
490 cur
->next
= mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence
) );
492 if( cur
->next
== NULL
)
493 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
494 MBEDTLS_ERR_ASN1_ALLOC_FAILED
);
506 /* Set final sequence entry's next pointer to NULL */
510 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
511 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
517 * X.509 v3 extensions
519 * TODO: Perform all of the basic constraints tests required by the RFC
520 * TODO: Set values for undetected extensions to a sane default?
523 static int x509_get_crt_ext( unsigned char **p
,
524 const unsigned char *end
,
525 mbedtls_x509_crt
*crt
)
529 unsigned char *end_ext_data
, *end_ext_octet
;
531 if( ( ret
= mbedtls_x509_get_ext( p
, end
, &crt
->v3_ext
, 3 ) ) != 0 )
533 if( ret
== MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
)
542 * Extension ::= SEQUENCE {
543 * extnID OBJECT IDENTIFIER,
544 * critical BOOLEAN DEFAULT FALSE,
545 * extnValue OCTET STRING }
547 mbedtls_x509_buf extn_oid
= {0, 0, NULL
};
548 int is_critical
= 0; /* DEFAULT FALSE */
551 if( ( ret
= mbedtls_asn1_get_tag( p
, end
, &len
,
552 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
553 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
555 end_ext_data
= *p
+ len
;
557 /* Get extension ID */
560 if( ( ret
= mbedtls_asn1_get_tag( p
, end
, &extn_oid
.len
, MBEDTLS_ASN1_OID
) ) != 0 )
561 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
566 if( ( end
- *p
) < 1 )
567 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
568 MBEDTLS_ERR_ASN1_OUT_OF_DATA
);
570 /* Get optional critical */
571 if( ( ret
= mbedtls_asn1_get_bool( p
, end_ext_data
, &is_critical
) ) != 0 &&
572 ( ret
!= MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
) )
573 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
575 /* Data should be octet string type */
576 if( ( ret
= mbedtls_asn1_get_tag( p
, end_ext_data
, &len
,
577 MBEDTLS_ASN1_OCTET_STRING
) ) != 0 )
578 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+ ret
);
580 end_ext_octet
= *p
+ len
;
582 if( end_ext_octet
!= end_ext_data
)
583 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
584 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
587 * Detect supported extensions
589 ret
= mbedtls_oid_get_x509_ext_type( &extn_oid
, &ext_type
);
593 /* No parser found, skip extension */
596 #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
599 /* Data is marked as critical: fail */
600 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
601 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG
);
607 /* Forbid repeated extensions */
608 if( ( crt
->ext_types
& ext_type
) != 0 )
609 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
);
611 crt
->ext_types
|= ext_type
;
615 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS
:
616 /* Parse basic constraints */
617 if( ( ret
= x509_get_basic_constraints( p
, end_ext_octet
,
618 &crt
->ca_istrue
, &crt
->max_pathlen
) ) != 0 )
622 case MBEDTLS_X509_EXT_KEY_USAGE
:
623 /* Parse key usage */
624 if( ( ret
= x509_get_key_usage( p
, end_ext_octet
,
625 &crt
->key_usage
) ) != 0 )
629 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE
:
630 /* Parse extended key usage */
631 if( ( ret
= x509_get_ext_key_usage( p
, end_ext_octet
,
632 &crt
->ext_key_usage
) ) != 0 )
636 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME
:
637 /* Parse subject alt name */
638 if( ( ret
= x509_get_subject_alt_name( p
, end_ext_octet
,
639 &crt
->subject_alt_names
) ) != 0 )
643 case MBEDTLS_X509_EXT_NS_CERT_TYPE
:
644 /* Parse netscape certificate type */
645 if( ( ret
= x509_get_ns_cert_type( p
, end_ext_octet
,
646 &crt
->ns_cert_type
) ) != 0 )
651 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE
);
656 return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS
+
657 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
663 * Parse and fill a single X.509 certificate in DER format
665 static int x509_crt_parse_der_core( mbedtls_x509_crt
*crt
, const unsigned char *buf
,
670 unsigned char *p
, *end
, *crt_end
;
671 mbedtls_x509_buf sig_params1
, sig_params2
, sig_oid2
;
673 memset( &sig_params1
, 0, sizeof( mbedtls_x509_buf
) );
674 memset( &sig_params2
, 0, sizeof( mbedtls_x509_buf
) );
675 memset( &sig_oid2
, 0, sizeof( mbedtls_x509_buf
) );
678 * Check for valid input
680 if( crt
== NULL
|| buf
== NULL
)
681 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
683 p
= mbedtls_calloc( 1, len
= buflen
);
685 return( MBEDTLS_ERR_X509_ALLOC_FAILED
);
687 memcpy( p
, buf
, buflen
);
694 * Certificate ::= SEQUENCE {
695 * tbsCertificate TBSCertificate,
696 * signatureAlgorithm AlgorithmIdentifier,
697 * signatureValue BIT STRING }
699 if( ( ret
= mbedtls_asn1_get_tag( &p
, end
, &len
,
700 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
702 mbedtls_x509_crt_free( crt
);
703 return( MBEDTLS_ERR_X509_INVALID_FORMAT
);
706 if( len
> (size_t) ( end
- p
) )
708 mbedtls_x509_crt_free( crt
);
709 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+
710 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
715 * TBSCertificate ::= SEQUENCE {
719 if( ( ret
= mbedtls_asn1_get_tag( &p
, end
, &len
,
720 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
722 mbedtls_x509_crt_free( crt
);
723 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
727 crt
->tbs
.len
= end
- crt
->tbs
.p
;
730 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
732 * CertificateSerialNumber ::= INTEGER
734 * signature AlgorithmIdentifier
736 if( ( ret
= x509_get_version( &p
, end
, &crt
->version
) ) != 0 ||
737 ( ret
= mbedtls_x509_get_serial( &p
, end
, &crt
->serial
) ) != 0 ||
738 ( ret
= mbedtls_x509_get_alg( &p
, end
, &crt
->sig_oid
,
739 &sig_params1
) ) != 0 )
741 mbedtls_x509_crt_free( crt
);
747 if( crt
->version
> 3 )
749 mbedtls_x509_crt_free( crt
);
750 return( MBEDTLS_ERR_X509_UNKNOWN_VERSION
);
753 if( ( ret
= mbedtls_x509_get_sig_alg( &crt
->sig_oid
, &sig_params1
,
754 &crt
->sig_md
, &crt
->sig_pk
,
755 &crt
->sig_opts
) ) != 0 )
757 mbedtls_x509_crt_free( crt
);
764 crt
->issuer_raw
.p
= p
;
766 if( ( ret
= mbedtls_asn1_get_tag( &p
, end
, &len
,
767 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
769 mbedtls_x509_crt_free( crt
);
770 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
773 if( ( ret
= mbedtls_x509_get_name( &p
, p
+ len
, &crt
->issuer
) ) != 0 )
775 mbedtls_x509_crt_free( crt
);
779 crt
->issuer_raw
.len
= p
- crt
->issuer_raw
.p
;
782 * Validity ::= SEQUENCE {
787 if( ( ret
= x509_get_dates( &p
, end
, &crt
->valid_from
,
788 &crt
->valid_to
) ) != 0 )
790 mbedtls_x509_crt_free( crt
);
797 crt
->subject_raw
.p
= p
;
799 if( ( ret
= mbedtls_asn1_get_tag( &p
, end
, &len
,
800 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
) ) != 0 )
802 mbedtls_x509_crt_free( crt
);
803 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+ ret
);
806 if( len
&& ( ret
= mbedtls_x509_get_name( &p
, p
+ len
, &crt
->subject
) ) != 0 )
808 mbedtls_x509_crt_free( crt
);
812 crt
->subject_raw
.len
= p
- crt
->subject_raw
.p
;
815 * SubjectPublicKeyInfo
817 if( ( ret
= mbedtls_pk_parse_subpubkey( &p
, end
, &crt
->pk
) ) != 0 )
819 mbedtls_x509_crt_free( crt
);
824 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
825 * -- If present, version shall be v2 or v3
826 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
827 * -- If present, version shall be v2 or v3
828 * extensions [3] EXPLICIT Extensions OPTIONAL
829 * -- If present, version shall be v3
831 if( crt
->version
== 2 || crt
->version
== 3 )
833 ret
= x509_get_uid( &p
, end
, &crt
->issuer_id
, 1 );
836 mbedtls_x509_crt_free( crt
);
841 if( crt
->version
== 2 || crt
->version
== 3 )
843 ret
= x509_get_uid( &p
, end
, &crt
->subject_id
, 2 );
846 mbedtls_x509_crt_free( crt
);
851 #if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
852 if( crt
->version
== 3 )
855 ret
= x509_get_crt_ext( &p
, end
, crt
);
858 mbedtls_x509_crt_free( crt
);
865 mbedtls_x509_crt_free( crt
);
866 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+
867 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
874 * -- end of TBSCertificate
876 * signatureAlgorithm AlgorithmIdentifier,
877 * signatureValue BIT STRING
879 if( ( ret
= mbedtls_x509_get_alg( &p
, end
, &sig_oid2
, &sig_params2
) ) != 0 )
881 mbedtls_x509_crt_free( crt
);
885 if( crt
->sig_oid
.len
!= sig_oid2
.len
||
886 memcmp( crt
->sig_oid
.p
, sig_oid2
.p
, crt
->sig_oid
.len
) != 0 ||
887 sig_params1
.len
!= sig_params2
.len
||
888 ( sig_params1
.len
!= 0 &&
889 memcmp( sig_params1
.p
, sig_params2
.p
, sig_params1
.len
) != 0 ) )
891 mbedtls_x509_crt_free( crt
);
892 return( MBEDTLS_ERR_X509_SIG_MISMATCH
);
895 if( ( ret
= mbedtls_x509_get_sig( &p
, end
, &crt
->sig
) ) != 0 )
897 mbedtls_x509_crt_free( crt
);
903 mbedtls_x509_crt_free( crt
);
904 return( MBEDTLS_ERR_X509_INVALID_FORMAT
+
905 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
912 * Parse one X.509 certificate in DER format from a buffer and add them to a
915 int mbedtls_x509_crt_parse_der( mbedtls_x509_crt
*chain
, const unsigned char *buf
,
919 mbedtls_x509_crt
*crt
= chain
, *prev
= NULL
;
922 * Check for valid input
924 if( crt
== NULL
|| buf
== NULL
)
925 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
927 while( crt
->version
!= 0 && crt
->next
!= NULL
)
934 * Add new certificate on the end of the chain if needed.
936 if( crt
->version
!= 0 && crt
->next
== NULL
)
938 crt
->next
= mbedtls_calloc( 1, sizeof( mbedtls_x509_crt
) );
940 if( crt
->next
== NULL
)
941 return( MBEDTLS_ERR_X509_ALLOC_FAILED
);
944 mbedtls_x509_crt_init( crt
->next
);
948 if( ( ret
= x509_crt_parse_der_core( crt
, buf
, buflen
) ) != 0 )
963 * Parse one or more PEM certificates from a buffer and add them to the chained
966 int mbedtls_x509_crt_parse( mbedtls_x509_crt
*chain
, const unsigned char *buf
, size_t buflen
)
968 int success
= 0, first_error
= 0, total_failed
= 0;
969 int buf_format
= MBEDTLS_X509_FORMAT_DER
;
972 * Check for valid input
974 if( chain
== NULL
|| buf
== NULL
)
975 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
978 * Determine buffer content. Buffer contains either one DER certificate or
979 * one or more PEM certificates.
981 #if defined(MBEDTLS_PEM_PARSE_C)
982 if( buflen
!= 0 && buf
[buflen
- 1] == '\0' &&
983 strstr( (const char *) buf
, "-----BEGIN CERTIFICATE-----" ) != NULL
)
985 buf_format
= MBEDTLS_X509_FORMAT_PEM
;
989 if( buf_format
== MBEDTLS_X509_FORMAT_DER
)
990 return mbedtls_x509_crt_parse_der( chain
, buf
, buflen
);
992 #if defined(MBEDTLS_PEM_PARSE_C)
993 if( buf_format
== MBEDTLS_X509_FORMAT_PEM
)
996 mbedtls_pem_context pem
;
998 /* 1 rather than 0 since the terminating NULL byte is counted in */
1002 mbedtls_pem_init( &pem
);
1004 /* If we get there, we know the string is null-terminated */
1005 ret
= mbedtls_pem_read_buffer( &pem
,
1006 "-----BEGIN CERTIFICATE-----",
1007 "-----END CERTIFICATE-----",
1008 buf
, NULL
, 0, &use_len
);
1018 else if( ret
== MBEDTLS_ERR_PEM_BAD_INPUT_DATA
)
1022 else if( ret
!= MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT
)
1024 mbedtls_pem_free( &pem
);
1027 * PEM header and footer were found
1032 if( first_error
== 0 )
1041 ret
= mbedtls_x509_crt_parse_der( chain
, pem
.buf
, pem
.buflen
);
1043 mbedtls_pem_free( &pem
);
1048 * Quit parsing on a memory error
1050 if( ret
== MBEDTLS_ERR_X509_ALLOC_FAILED
)
1053 if( first_error
== 0 )
1063 #endif /* MBEDTLS_PEM_PARSE_C */
1066 return( total_failed
);
1067 else if( first_error
)
1068 return( first_error
);
1070 return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT
);
1073 #if defined(MBEDTLS_FS_IO)
1075 * Load one or more certificates and add them to the chained list
1077 int mbedtls_x509_crt_parse_file( mbedtls_x509_crt
*chain
, const char *path
)
1083 if( ( ret
= mbedtls_pk_load_file( path
, &buf
, &n
) ) != 0 )
1086 ret
= mbedtls_x509_crt_parse( chain
, buf
, n
);
1088 mbedtls_zeroize( buf
, n
);
1089 mbedtls_free( buf
);
1094 int mbedtls_x509_crt_parse_path( mbedtls_x509_crt
*chain
, const char *path
)
1097 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
1099 WCHAR szDir
[MAX_PATH
];
1100 char filename
[MAX_PATH
];
1102 size_t len
= strlen( path
);
1104 WIN32_FIND_DATAW file_data
;
1107 if( len
> MAX_PATH
- 3 )
1108 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
1110 memset( szDir
, 0, sizeof(szDir
) );
1111 memset( filename
, 0, MAX_PATH
);
1112 memcpy( filename
, path
, len
);
1113 filename
[len
++] = '\\';
1115 filename
[len
++] = '*';
1117 w_ret
= MultiByteToWideChar( CP_ACP
, 0, filename
, len
, szDir
,
1120 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
1122 hFind
= FindFirstFileW( szDir
, &file_data
);
1123 if( hFind
== INVALID_HANDLE_VALUE
)
1124 return( MBEDTLS_ERR_X509_FILE_IO_ERROR
);
1126 len
= MAX_PATH
- len
;
1129 memset( p
, 0, len
);
1131 if( file_data
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
1134 w_ret
= WideCharToMultiByte( CP_ACP
, 0, file_data
.cFileName
,
1135 lstrlenW( file_data
.cFileName
),
1139 return( MBEDTLS_ERR_X509_FILE_IO_ERROR
);
1141 w_ret
= mbedtls_x509_crt_parse_file( chain
, filename
);
1147 while( FindNextFileW( hFind
, &file_data
) != 0 );
1149 if( GetLastError() != ERROR_NO_MORE_FILES
)
1150 ret
= MBEDTLS_ERR_X509_FILE_IO_ERROR
;
1156 struct dirent
*entry
;
1157 char entry_name
[255];
1158 DIR *dir
= opendir( path
);
1161 return( MBEDTLS_ERR_X509_FILE_IO_ERROR
);
1163 #if defined(MBEDTLS_THREADING_PTHREAD)
1164 if( ( ret
= mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex
) ) != 0 )
1171 while( ( entry
= readdir( dir
) ) != NULL
)
1173 mbedtls_snprintf( entry_name
, sizeof entry_name
, "%s/%s", path
, entry
->d_name
);
1175 if( stat( entry_name
, &sb
) == -1 )
1178 ret
= MBEDTLS_ERR_X509_FILE_IO_ERROR
;
1182 if( !S_ISREG( sb
.st_mode
) )
1185 // Ignore parse errors
1187 t_ret
= mbedtls_x509_crt_parse_file( chain
, entry_name
);
1196 #if defined(MBEDTLS_THREADING_PTHREAD)
1197 if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex
) != 0 )
1198 ret
= MBEDTLS_ERR_THREADING_MUTEX_ERROR
;
1205 #endif /* MBEDTLS_FS_IO */
1207 static int x509_info_subject_alt_name( char **buf
, size_t *size
,
1208 const mbedtls_x509_sequence
*subject_alt_name
)
1213 const mbedtls_x509_sequence
*cur
= subject_alt_name
;
1214 const char *sep
= "";
1217 while( cur
!= NULL
)
1219 if( cur
->buf
.len
+ sep_len
>= n
)
1222 return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL
);
1225 n
-= cur
->buf
.len
+ sep_len
;
1226 for( i
= 0; i
< sep_len
; i
++ )
1228 for( i
= 0; i
< cur
->buf
.len
; i
++ )
1229 *p
++ = cur
->buf
.p
[i
];
1245 #define PRINT_ITEM(i) \
1247 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
1248 MBEDTLS_X509_SAFE_SNPRINTF; \
1252 #define CERT_TYPE(type,name) \
1253 if( ns_cert_type & type ) \
1256 static int x509_info_cert_type( char **buf
, size_t *size
,
1257 unsigned char ns_cert_type
)
1262 const char *sep
= "";
1264 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT
, "SSL Client" );
1265 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER
, "SSL Server" );
1266 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL
, "Email" );
1267 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING
, "Object Signing" );
1268 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED
, "Reserved" );
1269 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA
, "SSL CA" );
1270 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA
, "Email CA" );
1271 CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA
, "Object Signing CA" );
1279 #define KEY_USAGE(code,name) \
1280 if( key_usage & code ) \
1283 static int x509_info_key_usage( char **buf
, size_t *size
,
1284 unsigned int key_usage
)
1289 const char *sep
= "";
1291 KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE
, "Digital Signature" );
1292 KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION
, "Non Repudiation" );
1293 KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT
, "Key Encipherment" );
1294 KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT
, "Data Encipherment" );
1295 KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT
, "Key Agreement" );
1296 KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN
, "Key Cert Sign" );
1297 KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN
, "CRL Sign" );
1298 KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY
, "Encipher Only" );
1299 KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY
, "Decipher Only" );
1307 static int x509_info_ext_key_usage( char **buf
, size_t *size
,
1308 const mbedtls_x509_sequence
*extended_key_usage
)
1314 const mbedtls_x509_sequence
*cur
= extended_key_usage
;
1315 const char *sep
= "";
1317 while( cur
!= NULL
)
1319 if( mbedtls_oid_get_extended_key_usage( &cur
->buf
, &desc
) != 0 )
1322 ret
= mbedtls_snprintf( p
, n
, "%s%s", sep
, desc
);
1323 MBEDTLS_X509_SAFE_SNPRINTF
;
1337 * Return an informational string about the certificate.
1339 #define BEFORE_COLON 18
1341 int mbedtls_x509_crt_info( char *buf
, size_t size
, const char *prefix
,
1342 const mbedtls_x509_crt
*crt
)
1347 char key_size_str
[BEFORE_COLON
];
1352 ret
= mbedtls_snprintf( p
, n
, "%scert. version : %d\n",
1353 prefix
, crt
->version
);
1354 MBEDTLS_X509_SAFE_SNPRINTF
;
1355 ret
= mbedtls_snprintf( p
, n
, "%sserial number : ",
1357 MBEDTLS_X509_SAFE_SNPRINTF
;
1359 ret
= mbedtls_x509_serial_gets( p
, n
, &crt
->serial
);
1360 MBEDTLS_X509_SAFE_SNPRINTF
;
1362 ret
= mbedtls_snprintf( p
, n
, "\n%sissuer name : ", prefix
);
1363 MBEDTLS_X509_SAFE_SNPRINTF
;
1364 ret
= mbedtls_x509_dn_gets( p
, n
, &crt
->issuer
);
1365 MBEDTLS_X509_SAFE_SNPRINTF
;
1367 ret
= mbedtls_snprintf( p
, n
, "\n%ssubject name : ", prefix
);
1368 MBEDTLS_X509_SAFE_SNPRINTF
;
1369 ret
= mbedtls_x509_dn_gets( p
, n
, &crt
->subject
);
1370 MBEDTLS_X509_SAFE_SNPRINTF
;
1372 ret
= mbedtls_snprintf( p
, n
, "\n%sissued on : " \
1373 "%04d-%02d-%02d %02d:%02d:%02d", prefix
,
1374 crt
->valid_from
.year
, crt
->valid_from
.mon
,
1375 crt
->valid_from
.day
, crt
->valid_from
.hour
,
1376 crt
->valid_from
.min
, crt
->valid_from
.sec
);
1377 MBEDTLS_X509_SAFE_SNPRINTF
;
1379 ret
= mbedtls_snprintf( p
, n
, "\n%sexpires on : " \
1380 "%04d-%02d-%02d %02d:%02d:%02d", prefix
,
1381 crt
->valid_to
.year
, crt
->valid_to
.mon
,
1382 crt
->valid_to
.day
, crt
->valid_to
.hour
,
1383 crt
->valid_to
.min
, crt
->valid_to
.sec
);
1384 MBEDTLS_X509_SAFE_SNPRINTF
;
1386 ret
= mbedtls_snprintf( p
, n
, "\n%ssigned using : ", prefix
);
1387 MBEDTLS_X509_SAFE_SNPRINTF
;
1389 ret
= mbedtls_x509_sig_alg_gets( p
, n
, &crt
->sig_oid
, crt
->sig_pk
,
1390 crt
->sig_md
, crt
->sig_opts
);
1391 MBEDTLS_X509_SAFE_SNPRINTF
;
1394 if( ( ret
= mbedtls_x509_key_size_helper( key_size_str
, BEFORE_COLON
,
1395 mbedtls_pk_get_name( &crt
->pk
) ) ) != 0 )
1400 ret
= mbedtls_snprintf( p
, n
, "\n%s%-" BC
"s: %d bits", prefix
, key_size_str
,
1401 (int) mbedtls_pk_get_bitlen( &crt
->pk
) );
1402 MBEDTLS_X509_SAFE_SNPRINTF
;
1405 * Optional extensions
1408 if( crt
->ext_types
& MBEDTLS_X509_EXT_BASIC_CONSTRAINTS
)
1410 ret
= mbedtls_snprintf( p
, n
, "\n%sbasic constraints : CA=%s", prefix
,
1411 crt
->ca_istrue
? "true" : "false" );
1412 MBEDTLS_X509_SAFE_SNPRINTF
;
1414 if( crt
->max_pathlen
> 0 )
1416 ret
= mbedtls_snprintf( p
, n
, ", max_pathlen=%d", crt
->max_pathlen
- 1 );
1417 MBEDTLS_X509_SAFE_SNPRINTF
;
1421 if( crt
->ext_types
& MBEDTLS_X509_EXT_SUBJECT_ALT_NAME
)
1423 ret
= mbedtls_snprintf( p
, n
, "\n%ssubject alt name : ", prefix
);
1424 MBEDTLS_X509_SAFE_SNPRINTF
;
1426 if( ( ret
= x509_info_subject_alt_name( &p
, &n
,
1427 &crt
->subject_alt_names
) ) != 0 )
1431 if( crt
->ext_types
& MBEDTLS_X509_EXT_NS_CERT_TYPE
)
1433 ret
= mbedtls_snprintf( p
, n
, "\n%scert. type : ", prefix
);
1434 MBEDTLS_X509_SAFE_SNPRINTF
;
1436 if( ( ret
= x509_info_cert_type( &p
, &n
, crt
->ns_cert_type
) ) != 0 )
1440 if( crt
->ext_types
& MBEDTLS_X509_EXT_KEY_USAGE
)
1442 ret
= mbedtls_snprintf( p
, n
, "\n%skey usage : ", prefix
);
1443 MBEDTLS_X509_SAFE_SNPRINTF
;
1445 if( ( ret
= x509_info_key_usage( &p
, &n
, crt
->key_usage
) ) != 0 )
1449 if( crt
->ext_types
& MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE
)
1451 ret
= mbedtls_snprintf( p
, n
, "\n%sext key usage : ", prefix
);
1452 MBEDTLS_X509_SAFE_SNPRINTF
;
1454 if( ( ret
= x509_info_ext_key_usage( &p
, &n
,
1455 &crt
->ext_key_usage
) ) != 0 )
1459 ret
= mbedtls_snprintf( p
, n
, "\n" );
1460 MBEDTLS_X509_SAFE_SNPRINTF
;
1462 return( (int) ( size
- n
) );
1465 struct x509_crt_verify_string
{
1470 static const struct x509_crt_verify_string x509_crt_verify_strings
[] = {
1471 { MBEDTLS_X509_BADCERT_EXPIRED
, "The certificate validity has expired" },
1472 { MBEDTLS_X509_BADCERT_REVOKED
, "The certificate has been revoked (is on a CRL)" },
1473 { MBEDTLS_X509_BADCERT_CN_MISMATCH
, "The certificate Common Name (CN) does not match with the expected CN" },
1474 { MBEDTLS_X509_BADCERT_NOT_TRUSTED
, "The certificate is not correctly signed by the trusted CA" },
1475 { MBEDTLS_X509_BADCRL_NOT_TRUSTED
, "The CRL is not correctly signed by the trusted CA" },
1476 { MBEDTLS_X509_BADCRL_EXPIRED
, "The CRL is expired" },
1477 { MBEDTLS_X509_BADCERT_MISSING
, "Certificate was missing" },
1478 { MBEDTLS_X509_BADCERT_SKIP_VERIFY
, "Certificate verification was skipped" },
1479 { MBEDTLS_X509_BADCERT_OTHER
, "Other reason (can be used by verify callback)" },
1480 { MBEDTLS_X509_BADCERT_FUTURE
, "The certificate validity starts in the future" },
1481 { MBEDTLS_X509_BADCRL_FUTURE
, "The CRL is from the future" },
1482 { MBEDTLS_X509_BADCERT_KEY_USAGE
, "Usage does not match the keyUsage extension" },
1483 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE
, "Usage does not match the extendedKeyUsage extension" },
1484 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE
, "Usage does not match the nsCertType extension" },
1485 { MBEDTLS_X509_BADCERT_BAD_MD
, "The certificate is signed with an unacceptable hash." },
1486 { MBEDTLS_X509_BADCERT_BAD_PK
, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1487 { MBEDTLS_X509_BADCERT_BAD_KEY
, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
1488 { MBEDTLS_X509_BADCRL_BAD_MD
, "The CRL is signed with an unacceptable hash." },
1489 { MBEDTLS_X509_BADCRL_BAD_PK
, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
1490 { MBEDTLS_X509_BADCRL_BAD_KEY
, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
1494 int mbedtls_x509_crt_verify_info( char *buf
, size_t size
, const char *prefix
,
1498 const struct x509_crt_verify_string
*cur
;
1502 for( cur
= x509_crt_verify_strings
; cur
->string
!= NULL
; cur
++ )
1504 if( ( flags
& cur
->code
) == 0 )
1507 ret
= mbedtls_snprintf( p
, n
, "%s%s\n", prefix
, cur
->string
);
1508 MBEDTLS_X509_SAFE_SNPRINTF
;
1514 ret
= mbedtls_snprintf( p
, n
, "%sUnknown reason "
1515 "(this should not happen)\n", prefix
);
1516 MBEDTLS_X509_SAFE_SNPRINTF
;
1519 return( (int) ( size
- n
) );
1522 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1523 int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt
*crt
,
1524 unsigned int usage
)
1526 unsigned int usage_must
, usage_may
;
1527 unsigned int may_mask
= MBEDTLS_X509_KU_ENCIPHER_ONLY
1528 | MBEDTLS_X509_KU_DECIPHER_ONLY
;
1530 if( ( crt
->ext_types
& MBEDTLS_X509_EXT_KEY_USAGE
) == 0 )
1533 usage_must
= usage
& ~may_mask
;
1535 if( ( ( crt
->key_usage
& ~may_mask
) & usage_must
) != usage_must
)
1536 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
1538 usage_may
= usage
& may_mask
;
1540 if( ( ( crt
->key_usage
& may_mask
) | usage_may
) != usage_may
)
1541 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
1547 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
1548 int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt
*crt
,
1549 const char *usage_oid
,
1552 const mbedtls_x509_sequence
*cur
;
1554 /* Extension is not mandatory, absent means no restriction */
1555 if( ( crt
->ext_types
& MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE
) == 0 )
1559 * Look for the requested usage (or wildcard ANY) in our list
1561 for( cur
= &crt
->ext_key_usage
; cur
!= NULL
; cur
= cur
->next
)
1563 const mbedtls_x509_buf
*cur_oid
= &cur
->buf
;
1565 if( cur_oid
->len
== usage_len
&&
1566 memcmp( cur_oid
->p
, usage_oid
, usage_len
) == 0 )
1571 if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE
, cur_oid
) == 0 )
1575 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
1577 #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
1579 #if defined(MBEDTLS_X509_CRL_PARSE_C)
1581 * Return 1 if the certificate is revoked, or 0 otherwise.
1583 int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt
*crt
, const mbedtls_x509_crl
*crl
)
1585 const mbedtls_x509_crl_entry
*cur
= &crl
->entry
;
1587 while( cur
!= NULL
&& cur
->serial
.len
!= 0 )
1589 if( crt
->serial
.len
== cur
->serial
.len
&&
1590 memcmp( crt
->serial
.p
, cur
->serial
.p
, crt
->serial
.len
) == 0 )
1592 if( mbedtls_x509_time_is_past( &cur
->revocation_date
) )
1603 * Check that the given certificate is valid according to the CRL.
1605 static int x509_crt_verifycrl( mbedtls_x509_crt
*crt
, mbedtls_x509_crt
*ca
,
1606 mbedtls_x509_crl
*crl_list
,
1607 const mbedtls_x509_crt_profile
*profile
)
1610 unsigned char hash
[MBEDTLS_MD_MAX_SIZE
];
1611 const mbedtls_md_info_t
*md_info
;
1617 * TODO: What happens if no CRL is present?
1618 * Suggestion: Revocation state should be unknown if no CRL is present.
1619 * For backwards compatibility this is not yet implemented.
1622 while( crl_list
!= NULL
)
1624 if( crl_list
->version
== 0 ||
1625 crl_list
->issuer_raw
.len
!= ca
->subject_raw
.len
||
1626 memcmp( crl_list
->issuer_raw
.p
, ca
->subject_raw
.p
,
1627 crl_list
->issuer_raw
.len
) != 0 )
1629 crl_list
= crl_list
->next
;
1634 * Check if the CA is configured to sign CRLs
1636 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1637 if( mbedtls_x509_crt_check_key_usage( ca
, MBEDTLS_X509_KU_CRL_SIGN
) != 0 )
1639 flags
|= MBEDTLS_X509_BADCRL_NOT_TRUSTED
;
1645 * Check if CRL is correctly signed by the trusted CA
1647 if( x509_profile_check_md_alg( profile
, crl_list
->sig_md
) != 0 )
1648 flags
|= MBEDTLS_X509_BADCRL_BAD_MD
;
1650 if( x509_profile_check_pk_alg( profile
, crl_list
->sig_pk
) != 0 )
1651 flags
|= MBEDTLS_X509_BADCRL_BAD_PK
;
1653 md_info
= mbedtls_md_info_from_type( crl_list
->sig_md
);
1654 if( md_info
== NULL
)
1657 * Cannot check 'unknown' hash
1659 flags
|= MBEDTLS_X509_BADCRL_NOT_TRUSTED
;
1663 mbedtls_md( md_info
, crl_list
->tbs
.p
, crl_list
->tbs
.len
, hash
);
1665 if( x509_profile_check_key( profile
, crl_list
->sig_pk
, &ca
->pk
) != 0 )
1666 flags
|= MBEDTLS_X509_BADCERT_BAD_KEY
;
1668 if( mbedtls_pk_verify_ext( crl_list
->sig_pk
, crl_list
->sig_opts
, &ca
->pk
,
1669 crl_list
->sig_md
, hash
, mbedtls_md_get_size( md_info
),
1670 crl_list
->sig
.p
, crl_list
->sig
.len
) != 0 )
1672 flags
|= MBEDTLS_X509_BADCRL_NOT_TRUSTED
;
1677 * Check for validity of CRL (Do not drop out)
1679 if( mbedtls_x509_time_is_past( &crl_list
->next_update
) )
1680 flags
|= MBEDTLS_X509_BADCRL_EXPIRED
;
1682 if( mbedtls_x509_time_is_future( &crl_list
->this_update
) )
1683 flags
|= MBEDTLS_X509_BADCRL_FUTURE
;
1686 * Check if certificate is revoked
1688 if( mbedtls_x509_crt_is_revoked( crt
, crl_list
) )
1690 flags
|= MBEDTLS_X509_BADCERT_REVOKED
;
1694 crl_list
= crl_list
->next
;
1699 #endif /* MBEDTLS_X509_CRL_PARSE_C */
1702 * Like memcmp, but case-insensitive and always returns -1 if different
1704 static int x509_memcasecmp( const void *s1
, const void *s2
, size_t len
)
1708 const unsigned char *n1
= s1
, *n2
= s2
;
1710 for( i
= 0; i
< len
; i
++ )
1712 diff
= n1
[i
] ^ n2
[i
];
1718 ( ( n1
[i
] >= 'a' && n1
[i
] <= 'z' ) ||
1719 ( n1
[i
] >= 'A' && n1
[i
] <= 'Z' ) ) )
1731 * Return 0 if name matches wildcard, -1 otherwise
1733 static int x509_check_wildcard( const char *cn
, mbedtls_x509_buf
*name
)
1736 size_t cn_idx
= 0, cn_len
= strlen( cn
);
1738 if( name
->len
< 3 || name
->p
[0] != '*' || name
->p
[1] != '.' )
1741 for( i
= 0; i
< cn_len
; ++i
)
1753 if( cn_len
- cn_idx
== name
->len
- 1 &&
1754 x509_memcasecmp( name
->p
+ 1, cn
+ cn_idx
, name
->len
- 1 ) == 0 )
1763 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
1764 * variations (but not all).
1766 * Return 0 if equal, -1 otherwise.
1768 static int x509_string_cmp( const mbedtls_x509_buf
*a
, const mbedtls_x509_buf
*b
)
1770 if( a
->tag
== b
->tag
&&
1772 memcmp( a
->p
, b
->p
, b
->len
) == 0 )
1777 if( ( a
->tag
== MBEDTLS_ASN1_UTF8_STRING
|| a
->tag
== MBEDTLS_ASN1_PRINTABLE_STRING
) &&
1778 ( b
->tag
== MBEDTLS_ASN1_UTF8_STRING
|| b
->tag
== MBEDTLS_ASN1_PRINTABLE_STRING
) &&
1780 x509_memcasecmp( a
->p
, b
->p
, b
->len
) == 0 )
1789 * Compare two X.509 Names (aka rdnSequence).
1791 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
1792 * we sometimes return unequal when the full algorithm would return equal,
1793 * but never the other way. (In particular, we don't do Unicode normalisation
1794 * or space folding.)
1796 * Return 0 if equal, -1 otherwise.
1798 static int x509_name_cmp( const mbedtls_x509_name
*a
, const mbedtls_x509_name
*b
)
1800 /* Avoid recursion, it might not be optimised by the compiler */
1801 while( a
!= NULL
|| b
!= NULL
)
1803 if( a
== NULL
|| b
== NULL
)
1807 if( a
->oid
.tag
!= b
->oid
.tag
||
1808 a
->oid
.len
!= b
->oid
.len
||
1809 memcmp( a
->oid
.p
, b
->oid
.p
, b
->oid
.len
) != 0 )
1815 if( x509_string_cmp( &a
->val
, &b
->val
) != 0 )
1818 /* structure of the list of sets */
1819 if( a
->next_merged
!= b
->next_merged
)
1826 /* a == NULL == b */
1831 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
1832 * Return 0 if yes, -1 if not.
1834 * top means parent is a locally-trusted certificate
1835 * bottom means child is the end entity cert
1837 static int x509_crt_check_parent( const mbedtls_x509_crt
*child
,
1838 const mbedtls_x509_crt
*parent
,
1839 int top
, int bottom
)
1843 /* Parent must be the issuer */
1844 if( x509_name_cmp( &child
->issuer
, &parent
->subject
) != 0 )
1847 /* Parent must have the basicConstraints CA bit set as a general rule */
1850 /* Exception: v1/v2 certificates that are locally trusted. */
1851 if( top
&& parent
->version
< 3 )
1854 /* Exception: self-signed end-entity certs that are locally trusted. */
1855 if( top
&& bottom
&&
1856 child
->raw
.len
== parent
->raw
.len
&&
1857 memcmp( child
->raw
.p
, parent
->raw
.p
, child
->raw
.len
) == 0 )
1862 if( need_ca_bit
&& ! parent
->ca_istrue
)
1865 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
1867 mbedtls_x509_crt_check_key_usage( parent
, MBEDTLS_X509_KU_KEY_CERT_SIGN
) != 0 )
1876 static int x509_crt_verify_top(
1877 mbedtls_x509_crt
*child
, mbedtls_x509_crt
*trust_ca
,
1878 mbedtls_x509_crl
*ca_crl
,
1879 const mbedtls_x509_crt_profile
*profile
,
1880 int path_cnt
, int self_cnt
, uint32_t *flags
,
1881 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
1885 uint32_t ca_flags
= 0;
1887 unsigned char hash
[MBEDTLS_MD_MAX_SIZE
];
1888 const mbedtls_md_info_t
*md_info
;
1890 if( mbedtls_x509_time_is_past( &child
->valid_to
) )
1891 *flags
|= MBEDTLS_X509_BADCERT_EXPIRED
;
1893 if( mbedtls_x509_time_is_future( &child
->valid_from
) )
1894 *flags
|= MBEDTLS_X509_BADCERT_FUTURE
;
1896 if( x509_profile_check_md_alg( profile
, child
->sig_md
) != 0 )
1897 *flags
|= MBEDTLS_X509_BADCERT_BAD_MD
;
1899 if( x509_profile_check_pk_alg( profile
, child
->sig_pk
) != 0 )
1900 *flags
|= MBEDTLS_X509_BADCERT_BAD_PK
;
1903 * Child is the top of the chain. Check against the trust_ca list.
1905 *flags
|= MBEDTLS_X509_BADCERT_NOT_TRUSTED
;
1907 md_info
= mbedtls_md_info_from_type( child
->sig_md
);
1908 if( md_info
== NULL
)
1911 * Cannot check 'unknown', no need to try any CA
1916 mbedtls_md( md_info
, child
->tbs
.p
, child
->tbs
.len
, hash
);
1918 for( /* trust_ca */ ; trust_ca
!= NULL
; trust_ca
= trust_ca
->next
)
1920 if( x509_crt_check_parent( child
, trust_ca
, 1, path_cnt
== 0 ) != 0 )
1923 check_path_cnt
= path_cnt
+ 1;
1926 * Reduce check_path_cnt to check against if top of the chain is
1927 * the same as the trusted CA
1929 if( child
->subject_raw
.len
== trust_ca
->subject_raw
.len
&&
1930 memcmp( child
->subject_raw
.p
, trust_ca
->subject_raw
.p
,
1931 child
->issuer_raw
.len
) == 0 )
1936 /* Self signed certificates do not count towards the limit */
1937 if( trust_ca
->max_pathlen
> 0 &&
1938 trust_ca
->max_pathlen
< check_path_cnt
- self_cnt
)
1943 if( mbedtls_pk_verify_ext( child
->sig_pk
, child
->sig_opts
, &trust_ca
->pk
,
1944 child
->sig_md
, hash
, mbedtls_md_get_size( md_info
),
1945 child
->sig
.p
, child
->sig
.len
) != 0 )
1951 * Top of chain is signed by a trusted CA
1953 *flags
&= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED
;
1955 if( x509_profile_check_key( profile
, child
->sig_pk
, &trust_ca
->pk
) != 0 )
1956 *flags
|= MBEDTLS_X509_BADCERT_BAD_KEY
;
1962 * If top of chain is not the same as the trusted CA send a verify request
1963 * to the callback for any issues with validity and CRL presence for the
1964 * trusted CA certificate.
1966 if( trust_ca
!= NULL
&&
1967 ( child
->subject_raw
.len
!= trust_ca
->subject_raw
.len
||
1968 memcmp( child
->subject_raw
.p
, trust_ca
->subject_raw
.p
,
1969 child
->issuer_raw
.len
) != 0 ) )
1971 #if defined(MBEDTLS_X509_CRL_PARSE_C)
1972 /* Check trusted CA's CRL for the chain's top crt */
1973 *flags
|= x509_crt_verifycrl( child
, trust_ca
, ca_crl
, profile
);
1978 if( mbedtls_x509_time_is_past( &trust_ca
->valid_to
) )
1979 ca_flags
|= MBEDTLS_X509_BADCERT_EXPIRED
;
1981 if( mbedtls_x509_time_is_future( &trust_ca
->valid_from
) )
1982 ca_flags
|= MBEDTLS_X509_BADCERT_FUTURE
;
1984 if( NULL
!= f_vrfy
)
1986 if( ( ret
= f_vrfy( p_vrfy
, trust_ca
, path_cnt
+ 1,
1987 &ca_flags
) ) != 0 )
1994 /* Call callback on top cert */
1995 if( NULL
!= f_vrfy
)
1997 if( ( ret
= f_vrfy( p_vrfy
, child
, path_cnt
, flags
) ) != 0 )
2006 static int x509_crt_verify_child(
2007 mbedtls_x509_crt
*child
, mbedtls_x509_crt
*parent
,
2008 mbedtls_x509_crt
*trust_ca
, mbedtls_x509_crl
*ca_crl
,
2009 const mbedtls_x509_crt_profile
*profile
,
2010 int path_cnt
, int self_cnt
, uint32_t *flags
,
2011 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
2015 uint32_t parent_flags
= 0;
2016 unsigned char hash
[MBEDTLS_MD_MAX_SIZE
];
2017 mbedtls_x509_crt
*grandparent
;
2018 const mbedtls_md_info_t
*md_info
;
2020 /* Counting intermediate self signed certificates */
2021 if( ( path_cnt
!= 0 ) && x509_name_cmp( &child
->issuer
, &child
->subject
) == 0 )
2024 /* path_cnt is 0 for the first intermediate CA */
2025 if( 1 + path_cnt
> MBEDTLS_X509_MAX_INTERMEDIATE_CA
)
2027 *flags
|= MBEDTLS_X509_BADCERT_NOT_TRUSTED
;
2028 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
);
2031 if( mbedtls_x509_time_is_past( &child
->valid_to
) )
2032 *flags
|= MBEDTLS_X509_BADCERT_EXPIRED
;
2034 if( mbedtls_x509_time_is_future( &child
->valid_from
) )
2035 *flags
|= MBEDTLS_X509_BADCERT_FUTURE
;
2037 if( x509_profile_check_md_alg( profile
, child
->sig_md
) != 0 )
2038 *flags
|= MBEDTLS_X509_BADCERT_BAD_MD
;
2040 if( x509_profile_check_pk_alg( profile
, child
->sig_pk
) != 0 )
2041 *flags
|= MBEDTLS_X509_BADCERT_BAD_PK
;
2043 md_info
= mbedtls_md_info_from_type( child
->sig_md
);
2044 if( md_info
== NULL
)
2047 * Cannot check 'unknown' hash
2049 *flags
|= MBEDTLS_X509_BADCERT_NOT_TRUSTED
;
2053 mbedtls_md( md_info
, child
->tbs
.p
, child
->tbs
.len
, hash
);
2055 if( x509_profile_check_key( profile
, child
->sig_pk
, &parent
->pk
) != 0 )
2056 *flags
|= MBEDTLS_X509_BADCERT_BAD_KEY
;
2058 if( mbedtls_pk_verify_ext( child
->sig_pk
, child
->sig_opts
, &parent
->pk
,
2059 child
->sig_md
, hash
, mbedtls_md_get_size( md_info
),
2060 child
->sig
.p
, child
->sig
.len
) != 0 )
2062 *flags
|= MBEDTLS_X509_BADCERT_NOT_TRUSTED
;
2066 #if defined(MBEDTLS_X509_CRL_PARSE_C)
2067 /* Check trusted CA's CRL for the given crt */
2068 *flags
|= x509_crt_verifycrl(child
, parent
, ca_crl
, profile
);
2071 /* Look for a grandparent in trusted CAs */
2072 for( grandparent
= trust_ca
;
2073 grandparent
!= NULL
;
2074 grandparent
= grandparent
->next
)
2076 if( x509_crt_check_parent( parent
, grandparent
,
2077 0, path_cnt
== 0 ) == 0 )
2081 if( grandparent
!= NULL
)
2083 ret
= x509_crt_verify_top( parent
, grandparent
, ca_crl
, profile
,
2084 path_cnt
+ 1, self_cnt
, &parent_flags
, f_vrfy
, p_vrfy
);
2090 /* Look for a grandparent upwards the chain */
2091 for( grandparent
= parent
->next
;
2092 grandparent
!= NULL
;
2093 grandparent
= grandparent
->next
)
2095 /* +2 because the current step is not yet accounted for
2096 * and because max_pathlen is one higher than it should be.
2097 * Also self signed certificates do not count to the limit. */
2098 if( grandparent
->max_pathlen
> 0 &&
2099 grandparent
->max_pathlen
< 2 + path_cnt
- self_cnt
)
2104 if( x509_crt_check_parent( parent
, grandparent
,
2105 0, path_cnt
== 0 ) == 0 )
2109 /* Is our parent part of the chain or at the top? */
2110 if( grandparent
!= NULL
)
2112 ret
= x509_crt_verify_child( parent
, grandparent
, trust_ca
, ca_crl
,
2113 profile
, path_cnt
+ 1, self_cnt
, &parent_flags
,
2120 ret
= x509_crt_verify_top( parent
, trust_ca
, ca_crl
, profile
,
2121 path_cnt
+ 1, self_cnt
, &parent_flags
,
2128 /* child is verified to be a child of the parent, call verify callback */
2129 if( NULL
!= f_vrfy
)
2130 if( ( ret
= f_vrfy( p_vrfy
, child
, path_cnt
, flags
) ) != 0 )
2133 *flags
|= parent_flags
;
2139 * Verify the certificate validity
2141 int mbedtls_x509_crt_verify( mbedtls_x509_crt
*crt
,
2142 mbedtls_x509_crt
*trust_ca
,
2143 mbedtls_x509_crl
*ca_crl
,
2144 const char *cn
, uint32_t *flags
,
2145 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
2148 return( mbedtls_x509_crt_verify_with_profile( crt
, trust_ca
, ca_crl
,
2149 &mbedtls_x509_crt_profile_default
, cn
, flags
, f_vrfy
, p_vrfy
) );
2154 * Verify the certificate validity, with profile
2156 int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt
*crt
,
2157 mbedtls_x509_crt
*trust_ca
,
2158 mbedtls_x509_crl
*ca_crl
,
2159 const mbedtls_x509_crt_profile
*profile
,
2160 const char *cn
, uint32_t *flags
,
2161 int (*f_vrfy
)(void *, mbedtls_x509_crt
*, int, uint32_t *),
2166 int pathlen
= 0, selfsigned
= 0;
2167 mbedtls_x509_crt
*parent
;
2168 mbedtls_x509_name
*name
;
2169 mbedtls_x509_sequence
*cur
= NULL
;
2170 mbedtls_pk_type_t pk_type
;
2172 if( profile
== NULL
)
2173 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA
);
2179 name
= &crt
->subject
;
2180 cn_len
= strlen( cn
);
2182 if( crt
->ext_types
& MBEDTLS_X509_EXT_SUBJECT_ALT_NAME
)
2184 cur
= &crt
->subject_alt_names
;
2186 while( cur
!= NULL
)
2188 if( cur
->buf
.len
== cn_len
&&
2189 x509_memcasecmp( cn
, cur
->buf
.p
, cn_len
) == 0 )
2192 if( cur
->buf
.len
> 2 &&
2193 memcmp( cur
->buf
.p
, "*.", 2 ) == 0 &&
2194 x509_check_wildcard( cn
, &cur
->buf
) == 0 )
2203 *flags
|= MBEDTLS_X509_BADCERT_CN_MISMATCH
;
2207 while( name
!= NULL
)
2209 if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN
, &name
->oid
) == 0 )
2211 if( name
->val
.len
== cn_len
&&
2212 x509_memcasecmp( name
->val
.p
, cn
, cn_len
) == 0 )
2215 if( name
->val
.len
> 2 &&
2216 memcmp( name
->val
.p
, "*.", 2 ) == 0 &&
2217 x509_check_wildcard( cn
, &name
->val
) == 0 )
2225 *flags
|= MBEDTLS_X509_BADCERT_CN_MISMATCH
;
2229 /* Check the type and size of the key */
2230 pk_type
= mbedtls_pk_get_type( &crt
->pk
);
2232 if( x509_profile_check_pk_alg( profile
, pk_type
) != 0 )
2233 *flags
|= MBEDTLS_X509_BADCERT_BAD_PK
;
2235 if( x509_profile_check_key( profile
, pk_type
, &crt
->pk
) != 0 )
2236 *flags
|= MBEDTLS_X509_BADCERT_BAD_KEY
;
2238 /* Look for a parent in trusted CAs */
2239 for( parent
= trust_ca
; parent
!= NULL
; parent
= parent
->next
)
2241 if( x509_crt_check_parent( crt
, parent
, 0, pathlen
== 0 ) == 0 )
2245 if( parent
!= NULL
)
2247 ret
= x509_crt_verify_top( crt
, parent
, ca_crl
, profile
,
2248 pathlen
, selfsigned
, flags
, f_vrfy
, p_vrfy
);
2254 /* Look for a parent upwards the chain */
2255 for( parent
= crt
->next
; parent
!= NULL
; parent
= parent
->next
)
2257 /* +2 because the current step is not yet accounted for
2258 * and because max_pathlen is one higher than it should be */
2259 if( parent
->max_pathlen
> 0 &&
2260 parent
->max_pathlen
< 2 + pathlen
)
2265 if( x509_crt_check_parent( crt
, parent
, 0, pathlen
== 0 ) == 0 )
2269 /* Are we part of the chain or at the top? */
2270 if( parent
!= NULL
)
2272 ret
= x509_crt_verify_child( crt
, parent
, trust_ca
, ca_crl
, profile
,
2273 pathlen
, selfsigned
, flags
, f_vrfy
, p_vrfy
);
2279 ret
= x509_crt_verify_top( crt
, trust_ca
, ca_crl
, profile
,
2280 pathlen
, selfsigned
, flags
, f_vrfy
, p_vrfy
);
2287 return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED
);
2293 * Initialize a certificate chain
2295 void mbedtls_x509_crt_init( mbedtls_x509_crt
*crt
)
2297 memset( crt
, 0, sizeof(mbedtls_x509_crt
) );
2301 * Unallocate all certificate data
2303 void mbedtls_x509_crt_free( mbedtls_x509_crt
*crt
)
2305 mbedtls_x509_crt
*cert_cur
= crt
;
2306 mbedtls_x509_crt
*cert_prv
;
2307 mbedtls_x509_name
*name_cur
;
2308 mbedtls_x509_name
*name_prv
;
2309 mbedtls_x509_sequence
*seq_cur
;
2310 mbedtls_x509_sequence
*seq_prv
;
2317 mbedtls_pk_free( &cert_cur
->pk
);
2319 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
2320 mbedtls_free( cert_cur
->sig_opts
);
2323 name_cur
= cert_cur
->issuer
.next
;
2324 while( name_cur
!= NULL
)
2326 name_prv
= name_cur
;
2327 name_cur
= name_cur
->next
;
2328 mbedtls_zeroize( name_prv
, sizeof( mbedtls_x509_name
) );
2329 mbedtls_free( name_prv
);
2332 name_cur
= cert_cur
->subject
.next
;
2333 while( name_cur
!= NULL
)
2335 name_prv
= name_cur
;
2336 name_cur
= name_cur
->next
;
2337 mbedtls_zeroize( name_prv
, sizeof( mbedtls_x509_name
) );
2338 mbedtls_free( name_prv
);
2341 seq_cur
= cert_cur
->ext_key_usage
.next
;
2342 while( seq_cur
!= NULL
)
2345 seq_cur
= seq_cur
->next
;
2346 mbedtls_zeroize( seq_prv
, sizeof( mbedtls_x509_sequence
) );
2347 mbedtls_free( seq_prv
);
2350 seq_cur
= cert_cur
->subject_alt_names
.next
;
2351 while( seq_cur
!= NULL
)
2354 seq_cur
= seq_cur
->next
;
2355 mbedtls_zeroize( seq_prv
, sizeof( mbedtls_x509_sequence
) );
2356 mbedtls_free( seq_prv
);
2359 if( cert_cur
->raw
.p
!= NULL
)
2361 mbedtls_zeroize( cert_cur
->raw
.p
, cert_cur
->raw
.len
);
2362 mbedtls_free( cert_cur
->raw
.p
);
2365 cert_cur
= cert_cur
->next
;
2367 while( cert_cur
!= NULL
);
2372 cert_prv
= cert_cur
;
2373 cert_cur
= cert_cur
->next
;
2375 mbedtls_zeroize( cert_prv
, sizeof( mbedtls_x509_crt
) );
2376 if( cert_prv
!= crt
)
2377 mbedtls_free( cert_prv
);
2379 while( cert_cur
!= NULL
);
2382 #endif /* MBEDTLS_X509_CRT_PARSE_C */