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.]
63 #include "internal/cryptlib.h"
64 #include <openssl/crypto.h>
65 #include <openssl/lhash.h>
66 #include <openssl/buffer.h>
67 #include <openssl/evp.h>
68 #include <openssl/asn1.h>
69 #include <openssl/x509.h>
70 #include <openssl/x509v3.h>
71 #include <openssl/objects.h>
74 /* CRL score values */
76 /* No unhandled critical extensions */
78 #define CRL_SCORE_NOCRITICAL 0x100
80 /* certificate is within CRL scope */
82 #define CRL_SCORE_SCOPE 0x080
86 #define CRL_SCORE_TIME 0x040
88 /* Issuer name matches certificate */
90 #define CRL_SCORE_ISSUER_NAME 0x020
92 /* If this score or above CRL is probably valid */
94 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
96 /* CRL issuer is certificate issuer */
98 #define CRL_SCORE_ISSUER_CERT 0x018
100 /* CRL issuer is on certificate path */
102 #define CRL_SCORE_SAME_PATH 0x008
104 /* CRL issuer matches CRL AKID */
106 #define CRL_SCORE_AKID 0x004
108 /* Have a delta CRL with valid times */
110 #define CRL_SCORE_TIME_DELTA 0x002
112 static int null_callback(int ok, X509_STORE_CTX *e);
113 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
114 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
115 static int check_chain_extensions(X509_STORE_CTX *ctx);
116 static int check_name_constraints(X509_STORE_CTX *ctx);
117 static int check_id(X509_STORE_CTX *ctx);
118 static int check_trust(X509_STORE_CTX *ctx);
119 static int check_revocation(X509_STORE_CTX *ctx);
120 static int check_cert(X509_STORE_CTX *ctx);
121 static int check_policy(X509_STORE_CTX *ctx);
122 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
124 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
125 unsigned int *preasons, X509_CRL *crl, X509 *x);
126 static int get_crl_delta(X509_STORE_CTX *ctx,
127 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
128 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
129 int *pcrl_score, X509_CRL *base,
130 STACK_OF(X509_CRL) *crls);
131 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
133 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
134 unsigned int *preasons);
135 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
136 static int check_crl_chain(X509_STORE_CTX *ctx,
137 STACK_OF(X509) *cert_path,
138 STACK_OF(X509) *crl_path);
140 static int internal_verify(X509_STORE_CTX *ctx);
142 static int null_callback(int ok, X509_STORE_CTX *e)
147 /* Return 1 is a certificate is self signed */
148 static int cert_self_signed(X509 *x)
150 X509_check_purpose(x, -1, 0);
151 if (x->ex_flags & EXFLAG_SS)
157 /* Given a certificate try and find an exact match in the store */
159 static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
161 STACK_OF(X509) *certs;
164 /* Lookup all certs with matching subject name */
165 certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
168 /* Look for exact match */
169 for (i = 0; i < sk_X509_num(certs); i++) {
170 xtmp = sk_X509_value(certs, i);
171 if (!X509_cmp(xtmp, x))
174 if (i < sk_X509_num(certs))
178 sk_X509_pop_free(certs, X509_free);
182 int X509_verify_cert(X509_STORE_CTX *ctx)
184 X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
186 X509_VERIFY_PARAM *param = ctx->param;
187 int depth, i, ok = 0;
189 int (*cb) (int xok, X509_STORE_CTX *xctx);
190 STACK_OF(X509) *sktmp = NULL;
191 if (ctx->cert == NULL) {
192 X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
195 if (ctx->chain != NULL) {
197 * This X509_STORE_CTX has already been used to verify a cert. We
198 * cannot do another one.
200 X509err(X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
207 * first we make sure the chain we are going to build is present and that
208 * the first entry is in place
210 if (((ctx->chain = sk_X509_new_null()) == NULL) ||
211 (!sk_X509_push(ctx->chain, ctx->cert))) {
212 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
215 X509_up_ref(ctx->cert);
216 ctx->last_untrusted = 1;
218 /* We use a temporary STACK so we can chop and hack at it */
219 if (ctx->untrusted != NULL
220 && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
221 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
225 num = sk_X509_num(ctx->chain);
226 x = sk_X509_value(ctx->chain, num - 1);
227 depth = param->depth;
230 /* If we have enough, we break */
232 break; /* FIXME: If this happens, we should take
233 * note of it and, if appropriate, use the
234 * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
237 /* If we are self signed, we break */
238 if (cert_self_signed(x))
241 * If asked see if we can find issuer in trusted store first
243 if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
244 ok = ctx->get_issuer(&xtmp, ctx, x);
248 * If successful for now free up cert so it will be picked up
257 /* If we were passed a cert chain, use it first */
258 if (ctx->untrusted != NULL) {
259 xtmp = find_issuer(ctx, sktmp, x);
261 if (!sk_X509_push(ctx->chain, xtmp)) {
262 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
266 (void)sk_X509_delete_ptr(sktmp, xtmp);
267 ctx->last_untrusted++;
271 * reparse the full chain for the next one
279 /* Remember how many untrusted certs we have */
282 * at this point, chain should contain a list of untrusted certificates.
283 * We now need to add at least one trusted one, if possible, otherwise we
289 * Examine last certificate in chain and see if it is self signed.
291 i = sk_X509_num(ctx->chain);
292 x = sk_X509_value(ctx->chain, i - 1);
293 if (cert_self_signed(x)) {
294 /* we have a self signed certificate */
295 if (sk_X509_num(ctx->chain) == 1) {
297 * We have a single self signed certificate: see if we can
298 * find it in the store. We must have an exact match to avoid
299 * possible impersonation.
301 ok = ctx->get_issuer(&xtmp, ctx, x);
302 if ((ok <= 0) || X509_cmp(x, xtmp)) {
303 ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
304 ctx->current_cert = x;
305 ctx->error_depth = i - 1;
314 * We have a match: replace certificate with store
315 * version so we get any trust settings.
319 (void)sk_X509_set(ctx->chain, i - 1, x);
320 ctx->last_untrusted = 0;
324 * extract and save self signed certificate for later use
326 chain_ss = sk_X509_pop(ctx->chain);
327 ctx->last_untrusted--;
330 x = sk_X509_value(ctx->chain, num - 1);
333 /* We now lookup certs from the certificate store */
335 /* If we have enough, we break */
338 /* If we are self signed, we break */
339 if (cert_self_signed(x))
341 ok = ctx->get_issuer(&xtmp, ctx, x);
348 if (!sk_X509_push(ctx->chain, x)) {
350 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
357 /* we now have our chain, lets check it... */
358 i = check_trust(ctx);
360 /* If explicitly rejected error */
361 if (i == X509_TRUST_REJECTED)
364 * If it's not explicitly trusted then check if there is an alternative
365 * chain that could be used. We only do this if we haven't already
366 * checked via TRUSTED_FIRST and the user hasn't switched off alternate
370 if (i != X509_TRUST_TRUSTED
371 && !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
372 && !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
374 STACK_OF(X509) *chtmp = ctx->chain;
375 xtmp2 = sk_X509_value(ctx->chain, j - 1);
377 * Temporarily set chain to NULL so we don't discount
378 * duplicates: the same certificate could be an untrusted
379 * CA found in the trusted store.
382 ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
386 /* Check if we found an alternate chain */
389 * Free up the found cert we'll add it again later
394 * Dump all the certs above this point - we've found an
398 xtmp = sk_X509_pop(ctx->chain);
402 ctx->last_untrusted = sk_X509_num(ctx->chain);
411 * If not explicitly trusted then indicate error unless it's a single
412 * self signed certificate in which case we've indicated an error already
413 * and set bad_chain == 1
415 if (i != X509_TRUST_TRUSTED && !bad_chain) {
416 if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
417 if (ctx->last_untrusted >= num)
418 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
420 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
421 ctx->current_cert = x;
424 sk_X509_push(ctx->chain, chain_ss);
426 ctx->last_untrusted = num;
427 ctx->current_cert = chain_ss;
428 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
432 ctx->error_depth = num - 1;
439 /* We have the chain complete: now we need to check its purpose */
440 ok = check_chain_extensions(ctx);
445 /* Check name constraints */
447 ok = check_name_constraints(ctx);
457 /* We may as well copy down any DSA parameters that are required */
458 X509_get_pubkey_parameters(NULL, ctx->chain);
461 * Check revocation status: we do this after copying parameters because
462 * they may be needed for CRL signature verification.
465 ok = ctx->check_revocation(ctx);
469 i = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
471 if (i != X509_V_OK) {
473 ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
479 /* At this point, we have a chain and need to verify it */
480 if (ctx->verify != NULL)
481 ok = ctx->verify(ctx);
483 ok = internal_verify(ctx);
487 /* RFC 3779 path validation, now that CRL check has been done */
488 ok = v3_asid_validate_path(ctx);
491 ok = v3_addr_validate_path(ctx);
495 /* If we get this far evaluate policies */
496 if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
497 ok = ctx->check_policy(ctx);
502 X509_get_pubkey_parameters(NULL, ctx->chain);
510 * Given a STACK_OF(X509) find the issuer of cert (if any)
513 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
516 X509 *issuer, *rv = NULL;;
517 for (i = 0; i < sk_X509_num(sk); i++) {
518 issuer = sk_X509_value(sk, i);
519 if (ctx->check_issued(ctx, x, issuer)) {
521 if (x509_check_cert_time(ctx, rv, 1))
528 /* Given a possible certificate and issuer check them */
530 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
534 return cert_self_signed(x);
535 ret = X509_check_issued(issuer, x);
536 if (ret == X509_V_OK) {
539 /* Special case: single self signed certificate */
540 if (cert_self_signed(x) && sk_X509_num(ctx->chain) == 1)
542 for (i = 0; i < sk_X509_num(ctx->chain); i++) {
543 ch = sk_X509_value(ctx->chain, i);
544 if (ch == issuer || !X509_cmp(ch, issuer)) {
545 ret = X509_V_ERR_PATH_LOOP;
551 if (ret == X509_V_OK)
553 /* If we haven't asked for issuer errors don't set ctx */
554 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
558 ctx->current_cert = x;
559 ctx->current_issuer = issuer;
560 return ctx->verify_cb(0, ctx);
563 /* Alternative lookup method: look from a STACK stored in other_ctx */
565 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
567 *issuer = find_issuer(ctx, ctx->other_ctx, x);
569 X509_up_ref(*issuer);
576 * Check a certificate chains extensions for consistency with the supplied
580 static int check_chain_extensions(X509_STORE_CTX *ctx)
582 int i, ok = 0, must_be_ca, plen = 0;
584 int (*cb) (int xok, X509_STORE_CTX *xctx);
585 int proxy_path_length = 0;
587 int allow_proxy_certs;
591 * must_be_ca can have 1 of 3 values:
592 * -1: we accept both CA and non-CA certificates, to allow direct
593 * use of self-signed certificates (which are marked as CA).
594 * 0: we only accept non-CA certificates. This is currently not
595 * used, but the possibility is present for future extensions.
596 * 1: we only accept CA certificates. This is currently used for
597 * all certificates in the chain except the leaf certificate.
601 /* CRL path validation */
603 allow_proxy_certs = 0;
604 purpose = X509_PURPOSE_CRL_SIGN;
607 ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
609 * A hack to keep people who don't want to modify their software
612 if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
613 allow_proxy_certs = 1;
614 purpose = ctx->param->purpose;
617 /* Check all untrusted certificates */
618 for (i = 0; i < ctx->last_untrusted; i++) {
620 x = sk_X509_value(ctx->chain, i);
621 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
622 && (x->ex_flags & EXFLAG_CRITICAL)) {
623 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
624 ctx->error_depth = i;
625 ctx->current_cert = x;
630 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
631 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
632 ctx->error_depth = i;
633 ctx->current_cert = x;
638 ret = X509_check_ca(x);
639 switch (must_be_ca) {
641 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
642 && (ret != 1) && (ret != 0)) {
644 ctx->error = X509_V_ERR_INVALID_CA;
651 ctx->error = X509_V_ERR_INVALID_NON_CA;
657 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
660 ctx->error = X509_V_ERR_INVALID_CA;
666 ctx->error_depth = i;
667 ctx->current_cert = x;
672 if (ctx->param->purpose > 0) {
673 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
675 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
677 ctx->error = X509_V_ERR_INVALID_PURPOSE;
678 ctx->error_depth = i;
679 ctx->current_cert = x;
685 /* Check pathlen if not self issued */
686 if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
687 && (x->ex_pathlen != -1)
688 && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
689 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
690 ctx->error_depth = i;
691 ctx->current_cert = x;
696 /* Increment path length if not self issued */
697 if (!(x->ex_flags & EXFLAG_SI))
700 * If this certificate is a proxy certificate, the next certificate
701 * must be another proxy certificate or a EE certificate. If not,
702 * the next certificate must be a CA certificate.
704 if (x->ex_flags & EXFLAG_PROXY) {
705 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
706 ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
707 ctx->error_depth = i;
708 ctx->current_cert = x;
723 static int check_name_constraints(X509_STORE_CTX *ctx)
727 /* Check name constraints for all certificates */
728 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
729 x = sk_X509_value(ctx->chain, i);
730 /* Ignore self issued certs unless last in chain */
731 if (i && (x->ex_flags & EXFLAG_SI))
734 * Check against constraints for all certificates higher in chain
735 * including trust anchor. Trust anchor not strictly speaking needed
736 * but if it includes constraints it is to be assumed it expects them
739 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
740 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
742 rv = NAME_CONSTRAINTS_check(x, nc);
743 if (rv != X509_V_OK) {
745 ctx->error_depth = i;
746 ctx->current_cert = x;
747 if (!ctx->verify_cb(0, ctx))
756 static int check_id_error(X509_STORE_CTX *ctx, int errcode)
758 ctx->error = errcode;
759 ctx->current_cert = ctx->cert;
760 ctx->error_depth = 0;
761 return ctx->verify_cb(0, ctx);
764 static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
767 int n = sk_OPENSSL_STRING_num(id->hosts);
770 if (id->peername != NULL) {
771 OPENSSL_free(id->peername);
774 for (i = 0; i < n; ++i) {
775 name = sk_OPENSSL_STRING_value(id->hosts, i);
776 if (X509_check_host(x, name, 0, id->hostflags, &id->peername) > 0)
782 static int check_id(X509_STORE_CTX *ctx)
784 X509_VERIFY_PARAM *vpm = ctx->param;
785 X509_VERIFY_PARAM_ID *id = vpm->id;
787 if (id->hosts && check_hosts(x, id) <= 0) {
788 if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
791 if (id->email && X509_check_email(x, id->email, id->emaillen, 0) <= 0) {
792 if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
795 if (id->ip && X509_check_ip(x, id->ip, id->iplen, 0) <= 0) {
796 if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
802 static int check_trust(X509_STORE_CTX *ctx)
806 int (*cb) (int xok, X509_STORE_CTX *xctx);
808 /* Check all trusted certificates in chain */
809 for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
810 x = sk_X509_value(ctx->chain, i);
811 ok = X509_check_trust(x, ctx->param->trust, 0);
812 /* If explicitly trusted return trusted */
813 if (ok == X509_TRUST_TRUSTED)
814 return X509_TRUST_TRUSTED;
816 * If explicitly rejected notify callback and reject if not
819 if (ok == X509_TRUST_REJECTED) {
820 ctx->error_depth = i;
821 ctx->current_cert = x;
822 ctx->error = X509_V_ERR_CERT_REJECTED;
825 return X509_TRUST_REJECTED;
829 * If we accept partial chains and have at least one trusted certificate
832 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
834 if (ctx->last_untrusted < sk_X509_num(ctx->chain))
835 return X509_TRUST_TRUSTED;
836 x = sk_X509_value(ctx->chain, 0);
837 mx = lookup_cert_match(ctx, x);
839 (void)sk_X509_set(ctx->chain, 0, mx);
841 ctx->last_untrusted = 0;
842 return X509_TRUST_TRUSTED;
847 * If no trusted certs in chain at all return untrusted and allow
848 * standard (no issuer cert) etc errors to be indicated.
850 return X509_TRUST_UNTRUSTED;
853 static int check_revocation(X509_STORE_CTX *ctx)
855 int i = 0, last = 0, ok = 0;
856 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
858 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
859 last = sk_X509_num(ctx->chain) - 1;
861 /* If checking CRL paths this isn't the EE certificate */
866 for (i = 0; i <= last; i++) {
867 ctx->error_depth = i;
868 ok = check_cert(ctx);
875 static int check_cert(X509_STORE_CTX *ctx)
877 X509_CRL *crl = NULL, *dcrl = NULL;
879 int ok = 0, cnum = 0;
880 unsigned int last_reasons = 0;
881 cnum = ctx->error_depth;
882 x = sk_X509_value(ctx->chain, cnum);
883 ctx->current_cert = x;
884 ctx->current_issuer = NULL;
885 ctx->current_crl_score = 0;
886 ctx->current_reasons = 0;
887 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
888 last_reasons = ctx->current_reasons;
889 /* Try to retrieve relevant CRL */
891 ok = ctx->get_crl(ctx, &crl, x);
893 ok = get_crl_delta(ctx, &crl, &dcrl, x);
895 * If error looking up CRL, nothing we can do except notify callback
898 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
899 ok = ctx->verify_cb(0, ctx);
902 ctx->current_crl = crl;
903 ok = ctx->check_crl(ctx, crl);
908 ok = ctx->check_crl(ctx, dcrl);
911 ok = ctx->cert_crl(ctx, dcrl, x);
917 /* Don't look in full CRL if delta reason is removefromCRL */
919 ok = ctx->cert_crl(ctx, crl, x);
929 * If reasons not updated we wont get anywhere by another iteration,
932 if (last_reasons == ctx->current_reasons) {
933 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
934 ok = ctx->verify_cb(0, ctx);
942 ctx->current_crl = NULL;
947 /* Check CRL times against values in X509_STORE_CTX */
949 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
954 ctx->current_crl = crl;
955 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
956 ptime = &ctx->param->check_time;
960 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
964 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
965 if (!ctx->verify_cb(0, ctx))
972 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
973 if (!ctx->verify_cb(0, ctx))
977 if (X509_CRL_get_nextUpdate(crl)) {
978 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
983 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
984 if (!ctx->verify_cb(0, ctx))
987 /* Ignore expiry of base CRL is delta is valid */
988 if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
991 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
992 if (!ctx->verify_cb(0, ctx))
998 ctx->current_crl = NULL;
1003 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
1004 X509 **pissuer, int *pscore, unsigned int *preasons,
1005 STACK_OF(X509_CRL) *crls)
1007 int i, crl_score, best_score = *pscore;
1008 unsigned int reasons, best_reasons = 0;
1009 X509 *x = ctx->current_cert;
1010 X509_CRL *crl, *best_crl = NULL;
1011 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
1013 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1014 crl = sk_X509_CRL_value(crls, i);
1015 reasons = *preasons;
1016 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
1018 if (crl_score > best_score) {
1020 best_crl_issuer = crl_issuer;
1021 best_score = crl_score;
1022 best_reasons = reasons;
1027 X509_CRL_free(*pcrl);
1029 *pissuer = best_crl_issuer;
1030 *pscore = best_score;
1031 *preasons = best_reasons;
1032 X509_CRL_up_ref(best_crl);
1033 X509_CRL_free(*pdcrl);
1035 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
1038 if (best_score >= CRL_SCORE_VALID)
1045 * Compare two CRL extensions for delta checking purposes. They should be
1046 * both present or both absent. If both present all fields must be identical.
1049 static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
1051 ASN1_OCTET_STRING *exta, *extb;
1053 i = X509_CRL_get_ext_by_NID(a, nid, -1);
1055 /* Can't have multiple occurrences */
1056 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
1058 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
1062 i = X509_CRL_get_ext_by_NID(b, nid, -1);
1066 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
1068 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
1078 if (ASN1_OCTET_STRING_cmp(exta, extb))
1084 /* See if a base and delta are compatible */
1086 static int check_delta_base(X509_CRL *delta, X509_CRL *base)
1088 /* Delta CRL must be a delta */
1089 if (!delta->base_crl_number)
1091 /* Base must have a CRL number */
1092 if (!base->crl_number)
1094 /* Issuer names must match */
1095 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
1097 /* AKID and IDP must match */
1098 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
1100 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
1102 /* Delta CRL base number must not exceed Full CRL number. */
1103 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
1105 /* Delta CRL number must exceed full CRL number */
1106 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
1112 * For a given base CRL find a delta... maybe extend to delta scoring or
1113 * retrieve a chain of deltas...
1116 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
1117 X509_CRL *base, STACK_OF(X509_CRL) *crls)
1121 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
1123 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
1125 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
1126 delta = sk_X509_CRL_value(crls, i);
1127 if (check_delta_base(delta, base)) {
1128 if (check_crl_time(ctx, delta, 0))
1129 *pscore |= CRL_SCORE_TIME_DELTA;
1130 X509_CRL_up_ref(delta);
1139 * For a given CRL return how suitable it is for the supplied certificate
1140 * 'x'. The return value is a mask of several criteria. If the issuer is not
1141 * the certificate issuer this is returned in *pissuer. The reasons mask is
1142 * also used to determine if the CRL is suitable: if no new reasons the CRL
1143 * is rejected, otherwise reasons is updated.
1146 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
1147 unsigned int *preasons, X509_CRL *crl, X509 *x)
1151 unsigned int tmp_reasons = *preasons, crl_reasons;
1153 /* First see if we can reject CRL straight away */
1155 /* Invalid IDP cannot be processed */
1156 if (crl->idp_flags & IDP_INVALID)
1158 /* Reason codes or indirect CRLs need extended CRL support */
1159 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
1160 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
1162 } else if (crl->idp_flags & IDP_REASONS) {
1163 /* If no new reasons reject */
1164 if (!(crl->idp_reasons & ~tmp_reasons))
1167 /* Don't process deltas at this stage */
1168 else if (crl->base_crl_number)
1170 /* If issuer name doesn't match certificate need indirect CRL */
1171 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
1172 if (!(crl->idp_flags & IDP_INDIRECT))
1175 crl_score |= CRL_SCORE_ISSUER_NAME;
1177 if (!(crl->flags & EXFLAG_CRITICAL))
1178 crl_score |= CRL_SCORE_NOCRITICAL;
1181 if (check_crl_time(ctx, crl, 0))
1182 crl_score |= CRL_SCORE_TIME;
1184 /* Check authority key ID and locate certificate issuer */
1185 crl_akid_check(ctx, crl, pissuer, &crl_score);
1187 /* If we can't locate certificate issuer at this point forget it */
1189 if (!(crl_score & CRL_SCORE_AKID))
1192 /* Check cert for matching CRL distribution points */
1194 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1195 /* If no new reasons reject */
1196 if (!(crl_reasons & ~tmp_reasons))
1198 tmp_reasons |= crl_reasons;
1199 crl_score |= CRL_SCORE_SCOPE;
1202 *preasons = tmp_reasons;
1208 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
1209 X509 **pissuer, int *pcrl_score)
1211 X509 *crl_issuer = NULL;
1212 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1213 int cidx = ctx->error_depth;
1216 if (cidx != sk_X509_num(ctx->chain) - 1)
1219 crl_issuer = sk_X509_value(ctx->chain, cidx);
1221 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1222 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1223 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
1224 *pissuer = crl_issuer;
1229 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1230 crl_issuer = sk_X509_value(ctx->chain, cidx);
1231 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1233 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1234 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
1235 *pissuer = crl_issuer;
1240 /* Anything else needs extended CRL support */
1242 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1246 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1247 * untrusted certificates.
1249 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1250 crl_issuer = sk_X509_value(ctx->untrusted, i);
1251 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1253 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1254 *pissuer = crl_issuer;
1255 *pcrl_score |= CRL_SCORE_AKID;
1262 * Check the path of a CRL issuer certificate. This creates a new
1263 * X509_STORE_CTX and populates it with most of the parameters from the
1264 * parent. This could be optimised somewhat since a lot of path checking will
1265 * be duplicated by the parent, but this will rarely be used in practice.
1268 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1270 X509_STORE_CTX crl_ctx;
1272 /* Don't allow recursive CRL path validation */
1275 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
1278 crl_ctx.crls = ctx->crls;
1279 /* Copy verify params across */
1280 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1282 crl_ctx.parent = ctx;
1283 crl_ctx.verify_cb = ctx->verify_cb;
1285 /* Verify CRL issuer */
1286 ret = X509_verify_cert(&crl_ctx);
1291 /* Check chain is acceptable */
1293 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1295 X509_STORE_CTX_cleanup(&crl_ctx);
1300 * RFC3280 says nothing about the relationship between CRL path and
1301 * certificate path, which could lead to situations where a certificate could
1302 * be revoked or validated by a CA not authorised to do so. RFC5280 is more
1303 * strict and states that the two paths must end in the same trust anchor,
1304 * though some discussions remain... until this is resolved we use the
1308 static int check_crl_chain(X509_STORE_CTX *ctx,
1309 STACK_OF(X509) *cert_path,
1310 STACK_OF(X509) *crl_path)
1312 X509 *cert_ta, *crl_ta;
1313 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1314 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1315 if (!X509_cmp(cert_ta, crl_ta))
1321 * Check for match between two dist point names: three separate cases.
1322 * 1. Both are relative names and compare X509_NAME types.
1323 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1324 * 3. Both are full names and compare two GENERAL_NAMES.
1325 * 4. One is NULL: automatic match.
1328 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1330 X509_NAME *nm = NULL;
1331 GENERAL_NAMES *gens = NULL;
1332 GENERAL_NAME *gena, *genb;
1339 /* Case 1: two X509_NAME */
1343 if (!X509_NAME_cmp(a->dpname, b->dpname))
1348 /* Case 2: set name and GENERAL_NAMES appropriately */
1350 gens = b->name.fullname;
1351 } else if (b->type == 1) {
1354 /* Case 2: set name and GENERAL_NAMES appropriately */
1355 gens = a->name.fullname;
1359 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1361 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1362 gena = sk_GENERAL_NAME_value(gens, i);
1363 if (gena->type != GEN_DIRNAME)
1365 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1371 /* Else case 3: two GENERAL_NAMES */
1373 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1374 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1375 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1376 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1377 if (!GENERAL_NAME_cmp(gena, genb))
1386 static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1389 X509_NAME *nm = X509_CRL_get_issuer(crl);
1390 /* If no CRLissuer return is successful iff don't need a match */
1392 return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
1393 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1394 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1395 if (gen->type != GEN_DIRNAME)
1397 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1403 /* Check CRLDP and IDP */
1405 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
1406 unsigned int *preasons)
1409 if (crl->idp_flags & IDP_ONLYATTR)
1411 if (x->ex_flags & EXFLAG_CA) {
1412 if (crl->idp_flags & IDP_ONLYUSER)
1415 if (crl->idp_flags & IDP_ONLYCA)
1418 *preasons = crl->idp_reasons;
1419 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1420 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1421 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1422 if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1423 *preasons &= dp->dp_reasons;
1428 if ((!crl->idp || !crl->idp->distpoint)
1429 && (crl_score & CRL_SCORE_ISSUER_NAME))
1435 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1436 * to find a delta CRL too
1439 static int get_crl_delta(X509_STORE_CTX *ctx,
1440 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1443 X509 *issuer = NULL;
1445 unsigned int reasons;
1446 X509_CRL *crl = NULL, *dcrl = NULL;
1447 STACK_OF(X509_CRL) *skcrl;
1448 X509_NAME *nm = X509_get_issuer_name(x);
1449 reasons = ctx->current_reasons;
1450 ok = get_crl_sk(ctx, &crl, &dcrl,
1451 &issuer, &crl_score, &reasons, ctx->crls);
1456 /* Lookup CRLs from store */
1458 skcrl = ctx->lookup_crls(ctx, nm);
1460 /* If no CRLs found and a near match from get_crl_sk use that */
1464 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1466 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1470 /* If we got any kind of CRL use it and return success */
1472 ctx->current_issuer = issuer;
1473 ctx->current_crl_score = crl_score;
1474 ctx->current_reasons = reasons;
1483 /* Check CRL validity */
1484 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1486 X509 *issuer = NULL;
1487 EVP_PKEY *ikey = NULL;
1488 int ok = 0, chnum, cnum;
1489 cnum = ctx->error_depth;
1490 chnum = sk_X509_num(ctx->chain) - 1;
1491 /* if we have an alternative CRL issuer cert use that */
1492 if (ctx->current_issuer)
1493 issuer = ctx->current_issuer;
1496 * Else find CRL issuer: if not last certificate then issuer is next
1497 * certificate in chain.
1499 else if (cnum < chnum)
1500 issuer = sk_X509_value(ctx->chain, cnum + 1);
1502 issuer = sk_X509_value(ctx->chain, chnum);
1503 /* If not self signed, can't check signature */
1504 if (!ctx->check_issued(ctx, issuer, issuer)) {
1505 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1506 ok = ctx->verify_cb(0, ctx);
1514 * Skip most tests for deltas because they have already been done
1516 if (!crl->base_crl_number) {
1517 /* Check for cRLSign bit if keyUsage present */
1518 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1519 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1520 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1521 ok = ctx->verify_cb(0, ctx);
1526 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1527 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1528 ok = ctx->verify_cb(0, ctx);
1533 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1534 if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
1535 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1536 ok = ctx->verify_cb(0, ctx);
1542 if (crl->idp_flags & IDP_INVALID) {
1543 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1544 ok = ctx->verify_cb(0, ctx);
1551 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1552 ok = check_crl_time(ctx, crl, 1);
1557 /* Attempt to get issuer certificate public key */
1558 ikey = X509_get_pubkey(issuer);
1561 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1562 ok = ctx->verify_cb(0, ctx);
1567 rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
1568 if (rv != X509_V_OK) {
1570 ok = ctx->verify_cb(0, ctx);
1574 /* Verify CRL signature */
1575 if (X509_CRL_verify(crl, ikey) <= 0) {
1576 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1577 ok = ctx->verify_cb(0, ctx);
1587 EVP_PKEY_free(ikey);
1591 /* Check certificate against CRL */
1592 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1597 * The rules changed for this... previously if a CRL contained unhandled
1598 * critical extensions it could still be used to indicate a certificate
1599 * was revoked. This has since been changed since critical extension can
1600 * change the meaning of CRL entries.
1602 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
1603 && (crl->flags & EXFLAG_CRITICAL)) {
1604 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1605 ok = ctx->verify_cb(0, ctx);
1610 * Look for serial number of certificate in CRL If found make sure reason
1611 * is not removeFromCRL.
1613 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1614 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1616 ctx->error = X509_V_ERR_CERT_REVOKED;
1617 ok = ctx->verify_cb(0, ctx);
1625 static int check_policy(X509_STORE_CTX *ctx)
1630 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1631 ctx->param->policies, ctx->param->flags);
1633 X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
1636 /* Invalid or inconsistent extensions */
1639 * Locate certificates with bad extensions and notify callback.
1643 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1644 x = sk_X509_value(ctx->chain, i);
1645 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1647 ctx->current_cert = x;
1648 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1649 if (!ctx->verify_cb(0, ctx))
1655 ctx->current_cert = NULL;
1656 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1657 return ctx->verify_cb(0, ctx);
1660 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1661 ctx->current_cert = NULL;
1662 ctx->error = X509_V_OK;
1663 if (!ctx->verify_cb(2, ctx))
1670 int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int quiet)
1675 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1676 ptime = &ctx->param->check_time;
1680 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1684 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1685 ctx->current_cert = x;
1686 if (!ctx->verify_cb(0, ctx))
1693 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1694 ctx->current_cert = x;
1695 if (!ctx->verify_cb(0, ctx))
1699 i = X509_cmp_time(X509_get_notAfter(x), ptime);
1703 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1704 ctx->current_cert = x;
1705 if (!ctx->verify_cb(0, ctx))
1712 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1713 ctx->current_cert = x;
1714 if (!ctx->verify_cb(0, ctx))
1721 static int internal_verify(X509_STORE_CTX *ctx)
1725 EVP_PKEY *pkey = NULL;
1726 int (*cb) (int xok, X509_STORE_CTX *xctx);
1728 cb = ctx->verify_cb;
1730 n = sk_X509_num(ctx->chain);
1731 ctx->error_depth = n - 1;
1733 xi = sk_X509_value(ctx->chain, n);
1735 if (ctx->check_issued(ctx, xi, xi))
1738 if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
1743 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1744 ctx->current_cert = xi;
1749 ctx->error_depth = n;
1750 xs = sk_X509_value(ctx->chain, n);
1754 /* ctx->error=0; not needed */
1756 ctx->error_depth = n;
1759 * Skip signature check for self signed certificates unless
1760 * explicitly asked for. It doesn't add any security and just wastes
1765 || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
1766 if ((pkey = X509_get_pubkey(xi)) == NULL) {
1767 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1768 ctx->current_cert = xi;
1769 ok = (*cb) (0, ctx);
1772 } else if (X509_verify(xs, pkey) <= 0) {
1773 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1774 ctx->current_cert = xs;
1775 ok = (*cb) (0, ctx);
1777 EVP_PKEY_free(pkey);
1781 EVP_PKEY_free(pkey);
1788 ok = x509_check_cert_time(ctx, xs, 0);
1792 /* The last error (if any) is still in the error value */
1793 ctx->current_issuer = xi;
1794 ctx->current_cert = xs;
1795 ok = (*cb) (1, ctx);
1802 xs = sk_X509_value(ctx->chain, n);
1810 int X509_cmp_current_time(const ASN1_TIME *ctm)
1812 return X509_cmp_time(ctm, NULL);
1815 int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1820 char buff1[24], buff2[24], *p;
1821 int i, j, remaining;
1824 remaining = ctm->length;
1825 str = (char *)ctm->data;
1827 * Note that the following (historical) code allows much more slack in the
1828 * time format than RFC5280. In RFC5280, the representation is fixed:
1829 * UTCTime: YYMMDDHHMMSSZ
1830 * GeneralizedTime: YYYYMMDDHHMMSSZ
1832 if (ctm->type == V_ASN1_UTCTIME) {
1833 /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
1834 int min_length = sizeof("YYMMDDHHMMZ") - 1;
1835 int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
1836 if (remaining < min_length || remaining > max_length)
1843 /* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
1844 int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
1845 int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
1846 if (remaining < min_length || remaining > max_length)
1854 if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
1865 * Skip any (up to three) fractional seconds...
1866 * TODO(emilia): in RFC5280, fractional seconds are forbidden.
1867 * Can we just kill them altogether?
1869 if (remaining && *str == '.') {
1872 for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
1873 if (*str < '0' || *str > '9')
1882 /* We now need either a terminating 'Z' or an offset. */
1891 if ((*str != '+') && (*str != '-'))
1893 /* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
1896 if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
1897 str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
1899 offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
1900 offset += (str[3] - '0') * 10 + (str[4] - '0');
1904 atm.type = ctm->type;
1906 atm.length = sizeof(buff2);
1907 atm.data = (unsigned char *)buff2;
1909 if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
1912 if (ctm->type == V_ASN1_UTCTIME) {
1913 i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
1915 i += 100; /* cf. RFC 2459 */
1916 j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
1925 i = strcmp(buff1, buff2);
1926 if (i == 0) /* wait a second then return younger :-) */
1932 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
1934 return X509_time_adj(s, adj, NULL);
1937 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
1939 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1942 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
1943 int offset_day, long offset_sec, time_t *in_tm)
1952 if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
1953 if (s->type == V_ASN1_UTCTIME)
1954 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
1955 if (s->type == V_ASN1_GENERALIZEDTIME)
1956 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
1958 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1961 int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1963 EVP_PKEY *ktmp = NULL, *ktmp2;
1966 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1969 for (i = 0; i < sk_X509_num(chain); i++) {
1970 ktmp = X509_get_pubkey(sk_X509_value(chain, i));
1972 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1973 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1976 if (!EVP_PKEY_missing_parameters(ktmp))
1978 EVP_PKEY_free(ktmp);
1982 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1983 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1987 /* first, populate the other certs */
1988 for (j = i - 1; j >= 0; j--) {
1989 ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
1990 EVP_PKEY_copy_parameters(ktmp2, ktmp);
1991 EVP_PKEY_free(ktmp2);
1995 EVP_PKEY_copy_parameters(pkey, ktmp);
1996 EVP_PKEY_free(ktmp);
2000 /* Make a delta CRL as the diff between two full CRLs */
2002 X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
2003 EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
2005 X509_CRL *crl = NULL;
2007 STACK_OF(X509_REVOKED) *revs = NULL;
2008 /* CRLs can't be delta already */
2009 if (base->base_crl_number || newer->base_crl_number) {
2010 X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_ALREADY_DELTA);
2013 /* Base and new CRL must have a CRL number */
2014 if (!base->crl_number || !newer->crl_number) {
2015 X509err(X509_F_X509_CRL_DIFF, X509_R_NO_CRL_NUMBER);
2018 /* Issuer names must match */
2019 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
2020 X509err(X509_F_X509_CRL_DIFF, X509_R_ISSUER_MISMATCH);
2023 /* AKID and IDP must match */
2024 if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
2025 X509err(X509_F_X509_CRL_DIFF, X509_R_AKID_MISMATCH);
2028 if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
2029 X509err(X509_F_X509_CRL_DIFF, X509_R_IDP_MISMATCH);
2032 /* Newer CRL number must exceed full CRL number */
2033 if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
2034 X509err(X509_F_X509_CRL_DIFF, X509_R_NEWER_CRL_NOT_NEWER);
2037 /* CRLs must verify */
2038 if (skey && (X509_CRL_verify(base, skey) <= 0 ||
2039 X509_CRL_verify(newer, skey) <= 0)) {
2040 X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_VERIFY_FAILURE);
2043 /* Create new CRL */
2044 crl = X509_CRL_new();
2045 if (!crl || !X509_CRL_set_version(crl, 1))
2047 /* Set issuer name */
2048 if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
2051 if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
2053 if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
2056 /* Set base CRL number: must be critical */
2058 if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
2062 * Copy extensions across from newest CRL to delta: this will set CRL
2063 * number to correct value too.
2066 for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
2067 X509_EXTENSION *ext;
2068 ext = X509_CRL_get_ext(newer, i);
2069 if (!X509_CRL_add_ext(crl, ext, -1))
2073 /* Go through revoked entries, copying as needed */
2075 revs = X509_CRL_get_REVOKED(newer);
2077 for (i = 0; i < sk_X509_REVOKED_num(revs); i++) {
2078 X509_REVOKED *rvn, *rvtmp;
2079 rvn = sk_X509_REVOKED_value(revs, i);
2081 * Add only if not also in base. TODO: need something cleverer here
2082 * for some more complex CRLs covering multiple CAs.
2084 if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) {
2085 rvtmp = X509_REVOKED_dup(rvn);
2088 if (!X509_CRL_add0_revoked(crl, rvtmp)) {
2089 X509_REVOKED_free(rvtmp);
2094 /* TODO: optionally prune deleted entries */
2096 if (skey && md && !X509_CRL_sign(crl, skey, md))
2102 X509err(X509_F_X509_CRL_DIFF, ERR_R_MALLOC_FAILURE);
2107 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
2108 CRYPTO_EX_new *new_func,
2109 CRYPTO_EX_dup *dup_func,
2110 CRYPTO_EX_free *free_func)
2113 * This function is (usually) called only once, by
2114 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
2116 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
2117 new_func, dup_func, free_func);
2120 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
2122 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
2125 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
2127 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
2130 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
2135 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
2140 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
2142 return ctx->error_depth;
2145 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
2147 return ctx->current_cert;
2150 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
2155 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
2159 return X509_chain_up_ref(ctx->chain);
2162 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
2164 return ctx->current_issuer;
2167 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
2169 return ctx->current_crl;
2172 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
2177 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
2182 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2184 ctx->untrusted = sk;
2187 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
2192 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
2194 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
2197 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
2199 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
2203 * This function is used to set the X509_STORE_CTX purpose and trust values.
2204 * This is intended to be used when another structure has its own trust and
2205 * purpose values which (if set) will be inherited by the ctx. If they aren't
2206 * set then we will usually have a default purpose in mind which should then
2207 * be used to set the trust value. An example of this is SSL use: an SSL
2208 * structure will have its own purpose and trust settings which the
2209 * application can set: if they aren't set then we use the default of SSL
2213 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
2214 int purpose, int trust)
2217 /* If purpose not set use default */
2219 purpose = def_purpose;
2220 /* If we have a purpose then check it is valid */
2223 idx = X509_PURPOSE_get_by_id(purpose);
2225 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
2226 X509_R_UNKNOWN_PURPOSE_ID);
2229 ptmp = X509_PURPOSE_get0(idx);
2230 if (ptmp->trust == X509_TRUST_DEFAULT) {
2231 idx = X509_PURPOSE_get_by_id(def_purpose);
2233 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
2234 X509_R_UNKNOWN_PURPOSE_ID);
2237 ptmp = X509_PURPOSE_get0(idx);
2239 /* If trust not set then get from purpose default */
2241 trust = ptmp->trust;
2244 idx = X509_TRUST_get_by_id(trust);
2246 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
2247 X509_R_UNKNOWN_TRUST_ID);
2252 if (purpose && !ctx->param->purpose)
2253 ctx->param->purpose = purpose;
2254 if (trust && !ctx->param->trust)
2255 ctx->param->trust = trust;
2259 X509_STORE_CTX *X509_STORE_CTX_new(void)
2261 X509_STORE_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
2264 X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
2267 memset(ctx, 0, sizeof(*ctx));
2271 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
2275 X509_STORE_CTX_cleanup(ctx);
2279 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
2280 STACK_OF(X509) *chain)
2284 ctx->current_method = 0;
2286 ctx->untrusted = chain;
2288 ctx->last_untrusted = 0;
2289 ctx->other_ctx = NULL;
2293 ctx->explicit_policy = 0;
2294 ctx->error_depth = 0;
2295 ctx->current_cert = NULL;
2296 ctx->current_issuer = NULL;
2297 ctx->current_crl = NULL;
2298 ctx->current_crl_score = 0;
2299 ctx->current_reasons = 0;
2303 ctx->param = X509_VERIFY_PARAM_new();
2306 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2311 * Inherit callbacks and flags from X509_STORE if not set use defaults.
2315 ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2317 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
2320 ctx->verify_cb = store->verify_cb;
2321 ctx->cleanup = store->cleanup;
2326 ret = X509_VERIFY_PARAM_inherit(ctx->param,
2327 X509_VERIFY_PARAM_lookup("default"));
2330 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2334 if (store && store->check_issued)
2335 ctx->check_issued = store->check_issued;
2337 ctx->check_issued = check_issued;
2339 if (store && store->get_issuer)
2340 ctx->get_issuer = store->get_issuer;
2342 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2344 if (store && store->verify_cb)
2345 ctx->verify_cb = store->verify_cb;
2347 ctx->verify_cb = null_callback;
2349 if (store && store->verify)
2350 ctx->verify = store->verify;
2352 ctx->verify = internal_verify;
2354 if (store && store->check_revocation)
2355 ctx->check_revocation = store->check_revocation;
2357 ctx->check_revocation = check_revocation;
2359 if (store && store->get_crl)
2360 ctx->get_crl = store->get_crl;
2362 ctx->get_crl = NULL;
2364 if (store && store->check_crl)
2365 ctx->check_crl = store->check_crl;
2367 ctx->check_crl = check_crl;
2369 if (store && store->cert_crl)
2370 ctx->cert_crl = store->cert_crl;
2372 ctx->cert_crl = cert_crl;
2374 if (store && store->lookup_certs)
2375 ctx->lookup_certs = store->lookup_certs;
2377 ctx->lookup_certs = X509_STORE_get1_certs;
2379 if (store && store->lookup_crls)
2380 ctx->lookup_crls = store->lookup_crls;
2382 ctx->lookup_crls = X509_STORE_get1_crls;
2384 ctx->check_policy = check_policy;
2387 * Since X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we
2388 * put a corresponding "new" here.
2390 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2393 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2400 * Set alternative lookup method: just a STACK of trusted certificates. This
2401 * avoids X509_STORE nastiness where it isn't needed.
2404 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2406 ctx->other_ctx = sk;
2407 ctx->get_issuer = get_issuer_sk;
2410 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2414 if (ctx->param != NULL) {
2415 if (ctx->parent == NULL)
2416 X509_VERIFY_PARAM_free(ctx->param);
2419 X509_policy_tree_free(ctx->tree);
2421 sk_X509_pop_free(ctx->chain, X509_free);
2423 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
2424 memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
2427 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2429 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2432 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2434 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2437 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
2440 X509_VERIFY_PARAM_set_time(ctx->param, t);
2443 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2444 int (*verify_cb) (int, X509_STORE_CTX *))
2446 ctx->verify_cb = verify_cb;
2449 X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2454 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2456 return ctx->explicit_policy;
2459 int X509_STORE_CTX_get_num_untrusted(X509_STORE_CTX *ctx)
2461 return ctx->last_untrusted;
2464 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2466 const X509_VERIFY_PARAM *param;
2467 param = X509_VERIFY_PARAM_lookup(name);
2470 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2473 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2478 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2480 X509_VERIFY_PARAM_free(ctx->param);