Debian Codegraph @

external/boringssl/crypto/x509/x509_vfy.c

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