1 /* $OpenBSD: x509_verify.c,v 1.13.4.1 2021/02/03 07:06:13 tb Exp $ */
3 * Copyright (c) 2020 Bob Beck <beck@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* x509_verify - inspired by golang's crypto/x509/Verify */
26 #include <openssl/safestack.h>
27 #include <openssl/x509.h>
28 #include <openssl/x509v3.h>
30 #include "x509_internal.h"
31 #include "x509_issuer_cache.h"
33 static int x509_verify_cert_valid(struct x509_verify_ctx *ctx, X509 *cert,
34 struct x509_verify_chain *current_chain);
35 static void x509_verify_build_chains(struct x509_verify_ctx *ctx, X509 *cert,
36 struct x509_verify_chain *current_chain);
37 static int x509_verify_cert_error(struct x509_verify_ctx *ctx, X509 *cert,
38 size_t depth, int error, int ok);
39 static void x509_verify_chain_free(struct x509_verify_chain *chain);
41 #define X509_VERIFY_CERT_HASH (EVP_sha512())
43 struct x509_verify_chain *
44 x509_verify_chain_new(void)
46 struct x509_verify_chain *chain;
48 if ((chain = calloc(1, sizeof(*chain))) == NULL)
50 if ((chain->certs = sk_X509_new_null()) == NULL)
52 if ((chain->names = x509_constraints_names_new()) == NULL)
57 x509_verify_chain_free(chain);
62 x509_verify_chain_clear(struct x509_verify_chain *chain)
64 sk_X509_pop_free(chain->certs, X509_free);
66 x509_constraints_names_free(chain->names);
71 x509_verify_chain_free(struct x509_verify_chain *chain)
75 x509_verify_chain_clear(chain);
79 static struct x509_verify_chain *
80 x509_verify_chain_dup(struct x509_verify_chain *chain)
82 struct x509_verify_chain *new_chain;
84 if ((new_chain = calloc(1, sizeof(*chain))) == NULL)
86 if ((new_chain->certs = X509_chain_up_ref(chain->certs)) == NULL)
88 if ((new_chain->names =
89 x509_constraints_names_dup(chain->names)) == NULL)
93 x509_verify_chain_free(new_chain);
98 x509_verify_chain_append(struct x509_verify_chain *chain, X509 *cert,
101 int verify_err = X509_V_ERR_UNSPECIFIED;
103 if (!x509_constraints_extract_names(chain->names, cert,
104 sk_X509_num(chain->certs) == 0, &verify_err)) {
109 if (!sk_X509_push(chain->certs, cert)) {
111 *error = X509_V_ERR_OUT_OF_MEM;
118 x509_verify_chain_last(struct x509_verify_chain *chain)
122 if (chain->certs == NULL)
124 if ((last = sk_X509_num(chain->certs) - 1) < 0)
126 return sk_X509_value(chain->certs, last);
130 x509_verify_chain_leaf(struct x509_verify_chain *chain)
132 if (chain->certs == NULL)
134 return sk_X509_value(chain->certs, 0);
138 x509_verify_ctx_reset(struct x509_verify_ctx *ctx)
142 for (i = 0; i < ctx->chains_count; i++)
143 x509_verify_chain_free(ctx->chains[i]);
145 ctx->error_depth = 0;
146 ctx->chains_count = 0;
148 ctx->check_time = NULL;
152 x509_verify_ctx_clear(struct x509_verify_ctx *ctx)
154 x509_verify_ctx_reset(ctx);
155 sk_X509_pop_free(ctx->intermediates, X509_free);
157 memset(ctx, 0, sizeof(*ctx));
161 x509_verify_ctx_cert_is_root(struct x509_verify_ctx *ctx, X509 *cert)
165 for (i = 0; i < sk_X509_num(ctx->roots); i++) {
166 if (X509_cmp(sk_X509_value(ctx->roots, i), cert) == 0)
173 x509_verify_ctx_set_xsc_chain(struct x509_verify_ctx *ctx,
174 struct x509_verify_chain *chain)
177 X509 *last = x509_verify_chain_last(chain);
179 if (ctx->xsc == NULL)
182 depth = sk_X509_num(chain->certs);
186 ctx->xsc->last_untrusted = depth ? depth - 1 : 0;
187 sk_X509_pop_free(ctx->xsc->chain, X509_free);
188 ctx->xsc->chain = X509_chain_up_ref(chain->certs);
189 if (ctx->xsc->chain == NULL)
190 return x509_verify_cert_error(ctx, last, depth,
191 X509_V_ERR_OUT_OF_MEM, 0);
195 /* Add a validated chain to our list of valid chains */
197 x509_verify_ctx_add_chain(struct x509_verify_ctx *ctx,
198 struct x509_verify_chain *chain)
201 X509 *last = x509_verify_chain_last(chain);
203 depth = sk_X509_num(chain->certs);
207 if (ctx->chains_count >= ctx->max_chains)
208 return x509_verify_cert_error(ctx, last, depth,
209 X509_V_ERR_CERT_CHAIN_TOO_LONG, 0);
212 * If we have a legacy xsc, choose a validated chain,
213 * and apply the extensions, revocation, and policy checks
214 * just like the legacy code did. We do this here instead
215 * of as building the chains to more easily support the
216 * callback and the bewildering array of VERIFY_PARAM
217 * knobs that are there for the fiddling.
219 if (ctx->xsc != NULL) {
220 if (!x509_verify_ctx_set_xsc_chain(ctx, chain))
224 * XXX currently this duplicates some work done
225 * in chain build, but we keep it here until
226 * we have feature parity
228 if (!x509_vfy_check_chain_extensions(ctx->xsc))
231 if (!x509_constraints_chain(ctx->xsc->chain,
232 &ctx->xsc->error, &ctx->xsc->error_depth)) {
233 X509 *cert = sk_X509_value(ctx->xsc->chain, depth);
234 if (!x509_verify_cert_error(ctx, cert,
235 ctx->xsc->error_depth, ctx->xsc->error, 0))
239 if (!x509_vfy_check_revocation(ctx->xsc))
242 if (!x509_vfy_check_policy(ctx->xsc))
246 * no xsc means we are being called from the non-legacy API,
247 * extensions and purpose are dealt with as the chain is built.
249 * The non-legacy api returns multiple chains but does not do
250 * any revocation checking (it must be done by the caller on
251 * any chain they wish to use)
254 if ((ctx->chains[ctx->chains_count] = x509_verify_chain_dup(chain)) ==
256 return x509_verify_cert_error(ctx, last, depth,
257 X509_V_ERR_OUT_OF_MEM, 0);
260 ctx->error = X509_V_OK;
261 ctx->error_depth = depth;
266 x509_verify_potential_parent(struct x509_verify_ctx *ctx, X509 *parent,
269 if (ctx->xsc != NULL)
270 return (ctx->xsc->check_issued(ctx->xsc, child, parent));
273 return X509_check_issued(child, parent) != X509_V_OK;
277 x509_verify_parent_signature(X509 *parent, X509 *child,
278 unsigned char *child_md, int *error)
280 unsigned char parent_md[EVP_MAX_MD_SIZE] = { 0 };
285 /* Use cached value if we have it */
286 if (child_md != NULL) {
287 if (!X509_digest(parent, X509_VERIFY_CERT_HASH, parent_md,
290 if ((cached = x509_issuer_cache_find(parent_md, child_md)) >= 0)
294 /* Check signature. Did parent sign child? */
295 if ((pkey = X509_get_pubkey(parent)) == NULL) {
296 *error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
299 if (X509_verify(child, pkey) <= 0)
300 *error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
304 /* Add result to cache */
305 if (child_md != NULL)
306 x509_issuer_cache_add(parent_md, child_md, ret);
314 x509_verify_consider_candidate(struct x509_verify_ctx *ctx, X509 *cert,
315 unsigned char *cert_md, int is_root_cert, X509 *candidate,
316 struct x509_verify_chain *current_chain)
318 int depth = sk_X509_num(current_chain->certs);
319 struct x509_verify_chain *new_chain;
322 /* Fail if the certificate is already in the chain */
323 for (i = 0; i < sk_X509_num(current_chain->certs); i++) {
324 if (X509_cmp(sk_X509_value(current_chain->certs, i),
329 if (ctx->sig_checks++ > X509_VERIFY_MAX_SIGCHECKS) {
330 /* don't allow callback to override safety check */
331 (void) x509_verify_cert_error(ctx, candidate, depth,
332 X509_V_ERR_CERT_CHAIN_TOO_LONG, 0);
337 if (!x509_verify_parent_signature(candidate, cert, cert_md,
339 if (!x509_verify_cert_error(ctx, candidate, depth,
344 if (!x509_verify_cert_valid(ctx, candidate, current_chain))
347 /* candidate is good, add it to a copy of the current chain */
348 if ((new_chain = x509_verify_chain_dup(current_chain)) == NULL) {
349 x509_verify_cert_error(ctx, candidate, depth,
350 X509_V_ERR_OUT_OF_MEM, 0);
353 if (!x509_verify_chain_append(new_chain, candidate, &ctx->error)) {
354 x509_verify_cert_error(ctx, candidate, depth,
356 x509_verify_chain_free(new_chain);
361 * If candidate is a trusted root, we have a validated chain,
362 * so we save it. Otherwise, recurse until we find a root or
366 if (!x509_verify_ctx_set_xsc_chain(ctx, new_chain)) {
367 x509_verify_chain_free(new_chain);
370 if (x509_verify_cert_error(ctx, candidate, depth, X509_V_OK, 1)) {
371 (void) x509_verify_ctx_add_chain(ctx, new_chain);
376 x509_verify_build_chains(ctx, candidate, new_chain);
379 x509_verify_chain_free(new_chain);
384 x509_verify_cert_error(struct x509_verify_ctx *ctx, X509 *cert, size_t depth,
388 ctx->error_depth = depth;
389 if (ctx->xsc != NULL) {
390 ctx->xsc->error = error;
391 ctx->xsc->error_depth = depth;
392 ctx->xsc->current_cert = cert;
393 return ctx->xsc->verify_cb(ok, ctx->xsc);
399 x509_verify_build_chains(struct x509_verify_ctx *ctx, X509 *cert,
400 struct x509_verify_chain *current_chain)
402 unsigned char cert_md[EVP_MAX_MD_SIZE] = { 0 };
406 depth = sk_X509_num(current_chain->certs);
410 if (depth >= ctx->max_depth &&
411 !x509_verify_cert_error(ctx, cert, depth,
412 X509_V_ERR_CERT_CHAIN_TOO_LONG, 0))
415 if (!X509_digest(cert, X509_VERIFY_CERT_HASH, cert_md, NULL) &&
416 !x509_verify_cert_error(ctx, cert, depth,
417 X509_V_ERR_UNSPECIFIED, 0))
420 count = ctx->chains_count;
421 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
422 ctx->error_depth = depth;
424 for (i = 0; i < sk_X509_num(ctx->roots); i++) {
425 candidate = sk_X509_value(ctx->roots, i);
426 if (x509_verify_potential_parent(ctx, candidate, cert)) {
427 x509_verify_consider_candidate(ctx, cert,
428 cert_md, 1, candidate, current_chain);
432 if (ctx->intermediates != NULL) {
433 for (i = 0; i < sk_X509_num(ctx->intermediates); i++) {
434 candidate = sk_X509_value(ctx->intermediates, i);
435 if (x509_verify_potential_parent(ctx, candidate, cert)) {
436 x509_verify_consider_candidate(ctx, cert,
437 cert_md, 0, candidate, current_chain);
441 if (ctx->chains_count > count) {
442 if (ctx->xsc != NULL) {
443 ctx->xsc->error = X509_V_OK;
444 ctx->xsc->error_depth = depth;
445 ctx->xsc->current_cert = cert;
446 (void) ctx->xsc->verify_cb(1, ctx->xsc);
448 } else if (ctx->error_depth == depth) {
449 (void) x509_verify_cert_error(ctx, cert, depth,
455 x509_verify_cert_hostname(struct x509_verify_ctx *ctx, X509 *cert, char *name)
461 if (ctx->xsc != NULL)
462 return x509_vfy_check_id(ctx->xsc);
465 if ((candidate = strdup(name)) == NULL) {
466 ctx->error = X509_V_ERR_OUT_OF_MEM;
469 if ((len = strlen(candidate)) < 1) {
470 ctx->error = X509_V_ERR_UNSPECIFIED; /* XXX */
474 /* IP addresses may be written in [ ]. */
475 if (candidate[0] == '[' && candidate[len - 1] == ']') {
476 candidate[len - 1] = '\0';
477 if (X509_check_ip_asc(cert, candidate + 1, 0) <= 0) {
478 ctx->error = X509_V_ERR_IP_ADDRESS_MISMATCH;
484 if (ctx->xsc == NULL)
485 flags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
487 if (X509_check_host(cert, candidate, len, flags, NULL) <= 0) {
488 ctx->error = X509_V_ERR_HOSTNAME_MISMATCH;
496 return x509_verify_cert_error(ctx, cert, 0, ctx->error, 0);
500 x509_verify_set_check_time(struct x509_verify_ctx *ctx) {
501 if (ctx->xsc != NULL) {
502 if (ctx->xsc->param->flags & X509_V_FLAG_USE_CHECK_TIME) {
503 ctx->check_time = &ctx->xsc->param->check_time;
506 if (ctx->xsc->param->flags & X509_V_FLAG_NO_CHECK_TIME)
510 ctx->check_time = NULL;
515 x509_verify_asn1_time_to_tm(const ASN1_TIME *atime, struct tm *tm, int notafter)
519 memset(tm, 0, sizeof(*tm));
521 type = ASN1_time_parse(atime->data, atime->length, tm, atime->type);
525 /* RFC 5280 section 4.1.2.5 */
526 if (tm->tm_year < 150 && type != V_ASN1_UTCTIME)
528 if (tm->tm_year >= 150 && type != V_ASN1_GENERALIZEDTIME)
533 * If we are a completely broken operating system with a
534 * 32 bit time_t, and we have been told this is a notafter
535 * date, limit the date to a 32 bit representable value.
537 if (!ASN1_time_tm_clamp_notafter(tm))
542 * Defensively fail if the time string is not representable as
543 * a time_t. A time_t must be sane if you care about times after
546 if (timegm(tm) == -1)
553 x509_verify_cert_time(int is_notafter, const ASN1_TIME *cert_asn1,
554 time_t *cmp_time, int *error)
556 struct tm cert_tm, when_tm;
559 if (cmp_time == NULL)
564 if (!x509_verify_asn1_time_to_tm(cert_asn1, &cert_tm,
566 *error = is_notafter ?
567 X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD :
568 X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
572 if (gmtime_r(&when, &when_tm) == NULL) {
573 *error = X509_V_ERR_UNSPECIFIED;
578 if (ASN1_time_tm_cmp(&cert_tm, &when_tm) == -1) {
579 *error = X509_V_ERR_CERT_HAS_EXPIRED;
583 if (ASN1_time_tm_cmp(&cert_tm, &when_tm) == 1) {
584 *error = X509_V_ERR_CERT_NOT_YET_VALID;
593 x509_verify_validate_constraints(X509 *cert,
594 struct x509_verify_chain *current_chain, int *error)
596 struct x509_constraints_names *excluded = NULL;
597 struct x509_constraints_names *permitted = NULL;
598 int err = X509_V_ERR_UNSPECIFIED;
600 if (current_chain == NULL)
603 if (cert->nc != NULL) {
604 if ((permitted = x509_constraints_names_new()) == NULL) {
605 err = X509_V_ERR_OUT_OF_MEM;
608 if ((excluded = x509_constraints_names_new()) == NULL) {
609 err = X509_V_ERR_OUT_OF_MEM;
612 if (!x509_constraints_extract_constraints(cert,
613 permitted, excluded, &err))
615 if (!x509_constraints_check(current_chain->names,
616 permitted, excluded, &err))
618 x509_constraints_names_free(excluded);
619 x509_constraints_names_free(permitted);
625 x509_constraints_names_free(excluded);
626 x509_constraints_names_free(permitted);
631 x509_verify_cert_extensions(struct x509_verify_ctx *ctx, X509 *cert, int need_ca)
633 if (!(cert->ex_flags & EXFLAG_SET)) {
634 CRYPTO_w_lock(CRYPTO_LOCK_X509);
635 x509v3_cache_extensions(cert);
636 CRYPTO_w_unlock(CRYPTO_LOCK_X509);
639 if (ctx->xsc != NULL)
640 return 1; /* legacy is checked after chain is built */
642 if (cert->ex_flags & EXFLAG_CRITICAL) {
643 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
646 /* No we don't care about v1, netscape, and other ancient silliness */
647 if (need_ca && (!(cert->ex_flags & EXFLAG_BCONS) &&
648 (cert->ex_flags & EXFLAG_CA))) {
649 ctx->error = X509_V_ERR_INVALID_CA;
652 if (ctx->purpose > 0 && X509_check_purpose(cert, ctx->purpose, need_ca)) {
653 ctx->error = X509_V_ERR_INVALID_PURPOSE;
657 /* XXX support proxy certs later in new api */
658 if (ctx->xsc == NULL && cert->ex_flags & EXFLAG_PROXY) {
659 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
666 /* Validate that cert is a possible candidate to append to current_chain */
668 x509_verify_cert_valid(struct x509_verify_ctx *ctx, X509 *cert,
669 struct x509_verify_chain *current_chain)
671 X509 *issuer_candidate;
672 int should_be_ca = current_chain != NULL;
675 if (current_chain != NULL)
676 depth = sk_X509_num(current_chain->certs);
678 if (!x509_verify_cert_extensions(ctx, cert, should_be_ca))
682 issuer_candidate = x509_verify_chain_last(current_chain);
683 if (issuer_candidate != NULL &&
684 !X509_check_issued(issuer_candidate, cert))
685 if (!x509_verify_cert_error(ctx, cert, depth,
686 X509_V_ERR_SUBJECT_ISSUER_MISMATCH, 0))
690 if (x509_verify_set_check_time(ctx)) {
691 if (!x509_verify_cert_time(0, X509_get_notBefore(cert),
692 ctx->check_time, &ctx->error)) {
693 if (!x509_verify_cert_error(ctx, cert, depth,
698 if (!x509_verify_cert_time(1, X509_get_notAfter(cert),
699 ctx->check_time, &ctx->error)) {
700 if (!x509_verify_cert_error(ctx, cert, depth,
706 if (!x509_verify_validate_constraints(cert, current_chain,
707 &ctx->error) && !x509_verify_cert_error(ctx, cert, depth,
714 struct x509_verify_ctx *
715 x509_verify_ctx_new_from_xsc(X509_STORE_CTX *xsc, STACK_OF(X509) *roots)
717 struct x509_verify_ctx *ctx;
723 if ((ctx = x509_verify_ctx_new(roots)) == NULL)
728 if (xsc->untrusted &&
729 (ctx->intermediates = X509_chain_up_ref(xsc->untrusted)) == NULL)
732 max_depth = X509_VERIFY_MAX_CHAIN_CERTS;
733 if (xsc->param->depth > 0 && xsc->param->depth < X509_VERIFY_MAX_CHAIN_CERTS)
734 max_depth = xsc->param->depth;
735 if (!x509_verify_ctx_set_max_depth(ctx, max_depth))
740 x509_verify_ctx_free(ctx);
746 struct x509_verify_ctx *
747 x509_verify_ctx_new(STACK_OF(X509) *roots)
749 struct x509_verify_ctx *ctx;
754 if ((ctx = calloc(1, sizeof(struct x509_verify_ctx))) == NULL)
757 if ((ctx->roots = X509_chain_up_ref(roots)) == NULL)
760 ctx->max_depth = X509_VERIFY_MAX_CHAIN_CERTS;
761 ctx->max_chains = X509_VERIFY_MAX_CHAINS;
762 ctx->max_sigs = X509_VERIFY_MAX_SIGCHECKS;
764 if ((ctx->chains = calloc(X509_VERIFY_MAX_CHAINS,
765 sizeof(*ctx->chains))) == NULL)
770 x509_verify_ctx_free(ctx);
775 x509_verify_ctx_free(struct x509_verify_ctx *ctx)
779 sk_X509_pop_free(ctx->roots, X509_free);
780 x509_verify_ctx_clear(ctx);
785 x509_verify_ctx_set_max_depth(struct x509_verify_ctx *ctx, size_t max)
787 if (max < 1 || max > X509_VERIFY_MAX_CHAIN_CERTS)
789 ctx->max_depth = max;
794 x509_verify_ctx_set_max_chains(struct x509_verify_ctx *ctx, size_t max)
796 if (max < 1 || max > X509_VERIFY_MAX_CHAINS)
798 ctx->max_chains = max;
803 x509_verify_ctx_set_max_signatures(struct x509_verify_ctx *ctx, size_t max)
805 if (max < 1 || max > 100000)
812 x509_verify_ctx_set_purpose(struct x509_verify_ctx *ctx, int purpose)
814 if (purpose < X509_PURPOSE_MIN || purpose > X509_PURPOSE_MAX)
816 ctx->purpose = purpose;
821 x509_verify_ctx_set_intermediates(struct x509_verify_ctx *ctx,
822 STACK_OF(X509) *intermediates)
824 if ((ctx->intermediates = X509_chain_up_ref(intermediates)) == NULL)
830 x509_verify_ctx_error_string(struct x509_verify_ctx *ctx)
832 return X509_verify_cert_error_string(ctx->error);
836 x509_verify_ctx_error_depth(struct x509_verify_ctx *ctx)
838 return ctx->error_depth;
842 x509_verify_ctx_chain(struct x509_verify_ctx *ctx, size_t i)
844 if (i >= ctx->chains_count)
846 return ctx->chains[i]->certs;
850 x509_verify(struct x509_verify_ctx *ctx, X509 *leaf, char *name)
852 struct x509_verify_chain *current_chain;
854 if (ctx->roots == NULL || ctx->max_depth == 0) {
855 ctx->error = X509_V_ERR_INVALID_CALL;
859 if (ctx->xsc != NULL) {
860 if (leaf != NULL || name != NULL) {
861 ctx->error = X509_V_ERR_INVALID_CALL;
864 leaf = ctx->xsc->cert;
868 * The legacy code expects the top level cert to be
869 * there, even if we didn't find a chain. So put it
870 * there, we will clobber it later if we find a valid
873 if ((ctx->xsc->chain = sk_X509_new_null()) == NULL) {
874 ctx->error = X509_V_ERR_OUT_OF_MEM;
877 if (!X509_up_ref(leaf)) {
878 ctx->error = X509_V_ERR_OUT_OF_MEM;
881 if (!sk_X509_push(ctx->xsc->chain, leaf)) {
883 ctx->error = X509_V_ERR_OUT_OF_MEM;
886 ctx->xsc->error_depth = 0;
887 ctx->xsc->current_cert = leaf;
890 if (!x509_verify_cert_valid(ctx, leaf, NULL))
893 if (!x509_verify_cert_hostname(ctx, leaf, name))
896 if ((current_chain = x509_verify_chain_new()) == NULL) {
897 ctx->error = X509_V_ERR_OUT_OF_MEM;
900 if (!x509_verify_chain_append(current_chain, leaf, &ctx->error)) {
901 x509_verify_chain_free(current_chain);
904 if (x509_verify_ctx_cert_is_root(ctx, leaf))
905 x509_verify_ctx_add_chain(ctx, current_chain);
907 x509_verify_build_chains(ctx, leaf, current_chain);
909 x509_verify_chain_free(current_chain);
913 * We could not find a validated chain, and for some reason do not
916 if (ctx->chains_count == 0 && ctx->error == 0)
917 ctx->error = X509_V_ERR_UNSPECIFIED;
919 /* Clear whatever errors happened if we have any validated chain */
920 if (ctx->chains_count > 0)
921 ctx->error = X509_V_OK;
923 if (ctx->xsc != NULL) {
924 ctx->xsc->error = ctx->error;
925 return ctx->xsc->verify_cb(ctx->chains_count, ctx->xsc);
927 return (ctx->chains_count);