xref: /openbsd-src/lib/libcrypto/x509/x509_purp.c (revision 5a38ef86d0b61900239c7913d24a05e7b88a58f0)
1 /* $OpenBSD: x509_purp.c,v 1.13 2021/11/04 23:52:34 beck Exp $ */
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  */
58 
59 #include <stdio.h>
60 #include <string.h>
61 
62 #include <openssl/opensslconf.h>
63 
64 #include <openssl/err.h>
65 #include <openssl/x509v3.h>
66 #include <openssl/x509_vfy.h>
67 
68 #include "x509_internal.h"
69 #include "x509_lcl.h"
70 
71 #define V1_ROOT (EXFLAG_V1|EXFLAG_SS)
72 #define ku_reject(x, usage) \
73 	(((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
74 #define xku_reject(x, usage) \
75 	(((x)->ex_flags & EXFLAG_XKUSAGE) && !((x)->ex_xkusage & (usage)))
76 #define ns_reject(x, usage) \
77 	(((x)->ex_flags & EXFLAG_NSCERT) && !((x)->ex_nscert & (usage)))
78 
79 void x509v3_cache_extensions(X509 *x);
80 
81 static int check_ssl_ca(const X509 *x);
82 static int check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x,
83     int ca);
84 static int check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x,
85     int ca);
86 static int check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x,
87     int ca);
88 static int purpose_smime(const X509 *x, int ca);
89 static int check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x,
90     int ca);
91 static int check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x,
92     int ca);
93 static int check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x,
94     int ca);
95 static int check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x,
96     int ca);
97 static int no_check(const X509_PURPOSE *xp, const X509 *x, int ca);
98 static int ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca);
99 
100 static int xp_cmp(const X509_PURPOSE * const *a, const X509_PURPOSE * const *b);
101 static void xptable_free(X509_PURPOSE *p);
102 
103 static X509_PURPOSE xstandard[] = {
104 	{X509_PURPOSE_SSL_CLIENT, X509_TRUST_SSL_CLIENT, 0, check_purpose_ssl_client, "SSL client", "sslclient", NULL},
105 	{X509_PURPOSE_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ssl_server, "SSL server", "sslserver", NULL},
106 	{X509_PURPOSE_NS_SSL_SERVER, X509_TRUST_SSL_SERVER, 0, check_purpose_ns_ssl_server, "Netscape SSL server", "nssslserver", NULL},
107 	{X509_PURPOSE_SMIME_SIGN, X509_TRUST_EMAIL, 0, check_purpose_smime_sign, "S/MIME signing", "smimesign", NULL},
108 	{X509_PURPOSE_SMIME_ENCRYPT, X509_TRUST_EMAIL, 0, check_purpose_smime_encrypt, "S/MIME encryption", "smimeencrypt", NULL},
109 	{X509_PURPOSE_CRL_SIGN, X509_TRUST_COMPAT, 0, check_purpose_crl_sign, "CRL signing", "crlsign", NULL},
110 	{X509_PURPOSE_ANY, X509_TRUST_DEFAULT, 0, no_check, "Any Purpose", "any", NULL},
111 	{X509_PURPOSE_OCSP_HELPER, X509_TRUST_COMPAT, 0, ocsp_helper, "OCSP helper", "ocsphelper", NULL},
112 	{X509_PURPOSE_TIMESTAMP_SIGN, X509_TRUST_TSA, 0, check_purpose_timestamp_sign, "Time Stamp signing", "timestampsign", NULL},
113 };
114 
115 #define X509_PURPOSE_COUNT (sizeof(xstandard)/sizeof(X509_PURPOSE))
116 
117 static STACK_OF(X509_PURPOSE) *xptable = NULL;
118 
119 static int
120 xp_cmp(const X509_PURPOSE * const *a, const X509_PURPOSE * const *b)
121 {
122 	return (*a)->purpose - (*b)->purpose;
123 }
124 
125 /* As much as I'd like to make X509_check_purpose use a "const" X509*
126  * I really can't because it does recalculate hashes and do other non-const
127  * things. */
128 int
129 X509_check_purpose(X509 *x, int id, int ca)
130 {
131 	int idx;
132 	const X509_PURPOSE *pt;
133 
134 	if (!(x->ex_flags & EXFLAG_SET)) {
135 		CRYPTO_w_lock(CRYPTO_LOCK_X509);
136 		x509v3_cache_extensions(x);
137 		CRYPTO_w_unlock(CRYPTO_LOCK_X509);
138 		if (x->ex_flags & EXFLAG_INVALID)
139 			return -1;
140 	}
141 	if (id == -1)
142 		return 1;
143 	idx = X509_PURPOSE_get_by_id(id);
144 	if (idx == -1)
145 		return -1;
146 	pt = X509_PURPOSE_get0(idx);
147 	return pt->check_purpose(pt, x, ca);
148 }
149 
150 int
151 X509_PURPOSE_set(int *p, int purpose)
152 {
153 	if (X509_PURPOSE_get_by_id(purpose) == -1) {
154 		X509V3error(X509V3_R_INVALID_PURPOSE);
155 		return 0;
156 	}
157 	*p = purpose;
158 	return 1;
159 }
160 
161 int
162 X509_PURPOSE_get_count(void)
163 {
164 	if (!xptable)
165 		return X509_PURPOSE_COUNT;
166 	return sk_X509_PURPOSE_num(xptable) + X509_PURPOSE_COUNT;
167 }
168 
169 X509_PURPOSE *
170 X509_PURPOSE_get0(int idx)
171 {
172 	if (idx < 0)
173 		return NULL;
174 	if (idx < (int)X509_PURPOSE_COUNT)
175 		return xstandard + idx;
176 	return sk_X509_PURPOSE_value(xptable, idx - X509_PURPOSE_COUNT);
177 }
178 
179 int
180 X509_PURPOSE_get_by_sname(const char *sname)
181 {
182 	int i;
183 	X509_PURPOSE *xptmp;
184 
185 	for (i = 0; i < X509_PURPOSE_get_count(); i++) {
186 		xptmp = X509_PURPOSE_get0(i);
187 		if (!strcmp(xptmp->sname, sname))
188 			return i;
189 	}
190 	return -1;
191 }
192 
193 int
194 X509_PURPOSE_get_by_id(int purpose)
195 {
196 	X509_PURPOSE tmp;
197 	int idx;
198 
199 	if ((purpose >= X509_PURPOSE_MIN) && (purpose <= X509_PURPOSE_MAX))
200 		return purpose - X509_PURPOSE_MIN;
201 	tmp.purpose = purpose;
202 	if (!xptable)
203 		return -1;
204 	idx = sk_X509_PURPOSE_find(xptable, &tmp);
205 	if (idx == -1)
206 		return -1;
207 	return idx + X509_PURPOSE_COUNT;
208 }
209 
210 int
211 X509_PURPOSE_add(int id, int trust, int flags,
212     int (*ck)(const X509_PURPOSE *, const X509 *, int), const char *name,
213     const char *sname, void *arg)
214 {
215 	int idx;
216 	X509_PURPOSE *ptmp;
217 	char *name_dup, *sname_dup;
218 
219 	name_dup = sname_dup = NULL;
220 
221 	if (name == NULL || sname == NULL) {
222 		X509V3error(X509V3_R_INVALID_NULL_ARGUMENT);
223 		return 0;
224 	}
225 
226 	/* This is set according to what we change: application can't set it */
227 	flags &= ~X509_PURPOSE_DYNAMIC;
228 	/* This will always be set for application modified trust entries */
229 	flags |= X509_PURPOSE_DYNAMIC_NAME;
230 	/* Get existing entry if any */
231 	idx = X509_PURPOSE_get_by_id(id);
232 	/* Need a new entry */
233 	if (idx == -1) {
234 		if ((ptmp = malloc(sizeof(X509_PURPOSE))) == NULL) {
235 			X509V3error(ERR_R_MALLOC_FAILURE);
236 			return 0;
237 		}
238 		ptmp->flags = X509_PURPOSE_DYNAMIC;
239 	} else
240 		ptmp = X509_PURPOSE_get0(idx);
241 
242 	if ((name_dup = strdup(name)) == NULL)
243 		goto err;
244 	if ((sname_dup = strdup(sname)) == NULL)
245 		goto err;
246 
247 	/* free existing name if dynamic */
248 	if (ptmp->flags & X509_PURPOSE_DYNAMIC_NAME) {
249 		free(ptmp->name);
250 		free(ptmp->sname);
251 	}
252 	/* dup supplied name */
253 	ptmp->name = name_dup;
254 	ptmp->sname = sname_dup;
255 	/* Keep the dynamic flag of existing entry */
256 	ptmp->flags &= X509_PURPOSE_DYNAMIC;
257 	/* Set all other flags */
258 	ptmp->flags |= flags;
259 
260 	ptmp->purpose = id;
261 	ptmp->trust = trust;
262 	ptmp->check_purpose = ck;
263 	ptmp->usr_data = arg;
264 
265 	/* If its a new entry manage the dynamic table */
266 	if (idx == -1) {
267 		if (xptable == NULL &&
268 		    (xptable = sk_X509_PURPOSE_new(xp_cmp)) == NULL)
269 			goto err;
270 		if (sk_X509_PURPOSE_push(xptable, ptmp) == 0)
271 			goto err;
272 	}
273 	return 1;
274 
275 err:
276 	free(name_dup);
277 	free(sname_dup);
278 	if (idx == -1)
279 		free(ptmp);
280 	X509V3error(ERR_R_MALLOC_FAILURE);
281 	return 0;
282 }
283 
284 static void
285 xptable_free(X509_PURPOSE *p)
286 {
287 	if (!p)
288 		return;
289 	if (p->flags & X509_PURPOSE_DYNAMIC) {
290 		if (p->flags & X509_PURPOSE_DYNAMIC_NAME) {
291 			free(p->name);
292 			free(p->sname);
293 		}
294 		free(p);
295 	}
296 }
297 
298 void
299 X509_PURPOSE_cleanup(void)
300 {
301 	sk_X509_PURPOSE_pop_free(xptable, xptable_free);
302 	xptable = NULL;
303 }
304 
305 int
306 X509_PURPOSE_get_id(const X509_PURPOSE *xp)
307 {
308 	return xp->purpose;
309 }
310 
311 char *
312 X509_PURPOSE_get0_name(const X509_PURPOSE *xp)
313 {
314 	return xp->name;
315 }
316 
317 char *
318 X509_PURPOSE_get0_sname(const X509_PURPOSE *xp)
319 {
320 	return xp->sname;
321 }
322 
323 int
324 X509_PURPOSE_get_trust(const X509_PURPOSE *xp)
325 {
326 	return xp->trust;
327 }
328 
329 static int
330 nid_cmp(const int *a, const int *b)
331 {
332 	return *a - *b;
333 }
334 
335 static int nid_cmp_BSEARCH_CMP_FN(const void *, const void *);
336 static int nid_cmp(int const *, int const *);
337 static int *OBJ_bsearch_nid(int *key, int const *base, int num);
338 
339 static int
340 nid_cmp_BSEARCH_CMP_FN(const void *a_, const void *b_)
341 {
342 	int const *a = a_;
343 	int const *b = b_;
344 	return nid_cmp(a, b);
345 }
346 
347 static int *
348 OBJ_bsearch_nid(int *key, int const *base, int num)
349 {
350 	return (int *)OBJ_bsearch_(key, base, num, sizeof(int),
351 	    nid_cmp_BSEARCH_CMP_FN);
352 }
353 
354 int
355 X509_supported_extension(X509_EXTENSION *ex)
356 {
357 	/* This table is a list of the NIDs of supported extensions:
358 	 * that is those which are used by the verify process. If
359 	 * an extension is critical and doesn't appear in this list
360 	 * then the verify process will normally reject the certificate.
361 	 * The list must be kept in numerical order because it will be
362 	 * searched using bsearch.
363 	 */
364 
365 	static const int supported_nids[] = {
366 		NID_netscape_cert_type, /* 71 */
367 		NID_key_usage,		/* 83 */
368 		NID_subject_alt_name,	/* 85 */
369 		NID_basic_constraints,	/* 87 */
370 		NID_certificate_policies, /* 89 */
371 		NID_ext_key_usage,	/* 126 */
372 #ifndef OPENSSL_NO_RFC3779
373 		NID_sbgp_ipAddrBlock,   /* 290 */
374 		NID_sbgp_autonomousSysNum, /* 291 */
375 #endif
376 		NID_policy_constraints,	/* 401 */
377 		NID_proxyCertInfo,	/* 663 */
378 		NID_name_constraints,	/* 666 */
379 		NID_policy_mappings,	/* 747 */
380 		NID_inhibit_any_policy	/* 748 */
381 	};
382 
383 	int ex_nid = OBJ_obj2nid(X509_EXTENSION_get_object(ex));
384 
385 	if (ex_nid == NID_undef)
386 		return 0;
387 
388 	if (OBJ_bsearch_nid(&ex_nid, supported_nids,
389 	    sizeof(supported_nids) / sizeof(int)))
390 		return 1;
391 	return 0;
392 }
393 
394 static void
395 setup_dp(X509 *x, DIST_POINT *dp)
396 {
397 	X509_NAME *iname = NULL;
398 	int i;
399 
400 	if (dp->reasons) {
401 		if (dp->reasons->length > 0)
402 			dp->dp_reasons = dp->reasons->data[0];
403 		if (dp->reasons->length > 1)
404 			dp->dp_reasons |= (dp->reasons->data[1] << 8);
405 		dp->dp_reasons &= CRLDP_ALL_REASONS;
406 	} else
407 		dp->dp_reasons = CRLDP_ALL_REASONS;
408 	if (!dp->distpoint || (dp->distpoint->type != 1))
409 		return;
410 	for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
411 		GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
412 		if (gen->type == GEN_DIRNAME) {
413 			iname = gen->d.directoryName;
414 			break;
415 		}
416 	}
417 	if (!iname)
418 		iname = X509_get_issuer_name(x);
419 
420 	DIST_POINT_set_dpname(dp->distpoint, iname);
421 
422 }
423 
424 static void
425 setup_crldp(X509 *x)
426 {
427 	int i;
428 
429 	x->crldp = X509_get_ext_d2i(x, NID_crl_distribution_points, &i, NULL);
430 	if (x->crldp == NULL && i != -1) {
431 		x->ex_flags |= EXFLAG_INVALID;
432 		return;
433 	}
434 
435 	for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++)
436 		setup_dp(x, sk_DIST_POINT_value(x->crldp, i));
437 }
438 
439 void
440 x509v3_cache_extensions(X509 *x)
441 {
442 	BASIC_CONSTRAINTS *bs;
443 	PROXY_CERT_INFO_EXTENSION *pci;
444 	ASN1_BIT_STRING *usage;
445 	ASN1_BIT_STRING *ns;
446 	EXTENDED_KEY_USAGE *extusage;
447 	X509_EXTENSION *ex;
448 	int i;
449 
450 	if (x->ex_flags & EXFLAG_SET)
451 		return;
452 
453 	X509_digest(x, X509_CERT_HASH_EVP, x->hash, NULL);
454 
455 	/* V1 should mean no extensions ... */
456 	if (!X509_get_version(x))
457 		x->ex_flags |= EXFLAG_V1;
458 
459 	/* Handle basic constraints */
460 	if ((bs = X509_get_ext_d2i(x, NID_basic_constraints, &i, NULL))) {
461 		if (bs->ca)
462 			x->ex_flags |= EXFLAG_CA;
463 		if (bs->pathlen) {
464 			if ((bs->pathlen->type == V_ASN1_NEG_INTEGER) ||
465 			    !bs->ca) {
466 				x->ex_flags |= EXFLAG_INVALID;
467 				x->ex_pathlen = 0;
468 			} else
469 				x->ex_pathlen = ASN1_INTEGER_get(bs->pathlen);
470 		} else
471 			x->ex_pathlen = -1;
472 		BASIC_CONSTRAINTS_free(bs);
473 		x->ex_flags |= EXFLAG_BCONS;
474 	} else if (i != -1) {
475 		x->ex_flags |= EXFLAG_INVALID;
476 	}
477 
478 	/* Handle proxy certificates */
479 	if ((pci = X509_get_ext_d2i(x, NID_proxyCertInfo, &i, NULL))) {
480 		if (x->ex_flags & EXFLAG_CA ||
481 		    X509_get_ext_by_NID(x, NID_subject_alt_name, -1) >= 0 ||
482 		    X509_get_ext_by_NID(x, NID_issuer_alt_name, -1) >= 0) {
483 			x->ex_flags |= EXFLAG_INVALID;
484 		}
485 		if (pci->pcPathLengthConstraint) {
486 			if (pci->pcPathLengthConstraint->type ==
487 			    V_ASN1_NEG_INTEGER) {
488 				x->ex_flags |= EXFLAG_INVALID;
489 				x->ex_pcpathlen = 0;
490 			} else
491 				x->ex_pcpathlen =
492 				    ASN1_INTEGER_get(pci->
493 				      pcPathLengthConstraint);
494 		} else
495 			x->ex_pcpathlen = -1;
496 		PROXY_CERT_INFO_EXTENSION_free(pci);
497 		x->ex_flags |= EXFLAG_PROXY;
498 	} else if (i != -1) {
499 		x->ex_flags |= EXFLAG_INVALID;
500 	}
501 
502 	/* Handle key usage */
503 	if ((usage = X509_get_ext_d2i(x, NID_key_usage, &i, NULL))) {
504 		if (usage->length > 0) {
505 			x->ex_kusage = usage->data[0];
506 			if (usage->length > 1)
507 				x->ex_kusage |= usage->data[1] << 8;
508 		} else
509 			x->ex_kusage = 0;
510 		x->ex_flags |= EXFLAG_KUSAGE;
511 		ASN1_BIT_STRING_free(usage);
512 	} else if (i != -1) {
513 		x->ex_flags |= EXFLAG_INVALID;
514 	}
515 
516 	x->ex_xkusage = 0;
517 	if ((extusage = X509_get_ext_d2i(x, NID_ext_key_usage, &i, NULL))) {
518 		x->ex_flags |= EXFLAG_XKUSAGE;
519 		for (i = 0; i < sk_ASN1_OBJECT_num(extusage); i++) {
520 			switch (OBJ_obj2nid(sk_ASN1_OBJECT_value(extusage, i))) {
521 			case NID_server_auth:
522 				x->ex_xkusage |= XKU_SSL_SERVER;
523 				break;
524 
525 			case NID_client_auth:
526 				x->ex_xkusage |= XKU_SSL_CLIENT;
527 				break;
528 
529 			case NID_email_protect:
530 				x->ex_xkusage |= XKU_SMIME;
531 				break;
532 
533 			case NID_code_sign:
534 				x->ex_xkusage |= XKU_CODE_SIGN;
535 				break;
536 
537 			case NID_ms_sgc:
538 			case NID_ns_sgc:
539 				x->ex_xkusage |= XKU_SGC;
540 				break;
541 
542 			case NID_OCSP_sign:
543 				x->ex_xkusage |= XKU_OCSP_SIGN;
544 				break;
545 
546 			case NID_time_stamp:
547 				x->ex_xkusage |= XKU_TIMESTAMP;
548 				break;
549 
550 			case NID_dvcs:
551 				x->ex_xkusage |= XKU_DVCS;
552 				break;
553 
554 			case NID_anyExtendedKeyUsage:
555 				x->ex_xkusage |= XKU_ANYEKU;
556 				break;
557 			}
558 		}
559 		sk_ASN1_OBJECT_pop_free(extusage, ASN1_OBJECT_free);
560 	} else if (i != -1) {
561 		x->ex_flags |= EXFLAG_INVALID;
562 	}
563 
564 	if ((ns = X509_get_ext_d2i(x, NID_netscape_cert_type, &i, NULL))) {
565 		if (ns->length > 0)
566 			x->ex_nscert = ns->data[0];
567 		else
568 			x->ex_nscert = 0;
569 		x->ex_flags |= EXFLAG_NSCERT;
570 		ASN1_BIT_STRING_free(ns);
571 	} else if (i != -1) {
572 		x->ex_flags |= EXFLAG_INVALID;
573 	}
574 
575 	x->skid = X509_get_ext_d2i(x, NID_subject_key_identifier, &i, NULL);
576 	if (x->skid == NULL && i != -1)
577 		x->ex_flags |= EXFLAG_INVALID;
578 	x->akid = X509_get_ext_d2i(x, NID_authority_key_identifier, &i, NULL);
579 	if (x->akid == NULL && i != -1)
580 		x->ex_flags |= EXFLAG_INVALID;
581 
582 	/* Does subject name match issuer? */
583 	if (!X509_NAME_cmp(X509_get_subject_name(x), X509_get_issuer_name(x))) {
584 		x->ex_flags |= EXFLAG_SI;
585 		/* If SKID matches AKID also indicate self signed. */
586 		if (X509_check_akid(x, x->akid) == X509_V_OK &&
587 		    !ku_reject(x, KU_KEY_CERT_SIGN))
588 			x->ex_flags |= EXFLAG_SS;
589 	}
590 
591 	x->altname = X509_get_ext_d2i(x, NID_subject_alt_name, &i, NULL);
592 	if (x->altname == NULL && i != -1)
593 		x->ex_flags |= EXFLAG_INVALID;
594 	x->nc = X509_get_ext_d2i(x, NID_name_constraints, &i, NULL);
595 	if (!x->nc && (i != -1))
596 		x->ex_flags |= EXFLAG_INVALID;
597 	setup_crldp(x);
598 
599 #ifndef OPENSSL_NO_RFC3779
600 	x->rfc3779_addr = X509_get_ext_d2i(x, NID_sbgp_ipAddrBlock, &i, NULL);
601 	if (x->rfc3779_addr == NULL && i != -1)
602 		x->ex_flags |= EXFLAG_INVALID;
603 	x->rfc3779_asid = X509_get_ext_d2i(x, NID_sbgp_autonomousSysNum, &i, NULL);
604 	if (x->rfc3779_asid == NULL && i != -1)
605 		x->ex_flags |= EXFLAG_INVALID;
606 #endif
607 
608 	for (i = 0; i < X509_get_ext_count(x); i++) {
609 		ex = X509_get_ext(x, i);
610 		if (OBJ_obj2nid(X509_EXTENSION_get_object(ex)) ==
611 		    NID_freshest_crl)
612 			x->ex_flags |= EXFLAG_FRESHEST;
613 		if (!X509_EXTENSION_get_critical(ex))
614 			continue;
615 		if (!X509_supported_extension(ex)) {
616 			x->ex_flags |= EXFLAG_CRITICAL;
617 			break;
618 		}
619 	}
620 
621 	x509_verify_cert_info_populate(x);
622 
623 	x->ex_flags |= EXFLAG_SET;
624 }
625 
626 /* CA checks common to all purposes
627  * return codes:
628  * 0 not a CA
629  * 1 is a CA
630  * 2 basicConstraints absent so "maybe" a CA
631  * 3 basicConstraints absent but self signed V1.
632  * 4 basicConstraints absent but keyUsage present and keyCertSign asserted.
633  */
634 
635 static int
636 check_ca(const X509 *x)
637 {
638 	/* keyUsage if present should allow cert signing */
639 	if (ku_reject(x, KU_KEY_CERT_SIGN))
640 		return 0;
641 	if (x->ex_flags & EXFLAG_BCONS) {
642 		if (x->ex_flags & EXFLAG_CA)
643 			return 1;
644 		/* If basicConstraints says not a CA then say so */
645 		else
646 			return 0;
647 	} else {
648 		/* we support V1 roots for...  uh, I don't really know why. */
649 		if ((x->ex_flags & V1_ROOT) == V1_ROOT)
650 			return 3;
651 		/* If key usage present it must have certSign so tolerate it */
652 		else if (x->ex_flags & EXFLAG_KUSAGE)
653 			return 4;
654 		/* Older certificates could have Netscape-specific CA types */
655 		else if (x->ex_flags & EXFLAG_NSCERT &&
656 		    x->ex_nscert & NS_ANY_CA)
657 			return 5;
658 		/* can this still be regarded a CA certificate?  I doubt it */
659 		return 0;
660 	}
661 }
662 
663 int
664 X509_check_ca(X509 *x)
665 {
666 	if (!(x->ex_flags & EXFLAG_SET)) {
667 		CRYPTO_w_lock(CRYPTO_LOCK_X509);
668 		x509v3_cache_extensions(x);
669 		CRYPTO_w_unlock(CRYPTO_LOCK_X509);
670 		if (x->ex_flags & EXFLAG_INVALID)
671 			return X509_V_ERR_UNSPECIFIED;
672 	}
673 
674 	return check_ca(x);
675 }
676 
677 /* Check SSL CA: common checks for SSL client and server */
678 static int
679 check_ssl_ca(const X509 *x)
680 {
681 	int ca_ret;
682 
683 	ca_ret = check_ca(x);
684 	if (!ca_ret)
685 		return 0;
686 	/* check nsCertType if present */
687 	if (ca_ret != 5 || x->ex_nscert & NS_SSL_CA)
688 		return ca_ret;
689 	else
690 		return 0;
691 }
692 
693 static int
694 check_purpose_ssl_client(const X509_PURPOSE *xp, const X509 *x, int ca)
695 {
696 	if (xku_reject(x, XKU_SSL_CLIENT))
697 		return 0;
698 	if (ca)
699 		return check_ssl_ca(x);
700 	/* We need to do digital signatures with it */
701 	if (ku_reject(x, KU_DIGITAL_SIGNATURE))
702 		return 0;
703 	/* nsCertType if present should allow SSL client use */
704 	if (ns_reject(x, NS_SSL_CLIENT))
705 		return 0;
706 	return 1;
707 }
708 
709 static int
710 check_purpose_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
711 {
712 	if (xku_reject(x, XKU_SSL_SERVER|XKU_SGC))
713 		return 0;
714 	if (ca)
715 		return check_ssl_ca(x);
716 
717 	if (ns_reject(x, NS_SSL_SERVER))
718 		return 0;
719 	/* Now as for keyUsage: we'll at least need to sign OR encipher */
720 	if (ku_reject(x, KU_DIGITAL_SIGNATURE|KU_KEY_ENCIPHERMENT))
721 		return 0;
722 
723 	return 1;
724 }
725 
726 static int
727 check_purpose_ns_ssl_server(const X509_PURPOSE *xp, const X509 *x, int ca)
728 {
729 	int ret;
730 
731 	ret = check_purpose_ssl_server(xp, x, ca);
732 	if (!ret || ca)
733 		return ret;
734 	/* We need to encipher or Netscape complains */
735 	if (ku_reject(x, KU_KEY_ENCIPHERMENT))
736 		return 0;
737 	return ret;
738 }
739 
740 /* common S/MIME checks */
741 static int
742 purpose_smime(const X509 *x, int ca)
743 {
744 	if (xku_reject(x, XKU_SMIME))
745 		return 0;
746 	if (ca) {
747 		int ca_ret;
748 		ca_ret = check_ca(x);
749 		if (!ca_ret)
750 			return 0;
751 		/* check nsCertType if present */
752 		if (ca_ret != 5 || x->ex_nscert & NS_SMIME_CA)
753 			return ca_ret;
754 		else
755 			return 0;
756 	}
757 	if (x->ex_flags & EXFLAG_NSCERT) {
758 		if (x->ex_nscert & NS_SMIME)
759 			return 1;
760 		/* Workaround for some buggy certificates */
761 		if (x->ex_nscert & NS_SSL_CLIENT)
762 			return 2;
763 		return 0;
764 	}
765 	return 1;
766 }
767 
768 static int
769 check_purpose_smime_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
770 {
771 	int ret;
772 
773 	ret = purpose_smime(x, ca);
774 	if (!ret || ca)
775 		return ret;
776 	if (ku_reject(x, KU_DIGITAL_SIGNATURE|KU_NON_REPUDIATION))
777 		return 0;
778 	return ret;
779 }
780 
781 static int
782 check_purpose_smime_encrypt(const X509_PURPOSE *xp, const X509 *x, int ca)
783 {
784 	int ret;
785 
786 	ret = purpose_smime(x, ca);
787 	if (!ret || ca)
788 		return ret;
789 	if (ku_reject(x, KU_KEY_ENCIPHERMENT))
790 		return 0;
791 	return ret;
792 }
793 
794 static int
795 check_purpose_crl_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
796 {
797 	if (ca) {
798 		int ca_ret;
799 		if ((ca_ret = check_ca(x)) != 2)
800 			return ca_ret;
801 		else
802 			return 0;
803 	}
804 	if (ku_reject(x, KU_CRL_SIGN))
805 		return 0;
806 	return 1;
807 }
808 
809 /* OCSP helper: this is *not* a full OCSP check. It just checks that
810  * each CA is valid. Additional checks must be made on the chain.
811  */
812 static int
813 ocsp_helper(const X509_PURPOSE *xp, const X509 *x, int ca)
814 {
815 	/* Must be a valid CA.  Should we really support the "I don't know"
816 	   value (2)? */
817 	if (ca)
818 		return check_ca(x);
819 	/* leaf certificate is checked in OCSP_verify() */
820 	return 1;
821 }
822 
823 static int
824 check_purpose_timestamp_sign(const X509_PURPOSE *xp, const X509 *x, int ca)
825 {
826 	int i_ext;
827 
828 	/* If ca is true we must return if this is a valid CA certificate. */
829 	if (ca)
830 		return check_ca(x);
831 
832 	/*
833 	 * Check the optional key usage field:
834 	 * if Key Usage is present, it must be one of digitalSignature
835 	 * and/or nonRepudiation (other values are not consistent and shall
836 	 * be rejected).
837 	 */
838 	if ((x->ex_flags & EXFLAG_KUSAGE) &&
839 	    ((x->ex_kusage & ~(KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE)) ||
840 	    !(x->ex_kusage & (KU_NON_REPUDIATION | KU_DIGITAL_SIGNATURE))))
841 		return 0;
842 
843 	/* Only time stamp key usage is permitted and it's required. */
844 	if (!(x->ex_flags & EXFLAG_XKUSAGE) || x->ex_xkusage != XKU_TIMESTAMP)
845 		return 0;
846 
847 	/* Extended Key Usage MUST be critical */
848 	i_ext = X509_get_ext_by_NID((X509 *) x, NID_ext_key_usage, -1);
849 	if (i_ext >= 0) {
850 		X509_EXTENSION *ext = X509_get_ext((X509 *) x, i_ext);
851 		if (!X509_EXTENSION_get_critical(ext))
852 			return 0;
853 	}
854 
855 	return 1;
856 }
857 
858 static int
859 no_check(const X509_PURPOSE *xp, const X509 *x, int ca)
860 {
861 	return 1;
862 }
863 
864 /* Various checks to see if one certificate issued the second.
865  * This can be used to prune a set of possible issuer certificates
866  * which have been looked up using some simple method such as by
867  * subject name.
868  * These are:
869  * 1. Check issuer_name(subject) == subject_name(issuer)
870  * 2. If akid(subject) exists check it matches issuer
871  * 3. If key_usage(issuer) exists check it supports certificate signing
872  * returns 0 for OK, positive for reason for mismatch, reasons match
873  * codes for X509_verify_cert()
874  */
875 
876 int
877 X509_check_issued(X509 *issuer, X509 *subject)
878 {
879 	if (X509_NAME_cmp(X509_get_subject_name(issuer),
880 	    X509_get_issuer_name(subject)))
881 		return X509_V_ERR_SUBJECT_ISSUER_MISMATCH;
882 	if (!(issuer->ex_flags & EXFLAG_SET)) {
883 		CRYPTO_w_lock(CRYPTO_LOCK_X509);
884 		x509v3_cache_extensions(issuer);
885 		CRYPTO_w_unlock(CRYPTO_LOCK_X509);
886 	}
887 	if (issuer->ex_flags & EXFLAG_INVALID)
888 		return X509_V_ERR_UNSPECIFIED;
889 	if (!(subject->ex_flags & EXFLAG_SET)) {
890 		CRYPTO_w_lock(CRYPTO_LOCK_X509);
891 		x509v3_cache_extensions(subject);
892 		CRYPTO_w_unlock(CRYPTO_LOCK_X509);
893 	}
894 	if (subject->ex_flags & EXFLAG_INVALID)
895 		return X509_V_ERR_UNSPECIFIED;
896 
897 	if (subject->akid) {
898 		int ret = X509_check_akid(issuer, subject->akid);
899 		if (ret != X509_V_OK)
900 			return ret;
901 	}
902 
903 	if (subject->ex_flags & EXFLAG_PROXY) {
904 		if (ku_reject(issuer, KU_DIGITAL_SIGNATURE))
905 			return X509_V_ERR_KEYUSAGE_NO_DIGITAL_SIGNATURE;
906 	} else if (ku_reject(issuer, KU_KEY_CERT_SIGN))
907 		return X509_V_ERR_KEYUSAGE_NO_CERTSIGN;
908 	return X509_V_OK;
909 }
910 
911 int
912 X509_check_akid(X509 *issuer, AUTHORITY_KEYID *akid)
913 {
914 	if (!akid)
915 		return X509_V_OK;
916 
917 	/* Check key ids (if present) */
918 	if (akid->keyid && issuer->skid &&
919 	    ASN1_OCTET_STRING_cmp(akid->keyid, issuer->skid) )
920 		return X509_V_ERR_AKID_SKID_MISMATCH;
921 	/* Check serial number */
922 	if (akid->serial &&
923 	    ASN1_INTEGER_cmp(X509_get_serialNumber(issuer), akid->serial))
924 		return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
925 	/* Check issuer name */
926 	if (akid->issuer) {
927 		/* Ugh, for some peculiar reason AKID includes
928 		 * SEQUENCE OF GeneralName. So look for a DirName.
929 		 * There may be more than one but we only take any
930 		 * notice of the first.
931 		 */
932 		GENERAL_NAMES *gens;
933 		GENERAL_NAME *gen;
934 		X509_NAME *nm = NULL;
935 		int i;
936 		gens = akid->issuer;
937 		for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
938 			gen = sk_GENERAL_NAME_value(gens, i);
939 			if (gen->type == GEN_DIRNAME) {
940 				nm = gen->d.dirn;
941 				break;
942 			}
943 		}
944 		if (nm && X509_NAME_cmp(nm, X509_get_issuer_name(issuer)))
945 			return X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH;
946 	}
947 	return X509_V_OK;
948 }
949 
950 uint32_t
951 X509_get_extension_flags(X509 *x)
952 {
953 	/* Call for side-effect of computing hash and caching extensions */
954 	if (X509_check_purpose(x, -1, -1) != 1)
955 		return 0;
956 
957 	return x->ex_flags;
958 }
959 
960 uint32_t
961 X509_get_key_usage(X509 *x)
962 {
963 	/* Call for side-effect of computing hash and caching extensions */
964 	if (X509_check_purpose(x, -1, -1) != 1)
965 		return 0;
966 
967 	if (x->ex_flags & EXFLAG_KUSAGE)
968 		return x->ex_kusage;
969 
970 	return UINT32_MAX;
971 }
972 
973 uint32_t
974 X509_get_extended_key_usage(X509 *x)
975 {
976 	/* Call for side-effect of computing hash and caching extensions */
977 	if (X509_check_purpose(x, -1, -1) != 1)
978 		return 0;
979 
980 	if (x->ex_flags & EXFLAG_XKUSAGE)
981 		return x->ex_xkusage;
982 
983 	return UINT32_MAX;
984 }
985