1 /* v3_purp.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2004 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com). */
56 
57 #include <stdio.h>
58 
59 #include <string.h>
60 
61 #include <openssl/buf.h>
62 #include <openssl/err.h>
63 #include <openssl/digest.h>
64 #include <openssl/mem.h>
65 #include <openssl/obj.h>
66 #include <openssl/thread.h>
67 #include <openssl/x509_vfy.h>
68 #include <openssl/x509v3.h>
69 
70 #include "../internal.h"
71 
72 
73 static void x509v3_cache_extensions(X509 *x);
74 
75 static int check_ssl_ca(const X509 *x);
76 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca);
77 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
78 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca);
79 static int purpose_smime(const X509 *x, int ca);
80 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
81 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca);
82 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
83 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, int ca);
84 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
85 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
86 
87 static int xp_cmp(const X509_PURPOSE **a, const X509_PURPOSE **b);
88 static void xptable_free(X509_PURPOSE *p);
89 
90 static X509_PURPOSE xstandard[] = {
91 	{X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, check_purpose_ssl_client, (char *) "SSL client", (char *) "sslclient", NULL},
92 	{X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ssl_server, (char *) "SSL server", (char *) "sslserver", NULL},
93 	{X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ns_ssl_server, (char *) "Netscape SSL server", (char *) "nssslserver", NULL},
94 	{X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, (char *) "S/MIME signing", (char *) "smimesign", NULL},
95 	{X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, check_purpose_smime_encrypt, (char *) "S/MIME encryption", (char *) "smimeencrypt", NULL},
96 	{X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, (char *) "CRL signing", (char *) "crlsign", NULL},
97 	{X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, (char *) "Any Purpose", (char *) "any", NULL},
98 	{X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper, (char *) "OCSP helper", (char *) "ocsphelper", NULL},
99 	{X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, check_purpose_timestamp_sign, (char *) "Time Stamp signing", (char *) "timestampsign", NULL},
100 };
101 
102 #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
103 
104 static STACK_OF(X509_PURPOSE) *xptable = NULL;
105 
xp_cmp(const X509_PURPOSE ** a,const X509_PURPOSE ** b)106 static int xp_cmp(const X509_PURPOSE **a, const X509_PURPOSE **b)
107 {
108 	return (*a)->purpose - (*b)->purpose;
109 }
110 
111 /* As much as I'd like to make X509_check_purpose use a "const" X509*
112  * I really can't because it does recalculate hashes and do other non-const
113  * things. */
X509_check_purpose(X509 * x,int id,int ca)114 int X509_check_purpose(X509 *x, int id, int ca)
115 {
116 	int idx;
117 	const X509_PURPOSE *pt;
118 	if(!(x->ex_flags & EXFLAG_SET)) {
119 		x509v3_cache_extensions(x);
120 	}
121 	if(id == -1) return 1;
122 	idx = X509_PURPOSE_get_by_id(id);
123 	if(idx == -1) return -1;
124 	pt = X509_PURPOSE_get0(idx);
125 	return pt->check_purpose(pt, x, ca);
126 }
127 
X509_PURPOSE_set(int * p,int purpose)128 int X509_PURPOSE_set(int *p, int purpose)
129 {
130 	if(X509_PURPOSE_get_by_id(purpose) == -1) {
131 		OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_set, X509V3_R_INVALID_PURPOSE);
132 		return 0;
133 	}
134 	*p = purpose;
135 	return 1;
136 }
137 
X509_PURPOSE_get_count(void)138 int X509_PURPOSE_get_count(void)
139 {
140 	if(!xptable) return X509_PURPOSE_COUNT;
141 	return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
142 }
143 
X509_PURPOSE_get0(int idx)144 X509_PURPOSE * X509_PURPOSE_get0(int idx)
145 {
146 	if(idx < 0) return NULL;
147 	if(idx < (int)X509_PURPOSE_COUNT) return xstandard + idx;
148 	return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
149 }
150 
X509_PURPOSE_get_by_sname(char * sname)151 int X509_PURPOSE_get_by_sname(char *sname)
152 {
153 	int i;
154 	X509_PURPOSE *xptmp;
155 	for(i = 0; i < X509_PURPOSE_get_count(); i++) {
156 		xptmp = X509_PURPOSE_get0(i);
157 		if(!strcmp(xptmp->sname, sname)) return i;
158 	}
159 	return -1;
160 }
161 
X509_PURPOSE_get_by_id(int purpose)162 int X509_PURPOSE_get_by_id(int purpose)
163 {
164 	X509_PURPOSE tmp;
165 	size_t idx;
166 
167 	if((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
168 		return purpose - X509_PURPOSE_MIN;
169 	tmp.purpose = purpose;
170 	if(!xptable) return -1;
171 
172 	if (!sk_X509_PURPOSE_find(xptable, &idx, &tmp))
173 		return -1;
174 	return idx + X509_PURPOSE_COUNT;
175 }
176 
X509_PURPOSE_add(int id,int trust,int flags,int (* ck)(const X509_PURPOSE *,const X509 *,int),char * name,char * sname,void * arg)177 int X509_PURPOSE_add(int id, int trust, int flags,
178 			int (*ck)(const X509_PURPOSE *, const X509 *, int),
179 					char *name, char *sname, void *arg)
180 {
181 	int idx;
182 	X509_PURPOSE *ptmp;
183 	char *name_dup, *sname_dup;
184 
185 	/* This is set according to what we change: application can't set it */
186 	flags &= ~X509_PURPOSE_DYNAMIC;
187 	/* This will always be set for application modified trust entries */
188 	flags |= X509_PURPOSE_DYNAMIC_NAME;
189 	/* Get existing entry if any */
190 	idx = X509_PURPOSE_get_by_id(id);
191 	/* Need a new entry */
192 	if(idx == -1) {
193 		if(!(ptmp = OPENSSL_malloc(sizeof(X509_PURPOSE)))) {
194 			OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_add, ERR_R_MALLOC_FAILURE);
195 			return 0;
196 		}
197 		ptmp->flags = X509_PURPOSE_DYNAMIC;
198 	} else ptmp = X509_PURPOSE_get0(idx);
199 
200 	/* Duplicate the supplied names. */
201 	name_dup = BUF_strdup(name);
202 	sname_dup = BUF_strdup(sname);
203 	if (name_dup == NULL || sname_dup == NULL) {
204 		OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_add, ERR_R_MALLOC_FAILURE);
205 		if (name_dup != NULL)
206 			OPENSSL_free(name_dup);
207 		if (sname_dup != NULL)
208 			OPENSSL_free(sname_dup);
209 		if (idx == -1)
210 			OPENSSL_free(ptmp);
211 		return 0;
212 	}
213 
214 	/* OPENSSL_free existing name if dynamic */
215 	if(ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
216 		OPENSSL_free(ptmp->name);
217 		OPENSSL_free(ptmp->sname);
218 	}
219 	/* dup supplied name */
220 	ptmp->name = name_dup;
221 	ptmp->sname = sname_dup;
222 	/* Keep the dynamic flag of existing entry */
223 	ptmp->flags &= X509_PURPOSE_DYNAMIC;
224 	/* Set all other flags */
225 	ptmp->flags |= flags;
226 
227 	ptmp->purpose = id;
228 	ptmp->trust = trust;
229 	ptmp->check_purpose = ck;
230 	ptmp->usr_data = arg;
231 
232 	/* If its a new entry manage the dynamic table */
233 	if(idx == -1) {
234 		if(!xptable && !(xptable = sk_X509_PURPOSE_new(xp_cmp))) {
235 			OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_add, ERR_R_MALLOC_FAILURE);
236 			xptable_free(ptmp);
237 			return 0;
238 		}
239 		if (!sk_X509_PURPOSE_push(xptable, ptmp)) {
240 			OPENSSL_PUT_ERROR(X509V3, X509_PURPOSE_add, ERR_R_MALLOC_FAILURE);
241 			xptable_free(ptmp);
242 			return 0;
243 		}
244 	}
245 	return 1;
246 }
247 
xptable_free(X509_PURPOSE * p)248 static void xptable_free(X509_PURPOSE *p)
249 	{
250 	if(!p) return;
251 	if (p->flags & X509_PURPOSE_DYNAMIC)
252 		{
253 		if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
254 			OPENSSL_free(p->name);
255 			OPENSSL_free(p->sname);
256 		}
257 		OPENSSL_free(p);
258 		}
259 	}
260 
X509_PURPOSE_cleanup(void)261 void X509_PURPOSE_cleanup(void)
262 {
263 	unsigned int i;
264 	sk_X509_PURPOSE_pop_free(xptable, xptable_free);
265 	for(i = 0; i < X509_PURPOSE_COUNT; i++) xptable_free(xstandard + i);
266 	xptable = NULL;
267 }
268 
X509_PURPOSE_get_id(X509_PURPOSE * xp)269 int X509_PURPOSE_get_id(X509_PURPOSE *xp)
270 {
271 	return xp->purpose;
272 }
273 
X509_PURPOSE_get0_name(X509_PURPOSE * xp)274 char *X509_PURPOSE_get0_name(X509_PURPOSE *xp)
275 {
276 	return xp->name;
277 }
278 
X509_PURPOSE_get0_sname(X509_PURPOSE * xp)279 char *X509_PURPOSE_get0_sname(X509_PURPOSE *xp)
280 {
281 	return xp->sname;
282 }
283 
X509_PURPOSE_get_trust(X509_PURPOSE * xp)284 int X509_PURPOSE_get_trust(X509_PURPOSE *xp)
285 {
286 	return xp->trust;
287 }
288 
nid_cmp(const void * void_a,const void * void_b)289 static int nid_cmp(const void *void_a, const void *void_b)
290 	{
291 	const int *a = void_a, *b = void_b;
292 
293 	return *a - *b;
294 	}
295 
X509_supported_extension(X509_EXTENSION * ex)296 int X509_supported_extension(X509_EXTENSION *ex)
297 	{
298 	/* This table is a list of the NIDs of supported extensions:
299 	 * that is those which are used by the verify process. If
300 	 * an extension is critical and doesn't appear in this list
301 	 * then the verify process will normally reject the certificate.
302 	 * The list must be kept in numerical order because it will be
303 	 * searched using bsearch.
304 	 */
305 
306 	static const int supported_nids[] = {
307 		NID_netscape_cert_type, /* 71 */
308         	NID_key_usage,		/* 83 */
309 		NID_subject_alt_name,	/* 85 */
310 		NID_basic_constraints,	/* 87 */
311 		NID_certificate_policies, /* 89 */
312         	NID_ext_key_usage,	/* 126 */
313 		NID_policy_constraints,	/* 401 */
314 		NID_proxyCertInfo,	/* 663 */
315 		NID_name_constraints,	/* 666 */
316 		NID_policy_mappings,	/* 747 */
317 		NID_inhibit_any_policy	/* 748 */
318 	};
319 
320 	int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
321 
322 	if (ex_nid == NID_undef)
323 		return 0;
324 
325 	if (bsearch(&ex_nid, supported_nids, sizeof(supported_nids)/sizeof(int), sizeof(int), nid_cmp) != NULL)
326 		return 1;
327 	return 0;
328 	}
329 
setup_dp(X509 * x,DIST_POINT * dp)330 static void setup_dp(X509 *x, DIST_POINT *dp)
331 	{
332 	X509_NAME *iname = NULL;
333 	size_t i;
334 	if (dp->reasons)
335 		{
336 		if (dp->reasons->length > 0)
337 			dp->dp_reasons = dp->reasons->data[0];
338 		if (dp->reasons->length > 1)
339 			dp->dp_reasons |= (dp->reasons->data[1] << 8);
340 		dp->dp_reasons &= CRLDP_ALL_REASONS;
341 		}
342 	else
343 		dp->dp_reasons = CRLDP_ALL_REASONS;
344 	if (!dp->distpoint || (dp->distpoint->type != 1))
345 		return;
346 	for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++)
347 		{
348 		GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
349 		if (gen->type == GEN_DIRNAME)
350 			{
351 			iname = gen->d.directoryName;
352 			break;
353 			}
354 		}
355 	if (!iname)
356 		iname = X509_get_issuer_name(x);
357 
358 	DIST_POINT_set_dpname(dp->distpoint, iname);
359 
360 	}
361 
setup_crldp(X509 * x)362 static void setup_crldp(X509 *x)
363 	{
364 	size_t i;
365 	x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, NULL, NULL);
366 	for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
367 		setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
368 	}
369 
370 /* g_x509_cache_extensions_lock is used to protect against concurrent calls to
371  * |x509v3_cache_extensions|. Ideally this would be done with a |CRYPTO_once_t|
372  * in the |X509| structure, but |CRYPTO_once_t| isn't public.
373  *
374  * Note: it's not entirely clear whether this lock is needed. Not all paths to
375  * this function took a lock in OpenSSL. */
376 static struct CRYPTO_STATIC_MUTEX g_x509_cache_extensions_lock =
377     CRYPTO_STATIC_MUTEX_INIT;
378 
x509v3_cache_extensions(X509 * x)379 static void x509v3_cache_extensions(X509 *x)
380 {
381 	BASIC_CONSTRAINTS *bs;
382 	PROXY_CERT_INFO_EXTENSION *pci;
383 	ASN1_BIT_STRING *usage;
384 	ASN1_BIT_STRING *ns;
385 	EXTENDED_KEY_USAGE *extusage;
386 	X509_EXTENSION *ex;
387 	size_t i;
388 	int j;
389 
390 	CRYPTO_STATIC_MUTEX_lock_write(&g_x509_cache_extensions_lock);
391 
392 	if(x->ex_flags & EXFLAG_SET)
393 		{
394 		CRYPTO_STATIC_MUTEX_unlock(&g_x509_cache_extensions_lock);
395 		return;
396 		}
397 
398 	X509_digest(x, EVP_sha1(), x->sha1_hash, NULL);
399 	/* V1 should mean no extensions ... */
400 	if(!X509_get_version(x)) x->ex_flags |= EXFLAG_V1;
401 	/* Handle basic constraints */
402 	if((bs=X509_get_ext_d2i(x, NID_basic_constraints, NULL, NULL))) {
403 		if(bs->ca) x->ex_flags |= EXFLAG_CA;
404 		if(bs->pathlen) {
405 			if((bs->pathlen->type == V_ASN1_NEG_INTEGER)
406 						|| !bs->ca) {
407 				x->ex_flags |= EXFLAG_INVALID;
408 				x->ex_pathlen = 0;
409 			} else x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
410 		} else x->ex_pathlen = -1;
411 		BASIC_CONSTRAINTS_free(bs);
412 		x->ex_flags |= EXFLAG_BCONS;
413 	}
414 	/* Handle proxy certificates */
415 	if((pci=X509_get_ext_d2i(x, NID_proxyCertInfo, NULL, NULL))) {
416 		if (x->ex_flags & EXFLAG_CA
417 		    || X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0
418 		    || X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
419 			x->ex_flags |= EXFLAG_INVALID;
420 		}
421 		if (pci->pcPathLengthConstraint) {
422 			x->ex_pcpathlen =
423 				ASN1_INTEGER_get(pci->pcPathLengthConstraint);
424 		} else x->ex_pcpathlen = -1;
425 		PROXY_CERT_INFO_EXTENSION_free(pci);
426 		x->ex_flags |= EXFLAG_PROXY;
427 	}
428 	/* Handle key usage */
429 	if((usage=X509_get_ext_d2i(x, NID_key_usage, NULL, NULL))) {
430 		if(usage->length > 0) {
431 			x->ex_kusage = usage->data[0];
432 			if(usage->length > 1)
433 				x->ex_kusage |= usage->data[1] << 8;
434 		} else x->ex_kusage = 0;
435 		x->ex_flags |= EXFLAG_KUSAGE;
436 		ASN1_BIT_STRING_free(usage);
437 	}
438 	x->ex_xkusage = 0;
439 	if((extusage=X509_get_ext_d2i(x, NID_ext_key_usage, NULL, NULL))) {
440 		x->ex_flags |= EXFLAG_XKUSAGE;
441 		for(i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
442 			switch(OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage,i))) {
443 				case NID_server_auth:
444 				x->ex_xkusage |= XKU_SSL_SERVER;
445 				break;
446 
447 				case NID_client_auth:
448 				x->ex_xkusage |= XKU_SSL_CLIENT;
449 				break;
450 
451 				case NID_email_protect:
452 				x->ex_xkusage |= XKU_SMIME;
453 				break;
454 
455 				case NID_code_sign:
456 				x->ex_xkusage |= XKU_CODE_SIGN;
457 				break;
458 
459 				case NID_ms_sgc:
460 				case NID_ns_sgc:
461 				x->ex_xkusage |= XKU_SGC;
462 				break;
463 
464 				case NID_OCSP_sign:
465 				x->ex_xkusage |= XKU_OCSP_SIGN;
466 				break;
467 
468 				case NID_time_stamp:
469 				x->ex_xkusage |= XKU_TIMESTAMP;
470 				break;
471 
472 				case NID_dvcs:
473 				x->ex_xkusage |= XKU_DVCS;
474 				break;
475 
476 				case NID_anyExtendedKeyUsage:
477 				x->ex_xkusage |= XKU_ANYEKU;
478 				break;
479 			}
480 		}
481 		sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
482 	}
483 
484 	if((ns=X509_get_ext_d2i(x, NID_netscape_cert_type, NULL, NULL))) {
485 		if(ns->length > 0) x->ex_nscert = ns->data[0];
486 		else x->ex_nscert = 0;
487 		x->ex_flags |= EXFLAG_NSCERT;
488 		ASN1_BIT_STRING_free(ns);
489 	}
490 	x->skid =X509_get_ext_d2i(x, NID_subject_key_identifier, NULL, NULL);
491 	x->akid =X509_get_ext_d2i(x, NID_authority_key_identifier, NULL, NULL);
492 	/* Does subject name match issuer ? */
493 	if(!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x)))
494 			{
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 				x->ex_flags |= EXFLAG_SS;
499 			}
500 	x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
501 	x->nc = X509_get_ext_d2i(x, NID_name_constraints, &j, NULL);
502 	if (!x->nc && (j != -1))
503 		x->ex_flags |= EXFLAG_INVALID;
504 	setup_crldp(x);
505 
506 	for (j = 0; j < X509_get_ext_count(x); j++)
507 		{
508 		ex = X509_get_ext(x, j);
509 		if (OBJ_obj2nid(X509_EXTENSION_get_object(ex))
510 					== NID_freshest_crl)
511 			x->ex_flags |= EXFLAG_FRESHEST;
512 		if (!X509_EXTENSION_get_critical(ex))
513 			continue;
514 		if (!X509_supported_extension(ex))
515 			{
516 			x->ex_flags |= EXFLAG_CRITICAL;
517 			break;
518 			}
519 		}
520 	x->ex_flags |= EXFLAG_SET;
521 
522 	CRYPTO_STATIC_MUTEX_unlock(&g_x509_cache_extensions_lock);
523 }
524 
525 /* CA checks common to all purposes
526  * return codes:
527  * 0 not a CA
528  * 1 is a CA
529  * 2 basicConstraints absent so "maybe" a CA
530  * 3 basicConstraints absent but self signed V1.
531  * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
532  */
533 
534 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
535 #define ku_reject(x, usage) \
536 	(((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
537 #define xku_reject(x, usage) \
538 	(((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
539 #define ns_reject(x, usage) \
540 	(((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
541 
check_ca(const X509 * x)542 static int check_ca(const X509 *x)
543 {
544 	/* keyUsage if present should allow cert signing */
545 	if(ku_reject(x, KU_KEY_CERT_SIGN)) return 0;
546 	if(x->ex_flags & EXFLAG_BCONS) {
547 		if(x->ex_flags & EXFLAG_CA) return 1;
548 		/* If basicConstraints says not a CA then say so */
549 		else return 0;
550 	} else {
551 		/* we support V1 roots for...  uh, I don't really know why. */
552 		if((x->ex_flags & V1_ROOT) == V1_ROOT) return 3;
553 		/* If key usage present it must have certSign so tolerate it */
554 		else if (x->ex_flags & EXFLAG_KUSAGE) return 4;
555 		/* Older certificates could have Netscape-specific CA types */
556 		else if (x->ex_flags & EXFLAG_NSCERT
557 			 && x->ex_nscert & NS_ANY_CA) return 5;
558 		/* can this still be regarded a CA certificate?  I doubt it */
559 		return 0;
560 	}
561 }
562 
X509_check_ca(X509 * x)563 int X509_check_ca(X509 *x)
564 {
565 	if(!(x->ex_flags & EXFLAG_SET)) {
566 		x509v3_cache_extensions(x);
567 	}
568 
569 	return check_ca(x);
570 }
571 
572 /* Check SSL CA: common checks for SSL client and server */
check_ssl_ca(const X509 * x)573 static int check_ssl_ca(const X509 *x)
574 {
575 	int ca_ret;
576 	ca_ret = check_ca(x);
577 	if(!ca_ret) return 0;
578 	/* check nsCertType if present */
579 	if(ca_ret != 5 || x->ex_nscert & NS_SSL_CA) return ca_ret;
580 	else return 0;
581 }
582 
583 
check_purpose_ssl_client(const X509_PURPOSE * xp,const X509 * x,int ca)584 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca)
585 {
586 	if(xku_reject(x,XKU_SSL_CLIENT)) return 0;
587 	if(ca) return check_ssl_ca(x);
588 	/* We need to do digital signatures or key agreement */
589 	if(ku_reject(x,KU_DIGITAL_SIGNATURE|KU_KEY_AGREEMENT)) return 0;
590 	/* nsCertType if present should allow SSL client use */
591 	if(ns_reject(x, NS_SSL_CLIENT)) return 0;
592 	return 1;
593 }
594 /* Key usage needed for TLS/SSL server: digital signature, encipherment or
595  * key agreement. The ssl code can check this more thoroughly for individual
596  * key types.
597  */
598 #define KU_TLS \
599 	KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT|KU_KEY_AGREEMENT
600 
check_purpose_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)601 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
602 {
603 	if(xku_reject(x,XKU_SSL_SERVER|XKU_SGC)) return 0;
604 	if(ca) return check_ssl_ca(x);
605 
606 	if(ns_reject(x, NS_SSL_SERVER)) return 0;
607 	if(ku_reject(x, KU_TLS)) return 0;
608 
609 	return 1;
610 
611 }
612 
check_purpose_ns_ssl_server(const X509_PURPOSE * xp,const X509 * x,int ca)613 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
614 {
615 	int ret;
616 	ret = check_purpose_ssl_server(xp, x, ca);
617 	if(!ret || ca) return ret;
618 	/* We need to encipher or Netscape complains */
619 	if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
620 	return ret;
621 }
622 
623 /* common S/MIME checks */
purpose_smime(const X509 * x,int ca)624 static int purpose_smime(const X509 *x, int ca)
625 {
626 	if(xku_reject(x,XKU_SMIME)) return 0;
627 	if(ca) {
628 		int ca_ret;
629 		ca_ret = check_ca(x);
630 		if(!ca_ret) return 0;
631 		/* check nsCertType if present */
632 		if(ca_ret != 5 || x->ex_nscert & NS_SMIME_CA) return ca_ret;
633 		else return 0;
634 	}
635 	if(x->ex_flags & EXFLAG_NSCERT) {
636 		if(x->ex_nscert & NS_SMIME) return 1;
637 		/* Workaround for some buggy certificates */
638 		if(x->ex_nscert & NS_SSL_CLIENT) return 2;
639 		return 0;
640 	}
641 	return 1;
642 }
643 
check_purpose_smime_sign(const X509_PURPOSE * xp,const X509 * x,int ca)644 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
645 {
646 	int ret;
647 	ret = purpose_smime(x, ca);
648 	if(!ret || ca) return ret;
649 	if(ku_reject(x, KU_DIGITAL_SIGNATURE|KU_NON_REPUDIATION)) return 0;
650 	return ret;
651 }
652 
check_purpose_smime_encrypt(const X509_PURPOSE * xp,const X509 * x,int ca)653 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca)
654 {
655 	int ret;
656 	ret = purpose_smime(x, ca);
657 	if(!ret || ca) return ret;
658 	if(ku_reject(x, KU_KEY_ENCIPHERMENT)) return 0;
659 	return ret;
660 }
661 
check_purpose_crl_sign(const X509_PURPOSE * xp,const X509 * x,int ca)662 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
663 {
664 	if(ca) {
665 		int ca_ret;
666 		if((ca_ret = check_ca(x)) != 2) return ca_ret;
667 		else return 0;
668 	}
669 	if(ku_reject(x, KU_CRL_SIGN)) return 0;
670 	return 1;
671 }
672 
673 /* OCSP helper: this is *not* a full OCSP check. It just checks that
674  * each CA is valid. Additional checks must be made on the chain.
675  */
676 
ocsp_helper(const X509_PURPOSE * xp,const X509 * x,int ca)677 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
678 {
679 	/* Must be a valid CA.  Should we really support the "I don't know"
680 	   value (2)? */
681 	if(ca) return check_ca(x);
682 	/* leaf certificate is checked in OCSP_verify() */
683 	return 1;
684 }
685 
check_purpose_timestamp_sign(const X509_PURPOSE * xp,const X509 * x,int ca)686 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
687 					int ca)
688 {
689 	int i_ext;
690 
691 	/* If ca is true we must return if this is a valid CA certificate. */
692 	if (ca) return check_ca(x);
693 
694 	/*
695 	 * Check the optional key usage field:
696 	 * if Key Usage is present, it must be one of digitalSignature
697 	 * and/or nonRepudiation (other values are not consistent and shall
698 	 * be rejected).
699 	 */
700 	if ((x->ex_flags & EXFLAG_KUSAGE)
701 	    && ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
702 		!(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
703 		return 0;
704 
705 	/* Only time stamp key usage is permitted and it's required. */
706 	if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
707 		return 0;
708 
709 	/* Extended Key Usage MUST be critical */
710 	i_ext = X509_get_ext_by_NID((X509 *) x, NID_ext_key_usage, -1);
711 	if (i_ext >= 0)
712 		{
713 		X509_EXTENSION *ext = X509_get_ext((X509 *) x, i_ext);
714 		if (!X509_EXTENSION_get_critical(ext))
715 			return 0;
716 		}
717 
718 	return 1;
719 }
720 
no_check(const X509_PURPOSE * xp,const X509 * x,int ca)721 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
722 {
723 	return 1;
724 }
725 
726 /* Various checks to see if one certificate issued the second.
727  * This can be used to prune a set of possible issuer certificates
728  * which have been looked up using some simple method such as by
729  * subject name.
730  * These are:
731  * 1. Check issuer_name(subject) == subject_name(issuer)
732  * 2. If akid(subject) exists check it matches issuer
733  * 3. If key_usage(issuer) exists check it supports certificate signing
734  * returns 0 for OK, positive for reason for mismatch, reasons match
735  * codes for X509_verify_cert()
736  */
737 
X509_check_issued(X509 * issuer,X509 * subject)738 int X509_check_issued(X509 *issuer, X509 *subject)
739 {
740 	if(X509_NAME_cmp(X509_get_subject_name(issuer),
741 			X509_get_issuer_name(subject)))
742 				return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
743 	x509v3_cache_extensions(issuer);
744 	x509v3_cache_extensions(subject);
745 
746 	if(subject->akid)
747 		{
748 		int ret = X509_check_akid(issuer, subject->akid);
749 		if (ret != X509_V_OK)
750 			return ret;
751 		}
752 
753 	if(subject->ex_flags & EXFLAG_PROXY)
754 		{
755 		if(ku_reject(issuer, KU_DIGITAL_SIGNATURE))
756 			return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
757 		}
758 	else if(ku_reject(issuer, KU_KEY_CERT_SIGN))
759 		return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
760 	return X509_V_OK;
761 }
762 
X509_check_akid(X509 * issuer,AUTHORITY_KEYID * akid)763 int X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
764 	{
765 
766 	if(!akid)
767 		return X509_V_OK;
768 
769 	/* Check key ids (if present) */
770 	if(akid->keyid && issuer->skid &&
771 		 ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid) )
772 				return X509_V_ERR_AKID_SKID_MISMATCH;
773 	/* Check serial number */
774 	if(akid->serial &&
775 		ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
776 				return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
777 	/* Check issuer name */
778 	if(akid->issuer)
779 		{
780 		/* Ugh, for some peculiar reason AKID includes
781 		 * SEQUENCE OF GeneralName. So look for a DirName.
782 		 * There may be more than one but we only take any
783 		 * notice of the first.
784 		 */
785 		GENERAL_NAMES *gens;
786 		GENERAL_NAME *gen;
787 		X509_NAME *nm = NULL;
788 		size_t i;
789 		gens = akid->issuer;
790 		for(i = 0; i < sk_GENERAL_NAME_num(gens); i++)
791 			{
792 			gen = sk_GENERAL_NAME_value(gens, i);
793 			if(gen->type == GEN_DIRNAME)
794 				{
795 				nm = gen->d.dirn;
796 				break;
797 				}
798 			}
799 		if(nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
800 			return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
801 		}
802 	return X509_V_OK;
803 	}
804 
805