1 /* crypto/x509/x509_vfy.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
64 #include "internal/cryptlib.h"
65 #include <openssl/crypto.h>
66 #include <openssl/lhash.h>
67 #include <openssl/buffer.h>
68 #include <openssl/evp.h>
69 #include <openssl/asn1.h>
70 #include <openssl/x509.h>
71 #include <openssl/x509v3.h>
72 #include <openssl/objects.h>
73 #include <internal/dane.h>
74 #include <internal/x509_int.h>
77 /* CRL score values */
79 /* No unhandled critical extensions */
81 #define CRL_SCORE_NOCRITICAL 0x100
83 /* certificate is within CRL scope */
85 #define CRL_SCORE_SCOPE 0x080
89 #define CRL_SCORE_TIME 0x040
91 /* Issuer name matches certificate */
93 #define CRL_SCORE_ISSUER_NAME 0x020
95 /* If this score or above CRL is probably valid */
97 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
99 /* CRL issuer is certificate issuer */
101 #define CRL_SCORE_ISSUER_CERT 0x018
103 /* CRL issuer is on certificate path */
105 #define CRL_SCORE_SAME_PATH 0x008
107 /* CRL issuer matches CRL AKID */
109 #define CRL_SCORE_AKID 0x004
111 /* Have a delta CRL with valid times */
113 #define CRL_SCORE_TIME_DELTA 0x002
115 static int build_chain(X509_STORE_CTX *ctx);
116 static int verify_chain(X509_STORE_CTX *ctx);
117 static int null_callback(int ok, X509_STORE_CTX *e);
118 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
119 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
120 static int check_chain_extensions(X509_STORE_CTX *ctx);
121 static int check_name_constraints(X509_STORE_CTX *ctx);
122 static int check_id(X509_STORE_CTX *ctx);
123 static int check_trust(X509_STORE_CTX *ctx, int num_untrusted);
124 static int check_revocation(X509_STORE_CTX *ctx);
125 static int check_cert(X509_STORE_CTX *ctx);
126 static int check_policy(X509_STORE_CTX *ctx);
127 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
129 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
130 unsigned int *preasons, X509_CRL *crl, X509 *x);
131 static int get_crl_delta(X509_STORE_CTX *ctx,
132 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
133 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
134 int *pcrl_score, X509_CRL *base,
135 STACK_OF(X509_CRL) *crls);
136 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
138 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
139 unsigned int *preasons);
140 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
141 static int check_crl_chain(X509_STORE_CTX *ctx,
142 STACK_OF(X509) *cert_path,
143 STACK_OF(X509) *crl_path);
145 static int internal_verify(X509_STORE_CTX *ctx);
147 static int null_callback(int ok, X509_STORE_CTX *e)
152 /* Return 1 is a certificate is self signed */
153 static int cert_self_signed(X509 *x)
156 * FIXME: x509v3_cache_extensions() needs to detect more failures and not
157 * set EXFLAG_SET when that happens. Especially, if the failures are
158 * parse errors, rather than memory pressure!
160 X509_check_purpose(x, -1, 0);
161 if (x->ex_flags & EXFLAG_SS)
167 /* Given a certificate try and find an exact match in the store */
169 static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
171 STACK_OF(X509) *certs;
174 /* Lookup all certs with matching subject name */
175 certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
178 /* Look for exact match */
179 for (i = 0; i < sk_X509_num(certs); i++) {
180 xtmp = sk_X509_value(certs, i);
181 if (!X509_cmp(xtmp, x))
184 if (i < sk_X509_num(certs))
188 sk_X509_pop_free(certs, X509_free);
192 static int verify_chain(X509_STORE_CTX *ctx)
194 int (*cb) (int xok, X509_STORE_CTX *xctx) = ctx->verify_cb;
199 * Before either returning with an error, or continuing with CRL checks,
200 * instantiate chain public key parameters.
202 if ((ok = build_chain(ctx)) == 0 ||
203 (ok = check_chain_extensions(ctx)) == 0 ||
204 (ok = check_name_constraints(ctx)) == 0 ||
205 (ok = check_id(ctx)) == 0 || 1)
206 X509_get_pubkey_parameters(NULL, ctx->chain);
207 if (ok == 0 || (ok = ctx->check_revocation(ctx)) == 0)
210 err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
212 if (err != X509_V_OK) {
214 ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
215 if ((ok = cb(0, ctx)) == 0)
219 /* Verify chain signatures and expiration times */
220 ok = (ctx->verify != NULL) ? ctx->verify(ctx) : internal_verify(ctx);
224 #ifndef OPENSSL_NO_RFC3779
225 /* RFC 3779 path validation, now that CRL check has been done */
226 if ((ok = v3_asid_validate_path(ctx)) == 0)
228 if ((ok = v3_addr_validate_path(ctx)) == 0)
232 /* If we get this far evaluate policies */
233 if (ctx->param->flags & X509_V_FLAG_POLICY_CHECK)
234 ok = ctx->check_policy(ctx);
238 int X509_verify_cert(X509_STORE_CTX *ctx)
241 if (ctx->cert == NULL) {
242 X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
246 if (ctx->chain != NULL) {
248 * This X509_STORE_CTX has already been used to verify a cert. We
249 * cannot do another one.
251 X509err(X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
256 * first we make sure the chain we are going to build is present and that
257 * the first entry is in place
259 if (((ctx->chain = sk_X509_new_null()) == NULL) ||
260 (!sk_X509_push(ctx->chain, ctx->cert))) {
261 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
264 X509_up_ref(ctx->cert);
265 ctx->num_untrusted = 1;
267 return verify_chain(ctx);
271 * Given a STACK_OF(X509) find the issuer of cert (if any)
274 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
277 X509 *issuer, *rv = NULL;;
278 for (i = 0; i < sk_X509_num(sk); i++) {
279 issuer = sk_X509_value(sk, i);
280 if (ctx->check_issued(ctx, x, issuer)) {
282 if (x509_check_cert_time(ctx, rv, 1))
289 /* Given a possible certificate and issuer check them */
291 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
295 return cert_self_signed(x);
296 ret = X509_check_issued(issuer, x);
297 if (ret == X509_V_OK) {
300 /* Special case: single self signed certificate */
301 if (cert_self_signed(x) && sk_X509_num(ctx->chain) == 1)
303 for (i = 0; i < sk_X509_num(ctx->chain); i++) {
304 ch = sk_X509_value(ctx->chain, i);
305 if (ch == issuer || !X509_cmp(ch, issuer)) {
306 ret = X509_V_ERR_PATH_LOOP;
312 if (ret == X509_V_OK)
314 /* If we haven't asked for issuer errors don't set ctx */
315 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
319 ctx->current_cert = x;
320 ctx->current_issuer = issuer;
321 return ctx->verify_cb(0, ctx);
324 /* Alternative lookup method: look from a STACK stored in other_ctx */
326 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
328 *issuer = find_issuer(ctx, ctx->other_ctx, x);
330 X509_up_ref(*issuer);
337 * Check a certificate chains extensions for consistency with the supplied
341 static int check_chain_extensions(X509_STORE_CTX *ctx)
343 int i, ok = 0, must_be_ca, plen = 0;
345 int (*cb) (int xok, X509_STORE_CTX *xctx);
346 int proxy_path_length = 0;
348 int allow_proxy_certs;
352 * must_be_ca can have 1 of 3 values:
353 * -1: we accept both CA and non-CA certificates, to allow direct
354 * use of self-signed certificates (which are marked as CA).
355 * 0: we only accept non-CA certificates. This is currently not
356 * used, but the possibility is present for future extensions.
357 * 1: we only accept CA certificates. This is currently used for
358 * all certificates in the chain except the leaf certificate.
362 /* CRL path validation */
364 allow_proxy_certs = 0;
365 purpose = X509_PURPOSE_CRL_SIGN;
368 ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
370 * A hack to keep people who don't want to modify their software
373 if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
374 allow_proxy_certs = 1;
375 purpose = ctx->param->purpose;
378 /* Check all untrusted certificates */
379 for (i = 0; i == 0 || i < ctx->num_untrusted; i++) {
381 x = sk_X509_value(ctx->chain, i);
382 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
383 && (x->ex_flags & EXFLAG_CRITICAL)) {
384 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
385 ctx->error_depth = i;
386 ctx->current_cert = x;
391 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
392 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
393 ctx->error_depth = i;
394 ctx->current_cert = x;
399 ret = X509_check_ca(x);
400 switch (must_be_ca) {
402 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
403 && (ret != 1) && (ret != 0)) {
405 ctx->error = X509_V_ERR_INVALID_CA;
412 ctx->error = X509_V_ERR_INVALID_NON_CA;
418 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
421 ctx->error = X509_V_ERR_INVALID_CA;
427 ctx->error_depth = i;
428 ctx->current_cert = x;
433 if (ctx->param->purpose > 0) {
434 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
436 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
438 ctx->error = X509_V_ERR_INVALID_PURPOSE;
439 ctx->error_depth = i;
440 ctx->current_cert = x;
446 /* Check pathlen if not self issued */
447 if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
448 && (x->ex_pathlen != -1)
449 && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
450 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
451 ctx->error_depth = i;
452 ctx->current_cert = x;
457 /* Increment path length if not self issued */
458 if (!(x->ex_flags & EXFLAG_SI))
461 * If this certificate is a proxy certificate, the next certificate
462 * must be another proxy certificate or a EE certificate. If not,
463 * the next certificate must be a CA certificate.
465 if (x->ex_flags & EXFLAG_PROXY) {
466 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
467 ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
468 ctx->error_depth = i;
469 ctx->current_cert = x;
484 static int check_name_constraints(X509_STORE_CTX *ctx)
488 /* Check name constraints for all certificates */
489 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
490 x = sk_X509_value(ctx->chain, i);
491 /* Ignore self issued certs unless last in chain */
492 if (i && (x->ex_flags & EXFLAG_SI))
495 * Check against constraints for all certificates higher in chain
496 * including trust anchor. Trust anchor not strictly speaking needed
497 * but if it includes constraints it is to be assumed it expects them
500 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
501 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
503 rv = NAME_CONSTRAINTS_check(x, nc);
504 if (rv != X509_V_OK) {
506 ctx->error_depth = i;
507 ctx->current_cert = x;
508 if (!ctx->verify_cb(0, ctx))
517 static int check_id_error(X509_STORE_CTX *ctx, int errcode)
519 ctx->error = errcode;
520 ctx->current_cert = ctx->cert;
521 ctx->error_depth = 0;
522 return ctx->verify_cb(0, ctx);
525 static int check_hosts(X509 *x, X509_VERIFY_PARAM *vpm)
528 int n = sk_OPENSSL_STRING_num(vpm->hosts);
531 if (vpm->peername != NULL) {
532 OPENSSL_free(vpm->peername);
533 vpm->peername = NULL;
535 for (i = 0; i < n; ++i) {
536 name = sk_OPENSSL_STRING_value(vpm->hosts, i);
537 if (X509_check_host(x, name, 0, vpm->hostflags, &vpm->peername) > 0)
543 static int check_id(X509_STORE_CTX *ctx)
545 X509_VERIFY_PARAM *vpm = ctx->param;
547 if (vpm->hosts && check_hosts(x, vpm) <= 0) {
548 if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
551 if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) {
552 if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
555 if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) {
556 if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
562 static int check_trust(X509_STORE_CTX *ctx, int num_untrusted)
567 int (*cb) (int xok, X509_STORE_CTX *xctx) = ctx->verify_cb;
568 int num = sk_X509_num(ctx->chain);
572 * Check trusted certificates in chain at depth num_untrusted and up.
573 * Note, that depths 0..num_untrusted-1 may also contain trusted
574 * certificates, but the caller is expected to have already checked those,
575 * and wants to incrementally check just any added since.
577 for (i = num_untrusted; i < num; i++) {
578 x = sk_X509_value(ctx->chain, i);
579 trust = X509_check_trust(x, ctx->param->trust, 0);
580 /* If explicitly trusted return trusted */
581 if (trust == X509_TRUST_TRUSTED)
583 if (trust == X509_TRUST_REJECTED)
588 * If we are looking at a trusted certificate, and accept partial chains,
589 * the chain is PKIX trusted.
591 if (num_untrusted < num) {
592 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN)
594 return X509_TRUST_UNTRUSTED;
597 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
599 * Last-resort call with no new trusted certificates, check the leaf
600 * for a direct trust store match.
602 x = sk_X509_value(ctx->chain, 0);
603 mx = lookup_cert_match(ctx, x);
605 return X509_TRUST_UNTRUSTED;
608 * Check explicit auxiliary trust/reject settings. If none are set,
609 * we'll accept X509_TRUST_UNTRUSTED when not self-signed.
611 trust = X509_check_trust(mx, ctx->param->trust, 0);
612 if (trust == X509_TRUST_REJECTED) {
617 /* Replace leaf with trusted match */
618 (void) sk_X509_set(ctx->chain, 0, mx);
620 ctx->num_untrusted = 0;
625 * If no trusted certs in chain at all return untrusted and allow
626 * standard (no issuer cert) etc errors to be indicated.
628 return X509_TRUST_UNTRUSTED;
631 ctx->error_depth = i;
632 ctx->current_cert = x;
633 ctx->error = X509_V_ERR_CERT_REJECTED;
636 return X509_TRUST_REJECTED;
637 return X509_TRUST_UNTRUSTED;
640 return X509_TRUST_TRUSTED;
643 static int check_revocation(X509_STORE_CTX *ctx)
645 int i = 0, last = 0, ok = 0;
646 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
648 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
649 last = sk_X509_num(ctx->chain) - 1;
651 /* If checking CRL paths this isn't the EE certificate */
656 for (i = 0; i <= last; i++) {
657 ctx->error_depth = i;
658 ok = check_cert(ctx);
665 static int check_cert(X509_STORE_CTX *ctx)
667 X509_CRL *crl = NULL, *dcrl = NULL;
669 int ok = 0, cnum = 0;
670 unsigned int last_reasons = 0;
671 cnum = ctx->error_depth;
672 x = sk_X509_value(ctx->chain, cnum);
673 ctx->current_cert = x;
674 ctx->current_issuer = NULL;
675 ctx->current_crl_score = 0;
676 ctx->current_reasons = 0;
677 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
678 last_reasons = ctx->current_reasons;
679 /* Try to retrieve relevant CRL */
681 ok = ctx->get_crl(ctx, &crl, x);
683 ok = get_crl_delta(ctx, &crl, &dcrl, x);
685 * If error looking up CRL, nothing we can do except notify callback
688 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
689 ok = ctx->verify_cb(0, ctx);
692 ctx->current_crl = crl;
693 ok = ctx->check_crl(ctx, crl);
698 ok = ctx->check_crl(ctx, dcrl);
701 ok = ctx->cert_crl(ctx, dcrl, x);
707 /* Don't look in full CRL if delta reason is removefromCRL */
709 ok = ctx->cert_crl(ctx, crl, x);
719 * If reasons not updated we wont get anywhere by another iteration,
722 if (last_reasons == ctx->current_reasons) {
723 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
724 ok = ctx->verify_cb(0, ctx);
732 ctx->current_crl = NULL;
737 /* Check CRL times against values in X509_STORE_CTX */
739 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
744 ctx->current_crl = crl;
745 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
746 ptime = &ctx->param->check_time;
747 else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
752 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
756 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
757 if (!ctx->verify_cb(0, ctx))
764 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
765 if (!ctx->verify_cb(0, ctx))
769 if (X509_CRL_get_nextUpdate(crl)) {
770 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
775 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
776 if (!ctx->verify_cb(0, ctx))
779 /* Ignore expiry of base CRL is delta is valid */
780 if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
783 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
784 if (!ctx->verify_cb(0, ctx))
790 ctx->current_crl = NULL;
795 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
796 X509 **pissuer, int *pscore, unsigned int *preasons,
797 STACK_OF(X509_CRL) *crls)
799 int i, crl_score, best_score = *pscore;
800 unsigned int reasons, best_reasons = 0;
801 X509 *x = ctx->current_cert;
802 X509_CRL *crl, *best_crl = NULL;
803 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
805 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
806 crl = sk_X509_CRL_value(crls, i);
808 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
810 if (crl_score > best_score) {
812 best_crl_issuer = crl_issuer;
813 best_score = crl_score;
814 best_reasons = reasons;
819 X509_CRL_free(*pcrl);
821 *pissuer = best_crl_issuer;
822 *pscore = best_score;
823 *preasons = best_reasons;
824 X509_CRL_up_ref(best_crl);
825 X509_CRL_free(*pdcrl);
827 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
830 if (best_score >= CRL_SCORE_VALID)
837 * Compare two CRL extensions for delta checking purposes. They should be
838 * both present or both absent. If both present all fields must be identical.
841 static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
843 ASN1_OCTET_STRING *exta, *extb;
845 i = X509_CRL_get_ext_by_NID(a, nid, -1);
847 /* Can't have multiple occurrences */
848 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
850 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
854 i = X509_CRL_get_ext_by_NID(b, nid, -1);
858 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
860 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
870 if (ASN1_OCTET_STRING_cmp(exta, extb))
876 /* See if a base and delta are compatible */
878 static int check_delta_base(X509_CRL *delta, X509_CRL *base)
880 /* Delta CRL must be a delta */
881 if (!delta->base_crl_number)
883 /* Base must have a CRL number */
884 if (!base->crl_number)
886 /* Issuer names must match */
887 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
889 /* AKID and IDP must match */
890 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
892 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
894 /* Delta CRL base number must not exceed Full CRL number. */
895 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
897 /* Delta CRL number must exceed full CRL number */
898 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
904 * For a given base CRL find a delta... maybe extend to delta scoring or
905 * retrieve a chain of deltas...
908 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
909 X509_CRL *base, STACK_OF(X509_CRL) *crls)
913 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
915 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
917 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
918 delta = sk_X509_CRL_value(crls, i);
919 if (check_delta_base(delta, base)) {
920 if (check_crl_time(ctx, delta, 0))
921 *pscore |= CRL_SCORE_TIME_DELTA;
922 X509_CRL_up_ref(delta);
931 * For a given CRL return how suitable it is for the supplied certificate
932 * 'x'. The return value is a mask of several criteria. If the issuer is not
933 * the certificate issuer this is returned in *pissuer. The reasons mask is
934 * also used to determine if the CRL is suitable: if no new reasons the CRL
935 * is rejected, otherwise reasons is updated.
938 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
939 unsigned int *preasons, X509_CRL *crl, X509 *x)
943 unsigned int tmp_reasons = *preasons, crl_reasons;
945 /* First see if we can reject CRL straight away */
947 /* Invalid IDP cannot be processed */
948 if (crl->idp_flags & IDP_INVALID)
950 /* Reason codes or indirect CRLs need extended CRL support */
951 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
952 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
954 } else if (crl->idp_flags & IDP_REASONS) {
955 /* If no new reasons reject */
956 if (!(crl->idp_reasons & ~tmp_reasons))
959 /* Don't process deltas at this stage */
960 else if (crl->base_crl_number)
962 /* If issuer name doesn't match certificate need indirect CRL */
963 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
964 if (!(crl->idp_flags & IDP_INDIRECT))
967 crl_score |= CRL_SCORE_ISSUER_NAME;
969 if (!(crl->flags & EXFLAG_CRITICAL))
970 crl_score |= CRL_SCORE_NOCRITICAL;
973 if (check_crl_time(ctx, crl, 0))
974 crl_score |= CRL_SCORE_TIME;
976 /* Check authority key ID and locate certificate issuer */
977 crl_akid_check(ctx, crl, pissuer, &crl_score);
979 /* If we can't locate certificate issuer at this point forget it */
981 if (!(crl_score & CRL_SCORE_AKID))
984 /* Check cert for matching CRL distribution points */
986 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
987 /* If no new reasons reject */
988 if (!(crl_reasons & ~tmp_reasons))
990 tmp_reasons |= crl_reasons;
991 crl_score |= CRL_SCORE_SCOPE;
994 *preasons = tmp_reasons;
1000 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
1001 X509 **pissuer, int *pcrl_score)
1003 X509 *crl_issuer = NULL;
1004 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1005 int cidx = ctx->error_depth;
1008 if (cidx != sk_X509_num(ctx->chain) - 1)
1011 crl_issuer = sk_X509_value(ctx->chain, cidx);
1013 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1014 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1015 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
1016 *pissuer = crl_issuer;
1021 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1022 crl_issuer = sk_X509_value(ctx->chain, cidx);
1023 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1025 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1026 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
1027 *pissuer = crl_issuer;
1032 /* Anything else needs extended CRL support */
1034 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1038 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1039 * untrusted certificates.
1041 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1042 crl_issuer = sk_X509_value(ctx->untrusted, i);
1043 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1045 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1046 *pissuer = crl_issuer;
1047 *pcrl_score |= CRL_SCORE_AKID;
1054 * Check the path of a CRL issuer certificate. This creates a new
1055 * X509_STORE_CTX and populates it with most of the parameters from the
1056 * parent. This could be optimised somewhat since a lot of path checking will
1057 * be duplicated by the parent, but this will rarely be used in practice.
1060 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1062 X509_STORE_CTX crl_ctx;
1064 /* Don't allow recursive CRL path validation */
1067 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
1070 crl_ctx.crls = ctx->crls;
1071 /* Copy verify params across */
1072 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1074 crl_ctx.parent = ctx;
1075 crl_ctx.verify_cb = ctx->verify_cb;
1077 /* Verify CRL issuer */
1078 ret = X509_verify_cert(&crl_ctx);
1083 /* Check chain is acceptable */
1085 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1087 X509_STORE_CTX_cleanup(&crl_ctx);
1092 * RFC3280 says nothing about the relationship between CRL path and
1093 * certificate path, which could lead to situations where a certificate could
1094 * be revoked or validated by a CA not authorised to do so. RFC5280 is more
1095 * strict and states that the two paths must end in the same trust anchor,
1096 * though some discussions remain... until this is resolved we use the
1100 static int check_crl_chain(X509_STORE_CTX *ctx,
1101 STACK_OF(X509) *cert_path,
1102 STACK_OF(X509) *crl_path)
1104 X509 *cert_ta, *crl_ta;
1105 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1106 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1107 if (!X509_cmp(cert_ta, crl_ta))
1113 * Check for match between two dist point names: three separate cases.
1114 * 1. Both are relative names and compare X509_NAME types.
1115 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1116 * 3. Both are full names and compare two GENERAL_NAMES.
1117 * 4. One is NULL: automatic match.
1120 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1122 X509_NAME *nm = NULL;
1123 GENERAL_NAMES *gens = NULL;
1124 GENERAL_NAME *gena, *genb;
1131 /* Case 1: two X509_NAME */
1135 if (!X509_NAME_cmp(a->dpname, b->dpname))
1140 /* Case 2: set name and GENERAL_NAMES appropriately */
1142 gens = b->name.fullname;
1143 } else if (b->type == 1) {
1146 /* Case 2: set name and GENERAL_NAMES appropriately */
1147 gens = a->name.fullname;
1151 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1153 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1154 gena = sk_GENERAL_NAME_value(gens, i);
1155 if (gena->type != GEN_DIRNAME)
1157 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1163 /* Else case 3: two GENERAL_NAMES */
1165 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1166 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1167 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1168 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1169 if (!GENERAL_NAME_cmp(gena, genb))
1178 static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1181 X509_NAME *nm = X509_CRL_get_issuer(crl);
1182 /* If no CRLissuer return is successful iff don't need a match */
1184 return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
1185 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1186 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1187 if (gen->type != GEN_DIRNAME)
1189 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1195 /* Check CRLDP and IDP */
1197 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
1198 unsigned int *preasons)
1201 if (crl->idp_flags & IDP_ONLYATTR)
1203 if (x->ex_flags & EXFLAG_CA) {
1204 if (crl->idp_flags & IDP_ONLYUSER)
1207 if (crl->idp_flags & IDP_ONLYCA)
1210 *preasons = crl->idp_reasons;
1211 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1212 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1213 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1214 if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1215 *preasons &= dp->dp_reasons;
1220 if ((!crl->idp || !crl->idp->distpoint)
1221 && (crl_score & CRL_SCORE_ISSUER_NAME))
1227 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1228 * to find a delta CRL too
1231 static int get_crl_delta(X509_STORE_CTX *ctx,
1232 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1235 X509 *issuer = NULL;
1237 unsigned int reasons;
1238 X509_CRL *crl = NULL, *dcrl = NULL;
1239 STACK_OF(X509_CRL) *skcrl;
1240 X509_NAME *nm = X509_get_issuer_name(x);
1241 reasons = ctx->current_reasons;
1242 ok = get_crl_sk(ctx, &crl, &dcrl,
1243 &issuer, &crl_score, &reasons, ctx->crls);
1248 /* Lookup CRLs from store */
1250 skcrl = ctx->lookup_crls(ctx, nm);
1252 /* If no CRLs found and a near match from get_crl_sk use that */
1256 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1258 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1262 /* If we got any kind of CRL use it and return success */
1264 ctx->current_issuer = issuer;
1265 ctx->current_crl_score = crl_score;
1266 ctx->current_reasons = reasons;
1275 /* Check CRL validity */
1276 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1278 X509 *issuer = NULL;
1279 EVP_PKEY *ikey = NULL;
1280 int ok = 0, chnum, cnum;
1281 cnum = ctx->error_depth;
1282 chnum = sk_X509_num(ctx->chain) - 1;
1283 /* if we have an alternative CRL issuer cert use that */
1284 if (ctx->current_issuer)
1285 issuer = ctx->current_issuer;
1288 * Else find CRL issuer: if not last certificate then issuer is next
1289 * certificate in chain.
1291 else if (cnum < chnum)
1292 issuer = sk_X509_value(ctx->chain, cnum + 1);
1294 issuer = sk_X509_value(ctx->chain, chnum);
1295 /* If not self signed, can't check signature */
1296 if (!ctx->check_issued(ctx, issuer, issuer)) {
1297 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1298 ok = ctx->verify_cb(0, ctx);
1306 * Skip most tests for deltas because they have already been done
1308 if (!crl->base_crl_number) {
1309 /* Check for cRLSign bit if keyUsage present */
1310 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1311 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1312 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1313 ok = ctx->verify_cb(0, ctx);
1318 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1319 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1320 ok = ctx->verify_cb(0, ctx);
1325 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1326 if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
1327 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1328 ok = ctx->verify_cb(0, ctx);
1334 if (crl->idp_flags & IDP_INVALID) {
1335 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1336 ok = ctx->verify_cb(0, ctx);
1343 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1344 ok = check_crl_time(ctx, crl, 1);
1349 /* Attempt to get issuer certificate public key */
1350 ikey = X509_get0_pubkey(issuer);
1353 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1354 ok = ctx->verify_cb(0, ctx);
1359 rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
1360 if (rv != X509_V_OK) {
1362 ok = ctx->verify_cb(0, ctx);
1366 /* Verify CRL signature */
1367 if (X509_CRL_verify(crl, ikey) <= 0) {
1368 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1369 ok = ctx->verify_cb(0, ctx);
1382 /* Check certificate against CRL */
1383 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1388 * The rules changed for this... previously if a CRL contained unhandled
1389 * critical extensions it could still be used to indicate a certificate
1390 * was revoked. This has since been changed since critical extension can
1391 * change the meaning of CRL entries.
1393 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
1394 && (crl->flags & EXFLAG_CRITICAL)) {
1395 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1396 ok = ctx->verify_cb(0, ctx);
1401 * Look for serial number of certificate in CRL If found make sure reason
1402 * is not removeFromCRL.
1404 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1405 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1407 ctx->error = X509_V_ERR_CERT_REVOKED;
1408 ok = ctx->verify_cb(0, ctx);
1416 static int check_policy(X509_STORE_CTX *ctx)
1421 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1422 ctx->param->policies, ctx->param->flags);
1424 X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
1427 /* Invalid or inconsistent extensions */
1430 * Locate certificates with bad extensions and notify callback.
1434 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1435 x = sk_X509_value(ctx->chain, i);
1436 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1438 ctx->current_cert = x;
1439 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1440 if (!ctx->verify_cb(0, ctx))
1446 ctx->current_cert = NULL;
1447 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1448 return ctx->verify_cb(0, ctx);
1451 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1452 ctx->current_cert = NULL;
1453 ctx->error = X509_V_OK;
1454 if (!ctx->verify_cb(2, ctx))
1461 int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int quiet)
1466 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1467 ptime = &ctx->param->check_time;
1468 else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
1473 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1477 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1478 ctx->current_cert = x;
1479 if (!ctx->verify_cb(0, ctx))
1486 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1487 ctx->current_cert = x;
1488 if (!ctx->verify_cb(0, ctx))
1492 i = X509_cmp_time(X509_get_notAfter(x), ptime);
1496 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1497 ctx->current_cert = x;
1498 if (!ctx->verify_cb(0, ctx))
1505 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1506 ctx->current_cert = x;
1507 if (!ctx->verify_cb(0, ctx))
1514 static int internal_verify(X509_STORE_CTX *ctx)
1518 EVP_PKEY *pkey = NULL;
1519 int (*cb) (int xok, X509_STORE_CTX *xctx);
1521 cb = ctx->verify_cb;
1523 n = sk_X509_num(ctx->chain) - 1;
1524 ctx->error_depth = n;
1525 xi = sk_X509_value(ctx->chain, n);
1527 if (ctx->check_issued(ctx, xi, xi))
1530 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1535 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1536 ctx->current_cert = xi;
1541 ctx->error_depth = n;
1542 xs = sk_X509_value(ctx->chain, n);
1547 * Do not clear ctx->error=0, it must be "sticky", only the user's callback
1548 * is allowed to reset errors (at its own peril).
1551 ctx->error_depth = n;
1554 * Skip signature check for self signed certificates unless
1555 * explicitly asked for. It doesn't add any security and just wastes
1560 || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
1561 if ((pkey = X509_get0_pubkey(xi)) == NULL) {
1562 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1563 ctx->current_cert = xi;
1564 ok = (*cb) (0, ctx);
1567 } else if (X509_verify(xs, pkey) <= 0) {
1568 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1569 ctx->current_cert = xs;
1570 ok = (*cb) (0, ctx);
1579 ok = x509_check_cert_time(ctx, xs, 0);
1583 /* The last error (if any) is still in the error value */
1584 ctx->current_issuer = xi;
1585 ctx->current_cert = xs;
1586 ok = (*cb) (1, ctx);
1593 xs = sk_X509_value(ctx->chain, n);
1601 int X509_cmp_current_time(const ASN1_TIME *ctm)
1603 return X509_cmp_time(ctm, NULL);
1606 int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1611 char buff1[24], buff2[24], *p;
1612 int i, j, remaining;
1615 remaining = ctm->length;
1616 str = (char *)ctm->data;
1618 * Note that the following (historical) code allows much more slack in the
1619 * time format than RFC5280. In RFC5280, the representation is fixed:
1620 * UTCTime: YYMMDDHHMMSSZ
1621 * GeneralizedTime: YYYYMMDDHHMMSSZ
1623 if (ctm->type == V_ASN1_UTCTIME) {
1624 /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
1625 int min_length = sizeof("YYMMDDHHMMZ") - 1;
1626 int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
1627 if (remaining < min_length || remaining > max_length)
1634 /* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
1635 int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
1636 int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
1637 if (remaining < min_length || remaining > max_length)
1645 if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
1656 * Skip any (up to three) fractional seconds...
1657 * TODO(emilia): in RFC5280, fractional seconds are forbidden.
1658 * Can we just kill them altogether?
1660 if (remaining && *str == '.') {
1663 for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
1664 if (*str < '0' || *str > '9')
1673 /* We now need either a terminating 'Z' or an offset. */
1682 if ((*str != '+') && (*str != '-'))
1684 /* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
1687 if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
1688 str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
1690 offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
1691 offset += (str[3] - '0') * 10 + (str[4] - '0');
1695 atm.type = ctm->type;
1697 atm.length = sizeof(buff2);
1698 atm.data = (unsigned char *)buff2;
1700 if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
1703 if (ctm->type == V_ASN1_UTCTIME) {
1704 i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
1706 i += 100; /* cf. RFC 2459 */
1707 j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
1716 i = strcmp(buff1, buff2);
1717 if (i == 0) /* wait a second then return younger :-) */
1723 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
1725 return X509_time_adj(s, adj, NULL);
1728 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
1730 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1733 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
1734 int offset_day, long offset_sec, time_t *in_tm)
1743 if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
1744 if (s->type == V_ASN1_UTCTIME)
1745 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
1746 if (s->type == V_ASN1_GENERALIZEDTIME)
1747 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
1749 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1752 int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1754 EVP_PKEY *ktmp = NULL, *ktmp2;
1757 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1760 for (i = 0; i < sk_X509_num(chain); i++) {
1761 ktmp = X509_get0_pubkey(sk_X509_value(chain, i));
1763 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1764 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1767 if (!EVP_PKEY_missing_parameters(ktmp))
1771 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1772 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1776 /* first, populate the other certs */
1777 for (j = i - 1; j >= 0; j--) {
1778 ktmp2 = X509_get0_pubkey(sk_X509_value(chain, j));
1779 EVP_PKEY_copy_parameters(ktmp2, ktmp);
1783 EVP_PKEY_copy_parameters(pkey, ktmp);
1787 /* Make a delta CRL as the diff between two full CRLs */
1789 X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
1790 EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
1792 X509_CRL *crl = NULL;
1794 STACK_OF(X509_REVOKED) *revs = NULL;
1795 /* CRLs can't be delta already */
1796 if (base->base_crl_number || newer->base_crl_number) {
1797 X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_ALREADY_DELTA);
1800 /* Base and new CRL must have a CRL number */
1801 if (!base->crl_number || !newer->crl_number) {
1802 X509err(X509_F_X509_CRL_DIFF, X509_R_NO_CRL_NUMBER);
1805 /* Issuer names must match */
1806 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
1807 X509err(X509_F_X509_CRL_DIFF, X509_R_ISSUER_MISMATCH);
1810 /* AKID and IDP must match */
1811 if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
1812 X509err(X509_F_X509_CRL_DIFF, X509_R_AKID_MISMATCH);
1815 if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
1816 X509err(X509_F_X509_CRL_DIFF, X509_R_IDP_MISMATCH);
1819 /* Newer CRL number must exceed full CRL number */
1820 if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
1821 X509err(X509_F_X509_CRL_DIFF, X509_R_NEWER_CRL_NOT_NEWER);
1824 /* CRLs must verify */
1825 if (skey && (X509_CRL_verify(base, skey) <= 0 ||
1826 X509_CRL_verify(newer, skey) <= 0)) {
1827 X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_VERIFY_FAILURE);
1830 /* Create new CRL */
1831 crl = X509_CRL_new();
1832 if (crl == NULL || !X509_CRL_set_version(crl, 1))
1834 /* Set issuer name */
1835 if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
1838 if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
1840 if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
1843 /* Set base CRL number: must be critical */
1845 if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
1849 * Copy extensions across from newest CRL to delta: this will set CRL
1850 * number to correct value too.
1853 for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
1854 X509_EXTENSION *ext;
1855 ext = X509_CRL_get_ext(newer, i);
1856 if (!X509_CRL_add_ext(crl, ext, -1))
1860 /* Go through revoked entries, copying as needed */
1862 revs = X509_CRL_get_REVOKED(newer);
1864 for (i = 0; i < sk_X509_REVOKED_num(revs); i++) {
1865 X509_REVOKED *rvn, *rvtmp;
1866 rvn = sk_X509_REVOKED_value(revs, i);
1868 * Add only if not also in base. TODO: need something cleverer here
1869 * for some more complex CRLs covering multiple CAs.
1871 if (!X509_CRL_get0_by_serial(base, &rvtmp, &rvn->serialNumber)) {
1872 rvtmp = X509_REVOKED_dup(rvn);
1875 if (!X509_CRL_add0_revoked(crl, rvtmp)) {
1876 X509_REVOKED_free(rvtmp);
1881 /* TODO: optionally prune deleted entries */
1883 if (skey && md && !X509_CRL_sign(crl, skey, md))
1889 X509err(X509_F_X509_CRL_DIFF, ERR_R_MALLOC_FAILURE);
1894 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
1896 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1899 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
1901 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1904 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
1909 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
1914 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
1916 return ctx->error_depth;
1919 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
1921 return ctx->current_cert;
1924 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
1929 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
1933 return X509_chain_up_ref(ctx->chain);
1936 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
1938 return ctx->current_issuer;
1941 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
1943 return ctx->current_crl;
1946 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
1951 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
1956 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1958 ctx->untrusted = sk;
1961 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
1966 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
1968 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
1971 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
1973 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
1977 * This function is used to set the X509_STORE_CTX purpose and trust values.
1978 * This is intended to be used when another structure has its own trust and
1979 * purpose values which (if set) will be inherited by the ctx. If they aren't
1980 * set then we will usually have a default purpose in mind which should then
1981 * be used to set the trust value. An example of this is SSL use: an SSL
1982 * structure will have its own purpose and trust settings which the
1983 * application can set: if they aren't set then we use the default of SSL
1987 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
1988 int purpose, int trust)
1991 /* If purpose not set use default */
1993 purpose = def_purpose;
1994 /* If we have a purpose then check it is valid */
1997 idx = X509_PURPOSE_get_by_id(purpose);
1999 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
2000 X509_R_UNKNOWN_PURPOSE_ID);
2003 ptmp = X509_PURPOSE_get0(idx);
2004 if (ptmp->trust == X509_TRUST_DEFAULT) {
2005 idx = X509_PURPOSE_get_by_id(def_purpose);
2007 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
2008 X509_R_UNKNOWN_PURPOSE_ID);
2011 ptmp = X509_PURPOSE_get0(idx);
2013 /* If trust not set then get from purpose default */
2015 trust = ptmp->trust;
2018 idx = X509_TRUST_get_by_id(trust);
2020 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
2021 X509_R_UNKNOWN_TRUST_ID);
2026 if (purpose && !ctx->param->purpose)
2027 ctx->param->purpose = purpose;
2028 if (trust && !ctx->param->trust)
2029 ctx->param->trust = trust;
2033 X509_STORE_CTX *X509_STORE_CTX_new(void)
2035 X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
2038 X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
2044 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
2048 X509_STORE_CTX_cleanup(ctx);
2052 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
2053 STACK_OF(X509) *chain)
2058 ctx->current_method = 0;
2060 ctx->untrusted = chain;
2062 ctx->num_untrusted = 0;
2063 ctx->other_ctx = NULL;
2067 ctx->explicit_policy = 0;
2068 ctx->error_depth = 0;
2069 ctx->current_cert = NULL;
2070 ctx->current_issuer = NULL;
2071 ctx->current_crl = NULL;
2072 ctx->current_crl_score = 0;
2073 ctx->current_reasons = 0;
2077 /* Zero ex_data to make sure we're cleanup-safe */
2078 memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
2081 ctx->verify_cb = store->verify_cb;
2082 /* Seems to always be 0 in OpenSSL, else must be idempotent */
2083 ctx->cleanup = store->cleanup;
2087 if (store && store->check_issued)
2088 ctx->check_issued = store->check_issued;
2090 ctx->check_issued = check_issued;
2092 if (store && store->get_issuer)
2093 ctx->get_issuer = store->get_issuer;
2095 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2097 if (store && store->verify_cb)
2098 ctx->verify_cb = store->verify_cb;
2100 ctx->verify_cb = null_callback;
2102 if (store && store->verify)
2103 ctx->verify = store->verify;
2105 ctx->verify = internal_verify;
2107 if (store && store->check_revocation)
2108 ctx->check_revocation = store->check_revocation;
2110 ctx->check_revocation = check_revocation;
2112 if (store && store->get_crl)
2113 ctx->get_crl = store->get_crl;
2115 if (store && store->check_crl)
2116 ctx->check_crl = store->check_crl;
2118 ctx->check_crl = check_crl;
2120 if (store && store->cert_crl)
2121 ctx->cert_crl = store->cert_crl;
2123 ctx->cert_crl = cert_crl;
2125 if (store && store->lookup_certs)
2126 ctx->lookup_certs = store->lookup_certs;
2128 ctx->lookup_certs = X509_STORE_get1_certs;
2130 if (store && store->lookup_crls)
2131 ctx->lookup_crls = store->lookup_crls;
2133 ctx->lookup_crls = X509_STORE_get1_crls;
2135 ctx->check_policy = check_policy;
2137 ctx->param = X509_VERIFY_PARAM_new();
2138 if (ctx->param == NULL) {
2139 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2144 * Inherit callbacks and flags from X509_STORE if not set use defaults.
2147 ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2149 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
2152 ret = X509_VERIFY_PARAM_inherit(ctx->param,
2153 X509_VERIFY_PARAM_lookup("default"));
2156 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2160 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2163 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2167 * On error clean up allocated storage, if the store context was not
2168 * allocated with X509_STORE_CTX_new() this is our last chance to do so.
2170 X509_STORE_CTX_cleanup(ctx);
2175 * Set alternative lookup method: just a STACK of trusted certificates. This
2176 * avoids X509_STORE nastiness where it isn't needed.
2179 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2181 ctx->other_ctx = sk;
2182 ctx->get_issuer = get_issuer_sk;
2185 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2188 * We need to be idempotent because, unfortunately, free() also calls
2189 * cleanup(), so the natural call sequence new(), init(), cleanup(), free()
2190 * calls cleanup() for the same object twice! Thus we must zero the
2191 * pointers below after they're freed!
2193 /* Seems to always be 0 in OpenSSL, do this at most once. */
2194 if (ctx->cleanup != NULL) {
2196 ctx->cleanup = NULL;
2198 if (ctx->param != NULL) {
2199 if (ctx->parent == NULL)
2200 X509_VERIFY_PARAM_free(ctx->param);
2203 X509_policy_tree_free(ctx->tree);
2205 sk_X509_pop_free(ctx->chain, X509_free);
2207 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
2208 memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
2211 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2213 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2216 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2218 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2221 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
2224 X509_VERIFY_PARAM_set_time(ctx->param, t);
2227 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2228 int (*verify_cb) (int, X509_STORE_CTX *))
2230 ctx->verify_cb = verify_cb;
2233 X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2238 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2240 return ctx->explicit_policy;
2243 int X509_STORE_CTX_get_num_untrusted(X509_STORE_CTX *ctx)
2245 return ctx->num_untrusted;
2248 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2250 const X509_VERIFY_PARAM *param;
2251 param = X509_VERIFY_PARAM_lookup(name);
2254 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2257 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2262 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2264 X509_VERIFY_PARAM_free(ctx->param);
2268 void X509_STORE_CTX_set0_dane(X509_STORE_CTX *ctx, struct dane_st *dane)
2273 static int build_chain(X509_STORE_CTX *ctx)
2275 int (*cb) (int, X509_STORE_CTX *) = ctx->verify_cb;
2276 int num = sk_X509_num(ctx->chain);
2277 X509 *cert = sk_X509_value(ctx->chain, num - 1);
2278 int ss = cert_self_signed(cert);
2279 STACK_OF(X509) *sktmp = NULL;
2280 unsigned int search;
2281 int may_trusted = 1;
2282 int may_alternate = 0;
2283 int trust = X509_TRUST_UNTRUSTED;
2284 int alt_untrusted = 0;
2289 /* Our chain starts with a single untrusted element. */
2290 OPENSSL_assert(num == 1 && ctx->num_untrusted == num);
2292 #define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */
2293 #define S_DOTRUSTED (1 << 1) /* Search trusted store */
2294 #define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */
2296 * Set up search policy, untrusted if possible, trusted-first if enabled.
2297 * If not trusted-first, and alternate chains are not disabled, try
2298 * building an alternate chain if no luck with untrusted first.
2300 search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0;
2301 if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
2302 search |= S_DOTRUSTED;
2303 else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))
2307 * Shallow-copy the stack of untrusted certificates (with TLS, this is
2308 * typically the content of the peer's certificate message) so can make
2309 * multiple passes over it, while free to remove elements as we go.
2311 if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
2312 X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
2317 * Still absurdly large, but arithmetically safe, a lower hard upper bound
2318 * might be reasonable.
2320 if (ctx->param->depth > INT_MAX/2)
2321 ctx->param->depth = INT_MAX/2;
2324 * Try to Extend the chain until we reach an ultimately trusted issuer.
2325 * Build chains up to one longer the limit, later fail if we hit the limit,
2326 * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code.
2328 depth = ctx->param->depth + 1;
2330 while (search != 0) {
2335 * Look in the trust store if enabled for first lookup, or we've run
2336 * out of untrusted issuers and search here is not disabled. When
2337 * we exceed the depth limit, we simulate absence of a match.
2339 if ((search & S_DOTRUSTED) != 0) {
2340 STACK_OF(X509) *hide = ctx->chain;
2342 i = num = sk_X509_num(ctx->chain);
2343 if ((search & S_DOALTERNATE) != 0) {
2345 * As high up the chain as we can, look for an alternative
2346 * trusted issuer of an untrusted certificate that currently
2347 * has an untrusted issuer. We use the alt_untrusted variable
2348 * to track how far up the chain we find the first match. It
2349 * is only if and when we find a match, that we prune the chain
2350 * and reset ctx->num_untrusted to the reduced count of
2351 * untrusted certificates. While we're searching for such a
2352 * match (which may never be found), it is neither safe nor
2353 * wise to preemptively modify either the chain or
2354 * ctx->num_untrusted.
2356 * Note, like ctx->num_untrusted, alt_untrusted is a count of
2357 * untrusted certificates, not a "depth".
2361 x = sk_X509_value(ctx->chain, i-1);
2363 /* Suppress duplicate suppression */
2365 ok = (depth < num) ? 0 : ctx->get_issuer(&xtmp, ctx, x);
2369 trust = X509_TRUST_REJECTED;
2376 * Alternative trusted issuer for a mid-chain untrusted cert?
2377 * Pop the untrusted cert's successors and retry. We might now
2378 * be able to complete a valid chain via the trust store. Note
2379 * that despite the current trust-store match we might still
2380 * fail complete the chain to a suitable trust-anchor, in which
2381 * case we may prune some more untrusted certificates and try
2382 * again. Thus the S_DOALTERNATE bit may yet be turned on
2383 * again with an even shorter untrusted chain!
2385 if ((search & S_DOALTERNATE) != 0) {
2386 OPENSSL_assert(num > i && i > 0 && ss == 0);
2387 search &= ~S_DOALTERNATE;
2388 for (; num > i; --num)
2389 X509_free(sk_X509_pop(ctx->chain));
2390 ctx->num_untrusted = num;
2394 * Self-signed untrusted certificates get replaced by their
2395 * trusted matching issuer. Otherwise, grow the chain.
2398 if (!sk_X509_push(ctx->chain, x = xtmp)) {
2400 X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
2401 trust = X509_TRUST_REJECTED;
2405 ss = cert_self_signed(x);
2406 } else if (num == ctx->num_untrusted) {
2408 * We have a self-signed certificate that has the same
2409 * subject name (and perhaps keyid and/or serial number) as
2410 * a trust-anchor. We must have an exact match to avoid
2411 * possible impersonation via key substitution etc.
2413 if (X509_cmp(x, xtmp) != 0) {
2414 /* Self-signed untrusted mimic. */
2419 ctx->num_untrusted = --num;
2420 (void) sk_X509_set(ctx->chain, num, x = xtmp);
2425 * We've added a new trusted certificate to the chain, recheck
2426 * trust. If not done, and not self-signed look deeper.
2427 * Whether or not we're doing "trusted first", we no longer
2428 * look for untrusted certificates from the peer's chain.
2431 OPENSSL_assert(ctx->num_untrusted <= num);
2432 search &= ~S_DOUNTRUSTED;
2433 switch (trust = check_trust(ctx, num)) {
2434 case X509_TRUST_TRUSTED:
2435 case X509_TRUST_REJECTED:
2445 * No dispositive decision, and either self-signed or no match, if
2446 * we were doing untrusted-first, and alt-chains are not disabled,
2447 * do that, by repeatedly losing one untrusted element at a time,
2448 * and trying to extend the shorted chain.
2450 if ((search & S_DOUNTRUSTED) == 0) {
2451 /* Continue search for a trusted issuer of a shorter chain? */
2452 if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0)
2454 /* Still no luck and no fallbacks left? */
2455 if (!may_alternate || (search & S_DOALTERNATE) != 0 ||
2456 ctx->num_untrusted < 2)
2458 /* Search for a trusted issuer of a shorter chain */
2459 search |= S_DOALTERNATE;
2460 alt_untrusted = ctx->num_untrusted - 1;
2466 * Extend chain with peer-provided certificates
2468 if ((search & S_DOUNTRUSTED) != 0) {
2469 num = sk_X509_num(ctx->chain);
2470 OPENSSL_assert(num == ctx->num_untrusted);
2471 x = sk_X509_value(ctx->chain, num-1);
2472 xtmp = (depth < num) ? NULL : find_issuer(ctx, sktmp, x);
2475 * Once we run out of untrusted issuers, we stop looking for more
2476 * and start looking only in the trust store if enabled.
2479 search &= ~S_DOUNTRUSTED;
2481 search |= S_DOTRUSTED;
2485 if (!sk_X509_push(ctx->chain, x = xtmp)) {
2486 X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
2487 trust = X509_TRUST_REJECTED;
2492 ++ctx->num_untrusted;
2493 ss = cert_self_signed(xtmp);
2496 * Not strictly necessary, but saves cycles looking at the same
2497 * certificates over and over.
2499 (void) sk_X509_delete_ptr(sktmp, x);
2502 sk_X509_free(sktmp);
2505 * Last chance to make a trusted chain, check for direct leaf PKIX trust.
2507 if (sk_X509_num(ctx->chain) <= depth) {
2508 if (trust == X509_TRUST_UNTRUSTED &&
2509 sk_X509_num(ctx->chain) == ctx->num_untrusted)
2510 trust = check_trust(ctx, 1);
2514 case X509_TRUST_TRUSTED:
2516 case X509_TRUST_REJECTED:
2518 case X509_TRUST_UNTRUSTED:
2520 num = sk_X509_num(ctx->chain);
2521 ctx->current_cert = sk_X509_value(ctx->chain, num - 1);
2522 ctx->error_depth = num-1;
2524 ctx->error = X509_V_ERR_CERT_CHAIN_TOO_LONG;
2525 else if (ss && sk_X509_num(ctx->chain) == 1)
2526 ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
2528 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2529 else if (ctx->num_untrusted == num)
2530 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
2532 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;