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