1 /* $OpenBSD: x509_purp.c,v 1.6 2021/09/02 12:41:44 job Exp $ */ 2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3 * project 2001. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * licensing@OpenSSL.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 59 #include <stdio.h> 60 #include <string.h> 61 62 #include <openssl/opensslconf.h> 63 64 #include <openssl/err.h> 65 #include <openssl/x509v3.h> 66 #include <openssl/x509_vfy.h> 67 68 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS) 69 #define ku_reject(x, usage) \ 70 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) 71 #define xku_reject(x, usage) \ 72 (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage))) 73 #define ns_reject(x, usage) \ 74 (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage))) 75 76 void x509v3_cache_extensions(X509 *x); 77 78 static int check_ssl_ca(const X509 *x); 79 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, 80 int ca); 81 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, 82 int ca); 83 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, 84 int ca); 85 static int purpose_smime(const X509 *x, int ca); 86 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, 87 int ca); 88 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, 89 int ca); 90 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, 91 int ca); 92 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, 93 int ca); 94 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca); 95 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca); 96 97 static int xp_cmp(const X509_PURPOSE * const *a, const X509_PURPOSE * const *b); 98 static void xptable_free(X509_PURPOSE *p); 99 100 static X509_PURPOSE xstandard[] = { 101 {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, check_purpose_ssl_client, "SSL client", "sslclient", NULL}, 102 {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ssl_server, "SSL server", "sslserver", NULL}, 103 {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL}, 104 {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, "S/MIME signing", "smimesign", NULL}, 105 {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL}, 106 {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, "CRL signing", "crlsign", NULL}, 107 {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any", NULL}, 108 {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper, "OCSP helper", "ocsphelper", NULL}, 109 {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign", NULL}, 110 }; 111 112 #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE)) 113 114 static STACK_OF(X509_PURPOSE) *xptable = NULL; 115 116 static int 117 xp_cmp(const X509_PURPOSE * const *a, const X509_PURPOSE * const *b) 118 { 119 return (*a)->purpose - (*b)->purpose; 120 } 121 122 /* As much as I'd like to make X509_check_purpose use a "const" X509* 123 * I really can't because it does recalculate hashes and do other non-const 124 * things. */ 125 int 126 X509_check_purpose(X509 *x, int id, int ca) 127 { 128 int idx; 129 const X509_PURPOSE *pt; 130 131 if (!(x->ex_flags & EXFLAG_SET)) { 132 CRYPTO_w_lock(CRYPTO_LOCK_X509); 133 x509v3_cache_extensions(x); 134 CRYPTO_w_unlock(CRYPTO_LOCK_X509); 135 if (x->ex_flags & EXFLAG_INVALID) 136 return X509_V_ERR_UNSPECIFIED; 137 } 138 if (id == -1) 139 return 1; 140 idx = X509_PURPOSE_get_by_id(id); 141 if (idx == -1) 142 return -1; 143 pt = X509_PURPOSE_get0(idx); 144 return pt->check_purpose(pt, x, ca); 145 } 146 147 int 148 X509_PURPOSE_set(int *p, int purpose) 149 { 150 if (X509_PURPOSE_get_by_id(purpose) == -1) { 151 X509V3error(X509V3_R_INVALID_PURPOSE); 152 return 0; 153 } 154 *p = purpose; 155 return 1; 156 } 157 158 int 159 X509_PURPOSE_get_count(void) 160 { 161 if (!xptable) 162 return X509_PURPOSE_COUNT; 163 return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT; 164 } 165 166 X509_PURPOSE * 167 X509_PURPOSE_get0(int idx) 168 { 169 if (idx < 0) 170 return NULL; 171 if (idx < (int)X509_PURPOSE_COUNT) 172 return xstandard + idx; 173 return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT); 174 } 175 176 int 177 X509_PURPOSE_get_by_sname(const char *sname) 178 { 179 int i; 180 X509_PURPOSE *xptmp; 181 182 for (i = 0; i < X509_PURPOSE_get_count(); i++) { 183 xptmp = X509_PURPOSE_get0(i); 184 if (!strcmp(xptmp->sname, sname)) 185 return i; 186 } 187 return -1; 188 } 189 190 int 191 X509_PURPOSE_get_by_id(int purpose) 192 { 193 X509_PURPOSE tmp; 194 int idx; 195 196 if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX)) 197 return purpose - X509_PURPOSE_MIN; 198 tmp.purpose = purpose; 199 if (!xptable) 200 return -1; 201 idx = sk_X509_PURPOSE_find(xptable, &tmp); 202 if (idx == -1) 203 return -1; 204 return idx + X509_PURPOSE_COUNT; 205 } 206 207 int 208 X509_PURPOSE_add(int id, int trust, int flags, 209 int (*ck)(const X509_PURPOSE *, const X509 *, int), const char *name, 210 const char *sname, void *arg) 211 { 212 int idx; 213 X509_PURPOSE *ptmp; 214 char *name_dup, *sname_dup; 215 216 name_dup = sname_dup = NULL; 217 218 if (name == NULL || sname == NULL) { 219 X509V3error(X509V3_R_INVALID_NULL_ARGUMENT); 220 return 0; 221 } 222 223 /* This is set according to what we change: application can't set it */ 224 flags &= ~X509_PURPOSE_DYNAMIC; 225 /* This will always be set for application modified trust entries */ 226 flags |= X509_PURPOSE_DYNAMIC_NAME; 227 /* Get existing entry if any */ 228 idx = X509_PURPOSE_get_by_id(id); 229 /* Need a new entry */ 230 if (idx == -1) { 231 if ((ptmp = malloc(sizeof(X509_PURPOSE))) == NULL) { 232 X509V3error(ERR_R_MALLOC_FAILURE); 233 return 0; 234 } 235 ptmp->flags = X509_PURPOSE_DYNAMIC; 236 } else 237 ptmp = X509_PURPOSE_get0(idx); 238 239 if ((name_dup = strdup(name)) == NULL) 240 goto err; 241 if ((sname_dup = strdup(sname)) == NULL) 242 goto err; 243 244 /* free existing name if dynamic */ 245 if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) { 246 free(ptmp->name); 247 free(ptmp->sname); 248 } 249 /* dup supplied name */ 250 ptmp->name = name_dup; 251 ptmp->sname = sname_dup; 252 /* Keep the dynamic flag of existing entry */ 253 ptmp->flags &= X509_PURPOSE_DYNAMIC; 254 /* Set all other flags */ 255 ptmp->flags |= flags; 256 257 ptmp->purpose = id; 258 ptmp->trust = trust; 259 ptmp->check_purpose = ck; 260 ptmp->usr_data = arg; 261 262 /* If its a new entry manage the dynamic table */ 263 if (idx == -1) { 264 if (xptable == NULL && 265 (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) 266 goto err; 267 if (sk_X509_PURPOSE_push(xptable, ptmp) == 0) 268 goto err; 269 } 270 return 1; 271 272 err: 273 free(name_dup); 274 free(sname_dup); 275 if (idx == -1) 276 free(ptmp); 277 X509V3error(ERR_R_MALLOC_FAILURE); 278 return 0; 279 } 280 281 static void 282 xptable_free(X509_PURPOSE *p) 283 { 284 if (!p) 285 return; 286 if (p->flags & X509_PURPOSE_DYNAMIC) { 287 if (p->flags & X509_PURPOSE_DYNAMIC_NAME) { 288 free(p->name); 289 free(p->sname); 290 } 291 free(p); 292 } 293 } 294 295 void 296 X509_PURPOSE_cleanup(void) 297 { 298 sk_X509_PURPOSE_pop_free(xptable, xptable_free); 299 xptable = NULL; 300 } 301 302 int 303 X509_PURPOSE_get_id(const X509_PURPOSE *xp) 304 { 305 return xp->purpose; 306 } 307 308 char * 309 X509_PURPOSE_get0_name(const X509_PURPOSE *xp) 310 { 311 return xp->name; 312 } 313 314 char * 315 X509_PURPOSE_get0_sname(const X509_PURPOSE *xp) 316 { 317 return xp->sname; 318 } 319 320 int 321 X509_PURPOSE_get_trust(const X509_PURPOSE *xp) 322 { 323 return xp->trust; 324 } 325 326 static int 327 nid_cmp(const int *a, const int *b) 328 { 329 return *a - *b; 330 } 331 332 static int nid_cmp_BSEARCH_CMP_FN(const void *, const void *); 333 static int nid_cmp(int const *, int const *); 334 static int *OBJ_bsearch_nid(int *key, int const *base, int num); 335 336 static int 337 nid_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_) 338 { 339 int const *a = a_; 340 int const *b = b_; 341 return nid_cmp(a, b); 342 } 343 344 static int * 345 OBJ_bsearch_nid(int *key, int const *base, int num) 346 { 347 return (int *)OBJ_bsearch_(key, base, num, sizeof(int), 348 nid_cmp_BSEARCH_CMP_FN); 349 } 350 351 int 352 X509_supported_extension(X509_EXTENSION *ex) 353 { 354 /* This table is a list of the NIDs of supported extensions: 355 * that is those which are used by the verify process. If 356 * an extension is critical and doesn't appear in this list 357 * then the verify process will normally reject the certificate. 358 * The list must be kept in numerical order because it will be 359 * searched using bsearch. 360 */ 361 362 static const int supported_nids[] = { 363 NID_netscape_cert_type, /* 71 */ 364 NID_key_usage, /* 83 */ 365 NID_subject_alt_name, /* 85 */ 366 NID_basic_constraints, /* 87 */ 367 NID_certificate_policies, /* 89 */ 368 NID_ext_key_usage, /* 126 */ 369 #ifndef OPENSSL_NO_RFC3779 370 NID_sbgp_ipAddrBlock, /* 290 */ 371 NID_sbgp_autonomousSysNum, /* 291 */ 372 #endif 373 NID_policy_constraints, /* 401 */ 374 NID_proxyCertInfo, /* 663 */ 375 NID_name_constraints, /* 666 */ 376 NID_policy_mappings, /* 747 */ 377 NID_inhibit_any_policy /* 748 */ 378 }; 379 380 int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); 381 382 if (ex_nid == NID_undef) 383 return 0; 384 385 if (OBJ_bsearch_nid(&ex_nid, supported_nids, 386 sizeof(supported_nids) / sizeof(int))) 387 return 1; 388 return 0; 389 } 390 391 static void 392 setup_dp(X509 *x, DIST_POINT *dp) 393 { 394 X509_NAME *iname = NULL; 395 int i; 396 397 if (dp->reasons) { 398 if (dp->reasons->length > 0) 399 dp->dp_reasons = dp->reasons->data[0]; 400 if (dp->reasons->length > 1) 401 dp->dp_reasons |= (dp->reasons->data[1] << 8); 402 dp->dp_reasons &= CRLDP_ALL_REASONS; 403 } else 404 dp->dp_reasons = CRLDP_ALL_REASONS; 405 if (!dp->distpoint || (dp->distpoint->type != 1)) 406 return; 407 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { 408 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); 409 if (gen->type == GEN_DIRNAME) { 410 iname = gen->d.directoryName; 411 break; 412 } 413 } 414 if (!iname) 415 iname = X509_get_issuer_name(x); 416 417 DIST_POINT_set_dpname(dp->distpoint, iname); 418 419 } 420 421 static void 422 setup_crldp(X509 *x) 423 { 424 int i; 425 426 x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL); 427 if (x->crldp == NULL && i != -1) { 428 x->ex_flags |= EXFLAG_INVALID; 429 return; 430 } 431 432 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) 433 setup_dp(x, sk_DIST_POINT_value(x->crldp, i)); 434 } 435 436 void 437 x509v3_cache_extensions(X509 *x) 438 { 439 BASIC_CONSTRAINTS *bs; 440 PROXY_CERT_INFO_EXTENSION *pci; 441 ASN1_BIT_STRING *usage; 442 ASN1_BIT_STRING *ns; 443 EXTENDED_KEY_USAGE *extusage; 444 X509_EXTENSION *ex; 445 int i; 446 447 if (x->ex_flags & EXFLAG_SET) 448 return; 449 450 #ifndef OPENSSL_NO_SHA 451 X509_digest(x, EVP_sha1(), x->sha1_hash, NULL); 452 #endif 453 454 /* V1 should mean no extensions ... */ 455 if (!X509_get_version(x)) 456 x->ex_flags |= EXFLAG_V1; 457 458 /* Handle basic constraints */ 459 if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL))) { 460 if (bs->ca) 461 x->ex_flags |= EXFLAG_CA; 462 if (bs->pathlen) { 463 if ((bs->pathlen->type == V_ASN1_NEG_INTEGER) || 464 !bs->ca) { 465 x->ex_flags |= EXFLAG_INVALID; 466 x->ex_pathlen = 0; 467 } else 468 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen); 469 } else 470 x->ex_pathlen = -1; 471 BASIC_CONSTRAINTS_free(bs); 472 x->ex_flags |= EXFLAG_BCONS; 473 } else if (i != -1) { 474 x->ex_flags |= EXFLAG_INVALID; 475 } 476 477 /* Handle proxy certificates */ 478 if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) { 479 if (x->ex_flags & EXFLAG_CA || 480 X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0 || 481 X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) { 482 x->ex_flags |= EXFLAG_INVALID; 483 } 484 if (pci->pcPathLengthConstraint) { 485 if (pci->pcPathLengthConstraint->type == 486 V_ASN1_NEG_INTEGER) { 487 x->ex_flags |= EXFLAG_INVALID; 488 x->ex_pcpathlen = 0; 489 } else 490 x->ex_pcpathlen = 491 ASN1_INTEGER_get(pci-> 492 pcPathLengthConstraint); 493 } else 494 x->ex_pcpathlen = -1; 495 PROXY_CERT_INFO_EXTENSION_free(pci); 496 x->ex_flags |= EXFLAG_PROXY; 497 } else if (i != -1) { 498 x->ex_flags |= EXFLAG_INVALID; 499 } 500 501 /* Handle key usage */ 502 if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) { 503 if (usage->length > 0) { 504 x->ex_kusage = usage->data[0]; 505 if (usage->length > 1) 506 x->ex_kusage |= usage->data[1] << 8; 507 } else 508 x->ex_kusage = 0; 509 x->ex_flags |= EXFLAG_KUSAGE; 510 ASN1_BIT_STRING_free(usage); 511 } else if (i != -1) { 512 x->ex_flags |= EXFLAG_INVALID; 513 } 514 515 x->ex_xkusage = 0; 516 if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) { 517 x->ex_flags |= EXFLAG_XKUSAGE; 518 for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { 519 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { 520 case NID_server_auth: 521 x->ex_xkusage |= XKU_SSL_SERVER; 522 break; 523 524 case NID_client_auth: 525 x->ex_xkusage |= XKU_SSL_CLIENT; 526 break; 527 528 case NID_email_protect: 529 x->ex_xkusage |= XKU_SMIME; 530 break; 531 532 case NID_code_sign: 533 x->ex_xkusage |= XKU_CODE_SIGN; 534 break; 535 536 case NID_ms_sgc: 537 case NID_ns_sgc: 538 x->ex_xkusage |= XKU_SGC; 539 break; 540 541 case NID_OCSP_sign: 542 x->ex_xkusage |= XKU_OCSP_SIGN; 543 break; 544 545 case NID_time_stamp: 546 x->ex_xkusage |= XKU_TIMESTAMP; 547 break; 548 549 case NID_dvcs: 550 x->ex_xkusage |= XKU_DVCS; 551 break; 552 } 553 } 554 sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free); 555 } else if (i != -1) { 556 x->ex_flags |= EXFLAG_INVALID; 557 } 558 559 if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) { 560 if (ns->length > 0) 561 x->ex_nscert = ns->data[0]; 562 else 563 x->ex_nscert = 0; 564 x->ex_flags |= EXFLAG_NSCERT; 565 ASN1_BIT_STRING_free(ns); 566 } else if (i != -1) { 567 x->ex_flags |= EXFLAG_INVALID; 568 } 569 570 x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL); 571 if (x->skid == NULL && i != -1) 572 x->ex_flags |= EXFLAG_INVALID; 573 x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL); 574 if (x->akid == NULL && i != -1) 575 x->ex_flags |= EXFLAG_INVALID; 576 577 /* Does subject name match issuer? */ 578 if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) { 579 x->ex_flags |= EXFLAG_SI; 580 /* If SKID matches AKID also indicate self signed. */ 581 if (X509_check_akid(x, x->akid) == X509_V_OK && 582 !ku_reject(x, KU_KEY_CERT_SIGN)) 583 x->ex_flags |= EXFLAG_SS; 584 } 585 586 x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL); 587 if (x->altname == NULL && i != -1) 588 x->ex_flags |= EXFLAG_INVALID; 589 x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL); 590 if (!x->nc && (i != -1)) 591 x->ex_flags |= EXFLAG_INVALID; 592 setup_crldp(x); 593 594 #ifndef OPENSSL_NO_RFC3779 595 x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL); 596 if (x->rfc3779_addr == NULL && i != -1) 597 x->ex_flags |= EXFLAG_INVALID; 598 x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL); 599 if (x->rfc3779_asid == NULL && i != -1) 600 x->ex_flags |= EXFLAG_INVALID; 601 #endif 602 603 for (i = 0; i < X509_get_ext_count(x); i++) { 604 ex = X509_get_ext(x, i); 605 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == 606 NID_freshest_crl) 607 x->ex_flags |= EXFLAG_FRESHEST; 608 if (!X509_EXTENSION_get_critical(ex)) 609 continue; 610 if (!X509_supported_extension(ex)) { 611 x->ex_flags |= EXFLAG_CRITICAL; 612 break; 613 } 614 } 615 x->ex_flags |= EXFLAG_SET; 616 } 617 618 /* CA checks common to all purposes 619 * return codes: 620 * 0 not a CA 621 * 1 is a CA 622 * 2 basicConstraints absent so "maybe" a CA 623 * 3 basicConstraints absent but self signed V1. 624 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted. 625 */ 626 627 static int 628 check_ca(const X509 *x) 629 { 630 /* keyUsage if present should allow cert signing */ 631 if (ku_reject(x, KU_KEY_CERT_SIGN)) 632 return 0; 633 if (x->ex_flags & EXFLAG_BCONS) { 634 if (x->ex_flags & EXFLAG_CA) 635 return 1; 636 /* If basicConstraints says not a CA then say so */ 637 else 638 return 0; 639 } else { 640 /* we support V1 roots for... uh, I don't really know why. */ 641 if ((x->ex_flags & V1_ROOT) == V1_ROOT) 642 return 3; 643 /* If key usage present it must have certSign so tolerate it */ 644 else if (x->ex_flags & EXFLAG_KUSAGE) 645 return 4; 646 /* Older certificates could have Netscape-specific CA types */ 647 else if (x->ex_flags & EXFLAG_NSCERT && 648 x->ex_nscert & NS_ANY_CA) 649 return 5; 650 /* can this still be regarded a CA certificate? I doubt it */ 651 return 0; 652 } 653 } 654 655 int 656 X509_check_ca(X509 *x) 657 { 658 if (!(x->ex_flags & EXFLAG_SET)) { 659 CRYPTO_w_lock(CRYPTO_LOCK_X509); 660 x509v3_cache_extensions(x); 661 CRYPTO_w_unlock(CRYPTO_LOCK_X509); 662 if (x->ex_flags & EXFLAG_INVALID) 663 return X509_V_ERR_UNSPECIFIED; 664 } 665 666 return check_ca(x); 667 } 668 669 /* Check SSL CA: common checks for SSL client and server */ 670 static int 671 check_ssl_ca(const X509 *x) 672 { 673 int ca_ret; 674 675 ca_ret = check_ca(x); 676 if (!ca_ret) 677 return 0; 678 /* check nsCertType if present */ 679 if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA) 680 return ca_ret; 681 else 682 return 0; 683 } 684 685 static int 686 check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca) 687 { 688 if (xku_reject(x, XKU_SSL_CLIENT)) 689 return 0; 690 if (ca) 691 return check_ssl_ca(x); 692 /* We need to do digital signatures with it */ 693 if (ku_reject(x, KU_DIGITAL_SIGNATURE)) 694 return 0; 695 /* nsCertType if present should allow SSL client use */ 696 if (ns_reject(x, NS_SSL_CLIENT)) 697 return 0; 698 return 1; 699 } 700 701 static int 702 check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca) 703 { 704 if (xku_reject(x, XKU_SSL_SERVER|XKU_SGC)) 705 return 0; 706 if (ca) 707 return check_ssl_ca(x); 708 709 if (ns_reject(x, NS_SSL_SERVER)) 710 return 0; 711 /* Now as for keyUsage: we'll at least need to sign OR encipher */ 712 if (ku_reject(x, KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT)) 713 return 0; 714 715 return 1; 716 } 717 718 static int 719 check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca) 720 { 721 int ret; 722 723 ret = check_purpose_ssl_server(xp, x, ca); 724 if (!ret || ca) 725 return ret; 726 /* We need to encipher or Netscape complains */ 727 if (ku_reject(x, KU_KEY_ENCIPHERMENT)) 728 return 0; 729 return ret; 730 } 731 732 /* common S/MIME checks */ 733 static int 734 purpose_smime(const X509 *x, int ca) 735 { 736 if (xku_reject(x, XKU_SMIME)) 737 return 0; 738 if (ca) { 739 int ca_ret; 740 ca_ret = check_ca(x); 741 if (!ca_ret) 742 return 0; 743 /* check nsCertType if present */ 744 if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA) 745 return ca_ret; 746 else 747 return 0; 748 } 749 if (x->ex_flags & EXFLAG_NSCERT) { 750 if (x->ex_nscert & NS_SMIME) 751 return 1; 752 /* Workaround for some buggy certificates */ 753 if (x->ex_nscert & NS_SSL_CLIENT) 754 return 2; 755 return 0; 756 } 757 return 1; 758 } 759 760 static int 761 check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca) 762 { 763 int ret; 764 765 ret = purpose_smime(x, ca); 766 if (!ret || ca) 767 return ret; 768 if (ku_reject(x, KU_DIGITAL_SIGNATURE|KU_NON_REPUDIATION)) 769 return 0; 770 return ret; 771 } 772 773 static int 774 check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca) 775 { 776 int ret; 777 778 ret = purpose_smime(x, ca); 779 if (!ret || ca) 780 return ret; 781 if (ku_reject(x, KU_KEY_ENCIPHERMENT)) 782 return 0; 783 return ret; 784 } 785 786 static int 787 check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca) 788 { 789 if (ca) { 790 int ca_ret; 791 if ((ca_ret = check_ca(x)) != 2) 792 return ca_ret; 793 else 794 return 0; 795 } 796 if (ku_reject(x, KU_CRL_SIGN)) 797 return 0; 798 return 1; 799 } 800 801 /* OCSP helper: this is *not* a full OCSP check. It just checks that 802 * each CA is valid. Additional checks must be made on the chain. 803 */ 804 static int 805 ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca) 806 { 807 /* Must be a valid CA. Should we really support the "I don't know" 808 value (2)? */ 809 if (ca) 810 return check_ca(x); 811 /* leaf certificate is checked in OCSP_verify() */ 812 return 1; 813 } 814 815 static int 816 check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, int ca) 817 { 818 int i_ext; 819 820 /* If ca is true we must return if this is a valid CA certificate. */ 821 if (ca) 822 return check_ca(x); 823 824 /* 825 * Check the optional key usage field: 826 * if Key Usage is present, it must be one of digitalSignature 827 * and/or nonRepudiation (other values are not consistent and shall 828 * be rejected). 829 */ 830 if ((x->ex_flags & EXFLAG_KUSAGE) && 831 ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || 832 !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) 833 return 0; 834 835 /* Only time stamp key usage is permitted and it's required. */ 836 if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP) 837 return 0; 838 839 /* Extended Key Usage MUST be critical */ 840 i_ext = X509_get_ext_by_NID((X509 *) x, NID_ext_key_usage, -1); 841 if (i_ext >= 0) { 842 X509_EXTENSION *ext = X509_get_ext((X509 *) x, i_ext); 843 if (!X509_EXTENSION_get_critical(ext)) 844 return 0; 845 } 846 847 return 1; 848 } 849 850 static int 851 no_check(const X509_PURPOSE *xp, const X509 *x, int ca) 852 { 853 return 1; 854 } 855 856 /* Various checks to see if one certificate issued the second. 857 * This can be used to prune a set of possible issuer certificates 858 * which have been looked up using some simple method such as by 859 * subject name. 860 * These are: 861 * 1. Check issuer_name(subject) == subject_name(issuer) 862 * 2. If akid(subject) exists check it matches issuer 863 * 3. If key_usage(issuer) exists check it supports certificate signing 864 * returns 0 for OK, positive for reason for mismatch, reasons match 865 * codes for X509_verify_cert() 866 */ 867 868 int 869 X509_check_issued(X509 *issuer, X509 *subject) 870 { 871 if (X509_NAME_cmp(X509_get_subject_name(issuer), 872 X509_get_issuer_name(subject))) 873 return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; 874 x509v3_cache_extensions(issuer); 875 if (issuer->ex_flags & EXFLAG_INVALID) 876 return X509_V_ERR_UNSPECIFIED; 877 x509v3_cache_extensions(subject); 878 if (subject->ex_flags & EXFLAG_INVALID) 879 return X509_V_ERR_UNSPECIFIED; 880 881 if (subject->akid) { 882 int ret = X509_check_akid(issuer, subject->akid); 883 if (ret != X509_V_OK) 884 return ret; 885 } 886 887 if (subject->ex_flags & EXFLAG_PROXY) { 888 if (ku_reject(issuer, KU_DIGITAL_SIGNATURE)) 889 return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE; 890 } else if (ku_reject(issuer, KU_KEY_CERT_SIGN)) 891 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN; 892 return X509_V_OK; 893 } 894 895 int 896 X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid) 897 { 898 if (!akid) 899 return X509_V_OK; 900 901 /* Check key ids (if present) */ 902 if (akid->keyid && issuer->skid && 903 ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid) ) 904 return X509_V_ERR_AKID_SKID_MISMATCH; 905 /* Check serial number */ 906 if (akid->serial && 907 ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial)) 908 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; 909 /* Check issuer name */ 910 if (akid->issuer) { 911 /* Ugh, for some peculiar reason AKID includes 912 * SEQUENCE OF GeneralName. So look for a DirName. 913 * There may be more than one but we only take any 914 * notice of the first. 915 */ 916 GENERAL_NAMES *gens; 917 GENERAL_NAME *gen; 918 X509_NAME *nm = NULL; 919 int i; 920 gens = akid->issuer; 921 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { 922 gen = sk_GENERAL_NAME_value(gens, i); 923 if (gen->type == GEN_DIRNAME) { 924 nm = gen->d.dirn; 925 break; 926 } 927 } 928 if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer))) 929 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; 930 } 931 return X509_V_OK; 932 } 933