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.c2210
1 files changed, 0 insertions, 2210 deletions
diff --git a/src/lib/libcrypto/x509/x509_vfy.c b/src/lib/libcrypto/x509/x509_vfy.c
deleted file mode 100644
index f2dc356dc8..0000000000
--- a/src/lib/libcrypto/x509/x509_vfy.c
+++ /dev/null
@@ -1,2210 +0,0 @@
1/* $OpenBSD: x509_vfy.c,v 1.44 2015/07/19 05:42:55 miod 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 purpose = ctx->param->purpose;
487 }
488
489 /* Check all untrusted certificates */
490 for (i = 0; i < ctx->last_untrusted; i++) {
491 int ret;
492 x = sk_X509_value(ctx->chain, i);
493 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
494 (x->ex_flags & EXFLAG_CRITICAL)) {
495 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
496 ctx->error_depth = i;
497 ctx->current_cert = x;
498 ok = cb(0, ctx);
499 if (!ok)
500 goto end;
501 }
502 if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
503 ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
504 ctx->error_depth = i;
505 ctx->current_cert = x;
506 ok = cb(0, ctx);
507 if (!ok)
508 goto end;
509 }
510 ret = X509_check_ca(x);
511 switch (must_be_ca) {
512 case -1:
513 if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
514 (ret != 1) && (ret != 0)) {
515 ret = 0;
516 ctx->error = X509_V_ERR_INVALID_CA;
517 } else
518 ret = 1;
519 break;
520 case 0:
521 if (ret != 0) {
522 ret = 0;
523 ctx->error = X509_V_ERR_INVALID_NON_CA;
524 } else
525 ret = 1;
526 break;
527 default:
528 if ((ret == 0) ||
529 ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
530 (ret != 1))) {
531 ret = 0;
532 ctx->error = X509_V_ERR_INVALID_CA;
533 } else
534 ret = 1;
535 break;
536 }
537 if (ret == 0) {
538 ctx->error_depth = i;
539 ctx->current_cert = x;
540 ok = cb(0, ctx);
541 if (!ok)
542 goto end;
543 }
544 if (ctx->param->purpose > 0) {
545 ret = X509_check_purpose(x, purpose, must_be_ca > 0);
546 if ((ret == 0) ||
547 ((ctx->param->flags & X509_V_FLAG_X509_STRICT) &&
548 (ret != 1))) {
549 ctx->error = X509_V_ERR_INVALID_PURPOSE;
550 ctx->error_depth = i;
551 ctx->current_cert = x;
552 ok = cb(0, ctx);
553 if (!ok)
554 goto end;
555 }
556 }
557 /* Check pathlen if not self issued */
558 if ((i > 1) && !(x->ex_flags & EXFLAG_SI) &&
559 (x->ex_pathlen != -1) &&
560 (plen > (x->ex_pathlen + proxy_path_length + 1))) {
561 ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
562 ctx->error_depth = i;
563 ctx->current_cert = x;
564 ok = cb(0, ctx);
565 if (!ok)
566 goto end;
567 }
568 /* Increment path length if not self issued */
569 if (!(x->ex_flags & EXFLAG_SI))
570 plen++;
571 /* If this certificate is a proxy certificate, the next
572 certificate must be another proxy certificate or a EE
573 certificate. If not, the next certificate must be a
574 CA certificate. */
575 if (x->ex_flags & EXFLAG_PROXY) {
576 if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
577 ctx->error =
578 X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
579 ctx->error_depth = i;
580 ctx->current_cert = x;
581 ok = cb(0, ctx);
582 if (!ok)
583 goto end;
584 }
585 proxy_path_length++;
586 must_be_ca = 0;
587 } else
588 must_be_ca = 1;
589 }
590 ok = 1;
591
592end:
593 return ok;
594#endif
595}
596
597static int
598check_name_constraints(X509_STORE_CTX *ctx)
599{
600 X509 *x;
601 int i, j, rv;
602
603 /* Check name constraints for all certificates */
604 for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
605 x = sk_X509_value(ctx->chain, i);
606 /* Ignore self issued certs unless last in chain */
607 if (i && (x->ex_flags & EXFLAG_SI))
608 continue;
609 /* Check against constraints for all certificates higher in
610 * chain including trust anchor. Trust anchor not strictly
611 * speaking needed but if it includes constraints it is to be
612 * assumed it expects them to be obeyed.
613 */
614 for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
615 NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
616 if (nc) {
617 rv = NAME_CONSTRAINTS_check(x, nc);
618 if (rv != X509_V_OK) {
619 ctx->error = rv;
620 ctx->error_depth = i;
621 ctx->current_cert = x;
622 if (!ctx->verify_cb(0, ctx))
623 return 0;
624 }
625 }
626 }
627 }
628 return 1;
629}
630
631static int
632check_trust(X509_STORE_CTX *ctx)
633{
634#ifdef OPENSSL_NO_CHAIN_VERIFY
635 return 1;
636#else
637 int i, ok;
638 X509 *x;
639 int (*cb)(int xok, X509_STORE_CTX *xctx);
640
641 cb = ctx->verify_cb;
642 /* For now just check the last certificate in the chain */
643 i = sk_X509_num(ctx->chain) - 1;
644 x = sk_X509_value(ctx->chain, i);
645 ok = X509_check_trust(x, ctx->param->trust, 0);
646 if (ok == X509_TRUST_TRUSTED)
647 return 1;
648 ctx->error_depth = i;
649 ctx->current_cert = x;
650 if (ok == X509_TRUST_REJECTED)
651 ctx->error = X509_V_ERR_CERT_REJECTED;
652 else
653 ctx->error = X509_V_ERR_CERT_UNTRUSTED;
654 ok = cb(0, ctx);
655 return ok;
656#endif
657}
658
659static int
660check_revocation(X509_STORE_CTX *ctx)
661{
662 int i, last, ok;
663
664 if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
665 return 1;
666 if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
667 last = sk_X509_num(ctx->chain) - 1;
668 else {
669 /* If checking CRL paths this isn't the EE certificate */
670 if (ctx->parent)
671 return 1;
672 last = 0;
673 }
674 for (i = 0; i <= last; i++) {
675 ctx->error_depth = i;
676 ok = check_cert(ctx);
677 if (!ok)
678 return ok;
679 }
680 return 1;
681}
682
683static int
684check_cert(X509_STORE_CTX *ctx)
685{
686 X509_CRL *crl = NULL, *dcrl = NULL;
687 X509 *x;
688 int ok, cnum;
689 unsigned int last_reasons;
690
691 cnum = ctx->error_depth;
692 x = sk_X509_value(ctx->chain, cnum);
693 ctx->current_cert = x;
694 ctx->current_issuer = NULL;
695 ctx->current_crl_score = 0;
696 ctx->current_reasons = 0;
697 while (ctx->current_reasons != CRLDP_ALL_REASONS) {
698 last_reasons = ctx->current_reasons;
699 /* Try to retrieve relevant CRL */
700 if (ctx->get_crl)
701 ok = ctx->get_crl(ctx, &crl, x);
702 else
703 ok = get_crl_delta(ctx, &crl, &dcrl, x);
704 /* If error looking up CRL, nothing we can do except
705 * notify callback
706 */
707 if (!ok) {
708 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
709 ok = ctx->verify_cb(0, ctx);
710 goto err;
711 }
712 ctx->current_crl = crl;
713 ok = ctx->check_crl(ctx, crl);
714 if (!ok)
715 goto err;
716
717 if (dcrl) {
718 ok = ctx->check_crl(ctx, dcrl);
719 if (!ok)
720 goto err;
721 ok = ctx->cert_crl(ctx, dcrl, x);
722 if (!ok)
723 goto err;
724 } else
725 ok = 1;
726
727 /* Don't look in full CRL if delta reason is removefromCRL */
728 if (ok != 2) {
729 ok = ctx->cert_crl(ctx, crl, x);
730 if (!ok)
731 goto err;
732 }
733
734 ctx->current_crl = NULL;
735 X509_CRL_free(crl);
736 X509_CRL_free(dcrl);
737 crl = NULL;
738 dcrl = NULL;
739 /* If reasons not updated we wont get anywhere by
740 * another iteration, so exit loop.
741 */
742 if (last_reasons == ctx->current_reasons) {
743 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
744 ok = ctx->verify_cb(0, ctx);
745 goto err;
746 }
747 }
748
749err:
750 ctx->current_crl = NULL;
751 X509_CRL_free(crl);
752 X509_CRL_free(dcrl);
753 return ok;
754}
755
756/* Check CRL times against values in X509_STORE_CTX */
757
758static int
759check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
760{
761 time_t *ptime;
762 int i;
763
764 if (notify)
765 ctx->current_crl = crl;
766 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
767 ptime = &ctx->param->check_time;
768 else
769 ptime = NULL;
770
771 i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
772 if (i == 0) {
773 if (!notify)
774 return 0;
775 ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
776 if (!ctx->verify_cb(0, ctx))
777 return 0;
778 }
779
780 if (i > 0) {
781 if (!notify)
782 return 0;
783 ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
784 if (!ctx->verify_cb(0, ctx))
785 return 0;
786 }
787
788 if (X509_CRL_get_nextUpdate(crl)) {
789 i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
790
791 if (i == 0) {
792 if (!notify)
793 return 0;
794 ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
795 if (!ctx->verify_cb(0, ctx))
796 return 0;
797 }
798 /* Ignore expiry of base CRL is delta is valid */
799 if ((i < 0) &&
800 !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
801 if (!notify)
802 return 0;
803 ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
804 if (!ctx->verify_cb(0, ctx))
805 return 0;
806 }
807 }
808
809 if (notify)
810 ctx->current_crl = NULL;
811
812 return 1;
813}
814
815static int
816get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
817 X509 **pissuer, int *pscore, unsigned int *preasons,
818 STACK_OF(X509_CRL) *crls)
819{
820 int i, crl_score, best_score = *pscore;
821 unsigned int reasons, best_reasons = 0;
822 X509 *x = ctx->current_cert;
823 X509_CRL *crl, *best_crl = NULL;
824 X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
825
826 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
827 crl = sk_X509_CRL_value(crls, i);
828 reasons = *preasons;
829 crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
830
831 if (crl_score > best_score) {
832 best_crl = crl;
833 best_crl_issuer = crl_issuer;
834 best_score = crl_score;
835 best_reasons = reasons;
836 }
837 }
838
839 if (best_crl) {
840 if (*pcrl)
841 X509_CRL_free(*pcrl);
842 *pcrl = best_crl;
843 *pissuer = best_crl_issuer;
844 *pscore = best_score;
845 *preasons = best_reasons;
846 CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509_CRL);
847 if (*pdcrl) {
848 X509_CRL_free(*pdcrl);
849 *pdcrl = NULL;
850 }
851 get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
852 }
853
854 if (best_score >= CRL_SCORE_VALID)
855 return 1;
856
857 return 0;
858}
859
860/* Compare two CRL extensions for delta checking purposes. They should be
861 * both present or both absent. If both present all fields must be identical.
862 */
863
864static int
865crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
866{
867 ASN1_OCTET_STRING *exta, *extb;
868 int i;
869
870 i = X509_CRL_get_ext_by_NID(a, nid, -1);
871 if (i >= 0) {
872 /* Can't have multiple occurrences */
873 if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
874 return 0;
875 exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
876 } else
877 exta = NULL;
878
879 i = X509_CRL_get_ext_by_NID(b, nid, -1);
880
881 if (i >= 0) {
882 if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
883 return 0;
884 extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
885 } else
886 extb = NULL;
887
888 if (!exta && !extb)
889 return 1;
890
891 if (!exta || !extb)
892 return 0;
893
894 if (ASN1_OCTET_STRING_cmp(exta, extb))
895 return 0;
896
897 return 1;
898}
899
900/* See if a base and delta are compatible */
901
902static int
903check_delta_base(X509_CRL *delta, X509_CRL *base)
904{
905 /* Delta CRL must be a delta */
906 if (!delta->base_crl_number)
907 return 0;
908 /* Base must have a CRL number */
909 if (!base->crl_number)
910 return 0;
911 /* Issuer names must match */
912 if (X509_NAME_cmp(X509_CRL_get_issuer(base),
913 X509_CRL_get_issuer(delta)))
914 return 0;
915 /* AKID and IDP must match */
916 if (!crl_extension_match(delta, base, NID_authority_key_identifier))
917 return 0;
918 if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
919 return 0;
920 /* Delta CRL base number must not exceed Full CRL number. */
921 if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
922 return 0;
923 /* Delta CRL number must exceed full CRL number */
924 if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
925 return 1;
926 return 0;
927}
928
929/* For a given base CRL find a delta... maybe extend to delta scoring
930 * or retrieve a chain of deltas...
931 */
932
933static void
934get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore, X509_CRL *base,
935 STACK_OF(X509_CRL) *crls)
936{
937 X509_CRL *delta;
938 int i;
939
940 if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
941 return;
942 if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
943 return;
944 for (i = 0; i < sk_X509_CRL_num(crls); i++) {
945 delta = sk_X509_CRL_value(crls, i);
946 if (check_delta_base(delta, base)) {
947 if (check_crl_time(ctx, delta, 0))
948 *pscore |= CRL_SCORE_TIME_DELTA;
949 CRYPTO_add(&delta->references, 1, CRYPTO_LOCK_X509_CRL);
950 *dcrl = delta;
951 return;
952 }
953 }
954 *dcrl = NULL;
955}
956
957/* For a given CRL return how suitable it is for the supplied certificate 'x'.
958 * The return value is a mask of several criteria.
959 * If the issuer is not the certificate issuer this is returned in *pissuer.
960 * The reasons mask is also used to determine if the CRL is suitable: if
961 * no new reasons the CRL is rejected, otherwise reasons is updated.
962 */
963
964static int
965get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, unsigned int *preasons,
966 X509_CRL *crl, X509 *x)
967{
968 int crl_score = 0;
969 unsigned int tmp_reasons = *preasons, crl_reasons;
970
971 /* First see if we can reject CRL straight away */
972
973 /* Invalid IDP cannot be processed */
974 if (crl->idp_flags & IDP_INVALID)
975 return 0;
976 /* Reason codes or indirect CRLs need extended CRL support */
977 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
978 if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
979 return 0;
980 } else if (crl->idp_flags & IDP_REASONS) {
981 /* If no new reasons reject */
982 if (!(crl->idp_reasons & ~tmp_reasons))
983 return 0;
984 }
985 /* Don't process deltas at this stage */
986 else if (crl->base_crl_number)
987 return 0;
988 /* If issuer name doesn't match certificate need indirect CRL */
989 if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
990 if (!(crl->idp_flags & IDP_INDIRECT))
991 return 0;
992 } else
993 crl_score |= CRL_SCORE_ISSUER_NAME;
994
995 if (!(crl->flags & EXFLAG_CRITICAL))
996 crl_score |= CRL_SCORE_NOCRITICAL;
997
998 /* Check expiry */
999 if (check_crl_time(ctx, crl, 0))
1000 crl_score |= CRL_SCORE_TIME;
1001
1002 /* Check authority key ID and locate certificate issuer */
1003 crl_akid_check(ctx, crl, pissuer, &crl_score);
1004
1005 /* If we can't locate certificate issuer at this point forget it */
1006
1007 if (!(crl_score & CRL_SCORE_AKID))
1008 return 0;
1009
1010 /* Check cert for matching CRL distribution points */
1011
1012 if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
1013 /* If no new reasons reject */
1014 if (!(crl_reasons & ~tmp_reasons))
1015 return 0;
1016 tmp_reasons |= crl_reasons;
1017 crl_score |= CRL_SCORE_SCOPE;
1018 }
1019
1020 *preasons = tmp_reasons;
1021
1022 return crl_score;
1023}
1024
1025static void
1026crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
1027 int *pcrl_score)
1028{
1029 X509 *crl_issuer = NULL;
1030 X509_NAME *cnm = X509_CRL_get_issuer(crl);
1031 int cidx = ctx->error_depth;
1032 int i;
1033
1034 if (cidx != sk_X509_num(ctx->chain) - 1)
1035 cidx++;
1036
1037 crl_issuer = sk_X509_value(ctx->chain, cidx);
1038
1039 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1040 if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
1041 *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_ISSUER_CERT;
1042 *pissuer = crl_issuer;
1043 return;
1044 }
1045 }
1046
1047 for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
1048 crl_issuer = sk_X509_value(ctx->chain, cidx);
1049 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1050 continue;
1051 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1052 *pcrl_score |= CRL_SCORE_AKID|CRL_SCORE_SAME_PATH;
1053 *pissuer = crl_issuer;
1054 return;
1055 }
1056 }
1057
1058 /* Anything else needs extended CRL support */
1059
1060 if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
1061 return;
1062
1063 /* Otherwise the CRL issuer is not on the path. Look for it in the
1064 * set of untrusted certificates.
1065 */
1066 for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
1067 crl_issuer = sk_X509_value(ctx->untrusted, i);
1068 if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
1069 continue;
1070 if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
1071 *pissuer = crl_issuer;
1072 *pcrl_score |= CRL_SCORE_AKID;
1073 return;
1074 }
1075 }
1076}
1077
1078/* Check the path of a CRL issuer certificate. This creates a new
1079 * X509_STORE_CTX and populates it with most of the parameters from the
1080 * parent. This could be optimised somewhat since a lot of path checking
1081 * will be duplicated by the parent, but this will rarely be used in
1082 * practice.
1083 */
1084
1085static int
1086check_crl_path(X509_STORE_CTX *ctx, X509 *x)
1087{
1088 X509_STORE_CTX crl_ctx;
1089 int ret;
1090
1091 /* Don't allow recursive CRL path validation */
1092 if (ctx->parent)
1093 return 0;
1094 if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted)) {
1095 ret = -1;
1096 goto err;
1097 }
1098
1099 crl_ctx.crls = ctx->crls;
1100 /* Copy verify params across */
1101 X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
1102
1103 crl_ctx.parent = ctx;
1104 crl_ctx.verify_cb = ctx->verify_cb;
1105
1106 /* Verify CRL issuer */
1107 ret = X509_verify_cert(&crl_ctx);
1108
1109 if (ret <= 0)
1110 goto err;
1111
1112 /* Check chain is acceptable */
1113 ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
1114
1115err:
1116 X509_STORE_CTX_cleanup(&crl_ctx);
1117 return ret;
1118}
1119
1120/* RFC3280 says nothing about the relationship between CRL path
1121 * and certificate path, which could lead to situations where a
1122 * certificate could be revoked or validated by a CA not authorised
1123 * to do so. RFC5280 is more strict and states that the two paths must
1124 * end in the same trust anchor, though some discussions remain...
1125 * until this is resolved we use the RFC5280 version
1126 */
1127
1128static int
1129check_crl_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *cert_path,
1130 STACK_OF(X509) *crl_path)
1131{
1132 X509 *cert_ta, *crl_ta;
1133
1134 cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
1135 crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
1136 if (!X509_cmp(cert_ta, crl_ta))
1137 return 1;
1138 return 0;
1139}
1140
1141/* Check for match between two dist point names: three separate cases.
1142 * 1. Both are relative names and compare X509_NAME types.
1143 * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
1144 * 3. Both are full names and compare two GENERAL_NAMES.
1145 * 4. One is NULL: automatic match.
1146 */
1147
1148static int
1149idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
1150{
1151 X509_NAME *nm = NULL;
1152 GENERAL_NAMES *gens = NULL;
1153 GENERAL_NAME *gena, *genb;
1154 int i, j;
1155
1156 if (!a || !b)
1157 return 1;
1158 if (a->type == 1) {
1159 if (!a->dpname)
1160 return 0;
1161 /* Case 1: two X509_NAME */
1162 if (b->type == 1) {
1163 if (!b->dpname)
1164 return 0;
1165 if (!X509_NAME_cmp(a->dpname, b->dpname))
1166 return 1;
1167 else
1168 return 0;
1169 }
1170 /* Case 2: set name and GENERAL_NAMES appropriately */
1171 nm = a->dpname;
1172 gens = b->name.fullname;
1173 } else if (b->type == 1) {
1174 if (!b->dpname)
1175 return 0;
1176 /* Case 2: set name and GENERAL_NAMES appropriately */
1177 gens = a->name.fullname;
1178 nm = b->dpname;
1179 }
1180
1181 /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
1182 if (nm) {
1183 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1184 gena = sk_GENERAL_NAME_value(gens, i);
1185 if (gena->type != GEN_DIRNAME)
1186 continue;
1187 if (!X509_NAME_cmp(nm, gena->d.directoryName))
1188 return 1;
1189 }
1190 return 0;
1191 }
1192
1193 /* Else case 3: two GENERAL_NAMES */
1194
1195 for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
1196 gena = sk_GENERAL_NAME_value(a->name.fullname, i);
1197 for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
1198 genb = sk_GENERAL_NAME_value(b->name.fullname, j);
1199 if (!GENERAL_NAME_cmp(gena, genb))
1200 return 1;
1201 }
1202 }
1203
1204 return 0;
1205}
1206
1207static int
1208crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
1209{
1210 int i;
1211 X509_NAME *nm = X509_CRL_get_issuer(crl);
1212
1213 /* If no CRLissuer return is successful iff don't need a match */
1214 if (!dp->CRLissuer)
1215 return !!(crl_score & CRL_SCORE_ISSUER_NAME);
1216 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
1217 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
1218 if (gen->type != GEN_DIRNAME)
1219 continue;
1220 if (!X509_NAME_cmp(gen->d.directoryName, nm))
1221 return 1;
1222 }
1223 return 0;
1224}
1225
1226/* Check CRLDP and IDP */
1227
1228static int
1229crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score, unsigned int *preasons)
1230{
1231 int i;
1232
1233 if (crl->idp_flags & IDP_ONLYATTR)
1234 return 0;
1235 if (x->ex_flags & EXFLAG_CA) {
1236 if (crl->idp_flags & IDP_ONLYUSER)
1237 return 0;
1238 } else {
1239 if (crl->idp_flags & IDP_ONLYCA)
1240 return 0;
1241 }
1242 *preasons = crl->idp_reasons;
1243 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
1244 DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
1245 if (crldp_check_crlissuer(dp, crl, crl_score)) {
1246 if (!crl->idp ||
1247 idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
1248 *preasons &= dp->dp_reasons;
1249 return 1;
1250 }
1251 }
1252 }
1253 if ((!crl->idp || !crl->idp->distpoint) &&
1254 (crl_score & CRL_SCORE_ISSUER_NAME))
1255 return 1;
1256 return 0;
1257}
1258
1259/* Retrieve CRL corresponding to current certificate.
1260 * If deltas enabled try to find a delta CRL too
1261 */
1262
1263static int
1264get_crl_delta(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
1265{
1266 int ok;
1267 X509 *issuer = NULL;
1268 int crl_score = 0;
1269 unsigned int reasons;
1270 X509_CRL *crl = NULL, *dcrl = NULL;
1271 STACK_OF(X509_CRL) *skcrl;
1272 X509_NAME *nm = X509_get_issuer_name(x);
1273
1274 reasons = ctx->current_reasons;
1275 ok = get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons,
1276 ctx->crls);
1277 if (ok)
1278 goto done;
1279
1280 /* Lookup CRLs from store */
1281 skcrl = ctx->lookup_crls(ctx, nm);
1282
1283 /* If no CRLs found and a near match from get_crl_sk use that */
1284 if (!skcrl && crl)
1285 goto done;
1286
1287 get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
1288
1289 sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
1290
1291done:
1292
1293 /* If we got any kind of CRL use it and return success */
1294 if (crl) {
1295 ctx->current_issuer = issuer;
1296 ctx->current_crl_score = crl_score;
1297 ctx->current_reasons = reasons;
1298 *pcrl = crl;
1299 *pdcrl = dcrl;
1300 return 1;
1301 }
1302
1303 return 0;
1304}
1305
1306/* Check CRL validity */
1307static int
1308check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
1309{
1310 X509 *issuer = NULL;
1311 EVP_PKEY *ikey = NULL;
1312 int ok = 0, chnum, cnum;
1313
1314 cnum = ctx->error_depth;
1315 chnum = sk_X509_num(ctx->chain) - 1;
1316 /* if we have an alternative CRL issuer cert use that */
1317 if (ctx->current_issuer) {
1318 issuer = ctx->current_issuer;
1319 } else if (cnum < chnum) {
1320 /* Else find CRL issuer: if not last certificate then issuer
1321 * is next certificate in chain.
1322 */
1323 issuer = sk_X509_value(ctx->chain, cnum + 1);
1324 } else {
1325 issuer = sk_X509_value(ctx->chain, chnum);
1326 /* If not self signed, can't check signature */
1327 if (!ctx->check_issued(ctx, issuer, issuer)) {
1328 ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
1329 ok = ctx->verify_cb(0, ctx);
1330 if (!ok)
1331 goto err;
1332 }
1333 }
1334
1335 if (issuer) {
1336 /* Skip most tests for deltas because they have already
1337 * been done
1338 */
1339 if (!crl->base_crl_number) {
1340 /* Check for cRLSign bit if keyUsage present */
1341 if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
1342 !(issuer->ex_kusage & KU_CRL_SIGN)) {
1343 ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
1344 ok = ctx->verify_cb(0, ctx);
1345 if (!ok)
1346 goto err;
1347 }
1348
1349 if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
1350 ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
1351 ok = ctx->verify_cb(0, ctx);
1352 if (!ok)
1353 goto err;
1354 }
1355
1356 if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
1357 if (check_crl_path(ctx,
1358 ctx->current_issuer) <= 0) {
1359 ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
1360 ok = ctx->verify_cb(0, ctx);
1361 if (!ok)
1362 goto err;
1363 }
1364 }
1365
1366 if (crl->idp_flags & IDP_INVALID) {
1367 ctx->error = X509_V_ERR_INVALID_EXTENSION;
1368 ok = ctx->verify_cb(0, ctx);
1369 if (!ok)
1370 goto err;
1371 }
1372
1373
1374 }
1375
1376 if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
1377 ok = check_crl_time(ctx, crl, 1);
1378 if (!ok)
1379 goto err;
1380 }
1381
1382 /* Attempt to get issuer certificate public key */
1383 ikey = X509_get_pubkey(issuer);
1384
1385 if (!ikey) {
1386 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1387 ok = ctx->verify_cb(0, ctx);
1388 if (!ok)
1389 goto err;
1390 } else {
1391 /* Verify CRL signature */
1392 if (X509_CRL_verify(crl, ikey) <= 0) {
1393 ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
1394 ok = ctx->verify_cb(0, ctx);
1395 if (!ok)
1396 goto err;
1397 }
1398 }
1399 }
1400
1401 ok = 1;
1402
1403err:
1404 EVP_PKEY_free(ikey);
1405 return ok;
1406}
1407
1408/* Check certificate against CRL */
1409static int
1410cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
1411{
1412 int ok;
1413 X509_REVOKED *rev;
1414
1415 /* The rules changed for this... previously if a CRL contained
1416 * unhandled critical extensions it could still be used to indicate
1417 * a certificate was revoked. This has since been changed since
1418 * critical extension can change the meaning of CRL entries.
1419 */
1420 if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) &&
1421 (crl->flags & EXFLAG_CRITICAL)) {
1422 ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
1423 ok = ctx->verify_cb(0, ctx);
1424 if (!ok)
1425 return 0;
1426 }
1427 /* Look for serial number of certificate in CRL
1428 * If found make sure reason is not removeFromCRL.
1429 */
1430 if (X509_CRL_get0_by_cert(crl, &rev, x)) {
1431 if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
1432 return 2;
1433 ctx->error = X509_V_ERR_CERT_REVOKED;
1434 ok = ctx->verify_cb(0, ctx);
1435 if (!ok)
1436 return 0;
1437 }
1438
1439 return 1;
1440}
1441
1442static int
1443check_policy(X509_STORE_CTX *ctx)
1444{
1445 int ret;
1446
1447 if (ctx->parent)
1448 return 1;
1449 ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
1450 ctx->param->policies, ctx->param->flags);
1451 if (ret == 0) {
1452 X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
1453 return 0;
1454 }
1455 /* Invalid or inconsistent extensions */
1456 if (ret == -1) {
1457 /* Locate certificates with bad extensions and notify
1458 * callback.
1459 */
1460 X509 *x;
1461 int i;
1462 for (i = 1; i < sk_X509_num(ctx->chain); i++) {
1463 x = sk_X509_value(ctx->chain, i);
1464 if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
1465 continue;
1466 ctx->current_cert = x;
1467 ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
1468 if (!ctx->verify_cb(0, ctx))
1469 return 0;
1470 }
1471 return 1;
1472 }
1473 if (ret == -2) {
1474 ctx->current_cert = NULL;
1475 ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
1476 return ctx->verify_cb(0, ctx);
1477 }
1478
1479 if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
1480 ctx->current_cert = NULL;
1481 ctx->error = X509_V_OK;
1482 if (!ctx->verify_cb(2, ctx))
1483 return 0;
1484 }
1485
1486 return 1;
1487}
1488
1489int
1490x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int quiet)
1491{
1492 time_t *ptime;
1493 int i;
1494
1495 if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
1496 ptime = &ctx->param->check_time;
1497 else
1498 ptime = NULL;
1499
1500 i = X509_cmp_time(X509_get_notBefore(x), ptime);
1501 if (i == 0) {
1502 if (quiet)
1503 return 0;
1504 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
1505 ctx->current_cert = x;
1506 if (!ctx->verify_cb(0, ctx))
1507 return 0;
1508 }
1509
1510 if (i > 0) {
1511 if (quiet)
1512 return 0;
1513 ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
1514 ctx->current_cert = x;
1515 if (!ctx->verify_cb(0, ctx))
1516 return 0;
1517 }
1518
1519 i = X509_cmp_time(X509_get_notAfter(x), ptime);
1520 if (i == 0) {
1521 if (quiet)
1522 return 0;
1523 ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
1524 ctx->current_cert = x;
1525 if (!ctx->verify_cb(0, ctx))
1526 return 0;
1527 }
1528
1529 if (i < 0) {
1530 if (quiet)
1531 return 0;
1532 ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
1533 ctx->current_cert = x;
1534 if (!ctx->verify_cb(0, ctx))
1535 return 0;
1536 }
1537
1538 return 1;
1539}
1540
1541static int
1542internal_verify(X509_STORE_CTX *ctx)
1543{
1544 int ok = 0, n;
1545 X509 *xs, *xi;
1546 EVP_PKEY *pkey = NULL;
1547 int (*cb)(int xok, X509_STORE_CTX *xctx);
1548
1549 cb = ctx->verify_cb;
1550
1551 n = sk_X509_num(ctx->chain);
1552 ctx->error_depth = n - 1;
1553 n--;
1554 xi = sk_X509_value(ctx->chain, n);
1555
1556 if (ctx->check_issued(ctx, xi, xi))
1557 xs = xi;
1558 else {
1559 if (n <= 0) {
1560 ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
1561 ctx->current_cert = xi;
1562 ok = cb(0, ctx);
1563 goto end;
1564 } else {
1565 n--;
1566 ctx->error_depth = n;
1567 xs = sk_X509_value(ctx->chain, n);
1568 }
1569 }
1570
1571/* ctx->error=0; not needed */
1572 while (n >= 0) {
1573 ctx->error_depth = n;
1574
1575 /* Skip signature check for self signed certificates unless
1576 * explicitly asked for. It doesn't add any security and
1577 * just wastes time.
1578 */
1579 if (!xs->valid && (xs != xi ||
1580 (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
1581 if ((pkey = X509_get_pubkey(xi)) == NULL) {
1582 ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
1583 ctx->current_cert = xi;
1584 ok = (*cb)(0, ctx);
1585 if (!ok)
1586 goto end;
1587 } else if (X509_verify(xs, pkey) <= 0) {
1588 ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
1589 ctx->current_cert = xs;
1590 ok = (*cb)(0, ctx);
1591 if (!ok) {
1592 EVP_PKEY_free(pkey);
1593 goto end;
1594 }
1595 }
1596 EVP_PKEY_free(pkey);
1597 pkey = NULL;
1598 }
1599
1600 xs->valid = 1;
1601
1602 ok = x509_check_cert_time(ctx, xs, 0);
1603 if (!ok)
1604 goto end;
1605
1606 /* The last error (if any) is still in the error value */
1607 ctx->current_issuer = xi;
1608 ctx->current_cert = xs;
1609 ok = (*cb)(1, ctx);
1610 if (!ok)
1611 goto end;
1612
1613 n--;
1614 if (n >= 0) {
1615 xi = xs;
1616 xs = sk_X509_value(ctx->chain, n);
1617 }
1618 }
1619 ok = 1;
1620
1621end:
1622 return ok;
1623}
1624
1625int
1626X509_cmp_current_time(const ASN1_TIME *ctm)
1627{
1628 return X509_cmp_time(ctm, NULL);
1629}
1630
1631int
1632X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
1633{
1634 char *str;
1635 ASN1_TIME atm;
1636 long offset;
1637 char buff1[24], buff2[24], *p;
1638 int i, j;
1639
1640 p = buff1;
1641 i = ctm->length;
1642 str = (char *)ctm->data;
1643 if (ctm->type == V_ASN1_UTCTIME) {
1644 if ((i < 11) || (i > 17))
1645 return 0;
1646 memcpy(p, str, 10);
1647 p += 10;
1648 str += 10;
1649 i -= 10;
1650 } else {
1651 if (i < 13)
1652 return 0;
1653 memcpy(p, str, 12);
1654 p += 12;
1655 str += 12;
1656 i -= 12;
1657 }
1658
1659 if (i < 1)
1660 return 0;
1661 if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
1662 *(p++) = '0';
1663 *(p++) = '0';
1664 } else {
1665 if (i < 2)
1666 return 0;
1667 *(p++) = *(str++);
1668 *(p++) = *(str++);
1669 i -= 2;
1670 if (i < 1)
1671 return 0;
1672 /* Skip any fractional seconds... */
1673 if (*str == '.') {
1674 str++;
1675 i--;
1676 while (i > 1 && (*str >= '0') && (*str <= '9')) {
1677 str++;
1678 i--;
1679 }
1680 }
1681 }
1682 *(p++) = 'Z';
1683 *(p++) = '\0';
1684
1685 if (i < 1)
1686 return 0;
1687 if (*str == 'Z') {
1688 if (i != 1)
1689 return 0;
1690 offset = 0;
1691 } else {
1692 if (i != 5)
1693 return 0;
1694 if ((*str != '+') && (*str != '-'))
1695 return 0;
1696 if (str[1] < '0' || str[1] > '9' ||
1697 str[2] < '0' || str[2] > '9' ||
1698 str[3] < '0' || str[3] > '9' ||
1699 str[4] < '0' || str[4] > '9')
1700 return 0;
1701 offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
1702 offset += (str[3] - '0') * 10 + (str[4] - '0');
1703 if (*str == '-')
1704 offset = -offset;
1705 }
1706 atm.type = ctm->type;
1707 atm.flags = 0;
1708 atm.length = sizeof(buff2);
1709 atm.data = (unsigned char *)buff2;
1710
1711 if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
1712 return 0;
1713
1714 if (ctm->type == V_ASN1_UTCTIME) {
1715 i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
1716 if (i < 50)
1717 i += 100; /* cf. RFC 2459 */
1718 j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
1719 if (j < 50)
1720 j += 100;
1721 if (i < j)
1722 return -1;
1723 if (i > j)
1724 return 1;
1725 }
1726 i = strcmp(buff1, buff2);
1727 if (i == 0) /* wait a second then return younger :-) */
1728 return -1;
1729 else
1730 return i;
1731}
1732
1733ASN1_TIME *
1734X509_gmtime_adj(ASN1_TIME *s, long adj)
1735{
1736 return X509_time_adj(s, adj, NULL);
1737}
1738
1739ASN1_TIME *
1740X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
1741{
1742 return X509_time_adj_ex(s, 0, offset_sec, in_tm);
1743}
1744
1745ASN1_TIME *
1746X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec, time_t *in_tm)
1747{
1748 time_t t;
1749
1750 if (in_tm)
1751 t = *in_tm;
1752 else
1753 time(&t);
1754
1755 if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
1756 if (s->type == V_ASN1_UTCTIME)
1757 return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
1758 if (s->type == V_ASN1_GENERALIZEDTIME)
1759 return ASN1_GENERALIZEDTIME_adj(s, t, offset_day,
1760 offset_sec);
1761 }
1762 return ASN1_TIME_adj(s, t, offset_day, offset_sec);
1763}
1764
1765int
1766X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
1767{
1768 EVP_PKEY *ktmp = NULL, *ktmp2;
1769 int i, j;
1770
1771 if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
1772 return 1;
1773
1774 for (i = 0; i < sk_X509_num(chain); i++) {
1775 ktmp = X509_get_pubkey(sk_X509_value(chain, i));
1776 if (ktmp == NULL) {
1777 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1778 X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
1779 return 0;
1780 }
1781 if (!EVP_PKEY_missing_parameters(ktmp))
1782 break;
1783 else {
1784 EVP_PKEY_free(ktmp);
1785 ktmp = NULL;
1786 }
1787 }
1788 if (ktmp == NULL) {
1789 X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
1790 X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
1791 return 0;
1792 }
1793
1794 /* first, populate the other certs */
1795 for (j = i - 1; j >= 0; j--) {
1796 ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
1797 EVP_PKEY_copy_parameters(ktmp2, ktmp);
1798 EVP_PKEY_free(ktmp2);
1799 }
1800
1801 if (pkey != NULL)
1802 EVP_PKEY_copy_parameters(pkey, ktmp);
1803 EVP_PKEY_free(ktmp);
1804 return 1;
1805}
1806
1807int
1808X509_STORE_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
1809 CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
1810{
1811 /* This function is (usually) called only once, by
1812 * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c). */
1813 return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX,
1814 argl, argp, new_func, dup_func, free_func);
1815}
1816
1817int
1818X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
1819{
1820 return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
1821}
1822
1823void *
1824X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
1825{
1826 return CRYPTO_get_ex_data(&ctx->ex_data, idx);
1827}
1828
1829int
1830X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
1831{
1832 return ctx->error;
1833}
1834
1835void
1836X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
1837{
1838 ctx->error = err;
1839}
1840
1841int
1842X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
1843{
1844 return ctx->error_depth;
1845}
1846
1847X509 *
1848X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
1849{
1850 return ctx->current_cert;
1851}
1852
1853STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
1854{
1855 return ctx->chain;
1856}
1857
1858STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
1859{
1860 int i;
1861 X509 *x;
1862 STACK_OF(X509) *chain;
1863
1864 if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain)))
1865 return NULL;
1866 for (i = 0; i < sk_X509_num(chain); i++) {
1867 x = sk_X509_value(chain, i);
1868 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1869 }
1870 return chain;
1871}
1872
1873X509 *
1874X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
1875{
1876 return ctx->current_issuer;
1877}
1878
1879X509_CRL *
1880X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
1881{
1882 return ctx->current_crl;
1883}
1884
1885X509_STORE_CTX *
1886X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
1887{
1888 return ctx->parent;
1889}
1890
1891void
1892X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
1893{
1894 ctx->cert = x;
1895}
1896
1897void
1898X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
1899{
1900 ctx->untrusted = sk;
1901}
1902
1903void
1904X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
1905{
1906 ctx->crls = sk;
1907}
1908
1909int
1910X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
1911{
1912 return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
1913}
1914
1915int
1916X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
1917{
1918 return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
1919}
1920
1921/* This function is used to set the X509_STORE_CTX purpose and trust
1922 * values. This is intended to be used when another structure has its
1923 * own trust and purpose values which (if set) will be inherited by
1924 * the ctx. If they aren't set then we will usually have a default
1925 * purpose in mind which should then be used to set the trust value.
1926 * An example of this is SSL use: an SSL structure will have its own
1927 * purpose and trust settings which the application can set: if they
1928 * aren't set then we use the default of SSL client/server.
1929 */
1930
1931int
1932X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
1933 int purpose, int trust)
1934{
1935 int idx;
1936
1937 /* If purpose not set use default */
1938 if (!purpose)
1939 purpose = def_purpose;
1940 /* If we have a purpose then check it is valid */
1941 if (purpose) {
1942 X509_PURPOSE *ptmp;
1943 idx = X509_PURPOSE_get_by_id(purpose);
1944 if (idx == -1) {
1945 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1946 X509_R_UNKNOWN_PURPOSE_ID);
1947 return 0;
1948 }
1949 ptmp = X509_PURPOSE_get0(idx);
1950 if (ptmp->trust == X509_TRUST_DEFAULT) {
1951 idx = X509_PURPOSE_get_by_id(def_purpose);
1952 if (idx == -1) {
1953 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1954 X509_R_UNKNOWN_PURPOSE_ID);
1955 return 0;
1956 }
1957 ptmp = X509_PURPOSE_get0(idx);
1958 }
1959 /* If trust not set then get from purpose default */
1960 if (!trust)
1961 trust = ptmp->trust;
1962 }
1963 if (trust) {
1964 idx = X509_TRUST_get_by_id(trust);
1965 if (idx == -1) {
1966 X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
1967 X509_R_UNKNOWN_TRUST_ID);
1968 return 0;
1969 }
1970 }
1971
1972 if (purpose && !ctx->param->purpose)
1973 ctx->param->purpose = purpose;
1974 if (trust && !ctx->param->trust)
1975 ctx->param->trust = trust;
1976 return 1;
1977}
1978
1979X509_STORE_CTX *
1980X509_STORE_CTX_new(void)
1981{
1982 X509_STORE_CTX *ctx;
1983
1984 ctx = calloc(1, sizeof(X509_STORE_CTX));
1985 if (!ctx) {
1986 X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
1987 return NULL;
1988 }
1989 return ctx;
1990}
1991
1992void
1993X509_STORE_CTX_free(X509_STORE_CTX *ctx)
1994{
1995 if (ctx == NULL)
1996 return;
1997
1998 X509_STORE_CTX_cleanup(ctx);
1999 free(ctx);
2000}
2001
2002int
2003X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
2004 STACK_OF(X509) *chain)
2005{
2006 int param_ret = 1;
2007
2008 /*
2009 * Make sure everything is initialized properly even in case of an
2010 * early return due to an error.
2011 *
2012 * While this 'ctx' can be reused, X509_STORE_CTX_cleanup() will have
2013 * freed everything and memset ex_data anyway. This also allows us
2014 * to safely use X509_STORE_CTX variables from the stack which will
2015 * have uninitialized data.
2016 */
2017 memset(ctx, 0, sizeof(*ctx));
2018
2019 /*
2020 * Set values other than 0. Keep this in the same order as
2021 * X509_STORE_CTX except for values that may fail. All fields that
2022 * may fail should go last to make sure 'ctx' is as consistent as
2023 * possible even on early exits.
2024 */
2025 ctx->ctx = store;
2026 ctx->cert = x509;
2027 ctx->untrusted = chain;
2028
2029 if (store && store->verify)
2030 ctx->verify = store->verify;
2031 else
2032 ctx->verify = internal_verify;
2033
2034 if (store && store->verify_cb)
2035 ctx->verify_cb = store->verify_cb;
2036 else
2037 ctx->verify_cb = null_callback;
2038
2039 if (store && store->get_issuer)
2040 ctx->get_issuer = store->get_issuer;
2041 else
2042 ctx->get_issuer = X509_STORE_CTX_get1_issuer;
2043
2044 if (store && store->check_issued)
2045 ctx->check_issued = store->check_issued;
2046 else
2047 ctx->check_issued = check_issued;
2048
2049 if (store && store->check_revocation)
2050 ctx->check_revocation = store->check_revocation;
2051 else
2052 ctx->check_revocation = check_revocation;
2053
2054 if (store && store->get_crl)
2055 ctx->get_crl = store->get_crl;
2056 else
2057 ctx->get_crl = NULL;
2058
2059 if (store && store->check_crl)
2060 ctx->check_crl = store->check_crl;
2061 else
2062 ctx->check_crl = check_crl;
2063
2064 if (store && store->cert_crl)
2065 ctx->cert_crl = store->cert_crl;
2066 else
2067 ctx->cert_crl = cert_crl;
2068
2069 ctx->check_policy = check_policy;
2070
2071 if (store && store->lookup_certs)
2072 ctx->lookup_certs = store->lookup_certs;
2073 else
2074 ctx->lookup_certs = X509_STORE_get1_certs;
2075
2076 if (store && store->lookup_crls)
2077 ctx->lookup_crls = store->lookup_crls;
2078 else
2079 ctx->lookup_crls = X509_STORE_get1_crls;
2080
2081 if (store && store->cleanup)
2082 ctx->cleanup = store->cleanup;
2083 else
2084 ctx->cleanup = NULL;
2085
2086 ctx->param = X509_VERIFY_PARAM_new();
2087 if (!ctx->param) {
2088 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2089 return 0;
2090 }
2091
2092 /* Inherit callbacks and flags from X509_STORE if not set
2093 * use defaults.
2094 */
2095 if (store)
2096 param_ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
2097 else
2098 ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT|X509_VP_FLAG_ONCE;
2099
2100 if (param_ret)
2101 param_ret = X509_VERIFY_PARAM_inherit(ctx->param,
2102 X509_VERIFY_PARAM_lookup("default"));
2103
2104 if (param_ret == 0) {
2105 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2106 return 0;
2107 }
2108
2109 if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
2110 &(ctx->ex_data)) == 0) {
2111 X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
2112 return 0;
2113 }
2114 return 1;
2115}
2116
2117/* Set alternative lookup method: just a STACK of trusted certificates.
2118 * This avoids X509_STORE nastiness where it isn't needed.
2119 */
2120
2121void
2122X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
2123{
2124 ctx->other_ctx = sk;
2125 ctx->get_issuer = get_issuer_sk;
2126}
2127
2128void
2129X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
2130{
2131 if (ctx->cleanup)
2132 ctx->cleanup(ctx);
2133 if (ctx->param != NULL) {
2134 if (ctx->parent == NULL)
2135 X509_VERIFY_PARAM_free(ctx->param);
2136 ctx->param = NULL;
2137 }
2138 if (ctx->tree != NULL) {
2139 X509_policy_tree_free(ctx->tree);
2140 ctx->tree = NULL;
2141 }
2142 if (ctx->chain != NULL) {
2143 sk_X509_pop_free(ctx->chain, X509_free);
2144 ctx->chain = NULL;
2145 }
2146 CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX,
2147 ctx, &(ctx->ex_data));
2148 memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
2149}
2150
2151void
2152X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
2153{
2154 X509_VERIFY_PARAM_set_depth(ctx->param, depth);
2155}
2156
2157void
2158X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
2159{
2160 X509_VERIFY_PARAM_set_flags(ctx->param, flags);
2161}
2162
2163void
2164X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, time_t t)
2165{
2166 X509_VERIFY_PARAM_set_time(ctx->param, t);
2167}
2168
2169void
2170X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
2171 int (*verify_cb)(int, X509_STORE_CTX *))
2172{
2173 ctx->verify_cb = verify_cb;
2174}
2175
2176X509_POLICY_TREE *
2177X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
2178{
2179 return ctx->tree;
2180}
2181
2182int
2183X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
2184{
2185 return ctx->explicit_policy;
2186}
2187
2188int
2189X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
2190{
2191 const X509_VERIFY_PARAM *param;
2192 param = X509_VERIFY_PARAM_lookup(name);
2193 if (!param)
2194 return 0;
2195 return X509_VERIFY_PARAM_inherit(ctx->param, param);
2196}
2197
2198X509_VERIFY_PARAM *
2199X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
2200{
2201 return ctx->param;
2202}
2203
2204void
2205X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
2206{
2207 if (ctx->param)
2208 X509_VERIFY_PARAM_free(ctx->param);
2209 ctx->param = param;
2210}