10Sstevel@tonic-gate /* v3_purp.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate * project 2001.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate * Copyright (c) 1999-2004 The OpenSSL Project. All rights reserved.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate * are met:
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate *
150Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate * the documentation and/or other materials provided with the
180Sstevel@tonic-gate * distribution.
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate * software must display the following acknowledgment:
220Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate * endorse or promote products derived from this software without
270Sstevel@tonic-gate * prior written permission. For written permission, please contact
280Sstevel@tonic-gate * licensing@OpenSSL.org.
290Sstevel@tonic-gate *
300Sstevel@tonic-gate * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate * nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate * permission of the OpenSSL Project.
330Sstevel@tonic-gate *
340Sstevel@tonic-gate * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate * acknowledgment:
360Sstevel@tonic-gate * "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate * ====================================================================
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate * (eay@cryptsoft.com). This product includes software written by Tim
550Sstevel@tonic-gate * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate *
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
610Sstevel@tonic-gate #include <openssl/x509v3.h>
620Sstevel@tonic-gate #include <openssl/x509_vfy.h>
630Sstevel@tonic-gate
640Sstevel@tonic-gate static void x509v3_cache_extensions(X509 *x);
650Sstevel@tonic-gate
660Sstevel@tonic-gate static int check_ssl_ca(const X509 *x);
670Sstevel@tonic-gate static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca);
680Sstevel@tonic-gate static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
690Sstevel@tonic-gate static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
700Sstevel@tonic-gate static int purpose_smime(const X509 *x, int ca);
710Sstevel@tonic-gate static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
720Sstevel@tonic-gate static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca);
730Sstevel@tonic-gate static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
740Sstevel@tonic-gate static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
750Sstevel@tonic-gate static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
760Sstevel@tonic-gate
770Sstevel@tonic-gate static int xp_cmp(const X509_PURPOSE * const *a,
780Sstevel@tonic-gate const X509_PURPOSE * const *b);
790Sstevel@tonic-gate static void xptable_free(X509_PURPOSE *p);
800Sstevel@tonic-gate
810Sstevel@tonic-gate static X509_PURPOSE xstandard[] = {
820Sstevel@tonic-gate {X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, check_purpose_ssl_client, "SSL client", "sslclient", NULL},
830Sstevel@tonic-gate {X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ssl_server, "SSL server", "sslserver", NULL},
840Sstevel@tonic-gate {X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
850Sstevel@tonic-gate {X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, "S/MIME signing", "smimesign", NULL},
860Sstevel@tonic-gate {X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
870Sstevel@tonic-gate {X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, "CRL signing", "crlsign", NULL},
880Sstevel@tonic-gate {X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any", NULL},
890Sstevel@tonic-gate {X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper, "OCSP helper", "ocsphelper", NULL},
900Sstevel@tonic-gate };
910Sstevel@tonic-gate
920Sstevel@tonic-gate #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
930Sstevel@tonic-gate
940Sstevel@tonic-gate IMPLEMENT_STACK_OF(X509_PURPOSE)
950Sstevel@tonic-gate
960Sstevel@tonic-gate static STACK_OF(X509_PURPOSE) *xptable = NULL;
970Sstevel@tonic-gate
xp_cmp(const X509_PURPOSE * const * a,const X509_PURPOSE * const * b)980Sstevel@tonic-gate static int xp_cmp(const X509_PURPOSE * const *a,
990Sstevel@tonic-gate const X509_PURPOSE * const *b)
1000Sstevel@tonic-gate {
1010Sstevel@tonic-gate return (*a)->purpose - (*b)->purpose;
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate /* As much as I'd like to make X509_check_purpose use a "const" X509*
1050Sstevel@tonic-gate * I really can't because it does recalculate hashes and do other non-const
1060Sstevel@tonic-gate * things. */
X509_check_purpose(X509 * x,int id,int ca)1070Sstevel@tonic-gate int X509_check_purpose(X509 *x, int id, int ca)
1080Sstevel@tonic-gate {
1090Sstevel@tonic-gate int idx;
1100Sstevel@tonic-gate const X509_PURPOSE *pt;
1110Sstevel@tonic-gate if(!(x->ex_flags & EXFLAG_SET)) {
1120Sstevel@tonic-gate CRYPTO_w_lock(CRYPTO_LOCK_X509);
1130Sstevel@tonic-gate x509v3_cache_extensions(x);
1140Sstevel@tonic-gate CRYPTO_w_unlock(CRYPTO_LOCK_X509);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate if(id == -1) return 1;
1170Sstevel@tonic-gate idx = X509_PURPOSE_get_by_id(id);
1180Sstevel@tonic-gate if(idx == -1) return -1;
1190Sstevel@tonic-gate pt = X509_PURPOSE_get0(idx);
1200Sstevel@tonic-gate return pt->check_purpose(pt, x, ca);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
X509_PURPOSE_set(int * p,int purpose)1230Sstevel@tonic-gate int X509_PURPOSE_set(int *p, int purpose)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate if(X509_PURPOSE_get_by_id(purpose) == -1) {
1260Sstevel@tonic-gate X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE);
1270Sstevel@tonic-gate return 0;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate *p = purpose;
1300Sstevel@tonic-gate return 1;
1310Sstevel@tonic-gate }
1320Sstevel@tonic-gate
X509_PURPOSE_get_count(void)1330Sstevel@tonic-gate int X509_PURPOSE_get_count(void)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate if(!xptable) return X509_PURPOSE_COUNT;
1360Sstevel@tonic-gate return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
1370Sstevel@tonic-gate }
1380Sstevel@tonic-gate
X509_PURPOSE_get0(int idx)1390Sstevel@tonic-gate X509_PURPOSE * X509_PURPOSE_get0(int idx)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate if(idx < 0) return NULL;
142*2139Sjp161948 if(idx < (int)X509_PURPOSE_COUNT) return xstandard + idx;
1430Sstevel@tonic-gate return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
X509_PURPOSE_get_by_sname(char * sname)1460Sstevel@tonic-gate int X509_PURPOSE_get_by_sname(char *sname)
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate int i;
1490Sstevel@tonic-gate X509_PURPOSE *xptmp;
1500Sstevel@tonic-gate for(i = 0; i < X509_PURPOSE_get_count(); i++) {
1510Sstevel@tonic-gate xptmp = X509_PURPOSE_get0(i);
1520Sstevel@tonic-gate if(!strcmp(xptmp->sname, sname)) return i;
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate return -1;
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
X509_PURPOSE_get_by_id(int purpose)1570Sstevel@tonic-gate int X509_PURPOSE_get_by_id(int purpose)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate X509_PURPOSE tmp;
1600Sstevel@tonic-gate int idx;
1610Sstevel@tonic-gate if((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
1620Sstevel@tonic-gate return purpose - X509_PURPOSE_MIN;
1630Sstevel@tonic-gate tmp.purpose = purpose;
1640Sstevel@tonic-gate if(!xptable) return -1;
1650Sstevel@tonic-gate idx = sk_X509_PURPOSE_find(xptable, &tmp);
1660Sstevel@tonic-gate if(idx == -1) return -1;
1670Sstevel@tonic-gate return idx + X509_PURPOSE_COUNT;
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
X509_PURPOSE_add(int id,int trust,int flags,int (* ck)(const X509_PURPOSE *,const X509 *,int),char * name,char * sname,void * arg)1700Sstevel@tonic-gate int X509_PURPOSE_add(int id, int trust, int flags,
1710Sstevel@tonic-gate int (*ck)(const X509_PURPOSE *, const X509 *, int),
1720Sstevel@tonic-gate char *name, char *sname, void *arg)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate int idx;
1750Sstevel@tonic-gate X509_PURPOSE *ptmp;
1760Sstevel@tonic-gate /* This is set according to what we change: application can't set it */
1770Sstevel@tonic-gate flags &= ~X509_PURPOSE_DYNAMIC;
1780Sstevel@tonic-gate /* This will always be set for application modified trust entries */
1790Sstevel@tonic-gate flags |= X509_PURPOSE_DYNAMIC_NAME;
1800Sstevel@tonic-gate /* Get existing entry if any */
1810Sstevel@tonic-gate idx = X509_PURPOSE_get_by_id(id);
1820Sstevel@tonic-gate /* Need a new entry */
1830Sstevel@tonic-gate if(idx == -1) {
1840Sstevel@tonic-gate if(!(ptmp = OPENSSL_malloc(sizeof(X509_PURPOSE)))) {
1850Sstevel@tonic-gate X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
1860Sstevel@tonic-gate return 0;
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate ptmp->flags = X509_PURPOSE_DYNAMIC;
1890Sstevel@tonic-gate } else ptmp = X509_PURPOSE_get0(idx);
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate /* OPENSSL_free existing name if dynamic */
1920Sstevel@tonic-gate if(ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
1930Sstevel@tonic-gate OPENSSL_free(ptmp->name);
1940Sstevel@tonic-gate OPENSSL_free(ptmp->sname);
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate /* dup supplied name */
1970Sstevel@tonic-gate ptmp->name = BUF_strdup(name);
1980Sstevel@tonic-gate ptmp->sname = BUF_strdup(sname);
1990Sstevel@tonic-gate if(!ptmp->name || !ptmp->sname) {
2000Sstevel@tonic-gate X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
2010Sstevel@tonic-gate return 0;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate /* Keep the dynamic flag of existing entry */
2040Sstevel@tonic-gate ptmp->flags &= X509_PURPOSE_DYNAMIC;
2050Sstevel@tonic-gate /* Set all other flags */
2060Sstevel@tonic-gate ptmp->flags |= flags;
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate ptmp->purpose = id;
2090Sstevel@tonic-gate ptmp->trust = trust;
2100Sstevel@tonic-gate ptmp->check_purpose = ck;
2110Sstevel@tonic-gate ptmp->usr_data = arg;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate /* If its a new entry manage the dynamic table */
2140Sstevel@tonic-gate if(idx == -1) {
2150Sstevel@tonic-gate if(!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) {
2160Sstevel@tonic-gate X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
2170Sstevel@tonic-gate return 0;
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
2200Sstevel@tonic-gate X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
2210Sstevel@tonic-gate return 0;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate return 1;
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate
xptable_free(X509_PURPOSE * p)2270Sstevel@tonic-gate static void xptable_free(X509_PURPOSE *p)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate if(!p) return;
2300Sstevel@tonic-gate if (p->flags & X509_PURPOSE_DYNAMIC)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
2330Sstevel@tonic-gate OPENSSL_free(p->name);
2340Sstevel@tonic-gate OPENSSL_free(p->sname);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate OPENSSL_free(p);
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
X509_PURPOSE_cleanup(void)2400Sstevel@tonic-gate void X509_PURPOSE_cleanup(void)
2410Sstevel@tonic-gate {
242*2139Sjp161948 unsigned int i;
2430Sstevel@tonic-gate sk_X509_PURPOSE_pop_free(xptable, xptable_free);
2440Sstevel@tonic-gate for(i = 0; i < X509_PURPOSE_COUNT; i++) xptable_free(xstandard + i);
2450Sstevel@tonic-gate xptable = NULL;
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
X509_PURPOSE_get_id(X509_PURPOSE * xp)2480Sstevel@tonic-gate int X509_PURPOSE_get_id(X509_PURPOSE *xp)
2490Sstevel@tonic-gate {
2500Sstevel@tonic-gate return xp->purpose;
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
X509_PURPOSE_get0_name(X509_PURPOSE * xp)2530Sstevel@tonic-gate char *X509_PURPOSE_get0_name(X509_PURPOSE *xp)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate return xp->name;
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
X509_PURPOSE_get0_sname(X509_PURPOSE * xp)2580Sstevel@tonic-gate char *X509_PURPOSE_get0_sname(X509_PURPOSE *xp)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate return xp->sname;
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
X509_PURPOSE_get_trust(X509_PURPOSE * xp)2630Sstevel@tonic-gate int X509_PURPOSE_get_trust(X509_PURPOSE *xp)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate return xp->trust;
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate
nid_cmp(int * a,int * b)2680Sstevel@tonic-gate static int nid_cmp(int *a, int *b)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate return *a - *b;
2710Sstevel@tonic-gate }
2720Sstevel@tonic-gate
X509_supported_extension(X509_EXTENSION * ex)2730Sstevel@tonic-gate int X509_supported_extension(X509_EXTENSION *ex)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate /* This table is a list of the NIDs of supported extensions:
2760Sstevel@tonic-gate * that is those which are used by the verify process. If
2770Sstevel@tonic-gate * an extension is critical and doesn't appear in this list
2780Sstevel@tonic-gate * then the verify process will normally reject the certificate.
2790Sstevel@tonic-gate * The list must be kept in numerical order because it will be
2800Sstevel@tonic-gate * searched using bsearch.
2810Sstevel@tonic-gate */
2820Sstevel@tonic-gate
2830Sstevel@tonic-gate static int supported_nids[] = {
2840Sstevel@tonic-gate NID_netscape_cert_type, /* 71 */
2850Sstevel@tonic-gate NID_key_usage, /* 83 */
2860Sstevel@tonic-gate NID_subject_alt_name, /* 85 */
2870Sstevel@tonic-gate NID_basic_constraints, /* 87 */
288*2139Sjp161948 NID_ext_key_usage, /* 126 */
289*2139Sjp161948 NID_proxyCertInfo /* 661 */
2900Sstevel@tonic-gate };
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate int ex_nid;
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
2950Sstevel@tonic-gate
2960Sstevel@tonic-gate if (ex_nid == NID_undef)
2970Sstevel@tonic-gate return 0;
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate if (OBJ_bsearch((char *)&ex_nid, (char *)supported_nids,
3000Sstevel@tonic-gate sizeof(supported_nids)/sizeof(int), sizeof(int),
3010Sstevel@tonic-gate (int (*)(const void *, const void *))nid_cmp))
3020Sstevel@tonic-gate return 1;
3030Sstevel@tonic-gate return 0;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate
x509v3_cache_extensions(X509 * x)3070Sstevel@tonic-gate static void x509v3_cache_extensions(X509 *x)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate BASIC_CONSTRAINTS *bs;
310*2139Sjp161948 PROXY_CERT_INFO_EXTENSION *pci;
3110Sstevel@tonic-gate ASN1_BIT_STRING *usage;
3120Sstevel@tonic-gate ASN1_BIT_STRING *ns;
3130Sstevel@tonic-gate EXTENDED_KEY_USAGE *extusage;
3140Sstevel@tonic-gate X509_EXTENSION *ex;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate int i;
3170Sstevel@tonic-gate if(x->ex_flags & EXFLAG_SET) return;
3180Sstevel@tonic-gate #ifndef OPENSSL_NO_SHA
3190Sstevel@tonic-gate X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
3200Sstevel@tonic-gate #endif
3210Sstevel@tonic-gate /* Does subject name match issuer ? */
3220Sstevel@tonic-gate if(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)))
3230Sstevel@tonic-gate x->ex_flags |= EXFLAG_SS;
3240Sstevel@tonic-gate /* V1 should mean no extensions ... */
3250Sstevel@tonic-gate if(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1;
3260Sstevel@tonic-gate /* Handle basic constraints */
3270Sstevel@tonic-gate if((bs=X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
3280Sstevel@tonic-gate if(bs->ca) x->ex_flags |= EXFLAG_CA;
3290Sstevel@tonic-gate if(bs->pathlen) {
3300Sstevel@tonic-gate if((bs->pathlen->type == V_ASN1_NEG_INTEGER)
3310Sstevel@tonic-gate || !bs->ca) {
3320Sstevel@tonic-gate x->ex_flags |= EXFLAG_INVALID;
3330Sstevel@tonic-gate x->ex_pathlen = 0;
3340Sstevel@tonic-gate } else x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
3350Sstevel@tonic-gate } else x->ex_pathlen = -1;
3360Sstevel@tonic-gate BASIC_CONSTRAINTS_free(bs);
3370Sstevel@tonic-gate x->ex_flags |= EXFLAG_BCONS;
3380Sstevel@tonic-gate }
339*2139Sjp161948 /* Handle proxy certificates */
340*2139Sjp161948 if((pci=X509_get_ext_d2i(x, NID_proxyCertInfo, NULL, NULL))) {
341*2139Sjp161948 if (x->ex_flags & EXFLAG_CA
342*2139Sjp161948 || X509_get_ext_by_NID(x, NID_subject_alt_name, 0) >= 0
343*2139Sjp161948 || X509_get_ext_by_NID(x, NID_issuer_alt_name, 0) >= 0) {
344*2139Sjp161948 x->ex_flags |= EXFLAG_INVALID;
345*2139Sjp161948 }
346*2139Sjp161948 if (pci->pcPathLengthConstraint) {
347*2139Sjp161948 x->ex_pcpathlen =
348*2139Sjp161948 ASN1_INTEGER_get(pci->pcPathLengthConstraint);
349*2139Sjp161948 } else x->ex_pcpathlen = -1;
350*2139Sjp161948 PROXY_CERT_INFO_EXTENSION_free(pci);
351*2139Sjp161948 x->ex_flags |= EXFLAG_PROXY;
352*2139Sjp161948 }
3530Sstevel@tonic-gate /* Handle key usage */
3540Sstevel@tonic-gate if((usage=X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
3550Sstevel@tonic-gate if(usage->length > 0) {
3560Sstevel@tonic-gate x->ex_kusage = usage->data[0];
3570Sstevel@tonic-gate if(usage->length > 1)
3580Sstevel@tonic-gate x->ex_kusage |= usage->data[1] << 8;
3590Sstevel@tonic-gate } else x->ex_kusage = 0;
3600Sstevel@tonic-gate x->ex_flags |= EXFLAG_KUSAGE;
3610Sstevel@tonic-gate ASN1_BIT_STRING_free(usage);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate x->ex_xkusage = 0;
3640Sstevel@tonic-gate if((extusage=X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {
3650Sstevel@tonic-gate x->ex_flags |= EXFLAG_XKUSAGE;
3660Sstevel@tonic-gate for(i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
3670Sstevel@tonic-gate switch(OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage,i))) {
3680Sstevel@tonic-gate case NID_server_auth:
3690Sstevel@tonic-gate x->ex_xkusage |= XKU_SSL_SERVER;
3700Sstevel@tonic-gate break;
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate case NID_client_auth:
3730Sstevel@tonic-gate x->ex_xkusage |= XKU_SSL_CLIENT;
3740Sstevel@tonic-gate break;
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate case NID_email_protect:
3770Sstevel@tonic-gate x->ex_xkusage |= XKU_SMIME;
3780Sstevel@tonic-gate break;
3790Sstevel@tonic-gate
3800Sstevel@tonic-gate case NID_code_sign:
3810Sstevel@tonic-gate x->ex_xkusage |= XKU_CODE_SIGN;
3820Sstevel@tonic-gate break;
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate case NID_ms_sgc:
3850Sstevel@tonic-gate case NID_ns_sgc:
3860Sstevel@tonic-gate x->ex_xkusage |= XKU_SGC;
3870Sstevel@tonic-gate break;
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate case NID_OCSP_sign:
3900Sstevel@tonic-gate x->ex_xkusage |= XKU_OCSP_SIGN;
3910Sstevel@tonic-gate break;
3920Sstevel@tonic-gate
3930Sstevel@tonic-gate case NID_time_stamp:
3940Sstevel@tonic-gate x->ex_xkusage |= XKU_TIMESTAMP;
3950Sstevel@tonic-gate break;
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate case NID_dvcs:
3980Sstevel@tonic-gate x->ex_xkusage |= XKU_DVCS;
3990Sstevel@tonic-gate break;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
4030Sstevel@tonic-gate }
4040Sstevel@tonic-gate
4050Sstevel@tonic-gate if((ns=X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {
4060Sstevel@tonic-gate if(ns->length > 0) x->ex_nscert = ns->data[0];
4070Sstevel@tonic-gate else x->ex_nscert = 0;
4080Sstevel@tonic-gate x->ex_flags |= EXFLAG_NSCERT;
4090Sstevel@tonic-gate ASN1_BIT_STRING_free(ns);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate x->skid =X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);
4120Sstevel@tonic-gate x->akid =X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);
4130Sstevel@tonic-gate for (i = 0; i < X509_get_ext_count(x); i++)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate ex = X509_get_ext(x, i);
4160Sstevel@tonic-gate if (!X509_EXTENSION_get_critical(ex))
4170Sstevel@tonic-gate continue;
4180Sstevel@tonic-gate if (!X509_supported_extension(ex))
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate x->ex_flags |= EXFLAG_CRITICAL;
4210Sstevel@tonic-gate break;
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate }
4240Sstevel@tonic-gate x->ex_flags |= EXFLAG_SET;
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate /* CA checks common to all purposes
4280Sstevel@tonic-gate * return codes:
4290Sstevel@tonic-gate * 0 not a CA
4300Sstevel@tonic-gate * 1 is a CA
4310Sstevel@tonic-gate * 2 basicConstraints absent so "maybe" a CA
4320Sstevel@tonic-gate * 3 basicConstraints absent but self signed V1.
4330Sstevel@tonic-gate * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
4340Sstevel@tonic-gate */
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
4370Sstevel@tonic-gate #define ku_reject(x, usage) \
4380Sstevel@tonic-gate (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
4390Sstevel@tonic-gate #define xku_reject(x, usage) \
4400Sstevel@tonic-gate (((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
4410Sstevel@tonic-gate #define ns_reject(x, usage) \
4420Sstevel@tonic-gate (((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
4430Sstevel@tonic-gate
check_ca(const X509 * x)444*2139Sjp161948 static int check_ca(const X509 *x)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate /* keyUsage if present should allow cert signing */
4470Sstevel@tonic-gate if(ku_reject(x, KU_KEY_CERT_SIGN)) return 0;
4480Sstevel@tonic-gate if(x->ex_flags & EXFLAG_BCONS) {
4490Sstevel@tonic-gate if(x->ex_flags & EXFLAG_CA) return 1;
4500Sstevel@tonic-gate /* If basicConstraints says not a CA then say so */
4510Sstevel@tonic-gate else return 0;
4520Sstevel@tonic-gate } else {
453*2139Sjp161948 /* we support V1 roots for... uh, I don't really know why. */
4540Sstevel@tonic-gate if((x->ex_flags & V1_ROOT) == V1_ROOT) return 3;
4550Sstevel@tonic-gate /* If key usage present it must have certSign so tolerate it */
4560Sstevel@tonic-gate else if (x->ex_flags & EXFLAG_KUSAGE) return 4;
457*2139Sjp161948 /* Older certificates could have Netscape-specific CA types */
458*2139Sjp161948 else if (x->ex_flags & EXFLAG_NSCERT
459*2139Sjp161948 && x->ex_nscert & NS_ANY_CA) return 5;
460*2139Sjp161948 /* can this still be regarded a CA certificate? I doubt it */
461*2139Sjp161948 return 0;
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate
X509_check_ca(X509 * x)465*2139Sjp161948 int X509_check_ca(X509 *x)
466*2139Sjp161948 {
467*2139Sjp161948 if(!(x->ex_flags & EXFLAG_SET)) {
468*2139Sjp161948 CRYPTO_w_lock(CRYPTO_LOCK_X509);
469*2139Sjp161948 x509v3_cache_extensions(x);
470*2139Sjp161948 CRYPTO_w_unlock(CRYPTO_LOCK_X509);
471*2139Sjp161948 }
472*2139Sjp161948
473*2139Sjp161948 return check_ca(x);
474*2139Sjp161948 }
475*2139Sjp161948
4760Sstevel@tonic-gate /* Check SSL CA: common checks for SSL client and server */
check_ssl_ca(const X509 * x)4770Sstevel@tonic-gate static int check_ssl_ca(const X509 *x)
4780Sstevel@tonic-gate {
4790Sstevel@tonic-gate int ca_ret;
480*2139Sjp161948 ca_ret = check_ca(x);
4810Sstevel@tonic-gate if(!ca_ret) return 0;
4820Sstevel@tonic-gate /* check nsCertType if present */
483*2139Sjp161948 if(ca_ret != 5 || x->ex_nscert & NS_SSL_CA) return ca_ret;
4840Sstevel@tonic-gate else return 0;
4850Sstevel@tonic-gate }
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate
check_purpose_ssl_client(const X509_PURPOSE * xp,const X509 * x,int ca)4880Sstevel@tonic-gate static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca)
4890Sstevel@tonic-gate {
4900Sstevel@tonic-gate if(xku_reject(x,XKU_SSL_CLIENT)) return 0;
4910Sstevel@tonic-gate if(ca) return check_ssl_ca(x);
4920Sstevel@tonic-gate /* We need to do digital signatures with it */
4930Sstevel@tonic-gate if(ku_reject(x,KU_DIGITAL_SIGNATURE)) return 0;
4940Sstevel@tonic-gate /* nsCertType if present should allow SSL client use */
4950Sstevel@tonic-gate if(ns_reject(x, NS_SSL_CLIENT)) return 0;
4960Sstevel@tonic-gate return 1;
4970Sstevel@tonic-gate }
4980Sstevel@tonic-gate
check_purpose_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)4990Sstevel@tonic-gate static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
5000Sstevel@tonic-gate {
5010Sstevel@tonic-gate if(xku_reject(x,XKU_SSL_SERVER|XKU_SGC)) return 0;
5020Sstevel@tonic-gate if(ca) return check_ssl_ca(x);
5030Sstevel@tonic-gate
5040Sstevel@tonic-gate if(ns_reject(x, NS_SSL_SERVER)) return 0;
5050Sstevel@tonic-gate /* Now as for keyUsage: we'll at least need to sign OR encipher */
5060Sstevel@tonic-gate if(ku_reject(x, KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT)) return 0;
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate return 1;
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
check_purpose_ns_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)5120Sstevel@tonic-gate static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate int ret;
5150Sstevel@tonic-gate ret = check_purpose_ssl_server(xp, x, ca);
5160Sstevel@tonic-gate if(!ret || ca) return ret;
5170Sstevel@tonic-gate /* We need to encipher or Netscape complains */
5180Sstevel@tonic-gate if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
5190Sstevel@tonic-gate return ret;
5200Sstevel@tonic-gate }
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate /* common S/MIME checks */
purpose_smime(const X509 * x,int ca)5230Sstevel@tonic-gate static int purpose_smime(const X509 *x, int ca)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate if(xku_reject(x,XKU_SMIME)) return 0;
5260Sstevel@tonic-gate if(ca) {
5270Sstevel@tonic-gate int ca_ret;
528*2139Sjp161948 ca_ret = check_ca(x);
5290Sstevel@tonic-gate if(!ca_ret) return 0;
5300Sstevel@tonic-gate /* check nsCertType if present */
531*2139Sjp161948 if(ca_ret != 5 || x->ex_nscert & NS_SMIME_CA) return ca_ret;
5320Sstevel@tonic-gate else return 0;
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate if(x->ex_flags & EXFLAG_NSCERT) {
5350Sstevel@tonic-gate if(x->ex_nscert & NS_SMIME) return 1;
5360Sstevel@tonic-gate /* Workaround for some buggy certificates */
5370Sstevel@tonic-gate if(x->ex_nscert & NS_SSL_CLIENT) return 2;
5380Sstevel@tonic-gate return 0;
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate return 1;
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate
check_purpose_smime_sign(const X509_PURPOSE * xp,const X509 * x,int ca)5430Sstevel@tonic-gate static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate int ret;
5460Sstevel@tonic-gate ret = purpose_smime(x, ca);
5470Sstevel@tonic-gate if(!ret || ca) return ret;
5480Sstevel@tonic-gate if(ku_reject(x, KU_DIGITAL_SIGNATURE|KU_NON_REPUDIATION)) return 0;
5490Sstevel@tonic-gate return ret;
5500Sstevel@tonic-gate }
5510Sstevel@tonic-gate
check_purpose_smime_encrypt(const X509_PURPOSE * xp,const X509 * x,int ca)5520Sstevel@tonic-gate static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate int ret;
5550Sstevel@tonic-gate ret = purpose_smime(x, ca);
5560Sstevel@tonic-gate if(!ret || ca) return ret;
5570Sstevel@tonic-gate if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
5580Sstevel@tonic-gate return ret;
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate
check_purpose_crl_sign(const X509_PURPOSE * xp,const X509 * x,int ca)5610Sstevel@tonic-gate static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate if(ca) {
5640Sstevel@tonic-gate int ca_ret;
565*2139Sjp161948 if((ca_ret = check_ca(x)) != 2) return ca_ret;
5660Sstevel@tonic-gate else return 0;
5670Sstevel@tonic-gate }
5680Sstevel@tonic-gate if(ku_reject(x, KU_CRL_SIGN)) return 0;
5690Sstevel@tonic-gate return 1;
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate /* OCSP helper: this is *not* a full OCSP check. It just checks that
5730Sstevel@tonic-gate * each CA is valid. Additional checks must be made on the chain.
5740Sstevel@tonic-gate */
5750Sstevel@tonic-gate
ocsp_helper(const X509_PURPOSE * xp,const X509 * x,int ca)5760Sstevel@tonic-gate static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
5770Sstevel@tonic-gate {
578*2139Sjp161948 /* Must be a valid CA. Should we really support the "I don't know"
579*2139Sjp161948 value (2)? */
580*2139Sjp161948 if(ca) return check_ca(x);
5810Sstevel@tonic-gate /* leaf certificate is checked in OCSP_verify() */
5820Sstevel@tonic-gate return 1;
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate
no_check(const X509_PURPOSE * xp,const X509 * x,int ca)5850Sstevel@tonic-gate static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
5860Sstevel@tonic-gate {
5870Sstevel@tonic-gate return 1;
5880Sstevel@tonic-gate }
5890Sstevel@tonic-gate
5900Sstevel@tonic-gate /* Various checks to see if one certificate issued the second.
5910Sstevel@tonic-gate * This can be used to prune a set of possible issuer certificates
5920Sstevel@tonic-gate * which have been looked up using some simple method such as by
5930Sstevel@tonic-gate * subject name.
5940Sstevel@tonic-gate * These are:
5950Sstevel@tonic-gate * 1. Check issuer_name(subject) == subject_name(issuer)
5960Sstevel@tonic-gate * 2. If akid(subject) exists check it matches issuer
5970Sstevel@tonic-gate * 3. If key_usage(issuer) exists check it supports certificate signing
5980Sstevel@tonic-gate * returns 0 for OK, positive for reason for mismatch, reasons match
5990Sstevel@tonic-gate * codes for X509_verify_cert()
6000Sstevel@tonic-gate */
6010Sstevel@tonic-gate
X509_check_issued(X509 * issuer,X509 * subject)6020Sstevel@tonic-gate int X509_check_issued(X509 *issuer, X509 *subject)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate if(X509_NAME_cmp(X509_get_subject_name(issuer),
6050Sstevel@tonic-gate X509_get_issuer_name(subject)))
6060Sstevel@tonic-gate return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
6070Sstevel@tonic-gate x509v3_cache_extensions(issuer);
6080Sstevel@tonic-gate x509v3_cache_extensions(subject);
6090Sstevel@tonic-gate if(subject->akid) {
6100Sstevel@tonic-gate /* Check key ids (if present) */
6110Sstevel@tonic-gate if(subject->akid->keyid && issuer->skid &&
6120Sstevel@tonic-gate ASN1_OCTET_STRING_cmp(subject->akid->keyid, issuer->skid) )
6130Sstevel@tonic-gate return X509_V_ERR_AKID_SKID_MISMATCH;
6140Sstevel@tonic-gate /* Check serial number */
6150Sstevel@tonic-gate if(subject->akid->serial &&
6160Sstevel@tonic-gate ASN1_INTEGER_cmp(X509_get_serialNumber(issuer),
6170Sstevel@tonic-gate subject->akid->serial))
6180Sstevel@tonic-gate return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
6190Sstevel@tonic-gate /* Check issuer name */
6200Sstevel@tonic-gate if(subject->akid->issuer) {
6210Sstevel@tonic-gate /* Ugh, for some peculiar reason AKID includes
6220Sstevel@tonic-gate * SEQUENCE OF GeneralName. So look for a DirName.
6230Sstevel@tonic-gate * There may be more than one but we only take any
6240Sstevel@tonic-gate * notice of the first.
6250Sstevel@tonic-gate */
6260Sstevel@tonic-gate GENERAL_NAMES *gens;
6270Sstevel@tonic-gate GENERAL_NAME *gen;
6280Sstevel@tonic-gate X509_NAME *nm = NULL;
6290Sstevel@tonic-gate int i;
6300Sstevel@tonic-gate gens = subject->akid->issuer;
6310Sstevel@tonic-gate for(i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
6320Sstevel@tonic-gate gen = sk_GENERAL_NAME_value(gens, i);
6330Sstevel@tonic-gate if(gen->type == GEN_DIRNAME) {
6340Sstevel@tonic-gate nm = gen->d.dirn;
6350Sstevel@tonic-gate break;
6360Sstevel@tonic-gate }
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate if(nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
6390Sstevel@tonic-gate return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate }
642*2139Sjp161948 if(subject->ex_flags & EXFLAG_PROXY)
643*2139Sjp161948 {
644*2139Sjp161948 if(ku_reject(issuer, KU_DIGITAL_SIGNATURE))
645*2139Sjp161948 return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
646*2139Sjp161948 }
647*2139Sjp161948 else if(ku_reject(issuer, KU_KEY_CERT_SIGN))
648*2139Sjp161948 return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
6490Sstevel@tonic-gate return X509_V_OK;
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
652