2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
12 #include "internal/numbers.h"
13 #include <openssl/x509v3.h>
14 #include <openssl/x509_vfy.h>
15 #include "crypto/x509.h"
16 #include "internal/tsan_assist.h"
18 static void x509v3_cache_extensions(X509 *x);
20 static int check_ssl_ca(const X509 *x);
21 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
23 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
25 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
27 static int purpose_smime(const X509 *x, int ca);
28 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
30 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
32 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
34 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
36 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
37 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
39 static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);
40 static void xptable_free(X509_PURPOSE *p);
42 static X509_PURPOSE xstandard[] = {
43 {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
44 check_purpose_ssl_client, "SSL client", "sslclient", NULL},
45 {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
46 check_purpose_ssl_server, "SSL server", "sslserver", NULL},
47 {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
48 check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
49 {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
50 "S/MIME signing", "smimesign", NULL},
51 {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
52 check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
53 {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
54 "CRL signing", "crlsign", NULL},
55 {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any",
57 {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
58 "OCSP helper", "ocsphelper", NULL},
59 {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
60 check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",
64 #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)
66 static STACK_OF(X509_PURPOSE) *xptable = NULL;
68 static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)
70 return (*a)->purpose - (*b)->purpose;
74 * As much as I'd like to make X509_check_purpose use a "const" X509* I
75 * really can't because it does recalculate hashes and do other non-const
78 int X509_check_purpose(X509 *x, int id, int ca)
81 const X509_PURPOSE *pt;
83 x509v3_cache_extensions(x);
84 if (x->ex_flags & EXFLAG_INVALID)
87 /* Return if side-effect only call */
90 idx = X509_PURPOSE_get_by_id(id);
93 pt = X509_PURPOSE_get0(idx);
94 return pt->check_purpose(pt, x, ca);
97 int X509_PURPOSE_set(int *p, int purpose)
99 if (X509_PURPOSE_get_by_id(purpose) == -1) {
100 X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE);
107 int X509_PURPOSE_get_count(void)
110 return X509_PURPOSE_COUNT;
111 return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
114 X509_PURPOSE *X509_PURPOSE_get0(int idx)
118 if (idx < (int)X509_PURPOSE_COUNT)
119 return xstandard + idx;
120 return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
123 int X509_PURPOSE_get_by_sname(const char *sname)
127 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
128 xptmp = X509_PURPOSE_get0(i);
129 if (strcmp(xptmp->sname, sname) == 0)
135 int X509_PURPOSE_get_by_id(int purpose)
140 if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
141 return purpose - X509_PURPOSE_MIN;
144 tmp.purpose = purpose;
145 idx = sk_X509_PURPOSE_find(xptable, &tmp);
148 return idx + X509_PURPOSE_COUNT;
151 int X509_PURPOSE_add(int id, int trust, int flags,
152 int (*ck) (const X509_PURPOSE *, const X509 *, int),
153 const char *name, const char *sname, void *arg)
158 * This is set according to what we change: application can't set it
160 flags &= ~X509_PURPOSE_DYNAMIC;
161 /* This will always be set for application modified trust entries */
162 flags |= X509_PURPOSE_DYNAMIC_NAME;
163 /* Get existing entry if any */
164 idx = X509_PURPOSE_get_by_id(id);
165 /* Need a new entry */
167 if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL) {
168 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
171 ptmp->flags = X509_PURPOSE_DYNAMIC;
173 ptmp = X509_PURPOSE_get0(idx);
175 /* OPENSSL_free existing name if dynamic */
176 if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
177 OPENSSL_free(ptmp->name);
178 OPENSSL_free(ptmp->sname);
180 /* dup supplied name */
181 ptmp->name = OPENSSL_strdup(name);
182 ptmp->sname = OPENSSL_strdup(sname);
183 if (!ptmp->name || !ptmp->sname) {
184 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
187 /* Keep the dynamic flag of existing entry */
188 ptmp->flags &= X509_PURPOSE_DYNAMIC;
189 /* Set all other flags */
190 ptmp->flags |= flags;
194 ptmp->check_purpose = ck;
195 ptmp->usr_data = arg;
197 /* If its a new entry manage the dynamic table */
200 && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) {
201 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
204 if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
205 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
212 OPENSSL_free(ptmp->name);
213 OPENSSL_free(ptmp->sname);
219 static void xptable_free(X509_PURPOSE *p)
223 if (p->flags & X509_PURPOSE_DYNAMIC) {
224 if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
225 OPENSSL_free(p->name);
226 OPENSSL_free(p->sname);
232 void X509_PURPOSE_cleanup(void)
234 sk_X509_PURPOSE_pop_free(xptable, xptable_free);
238 int X509_PURPOSE_get_id(const X509_PURPOSE *xp)
243 char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp)
248 char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)
253 int X509_PURPOSE_get_trust(const X509_PURPOSE *xp)
258 static int nid_cmp(const int *a, const int *b)
263 DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);
264 IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);
266 int X509_supported_extension(X509_EXTENSION *ex)
269 * This table is a list of the NIDs of supported extensions: that is
270 * those which are used by the verify process. If an extension is
271 * critical and doesn't appear in this list then the verify process will
272 * normally reject the certificate. The list must be kept in numerical
273 * order because it will be searched using bsearch.
276 static const int supported_nids[] = {
277 NID_netscape_cert_type, /* 71 */
278 NID_key_usage, /* 83 */
279 NID_subject_alt_name, /* 85 */
280 NID_basic_constraints, /* 87 */
281 NID_certificate_policies, /* 89 */
282 NID_crl_distribution_points, /* 103 */
283 NID_ext_key_usage, /* 126 */
284 #ifndef OPENSSL_NO_RFC3779
285 NID_sbgp_ipAddrBlock, /* 290 */
286 NID_sbgp_autonomousSysNum, /* 291 */
288 NID_policy_constraints, /* 401 */
289 NID_proxyCertInfo, /* 663 */
290 NID_name_constraints, /* 666 */
291 NID_policy_mappings, /* 747 */
292 NID_inhibit_any_policy /* 748 */
295 int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
297 if (ex_nid == NID_undef)
300 if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
305 static int setup_dp(X509 *x, DIST_POINT *dp)
307 X509_NAME *iname = NULL;
311 if (dp->reasons->length > 0)
312 dp->dp_reasons = dp->reasons->data[0];
313 if (dp->reasons->length > 1)
314 dp->dp_reasons |= (dp->reasons->data[1] << 8);
315 dp->dp_reasons &= CRLDP_ALL_REASONS;
317 dp->dp_reasons = CRLDP_ALL_REASONS;
318 if (!dp->distpoint || (dp->distpoint->type != 1))
320 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
321 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
322 if (gen->type == GEN_DIRNAME) {
323 iname = gen->d.directoryName;
328 iname = X509_get_issuer_name(x);
330 return DIST_POINT_set_dpname(dp->distpoint, iname);
333 static int setup_crldp(X509 *x)
337 x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
338 if (x->crldp == NULL && i != -1)
340 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
341 if (!setup_dp(x, sk_DIST_POINT_value(x->crldp, i)))
347 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
348 #define ku_reject(x, usage) \
349 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
350 #define xku_reject(x, usage) \
351 (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
352 #define ns_reject(x, usage) \
353 (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
355 static void x509v3_cache_extensions(X509 *x)
357 BASIC_CONSTRAINTS *bs;
358 PROXY_CERT_INFO_EXTENSION *pci;
359 ASN1_BIT_STRING *usage;
361 EXTENDED_KEY_USAGE *extusage;
366 /* fast lock-free check, see end of the function for details. */
367 if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
371 CRYPTO_THREAD_write_lock(x->lock);
372 if (x->ex_flags & EXFLAG_SET) {
373 CRYPTO_THREAD_unlock(x->lock);
377 if (!X509_digest(x, EVP_sha1(), x->sha1_hash, NULL))
378 x->ex_flags |= EXFLAG_INVALID;
379 /* V1 should mean no extensions ... */
380 if (!X509_get_version(x))
381 x->ex_flags |= EXFLAG_V1;
382 /* Handle basic constraints */
383 if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL))) {
385 x->ex_flags |= EXFLAG_CA;
387 if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
388 x->ex_flags |= EXFLAG_INVALID;
391 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
392 if (!bs->ca && x->ex_pathlen != 0) {
393 x->ex_flags |= EXFLAG_INVALID;
399 BASIC_CONSTRAINTS_free(bs);
400 x->ex_flags |= EXFLAG_BCONS;
401 } else if (i != -1) {
402 x->ex_flags |= EXFLAG_INVALID;
404 /* Handle proxy certificates */
405 if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
406 if (x->ex_flags & EXFLAG_CA
407 || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
408 || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
409 x->ex_flags |= EXFLAG_INVALID;
411 if (pci->pcPathLengthConstraint) {
412 x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
414 x->ex_pcpathlen = -1;
415 PROXY_CERT_INFO_EXTENSION_free(pci);
416 x->ex_flags |= EXFLAG_PROXY;
417 } else if (i != -1) {
418 x->ex_flags |= EXFLAG_INVALID;
420 /* Handle key usage */
421 if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
422 if (usage->length > 0) {
423 x->ex_kusage = usage->data[0];
424 if (usage->length > 1)
425 x->ex_kusage |= usage->data[1] << 8;
428 x->ex_flags |= EXFLAG_KUSAGE;
429 ASN1_BIT_STRING_free(usage);
430 } else if (i != -1) {
431 x->ex_flags |= EXFLAG_INVALID;
434 if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
435 x->ex_flags |= EXFLAG_XKUSAGE;
436 for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
437 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
438 case NID_server_auth:
439 x->ex_xkusage |= XKU_SSL_SERVER;
442 case NID_client_auth:
443 x->ex_xkusage |= XKU_SSL_CLIENT;
446 case NID_email_protect:
447 x->ex_xkusage |= XKU_SMIME;
451 x->ex_xkusage |= XKU_CODE_SIGN;
456 x->ex_xkusage |= XKU_SGC;
460 x->ex_xkusage |= XKU_OCSP_SIGN;
464 x->ex_xkusage |= XKU_TIMESTAMP;
468 x->ex_xkusage |= XKU_DVCS;
471 case NID_anyExtendedKeyUsage:
472 x->ex_xkusage |= XKU_ANYEKU;
476 sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
477 } else if (i != -1) {
478 x->ex_flags |= EXFLAG_INVALID;
481 if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
483 x->ex_nscert = ns->data[0];
486 x->ex_flags |= EXFLAG_NSCERT;
487 ASN1_BIT_STRING_free(ns);
488 } else if (i != -1) {
489 x->ex_flags |= EXFLAG_INVALID;
491 x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
492 if (x->skid == NULL && i != -1)
493 x->ex_flags |= EXFLAG_INVALID;
494 x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
495 if (x->akid == NULL && i != -1)
496 x->ex_flags |= EXFLAG_INVALID;
497 /* Does subject name match issuer ? */
498 if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
499 x->ex_flags |= EXFLAG_SI;
500 /* If SKID matches AKID also indicate self signed */
501 if (X509_check_akid(x, x->akid) == X509_V_OK &&
502 !ku_reject(x, KU_KEY_CERT_SIGN))
503 x->ex_flags |= EXFLAG_SS;
505 x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
506 if (x->altname == NULL && i != -1)
507 x->ex_flags |= EXFLAG_INVALID;
508 x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
509 if (x->nc == NULL && i != -1)
510 x->ex_flags |= EXFLAG_INVALID;
512 x->ex_flags |= EXFLAG_INVALID;
514 #ifndef OPENSSL_NO_RFC3779
515 x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
516 if (x->rfc3779_addr == NULL && i != -1)
517 x->ex_flags |= EXFLAG_INVALID;
518 x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
519 if (x->rfc3779_asid == NULL && i != -1)
520 x->ex_flags |= EXFLAG_INVALID;
522 for (i = 0; i < X509_get_ext_count(x); i++) {
523 ex = X509_get_ext(x, i);
524 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
526 x->ex_flags |= EXFLAG_FRESHEST;
527 if (!X509_EXTENSION_get_critical(ex))
529 if (!X509_supported_extension(ex)) {
530 x->ex_flags |= EXFLAG_CRITICAL;
534 x509_init_sig_info(x);
535 x->ex_flags |= EXFLAG_SET;
537 tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
539 * Above store triggers fast lock-free check in the beginning of the
540 * function. But one has to ensure that the structure is "stable", i.e.
541 * all stores are visible on all processors. Hence the release fence.
544 CRYPTO_THREAD_unlock(x->lock);
548 * CA checks common to all purposes
552 * 2 Only possible in older versions of openSSL when basicConstraints are absent
553 * new versions will not return this value. May be a CA
554 * 3 basicConstraints absent but self signed V1.
555 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
556 * 5 Netscape specific CA Flags present
559 static int check_ca(const X509 *x)
561 /* keyUsage if present should allow cert signing */
562 if (ku_reject(x, KU_KEY_CERT_SIGN))
564 if (x->ex_flags & EXFLAG_BCONS) {
565 if (x->ex_flags & EXFLAG_CA)
567 /* If basicConstraints says not a CA then say so */
571 /* we support V1 roots for... uh, I don't really know why. */
572 if ((x->ex_flags & V1_ROOT) == V1_ROOT)
575 * If key usage present it must have certSign so tolerate it
577 else if (x->ex_flags & EXFLAG_KUSAGE)
579 /* Older certificates could have Netscape-specific CA types */
580 else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
582 /* can this still be regarded a CA certificate? I doubt it */
587 void X509_set_proxy_flag(X509 *x)
589 x->ex_flags |= EXFLAG_PROXY;
592 void X509_set_proxy_pathlen(X509 *x, long l)
597 int X509_check_ca(X509 *x)
599 x509v3_cache_extensions(x);
604 /* Check SSL CA: common checks for SSL client and server */
605 static int check_ssl_ca(const X509 *x)
608 ca_ret = check_ca(x);
611 /* check nsCertType if present */
612 if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
618 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
621 if (xku_reject(x, XKU_SSL_CLIENT))
624 return check_ssl_ca(x);
625 /* We need to do digital signatures or key agreement */
626 if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
628 /* nsCertType if present should allow SSL client use */
629 if (ns_reject(x, NS_SSL_CLIENT))
635 * Key usage needed for TLS/SSL server: digital signature, encipherment or
636 * key agreement. The ssl code can check this more thoroughly for individual
640 KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
642 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
645 if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
648 return check_ssl_ca(x);
650 if (ns_reject(x, NS_SSL_SERVER))
652 if (ku_reject(x, KU_TLS))
659 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
663 ret = check_purpose_ssl_server(xp, x, ca);
666 /* We need to encipher or Netscape complains */
667 if (ku_reject(x, KU_KEY_ENCIPHERMENT))
672 /* common S/MIME checks */
673 static int purpose_smime(const X509 *x, int ca)
675 if (xku_reject(x, XKU_SMIME))
679 ca_ret = check_ca(x);
682 /* check nsCertType if present */
683 if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
688 if (x->ex_flags & EXFLAG_NSCERT) {
689 if (x->ex_nscert & NS_SMIME)
691 /* Workaround for some buggy certificates */
692 if (x->ex_nscert & NS_SSL_CLIENT)
699 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
703 ret = purpose_smime(x, ca);
706 if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
711 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
715 ret = purpose_smime(x, ca);
718 if (ku_reject(x, KU_KEY_ENCIPHERMENT))
723 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
728 if ((ca_ret = check_ca(x)) != 2)
733 if (ku_reject(x, KU_CRL_SIGN))
739 * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
740 * is valid. Additional checks must be made on the chain.
743 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
746 * Must be a valid CA. Should we really support the "I don't know" value
751 /* leaf certificate is checked in OCSP_verify() */
755 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
760 /* If ca is true we must return if this is a valid CA certificate. */
765 * Check the optional key usage field:
766 * if Key Usage is present, it must be one of digitalSignature
767 * and/or nonRepudiation (other values are not consistent and shall
770 if ((x->ex_flags & EXFLAG_KUSAGE)
771 && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
772 !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
775 /* Only time stamp key usage is permitted and it's required. */
776 if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
779 /* Extended Key Usage MUST be critical */
780 i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
782 X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
783 if (!X509_EXTENSION_get_critical(ext))
790 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
796 * Various checks to see if one certificate issued the second.
797 * This can be used to prune a set of possible issuer certificates
798 * which have been looked up using some simple method such as by
801 * 1. Check issuer_name(subject) == subject_name(issuer)
802 * 2. If akid(subject) exists check it matches issuer
803 * 3. If key_usage(issuer) exists check it supports certificate signing
804 * returns 0 for OK, positive for reason for mismatch, reasons match
805 * codes for X509_verify_cert()
808 int X509_check_issued(X509 *issuer, X509 *subject)
810 if (X509_NAME_cmp(X509_get_subject_name(issuer),
811 X509_get_issuer_name(subject)))
812 return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
814 x509v3_cache_extensions(issuer);
815 if (issuer->ex_flags & EXFLAG_INVALID)
816 return X509_V_ERR_UNSPECIFIED;
817 x509v3_cache_extensions(subject);
818 if (subject->ex_flags & EXFLAG_INVALID)
819 return X509_V_ERR_UNSPECIFIED;
822 int ret = X509_check_akid(issuer, subject->akid);
823 if (ret != X509_V_OK)
827 if (subject->ex_flags & EXFLAG_PROXY) {
828 if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
829 return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
830 } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
831 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
835 int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
841 /* Check key ids (if present) */
842 if (akid->keyid && issuer->skid &&
843 ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
844 return X509_V_ERR_AKID_SKID_MISMATCH;
845 /* Check serial number */
847 ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
848 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
849 /* Check issuer name */
852 * Ugh, for some peculiar reason AKID includes SEQUENCE OF
853 * GeneralName. So look for a DirName. There may be more than one but
854 * we only take any notice of the first.
858 X509_NAME *nm = NULL;
861 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
862 gen = sk_GENERAL_NAME_value(gens, i);
863 if (gen->type == GEN_DIRNAME) {
868 if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
869 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
874 uint32_t X509_get_extension_flags(X509 *x)
876 /* Call for side-effect of computing hash and caching extensions */
877 X509_check_purpose(x, -1, -1);
881 uint32_t X509_get_key_usage(X509 *x)
883 /* Call for side-effect of computing hash and caching extensions */
884 if (X509_check_purpose(x, -1, -1) != 1)
886 if (x->ex_flags & EXFLAG_KUSAGE)
891 uint32_t X509_get_extended_key_usage(X509 *x)
893 /* Call for side-effect of computing hash and caching extensions */
894 if (X509_check_purpose(x, -1, -1) != 1)
896 if (x->ex_flags & EXFLAG_XKUSAGE)
897 return x->ex_xkusage;
901 const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
903 /* Call for side-effect of computing hash and caching extensions */
904 if (X509_check_purpose(x, -1, -1) != 1)
909 const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
911 /* Call for side-effect of computing hash and caching extensions */
912 if (X509_check_purpose(x, -1, -1) != 1)
914 return (x->akid != NULL ? x->akid->keyid : NULL);
917 const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
919 /* Call for side-effect of computing hash and caching extensions */
920 if (X509_check_purpose(x, -1, -1) != 1)
922 return (x->akid != NULL ? x->akid->issuer : NULL);
925 const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
927 /* Call for side-effect of computing hash and caching extensions */
928 if (X509_check_purpose(x, -1, -1) != 1)
930 return (x->akid != NULL ? x->akid->serial : NULL);
933 long X509_get_pathlen(X509 *x)
935 /* Called for side effect of caching extensions */
936 if (X509_check_purpose(x, -1, -1) != 1
937 || (x->ex_flags & EXFLAG_BCONS) == 0)
939 return x->ex_pathlen;
942 long X509_get_proxy_pathlen(X509 *x)
944 /* Called for side effect of caching extensions */
945 if (X509_check_purpose(x, -1, -1) != 1
946 || (x->ex_flags & EXFLAG_PROXY) == 0)
948 return x->ex_pcpathlen;