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