| 1 |
// Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 2001. |
| 2 |
// Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved. |
| 3 |
// SPDX-License-Identifier: Apache-2.0 |
| 4 |
|
| 5 |
#include <assert.h> |
| 6 |
#include <limits.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <openssl/digest.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
#include <openssl/obj.h> |
| 13 |
#include <openssl/thread.h> |
| 14 |
#include <openssl/x509.h> |
| 15 |
|
| 16 |
#include "../internal.h" |
| 17 |
#include "internal.h" |
| 18 |
|
| 19 |
#define V1_ROOT (EXFLAG_V1 | EXFLAG_SS) |
| 20 |
#define ku_reject(x, usage) \ |
| 21 |
(((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage))) |
| 22 |
#define xku_reject(x, usage) \ |
| 23 |
(((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage))) |
| 24 |
#define ns_reject(x, usage) \ |
| 25 |
(((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage))) |
| 26 |
|
| 27 |
static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
| 28 |
int ca); |
| 29 |
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
| 30 |
int ca); |
| 31 |
static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
| 32 |
int ca); |
| 33 |
static int purpose_smime(const X509 *x, int ca); |
| 34 |
static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
| 35 |
int ca); |
| 36 |
static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
| 37 |
int ca); |
| 38 |
static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
| 39 |
int ca); |
| 40 |
static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
| 41 |
int ca); |
| 42 |
static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca); |
| 43 |
static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca); |
| 44 |
|
| 45 |
// X509_TRUST_NONE is not a valid |X509_TRUST_*| constant. It is used by |
| 46 |
// |X509_PURPOSE_ANY| to indicate that it has no corresponding trust type and |
| 47 |
// cannot be used with |X509_STORE_CTX_set_purpose|. |
| 48 |
#define X509_TRUST_NONE (-1) |
| 49 |
|
| 50 |
static const X509_PURPOSE xstandard[] = { |
| 51 |
{X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, |
| 52 |
check_purpose_ssl_client, (char *)"SSL client", (char *)"sslclient", NULL}, |
| 53 |
{X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, |
| 54 |
check_purpose_ssl_server, (char *)"SSL server", (char *)"sslserver", NULL}, |
| 55 |
{X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, |
| 56 |
check_purpose_ns_ssl_server, (char *)"Netscape SSL server", |
| 57 |
(char *)"nssslserver", NULL}, |
| 58 |
{X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, |
| 59 |
(char *)"S/MIME signing", (char *)"smimesign", NULL}, |
| 60 |
{X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, |
| 61 |
check_purpose_smime_encrypt, (char *)"S/MIME encryption", |
| 62 |
(char *)"smimeencrypt", NULL}, |
| 63 |
{X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, |
| 64 |
(char *)"CRL signing", (char *)"crlsign", NULL}, |
| 65 |
{X509_PURPOSE_ANY, X509_TRUST_NONE, 0, no_check, (char *)"Any Purpose", |
| 66 |
(char *)"any", NULL}, |
| 67 |
{X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper, |
| 68 |
(char *)"OCSP helper", (char *)"ocsphelper", NULL}, |
| 69 |
{X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, |
| 70 |
check_purpose_timestamp_sign, (char *)"Time Stamp signing", |
| 71 |
(char *)"timestampsign", NULL}, |
| 72 |
}; |
| 73 |
|
| 74 |
int X509_check_purpose(X509 *x, int id, int ca) { |
| 75 |
// This differs from OpenSSL, which uses -1 to indicate a fatal error and 0 to |
| 76 |
// indicate an invalid certificate. BoringSSL uses 0 for both. |
| 77 |
if (!x509v3_cache_extensions(x)) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
|
| 81 |
if (id == -1) { |
| 82 |
return 1; |
| 83 |
} |
| 84 |
int idx = X509_PURPOSE_get_by_id(id); |
| 85 |
if (idx == -1) { |
| 86 |
return 0; |
| 87 |
} |
| 88 |
const X509_PURPOSE *pt = X509_PURPOSE_get0(idx); |
| 89 |
return pt->check_purpose(pt, x, ca); |
| 90 |
} |
| 91 |
|
| 92 |
int X509_PURPOSE_set(int *p, int purpose) { |
| 93 |
if (X509_PURPOSE_get_by_id(purpose) == -1) { |
| 94 |
OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_PURPOSE); |
| 95 |
return 0; |
| 96 |
} |
| 97 |
*p = purpose; |
| 98 |
return 1; |
| 99 |
} |
| 100 |
|
| 101 |
int X509_PURPOSE_get_count(void) { return OPENSSL_ARRAY_SIZE(xstandard); } |
| 102 |
|
| 103 |
const X509_PURPOSE *X509_PURPOSE_get0(int idx) { |
| 104 |
if (idx < 0 || (size_t)idx >= OPENSSL_ARRAY_SIZE(xstandard)) { |
| 105 |
return NULL; |
| 106 |
} |
| 107 |
return xstandard + idx; |
| 108 |
} |
| 109 |
|
| 110 |
int X509_PURPOSE_get_by_sname(const char *sname) { |
| 111 |
const X509_PURPOSE *xptmp; |
| 112 |
for (int i = 0; i < X509_PURPOSE_get_count(); i++) { |
| 113 |
xptmp = X509_PURPOSE_get0(i); |
| 114 |
if (!strcmp(xptmp->sname, sname)) { |
| 115 |
return i; |
| 116 |
} |
| 117 |
} |
| 118 |
return -1; |
| 119 |
} |
| 120 |
|
| 121 |
int X509_PURPOSE_get_by_id(int purpose) { |
| 122 |
for (size_t i = 0; i <OPENSSL_ARRAY_SIZE(xstandard); i++) { |
| 123 |
if (xstandard[i].purpose == purpose) { |
| 124 |
OPENSSL_STATIC_ASSERT(OPENSSL_ARRAY_SIZE(xstandard) <= INT_MAX, |
| 125 |
indices_must_fit_in_int); |
| 126 |
return (int)i; |
| 127 |
} |
| 128 |
} |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
int X509_PURPOSE_get_id(const X509_PURPOSE *xp) { return xp->purpose; } |
| 133 |
|
| 134 |
char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp) { return xp->name; } |
| 135 |
|
| 136 |
char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp) { return xp->sname; } |
| 137 |
|
| 138 |
int X509_PURPOSE_get_trust(const X509_PURPOSE *xp) { return xp->trust; } |
| 139 |
|
| 140 |
int X509_supported_extension(const X509_EXTENSION *ex) { |
| 141 |
int nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex)); |
| 142 |
return nid == NID_netscape_cert_type || |
| 143 |
nid == NID_key_usage || |
| 144 |
nid == NID_subject_alt_name || |
| 145 |
nid == NID_basic_constraints || |
| 146 |
nid == NID_certificate_policies || |
| 147 |
nid == NID_crl_distribution_points || |
| 148 |
nid == NID_ext_key_usage || |
| 149 |
nid == NID_policy_constraints || |
| 150 |
nid == NID_name_constraints || |
| 151 |
nid == NID_policy_mappings || |
| 152 |
nid == NID_inhibit_any_policy; |
| 153 |
} |
| 154 |
|
| 155 |
static int setup_dp(X509 *x, DIST_POINT *dp) { |
| 156 |
if (!dp->distpoint || (dp->distpoint->type != 1)) { |
| 157 |
return 1; |
| 158 |
} |
| 159 |
X509_NAME *iname = NULL; |
| 160 |
for (size_t i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) { |
| 161 |
GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i); |
| 162 |
if (gen->type == GEN_DIRNAME) { |
| 163 |
iname = gen->d.directoryName; |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
if (!iname) { |
| 168 |
iname = X509_get_issuer_name(x); |
| 169 |
} |
| 170 |
|
| 171 |
return DIST_POINT_set_dpname(dp->distpoint, iname); |
| 172 |
} |
| 173 |
|
| 174 |
static int setup_crldp(X509 *x) { |
| 175 |
int j; |
| 176 |
x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &j, NULL); |
| 177 |
if (x->crldp == NULL && j != -1) { |
| 178 |
return 0; |
| 179 |
} |
| 180 |
for (size_t i = 0; i < sk_DIST_POINT_num(x->crldp); i++) { |
| 181 |
if (!setup_dp(x, sk_DIST_POINT_value(x->crldp, i))) { |
| 182 |
return 0; |
| 183 |
} |
| 184 |
} |
| 185 |
return 1; |
| 186 |
} |
| 187 |
|
| 188 |
int x509v3_cache_extensions(X509 *x) { |
| 189 |
BASIC_CONSTRAINTS *bs; |
| 190 |
ASN1_BIT_STRING *usage; |
| 191 |
ASN1_BIT_STRING *ns; |
| 192 |
EXTENDED_KEY_USAGE *extusage; |
| 193 |
size_t i; |
| 194 |
int j; |
| 195 |
|
| 196 |
CRYPTO_MUTEX_lock_read(&x->lock); |
| 197 |
const int is_set = x->ex_flags & EXFLAG_SET; |
| 198 |
CRYPTO_MUTEX_unlock_read(&x->lock); |
| 199 |
|
| 200 |
if (is_set) { |
| 201 |
return (x->ex_flags & EXFLAG_INVALID) == 0; |
| 202 |
} |
| 203 |
|
| 204 |
CRYPTO_MUTEX_lock_write(&x->lock); |
| 205 |
if (x->ex_flags & EXFLAG_SET) { |
| 206 |
CRYPTO_MUTEX_unlock_write(&x->lock); |
| 207 |
return (x->ex_flags & EXFLAG_INVALID) == 0; |
| 208 |
} |
| 209 |
|
| 210 |
if (!X509_digest(x, EVP_sha256(), x->cert_hash, NULL)) { |
| 211 |
x->ex_flags |= EXFLAG_INVALID; |
| 212 |
} |
| 213 |
// V1 should mean no extensions ... |
| 214 |
if (X509_get_version(x) == X509_VERSION_1) { |
| 215 |
x->ex_flags |= EXFLAG_V1; |
| 216 |
} |
| 217 |
// Handle basic constraints |
| 218 |
if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &j, NULL))) { |
| 219 |
if (bs->ca) { |
| 220 |
x->ex_flags |= EXFLAG_CA; |
| 221 |
} |
| 222 |
if (bs->pathlen) { |
| 223 |
if ((bs->pathlen->type == V_ASN1_NEG_INTEGER) || !bs->ca) { |
| 224 |
x->ex_flags |= EXFLAG_INVALID; |
| 225 |
x->ex_pathlen = 0; |
| 226 |
} else { |
| 227 |
// TODO(davidben): |ASN1_INTEGER_get| returns -1 on overflow, |
| 228 |
// which currently acts as if the constraint isn't present. This |
| 229 |
// works (an overflowing path length constraint may as well be |
| 230 |
// infinity), but Chromium's verifier simply treats values above |
| 231 |
// 255 as an error. |
| 232 |
x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen); |
| 233 |
} |
| 234 |
} else { |
| 235 |
x->ex_pathlen = -1; |
| 236 |
} |
| 237 |
BASIC_CONSTRAINTS_free(bs); |
| 238 |
x->ex_flags |= EXFLAG_BCONS; |
| 239 |
} else if (j != -1) { |
| 240 |
x->ex_flags |= EXFLAG_INVALID; |
| 241 |
} |
| 242 |
// Handle key usage |
| 243 |
if ((usage = X509_get_ext_d2i(x, NID_key_usage, &j, NULL))) { |
| 244 |
if (usage->length > 0) { |
| 245 |
x->ex_kusage = usage->data[0]; |
| 246 |
if (usage->length > 1) { |
| 247 |
x->ex_kusage |= usage->data[1] << 8; |
| 248 |
} |
| 249 |
} else { |
| 250 |
x->ex_kusage = 0; |
| 251 |
} |
| 252 |
x->ex_flags |= EXFLAG_KUSAGE; |
| 253 |
ASN1_BIT_STRING_free(usage); |
| 254 |
} else if (j != -1) { |
| 255 |
x->ex_flags |= EXFLAG_INVALID; |
| 256 |
} |
| 257 |
x->ex_xkusage = 0; |
| 258 |
if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &j, NULL))) { |
| 259 |
x->ex_flags |= EXFLAG_XKUSAGE; |
| 260 |
for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) { |
| 261 |
switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) { |
| 262 |
case NID_server_auth: |
| 263 |
x->ex_xkusage |= XKU_SSL_SERVER; |
| 264 |
break; |
| 265 |
|
| 266 |
case NID_client_auth: |
| 267 |
x->ex_xkusage |= XKU_SSL_CLIENT; |
| 268 |
break; |
| 269 |
|
| 270 |
case NID_email_protect: |
| 271 |
x->ex_xkusage |= XKU_SMIME; |
| 272 |
break; |
| 273 |
|
| 274 |
case NID_code_sign: |
| 275 |
x->ex_xkusage |= XKU_CODE_SIGN; |
| 276 |
break; |
| 277 |
|
| 278 |
case NID_ms_sgc: |
| 279 |
case NID_ns_sgc: |
| 280 |
x->ex_xkusage |= XKU_SGC; |
| 281 |
break; |
| 282 |
|
| 283 |
case NID_OCSP_sign: |
| 284 |
x->ex_xkusage |= XKU_OCSP_SIGN; |
| 285 |
break; |
| 286 |
|
| 287 |
case NID_time_stamp: |
| 288 |
x->ex_xkusage |= XKU_TIMESTAMP; |
| 289 |
break; |
| 290 |
|
| 291 |
case NID_dvcs: |
| 292 |
x->ex_xkusage |= XKU_DVCS; |
| 293 |
break; |
| 294 |
|
| 295 |
case NID_anyExtendedKeyUsage: |
| 296 |
x->ex_xkusage |= XKU_ANYEKU; |
| 297 |
break; |
| 298 |
} |
| 299 |
} |
| 300 |
sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free); |
| 301 |
} else if (j != -1) { |
| 302 |
x->ex_flags |= EXFLAG_INVALID; |
| 303 |
} |
| 304 |
|
| 305 |
if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &j, NULL))) { |
| 306 |
if (ns->length > 0) { |
| 307 |
x->ex_nscert = ns->data[0]; |
| 308 |
} else { |
| 309 |
x->ex_nscert = 0; |
| 310 |
} |
| 311 |
x->ex_flags |= EXFLAG_NSCERT; |
| 312 |
ASN1_BIT_STRING_free(ns); |
| 313 |
} else if (j != -1) { |
| 314 |
x->ex_flags |= EXFLAG_INVALID; |
| 315 |
} |
| 316 |
x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &j, NULL); |
| 317 |
if (x->skid == NULL && j != -1) { |
| 318 |
x->ex_flags |= EXFLAG_INVALID; |
| 319 |
} |
| 320 |
x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &j, NULL); |
| 321 |
if (x->akid == NULL && j != -1) { |
| 322 |
x->ex_flags |= EXFLAG_INVALID; |
| 323 |
} |
| 324 |
// Does subject name match issuer ? |
| 325 |
if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) { |
| 326 |
x->ex_flags |= EXFLAG_SI; |
| 327 |
// If SKID matches AKID also indicate self signed |
| 328 |
if (X509_check_akid(x, x->akid) == X509_V_OK && |
| 329 |
!ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) { |
| 330 |
x->ex_flags |= EXFLAG_SS; |
| 331 |
} |
| 332 |
} |
| 333 |
x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &j, NULL); |
| 334 |
if (x->altname == NULL && j != -1) { |
| 335 |
x->ex_flags |= EXFLAG_INVALID; |
| 336 |
} |
| 337 |
x->nc = X509_get_ext_d2i(x, NID_name_constraints, &j, NULL); |
| 338 |
if (x->nc == NULL && j != -1) { |
| 339 |
x->ex_flags |= EXFLAG_INVALID; |
| 340 |
} |
| 341 |
if (!setup_crldp(x)) { |
| 342 |
x->ex_flags |= EXFLAG_INVALID; |
| 343 |
} |
| 344 |
|
| 345 |
for (j = 0; j < X509_get_ext_count(x); j++) { |
| 346 |
const X509_EXTENSION *ex = X509_get_ext(x, j); |
| 347 |
if (!X509_EXTENSION_get_critical(ex)) { |
| 348 |
continue; |
| 349 |
} |
| 350 |
if (!X509_supported_extension(ex)) { |
| 351 |
x->ex_flags |= EXFLAG_CRITICAL; |
| 352 |
break; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
// Set x->sig_info. Errors here are ignored so that we emit similar errors |
| 357 |
// to OpenSSL, instead of failing early. |
| 358 |
(void)x509_init_signature_info(x); |
| 359 |
|
| 360 |
x->ex_flags |= EXFLAG_SET; |
| 361 |
|
| 362 |
CRYPTO_MUTEX_unlock_write(&x->lock); |
| 363 |
return (x->ex_flags & EXFLAG_INVALID) == 0; |
| 364 |
} |
| 365 |
|
| 366 |
// check_ca returns one if |x| should be considered a CA certificate and zero |
| 367 |
// otherwise. |
| 368 |
static int check_ca(const X509 *x) { |
| 369 |
// keyUsage if present should allow cert signing |
| 370 |
if (ku_reject(x, X509v3_KU_KEY_CERT_SIGN)) { |
| 371 |
return 0; |
| 372 |
} |
| 373 |
// Version 1 certificates are considered CAs and don't have extensions. |
| 374 |
if ((x->ex_flags & V1_ROOT) == V1_ROOT) { |
| 375 |
return 1; |
| 376 |
} |
| 377 |
// Otherwise, it's only a CA if basicConstraints says so. |
| 378 |
return ((x->ex_flags & EXFLAG_BCONS) && (x->ex_flags & EXFLAG_CA)); |
| 379 |
} |
| 380 |
|
| 381 |
int X509_check_ca(X509 *x) { |
| 382 |
if (!x509v3_cache_extensions(x)) { |
| 383 |
return 0; |
| 384 |
} |
| 385 |
return check_ca(x); |
| 386 |
} |
| 387 |
|
| 388 |
static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, |
| 389 |
int ca) { |
| 390 |
if (xku_reject(x, XKU_SSL_CLIENT)) { |
| 391 |
return 0; |
| 392 |
} |
| 393 |
if (ca) { |
| 394 |
// TODO(davidben): Move the various |check_ca| calls out of the |
| 395 |
// |check_purpose| callbacks. Those checks are purpose-independent. They are |
| 396 |
// also redundant when called from |X509_verify_cert|, though |
| 397 |
// not when |X509_check_purpose| is called directly. |
| 398 |
return check_ca(x); |
| 399 |
} |
| 400 |
// We need to do digital signatures or key agreement |
| 401 |
if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_AGREEMENT)) { |
| 402 |
return 0; |
| 403 |
} |
| 404 |
// nsCertType if present should allow SSL client use |
| 405 |
if (ns_reject(x, NS_SSL_CLIENT)) { |
| 406 |
return 0; |
| 407 |
} |
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
// Key usage needed for TLS/SSL server: digital signature, encipherment or |
| 412 |
// key agreement. The ssl code can check this more thoroughly for individual |
| 413 |
// key types. |
| 414 |
#define X509v3_KU_TLS \ |
| 415 |
(X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_KEY_ENCIPHERMENT | \ |
| 416 |
X509v3_KU_KEY_AGREEMENT) |
| 417 |
|
| 418 |
static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
| 419 |
int ca) { |
| 420 |
if (xku_reject(x, XKU_SSL_SERVER)) { |
| 421 |
return 0; |
| 422 |
} |
| 423 |
if (ca) { |
| 424 |
return check_ca(x); |
| 425 |
} |
| 426 |
|
| 427 |
if (ns_reject(x, NS_SSL_SERVER)) { |
| 428 |
return 0; |
| 429 |
} |
| 430 |
if (ku_reject(x, X509v3_KU_TLS)) { |
| 431 |
return 0; |
| 432 |
} |
| 433 |
|
| 434 |
return 1; |
| 435 |
} |
| 436 |
|
| 437 |
static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, |
| 438 |
int ca) { |
| 439 |
int ret = check_purpose_ssl_server(xp, x, ca); |
| 440 |
if (!ret || ca) { |
| 441 |
return ret; |
| 442 |
} |
| 443 |
// We need to encipher or Netscape complains |
| 444 |
if (ku_reject(x, X509v3_KU_KEY_ENCIPHERMENT)) { |
| 445 |
return 0; |
| 446 |
} |
| 447 |
return ret; |
| 448 |
} |
| 449 |
|
| 450 |
// purpose_smime returns one if |x| is a valid S/MIME leaf (|ca| is zero) or CA |
| 451 |
// (|ca| is one) certificate, and zero otherwise. |
| 452 |
static int purpose_smime(const X509 *x, int ca) { |
| 453 |
if (xku_reject(x, XKU_SMIME)) { |
| 454 |
return 0; |
| 455 |
} |
| 456 |
if (ca) { |
| 457 |
// check nsCertType if present |
| 458 |
if ((x->ex_flags & EXFLAG_NSCERT) && (x->ex_nscert & NS_SMIME_CA) == 0) { |
| 459 |
return 0; |
| 460 |
} |
| 461 |
|
| 462 |
return check_ca(x); |
| 463 |
} |
| 464 |
if (x->ex_flags & EXFLAG_NSCERT) { |
| 465 |
return (x->ex_nscert & NS_SMIME) == NS_SMIME; |
| 466 |
} |
| 467 |
return 1; |
| 468 |
} |
| 469 |
|
| 470 |
static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, |
| 471 |
int ca) { |
| 472 |
int ret = purpose_smime(x, ca); |
| 473 |
if (!ret || ca) { |
| 474 |
return ret; |
| 475 |
} |
| 476 |
if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE | X509v3_KU_NON_REPUDIATION)) { |
| 477 |
return 0; |
| 478 |
} |
| 479 |
return ret; |
| 480 |
} |
| 481 |
|
| 482 |
static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, |
| 483 |
int ca) { |
| 484 |
int ret = purpose_smime(x, ca); |
| 485 |
if (!ret || ca) { |
| 486 |
return ret; |
| 487 |
} |
| 488 |
if (ku_reject(x, X509v3_KU_KEY_ENCIPHERMENT)) { |
| 489 |
return 0; |
| 490 |
} |
| 491 |
return ret; |
| 492 |
} |
| 493 |
|
| 494 |
static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, |
| 495 |
int ca) { |
| 496 |
if (ca) { |
| 497 |
return check_ca(x); |
| 498 |
} |
| 499 |
if (ku_reject(x, X509v3_KU_CRL_SIGN)) { |
| 500 |
return 0; |
| 501 |
} |
| 502 |
return 1; |
| 503 |
} |
| 504 |
|
| 505 |
// OCSP helper: this is *not* a full OCSP check. It just checks that each CA |
| 506 |
// is valid. Additional checks must be made on the chain. |
| 507 |
|
| 508 |
static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca) { |
| 509 |
if (ca) { |
| 510 |
return check_ca(x); |
| 511 |
} |
| 512 |
// leaf certificate is checked in OCSP_verify() |
| 513 |
return 1; |
| 514 |
} |
| 515 |
|
| 516 |
static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, |
| 517 |
int ca) { |
| 518 |
// If ca is true we must return if this is a valid CA certificate. |
| 519 |
if (ca) { |
| 520 |
return check_ca(x); |
| 521 |
} |
| 522 |
|
| 523 |
// Check the optional key usage field: |
| 524 |
// if Key Usage is present, it must be one of digitalSignature |
| 525 |
// and/or nonRepudiation (other values are not consistent and shall |
| 526 |
// be rejected). |
| 527 |
if ((x->ex_flags & EXFLAG_KUSAGE) && |
| 528 |
((x->ex_kusage & |
| 529 |
~(X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)) || |
| 530 |
!(x->ex_kusage & |
| 531 |
(X509v3_KU_NON_REPUDIATION | X509v3_KU_DIGITAL_SIGNATURE)))) { |
| 532 |
return 0; |
| 533 |
} |
| 534 |
|
| 535 |
// Only time stamp key usage is permitted and it's required. |
| 536 |
if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP) { |
| 537 |
return 0; |
| 538 |
} |
| 539 |
|
| 540 |
// Extended Key Usage MUST be critical |
| 541 |
int i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1); |
| 542 |
if (i_ext >= 0) { |
| 543 |
const X509_EXTENSION *ext = X509_get_ext(x, i_ext); |
| 544 |
if (!X509_EXTENSION_get_critical(ext)) { |
| 545 |
return 0; |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
return 1; |
| 550 |
} |
| 551 |
|
| 552 |
static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca) { return 1; } |
| 553 |
|
| 554 |
int X509_check_issued(X509 *issuer, X509 *subject) { |
| 555 |
if (X509_NAME_cmp(X509_get_subject_name(issuer), |
| 556 |
X509_get_issuer_name(subject))) { |
| 557 |
return X509_V_ERR_SUBJECT_ISSUER_MISMATCH; |
| 558 |
} |
| 559 |
if (!x509v3_cache_extensions(issuer) || !x509v3_cache_extensions(subject)) { |
| 560 |
return X509_V_ERR_UNSPECIFIED; |
| 561 |
} |
| 562 |
|
| 563 |
if (subject->akid) { |
| 564 |
int ret = X509_check_akid(issuer, subject->akid); |
| 565 |
if (ret != X509_V_OK) { |
| 566 |
return ret; |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
if (ku_reject(issuer, X509v3_KU_KEY_CERT_SIGN)) { |
| 571 |
return X509_V_ERR_KEYUSAGE_NO_CERTSIGN; |
| 572 |
} |
| 573 |
return X509_V_OK; |
| 574 |
} |
| 575 |
|
| 576 |
int X509_check_akid(X509 *issuer, const AUTHORITY_KEYID *akid) { |
| 577 |
if (!akid) { |
| 578 |
return X509_V_OK; |
| 579 |
} |
| 580 |
|
| 581 |
// Check key ids (if present) |
| 582 |
if (akid->keyid && issuer->skid && |
| 583 |
ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid)) { |
| 584 |
return X509_V_ERR_AKID_SKID_MISMATCH; |
| 585 |
} |
| 586 |
// Check serial number |
| 587 |
if (akid->serial && |
| 588 |
ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial)) { |
| 589 |
return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
| 590 |
} |
| 591 |
// Check issuer name |
| 592 |
if (akid->issuer) { |
| 593 |
// Ugh, for some peculiar reason AKID includes SEQUENCE OF |
| 594 |
// GeneralName. So look for a DirName. There may be more than one but |
| 595 |
// we only take any notice of the first. |
| 596 |
GENERAL_NAMES *gens; |
| 597 |
GENERAL_NAME *gen; |
| 598 |
X509_NAME *nm = NULL; |
| 599 |
size_t i; |
| 600 |
gens = akid->issuer; |
| 601 |
for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
| 602 |
gen = sk_GENERAL_NAME_value(gens, i); |
| 603 |
if (gen->type == GEN_DIRNAME) { |
| 604 |
nm = gen->d.dirn; |
| 605 |
break; |
| 606 |
} |
| 607 |
} |
| 608 |
if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer))) { |
| 609 |
return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH; |
| 610 |
} |
| 611 |
} |
| 612 |
return X509_V_OK; |
| 613 |
} |
| 614 |
|
| 615 |
uint32_t X509_get_extension_flags(X509 *x) { |
| 616 |
// Ignore the return value. On failure, |x->ex_flags| will include |
| 617 |
// |EXFLAG_INVALID|. |
| 618 |
x509v3_cache_extensions(x); |
| 619 |
return x->ex_flags; |
| 620 |
} |
| 621 |
|
| 622 |
uint32_t X509_get_key_usage(X509 *x) { |
| 623 |
if (!x509v3_cache_extensions(x)) { |
| 624 |
return 0; |
| 625 |
} |
| 626 |
if (x->ex_flags & EXFLAG_KUSAGE) { |
| 627 |
return x->ex_kusage; |
| 628 |
} |
| 629 |
// If there is no extension, key usage is unconstrained, so set all bits to |
| 630 |
// one. Note that, although we use |UINT32_MAX|, |ex_kusage| only contains the |
| 631 |
// first 16 bits when the extension is present. |
| 632 |
return UINT32_MAX; |
| 633 |
} |
| 634 |
|
| 635 |
uint32_t X509_get_extended_key_usage(X509 *x) { |
| 636 |
if (!x509v3_cache_extensions(x)) { |
| 637 |
return 0; |
| 638 |
} |
| 639 |
if (x->ex_flags & EXFLAG_XKUSAGE) { |
| 640 |
return x->ex_xkusage; |
| 641 |
} |
| 642 |
// If there is no extension, extended key usage is unconstrained, so set all |
| 643 |
// bits to one. |
| 644 |
return UINT32_MAX; |
| 645 |
} |
| 646 |
|
| 647 |
const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x509) { |
| 648 |
if (!x509v3_cache_extensions(x509)) { |
| 649 |
return NULL; |
| 650 |
} |
| 651 |
return x509->skid; |
| 652 |
} |
| 653 |
|
| 654 |
const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x509) { |
| 655 |
if (!x509v3_cache_extensions(x509)) { |
| 656 |
return NULL; |
| 657 |
} |
| 658 |
return x509->akid != NULL ? x509->akid->keyid : NULL; |
| 659 |
} |
| 660 |
|
| 661 |
const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x509) { |
| 662 |
if (!x509v3_cache_extensions(x509)) { |
| 663 |
return NULL; |
| 664 |
} |
| 665 |
return x509->akid != NULL ? x509->akid->issuer : NULL; |
| 666 |
} |
| 667 |
|
| 668 |
const ASN1_INTEGER *X509_get0_authority_serial(X509 *x509) { |
| 669 |
if (!x509v3_cache_extensions(x509)) { |
| 670 |
return NULL; |
| 671 |
} |
| 672 |
return x509->akid != NULL ? x509->akid->serial : NULL; |
| 673 |
} |
| 674 |
|
| 675 |
long X509_get_pathlen(X509 *x509) { |
| 676 |
if (!x509v3_cache_extensions(x509) || (x509->ex_flags & EXFLAG_BCONS) == 0) { |
| 677 |
return -1; |
| 678 |
} |
| 679 |
return x509->ex_pathlen; |
| 680 |
} |