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/x509_int.h>
76 /* CRL score values */
78 /* No unhandled critical extensions */
80 #define CRL_SCORE_NOCRITICAL 0x100
82 /* certificate is within CRL scope */
84 #define CRL_SCORE_SCOPE 0x080
88 #define CRL_SCORE_TIME 0x040
90 /* Issuer name matches certificate */
92 #define CRL_SCORE_ISSUER_NAME 0x020
94 /* If this score or above CRL is probably valid */
96 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
98 /* CRL issuer is certificate issuer */
100 #define CRL_SCORE_ISSUER_CERT 0x018
102 /* CRL issuer is on certificate path */
104 #define CRL_SCORE_SAME_PATH 0x008
106 /* CRL issuer matches CRL AKID */
108 #define CRL_SCORE_AKID 0x004
110 /* Have a delta CRL with valid times */
112 #define CRL_SCORE_TIME_DELTA 0x002
114 static int build_chain(X509_STORE_CTX *ctx);
115 static int verify_chain(X509_STORE_CTX *ctx);
116 static int null_callback(int ok, X509_STORE_CTX *e);
117 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
118 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
119 static int check_chain_extensions(X509_STORE_CTX *ctx);
120 static int check_name_constraints(X509_STORE_CTX *ctx);
121 static int check_id(X509_STORE_CTX *ctx);
122 static int check_trust(X509_STORE_CTX *ctx, int num_untrusted);
123 static int check_revocation(X509_STORE_CTX *ctx);
124 static int check_cert(X509_STORE_CTX *ctx);
125 static int check_policy(X509_STORE_CTX *ctx);
126 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
128 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
129 unsigned int *preasons, X509_CRL *crl, X509 *x);
130 static int get_crl_delta(X509_STORE_CTX *ctx,
131 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
132 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
133 int *pcrl_score, X509_CRL *base,
134 STACK_OF(X509_CRL) *crls);
135 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
137 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
138 unsigned int *preasons);
139 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
140 static int check_crl_chain(X509_STORE_CTX *ctx,
141 STACK_OF(X509) *cert_path,
142 STACK_OF(X509) *crl_path);
144 static int internal_verify(X509_STORE_CTX *ctx);
146 static int null_callback(int ok, X509_STORE_CTX *e)
151 /* Return 1 is a certificate is self signed */
152 static int cert_self_signed(X509 *x)
155 * FIXME: x509v3_cache_extensions() needs to detect more failures and not
156 * set EXFLAG_SET when that happens. Especially, if the failures are
157 * parse errors, rather than memory pressure!
159 X509_check_purpose(x, -1, 0);
160 if (x->ex_flags & EXFLAG_SS)
166 /* Given a certificate try and find an exact match in the store */
168 static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
170 STACK_OF(X509) *certs;
173 /* Lookup all certs with matching subject name */
174 certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
177 /* Look for exact match */
178 for (i = 0; i < sk_X509_num(certs); i++) {
179 xtmp = sk_X509_value(certs, i);
180 if (!X509_cmp(xtmp, x))
183 if (i < sk_X509_num(certs))
187 sk_X509_pop_free(certs, X509_free);
191 static int verify_chain(X509_STORE_CTX *ctx)
193 int (*cb) (int xok, X509_STORE_CTX *xctx) = ctx->verify_cb;
198 * Before either returning with an error, or continuing with CRL checks,
199 * instantiate chain public key parameters.
201 if ((ok = build_chain(ctx)) == 0 ||
202 (ok = check_chain_extensions(ctx)) == 0 ||
203 (ok = check_name_constraints(ctx)) == 0 ||
204 (ok = check_id(ctx)) == 0 || 1)
205 X509_get_pubkey_parameters(NULL, ctx->chain);
206 if (ok == 0 || (ok = ctx->check_revocation(ctx)) == 0)
209 err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
211 if (err != X509_V_OK) {
213 ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
214 if ((ok = cb(0, ctx)) == 0)
218 /* Verify chain signatures and expiration times */
219 ok = (ctx->verify != NULL) ? ctx->verify(ctx) : internal_verify(ctx);
223 #ifndef OPENSSL_NO_RFC3779
224 /* RFC 3779 path validation, now that CRL check has been done */
225 if ((ok = v3_asid_validate_path(ctx)) == 0)
227 if ((ok = v3_addr_validate_path(ctx)) == 0)
231 /* If we get this far evaluate policies */
232 if (ctx->param->flags & X509_V_FLAG_POLICY_CHECK)
233 ok = ctx->check_policy(ctx);
237 int X509_verify_cert(X509_STORE_CTX *ctx)
240 if (ctx->cert == NULL) {
241 X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
245 if (ctx->chain != NULL) {
247 * This X509_STORE_CTX has already been used to verify a cert. We
248 * cannot do another one.
250 X509err(X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
255 * first we make sure the chain we are going to build is present and that
256 * the first entry is in place
258 if (((ctx->chain = sk_X509_new_null()) == NULL) ||
259 (!sk_X509_push(ctx->chain, ctx->cert))) {
260 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
263 X509_up_ref(ctx->cert);
264 ctx->num_untrusted = 1;
266 return verify_chain(ctx);
270 * Given a STACK_OF(X509) find the issuer of cert (if any)
273 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
276 X509 *issuer, *rv = NULL;;
277 for (i = 0; i < sk_X509_num(sk); i++) {
278 issuer = sk_X509_value(sk, i);
279 if (ctx->check_issued(ctx, x, issuer)) {
281 if (x509_check_cert_time(ctx, rv, 1))
288 /* Given a possible certificate and issuer check them */
290 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
294 return cert_self_signed(x);
295 ret = X509_check_issued(issuer, x);
296 if (ret == X509_V_OK) {
299 /* Special case: single self signed certificate */
300 if (cert_self_signed(x) && sk_X509_num(ctx->chain) == 1)
302 for (i = 0; i < sk_X509_num(ctx->chain); i++) {
303 ch = sk_X509_value(ctx->chain, i);
304 if (ch == issuer || !X509_cmp(ch, issuer)) {
305 ret = X509_V_ERR_PATH_LOOP;
311 if (ret == X509_V_OK)
313 /* If we haven't asked for issuer errors don't set ctx */
314 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
318 ctx->current_cert = x;
319 ctx->current_issuer = issuer;
320 return ctx->verify_cb(0, ctx);
323 /* Alternative lookup method: look from a STACK stored in other_ctx */
325 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
327 *issuer = find_issuer(ctx, ctx->other_ctx, x);
329 X509_up_ref(*issuer);
336 * Check a certificate chains extensions for consistency with the supplied
340 static int check_chain_extensions(X509_STORE_CTX *ctx)
342 int i, ok = 0, must_be_ca, plen = 0;
344 int (*cb) (int xok, X509_STORE_CTX *xctx);
345 int proxy_path_length = 0;
347 int allow_proxy_certs;
351 * must_be_ca can have 1 of 3 values:
352 * -1: we accept both CA and non-CA certificates, to allow direct
353 * use of self-signed certificates (which are marked as CA).
354 * 0: we only accept non-CA certificates. This is currently not
355 * used, but the possibility is present for future extensions.
356 * 1: we only accept CA certificates. This is currently used for
357 * all certificates in the chain except the leaf certificate.
361 /* CRL path validation */
363 allow_proxy_certs = 0;
364 purpose = X509_PURPOSE_CRL_SIGN;
367 ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
369 * A hack to keep people who don't want to modify their software
372 if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
373 allow_proxy_certs = 1;
374 purpose = ctx->param->purpose;
377 /* Check all untrusted certificates */
378 for (i = 0; i == 0 || i < ctx->num_untrusted; i++) {
380 x = sk_X509_value(ctx->chain, i);
381 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
382 && (x->ex_flags & EXFLAG_CRITICAL)) {
383 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
384 ctx->error_depth = i;
385 ctx->current_cert = x;
390 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
391 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
392 ctx->error_depth = i;
393 ctx->current_cert = x;
398 ret = X509_check_ca(x);
399 switch (must_be_ca) {
401 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
402 && (ret != 1) && (ret != 0)) {
404 ctx->error = X509_V_ERR_INVALID_CA;
411 ctx->error = X509_V_ERR_INVALID_NON_CA;
417 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
420 ctx->error = X509_V_ERR_INVALID_CA;
426 ctx->error_depth = i;
427 ctx->current_cert = x;
432 if (ctx->param->purpose > 0) {
433 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
435 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
437 ctx->error = X509_V_ERR_INVALID_PURPOSE;
438 ctx->error_depth = i;
439 ctx->current_cert = x;
445 /* Check pathlen if not self issued */
446 if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
447 && (x->ex_pathlen != -1)
448 && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
449 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
450 ctx->error_depth = i;
451 ctx->current_cert = x;
456 /* Increment path length if not self issued */
457 if (!(x->ex_flags & EXFLAG_SI))
460 * If this certificate is a proxy certificate, the next certificate
461 * must be another proxy certificate or a EE certificate. If not,
462 * the next certificate must be a CA certificate.
464 if (x->ex_flags & EXFLAG_PROXY) {
465 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
466 ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
467 ctx->error_depth = i;
468 ctx->current_cert = x;
483 static int check_name_constraints(X509_STORE_CTX *ctx)
487 /* Check name constraints for all certificates */
488 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
489 x = sk_X509_value(ctx->chain, i);
490 /* Ignore self issued certs unless last in chain */
491 if (i && (x->ex_flags & EXFLAG_SI))
494 * Check against constraints for all certificates higher in chain
495 * including trust anchor. Trust anchor not strictly speaking needed
496 * but if it includes constraints it is to be assumed it expects them
499 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
500 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
502 rv = NAME_CONSTRAINTS_check(x, nc);
503 if (rv != X509_V_OK) {
505 ctx->error_depth = i;
506 ctx->current_cert = x;
507 if (!ctx->verify_cb(0, ctx))
516 static int check_id_error(X509_STORE_CTX *ctx, int errcode)
518 ctx->error = errcode;
519 ctx->current_cert = ctx->cert;
520 ctx->error_depth = 0;
521 return ctx->verify_cb(0, ctx);
524 static int check_hosts(X509 *x, X509_VERIFY_PARAM *vpm)
527 int n = sk_OPENSSL_STRING_num(vpm->hosts);
530 if (vpm->peername != NULL) {
531 OPENSSL_free(vpm->peername);
532 vpm->peername = NULL;
534 for (i = 0; i < n; ++i) {
535 name = sk_OPENSSL_STRING_value(vpm->hosts, i);
536 if (X509_check_host(x, name, 0, vpm->hostflags, &vpm->peername) > 0)
542 static int check_id(X509_STORE_CTX *ctx)
544 X509_VERIFY_PARAM *vpm = ctx->param;
546 if (vpm->hosts && check_hosts(x, vpm) <= 0) {
547 if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
550 if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) {
551 if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
554 if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) {
555 if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
561 static int check_trust(X509_STORE_CTX *ctx, int num_untrusted)
566 int (*cb) (int xok, X509_STORE_CTX *xctx) = ctx->verify_cb;
567 int num = sk_X509_num(ctx->chain);
571 * Check trusted certificates in chain at depth num_untrusted and up.
572 * Note, that depths 0..num_untrusted-1 may also contain trusted
573 * certificates, but the caller is expected to have already checked those,
574 * and wants to incrementally check just any added since.
576 for (i = num_untrusted; i < num; i++) {
577 x = sk_X509_value(ctx->chain, i);
578 trust = X509_check_trust(x, ctx->param->trust, 0);
579 /* If explicitly trusted return trusted */
580 if (trust == X509_TRUST_TRUSTED)
582 if (trust == X509_TRUST_REJECTED)
587 * If we are looking at a trusted certificate, and accept partial chains,
588 * the chain is PKIX trusted.
590 if (num_untrusted < num) {
591 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN)
593 return X509_TRUST_UNTRUSTED;
596 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
598 * Last-resort call with no new trusted certificates, check the leaf
599 * for a direct trust store match.
601 x = sk_X509_value(ctx->chain, 0);
602 mx = lookup_cert_match(ctx, x);
604 return X509_TRUST_UNTRUSTED;
607 * Check explicit auxiliary trust/reject settings. If none are set,
608 * we'll accept X509_TRUST_UNTRUSTED when not self-signed.
610 trust = X509_check_trust(mx, ctx->param->trust, 0);
611 if (trust == X509_TRUST_REJECTED) {
616 /* Replace leaf with trusted match */
617 (void) sk_X509_set(ctx->chain, 0, mx);
619 ctx->num_untrusted = 0;
624 * If no trusted certs in chain at all return untrusted and allow
625 * standard (no issuer cert) etc errors to be indicated.
627 return X509_TRUST_UNTRUSTED;
630 ctx->error_depth = i;
631 ctx->current_cert = x;
632 ctx->error = X509_V_ERR_CERT_REJECTED;
635 return X509_TRUST_REJECTED;
636 return X509_TRUST_UNTRUSTED;
639 return X509_TRUST_TRUSTED;
642 static int check_revocation(X509_STORE_CTX *ctx)
644 int i = 0, last = 0, ok = 0;
645 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
647 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
648 last = sk_X509_num(ctx->chain) - 1;
650 /* If checking CRL paths this isn't the EE certificate */
655 for (i = 0; i <= last; i++) {
656 ctx->error_depth = i;
657 ok = check_cert(ctx);
664 static int check_cert(X509_STORE_CTX *ctx)
666 X509_CRL *crl = NULL, *dcrl = NULL;
668 int ok = 0, cnum = 0;
669 unsigned int last_reasons = 0;
670 cnum = ctx->error_depth;
671 x = sk_X509_value(ctx->chain, cnum);
672 ctx->current_cert = x;
673 ctx->current_issuer = NULL;
674 ctx->current_crl_score = 0;
675 ctx->current_reasons = 0;
676 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
677 last_reasons = ctx->current_reasons;
678 /* Try to retrieve relevant CRL */
680 ok = ctx->get_crl(ctx, &crl, x);
682 ok = get_crl_delta(ctx, &crl, &dcrl, x);
684 * If error looking up CRL, nothing we can do except notify callback
687 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
688 ok = ctx->verify_cb(0, ctx);
691 ctx->current_crl = crl;
692 ok = ctx->check_crl(ctx, crl);
697 ok = ctx->check_crl(ctx, dcrl);
700 ok = ctx->cert_crl(ctx, dcrl, x);
706 /* Don't look in full CRL if delta reason is removefromCRL */
708 ok = ctx->cert_crl(ctx, crl, x);
718 * If reasons not updated we wont get anywhere by another iteration,
721 if (last_reasons == ctx->current_reasons) {
722 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
723 ok = ctx->verify_cb(0, ctx);
731 ctx->current_crl = NULL;
736 /* Check CRL times against values in X509_STORE_CTX */
738 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
743 ctx->current_crl = crl;
744 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
745 ptime = &ctx->param->check_time;
746 else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
751 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
755 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
756 if (!ctx->verify_cb(0, ctx))
763 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
764 if (!ctx->verify_cb(0, ctx))
768 if (X509_CRL_get_nextUpdate(crl)) {
769 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
774 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
775 if (!ctx->verify_cb(0, ctx))
778 /* Ignore expiry of base CRL is delta is valid */
779 if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
782 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
783 if (!ctx->verify_cb(0, ctx))
789 ctx->current_crl = NULL;
794 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
795 X509 **pissuer, int *pscore, unsigned int *preasons,
796 STACK_OF(X509_CRL) *crls)
798 int i, crl_score, best_score = *pscore;
799 unsigned int reasons, best_reasons = 0;
800 X509 *x = ctx->current_cert;
801 X509_CRL *crl, *best_crl = NULL;
802 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
804 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
805 crl = sk_X509_CRL_value(crls, i);
807 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
809 if (crl_score > best_score) {
811 best_crl_issuer = crl_issuer;
812 best_score = crl_score;
813 best_reasons = reasons;
818 X509_CRL_free(*pcrl);
820 *pissuer = best_crl_issuer;
821 *pscore = best_score;
822 *preasons = best_reasons;
823 X509_CRL_up_ref(best_crl);
824 X509_CRL_free(*pdcrl);
826 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
829 if (best_score >= CRL_SCORE_VALID)
836 * Compare two CRL extensions for delta checking purposes. They should be
837 * both present or both absent. If both present all fields must be identical.
840 static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
842 ASN1_OCTET_STRING *exta, *extb;
844 i = X509_CRL_get_ext_by_NID(a, nid, -1);
846 /* Can't have multiple occurrences */
847 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
849 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
853 i = X509_CRL_get_ext_by_NID(b, nid, -1);
857 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
859 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
869 if (ASN1_OCTET_STRING_cmp(exta, extb))
875 /* See if a base and delta are compatible */
877 static int check_delta_base(X509_CRL *delta, X509_CRL *base)
879 /* Delta CRL must be a delta */
880 if (!delta->base_crl_number)
882 /* Base must have a CRL number */
883 if (!base->crl_number)
885 /* Issuer names must match */
886 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
888 /* AKID and IDP must match */
889 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
891 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
893 /* Delta CRL base number must not exceed Full CRL number. */
894 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
896 /* Delta CRL number must exceed full CRL number */
897 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
903 * For a given base CRL find a delta... maybe extend to delta scoring or
904 * retrieve a chain of deltas...
907 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
908 X509_CRL *base, STACK_OF(X509_CRL) *crls)
912 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
914 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
916 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
917 delta = sk_X509_CRL_value(crls, i);
918 if (check_delta_base(delta, base)) {
919 if (check_crl_time(ctx, delta, 0))
920 *pscore |= CRL_SCORE_TIME_DELTA;
921 X509_CRL_up_ref(delta);
930 * For a given CRL return how suitable it is for the supplied certificate
931 * 'x'. The return value is a mask of several criteria. If the issuer is not
932 * the certificate issuer this is returned in *pissuer. The reasons mask is
933 * also used to determine if the CRL is suitable: if no new reasons the CRL
934 * is rejected, otherwise reasons is updated.
937 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
938 unsigned int *preasons, X509_CRL *crl, X509 *x)
942 unsigned int tmp_reasons = *preasons, crl_reasons;
944 /* First see if we can reject CRL straight away */
946 /* Invalid IDP cannot be processed */
947 if (crl->idp_flags & IDP_INVALID)
949 /* Reason codes or indirect CRLs need extended CRL support */
950 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
951 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
953 } else if (crl->idp_flags & IDP_REASONS) {
954 /* If no new reasons reject */
955 if (!(crl->idp_reasons & ~tmp_reasons))
958 /* Don't process deltas at this stage */
959 else if (crl->base_crl_number)
961 /* If issuer name doesn't match certificate need indirect CRL */
962 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
963 if (!(crl->idp_flags & IDP_INDIRECT))
966 crl_score |= CRL_SCORE_ISSUER_NAME;
968 if (!(crl->flags & EXFLAG_CRITICAL))
969 crl_score |= CRL_SCORE_NOCRITICAL;
972 if (check_crl_time(ctx, crl, 0))
973 crl_score |= CRL_SCORE_TIME;
975 /* Check authority key ID and locate certificate issuer */
976 crl_akid_check(ctx, crl, pissuer, &crl_score);
978 /* If we can't locate certificate issuer at this point forget it */
980 if (!(crl_score & CRL_SCORE_AKID))
983 /* Check cert for matching CRL distribution points */
985 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
986 /* If no new reasons reject */
987 if (!(crl_reasons & ~tmp_reasons))
989 tmp_reasons |= crl_reasons;
990 crl_score |= CRL_SCORE_SCOPE;
993 *preasons = tmp_reasons;
999 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
1000 X509 **pissuer, int *pcrl_score)
1002 X509 *crl_issuer = NULL;
1003 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1004 int cidx = ctx->error_depth;
1007 if (cidx != sk_X509_num(ctx->chain) - 1)
1010 crl_issuer = sk_X509_value(ctx->chain, cidx);
1012 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1013 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1014 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
1015 *pissuer = crl_issuer;
1020 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1021 crl_issuer = sk_X509_value(ctx->chain, cidx);
1022 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1024 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1025 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
1026 *pissuer = crl_issuer;
1031 /* Anything else needs extended CRL support */
1033 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1037 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1038 * untrusted certificates.
1040 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1041 crl_issuer = sk_X509_value(ctx->untrusted, i);
1042 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1044 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1045 *pissuer = crl_issuer;
1046 *pcrl_score |= CRL_SCORE_AKID;
1053 * Check the path of a CRL issuer certificate. This creates a new
1054 * X509_STORE_CTX and populates it with most of the parameters from the
1055 * parent. This could be optimised somewhat since a lot of path checking will
1056 * be duplicated by the parent, but this will rarely be used in practice.
1059 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1061 X509_STORE_CTX crl_ctx;
1063 /* Don't allow recursive CRL path validation */
1066 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
1069 crl_ctx.crls = ctx->crls;
1070 /* Copy verify params across */
1071 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1073 crl_ctx.parent = ctx;
1074 crl_ctx.verify_cb = ctx->verify_cb;
1076 /* Verify CRL issuer */
1077 ret = X509_verify_cert(&crl_ctx);
1082 /* Check chain is acceptable */
1084 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1086 X509_STORE_CTX_cleanup(&crl_ctx);
1091 * RFC3280 says nothing about the relationship between CRL path and
1092 * certificate path, which could lead to situations where a certificate could
1093 * be revoked or validated by a CA not authorised to do so. RFC5280 is more
1094 * strict and states that the two paths must end in the same trust anchor,
1095 * though some discussions remain... until this is resolved we use the
1099 static int check_crl_chain(X509_STORE_CTX *ctx,
1100 STACK_OF(X509) *cert_path,
1101 STACK_OF(X509) *crl_path)
1103 X509 *cert_ta, *crl_ta;
1104 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1105 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1106 if (!X509_cmp(cert_ta, crl_ta))
1112 * Check for match between two dist point names: three separate cases.
1113 * 1. Both are relative names and compare X509_NAME types.
1114 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1115 * 3. Both are full names and compare two GENERAL_NAMES.
1116 * 4. One is NULL: automatic match.
1119 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1121 X509_NAME *nm = NULL;
1122 GENERAL_NAMES *gens = NULL;
1123 GENERAL_NAME *gena, *genb;
1130 /* Case 1: two X509_NAME */
1134 if (!X509_NAME_cmp(a->dpname, b->dpname))
1139 /* Case 2: set name and GENERAL_NAMES appropriately */
1141 gens = b->name.fullname;
1142 } else if (b->type == 1) {
1145 /* Case 2: set name and GENERAL_NAMES appropriately */
1146 gens = a->name.fullname;
1150 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1152 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1153 gena = sk_GENERAL_NAME_value(gens, i);
1154 if (gena->type != GEN_DIRNAME)
1156 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1162 /* Else case 3: two GENERAL_NAMES */
1164 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1165 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1166 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1167 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1168 if (!GENERAL_NAME_cmp(gena, genb))
1177 static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1180 X509_NAME *nm = X509_CRL_get_issuer(crl);
1181 /* If no CRLissuer return is successful iff don't need a match */
1183 return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
1184 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1185 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1186 if (gen->type != GEN_DIRNAME)
1188 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1194 /* Check CRLDP and IDP */
1196 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
1197 unsigned int *preasons)
1200 if (crl->idp_flags & IDP_ONLYATTR)
1202 if (x->ex_flags & EXFLAG_CA) {
1203 if (crl->idp_flags & IDP_ONLYUSER)
1206 if (crl->idp_flags & IDP_ONLYCA)
1209 *preasons = crl->idp_reasons;
1210 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1211 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1212 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1213 if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1214 *preasons &= dp->dp_reasons;
1219 if ((!crl->idp || !crl->idp->distpoint)
1220 && (crl_score & CRL_SCORE_ISSUER_NAME))
1226 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1227 * to find a delta CRL too
1230 static int get_crl_delta(X509_STORE_CTX *ctx,
1231 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1234 X509 *issuer = NULL;
1236 unsigned int reasons;
1237 X509_CRL *crl = NULL, *dcrl = NULL;
1238 STACK_OF(X509_CRL) *skcrl;
1239 X509_NAME *nm = X509_get_issuer_name(x);
1240 reasons = ctx->current_reasons;
1241 ok = get_crl_sk(ctx, &crl, &dcrl,
1242 &issuer, &crl_score, &reasons, ctx->crls);
1247 /* Lookup CRLs from store */
1249 skcrl = ctx->lookup_crls(ctx, nm);
1251 /* If no CRLs found and a near match from get_crl_sk use that */
1255 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1257 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1261 /* If we got any kind of CRL use it and return success */
1263 ctx->current_issuer = issuer;
1264 ctx->current_crl_score = crl_score;
1265 ctx->current_reasons = reasons;
1274 /* Check CRL validity */
1275 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1277 X509 *issuer = NULL;
1278 EVP_PKEY *ikey = NULL;
1279 int ok = 0, chnum, cnum;
1280 cnum = ctx->error_depth;
1281 chnum = sk_X509_num(ctx->chain) - 1;
1282 /* if we have an alternative CRL issuer cert use that */
1283 if (ctx->current_issuer)
1284 issuer = ctx->current_issuer;
1287 * Else find CRL issuer: if not last certificate then issuer is next
1288 * certificate in chain.
1290 else if (cnum < chnum)
1291 issuer = sk_X509_value(ctx->chain, cnum + 1);
1293 issuer = sk_X509_value(ctx->chain, chnum);
1294 /* If not self signed, can't check signature */
1295 if (!ctx->check_issued(ctx, issuer, issuer)) {
1296 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1297 ok = ctx->verify_cb(0, ctx);
1305 * Skip most tests for deltas because they have already been done
1307 if (!crl->base_crl_number) {
1308 /* Check for cRLSign bit if keyUsage present */
1309 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1310 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1311 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1312 ok = ctx->verify_cb(0, ctx);
1317 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1318 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1319 ok = ctx->verify_cb(0, ctx);
1324 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1325 if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
1326 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1327 ok = ctx->verify_cb(0, ctx);
1333 if (crl->idp_flags & IDP_INVALID) {
1334 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1335 ok = ctx->verify_cb(0, ctx);
1342 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1343 ok = check_crl_time(ctx, crl, 1);
1348 /* Attempt to get issuer certificate public key */
1349 ikey = X509_get0_pubkey(issuer);
1352 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1353 ok = ctx->verify_cb(0, ctx);
1358 rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
1359 if (rv != X509_V_OK) {
1361 ok = ctx->verify_cb(0, ctx);
1365 /* Verify CRL signature */
1366 if (X509_CRL_verify(crl, ikey) <= 0) {
1367 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1368 ok = ctx->verify_cb(0, ctx);
1381 /* Check certificate against CRL */
1382 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1387 * The rules changed for this... previously if a CRL contained unhandled
1388 * critical extensions it could still be used to indicate a certificate
1389 * was revoked. This has since been changed since critical extension can
1390 * change the meaning of CRL entries.
1392 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
1393 && (crl->flags & EXFLAG_CRITICAL)) {
1394 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1395 ok = ctx->verify_cb(0, ctx);
1400 * Look for serial number of certificate in CRL If found make sure reason
1401 * is not removeFromCRL.
1403 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1404 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1406 ctx->error = X509_V_ERR_CERT_REVOKED;
1407 ok = ctx->verify_cb(0, ctx);
1415 static int check_policy(X509_STORE_CTX *ctx)
1420 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1421 ctx->param->policies, ctx->param->flags);
1423 X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
1426 /* Invalid or inconsistent extensions */
1429 * Locate certificates with bad extensions and notify callback.
1433 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1434 x = sk_X509_value(ctx->chain, i);
1435 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1437 ctx->current_cert = x;
1438 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1439 if (!ctx->verify_cb(0, ctx))
1445 ctx->current_cert = NULL;
1446 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1447 return ctx->verify_cb(0, ctx);
1450 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1451 ctx->current_cert = NULL;
1452 ctx->error = X509_V_OK;
1453 if (!ctx->verify_cb(2, ctx))
1460 int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int quiet)
1465 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1466 ptime = &ctx->param->check_time;
1467 else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
1472 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1476 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1477 ctx->current_cert = x;
1478 if (!ctx->verify_cb(0, ctx))
1485 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1486 ctx->current_cert = x;
1487 if (!ctx->verify_cb(0, ctx))
1491 i = X509_cmp_time(X509_get_notAfter(x), ptime);
1495 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1496 ctx->current_cert = x;
1497 if (!ctx->verify_cb(0, ctx))
1504 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1505 ctx->current_cert = x;
1506 if (!ctx->verify_cb(0, ctx))
1513 static int internal_verify(X509_STORE_CTX *ctx)
1517 EVP_PKEY *pkey = NULL;
1518 int (*cb) (int xok, X509_STORE_CTX *xctx);
1520 cb = ctx->verify_cb;
1522 n = sk_X509_num(ctx->chain) - 1;
1523 ctx->error_depth = n;
1524 xi = sk_X509_value(ctx->chain, n);
1526 if (ctx->check_issued(ctx, xi, xi))
1529 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1534 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1535 ctx->current_cert = xi;
1540 ctx->error_depth = n;
1541 xs = sk_X509_value(ctx->chain, n);
1546 * Do not clear ctx->error=0, it must be "sticky", only the user's callback
1547 * is allowed to reset errors (at its own peril).
1550 ctx->error_depth = n;
1553 * Skip signature check for self signed certificates unless
1554 * explicitly asked for. It doesn't add any security and just wastes
1559 || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
1560 if ((pkey = X509_get0_pubkey(xi)) == NULL) {
1561 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1562 ctx->current_cert = xi;
1563 ok = (*cb) (0, ctx);
1566 } else if (X509_verify(xs, pkey) <= 0) {
1567 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1568 ctx->current_cert = xs;
1569 ok = (*cb) (0, ctx);
1578 ok = x509_check_cert_time(ctx, xs, 0);
1582 /* The last error (if any) is still in the error value */
1583 ctx->current_issuer = xi;
1584 ctx->current_cert = xs;
1585 ok = (*cb) (1, ctx);
1592 xs = sk_X509_value(ctx->chain, n);
1600 int X509_cmp_current_time(const ASN1_TIME *ctm)
1602 return X509_cmp_time(ctm, NULL);
1605 int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1610 char buff1[24], buff2[24], *p;
1611 int i, j, remaining;
1614 remaining = ctm->length;
1615 str = (char *)ctm->data;
1617 * Note that the following (historical) code allows much more slack in the
1618 * time format than RFC5280. In RFC5280, the representation is fixed:
1619 * UTCTime: YYMMDDHHMMSSZ
1620 * GeneralizedTime: YYYYMMDDHHMMSSZ
1622 if (ctm->type == V_ASN1_UTCTIME) {
1623 /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
1624 int min_length = sizeof("YYMMDDHHMMZ") - 1;
1625 int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
1626 if (remaining < min_length || remaining > max_length)
1633 /* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
1634 int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
1635 int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
1636 if (remaining < min_length || remaining > max_length)
1644 if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
1655 * Skip any (up to three) fractional seconds...
1656 * TODO(emilia): in RFC5280, fractional seconds are forbidden.
1657 * Can we just kill them altogether?
1659 if (remaining && *str == '.') {
1662 for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
1663 if (*str < '0' || *str > '9')
1672 /* We now need either a terminating 'Z' or an offset. */
1681 if ((*str != '+') && (*str != '-'))
1683 /* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
1686 if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
1687 str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
1689 offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
1690 offset += (str[3] - '0') * 10 + (str[4] - '0');
1694 atm.type = ctm->type;
1696 atm.length = sizeof(buff2);
1697 atm.data = (unsigned char *)buff2;
1699 if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
1702 if (ctm->type == V_ASN1_UTCTIME) {
1703 i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
1705 i += 100; /* cf. RFC 2459 */
1706 j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
1715 i = strcmp(buff1, buff2);
1716 if (i == 0) /* wait a second then return younger :-) */
1722 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
1724 return X509_time_adj(s, adj, NULL);
1727 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
1729 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1732 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
1733 int offset_day, long offset_sec, time_t *in_tm)
1742 if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
1743 if (s->type == V_ASN1_UTCTIME)
1744 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
1745 if (s->type == V_ASN1_GENERALIZEDTIME)
1746 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
1748 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1751 int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1753 EVP_PKEY *ktmp = NULL, *ktmp2;
1756 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1759 for (i = 0; i < sk_X509_num(chain); i++) {
1760 ktmp = X509_get0_pubkey(sk_X509_value(chain, i));
1762 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1763 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1766 if (!EVP_PKEY_missing_parameters(ktmp))
1770 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1771 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1775 /* first, populate the other certs */
1776 for (j = i - 1; j >= 0; j--) {
1777 ktmp2 = X509_get0_pubkey(sk_X509_value(chain, j));
1778 EVP_PKEY_copy_parameters(ktmp2, ktmp);
1782 EVP_PKEY_copy_parameters(pkey, ktmp);
1786 /* Make a delta CRL as the diff between two full CRLs */
1788 X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
1789 EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
1791 X509_CRL *crl = NULL;
1793 STACK_OF(X509_REVOKED) *revs = NULL;
1794 /* CRLs can't be delta already */
1795 if (base->base_crl_number || newer->base_crl_number) {
1796 X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_ALREADY_DELTA);
1799 /* Base and new CRL must have a CRL number */
1800 if (!base->crl_number || !newer->crl_number) {
1801 X509err(X509_F_X509_CRL_DIFF, X509_R_NO_CRL_NUMBER);
1804 /* Issuer names must match */
1805 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
1806 X509err(X509_F_X509_CRL_DIFF, X509_R_ISSUER_MISMATCH);
1809 /* AKID and IDP must match */
1810 if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
1811 X509err(X509_F_X509_CRL_DIFF, X509_R_AKID_MISMATCH);
1814 if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
1815 X509err(X509_F_X509_CRL_DIFF, X509_R_IDP_MISMATCH);
1818 /* Newer CRL number must exceed full CRL number */
1819 if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
1820 X509err(X509_F_X509_CRL_DIFF, X509_R_NEWER_CRL_NOT_NEWER);
1823 /* CRLs must verify */
1824 if (skey && (X509_CRL_verify(base, skey) <= 0 ||
1825 X509_CRL_verify(newer, skey) <= 0)) {
1826 X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_VERIFY_FAILURE);
1829 /* Create new CRL */
1830 crl = X509_CRL_new();
1831 if (crl == NULL || !X509_CRL_set_version(crl, 1))
1833 /* Set issuer name */
1834 if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
1837 if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
1839 if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
1842 /* Set base CRL number: must be critical */
1844 if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
1848 * Copy extensions across from newest CRL to delta: this will set CRL
1849 * number to correct value too.
1852 for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
1853 X509_EXTENSION *ext;
1854 ext = X509_CRL_get_ext(newer, i);
1855 if (!X509_CRL_add_ext(crl, ext, -1))
1859 /* Go through revoked entries, copying as needed */
1861 revs = X509_CRL_get_REVOKED(newer);
1863 for (i = 0; i < sk_X509_REVOKED_num(revs); i++) {
1864 X509_REVOKED *rvn, *rvtmp;
1865 rvn = sk_X509_REVOKED_value(revs, i);
1867 * Add only if not also in base. TODO: need something cleverer here
1868 * for some more complex CRLs covering multiple CAs.
1870 if (!X509_CRL_get0_by_serial(base, &rvtmp, &rvn->serialNumber)) {
1871 rvtmp = X509_REVOKED_dup(rvn);
1874 if (!X509_CRL_add0_revoked(crl, rvtmp)) {
1875 X509_REVOKED_free(rvtmp);
1880 /* TODO: optionally prune deleted entries */
1882 if (skey && md && !X509_CRL_sign(crl, skey, md))
1888 X509err(X509_F_X509_CRL_DIFF, ERR_R_MALLOC_FAILURE);
1893 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
1895 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1898 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
1900 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1903 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
1908 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
1913 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
1915 return ctx->error_depth;
1918 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
1920 return ctx->current_cert;
1923 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
1928 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
1932 return X509_chain_up_ref(ctx->chain);
1935 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
1937 return ctx->current_issuer;
1940 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
1942 return ctx->current_crl;
1945 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
1950 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
1955 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1957 ctx->untrusted = sk;
1960 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
1965 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
1967 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
1970 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
1972 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
1976 * This function is used to set the X509_STORE_CTX purpose and trust values.
1977 * This is intended to be used when another structure has its own trust and
1978 * purpose values which (if set) will be inherited by the ctx. If they aren't
1979 * set then we will usually have a default purpose in mind which should then
1980 * be used to set the trust value. An example of this is SSL use: an SSL
1981 * structure will have its own purpose and trust settings which the
1982 * application can set: if they aren't set then we use the default of SSL
1986 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
1987 int purpose, int trust)
1990 /* If purpose not set use default */
1992 purpose = def_purpose;
1993 /* If we have a purpose then check it is valid */
1996 idx = X509_PURPOSE_get_by_id(purpose);
1998 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1999 X509_R_UNKNOWN_PURPOSE_ID);
2002 ptmp = X509_PURPOSE_get0(idx);
2003 if (ptmp->trust == X509_TRUST_DEFAULT) {
2004 idx = X509_PURPOSE_get_by_id(def_purpose);
2006 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
2007 X509_R_UNKNOWN_PURPOSE_ID);
2010 ptmp = X509_PURPOSE_get0(idx);
2012 /* If trust not set then get from purpose default */
2014 trust = ptmp->trust;
2017 idx = X509_TRUST_get_by_id(trust);
2019 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
2020 X509_R_UNKNOWN_TRUST_ID);
2025 if (purpose && !ctx->param->purpose)
2026 ctx->param->purpose = purpose;
2027 if (trust && !ctx->param->trust)
2028 ctx->param->trust = trust;
2032 X509_STORE_CTX *X509_STORE_CTX_new(void)
2034 X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
2037 X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
2043 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
2047 X509_STORE_CTX_cleanup(ctx);
2051 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
2052 STACK_OF(X509) *chain)
2057 ctx->current_method = 0;
2059 ctx->untrusted = chain;
2061 ctx->num_untrusted = 0;
2062 ctx->other_ctx = NULL;
2066 ctx->explicit_policy = 0;
2067 ctx->error_depth = 0;
2068 ctx->current_cert = NULL;
2069 ctx->current_issuer = NULL;
2070 ctx->current_crl = NULL;
2071 ctx->current_crl_score = 0;
2072 ctx->current_reasons = 0;
2075 /* Zero ex_data to make sure we're cleanup-safe */
2076 memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
2079 ctx->verify_cb = store->verify_cb;
2080 /* Seems to always be 0 in OpenSSL, else must be idempotent */
2081 ctx->cleanup = store->cleanup;
2085 if (store && store->check_issued)
2086 ctx->check_issued = store->check_issued;
2088 ctx->check_issued = check_issued;
2090 if (store && store->get_issuer)
2091 ctx->get_issuer = store->get_issuer;
2093 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2095 if (store && store->verify_cb)
2096 ctx->verify_cb = store->verify_cb;
2098 ctx->verify_cb = null_callback;
2100 if (store && store->verify)
2101 ctx->verify = store->verify;
2103 ctx->verify = internal_verify;
2105 if (store && store->check_revocation)
2106 ctx->check_revocation = store->check_revocation;
2108 ctx->check_revocation = check_revocation;
2110 if (store && store->get_crl)
2111 ctx->get_crl = store->get_crl;
2113 if (store && store->check_crl)
2114 ctx->check_crl = store->check_crl;
2116 ctx->check_crl = check_crl;
2118 if (store && store->cert_crl)
2119 ctx->cert_crl = store->cert_crl;
2121 ctx->cert_crl = cert_crl;
2123 if (store && store->lookup_certs)
2124 ctx->lookup_certs = store->lookup_certs;
2126 ctx->lookup_certs = X509_STORE_get1_certs;
2128 if (store && store->lookup_crls)
2129 ctx->lookup_crls = store->lookup_crls;
2131 ctx->lookup_crls = X509_STORE_get1_crls;
2133 ctx->check_policy = check_policy;
2135 ctx->param = X509_VERIFY_PARAM_new();
2136 if (ctx->param == NULL) {
2137 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2142 * Inherit callbacks and flags from X509_STORE if not set use defaults.
2145 ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2147 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
2150 ret = X509_VERIFY_PARAM_inherit(ctx->param,
2151 X509_VERIFY_PARAM_lookup("default"));
2154 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2158 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2161 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2165 * On error clean up allocated storage, if the store context was not
2166 * allocated with X509_STORE_CTX_new() this is our last chance to do so.
2168 X509_STORE_CTX_cleanup(ctx);
2173 * Set alternative lookup method: just a STACK of trusted certificates. This
2174 * avoids X509_STORE nastiness where it isn't needed.
2177 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2179 ctx->other_ctx = sk;
2180 ctx->get_issuer = get_issuer_sk;
2183 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2186 * We need to be idempotent because, unfortunately, free() also calls
2187 * cleanup(), so the natural call sequence new(), init(), cleanup(), free()
2188 * calls cleanup() for the same object twice! Thus we must zero the
2189 * pointers below after they're freed!
2191 /* Seems to always be 0 in OpenSSL, do this at most once. */
2192 if (ctx->cleanup != NULL) {
2194 ctx->cleanup = NULL;
2196 if (ctx->param != NULL) {
2197 if (ctx->parent == NULL)
2198 X509_VERIFY_PARAM_free(ctx->param);
2201 X509_policy_tree_free(ctx->tree);
2203 sk_X509_pop_free(ctx->chain, X509_free);
2205 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
2206 memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
2209 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2211 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2214 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2216 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2219 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
2222 X509_VERIFY_PARAM_set_time(ctx->param, t);
2225 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2226 int (*verify_cb) (int, X509_STORE_CTX *))
2228 ctx->verify_cb = verify_cb;
2231 X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2236 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2238 return ctx->explicit_policy;
2241 int X509_STORE_CTX_get_num_untrusted(X509_STORE_CTX *ctx)
2243 return ctx->num_untrusted;
2246 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2248 const X509_VERIFY_PARAM *param;
2249 param = X509_VERIFY_PARAM_lookup(name);
2252 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2255 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2260 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2262 X509_VERIFY_PARAM_free(ctx->param);
2266 static int build_chain(X509_STORE_CTX *ctx)
2268 int (*cb) (int, X509_STORE_CTX *) = ctx->verify_cb;
2269 int num = sk_X509_num(ctx->chain);
2270 X509 *cert = sk_X509_value(ctx->chain, num - 1);
2271 int ss = cert_self_signed(cert);
2272 STACK_OF(X509) *sktmp = NULL;
2273 unsigned int search;
2274 int may_trusted = 1;
2275 int may_alternate = 0;
2276 int trust = X509_TRUST_UNTRUSTED;
2277 int alt_untrusted = 0;
2282 /* Our chain starts with a single untrusted element. */
2283 OPENSSL_assert(num == 1 && ctx->num_untrusted == num);
2285 #define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */
2286 #define S_DOTRUSTED (1 << 1) /* Search trusted store */
2287 #define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */
2289 * Set up search policy, untrusted if possible, trusted-first if enabled.
2290 * If not trusted-first, and alternate chains are not disabled, try
2291 * building an alternate chain if no luck with untrusted first.
2293 search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0;
2294 if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
2295 search |= S_DOTRUSTED;
2296 else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))
2300 * Shallow-copy the stack of untrusted certificates (with TLS, this is
2301 * typically the content of the peer's certificate message) so can make
2302 * multiple passes over it, while free to remove elements as we go.
2304 if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
2305 X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
2310 * Still absurdly large, but arithmetically safe, a lower hard upper bound
2311 * might be reasonable.
2313 if (ctx->param->depth > INT_MAX/2)
2314 ctx->param->depth = INT_MAX/2;
2317 * Try to Extend the chain until we reach an ultimately trusted issuer.
2318 * Build chains up to one longer the limit, later fail if we hit the limit,
2319 * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code.
2321 depth = ctx->param->depth + 1;
2323 while (search != 0) {
2328 * Look in the trust store if enabled for first lookup, or we've run
2329 * out of untrusted issuers and search here is not disabled. When
2330 * we exceed the depth limit, we simulate absence of a match.
2332 if ((search & S_DOTRUSTED) != 0) {
2333 STACK_OF(X509) *hide = ctx->chain;
2335 i = num = sk_X509_num(ctx->chain);
2336 if ((search & S_DOALTERNATE) != 0) {
2338 * As high up the chain as we can, look for an alternative
2339 * trusted issuer of an untrusted certificate that currently
2340 * has an untrusted issuer. We use the alt_untrusted variable
2341 * to track how far up the chain we find the first match. It
2342 * is only if and when we find a match, that we prune the chain
2343 * and reset ctx->num_untrusted to the reduced count of
2344 * untrusted certificates. While we're searching for such a
2345 * match (which may never be found), it is neither safe nor
2346 * wise to preemptively modify either the chain or
2347 * ctx->num_untrusted.
2349 * Note, like ctx->num_untrusted, alt_untrusted is a count of
2350 * untrusted certificates, not a "depth".
2354 x = sk_X509_value(ctx->chain, i-1);
2356 /* Suppress duplicate suppression */
2358 ok = (depth < num) ? 0 : ctx->get_issuer(&xtmp, ctx, x);
2362 trust = X509_TRUST_REJECTED;
2369 * Alternative trusted issuer for a mid-chain untrusted cert?
2370 * Pop the untrusted cert's successors and retry. We might now
2371 * be able to complete a valid chain via the trust store. Note
2372 * that despite the current trust-store match we might still
2373 * fail complete the chain to a suitable trust-anchor, in which
2374 * case we may prune some more untrusted certificates and try
2375 * again. Thus the S_DOALTERNATE bit may yet be turned on
2376 * again with an even shorter untrusted chain!
2378 if ((search & S_DOALTERNATE) != 0) {
2379 OPENSSL_assert(num > i && i > 0 && ss == 0);
2380 search &= ~S_DOALTERNATE;
2381 for (; num > i; --num)
2382 X509_free(sk_X509_pop(ctx->chain));
2383 ctx->num_untrusted = num;
2387 * Self-signed untrusted certificates get replaced by their
2388 * trusted matching issuer. Otherwise, grow the chain.
2391 if (!sk_X509_push(ctx->chain, x = xtmp)) {
2393 X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
2394 trust = X509_TRUST_REJECTED;
2398 ss = cert_self_signed(x);
2399 } else if (num == ctx->num_untrusted) {
2401 * We have a self-signed certificate that has the same
2402 * subject name (and perhaps keyid and/or serial number) as
2403 * a trust-anchor. We must have an exact match to avoid
2404 * possible impersonation via key substitution etc.
2406 if (X509_cmp(x, xtmp) != 0) {
2407 /* Self-signed untrusted mimic. */
2412 ctx->num_untrusted = --num;
2413 (void) sk_X509_set(ctx->chain, num, x = xtmp);
2418 * We've added a new trusted certificate to the chain, recheck
2419 * trust. If not done, and not self-signed look deeper.
2420 * Whether or not we're doing "trusted first", we no longer
2421 * look for untrusted certificates from the peer's chain.
2424 OPENSSL_assert(ctx->num_untrusted <= num);
2425 search &= ~S_DOUNTRUSTED;
2426 switch (trust = check_trust(ctx, num)) {
2427 case X509_TRUST_TRUSTED:
2428 case X509_TRUST_REJECTED:
2438 * No dispositive decision, and either self-signed or no match, if
2439 * we were doing untrusted-first, and alt-chains are not disabled,
2440 * do that, by repeatedly losing one untrusted element at a time,
2441 * and trying to extend the shorted chain.
2443 if ((search & S_DOUNTRUSTED) == 0) {
2444 /* Continue search for a trusted issuer of a shorter chain? */
2445 if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0)
2447 /* Still no luck and no fallbacks left? */
2448 if (!may_alternate || (search & S_DOALTERNATE) != 0 ||
2449 ctx->num_untrusted < 2)
2451 /* Search for a trusted issuer of a shorter chain */
2452 search |= S_DOALTERNATE;
2453 alt_untrusted = ctx->num_untrusted - 1;
2459 * Extend chain with peer-provided certificates
2461 if ((search & S_DOUNTRUSTED) != 0) {
2462 num = sk_X509_num(ctx->chain);
2463 OPENSSL_assert(num == ctx->num_untrusted);
2464 x = sk_X509_value(ctx->chain, num-1);
2465 xtmp = (depth < num) ? NULL : find_issuer(ctx, sktmp, x);
2468 * Once we run out of untrusted issuers, we stop looking for more
2469 * and start looking only in the trust store if enabled.
2472 search &= ~S_DOUNTRUSTED;
2474 search |= S_DOTRUSTED;
2478 if (!sk_X509_push(ctx->chain, x = xtmp)) {
2479 X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
2480 trust = X509_TRUST_REJECTED;
2485 ++ctx->num_untrusted;
2486 ss = cert_self_signed(xtmp);
2489 * Not strictly necessary, but saves cycles looking at the same
2490 * certificates over and over.
2492 (void) sk_X509_delete_ptr(sktmp, x);
2495 sk_X509_free(sktmp);
2498 * Last chance to make a trusted chain, check for direct leaf PKIX trust.
2500 if (sk_X509_num(ctx->chain) <= depth) {
2501 if (trust == X509_TRUST_UNTRUSTED &&
2502 sk_X509_num(ctx->chain) == ctx->num_untrusted)
2503 trust = check_trust(ctx, 1);
2507 case X509_TRUST_TRUSTED:
2509 case X509_TRUST_REJECTED:
2511 case X509_TRUST_UNTRUSTED:
2513 num = sk_X509_num(ctx->chain);
2514 ctx->current_cert = sk_X509_value(ctx->chain, num - 1);
2515 ctx->error_depth = num-1;
2517 ctx->error = X509_V_ERR_CERT_CHAIN_TOO_LONG;
2518 else if (ss && sk_X509_num(ctx->chain) == 1)
2519 ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
2521 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
2522 else if (ctx->num_untrusted == num)
2523 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
2525 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;