diff options
Diffstat (limited to 'src/lib/libcrypto/x509/x509_purp.c')
| -rw-r--r-- | src/lib/libcrypto/x509/x509_purp.c | 930 |
1 files changed, 0 insertions, 930 deletions
diff --git a/src/lib/libcrypto/x509/x509_purp.c b/src/lib/libcrypto/x509/x509_purp.c deleted file mode 100644 index 619a4b890a..0000000000 --- a/src/lib/libcrypto/x509/x509_purp.c +++ /dev/null | |||
| @@ -1,930 +0,0 @@ | |||
| 1 | /* $OpenBSD: x509_purp.c,v 1.43 2024/07/12 18:15:10 beck 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 | struct x509_purpose_st { | ||
| 72 | int purpose; | ||
| 73 | int trust; /* Default trust ID */ | ||
| 74 | int flags; | ||
| 75 | int (*check_purpose)(const struct x509_purpose_st *, const X509 *, int); | ||
| 76 | char *name; | ||
| 77 | char *sname; | ||
| 78 | void *usr_data; | ||
| 79 | } /* X509_PURPOSE */; | ||
| 80 | |||
| 81 | #define V1_ROOT (EXFLAG_V1|EXFLAG_SS) | ||
| 82 | #define ku_reject(x, usage) \ | ||
| 83 | (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) | ||
| 84 | #define xku_reject(x, usage) \ | ||
| 85 | (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage))) | ||
| 86 | #define ns_reject(x, usage) \ | ||
| 87 | (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage))) | ||
| 88 | |||
| 89 | static int check_ssl_ca(const X509 *x); | ||
| 90 | static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, | ||
| 91 | int ca); | ||
| 92 | static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, | ||
| 93 | int ca); | ||
| 94 | static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, | ||
| 95 | int ca); | ||
| 96 | static int purpose_smime(const X509 *x, int ca); | ||
| 97 | static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, | ||
| 98 | int ca); | ||
| 99 | static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, | ||
| 100 | int ca); | ||
| 101 | static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, | ||
| 102 | int ca); | ||
| 103 | static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, | ||
| 104 | int ca); | ||
| 105 | static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca); | ||
| 106 | static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca); | ||
| 107 | |||
| 108 | static const X509_PURPOSE xstandard[] = { | ||
| 109 | { | ||
| 110 | .purpose = X509_PURPOSE_SSL_CLIENT, | ||
| 111 | .trust = X509_TRUST_SSL_CLIENT, | ||
| 112 | .check_purpose = check_purpose_ssl_client, | ||
| 113 | .name = "SSL client", | ||
| 114 | .sname = "sslclient", | ||
| 115 | }, | ||
| 116 | { | ||
| 117 | .purpose = X509_PURPOSE_SSL_SERVER, | ||
| 118 | .trust = X509_TRUST_SSL_SERVER, | ||
| 119 | .check_purpose = check_purpose_ssl_server, | ||
| 120 | .name = "SSL server", | ||
| 121 | .sname = "sslserver", | ||
| 122 | }, | ||
| 123 | { | ||
| 124 | .purpose = X509_PURPOSE_NS_SSL_SERVER, | ||
| 125 | .trust = X509_TRUST_SSL_SERVER, | ||
| 126 | .check_purpose = check_purpose_ns_ssl_server, | ||
| 127 | .name = "Netscape SSL server", | ||
| 128 | .sname = "nssslserver", | ||
| 129 | }, | ||
| 130 | { | ||
| 131 | .purpose = X509_PURPOSE_SMIME_SIGN, | ||
| 132 | .trust = X509_TRUST_EMAIL, | ||
| 133 | .check_purpose = check_purpose_smime_sign, | ||
| 134 | .name = "S/MIME signing", | ||
| 135 | .sname = "smimesign", | ||
| 136 | }, | ||
| 137 | { | ||
| 138 | .purpose = X509_PURPOSE_SMIME_ENCRYPT, | ||
| 139 | .trust = X509_TRUST_EMAIL, | ||
| 140 | .check_purpose = check_purpose_smime_encrypt, | ||
| 141 | .name = "S/MIME encryption", | ||
| 142 | .sname = "smimeencrypt", | ||
| 143 | }, | ||
| 144 | { | ||
| 145 | .purpose = X509_PURPOSE_CRL_SIGN, | ||
| 146 | .trust = X509_TRUST_COMPAT, | ||
| 147 | .check_purpose = check_purpose_crl_sign, | ||
| 148 | .name = "CRL signing", | ||
| 149 | .sname = "crlsign", | ||
| 150 | }, | ||
| 151 | { | ||
| 152 | .purpose = X509_PURPOSE_ANY, | ||
| 153 | .trust = X509_TRUST_ACCEPT_ALL, | ||
| 154 | .check_purpose = no_check, | ||
| 155 | .name = "Any Purpose", | ||
| 156 | .sname = "any", | ||
| 157 | }, | ||
| 158 | { | ||
| 159 | .purpose = X509_PURPOSE_OCSP_HELPER, | ||
| 160 | .trust = X509_TRUST_COMPAT, | ||
| 161 | .check_purpose = ocsp_helper, | ||
| 162 | .name = "OCSP helper", | ||
| 163 | .sname = "ocsphelper", | ||
| 164 | }, | ||
| 165 | { | ||
| 166 | .purpose = X509_PURPOSE_TIMESTAMP_SIGN, | ||
| 167 | .trust = X509_TRUST_TSA, | ||
| 168 | .check_purpose = check_purpose_timestamp_sign, | ||
| 169 | .name = "Time Stamp signing", | ||
| 170 | .sname = "timestampsign", | ||
| 171 | }, | ||
| 172 | }; | ||
| 173 | |||
| 174 | #define X509_PURPOSE_COUNT (sizeof(xstandard) / sizeof(xstandard[0])) | ||
| 175 | |||
| 176 | /* As much as I'd like to make X509_check_purpose use a "const" X509* | ||
| 177 | * I really can't because it does recalculate hashes and do other non-const | ||
| 178 | * things. */ | ||
| 179 | int | ||
| 180 | X509_check_purpose(X509 *x, int id, int ca) | ||
| 181 | { | ||
| 182 | int idx; | ||
| 183 | const X509_PURPOSE *pt; | ||
| 184 | |||
| 185 | if (!x509v3_cache_extensions(x)) | ||
| 186 | return -1; | ||
| 187 | |||
| 188 | if (id == -1) | ||
| 189 | return 1; | ||
| 190 | |||
| 191 | if ((idx = X509_PURPOSE_get_by_id(id)) == -1) | ||
| 192 | return -1; | ||
| 193 | if ((pt = X509_PURPOSE_get0(idx)) == NULL) | ||
| 194 | return -1; | ||
| 195 | |||
| 196 | return pt->check_purpose(pt, x, ca); | ||
| 197 | } | ||
| 198 | LCRYPTO_ALIAS(X509_check_purpose); | ||
| 199 | |||
| 200 | int | ||
| 201 | X509_PURPOSE_get_count(void) | ||
| 202 | { | ||
| 203 | return X509_PURPOSE_COUNT; | ||
| 204 | } | ||
| 205 | LCRYPTO_ALIAS(X509_PURPOSE_get_count); | ||
| 206 | |||
| 207 | const X509_PURPOSE * | ||
| 208 | X509_PURPOSE_get0(int idx) | ||
| 209 | { | ||
| 210 | if (idx < 0 || (size_t)idx >= X509_PURPOSE_COUNT) | ||
| 211 | return NULL; | ||
| 212 | |||
| 213 | return &xstandard[idx]; | ||
| 214 | } | ||
| 215 | LCRYPTO_ALIAS(X509_PURPOSE_get0); | ||
| 216 | |||
| 217 | int | ||
| 218 | X509_PURPOSE_get_by_sname(const char *sname) | ||
| 219 | { | ||
| 220 | int i; | ||
| 221 | const X509_PURPOSE *xptmp; | ||
| 222 | |||
| 223 | for (i = 0; i < X509_PURPOSE_get_count(); i++) { | ||
| 224 | xptmp = X509_PURPOSE_get0(i); | ||
| 225 | if (!strcmp(xptmp->sname, sname)) | ||
| 226 | return i; | ||
| 227 | } | ||
| 228 | return -1; | ||
| 229 | } | ||
| 230 | LCRYPTO_ALIAS(X509_PURPOSE_get_by_sname); | ||
| 231 | |||
| 232 | int | ||
| 233 | X509_PURPOSE_get_by_id(int purpose) | ||
| 234 | { | ||
| 235 | /* | ||
| 236 | * Ensure the purpose identifier is between MIN and MAX inclusive. | ||
| 237 | * If so, translate it to an index into the xstandard[] table. | ||
| 238 | */ | ||
| 239 | if (purpose < X509_PURPOSE_MIN || purpose > X509_PURPOSE_MAX) | ||
| 240 | return -1; | ||
| 241 | |||
| 242 | return purpose - X509_PURPOSE_MIN; | ||
| 243 | } | ||
| 244 | |||
| 245 | int | ||
| 246 | X509_PURPOSE_get_id(const X509_PURPOSE *xp) | ||
| 247 | { | ||
| 248 | return xp->purpose; | ||
| 249 | } | ||
| 250 | LCRYPTO_ALIAS(X509_PURPOSE_get_id); | ||
| 251 | |||
| 252 | const char * | ||
| 253 | X509_PURPOSE_get0_name(const X509_PURPOSE *xp) | ||
| 254 | { | ||
| 255 | return xp->name; | ||
| 256 | } | ||
| 257 | LCRYPTO_ALIAS(X509_PURPOSE_get0_name); | ||
| 258 | |||
| 259 | const char * | ||
| 260 | X509_PURPOSE_get0_sname(const X509_PURPOSE *xp) | ||
| 261 | { | ||
| 262 | return xp->sname; | ||
| 263 | } | ||
| 264 | LCRYPTO_ALIAS(X509_PURPOSE_get0_sname); | ||
| 265 | |||
| 266 | int | ||
| 267 | X509_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 | */ | ||
| 277 | int | ||
| 278 | X509_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 | } | ||
| 300 | LCRYPTO_ALIAS(X509_supported_extension); | ||
| 301 | |||
| 302 | static void | ||
| 303 | setup_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 | |||
| 331 | static void | ||
| 332 | setup_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 | |||
| 346 | static int | ||
| 347 | x509_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 | |||
| 353 | static int | ||
| 354 | x509_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 | |||
| 387 | static void | ||
| 388 | x509v3_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 | |||
| 572 | int | ||
| 573 | x509v3_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 | |||
| 593 | static int | ||
| 594 | check_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 | |||
| 621 | int | ||
| 622 | X509_check_ca(X509 *x) | ||
| 623 | { | ||
| 624 | x509v3_cache_extensions(x); | ||
| 625 | |||
| 626 | return check_ca(x); | ||
| 627 | } | ||
| 628 | LCRYPTO_ALIAS(X509_check_ca); | ||
| 629 | |||
| 630 | /* Check SSL CA: common checks for SSL client and server */ | ||
| 631 | static int | ||
| 632 | check_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 | |||
| 646 | static int | ||
| 647 | check_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 | |||
| 662 | static int | ||
| 663 | check_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 | |||
| 679 | static int | ||
| 680 | check_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 */ | ||
| 694 | static int | ||
| 695 | purpose_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 | |||
| 721 | static int | ||
| 722 | check_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 | |||
| 734 | static int | ||
| 735 | check_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 | |||
| 747 | static int | ||
| 748 | check_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 | */ | ||
| 765 | static int | ||
| 766 | ocsp_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 | |||
| 776 | static int | ||
| 777 | check_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 | |||
| 811 | static int | ||
| 812 | no_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 | |||
| 829 | int | ||
| 830 | X509_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 | } | ||
| 851 | LCRYPTO_ALIAS(X509_check_issued); | ||
| 852 | |||
| 853 | int | ||
| 854 | X509_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 | } | ||
| 891 | LCRYPTO_ALIAS(X509_check_akid); | ||
| 892 | |||
| 893 | uint32_t | ||
| 894 | X509_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 | } | ||
| 902 | LCRYPTO_ALIAS(X509_get_extension_flags); | ||
| 903 | |||
| 904 | uint32_t | ||
| 905 | X509_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 | } | ||
| 916 | LCRYPTO_ALIAS(X509_get_key_usage); | ||
| 917 | |||
| 918 | uint32_t | ||
| 919 | X509_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 | } | ||
| 930 | LCRYPTO_ALIAS(X509_get_extended_key_usage); | ||
