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