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);
169 * first we make sure the chain we are going to build is present and that
170 * the first entry is in place
172 if (ctx->chain == NULL) {
173 if (((ctx->chain = sk_X509_new_null()) == NULL) ||
174 (!sk_X509_push(ctx->chain, ctx->cert))) {
175 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
178 CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
179 ctx->last_untrusted = 1;
182 /* We use a temporary STACK so we can chop and hack at it */
183 if (ctx->untrusted != NULL
184 && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
185 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
189 num = sk_X509_num(ctx->chain);
190 x = sk_X509_value(ctx->chain, num - 1);
191 depth = param->depth;
194 /* If we have enough, we break */
196 break; /* FIXME: If this happens, we should take
197 * note of it and, if appropriate, use the
198 * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
201 /* If we are self signed, we break */
202 if (ctx->check_issued(ctx, x, x))
205 /* If we were passed a cert chain, use it first */
206 if (ctx->untrusted != NULL) {
207 xtmp = find_issuer(ctx, sktmp, x);
209 if (!sk_X509_push(ctx->chain, xtmp)) {
210 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
213 CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
214 (void)sk_X509_delete_ptr(sktmp, xtmp);
215 ctx->last_untrusted++;
219 * reparse the full chain for the next one
227 /* Remember how many untrusted certs we have */
230 * at this point, chain should contain a list of untrusted certificates.
231 * We now need to add at least one trusted one, if possible, otherwise we
237 * Examine last certificate in chain and see if it is self signed.
239 i = sk_X509_num(ctx->chain);
240 x = sk_X509_value(ctx->chain, i - 1);
241 if (ctx->check_issued(ctx, x, x)) {
242 /* we have a self signed certificate */
243 if (sk_X509_num(ctx->chain) == 1) {
245 * We have a single self signed certificate: see if we can
246 * find it in the store. We must have an exact match to avoid
247 * possible impersonation.
249 ok = ctx->get_issuer(&xtmp, ctx, x);
250 if ((ok <= 0) || X509_cmp(x, xtmp)) {
251 ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
252 ctx->current_cert = x;
253 ctx->error_depth = i - 1;
262 * We have a match: replace certificate with store
263 * version so we get any trust settings.
267 (void)sk_X509_set(ctx->chain, i - 1, x);
268 ctx->last_untrusted = 0;
272 * extract and save self signed certificate for later use
274 chain_ss = sk_X509_pop(ctx->chain);
275 ctx->last_untrusted--;
278 x = sk_X509_value(ctx->chain, num - 1);
281 /* We now lookup certs from the certificate store */
283 /* If we have enough, we break */
286 /* If we are self signed, we break */
287 if (ctx->check_issued(ctx, x, x))
289 ok = ctx->get_issuer(&xtmp, ctx, x);
295 if (!sk_X509_push(ctx->chain, x)) {
297 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
304 * If we haven't got a least one certificate from our store then check
305 * if there is an alternative chain that could be used. We only do this
306 * if the user hasn't switched off alternate chain checking
309 if (j == ctx->last_untrusted &&
310 !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
312 xtmp2 = sk_X509_value(ctx->chain, j - 1);
313 ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
316 /* Check if we found an alternate chain */
319 * Free up the found cert we'll add it again later
324 * Dump all the certs above this point - we've found an
328 xtmp = sk_X509_pop(ctx->chain);
331 ctx->last_untrusted--;
340 /* Is last certificate looked up self signed? */
341 if (!ctx->check_issued(ctx, x, x)) {
342 if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
343 if (ctx->last_untrusted >= num)
344 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
346 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
347 ctx->current_cert = x;
350 sk_X509_push(ctx->chain, chain_ss);
352 ctx->last_untrusted = num;
353 ctx->current_cert = chain_ss;
354 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
358 ctx->error_depth = num - 1;
365 /* We have the chain complete: now we need to check its purpose */
366 ok = check_chain_extensions(ctx);
371 /* Check name constraints */
373 ok = check_name_constraints(ctx);
378 /* The chain extensions are OK: check trust */
380 if (param->trust > 0)
381 ok = check_trust(ctx);
386 /* We may as well copy down any DSA parameters that are required */
387 X509_get_pubkey_parameters(NULL, ctx->chain);
390 * Check revocation status: we do this after copying parameters because
391 * they may be needed for CRL signature verification.
394 ok = ctx->check_revocation(ctx);
398 /* At this point, we have a chain and need to verify it */
399 if (ctx->verify != NULL)
400 ok = ctx->verify(ctx);
402 ok = internal_verify(ctx);
406 #ifndef OPENSSL_NO_RFC3779
407 /* RFC 3779 path validation, now that CRL check has been done */
408 ok = v3_asid_validate_path(ctx);
411 ok = v3_addr_validate_path(ctx);
416 /* If we get this far evaluate policies */
417 if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
418 ok = ctx->check_policy(ctx);
423 X509_get_pubkey_parameters(NULL, ctx->chain);
427 if (chain_ss != NULL)
433 * Given a STACK_OF(X509) find the issuer of cert (if any)
436 static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
440 for (i = 0; i < sk_X509_num(sk); i++) {
441 issuer = sk_X509_value(sk, i);
442 if (ctx->check_issued(ctx, x, issuer))
448 /* Given a possible certificate and issuer check them */
450 static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
453 ret = X509_check_issued(issuer, x);
454 if (ret == X509_V_OK)
456 /* If we haven't asked for issuer errors don't set ctx */
457 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
461 ctx->current_cert = x;
462 ctx->current_issuer = issuer;
463 return ctx->verify_cb(0, ctx);
467 /* Alternative lookup method: look from a STACK stored in other_ctx */
469 static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
471 *issuer = find_issuer(ctx, ctx->other_ctx, x);
473 CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
480 * Check a certificate chains extensions for consistency with the supplied
484 static int check_chain_extensions(X509_STORE_CTX *ctx)
486 #ifdef OPENSSL_NO_CHAIN_VERIFY
489 int i, ok = 0, must_be_ca, plen = 0;
491 int (*cb) (int xok, X509_STORE_CTX *xctx);
492 int proxy_path_length = 0;
494 int allow_proxy_certs;
498 * must_be_ca can have 1 of 3 values:
499 * -1: we accept both CA and non-CA certificates, to allow direct
500 * use of self-signed certificates (which are marked as CA).
501 * 0: we only accept non-CA certificates. This is currently not
502 * used, but the possibility is present for future extensions.
503 * 1: we only accept CA certificates. This is currently used for
504 * all certificates in the chain except the leaf certificate.
508 /* CRL path validation */
510 allow_proxy_certs = 0;
511 purpose = X509_PURPOSE_CRL_SIGN;
514 ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
516 * A hack to keep people who don't want to modify their software
519 if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
520 allow_proxy_certs = 1;
521 purpose = ctx->param->purpose;
524 /* Check all untrusted certificates */
525 for (i = 0; i < ctx->last_untrusted; i++) {
527 x = sk_X509_value(ctx->chain, i);
528 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
529 && (x->ex_flags & EXFLAG_CRITICAL)) {
530 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
531 ctx->error_depth = i;
532 ctx->current_cert = x;
537 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
538 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
539 ctx->error_depth = i;
540 ctx->current_cert = x;
545 ret = X509_check_ca(x);
546 switch (must_be_ca) {
548 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
549 && (ret != 1) && (ret != 0)) {
551 ctx->error = X509_V_ERR_INVALID_CA;
558 ctx->error = X509_V_ERR_INVALID_NON_CA;
564 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
567 ctx->error = X509_V_ERR_INVALID_CA;
573 ctx->error_depth = i;
574 ctx->current_cert = x;
579 if (ctx->param->purpose > 0) {
580 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
582 || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
584 ctx->error = X509_V_ERR_INVALID_PURPOSE;
585 ctx->error_depth = i;
586 ctx->current_cert = x;
592 /* Check pathlen if not self issued */
593 if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
594 && (x->ex_pathlen != -1)
595 && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
596 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
597 ctx->error_depth = i;
598 ctx->current_cert = x;
603 /* Increment path length if not self issued */
604 if (!(x->ex_flags & EXFLAG_SI))
607 * If this certificate is a proxy certificate, the next certificate
608 * must be another proxy certificate or a EE certificate. If not,
609 * the next certificate must be a CA certificate.
611 if (x->ex_flags & EXFLAG_PROXY) {
612 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
613 ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
614 ctx->error_depth = i;
615 ctx->current_cert = x;
631 static int check_name_constraints(X509_STORE_CTX *ctx)
635 /* Check name constraints for all certificates */
636 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
637 x = sk_X509_value(ctx->chain, i);
638 /* Ignore self issued certs unless last in chain */
639 if (i && (x->ex_flags & EXFLAG_SI))
642 * Check against constraints for all certificates higher in chain
643 * including trust anchor. Trust anchor not strictly speaking needed
644 * but if it includes constraints it is to be assumed it expects them
647 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
648 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
650 rv = NAME_CONSTRAINTS_check(x, nc);
651 if (rv != X509_V_OK) {
653 ctx->error_depth = i;
654 ctx->current_cert = x;
655 if (!ctx->verify_cb(0, ctx))
664 static int check_trust(X509_STORE_CTX *ctx)
666 #ifdef OPENSSL_NO_CHAIN_VERIFY
671 int (*cb) (int xok, X509_STORE_CTX *xctx);
673 /* For now just check the last certificate in the chain */
674 i = sk_X509_num(ctx->chain) - 1;
675 x = sk_X509_value(ctx->chain, i);
676 ok = X509_check_trust(x, ctx->param->trust, 0);
677 if (ok == X509_TRUST_TRUSTED)
679 ctx->error_depth = i;
680 ctx->current_cert = x;
681 if (ok == X509_TRUST_REJECTED)
682 ctx->error = X509_V_ERR_CERT_REJECTED;
684 ctx->error = X509_V_ERR_CERT_UNTRUSTED;
690 static int check_revocation(X509_STORE_CTX *ctx)
693 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
695 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
696 last = sk_X509_num(ctx->chain) - 1;
698 /* If checking CRL paths this isn't the EE certificate */
703 for (i = 0; i <= last; i++) {
704 ctx->error_depth = i;
705 ok = check_cert(ctx);
712 static int check_cert(X509_STORE_CTX *ctx)
714 X509_CRL *crl = NULL, *dcrl = NULL;
717 unsigned int last_reasons;
718 cnum = ctx->error_depth;
719 x = sk_X509_value(ctx->chain, cnum);
720 ctx->current_cert = x;
721 ctx->current_issuer = NULL;
722 ctx->current_crl_score = 0;
723 ctx->current_reasons = 0;
724 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
725 last_reasons = ctx->current_reasons;
726 /* Try to retrieve relevant CRL */
728 ok = ctx->get_crl(ctx, &crl, x);
730 ok = get_crl_delta(ctx, &crl, &dcrl, x);
732 * If error looking up CRL, nothing we can do except notify callback
735 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
736 ok = ctx->verify_cb(0, ctx);
739 ctx->current_crl = crl;
740 ok = ctx->check_crl(ctx, crl);
745 ok = ctx->check_crl(ctx, dcrl);
748 ok = ctx->cert_crl(ctx, dcrl, x);
754 /* Don't look in full CRL if delta reason is removefromCRL */
756 ok = ctx->cert_crl(ctx, crl, x);
766 * If reasons not updated we wont get anywhere by another iteration,
769 if (last_reasons == ctx->current_reasons) {
770 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
771 ok = ctx->verify_cb(0, ctx);
779 ctx->current_crl = NULL;
784 /* Check CRL times against values in X509_STORE_CTX */
786 static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
791 ctx->current_crl = crl;
792 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
793 ptime = &ctx->param->check_time;
797 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
801 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
802 if (!ctx->verify_cb(0, ctx))
809 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
810 if (!ctx->verify_cb(0, ctx))
814 if (X509_CRL_get_nextUpdate(crl)) {
815 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
820 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
821 if (!ctx->verify_cb(0, ctx))
824 /* Ignore expiry of base CRL is delta is valid */
825 if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
828 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
829 if (!ctx->verify_cb(0, ctx))
835 ctx->current_crl = NULL;
840 static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
841 X509 **pissuer, int *pscore, unsigned int *preasons,
842 STACK_OF(X509_CRL) *crls)
844 int i, crl_score, best_score = *pscore;
845 unsigned int reasons, best_reasons = 0;
846 X509 *x = ctx->current_cert;
847 X509_CRL *crl, *best_crl = NULL;
848 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
850 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
851 crl = sk_X509_CRL_value(crls, i);
853 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
855 if (crl_score > best_score) {
857 best_crl_issuer = crl_issuer;
858 best_score = crl_score;
859 best_reasons = reasons;
865 X509_CRL_free(*pcrl);
867 *pissuer = best_crl_issuer;
868 *pscore = best_score;
869 *preasons = best_reasons;
870 CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
872 X509_CRL_free(*pdcrl);
875 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
878 if (best_score >= CRL_SCORE_VALID)
885 * Compare two CRL extensions for delta checking purposes. They should be
886 * both present or both absent. If both present all fields must be identical.
889 static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
891 ASN1_OCTET_STRING *exta, *extb;
893 i = X509_CRL_get_ext_by_NID(a, nid, -1);
895 /* Can't have multiple occurrences */
896 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
898 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
902 i = X509_CRL_get_ext_by_NID(b, nid, -1);
906 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
908 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
918 if (ASN1_OCTET_STRING_cmp(exta, extb))
924 /* See if a base and delta are compatible */
926 static int check_delta_base(X509_CRL *delta, X509_CRL *base)
928 /* Delta CRL must be a delta */
929 if (!delta->base_crl_number)
931 /* Base must have a CRL number */
932 if (!base->crl_number)
934 /* Issuer names must match */
935 if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
937 /* AKID and IDP must match */
938 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
940 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
942 /* Delta CRL base number must not exceed Full CRL number. */
943 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
945 /* Delta CRL number must exceed full CRL number */
946 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
952 * For a given base CRL find a delta... maybe extend to delta scoring or
953 * retrieve a chain of deltas...
956 static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
957 X509_CRL *base, STACK_OF(X509_CRL) *crls)
961 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
963 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
965 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
966 delta = sk_X509_CRL_value(crls, i);
967 if (check_delta_base(delta, base)) {
968 if (check_crl_time(ctx, delta, 0))
969 *pscore |= CRL_SCORE_TIME_DELTA;
970 CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
979 * For a given CRL return how suitable it is for the supplied certificate
980 * 'x'. The return value is a mask of several criteria. If the issuer is not
981 * the certificate issuer this is returned in *pissuer. The reasons mask is
982 * also used to determine if the CRL is suitable: if no new reasons the CRL
983 * is rejected, otherwise reasons is updated.
986 static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
987 unsigned int *preasons, X509_CRL *crl, X509 *x)
991 unsigned int tmp_reasons = *preasons, crl_reasons;
993 /* First see if we can reject CRL straight away */
995 /* Invalid IDP cannot be processed */
996 if (crl->idp_flags & IDP_INVALID)
998 /* Reason codes or indirect CRLs need extended CRL support */
999 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
1000 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
1002 } else if (crl->idp_flags & IDP_REASONS) {
1003 /* If no new reasons reject */
1004 if (!(crl->idp_reasons & ~tmp_reasons))
1007 /* Don't process deltas at this stage */
1008 else if (crl->base_crl_number)
1010 /* If issuer name doesn't match certificate need indirect CRL */
1011 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
1012 if (!(crl->idp_flags & IDP_INDIRECT))
1015 crl_score |= CRL_SCORE_ISSUER_NAME;
1017 if (!(crl->flags & EXFLAG_CRITICAL))
1018 crl_score |= CRL_SCORE_NOCRITICAL;
1021 if (check_crl_time(ctx, crl, 0))
1022 crl_score |= CRL_SCORE_TIME;
1024 /* Check authority key ID and locate certificate issuer */
1025 crl_akid_check(ctx, crl, pissuer, &crl_score);
1027 /* If we can't locate certificate issuer at this point forget it */
1029 if (!(crl_score & CRL_SCORE_AKID))
1032 /* Check cert for matching CRL distribution points */
1034 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1035 /* If no new reasons reject */
1036 if (!(crl_reasons & ~tmp_reasons))
1038 tmp_reasons |= crl_reasons;
1039 crl_score |= CRL_SCORE_SCOPE;
1042 *preasons = tmp_reasons;
1048 static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
1049 X509 **pissuer, int *pcrl_score)
1051 X509 *crl_issuer = NULL;
1052 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1053 int cidx = ctx->error_depth;
1056 if (cidx != sk_X509_num(ctx->chain) - 1)
1059 crl_issuer = sk_X509_value(ctx->chain, cidx);
1061 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1062 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1063 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
1064 *pissuer = crl_issuer;
1069 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1070 crl_issuer = sk_X509_value(ctx->chain, cidx);
1071 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1073 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1074 *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
1075 *pissuer = crl_issuer;
1080 /* Anything else needs extended CRL support */
1082 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1086 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1087 * untrusted certificates.
1089 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1090 crl_issuer = sk_X509_value(ctx->untrusted, i);
1091 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1093 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1094 *pissuer = crl_issuer;
1095 *pcrl_score |= CRL_SCORE_AKID;
1102 * Check the path of a CRL issuer certificate. This creates a new
1103 * X509_STORE_CTX and populates it with most of the parameters from the
1104 * parent. This could be optimised somewhat since a lot of path checking will
1105 * be duplicated by the parent, but this will rarely be used in practice.
1108 static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1110 X509_STORE_CTX crl_ctx;
1112 /* Don't allow recursive CRL path validation */
1115 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
1118 crl_ctx.crls = ctx->crls;
1119 /* Copy verify params across */
1120 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1122 crl_ctx.parent = ctx;
1123 crl_ctx.verify_cb = ctx->verify_cb;
1125 /* Verify CRL issuer */
1126 ret = X509_verify_cert(&crl_ctx);
1131 /* Check chain is acceptable */
1133 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1135 X509_STORE_CTX_cleanup(&crl_ctx);
1140 * RFC3280 says nothing about the relationship between CRL path and
1141 * certificate path, which could lead to situations where a certificate could
1142 * be revoked or validated by a CA not authorised to do so. RFC5280 is more
1143 * strict and states that the two paths must end in the same trust anchor,
1144 * though some discussions remain... until this is resolved we use the
1148 static int check_crl_chain(X509_STORE_CTX *ctx,
1149 STACK_OF(X509) *cert_path,
1150 STACK_OF(X509) *crl_path)
1152 X509 *cert_ta, *crl_ta;
1153 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1154 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1155 if (!X509_cmp(cert_ta, crl_ta))
1161 * Check for match between two dist point names: three separate cases.
1162 * 1. Both are relative names and compare X509_NAME types.
1163 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1164 * 3. Both are full names and compare two GENERAL_NAMES.
1165 * 4. One is NULL: automatic match.
1168 static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1170 X509_NAME *nm = NULL;
1171 GENERAL_NAMES *gens = NULL;
1172 GENERAL_NAME *gena, *genb;
1179 /* Case 1: two X509_NAME */
1183 if (!X509_NAME_cmp(a->dpname, b->dpname))
1188 /* Case 2: set name and GENERAL_NAMES appropriately */
1190 gens = b->name.fullname;
1191 } else if (b->type == 1) {
1194 /* Case 2: set name and GENERAL_NAMES appropriately */
1195 gens = a->name.fullname;
1199 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1201 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1202 gena = sk_GENERAL_NAME_value(gens, i);
1203 if (gena->type != GEN_DIRNAME)
1205 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1211 /* Else case 3: two GENERAL_NAMES */
1213 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1214 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1215 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1216 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1217 if (!GENERAL_NAME_cmp(gena, genb))
1226 static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1229 X509_NAME *nm = X509_CRL_get_issuer(crl);
1230 /* If no CRLissuer return is successful iff don't need a match */
1232 return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
1233 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1234 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1235 if (gen->type != GEN_DIRNAME)
1237 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1243 /* Check CRLDP and IDP */
1245 static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
1246 unsigned int *preasons)
1249 if (crl->idp_flags & IDP_ONLYATTR)
1251 if (x->ex_flags & EXFLAG_CA) {
1252 if (crl->idp_flags & IDP_ONLYUSER)
1255 if (crl->idp_flags & IDP_ONLYCA)
1258 *preasons = crl->idp_reasons;
1259 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1260 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1261 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1262 if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1263 *preasons &= dp->dp_reasons;
1268 if ((!crl->idp || !crl->idp->distpoint)
1269 && (crl_score & CRL_SCORE_ISSUER_NAME))
1275 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1276 * to find a delta CRL too
1279 static int get_crl_delta(X509_STORE_CTX *ctx,
1280 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1283 X509 *issuer = NULL;
1285 unsigned int reasons;
1286 X509_CRL *crl = NULL, *dcrl = NULL;
1287 STACK_OF(X509_CRL) *skcrl;
1288 X509_NAME *nm = X509_get_issuer_name(x);
1289 reasons = ctx->current_reasons;
1290 ok = get_crl_sk(ctx, &crl, &dcrl,
1291 &issuer, &crl_score, &reasons, ctx->crls);
1296 /* Lookup CRLs from store */
1298 skcrl = ctx->lookup_crls(ctx, nm);
1300 /* If no CRLs found and a near match from get_crl_sk use that */
1304 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1306 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1310 /* If we got any kind of CRL use it and return success */
1312 ctx->current_issuer = issuer;
1313 ctx->current_crl_score = crl_score;
1314 ctx->current_reasons = reasons;
1323 /* Check CRL validity */
1324 static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1326 X509 *issuer = NULL;
1327 EVP_PKEY *ikey = NULL;
1328 int ok = 0, chnum, cnum;
1329 cnum = ctx->error_depth;
1330 chnum = sk_X509_num(ctx->chain) - 1;
1331 /* if we have an alternative CRL issuer cert use that */
1332 if (ctx->current_issuer)
1333 issuer = ctx->current_issuer;
1336 * Else find CRL issuer: if not last certificate then issuer is next
1337 * certificate in chain.
1339 else if (cnum < chnum)
1340 issuer = sk_X509_value(ctx->chain, cnum + 1);
1342 issuer = sk_X509_value(ctx->chain, chnum);
1343 /* If not self signed, can't check signature */
1344 if (!ctx->check_issued(ctx, issuer, issuer)) {
1345 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1346 ok = ctx->verify_cb(0, ctx);
1354 * Skip most tests for deltas because they have already been done
1356 if (!crl->base_crl_number) {
1357 /* Check for cRLSign bit if keyUsage present */
1358 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1359 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1360 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1361 ok = ctx->verify_cb(0, ctx);
1366 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1367 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1368 ok = ctx->verify_cb(0, ctx);
1373 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1374 if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
1375 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1376 ok = ctx->verify_cb(0, ctx);
1382 if (crl->idp_flags & IDP_INVALID) {
1383 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1384 ok = ctx->verify_cb(0, ctx);
1391 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1392 ok = check_crl_time(ctx, crl, 1);
1397 /* Attempt to get issuer certificate public key */
1398 ikey = X509_get_pubkey(issuer);
1401 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1402 ok = ctx->verify_cb(0, ctx);
1406 /* Verify CRL signature */
1407 if (X509_CRL_verify(crl, ikey) <= 0) {
1408 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1409 ok = ctx->verify_cb(0, ctx);
1419 EVP_PKEY_free(ikey);
1423 /* Check certificate against CRL */
1424 static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1429 * The rules changed for this... previously if a CRL contained unhandled
1430 * critical extensions it could still be used to indicate a certificate
1431 * was revoked. This has since been changed since critical extension can
1432 * change the meaning of CRL entries.
1434 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
1435 && (crl->flags & EXFLAG_CRITICAL)) {
1436 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1437 ok = ctx->verify_cb(0, ctx);
1442 * Look for serial number of certificate in CRL If found make sure reason
1443 * is not removeFromCRL.
1445 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1446 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1448 ctx->error = X509_V_ERR_CERT_REVOKED;
1449 ok = ctx->verify_cb(0, ctx);
1457 static int check_policy(X509_STORE_CTX *ctx)
1462 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1463 ctx->param->policies, ctx->param->flags);
1465 X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
1468 /* Invalid or inconsistent extensions */
1471 * Locate certificates with bad extensions and notify callback.
1475 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1476 x = sk_X509_value(ctx->chain, i);
1477 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1479 ctx->current_cert = x;
1480 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1481 if (!ctx->verify_cb(0, ctx))
1487 ctx->current_cert = NULL;
1488 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1489 return ctx->verify_cb(0, ctx);
1492 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1493 ctx->current_cert = NULL;
1494 ctx->error = X509_V_OK;
1495 if (!ctx->verify_cb(2, ctx))
1502 static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
1507 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1508 ptime = &ctx->param->check_time;
1512 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1514 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1515 ctx->current_cert = x;
1516 if (!ctx->verify_cb(0, ctx))
1521 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1522 ctx->current_cert = x;
1523 if (!ctx->verify_cb(0, ctx))
1527 i = X509_cmp_time(X509_get_notAfter(x), ptime);
1529 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1530 ctx->current_cert = x;
1531 if (!ctx->verify_cb(0, ctx))
1536 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1537 ctx->current_cert = x;
1538 if (!ctx->verify_cb(0, ctx))
1545 static int internal_verify(X509_STORE_CTX *ctx)
1549 EVP_PKEY *pkey = NULL;
1550 int (*cb) (int xok, X509_STORE_CTX *xctx);
1552 cb = ctx->verify_cb;
1554 n = sk_X509_num(ctx->chain);
1555 ctx->error_depth = n - 1;
1557 xi = sk_X509_value(ctx->chain, n);
1559 if (ctx->check_issued(ctx, xi, xi))
1563 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1564 ctx->current_cert = xi;
1569 ctx->error_depth = n;
1570 xs = sk_X509_value(ctx->chain, n);
1574 /* ctx->error=0; not needed */
1576 ctx->error_depth = n;
1579 * Skip signature check for self signed certificates unless
1580 * explicitly asked for. It doesn't add any security and just wastes
1585 || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
1586 if ((pkey = X509_get_pubkey(xi)) == NULL) {
1587 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1588 ctx->current_cert = xi;
1589 ok = (*cb) (0, ctx);
1592 } else if (X509_verify(xs, pkey) <= 0) {
1593 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1594 ctx->current_cert = xs;
1595 ok = (*cb) (0, ctx);
1597 EVP_PKEY_free(pkey);
1601 EVP_PKEY_free(pkey);
1607 ok = check_cert_time(ctx, xs);
1611 /* The last error (if any) is still in the error value */
1612 ctx->current_issuer = xi;
1613 ctx->current_cert = xs;
1614 ok = (*cb) (1, ctx);
1621 xs = sk_X509_value(ctx->chain, n);
1629 int X509_cmp_current_time(const ASN1_TIME *ctm)
1631 return X509_cmp_time(ctm, NULL);
1634 int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1639 char buff1[24], buff2[24], *p;
1640 int i, j, remaining;
1643 remaining = ctm->length;
1644 str = (char *)ctm->data;
1646 * Note that the following (historical) code allows much more slack in the
1647 * time format than RFC5280. In RFC5280, the representation is fixed:
1648 * UTCTime: YYMMDDHHMMSSZ
1649 * GeneralizedTime: YYYYMMDDHHMMSSZ
1651 if (ctm->type == V_ASN1_UTCTIME) {
1652 /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
1653 int min_length = sizeof("YYMMDDHHMMZ") - 1;
1654 int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
1655 if (remaining < min_length || remaining > max_length)
1662 /* YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm */
1663 int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
1664 int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
1665 if (remaining < min_length || remaining > max_length)
1673 if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
1684 * Skip any (up to three) fractional seconds...
1685 * TODO(emilia): in RFC5280, fractional seconds are forbidden.
1686 * Can we just kill them altogether?
1688 if (remaining && *str == '.') {
1691 for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
1692 if (*str < '0' || *str > '9')
1701 /* We now need either a terminating 'Z' or an offset. */
1710 if ((*str != '+') && (*str != '-'))
1712 /* Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280. */
1715 if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
1716 str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
1718 offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
1719 offset += (str[3] - '0') * 10 + (str[4] - '0');
1723 atm.type = ctm->type;
1725 atm.length = sizeof(buff2);
1726 atm.data = (unsigned char *)buff2;
1728 if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
1731 if (ctm->type == V_ASN1_UTCTIME) {
1732 i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
1734 i += 100; /* cf. RFC 2459 */
1735 j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
1744 i = strcmp(buff1, buff2);
1745 if (i == 0) /* wait a second then return younger :-) */
1751 ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
1753 return X509_time_adj(s, adj, NULL);
1756 ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
1758 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1761 ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
1762 int offset_day, long offset_sec, time_t *in_tm)
1771 if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
1772 if (s->type == V_ASN1_UTCTIME)
1773 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
1774 if (s->type == V_ASN1_GENERALIZEDTIME)
1775 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
1777 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1780 int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1782 EVP_PKEY *ktmp = NULL, *ktmp2;
1785 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1788 for (i = 0; i < sk_X509_num(chain); i++) {
1789 ktmp = X509_get_pubkey(sk_X509_value(chain, i));
1791 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1792 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1795 if (!EVP_PKEY_missing_parameters(ktmp))
1798 EVP_PKEY_free(ktmp);
1803 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1804 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1808 /* first, populate the other certs */
1809 for (j = i - 1; j >= 0; j--) {
1810 ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
1811 EVP_PKEY_copy_parameters(ktmp2, ktmp);
1812 EVP_PKEY_free(ktmp2);
1816 EVP_PKEY_copy_parameters(pkey, ktmp);
1817 EVP_PKEY_free(ktmp);
1821 int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
1822 CRYPTO_EX_new *new_func,
1823 CRYPTO_EX_dup *dup_func,
1824 CRYPTO_EX_free *free_func)
1827 * This function is (usually) called only once, by
1828 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
1830 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
1831 new_func, dup_func, free_func);
1834 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
1836 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1839 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
1841 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1844 int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
1849 void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
1854 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
1856 return ctx->error_depth;
1859 X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
1861 return ctx->current_cert;
1864 STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
1869 STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
1873 STACK_OF(X509) *chain;
1874 if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain)))
1876 for (i = 0; i < sk_X509_num(chain); i++) {
1877 x = sk_X509_value(chain, i);
1878 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1883 X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
1885 return ctx->current_issuer;
1888 X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
1890 return ctx->current_crl;
1893 X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
1898 void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
1903 void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1905 ctx->untrusted = sk;
1908 void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
1913 int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
1915 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
1918 int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
1920 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
1924 * This function is used to set the X509_STORE_CTX purpose and trust values.
1925 * This is intended to be used when another structure has its own trust and
1926 * purpose values which (if set) will be inherited by the ctx. If they aren't
1927 * set then we will usually have a default purpose in mind which should then
1928 * be used to set the trust value. An example of this is SSL use: an SSL
1929 * structure will have its own purpose and trust settings which the
1930 * application can set: if they aren't set then we use the default of SSL
1934 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
1935 int purpose, int trust)
1938 /* If purpose not set use default */
1940 purpose = def_purpose;
1941 /* If we have a purpose then check it is valid */
1944 idx = X509_PURPOSE_get_by_id(purpose);
1946 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1947 X509_R_UNKNOWN_PURPOSE_ID);
1950 ptmp = X509_PURPOSE_get0(idx);
1951 if (ptmp->trust == X509_TRUST_DEFAULT) {
1952 idx = X509_PURPOSE_get_by_id(def_purpose);
1954 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1955 X509_R_UNKNOWN_PURPOSE_ID);
1958 ptmp = X509_PURPOSE_get0(idx);
1960 /* If trust not set then get from purpose default */
1962 trust = ptmp->trust;
1965 idx = X509_TRUST_get_by_id(trust);
1967 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1968 X509_R_UNKNOWN_TRUST_ID);
1973 if (purpose && !ctx->param->purpose)
1974 ctx->param->purpose = purpose;
1975 if (trust && !ctx->param->trust)
1976 ctx->param->trust = trust;
1980 X509_STORE_CTX *X509_STORE_CTX_new(void)
1982 X509_STORE_CTX *ctx;
1983 ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
1985 X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
1988 memset(ctx, 0, sizeof(X509_STORE_CTX));
1992 void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
1996 X509_STORE_CTX_cleanup(ctx);
2000 int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
2001 STACK_OF(X509) *chain)
2005 ctx->current_method = 0;
2007 ctx->untrusted = chain;
2009 ctx->last_untrusted = 0;
2010 ctx->other_ctx = NULL;
2014 ctx->explicit_policy = 0;
2015 ctx->error_depth = 0;
2016 ctx->current_cert = NULL;
2017 ctx->current_issuer = NULL;
2018 ctx->current_crl = NULL;
2019 ctx->current_crl_score = 0;
2020 ctx->current_reasons = 0;
2024 ctx->param = X509_VERIFY_PARAM_new();
2027 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2032 * Inherit callbacks and flags from X509_STORE if not set use defaults.
2036 ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2038 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
2041 ctx->verify_cb = store->verify_cb;
2042 ctx->cleanup = store->cleanup;
2047 ret = X509_VERIFY_PARAM_inherit(ctx->param,
2048 X509_VERIFY_PARAM_lookup("default"));
2051 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2055 if (store && store->check_issued)
2056 ctx->check_issued = store->check_issued;
2058 ctx->check_issued = check_issued;
2060 if (store && store->get_issuer)
2061 ctx->get_issuer = store->get_issuer;
2063 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2065 if (store && store->verify_cb)
2066 ctx->verify_cb = store->verify_cb;
2068 ctx->verify_cb = null_callback;
2070 if (store && store->verify)
2071 ctx->verify = store->verify;
2073 ctx->verify = internal_verify;
2075 if (store && store->check_revocation)
2076 ctx->check_revocation = store->check_revocation;
2078 ctx->check_revocation = check_revocation;
2080 if (store && store->get_crl)
2081 ctx->get_crl = store->get_crl;
2083 ctx->get_crl = NULL;
2085 if (store && store->check_crl)
2086 ctx->check_crl = store->check_crl;
2088 ctx->check_crl = check_crl;
2090 if (store && store->cert_crl)
2091 ctx->cert_crl = store->cert_crl;
2093 ctx->cert_crl = cert_crl;
2095 if (store && store->lookup_certs)
2096 ctx->lookup_certs = store->lookup_certs;
2098 ctx->lookup_certs = X509_STORE_get1_certs;
2100 if (store && store->lookup_crls)
2101 ctx->lookup_crls = store->lookup_crls;
2103 ctx->lookup_crls = X509_STORE_get1_crls;
2105 ctx->check_policy = check_policy;
2108 * This memset() can't make any sense anyway, so it's removed. As
2109 * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
2110 * corresponding "new" here and remove this bogus initialisation.
2112 /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
2113 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2116 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2123 * Set alternative lookup method: just a STACK of trusted certificates. This
2124 * avoids X509_STORE nastiness where it isn't needed.
2127 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2129 ctx->other_ctx = sk;
2130 ctx->get_issuer = get_issuer_sk;
2133 void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2137 if (ctx->param != NULL) {
2138 if (ctx->parent == NULL)
2139 X509_VERIFY_PARAM_free(ctx->param);
2142 if (ctx->tree != NULL) {
2143 X509_policy_tree_free(ctx->tree);
2146 if (ctx->chain != NULL) {
2147 sk_X509_pop_free(ctx->chain, X509_free);
2150 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
2151 memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
2154 void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2156 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2159 void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2161 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2164 void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
2167 X509_VERIFY_PARAM_set_time(ctx->param, t);
2170 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2171 int (*verify_cb) (int, X509_STORE_CTX *))
2173 ctx->verify_cb = verify_cb;
2176 X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2181 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2183 return ctx->explicit_policy;
2186 int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2188 const X509_VERIFY_PARAM *param;
2189 param = X509_VERIFY_PARAM_lookup(name);
2192 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2195 X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2200 void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2203 X509_VERIFY_PARAM_free(ctx->param);
2207 IMPLEMENT_STACK_OF(X509)
2209 IMPLEMENT_ASN1_SET_OF(X509)
2211 IMPLEMENT_STACK_OF(X509_NAME)
2213 IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
2215 IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)