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