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