1 /* $OpenBSD: x509_vfy.c,v 1.81.4.1 2021/02/03 07:06:13 tb Exp $ */
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.]
65 #include <openssl/opensslconf.h>
67 #include <openssl/asn1.h>
68 #include <openssl/buffer.h>
69 #include <openssl/crypto.h>
70 #include <openssl/err.h>
71 #include <openssl/evp.h>
72 #include <openssl/lhash.h>
73 #include <openssl/objects.h>
74 #include <openssl/x509.h>
75 #include <openssl/x509v3.h>
76 #include "asn1_locl.h"
78 #include "x509_internal.h"
80 #include "x509_internal.h"
82 /* CRL score values */
84 /* No unhandled critical extensions */
86 #define CRL_SCORE_NOCRITICAL 0x100
88 /* certificate is within CRL scope */
90 #define CRL_SCORE_SCOPE 0x080
94 #define CRL_SCORE_TIME 0x040
96 /* Issuer name matches certificate */
98 #define CRL_SCORE_ISSUER_NAME 0x020
100 /* If this score or above CRL is probably valid */
102 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
104 /* CRL issuer is certificate issuer */
106 #define CRL_SCORE_ISSUER_CERT 0x018
108 /* CRL issuer is on certificate path */
110 #define CRL_SCORE_SAME_PATH 0x008
112 /* CRL issuer matches CRL AKID */
114 #define CRL_SCORE_AKID 0x004
116 /* Have a delta CRL with valid times */
118 #define CRL_SCORE_TIME_DELTA 0x002
120 static int null_callback(int ok, X509_STORE_CTX *e);
121 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
122 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x,
124 static int check_chain_extensions(X509_STORE_CTX *ctx);
125 static int check_name_constraints(X509_STORE_CTX *ctx);
126 static int check_trust(X509_STORE_CTX *ctx);
127 static int check_revocation(X509_STORE_CTX *ctx);
128 static int check_cert(X509_STORE_CTX *ctx, STACK_OF(X509) *chain, int depth);
129 static int check_policy(X509_STORE_CTX *ctx);
131 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
132 unsigned int *preasons, X509_CRL *crl, X509 *x);
133 static int get_crl_delta(X509_STORE_CTX *ctx,
134 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
135 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pcrl_score,
136 X509_CRL *base, STACK_OF(X509_CRL) *crls);
137 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
139 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
140 unsigned int *preasons);
141 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
142 static int check_crl_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *cert_path,
143 STACK_OF(X509) *crl_path);
144 static int X509_cmp_time_internal(const ASN1_TIME *ctm, time_t *cmp_time,
147 static int internal_verify(X509_STORE_CTX *ctx);
148 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
150 int ASN1_time_tm_clamp_notafter(struct tm *tm);
153 null_callback(int ok, X509_STORE_CTX *e)
160 x509_subject_cmp(X509 **a, X509 **b)
162 return X509_subject_name_cmp(*a, *b);
166 /* Return 1 if a certificate is self signed */
168 cert_self_signed(X509 *x)
170 X509_check_purpose(x, -1, 0);
171 if (x->ex_flags & EXFLAG_SS)
178 check_id_error(X509_STORE_CTX *ctx, int errcode)
180 ctx->error = errcode;
181 ctx->current_cert = ctx->cert;
182 ctx->error_depth = 0;
183 return ctx->verify_cb(0, ctx);
187 check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
192 n = sk_OPENSSL_STRING_num(id->hosts);
196 for (i = 0; i < n; ++i) {
197 name = sk_OPENSSL_STRING_value(id->hosts, i);
198 if (X509_check_host(x, name, strlen(name), id->hostflags,
206 check_id(X509_STORE_CTX *ctx)
208 X509_VERIFY_PARAM *vpm = ctx->param;
209 X509_VERIFY_PARAM_ID *id = vpm->id;
212 if (id->hosts && check_hosts(x, id) <= 0) {
213 if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
216 if (id->email != NULL && X509_check_email(x, id->email, id->emaillen, 0)
218 if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
221 if (id->ip != NULL && X509_check_ip(x, id->ip, id->iplen, 0) <= 0) {
222 if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
229 x509_vfy_check_id(X509_STORE_CTX *ctx) {
230 return check_id(ctx);
234 * This is the effectively broken legacy OpenSSL chain builder. It
235 * might find an unvalidated chain and leave it sitting in
236 * ctx->chain. It does not correctly handle many cases where multiple
237 * chains could exist.
239 * Oh no.. I know a dirty word...
243 X509_verify_cert_legacy_build_chain(X509_STORE_CTX *ctx, int *bad)
245 X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
247 X509_VERIFY_PARAM *param = ctx->param;
248 int depth, i, ok = 0;
249 int num, j, retry, trust;
250 int (*cb) (int xok, X509_STORE_CTX *xctx);
251 STACK_OF(X509) *sktmp = NULL;
256 * First we make sure the chain we are going to build is
257 * present and that the first entry is in place.
259 ctx->chain = sk_X509_new_null();
260 if (ctx->chain == NULL || !sk_X509_push(ctx->chain, ctx->cert)) {
261 X509error(ERR_R_MALLOC_FAILURE);
262 ctx->error = X509_V_ERR_OUT_OF_MEM;
265 X509_up_ref(ctx->cert);
266 ctx->last_untrusted = 1;
268 /* We use a temporary STACK so we can chop and hack at it */
269 if (ctx->untrusted != NULL &&
270 (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
271 X509error(ERR_R_MALLOC_FAILURE);
272 ctx->error = X509_V_ERR_OUT_OF_MEM;
276 num = sk_X509_num(ctx->chain);
277 x = sk_X509_value(ctx->chain, num - 1);
278 depth = param->depth;
281 /* If we have enough, we break */
282 /* FIXME: If this happens, we should take
283 * note of it and, if appropriate, use the
284 * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
289 /* If we are self signed, we break */
290 if (cert_self_signed(x))
293 * If asked see if we can find issuer in trusted store first
295 if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
296 ok = ctx->get_issuer(&xtmp, ctx, x);
298 ctx->error = X509_V_ERR_STORE_LOOKUP;
302 * If successful for now free up cert so it
303 * will be picked up again later.
310 /* If we were passed a cert chain, use it first */
311 if (ctx->untrusted != NULL) {
313 * If we do not find a non-expired untrusted cert, peek
314 * ahead and see if we can satisify this from the trusted
315 * store. If not, see if we have an expired untrusted cert.
317 xtmp = find_issuer(ctx, sktmp, x, 0);
319 !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)) {
320 ok = ctx->get_issuer(&xtmp, ctx, x);
322 ctx->error = X509_V_ERR_STORE_LOOKUP;
329 xtmp = find_issuer(ctx, sktmp, x, 1);
332 if (!sk_X509_push(ctx->chain, xtmp)) {
333 X509error(ERR_R_MALLOC_FAILURE);
334 ctx->error = X509_V_ERR_OUT_OF_MEM;
339 (void)sk_X509_delete_ptr(sktmp, xtmp);
340 ctx->last_untrusted++;
344 * reparse the full chain for the next one
351 /* Remember how many untrusted certs we have */
355 * At this point, chain should contain a list of untrusted
356 * certificates. We now need to add at least one trusted one,
357 * if possible, otherwise we complain.
362 * Examine last certificate in chain and see if it is
365 i = sk_X509_num(ctx->chain);
366 x = sk_X509_value(ctx->chain, i - 1);
367 if (cert_self_signed(x)) {
368 /* we have a self signed certificate */
371 * We have a single self signed
372 * certificate: see if we can find it
373 * in the store. We must have an exact
374 * match to avoid possible
377 ok = ctx->get_issuer(&xtmp, ctx, x);
378 if ((ok <= 0) || X509_cmp(x, xtmp)) {
379 ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
380 ctx->current_cert = x;
381 ctx->error_depth = i - 1;
390 * We have a match: replace
391 * certificate with store
392 * version so we get any trust
397 (void)sk_X509_set(ctx->chain, i - 1, x);
398 ctx->last_untrusted = 0;
402 * extract and save self signed
403 * certificate for later use
405 chain_ss = sk_X509_pop(ctx->chain);
406 ctx->last_untrusted--;
409 x = sk_X509_value(ctx->chain, num - 1);
412 /* We now lookup certs from the certificate store */
414 /* If we have enough, we break */
417 /* If we are self signed, we break */
418 if (cert_self_signed(x))
420 ok = ctx->get_issuer(&xtmp, ctx, x);
423 ctx->error = X509_V_ERR_STORE_LOOKUP;
429 if (!sk_X509_push(ctx->chain, x)) {
431 X509error(ERR_R_MALLOC_FAILURE);
432 ctx->error = X509_V_ERR_OUT_OF_MEM;
439 /* we now have our chain, lets check it... */
440 trust = check_trust(ctx);
442 /* If explicitly rejected error */
443 if (trust == X509_TRUST_REJECTED) {
448 * If it's not explicitly trusted then check if there
449 * is an alternative chain that could be used. We only
450 * do this if we haven't already checked via
451 * TRUSTED_FIRST and the user hasn't switched off
452 * alternate chain checking
455 if (trust != X509_TRUST_TRUSTED &&
456 !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) &&
457 !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
459 xtmp2 = sk_X509_value(ctx->chain, j - 1);
460 ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
463 /* Check if we found an alternate chain */
466 * Free up the found cert
467 * we'll add it again later
471 * Dump all the certs above
472 * this point - we've found an
476 xtmp = sk_X509_pop(ctx->chain);
480 ctx->last_untrusted = sk_X509_num(ctx->chain);
489 * If not explicitly trusted then indicate error unless it's a single
490 * self signed certificate in which case we've indicated an error already
491 * and set bad_chain == 1
493 if (trust != X509_TRUST_TRUSTED && !bad_chain) {
494 if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
495 if (ctx->last_untrusted >= num)
496 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
498 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
499 ctx->current_cert = x;
501 if (!sk_X509_push(ctx->chain, chain_ss)) {
502 X509error(ERR_R_MALLOC_FAILURE);
503 ctx->error = X509_V_ERR_OUT_OF_MEM;
508 ctx->last_untrusted = num;
509 ctx->current_cert = chain_ss;
510 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
514 ctx->error_depth = num - 1;
528 X509_verify_cert_legacy(X509_STORE_CTX *ctx)
530 int ok = 0, bad_chain;
532 ctx->error = X509_V_OK; /* Initialize to OK */
534 ok = X509_verify_cert_legacy_build_chain(ctx, &bad_chain);
538 /* We have the chain complete: now we need to check its purpose */
539 ok = check_chain_extensions(ctx);
543 /* Check name constraints */
544 ok = check_name_constraints(ctx);
553 * Check revocation status: we do this after copying parameters because
554 * they may be needed for CRL signature verification.
556 ok = ctx->check_revocation(ctx);
560 /* At this point, we have a chain and need to verify it */
561 if (ctx->verify != NULL)
562 ok = ctx->verify(ctx);
564 ok = internal_verify(ctx);
568 /* If we get this far evaluate policies */
569 if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
570 ok = ctx->check_policy(ctx);
573 /* Safety net, error returns must set ctx->error */
574 if (ok <= 0 && ctx->error == X509_V_OK)
575 ctx->error = X509_V_ERR_UNSPECIFIED;
581 X509_verify_cert(X509_STORE_CTX *ctx)
583 STACK_OF(X509) *roots = NULL;
584 struct x509_verify_ctx *vctx = NULL;
587 if (ctx->cert == NULL) {
588 X509error(X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
589 ctx->error = X509_V_ERR_INVALID_CALL;
592 if (ctx->chain != NULL) {
594 * This X509_STORE_CTX has already been used to verify
595 * a cert. We cannot do another one.
597 X509error(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
598 ctx->error = X509_V_ERR_INVALID_CALL;
601 if (ctx->param->id->poisoned) {
603 * This X509_STORE_CTX had failures setting
604 * up verify parameters. We can not use it.
606 X509error(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
607 ctx->error = X509_V_ERR_INVALID_CALL;
610 if (ctx->error != X509_V_ERR_INVALID_CALL) {
612 * This X509_STORE_CTX has not been properly initialized.
614 X509error(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
615 ctx->error = X509_V_ERR_INVALID_CALL;
620 * If flags request legacy, use the legacy verifier. If we
621 * requested "no alt chains" from the age of hammer pants, use
622 * the legacy verifier because the multi chain verifier really
623 * does find all the "alt chains".
625 * XXX deprecate the NO_ALT_CHAINS flag?
627 if ((ctx->param->flags & X509_V_FLAG_LEGACY_VERIFY) ||
628 (ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))
629 return X509_verify_cert_legacy(ctx);
631 /* Use the modern multi-chain verifier from x509_verify_cert */
633 /* Find our trusted roots */
634 ctx->error = X509_V_ERR_OUT_OF_MEM;
636 if (ctx->get_issuer == get_issuer_sk) {
638 * We are using the trusted stack method. so
639 * the roots are in the aptly named "ctx->other_ctx"
640 * pointer. (It could have been called "al")
642 if ((roots = X509_chain_up_ref(ctx->other_ctx)) == NULL)
646 * We have a X509_STORE and need to pull out the roots.
647 * Don't look Ethel...
649 STACK_OF(X509_OBJECT) *objs;
652 if ((roots = sk_X509_new_null()) == NULL)
655 CRYPTO_w_lock(CRYPTO_LOCK_X509_STORE);
656 if ((objs = X509_STORE_get0_objects(ctx->ctx)) == NULL)
658 for (i = 0; good && i < sk_X509_OBJECT_num(objs); i++) {
661 obj = sk_X509_OBJECT_value(objs, i);
662 if (obj->type != X509_LU_X509)
664 root = obj->data.x509;
665 if (X509_up_ref(root) == 0)
667 if (sk_X509_push(roots, root) == 0) {
672 CRYPTO_w_unlock(CRYPTO_LOCK_X509_STORE);
675 sk_X509_pop_free(roots, X509_free);
680 if ((vctx = x509_verify_ctx_new_from_xsc(ctx, roots)) != NULL) {
681 ctx->error = X509_V_OK; /* Initialize to OK */
682 chain_count = x509_verify(vctx, NULL, NULL);
685 sk_X509_pop_free(roots, X509_free);
686 x509_verify_ctx_free(vctx);
688 /* if we succeed we have a chain in ctx->chain */
689 return (chain_count > 0 && ctx->chain != NULL);
692 /* Given a STACK_OF(X509) find the issuer of cert (if any)
696 find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x,
700 X509 *issuer, *rv = NULL;
702 for (i = 0; i < sk_X509_num(sk); i++) {
703 issuer = sk_X509_value(sk, i);
704 if (ctx->check_issued(ctx, x, issuer)) {
705 if (x509_check_cert_time(ctx, issuer, -1))
714 /* Given a possible certificate and issuer check them */
717 check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
721 ret = X509_check_issued(issuer, x);
722 if (ret == X509_V_OK)
724 /* If we haven't asked for issuer errors don't set ctx */
725 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
729 ctx->current_cert = x;
730 ctx->current_issuer = issuer;
731 return ctx->verify_cb(0, ctx);
734 /* Alternative lookup method: look from a STACK stored in other_ctx */
737 get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
739 *issuer = find_issuer(ctx, ctx->other_ctx, x, 1);
741 CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
747 /* Check a certificate chains extensions for consistency
748 * with the supplied purpose
752 x509_vfy_check_chain_extensions(X509_STORE_CTX *ctx)
754 #ifdef OPENSSL_NO_CHAIN_VERIFY
757 int i, ok = 0, must_be_ca, plen = 0;
759 int (*cb)(int xok, X509_STORE_CTX *xctx);
760 int proxy_path_length = 0;
762 int allow_proxy_certs;
766 /* must_be_ca can have 1 of 3 values:
767 -1: we accept both CA and non-CA certificates, to allow direct
768 use of self-signed certificates (which are marked as CA).
769 0: we only accept non-CA certificates. This is currently not
770 used, but the possibility is present for future extensions.
771 1: we only accept CA certificates. This is currently used for
772 all certificates in the chain except the leaf certificate.
776 /* CRL path validation */
778 allow_proxy_certs = 0;
779 purpose = X509_PURPOSE_CRL_SIGN;
782 !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
783 purpose = ctx->param->purpose;
786 /* Check all untrusted certificates */
787 for (i = 0; i < ctx->last_untrusted; i++) {
789 x = sk_X509_value(ctx->chain, i);
790 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
791 (x->ex_flags & EXFLAG_CRITICAL)) {
792 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
793 ctx->error_depth = i;
794 ctx->current_cert = x;
799 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
800 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
801 ctx->error_depth = i;
802 ctx->current_cert = x;
807 ret = X509_check_ca(x);
808 switch (must_be_ca) {
810 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
811 (ret != 1) && (ret != 0)) {
813 ctx->error = X509_V_ERR_INVALID_CA;
820 ctx->error = X509_V_ERR_INVALID_NON_CA;
826 ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
829 ctx->error = X509_V_ERR_INVALID_CA;
835 ctx->error_depth = i;
836 ctx->current_cert = x;
841 if (ctx->param->purpose > 0) {
842 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
844 ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
846 ctx->error = X509_V_ERR_INVALID_PURPOSE;
847 ctx->error_depth = i;
848 ctx->current_cert = x;
854 /* Check pathlen if not self issued */
855 if ((i > 1) && !(x->ex_flags & EXFLAG_SI) &&
856 (x->ex_pathlen != -1) &&
857 (plen > (x->ex_pathlen + proxy_path_length + 1))) {
858 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
859 ctx->error_depth = i;
860 ctx->current_cert = x;
865 /* Increment path length if not self issued */
866 if (!(x->ex_flags & EXFLAG_SI))
868 /* If this certificate is a proxy certificate, the next
869 certificate must be another proxy certificate or a EE
870 certificate. If not, the next certificate must be a
872 if (x->ex_flags & EXFLAG_PROXY) {
873 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
875 X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
876 ctx->error_depth = i;
877 ctx->current_cert = x;
895 check_chain_extensions(X509_STORE_CTX *ctx) {
896 return x509_vfy_check_chain_extensions(ctx);
900 check_name_constraints(X509_STORE_CTX *ctx)
902 if (!x509_constraints_chain(ctx->chain, &ctx->error,
903 &ctx->error_depth)) {
904 ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
905 if (!ctx->verify_cb(0, ctx))
911 /* Given a certificate try and find an exact match in the store */
913 static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
915 STACK_OF(X509) *certs;
919 /* Lookup all certs with matching subject name */
920 certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
924 /* Look for exact match */
925 for (i = 0; i < sk_X509_num(certs); i++) {
926 xtmp = sk_X509_value(certs, i);
927 if (!X509_cmp(xtmp, x))
931 if (i < sk_X509_num(certs))
936 sk_X509_pop_free(certs, X509_free);
940 static int check_trust(X509_STORE_CTX *ctx)
945 int (*cb) (int xok, X509_STORE_CTX *xctx);
948 /* Check all trusted certificates in chain */
949 for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
950 x = sk_X509_value(ctx->chain, i);
951 ok = X509_check_trust(x, ctx->param->trust, 0);
953 /* If explicitly trusted return trusted */
954 if (ok == X509_TRUST_TRUSTED)
955 return X509_TRUST_TRUSTED;
957 * If explicitly rejected notify callback and reject if not
960 if (ok == X509_TRUST_REJECTED) {
961 ctx->error_depth = i;
962 ctx->current_cert = x;
963 ctx->error = X509_V_ERR_CERT_REJECTED;
966 return X509_TRUST_REJECTED;
970 * If we accept partial chains and have at least one trusted certificate
973 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
975 if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain))
976 return X509_TRUST_TRUSTED;
977 x = sk_X509_value(ctx->chain, 0);
978 mx = lookup_cert_match(ctx, x);
980 (void)sk_X509_set(ctx->chain, 0, mx);
982 ctx->last_untrusted = 0;
983 return X509_TRUST_TRUSTED;
988 * If no trusted certs in chain at all return untrusted and allow
989 * standard (no issuer cert) etc errors to be indicated.
991 return X509_TRUST_UNTRUSTED;
994 int x509_vfy_check_trust(X509_STORE_CTX *ctx)
996 return check_trust(ctx);
1000 check_revocation(X509_STORE_CTX *ctx)
1004 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
1006 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
1007 last = sk_X509_num(ctx->chain) - 1;
1009 /* If checking CRL paths this isn't the EE certificate */
1014 for (i = 0; i <= last; i++) {
1015 ok = check_cert(ctx, ctx->chain, i);
1023 x509_vfy_check_revocation(X509_STORE_CTX *ctx)
1025 return check_revocation(ctx);
1029 check_cert(X509_STORE_CTX *ctx, STACK_OF(X509) *chain, int depth)
1031 X509_CRL *crl = NULL, *dcrl = NULL;
1034 unsigned int last_reasons;
1036 cnum = ctx->error_depth = depth;
1037 x = sk_X509_value(chain, cnum);
1038 ctx->current_cert = x;
1039 ctx->current_issuer = NULL;
1040 ctx->current_crl_score = 0;
1041 ctx->current_reasons = 0;
1042 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
1043 last_reasons = ctx->current_reasons;
1044 /* Try to retrieve relevant CRL */
1046 ok = ctx->get_crl(ctx, &crl, x);
1048 ok = get_crl_delta(ctx, &crl, &dcrl, x);
1049 /* If error looking up CRL, nothing we can do except
1053 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
1054 ok = ctx->verify_cb(0, ctx);
1057 ctx->current_crl = crl;
1058 ok = ctx->check_crl(ctx, crl);
1063 ok = ctx->check_crl(ctx, dcrl);
1066 ok = ctx->cert_crl(ctx, dcrl, x);
1072 /* Don't look in full CRL if delta reason is removefromCRL */
1074 ok = ctx->cert_crl(ctx, crl, x);
1079 ctx->current_crl = NULL;
1081 X509_CRL_free(dcrl);
1084 /* If reasons not updated we wont get anywhere by
1085 * another iteration, so exit loop.
1087 if (last_reasons == ctx->current_reasons) {
1088 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
1089 ok = ctx->verify_cb(0, ctx);
1095 ctx->current_crl = NULL;
1097 X509_CRL_free(dcrl);
1101 /* Check CRL times against values in X509_STORE_CTX */
1104 check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
1106 time_t *ptime = NULL;
1109 if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
1112 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1113 ptime = &ctx->param->check_time;
1116 ctx->current_crl = crl;
1118 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
1122 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
1123 if (!ctx->verify_cb(0, ctx))
1130 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
1131 if (!ctx->verify_cb(0, ctx))
1135 if (X509_CRL_get_nextUpdate(crl)) {
1136 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
1141 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
1142 if (!ctx->verify_cb(0, ctx))
1145 /* Ignore expiry of base CRL is delta is valid */
1147 !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
1150 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
1151 if (!ctx->verify_cb(0, ctx))
1157 ctx->current_crl = NULL;
1163 get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
1164 X509 **pissuer, int *pscore, unsigned int *preasons,
1165 STACK_OF(X509_CRL) *crls)
1167 int i, crl_score, best_score = *pscore;
1168 unsigned int reasons, best_reasons = 0;
1169 X509 *x = ctx->current_cert;
1170 X509_CRL *crl, *best_crl = NULL;
1171 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
1173 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1174 crl = sk_X509_CRL_value(crls, i);
1175 reasons = *preasons;
1176 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
1178 if (crl_score > best_score) {
1180 best_crl_issuer = crl_issuer;
1181 best_score = crl_score;
1182 best_reasons = reasons;
1188 X509_CRL_free(*pcrl);
1190 *pissuer = best_crl_issuer;
1191 *pscore = best_score;
1192 *preasons = best_reasons;
1193 CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
1195 X509_CRL_free(*pdcrl);
1198 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
1201 if (best_score >= CRL_SCORE_VALID)
1207 /* Compare two CRL extensions for delta checking purposes. They should be
1208 * both present or both absent. If both present all fields must be identical.
1212 crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
1214 ASN1_OCTET_STRING *exta, *extb;
1217 i = X509_CRL_get_ext_by_NID(a, nid, -1);
1219 /* Can't have multiple occurrences */
1220 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
1222 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
1226 i = X509_CRL_get_ext_by_NID(b, nid, -1);
1229 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
1231 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
1241 if (ASN1_OCTET_STRING_cmp(exta, extb))
1247 /* See if a base and delta are compatible */
1250 check_delta_base(X509_CRL *delta, X509_CRL *base)
1252 /* Delta CRL must be a delta */
1253 if (!delta->base_crl_number)
1255 /* Base must have a CRL number */
1256 if (!base->crl_number)
1258 /* Issuer names must match */
1259 if (X509_NAME_cmp(X509_CRL_get_issuer(base),
1260 X509_CRL_get_issuer(delta)))
1262 /* AKID and IDP must match */
1263 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
1265 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
1267 /* Delta CRL base number must not exceed Full CRL number. */
1268 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
1270 /* Delta CRL number must exceed full CRL number */
1271 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
1276 /* For a given base CRL find a delta... maybe extend to delta scoring
1277 * or retrieve a chain of deltas...
1281 get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore, X509_CRL *base,
1282 STACK_OF(X509_CRL) *crls)
1287 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
1289 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
1291 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1292 delta = sk_X509_CRL_value(crls, i);
1293 if (check_delta_base(delta, base)) {
1294 if (check_crl_time(ctx, delta, 0))
1295 *pscore |= CRL_SCORE_TIME_DELTA;
1296 CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
1304 /* For a given CRL return how suitable it is for the supplied certificate 'x'.
1305 * The return value is a mask of several criteria.
1306 * If the issuer is not the certificate issuer this is returned in *pissuer.
1307 * The reasons mask is also used to determine if the CRL is suitable: if
1308 * no new reasons the CRL is rejected, otherwise reasons is updated.
1312 get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, unsigned int *preasons,
1313 X509_CRL *crl, X509 *x)
1316 unsigned int tmp_reasons = *preasons, crl_reasons;
1318 /* First see if we can reject CRL straight away */
1320 /* Invalid IDP cannot be processed */
1321 if (crl->idp_flags & IDP_INVALID)
1323 /* Reason codes or indirect CRLs need extended CRL support */
1324 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
1325 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
1327 } else if (crl->idp_flags & IDP_REASONS) {
1328 /* If no new reasons reject */
1329 if (!(crl->idp_reasons & ~tmp_reasons))
1332 /* Don't process deltas at this stage */
1333 else if (crl->base_crl_number)
1335 /* If issuer name doesn't match certificate need indirect CRL */
1336 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
1337 if (!(crl->idp_flags & IDP_INDIRECT))
1340 crl_score |= CRL_SCORE_ISSUER_NAME;
1342 if (!(crl->flags & EXFLAG_CRITICAL))
1343 crl_score |= CRL_SCORE_NOCRITICAL;
1346 if (check_crl_time(ctx, crl, 0))
1347 crl_score |= CRL_SCORE_TIME;
1349 /* Check authority key ID and locate certificate issuer */
1350 crl_akid_check(ctx, crl, pissuer, &crl_score);
1352 /* If we can't locate certificate issuer at this point forget it */
1354 if (!(crl_score & CRL_SCORE_AKID))
1357 /* Check cert for matching CRL distribution points */
1359 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1360 /* If no new reasons reject */
1361 if (!(crl_reasons & ~tmp_reasons))
1363 tmp_reasons |= crl_reasons;
1364 crl_score |= CRL_SCORE_SCOPE;
1367 *preasons = tmp_reasons;
1373 crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
1376 X509 *crl_issuer = NULL;
1377 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1378 int cidx = ctx->error_depth;
1381 if (cidx != sk_X509_num(ctx->chain) - 1)
1384 crl_issuer = sk_X509_value(ctx->chain, cidx);
1386 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1387 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1388 *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_ISSUER_CERT;
1389 *pissuer = crl_issuer;
1394 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1395 crl_issuer = sk_X509_value(ctx->chain, cidx);
1396 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1398 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1399 *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_SAME_PATH;
1400 *pissuer = crl_issuer;
1405 /* Anything else needs extended CRL support */
1407 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1410 /* Otherwise the CRL issuer is not on the path. Look for it in the
1411 * set of untrusted certificates.
1413 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1414 crl_issuer = sk_X509_value(ctx->untrusted, i);
1415 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1417 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1418 *pissuer = crl_issuer;
1419 *pcrl_score |= CRL_SCORE_AKID;
1425 /* Check the path of a CRL issuer certificate. This creates a new
1426 * X509_STORE_CTX and populates it with most of the parameters from the
1427 * parent. This could be optimised somewhat since a lot of path checking
1428 * will be duplicated by the parent, but this will rarely be used in
1433 check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1435 X509_STORE_CTX crl_ctx;
1438 /* Don't allow recursive CRL path validation */
1441 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted)) {
1446 crl_ctx.crls = ctx->crls;
1447 /* Copy verify params across */
1448 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1450 crl_ctx.parent = ctx;
1451 crl_ctx.verify_cb = ctx->verify_cb;
1453 /* Verify CRL issuer */
1454 ret = X509_verify_cert(&crl_ctx);
1459 /* Check chain is acceptable */
1460 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1463 X509_STORE_CTX_cleanup(&crl_ctx);
1467 /* RFC3280 says nothing about the relationship between CRL path
1468 * and certificate path, which could lead to situations where a
1469 * certificate could be revoked or validated by a CA not authorised
1470 * to do so. RFC5280 is more strict and states that the two paths must
1471 * end in the same trust anchor, though some discussions remain...
1472 * until this is resolved we use the RFC5280 version
1476 check_crl_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *cert_path,
1477 STACK_OF(X509) *crl_path)
1479 X509 *cert_ta, *crl_ta;
1481 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1482 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1483 if (!X509_cmp(cert_ta, crl_ta))
1488 /* Check for match between two dist point names: three separate cases.
1489 * 1. Both are relative names and compare X509_NAME types.
1490 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1491 * 3. Both are full names and compare two GENERAL_NAMES.
1492 * 4. One is NULL: automatic match.
1496 idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1498 X509_NAME *nm = NULL;
1499 GENERAL_NAMES *gens = NULL;
1500 GENERAL_NAME *gena, *genb;
1508 /* Case 1: two X509_NAME */
1512 if (!X509_NAME_cmp(a->dpname, b->dpname))
1517 /* Case 2: set name and GENERAL_NAMES appropriately */
1519 gens = b->name.fullname;
1520 } else if (b->type == 1) {
1523 /* Case 2: set name and GENERAL_NAMES appropriately */
1524 gens = a->name.fullname;
1528 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1530 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1531 gena = sk_GENERAL_NAME_value(gens, i);
1532 if (gena->type != GEN_DIRNAME)
1534 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1540 /* Else case 3: two GENERAL_NAMES */
1542 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1543 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1544 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1545 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1546 if (!GENERAL_NAME_cmp(gena, genb))
1555 crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1558 X509_NAME *nm = X509_CRL_get_issuer(crl);
1560 /* If no CRLissuer return is successful iff don't need a match */
1562 return !!(crl_score & CRL_SCORE_ISSUER_NAME);
1563 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1564 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1565 if (gen->type != GEN_DIRNAME)
1567 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1573 /* Check CRLDP and IDP */
1576 crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, unsigned int *preasons)
1580 if (crl->idp_flags & IDP_ONLYATTR)
1582 if (x->ex_flags & EXFLAG_CA) {
1583 if (crl->idp_flags & IDP_ONLYUSER)
1586 if (crl->idp_flags & IDP_ONLYCA)
1589 *preasons = crl->idp_reasons;
1590 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1591 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1592 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1594 idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1595 *preasons &= dp->dp_reasons;
1600 if ((!crl->idp || !crl->idp->distpoint) &&
1601 (crl_score & CRL_SCORE_ISSUER_NAME))
1606 /* Retrieve CRL corresponding to current certificate.
1607 * If deltas enabled try to find a delta CRL too
1611 get_crl_delta(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1614 X509 *issuer = NULL;
1616 unsigned int reasons;
1617 X509_CRL *crl = NULL, *dcrl = NULL;
1618 STACK_OF(X509_CRL) *skcrl;
1619 X509_NAME *nm = X509_get_issuer_name(x);
1621 reasons = ctx->current_reasons;
1622 ok = get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons,
1627 /* Lookup CRLs from store */
1628 skcrl = ctx->lookup_crls(ctx, nm);
1630 /* If no CRLs found and a near match from get_crl_sk use that */
1634 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1636 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1640 /* If we got any kind of CRL use it and return success */
1642 ctx->current_issuer = issuer;
1643 ctx->current_crl_score = crl_score;
1644 ctx->current_reasons = reasons;
1653 /* Check CRL validity */
1655 check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1657 X509 *issuer = NULL;
1658 EVP_PKEY *ikey = NULL;
1659 int ok = 0, chnum, cnum;
1661 cnum = ctx->error_depth;
1662 chnum = sk_X509_num(ctx->chain) - 1;
1663 /* if we have an alternative CRL issuer cert use that */
1664 if (ctx->current_issuer) {
1665 issuer = ctx->current_issuer;
1666 } else if (cnum < chnum) {
1668 * Else find CRL issuer: if not last certificate then issuer
1669 * is next certificate in chain.
1671 issuer = sk_X509_value(ctx->chain, cnum + 1);
1673 issuer = sk_X509_value(ctx->chain, chnum);
1674 /* If not self signed, can't check signature */
1675 if (!ctx->check_issued(ctx, issuer, issuer)) {
1676 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1677 ok = ctx->verify_cb(0, ctx);
1684 /* Skip most tests for deltas because they have already
1687 if (!crl->base_crl_number) {
1688 /* Check for cRLSign bit if keyUsage present */
1689 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1690 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1691 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1692 ok = ctx->verify_cb(0, ctx);
1697 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1698 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1699 ok = ctx->verify_cb(0, ctx);
1704 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1705 if (check_crl_path(ctx,
1706 ctx->current_issuer) <= 0) {
1707 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1708 ok = ctx->verify_cb(0, ctx);
1714 if (crl->idp_flags & IDP_INVALID) {
1715 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1716 ok = ctx->verify_cb(0, ctx);
1724 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1725 ok = check_crl_time(ctx, crl, 1);
1730 /* Attempt to get issuer certificate public key */
1731 ikey = X509_get_pubkey(issuer);
1734 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1735 ok = ctx->verify_cb(0, ctx);
1739 /* Verify CRL signature */
1740 if (X509_CRL_verify(crl, ikey) <= 0) {
1741 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1742 ok = ctx->verify_cb(0, ctx);
1752 EVP_PKEY_free(ikey);
1756 /* Check certificate against CRL */
1758 cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1763 /* The rules changed for this... previously if a CRL contained
1764 * unhandled critical extensions it could still be used to indicate
1765 * a certificate was revoked. This has since been changed since
1766 * critical extension can change the meaning of CRL entries.
1768 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
1769 (crl->flags & EXFLAG_CRITICAL)) {
1770 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1771 ok = ctx->verify_cb(0, ctx);
1775 /* Look for serial number of certificate in CRL
1776 * If found make sure reason is not removeFromCRL.
1778 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1779 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1781 ctx->error = X509_V_ERR_CERT_REVOKED;
1782 ok = ctx->verify_cb(0, ctx);
1791 x509_vfy_check_policy(X509_STORE_CTX *ctx)
1798 /* X509_policy_check always allocates a new tree. */
1799 X509_policy_tree_free(ctx->tree);
1802 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1803 ctx->param->policies, ctx->param->flags);
1805 X509error(ERR_R_MALLOC_FAILURE);
1808 /* Invalid or inconsistent extensions */
1810 /* Locate certificates with bad extensions and notify
1815 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1816 x = sk_X509_value(ctx->chain, i);
1817 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1819 ctx->current_cert = x;
1820 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1821 if (!ctx->verify_cb(0, ctx))
1827 ctx->current_cert = NULL;
1828 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1829 return ctx->verify_cb(0, ctx);
1832 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1833 ctx->current_cert = NULL;
1834 ctx->error = X509_V_OK;
1835 if (!ctx->verify_cb(2, ctx))
1843 check_policy(X509_STORE_CTX *ctx)
1845 return x509_vfy_check_policy(ctx);
1849 * Inform the verify callback of an error.
1851 * If x is not NULL it is the error cert, otherwise use the chain cert
1854 * If err is not X509_V_OK, that's the error value, otherwise leave
1855 * unchanged (presumably set by the caller).
1857 * Returns 0 to abort verification with an error, non-zero to continue.
1860 verify_cb_cert(X509_STORE_CTX *ctx, X509 *x, int depth, int err)
1862 ctx->error_depth = depth;
1863 ctx->current_cert = (x != NULL) ? x : sk_X509_value(ctx->chain, depth);
1864 if (err != X509_V_OK)
1866 return ctx->verify_cb(0, ctx);
1870 * Check certificate validity times.
1872 * If depth >= 0, invoke verification callbacks on error, otherwise just return
1873 * the validation status.
1875 * Return 1 on success, 0 otherwise.
1878 x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth)
1883 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1884 ptime = &ctx->param->check_time;
1885 else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
1890 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1891 if (i >= 0 && depth < 0)
1893 if (i == 0 && !verify_cb_cert(ctx, x, depth,
1894 X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD))
1896 if (i > 0 && !verify_cb_cert(ctx, x, depth,
1897 X509_V_ERR_CERT_NOT_YET_VALID))
1900 i = X509_cmp_time_internal(X509_get_notAfter(x), ptime, 1);
1901 if (i <= 0 && depth < 0)
1903 if (i == 0 && !verify_cb_cert(ctx, x, depth,
1904 X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD))
1906 if (i < 0 && !verify_cb_cert(ctx, x, depth,
1907 X509_V_ERR_CERT_HAS_EXPIRED))
1913 internal_verify(X509_STORE_CTX *ctx)
1915 int n = sk_X509_num(ctx->chain) - 1;
1916 X509 *xi = sk_X509_value(ctx->chain, n);
1919 if (ctx->check_issued(ctx, xi, xi))
1922 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1927 return verify_cb_cert(ctx, xi, 0,
1928 X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);
1930 ctx->error_depth = n;
1931 xs = sk_X509_value(ctx->chain, n);
1935 * Do not clear ctx->error=0, it must be "sticky", only the
1936 * user's callback is allowed to reset errors (at its own
1942 * Skip signature check for self signed certificates
1943 * unless explicitly asked for. It doesn't add any
1944 * security and just wastes time. If the issuer's
1945 * public key is unusable, report the issuer
1946 * certificate and its depth (rather than the depth of
1950 (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
1952 if ((pkey = X509_get_pubkey(xi)) == NULL) {
1953 if (!verify_cb_cert(ctx, xi, xi != xs ? n+1 : n,
1954 X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY))
1956 } else if (X509_verify(xs, pkey) <= 0) {
1957 if (!verify_cb_cert(ctx, xs, n,
1958 X509_V_ERR_CERT_SIGNATURE_FAILURE)) {
1959 EVP_PKEY_free(pkey);
1963 EVP_PKEY_free(pkey);
1966 /* Calls verify callback as needed */
1967 if (!x509_check_cert_time(ctx, xs, n))
1971 * Signal success at this depth. However, the
1972 * previous error (if any) is retained.
1974 ctx->current_issuer = xi;
1975 ctx->current_cert = xs;
1976 ctx->error_depth = n;
1977 if (!ctx->verify_cb(1, ctx))
1982 xs = sk_X509_value(ctx->chain, n);
1989 X509_cmp_current_time(const ASN1_TIME *ctm)
1991 return X509_cmp_time(ctm, NULL);
1995 * Compare a possibly unvalidated ASN1_TIME string against a time_t
1996 * using RFC 5280 rules for the time string. If *cmp_time is NULL
1997 * the current system time is used.
1999 * XXX NOTE that unlike what you expect a "cmp" function to do in C,
2000 * XXX this one is "special", and returns 0 for error.
2003 * -1 if the ASN1_time is earlier than OR the same as *cmp_time.
2004 * 1 if the ASN1_time is later than *cmp_time.
2008 X509_cmp_time_internal(const ASN1_TIME *ctm, time_t *cmp_time, int clamp_notafter)
2014 if (cmp_time == NULL)
2015 compare = time(NULL);
2017 compare = *cmp_time;
2019 memset(&tm1, 0, sizeof(tm1));
2021 if (!x509_verify_asn1_time_to_tm(ctm, &tm1, clamp_notafter))
2022 goto out; /* invalid time */
2024 if (gmtime_r(&compare, &tm2) == NULL)
2027 ret = ASN1_time_tm_cmp(&tm1, &tm2);
2029 ret = -1; /* 0 is used for error, so map same to less than */
2035 X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
2037 return X509_cmp_time_internal(ctm, cmp_time, 0);
2042 X509_gmtime_adj(ASN1_TIME *s, long adj)
2044 return X509_time_adj(s, adj, NULL);
2048 X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_time)
2050 return X509_time_adj_ex(s, 0, offset_sec, in_time);
2054 X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec, time_t *in_time)
2057 if (in_time == NULL)
2062 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
2066 X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
2068 EVP_PKEY *ktmp = NULL, *ktmp2;
2071 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
2074 for (i = 0; i < sk_X509_num(chain); i++) {
2075 ktmp = X509_get_pubkey(sk_X509_value(chain, i));
2077 X509error(X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
2080 if (!EVP_PKEY_missing_parameters(ktmp))
2083 EVP_PKEY_free(ktmp);
2088 X509error(X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
2092 /* first, populate the other certs */
2093 for (j = i - 1; j >= 0; j--) {
2094 ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
2095 EVP_PKEY_copy_parameters(ktmp2, ktmp);
2096 EVP_PKEY_free(ktmp2);
2100 EVP_PKEY_copy_parameters(pkey, ktmp);
2101 EVP_PKEY_free(ktmp);
2106 X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
2107 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
2109 /* This function is (usually) called only once, by
2110 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
2111 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX,
2112 argl, argp, new_func, dup_func, free_func);
2116 X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
2118 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
2122 X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
2124 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
2128 X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
2134 X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
2140 X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
2142 return ctx->error_depth;
2146 X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
2148 return ctx->current_cert;
2152 X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
2158 X509_STORE_CTX_get0_chain(X509_STORE_CTX *xs)
2164 X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
2168 STACK_OF(X509) *chain;
2170 if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain)))
2172 for (i = 0; i < sk_X509_num(chain); i++) {
2173 x = sk_X509_value(chain, i);
2174 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
2180 X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
2182 return ctx->current_issuer;
2186 X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
2188 return ctx->current_crl;
2192 X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
2198 X509_STORE_CTX_get0_store(X509_STORE_CTX *xs)
2204 X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
2210 X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2212 ctx->untrusted = sk;
2216 X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
2222 X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
2224 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
2228 X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
2230 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
2233 /* This function is used to set the X509_STORE_CTX purpose and trust
2234 * values. This is intended to be used when another structure has its
2235 * own trust and purpose values which (if set) will be inherited by
2236 * the ctx. If they aren't set then we will usually have a default
2237 * purpose in mind which should then be used to set the trust value.
2238 * An example of this is SSL use: an SSL structure will have its own
2239 * purpose and trust settings which the application can set: if they
2240 * aren't set then we use the default of SSL client/server.
2244 X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
2245 int purpose, int trust)
2249 /* If purpose not set use default */
2251 purpose = def_purpose;
2252 /* If we have a purpose then check it is valid */
2255 idx = X509_PURPOSE_get_by_id(purpose);
2257 X509error(X509_R_UNKNOWN_PURPOSE_ID);
2260 ptmp = X509_PURPOSE_get0(idx);
2261 if (ptmp->trust == X509_TRUST_DEFAULT) {
2262 idx = X509_PURPOSE_get_by_id(def_purpose);
2264 X509error(X509_R_UNKNOWN_PURPOSE_ID);
2267 ptmp = X509_PURPOSE_get0(idx);
2269 /* If trust not set then get from purpose default */
2271 trust = ptmp->trust;
2274 idx = X509_TRUST_get_by_id(trust);
2276 X509error(X509_R_UNKNOWN_TRUST_ID);
2281 if (purpose && !ctx->param->purpose)
2282 ctx->param->purpose = purpose;
2283 if (trust && !ctx->param->trust)
2284 ctx->param->trust = trust;
2289 X509_STORE_CTX_new(void)
2291 X509_STORE_CTX *ctx;
2293 ctx = calloc(1, sizeof(X509_STORE_CTX));
2295 X509error(ERR_R_MALLOC_FAILURE);
2302 X509_STORE_CTX_free(X509_STORE_CTX *ctx)
2307 X509_STORE_CTX_cleanup(ctx);
2312 X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
2313 STACK_OF(X509) *chain)
2318 * Make sure everything is initialized properly even in case of an
2319 * early return due to an error.
2321 * While this 'ctx' can be reused, X509_STORE_CTX_cleanup() will have
2322 * freed everything and memset ex_data anyway. This also allows us
2323 * to safely use X509_STORE_CTX variables from the stack which will
2324 * have uninitialized data.
2326 memset(ctx, 0, sizeof(*ctx));
2329 * Start with this set to not valid - it will be set to valid
2330 * in X509_verify_cert.
2332 ctx->error = X509_V_ERR_INVALID_CALL;
2335 * Set values other than 0. Keep this in the same order as
2336 * X509_STORE_CTX except for values that may fail. All fields that
2337 * may fail should go last to make sure 'ctx' is as consistent as
2338 * possible even on early exits.
2342 ctx->untrusted = chain;
2344 if (store && store->verify)
2345 ctx->verify = store->verify;
2347 ctx->verify = internal_verify;
2349 if (store && store->verify_cb)
2350 ctx->verify_cb = store->verify_cb;
2352 ctx->verify_cb = null_callback;
2354 if (store && store->get_issuer)
2355 ctx->get_issuer = store->get_issuer;
2357 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2359 if (store && store->check_issued)
2360 ctx->check_issued = store->check_issued;
2362 ctx->check_issued = check_issued;
2364 if (store && store->check_revocation)
2365 ctx->check_revocation = store->check_revocation;
2367 ctx->check_revocation = check_revocation;
2369 if (store && store->get_crl)
2370 ctx->get_crl = store->get_crl;
2372 ctx->get_crl = NULL;
2374 if (store && store->check_crl)
2375 ctx->check_crl = store->check_crl;
2377 ctx->check_crl = check_crl;
2379 if (store && store->cert_crl)
2380 ctx->cert_crl = store->cert_crl;
2382 ctx->cert_crl = cert_crl;
2384 ctx->check_policy = check_policy;
2386 if (store && store->lookup_certs)
2387 ctx->lookup_certs = store->lookup_certs;
2389 ctx->lookup_certs = X509_STORE_get1_certs;
2391 if (store && store->lookup_crls)
2392 ctx->lookup_crls = store->lookup_crls;
2394 ctx->lookup_crls = X509_STORE_get1_crls;
2396 if (store && store->cleanup)
2397 ctx->cleanup = store->cleanup;
2399 ctx->cleanup = NULL;
2401 ctx->param = X509_VERIFY_PARAM_new();
2403 X509error(ERR_R_MALLOC_FAILURE);
2407 /* Inherit callbacks and flags from X509_STORE if not set
2411 param_ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2413 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE;
2416 param_ret = X509_VERIFY_PARAM_inherit(ctx->param,
2417 X509_VERIFY_PARAM_lookup("default"));
2419 if (param_ret == 0) {
2420 X509error(ERR_R_MALLOC_FAILURE);
2424 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2425 &(ctx->ex_data)) == 0) {
2426 X509error(ERR_R_MALLOC_FAILURE);
2432 /* Set alternative lookup method: just a STACK of trusted certificates.
2433 * This avoids X509_STORE nastiness where it isn't needed.
2437 X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2439 ctx->other_ctx = sk;
2440 ctx->get_issuer = get_issuer_sk;
2444 X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2446 X509_STORE_CTX_trusted_stack(ctx, sk);
2450 X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2454 if (ctx->param != NULL) {
2455 if (ctx->parent == NULL)
2456 X509_VERIFY_PARAM_free(ctx->param);
2459 if (ctx->tree != NULL) {
2460 X509_policy_tree_free(ctx->tree);
2463 if (ctx->chain != NULL) {
2464 sk_X509_pop_free(ctx->chain, X509_free);
2467 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX,
2468 ctx, &(ctx->ex_data));
2469 memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
2473 X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2475 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2479 X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2481 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2485 X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t)
2487 X509_VERIFY_PARAM_set_time(ctx->param, t);
2491 X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2492 int (*verify_cb)(int, X509_STORE_CTX *))
2494 ctx->verify_cb = verify_cb;
2498 X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx)
2504 X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx)
2506 return ctx->untrusted;
2510 X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2512 ctx->untrusted = sk;
2516 X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2522 X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2524 return ctx->explicit_policy;
2528 X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2530 const X509_VERIFY_PARAM *param;
2531 param = X509_VERIFY_PARAM_lookup(name);
2534 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2538 X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2544 X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2547 X509_VERIFY_PARAM_free(ctx->param);