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