133965Sjdp/* $OpenBSD: x509_purp.c,v 1.42 2024/05/15 18:10:03 tb Exp $ */ 2130561Sobrien/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL 3218822Sdim * project 2001. 433965Sjdp */ 533965Sjdp/* ==================================================================== 633965Sjdp * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. 733965Sjdp * 833965Sjdp * Redistribution and use in source and binary forms, with or without 977298Sobrien * modification, are permitted provided that the following conditions 1033965Sjdp * are met: 1133965Sjdp * 1233965Sjdp * 1. Redistributions of source code must retain the above copyright 1333965Sjdp * notice, this list of conditions and the following disclaimer. 1433965Sjdp * 1533965Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1633965Sjdp * notice, this list of conditions and the following disclaimer in 1733965Sjdp * the documentation and/or other materials provided with the 1833965Sjdp * distribution. 19218822Sdim * 20218822Sdim * 3. All advertising materials mentioning features or use of this 2133965Sjdp * software must display the following acknowledgment: 2233965Sjdp * "This product includes software developed by the OpenSSL Project 2333965Sjdp * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2433965Sjdp * 2533965Sjdp * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2633965Sjdp * endorse or promote products derived from this software without 2733965Sjdp * prior written permission. For written permission, please contact 2833965Sjdp * licensing@OpenSSL.org. 2933965Sjdp * 3077298Sobrien * 5. Products derived from this software may not be called "OpenSSL" 3177298Sobrien * nor may "OpenSSL" appear in their names without prior written 3277298Sobrien * permission of the OpenSSL Project. 3360484Sobrien * 3433965Sjdp * 6. Redistributions of any form whatsoever must retain the following 3560484Sobrien * acknowledgment: 3633965Sjdp * "This product includes software developed by the OpenSSL Project 3733965Sjdp * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3833965Sjdp * 3933965Sjdp * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4060484Sobrien * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4160484Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4260484Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4377298Sobrien * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4460484Sobrien * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4560484Sobrien * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4660484Sobrien * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4760484Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4860484Sobrien * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49104834Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50104834Sobrien * OF THE POSSIBILITY OF SUCH DAMAGE. 5160484Sobrien * ==================================================================== 5260484Sobrien * 5360484Sobrien * This product includes cryptographic software written by Eric Young 5460484Sobrien * (eay@cryptsoft.com). This product includes software written by Tim 5560484Sobrien * Hudson (tjh@cryptsoft.com). 5660484Sobrien * 5760484Sobrien */ 5860484Sobrien 5933965Sjdp#include <stdio.h> 6033965Sjdp#include <string.h> 6133965Sjdp 6260484Sobrien#include <openssl/opensslconf.h> 6360484Sobrien 6460484Sobrien#include <openssl/err.h> 6533965Sjdp#include <openssl/x509v3.h> 6633965Sjdp#include <openssl/x509_vfy.h> 6733965Sjdp 6833965Sjdp#include "x509_internal.h" 6933965Sjdp#include "x509_local.h" 7033965Sjdp 7160484Sobrienstruct x509_purpose_st { 7260484Sobrien int purpose; 7360484Sobrien int trust; /* Default trust ID */ 7460484Sobrien int flags; 7560484Sobrien int (*check_purpose)(const struct x509_purpose_st *, const X509 *, int); 7660484Sobrien char *name; 7760484Sobrien char *sname; 7860484Sobrien void *usr_data; 7933965Sjdp} /* X509_PURPOSE */; 8033965Sjdp 8133965Sjdp#define V1_ROOT (EXFLAG_V1|EXFLAG_SS) 8233965Sjdp#define ku_reject(x, usage) \ 8377298Sobrien (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) 84218822Sdim#define xku_reject(x, usage) \ 8577298Sobrien (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage))) 8677298Sobrien#define ns_reject(x, usage) \ 87104834Sobrien (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage))) 8877298Sobrien 89104834Sobrienstatic int check_ssl_ca(const X509 *x); 9077298Sobrienstatic int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, 9133965Sjdp int ca); 9233965Sjdpstatic int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, 9333965Sjdp int ca); 9433965Sjdpstatic int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, 9533965Sjdp int ca); 9677298Sobrienstatic int purpose_smime(const X509 *x, int ca); 9733965Sjdpstatic int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, 9877298Sobrien int ca); 99130561Sobrienstatic int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, 10033965Sjdp int ca); 10133965Sjdpstatic int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, 10277298Sobrien int ca); 10333965Sjdpstatic int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, 10460484Sobrien int ca); 10560484Sobrienstatic int no_check(const X509_PURPOSE *xp, const X509 *x, int ca); 10660484Sobrienstatic int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca); 10760484Sobrien 10860484Sobrienstatic const X509_PURPOSE xstandard[] = { 10933965Sjdp { 11060484Sobrien .purpose = X509_PURPOSE_SSL_CLIENT, 11160484Sobrien .trust = X509_TRUST_SSL_CLIENT, 11233965Sjdp .check_purpose = check_purpose_ssl_client, 11360484Sobrien .name = "SSL client", 11460484Sobrien .sname = "sslclient", 11560484Sobrien }, 11660484Sobrien { 11760484Sobrien .purpose = X509_PURPOSE_SSL_SERVER, 11833965Sjdp .trust = X509_TRUST_SSL_SERVER, 11960484Sobrien .check_purpose = check_purpose_ssl_server, 12060484Sobrien .name = "SSL server", 12133965Sjdp .sname = "sslserver", 122130561Sobrien }, 12360484Sobrien { 12460484Sobrien .purpose = X509_PURPOSE_NS_SSL_SERVER, 12560484Sobrien .trust = X509_TRUST_SSL_SERVER, 12660484Sobrien .check_purpose = check_purpose_ns_ssl_server, 12733965Sjdp .name = "Netscape SSL server", 12860484Sobrien .sname = "nssslserver", 12960484Sobrien }, 13033965Sjdp { 13133965Sjdp .purpose = X509_PURPOSE_SMIME_SIGN, 13233965Sjdp .trust = X509_TRUST_EMAIL, 13360484Sobrien .check_purpose = check_purpose_smime_sign, 13433965Sjdp .name = "S/MIME signing", 13560484Sobrien .sname = "smimesign", 136130561Sobrien }, 13733965Sjdp { 13877298Sobrien .purpose = X509_PURPOSE_SMIME_ENCRYPT, 13977298Sobrien .trust = X509_TRUST_EMAIL, 14077298Sobrien .check_purpose = check_purpose_smime_encrypt, 141130561Sobrien .name = "S/MIME encryption", 14277298Sobrien .sname = "smimeencrypt", 14360484Sobrien }, 14433965Sjdp { 14560484Sobrien .purpose = X509_PURPOSE_CRL_SIGN, 146130561Sobrien .trust = X509_TRUST_COMPAT, 14733965Sjdp .check_purpose = check_purpose_crl_sign, 148218822Sdim .name = "CRL signing", 149218822Sdim .sname = "crlsign", 150218822Sdim }, 151218822Sdim { 152218822Sdim .purpose = X509_PURPOSE_ANY, 153218822Sdim .trust = X509_TRUST_DEFAULT, 154218822Sdim .check_purpose = no_check, 15577298Sobrien .name = "Any Purpose", 15633965Sjdp .sname = "any", 15777298Sobrien }, 158218822Sdim { 15933965Sjdp .purpose = X509_PURPOSE_OCSP_HELPER, 160130561Sobrien .trust = X509_TRUST_COMPAT, 16133965Sjdp .check_purpose = ocsp_helper, 162130561Sobrien .name = "OCSP helper", 163130561Sobrien .sname = "ocsphelper", 164130561Sobrien }, 165130561Sobrien { 166130561Sobrien .purpose = X509_PURPOSE_TIMESTAMP_SIGN, 167130561Sobrien .trust = X509_TRUST_TSA, 168130561Sobrien .check_purpose = check_purpose_timestamp_sign, 169130561Sobrien .name = "Time Stamp signing", 170130561Sobrien .sname = "timestampsign", 171130561Sobrien }, 17233965Sjdp}; 17333965Sjdp 174130561Sobrien#define X509_PURPOSE_COUNT (sizeof(xstandard) / sizeof(xstandard[0])) 17560484Sobrien 17633965Sjdp/* As much as I'd like to make X509_check_purpose use a "const" X509* 17760484Sobrien * I really can't because it does recalculate hashes and do other non-const 17833965Sjdp * things. */ 179130561Sobrienint 18060484SobrienX509_check_purpose(X509 *x, int id, int ca) 18160484Sobrien{ 18260484Sobrien int idx; 18360484Sobrien const X509_PURPOSE *pt; 184130561Sobrien 18560484Sobrien if (!x509v3_cache_extensions(x)) 18660484Sobrien return -1; 18760484Sobrien 18860484Sobrien if (id == -1) 189130561Sobrien return 1; 19060484Sobrien 19189857Sobrien if ((idx = X509_PURPOSE_get_by_id(id)) == -1) 19289857Sobrien return -1; 19360484Sobrien if ((pt = X509_PURPOSE_get0(idx)) == NULL) 19433965Sjdp return -1; 19577298Sobrien 19677298Sobrien return pt->check_purpose(pt, x, ca); 19777298Sobrien} 19877298SobrienLCRYPTO_ALIAS(X509_check_purpose); 19977298Sobrien 20077298Sobrienint 20133965SjdpX509_PURPOSE_get_count(void) 20233965Sjdp{ 20333965Sjdp return X509_PURPOSE_COUNT; 20433965Sjdp} 20533965SjdpLCRYPTO_ALIAS(X509_PURPOSE_get_count); 206130561Sobrien 20733965Sjdpconst X509_PURPOSE * 20833965SjdpX509_PURPOSE_get0(int idx) 20960484Sobrien{ 21033965Sjdp if (idx < 0 || (size_t)idx >= X509_PURPOSE_COUNT) 21133965Sjdp return NULL; 21233965Sjdp 21360484Sobrien return &xstandard[idx]; 21460484Sobrien} 21533965SjdpLCRYPTO_ALIAS(X509_PURPOSE_get0); 21660484Sobrien 21760484Sobrienint 21877298SobrienX509_PURPOSE_get_by_sname(const char *sname) 21960484Sobrien{ 22060484Sobrien int i; 22160484Sobrien const X509_PURPOSE *xptmp; 22260484Sobrien 22360484Sobrien for (i = 0; i < X509_PURPOSE_get_count(); i++) { 22433965Sjdp xptmp = X509_PURPOSE_get0(i); 22533965Sjdp if (!strcmp(xptmp->sname, sname)) 22677298Sobrien return i; 22777298Sobrien } 228130561Sobrien return -1; 22960484Sobrien} 23033965SjdpLCRYPTO_ALIAS(X509_PURPOSE_get_by_sname); 23160484Sobrien 23233965Sjdpint 233130561SobrienX509_PURPOSE_get_by_id(int purpose) 23460484Sobrien{ 23533965Sjdp /* 23660484Sobrien * Ensure the purpose identifier is between MIN and MAX inclusive. 23733965Sjdp * If so, translate it to an index into the xstandard[] table. 23833965Sjdp */ 239218822Sdim if (purpose < X509_PURPOSE_MIN || purpose > X509_PURPOSE_MAX) 24033965Sjdp return -1; 24133965Sjdp 242130561Sobrien return purpose - X509_PURPOSE_MIN; 24333965Sjdp} 244218822Sdim 245218822Sdimint 246218822SdimX509_PURPOSE_get_id(const X509_PURPOSE *xp) 24733965Sjdp{ 24833965Sjdp return xp->purpose; 249} 250LCRYPTO_ALIAS(X509_PURPOSE_get_id); 251 252const char * 253X509_PURPOSE_get0_name(const X509_PURPOSE *xp) 254{ 255 return xp->name; 256} 257LCRYPTO_ALIAS(X509_PURPOSE_get0_name); 258 259const char * 260X509_PURPOSE_get0_sname(const X509_PURPOSE *xp) 261{ 262 return xp->sname; 263} 264LCRYPTO_ALIAS(X509_PURPOSE_get0_sname); 265 266int 267X509_PURPOSE_get_trust(const X509_PURPOSE *xp) 268{ 269 return xp->trust; 270} 271 272/* 273 * List of NIDs of extensions supported by the verifier. If an extension 274 * is critical and doesn't appear in this list, then the certificate will 275 * normally be rejected. 276 */ 277int 278X509_supported_extension(X509_EXTENSION *ext) 279{ 280 switch (OBJ_obj2nid(X509_EXTENSION_get_object(ext))) { 281 case NID_basic_constraints: 282 case NID_certificate_policies: 283 case NID_ext_key_usage: 284 case NID_inhibit_any_policy: 285 case NID_key_usage: 286 case NID_name_constraints: 287 case NID_netscape_cert_type: 288 case NID_policy_constraints: 289 case NID_policy_mappings: 290#ifndef OPENSSL_NO_RFC3779 291 case NID_sbgp_ipAddrBlock: 292 case NID_sbgp_autonomousSysNum: 293#endif 294 case NID_subject_alt_name: 295 return 1; 296 default: 297 return 0; 298 } 299} 300LCRYPTO_ALIAS(X509_supported_extension); 301 302static void 303setup_dp(X509 *x, DIST_POINT *dp) 304{ 305 X509_NAME *iname = NULL; 306 int i; 307 308 if (dp->reasons) { 309 if (dp->reasons->length > 0) 310 dp->dp_reasons = dp->reasons->data[0]; 311 if (dp->reasons->length > 1) 312 dp->dp_reasons |= (dp->reasons->data[1] << 8); 313 dp->dp_reasons &= CRLDP_ALL_REASONS; 314 } else 315 dp->dp_reasons = CRLDP_ALL_REASONS; 316 if (!dp->distpoint || (dp->distpoint->type != 1)) 317 return; 318 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { 319 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); 320 if (gen->type == GEN_DIRNAME) { 321 iname = gen->d.directoryName; 322 break; 323 } 324 } 325 if (!iname) 326 iname = X509_get_issuer_name(x); 327 328 DIST_POINT_set_dpname(dp->distpoint, iname); 329} 330 331static void 332setup_crldp(X509 *x) 333{ 334 int i; 335 336 x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL); 337 if (x->crldp == NULL && i != -1) { 338 x->ex_flags |= EXFLAG_INVALID; 339 return; 340 } 341 342 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) 343 setup_dp(x, sk_DIST_POINT_value(x->crldp, i)); 344} 345 346static int 347x509_extension_oid_cmp(const X509_EXTENSION *const *a, 348 const X509_EXTENSION *const *b) 349{ 350 return OBJ_cmp((*a)->object, (*b)->object); 351} 352 353static int 354x509_extension_oids_are_unique(X509 *x509) 355{ 356 STACK_OF(X509_EXTENSION) *exts = NULL; 357 const X509_EXTENSION *prev_ext, *curr_ext; 358 int i; 359 int ret = 0; 360 361 if (X509_get_ext_count(x509) <= 1) 362 goto done; 363 364 if ((exts = sk_X509_EXTENSION_dup(x509->cert_info->extensions)) == NULL) 365 goto err; 366 367 (void)sk_X509_EXTENSION_set_cmp_func(exts, x509_extension_oid_cmp); 368 sk_X509_EXTENSION_sort(exts); 369 370 prev_ext = sk_X509_EXTENSION_value(exts, 0); 371 for (i = 1; i < sk_X509_EXTENSION_num(exts); i++) { 372 curr_ext = sk_X509_EXTENSION_value(exts, i); 373 if (x509_extension_oid_cmp(&prev_ext, &curr_ext) == 0) 374 goto err; 375 prev_ext = curr_ext; 376 } 377 378 done: 379 ret = 1; 380 381 err: 382 sk_X509_EXTENSION_free(exts); 383 384 return ret; 385} 386 387static void 388x509v3_cache_extensions_internal(X509 *x) 389{ 390 BASIC_CONSTRAINTS *bs; 391 ASN1_BIT_STRING *usage; 392 ASN1_BIT_STRING *ns; 393 EXTENDED_KEY_USAGE *extusage; 394 X509_EXTENSION *ex; 395 long version; 396 int i; 397 398 if (x->ex_flags & EXFLAG_SET) 399 return; 400 401 /* 402 * XXX - this should really only set EXFLAG_INVALID if extensions are 403 * invalid. However, the X509_digest() failure matches OpenSSL/BoringSSL 404 * behavior and the version checks are at least vaguely related to 405 * extensions. 406 */ 407 408 if (!X509_digest(x, X509_CERT_HASH_EVP, x->hash, NULL)) 409 x->ex_flags |= EXFLAG_INVALID; 410 411 version = X509_get_version(x); 412 if (version < 0 || version > 2) 413 x->ex_flags |= EXFLAG_INVALID; 414 if (version == 0) { 415 x->ex_flags |= EXFLAG_V1; 416 /* UIDs may only appear in v2 or v3 certs */ 417 if (x->cert_info->issuerUID != NULL || 418 x->cert_info->subjectUID != NULL) 419 x->ex_flags |= EXFLAG_INVALID; 420 } 421 if (version != 2 && X509_get_ext_count(x) != 0) 422 x->ex_flags |= EXFLAG_INVALID; 423 424 /* Handle basic constraints */ 425 if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL))) { 426 if (bs->ca) 427 x->ex_flags |= EXFLAG_CA; 428 if (bs->pathlen) { 429 if ((bs->pathlen->type == V_ASN1_NEG_INTEGER) || 430 !bs->ca) { 431 x->ex_flags |= EXFLAG_INVALID; 432 x->ex_pathlen = 0; 433 } else 434 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen); 435 } else 436 x->ex_pathlen = -1; 437 BASIC_CONSTRAINTS_free(bs); 438 x->ex_flags |= EXFLAG_BCONS; 439 } else if (i != -1) { 440 x->ex_flags |= EXFLAG_INVALID; 441 } 442 443 /* Handle key usage */ 444 if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) { 445 if (usage->length > 0) { 446 x->ex_kusage = usage->data[0]; 447 if (usage->length > 1) 448 x->ex_kusage |= usage->data[1] << 8; 449 } else 450 x->ex_kusage = 0; 451 x->ex_flags |= EXFLAG_KUSAGE; 452 ASN1_BIT_STRING_free(usage); 453 } else if (i != -1) { 454 x->ex_flags |= EXFLAG_INVALID; 455 } 456 457 x->ex_xkusage = 0; 458 if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) { 459 x->ex_flags |= EXFLAG_XKUSAGE; 460 for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { 461 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { 462 case NID_server_auth: 463 x->ex_xkusage |= XKU_SSL_SERVER; 464 break; 465 466 case NID_client_auth: 467 x->ex_xkusage |= XKU_SSL_CLIENT; 468 break; 469 470 case NID_email_protect: 471 x->ex_xkusage |= XKU_SMIME; 472 break; 473 474 case NID_code_sign: 475 x->ex_xkusage |= XKU_CODE_SIGN; 476 break; 477 478 case NID_ms_sgc: 479 case NID_ns_sgc: 480 x->ex_xkusage |= XKU_SGC; 481 break; 482 483 case NID_OCSP_sign: 484 x->ex_xkusage |= XKU_OCSP_SIGN; 485 break; 486 487 case NID_time_stamp: 488 x->ex_xkusage |= XKU_TIMESTAMP; 489 break; 490 491 case NID_dvcs: 492 x->ex_xkusage |= XKU_DVCS; 493 break; 494 495 case NID_anyExtendedKeyUsage: 496 x->ex_xkusage |= XKU_ANYEKU; 497 break; 498 } 499 } 500 sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free); 501 } else if (i != -1) { 502 x->ex_flags |= EXFLAG_INVALID; 503 } 504 505 if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) { 506 if (ns->length > 0) 507 x->ex_nscert = ns->data[0]; 508 else 509 x->ex_nscert = 0; 510 x->ex_flags |= EXFLAG_NSCERT; 511 ASN1_BIT_STRING_free(ns); 512 } else if (i != -1) { 513 x->ex_flags |= EXFLAG_INVALID; 514 } 515 516 x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL); 517 if (x->skid == NULL && i != -1) 518 x->ex_flags |= EXFLAG_INVALID; 519 x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL); 520 if (x->akid == NULL && i != -1) 521 x->ex_flags |= EXFLAG_INVALID; 522 523 /* Does subject name match issuer? */ 524 if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) { 525 x->ex_flags |= EXFLAG_SI; 526 /* If SKID matches AKID also indicate self signed. */ 527 if (X509_check_akid(x, x->akid) == X509_V_OK && 528 !ku_reject(x, KU_KEY_CERT_SIGN)) 529 x->ex_flags |= EXFLAG_SS; 530 } 531 532 x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL); 533 if (x->altname == NULL && i != -1) 534 x->ex_flags |= EXFLAG_INVALID; 535 x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL); 536 if (!x->nc && (i != -1)) 537 x->ex_flags |= EXFLAG_INVALID; 538 setup_crldp(x); 539 540#ifndef OPENSSL_NO_RFC3779 541 x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL); 542 if (x->rfc3779_addr == NULL && i != -1) 543 x->ex_flags |= EXFLAG_INVALID; 544 if (!X509v3_addr_is_canonical(x->rfc3779_addr)) 545 x->ex_flags |= EXFLAG_INVALID; 546 x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL); 547 if (x->rfc3779_asid == NULL && i != -1) 548 x->ex_flags |= EXFLAG_INVALID; 549 if (!X509v3_asid_is_canonical(x->rfc3779_asid)) 550 x->ex_flags |= EXFLAG_INVALID; 551#endif 552 553 for (i = 0; i < X509_get_ext_count(x); i++) { 554 ex = X509_get_ext(x, i); 555 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) == 556 NID_freshest_crl) 557 x->ex_flags |= EXFLAG_FRESHEST; 558 if (!X509_EXTENSION_get_critical(ex)) 559 continue; 560 if (!X509_supported_extension(ex)) { 561 x->ex_flags |= EXFLAG_CRITICAL; 562 break; 563 } 564 } 565 566 if (!x509_extension_oids_are_unique(x)) 567 x->ex_flags |= EXFLAG_INVALID; 568 569 x->ex_flags |= EXFLAG_SET; 570} 571 572int 573x509v3_cache_extensions(X509 *x) 574{ 575 if ((x->ex_flags & EXFLAG_SET) == 0) { 576 CRYPTO_w_lock(CRYPTO_LOCK_X509); 577 x509v3_cache_extensions_internal(x); 578 CRYPTO_w_unlock(CRYPTO_LOCK_X509); 579 } 580 581 return (x->ex_flags & EXFLAG_INVALID) == 0; 582} 583 584/* CA checks common to all purposes 585 * return codes: 586 * 0 not a CA 587 * 1 is a CA 588 * 2 basicConstraints absent so "maybe" a CA 589 * 3 basicConstraints absent but self signed V1. 590 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted. 591 */ 592 593static int 594check_ca(const X509 *x) 595{ 596 /* keyUsage if present should allow cert signing */ 597 if (ku_reject(x, KU_KEY_CERT_SIGN)) 598 return 0; 599 if (x->ex_flags & EXFLAG_BCONS) { 600 if (x->ex_flags & EXFLAG_CA) 601 return 1; 602 /* If basicConstraints says not a CA then say so */ 603 else 604 return 0; 605 } else { 606 /* we support V1 roots for... uh, I don't really know why. */ 607 if ((x->ex_flags & V1_ROOT) == V1_ROOT) 608 return 3; 609 /* If key usage present it must have certSign so tolerate it */ 610 else if (x->ex_flags & EXFLAG_KUSAGE) 611 return 4; 612 /* Older certificates could have Netscape-specific CA types */ 613 else if (x->ex_flags & EXFLAG_NSCERT && 614 x->ex_nscert & NS_ANY_CA) 615 return 5; 616 /* can this still be regarded a CA certificate? I doubt it */ 617 return 0; 618 } 619} 620 621int 622X509_check_ca(X509 *x) 623{ 624 x509v3_cache_extensions(x); 625 626 return check_ca(x); 627} 628LCRYPTO_ALIAS(X509_check_ca); 629 630/* Check SSL CA: common checks for SSL client and server */ 631static int 632check_ssl_ca(const X509 *x) 633{ 634 int ca_ret; 635 636 ca_ret = check_ca(x); 637 if (!ca_ret) 638 return 0; 639 /* check nsCertType if present */ 640 if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA) 641 return ca_ret; 642 else 643 return 0; 644} 645 646static int 647check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca) 648{ 649 if (xku_reject(x, XKU_SSL_CLIENT)) 650 return 0; 651 if (ca) 652 return check_ssl_ca(x); 653 /* We need to do digital signatures with it */ 654 if (ku_reject(x, KU_DIGITAL_SIGNATURE)) 655 return 0; 656 /* nsCertType if present should allow SSL client use */ 657 if (ns_reject(x, NS_SSL_CLIENT)) 658 return 0; 659 return 1; 660} 661 662static int 663check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca) 664{ 665 if (xku_reject(x, XKU_SSL_SERVER|XKU_SGC)) 666 return 0; 667 if (ca) 668 return check_ssl_ca(x); 669 670 if (ns_reject(x, NS_SSL_SERVER)) 671 return 0; 672 /* Now as for keyUsage: we'll at least need to sign OR encipher */ 673 if (ku_reject(x, KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT)) 674 return 0; 675 676 return 1; 677} 678 679static int 680check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca) 681{ 682 int ret; 683 684 ret = check_purpose_ssl_server(xp, x, ca); 685 if (!ret || ca) 686 return ret; 687 /* We need to encipher or Netscape complains */ 688 if (ku_reject(x, KU_KEY_ENCIPHERMENT)) 689 return 0; 690 return ret; 691} 692 693/* common S/MIME checks */ 694static int 695purpose_smime(const X509 *x, int ca) 696{ 697 if (xku_reject(x, XKU_SMIME)) 698 return 0; 699 if (ca) { 700 int ca_ret; 701 ca_ret = check_ca(x); 702 if (!ca_ret) 703 return 0; 704 /* check nsCertType if present */ 705 if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA) 706 return ca_ret; 707 else 708 return 0; 709 } 710 if (x->ex_flags & EXFLAG_NSCERT) { 711 if (x->ex_nscert & NS_SMIME) 712 return 1; 713 /* Workaround for some buggy certificates */ 714 if (x->ex_nscert & NS_SSL_CLIENT) 715 return 2; 716 return 0; 717 } 718 return 1; 719} 720 721static int 722check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca) 723{ 724 int ret; 725 726 ret = purpose_smime(x, ca); 727 if (!ret || ca) 728 return ret; 729 if (ku_reject(x, KU_DIGITAL_SIGNATURE|KU_NON_REPUDIATION)) 730 return 0; 731 return ret; 732} 733 734static int 735check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca) 736{ 737 int ret; 738 739 ret = purpose_smime(x, ca); 740 if (!ret || ca) 741 return ret; 742 if (ku_reject(x, KU_KEY_ENCIPHERMENT)) 743 return 0; 744 return ret; 745} 746 747static int 748check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca) 749{ 750 if (ca) { 751 int ca_ret; 752 if ((ca_ret = check_ca(x)) != 2) 753 return ca_ret; 754 else 755 return 0; 756 } 757 if (ku_reject(x, KU_CRL_SIGN)) 758 return 0; 759 return 1; 760} 761 762/* OCSP helper: this is *not* a full OCSP check. It just checks that 763 * each CA is valid. Additional checks must be made on the chain. 764 */ 765static int 766ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca) 767{ 768 /* Must be a valid CA. Should we really support the "I don't know" 769 value (2)? */ 770 if (ca) 771 return check_ca(x); 772 /* leaf certificate is checked in OCSP_verify() */ 773 return 1; 774} 775 776static int 777check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, int ca) 778{ 779 int i_ext; 780 781 /* If ca is true we must return if this is a valid CA certificate. */ 782 if (ca) 783 return check_ca(x); 784 785 /* 786 * Check the optional key usage field: 787 * if Key Usage is present, it must be one of digitalSignature 788 * and/or nonRepudiation (other values are not consistent and shall 789 * be rejected). 790 */ 791 if ((x->ex_flags & EXFLAG_KUSAGE) && 792 ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) || 793 !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)))) 794 return 0; 795 796 /* Only time stamp key usage is permitted and it's required. */ 797 if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP) 798 return 0; 799 800 /* Extended Key Usage MUST be critical */ 801 i_ext = X509_get_ext_by_NID((X509 *) x, NID_ext_key_usage, -1); 802 if (i_ext >= 0) { 803 X509_EXTENSION *ext = X509_get_ext((X509 *) x, i_ext); 804 if (!X509_EXTENSION_get_critical(ext)) 805 return 0; 806 } 807 808 return 1; 809} 810 811static int 812no_check(const X509_PURPOSE *xp, const X509 *x, int ca) 813{ 814 return 1; 815} 816 817/* Various checks to see if one certificate issued the second. 818 * This can be used to prune a set of possible issuer certificates 819 * which have been looked up using some simple method such as by 820 * subject name. 821 * These are: 822 * 1. Check issuer_name(subject) == subject_name(issuer) 823 * 2. If akid(subject) exists check it matches issuer 824 * 3. If key_usage(issuer) exists check it supports certificate signing 825 * returns 0 for OK, positive for reason for mismatch, reasons match 826 * codes for X509_verify_cert() 827 */ 828 829int 830X509_check_issued(X509 *issuer, X509 *subject) 831{ 832 if (X509_NAME_cmp(X509_get_subject_name(issuer), 833 X509_get_issuer_name(subject))) 834 return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; 835 836 if (!x509v3_cache_extensions(issuer)) 837 return X509_V_ERR_UNSPECIFIED; 838 if (!x509v3_cache_extensions(subject)) 839 return X509_V_ERR_UNSPECIFIED; 840 841 if (subject->akid) { 842 int ret = X509_check_akid(issuer, subject->akid); 843 if (ret != X509_V_OK) 844 return ret; 845 } 846 847 if (ku_reject(issuer, KU_KEY_CERT_SIGN)) 848 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN; 849 return X509_V_OK; 850} 851LCRYPTO_ALIAS(X509_check_issued); 852 853int 854X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid) 855{ 856 if (!akid) 857 return X509_V_OK; 858 859 /* Check key ids (if present) */ 860 if (akid->keyid && issuer->skid && 861 ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) 862 return X509_V_ERR_AKID_SKID_MISMATCH; 863 /* Check serial number */ 864 if (akid->serial && 865 ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial)) 866 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; 867 /* Check issuer name */ 868 if (akid->issuer) { 869 /* Ugh, for some peculiar reason AKID includes 870 * SEQUENCE OF GeneralName. So look for a DirName. 871 * There may be more than one but we only take any 872 * notice of the first. 873 */ 874 GENERAL_NAMES *gens; 875 GENERAL_NAME *gen; 876 X509_NAME *nm = NULL; 877 int i; 878 gens = akid->issuer; 879 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { 880 gen = sk_GENERAL_NAME_value(gens, i); 881 if (gen->type == GEN_DIRNAME) { 882 nm = gen->d.dirn; 883 break; 884 } 885 } 886 if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer))) 887 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; 888 } 889 return X509_V_OK; 890} 891LCRYPTO_ALIAS(X509_check_akid); 892 893uint32_t 894X509_get_extension_flags(X509 *x) 895{ 896 /* Call for side-effect of computing hash and caching extensions */ 897 if (X509_check_purpose(x, -1, -1) != 1) 898 return EXFLAG_INVALID; 899 900 return x->ex_flags; 901} 902LCRYPTO_ALIAS(X509_get_extension_flags); 903 904uint32_t 905X509_get_key_usage(X509 *x) 906{ 907 /* Call for side-effect of computing hash and caching extensions */ 908 if (X509_check_purpose(x, -1, -1) != 1) 909 return 0; 910 911 if (x->ex_flags & EXFLAG_KUSAGE) 912 return x->ex_kusage; 913 914 return UINT32_MAX; 915} 916LCRYPTO_ALIAS(X509_get_key_usage); 917 918uint32_t 919X509_get_extended_key_usage(X509 *x) 920{ 921 /* Call for side-effect of computing hash and caching extensions */ 922 if (X509_check_purpose(x, -1, -1) != 1) 923 return 0; 924 925 if (x->ex_flags & EXFLAG_XKUSAGE) 926 return x->ex_xkusage; 927 928 return UINT32_MAX; 929} 930LCRYPTO_ALIAS(X509_get_extended_key_usage); 931