2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 DEFINE_STACK_OF(GENERAL_NAME)
19 DEFINE_STACK_OF(DIST_POINT)
20 DEFINE_STACK_OF(X509_PURPOSE)
21 DEFINE_STACK_OF(ASN1_OBJECT)
23 static int check_ssl_ca(const X509 *x);
24 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
26 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
28 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
30 static int purpose_smime(const X509 *x, int ca);
31 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
33 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
35 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
37 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
39 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
40 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
42 static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b);
43 static void xptable_free(X509_PURPOSE *p);
45 static X509_PURPOSE xstandard[] = {
46 {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0,
47 check_purpose_ssl_client, "SSL client", "sslclient", NULL},
48 {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
49 check_purpose_ssl_server, "SSL server", "sslserver", NULL},
50 {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0,
51 check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
52 {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign,
53 "S/MIME signing", "smimesign", NULL},
54 {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0,
55 check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
56 {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign,
57 "CRL signing", "crlsign", NULL},
58 {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any",
60 {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper,
61 "OCSP helper", "ocsphelper", NULL},
62 {X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0,
63 check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign",
67 #define X509_PURPOSE_COUNT OSSL_NELEM(xstandard)
69 static STACK_OF(X509_PURPOSE) *xptable = NULL;
71 static int xp_cmp(const X509_PURPOSE *const *a, const X509_PURPOSE *const *b)
73 return (*a)->purpose - (*b)->purpose;
77 * As much as I'd like to make X509_check_purpose use a "const" X509* I
78 * really can't because it does recalculate hashes and do other non-const
81 int X509_check_purpose(X509 *x, int id, int ca)
84 const X509_PURPOSE *pt;
86 if (!X509v3_cache_extensions(x, NULL, NULL))
89 /* Return if side-effect only call */
92 idx = X509_PURPOSE_get_by_id(id);
95 pt = X509_PURPOSE_get0(idx);
96 return pt->check_purpose(pt, x, ca);
99 int X509_PURPOSE_set(int *p, int purpose)
101 if (X509_PURPOSE_get_by_id(purpose) == -1) {
102 X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE);
109 int X509_PURPOSE_get_count(void)
112 return X509_PURPOSE_COUNT;
113 return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
116 X509_PURPOSE *X509_PURPOSE_get0(int idx)
120 if (idx < (int)X509_PURPOSE_COUNT)
121 return xstandard + idx;
122 return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
125 int X509_PURPOSE_get_by_sname(const char *sname)
129 for (i = 0; i < X509_PURPOSE_get_count(); i++) {
130 xptmp = X509_PURPOSE_get0(i);
131 if (strcmp(xptmp->sname, sname) == 0)
137 int X509_PURPOSE_get_by_id(int purpose)
142 if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
143 return purpose - X509_PURPOSE_MIN;
146 tmp.purpose = purpose;
147 idx = sk_X509_PURPOSE_find(xptable, &tmp);
150 return idx + X509_PURPOSE_COUNT;
153 int X509_PURPOSE_add(int id, int trust, int flags,
154 int (*ck) (const X509_PURPOSE *, const X509 *, int),
155 const char *name, const char *sname, void *arg)
160 * This is set according to what we change: application can't set it
162 flags &= ~X509_PURPOSE_DYNAMIC;
163 /* This will always be set for application modified trust entries */
164 flags |= X509_PURPOSE_DYNAMIC_NAME;
165 /* Get existing entry if any */
166 idx = X509_PURPOSE_get_by_id(id);
167 /* Need a new entry */
169 if ((ptmp = OPENSSL_malloc(sizeof(*ptmp))) == NULL) {
170 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
173 ptmp->flags = X509_PURPOSE_DYNAMIC;
175 ptmp = X509_PURPOSE_get0(idx);
177 /* OPENSSL_free existing name if dynamic */
178 if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
179 OPENSSL_free(ptmp->name);
180 OPENSSL_free(ptmp->sname);
182 /* dup supplied name */
183 ptmp->name = OPENSSL_strdup(name);
184 ptmp->sname = OPENSSL_strdup(sname);
185 if (ptmp->name == NULL|| ptmp->sname == NULL) {
186 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
189 /* Keep the dynamic flag of existing entry */
190 ptmp->flags &= X509_PURPOSE_DYNAMIC;
191 /* Set all other flags */
192 ptmp->flags |= flags;
196 ptmp->check_purpose = ck;
197 ptmp->usr_data = arg;
199 /* If its a new entry manage the dynamic table */
202 && (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL) {
203 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
206 if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
207 X509V3err(X509V3_F_X509_PURPOSE_ADD, ERR_R_MALLOC_FAILURE);
214 OPENSSL_free(ptmp->name);
215 OPENSSL_free(ptmp->sname);
221 static void xptable_free(X509_PURPOSE *p)
225 if (p->flags & X509_PURPOSE_DYNAMIC) {
226 if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
227 OPENSSL_free(p->name);
228 OPENSSL_free(p->sname);
234 void X509_PURPOSE_cleanup(void)
236 sk_X509_PURPOSE_pop_free(xptable, xptable_free);
240 int X509_PURPOSE_get_id(const X509_PURPOSE *xp)
245 char *X509_PURPOSE_get0_name(const X509_PURPOSE *xp)
250 char *X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)
255 int X509_PURPOSE_get_trust(const X509_PURPOSE *xp)
260 static int nid_cmp(const int *a, const int *b)
265 DECLARE_OBJ_BSEARCH_CMP_FN(int, int, nid);
266 IMPLEMENT_OBJ_BSEARCH_CMP_FN(int, int, nid);
268 int X509_supported_extension(X509_EXTENSION *ex)
271 * This table is a list of the NIDs of supported extensions: that is
272 * those which are used by the verify process. If an extension is
273 * critical and doesn't appear in this list then the verify process will
274 * normally reject the certificate. The list must be kept in numerical
275 * order because it will be searched using bsearch.
278 static const int supported_nids[] = {
279 NID_netscape_cert_type, /* 71 */
280 NID_key_usage, /* 83 */
281 NID_subject_alt_name, /* 85 */
282 NID_basic_constraints, /* 87 */
283 NID_certificate_policies, /* 89 */
284 NID_crl_distribution_points, /* 103 */
285 NID_ext_key_usage, /* 126 */
286 #ifndef OPENSSL_NO_RFC3779
287 NID_sbgp_ipAddrBlock, /* 290 */
288 NID_sbgp_autonomousSysNum, /* 291 */
290 NID_policy_constraints, /* 401 */
291 NID_proxyCertInfo, /* 663 */
292 NID_name_constraints, /* 666 */
293 NID_policy_mappings, /* 747 */
294 NID_inhibit_any_policy /* 748 */
297 int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
299 if (ex_nid == NID_undef)
302 if (OBJ_bsearch_nid(&ex_nid, supported_nids, OSSL_NELEM(supported_nids)))
307 static int setup_dp(X509 *x, DIST_POINT *dp)
309 const X509_NAME *iname = NULL;
313 if (dp->reasons->length > 0)
314 dp->dp_reasons = dp->reasons->data[0];
315 if (dp->reasons->length > 1)
316 dp->dp_reasons |= (dp->reasons->data[1] << 8);
317 dp->dp_reasons &= CRLDP_ALL_REASONS;
319 dp->dp_reasons = CRLDP_ALL_REASONS;
320 if (!dp->distpoint || (dp->distpoint->type != 1))
322 for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
323 GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
324 if (gen->type == GEN_DIRNAME) {
325 iname = gen->d.directoryName;
330 iname = X509_get_issuer_name(x);
332 return DIST_POINT_set_dpname(dp->distpoint, iname);
335 static int setup_crldp(X509 *x)
339 x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
340 if (x->crldp == NULL && i != -1)
342 for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
343 if (!setup_dp(x, sk_DIST_POINT_value(x->crldp, i)))
349 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
350 #define ku_reject(x, usage) \
351 (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
352 #define xku_reject(x, usage) \
353 (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
354 #define ns_reject(x, usage) \
355 (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
357 /* this caches also further information, e.g., if the cert is self-issued */
358 int X509v3_cache_extensions(X509 *x, OPENSSL_CTX *libctx, const char *propq)
360 BASIC_CONSTRAINTS *bs;
361 PROXY_CERT_INFO_EXTENSION *pci;
362 ASN1_BIT_STRING *usage;
364 EXTENDED_KEY_USAGE *extusage;
370 /* fast lock-free check, see end of the function for details. */
371 if (tsan_ld_acq((TSAN_QUALIFIER int *)&x->ex_cached))
372 return (x->ex_flags & EXFLAG_INVALID) == 0;
375 CRYPTO_THREAD_write_lock(x->lock);
376 if (x->ex_flags & EXFLAG_SET) {
377 CRYPTO_THREAD_unlock(x->lock);
378 return (x->ex_flags & EXFLAG_INVALID) == 0;
381 sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
382 if (sha1 == NULL || !X509_digest(x, sha1, x->sha1_hash, NULL))
383 x->ex_flags |= EXFLAG_INVALID;
386 /* V1 should mean no extensions ... */
387 if (!X509_get_version(x))
388 x->ex_flags |= EXFLAG_V1;
389 /* Handle basic constraints */
390 if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL))) {
392 x->ex_flags |= EXFLAG_CA;
394 if (bs->pathlen->type == V_ASN1_NEG_INTEGER) {
395 x->ex_flags |= EXFLAG_INVALID;
398 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
399 if (!bs->ca && x->ex_pathlen != 0) {
400 x->ex_flags |= EXFLAG_INVALID;
406 BASIC_CONSTRAINTS_free(bs);
407 x->ex_flags |= EXFLAG_BCONS;
408 } else if (i != -1) {
409 x->ex_flags |= EXFLAG_INVALID;
411 /* Handle proxy certificates */
412 if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
413 if (x->ex_flags & EXFLAG_CA
414 || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
415 || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
416 x->ex_flags |= EXFLAG_INVALID;
418 if (pci->pcPathLengthConstraint) {
419 x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
421 x->ex_pcpathlen = -1;
422 PROXY_CERT_INFO_EXTENSION_free(pci);
423 x->ex_flags |= EXFLAG_PROXY;
424 } else if (i != -1) {
425 x->ex_flags |= EXFLAG_INVALID;
427 /* Handle key usage */
428 if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
429 if (usage->length > 0) {
430 x->ex_kusage = usage->data[0];
431 if (usage->length > 1)
432 x->ex_kusage |= usage->data[1] << 8;
435 x->ex_flags |= EXFLAG_KUSAGE;
436 ASN1_BIT_STRING_free(usage);
437 } else if (i != -1) {
438 x->ex_flags |= EXFLAG_INVALID;
441 if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
442 x->ex_flags |= EXFLAG_XKUSAGE;
443 for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
444 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
445 case NID_server_auth:
446 x->ex_xkusage |= XKU_SSL_SERVER;
449 case NID_client_auth:
450 x->ex_xkusage |= XKU_SSL_CLIENT;
453 case NID_email_protect:
454 x->ex_xkusage |= XKU_SMIME;
458 x->ex_xkusage |= XKU_CODE_SIGN;
463 x->ex_xkusage |= XKU_SGC;
467 x->ex_xkusage |= XKU_OCSP_SIGN;
471 x->ex_xkusage |= XKU_TIMESTAMP;
475 x->ex_xkusage |= XKU_DVCS;
478 case NID_anyExtendedKeyUsage:
479 x->ex_xkusage |= XKU_ANYEKU;
483 sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
484 } else if (i != -1) {
485 x->ex_flags |= EXFLAG_INVALID;
488 if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
490 x->ex_nscert = ns->data[0];
493 x->ex_flags |= EXFLAG_NSCERT;
494 ASN1_BIT_STRING_free(ns);
495 } else if (i != -1) {
496 x->ex_flags |= EXFLAG_INVALID;
498 x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
499 if (x->skid == NULL && i != -1)
500 x->ex_flags |= EXFLAG_INVALID;
501 x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
502 if (x->akid == NULL && i != -1)
503 x->ex_flags |= EXFLAG_INVALID;
504 /* Does subject name match issuer ? */
505 if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
506 x->ex_flags |= EXFLAG_SI; /* cert is self-issued */
507 if (X509_check_akid(x, x->akid) == X509_V_OK /* SKID matches AKID */
508 && !ku_reject(x, KU_KEY_CERT_SIGN))
509 x->ex_flags |= EXFLAG_SS; /* indicate self-signed */
511 x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
512 if (x->altname == NULL && i != -1)
513 x->ex_flags |= EXFLAG_INVALID;
514 x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
515 if (x->nc == NULL && i != -1)
516 x->ex_flags |= EXFLAG_INVALID;
518 x->ex_flags |= EXFLAG_INVALID;
520 #ifndef OPENSSL_NO_RFC3779
521 x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
522 if (x->rfc3779_addr == NULL && i != -1)
523 x->ex_flags |= EXFLAG_INVALID;
524 x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
525 if (x->rfc3779_asid == NULL && i != -1)
526 x->ex_flags |= EXFLAG_INVALID;
528 for (i = 0; i < X509_get_ext_count(x); i++) {
529 ex = X509_get_ext(x, i);
530 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
532 x->ex_flags |= EXFLAG_FRESHEST;
533 if (!X509_EXTENSION_get_critical(ex))
535 if (!X509_supported_extension(ex)) {
536 x->ex_flags |= EXFLAG_CRITICAL;
540 x509_init_sig_info(x);
541 x->ex_flags |= EXFLAG_SET;
543 tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
545 * Above store triggers fast lock-free check in the beginning of the
546 * function. But one has to ensure that the structure is "stable", i.e.
547 * all stores are visible on all processors. Hence the release fence.
550 CRYPTO_THREAD_unlock(x->lock);
552 return (x->ex_flags & EXFLAG_INVALID) == 0;
556 * CA checks common to all purposes
560 * 2 Only possible in older versions of openSSL when basicConstraints are absent
561 * new versions will not return this value. May be a CA
562 * 3 basicConstraints absent but self-signed V1.
563 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
564 * 5 Netscape specific CA Flags present
567 static int check_ca(const X509 *x)
569 /* keyUsage if present should allow cert signing */
570 if (ku_reject(x, KU_KEY_CERT_SIGN))
572 if (x->ex_flags & EXFLAG_BCONS) {
573 if (x->ex_flags & EXFLAG_CA)
575 /* If basicConstraints says not a CA then say so */
579 /* we support V1 roots for... uh, I don't really know why. */
580 if ((x->ex_flags & V1_ROOT) == V1_ROOT)
583 * If key usage present it must have certSign so tolerate it
585 else if (x->ex_flags & EXFLAG_KUSAGE)
587 /* Older certificates could have Netscape-specific CA types */
588 else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
590 /* can this still be regarded a CA certificate? I doubt it */
595 void X509_set_proxy_flag(X509 *x)
597 x->ex_flags |= EXFLAG_PROXY;
600 void X509_set_proxy_pathlen(X509 *x, long l)
605 int X509_check_ca(X509 *x)
607 /* Note 0 normally means "not a CA" - but in this case means error. */
608 if (!X509v3_cache_extensions(x, NULL, NULL))
614 /* Check SSL CA: common checks for SSL client and server */
615 static int check_ssl_ca(const X509 *x)
618 ca_ret = check_ca(x);
621 /* check nsCertType if present */
622 if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
628 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
631 if (xku_reject(x, XKU_SSL_CLIENT))
634 return check_ssl_ca(x);
635 /* We need to do digital signatures or key agreement */
636 if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
638 /* nsCertType if present should allow SSL client use */
639 if (ns_reject(x, NS_SSL_CLIENT))
645 * Key usage needed for TLS/SSL server: digital signature, encipherment or
646 * key agreement. The ssl code can check this more thoroughly for individual
650 KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
652 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
655 if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
658 return check_ssl_ca(x);
660 if (ns_reject(x, NS_SSL_SERVER))
662 if (ku_reject(x, KU_TLS))
669 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
673 ret = check_purpose_ssl_server(xp, x, ca);
676 /* We need to encipher or Netscape complains */
677 if (ku_reject(x, KU_KEY_ENCIPHERMENT))
682 /* common S/MIME checks */
683 static int purpose_smime(const X509 *x, int ca)
685 if (xku_reject(x, XKU_SMIME))
689 ca_ret = check_ca(x);
692 /* check nsCertType if present */
693 if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
698 if (x->ex_flags & EXFLAG_NSCERT) {
699 if (x->ex_nscert & NS_SMIME)
701 /* Workaround for some buggy certificates */
702 if (x->ex_nscert & NS_SSL_CLIENT)
709 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
713 ret = purpose_smime(x, ca);
716 if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
721 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
725 ret = purpose_smime(x, ca);
728 if (ku_reject(x, KU_KEY_ENCIPHERMENT))
733 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
738 if ((ca_ret = check_ca(x)) != 2)
743 if (ku_reject(x, KU_CRL_SIGN))
749 * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
750 * is valid. Additional checks must be made on the chain.
753 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
756 * Must be a valid CA. Should we really support the "I don't know" value
761 /* leaf certificate is checked in OCSP_verify() */
765 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
770 /* If ca is true we must return if this is a valid CA certificate. */
775 * Check the optional key usage field:
776 * if Key Usage is present, it must be one of digitalSignature
777 * and/or nonRepudiation (other values are not consistent and shall
780 if ((x->ex_flags & EXFLAG_KUSAGE)
781 && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
782 !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
785 /* Only time stamp key usage is permitted and it's required. */
786 if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
789 /* Extended Key Usage MUST be critical */
790 i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
792 X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
793 if (!X509_EXTENSION_get_critical(ext))
800 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
806 * Various checks to see if one certificate potentially issued the second.
807 * This can be used to prune a set of possible issuer certificates which
808 * have been looked up using some simple method such as by subject name.
810 * 1. Check issuer_name(subject) == subject_name(issuer)
811 * 2. If akid(subject) exists, check that it matches issuer
812 * 3. Check that issuer public key algorithm matches subject signature algorithm
813 * 4. Check that any key_usage(issuer) allows certificate signing
814 * Note that this does not include actually checking the signature.
815 * Returns 0 for OK, or positive for reason for mismatch
816 * where reason codes match those for X509_verify_cert().
819 int x509_check_issued_int(X509 *issuer, X509 *subject, OPENSSL_CTX *libctx,
822 if (X509_NAME_cmp(X509_get_subject_name(issuer),
823 X509_get_issuer_name(subject)))
824 return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
826 if (!X509v3_cache_extensions(issuer, libctx, propq)
827 || !X509v3_cache_extensions(subject, libctx, propq))
828 return X509_V_ERR_UNSPECIFIED;
831 int ret = X509_check_akid(issuer, subject->akid);
832 if (ret != X509_V_OK)
836 /* check if the subject signature alg matches the issuer's PUBKEY alg */
838 EVP_PKEY *i_pkey = X509_get0_pubkey(issuer);
839 X509_ALGOR *s_algor = &subject->cert_info.signature;
840 int s_pknid = NID_undef, s_mdnid = NID_undef;
843 return X509_V_ERR_NO_ISSUER_PUBLIC_KEY;
845 if (!OBJ_find_sigid_algs(OBJ_obj2nid(s_algor->algorithm),
847 || EVP_PKEY_type(s_pknid) != EVP_PKEY_base_id(i_pkey))
848 return X509_V_ERR_SIGNATURE_ALGORITHM_MISMATCH;
851 if (subject->ex_flags & EXFLAG_PROXY) {
852 if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
853 return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
854 } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
855 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
859 int X509_check_issued(X509 *issuer, X509 *subject)
861 return x509_check_issued_int(issuer, subject, NULL, NULL);
864 int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
870 /* Check key ids (if present) */
871 if (akid->keyid && issuer->skid &&
872 ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
873 return X509_V_ERR_AKID_SKID_MISMATCH;
874 /* Check serial number */
876 ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
877 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
878 /* Check issuer name */
881 * Ugh, for some peculiar reason AKID includes SEQUENCE OF
882 * GeneralName. So look for a DirName. There may be more than one but
883 * we only take any notice of the first.
887 X509_NAME *nm = NULL;
890 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
891 gen = sk_GENERAL_NAME_value(gens, i);
892 if (gen->type == GEN_DIRNAME) {
897 if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
898 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
903 uint32_t X509_get_extension_flags(X509 *x)
905 /* Call for side-effect of computing hash and caching extensions */
906 X509_check_purpose(x, -1, -1);
910 uint32_t X509_get_key_usage(X509 *x)
912 /* Call for side-effect of computing hash and caching extensions */
913 if (X509_check_purpose(x, -1, -1) != 1)
915 if (x->ex_flags & EXFLAG_KUSAGE)
920 uint32_t X509_get_extended_key_usage(X509 *x)
922 /* Call for side-effect of computing hash and caching extensions */
923 if (X509_check_purpose(x, -1, -1) != 1)
925 if (x->ex_flags & EXFLAG_XKUSAGE)
926 return x->ex_xkusage;
930 const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
932 /* Call for side-effect of computing hash and caching extensions */
933 if (X509_check_purpose(x, -1, -1) != 1)
938 const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
940 /* Call for side-effect of computing hash and caching extensions */
941 if (X509_check_purpose(x, -1, -1) != 1)
943 return (x->akid != NULL ? x->akid->keyid : NULL);
946 const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
948 /* Call for side-effect of computing hash and caching extensions */
949 if (X509_check_purpose(x, -1, -1) != 1)
951 return (x->akid != NULL ? x->akid->issuer : NULL);
954 const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
956 /* Call for side-effect of computing hash and caching extensions */
957 if (X509_check_purpose(x, -1, -1) != 1)
959 return (x->akid != NULL ? x->akid->serial : NULL);
962 long X509_get_pathlen(X509 *x)
964 /* Called for side effect of caching extensions */
965 if (X509_check_purpose(x, -1, -1) != 1
966 || (x->ex_flags & EXFLAG_BCONS) == 0)
968 return x->ex_pathlen;
971 long X509_get_proxy_pathlen(X509 *x)
973 /* Called for side effect of caching extensions */
974 if (X509_check_purpose(x, -1, -1) != 1
975 || (x->ex_flags & EXFLAG_PROXY) == 0)
977 return x->ex_pcpathlen;