summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509/x509_vfy.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/x509/x509_vfy.c')
-rw-r--r--src/lib/libcrypto/x509/x509_vfy.c2194
1 files changed, 0 insertions, 2194 deletions
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c
deleted file mode 100644
index c383fda4f2..0000000000
--- a/src/lib/libcrypto/x509/x509_vfy.c
+++ /dev/null
@@ -1,2194 +0,0 @@
1/* $OpenBSD: x509_vfy.c,v 1.40 2015/02/11 02:17:59 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
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.
8 *
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).
15 *
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.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
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)"
40 *
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
51 * SUCH DAMAGE.
52 *
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.]
57 */
58
59#include <errno.h>
60#include <stdio.h>
61#include <string.h>
62#include <time.h>
63#include <unistd.h>
64
65#include <openssl/opensslconf.h>
66
67#include <openssl/asn1.h>
68#include <openssl/buffer.h>
69#include <openssl/crypto.h>
70#include <openssl/err.h>
71#include <openssl/evp.h>
72#include <openssl/lhash.h>
73#include <openssl/objects.h>
74#include <openssl/x509.h>
75#include <openssl/x509v3.h>
76#include "x509_lcl.h"
77
78/* CRL score values */
79
80/* No unhandled critical extensions */
81
82#define CRL_SCORE_NOCRITICAL 0x100
83
84/* certificate is within CRL scope */
85
86#define CRL_SCORE_SCOPE 0x080
87
88/* CRL times valid */
89
90#define CRL_SCORE_TIME 0x040
91
92/* Issuer name matches certificate */
93
94#define CRL_SCORE_ISSUER_NAME 0x020
95
96/* If this score or above CRL is probably valid */
97
98#define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
99
100/* CRL issuer is certificate issuer */
101
102#define CRL_SCORE_ISSUER_CERT 0x018
103
104/* CRL issuer is on certificate path */
105
106#define CRL_SCORE_SAME_PATH 0x008
107
108/* CRL issuer matches CRL AKID */
109
110#define CRL_SCORE_AKID 0x004
111
112/* Have a delta CRL with valid times */
113
114#define CRL_SCORE_TIME_DELTA 0x002
115
116static int null_callback(int ok, X509_STORE_CTX *e);
117static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
118static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
119static int check_chain_extensions(X509_STORE_CTX *ctx);
120static int check_name_constraints(X509_STORE_CTX *ctx);
121static int check_trust(X509_STORE_CTX *ctx);
122static int check_revocation(X509_STORE_CTX *ctx);
123static int check_cert(X509_STORE_CTX *ctx);
124static int check_policy(X509_STORE_CTX *ctx);
125
126static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
127 unsigned int *preasons, X509_CRL *crl, X509 *x);
128static int get_crl_delta(X509_STORE_CTX *ctx,
129 X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
130static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pcrl_score,
131 X509_CRL *base, STACK_OF(X509_CRL) *crls);
132static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
133 int *pcrl_score);
134static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
135 unsigned int *preasons);
136static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
137static int check_crl_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *cert_path,
138 STACK_OF(X509) *crl_path);
139
140static int internal_verify(X509_STORE_CTX *ctx);
141
142static int
143null_callback(int ok, X509_STORE_CTX *e)
144{
145 return ok;
146}
147
148#if 0
149static int
150x509_subject_cmp(X509 **a, X509 **b)
151{
152 return X509_subject_name_cmp(*a, *b);
153}
154#endif
155
156int
157X509_verify_cert(X509_STORE_CTX *ctx)
158{
159 X509 *x, *xtmp, *chain_ss = NULL;
160 int bad_chain = 0;
161 X509_VERIFY_PARAM *param = ctx->param;
162 int depth, i, ok = 0;
163 int num;
164 int (*cb)(int xok, X509_STORE_CTX *xctx);
165 STACK_OF(X509) *sktmp = NULL;
166
167 if (ctx->cert == NULL) {
168 X509err(X509_F_X509_VERIFY_CERT,
169 X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
170 return -1;
171 }
172
173 cb = ctx->verify_cb;
174
175 /* first we make sure the chain we are going to build is
176 * present and that the first entry is in place */
177 if (ctx->chain == NULL) {
178 if (((ctx->chain = sk_X509_new_null()) == NULL) ||
179 (!sk_X509_push(ctx->chain, ctx->cert))) {
180 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
181 goto end;
182 }
183 CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
184 ctx->last_untrusted = 1;
185 }
186
187 /* We use a temporary STACK so we can chop and hack at it */
188 if (ctx->untrusted != NULL &&
189 (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
190 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
191 goto end;
192 }
193
194 num = sk_X509_num(ctx->chain);
195 x = sk_X509_value(ctx->chain, num - 1);
196 depth = param->depth;
197
198 for (;;) {
199 /* If we have enough, we break */
200 if (depth < num)
201 break; /* FIXME: If this happens, we should take
202 * note of it and, if appropriate, use the
203 * X509_V_ERR_CERT_CHAIN_TOO_LONG error
204 * code later.
205 */
206
207 /* If we are self signed, we break */
208 if (ctx->check_issued(ctx, x, x))
209 break;
210
211 /* If we were passed a cert chain, use it first */
212 if (ctx->untrusted != NULL) {
213 xtmp = find_issuer(ctx, sktmp, x);
214 if (xtmp != NULL) {
215 if (!sk_X509_push(ctx->chain, xtmp)) {
216 X509err(X509_F_X509_VERIFY_CERT,
217 ERR_R_MALLOC_FAILURE);
218 goto end;
219 }
220 CRYPTO_add(&xtmp->references, 1,
221 CRYPTO_LOCK_X509);
222 (void)sk_X509_delete_ptr(sktmp, xtmp);
223 ctx->last_untrusted++;
224 x = xtmp;
225 num++;
226 /* reparse the full chain for
227 * the next one */
228 continue;
229 }
230 }
231 break;
232 }
233 sk_X509_free(sktmp);
234 sktmp = NULL;
235
236 /* at this point, chain should contain a list of untrusted
237 * certificates. We now need to add at least one trusted one,
238 * if possible, otherwise we complain. */
239
240 /* Examine last certificate in chain and see if it
241 * is self signed.
242 */
243
244 i = sk_X509_num(ctx->chain);
245 x = sk_X509_value(ctx->chain, i - 1);
246 if (ctx->check_issued(ctx, x, x)) {
247 /* we have a self signed certificate */
248 if (sk_X509_num(ctx->chain) == 1) {
249 /* We have a single self signed certificate: see if
250 * we can find it in the store. We must have an exact
251 * match to avoid possible impersonation.
252 */
253 ok = ctx->get_issuer(&xtmp, ctx, x);
254 if ((ok <= 0) || X509_cmp(x, xtmp)) {
255 ctx->error =
256 X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
257 ctx->current_cert = x;
258 ctx->error_depth = i - 1;
259 if (ok == 1)
260 X509_free(xtmp);
261 bad_chain = 1;
262 ok = cb(0, ctx);
263 if (!ok)
264 goto end;
265 } else {
266 /* We have a match: replace certificate with store version
267 * so we get any trust settings.
268 */
269 X509_free(x);
270 x = xtmp;
271 (void)sk_X509_set(ctx->chain, i - 1, x);
272 ctx->last_untrusted = 0;
273 }
274 } else {
275 /* extract and save self signed certificate for later use */
276 chain_ss = sk_X509_pop(ctx->chain);
277 ctx->last_untrusted--;
278 num--;
279 x = sk_X509_value(ctx->chain, num - 1);
280 }
281 }
282
283 /* We now lookup certs from the certificate store */
284 for (;;) {
285 /* If we have enough, we break */
286 if (depth < num)
287 break;
288
289 /* If we are self signed, we break */
290 if (ctx->check_issued(ctx, x, x))
291 break;
292
293 ok = ctx->get_issuer(&xtmp, ctx, x);
294 if (ok < 0)
295 return ok;
296 if (ok == 0)
297 break;
298
299 x = xtmp;
300 if (!sk_X509_push(ctx->chain, x)) {
301 X509_free(xtmp);
302 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
303 return 0;
304 }
305 num++;
306 }
307
308 /* we now have our chain, lets check it... */
309
310 /* Is last certificate looked up self signed? */
311 if (!ctx->check_issued(ctx, x, x)) {
312 if ((chain_ss == NULL) ||
313 !ctx->check_issued(ctx, x, chain_ss)) {
314 if (ctx->last_untrusted >= num)
315 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
316 else
317 ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
318 ctx->current_cert = x;
319 } else {
320
321 if (!sk_X509_push(ctx->chain, chain_ss)) {
322 X509_free(chain_ss);
323 X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
324 return 0;
325 }
326 num++;
327 ctx->last_untrusted = num;
328 ctx->current_cert = chain_ss;
329 ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
330 chain_ss = NULL;
331 }
332
333 ctx->error_depth = num - 1;
334 bad_chain = 1;
335 ok = cb(0, ctx);
336 if (!ok)
337 goto end;
338 }
339
340 /* We have the chain complete: now we need to check its purpose */
341 ok = check_chain_extensions(ctx);
342
343 if (!ok)
344 goto end;
345
346 /* Check name constraints */
347
348 ok = check_name_constraints(ctx);
349
350 if (!ok)
351 goto end;
352
353 /* The chain extensions are OK: check trust */
354
355 if (param->trust > 0)
356 ok = check_trust(ctx);
357
358 if (!ok)
359 goto end;
360
361 /* We may as well copy down any DSA parameters that are required */
362 X509_get_pubkey_parameters(NULL, ctx->chain);
363
364 /* Check revocation status: we do this after copying parameters
365 * because they may be needed for CRL signature verification.
366 */
367
368 ok = ctx->check_revocation(ctx);
369 if (!ok)
370 goto end;
371
372 /* At this point, we have a chain and need to verify it */
373 if (ctx->verify != NULL)
374 ok = ctx->verify(ctx);
375 else
376 ok = internal_verify(ctx);
377 if (!ok)
378 goto end;
379
380 /* If we get this far evaluate policies */
381 if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
382 ok = ctx->check_policy(ctx);
383 if (!ok)
384 goto end;
385 if (0) {
386end:
387 X509_get_pubkey_parameters(NULL, ctx->chain);
388 }
389 if (sktmp != NULL)
390 sk_X509_free(sktmp);
391 if (chain_ss != NULL)
392 X509_free(chain_ss);
393 return ok;
394}
395
396
397/* Given a STACK_OF(X509) find the issuer of cert (if any)
398 */
399
400static X509 *
401find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
402{
403 int i;
404 X509 *issuer, *rv = NULL;
405
406 for (i = 0; i < sk_X509_num(sk); i++) {
407 issuer = sk_X509_value(sk, i);
408 if (ctx->check_issued(ctx, x, issuer)) {
409 rv = issuer;
410 if (x509_check_cert_time(ctx, rv, 1))
411 break;
412 }
413 }
414 return rv;
415}
416
417/* Given a possible certificate and issuer check them */
418
419static int
420check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
421{
422 int ret;
423
424 ret = X509_check_issued(issuer, x);
425 if (ret == X509_V_OK)
426 return 1;
427 /* If we haven't asked for issuer errors don't set ctx */
428 if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
429 return 0;
430
431 ctx->error = ret;
432 ctx->current_cert = x;
433 ctx->current_issuer = issuer;
434 return ctx->verify_cb(0, ctx);
435}
436
437/* Alternative lookup method: look from a STACK stored in other_ctx */
438
439static int
440get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
441{
442 *issuer = find_issuer(ctx, ctx->other_ctx, x);
443 if (*issuer) {
444 CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
445 return 1;
446 } else
447 return 0;
448}
449
450/* Check a certificate chains extensions for consistency
451 * with the supplied purpose
452 */
453
454static int
455check_chain_extensions(X509_STORE_CTX *ctx)
456{
457#ifdef OPENSSL_NO_CHAIN_VERIFY
458 return 1;
459#else
460 int i, ok = 0, must_be_ca, plen = 0;
461 X509 *x;
462 int (*cb)(int xok, X509_STORE_CTX *xctx);
463 int proxy_path_length = 0;
464 int purpose;
465 int allow_proxy_certs;
466
467 cb = ctx->verify_cb;
468
469 /* must_be_ca can have 1 of 3 values:
470 -1: we accept both CA and non-CA certificates, to allow direct
471 use of self-signed certificates (which are marked as CA).
472 0: we only accept non-CA certificates. This is currently not
473 used, but the possibility is present for future extensions.
474 1: we only accept CA certificates. This is currently used for
475 all certificates in the chain except the leaf certificate.
476 */
477 must_be_ca = -1;
478
479 /* CRL path validation */
480 if (ctx->parent) {
481 allow_proxy_certs = 0;
482 purpose = X509_PURPOSE_CRL_SIGN;
483 } else {
484 allow_proxy_certs =
485 !!(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
486#if 0
487 /* A hack to keep people who don't want to modify their
488 software happy */
489 if (issetugid() == 0 && getenv("OPENSSL_ALLOW_PROXY_CERTS"))
490 allow_proxy_certs = 1;
491#endif
492 purpose = ctx->param->purpose;
493 }
494
495 /* Check all untrusted certificates */
496 for (i = 0; i < ctx->last_untrusted; i++) {
497 int ret;
498 x = sk_X509_value(ctx->chain, i);
499 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
500 (x->ex_flags & EXFLAG_CRITICAL)) {
501 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
502 ctx->error_depth = i;
503 ctx->current_cert = x;
504 ok = cb(0, ctx);
505 if (!ok)
506 goto end;
507 }
508 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
509 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
510 ctx->error_depth = i;
511 ctx->current_cert = x;
512 ok = cb(0, ctx);
513 if (!ok)
514 goto end;
515 }
516 ret = X509_check_ca(x);
517 switch (must_be_ca) {
518 case -1:
519 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
520 (ret != 1) && (ret != 0)) {
521 ret = 0;
522 ctx->error = X509_V_ERR_INVALID_CA;
523 } else
524 ret = 1;
525 break;
526 case 0:
527 if (ret != 0) {
528 ret = 0;
529 ctx->error = X509_V_ERR_INVALID_NON_CA;
530 } else
531 ret = 1;
532 break;
533 default:
534 if ((ret == 0) ||
535 ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
536 (ret != 1))) {
537 ret = 0;
538 ctx->error = X509_V_ERR_INVALID_CA;
539 } else
540 ret = 1;
541 break;
542 }
543 if (ret == 0) {
544 ctx->error_depth = i;
545 ctx->current_cert = x;
546 ok = cb(0, ctx);
547 if (!ok)
548 goto end;
549 }
550 if (ctx->param->purpose > 0) {
551 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
552 if ((ret == 0) ||
553 ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
554 (ret != 1))) {
555 ctx->error = X509_V_ERR_INVALID_PURPOSE;
556 ctx->error_depth = i;
557 ctx->current_cert = x;
558 ok = cb(0, ctx);
559 if (!ok)
560 goto end;
561 }
562 }
563 /* Check pathlen if not self issued */
564 if ((i > 1) && !(x->ex_flags & EXFLAG_SI) &&
565 (x->ex_pathlen != -1) &&
566 (plen > (x->ex_pathlen + proxy_path_length + 1))) {
567 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
568 ctx->error_depth = i;
569 ctx->current_cert = x;
570 ok = cb(0, ctx);
571 if (!ok)
572 goto end;
573 }
574 /* Increment path length if not self issued */
575 if (!(x->ex_flags & EXFLAG_SI))
576 plen++;
577 /* If this certificate is a proxy certificate, the next
578 certificate must be another proxy certificate or a EE
579 certificate. If not, the next certificate must be a
580 CA certificate. */
581 if (x->ex_flags & EXFLAG_PROXY) {
582 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
583 ctx->error =
584 X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
585 ctx->error_depth = i;
586 ctx->current_cert = x;
587 ok = cb(0, ctx);
588 if (!ok)
589 goto end;
590 }
591 proxy_path_length++;
592 must_be_ca = 0;
593 } else
594 must_be_ca = 1;
595 }
596 ok = 1;
597
598end:
599 return ok;
600#endif
601}
602
603static int
604check_name_constraints(X509_STORE_CTX *ctx)
605{
606 X509 *x;
607 int i, j, rv;
608
609 /* Check name constraints for all certificates */
610 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
611 x = sk_X509_value(ctx->chain, i);
612 /* Ignore self issued certs unless last in chain */
613 if (i && (x->ex_flags & EXFLAG_SI))
614 continue;
615 /* Check against constraints for all certificates higher in
616 * chain including trust anchor. Trust anchor not strictly
617 * speaking needed but if it includes constraints it is to be
618 * assumed it expects them to be obeyed.
619 */
620 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
621 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
622 if (nc) {
623 rv = NAME_CONSTRAINTS_check(x, nc);
624 if (rv != X509_V_OK) {
625 ctx->error = rv;
626 ctx->error_depth = i;
627 ctx->current_cert = x;
628 if (!ctx->verify_cb(0, ctx))
629 return 0;
630 }
631 }
632 }
633 }
634 return 1;
635}
636
637static int
638check_trust(X509_STORE_CTX *ctx)
639{
640#ifdef OPENSSL_NO_CHAIN_VERIFY
641 return 1;
642#else
643 int i, ok;
644 X509 *x;
645 int (*cb)(int xok, X509_STORE_CTX *xctx);
646
647 cb = ctx->verify_cb;
648 /* For now just check the last certificate in the chain */
649 i = sk_X509_num(ctx->chain) - 1;
650 x = sk_X509_value(ctx->chain, i);
651 ok = X509_check_trust(x, ctx->param->trust, 0);
652 if (ok == X509_TRUST_TRUSTED)
653 return 1;
654 ctx->error_depth = i;
655 ctx->current_cert = x;
656 if (ok == X509_TRUST_REJECTED)
657 ctx->error = X509_V_ERR_CERT_REJECTED;
658 else
659 ctx->error = X509_V_ERR_CERT_UNTRUSTED;
660 ok = cb(0, ctx);
661 return ok;
662#endif
663}
664
665static int
666check_revocation(X509_STORE_CTX *ctx)
667{
668 int i, last, ok;
669
670 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
671 return 1;
672 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
673 last = sk_X509_num(ctx->chain) - 1;
674 else {
675 /* If checking CRL paths this isn't the EE certificate */
676 if (ctx->parent)
677 return 1;
678 last = 0;
679 }
680 for (i = 0; i <= last; i++) {
681 ctx->error_depth = i;
682 ok = check_cert(ctx);
683 if (!ok)
684 return ok;
685 }
686 return 1;
687}
688
689static int
690check_cert(X509_STORE_CTX *ctx)
691{
692 X509_CRL *crl = NULL, *dcrl = NULL;
693 X509 *x;
694 int ok, cnum;
695 unsigned int last_reasons;
696
697 cnum = ctx->error_depth;
698 x = sk_X509_value(ctx->chain, cnum);
699 ctx->current_cert = x;
700 ctx->current_issuer = NULL;
701 ctx->current_crl_score = 0;
702 ctx->current_reasons = 0;
703 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
704 last_reasons = ctx->current_reasons;
705 /* Try to retrieve relevant CRL */
706 if (ctx->get_crl)
707 ok = ctx->get_crl(ctx, &crl, x);
708 else
709 ok = get_crl_delta(ctx, &crl, &dcrl, x);
710 /* If error looking up CRL, nothing we can do except
711 * notify callback
712 */
713 if (!ok) {
714 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
715 ok = ctx->verify_cb(0, ctx);
716 goto err;
717 }
718 ctx->current_crl = crl;
719 ok = ctx->check_crl(ctx, crl);
720 if (!ok)
721 goto err;
722
723 if (dcrl) {
724 ok = ctx->check_crl(ctx, dcrl);
725 if (!ok)
726 goto err;
727 ok = ctx->cert_crl(ctx, dcrl, x);
728 if (!ok)
729 goto err;
730 } else
731 ok = 1;
732
733 /* Don't look in full CRL if delta reason is removefromCRL */
734 if (ok != 2) {
735 ok = ctx->cert_crl(ctx, crl, x);
736 if (!ok)
737 goto err;
738 }
739
740 ctx->current_crl = NULL;
741 X509_CRL_free(crl);
742 X509_CRL_free(dcrl);
743 crl = NULL;
744 dcrl = NULL;
745 /* If reasons not updated we wont get anywhere by
746 * another iteration, so exit loop.
747 */
748 if (last_reasons == ctx->current_reasons) {
749 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
750 ok = ctx->verify_cb(0, ctx);
751 goto err;
752 }
753 }
754
755err:
756 ctx->current_crl = NULL;
757 X509_CRL_free(crl);
758 X509_CRL_free(dcrl);
759 return ok;
760}
761
762/* Check CRL times against values in X509_STORE_CTX */
763
764static int
765check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
766{
767 time_t *ptime;
768 int i;
769
770 if (notify)
771 ctx->current_crl = crl;
772 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
773 ptime = &ctx->param->check_time;
774 else
775 ptime = NULL;
776
777 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
778 if (i == 0) {
779 if (!notify)
780 return 0;
781 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
782 if (!ctx->verify_cb(0, ctx))
783 return 0;
784 }
785
786 if (i > 0) {
787 if (!notify)
788 return 0;
789 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
790 if (!ctx->verify_cb(0, ctx))
791 return 0;
792 }
793
794 if (X509_CRL_get_nextUpdate(crl)) {
795 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
796
797 if (i == 0) {
798 if (!notify)
799 return 0;
800 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
801 if (!ctx->verify_cb(0, ctx))
802 return 0;
803 }
804 /* Ignore expiry of base CRL is delta is valid */
805 if ((i < 0) &&
806 !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
807 if (!notify)
808 return 0;
809 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
810 if (!ctx->verify_cb(0, ctx))
811 return 0;
812 }
813 }
814
815 if (notify)
816 ctx->current_crl = NULL;
817
818 return 1;
819}
820
821static int
822get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
823 X509 **pissuer, int *pscore, unsigned int *preasons,
824 STACK_OF(X509_CRL) *crls)
825{
826 int i, crl_score, best_score = *pscore;
827 unsigned int reasons, best_reasons = 0;
828 X509 *x = ctx->current_cert;
829 X509_CRL *crl, *best_crl = NULL;
830 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
831
832 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
833 crl = sk_X509_CRL_value(crls, i);
834 reasons = *preasons;
835 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
836
837 if (crl_score > best_score) {
838 best_crl = crl;
839 best_crl_issuer = crl_issuer;
840 best_score = crl_score;
841 best_reasons = reasons;
842 }
843 }
844
845 if (best_crl) {
846 if (*pcrl)
847 X509_CRL_free(*pcrl);
848 *pcrl = best_crl;
849 *pissuer = best_crl_issuer;
850 *pscore = best_score;
851 *preasons = best_reasons;
852 CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
853 if (*pdcrl) {
854 X509_CRL_free(*pdcrl);
855 *pdcrl = NULL;
856 }
857 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
858 }
859
860 if (best_score >= CRL_SCORE_VALID)
861 return 1;
862
863 return 0;
864}
865
866/* Compare two CRL extensions for delta checking purposes. They should be
867 * both present or both absent. If both present all fields must be identical.
868 */
869
870static int
871crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
872{
873 ASN1_OCTET_STRING *exta, *extb;
874 int i;
875
876 i = X509_CRL_get_ext_by_NID(a, nid, -1);
877 if (i >= 0) {
878 /* Can't have multiple occurrences */
879 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
880 return 0;
881 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
882 } else
883 exta = NULL;
884
885 i = X509_CRL_get_ext_by_NID(b, nid, -1);
886
887 if (i >= 0) {
888 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
889 return 0;
890 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
891 } else
892 extb = NULL;
893
894 if (!exta && !extb)
895 return 1;
896
897 if (!exta || !extb)
898 return 0;
899
900 if (ASN1_OCTET_STRING_cmp(exta, extb))
901 return 0;
902
903 return 1;
904}
905
906/* See if a base and delta are compatible */
907
908static int
909check_delta_base(X509_CRL *delta, X509_CRL *base)
910{
911 /* Delta CRL must be a delta */
912 if (!delta->base_crl_number)
913 return 0;
914 /* Base must have a CRL number */
915 if (!base->crl_number)
916 return 0;
917 /* Issuer names must match */
918 if (X509_NAME_cmp(X509_CRL_get_issuer(base),
919 X509_CRL_get_issuer(delta)))
920 return 0;
921 /* AKID and IDP must match */
922 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
923 return 0;
924 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
925 return 0;
926 /* Delta CRL base number must not exceed Full CRL number. */
927 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
928 return 0;
929 /* Delta CRL number must exceed full CRL number */
930 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
931 return 1;
932 return 0;
933}
934
935/* For a given base CRL find a delta... maybe extend to delta scoring
936 * or retrieve a chain of deltas...
937 */
938
939static void
940get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore, X509_CRL *base,
941 STACK_OF(X509_CRL) *crls)
942{
943 X509_CRL *delta;
944 int i;
945
946 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
947 return;
948 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
949 return;
950 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
951 delta = sk_X509_CRL_value(crls, i);
952 if (check_delta_base(delta, base)) {
953 if (check_crl_time(ctx, delta, 0))
954 *pscore |= CRL_SCORE_TIME_DELTA;
955 CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
956 *dcrl = delta;
957 return;
958 }
959 }
960 *dcrl = NULL;
961}
962
963/* For a given CRL return how suitable it is for the supplied certificate 'x'.
964 * The return value is a mask of several criteria.
965 * If the issuer is not the certificate issuer this is returned in *pissuer.
966 * The reasons mask is also used to determine if the CRL is suitable: if
967 * no new reasons the CRL is rejected, otherwise reasons is updated.
968 */
969
970static int
971get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, unsigned int *preasons,
972 X509_CRL *crl, X509 *x)
973{
974 int crl_score = 0;
975 unsigned int tmp_reasons = *preasons, crl_reasons;
976
977 /* First see if we can reject CRL straight away */
978
979 /* Invalid IDP cannot be processed */
980 if (crl->idp_flags & IDP_INVALID)
981 return 0;
982 /* Reason codes or indirect CRLs need extended CRL support */
983 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
984 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
985 return 0;
986 } else if (crl->idp_flags & IDP_REASONS) {
987 /* If no new reasons reject */
988 if (!(crl->idp_reasons & ~tmp_reasons))
989 return 0;
990 }
991 /* Don't process deltas at this stage */
992 else if (crl->base_crl_number)
993 return 0;
994 /* If issuer name doesn't match certificate need indirect CRL */
995 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
996 if (!(crl->idp_flags & IDP_INDIRECT))
997 return 0;
998 } else
999 crl_score |= CRL_SCORE_ISSUER_NAME;
1000
1001 if (!(crl->flags & EXFLAG_CRITICAL))
1002 crl_score |= CRL_SCORE_NOCRITICAL;
1003
1004 /* Check expiry */
1005 if (check_crl_time(ctx, crl, 0))
1006 crl_score |= CRL_SCORE_TIME;
1007
1008 /* Check authority key ID and locate certificate issuer */
1009 crl_akid_check(ctx, crl, pissuer, &crl_score);
1010
1011 /* If we can't locate certificate issuer at this point forget it */
1012
1013 if (!(crl_score & CRL_SCORE_AKID))
1014 return 0;
1015
1016 /* Check cert for matching CRL distribution points */
1017
1018 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1019 /* If no new reasons reject */
1020 if (!(crl_reasons & ~tmp_reasons))
1021 return 0;
1022 tmp_reasons |= crl_reasons;
1023 crl_score |= CRL_SCORE_SCOPE;
1024 }
1025
1026 *preasons = tmp_reasons;
1027
1028 return crl_score;
1029}
1030
1031static void
1032crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
1033 int *pcrl_score)
1034{
1035 X509 *crl_issuer = NULL;
1036 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1037 int cidx = ctx->error_depth;
1038 int i;
1039
1040 if (cidx != sk_X509_num(ctx->chain) - 1)
1041 cidx++;
1042
1043 crl_issuer = sk_X509_value(ctx->chain, cidx);
1044
1045 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1046 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1047 *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_ISSUER_CERT;
1048 *pissuer = crl_issuer;
1049 return;
1050 }
1051 }
1052
1053 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1054 crl_issuer = sk_X509_value(ctx->chain, cidx);
1055 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1056 continue;
1057 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1058 *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_SAME_PATH;
1059 *pissuer = crl_issuer;
1060 return;
1061 }
1062 }
1063
1064 /* Anything else needs extended CRL support */
1065
1066 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1067 return;
1068
1069 /* Otherwise the CRL issuer is not on the path. Look for it in the
1070 * set of untrusted certificates.
1071 */
1072 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1073 crl_issuer = sk_X509_value(ctx->untrusted, i);
1074 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1075 continue;
1076 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1077 *pissuer = crl_issuer;
1078 *pcrl_score |= CRL_SCORE_AKID;
1079 return;
1080 }
1081 }
1082}
1083
1084/* Check the path of a CRL issuer certificate. This creates a new
1085 * X509_STORE_CTX and populates it with most of the parameters from the
1086 * parent. This could be optimised somewhat since a lot of path checking
1087 * will be duplicated by the parent, but this will rarely be used in
1088 * practice.
1089 */
1090
1091static int
1092check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1093{
1094 X509_STORE_CTX crl_ctx;
1095 int ret;
1096
1097 /* Don't allow recursive CRL path validation */
1098 if (ctx->parent)
1099 return 0;
1100 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
1101 return -1;
1102
1103 crl_ctx.crls = ctx->crls;
1104 /* Copy verify params across */
1105 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1106
1107 crl_ctx.parent = ctx;
1108 crl_ctx.verify_cb = ctx->verify_cb;
1109
1110 /* Verify CRL issuer */
1111 ret = X509_verify_cert(&crl_ctx);
1112
1113 if (ret <= 0)
1114 goto err;
1115
1116 /* Check chain is acceptable */
1117 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1118
1119err:
1120 X509_STORE_CTX_cleanup(&crl_ctx);
1121 return ret;
1122}
1123
1124/* RFC3280 says nothing about the relationship between CRL path
1125 * and certificate path, which could lead to situations where a
1126 * certificate could be revoked or validated by a CA not authorised
1127 * to do so. RFC5280 is more strict and states that the two paths must
1128 * end in the same trust anchor, though some discussions remain...
1129 * until this is resolved we use the RFC5280 version
1130 */
1131
1132static int
1133check_crl_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *cert_path,
1134 STACK_OF(X509) *crl_path)
1135{
1136 X509 *cert_ta, *crl_ta;
1137
1138 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1139 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1140 if (!X509_cmp(cert_ta, crl_ta))
1141 return 1;
1142 return 0;
1143}
1144
1145/* Check for match between two dist point names: three separate cases.
1146 * 1. Both are relative names and compare X509_NAME types.
1147 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1148 * 3. Both are full names and compare two GENERAL_NAMES.
1149 * 4. One is NULL: automatic match.
1150 */
1151
1152static int
1153idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1154{
1155 X509_NAME *nm = NULL;
1156 GENERAL_NAMES *gens = NULL;
1157 GENERAL_NAME *gena, *genb;
1158 int i, j;
1159
1160 if (!a || !b)
1161 return 1;
1162 if (a->type == 1) {
1163 if (!a->dpname)
1164 return 0;
1165 /* Case 1: two X509_NAME */
1166 if (b->type == 1) {
1167 if (!b->dpname)
1168 return 0;
1169 if (!X509_NAME_cmp(a->dpname, b->dpname))
1170 return 1;
1171 else
1172 return 0;
1173 }
1174 /* Case 2: set name and GENERAL_NAMES appropriately */
1175 nm = a->dpname;
1176 gens = b->name.fullname;
1177 } else if (b->type == 1) {
1178 if (!b->dpname)
1179 return 0;
1180 /* Case 2: set name and GENERAL_NAMES appropriately */
1181 gens = a->name.fullname;
1182 nm = b->dpname;
1183 }
1184
1185 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1186 if (nm) {
1187 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1188 gena = sk_GENERAL_NAME_value(gens, i);
1189 if (gena->type != GEN_DIRNAME)
1190 continue;
1191 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1192 return 1;
1193 }
1194 return 0;
1195 }
1196
1197 /* Else case 3: two GENERAL_NAMES */
1198
1199 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1200 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1201 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1202 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1203 if (!GENERAL_NAME_cmp(gena, genb))
1204 return 1;
1205 }
1206 }
1207
1208 return 0;
1209}
1210
1211static int
1212crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1213{
1214 int i;
1215 X509_NAME *nm = X509_CRL_get_issuer(crl);
1216
1217 /* If no CRLissuer return is successful iff don't need a match */
1218 if (!dp->CRLissuer)
1219 return !!(crl_score & CRL_SCORE_ISSUER_NAME);
1220 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1221 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1222 if (gen->type != GEN_DIRNAME)
1223 continue;
1224 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1225 return 1;
1226 }
1227 return 0;
1228}
1229
1230/* Check CRLDP and IDP */
1231
1232static int
1233crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, unsigned int *preasons)
1234{
1235 int i;
1236
1237 if (crl->idp_flags & IDP_ONLYATTR)
1238 return 0;
1239 if (x->ex_flags & EXFLAG_CA) {
1240 if (crl->idp_flags & IDP_ONLYUSER)
1241 return 0;
1242 } else {
1243 if (crl->idp_flags & IDP_ONLYCA)
1244 return 0;
1245 }
1246 *preasons = crl->idp_reasons;
1247 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1248 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1249 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1250 if (!crl->idp ||
1251 idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1252 *preasons &= dp->dp_reasons;
1253 return 1;
1254 }
1255 }
1256 }
1257 if ((!crl->idp || !crl->idp->distpoint) &&
1258 (crl_score & CRL_SCORE_ISSUER_NAME))
1259 return 1;
1260 return 0;
1261}
1262
1263/* Retrieve CRL corresponding to current certificate.
1264 * If deltas enabled try to find a delta CRL too
1265 */
1266
1267static int
1268get_crl_delta(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1269{
1270 int ok;
1271 X509 *issuer = NULL;
1272 int crl_score = 0;
1273 unsigned int reasons;
1274 X509_CRL *crl = NULL, *dcrl = NULL;
1275 STACK_OF(X509_CRL) *skcrl;
1276 X509_NAME *nm = X509_get_issuer_name(x);
1277
1278 reasons = ctx->current_reasons;
1279 ok = get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons,
1280 ctx->crls);
1281 if (ok)
1282 goto done;
1283
1284 /* Lookup CRLs from store */
1285 skcrl = ctx->lookup_crls(ctx, nm);
1286
1287 /* If no CRLs found and a near match from get_crl_sk use that */
1288 if (!skcrl && crl)
1289 goto done;
1290
1291 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1292
1293 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1294
1295done:
1296
1297 /* If we got any kind of CRL use it and return success */
1298 if (crl) {
1299 ctx->current_issuer = issuer;
1300 ctx->current_crl_score = crl_score;
1301 ctx->current_reasons = reasons;
1302 *pcrl = crl;
1303 *pdcrl = dcrl;
1304 return 1;
1305 }
1306
1307 return 0;
1308}
1309
1310/* Check CRL validity */
1311static int
1312check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1313{
1314 X509 *issuer = NULL;
1315 EVP_PKEY *ikey = NULL;
1316 int ok = 0, chnum, cnum;
1317
1318 cnum = ctx->error_depth;
1319 chnum = sk_X509_num(ctx->chain) - 1;
1320 /* if we have an alternative CRL issuer cert use that */
1321 if (ctx->current_issuer) {
1322 issuer = ctx->current_issuer;
1323 } else if (cnum < chnum) {
1324 /* Else find CRL issuer: if not last certificate then issuer
1325 * is next certificate in chain.
1326 */
1327 issuer = sk_X509_value(ctx->chain, cnum + 1);
1328 } else {
1329 issuer = sk_X509_value(ctx->chain, chnum);
1330 /* If not self signed, can't check signature */
1331 if (!ctx->check_issued(ctx, issuer, issuer)) {
1332 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1333 ok = ctx->verify_cb(0, ctx);
1334 if (!ok)
1335 goto err;
1336 }
1337 }
1338
1339 if (issuer) {
1340 /* Skip most tests for deltas because they have already
1341 * been done
1342 */
1343 if (!crl->base_crl_number) {
1344 /* Check for cRLSign bit if keyUsage present */
1345 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1346 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1347 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1348 ok = ctx->verify_cb(0, ctx);
1349 if (!ok)
1350 goto err;
1351 }
1352
1353 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1354 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1355 ok = ctx->verify_cb(0, ctx);
1356 if (!ok)
1357 goto err;
1358 }
1359
1360 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1361 if (check_crl_path(ctx,
1362 ctx->current_issuer) <= 0) {
1363 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1364 ok = ctx->verify_cb(0, ctx);
1365 if (!ok)
1366 goto err;
1367 }
1368 }
1369
1370 if (crl->idp_flags & IDP_INVALID) {
1371 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1372 ok = ctx->verify_cb(0, ctx);
1373 if (!ok)
1374 goto err;
1375 }
1376
1377
1378 }
1379
1380 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1381 ok = check_crl_time(ctx, crl, 1);
1382 if (!ok)
1383 goto err;
1384 }
1385
1386 /* Attempt to get issuer certificate public key */
1387 ikey = X509_get_pubkey(issuer);
1388
1389 if (!ikey) {
1390 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1391 ok = ctx->verify_cb(0, ctx);
1392 if (!ok)
1393 goto err;
1394 } else {
1395 /* Verify CRL signature */
1396 if (X509_CRL_verify(crl, ikey) <= 0) {
1397 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1398 ok = ctx->verify_cb(0, ctx);
1399 if (!ok)
1400 goto err;
1401 }
1402 }
1403 }
1404
1405 ok = 1;
1406
1407err:
1408 EVP_PKEY_free(ikey);
1409 return ok;
1410}
1411
1412/* Check certificate against CRL */
1413static int
1414cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1415{
1416 int ok;
1417 X509_REVOKED *rev;
1418
1419 /* The rules changed for this... previously if a CRL contained
1420 * unhandled critical extensions it could still be used to indicate
1421 * a certificate was revoked. This has since been changed since
1422 * critical extension can change the meaning of CRL entries.
1423 */
1424 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
1425 (crl->flags & EXFLAG_CRITICAL)) {
1426 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1427 ok = ctx->verify_cb(0, ctx);
1428 if (!ok)
1429 return 0;
1430 }
1431 /* Look for serial number of certificate in CRL
1432 * If found make sure reason is not removeFromCRL.
1433 */
1434 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1435 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1436 return 2;
1437 ctx->error = X509_V_ERR_CERT_REVOKED;
1438 ok = ctx->verify_cb(0, ctx);
1439 if (!ok)
1440 return 0;
1441 }
1442
1443 return 1;
1444}
1445
1446static int
1447check_policy(X509_STORE_CTX *ctx)
1448{
1449 int ret;
1450
1451 if (ctx->parent)
1452 return 1;
1453 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1454 ctx->param->policies, ctx->param->flags);
1455 if (ret == 0) {
1456 X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
1457 return 0;
1458 }
1459 /* Invalid or inconsistent extensions */
1460 if (ret == -1) {
1461 /* Locate certificates with bad extensions and notify
1462 * callback.
1463 */
1464 X509 *x;
1465 int i;
1466 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1467 x = sk_X509_value(ctx->chain, i);
1468 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1469 continue;
1470 ctx->current_cert = x;
1471 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1472 if (!ctx->verify_cb(0, ctx))
1473 return 0;
1474 }
1475 return 1;
1476 }
1477 if (ret == -2) {
1478 ctx->current_cert = NULL;
1479 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1480 return ctx->verify_cb(0, ctx);
1481 }
1482
1483 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1484 ctx->current_cert = NULL;
1485 ctx->error = X509_V_OK;
1486 if (!ctx->verify_cb(2, ctx))
1487 return 0;
1488 }
1489
1490 return 1;
1491}
1492
1493int
1494x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int quiet)
1495{
1496 time_t *ptime;
1497 int i;
1498
1499 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1500 ptime = &ctx->param->check_time;
1501 else
1502 ptime = NULL;
1503
1504 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1505 if (i == 0) {
1506 if (quiet)
1507 return 0;
1508 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1509 ctx->current_cert = x;
1510 if (!ctx->verify_cb(0, ctx))
1511 return 0;
1512 }
1513
1514 if (i > 0) {
1515 if (quiet)
1516 return 0;
1517 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1518 ctx->current_cert = x;
1519 if (!ctx->verify_cb(0, ctx))
1520 return 0;
1521 }
1522
1523 i = X509_cmp_time(X509_get_notAfter(x), ptime);
1524 if (i == 0) {
1525 if (quiet)
1526 return 0;
1527 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1528 ctx->current_cert = x;
1529 if (!ctx->verify_cb(0, ctx))
1530 return 0;
1531 }
1532
1533 if (i < 0) {
1534 if (quiet)
1535 return 0;
1536 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1537 ctx->current_cert = x;
1538 if (!ctx->verify_cb(0, ctx))
1539 return 0;
1540 }
1541
1542 return 1;
1543}
1544
1545static int
1546internal_verify(X509_STORE_CTX *ctx)
1547{
1548 int ok = 0, n;
1549 X509 *xs, *xi;
1550 EVP_PKEY *pkey = NULL;
1551 int (*cb)(int xok, X509_STORE_CTX *xctx);
1552
1553 cb = ctx->verify_cb;
1554
1555 n = sk_X509_num(ctx->chain);
1556 ctx->error_depth = n - 1;
1557 n--;
1558 xi = sk_X509_value(ctx->chain, n);
1559
1560 if (ctx->check_issued(ctx, xi, xi))
1561 xs = xi;
1562 else {
1563 if (n <= 0) {
1564 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1565 ctx->current_cert = xi;
1566 ok = cb(0, ctx);
1567 goto end;
1568 } else {
1569 n--;
1570 ctx->error_depth = n;
1571 xs = sk_X509_value(ctx->chain, n);
1572 }
1573 }
1574
1575/* ctx->error=0; not needed */
1576 while (n >= 0) {
1577 ctx->error_depth = n;
1578
1579 /* Skip signature check for self signed certificates unless
1580 * explicitly asked for. It doesn't add any security and
1581 * just wastes time.
1582 */
1583 if (!xs->valid && (xs != xi ||
1584 (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
1585 if ((pkey = X509_get_pubkey(xi)) == NULL) {
1586 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1587 ctx->current_cert = xi;
1588 ok = (*cb)(0, ctx);
1589 if (!ok)
1590 goto end;
1591 } else if (X509_verify(xs, pkey) <= 0) {
1592 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1593 ctx->current_cert = xs;
1594 ok = (*cb)(0, ctx);
1595 if (!ok) {
1596 EVP_PKEY_free(pkey);
1597 goto end;
1598 }
1599 }
1600 EVP_PKEY_free(pkey);
1601 pkey = NULL;
1602 }
1603
1604 xs->valid = 1;
1605
1606 ok = x509_check_cert_time(ctx, xs, 0);
1607 if (!ok)
1608 goto end;
1609
1610 /* The last error (if any) is still in the error value */
1611 ctx->current_issuer = xi;
1612 ctx->current_cert = xs;
1613 ok = (*cb)(1, ctx);
1614 if (!ok)
1615 goto end;
1616
1617 n--;
1618 if (n >= 0) {
1619 xi = xs;
1620 xs = sk_X509_value(ctx->chain, n);
1621 }
1622 }
1623 ok = 1;
1624
1625end:
1626 return ok;
1627}
1628
1629int
1630X509_cmp_current_time(const ASN1_TIME *ctm)
1631{
1632 return X509_cmp_time(ctm, NULL);
1633}
1634
1635int
1636X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1637{
1638 char *str;
1639 ASN1_TIME atm;
1640 long offset;
1641 char buff1[24], buff2[24], *p;
1642 int i, j;
1643
1644 p = buff1;
1645 i = ctm->length;
1646 str = (char *)ctm->data;
1647 if (ctm->type == V_ASN1_UTCTIME) {
1648 if ((i < 11) || (i > 17))
1649 return 0;
1650 memcpy(p, str, 10);
1651 p += 10;
1652 str += 10;
1653 } else {
1654 if (i < 13)
1655 return 0;
1656 memcpy(p, str, 12);
1657 p += 12;
1658 str += 12;
1659 }
1660
1661 if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
1662 *(p++) = '0';
1663 *(p++) = '0';
1664 } else {
1665 *(p++) = *(str++);
1666 *(p++) = *(str++);
1667 /* Skip any fractional seconds... */
1668 if (*str == '.') {
1669 str++;
1670 while ((*str >= '0') && (*str <= '9'))
1671 str++;
1672 }
1673 }
1674 *(p++) = 'Z';
1675 *(p++) = '\0';
1676
1677 if (*str == 'Z')
1678 offset = 0;
1679 else {
1680 if ((*str != '+') && (*str != '-'))
1681 return 0;
1682 offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
1683 offset += (str[3] - '0') * 10 + (str[4] - '0');
1684 if (*str == '-')
1685 offset = -offset;
1686 }
1687 atm.type = ctm->type;
1688 atm.flags = 0;
1689 atm.length = sizeof(buff2);
1690 atm.data = (unsigned char *)buff2;
1691
1692 if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
1693 return 0;
1694
1695 if (ctm->type == V_ASN1_UTCTIME) {
1696 i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
1697 if (i < 50)
1698 i += 100; /* cf. RFC 2459 */
1699 j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
1700 if (j < 50)
1701 j += 100;
1702 if (i < j)
1703 return -1;
1704 if (i > j)
1705 return 1;
1706 }
1707 i = strcmp(buff1, buff2);
1708 if (i == 0) /* wait a second then return younger :-) */
1709 return -1;
1710 else
1711 return i;
1712}
1713
1714ASN1_TIME *
1715X509_gmtime_adj(ASN1_TIME *s, long adj)
1716{
1717 return X509_time_adj(s, adj, NULL);
1718}
1719
1720ASN1_TIME *
1721X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
1722{
1723 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1724}
1725
1726ASN1_TIME *
1727X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec, time_t *in_tm)
1728{
1729 time_t t;
1730
1731 if (in_tm)
1732 t = *in_tm;
1733 else
1734 time(&t);
1735
1736 if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
1737 if (s->type == V_ASN1_UTCTIME)
1738 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
1739 if (s->type == V_ASN1_GENERALIZEDTIME)
1740 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day,
1741 offset_sec);
1742 }
1743 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1744}
1745
1746int
1747X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1748{
1749 EVP_PKEY *ktmp = NULL, *ktmp2;
1750 int i, j;
1751
1752 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1753 return 1;
1754
1755 for (i = 0; i < sk_X509_num(chain); i++) {
1756 ktmp = X509_get_pubkey(sk_X509_value(chain, i));
1757 if (ktmp == NULL) {
1758 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1759 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1760 return 0;
1761 }
1762 if (!EVP_PKEY_missing_parameters(ktmp))
1763 break;
1764 else {
1765 EVP_PKEY_free(ktmp);
1766 ktmp = NULL;
1767 }
1768 }
1769 if (ktmp == NULL) {
1770 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1771 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1772 return 0;
1773 }
1774
1775 /* first, populate the other certs */
1776 for (j = i - 1; j >= 0; j--) {
1777 ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
1778 EVP_PKEY_copy_parameters(ktmp2, ktmp);
1779 EVP_PKEY_free(ktmp2);
1780 }
1781
1782 if (pkey != NULL)
1783 EVP_PKEY_copy_parameters(pkey, ktmp);
1784 EVP_PKEY_free(ktmp);
1785 return 1;
1786}
1787
1788int
1789X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
1790 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
1791{
1792 /* This function is (usually) called only once, by
1793 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
1794 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX,
1795 argl, argp, new_func, dup_func, free_func);
1796}
1797
1798int
1799X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
1800{
1801 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1802}
1803
1804void *
1805X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
1806{
1807 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1808}
1809
1810int
1811X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
1812{
1813 return ctx->error;
1814}
1815
1816void
1817X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
1818{
1819 ctx->error = err;
1820}
1821
1822int
1823X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
1824{
1825 return ctx->error_depth;
1826}
1827
1828X509 *
1829X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
1830{
1831 return ctx->current_cert;
1832}
1833
1834STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
1835{
1836 return ctx->chain;
1837}
1838
1839STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
1840{
1841 int i;
1842 X509 *x;
1843 STACK_OF(X509) *chain;
1844
1845 if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain)))
1846 return NULL;
1847 for (i = 0; i < sk_X509_num(chain); i++) {
1848 x = sk_X509_value(chain, i);
1849 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1850 }
1851 return chain;
1852}
1853
1854X509 *
1855X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
1856{
1857 return ctx->current_issuer;
1858}
1859
1860X509_CRL *
1861X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
1862{
1863 return ctx->current_crl;
1864}
1865
1866X509_STORE_CTX *
1867X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
1868{
1869 return ctx->parent;
1870}
1871
1872void
1873X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
1874{
1875 ctx->cert = x;
1876}
1877
1878void
1879X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1880{
1881 ctx->untrusted = sk;
1882}
1883
1884void
1885X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
1886{
1887 ctx->crls = sk;
1888}
1889
1890int
1891X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
1892{
1893 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
1894}
1895
1896int
1897X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
1898{
1899 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
1900}
1901
1902/* This function is used to set the X509_STORE_CTX purpose and trust
1903 * values. This is intended to be used when another structure has its
1904 * own trust and purpose values which (if set) will be inherited by
1905 * the ctx. If they aren't set then we will usually have a default
1906 * purpose in mind which should then be used to set the trust value.
1907 * An example of this is SSL use: an SSL structure will have its own
1908 * purpose and trust settings which the application can set: if they
1909 * aren't set then we use the default of SSL client/server.
1910 */
1911
1912int
1913X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
1914 int purpose, int trust)
1915{
1916 int idx;
1917
1918 /* If purpose not set use default */
1919 if (!purpose)
1920 purpose = def_purpose;
1921 /* If we have a purpose then check it is valid */
1922 if (purpose) {
1923 X509_PURPOSE *ptmp;
1924 idx = X509_PURPOSE_get_by_id(purpose);
1925 if (idx == -1) {
1926 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1927 X509_R_UNKNOWN_PURPOSE_ID);
1928 return 0;
1929 }
1930 ptmp = X509_PURPOSE_get0(idx);
1931 if (ptmp->trust == X509_TRUST_DEFAULT) {
1932 idx = X509_PURPOSE_get_by_id(def_purpose);
1933 if (idx == -1) {
1934 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1935 X509_R_UNKNOWN_PURPOSE_ID);
1936 return 0;
1937 }
1938 ptmp = X509_PURPOSE_get0(idx);
1939 }
1940 /* If trust not set then get from purpose default */
1941 if (!trust)
1942 trust = ptmp->trust;
1943 }
1944 if (trust) {
1945 idx = X509_TRUST_get_by_id(trust);
1946 if (idx == -1) {
1947 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1948 X509_R_UNKNOWN_TRUST_ID);
1949 return 0;
1950 }
1951 }
1952
1953 if (purpose && !ctx->param->purpose)
1954 ctx->param->purpose = purpose;
1955 if (trust && !ctx->param->trust)
1956 ctx->param->trust = trust;
1957 return 1;
1958}
1959
1960X509_STORE_CTX *
1961X509_STORE_CTX_new(void)
1962{
1963 X509_STORE_CTX *ctx;
1964
1965 ctx = calloc(1, sizeof(X509_STORE_CTX));
1966 if (!ctx) {
1967 X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
1968 return NULL;
1969 }
1970 return ctx;
1971}
1972
1973void
1974X509_STORE_CTX_free(X509_STORE_CTX *ctx)
1975{
1976 if (ctx == NULL)
1977 return;
1978
1979 X509_STORE_CTX_cleanup(ctx);
1980 free(ctx);
1981}
1982
1983int
1984X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
1985 STACK_OF(X509) *chain)
1986{
1987 int ret = 1;
1988
1989 ctx->ctx = store;
1990 ctx->current_method = 0;
1991 ctx->cert = x509;
1992 ctx->untrusted = chain;
1993 ctx->crls = NULL;
1994 ctx->last_untrusted = 0;
1995 ctx->other_ctx = NULL;
1996 ctx->valid = 0;
1997 ctx->chain = NULL;
1998 ctx->error = 0;
1999 ctx->explicit_policy = 0;
2000 ctx->error_depth = 0;
2001 ctx->current_cert = NULL;
2002 ctx->current_issuer = NULL;
2003 ctx->current_crl = NULL;
2004 ctx->current_crl_score = 0;
2005 ctx->current_reasons = 0;
2006 ctx->tree = NULL;
2007 ctx->parent = NULL;
2008
2009 ctx->param = X509_VERIFY_PARAM_new();
2010
2011 if (!ctx->param) {
2012 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2013 return 0;
2014 }
2015
2016 /* Inherit callbacks and flags from X509_STORE if not set
2017 * use defaults.
2018 */
2019
2020 if (store)
2021 ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2022 else
2023 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE;
2024
2025 if (store) {
2026 ctx->verify_cb = store->verify_cb;
2027 ctx->cleanup = store->cleanup;
2028 } else
2029 ctx->cleanup = 0;
2030
2031 if (ret)
2032 ret = X509_VERIFY_PARAM_inherit(ctx->param,
2033 X509_VERIFY_PARAM_lookup("default"));
2034
2035 if (ret == 0) {
2036 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2037 return 0;
2038 }
2039
2040 if (store && store->check_issued)
2041 ctx->check_issued = store->check_issued;
2042 else
2043 ctx->check_issued = check_issued;
2044
2045 if (store && store->get_issuer)
2046 ctx->get_issuer = store->get_issuer;
2047 else
2048 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2049
2050 if (store && store->verify_cb)
2051 ctx->verify_cb = store->verify_cb;
2052 else
2053 ctx->verify_cb = null_callback;
2054
2055 if (store && store->verify)
2056 ctx->verify = store->verify;
2057 else
2058 ctx->verify = internal_verify;
2059
2060 if (store && store->check_revocation)
2061 ctx->check_revocation = store->check_revocation;
2062 else
2063 ctx->check_revocation = check_revocation;
2064
2065 if (store && store->get_crl)
2066 ctx->get_crl = store->get_crl;
2067 else
2068 ctx->get_crl = NULL;
2069
2070 if (store && store->check_crl)
2071 ctx->check_crl = store->check_crl;
2072 else
2073 ctx->check_crl = check_crl;
2074
2075 if (store && store->cert_crl)
2076 ctx->cert_crl = store->cert_crl;
2077 else
2078 ctx->cert_crl = cert_crl;
2079
2080 if (store && store->lookup_certs)
2081 ctx->lookup_certs = store->lookup_certs;
2082 else
2083 ctx->lookup_certs = X509_STORE_get1_certs;
2084
2085 if (store && store->lookup_crls)
2086 ctx->lookup_crls = store->lookup_crls;
2087 else
2088 ctx->lookup_crls = X509_STORE_get1_crls;
2089
2090 ctx->check_policy = check_policy;
2091
2092
2093 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2094 &(ctx->ex_data)) == 0) {
2095 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2096 return 0;
2097 }
2098 return 1;
2099}
2100
2101/* Set alternative lookup method: just a STACK of trusted certificates.
2102 * This avoids X509_STORE nastiness where it isn't needed.
2103 */
2104
2105void
2106X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2107{
2108 ctx->other_ctx = sk;
2109 ctx->get_issuer = get_issuer_sk;
2110}
2111
2112void
2113X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2114{
2115 if (ctx->cleanup)
2116 ctx->cleanup(ctx);
2117 if (ctx->param != NULL) {
2118 if (ctx->parent == NULL)
2119 X509_VERIFY_PARAM_free(ctx->param);
2120 ctx->param = NULL;
2121 }
2122 if (ctx->tree != NULL) {
2123 X509_policy_tree_free(ctx->tree);
2124 ctx->tree = NULL;
2125 }
2126 if (ctx->chain != NULL) {
2127 sk_X509_pop_free(ctx->chain, X509_free);
2128 ctx->chain = NULL;
2129 }
2130 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX,
2131 ctx, &(ctx->ex_data));
2132 memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
2133}
2134
2135void
2136X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2137{
2138 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2139}
2140
2141void
2142X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2143{
2144 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2145}
2146
2147void
2148X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t)
2149{
2150 X509_VERIFY_PARAM_set_time(ctx->param, t);
2151}
2152
2153void
2154X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2155 int (*verify_cb)(int, X509_STORE_CTX *))
2156{
2157 ctx->verify_cb = verify_cb;
2158}
2159
2160X509_POLICY_TREE *
2161X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2162{
2163 return ctx->tree;
2164}
2165
2166int
2167X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2168{
2169 return ctx->explicit_policy;
2170}
2171
2172int
2173X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2174{
2175 const X509_VERIFY_PARAM *param;
2176 param = X509_VERIFY_PARAM_lookup(name);
2177 if (!param)
2178 return 0;
2179 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2180}
2181
2182X509_VERIFY_PARAM *
2183X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2184{
2185 return ctx->param;
2186}
2187
2188void
2189X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2190{
2191 if (ctx->param)
2192 X509_VERIFY_PARAM_free(ctx->param);
2193 ctx->param = param;
2194}