ndef parser vcard now handles xvcard
[RRG-proxmark3.git] / common / mbedtls / x509_crt.c
blob850893f0aa0905b225294f4ea1f8df583478b647
1 /*
2 * X.509 certificate parsing and verification
4 * Copyright The Mbed TLS Contributors
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.
20 * The ITU-T X.509 standard defines a certificate format for PKI.
22 * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
23 * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
24 * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
26 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
27 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
29 * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
32 #include "common.h"
34 #if defined(MBEDTLS_X509_CRT_PARSE_C)
36 #include "mbedtls/x509_crt.h"
37 #include "mbedtls/error.h"
38 #include "mbedtls/oid.h"
39 #include "mbedtls/platform_util.h"
41 #include <string.h>
43 #if defined(MBEDTLS_PEM_PARSE_C)
44 #include "mbedtls/pem.h"
45 #endif
47 #if defined(MBEDTLS_USE_PSA_CRYPTO)
48 #include "psa/crypto.h"
49 #include "mbedtls/psa_util.h"
50 #endif
52 #if defined(MBEDTLS_PLATFORM_C)
53 #include "mbedtls/platform.h"
54 #else
55 #include <stdio.h>
56 #include <stdlib.h>
57 #define mbedtls_free free
58 #define mbedtls_calloc calloc
59 #define mbedtls_snprintf snprintf
60 #endif
62 #if defined(MBEDTLS_THREADING_C)
63 #include "mbedtls/threading.h"
64 #endif
66 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
67 #include <windows.h>
68 #else
69 #include <time.h>
70 #endif
72 #if defined(MBEDTLS_FS_IO)
73 #include <stdio.h>
74 #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
75 #include <sys/types.h>
76 #include <sys/stat.h>
77 #include <dirent.h>
78 #endif /* !_WIN32 || EFIX64 || EFI32 */
79 #endif
82 * Item in a verification chain: cert and flags for it
84 typedef struct {
85 mbedtls_x509_crt *crt;
86 uint32_t flags;
87 } x509_crt_verify_chain_item;
90 * Max size of verification chain: end-entity + intermediates + trusted root
92 #define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
95 * Default profile
97 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default = {
98 #if defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_CERTIFICATES)
99 /* Allow SHA-1 (weak, but still safe in controlled environments) */
100 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) |
101 #endif
102 /* Only SHA-2 hashes */
103 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA224) |
104 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
105 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
106 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
107 0xFFFFFFF, /* Any PK alg */
108 0xFFFFFFF, /* Any curve */
109 2048,
113 * Next-default profile
115 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next = {
116 /* Hashes from SHA-256 and above */
117 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
118 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384) |
119 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA512),
120 0xFFFFFFF, /* Any PK alg */
121 #if defined(MBEDTLS_ECP_C)
122 /* Curves at or above 128-bit security level */
123 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
124 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1) |
125 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP521R1) |
126 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP256R1) |
127 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP384R1) |
128 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_BP512R1) |
129 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256K1),
130 #else
132 #endif
133 2048,
137 * NSA Suite B Profile
139 const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb = {
140 /* Only SHA-256 and 384 */
141 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256) |
142 MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA384),
143 /* Only ECDSA */
144 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECDSA) |
145 MBEDTLS_X509_ID_FLAG(MBEDTLS_PK_ECKEY),
146 #if defined(MBEDTLS_ECP_C)
147 /* Only NIST P-256 and P-384 */
148 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP256R1) |
149 MBEDTLS_X509_ID_FLAG(MBEDTLS_ECP_DP_SECP384R1),
150 #else
152 #endif
157 * Check md_alg against profile
158 * Return 0 if md_alg is acceptable for this profile, -1 otherwise
160 static int x509_profile_check_md_alg(const mbedtls_x509_crt_profile *profile,
161 mbedtls_md_type_t md_alg) {
162 if (md_alg == MBEDTLS_MD_NONE)
163 return (-1);
165 if ((profile->allowed_mds & MBEDTLS_X509_ID_FLAG(md_alg)) != 0)
166 return (0);
168 return (-1);
172 * Check pk_alg against profile
173 * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
175 static int x509_profile_check_pk_alg(const mbedtls_x509_crt_profile *profile,
176 mbedtls_pk_type_t pk_alg) {
177 if (pk_alg == MBEDTLS_PK_NONE)
178 return (-1);
180 if ((profile->allowed_pks & MBEDTLS_X509_ID_FLAG(pk_alg)) != 0)
181 return (0);
183 return (-1);
187 * Check key against profile
188 * Return 0 if pk is acceptable for this profile, -1 otherwise
190 static int x509_profile_check_key(const mbedtls_x509_crt_profile *profile,
191 const mbedtls_pk_context *pk) {
192 const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type(pk);
194 #if defined(MBEDTLS_RSA_C)
195 if (pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS) {
196 if (mbedtls_pk_get_bitlen(pk) >= profile->rsa_min_bitlen)
197 return (0);
199 return (-1);
201 #endif
203 #if defined(MBEDTLS_ECP_C)
204 if (pk_alg == MBEDTLS_PK_ECDSA ||
205 pk_alg == MBEDTLS_PK_ECKEY ||
206 pk_alg == MBEDTLS_PK_ECKEY_DH) {
207 const mbedtls_ecp_group_id gid = mbedtls_pk_ec(*pk)->grp.id;
209 if (gid == MBEDTLS_ECP_DP_NONE)
210 return (-1);
212 if ((profile->allowed_curves & MBEDTLS_X509_ID_FLAG(gid)) != 0)
213 return (0);
215 return (-1);
217 #endif
219 return (-1);
223 * Like memcmp, but case-insensitive and always returns -1 if different
225 static int x509_memcasecmp(const void *s1, const void *s2, size_t len) {
226 size_t i;
227 unsigned char diff;
228 const unsigned char *n1 = s1, *n2 = s2;
230 for (i = 0; i < len; i++) {
231 diff = n1[i] ^ n2[i];
233 if (diff == 0)
234 continue;
236 if (diff == 32 &&
237 ((n1[i] >= 'a' && n1[i] <= 'z') ||
238 (n1[i] >= 'A' && n1[i] <= 'Z'))) {
239 continue;
242 return (-1);
245 return (0);
249 * Return 0 if name matches wildcard, -1 otherwise
251 static int x509_check_wildcard(const char *cn, const mbedtls_x509_buf *name) {
252 size_t i;
253 size_t cn_idx = 0, cn_len = strlen(cn);
255 /* We can't have a match if there is no wildcard to match */
256 if (name->len < 3 || name->p[0] != '*' || name->p[1] != '.')
257 return (-1);
259 for (i = 0; i < cn_len; ++i) {
260 if (cn[i] == '.') {
261 cn_idx = i;
262 break;
266 if (cn_idx == 0)
267 return (-1);
269 if (cn_len - cn_idx == name->len - 1 &&
270 x509_memcasecmp(name->p + 1, cn + cn_idx, name->len - 1) == 0) {
271 return (0);
274 return (-1);
278 * Compare two X.509 strings, case-insensitive, and allowing for some encoding
279 * variations (but not all).
281 * Return 0 if equal, -1 otherwise.
283 static int x509_string_cmp(const mbedtls_x509_buf *a, const mbedtls_x509_buf *b) {
284 if (a->tag == b->tag &&
285 a->len == b->len &&
286 memcmp(a->p, b->p, b->len) == 0) {
287 return (0);
290 if ((a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
291 (b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING) &&
292 a->len == b->len &&
293 x509_memcasecmp(a->p, b->p, b->len) == 0) {
294 return (0);
297 return (-1);
301 * Compare two X.509 Names (aka rdnSequence).
303 * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
304 * we sometimes return unequal when the full algorithm would return equal,
305 * but never the other way. (In particular, we don't do Unicode normalisation
306 * or space folding.)
308 * Return 0 if equal, -1 otherwise.
310 static int x509_name_cmp(const mbedtls_x509_name *a, const mbedtls_x509_name *b) {
311 /* Avoid recursion, it might not be optimised by the compiler */
312 while (a != NULL || b != NULL) {
313 if (a == NULL || b == NULL)
314 return (-1);
316 /* type */
317 if (a->oid.tag != b->oid.tag ||
318 a->oid.len != b->oid.len ||
319 memcmp(a->oid.p, b->oid.p, b->oid.len) != 0) {
320 return (-1);
323 /* value */
324 if (x509_string_cmp(&a->val, &b->val) != 0)
325 return (-1);
327 /* structure of the list of sets */
328 if (a->next_merged != b->next_merged)
329 return (-1);
331 a = a->next;
332 b = b->next;
335 /* a == NULL == b */
336 return (0);
340 * Reset (init or clear) a verify_chain
342 static void x509_crt_verify_chain_reset(
343 mbedtls_x509_crt_verify_chain *ver_chain) {
344 size_t i;
346 for (i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++) {
347 ver_chain->items[i].crt = NULL;
348 ver_chain->items[i].flags = (uint32_t) -1;
351 ver_chain->len = 0;
353 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
354 ver_chain->trust_ca_cb_result = NULL;
355 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
359 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
361 static int x509_get_version(unsigned char **p,
362 const unsigned char *end,
363 int *ver) {
364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
365 size_t len;
367 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
368 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0)) != 0) {
369 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
370 *ver = 0;
371 return (0);
374 return (MBEDTLS_ERR_X509_INVALID_FORMAT + ret);
377 end = *p + len;
379 if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0)
380 return (MBEDTLS_ERR_X509_INVALID_VERSION + ret);
382 if (*p != end)
383 return (MBEDTLS_ERR_X509_INVALID_VERSION +
384 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
386 return (0);
390 * Validity ::= SEQUENCE {
391 * notBefore Time,
392 * notAfter Time }
394 static int x509_get_dates(unsigned char **p,
395 const unsigned char *end,
396 mbedtls_x509_time *from,
397 mbedtls_x509_time *to) {
398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
399 size_t len;
401 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
402 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0)
403 return (MBEDTLS_ERR_X509_INVALID_DATE + ret);
405 end = *p + len;
407 if ((ret = mbedtls_x509_get_time(p, end, from)) != 0)
408 return (ret);
410 if ((ret = mbedtls_x509_get_time(p, end, to)) != 0)
411 return (ret);
413 if (*p != end)
414 return (MBEDTLS_ERR_X509_INVALID_DATE +
415 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
417 return (0);
421 * X.509 v2/v3 unique identifier (not parsed)
423 static int x509_get_uid(unsigned char **p,
424 const unsigned char *end,
425 mbedtls_x509_buf *uid, int n) {
426 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
428 if (*p == end)
429 return (0);
431 uid->tag = **p;
433 if ((ret = mbedtls_asn1_get_tag(p, end, &uid->len,
434 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n)) != 0) {
435 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
436 return (0);
438 return (MBEDTLS_ERR_X509_INVALID_FORMAT + ret);
441 uid->p = *p;
442 *p += uid->len;
444 return (0);
447 static int x509_get_basic_constraints(unsigned char **p,
448 const unsigned char *end,
449 int *ca_istrue,
450 int *max_pathlen) {
451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
452 size_t len;
455 * BasicConstraints ::= SEQUENCE {
456 * cA BOOLEAN DEFAULT FALSE,
457 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
459 *ca_istrue = 0; /* DEFAULT FALSE */
460 *max_pathlen = 0; /* endless */
462 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
463 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0)
464 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
466 if (*p == end)
467 return (0);
469 if ((ret = mbedtls_asn1_get_bool(p, end, ca_istrue)) != 0) {
470 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)
471 ret = mbedtls_asn1_get_int(p, end, ca_istrue);
473 if (ret != 0)
474 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
476 if (*ca_istrue != 0)
477 *ca_istrue = 1;
480 if (*p == end)
481 return (0);
483 if ((ret = mbedtls_asn1_get_int(p, end, max_pathlen)) != 0)
484 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
486 if (*p != end)
487 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
488 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
490 /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
491 * overflow, which is an undefined behavior. */
492 if (*max_pathlen == INT_MAX)
493 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
494 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
496 (*max_pathlen)++;
498 return (0);
501 static int x509_get_ns_cert_type(unsigned char **p,
502 const unsigned char *end,
503 unsigned char *ns_cert_type) {
504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
505 mbedtls_x509_bitstring bs = { 0, 0, NULL };
507 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0)
508 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
510 if (bs.len != 1)
511 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
512 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
514 /* Get actual bitstring */
515 *ns_cert_type = *bs.p;
516 return (0);
519 static int x509_get_key_usage(unsigned char **p,
520 const unsigned char *end,
521 unsigned int *key_usage) {
522 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
523 size_t i;
524 mbedtls_x509_bitstring bs = { 0, 0, NULL };
526 if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0)
527 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
529 if (bs.len < 1)
530 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
531 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
533 /* Get actual bitstring */
534 *key_usage = 0;
535 for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
536 *key_usage |= (unsigned int) bs.p[i] << (8 * i);
539 return (0);
543 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
545 * KeyPurposeId ::= OBJECT IDENTIFIER
547 static int x509_get_ext_key_usage(unsigned char **p,
548 const unsigned char *end,
549 mbedtls_x509_sequence *ext_key_usage) {
550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
552 if ((ret = mbedtls_asn1_get_sequence_of(p, end, ext_key_usage, MBEDTLS_ASN1_OID)) != 0)
553 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
555 /* Sequence length must be >= 1 */
556 if (ext_key_usage->buf.p == NULL)
557 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
558 MBEDTLS_ERR_ASN1_INVALID_LENGTH);
560 return (0);
564 * SubjectAltName ::= GeneralNames
566 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
568 * GeneralName ::= CHOICE {
569 * otherName [0] OtherName,
570 * rfc822Name [1] IA5String,
571 * dNSName [2] IA5String,
572 * x400Address [3] ORAddress,
573 * directoryName [4] Name,
574 * ediPartyName [5] EDIPartyName,
575 * uniformResourceIdentifier [6] IA5String,
576 * iPAddress [7] OCTET STRING,
577 * registeredID [8] OBJECT IDENTIFIER }
579 * OtherName ::= SEQUENCE {
580 * type-id OBJECT IDENTIFIER,
581 * value [0] EXPLICIT ANY DEFINED BY type-id }
583 * EDIPartyName ::= SEQUENCE {
584 * nameAssigner [0] DirectoryString OPTIONAL,
585 * partyName [1] DirectoryString }
587 * NOTE: we list all types, but only use dNSName and otherName
588 * of type HwModuleName, as defined in RFC 4108, at this point.
590 static int x509_get_subject_alt_name(unsigned char **p,
591 const unsigned char *end,
592 mbedtls_x509_sequence *subject_alt_name) {
593 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
594 size_t len, tag_len;
595 mbedtls_asn1_buf *buf;
596 unsigned char tag;
597 mbedtls_asn1_sequence *cur = subject_alt_name;
599 /* Get main sequence tag */
600 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
601 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0)
602 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
604 if (*p + len != end)
605 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
606 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
608 while (*p < end) {
609 mbedtls_x509_subject_alternative_name dummy_san_buf;
610 memset(&dummy_san_buf, 0, sizeof(dummy_san_buf));
612 tag = **p;
613 (*p)++;
614 if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0)
615 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
617 if ((tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
618 MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
619 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
620 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
624 * Check that the SAN is structured correctly.
626 ret = mbedtls_x509_parse_subject_alt_name(&(cur->buf), &dummy_san_buf);
628 * In case the extension is malformed, return an error,
629 * and clear the allocated sequences.
631 if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
632 mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
633 mbedtls_x509_sequence *seq_prv;
634 while (seq_cur != NULL) {
635 seq_prv = seq_cur;
636 seq_cur = seq_cur->next;
637 mbedtls_platform_zeroize(seq_prv,
638 sizeof(mbedtls_x509_sequence));
639 mbedtls_free(seq_prv);
641 subject_alt_name->next = NULL;
642 return (ret);
645 /* Allocate and assign next pointer */
646 if (cur->buf.p != NULL) {
647 if (cur->next != NULL)
648 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS);
650 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
652 if (cur->next == NULL)
653 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
654 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
656 cur = cur->next;
659 buf = &(cur->buf);
660 buf->tag = tag;
661 buf->p = *p;
662 buf->len = tag_len;
663 *p += buf->len;
666 /* Set final sequence entry's next pointer to NULL */
667 cur->next = NULL;
669 if (*p != end)
670 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
671 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
673 return (0);
677 * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
679 * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
681 * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
683 * PolicyInformation ::= SEQUENCE {
684 * policyIdentifier CertPolicyId,
685 * policyQualifiers SEQUENCE SIZE (1..MAX) OF
686 * PolicyQualifierInfo OPTIONAL }
688 * CertPolicyId ::= OBJECT IDENTIFIER
690 * PolicyQualifierInfo ::= SEQUENCE {
691 * policyQualifierId PolicyQualifierId,
692 * qualifier ANY DEFINED BY policyQualifierId }
694 * -- policyQualifierIds for Internet policy qualifiers
696 * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
697 * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
698 * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
700 * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
702 * Qualifier ::= CHOICE {
703 * cPSuri CPSuri,
704 * userNotice UserNotice }
706 * CPSuri ::= IA5String
708 * UserNotice ::= SEQUENCE {
709 * noticeRef NoticeReference OPTIONAL,
710 * explicitText DisplayText OPTIONAL }
712 * NoticeReference ::= SEQUENCE {
713 * organization DisplayText,
714 * noticeNumbers SEQUENCE OF INTEGER }
716 * DisplayText ::= CHOICE {
717 * ia5String IA5String (SIZE (1..200)),
718 * visibleString VisibleString (SIZE (1..200)),
719 * bmpString BMPString (SIZE (1..200)),
720 * utf8String UTF8String (SIZE (1..200)) }
722 * NOTE: we only parse and use anyPolicy without qualifiers at this point
723 * as defined in RFC 5280.
725 static int x509_get_certificate_policies(unsigned char **p,
726 const unsigned char *end,
727 mbedtls_x509_sequence *certificate_policies) {
728 int ret, parse_ret = 0;
729 size_t len;
730 mbedtls_asn1_buf *buf;
731 mbedtls_asn1_sequence *cur = certificate_policies;
733 /* Get main sequence tag */
734 ret = mbedtls_asn1_get_tag(p, end, &len,
735 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
736 if (ret != 0)
737 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
739 if (*p + len != end)
740 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
741 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
744 * Cannot be an empty sequence.
746 if (len == 0)
747 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
748 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
750 while (*p < end) {
751 mbedtls_x509_buf policy_oid;
752 const unsigned char *policy_end;
755 * Get the policy sequence
757 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
758 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0)
759 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
761 policy_end = *p + len;
763 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
764 MBEDTLS_ASN1_OID)) != 0)
765 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
767 policy_oid.tag = MBEDTLS_ASN1_OID;
768 policy_oid.len = len;
769 policy_oid.p = *p;
772 * Only AnyPolicy is currently supported when enforcing policy.
774 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_POLICY, &policy_oid) != 0) {
776 * Set the parsing return code but continue parsing, in case this
777 * extension is critical and MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION
778 * is configured.
780 parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
783 /* Allocate and assign next pointer */
784 if (cur->buf.p != NULL) {
785 if (cur->next != NULL)
786 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS);
788 cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
790 if (cur->next == NULL)
791 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
792 MBEDTLS_ERR_ASN1_ALLOC_FAILED);
794 cur = cur->next;
797 buf = &(cur->buf);
798 buf->tag = policy_oid.tag;
799 buf->p = policy_oid.p;
800 buf->len = policy_oid.len;
802 *p += len;
805 * If there is an optional qualifier, then *p < policy_end
806 * Check the Qualifier len to verify it doesn't exceed policy_end.
808 if (*p < policy_end) {
809 if ((ret = mbedtls_asn1_get_tag(p, policy_end, &len,
810 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0)
811 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
813 * Skip the optional policy qualifiers.
815 *p += len;
818 if (*p != policy_end)
819 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
820 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
823 /* Set final sequence entry's next pointer to NULL */
824 cur->next = NULL;
826 if (*p != end)
827 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
828 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
830 return (parse_ret);
834 * X.509 v3 extensions
837 static int x509_get_crt_ext(unsigned char **p,
838 const unsigned char *end,
839 mbedtls_x509_crt *crt,
840 mbedtls_x509_crt_ext_cb_t cb,
841 void *p_ctx) {
842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
843 size_t len;
844 unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
846 if (*p == end)
847 return (0);
849 if ((ret = mbedtls_x509_get_ext(p, end, &crt->v3_ext, 3)) != 0)
850 return (ret);
852 end = crt->v3_ext.p + crt->v3_ext.len;
853 while (*p < end) {
855 * Extension ::= SEQUENCE {
856 * extnID OBJECT IDENTIFIER,
857 * critical BOOLEAN DEFAULT FALSE,
858 * extnValue OCTET STRING }
860 mbedtls_x509_buf extn_oid = {0, 0, NULL};
861 int is_critical = 0; /* DEFAULT FALSE */
862 int ext_type = 0;
864 if ((ret = mbedtls_asn1_get_tag(p, end, &len,
865 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0)
866 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
868 end_ext_data = *p + len;
870 /* Get extension ID */
871 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
872 MBEDTLS_ASN1_OID)) != 0)
873 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
875 extn_oid.tag = MBEDTLS_ASN1_OID;
876 extn_oid.p = *p;
877 *p += extn_oid.len;
879 /* Get optional critical */
880 if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
881 (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG))
882 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
884 /* Data should be octet string type */
885 if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
886 MBEDTLS_ASN1_OCTET_STRING)) != 0)
887 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
889 start_ext_octet = *p;
890 end_ext_octet = *p + len;
892 if (end_ext_octet != end_ext_data)
893 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
894 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
897 * Detect supported extensions
899 ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
901 if (ret != 0) {
902 /* Give the callback (if any) a chance to handle the extension */
903 if (cb != NULL) {
904 ret = cb(p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet);
905 if (ret != 0 && is_critical)
906 return (ret);
907 *p = end_ext_octet;
908 continue;
911 /* No parser found, skip extension */
912 *p = end_ext_octet;
914 #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
915 if (is_critical) {
916 /* Data is marked as critical: fail */
917 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
918 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
920 #endif
921 continue;
924 /* Forbid repeated extensions */
925 if ((crt->ext_types & ext_type) != 0)
926 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS);
928 crt->ext_types |= ext_type;
930 switch (ext_type) {
931 case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
932 /* Parse basic constraints */
933 if ((ret = x509_get_basic_constraints(p, end_ext_octet,
934 &crt->ca_istrue, &crt->max_pathlen)) != 0)
935 return (ret);
936 break;
938 case MBEDTLS_X509_EXT_KEY_USAGE:
939 /* Parse key usage */
940 if ((ret = x509_get_key_usage(p, end_ext_octet,
941 &crt->key_usage)) != 0)
942 return (ret);
943 break;
945 case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
946 /* Parse extended key usage */
947 if ((ret = x509_get_ext_key_usage(p, end_ext_octet,
948 &crt->ext_key_usage)) != 0)
949 return (ret);
950 break;
952 case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
953 /* Parse subject alt name */
954 if ((ret = x509_get_subject_alt_name(p, end_ext_octet,
955 &crt->subject_alt_names)) != 0)
956 return (ret);
957 break;
959 case MBEDTLS_X509_EXT_NS_CERT_TYPE:
960 /* Parse netscape certificate type */
961 if ((ret = x509_get_ns_cert_type(p, end_ext_octet,
962 &crt->ns_cert_type)) != 0)
963 return (ret);
964 break;
966 case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
967 /* Parse certificate policies type */
968 if ((ret = x509_get_certificate_policies(p, end_ext_octet,
969 &crt->certificate_policies)) != 0) {
970 /* Give the callback (if any) a chance to handle the extension
971 * if it contains unsupported policies */
972 if (ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
973 cb(p_ctx, crt, &extn_oid, is_critical,
974 start_ext_octet, end_ext_octet) == 0)
975 break;
977 #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
978 if (is_critical)
979 return (ret);
980 else
981 #endif
983 * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
984 * cannot interpret or enforce the policy. However, it is up to
985 * the user to choose how to enforce the policies,
986 * unless the extension is critical.
988 if (ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE)
989 return (ret);
991 break;
993 default:
995 * If this is a non-critical extension, which the oid layer
996 * supports, but there isn't an x509 parser for it,
997 * skip the extension.
999 #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
1000 if (is_critical)
1001 return (MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
1002 else
1003 #endif
1004 *p = end_ext_octet;
1008 if (*p != end)
1009 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1010 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1012 return (0);
1016 * Parse and fill a single X.509 certificate in DER format
1018 static int x509_crt_parse_der_core(mbedtls_x509_crt *crt,
1019 const unsigned char *buf,
1020 size_t buflen,
1021 int make_copy,
1022 mbedtls_x509_crt_ext_cb_t cb,
1023 void *p_ctx) {
1024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1025 size_t len;
1026 unsigned char *p, *end, *crt_end;
1027 mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
1029 memset(&sig_params1, 0, sizeof(mbedtls_x509_buf));
1030 memset(&sig_params2, 0, sizeof(mbedtls_x509_buf));
1031 memset(&sig_oid2, 0, sizeof(mbedtls_x509_buf));
1034 * Check for valid input
1036 if (crt == NULL || buf == NULL)
1037 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
1039 /* Use the original buffer until we figure out actual length. */
1040 p = (unsigned char *) buf;
1041 len = buflen;
1042 end = p + len;
1045 * Certificate ::= SEQUENCE {
1046 * tbsCertificate TBSCertificate,
1047 * signatureAlgorithm AlgorithmIdentifier,
1048 * signatureValue BIT STRING }
1050 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1051 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1052 mbedtls_x509_crt_free(crt);
1053 return (MBEDTLS_ERR_X509_INVALID_FORMAT);
1056 end = crt_end = p + len;
1057 crt->raw.len = crt_end - buf;
1058 if (make_copy != 0) {
1059 /* Create and populate a new buffer for the raw field. */
1060 crt->raw.p = p = mbedtls_calloc(1, crt->raw.len);
1061 if (crt->raw.p == NULL)
1062 return (MBEDTLS_ERR_X509_ALLOC_FAILED);
1064 memcpy(crt->raw.p, buf, crt->raw.len);
1065 crt->own_buffer = 1;
1067 p += crt->raw.len - len;
1068 end = crt_end = p + len;
1069 } else {
1070 crt->raw.p = (unsigned char *) buf;
1071 crt->own_buffer = 0;
1075 * TBSCertificate ::= SEQUENCE {
1077 crt->tbs.p = p;
1079 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1080 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1081 mbedtls_x509_crt_free(crt);
1082 return (MBEDTLS_ERR_X509_INVALID_FORMAT + ret);
1085 end = p + len;
1086 crt->tbs.len = end - crt->tbs.p;
1089 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1091 * CertificateSerialNumber ::= INTEGER
1093 * signature AlgorithmIdentifier
1095 if ((ret = x509_get_version(&p, end, &crt->version)) != 0 ||
1096 (ret = mbedtls_x509_get_serial(&p, end, &crt->serial)) != 0 ||
1097 (ret = mbedtls_x509_get_alg(&p, end, &crt->sig_oid,
1098 &sig_params1)) != 0) {
1099 mbedtls_x509_crt_free(crt);
1100 return (ret);
1103 if (crt->version < 0 || crt->version > 2) {
1104 mbedtls_x509_crt_free(crt);
1105 return (MBEDTLS_ERR_X509_UNKNOWN_VERSION);
1108 crt->version++;
1110 if ((ret = mbedtls_x509_get_sig_alg(&crt->sig_oid, &sig_params1,
1111 &crt->sig_md, &crt->sig_pk,
1112 &crt->sig_opts)) != 0) {
1113 mbedtls_x509_crt_free(crt);
1114 return (ret);
1118 * issuer Name
1120 crt->issuer_raw.p = p;
1122 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1123 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1124 mbedtls_x509_crt_free(crt);
1125 return (MBEDTLS_ERR_X509_INVALID_FORMAT + ret);
1128 if ((ret = mbedtls_x509_get_name(&p, p + len, &crt->issuer)) != 0) {
1129 mbedtls_x509_crt_free(crt);
1130 return (ret);
1133 crt->issuer_raw.len = p - crt->issuer_raw.p;
1136 * Validity ::= SEQUENCE {
1137 * notBefore Time,
1138 * notAfter Time }
1141 if ((ret = x509_get_dates(&p, end, &crt->valid_from,
1142 &crt->valid_to)) != 0) {
1143 mbedtls_x509_crt_free(crt);
1144 return (ret);
1148 * subject Name
1150 crt->subject_raw.p = p;
1152 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1153 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
1154 mbedtls_x509_crt_free(crt);
1155 return (MBEDTLS_ERR_X509_INVALID_FORMAT + ret);
1158 if (len && (ret = mbedtls_x509_get_name(&p, p + len, &crt->subject)) != 0) {
1159 mbedtls_x509_crt_free(crt);
1160 return (ret);
1163 crt->subject_raw.len = p - crt->subject_raw.p;
1166 * SubjectPublicKeyInfo
1168 crt->pk_raw.p = p;
1169 if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &crt->pk)) != 0) {
1170 mbedtls_x509_crt_free(crt);
1171 return (ret);
1173 crt->pk_raw.len = p - crt->pk_raw.p;
1176 * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
1177 * -- If present, version shall be v2 or v3
1178 * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
1179 * -- If present, version shall be v2 or v3
1180 * extensions [3] EXPLICIT Extensions OPTIONAL
1181 * -- If present, version shall be v3
1183 if (crt->version == 2 || crt->version == 3) {
1184 ret = x509_get_uid(&p, end, &crt->issuer_id, 1);
1185 if (ret != 0) {
1186 mbedtls_x509_crt_free(crt);
1187 return (ret);
1191 if (crt->version == 2 || crt->version == 3) {
1192 ret = x509_get_uid(&p, end, &crt->subject_id, 2);
1193 if (ret != 0) {
1194 mbedtls_x509_crt_free(crt);
1195 return (ret);
1199 #if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
1200 if (crt->version == 3)
1201 #endif
1203 ret = x509_get_crt_ext(&p, end, crt, cb, p_ctx);
1204 if (ret != 0) {
1205 mbedtls_x509_crt_free(crt);
1206 return (ret);
1210 if (p != end) {
1211 mbedtls_x509_crt_free(crt);
1212 return (MBEDTLS_ERR_X509_INVALID_FORMAT +
1213 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1216 end = crt_end;
1220 * -- end of TBSCertificate
1222 * signatureAlgorithm AlgorithmIdentifier,
1223 * signatureValue BIT STRING
1225 if ((ret = mbedtls_x509_get_alg(&p, end, &sig_oid2, &sig_params2)) != 0) {
1226 mbedtls_x509_crt_free(crt);
1227 return (ret);
1230 if (crt->sig_oid.len != sig_oid2.len ||
1231 memcmp(crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len) != 0 ||
1232 sig_params1.tag != sig_params2.tag ||
1233 sig_params1.len != sig_params2.len ||
1234 (sig_params1.len != 0 &&
1235 memcmp(sig_params1.p, sig_params2.p, sig_params1.len) != 0)) {
1236 mbedtls_x509_crt_free(crt);
1237 return (MBEDTLS_ERR_X509_SIG_MISMATCH);
1240 if ((ret = mbedtls_x509_get_sig(&p, end, &crt->sig)) != 0) {
1241 mbedtls_x509_crt_free(crt);
1242 return (ret);
1245 if (p != end) {
1246 mbedtls_x509_crt_free(crt);
1247 return (MBEDTLS_ERR_X509_INVALID_FORMAT +
1248 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1251 return (0);
1255 * Parse one X.509 certificate in DER format from a buffer and add them to a
1256 * chained list
1258 static int mbedtls_x509_crt_parse_der_internal(mbedtls_x509_crt *chain,
1259 const unsigned char *buf,
1260 size_t buflen,
1261 int make_copy,
1262 mbedtls_x509_crt_ext_cb_t cb,
1263 void *p_ctx) {
1264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1265 mbedtls_x509_crt *crt = chain, *prev = NULL;
1268 * Check for valid input
1270 if (crt == NULL || buf == NULL)
1271 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
1273 while (crt->version != 0 && crt->next != NULL) {
1274 prev = crt;
1275 crt = crt->next;
1279 * Add new certificate on the end of the chain if needed.
1281 if (crt->version != 0 && crt->next == NULL) {
1282 crt->next = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
1284 if (crt->next == NULL)
1285 return (MBEDTLS_ERR_X509_ALLOC_FAILED);
1287 prev = crt;
1288 mbedtls_x509_crt_init(crt->next);
1289 crt = crt->next;
1292 ret = x509_crt_parse_der_core(crt, buf, buflen, make_copy, cb, p_ctx);
1293 if (ret != 0) {
1294 if (prev)
1295 prev->next = NULL;
1297 if (crt != chain)
1298 mbedtls_free(crt);
1300 return (ret);
1303 return (0);
1306 int mbedtls_x509_crt_parse_der_nocopy(mbedtls_x509_crt *chain,
1307 const unsigned char *buf,
1308 size_t buflen) {
1309 return (mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 0, NULL, NULL));
1312 int mbedtls_x509_crt_parse_der_with_ext_cb(mbedtls_x509_crt *chain,
1313 const unsigned char *buf,
1314 size_t buflen,
1315 int make_copy,
1316 mbedtls_x509_crt_ext_cb_t cb,
1317 void *p_ctx) {
1318 return (mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, make_copy, cb, p_ctx));
1321 int mbedtls_x509_crt_parse_der(mbedtls_x509_crt *chain,
1322 const unsigned char *buf,
1323 size_t buflen) {
1324 return (mbedtls_x509_crt_parse_der_internal(chain, buf, buflen, 1, NULL, NULL));
1328 * Parse one or more PEM certificates from a buffer and add them to the chained
1329 * list
1331 int mbedtls_x509_crt_parse(mbedtls_x509_crt *chain,
1332 const unsigned char *buf,
1333 size_t buflen) {
1334 #if defined(MBEDTLS_PEM_PARSE_C)
1335 int success = 0, first_error = 0, total_failed = 0;
1336 int buf_format = MBEDTLS_X509_FORMAT_DER;
1337 #endif
1340 * Check for valid input
1342 if (chain == NULL || buf == NULL)
1343 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
1346 * Determine buffer content. Buffer contains either one DER certificate or
1347 * one or more PEM certificates.
1349 #if defined(MBEDTLS_PEM_PARSE_C)
1350 if (buflen != 0 && buf[buflen - 1] == '\0' &&
1351 strstr((const char *) buf, "-----BEGIN CERTIFICATE-----") != NULL) {
1352 buf_format = MBEDTLS_X509_FORMAT_PEM;
1355 if (buf_format == MBEDTLS_X509_FORMAT_DER)
1356 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1357 #else
1358 return mbedtls_x509_crt_parse_der(chain, buf, buflen);
1359 #endif
1361 #if defined(MBEDTLS_PEM_PARSE_C)
1362 if (buf_format == MBEDTLS_X509_FORMAT_PEM) {
1363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1364 mbedtls_pem_context pem;
1366 /* 1 rather than 0 since the terminating NULL byte is counted in */
1367 while (buflen > 1) {
1368 size_t use_len;
1369 mbedtls_pem_init(&pem);
1371 /* If we get there, we know the string is null-terminated */
1372 ret = mbedtls_pem_read_buffer(&pem,
1373 "-----BEGIN CERTIFICATE-----",
1374 "-----END CERTIFICATE-----",
1375 buf, NULL, 0, &use_len);
1377 if (ret == 0) {
1379 * Was PEM encoded
1381 buflen -= use_len;
1382 buf += use_len;
1383 } else if (ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA) {
1384 return (ret);
1385 } else if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
1386 mbedtls_pem_free(&pem);
1389 * PEM header and footer were found
1391 buflen -= use_len;
1392 buf += use_len;
1394 if (first_error == 0)
1395 first_error = ret;
1397 total_failed++;
1398 continue;
1399 } else
1400 break;
1402 ret = mbedtls_x509_crt_parse_der(chain, pem.buf, pem.buflen);
1404 mbedtls_pem_free(&pem);
1406 if (ret != 0) {
1408 * Quit parsing on a memory error
1410 if (ret == MBEDTLS_ERR_X509_ALLOC_FAILED)
1411 return (ret);
1413 if (first_error == 0)
1414 first_error = ret;
1416 total_failed++;
1417 continue;
1420 success = 1;
1424 if (success)
1425 return (total_failed);
1426 else if (first_error)
1427 return (first_error);
1428 else
1429 return (MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT);
1430 #endif /* MBEDTLS_PEM_PARSE_C */
1433 #if defined(MBEDTLS_FS_IO)
1435 * Load one or more certificates and add them to the chained list
1437 int mbedtls_x509_crt_parse_file(mbedtls_x509_crt *chain, const char *path) {
1438 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1439 size_t n;
1440 unsigned char *buf;
1442 if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0)
1443 return (ret);
1445 ret = mbedtls_x509_crt_parse(chain, buf, n);
1447 mbedtls_platform_zeroize(buf, n);
1448 mbedtls_free(buf);
1450 return (ret);
1453 int mbedtls_x509_crt_parse_path(mbedtls_x509_crt *chain, const char *path) {
1454 int ret = 0;
1455 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
1456 int w_ret;
1457 WCHAR szDir[MAX_PATH];
1458 char filename[MAX_PATH];
1459 char *p;
1460 size_t len = strlen(path);
1462 WIN32_FIND_DATAW file_data;
1463 HANDLE hFind;
1465 if (len > MAX_PATH - 3)
1466 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
1468 memset(szDir, 0, sizeof(szDir));
1469 memset(filename, 0, MAX_PATH);
1470 memcpy(filename, path, len);
1471 filename[len++] = '\\';
1472 p = filename + len;
1473 filename[len++] = '*';
1475 w_ret = MultiByteToWideChar(CP_ACP, 0, filename, (int)len, szDir,
1476 MAX_PATH - 3);
1477 if (w_ret == 0)
1478 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
1480 hFind = FindFirstFileW(szDir, &file_data);
1481 if (hFind == INVALID_HANDLE_VALUE)
1482 return (MBEDTLS_ERR_X509_FILE_IO_ERROR);
1484 len = MAX_PATH - len;
1485 do {
1486 memset(p, 0, len);
1488 if (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1489 continue;
1491 w_ret = WideCharToMultiByte(CP_ACP, 0, file_data.cFileName,
1492 lstrlenW(file_data.cFileName),
1493 p, (int) len - 1,
1494 NULL, NULL);
1495 if (w_ret == 0) {
1496 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1497 goto cleanup;
1500 w_ret = mbedtls_x509_crt_parse_file(chain, filename);
1501 if (w_ret < 0)
1502 ret++;
1503 else
1504 ret += w_ret;
1505 } while (FindNextFileW(hFind, &file_data) != 0);
1507 if (GetLastError() != ERROR_NO_MORE_FILES)
1508 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1510 cleanup:
1511 FindClose(hFind);
1512 #else /* _WIN32 */
1513 int t_ret;
1514 int snp_ret;
1515 struct stat sb;
1516 struct dirent *entry;
1517 char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
1518 DIR *dir = opendir(path);
1520 if (dir == NULL)
1521 return (MBEDTLS_ERR_X509_FILE_IO_ERROR);
1523 #if defined(MBEDTLS_THREADING_C)
1524 if ((ret = mbedtls_mutex_lock(&mbedtls_threading_readdir_mutex)) != 0) {
1525 closedir(dir);
1526 return (ret);
1528 #endif /* MBEDTLS_THREADING_C */
1530 memset(&sb, 0, sizeof(sb));
1532 while ((entry = readdir(dir)) != NULL) {
1533 snp_ret = mbedtls_snprintf(entry_name, sizeof entry_name,
1534 "%s/%s", path, entry->d_name);
1536 if (snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name) {
1537 ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
1538 goto cleanup;
1539 } else if (stat(entry_name, &sb) == -1) {
1540 ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
1541 goto cleanup;
1544 if (!S_ISREG(sb.st_mode))
1545 continue;
1547 // Ignore parse errors
1549 t_ret = mbedtls_x509_crt_parse_file(chain, entry_name);
1550 if (t_ret < 0)
1551 ret++;
1552 else
1553 ret += t_ret;
1556 cleanup:
1557 closedir(dir);
1559 #if defined(MBEDTLS_THREADING_C)
1560 if (mbedtls_mutex_unlock(&mbedtls_threading_readdir_mutex) != 0)
1561 ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
1562 #endif /* MBEDTLS_THREADING_C */
1564 #endif /* _WIN32 */
1566 return (ret);
1568 #endif /* MBEDTLS_FS_IO */
1571 * OtherName ::= SEQUENCE {
1572 * type-id OBJECT IDENTIFIER,
1573 * value [0] EXPLICIT ANY DEFINED BY type-id }
1575 * HardwareModuleName ::= SEQUENCE {
1576 * hwType OBJECT IDENTIFIER,
1577 * hwSerialNum OCTET STRING }
1579 * NOTE: we currently only parse and use otherName of type HwModuleName,
1580 * as defined in RFC 4108.
1582 static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
1583 mbedtls_x509_san_other_name *other_name) {
1584 int ret = 0;
1585 size_t len;
1586 unsigned char *p = subject_alt_name->p;
1587 const unsigned char *end = p + subject_alt_name->len;
1588 mbedtls_x509_buf cur_oid;
1590 if ((subject_alt_name->tag &
1591 (MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
1592 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
1594 * The given subject alternative name is not of type "othername".
1596 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
1599 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1600 MBEDTLS_ASN1_OID)) != 0)
1601 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
1603 cur_oid.tag = MBEDTLS_ASN1_OID;
1604 cur_oid.p = p;
1605 cur_oid.len = len;
1608 * Only HwModuleName is currently supported.
1610 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
1611 return (MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
1614 if (p + len >= end) {
1615 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1616 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1617 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1619 p += len;
1620 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1621 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) != 0)
1622 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
1624 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1625 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0)
1626 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
1628 if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0)
1629 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
1631 other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
1632 other_name->value.hardware_module_name.oid.p = p;
1633 other_name->value.hardware_module_name.oid.len = len;
1635 if (p + len >= end) {
1636 mbedtls_platform_zeroize(other_name, sizeof(*other_name));
1637 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1638 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1640 p += len;
1641 if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
1642 MBEDTLS_ASN1_OCTET_STRING)) != 0)
1643 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret);
1645 other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
1646 other_name->value.hardware_module_name.val.p = p;
1647 other_name->value.hardware_module_name.val.len = len;
1648 p += len;
1649 if (p != end) {
1650 mbedtls_platform_zeroize(other_name,
1651 sizeof(*other_name));
1652 return (MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
1653 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
1655 return (0);
1658 static int x509_info_subject_alt_name(char **buf, size_t *size,
1659 const mbedtls_x509_sequence
1660 *subject_alt_name,
1661 const char *prefix) {
1662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1663 size_t n = *size;
1664 char *p = *buf;
1665 const mbedtls_x509_sequence *cur = subject_alt_name;
1666 mbedtls_x509_subject_alternative_name san;
1667 int parse_ret;
1669 while (cur != NULL) {
1670 memset(&san, 0, sizeof(san));
1671 parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
1672 if (parse_ret != 0) {
1673 if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
1674 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1675 MBEDTLS_X509_SAFE_SNPRINTF;
1676 } else {
1677 ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
1678 MBEDTLS_X509_SAFE_SNPRINTF;
1680 cur = cur->next;
1681 continue;
1684 switch (san.type) {
1686 * otherName
1688 case MBEDTLS_X509_SAN_OTHER_NAME: {
1689 mbedtls_x509_san_other_name *other_name = &san.san.other_name;
1691 ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
1692 MBEDTLS_X509_SAFE_SNPRINTF;
1694 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
1695 &other_name->value.hardware_module_name.oid) != 0) {
1696 ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
1697 MBEDTLS_X509_SAFE_SNPRINTF;
1698 ret = mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
1699 MBEDTLS_X509_SAFE_SNPRINTF;
1701 ret = mbedtls_oid_get_numeric_string(p, n, &other_name->value.hardware_module_name.oid);
1702 MBEDTLS_X509_SAFE_SNPRINTF;
1704 ret = mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
1705 MBEDTLS_X509_SAFE_SNPRINTF;
1707 if (other_name->value.hardware_module_name.val.len >= n) {
1708 *p = '\0';
1709 return (MBEDTLS_ERR_X509_BUFFER_TOO_SMALL);
1712 memcpy(p, other_name->value.hardware_module_name.val.p,
1713 other_name->value.hardware_module_name.val.len);
1714 p += other_name->value.hardware_module_name.val.len;
1716 n -= other_name->value.hardware_module_name.val.len;
1718 }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
1720 break;
1723 * dNSName
1725 case MBEDTLS_X509_SAN_DNS_NAME: {
1726 ret = mbedtls_snprintf(p, n, "\n%s dNSName : ", prefix);
1727 MBEDTLS_X509_SAFE_SNPRINTF;
1728 if (san.san.unstructured_name.len >= n) {
1729 *p = '\0';
1730 return (MBEDTLS_ERR_X509_BUFFER_TOO_SMALL);
1733 memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
1734 p += san.san.unstructured_name.len;
1735 n -= san.san.unstructured_name.len;
1737 break;
1740 * Type not supported, skip item.
1742 default:
1743 ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
1744 MBEDTLS_X509_SAFE_SNPRINTF;
1745 break;
1748 cur = cur->next;
1751 *p = '\0';
1753 *size = n;
1754 *buf = p;
1756 return (0);
1759 int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
1760 mbedtls_x509_subject_alternative_name *san) {
1761 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1762 switch (san_buf->tag &
1763 (MBEDTLS_ASN1_TAG_CLASS_MASK |
1764 MBEDTLS_ASN1_TAG_VALUE_MASK)) {
1766 * otherName
1768 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME): {
1769 mbedtls_x509_san_other_name other_name;
1771 ret = x509_get_other_name(san_buf, &other_name);
1772 if (ret != 0)
1773 return (ret);
1775 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1776 san->type = MBEDTLS_X509_SAN_OTHER_NAME;
1777 memcpy(&san->san.other_name,
1778 &other_name, sizeof(other_name));
1781 break;
1784 * dNSName
1786 case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME): {
1787 memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
1788 san->type = MBEDTLS_X509_SAN_DNS_NAME;
1790 memcpy(&san->san.unstructured_name,
1791 san_buf, sizeof(*san_buf));
1794 break;
1797 * Type not supported
1799 default:
1800 return (MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE);
1802 return (0);
1805 #define PRINT_ITEM(i) \
1807 ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
1808 MBEDTLS_X509_SAFE_SNPRINTF; \
1809 sep = ", "; \
1812 #define CERT_TYPE(type,name) \
1813 if( ns_cert_type & (type) ) \
1814 PRINT_ITEM( name );
1816 static int x509_info_cert_type(char **buf, size_t *size,
1817 unsigned char ns_cert_type) {
1818 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1819 size_t n = *size;
1820 char *p = *buf;
1821 const char *sep = "";
1823 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
1824 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
1825 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
1826 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
1827 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
1828 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
1829 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
1830 CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
1832 *size = n;
1833 *buf = p;
1835 return (0);
1838 #define KEY_USAGE(code,name) \
1839 if( key_usage & (code) ) \
1840 PRINT_ITEM( name );
1842 static int x509_info_key_usage(char **buf, size_t *size,
1843 unsigned int key_usage) {
1844 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1845 size_t n = *size;
1846 char *p = *buf;
1847 const char *sep = "";
1849 KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
1850 KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
1851 KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
1852 KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
1853 KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
1854 KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
1855 KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
1856 KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
1857 KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
1859 *size = n;
1860 *buf = p;
1862 return (0);
1865 static int x509_info_ext_key_usage(char **buf, size_t *size,
1866 const mbedtls_x509_sequence *extended_key_usage) {
1867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1868 const char *desc;
1869 size_t n = *size;
1870 char *p = *buf;
1871 const mbedtls_x509_sequence *cur = extended_key_usage;
1872 const char *sep = "";
1874 while (cur != NULL) {
1875 if (mbedtls_oid_get_extended_key_usage(&cur->buf, &desc) != 0)
1876 desc = "???";
1878 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
1879 MBEDTLS_X509_SAFE_SNPRINTF;
1881 sep = ", ";
1883 cur = cur->next;
1886 *size = n;
1887 *buf = p;
1889 return (0);
1892 static int x509_info_cert_policies(char **buf, size_t *size,
1893 const mbedtls_x509_sequence *certificate_policies) {
1894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1895 const char *desc;
1896 size_t n = *size;
1897 char *p = *buf;
1898 const mbedtls_x509_sequence *cur = certificate_policies;
1899 const char *sep = "";
1901 while (cur != NULL) {
1902 if (mbedtls_oid_get_certificate_policies(&cur->buf, &desc) != 0)
1903 desc = "???";
1905 ret = mbedtls_snprintf(p, n, "%s%s", sep, desc);
1906 MBEDTLS_X509_SAFE_SNPRINTF;
1908 sep = ", ";
1910 cur = cur->next;
1913 *size = n;
1914 *buf = p;
1916 return (0);
1920 * Return an informational string about the certificate.
1922 #define BEFORE_COLON 18
1923 #define BC "18"
1924 int mbedtls_x509_crt_info(char *buf, size_t size, const char *prefix,
1925 const mbedtls_x509_crt *crt) {
1926 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1927 size_t n;
1928 char *p;
1929 char key_size_str[BEFORE_COLON];
1931 p = buf;
1932 n = size;
1934 if (NULL == crt) {
1935 ret = mbedtls_snprintf(p, n, "\nCertificate is uninitialised!\n");
1936 MBEDTLS_X509_SAFE_SNPRINTF;
1938 return ((int)(size - n));
1941 ret = mbedtls_snprintf(p, n, "%scert. version : %d\n",
1942 prefix, crt->version);
1943 MBEDTLS_X509_SAFE_SNPRINTF;
1944 ret = mbedtls_snprintf(p, n, "%sserial number : ",
1945 prefix);
1946 MBEDTLS_X509_SAFE_SNPRINTF;
1948 ret = mbedtls_x509_serial_gets(p, n, &crt->serial);
1949 MBEDTLS_X509_SAFE_SNPRINTF;
1951 ret = mbedtls_snprintf(p, n, "\n%sissuer name : ", prefix);
1952 MBEDTLS_X509_SAFE_SNPRINTF;
1953 ret = mbedtls_x509_dn_gets(p, n, &crt->issuer);
1954 MBEDTLS_X509_SAFE_SNPRINTF;
1956 ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
1957 MBEDTLS_X509_SAFE_SNPRINTF;
1958 ret = mbedtls_x509_dn_gets(p, n, &crt->subject);
1959 MBEDTLS_X509_SAFE_SNPRINTF;
1961 ret = mbedtls_snprintf(p, n, "\n%sissued on : " \
1962 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1963 crt->valid_from.year, crt->valid_from.mon,
1964 crt->valid_from.day, crt->valid_from.hour,
1965 crt->valid_from.min, crt->valid_from.sec);
1966 MBEDTLS_X509_SAFE_SNPRINTF;
1968 ret = mbedtls_snprintf(p, n, "\n%sexpires on : " \
1969 "%04d-%02d-%02d %02d:%02d:%02d", prefix,
1970 crt->valid_to.year, crt->valid_to.mon,
1971 crt->valid_to.day, crt->valid_to.hour,
1972 crt->valid_to.min, crt->valid_to.sec);
1973 MBEDTLS_X509_SAFE_SNPRINTF;
1975 ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
1976 MBEDTLS_X509_SAFE_SNPRINTF;
1978 ret = mbedtls_x509_sig_alg_gets(p, n, &crt->sig_oid, crt->sig_pk,
1979 crt->sig_md, crt->sig_opts);
1980 MBEDTLS_X509_SAFE_SNPRINTF;
1982 /* Key size */
1983 if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
1984 mbedtls_pk_get_name(&crt->pk))) != 0) {
1985 return (ret);
1988 ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
1989 (int) mbedtls_pk_get_bitlen(&crt->pk));
1990 MBEDTLS_X509_SAFE_SNPRINTF;
1993 * Optional extensions
1996 if (crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS) {
1997 ret = mbedtls_snprintf(p, n, "\n%sbasic constraints : CA=%s", prefix,
1998 crt->ca_istrue ? "true" : "false");
1999 MBEDTLS_X509_SAFE_SNPRINTF;
2001 if (crt->max_pathlen > 0) {
2002 ret = mbedtls_snprintf(p, n, ", max_pathlen=%d", crt->max_pathlen - 1);
2003 MBEDTLS_X509_SAFE_SNPRINTF;
2007 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2008 ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
2009 MBEDTLS_X509_SAFE_SNPRINTF;
2011 if ((ret = x509_info_subject_alt_name(&p, &n,
2012 &crt->subject_alt_names,
2013 prefix)) != 0)
2014 return (ret);
2017 if (crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
2018 ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
2019 MBEDTLS_X509_SAFE_SNPRINTF;
2021 if ((ret = x509_info_cert_type(&p, &n, crt->ns_cert_type)) != 0)
2022 return (ret);
2025 if (crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
2026 ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
2027 MBEDTLS_X509_SAFE_SNPRINTF;
2029 if ((ret = x509_info_key_usage(&p, &n, crt->key_usage)) != 0)
2030 return (ret);
2033 if (crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) {
2034 ret = mbedtls_snprintf(p, n, "\n%sext key usage : ", prefix);
2035 MBEDTLS_X509_SAFE_SNPRINTF;
2037 if ((ret = x509_info_ext_key_usage(&p, &n,
2038 &crt->ext_key_usage)) != 0)
2039 return (ret);
2042 if (crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES) {
2043 ret = mbedtls_snprintf(p, n, "\n%scertificate policies : ", prefix);
2044 MBEDTLS_X509_SAFE_SNPRINTF;
2046 if ((ret = x509_info_cert_policies(&p, &n,
2047 &crt->certificate_policies)) != 0)
2048 return (ret);
2051 ret = mbedtls_snprintf(p, n, "\n");
2052 MBEDTLS_X509_SAFE_SNPRINTF;
2054 return ((int)(size - n));
2057 struct x509_crt_verify_string {
2058 int code;
2059 const char *string;
2062 static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
2063 { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
2064 { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
2065 { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
2066 { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
2067 { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
2068 { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
2069 { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
2070 { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
2071 { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
2072 { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
2073 { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
2074 { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
2075 { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
2076 { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
2077 { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
2078 { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2079 { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
2080 { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
2081 { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
2082 { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
2083 { 0, NULL }
2086 int mbedtls_x509_crt_verify_info(char *buf, size_t size, const char *prefix,
2087 uint32_t flags) {
2088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2089 const struct x509_crt_verify_string *cur;
2090 char *p = buf;
2091 size_t n = size;
2093 for (cur = x509_crt_verify_strings; cur->string != NULL ; cur++) {
2094 if ((flags & cur->code) == 0)
2095 continue;
2097 ret = mbedtls_snprintf(p, n, "%s%s\n", prefix, cur->string);
2098 MBEDTLS_X509_SAFE_SNPRINTF;
2099 flags ^= cur->code;
2102 if (flags != 0) {
2103 ret = mbedtls_snprintf(p, n, "%sUnknown reason "
2104 "(this should not happen)\n", prefix);
2105 MBEDTLS_X509_SAFE_SNPRINTF;
2108 return ((int)(size - n));
2111 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
2112 int mbedtls_x509_crt_check_key_usage(const mbedtls_x509_crt *crt,
2113 unsigned int usage) {
2114 unsigned int usage_must, usage_may;
2115 unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
2116 | MBEDTLS_X509_KU_DECIPHER_ONLY;
2118 if ((crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) == 0)
2119 return (0);
2121 usage_must = usage & ~may_mask;
2123 if (((crt->key_usage & ~may_mask) & usage_must) != usage_must)
2124 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
2126 usage_may = usage & may_mask;
2128 if (((crt->key_usage & may_mask) | usage_may) != usage_may)
2129 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
2131 return (0);
2133 #endif
2135 #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
2136 int mbedtls_x509_crt_check_extended_key_usage(const mbedtls_x509_crt *crt,
2137 const char *usage_oid,
2138 size_t usage_len) {
2139 const mbedtls_x509_sequence *cur;
2141 /* Extension is not mandatory, absent means no restriction */
2142 if ((crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE) == 0)
2143 return (0);
2146 * Look for the requested usage (or wildcard ANY) in our list
2148 for (cur = &crt->ext_key_usage; cur != NULL; cur = cur->next) {
2149 const mbedtls_x509_buf *cur_oid = &cur->buf;
2151 if (cur_oid->len == usage_len &&
2152 memcmp(cur_oid->p, usage_oid, usage_len) == 0) {
2153 return (0);
2156 if (MBEDTLS_OID_CMP(MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid) == 0)
2157 return (0);
2160 return (MBEDTLS_ERR_X509_BAD_INPUT_DATA);
2162 #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
2164 #if defined(MBEDTLS_X509_CRL_PARSE_C)
2166 * Return 1 if the certificate is revoked, or 0 otherwise.
2168 int mbedtls_x509_crt_is_revoked(const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl) {
2169 const mbedtls_x509_crl_entry *cur = &crl->entry;
2171 while (cur != NULL && cur->serial.len != 0) {
2172 if (crt->serial.len == cur->serial.len &&
2173 memcmp(crt->serial.p, cur->serial.p, crt->serial.len) == 0) {
2174 return (1);
2177 cur = cur->next;
2180 return (0);
2184 * Check that the given certificate is not revoked according to the CRL.
2185 * Skip validation if no CRL for the given CA is present.
2187 static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
2188 mbedtls_x509_crl *crl_list,
2189 const mbedtls_x509_crt_profile *profile) {
2190 int flags = 0;
2191 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2192 const mbedtls_md_info_t *md_info;
2194 if (ca == NULL)
2195 return (flags);
2197 while (crl_list != NULL) {
2198 if (crl_list->version == 0 ||
2199 x509_name_cmp(&crl_list->issuer, &ca->subject) != 0) {
2200 crl_list = crl_list->next;
2201 continue;
2205 * Check if the CA is configured to sign CRLs
2207 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
2208 if (mbedtls_x509_crt_check_key_usage(ca,
2209 MBEDTLS_X509_KU_CRL_SIGN) != 0) {
2210 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2211 break;
2213 #endif
2216 * Check if CRL is correctly signed by the trusted CA
2218 if (x509_profile_check_md_alg(profile, crl_list->sig_md) != 0)
2219 flags |= MBEDTLS_X509_BADCRL_BAD_MD;
2221 if (x509_profile_check_pk_alg(profile, crl_list->sig_pk) != 0)
2222 flags |= MBEDTLS_X509_BADCRL_BAD_PK;
2224 md_info = mbedtls_md_info_from_type(crl_list->sig_md);
2225 if (mbedtls_md(md_info, crl_list->tbs.p, crl_list->tbs.len, hash) != 0) {
2226 /* Note: this can't happen except after an internal error */
2227 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2228 break;
2231 if (x509_profile_check_key(profile, &ca->pk) != 0)
2232 flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2234 if (mbedtls_pk_verify_ext(crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
2235 crl_list->sig_md, hash, mbedtls_md_get_size(md_info),
2236 crl_list->sig.p, crl_list->sig.len) != 0) {
2237 flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
2238 break;
2242 * Check for validity of CRL (Do not drop out)
2244 if (mbedtls_x509_time_is_past(&crl_list->next_update))
2245 flags |= MBEDTLS_X509_BADCRL_EXPIRED;
2247 if (mbedtls_x509_time_is_future(&crl_list->this_update))
2248 flags |= MBEDTLS_X509_BADCRL_FUTURE;
2251 * Check if certificate is revoked
2253 if (mbedtls_x509_crt_is_revoked(crt, crl_list)) {
2254 flags |= MBEDTLS_X509_BADCERT_REVOKED;
2255 break;
2258 crl_list = crl_list->next;
2261 return (flags);
2263 #endif /* MBEDTLS_X509_CRL_PARSE_C */
2266 * Check the signature of a certificate by its parent
2268 static int x509_crt_check_signature(const mbedtls_x509_crt *child,
2269 mbedtls_x509_crt *parent,
2270 mbedtls_x509_crt_restart_ctx *rs_ctx) {
2271 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
2272 size_t hash_len;
2273 #if !defined(MBEDTLS_USE_PSA_CRYPTO)
2274 const mbedtls_md_info_t *md_info;
2275 md_info = mbedtls_md_info_from_type(child->sig_md);
2276 hash_len = mbedtls_md_get_size(md_info);
2278 /* Note: hash errors can happen only after an internal error */
2279 if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0)
2280 return (-1);
2281 #else
2282 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
2283 psa_algorithm_t hash_alg = mbedtls_psa_translate_md(child->sig_md);
2285 if (psa_hash_setup(&hash_operation, hash_alg) != PSA_SUCCESS)
2286 return (-1);
2288 if (psa_hash_update(&hash_operation, child->tbs.p, child->tbs.len)
2289 != PSA_SUCCESS) {
2290 return (-1);
2293 if (psa_hash_finish(&hash_operation, hash, sizeof(hash), &hash_len)
2294 != PSA_SUCCESS) {
2295 return (-1);
2297 #endif /* MBEDTLS_USE_PSA_CRYPTO */
2298 /* Skip expensive computation on obvious mismatch */
2299 if (! mbedtls_pk_can_do(&parent->pk, child->sig_pk))
2300 return (-1);
2302 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2303 if (rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA) {
2304 return (mbedtls_pk_verify_restartable(&parent->pk,
2305 child->sig_md, hash, hash_len,
2306 child->sig.p, child->sig.len, &rs_ctx->pk));
2308 #else
2309 (void) rs_ctx;
2310 #endif
2312 return (mbedtls_pk_verify_ext(child->sig_pk, child->sig_opts, &parent->pk,
2313 child->sig_md, hash, hash_len,
2314 child->sig.p, child->sig.len));
2318 * Check if 'parent' is a suitable parent (signing CA) for 'child'.
2319 * Return 0 if yes, -1 if not.
2321 * top means parent is a locally-trusted certificate
2323 static int x509_crt_check_parent(const mbedtls_x509_crt *child,
2324 const mbedtls_x509_crt *parent,
2325 int top) {
2326 int need_ca_bit;
2328 /* Parent must be the issuer */
2329 if (x509_name_cmp(&child->issuer, &parent->subject) != 0)
2330 return (-1);
2332 /* Parent must have the basicConstraints CA bit set as a general rule */
2333 need_ca_bit = 1;
2335 /* Exception: v1/v2 certificates that are locally trusted. */
2336 if (top && parent->version < 3)
2337 need_ca_bit = 0;
2339 if (need_ca_bit && ! parent->ca_istrue)
2340 return (-1);
2342 #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
2343 if (need_ca_bit &&
2344 mbedtls_x509_crt_check_key_usage(parent, MBEDTLS_X509_KU_KEY_CERT_SIGN) != 0) {
2345 return (-1);
2347 #endif
2349 return (0);
2353 * Find a suitable parent for child in candidates, or return NULL.
2355 * Here suitable is defined as:
2356 * 1. subject name matches child's issuer
2357 * 2. if necessary, the CA bit is set and key usage allows signing certs
2358 * 3. for trusted roots, the signature is correct
2359 * (for intermediates, the signature is checked and the result reported)
2360 * 4. pathlen constraints are satisfied
2362 * If there's a suitable candidate which is also time-valid, return the first
2363 * such. Otherwise, return the first suitable candidate (or NULL if there is
2364 * none).
2366 * The rationale for this rule is that someone could have a list of trusted
2367 * roots with two versions on the same root with different validity periods.
2368 * (At least one user reported having such a list and wanted it to just work.)
2369 * The reason we don't just require time-validity is that generally there is
2370 * only one version, and if it's expired we want the flags to state that
2371 * rather than NOT_TRUSTED, as would be the case if we required it here.
2373 * The rationale for rule 3 (signature for trusted roots) is that users might
2374 * have two versions of the same CA with different keys in their list, and the
2375 * way we select the correct one is by checking the signature (as we don't
2376 * rely on key identifier extensions). (This is one way users might choose to
2377 * handle key rollover, another relies on self-issued certs, see [SIRO].)
2379 * Arguments:
2380 * - [in] child: certificate for which we're looking for a parent
2381 * - [in] candidates: chained list of potential parents
2382 * - [out] r_parent: parent found (or NULL)
2383 * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
2384 * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
2385 * of the chain, 0 otherwise
2386 * - [in] path_cnt: number of intermediates seen so far
2387 * - [in] self_cnt: number of self-signed intermediates seen so far
2388 * (will never be greater than path_cnt)
2389 * - [in-out] rs_ctx: context for restarting operations
2391 * Return value:
2392 * - 0 on success
2393 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
2395 static int x509_crt_find_parent_in(
2396 mbedtls_x509_crt *child,
2397 mbedtls_x509_crt *candidates,
2398 mbedtls_x509_crt **r_parent,
2399 int *r_signature_is_good,
2400 int top,
2401 unsigned path_cnt,
2402 unsigned self_cnt,
2403 mbedtls_x509_crt_restart_ctx *rs_ctx) {
2404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2405 mbedtls_x509_crt *parent, *fallback_parent;
2406 int signature_is_good = 0, fallback_signature_is_good;
2408 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2409 /* did we have something in progress? */
2410 if (rs_ctx != NULL && rs_ctx->parent != NULL) {
2411 /* restore saved state */
2412 parent = rs_ctx->parent;
2413 fallback_parent = rs_ctx->fallback_parent;
2414 fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
2416 /* clear saved state */
2417 rs_ctx->parent = NULL;
2418 rs_ctx->fallback_parent = NULL;
2419 rs_ctx->fallback_signature_is_good = 0;
2421 /* resume where we left */
2422 goto check_signature;
2424 #endif
2426 fallback_parent = NULL;
2427 fallback_signature_is_good = 0;
2429 for (parent = candidates; parent != NULL; parent = parent->next) {
2430 /* basic parenting skills (name, CA bit, key usage) */
2431 if (x509_crt_check_parent(child, parent, top) != 0)
2432 continue;
2434 /* +1 because stored max_pathlen is 1 higher that the actual value */
2435 if (parent->max_pathlen > 0 &&
2436 (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt) {
2437 continue;
2440 /* Signature */
2441 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2442 check_signature:
2443 #endif
2444 ret = x509_crt_check_signature(child, parent, rs_ctx);
2446 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2447 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2448 /* save state */
2449 rs_ctx->parent = parent;
2450 rs_ctx->fallback_parent = fallback_parent;
2451 rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
2453 return (ret);
2455 #else
2456 (void) ret;
2457 #endif
2459 signature_is_good = ret == 0;
2460 if (top && ! signature_is_good)
2461 continue;
2463 /* optional time check */
2464 if (mbedtls_x509_time_is_past(&parent->valid_to) ||
2465 mbedtls_x509_time_is_future(&parent->valid_from)) {
2466 if (fallback_parent == NULL) {
2467 fallback_parent = parent;
2468 fallback_signature_is_good = signature_is_good;
2471 continue;
2474 *r_parent = parent;
2475 *r_signature_is_good = signature_is_good;
2477 break;
2480 if (parent == NULL) {
2481 *r_parent = fallback_parent;
2482 *r_signature_is_good = fallback_signature_is_good;
2485 return (0);
2489 * Find a parent in trusted CAs or the provided chain, or return NULL.
2491 * Searches in trusted CAs first, and return the first suitable parent found
2492 * (see find_parent_in() for definition of suitable).
2494 * Arguments:
2495 * - [in] child: certificate for which we're looking for a parent, followed
2496 * by a chain of possible intermediates
2497 * - [in] trust_ca: list of locally trusted certificates
2498 * - [out] parent: parent found (or NULL)
2499 * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
2500 * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
2501 * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
2502 * - [in] self_cnt: number of self-signed certs in the chain so far
2503 * (will always be no greater than path_cnt)
2504 * - [in-out] rs_ctx: context for restarting operations
2506 * Return value:
2507 * - 0 on success
2508 * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
2510 static int x509_crt_find_parent(
2511 mbedtls_x509_crt *child,
2512 mbedtls_x509_crt *trust_ca,
2513 mbedtls_x509_crt **parent,
2514 int *parent_is_trusted,
2515 int *signature_is_good,
2516 unsigned path_cnt,
2517 unsigned self_cnt,
2518 mbedtls_x509_crt_restart_ctx *rs_ctx) {
2519 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2520 mbedtls_x509_crt *search_list;
2522 *parent_is_trusted = 1;
2524 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2525 /* restore then clear saved state if we have some stored */
2526 if (rs_ctx != NULL && rs_ctx->parent_is_trusted != -1) {
2527 *parent_is_trusted = rs_ctx->parent_is_trusted;
2528 rs_ctx->parent_is_trusted = -1;
2530 #endif
2532 while (1) {
2533 search_list = *parent_is_trusted ? trust_ca : child->next;
2535 ret = x509_crt_find_parent_in(child, search_list,
2536 parent, signature_is_good,
2537 *parent_is_trusted,
2538 path_cnt, self_cnt, rs_ctx);
2540 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2541 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2542 /* save state */
2543 rs_ctx->parent_is_trusted = *parent_is_trusted;
2544 return (ret);
2546 #else
2547 (void) ret;
2548 #endif
2550 /* stop here if found or already in second iteration */
2551 if (*parent != NULL || *parent_is_trusted == 0)
2552 break;
2554 /* prepare second iteration */
2555 *parent_is_trusted = 0;
2558 /* extra precaution against mistakes in the caller */
2559 if (*parent == NULL) {
2560 *parent_is_trusted = 0;
2561 *signature_is_good = 0;
2564 return (0);
2568 * Check if an end-entity certificate is locally trusted
2570 * Currently we require such certificates to be self-signed (actually only
2571 * check for self-issued as self-signatures are not checked)
2573 static int x509_crt_check_ee_locally_trusted(
2574 mbedtls_x509_crt *crt,
2575 mbedtls_x509_crt *trust_ca) {
2576 mbedtls_x509_crt *cur;
2578 /* must be self-issued */
2579 if (x509_name_cmp(&crt->issuer, &crt->subject) != 0)
2580 return (-1);
2582 /* look for an exact match with trusted cert */
2583 for (cur = trust_ca; cur != NULL; cur = cur->next) {
2584 if (crt->raw.len == cur->raw.len &&
2585 memcmp(crt->raw.p, cur->raw.p, crt->raw.len) == 0) {
2586 return (0);
2590 /* too bad */
2591 return (-1);
2595 * Build and verify a certificate chain
2597 * Given a peer-provided list of certificates EE, C1, ..., Cn and
2598 * a list of trusted certs R1, ... Rp, try to build and verify a chain
2599 * EE, Ci1, ... Ciq [, Rj]
2600 * such that every cert in the chain is a child of the next one,
2601 * jumping to a trusted root as early as possible.
2603 * Verify that chain and return it with flags for all issues found.
2605 * Special cases:
2606 * - EE == Rj -> return a one-element list containing it
2607 * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
2608 * -> return that chain with NOT_TRUSTED set on Ciq
2610 * Tests for (aspects of) this function should include at least:
2611 * - trusted EE
2612 * - EE -> trusted root
2613 * - EE -> intermediate CA -> trusted root
2614 * - if relevant: EE untrusted
2615 * - if relevant: EE -> intermediate, untrusted
2616 * with the aspect under test checked at each relevant level (EE, int, root).
2617 * For some aspects longer chains are required, but usually length 2 is
2618 * enough (but length 1 is not in general).
2620 * Arguments:
2621 * - [in] crt: the cert list EE, C1, ..., Cn
2622 * - [in] trust_ca: the trusted list R1, ..., Rp
2623 * - [in] ca_crl, profile: as in verify_with_profile()
2624 * - [out] ver_chain: the built and verified chain
2625 * Only valid when return value is 0, may contain garbage otherwise!
2626 * Restart note: need not be the same when calling again to resume.
2627 * - [in-out] rs_ctx: context for restarting operations
2629 * Return value:
2630 * - non-zero if the chain could not be fully built and examined
2631 * - 0 is the chain was successfully built and examined,
2632 * even if it was found to be invalid
2634 static int x509_crt_verify_chain(
2635 mbedtls_x509_crt *crt,
2636 mbedtls_x509_crt *trust_ca,
2637 mbedtls_x509_crl *ca_crl,
2638 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2639 void *p_ca_cb,
2640 const mbedtls_x509_crt_profile *profile,
2641 mbedtls_x509_crt_verify_chain *ver_chain,
2642 mbedtls_x509_crt_restart_ctx *rs_ctx) {
2643 /* Don't initialize any of those variables here, so that the compiler can
2644 * catch potential issues with jumping ahead when restarting */
2645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2646 uint32_t *flags;
2647 mbedtls_x509_crt_verify_chain_item *cur;
2648 mbedtls_x509_crt *child;
2649 mbedtls_x509_crt *parent;
2650 int parent_is_trusted;
2651 int child_is_trusted;
2652 int signature_is_good;
2653 unsigned self_cnt;
2654 mbedtls_x509_crt *cur_trust_ca = NULL;
2656 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2657 /* resume if we had an operation in progress */
2658 if (rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent) {
2659 /* restore saved state */
2660 *ver_chain = rs_ctx->ver_chain; /* struct copy */
2661 self_cnt = rs_ctx->self_cnt;
2663 /* restore derived state */
2664 cur = &ver_chain->items[ver_chain->len - 1];
2665 child = cur->crt;
2666 flags = &cur->flags;
2668 goto find_parent;
2670 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
2672 child = crt;
2673 self_cnt = 0;
2674 parent_is_trusted = 0;
2675 child_is_trusted = 0;
2677 while (1) {
2678 /* Add certificate to the verification chain */
2679 cur = &ver_chain->items[ver_chain->len];
2680 cur->crt = child;
2681 cur->flags = 0;
2682 ver_chain->len++;
2683 flags = &cur->flags;
2685 /* Check time-validity (all certificates) */
2686 if (mbedtls_x509_time_is_past(&child->valid_to))
2687 *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
2689 if (mbedtls_x509_time_is_future(&child->valid_from))
2690 *flags |= MBEDTLS_X509_BADCERT_FUTURE;
2692 /* Stop here for trusted roots (but not for trusted EE certs) */
2693 if (child_is_trusted)
2694 return (0);
2696 /* Check signature algorithm: MD & PK algs */
2697 if (x509_profile_check_md_alg(profile, child->sig_md) != 0)
2698 *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
2700 if (x509_profile_check_pk_alg(profile, child->sig_pk) != 0)
2701 *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2703 /* Special case: EE certs that are locally trusted */
2704 if (ver_chain->len == 1 &&
2705 x509_crt_check_ee_locally_trusted(child, trust_ca) == 0) {
2706 return (0);
2709 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2710 find_parent:
2711 #endif
2713 /* Obtain list of potential trusted signers from CA callback,
2714 * or use statically provided list. */
2715 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2716 if (f_ca_cb != NULL) {
2717 mbedtls_x509_crt_free(ver_chain->trust_ca_cb_result);
2718 mbedtls_free(ver_chain->trust_ca_cb_result);
2719 ver_chain->trust_ca_cb_result = NULL;
2721 ret = f_ca_cb(p_ca_cb, child, &ver_chain->trust_ca_cb_result);
2722 if (ret != 0)
2723 return (MBEDTLS_ERR_X509_FATAL_ERROR);
2725 cur_trust_ca = ver_chain->trust_ca_cb_result;
2726 } else
2727 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2729 ((void) f_ca_cb);
2730 ((void) p_ca_cb);
2731 cur_trust_ca = trust_ca;
2734 /* Look for a parent in trusted CAs or up the chain */
2735 ret = x509_crt_find_parent(child, cur_trust_ca, &parent,
2736 &parent_is_trusted, &signature_is_good,
2737 ver_chain->len - 1, self_cnt, rs_ctx);
2739 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2740 if (rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
2741 /* save state */
2742 rs_ctx->in_progress = x509_crt_rs_find_parent;
2743 rs_ctx->self_cnt = self_cnt;
2744 rs_ctx->ver_chain = *ver_chain; /* struct copy */
2746 return (ret);
2748 #else
2749 (void) ret;
2750 #endif
2752 /* No parent? We're done here */
2753 if (parent == NULL) {
2754 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2755 return (0);
2758 /* Count intermediate self-issued (not necessarily self-signed) certs.
2759 * These can occur with some strategies for key rollover, see [SIRO],
2760 * and should be excluded from max_pathlen checks. */
2761 if (ver_chain->len != 1 &&
2762 x509_name_cmp(&child->issuer, &child->subject) == 0) {
2763 self_cnt++;
2766 /* path_cnt is 0 for the first intermediate CA,
2767 * and if parent is trusted it's not an intermediate CA */
2768 if (! parent_is_trusted &&
2769 ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA) {
2770 /* return immediately to avoid overflow the chain array */
2771 return (MBEDTLS_ERR_X509_FATAL_ERROR);
2774 /* signature was checked while searching parent */
2775 if (! signature_is_good)
2776 *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
2778 /* check size of signing key */
2779 if (x509_profile_check_key(profile, &parent->pk) != 0)
2780 *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2782 #if defined(MBEDTLS_X509_CRL_PARSE_C)
2783 /* Check trusted CA's CRL for the given crt */
2784 *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile);
2785 #else
2786 (void) ca_crl;
2787 #endif
2789 /* prepare for next iteration */
2790 child = parent;
2791 parent = NULL;
2792 child_is_trusted = parent_is_trusted;
2793 signature_is_good = 0;
2798 * Check for CN match
2800 static int x509_crt_check_cn(const mbedtls_x509_buf *name,
2801 const char *cn, size_t cn_len) {
2802 /* try exact match */
2803 if (name->len == cn_len &&
2804 x509_memcasecmp(cn, name->p, cn_len) == 0) {
2805 return (0);
2808 /* try wildcard match */
2809 if (x509_check_wildcard(cn, name) == 0) {
2810 return (0);
2813 return (-1);
2817 * Check for SAN match, see RFC 5280 Section 4.2.1.6
2819 static int x509_crt_check_san(const mbedtls_x509_buf *name,
2820 const char *cn, size_t cn_len) {
2821 const unsigned char san_type = (unsigned char) name->tag &
2822 MBEDTLS_ASN1_TAG_VALUE_MASK;
2824 /* dNSName */
2825 if (san_type == MBEDTLS_X509_SAN_DNS_NAME)
2826 return (x509_crt_check_cn(name, cn, cn_len));
2828 /* (We may handle other types here later.) */
2830 /* Unrecognized type */
2831 return (-1);
2835 * Verify the requested CN - only call this if cn is not NULL!
2837 static void x509_crt_verify_name(const mbedtls_x509_crt *crt,
2838 const char *cn,
2839 uint32_t *flags) {
2840 const mbedtls_x509_name *name;
2841 const mbedtls_x509_sequence *cur;
2842 size_t cn_len = strlen(cn);
2844 if (crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
2845 for (cur = &crt->subject_alt_names; cur != NULL; cur = cur->next) {
2846 if (x509_crt_check_san(&cur->buf, cn, cn_len) == 0)
2847 break;
2850 if (cur == NULL)
2851 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2852 } else {
2853 for (name = &crt->subject; name != NULL; name = name->next) {
2854 if (MBEDTLS_OID_CMP(MBEDTLS_OID_AT_CN, &name->oid) == 0 &&
2855 x509_crt_check_cn(&name->val, cn, cn_len) == 0) {
2856 break;
2860 if (name == NULL)
2861 *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
2866 * Merge the flags for all certs in the chain, after calling callback
2868 static int x509_crt_merge_flags_with_cb(
2869 uint32_t *flags,
2870 const mbedtls_x509_crt_verify_chain *ver_chain,
2871 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2872 void *p_vrfy) {
2873 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2874 unsigned i;
2875 uint32_t cur_flags;
2876 const mbedtls_x509_crt_verify_chain_item *cur;
2878 for (i = ver_chain->len; i != 0; --i) {
2879 cur = &ver_chain->items[i - 1];
2880 cur_flags = cur->flags;
2882 if (NULL != f_vrfy)
2883 if ((ret = f_vrfy(p_vrfy, cur->crt, (int) i - 1, &cur_flags)) != 0)
2884 return (ret);
2886 *flags |= cur_flags;
2889 return (0);
2893 * Verify the certificate validity, with profile, restartable version
2895 * This function:
2896 * - checks the requested CN (if any)
2897 * - checks the type and size of the EE cert's key,
2898 * as that isn't done as part of chain building/verification currently
2899 * - builds and verifies the chain
2900 * - then calls the callback and merges the flags
2902 * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
2903 * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
2904 * verification routine to search for trusted signers, and CRLs will
2905 * be disabled. Otherwise, `trust_ca` will be used as the static list
2906 * of trusted signers, and `ca_crl` will be use as the static list
2907 * of CRLs.
2909 static int x509_crt_verify_restartable_ca_cb(mbedtls_x509_crt *crt,
2910 mbedtls_x509_crt *trust_ca,
2911 mbedtls_x509_crl *ca_crl,
2912 mbedtls_x509_crt_ca_cb_t f_ca_cb,
2913 void *p_ca_cb,
2914 const mbedtls_x509_crt_profile *profile,
2915 const char *cn, uint32_t *flags,
2916 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2917 void *p_vrfy,
2918 mbedtls_x509_crt_restart_ctx *rs_ctx) {
2919 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2920 mbedtls_pk_type_t pk_type;
2921 mbedtls_x509_crt_verify_chain ver_chain;
2922 uint32_t ee_flags;
2924 *flags = 0;
2925 ee_flags = 0;
2926 x509_crt_verify_chain_reset(&ver_chain);
2928 if (profile == NULL) {
2929 ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
2930 goto exit;
2933 /* check name if requested */
2934 if (cn != NULL)
2935 x509_crt_verify_name(crt, cn, &ee_flags);
2937 /* Check the type and size of the key */
2938 pk_type = mbedtls_pk_get_type(&crt->pk);
2940 if (x509_profile_check_pk_alg(profile, pk_type) != 0)
2941 ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
2943 if (x509_profile_check_key(profile, &crt->pk) != 0)
2944 ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
2946 /* Check the chain */
2947 ret = x509_crt_verify_chain(crt, trust_ca, ca_crl,
2948 f_ca_cb, p_ca_cb, profile,
2949 &ver_chain, rs_ctx);
2951 if (ret != 0)
2952 goto exit;
2954 /* Merge end-entity flags */
2955 ver_chain.items[0].flags |= ee_flags;
2957 /* Build final flags, calling callback on the way if any */
2958 ret = x509_crt_merge_flags_with_cb(flags, &ver_chain, f_vrfy, p_vrfy);
2960 exit:
2962 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
2963 mbedtls_x509_crt_free(ver_chain.trust_ca_cb_result);
2964 mbedtls_free(ver_chain.trust_ca_cb_result);
2965 ver_chain.trust_ca_cb_result = NULL;
2966 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
2968 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
2969 if (rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS)
2970 mbedtls_x509_crt_restart_free(rs_ctx);
2971 #endif
2973 /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
2974 * the SSL module for authmode optional, but non-zero return from the
2975 * callback means a fatal error so it shouldn't be ignored */
2976 if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED)
2977 ret = MBEDTLS_ERR_X509_FATAL_ERROR;
2979 if (ret != 0) {
2980 *flags = (uint32_t) -1;
2981 return (ret);
2984 if (*flags != 0)
2985 return (MBEDTLS_ERR_X509_CERT_VERIFY_FAILED);
2987 return (0);
2992 * Verify the certificate validity (default profile, not restartable)
2994 int mbedtls_x509_crt_verify(mbedtls_x509_crt *crt,
2995 mbedtls_x509_crt *trust_ca,
2996 mbedtls_x509_crl *ca_crl,
2997 const char *cn, uint32_t *flags,
2998 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
2999 void *p_vrfy) {
3000 return (x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3001 NULL, NULL,
3002 &mbedtls_x509_crt_profile_default,
3003 cn, flags,
3004 f_vrfy, p_vrfy, NULL));
3008 * Verify the certificate validity (user-chosen profile, not restartable)
3010 int mbedtls_x509_crt_verify_with_profile(mbedtls_x509_crt *crt,
3011 mbedtls_x509_crt *trust_ca,
3012 mbedtls_x509_crl *ca_crl,
3013 const mbedtls_x509_crt_profile *profile,
3014 const char *cn, uint32_t *flags,
3015 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3016 void *p_vrfy) {
3017 return (x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3018 NULL, NULL,
3019 profile, cn, flags,
3020 f_vrfy, p_vrfy, NULL));
3023 #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
3025 * Verify the certificate validity (user-chosen profile, CA callback,
3026 * not restartable).
3028 int mbedtls_x509_crt_verify_with_ca_cb(mbedtls_x509_crt *crt,
3029 mbedtls_x509_crt_ca_cb_t f_ca_cb,
3030 void *p_ca_cb,
3031 const mbedtls_x509_crt_profile *profile,
3032 const char *cn, uint32_t *flags,
3033 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3034 void *p_vrfy) {
3035 return (x509_crt_verify_restartable_ca_cb(crt, NULL, NULL,
3036 f_ca_cb, p_ca_cb,
3037 profile, cn, flags,
3038 f_vrfy, p_vrfy, NULL));
3040 #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
3042 int mbedtls_x509_crt_verify_restartable(mbedtls_x509_crt *crt,
3043 mbedtls_x509_crt *trust_ca,
3044 mbedtls_x509_crl *ca_crl,
3045 const mbedtls_x509_crt_profile *profile,
3046 const char *cn, uint32_t *flags,
3047 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
3048 void *p_vrfy,
3049 mbedtls_x509_crt_restart_ctx *rs_ctx) {
3050 return (x509_crt_verify_restartable_ca_cb(crt, trust_ca, ca_crl,
3051 NULL, NULL,
3052 profile, cn, flags,
3053 f_vrfy, p_vrfy, rs_ctx));
3058 * Initialize a certificate chain
3060 void mbedtls_x509_crt_init(mbedtls_x509_crt *crt) {
3061 memset(crt, 0, sizeof(mbedtls_x509_crt));
3065 * Unallocate all certificate data
3067 void mbedtls_x509_crt_free(mbedtls_x509_crt *crt) {
3068 mbedtls_x509_crt *cert_cur = crt;
3069 mbedtls_x509_crt *cert_prv;
3070 mbedtls_x509_name *name_cur;
3071 mbedtls_x509_name *name_prv;
3072 mbedtls_x509_sequence *seq_cur;
3073 mbedtls_x509_sequence *seq_prv;
3075 if (crt == NULL)
3076 return;
3078 do {
3079 mbedtls_pk_free(&cert_cur->pk);
3081 #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
3082 mbedtls_free(cert_cur->sig_opts);
3083 #endif
3085 name_cur = cert_cur->issuer.next;
3086 while (name_cur != NULL) {
3087 name_prv = name_cur;
3088 name_cur = name_cur->next;
3089 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3090 mbedtls_free(name_prv);
3093 name_cur = cert_cur->subject.next;
3094 while (name_cur != NULL) {
3095 name_prv = name_cur;
3096 name_cur = name_cur->next;
3097 mbedtls_platform_zeroize(name_prv, sizeof(mbedtls_x509_name));
3098 mbedtls_free(name_prv);
3101 seq_cur = cert_cur->ext_key_usage.next;
3102 while (seq_cur != NULL) {
3103 seq_prv = seq_cur;
3104 seq_cur = seq_cur->next;
3105 mbedtls_platform_zeroize(seq_prv,
3106 sizeof(mbedtls_x509_sequence));
3107 mbedtls_free(seq_prv);
3110 seq_cur = cert_cur->subject_alt_names.next;
3111 while (seq_cur != NULL) {
3112 seq_prv = seq_cur;
3113 seq_cur = seq_cur->next;
3114 mbedtls_platform_zeroize(seq_prv,
3115 sizeof(mbedtls_x509_sequence));
3116 mbedtls_free(seq_prv);
3119 seq_cur = cert_cur->certificate_policies.next;
3120 while (seq_cur != NULL) {
3121 seq_prv = seq_cur;
3122 seq_cur = seq_cur->next;
3123 mbedtls_platform_zeroize(seq_prv,
3124 sizeof(mbedtls_x509_sequence));
3125 mbedtls_free(seq_prv);
3128 if (cert_cur->raw.p != NULL && cert_cur->own_buffer) {
3129 mbedtls_platform_zeroize(cert_cur->raw.p, cert_cur->raw.len);
3130 mbedtls_free(cert_cur->raw.p);
3133 cert_cur = cert_cur->next;
3134 } while (cert_cur != NULL);
3136 cert_cur = crt;
3137 do {
3138 cert_prv = cert_cur;
3139 cert_cur = cert_cur->next;
3141 mbedtls_platform_zeroize(cert_prv, sizeof(mbedtls_x509_crt));
3142 if (cert_prv != crt)
3143 mbedtls_free(cert_prv);
3144 } while (cert_cur != NULL);
3147 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
3149 * Initialize a restart context
3151 void mbedtls_x509_crt_restart_init(mbedtls_x509_crt_restart_ctx *ctx) {
3152 mbedtls_pk_restart_init(&ctx->pk);
3154 ctx->parent = NULL;
3155 ctx->fallback_parent = NULL;
3156 ctx->fallback_signature_is_good = 0;
3158 ctx->parent_is_trusted = -1;
3160 ctx->in_progress = x509_crt_rs_none;
3161 ctx->self_cnt = 0;
3162 x509_crt_verify_chain_reset(&ctx->ver_chain);
3166 * Free the components of a restart context
3168 void mbedtls_x509_crt_restart_free(mbedtls_x509_crt_restart_ctx *ctx) {
3169 if (ctx == NULL)
3170 return;
3172 mbedtls_pk_restart_free(&ctx->pk);
3173 mbedtls_x509_crt_restart_init(ctx);
3175 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
3177 #endif /* MBEDTLS_X509_CRT_PARSE_C */