1 /* crypto/x509/x509_vfy.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
64 #include <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>
73 /* CRL score values */
75 /* No unhandled critical extensions */
77 #define CRL_SCORE_NOCRITICAL 0x100
79 /* certificate is within CRL scope */
81 #define CRL_SCORE_SCOPE 0x080
85 #define CRL_SCORE_TIME 0x040
87 /* Issuer name matches certificate */
89 #define CRL_SCORE_ISSUER_NAME 0x020
91 /* If this score or above CRL is probably valid */
93 #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
95 /* CRL issuer is certificate issuer */
97 #define CRL_SCORE_ISSUER_CERT 0x018
99 /* CRL issuer is on certificate path */
101 #define CRL_SCORE_SAME_PATH 0x008
103 /* CRL issuer matches CRL AKID */
105 #define CRL_SCORE_AKID 0x004
107 /* Have a delta CRL with valid times */
109 #define CRL_SCORE_TIME_DELTA 0x002
111 static int null_callback(int ok, X509_STORE_CTX *e);
112 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
113 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
114 static int check_chain_extensions(X509_STORE_CTX *ctx);
115 static int check_name_constraints(X509_STORE_CTX *ctx);
116 static int check_trust(X509_STORE_CTX *ctx);
117 static int check_revocation(X509_STORE_CTX *ctx);
118 static int check_cert(X509_STORE_CTX *ctx);
119 static int check_policy(X509_STORE_CTX *ctx);
121 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
122 unsigned int *preasons, X509_CRL *crl, X509 *x);
123 static int get_crl_delta(X509_STORE_CTX *ctx,
124 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
125 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
126 int *pcrl_score, X509_CRL *base,
127 STACK_OF(X509_CRL) *crls);
128 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
130 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
131 unsigned int *preasons);
132 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
133 static int check_crl_chain(X509_STORE_CTX *ctx,
134 STACK_OF(X509) *cert_path,
135 STACK_OF(X509) *crl_path);
137 static int internal_verify(X509_STORE_CTX *ctx);
138 const char X509_version[] = "X.509" OPENSSL_VERSION_PTEXT;
140 static int null_callback(int ok, X509_STORE_CTX *e)
146 static int x509_subject_cmp(X509 **a, X509 **b)
148 return X509_subject_name_cmp(*a, *b);
152 int X509_verify_cert(X509_STORE_CTX *ctx)
154 X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
156 X509_VERIFY_PARAM *param = ctx->param;
157 int depth, i, ok = 0;
159 int (*cb) (int xok, X509_STORE_CTX *xctx);
160 STACK_OF(X509) *sktmp = NULL;
161 if (ctx->cert == NULL) {
162 X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
165 if (ctx->chain != NULL) {
167 * This X509_STORE_CTX has already been used to verify a cert. We
168 * cannot do another one.
170 X509err(X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
177 * first we make sure the chain we are going to build is present and that
178 * the first entry is in place
180 if (((ctx->chain = sk_X509_new_null()) == NULL) ||
181 (!sk_X509_push(ctx->chain, ctx->cert))) {
182 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
185 CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
186 ctx->last_untrusted = 1;
188 /* We use a temporary STACK so we can chop and hack at it */
189 if (ctx->untrusted != NULL
190 && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
191 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
195 num = sk_X509_num(ctx->chain);
196 x = sk_X509_value(ctx->chain, num - 1);
197 depth = param->depth;
200 /* If we have enough, we break */
202 break; /* FIXME: If this happens, we should take
203 * note of it and, if appropriate, use the
204 * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
207 /* If we are self signed, we break */
208 if (ctx->check_issued(ctx, x, x))
211 /* If we were passed a cert chain, use it first */
212 if (ctx->untrusted != NULL) {
213 xtmp = find_issuer(ctx, sktmp, x);
215 if (!sk_X509_push(ctx->chain, xtmp)) {
216 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
219 CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
220 (void)sk_X509_delete_ptr(sktmp, xtmp);
221 ctx->last_untrusted++;
225 * reparse the full chain for the next one
233 /* Remember how many untrusted certs we have */
236 * at this point, chain should contain a list of untrusted certificates.
237 * We now need to add at least one trusted one, if possible, otherwise we
243 * Examine last certificate in chain and see if it is self signed.
245 i = sk_X509_num(ctx->chain);
246 x = sk_X509_value(ctx->chain, i - 1);
247 if (ctx->check_issued(ctx, x, x)) {
248 /* we have a self signed certificate */
249 if (sk_X509_num(ctx->chain) == 1) {
251 * We have a single self signed certificate: see if we can
252 * find it in the store. We must have an exact match to avoid
253 * possible impersonation.
255 ok = ctx->get_issuer(&xtmp, ctx, x);
256 if ((ok <= 0) || X509_cmp(x, xtmp)) {
257 ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
258 ctx->current_cert = x;
259 ctx->error_depth = i - 1;
268 * We have a match: replace certificate with store
269 * version so we get any trust settings.
273 (void)sk_X509_set(ctx->chain, i - 1, x);
274 ctx->last_untrusted = 0;
278 * extract and save self signed certificate for later use
280 chain_ss = sk_X509_pop(ctx->chain);
281 ctx->last_untrusted--;
284 x = sk_X509_value(ctx->chain, num - 1);
287 /* We now lookup certs from the certificate store */
289 /* If we have enough, we break */
292 /* If we are self signed, we break */
293 if (ctx->check_issued(ctx, x, x))
295 ok = ctx->get_issuer(&xtmp, ctx, x);
301 if (!sk_X509_push(ctx->chain, x)) {
303 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
310 * If we haven't got a least one certificate from our store then check
311 * if there is an alternative chain that could be used. We only do this
312 * if the user hasn't switched off alternate chain checking
315 if (num == ctx->last_untrusted &&
316 !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
318 xtmp2 = sk_X509_value(ctx->chain, j - 1);
319 ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
322 /* Check if we found an alternate chain */
325 * Free up the found cert we'll add it again later
330 * Dump all the certs above this point - we've found an
334 xtmp = sk_X509_pop(ctx->chain);
338 ctx->last_untrusted = sk_X509_num(ctx->chain);
346 /* Is last certificate looked up self signed? */
347 if (!ctx->check_issued(ctx, x, x)) {
348 if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
349 if (ctx->last_untrusted >= num)
350 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
352 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
353 ctx->current_cert = x;
356 sk_X509_push(ctx->chain, chain_ss);
358 ctx->last_untrusted = num;
359 ctx->current_cert = chain_ss;
360 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
364 ctx->error_depth = num - 1;
371 /* We have the chain complete: now we need to check its purpose */
372 ok = check_chain_extensions(ctx);
377 /* Check name constraints */
379 ok = check_name_constraints(ctx);
384 /* The chain extensions are OK: check trust */
386 if (param->trust > 0)
387 ok = check_trust(ctx);
392 /* We may as well copy down any DSA parameters that are required */
393 X509_get_pubkey_parameters(NULL, ctx->chain);
396 * Check revocation status: we do this after copying parameters because
397 * they may be needed for CRL signature verification.
400 ok = ctx->check_revocation(ctx);
404 /* At this point, we have a chain and need to verify it */
405 if (ctx->verify != NULL)
406 ok = ctx->verify(ctx);
408 ok = internal_verify(ctx);
412 #ifndef OPENSSL_NO_RFC3779
413 /* RFC 3779 path validation, now that CRL check has been done */
414 ok = v3_asid_validate_path(ctx);
417 ok = v3_addr_validate_path(ctx);
422 /* If we get this far evaluate policies */
423 if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
424 ok = ctx->check_policy(ctx);
429 X509_get_pubkey_parameters(NULL, ctx->chain);
433 if (chain_ss != NULL)
439 * Given a STACK_OF(X509) find the issuer of cert (if any)
442 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
446 for (i = 0; i < sk_X509_num(sk); i++) {
447 issuer = sk_X509_value(sk, i);
448 if (ctx->check_issued(ctx, x, issuer))
454 /* Given a possible certificate and issuer check them */
456 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
459 ret = X509_check_issued(issuer, x);
460 if (ret == X509_V_OK)
462 /* If we haven't asked for issuer errors don't set ctx */
463 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
467 ctx->current_cert = x;
468 ctx->current_issuer = issuer;
469 return ctx->verify_cb(0, ctx);
473 /* Alternative lookup method: look from a STACK stored in other_ctx */
475 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
477 *issuer = find_issuer(ctx, ctx->other_ctx, x);
479 CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
486 * Check a certificate chains extensions for consistency with the supplied
490 static int check_chain_extensions(X509_STORE_CTX *ctx)
492 #ifdef OPENSSL_NO_CHAIN_VERIFY
495 int i, ok = 0, must_be_ca, plen = 0;
497 int (*cb) (int xok, X509_STORE_CTX *xctx);
498 int proxy_path_length = 0;
500 int allow_proxy_certs;
504 * must_be_ca can have 1 of 3 values:
505 * -1: we accept both CA and non-CA certificates, to allow direct
506 * use of self-signed certificates (which are marked as CA).
507 * 0: we only accept non-CA certificates. This is currently not
508 * used, but the possibility is present for future extensions.
509 * 1: we only accept CA certificates. This is currently used for
510 * all certificates in the chain except the leaf certificate.
514 /* CRL path validation */
516 allow_proxy_certs = 0;
517 purpose = X509_PURPOSE_CRL_SIGN;
520 ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
522 * A hack to keep people who don't want to modify their software
525 if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
526 allow_proxy_certs = 1;
527 purpose = ctx->param->purpose;
530 /* Check all untrusted certificates */
531 for (i = 0; i < ctx->last_untrusted; i++) {
533 x = sk_X509_value(ctx->chain, i);
534 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
535 && (x->ex_flags & EXFLAG_CRITICAL)) {
536 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
537 ctx->error_depth = i;
538 ctx->current_cert = x;
543 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
544 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
545 ctx->error_depth = i;
546 ctx->current_cert = x;
551 ret = X509_check_ca(x);
552 switch (must_be_ca) {
554 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
555 && (ret != 1) && (ret != 0)) {
557 ctx->error = X509_V_ERR_INVALID_CA;
564 ctx->error = X509_V_ERR_INVALID_NON_CA;
570 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
573 ctx->error = X509_V_ERR_INVALID_CA;
579 ctx->error_depth = i;
580 ctx->current_cert = x;
585 if (ctx->param->purpose > 0) {
586 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
588 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
590 ctx->error = X509_V_ERR_INVALID_PURPOSE;
591 ctx->error_depth = i;
592 ctx->current_cert = x;
598 /* Check pathlen if not self issued */
599 if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
600 && (x->ex_pathlen != -1)
601 && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
602 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
603 ctx->error_depth = i;
604 ctx->current_cert = x;
609 /* Increment path length if not self issued */
610 if (!(x->ex_flags & EXFLAG_SI))
613 * If this certificate is a proxy certificate, the next certificate
614 * must be another proxy certificate or a EE certificate. If not,
615 * the next certificate must be a CA certificate.
617 if (x->ex_flags & EXFLAG_PROXY) {
618 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
619 ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
620 ctx->error_depth = i;
621 ctx->current_cert = x;
637 static int check_name_constraints(X509_STORE_CTX *ctx)
641 /* Check name constraints for all certificates */
642 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
643 x = sk_X509_value(ctx->chain, i);
644 /* Ignore self issued certs unless last in chain */
645 if (i && (x->ex_flags & EXFLAG_SI))
648 * Check against constraints for all certificates higher in chain
649 * including trust anchor. Trust anchor not strictly speaking needed
650 * but if it includes constraints it is to be assumed it expects them
653 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
654 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
656 rv = NAME_CONSTRAINTS_check(x, nc);
657 if (rv != X509_V_OK) {
659 ctx->error_depth = i;
660 ctx->current_cert = x;
661 if (!ctx->verify_cb(0, ctx))
670 static int check_trust(X509_STORE_CTX *ctx)
672 #ifdef OPENSSL_NO_CHAIN_VERIFY
677 int (*cb) (int xok, X509_STORE_CTX *xctx);
679 /* For now just check the last certificate in the chain */
680 i = sk_X509_num(ctx->chain) - 1;
681 x = sk_X509_value(ctx->chain, i);
682 ok = X509_check_trust(x, ctx->param->trust, 0);
683 if (ok == X509_TRUST_TRUSTED)
685 ctx->error_depth = i;
686 ctx->current_cert = x;
687 if (ok == X509_TRUST_REJECTED)
688 ctx->error = X509_V_ERR_CERT_REJECTED;
690 ctx->error = X509_V_ERR_CERT_UNTRUSTED;
696 static int check_revocation(X509_STORE_CTX *ctx)
699 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
701 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
702 last = sk_X509_num(ctx->chain) - 1;
704 /* If checking CRL paths this isn't the EE certificate */
709 for (i = 0; i <= last; i++) {
710 ctx->error_depth = i;
711 ok = check_cert(ctx);
718 static int check_cert(X509_STORE_CTX *ctx)
720 X509_CRL *crl = NULL, *dcrl = NULL;
723 unsigned int last_reasons;
724 cnum = ctx->error_depth;
725 x = sk_X509_value(ctx->chain, cnum);
726 ctx->current_cert = x;
727 ctx->current_issuer = NULL;
728 ctx->current_crl_score = 0;
729 ctx->current_reasons = 0;
730 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
731 last_reasons = ctx->current_reasons;
732 /* Try to retrieve relevant CRL */
734 ok = ctx->get_crl(ctx, &crl, x);
736 ok = get_crl_delta(ctx, &crl, &dcrl, x);
738 * If error looking up CRL, nothing we can do except notify callback
741 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
742 ok = ctx->verify_cb(0, ctx);
745 ctx->current_crl = crl;
746 ok = ctx->check_crl(ctx, crl);
751 ok = ctx->check_crl(ctx, dcrl);
754 ok = ctx->cert_crl(ctx, dcrl, x);
760 /* Don't look in full CRL if delta reason is removefromCRL */
762 ok = ctx->cert_crl(ctx, crl, x);
772 * If reasons not updated we wont get anywhere by another iteration,
775 if (last_reasons == ctx->current_reasons) {
776 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
777 ok = ctx->verify_cb(0, ctx);
785 ctx->current_crl = NULL;
790 /* Check CRL times against values in X509_STORE_CTX */
792 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
797 ctx->current_crl = crl;
798 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
799 ptime = &ctx->param->check_time;
803 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
807 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
808 if (!ctx->verify_cb(0, ctx))
815 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
816 if (!ctx->verify_cb(0, ctx))
820 if (X509_CRL_get_nextUpdate(crl)) {
821 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
826 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
827 if (!ctx->verify_cb(0, ctx))
830 /* Ignore expiry of base CRL is delta is valid */
831 if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
834 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
835 if (!ctx->verify_cb(0, ctx))
841 ctx->current_crl = NULL;
846 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
847 X509 **pissuer, int *pscore, unsigned int *preasons,
848 STACK_OF(X509_CRL) *crls)
850 int i, crl_score, best_score = *pscore;
851 unsigned int reasons, best_reasons = 0;
852 X509 *x = ctx->current_cert;
853 X509_CRL *crl, *best_crl = NULL;
854 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
856 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
857 crl = sk_X509_CRL_value(crls, i);
859 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
861 if (crl_score > best_score) {
863 best_crl_issuer = crl_issuer;
864 best_score = crl_score;
865 best_reasons = reasons;
871 X509_CRL_free(*pcrl);
873 *pissuer = best_crl_issuer;
874 *pscore = best_score;
875 *preasons = best_reasons;
876 CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
878 X509_CRL_free(*pdcrl);
881 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
884 if (best_score >= CRL_SCORE_VALID)
891 * Compare two CRL extensions for delta checking purposes. They should be
892 * both present or both absent. If both present all fields must be identical.
895 static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
897 ASN1_OCTET_STRING *exta, *extb;
899 i = X509_CRL_get_ext_by_NID(a, nid, -1);
901 /* Can't have multiple occurrences */
902 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
904 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
908 i = X509_CRL_get_ext_by_NID(b, nid, -1);
912 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
914 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
924 if (ASN1_OCTET_STRING_cmp(exta, extb))
930 /* See if a base and delta are compatible */
932 static int check_delta_base(X509_CRL *delta, X509_CRL *base)
934 /* Delta CRL must be a delta */
935 if (!delta->base_crl_number)
937 /* Base must have a CRL number */
938 if (!base->crl_number)
940 /* Issuer names must match */
941 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
943 /* AKID and IDP must match */
944 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
946 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
948 /* Delta CRL base number must not exceed Full CRL number. */
949 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
951 /* Delta CRL number must exceed full CRL number */
952 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
958 * For a given base CRL find a delta... maybe extend to delta scoring or
959 * retrieve a chain of deltas...
962 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
963 X509_CRL *base, STACK_OF(X509_CRL) *crls)
967 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
969 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
971 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
972 delta = sk_X509_CRL_value(crls, i);
973 if (check_delta_base(delta, base)) {
974 if (check_crl_time(ctx, delta, 0))
975 *pscore |= CRL_SCORE_TIME_DELTA;
976 CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
985 * For a given CRL return how suitable it is for the supplied certificate
986 * 'x'. The return value is a mask of several criteria. If the issuer is not
987 * the certificate issuer this is returned in *pissuer. The reasons mask is
988 * also used to determine if the CRL is suitable: if no new reasons the CRL
989 * is rejected, otherwise reasons is updated.
992 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
993 unsigned int *preasons, X509_CRL *crl, X509 *x)
997 unsigned int tmp_reasons = *preasons, crl_reasons;
999 /* First see if we can reject CRL straight away */
1001 /* Invalid IDP cannot be processed */
1002 if (crl->idp_flags & IDP_INVALID)
1004 /* Reason codes or indirect CRLs need extended CRL support */
1005 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
1006 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
1008 } else if (crl->idp_flags & IDP_REASONS) {
1009 /* If no new reasons reject */
1010 if (!(crl->idp_reasons & ~tmp_reasons))
1013 /* Don't process deltas at this stage */
1014 else if (crl->base_crl_number)
1016 /* If issuer name doesn't match certificate need indirect CRL */
1017 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
1018 if (!(crl->idp_flags & IDP_INDIRECT))
1021 crl_score |= CRL_SCORE_ISSUER_NAME;
1023 if (!(crl->flags & EXFLAG_CRITICAL))
1024 crl_score |= CRL_SCORE_NOCRITICAL;
1027 if (check_crl_time(ctx, crl, 0))
1028 crl_score |= CRL_SCORE_TIME;
1030 /* Check authority key ID and locate certificate issuer */
1031 crl_akid_check(ctx, crl, pissuer, &crl_score);
1033 /* If we can't locate certificate issuer at this point forget it */
1035 if (!(crl_score & CRL_SCORE_AKID))
1038 /* Check cert for matching CRL distribution points */
1040 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1041 /* If no new reasons reject */
1042 if (!(crl_reasons & ~tmp_reasons))
1044 tmp_reasons |= crl_reasons;
1045 crl_score |= CRL_SCORE_SCOPE;
1048 *preasons = tmp_reasons;
1054 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
1055 X509 **pissuer, int *pcrl_score)
1057 X509 *crl_issuer = NULL;
1058 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1059 int cidx = ctx->error_depth;
1062 if (cidx != sk_X509_num(ctx->chain) - 1)
1065 crl_issuer = sk_X509_value(ctx->chain, cidx);
1067 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1068 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1069 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
1070 *pissuer = crl_issuer;
1075 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1076 crl_issuer = sk_X509_value(ctx->chain, cidx);
1077 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1079 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1080 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
1081 *pissuer = crl_issuer;
1086 /* Anything else needs extended CRL support */
1088 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1092 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1093 * untrusted certificates.
1095 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1096 crl_issuer = sk_X509_value(ctx->untrusted, i);
1097 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1099 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1100 *pissuer = crl_issuer;
1101 *pcrl_score |= CRL_SCORE_AKID;
1108 * Check the path of a CRL issuer certificate. This creates a new
1109 * X509_STORE_CTX and populates it with most of the parameters from the
1110 * parent. This could be optimised somewhat since a lot of path checking will
1111 * be duplicated by the parent, but this will rarely be used in practice.
1114 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1116 X509_STORE_CTX crl_ctx;
1118 /* Don't allow recursive CRL path validation */
1121 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
1124 crl_ctx.crls = ctx->crls;
1125 /* Copy verify params across */
1126 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1128 crl_ctx.parent = ctx;
1129 crl_ctx.verify_cb = ctx->verify_cb;
1131 /* Verify CRL issuer */
1132 ret = X509_verify_cert(&crl_ctx);
1137 /* Check chain is acceptable */
1139 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1141 X509_STORE_CTX_cleanup(&crl_ctx);
1146 * RFC3280 says nothing about the relationship between CRL path and
1147 * certificate path, which could lead to situations where a certificate could
1148 * be revoked or validated by a CA not authorised to do so. RFC5280 is more
1149 * strict and states that the two paths must end in the same trust anchor,
1150 * though some discussions remain... until this is resolved we use the
1154 static int check_crl_chain(X509_STORE_CTX *ctx,
1155 STACK_OF(X509) *cert_path,
1156 STACK_OF(X509) *crl_path)
1158 X509 *cert_ta, *crl_ta;
1159 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1160 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1161 if (!X509_cmp(cert_ta, crl_ta))
1167 * Check for match between two dist point names: three separate cases.
1168 * 1. Both are relative names and compare X509_NAME types.
1169 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1170 * 3. Both are full names and compare two GENERAL_NAMES.
1171 * 4. One is NULL: automatic match.
1174 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1176 X509_NAME *nm = NULL;
1177 GENERAL_NAMES *gens = NULL;
1178 GENERAL_NAME *gena, *genb;
1185 /* Case 1: two X509_NAME */
1189 if (!X509_NAME_cmp(a->dpname, b->dpname))
1194 /* Case 2: set name and GENERAL_NAMES appropriately */
1196 gens = b->name.fullname;
1197 } else if (b->type == 1) {
1200 /* Case 2: set name and GENERAL_NAMES appropriately */
1201 gens = a->name.fullname;
1205 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1207 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1208 gena = sk_GENERAL_NAME_value(gens, i);
1209 if (gena->type != GEN_DIRNAME)
1211 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1217 /* Else case 3: two GENERAL_NAMES */
1219 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1220 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1221 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1222 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1223 if (!GENERAL_NAME_cmp(gena, genb))
1232 static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1235 X509_NAME *nm = X509_CRL_get_issuer(crl);
1236 /* If no CRLissuer return is successful iff don't need a match */
1238 return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
1239 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1240 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1241 if (gen->type != GEN_DIRNAME)
1243 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1249 /* Check CRLDP and IDP */
1251 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
1252 unsigned int *preasons)
1255 if (crl->idp_flags & IDP_ONLYATTR)
1257 if (x->ex_flags & EXFLAG_CA) {
1258 if (crl->idp_flags & IDP_ONLYUSER)
1261 if (crl->idp_flags & IDP_ONLYCA)
1264 *preasons = crl->idp_reasons;
1265 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1266 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1267 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1268 if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1269 *preasons &= dp->dp_reasons;
1274 if ((!crl->idp || !crl->idp->distpoint)
1275 && (crl_score & CRL_SCORE_ISSUER_NAME))
1281 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1282 * to find a delta CRL too
1285 static int get_crl_delta(X509_STORE_CTX *ctx,
1286 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1289 X509 *issuer = NULL;
1291 unsigned int reasons;
1292 X509_CRL *crl = NULL, *dcrl = NULL;
1293 STACK_OF(X509_CRL) *skcrl;
1294 X509_NAME *nm = X509_get_issuer_name(x);
1295 reasons = ctx->current_reasons;
1296 ok = get_crl_sk(ctx, &crl, &dcrl,
1297 &issuer, &crl_score, &reasons, ctx->crls);
1302 /* Lookup CRLs from store */
1304 skcrl = ctx->lookup_crls(ctx, nm);
1306 /* If no CRLs found and a near match from get_crl_sk use that */
1310 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1312 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1316 /* If we got any kind of CRL use it and return success */
1318 ctx->current_issuer = issuer;
1319 ctx->current_crl_score = crl_score;
1320 ctx->current_reasons = reasons;
1329 /* Check CRL validity */
1330 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1332 X509 *issuer = NULL;
1333 EVP_PKEY *ikey = NULL;
1334 int ok = 0, chnum, cnum;
1335 cnum = ctx->error_depth;
1336 chnum = sk_X509_num(ctx->chain) - 1;
1337 /* if we have an alternative CRL issuer cert use that */
1338 if (ctx->current_issuer)
1339 issuer = ctx->current_issuer;
1342 * Else find CRL issuer: if not last certificate then issuer is next
1343 * certificate in chain.
1345 else if (cnum < chnum)
1346 issuer = sk_X509_value(ctx->chain, cnum + 1);
1348 issuer = sk_X509_value(ctx->chain, chnum);
1349 /* If not self signed, can't check signature */
1350 if (!ctx->check_issued(ctx, issuer, issuer)) {
1351 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1352 ok = ctx->verify_cb(0, ctx);
1360 * Skip most tests for deltas because they have already been done
1362 if (!crl->base_crl_number) {
1363 /* Check for cRLSign bit if keyUsage present */
1364 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1365 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1366 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1367 ok = ctx->verify_cb(0, ctx);
1372 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1373 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1374 ok = ctx->verify_cb(0, ctx);
1379 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1380 if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
1381 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1382 ok = ctx->verify_cb(0, ctx);
1388 if (crl->idp_flags & IDP_INVALID) {
1389 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1390 ok = ctx->verify_cb(0, ctx);
1397 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1398 ok = check_crl_time(ctx, crl, 1);
1403 /* Attempt to get issuer certificate public key */
1404 ikey = X509_get_pubkey(issuer);
1407 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1408 ok = ctx->verify_cb(0, ctx);
1412 /* Verify CRL signature */
1413 if (X509_CRL_verify(crl, ikey) <= 0) {
1414 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1415 ok = ctx->verify_cb(0, ctx);
1425 EVP_PKEY_free(ikey);
1429 /* Check certificate against CRL */
1430 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1435 * The rules changed for this... previously if a CRL contained unhandled
1436 * critical extensions it could still be used to indicate a certificate
1437 * was revoked. This has since been changed since critical extension can
1438 * change the meaning of CRL entries.
1440 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
1441 && (crl->flags & EXFLAG_CRITICAL)) {
1442 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1443 ok = ctx->verify_cb(0, ctx);
1448 * Look for serial number of certificate in CRL If found make sure reason
1449 * is not removeFromCRL.
1451 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1452 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1454 ctx->error = X509_V_ERR_CERT_REVOKED;
1455 ok = ctx->verify_cb(0, ctx);
1463 static int check_policy(X509_STORE_CTX *ctx)
1468 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1469 ctx->param->policies, ctx->param->flags);
1471 X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
1474 /* Invalid or inconsistent extensions */
1477 * Locate certificates with bad extensions and notify callback.
1481 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1482 x = sk_X509_value(ctx->chain, i);
1483 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1485 ctx->current_cert = x;
1486 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1487 if (!ctx->verify_cb(0, ctx))
1493 ctx->current_cert = NULL;
1494 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1495 return ctx->verify_cb(0, ctx);
1498 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1499 ctx->current_cert = NULL;
1500 ctx->error = X509_V_OK;
1501 if (!ctx->verify_cb(2, ctx))
1508 static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
1513 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1514 ptime = &ctx->param->check_time;
1518 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1520 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1521 ctx->current_cert = x;
1522 if (!ctx->verify_cb(0, ctx))
1527 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1528 ctx->current_cert = x;
1529 if (!ctx->verify_cb(0, ctx))
1533 i = X509_cmp_time(X509_get_notAfter(x), ptime);
1535 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1536 ctx->current_cert = x;
1537 if (!ctx->verify_cb(0, ctx))
1542 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1543 ctx->current_cert = x;
1544 if (!ctx->verify_cb(0, ctx))
1551 static int internal_verify(X509_STORE_CTX *ctx)
1555 EVP_PKEY *pkey = NULL;
1556 int (*cb) (int xok, X509_STORE_CTX *xctx);
1558 cb = ctx->verify_cb;
1560 n = sk_X509_num(ctx->chain);
1561 ctx->error_depth = n - 1;
1563 xi = sk_X509_value(ctx->chain, n);
1565 if (ctx->check_issued(ctx, xi, xi))
1569 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1570 ctx->current_cert = xi;
1575 ctx->error_depth = n;
1576 xs = sk_X509_value(ctx->chain, n);
1580 /* ctx->error=0; not needed */
1582 ctx->error_depth = n;
1585 * Skip signature check for self signed certificates unless
1586 * explicitly asked for. It doesn't add any security and just wastes
1591 || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
1592 if ((pkey = X509_get_pubkey(xi)) == NULL) {
1593 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1594 ctx->current_cert = xi;
1595 ok = (*cb) (0, ctx);
1598 } else if (X509_verify(xs, pkey) <= 0) {
1599 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1600 ctx->current_cert = xs;
1601 ok = (*cb) (0, ctx);
1603 EVP_PKEY_free(pkey);
1607 EVP_PKEY_free(pkey);
1613 ok = check_cert_time(ctx, xs);
1617 /* The last error (if any) is still in the error value */
1618 ctx->current_issuer = xi;
1619 ctx->current_cert = xs;
1620 ok = (*cb) (1, ctx);
1627 xs = sk_X509_value(ctx->chain, n);
1635 int X509_cmp_current_time(const ASN1_TIME *ctm)
1637 return X509_cmp_time(ctm, NULL);
1640 int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1645 char buff1[24], buff2[24], *p;
1646 int i, j, remaining;
1649 remaining = ctm->length;
1650 str = (char *)ctm->data;
1652 * Note that the following (historical) code allows much more slack in the
1653 * time format than RFC5280. In RFC5280, the representation is fixed:
1654 * UTCTime: YYMMDDHHMMSSZ
1655 * GeneralizedTime: YYYYMMDDHHMMSSZ
1657 if (ctm->type == V_ASN1_UTCTIME) {
1658 /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
1659 int min_length = sizeof("YYMMDDHHMMZ") - 1;
1660 int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
1661 if (remaining < min_length || remaining > max_length)
1668 /* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
1669 int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
1670 int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
1671 if (remaining < min_length || remaining > max_length)
1679 if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
1690 * Skip any (up to three) fractional seconds...
1691 * TODO(emilia): in RFC5280, fractional seconds are forbidden.
1692 * Can we just kill them altogether?
1694 if (remaining && *str == '.') {
1697 for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
1698 if (*str < '0' || *str > '9')
1707 /* We now need either a terminating 'Z' or an offset. */
1716 if ((*str != '+') && (*str != '-'))
1718 /* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
1721 if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
1722 str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
1724 offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
1725 offset += (str[3] - '0') * 10 + (str[4] - '0');
1729 atm.type = ctm->type;
1731 atm.length = sizeof(buff2);
1732 atm.data = (unsigned char *)buff2;
1734 if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
1737 if (ctm->type == V_ASN1_UTCTIME) {
1738 i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
1740 i += 100; /* cf. RFC 2459 */
1741 j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
1750 i = strcmp(buff1, buff2);
1751 if (i == 0) /* wait a second then return younger :-) */
1757 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
1759 return X509_time_adj(s, adj, NULL);
1762 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
1764 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1767 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
1768 int offset_day, long offset_sec, time_t *in_tm)
1777 if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
1778 if (s->type == V_ASN1_UTCTIME)
1779 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
1780 if (s->type == V_ASN1_GENERALIZEDTIME)
1781 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
1783 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1786 int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1788 EVP_PKEY *ktmp = NULL, *ktmp2;
1791 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1794 for (i = 0; i < sk_X509_num(chain); i++) {
1795 ktmp = X509_get_pubkey(sk_X509_value(chain, i));
1797 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1798 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1801 if (!EVP_PKEY_missing_parameters(ktmp))
1804 EVP_PKEY_free(ktmp);
1809 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1810 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1814 /* first, populate the other certs */
1815 for (j = i - 1; j >= 0; j--) {
1816 ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
1817 EVP_PKEY_copy_parameters(ktmp2, ktmp);
1818 EVP_PKEY_free(ktmp2);
1822 EVP_PKEY_copy_parameters(pkey, ktmp);
1823 EVP_PKEY_free(ktmp);
1827 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
1828 CRYPTO_EX_new *new_func,
1829 CRYPTO_EX_dup *dup_func,
1830 CRYPTO_EX_free *free_func)
1833 * This function is (usually) called only once, by
1834 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
1836 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
1837 new_func, dup_func, free_func);
1840 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
1842 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1845 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
1847 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1850 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
1855 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
1860 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
1862 return ctx->error_depth;
1865 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
1867 return ctx->current_cert;
1870 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
1875 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
1879 STACK_OF(X509) *chain;
1880 if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain)))
1882 for (i = 0; i < sk_X509_num(chain); i++) {
1883 x = sk_X509_value(chain, i);
1884 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1889 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
1891 return ctx->current_issuer;
1894 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
1896 return ctx->current_crl;
1899 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
1904 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
1909 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1911 ctx->untrusted = sk;
1914 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
1919 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
1921 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
1924 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
1926 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
1930 * This function is used to set the X509_STORE_CTX purpose and trust values.
1931 * This is intended to be used when another structure has its own trust and
1932 * purpose values which (if set) will be inherited by the ctx. If they aren't
1933 * set then we will usually have a default purpose in mind which should then
1934 * be used to set the trust value. An example of this is SSL use: an SSL
1935 * structure will have its own purpose and trust settings which the
1936 * application can set: if they aren't set then we use the default of SSL
1940 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
1941 int purpose, int trust)
1944 /* If purpose not set use default */
1946 purpose = def_purpose;
1947 /* If we have a purpose then check it is valid */
1950 idx = X509_PURPOSE_get_by_id(purpose);
1952 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1953 X509_R_UNKNOWN_PURPOSE_ID);
1956 ptmp = X509_PURPOSE_get0(idx);
1957 if (ptmp->trust == X509_TRUST_DEFAULT) {
1958 idx = X509_PURPOSE_get_by_id(def_purpose);
1960 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1961 X509_R_UNKNOWN_PURPOSE_ID);
1964 ptmp = X509_PURPOSE_get0(idx);
1966 /* If trust not set then get from purpose default */
1968 trust = ptmp->trust;
1971 idx = X509_TRUST_get_by_id(trust);
1973 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1974 X509_R_UNKNOWN_TRUST_ID);
1979 if (purpose && !ctx->param->purpose)
1980 ctx->param->purpose = purpose;
1981 if (trust && !ctx->param->trust)
1982 ctx->param->trust = trust;
1986 X509_STORE_CTX *X509_STORE_CTX_new(void)
1988 X509_STORE_CTX *ctx;
1989 ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
1991 X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
1994 memset(ctx, 0, sizeof(X509_STORE_CTX));
1998 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
2002 X509_STORE_CTX_cleanup(ctx);
2006 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
2007 STACK_OF(X509) *chain)
2011 ctx->current_method = 0;
2013 ctx->untrusted = chain;
2015 ctx->last_untrusted = 0;
2016 ctx->other_ctx = NULL;
2020 ctx->explicit_policy = 0;
2021 ctx->error_depth = 0;
2022 ctx->current_cert = NULL;
2023 ctx->current_issuer = NULL;
2024 ctx->current_crl = NULL;
2025 ctx->current_crl_score = 0;
2026 ctx->current_reasons = 0;
2030 ctx->param = X509_VERIFY_PARAM_new();
2033 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2038 * Inherit callbacks and flags from X509_STORE if not set use defaults.
2042 ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2044 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
2047 ctx->verify_cb = store->verify_cb;
2048 ctx->cleanup = store->cleanup;
2053 ret = X509_VERIFY_PARAM_inherit(ctx->param,
2054 X509_VERIFY_PARAM_lookup("default"));
2057 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2061 if (store && store->check_issued)
2062 ctx->check_issued = store->check_issued;
2064 ctx->check_issued = check_issued;
2066 if (store && store->get_issuer)
2067 ctx->get_issuer = store->get_issuer;
2069 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2071 if (store && store->verify_cb)
2072 ctx->verify_cb = store->verify_cb;
2074 ctx->verify_cb = null_callback;
2076 if (store && store->verify)
2077 ctx->verify = store->verify;
2079 ctx->verify = internal_verify;
2081 if (store && store->check_revocation)
2082 ctx->check_revocation = store->check_revocation;
2084 ctx->check_revocation = check_revocation;
2086 if (store && store->get_crl)
2087 ctx->get_crl = store->get_crl;
2089 ctx->get_crl = NULL;
2091 if (store && store->check_crl)
2092 ctx->check_crl = store->check_crl;
2094 ctx->check_crl = check_crl;
2096 if (store && store->cert_crl)
2097 ctx->cert_crl = store->cert_crl;
2099 ctx->cert_crl = cert_crl;
2101 if (store && store->lookup_certs)
2102 ctx->lookup_certs = store->lookup_certs;
2104 ctx->lookup_certs = X509_STORE_get1_certs;
2106 if (store && store->lookup_crls)
2107 ctx->lookup_crls = store->lookup_crls;
2109 ctx->lookup_crls = X509_STORE_get1_crls;
2111 ctx->check_policy = check_policy;
2114 * This memset() can't make any sense anyway, so it's removed. As
2115 * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
2116 * corresponding "new" here and remove this bogus initialisation.
2118 /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
2119 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2122 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2129 * Set alternative lookup method: just a STACK of trusted certificates. This
2130 * avoids X509_STORE nastiness where it isn't needed.
2133 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2135 ctx->other_ctx = sk;
2136 ctx->get_issuer = get_issuer_sk;
2139 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2143 if (ctx->param != NULL) {
2144 if (ctx->parent == NULL)
2145 X509_VERIFY_PARAM_free(ctx->param);
2148 if (ctx->tree != NULL) {
2149 X509_policy_tree_free(ctx->tree);
2152 if (ctx->chain != NULL) {
2153 sk_X509_pop_free(ctx->chain, X509_free);
2156 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
2157 memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
2160 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2162 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2165 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2167 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2170 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
2173 X509_VERIFY_PARAM_set_time(ctx->param, t);
2176 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2177 int (*verify_cb) (int, X509_STORE_CTX *))
2179 ctx->verify_cb = verify_cb;
2182 X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2187 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2189 return ctx->explicit_policy;
2192 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2194 const X509_VERIFY_PARAM *param;
2195 param = X509_VERIFY_PARAM_lookup(name);
2198 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2201 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2206 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2209 X509_VERIFY_PARAM_free(ctx->param);
2213 IMPLEMENT_STACK_OF(X509)
2215 IMPLEMENT_ASN1_SET_OF(X509)
2217 IMPLEMENT_STACK_OF(X509_NAME)
2219 IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
2221 IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)