xref: /onnv-gate/usr/src/common/openssl/crypto/x509v3/v3_purp.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* v3_purp.c */
2*0Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*0Sstevel@tonic-gate  * project 2001.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /* ====================================================================
6*0Sstevel@tonic-gate  * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
17*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
18*0Sstevel@tonic-gate  *    distribution.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
21*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
22*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
23*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
27*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
28*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
31*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
32*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
35*0Sstevel@tonic-gate  *    acknowledgment:
36*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
37*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*0Sstevel@tonic-gate  * ====================================================================
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
54*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
55*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include "cryptlib.h"
61*0Sstevel@tonic-gate #include <openssl/x509v3.h>
62*0Sstevel@tonic-gate #include <openssl/x509_vfy.h>
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static void x509v3_cache_extensions(X509 *x);
65*0Sstevel@tonic-gate 
66*0Sstevel@tonic-gate static int ca_check(const X509 *x);
67*0Sstevel@tonic-gate static int check_ssl_ca(const X509 *x);
68*0Sstevel@tonic-gate static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca);
69*0Sstevel@tonic-gate static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
70*0Sstevel@tonic-gate static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
71*0Sstevel@tonic-gate static int purpose_smime(const X509 *x, int ca);
72*0Sstevel@tonic-gate static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
73*0Sstevel@tonic-gate static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca);
74*0Sstevel@tonic-gate static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
75*0Sstevel@tonic-gate static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
76*0Sstevel@tonic-gate static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static int xp_cmp(const X509_PURPOSE * const *a,
79*0Sstevel@tonic-gate 		const X509_PURPOSE * const *b);
80*0Sstevel@tonic-gate static void xptable_free(X509_PURPOSE *p);
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static X509_PURPOSE xstandard[] = {
83*0Sstevel@tonic-gate 	{X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, check_purpose_ssl_client, "SSL client", "sslclient", NULL},
84*0Sstevel@tonic-gate 	{X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ssl_server, "SSL server", "sslserver", NULL},
85*0Sstevel@tonic-gate 	{X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
86*0Sstevel@tonic-gate 	{X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, "S/MIME signing", "smimesign", NULL},
87*0Sstevel@tonic-gate 	{X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
88*0Sstevel@tonic-gate 	{X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, "CRL signing", "crlsign", NULL},
89*0Sstevel@tonic-gate 	{X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any", NULL},
90*0Sstevel@tonic-gate 	{X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper, "OCSP helper", "ocsphelper", NULL},
91*0Sstevel@tonic-gate };
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate IMPLEMENT_STACK_OF(X509_PURPOSE)
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate static STACK_OF(X509_PURPOSE) *xptable = NULL;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate static int xp_cmp(const X509_PURPOSE * const *a,
100*0Sstevel@tonic-gate 		const X509_PURPOSE * const *b)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 	return (*a)->purpose - (*b)->purpose;
103*0Sstevel@tonic-gate }
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate /* As much as I'd like to make X509_check_purpose use a "const" X509*
106*0Sstevel@tonic-gate  * I really can't because it does recalculate hashes and do other non-const
107*0Sstevel@tonic-gate  * things. */
108*0Sstevel@tonic-gate int X509_check_purpose(X509 *x, int id, int ca)
109*0Sstevel@tonic-gate {
110*0Sstevel@tonic-gate 	int idx;
111*0Sstevel@tonic-gate 	const X509_PURPOSE *pt;
112*0Sstevel@tonic-gate 	if(!(x->ex_flags & EXFLAG_SET)) {
113*0Sstevel@tonic-gate 		CRYPTO_w_lock(CRYPTO_LOCK_X509);
114*0Sstevel@tonic-gate 		x509v3_cache_extensions(x);
115*0Sstevel@tonic-gate 		CRYPTO_w_unlock(CRYPTO_LOCK_X509);
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 	if(id == -1) return 1;
118*0Sstevel@tonic-gate 	idx = X509_PURPOSE_get_by_id(id);
119*0Sstevel@tonic-gate 	if(idx == -1) return -1;
120*0Sstevel@tonic-gate 	pt = X509_PURPOSE_get0(idx);
121*0Sstevel@tonic-gate 	return pt->check_purpose(pt, x, ca);
122*0Sstevel@tonic-gate }
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate int X509_PURPOSE_set(int *p, int purpose)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	if(X509_PURPOSE_get_by_id(purpose) == -1) {
127*0Sstevel@tonic-gate 		X509V3err(X509V3_F_X509_PURPOSE_SET, X509V3_R_INVALID_PURPOSE);
128*0Sstevel@tonic-gate 		return 0;
129*0Sstevel@tonic-gate 	}
130*0Sstevel@tonic-gate 	*p = purpose;
131*0Sstevel@tonic-gate 	return 1;
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate int X509_PURPOSE_get_count(void)
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate 	if(!xptable) return X509_PURPOSE_COUNT;
137*0Sstevel@tonic-gate 	return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate X509_PURPOSE * X509_PURPOSE_get0(int idx)
141*0Sstevel@tonic-gate {
142*0Sstevel@tonic-gate 	if(idx < 0) return NULL;
143*0Sstevel@tonic-gate 	if(idx < X509_PURPOSE_COUNT) return xstandard + idx;
144*0Sstevel@tonic-gate 	return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate int X509_PURPOSE_get_by_sname(char *sname)
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	int i;
150*0Sstevel@tonic-gate 	X509_PURPOSE *xptmp;
151*0Sstevel@tonic-gate 	for(i = 0; i < X509_PURPOSE_get_count(); i++) {
152*0Sstevel@tonic-gate 		xptmp = X509_PURPOSE_get0(i);
153*0Sstevel@tonic-gate 		if(!strcmp(xptmp->sname, sname)) return i;
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 	return -1;
156*0Sstevel@tonic-gate }
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate int X509_PURPOSE_get_by_id(int purpose)
159*0Sstevel@tonic-gate {
160*0Sstevel@tonic-gate 	X509_PURPOSE tmp;
161*0Sstevel@tonic-gate 	int idx;
162*0Sstevel@tonic-gate 	if((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
163*0Sstevel@tonic-gate 		return purpose - X509_PURPOSE_MIN;
164*0Sstevel@tonic-gate 	tmp.purpose = purpose;
165*0Sstevel@tonic-gate 	if(!xptable) return -1;
166*0Sstevel@tonic-gate 	idx = sk_X509_PURPOSE_find(xptable, &tmp);
167*0Sstevel@tonic-gate 	if(idx == -1) return -1;
168*0Sstevel@tonic-gate 	return idx + X509_PURPOSE_COUNT;
169*0Sstevel@tonic-gate }
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate int X509_PURPOSE_add(int id, int trust, int flags,
172*0Sstevel@tonic-gate 			int (*ck)(const X509_PURPOSE *, const X509 *, int),
173*0Sstevel@tonic-gate 					char *name, char *sname, void *arg)
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	int idx;
176*0Sstevel@tonic-gate 	X509_PURPOSE *ptmp;
177*0Sstevel@tonic-gate 	/* This is set according to what we change: application can't set it */
178*0Sstevel@tonic-gate 	flags &= ~X509_PURPOSE_DYNAMIC;
179*0Sstevel@tonic-gate 	/* This will always be set for application modified trust entries */
180*0Sstevel@tonic-gate 	flags |= X509_PURPOSE_DYNAMIC_NAME;
181*0Sstevel@tonic-gate 	/* Get existing entry if any */
182*0Sstevel@tonic-gate 	idx = X509_PURPOSE_get_by_id(id);
183*0Sstevel@tonic-gate 	/* Need a new entry */
184*0Sstevel@tonic-gate 	if(idx == -1) {
185*0Sstevel@tonic-gate 		if(!(ptmp = OPENSSL_malloc(sizeof(X509_PURPOSE)))) {
186*0Sstevel@tonic-gate 			X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
187*0Sstevel@tonic-gate 			return 0;
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 		ptmp->flags = X509_PURPOSE_DYNAMIC;
190*0Sstevel@tonic-gate 	} else ptmp = X509_PURPOSE_get0(idx);
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	/* OPENSSL_free existing name if dynamic */
193*0Sstevel@tonic-gate 	if(ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
194*0Sstevel@tonic-gate 		OPENSSL_free(ptmp->name);
195*0Sstevel@tonic-gate 		OPENSSL_free(ptmp->sname);
196*0Sstevel@tonic-gate 	}
197*0Sstevel@tonic-gate 	/* dup supplied name */
198*0Sstevel@tonic-gate 	ptmp->name = BUF_strdup(name);
199*0Sstevel@tonic-gate 	ptmp->sname = BUF_strdup(sname);
200*0Sstevel@tonic-gate 	if(!ptmp->name || !ptmp->sname) {
201*0Sstevel@tonic-gate 		X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
202*0Sstevel@tonic-gate 		return 0;
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 	/* Keep the dynamic flag of existing entry */
205*0Sstevel@tonic-gate 	ptmp->flags &= X509_PURPOSE_DYNAMIC;
206*0Sstevel@tonic-gate 	/* Set all other flags */
207*0Sstevel@tonic-gate 	ptmp->flags |= flags;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	ptmp->purpose = id;
210*0Sstevel@tonic-gate 	ptmp->trust = trust;
211*0Sstevel@tonic-gate 	ptmp->check_purpose = ck;
212*0Sstevel@tonic-gate 	ptmp->usr_data = arg;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	/* If its a new entry manage the dynamic table */
215*0Sstevel@tonic-gate 	if(idx == -1) {
216*0Sstevel@tonic-gate 		if(!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) {
217*0Sstevel@tonic-gate 			X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
218*0Sstevel@tonic-gate 			return 0;
219*0Sstevel@tonic-gate 		}
220*0Sstevel@tonic-gate 		if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
221*0Sstevel@tonic-gate 			X509V3err(X509V3_F_X509_PURPOSE_ADD,ERR_R_MALLOC_FAILURE);
222*0Sstevel@tonic-gate 			return 0;
223*0Sstevel@tonic-gate 		}
224*0Sstevel@tonic-gate 	}
225*0Sstevel@tonic-gate 	return 1;
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate static void xptable_free(X509_PURPOSE *p)
229*0Sstevel@tonic-gate 	{
230*0Sstevel@tonic-gate 	if(!p) return;
231*0Sstevel@tonic-gate 	if (p->flags & X509_PURPOSE_DYNAMIC)
232*0Sstevel@tonic-gate 		{
233*0Sstevel@tonic-gate 		if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
234*0Sstevel@tonic-gate 			OPENSSL_free(p->name);
235*0Sstevel@tonic-gate 			OPENSSL_free(p->sname);
236*0Sstevel@tonic-gate 		}
237*0Sstevel@tonic-gate 		OPENSSL_free(p);
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate void X509_PURPOSE_cleanup(void)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 	int i;
244*0Sstevel@tonic-gate 	sk_X509_PURPOSE_pop_free(xptable, xptable_free);
245*0Sstevel@tonic-gate 	for(i = 0; i < X509_PURPOSE_COUNT; i++) xptable_free(xstandard + i);
246*0Sstevel@tonic-gate 	xptable = NULL;
247*0Sstevel@tonic-gate }
248*0Sstevel@tonic-gate 
249*0Sstevel@tonic-gate int X509_PURPOSE_get_id(X509_PURPOSE *xp)
250*0Sstevel@tonic-gate {
251*0Sstevel@tonic-gate 	return xp->purpose;
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate 
254*0Sstevel@tonic-gate char *X509_PURPOSE_get0_name(X509_PURPOSE *xp)
255*0Sstevel@tonic-gate {
256*0Sstevel@tonic-gate 	return xp->name;
257*0Sstevel@tonic-gate }
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate char *X509_PURPOSE_get0_sname(X509_PURPOSE *xp)
260*0Sstevel@tonic-gate {
261*0Sstevel@tonic-gate 	return xp->sname;
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate int X509_PURPOSE_get_trust(X509_PURPOSE *xp)
265*0Sstevel@tonic-gate {
266*0Sstevel@tonic-gate 	return xp->trust;
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate static int nid_cmp(int *a, int *b)
270*0Sstevel@tonic-gate 	{
271*0Sstevel@tonic-gate 	return *a - *b;
272*0Sstevel@tonic-gate 	}
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate int X509_supported_extension(X509_EXTENSION *ex)
275*0Sstevel@tonic-gate 	{
276*0Sstevel@tonic-gate 	/* This table is a list of the NIDs of supported extensions:
277*0Sstevel@tonic-gate 	 * that is those which are used by the verify process. If
278*0Sstevel@tonic-gate 	 * an extension is critical and doesn't appear in this list
279*0Sstevel@tonic-gate 	 * then the verify process will normally reject the certificate.
280*0Sstevel@tonic-gate 	 * The list must be kept in numerical order because it will be
281*0Sstevel@tonic-gate 	 * searched using bsearch.
282*0Sstevel@tonic-gate 	 */
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	static int supported_nids[] = {
285*0Sstevel@tonic-gate 		NID_netscape_cert_type, /* 71 */
286*0Sstevel@tonic-gate         	NID_key_usage,		/* 83 */
287*0Sstevel@tonic-gate 		NID_subject_alt_name,	/* 85 */
288*0Sstevel@tonic-gate 		NID_basic_constraints,	/* 87 */
289*0Sstevel@tonic-gate         	NID_ext_key_usage	/* 126 */
290*0Sstevel@tonic-gate 	};
291*0Sstevel@tonic-gate 
292*0Sstevel@tonic-gate 	int ex_nid;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate 	if (ex_nid == NID_undef)
297*0Sstevel@tonic-gate 		return 0;
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 	if (OBJ_bsearch((char *)&ex_nid, (char *)supported_nids,
300*0Sstevel@tonic-gate 		sizeof(supported_nids)/sizeof(int), sizeof(int),
301*0Sstevel@tonic-gate 		(int (*)(const void *, const void *))nid_cmp))
302*0Sstevel@tonic-gate 		return 1;
303*0Sstevel@tonic-gate 	return 0;
304*0Sstevel@tonic-gate 	}
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate static void x509v3_cache_extensions(X509 *x)
308*0Sstevel@tonic-gate {
309*0Sstevel@tonic-gate 	BASIC_CONSTRAINTS *bs;
310*0Sstevel@tonic-gate 	ASN1_BIT_STRING *usage;
311*0Sstevel@tonic-gate 	ASN1_BIT_STRING *ns;
312*0Sstevel@tonic-gate 	EXTENDED_KEY_USAGE *extusage;
313*0Sstevel@tonic-gate 	X509_EXTENSION *ex;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	int i;
316*0Sstevel@tonic-gate 	if(x->ex_flags & EXFLAG_SET) return;
317*0Sstevel@tonic-gate #ifndef OPENSSL_NO_SHA
318*0Sstevel@tonic-gate 	X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
319*0Sstevel@tonic-gate #endif
320*0Sstevel@tonic-gate 	/* Does subject name match issuer ? */
321*0Sstevel@tonic-gate 	if(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)))
322*0Sstevel@tonic-gate 			 x->ex_flags |= EXFLAG_SS;
323*0Sstevel@tonic-gate 	/* V1 should mean no extensions ... */
324*0Sstevel@tonic-gate 	if(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1;
325*0Sstevel@tonic-gate 	/* Handle basic constraints */
326*0Sstevel@tonic-gate 	if((bs=X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
327*0Sstevel@tonic-gate 		if(bs->ca) x->ex_flags |= EXFLAG_CA;
328*0Sstevel@tonic-gate 		if(bs->pathlen) {
329*0Sstevel@tonic-gate 			if((bs->pathlen->type == V_ASN1_NEG_INTEGER)
330*0Sstevel@tonic-gate 						|| !bs->ca) {
331*0Sstevel@tonic-gate 				x->ex_flags |= EXFLAG_INVALID;
332*0Sstevel@tonic-gate 				x->ex_pathlen = 0;
333*0Sstevel@tonic-gate 			} else x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
334*0Sstevel@tonic-gate 		} else x->ex_pathlen = -1;
335*0Sstevel@tonic-gate 		BASIC_CONSTRAINTS_free(bs);
336*0Sstevel@tonic-gate 		x->ex_flags |= EXFLAG_BCONS;
337*0Sstevel@tonic-gate 	}
338*0Sstevel@tonic-gate 	/* Handle key usage */
339*0Sstevel@tonic-gate 	if((usage=X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
340*0Sstevel@tonic-gate 		if(usage->length > 0) {
341*0Sstevel@tonic-gate 			x->ex_kusage = usage->data[0];
342*0Sstevel@tonic-gate 			if(usage->length > 1)
343*0Sstevel@tonic-gate 				x->ex_kusage |= usage->data[1] << 8;
344*0Sstevel@tonic-gate 		} else x->ex_kusage = 0;
345*0Sstevel@tonic-gate 		x->ex_flags |= EXFLAG_KUSAGE;
346*0Sstevel@tonic-gate 		ASN1_BIT_STRING_free(usage);
347*0Sstevel@tonic-gate 	}
348*0Sstevel@tonic-gate 	x->ex_xkusage = 0;
349*0Sstevel@tonic-gate 	if((extusage=X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {
350*0Sstevel@tonic-gate 		x->ex_flags |= EXFLAG_XKUSAGE;
351*0Sstevel@tonic-gate 		for(i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
352*0Sstevel@tonic-gate 			switch(OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage,i))) {
353*0Sstevel@tonic-gate 				case NID_server_auth:
354*0Sstevel@tonic-gate 				x->ex_xkusage |= XKU_SSL_SERVER;
355*0Sstevel@tonic-gate 				break;
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 				case NID_client_auth:
358*0Sstevel@tonic-gate 				x->ex_xkusage |= XKU_SSL_CLIENT;
359*0Sstevel@tonic-gate 				break;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 				case NID_email_protect:
362*0Sstevel@tonic-gate 				x->ex_xkusage |= XKU_SMIME;
363*0Sstevel@tonic-gate 				break;
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 				case NID_code_sign:
366*0Sstevel@tonic-gate 				x->ex_xkusage |= XKU_CODE_SIGN;
367*0Sstevel@tonic-gate 				break;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 				case NID_ms_sgc:
370*0Sstevel@tonic-gate 				case NID_ns_sgc:
371*0Sstevel@tonic-gate 				x->ex_xkusage |= XKU_SGC;
372*0Sstevel@tonic-gate 				break;
373*0Sstevel@tonic-gate 
374*0Sstevel@tonic-gate 				case NID_OCSP_sign:
375*0Sstevel@tonic-gate 				x->ex_xkusage |= XKU_OCSP_SIGN;
376*0Sstevel@tonic-gate 				break;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 				case NID_time_stamp:
379*0Sstevel@tonic-gate 				x->ex_xkusage |= XKU_TIMESTAMP;
380*0Sstevel@tonic-gate 				break;
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 				case NID_dvcs:
383*0Sstevel@tonic-gate 				x->ex_xkusage |= XKU_DVCS;
384*0Sstevel@tonic-gate 				break;
385*0Sstevel@tonic-gate 			}
386*0Sstevel@tonic-gate 		}
387*0Sstevel@tonic-gate 		sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
388*0Sstevel@tonic-gate 	}
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	if((ns=X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {
391*0Sstevel@tonic-gate 		if(ns->length > 0) x->ex_nscert = ns->data[0];
392*0Sstevel@tonic-gate 		else x->ex_nscert = 0;
393*0Sstevel@tonic-gate 		x->ex_flags |= EXFLAG_NSCERT;
394*0Sstevel@tonic-gate 		ASN1_BIT_STRING_free(ns);
395*0Sstevel@tonic-gate 	}
396*0Sstevel@tonic-gate 	x->skid =X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);
397*0Sstevel@tonic-gate 	x->akid =X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);
398*0Sstevel@tonic-gate 	for (i = 0; i < X509_get_ext_count(x); i++)
399*0Sstevel@tonic-gate 		{
400*0Sstevel@tonic-gate 		ex = X509_get_ext(x, i);
401*0Sstevel@tonic-gate 		if (!X509_EXTENSION_get_critical(ex))
402*0Sstevel@tonic-gate 			continue;
403*0Sstevel@tonic-gate 		if (!X509_supported_extension(ex))
404*0Sstevel@tonic-gate 			{
405*0Sstevel@tonic-gate 			x->ex_flags |= EXFLAG_CRITICAL;
406*0Sstevel@tonic-gate 			break;
407*0Sstevel@tonic-gate 			}
408*0Sstevel@tonic-gate 		}
409*0Sstevel@tonic-gate 	x->ex_flags |= EXFLAG_SET;
410*0Sstevel@tonic-gate }
411*0Sstevel@tonic-gate 
412*0Sstevel@tonic-gate /* CA checks common to all purposes
413*0Sstevel@tonic-gate  * return codes:
414*0Sstevel@tonic-gate  * 0 not a CA
415*0Sstevel@tonic-gate  * 1 is a CA
416*0Sstevel@tonic-gate  * 2 basicConstraints absent so "maybe" a CA
417*0Sstevel@tonic-gate  * 3 basicConstraints absent but self signed V1.
418*0Sstevel@tonic-gate  * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
419*0Sstevel@tonic-gate  */
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
422*0Sstevel@tonic-gate #define ku_reject(x, usage) \
423*0Sstevel@tonic-gate 	(((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
424*0Sstevel@tonic-gate #define xku_reject(x, usage) \
425*0Sstevel@tonic-gate 	(((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
426*0Sstevel@tonic-gate #define ns_reject(x, usage) \
427*0Sstevel@tonic-gate 	(((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
428*0Sstevel@tonic-gate 
429*0Sstevel@tonic-gate static int ca_check(const X509 *x)
430*0Sstevel@tonic-gate {
431*0Sstevel@tonic-gate 	/* keyUsage if present should allow cert signing */
432*0Sstevel@tonic-gate 	if(ku_reject(x, KU_KEY_CERT_SIGN)) return 0;
433*0Sstevel@tonic-gate 	if(x->ex_flags & EXFLAG_BCONS) {
434*0Sstevel@tonic-gate 		if(x->ex_flags & EXFLAG_CA) return 1;
435*0Sstevel@tonic-gate 		/* If basicConstraints says not a CA then say so */
436*0Sstevel@tonic-gate 		else return 0;
437*0Sstevel@tonic-gate 	} else {
438*0Sstevel@tonic-gate 		if((x->ex_flags & V1_ROOT) == V1_ROOT) return 3;
439*0Sstevel@tonic-gate 		/* If key usage present it must have certSign so tolerate it */
440*0Sstevel@tonic-gate 		else if (x->ex_flags & EXFLAG_KUSAGE) return 4;
441*0Sstevel@tonic-gate 		else return 2;
442*0Sstevel@tonic-gate 	}
443*0Sstevel@tonic-gate }
444*0Sstevel@tonic-gate 
445*0Sstevel@tonic-gate /* Check SSL CA: common checks for SSL client and server */
446*0Sstevel@tonic-gate static int check_ssl_ca(const X509 *x)
447*0Sstevel@tonic-gate {
448*0Sstevel@tonic-gate 	int ca_ret;
449*0Sstevel@tonic-gate 	ca_ret = ca_check(x);
450*0Sstevel@tonic-gate 	if(!ca_ret) return 0;
451*0Sstevel@tonic-gate 	/* check nsCertType if present */
452*0Sstevel@tonic-gate 	if(x->ex_flags & EXFLAG_NSCERT) {
453*0Sstevel@tonic-gate 		if(x->ex_nscert & NS_SSL_CA) return ca_ret;
454*0Sstevel@tonic-gate 		return 0;
455*0Sstevel@tonic-gate 	}
456*0Sstevel@tonic-gate 	if(ca_ret != 2) return ca_ret;
457*0Sstevel@tonic-gate 	else return 0;
458*0Sstevel@tonic-gate }
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca)
462*0Sstevel@tonic-gate {
463*0Sstevel@tonic-gate 	if(xku_reject(x,XKU_SSL_CLIENT)) return 0;
464*0Sstevel@tonic-gate 	if(ca) return check_ssl_ca(x);
465*0Sstevel@tonic-gate 	/* We need to do digital signatures with it */
466*0Sstevel@tonic-gate 	if(ku_reject(x,KU_DIGITAL_SIGNATURE)) return 0;
467*0Sstevel@tonic-gate 	/* nsCertType if present should allow SSL client use */
468*0Sstevel@tonic-gate 	if(ns_reject(x, NS_SSL_CLIENT)) return 0;
469*0Sstevel@tonic-gate 	return 1;
470*0Sstevel@tonic-gate }
471*0Sstevel@tonic-gate 
472*0Sstevel@tonic-gate static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
473*0Sstevel@tonic-gate {
474*0Sstevel@tonic-gate 	if(xku_reject(x,XKU_SSL_SERVER|XKU_SGC)) return 0;
475*0Sstevel@tonic-gate 	if(ca) return check_ssl_ca(x);
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 	if(ns_reject(x, NS_SSL_SERVER)) return 0;
478*0Sstevel@tonic-gate 	/* Now as for keyUsage: we'll at least need to sign OR encipher */
479*0Sstevel@tonic-gate 	if(ku_reject(x, KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT)) return 0;
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate 	return 1;
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate }
484*0Sstevel@tonic-gate 
485*0Sstevel@tonic-gate static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
486*0Sstevel@tonic-gate {
487*0Sstevel@tonic-gate 	int ret;
488*0Sstevel@tonic-gate 	ret = check_purpose_ssl_server(xp, x, ca);
489*0Sstevel@tonic-gate 	if(!ret || ca) return ret;
490*0Sstevel@tonic-gate 	/* We need to encipher or Netscape complains */
491*0Sstevel@tonic-gate 	if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
492*0Sstevel@tonic-gate 	return ret;
493*0Sstevel@tonic-gate }
494*0Sstevel@tonic-gate 
495*0Sstevel@tonic-gate /* common S/MIME checks */
496*0Sstevel@tonic-gate static int purpose_smime(const X509 *x, int ca)
497*0Sstevel@tonic-gate {
498*0Sstevel@tonic-gate 	if(xku_reject(x,XKU_SMIME)) return 0;
499*0Sstevel@tonic-gate 	if(ca) {
500*0Sstevel@tonic-gate 		int ca_ret;
501*0Sstevel@tonic-gate 		ca_ret = ca_check(x);
502*0Sstevel@tonic-gate 		if(!ca_ret) return 0;
503*0Sstevel@tonic-gate 		/* check nsCertType if present */
504*0Sstevel@tonic-gate 		if(x->ex_flags & EXFLAG_NSCERT) {
505*0Sstevel@tonic-gate 			if(x->ex_nscert & NS_SMIME_CA) return ca_ret;
506*0Sstevel@tonic-gate 			return 0;
507*0Sstevel@tonic-gate 		}
508*0Sstevel@tonic-gate 		if(ca_ret != 2) return ca_ret;
509*0Sstevel@tonic-gate 		else return 0;
510*0Sstevel@tonic-gate 	}
511*0Sstevel@tonic-gate 	if(x->ex_flags & EXFLAG_NSCERT) {
512*0Sstevel@tonic-gate 		if(x->ex_nscert & NS_SMIME) return 1;
513*0Sstevel@tonic-gate 		/* Workaround for some buggy certificates */
514*0Sstevel@tonic-gate 		if(x->ex_nscert & NS_SSL_CLIENT) return 2;
515*0Sstevel@tonic-gate 		return 0;
516*0Sstevel@tonic-gate 	}
517*0Sstevel@tonic-gate 	return 1;
518*0Sstevel@tonic-gate }
519*0Sstevel@tonic-gate 
520*0Sstevel@tonic-gate static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
521*0Sstevel@tonic-gate {
522*0Sstevel@tonic-gate 	int ret;
523*0Sstevel@tonic-gate 	ret = purpose_smime(x, ca);
524*0Sstevel@tonic-gate 	if(!ret || ca) return ret;
525*0Sstevel@tonic-gate 	if(ku_reject(x, KU_DIGITAL_SIGNATURE|KU_NON_REPUDIATION)) return 0;
526*0Sstevel@tonic-gate 	return ret;
527*0Sstevel@tonic-gate }
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca)
530*0Sstevel@tonic-gate {
531*0Sstevel@tonic-gate 	int ret;
532*0Sstevel@tonic-gate 	ret = purpose_smime(x, ca);
533*0Sstevel@tonic-gate 	if(!ret || ca) return ret;
534*0Sstevel@tonic-gate 	if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
535*0Sstevel@tonic-gate 	return ret;
536*0Sstevel@tonic-gate }
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
539*0Sstevel@tonic-gate {
540*0Sstevel@tonic-gate 	if(ca) {
541*0Sstevel@tonic-gate 		int ca_ret;
542*0Sstevel@tonic-gate 		if((ca_ret = ca_check(x)) != 2) return ca_ret;
543*0Sstevel@tonic-gate 		else return 0;
544*0Sstevel@tonic-gate 	}
545*0Sstevel@tonic-gate 	if(ku_reject(x, KU_CRL_SIGN)) return 0;
546*0Sstevel@tonic-gate 	return 1;
547*0Sstevel@tonic-gate }
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate /* OCSP helper: this is *not* a full OCSP check. It just checks that
550*0Sstevel@tonic-gate  * each CA is valid. Additional checks must be made on the chain.
551*0Sstevel@tonic-gate  */
552*0Sstevel@tonic-gate 
553*0Sstevel@tonic-gate static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
554*0Sstevel@tonic-gate {
555*0Sstevel@tonic-gate 	/* Must be a valid CA */
556*0Sstevel@tonic-gate 	if(ca) {
557*0Sstevel@tonic-gate 		int ca_ret;
558*0Sstevel@tonic-gate 		ca_ret = ca_check(x);
559*0Sstevel@tonic-gate 		if(ca_ret != 2) return ca_ret;
560*0Sstevel@tonic-gate 		if(x->ex_flags & EXFLAG_NSCERT) {
561*0Sstevel@tonic-gate 			if(x->ex_nscert & NS_ANY_CA) return ca_ret;
562*0Sstevel@tonic-gate 			return 0;
563*0Sstevel@tonic-gate 		}
564*0Sstevel@tonic-gate 		return 0;
565*0Sstevel@tonic-gate 	}
566*0Sstevel@tonic-gate 	/* leaf certificate is checked in OCSP_verify() */
567*0Sstevel@tonic-gate 	return 1;
568*0Sstevel@tonic-gate }
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
571*0Sstevel@tonic-gate {
572*0Sstevel@tonic-gate 	return 1;
573*0Sstevel@tonic-gate }
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate /* Various checks to see if one certificate issued the second.
576*0Sstevel@tonic-gate  * This can be used to prune a set of possible issuer certificates
577*0Sstevel@tonic-gate  * which have been looked up using some simple method such as by
578*0Sstevel@tonic-gate  * subject name.
579*0Sstevel@tonic-gate  * These are:
580*0Sstevel@tonic-gate  * 1. Check issuer_name(subject) == subject_name(issuer)
581*0Sstevel@tonic-gate  * 2. If akid(subject) exists check it matches issuer
582*0Sstevel@tonic-gate  * 3. If key_usage(issuer) exists check it supports certificate signing
583*0Sstevel@tonic-gate  * returns 0 for OK, positive for reason for mismatch, reasons match
584*0Sstevel@tonic-gate  * codes for X509_verify_cert()
585*0Sstevel@tonic-gate  */
586*0Sstevel@tonic-gate 
587*0Sstevel@tonic-gate int X509_check_issued(X509 *issuer, X509 *subject)
588*0Sstevel@tonic-gate {
589*0Sstevel@tonic-gate 	if(X509_NAME_cmp(X509_get_subject_name(issuer),
590*0Sstevel@tonic-gate 			X509_get_issuer_name(subject)))
591*0Sstevel@tonic-gate 				return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
592*0Sstevel@tonic-gate 	x509v3_cache_extensions(issuer);
593*0Sstevel@tonic-gate 	x509v3_cache_extensions(subject);
594*0Sstevel@tonic-gate 	if(subject->akid) {
595*0Sstevel@tonic-gate 		/* Check key ids (if present) */
596*0Sstevel@tonic-gate 		if(subject->akid->keyid && issuer->skid &&
597*0Sstevel@tonic-gate 		 ASN1_OCTET_STRING_cmp(subject->akid->keyid, issuer->skid) )
598*0Sstevel@tonic-gate 				return X509_V_ERR_AKID_SKID_MISMATCH;
599*0Sstevel@tonic-gate 		/* Check serial number */
600*0Sstevel@tonic-gate 		if(subject->akid->serial &&
601*0Sstevel@tonic-gate 			ASN1_INTEGER_cmp(X509_get_serialNumber(issuer),
602*0Sstevel@tonic-gate 						subject->akid->serial))
603*0Sstevel@tonic-gate 				return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
604*0Sstevel@tonic-gate 		/* Check issuer name */
605*0Sstevel@tonic-gate 		if(subject->akid->issuer) {
606*0Sstevel@tonic-gate 			/* Ugh, for some peculiar reason AKID includes
607*0Sstevel@tonic-gate 			 * SEQUENCE OF GeneralName. So look for a DirName.
608*0Sstevel@tonic-gate 			 * There may be more than one but we only take any
609*0Sstevel@tonic-gate 			 * notice of the first.
610*0Sstevel@tonic-gate 			 */
611*0Sstevel@tonic-gate 			GENERAL_NAMES *gens;
612*0Sstevel@tonic-gate 			GENERAL_NAME *gen;
613*0Sstevel@tonic-gate 			X509_NAME *nm = NULL;
614*0Sstevel@tonic-gate 			int i;
615*0Sstevel@tonic-gate 			gens = subject->akid->issuer;
616*0Sstevel@tonic-gate 			for(i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
617*0Sstevel@tonic-gate 				gen = sk_GENERAL_NAME_value(gens, i);
618*0Sstevel@tonic-gate 				if(gen->type == GEN_DIRNAME) {
619*0Sstevel@tonic-gate 					nm = gen->d.dirn;
620*0Sstevel@tonic-gate 					break;
621*0Sstevel@tonic-gate 				}
622*0Sstevel@tonic-gate 			}
623*0Sstevel@tonic-gate 			if(nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
624*0Sstevel@tonic-gate 				return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
625*0Sstevel@tonic-gate 		}
626*0Sstevel@tonic-gate 	}
627*0Sstevel@tonic-gate 	if(ku_reject(issuer, KU_KEY_CERT_SIGN)) return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
628*0Sstevel@tonic-gate 	return X509_V_OK;
629*0Sstevel@tonic-gate }
630*0Sstevel@tonic-gate 
631