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