x509_trs.c revision 1.36
1/* $OpenBSD: x509_trs.c,v 1.36 2024/01/10 21:11:37 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 1999.
4 */
5/* ====================================================================
6 * Copyright (c) 1999 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/err.h>
63#include <openssl/x509v3.h>
64
65#include "x509_local.h"
66
67static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags);
68static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
69static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
70
71static int
72obj_trust(int id, X509 *x, int flags)
73{
74	ASN1_OBJECT *obj;
75	int i, nid;
76	X509_CERT_AUX *ax;
77
78	ax = x->aux;
79	if (!ax)
80		return X509_TRUST_UNTRUSTED;
81	if (ax->reject) {
82		for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
83			obj = sk_ASN1_OBJECT_value(ax->reject, i);
84			nid = OBJ_obj2nid(obj);
85			if (nid == id || nid == NID_anyExtendedKeyUsage)
86				return X509_TRUST_REJECTED;
87		}
88	}
89	if (ax->trust) {
90		for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
91			obj = sk_ASN1_OBJECT_value(ax->trust, i);
92			nid = OBJ_obj2nid(obj);
93			if (nid == id || nid == NID_anyExtendedKeyUsage)
94				return X509_TRUST_TRUSTED;
95		}
96	}
97	return X509_TRUST_UNTRUSTED;
98}
99
100/* WARNING: the following table should be kept in order of trust
101 * and without any gaps so we can just subtract the minimum trust
102 * value to get an index into the table
103 */
104
105static X509_TRUST trstandard[] = {
106	{
107		.trust = X509_TRUST_COMPAT,
108		.check_trust = trust_compat,
109		.name = "compatible",
110	},
111	{
112		.trust = X509_TRUST_SSL_CLIENT,
113		.check_trust = trust_1oidany,
114		.name = "SSL Client",
115		.arg1 = NID_client_auth,
116	},
117	{
118		.trust = X509_TRUST_SSL_SERVER,
119		.check_trust = trust_1oidany,
120		.name = "SSL Server",
121		.arg1 = NID_server_auth,
122	},
123	{
124		.trust = X509_TRUST_EMAIL,
125		.check_trust = trust_1oidany,
126		.name = "S/MIME email",
127		.arg1 = NID_email_protect,
128	},
129	{
130		.trust = X509_TRUST_OBJECT_SIGN,
131		.check_trust = trust_1oidany,
132		.name = "Object Signer",
133		.arg1 = NID_code_sign,
134	},
135	{
136		.trust = X509_TRUST_OCSP_SIGN,
137		.check_trust = trust_1oid,
138		.name = "OCSP responder",
139		.arg1 = NID_OCSP_sign,
140	},
141	{
142		.trust = X509_TRUST_OCSP_REQUEST,
143		.check_trust = trust_1oid,
144		.name = "OCSP request",
145		.arg1 = NID_ad_OCSP,
146	},
147	{
148		.trust = X509_TRUST_TSA,
149		.check_trust = trust_1oidany,
150		.name = "TSA server",
151		.arg1 = NID_time_stamp,
152	},
153};
154
155#define X509_TRUST_COUNT	(sizeof(trstandard) / sizeof(trstandard[0]))
156
157static int (*default_trust)(int id, X509 *x, int flags) = obj_trust;
158
159int
160(*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int)
161{
162	int (*oldtrust)(int , X509 *, int);
163
164	oldtrust = default_trust;
165	default_trust = trust;
166	return oldtrust;
167}
168LCRYPTO_ALIAS(X509_TRUST_set_default);
169
170int
171X509_check_trust(X509 *x, int id, int flags)
172{
173	X509_TRUST *pt;
174	int idx;
175
176	if (id == -1)
177		return 1;
178	/*
179	 * XXX beck/jsing This enables self signed certs to be trusted for
180	 * an unspecified id/trust flag value (this is NOT the
181	 * X509_TRUST_DEFAULT), which was the longstanding
182	 * openssl behaviour. boringssl does not have this behaviour.
183	 *
184	 * This should be revisited, but changing the default "not default"
185	 * may break things.
186	 */
187	if (id == 0) {
188		int rv;
189		rv = obj_trust(NID_anyExtendedKeyUsage, x, 0);
190		if (rv != X509_TRUST_UNTRUSTED)
191			return rv;
192		return trust_compat(NULL, x, 0);
193	}
194	idx = X509_TRUST_get_by_id(id);
195	if (idx == -1)
196		return default_trust(id, x, flags);
197	pt = X509_TRUST_get0(idx);
198	return pt->check_trust(pt, x, flags);
199}
200LCRYPTO_ALIAS(X509_check_trust);
201
202int
203X509_TRUST_get_count(void)
204{
205	return X509_TRUST_COUNT;
206}
207LCRYPTO_ALIAS(X509_TRUST_get_count);
208
209X509_TRUST *
210X509_TRUST_get0(int idx)
211{
212	if (idx < 0 || (size_t)idx >= X509_TRUST_COUNT)
213		return NULL;
214
215	return &trstandard[idx];
216}
217LCRYPTO_ALIAS(X509_TRUST_get0);
218
219int
220X509_TRUST_get_by_id(int id)
221{
222	/*
223	 * Ensure the trust identifier is between MIN and MAX inclusive.
224	 * If so, translate it into an index into the trstandard[] table.
225	 */
226	if (id < X509_TRUST_MIN || id > X509_TRUST_MAX)
227		return -1;
228
229	return id - X509_TRUST_MIN;
230}
231LCRYPTO_ALIAS(X509_TRUST_get_by_id);
232
233int
234X509_TRUST_set(int *t, int trust)
235{
236	if (X509_TRUST_get_by_id(trust) == -1) {
237		X509error(X509_R_INVALID_TRUST);
238		return 0;
239	}
240	*t = trust;
241	return 1;
242}
243LCRYPTO_ALIAS(X509_TRUST_set);
244
245int
246X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int),
247    const char *name, int arg1, void *arg2)
248{
249	X509error(ERR_R_DISABLED);
250	return 0;
251}
252LCRYPTO_ALIAS(X509_TRUST_add);
253
254void
255X509_TRUST_cleanup(void)
256{
257}
258LCRYPTO_ALIAS(X509_TRUST_cleanup);
259
260int
261X509_TRUST_get_flags(const X509_TRUST *xp)
262{
263	return xp->flags;
264}
265LCRYPTO_ALIAS(X509_TRUST_get_flags);
266
267char *
268X509_TRUST_get0_name(const X509_TRUST *xp)
269{
270	return xp->name;
271}
272LCRYPTO_ALIAS(X509_TRUST_get0_name);
273
274int
275X509_TRUST_get_trust(const X509_TRUST *xp)
276{
277	return xp->trust;
278}
279LCRYPTO_ALIAS(X509_TRUST_get_trust);
280
281static int
282trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
283{
284	if (x->aux && (x->aux->trust || x->aux->reject))
285		return obj_trust(trust->arg1, x, flags);
286	/* we don't have any trust settings: for compatibility
287	 * we return trusted if it is self signed
288	 */
289	return trust_compat(trust, x, flags);
290}
291
292static int
293trust_1oid(X509_TRUST *trust, X509 *x, int flags)
294{
295	if (x->aux)
296		return obj_trust(trust->arg1, x, flags);
297	return X509_TRUST_UNTRUSTED;
298}
299
300static int
301trust_compat(X509_TRUST *trust, X509 *x, int flags)
302{
303	X509_check_purpose(x, -1, 0);
304	if (x->ex_flags & EXFLAG_SS)
305		return X509_TRUST_TRUSTED;
306	else
307		return X509_TRUST_UNTRUSTED;
308}
309