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
, *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
228 * at this point, chain should contain a list of untrusted certificates.
229 * We now need to add at least one trusted one, if possible, otherwise we
234 * Examine last certificate in chain and see if it is self signed.
237 i
= sk_X509_num(ctx
->chain
);
238 x
= sk_X509_value(ctx
->chain
, i
- 1);
239 if (ctx
->check_issued(ctx
, x
, x
)) {
240 /* we have a self signed certificate */
241 if (sk_X509_num(ctx
->chain
) == 1) {
243 * We have a single self signed certificate: see if we can find
244 * it in the store. We must have an exact match to avoid possible
247 ok
= ctx
->get_issuer(&xtmp
, ctx
, x
);
248 if ((ok
<= 0) || X509_cmp(x
, xtmp
)) {
249 ctx
->error
= X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
;
250 ctx
->current_cert
= x
;
251 ctx
->error_depth
= i
- 1;
260 * We have a match: replace certificate with store version so
261 * we get any trust settings.
265 (void)sk_X509_set(ctx
->chain
, i
- 1, x
);
266 ctx
->last_untrusted
= 0;
270 * extract and save self signed certificate for later use
272 chain_ss
= sk_X509_pop(ctx
->chain
);
273 ctx
->last_untrusted
--;
275 x
= sk_X509_value(ctx
->chain
, num
- 1);
279 /* We now lookup certs from the certificate store */
281 /* If we have enough, we break */
285 /* If we are self signed, we break */
286 if (ctx
->check_issued(ctx
, x
, x
))
289 ok
= ctx
->get_issuer(&xtmp
, ctx
, x
);
297 if (!sk_X509_push(ctx
->chain
, x
)) {
299 X509err(X509_F_X509_VERIFY_CERT
, ERR_R_MALLOC_FAILURE
);
305 /* we now have our chain, lets check it... */
307 /* Is last certificate looked up self signed? */
308 if (!ctx
->check_issued(ctx
, x
, x
)) {
309 if ((chain_ss
== NULL
) || !ctx
->check_issued(ctx
, x
, chain_ss
)) {
310 if (ctx
->last_untrusted
>= num
)
311 ctx
->error
= X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
;
313 ctx
->error
= X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
;
314 ctx
->current_cert
= x
;
317 sk_X509_push(ctx
->chain
, chain_ss
);
319 ctx
->last_untrusted
= num
;
320 ctx
->current_cert
= chain_ss
;
321 ctx
->error
= X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
;
325 ctx
->error_depth
= num
- 1;
332 /* We have the chain complete: now we need to check its purpose */
333 ok
= check_chain_extensions(ctx
);
338 /* Check name constraints */
340 ok
= check_name_constraints(ctx
);
345 /* The chain extensions are OK: check trust */
347 if (param
->trust
> 0)
348 ok
= check_trust(ctx
);
353 /* We may as well copy down any DSA parameters that are required */
354 X509_get_pubkey_parameters(NULL
, ctx
->chain
);
357 * Check revocation status: we do this after copying parameters because
358 * they may be needed for CRL signature verification.
361 ok
= ctx
->check_revocation(ctx
);
365 /* At this point, we have a chain and need to verify it */
366 if (ctx
->verify
!= NULL
)
367 ok
= ctx
->verify(ctx
);
369 ok
= internal_verify(ctx
);
373 #ifndef OPENSSL_NO_RFC3779
374 /* RFC 3779 path validation, now that CRL check has been done */
375 ok
= v3_asid_validate_path(ctx
);
378 ok
= v3_addr_validate_path(ctx
);
383 /* If we get this far evaluate policies */
384 if (!bad_chain
&& (ctx
->param
->flags
& X509_V_FLAG_POLICY_CHECK
))
385 ok
= ctx
->check_policy(ctx
);
390 X509_get_pubkey_parameters(NULL
, ctx
->chain
);
394 if (chain_ss
!= NULL
)
400 * Given a STACK_OF(X509) find the issuer of cert (if any)
403 static X509
*find_issuer(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
, X509
*x
)
407 for (i
= 0; i
< sk_X509_num(sk
); i
++) {
408 issuer
= sk_X509_value(sk
, i
);
409 if (ctx
->check_issued(ctx
, x
, issuer
))
415 /* Given a possible certificate and issuer check them */
417 static int check_issued(X509_STORE_CTX
*ctx
, X509
*x
, X509
*issuer
)
420 ret
= X509_check_issued(issuer
, x
);
421 if (ret
== X509_V_OK
)
423 /* If we haven't asked for issuer errors don't set ctx */
424 if (!(ctx
->param
->flags
& X509_V_FLAG_CB_ISSUER_CHECK
))
428 ctx
->current_cert
= x
;
429 ctx
->current_issuer
= issuer
;
430 return ctx
->verify_cb(0, ctx
);
434 /* Alternative lookup method: look from a STACK stored in other_ctx */
436 static int get_issuer_sk(X509
**issuer
, X509_STORE_CTX
*ctx
, X509
*x
)
438 *issuer
= find_issuer(ctx
, ctx
->other_ctx
, x
);
440 CRYPTO_add(&(*issuer
)->references
, 1, CRYPTO_LOCK_X509
);
447 * Check a certificate chains extensions for consistency with the supplied
451 static int check_chain_extensions(X509_STORE_CTX
*ctx
)
453 #ifdef OPENSSL_NO_CHAIN_VERIFY
456 int i
, ok
= 0, must_be_ca
, plen
= 0;
458 int (*cb
) (int xok
, X509_STORE_CTX
*xctx
);
459 int proxy_path_length
= 0;
461 int allow_proxy_certs
;
465 * must_be_ca can have 1 of 3 values:
466 * -1: we accept both CA and non-CA certificates, to allow direct
467 * use of self-signed certificates (which are marked as CA).
468 * 0: we only accept non-CA certificates. This is currently not
469 * used, but the possibility is present for future extensions.
470 * 1: we only accept CA certificates. This is currently used for
471 * all certificates in the chain except the leaf certificate.
475 /* CRL path validation */
477 allow_proxy_certs
= 0;
478 purpose
= X509_PURPOSE_CRL_SIGN
;
481 ! !(ctx
->param
->flags
& X509_V_FLAG_ALLOW_PROXY_CERTS
);
483 * A hack to keep people who don't want to modify their software
486 if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
487 allow_proxy_certs
= 1;
488 purpose
= ctx
->param
->purpose
;
491 /* Check all untrusted certificates */
492 for (i
= 0; i
< ctx
->last_untrusted
; i
++) {
494 x
= sk_X509_value(ctx
->chain
, i
);
495 if (!(ctx
->param
->flags
& X509_V_FLAG_IGNORE_CRITICAL
)
496 && (x
->ex_flags
& EXFLAG_CRITICAL
)) {
497 ctx
->error
= X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION
;
498 ctx
->error_depth
= i
;
499 ctx
->current_cert
= x
;
504 if (!allow_proxy_certs
&& (x
->ex_flags
& EXFLAG_PROXY
)) {
505 ctx
->error
= X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED
;
506 ctx
->error_depth
= i
;
507 ctx
->current_cert
= x
;
512 ret
= X509_check_ca(x
);
513 switch (must_be_ca
) {
515 if ((ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
)
516 && (ret
!= 1) && (ret
!= 0)) {
518 ctx
->error
= X509_V_ERR_INVALID_CA
;
525 ctx
->error
= X509_V_ERR_INVALID_NON_CA
;
531 || ((ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
)
534 ctx
->error
= X509_V_ERR_INVALID_CA
;
540 ctx
->error_depth
= i
;
541 ctx
->current_cert
= x
;
546 if (ctx
->param
->purpose
> 0) {
547 ret
= X509_check_purpose(x
, purpose
, must_be_ca
> 0);
549 || ((ctx
->param
->flags
& X509_V_FLAG_X509_STRICT
)
551 ctx
->error
= X509_V_ERR_INVALID_PURPOSE
;
552 ctx
->error_depth
= i
;
553 ctx
->current_cert
= x
;
559 /* Check pathlen if not self issued */
560 if ((i
> 1) && !(x
->ex_flags
& EXFLAG_SI
)
561 && (x
->ex_pathlen
!= -1)
562 && (plen
> (x
->ex_pathlen
+ proxy_path_length
+ 1))) {
563 ctx
->error
= X509_V_ERR_PATH_LENGTH_EXCEEDED
;
564 ctx
->error_depth
= i
;
565 ctx
->current_cert
= x
;
570 /* Increment path length if not self issued */
571 if (!(x
->ex_flags
& EXFLAG_SI
))
574 * If this certificate is a proxy certificate, the next certificate
575 * must be another proxy certificate or a EE certificate. If not,
576 * the next certificate must be a CA certificate.
578 if (x
->ex_flags
& EXFLAG_PROXY
) {
579 if (x
->ex_pcpathlen
!= -1 && i
> x
->ex_pcpathlen
) {
580 ctx
->error
= X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED
;
581 ctx
->error_depth
= i
;
582 ctx
->current_cert
= x
;
598 static int check_name_constraints(X509_STORE_CTX
*ctx
)
602 /* Check name constraints for all certificates */
603 for (i
= sk_X509_num(ctx
->chain
) - 1; i
>= 0; i
--) {
604 x
= sk_X509_value(ctx
->chain
, i
);
605 /* Ignore self issued certs unless last in chain */
606 if (i
&& (x
->ex_flags
& EXFLAG_SI
))
609 * Check against constraints for all certificates higher in chain
610 * including trust anchor. Trust anchor not strictly speaking needed
611 * but if it includes constraints it is to be assumed it expects them
614 for (j
= sk_X509_num(ctx
->chain
) - 1; j
> i
; j
--) {
615 NAME_CONSTRAINTS
*nc
= sk_X509_value(ctx
->chain
, j
)->nc
;
617 rv
= NAME_CONSTRAINTS_check(x
, nc
);
618 if (rv
!= X509_V_OK
) {
620 ctx
->error_depth
= i
;
621 ctx
->current_cert
= x
;
622 if (!ctx
->verify_cb(0, ctx
))
631 static int check_trust(X509_STORE_CTX
*ctx
)
633 #ifdef OPENSSL_NO_CHAIN_VERIFY
638 int (*cb
) (int xok
, X509_STORE_CTX
*xctx
);
640 /* For now just check the last certificate in the chain */
641 i
= sk_X509_num(ctx
->chain
) - 1;
642 x
= sk_X509_value(ctx
->chain
, i
);
643 ok
= X509_check_trust(x
, ctx
->param
->trust
, 0);
644 if (ok
== X509_TRUST_TRUSTED
)
646 ctx
->error_depth
= i
;
647 ctx
->current_cert
= x
;
648 if (ok
== X509_TRUST_REJECTED
)
649 ctx
->error
= X509_V_ERR_CERT_REJECTED
;
651 ctx
->error
= X509_V_ERR_CERT_UNTRUSTED
;
657 static int check_revocation(X509_STORE_CTX
*ctx
)
660 if (!(ctx
->param
->flags
& X509_V_FLAG_CRL_CHECK
))
662 if (ctx
->param
->flags
& X509_V_FLAG_CRL_CHECK_ALL
)
663 last
= sk_X509_num(ctx
->chain
) - 1;
665 /* If checking CRL paths this isn't the EE certificate */
670 for (i
= 0; i
<= last
; i
++) {
671 ctx
->error_depth
= i
;
672 ok
= check_cert(ctx
);
679 static int check_cert(X509_STORE_CTX
*ctx
)
681 X509_CRL
*crl
= NULL
, *dcrl
= NULL
;
684 unsigned int last_reasons
;
685 cnum
= ctx
->error_depth
;
686 x
= sk_X509_value(ctx
->chain
, cnum
);
687 ctx
->current_cert
= x
;
688 ctx
->current_issuer
= NULL
;
689 ctx
->current_crl_score
= 0;
690 ctx
->current_reasons
= 0;
691 while (ctx
->current_reasons
!= CRLDP_ALL_REASONS
) {
692 last_reasons
= ctx
->current_reasons
;
693 /* Try to retrieve relevant CRL */
695 ok
= ctx
->get_crl(ctx
, &crl
, x
);
697 ok
= get_crl_delta(ctx
, &crl
, &dcrl
, x
);
699 * If error looking up CRL, nothing we can do except notify callback
702 ctx
->error
= X509_V_ERR_UNABLE_TO_GET_CRL
;
703 ok
= ctx
->verify_cb(0, ctx
);
706 ctx
->current_crl
= crl
;
707 ok
= ctx
->check_crl(ctx
, crl
);
712 ok
= ctx
->check_crl(ctx
, dcrl
);
715 ok
= ctx
->cert_crl(ctx
, dcrl
, x
);
721 /* Don't look in full CRL if delta reason is removefromCRL */
723 ok
= ctx
->cert_crl(ctx
, crl
, x
);
733 * If reasons not updated we wont get anywhere by another iteration,
736 if (last_reasons
== ctx
->current_reasons
) {
737 ctx
->error
= X509_V_ERR_UNABLE_TO_GET_CRL
;
738 ok
= ctx
->verify_cb(0, ctx
);
746 ctx
->current_crl
= NULL
;
751 /* Check CRL times against values in X509_STORE_CTX */
753 static int check_crl_time(X509_STORE_CTX
*ctx
, X509_CRL
*crl
, int notify
)
758 ctx
->current_crl
= crl
;
759 if (ctx
->param
->flags
& X509_V_FLAG_USE_CHECK_TIME
)
760 ptime
= &ctx
->param
->check_time
;
764 i
= X509_cmp_time(X509_CRL_get_lastUpdate(crl
), ptime
);
768 ctx
->error
= X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
;
769 if (!ctx
->verify_cb(0, ctx
))
776 ctx
->error
= X509_V_ERR_CRL_NOT_YET_VALID
;
777 if (!ctx
->verify_cb(0, ctx
))
781 if (X509_CRL_get_nextUpdate(crl
)) {
782 i
= X509_cmp_time(X509_CRL_get_nextUpdate(crl
), ptime
);
787 ctx
->error
= X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
;
788 if (!ctx
->verify_cb(0, ctx
))
791 /* Ignore expiry of base CRL is delta is valid */
792 if ((i
< 0) && !(ctx
->current_crl_score
& CRL_SCORE_TIME_DELTA
)) {
795 ctx
->error
= X509_V_ERR_CRL_HAS_EXPIRED
;
796 if (!ctx
->verify_cb(0, ctx
))
802 ctx
->current_crl
= NULL
;
807 static int get_crl_sk(X509_STORE_CTX
*ctx
, X509_CRL
**pcrl
, X509_CRL
**pdcrl
,
808 X509
**pissuer
, int *pscore
, unsigned int *preasons
,
809 STACK_OF(X509_CRL
) *crls
)
811 int i
, crl_score
, best_score
= *pscore
;
812 unsigned int reasons
, best_reasons
= 0;
813 X509
*x
= ctx
->current_cert
;
814 X509_CRL
*crl
, *best_crl
= NULL
;
815 X509
*crl_issuer
= NULL
, *best_crl_issuer
= NULL
;
817 for (i
= 0; i
< sk_X509_CRL_num(crls
); i
++) {
818 crl
= sk_X509_CRL_value(crls
, i
);
820 crl_score
= get_crl_score(ctx
, &crl_issuer
, &reasons
, crl
, x
);
822 if (crl_score
> best_score
) {
824 best_crl_issuer
= crl_issuer
;
825 best_score
= crl_score
;
826 best_reasons
= reasons
;
832 X509_CRL_free(*pcrl
);
834 *pissuer
= best_crl_issuer
;
835 *pscore
= best_score
;
836 *preasons
= best_reasons
;
837 CRYPTO_add(&best_crl
->references
, 1, CRYPTO_LOCK_X509_CRL
);
839 X509_CRL_free(*pdcrl
);
842 get_delta_sk(ctx
, pdcrl
, pscore
, best_crl
, crls
);
845 if (best_score
>= CRL_SCORE_VALID
)
852 * Compare two CRL extensions for delta checking purposes. They should be
853 * both present or both absent. If both present all fields must be identical.
856 static int crl_extension_match(X509_CRL
*a
, X509_CRL
*b
, int nid
)
858 ASN1_OCTET_STRING
*exta
, *extb
;
860 i
= X509_CRL_get_ext_by_NID(a
, nid
, -1);
862 /* Can't have multiple occurrences */
863 if (X509_CRL_get_ext_by_NID(a
, nid
, i
) != -1)
865 exta
= X509_EXTENSION_get_data(X509_CRL_get_ext(a
, i
));
869 i
= X509_CRL_get_ext_by_NID(b
, nid
, -1);
873 if (X509_CRL_get_ext_by_NID(b
, nid
, i
) != -1)
875 extb
= X509_EXTENSION_get_data(X509_CRL_get_ext(b
, i
));
885 if (ASN1_OCTET_STRING_cmp(exta
, extb
))
891 /* See if a base and delta are compatible */
893 static int check_delta_base(X509_CRL
*delta
, X509_CRL
*base
)
895 /* Delta CRL must be a delta */
896 if (!delta
->base_crl_number
)
898 /* Base must have a CRL number */
899 if (!base
->crl_number
)
901 /* Issuer names must match */
902 if (X509_NAME_cmp(X509_CRL_get_issuer(base
), X509_CRL_get_issuer(delta
)))
904 /* AKID and IDP must match */
905 if (!crl_extension_match(delta
, base
, NID_authority_key_identifier
))
907 if (!crl_extension_match(delta
, base
, NID_issuing_distribution_point
))
909 /* Delta CRL base number must not exceed Full CRL number. */
910 if (ASN1_INTEGER_cmp(delta
->base_crl_number
, base
->crl_number
) > 0)
912 /* Delta CRL number must exceed full CRL number */
913 if (ASN1_INTEGER_cmp(delta
->crl_number
, base
->crl_number
) > 0)
919 * For a given base CRL find a delta... maybe extend to delta scoring or
920 * retrieve a chain of deltas...
923 static void get_delta_sk(X509_STORE_CTX
*ctx
, X509_CRL
**dcrl
, int *pscore
,
924 X509_CRL
*base
, STACK_OF(X509_CRL
) *crls
)
928 if (!(ctx
->param
->flags
& X509_V_FLAG_USE_DELTAS
))
930 if (!((ctx
->current_cert
->ex_flags
| base
->flags
) & EXFLAG_FRESHEST
))
932 for (i
= 0; i
< sk_X509_CRL_num(crls
); i
++) {
933 delta
= sk_X509_CRL_value(crls
, i
);
934 if (check_delta_base(delta
, base
)) {
935 if (check_crl_time(ctx
, delta
, 0))
936 *pscore
|= CRL_SCORE_TIME_DELTA
;
937 CRYPTO_add(&delta
->references
, 1, CRYPTO_LOCK_X509_CRL
);
946 * For a given CRL return how suitable it is for the supplied certificate
947 * 'x'. The return value is a mask of several criteria. If the issuer is not
948 * the certificate issuer this is returned in *pissuer. The reasons mask is
949 * also used to determine if the CRL is suitable: if no new reasons the CRL
950 * is rejected, otherwise reasons is updated.
953 static int get_crl_score(X509_STORE_CTX
*ctx
, X509
**pissuer
,
954 unsigned int *preasons
, X509_CRL
*crl
, X509
*x
)
958 unsigned int tmp_reasons
= *preasons
, crl_reasons
;
960 /* First see if we can reject CRL straight away */
962 /* Invalid IDP cannot be processed */
963 if (crl
->idp_flags
& IDP_INVALID
)
965 /* Reason codes or indirect CRLs need extended CRL support */
966 if (!(ctx
->param
->flags
& X509_V_FLAG_EXTENDED_CRL_SUPPORT
)) {
967 if (crl
->idp_flags
& (IDP_INDIRECT
| IDP_REASONS
))
969 } else if (crl
->idp_flags
& IDP_REASONS
) {
970 /* If no new reasons reject */
971 if (!(crl
->idp_reasons
& ~tmp_reasons
))
974 /* Don't process deltas at this stage */
975 else if (crl
->base_crl_number
)
977 /* If issuer name doesn't match certificate need indirect CRL */
978 if (X509_NAME_cmp(X509_get_issuer_name(x
), X509_CRL_get_issuer(crl
))) {
979 if (!(crl
->idp_flags
& IDP_INDIRECT
))
982 crl_score
|= CRL_SCORE_ISSUER_NAME
;
984 if (!(crl
->flags
& EXFLAG_CRITICAL
))
985 crl_score
|= CRL_SCORE_NOCRITICAL
;
988 if (check_crl_time(ctx
, crl
, 0))
989 crl_score
|= CRL_SCORE_TIME
;
991 /* Check authority key ID and locate certificate issuer */
992 crl_akid_check(ctx
, crl
, pissuer
, &crl_score
);
994 /* If we can't locate certificate issuer at this point forget it */
996 if (!(crl_score
& CRL_SCORE_AKID
))
999 /* Check cert for matching CRL distribution points */
1001 if (crl_crldp_check(x
, crl
, crl_score
, &crl_reasons
)) {
1002 /* If no new reasons reject */
1003 if (!(crl_reasons
& ~tmp_reasons
))
1005 tmp_reasons
|= crl_reasons
;
1006 crl_score
|= CRL_SCORE_SCOPE
;
1009 *preasons
= tmp_reasons
;
1015 static void crl_akid_check(X509_STORE_CTX
*ctx
, X509_CRL
*crl
,
1016 X509
**pissuer
, int *pcrl_score
)
1018 X509
*crl_issuer
= NULL
;
1019 X509_NAME
*cnm
= X509_CRL_get_issuer(crl
);
1020 int cidx
= ctx
->error_depth
;
1023 if (cidx
!= sk_X509_num(ctx
->chain
) - 1)
1026 crl_issuer
= sk_X509_value(ctx
->chain
, cidx
);
1028 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1029 if (*pcrl_score
& CRL_SCORE_ISSUER_NAME
) {
1030 *pcrl_score
|= CRL_SCORE_AKID
| CRL_SCORE_ISSUER_CERT
;
1031 *pissuer
= crl_issuer
;
1036 for (cidx
++; cidx
< sk_X509_num(ctx
->chain
); cidx
++) {
1037 crl_issuer
= sk_X509_value(ctx
->chain
, cidx
);
1038 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer
), cnm
))
1040 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1041 *pcrl_score
|= CRL_SCORE_AKID
| CRL_SCORE_SAME_PATH
;
1042 *pissuer
= crl_issuer
;
1047 /* Anything else needs extended CRL support */
1049 if (!(ctx
->param
->flags
& X509_V_FLAG_EXTENDED_CRL_SUPPORT
))
1053 * Otherwise the CRL issuer is not on the path. Look for it in the set of
1054 * untrusted certificates.
1056 for (i
= 0; i
< sk_X509_num(ctx
->untrusted
); i
++) {
1057 crl_issuer
= sk_X509_value(ctx
->untrusted
, i
);
1058 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer
), cnm
))
1060 if (X509_check_akid(crl_issuer
, crl
->akid
) == X509_V_OK
) {
1061 *pissuer
= crl_issuer
;
1062 *pcrl_score
|= CRL_SCORE_AKID
;
1069 * Check the path of a CRL issuer certificate. This creates a new
1070 * X509_STORE_CTX and populates it with most of the parameters from the
1071 * parent. This could be optimised somewhat since a lot of path checking will
1072 * be duplicated by the parent, but this will rarely be used in practice.
1075 static int check_crl_path(X509_STORE_CTX
*ctx
, X509
*x
)
1077 X509_STORE_CTX crl_ctx
;
1079 /* Don't allow recursive CRL path validation */
1082 if (!X509_STORE_CTX_init(&crl_ctx
, ctx
->ctx
, x
, ctx
->untrusted
))
1085 crl_ctx
.crls
= ctx
->crls
;
1086 /* Copy verify params across */
1087 X509_STORE_CTX_set0_param(&crl_ctx
, ctx
->param
);
1089 crl_ctx
.parent
= ctx
;
1090 crl_ctx
.verify_cb
= ctx
->verify_cb
;
1092 /* Verify CRL issuer */
1093 ret
= X509_verify_cert(&crl_ctx
);
1098 /* Check chain is acceptable */
1100 ret
= check_crl_chain(ctx
, ctx
->chain
, crl_ctx
.chain
);
1102 X509_STORE_CTX_cleanup(&crl_ctx
);
1107 * RFC3280 says nothing about the relationship between CRL path and
1108 * certificate path, which could lead to situations where a certificate could
1109 * be revoked or validated by a CA not authorised to do so. RFC5280 is more
1110 * strict and states that the two paths must end in the same trust anchor,
1111 * though some discussions remain... until this is resolved we use the
1115 static int check_crl_chain(X509_STORE_CTX
*ctx
,
1116 STACK_OF(X509
) *cert_path
,
1117 STACK_OF(X509
) *crl_path
)
1119 X509
*cert_ta
, *crl_ta
;
1120 cert_ta
= sk_X509_value(cert_path
, sk_X509_num(cert_path
) - 1);
1121 crl_ta
= sk_X509_value(crl_path
, sk_X509_num(crl_path
) - 1);
1122 if (!X509_cmp(cert_ta
, crl_ta
))
1128 * Check for match between two dist point names: three separate cases.
1129 * 1. Both are relative names and compare X509_NAME types.
1130 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1131 * 3. Both are full names and compare two GENERAL_NAMES.
1132 * 4. One is NULL: automatic match.
1135 static int idp_check_dp(DIST_POINT_NAME
*a
, DIST_POINT_NAME
*b
)
1137 X509_NAME
*nm
= NULL
;
1138 GENERAL_NAMES
*gens
= NULL
;
1139 GENERAL_NAME
*gena
, *genb
;
1146 /* Case 1: two X509_NAME */
1150 if (!X509_NAME_cmp(a
->dpname
, b
->dpname
))
1155 /* Case 2: set name and GENERAL_NAMES appropriately */
1157 gens
= b
->name
.fullname
;
1158 } else if (b
->type
== 1) {
1161 /* Case 2: set name and GENERAL_NAMES appropriately */
1162 gens
= a
->name
.fullname
;
1166 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1168 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
1169 gena
= sk_GENERAL_NAME_value(gens
, i
);
1170 if (gena
->type
!= GEN_DIRNAME
)
1172 if (!X509_NAME_cmp(nm
, gena
->d
.directoryName
))
1178 /* Else case 3: two GENERAL_NAMES */
1180 for (i
= 0; i
< sk_GENERAL_NAME_num(a
->name
.fullname
); i
++) {
1181 gena
= sk_GENERAL_NAME_value(a
->name
.fullname
, i
);
1182 for (j
= 0; j
< sk_GENERAL_NAME_num(b
->name
.fullname
); j
++) {
1183 genb
= sk_GENERAL_NAME_value(b
->name
.fullname
, j
);
1184 if (!GENERAL_NAME_cmp(gena
, genb
))
1193 static int crldp_check_crlissuer(DIST_POINT
*dp
, X509_CRL
*crl
, int crl_score
)
1196 X509_NAME
*nm
= X509_CRL_get_issuer(crl
);
1197 /* If no CRLissuer return is successful iff don't need a match */
1199 return ! !(crl_score
& CRL_SCORE_ISSUER_NAME
);
1200 for (i
= 0; i
< sk_GENERAL_NAME_num(dp
->CRLissuer
); i
++) {
1201 GENERAL_NAME
*gen
= sk_GENERAL_NAME_value(dp
->CRLissuer
, i
);
1202 if (gen
->type
!= GEN_DIRNAME
)
1204 if (!X509_NAME_cmp(gen
->d
.directoryName
, nm
))
1210 /* Check CRLDP and IDP */
1212 static int crl_crldp_check(X509
*x
, X509_CRL
*crl
, int crl_score
,
1213 unsigned int *preasons
)
1216 if (crl
->idp_flags
& IDP_ONLYATTR
)
1218 if (x
->ex_flags
& EXFLAG_CA
) {
1219 if (crl
->idp_flags
& IDP_ONLYUSER
)
1222 if (crl
->idp_flags
& IDP_ONLYCA
)
1225 *preasons
= crl
->idp_reasons
;
1226 for (i
= 0; i
< sk_DIST_POINT_num(x
->crldp
); i
++) {
1227 DIST_POINT
*dp
= sk_DIST_POINT_value(x
->crldp
, i
);
1228 if (crldp_check_crlissuer(dp
, crl
, crl_score
)) {
1229 if (!crl
->idp
|| idp_check_dp(dp
->distpoint
, crl
->idp
->distpoint
)) {
1230 *preasons
&= dp
->dp_reasons
;
1235 if ((!crl
->idp
|| !crl
->idp
->distpoint
)
1236 && (crl_score
& CRL_SCORE_ISSUER_NAME
))
1242 * Retrieve CRL corresponding to current certificate. If deltas enabled try
1243 * to find a delta CRL too
1246 static int get_crl_delta(X509_STORE_CTX
*ctx
,
1247 X509_CRL
**pcrl
, X509_CRL
**pdcrl
, X509
*x
)
1250 X509
*issuer
= NULL
;
1252 unsigned int reasons
;
1253 X509_CRL
*crl
= NULL
, *dcrl
= NULL
;
1254 STACK_OF(X509_CRL
) *skcrl
;
1255 X509_NAME
*nm
= X509_get_issuer_name(x
);
1256 reasons
= ctx
->current_reasons
;
1257 ok
= get_crl_sk(ctx
, &crl
, &dcrl
,
1258 &issuer
, &crl_score
, &reasons
, ctx
->crls
);
1263 /* Lookup CRLs from store */
1265 skcrl
= ctx
->lookup_crls(ctx
, nm
);
1267 /* If no CRLs found and a near match from get_crl_sk use that */
1271 get_crl_sk(ctx
, &crl
, &dcrl
, &issuer
, &crl_score
, &reasons
, skcrl
);
1273 sk_X509_CRL_pop_free(skcrl
, X509_CRL_free
);
1277 /* If we got any kind of CRL use it and return success */
1279 ctx
->current_issuer
= issuer
;
1280 ctx
->current_crl_score
= crl_score
;
1281 ctx
->current_reasons
= reasons
;
1290 /* Check CRL validity */
1291 static int check_crl(X509_STORE_CTX
*ctx
, X509_CRL
*crl
)
1293 X509
*issuer
= NULL
;
1294 EVP_PKEY
*ikey
= NULL
;
1295 int ok
= 0, chnum
, cnum
;
1296 cnum
= ctx
->error_depth
;
1297 chnum
= sk_X509_num(ctx
->chain
) - 1;
1298 /* if we have an alternative CRL issuer cert use that */
1299 if (ctx
->current_issuer
)
1300 issuer
= ctx
->current_issuer
;
1303 * Else find CRL issuer: if not last certificate then issuer is next
1304 * certificate in chain.
1306 else if (cnum
< chnum
)
1307 issuer
= sk_X509_value(ctx
->chain
, cnum
+ 1);
1309 issuer
= sk_X509_value(ctx
->chain
, chnum
);
1310 /* If not self signed, can't check signature */
1311 if (!ctx
->check_issued(ctx
, issuer
, issuer
)) {
1312 ctx
->error
= X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
;
1313 ok
= ctx
->verify_cb(0, ctx
);
1321 * Skip most tests for deltas because they have already been done
1323 if (!crl
->base_crl_number
) {
1324 /* Check for cRLSign bit if keyUsage present */
1325 if ((issuer
->ex_flags
& EXFLAG_KUSAGE
) &&
1326 !(issuer
->ex_kusage
& KU_CRL_SIGN
)) {
1327 ctx
->error
= X509_V_ERR_KEYUSAGE_NO_CRL_SIGN
;
1328 ok
= ctx
->verify_cb(0, ctx
);
1333 if (!(ctx
->current_crl_score
& CRL_SCORE_SCOPE
)) {
1334 ctx
->error
= X509_V_ERR_DIFFERENT_CRL_SCOPE
;
1335 ok
= ctx
->verify_cb(0, ctx
);
1340 if (!(ctx
->current_crl_score
& CRL_SCORE_SAME_PATH
)) {
1341 if (check_crl_path(ctx
, ctx
->current_issuer
) <= 0) {
1342 ctx
->error
= X509_V_ERR_CRL_PATH_VALIDATION_ERROR
;
1343 ok
= ctx
->verify_cb(0, ctx
);
1349 if (crl
->idp_flags
& IDP_INVALID
) {
1350 ctx
->error
= X509_V_ERR_INVALID_EXTENSION
;
1351 ok
= ctx
->verify_cb(0, ctx
);
1358 if (!(ctx
->current_crl_score
& CRL_SCORE_TIME
)) {
1359 ok
= check_crl_time(ctx
, crl
, 1);
1364 /* Attempt to get issuer certificate public key */
1365 ikey
= X509_get_pubkey(issuer
);
1368 ctx
->error
= X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
;
1369 ok
= ctx
->verify_cb(0, ctx
);
1373 /* Verify CRL signature */
1374 if (X509_CRL_verify(crl
, ikey
) <= 0) {
1375 ctx
->error
= X509_V_ERR_CRL_SIGNATURE_FAILURE
;
1376 ok
= ctx
->verify_cb(0, ctx
);
1386 EVP_PKEY_free(ikey
);
1390 /* Check certificate against CRL */
1391 static int cert_crl(X509_STORE_CTX
*ctx
, X509_CRL
*crl
, X509
*x
)
1396 * The rules changed for this... previously if a CRL contained unhandled
1397 * critical extensions it could still be used to indicate a certificate
1398 * was revoked. This has since been changed since critical extension can
1399 * change the meaning of CRL entries.
1401 if (!(ctx
->param
->flags
& X509_V_FLAG_IGNORE_CRITICAL
)
1402 && (crl
->flags
& EXFLAG_CRITICAL
)) {
1403 ctx
->error
= X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION
;
1404 ok
= ctx
->verify_cb(0, ctx
);
1409 * Look for serial number of certificate in CRL If found make sure reason
1410 * is not removeFromCRL.
1412 if (X509_CRL_get0_by_cert(crl
, &rev
, x
)) {
1413 if (rev
->reason
== CRL_REASON_REMOVE_FROM_CRL
)
1415 ctx
->error
= X509_V_ERR_CERT_REVOKED
;
1416 ok
= ctx
->verify_cb(0, ctx
);
1424 static int check_policy(X509_STORE_CTX
*ctx
)
1429 ret
= X509_policy_check(&ctx
->tree
, &ctx
->explicit_policy
, ctx
->chain
,
1430 ctx
->param
->policies
, ctx
->param
->flags
);
1432 X509err(X509_F_CHECK_POLICY
, ERR_R_MALLOC_FAILURE
);
1435 /* Invalid or inconsistent extensions */
1438 * Locate certificates with bad extensions and notify callback.
1442 for (i
= 1; i
< sk_X509_num(ctx
->chain
); i
++) {
1443 x
= sk_X509_value(ctx
->chain
, i
);
1444 if (!(x
->ex_flags
& EXFLAG_INVALID_POLICY
))
1446 ctx
->current_cert
= x
;
1447 ctx
->error
= X509_V_ERR_INVALID_POLICY_EXTENSION
;
1448 if (!ctx
->verify_cb(0, ctx
))
1454 ctx
->current_cert
= NULL
;
1455 ctx
->error
= X509_V_ERR_NO_EXPLICIT_POLICY
;
1456 return ctx
->verify_cb(0, ctx
);
1459 if (ctx
->param
->flags
& X509_V_FLAG_NOTIFY_POLICY
) {
1460 ctx
->current_cert
= NULL
;
1461 ctx
->error
= X509_V_OK
;
1462 if (!ctx
->verify_cb(2, ctx
))
1469 static int check_cert_time(X509_STORE_CTX
*ctx
, X509
*x
)
1474 if (ctx
->param
->flags
& X509_V_FLAG_USE_CHECK_TIME
)
1475 ptime
= &ctx
->param
->check_time
;
1479 i
= X509_cmp_time(X509_get_notBefore(x
), ptime
);
1481 ctx
->error
= X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
;
1482 ctx
->current_cert
= x
;
1483 if (!ctx
->verify_cb(0, ctx
))
1488 ctx
->error
= X509_V_ERR_CERT_NOT_YET_VALID
;
1489 ctx
->current_cert
= x
;
1490 if (!ctx
->verify_cb(0, ctx
))
1494 i
= X509_cmp_time(X509_get_notAfter(x
), ptime
);
1496 ctx
->error
= X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
;
1497 ctx
->current_cert
= x
;
1498 if (!ctx
->verify_cb(0, ctx
))
1503 ctx
->error
= X509_V_ERR_CERT_HAS_EXPIRED
;
1504 ctx
->current_cert
= x
;
1505 if (!ctx
->verify_cb(0, ctx
))
1512 static int internal_verify(X509_STORE_CTX
*ctx
)
1516 EVP_PKEY
*pkey
= NULL
;
1517 int (*cb
) (int xok
, X509_STORE_CTX
*xctx
);
1519 cb
= ctx
->verify_cb
;
1521 n
= sk_X509_num(ctx
->chain
);
1522 ctx
->error_depth
= n
- 1;
1524 xi
= sk_X509_value(ctx
->chain
, n
);
1526 if (ctx
->check_issued(ctx
, xi
, xi
))
1530 ctx
->error
= X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
;
1531 ctx
->current_cert
= xi
;
1536 ctx
->error_depth
= n
;
1537 xs
= sk_X509_value(ctx
->chain
, n
);
1541 /* ctx->error=0; not needed */
1543 ctx
->error_depth
= n
;
1546 * Skip signature check for self signed certificates unless
1547 * explicitly asked for. It doesn't add any security and just wastes
1552 || (ctx
->param
->flags
& X509_V_FLAG_CHECK_SS_SIGNATURE
))) {
1553 if ((pkey
= X509_get_pubkey(xi
)) == NULL
) {
1554 ctx
->error
= X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
;
1555 ctx
->current_cert
= xi
;
1556 ok
= (*cb
) (0, ctx
);
1559 } else if (X509_verify(xs
, pkey
) <= 0) {
1560 ctx
->error
= X509_V_ERR_CERT_SIGNATURE_FAILURE
;
1561 ctx
->current_cert
= xs
;
1562 ok
= (*cb
) (0, ctx
);
1564 EVP_PKEY_free(pkey
);
1568 EVP_PKEY_free(pkey
);
1574 ok
= check_cert_time(ctx
, xs
);
1578 /* The last error (if any) is still in the error value */
1579 ctx
->current_issuer
= xi
;
1580 ctx
->current_cert
= xs
;
1581 ok
= (*cb
) (1, ctx
);
1588 xs
= sk_X509_value(ctx
->chain
, n
);
1596 int X509_cmp_current_time(const ASN1_TIME
*ctm
)
1598 return X509_cmp_time(ctm
, NULL
);
1601 int X509_cmp_time(const ASN1_TIME
*ctm
, time_t *cmp_time
)
1606 char buff1
[24], buff2
[24], *p
;
1611 str
= (char *)ctm
->data
;
1612 if (ctm
->type
== V_ASN1_UTCTIME
) {
1613 if ((i
< 11) || (i
> 17))
1626 if ((*str
== 'Z') || (*str
== '-') || (*str
== '+')) {
1632 /* Skip any fractional seconds... */
1635 while ((*str
>= '0') && (*str
<= '9'))
1646 if ((*str
!= '+') && (*str
!= '-'))
1648 offset
= ((str
[1] - '0') * 10 + (str
[2] - '0')) * 60;
1649 offset
+= (str
[3] - '0') * 10 + (str
[4] - '0');
1653 atm
.type
= ctm
->type
;
1655 atm
.length
= sizeof(buff2
);
1656 atm
.data
= (unsigned char *)buff2
;
1658 if (X509_time_adj(&atm
, offset
* 60, cmp_time
) == NULL
)
1661 if (ctm
->type
== V_ASN1_UTCTIME
) {
1662 i
= (buff1
[0] - '0') * 10 + (buff1
[1] - '0');
1664 i
+= 100; /* cf. RFC 2459 */
1665 j
= (buff2
[0] - '0') * 10 + (buff2
[1] - '0');
1674 i
= strcmp(buff1
, buff2
);
1675 if (i
== 0) /* wait a second then return younger :-) */
1681 ASN1_TIME
*X509_gmtime_adj(ASN1_TIME
*s
, long adj
)
1683 return X509_time_adj(s
, adj
, NULL
);
1686 ASN1_TIME
*X509_time_adj(ASN1_TIME
*s
, long offset_sec
, time_t *in_tm
)
1688 return X509_time_adj_ex(s
, 0, offset_sec
, in_tm
);
1691 ASN1_TIME
*X509_time_adj_ex(ASN1_TIME
*s
,
1692 int offset_day
, long offset_sec
, time_t *in_tm
)
1701 if (s
&& !(s
->flags
& ASN1_STRING_FLAG_MSTRING
)) {
1702 if (s
->type
== V_ASN1_UTCTIME
)
1703 return ASN1_UTCTIME_adj(s
, t
, offset_day
, offset_sec
);
1704 if (s
->type
== V_ASN1_GENERALIZEDTIME
)
1705 return ASN1_GENERALIZEDTIME_adj(s
, t
, offset_day
, offset_sec
);
1707 return ASN1_TIME_adj(s
, t
, offset_day
, offset_sec
);
1710 int X509_get_pubkey_parameters(EVP_PKEY
*pkey
, STACK_OF(X509
) *chain
)
1712 EVP_PKEY
*ktmp
= NULL
, *ktmp2
;
1715 if ((pkey
!= NULL
) && !EVP_PKEY_missing_parameters(pkey
))
1718 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
1719 ktmp
= X509_get_pubkey(sk_X509_value(chain
, i
));
1721 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS
,
1722 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY
);
1725 if (!EVP_PKEY_missing_parameters(ktmp
))
1728 EVP_PKEY_free(ktmp
);
1733 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS
,
1734 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN
);
1738 /* first, populate the other certs */
1739 for (j
= i
- 1; j
>= 0; j
--) {
1740 ktmp2
= X509_get_pubkey(sk_X509_value(chain
, j
));
1741 EVP_PKEY_copy_parameters(ktmp2
, ktmp
);
1742 EVP_PKEY_free(ktmp2
);
1746 EVP_PKEY_copy_parameters(pkey
, ktmp
);
1747 EVP_PKEY_free(ktmp
);
1751 int X509_STORE_CTX_get_ex_new_index(long argl
, void *argp
,
1752 CRYPTO_EX_new
*new_func
,
1753 CRYPTO_EX_dup
*dup_func
,
1754 CRYPTO_EX_free
*free_func
)
1757 * This function is (usually) called only once, by
1758 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
1760 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX
, argl
, argp
,
1761 new_func
, dup_func
, free_func
);
1764 int X509_STORE_CTX_set_ex_data(X509_STORE_CTX
*ctx
, int idx
, void *data
)
1766 return CRYPTO_set_ex_data(&ctx
->ex_data
, idx
, data
);
1769 void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX
*ctx
, int idx
)
1771 return CRYPTO_get_ex_data(&ctx
->ex_data
, idx
);
1774 int X509_STORE_CTX_get_error(X509_STORE_CTX
*ctx
)
1779 void X509_STORE_CTX_set_error(X509_STORE_CTX
*ctx
, int err
)
1784 int X509_STORE_CTX_get_error_depth(X509_STORE_CTX
*ctx
)
1786 return ctx
->error_depth
;
1789 X509
*X509_STORE_CTX_get_current_cert(X509_STORE_CTX
*ctx
)
1791 return ctx
->current_cert
;
1794 STACK_OF(X509
) *X509_STORE_CTX_get_chain(X509_STORE_CTX
*ctx
)
1799 STACK_OF(X509
) *X509_STORE_CTX_get1_chain(X509_STORE_CTX
*ctx
)
1803 STACK_OF(X509
) *chain
;
1804 if (!ctx
->chain
|| !(chain
= sk_X509_dup(ctx
->chain
)))
1806 for (i
= 0; i
< sk_X509_num(chain
); i
++) {
1807 x
= sk_X509_value(chain
, i
);
1808 CRYPTO_add(&x
->references
, 1, CRYPTO_LOCK_X509
);
1813 X509
*X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX
*ctx
)
1815 return ctx
->current_issuer
;
1818 X509_CRL
*X509_STORE_CTX_get0_current_crl(X509_STORE_CTX
*ctx
)
1820 return ctx
->current_crl
;
1823 X509_STORE_CTX
*X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX
*ctx
)
1828 void X509_STORE_CTX_set_cert(X509_STORE_CTX
*ctx
, X509
*x
)
1833 void X509_STORE_CTX_set_chain(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
1835 ctx
->untrusted
= sk
;
1838 void X509_STORE_CTX_set0_crls(X509_STORE_CTX
*ctx
, STACK_OF(X509_CRL
) *sk
)
1843 int X509_STORE_CTX_set_purpose(X509_STORE_CTX
*ctx
, int purpose
)
1845 return X509_STORE_CTX_purpose_inherit(ctx
, 0, purpose
, 0);
1848 int X509_STORE_CTX_set_trust(X509_STORE_CTX
*ctx
, int trust
)
1850 return X509_STORE_CTX_purpose_inherit(ctx
, 0, 0, trust
);
1854 * This function is used to set the X509_STORE_CTX purpose and trust values.
1855 * This is intended to be used when another structure has its own trust and
1856 * purpose values which (if set) will be inherited by the ctx. If they aren't
1857 * set then we will usually have a default purpose in mind which should then
1858 * be used to set the trust value. An example of this is SSL use: an SSL
1859 * structure will have its own purpose and trust settings which the
1860 * application can set: if they aren't set then we use the default of SSL
1864 int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX
*ctx
, int def_purpose
,
1865 int purpose
, int trust
)
1868 /* If purpose not set use default */
1870 purpose
= def_purpose
;
1871 /* If we have a purpose then check it is valid */
1874 idx
= X509_PURPOSE_get_by_id(purpose
);
1876 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT
,
1877 X509_R_UNKNOWN_PURPOSE_ID
);
1880 ptmp
= X509_PURPOSE_get0(idx
);
1881 if (ptmp
->trust
== X509_TRUST_DEFAULT
) {
1882 idx
= X509_PURPOSE_get_by_id(def_purpose
);
1884 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT
,
1885 X509_R_UNKNOWN_PURPOSE_ID
);
1888 ptmp
= X509_PURPOSE_get0(idx
);
1890 /* If trust not set then get from purpose default */
1892 trust
= ptmp
->trust
;
1895 idx
= X509_TRUST_get_by_id(trust
);
1897 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT
,
1898 X509_R_UNKNOWN_TRUST_ID
);
1903 if (purpose
&& !ctx
->param
->purpose
)
1904 ctx
->param
->purpose
= purpose
;
1905 if (trust
&& !ctx
->param
->trust
)
1906 ctx
->param
->trust
= trust
;
1910 X509_STORE_CTX
*X509_STORE_CTX_new(void)
1912 X509_STORE_CTX
*ctx
;
1913 ctx
= (X509_STORE_CTX
*)OPENSSL_malloc(sizeof(X509_STORE_CTX
));
1915 X509err(X509_F_X509_STORE_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
1918 memset(ctx
, 0, sizeof(X509_STORE_CTX
));
1922 void X509_STORE_CTX_free(X509_STORE_CTX
*ctx
)
1924 X509_STORE_CTX_cleanup(ctx
);
1928 int X509_STORE_CTX_init(X509_STORE_CTX
*ctx
, X509_STORE
*store
, X509
*x509
,
1929 STACK_OF(X509
) *chain
)
1933 ctx
->current_method
= 0;
1935 ctx
->untrusted
= chain
;
1937 ctx
->last_untrusted
= 0;
1938 ctx
->other_ctx
= NULL
;
1942 ctx
->explicit_policy
= 0;
1943 ctx
->error_depth
= 0;
1944 ctx
->current_cert
= NULL
;
1945 ctx
->current_issuer
= NULL
;
1946 ctx
->current_crl
= NULL
;
1947 ctx
->current_crl_score
= 0;
1948 ctx
->current_reasons
= 0;
1952 ctx
->param
= X509_VERIFY_PARAM_new();
1955 X509err(X509_F_X509_STORE_CTX_INIT
, ERR_R_MALLOC_FAILURE
);
1960 * Inherit callbacks and flags from X509_STORE if not set use defaults.
1964 ret
= X509_VERIFY_PARAM_inherit(ctx
->param
, store
->param
);
1966 ctx
->param
->inh_flags
|= X509_VP_FLAG_DEFAULT
| X509_VP_FLAG_ONCE
;
1969 ctx
->verify_cb
= store
->verify_cb
;
1970 ctx
->cleanup
= store
->cleanup
;
1975 ret
= X509_VERIFY_PARAM_inherit(ctx
->param
,
1976 X509_VERIFY_PARAM_lookup("default"));
1979 X509err(X509_F_X509_STORE_CTX_INIT
, ERR_R_MALLOC_FAILURE
);
1983 if (store
&& store
->check_issued
)
1984 ctx
->check_issued
= store
->check_issued
;
1986 ctx
->check_issued
= check_issued
;
1988 if (store
&& store
->get_issuer
)
1989 ctx
->get_issuer
= store
->get_issuer
;
1991 ctx
->get_issuer
= X509_STORE_CTX_get1_issuer
;
1993 if (store
&& store
->verify_cb
)
1994 ctx
->verify_cb
= store
->verify_cb
;
1996 ctx
->verify_cb
= null_callback
;
1998 if (store
&& store
->verify
)
1999 ctx
->verify
= store
->verify
;
2001 ctx
->verify
= internal_verify
;
2003 if (store
&& store
->check_revocation
)
2004 ctx
->check_revocation
= store
->check_revocation
;
2006 ctx
->check_revocation
= check_revocation
;
2008 if (store
&& store
->get_crl
)
2009 ctx
->get_crl
= store
->get_crl
;
2011 ctx
->get_crl
= NULL
;
2013 if (store
&& store
->check_crl
)
2014 ctx
->check_crl
= store
->check_crl
;
2016 ctx
->check_crl
= check_crl
;
2018 if (store
&& store
->cert_crl
)
2019 ctx
->cert_crl
= store
->cert_crl
;
2021 ctx
->cert_crl
= cert_crl
;
2023 if (store
&& store
->lookup_certs
)
2024 ctx
->lookup_certs
= store
->lookup_certs
;
2026 ctx
->lookup_certs
= X509_STORE_get1_certs
;
2028 if (store
&& store
->lookup_crls
)
2029 ctx
->lookup_crls
= store
->lookup_crls
;
2031 ctx
->lookup_crls
= X509_STORE_get1_crls
;
2033 ctx
->check_policy
= check_policy
;
2036 * This memset() can't make any sense anyway, so it's removed. As
2037 * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
2038 * corresponding "new" here and remove this bogus initialisation.
2040 /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
2041 if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX
, ctx
,
2044 X509err(X509_F_X509_STORE_CTX_INIT
, ERR_R_MALLOC_FAILURE
);
2051 * Set alternative lookup method: just a STACK of trusted certificates. This
2052 * avoids X509_STORE nastiness where it isn't needed.
2055 void X509_STORE_CTX_trusted_stack(X509_STORE_CTX
*ctx
, STACK_OF(X509
) *sk
)
2057 ctx
->other_ctx
= sk
;
2058 ctx
->get_issuer
= get_issuer_sk
;
2061 void X509_STORE_CTX_cleanup(X509_STORE_CTX
*ctx
)
2065 if (ctx
->param
!= NULL
) {
2066 if (ctx
->parent
== NULL
)
2067 X509_VERIFY_PARAM_free(ctx
->param
);
2070 if (ctx
->tree
!= NULL
) {
2071 X509_policy_tree_free(ctx
->tree
);
2074 if (ctx
->chain
!= NULL
) {
2075 sk_X509_pop_free(ctx
->chain
, X509_free
);
2078 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX
, ctx
, &(ctx
->ex_data
));
2079 memset(&ctx
->ex_data
, 0, sizeof(CRYPTO_EX_DATA
));
2082 void X509_STORE_CTX_set_depth(X509_STORE_CTX
*ctx
, int depth
)
2084 X509_VERIFY_PARAM_set_depth(ctx
->param
, depth
);
2087 void X509_STORE_CTX_set_flags(X509_STORE_CTX
*ctx
, unsigned long flags
)
2089 X509_VERIFY_PARAM_set_flags(ctx
->param
, flags
);
2092 void X509_STORE_CTX_set_time(X509_STORE_CTX
*ctx
, unsigned long flags
,
2095 X509_VERIFY_PARAM_set_time(ctx
->param
, t
);
2098 void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX
*ctx
,
2099 int (*verify_cb
) (int, X509_STORE_CTX
*))
2101 ctx
->verify_cb
= verify_cb
;
2104 X509_POLICY_TREE
*X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX
*ctx
)
2109 int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX
*ctx
)
2111 return ctx
->explicit_policy
;
2114 int X509_STORE_CTX_set_default(X509_STORE_CTX
*ctx
, const char *name
)
2116 const X509_VERIFY_PARAM
*param
;
2117 param
= X509_VERIFY_PARAM_lookup(name
);
2120 return X509_VERIFY_PARAM_inherit(ctx
->param
, param
);
2123 X509_VERIFY_PARAM
*X509_STORE_CTX_get0_param(X509_STORE_CTX
*ctx
)
2128 void X509_STORE_CTX_set0_param(X509_STORE_CTX
*ctx
, X509_VERIFY_PARAM
*param
)
2131 X509_VERIFY_PARAM_free(ctx
->param
);
2135 IMPLEMENT_STACK_OF(X509
)
2137 IMPLEMENT_ASN1_SET_OF(X509
)
2139 IMPLEMENT_STACK_OF(X509_NAME
)
2141 IMPLEMENT_STACK_OF(X509_ATTRIBUTE
)
2143 IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE
)