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)
389 x->ex_flags |= EXFLAG_INVALID;
392 x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
395 BASIC_CONSTRAINTS_free(bs);
396 x->ex_flags |= EXFLAG_BCONS;
397 } else if (i != -1) {
398 x->ex_flags |= EXFLAG_INVALID;
400 /* Handle proxy certificates */
401 if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
402 if (x->ex_flags & EXFLAG_CA
403 || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
404 || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
405 x->ex_flags |= EXFLAG_INVALID;
407 if (pci->pcPathLengthConstraint) {
408 x->ex_pcpathlen = ASN1_INTEGER_get(pci->pcPathLengthConstraint);
410 x->ex_pcpathlen = -1;
411 PROXY_CERT_INFO_EXTENSION_free(pci);
412 x->ex_flags |= EXFLAG_PROXY;
413 } else if (i != -1) {
414 x->ex_flags |= EXFLAG_INVALID;
416 /* Handle key usage */
417 if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
418 if (usage->length > 0) {
419 x->ex_kusage = usage->data[0];
420 if (usage->length > 1)
421 x->ex_kusage |= usage->data[1] << 8;
424 x->ex_flags |= EXFLAG_KUSAGE;
425 ASN1_BIT_STRING_free(usage);
426 } else if (i != -1) {
427 x->ex_flags |= EXFLAG_INVALID;
430 if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
431 x->ex_flags |= EXFLAG_XKUSAGE;
432 for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
433 switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
434 case NID_server_auth:
435 x->ex_xkusage |= XKU_SSL_SERVER;
438 case NID_client_auth:
439 x->ex_xkusage |= XKU_SSL_CLIENT;
442 case NID_email_protect:
443 x->ex_xkusage |= XKU_SMIME;
447 x->ex_xkusage |= XKU_CODE_SIGN;
452 x->ex_xkusage |= XKU_SGC;
456 x->ex_xkusage |= XKU_OCSP_SIGN;
460 x->ex_xkusage |= XKU_TIMESTAMP;
464 x->ex_xkusage |= XKU_DVCS;
467 case NID_anyExtendedKeyUsage:
468 x->ex_xkusage |= XKU_ANYEKU;
472 sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
473 } else if (i != -1) {
474 x->ex_flags |= EXFLAG_INVALID;
477 if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
479 x->ex_nscert = ns->data[0];
482 x->ex_flags |= EXFLAG_NSCERT;
483 ASN1_BIT_STRING_free(ns);
484 } else if (i != -1) {
485 x->ex_flags |= EXFLAG_INVALID;
487 x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
488 if (x->skid == NULL && i != -1)
489 x->ex_flags |= EXFLAG_INVALID;
490 x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
491 if (x->akid == NULL && i != -1)
492 x->ex_flags |= EXFLAG_INVALID;
493 /* Does subject name match issuer ? */
494 if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
495 x->ex_flags |= EXFLAG_SI;
496 /* If SKID matches AKID also indicate self signed */
497 if (X509_check_akid(x, x->akid) == X509_V_OK &&
498 !ku_reject(x, KU_KEY_CERT_SIGN))
499 x->ex_flags |= EXFLAG_SS;
501 x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
502 if (x->altname == NULL && i != -1)
503 x->ex_flags |= EXFLAG_INVALID;
504 x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
505 if (x->nc == NULL && i != -1)
506 x->ex_flags |= EXFLAG_INVALID;
508 x->ex_flags |= EXFLAG_INVALID;
510 #ifndef OPENSSL_NO_RFC3779
511 x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
512 if (x->rfc3779_addr == NULL && i != -1)
513 x->ex_flags |= EXFLAG_INVALID;
514 x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
515 if (x->rfc3779_asid == NULL && i != -1)
516 x->ex_flags |= EXFLAG_INVALID;
518 for (i = 0; i < X509_get_ext_count(x); i++) {
519 ex = X509_get_ext(x, i);
520 if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
522 x->ex_flags |= EXFLAG_FRESHEST;
523 if (!X509_EXTENSION_get_critical(ex))
525 if (!X509_supported_extension(ex)) {
526 x->ex_flags |= EXFLAG_CRITICAL;
530 x509_init_sig_info(x);
531 x->ex_flags |= EXFLAG_SET;
533 tsan_st_rel((TSAN_QUALIFIER int *)&x->ex_cached, 1);
535 * Above store triggers fast lock-free check in the beginning of the
536 * function. But one has to ensure that the structure is "stable", i.e.
537 * all stores are visible on all processors. Hence the release fence.
540 CRYPTO_THREAD_unlock(x->lock);
544 * CA checks common to all purposes
548 * 2 basicConstraints absent so "maybe" a CA
549 * 3 basicConstraints absent but self signed V1.
550 * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
553 static int check_ca(const X509 *x)
555 /* keyUsage if present should allow cert signing */
556 if (ku_reject(x, KU_KEY_CERT_SIGN))
558 if (x->ex_flags & EXFLAG_BCONS) {
559 if (x->ex_flags & EXFLAG_CA)
561 /* If basicConstraints says not a CA then say so */
565 /* we support V1 roots for... uh, I don't really know why. */
566 if ((x->ex_flags & V1_ROOT) == V1_ROOT)
569 * If key usage present it must have certSign so tolerate it
571 else if (x->ex_flags & EXFLAG_KUSAGE)
573 /* Older certificates could have Netscape-specific CA types */
574 else if (x->ex_flags & EXFLAG_NSCERT && x->ex_nscert & NS_ANY_CA)
576 /* can this still be regarded a CA certificate? I doubt it */
581 void X509_set_proxy_flag(X509 *x)
583 x->ex_flags |= EXFLAG_PROXY;
586 void X509_set_proxy_pathlen(X509 *x, long l)
591 int X509_check_ca(X509 *x)
593 x509v3_cache_extensions(x);
598 /* Check SSL CA: common checks for SSL client and server */
599 static int check_ssl_ca(const X509 *x)
602 ca_ret = check_ca(x);
605 /* check nsCertType if present */
606 if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
612 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
615 if (xku_reject(x, XKU_SSL_CLIENT))
618 return check_ssl_ca(x);
619 /* We need to do digital signatures or key agreement */
620 if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_KEY_AGREEMENT))
622 /* nsCertType if present should allow SSL client use */
623 if (ns_reject(x, NS_SSL_CLIENT))
629 * Key usage needed for TLS/SSL server: digital signature, encipherment or
630 * key agreement. The ssl code can check this more thoroughly for individual
634 KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
636 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
639 if (xku_reject(x, XKU_SSL_SERVER | XKU_SGC))
642 return check_ssl_ca(x);
644 if (ns_reject(x, NS_SSL_SERVER))
646 if (ku_reject(x, KU_TLS))
653 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
657 ret = check_purpose_ssl_server(xp, x, ca);
660 /* We need to encipher or Netscape complains */
661 if (ku_reject(x, KU_KEY_ENCIPHERMENT))
666 /* common S/MIME checks */
667 static int purpose_smime(const X509 *x, int ca)
669 if (xku_reject(x, XKU_SMIME))
673 ca_ret = check_ca(x);
676 /* check nsCertType if present */
677 if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
682 if (x->ex_flags & EXFLAG_NSCERT) {
683 if (x->ex_nscert & NS_SMIME)
685 /* Workaround for some buggy certificates */
686 if (x->ex_nscert & NS_SSL_CLIENT)
693 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
697 ret = purpose_smime(x, ca);
700 if (ku_reject(x, KU_DIGITAL_SIGNATURE | KU_NON_REPUDIATION))
705 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
709 ret = purpose_smime(x, ca);
712 if (ku_reject(x, KU_KEY_ENCIPHERMENT))
717 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
722 if ((ca_ret = check_ca(x)) != 2)
727 if (ku_reject(x, KU_CRL_SIGN))
733 * OCSP helper: this is *not* a full OCSP check. It just checks that each CA
734 * is valid. Additional checks must be made on the chain.
737 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
740 * Must be a valid CA. Should we really support the "I don't know" value
745 /* leaf certificate is checked in OCSP_verify() */
749 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
754 /* If ca is true we must return if this is a valid CA certificate. */
759 * Check the optional key usage field:
760 * if Key Usage is present, it must be one of digitalSignature
761 * and/or nonRepudiation (other values are not consistent and shall
764 if ((x->ex_flags & EXFLAG_KUSAGE)
765 && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
766 !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
769 /* Only time stamp key usage is permitted and it's required. */
770 if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
773 /* Extended Key Usage MUST be critical */
774 i_ext = X509_get_ext_by_NID(x, NID_ext_key_usage, -1);
776 X509_EXTENSION *ext = X509_get_ext((X509 *)x, i_ext);
777 if (!X509_EXTENSION_get_critical(ext))
784 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
790 * Various checks to see if one certificate issued the second.
791 * This can be used to prune a set of possible issuer certificates
792 * which have been looked up using some simple method such as by
795 * 1. Check issuer_name(subject) == subject_name(issuer)
796 * 2. If akid(subject) exists check it matches issuer
797 * 3. If key_usage(issuer) exists check it supports certificate signing
798 * returns 0 for OK, positive for reason for mismatch, reasons match
799 * codes for X509_verify_cert()
802 int X509_check_issued(X509 *issuer, X509 *subject)
804 if (X509_NAME_cmp(X509_get_subject_name(issuer),
805 X509_get_issuer_name(subject)))
806 return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
808 x509v3_cache_extensions(issuer);
809 if (issuer->ex_flags & EXFLAG_INVALID)
810 return X509_V_ERR_UNSPECIFIED;
811 x509v3_cache_extensions(subject);
812 if (subject->ex_flags & EXFLAG_INVALID)
813 return X509_V_ERR_UNSPECIFIED;
816 int ret = X509_check_akid(issuer, subject->akid);
817 if (ret != X509_V_OK)
821 if (subject->ex_flags & EXFLAG_PROXY) {
822 if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
823 return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
824 } else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
825 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
829 int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
835 /* Check key ids (if present) */
836 if (akid->keyid && issuer->skid &&
837 ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid))
838 return X509_V_ERR_AKID_SKID_MISMATCH;
839 /* Check serial number */
841 ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
842 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
843 /* Check issuer name */
846 * Ugh, for some peculiar reason AKID includes SEQUENCE OF
847 * GeneralName. So look for a DirName. There may be more than one but
848 * we only take any notice of the first.
852 X509_NAME *nm = NULL;
855 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
856 gen = sk_GENERAL_NAME_value(gens, i);
857 if (gen->type == GEN_DIRNAME) {
862 if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
863 return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
868 uint32_t X509_get_extension_flags(X509 *x)
870 /* Call for side-effect of computing hash and caching extensions */
871 X509_check_purpose(x, -1, -1);
875 uint32_t X509_get_key_usage(X509 *x)
877 /* Call for side-effect of computing hash and caching extensions */
878 if (X509_check_purpose(x, -1, -1) != 1)
880 if (x->ex_flags & EXFLAG_KUSAGE)
885 uint32_t X509_get_extended_key_usage(X509 *x)
887 /* Call for side-effect of computing hash and caching extensions */
888 if (X509_check_purpose(x, -1, -1) != 1)
890 if (x->ex_flags & EXFLAG_XKUSAGE)
891 return x->ex_xkusage;
895 const ASN1_OCTET_STRING *X509_get0_subject_key_id(X509 *x)
897 /* Call for side-effect of computing hash and caching extensions */
898 if (X509_check_purpose(x, -1, -1) != 1)
903 const ASN1_OCTET_STRING *X509_get0_authority_key_id(X509 *x)
905 /* Call for side-effect of computing hash and caching extensions */
906 if (X509_check_purpose(x, -1, -1) != 1)
908 return (x->akid != NULL ? x->akid->keyid : NULL);
911 const GENERAL_NAMES *X509_get0_authority_issuer(X509 *x)
913 /* Call for side-effect of computing hash and caching extensions */
914 if (X509_check_purpose(x, -1, -1) != 1)
916 return (x->akid != NULL ? x->akid->issuer : NULL);
919 const ASN1_INTEGER *X509_get0_authority_serial(X509 *x)
921 /* Call for side-effect of computing hash and caching extensions */
922 if (X509_check_purpose(x, -1, -1) != 1)
924 return (x->akid != NULL ? x->akid->serial : NULL);
927 long X509_get_pathlen(X509 *x)
929 /* Called for side effect of caching extensions */
930 if (X509_check_purpose(x, -1, -1) != 1
931 || (x->ex_flags & EXFLAG_BCONS) == 0)
933 return x->ex_pathlen;
936 long X509_get_proxy_pathlen(X509 *x)
938 /* Called for side effect of caching extensions */
939 if (X509_check_purpose(x, -1, -1) != 1
940 || (x->ex_flags & EXFLAG_PROXY) == 0)
942 return x->ex_pcpathlen;