1 /* $OpenBSD: x509_trs.c,v 1.25 2021/11/01 20:53:08 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_lcl.h"
66 
67 static int tr_cmp(const X509_TRUST * const *a, const X509_TRUST * const *b);
68 static void trtable_free(X509_TRUST *p);
69 
70 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags);
71 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
72 static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
73 
74 static int obj_trust(int id, X509 *x, int flags);
75 static int (*default_trust)(int id, X509 *x, int flags) = obj_trust;
76 
77 /* WARNING: the following table should be kept in order of trust
78  * and without any gaps so we can just subtract the minimum trust
79  * value to get an index into the table
80  */
81 
82 static X509_TRUST trstandard[] = {
83 	{X509_TRUST_COMPAT, 0, trust_compat, "compatible", 0, NULL},
84 	{X509_TRUST_SSL_CLIENT, 0, trust_1oidany, "SSL Client", NID_client_auth, NULL},
85 	{X509_TRUST_SSL_SERVER, 0, trust_1oidany, "SSL Server", NID_server_auth, NULL},
86 	{X509_TRUST_EMAIL, 0, trust_1oidany, "S/MIME email", NID_email_protect, NULL},
87 	{X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, "Object Signer", NID_code_sign, NULL},
88 	{X509_TRUST_OCSP_SIGN, 0, trust_1oid, "OCSP responder", NID_OCSP_sign, NULL},
89 	{X509_TRUST_OCSP_REQUEST, 0, trust_1oid, "OCSP request", NID_ad_OCSP, NULL},
90 	{X509_TRUST_TSA, 0, trust_1oidany, "TSA server", NID_time_stamp, NULL}
91 };
92 
93 #define X509_TRUST_COUNT	(sizeof(trstandard)/sizeof(X509_TRUST))
94 
95 static STACK_OF(X509_TRUST) *trtable = NULL;
96 
97 static int
98 tr_cmp(const X509_TRUST * const *a, const X509_TRUST * const *b)
99 {
100 	return (*a)->trust - (*b)->trust;
101 }
102 
103 int
104 (*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int)
105 {
106 	int (*oldtrust)(int , X509 *, int);
107 
108 	oldtrust = default_trust;
109 	default_trust = trust;
110 	return oldtrust;
111 }
112 
113 int
114 X509_check_trust(X509 *x, int id, int flags)
115 {
116 	X509_TRUST *pt;
117 	int idx;
118 
119 	if (id == -1)
120 		return 1;
121 	/*
122 	 * XXX beck/jsing This enables self signed certs to be trusted for
123 	 * an unspecified id/trust flag value (this is NOT the
124 	 * X509_TRUST_DEFAULT), which was the longstanding
125 	 * openssl behaviour. boringssl does not have this behaviour.
126 	 *
127 	 * This should be revisited, but changing the default "not default"
128 	 * may break things.
129 	 */
130 	if (id == 0) {
131 		int rv;
132 		rv = obj_trust(NID_anyExtendedKeyUsage, x, 0);
133 		if (rv != X509_TRUST_UNTRUSTED)
134 			return rv;
135 		return trust_compat(NULL, x, 0);
136 	}
137 	idx = X509_TRUST_get_by_id(id);
138 	if (idx == -1)
139 		return default_trust(id, x, flags);
140 	pt = X509_TRUST_get0(idx);
141 	return pt->check_trust(pt, x, flags);
142 }
143 
144 int
145 X509_TRUST_get_count(void)
146 {
147 	if (!trtable)
148 		return X509_TRUST_COUNT;
149 	return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
150 }
151 
152 X509_TRUST *
153 X509_TRUST_get0(int idx)
154 {
155 	if (idx < 0)
156 		return NULL;
157 	if (idx < (int)X509_TRUST_COUNT)
158 		return trstandard + idx;
159 	return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
160 }
161 
162 int
163 X509_TRUST_get_by_id(int id)
164 {
165 	X509_TRUST tmp;
166 	int idx;
167 
168 	if ((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
169 		return id - X509_TRUST_MIN;
170 	tmp.trust = id;
171 	if (!trtable)
172 		return -1;
173 	idx = sk_X509_TRUST_find(trtable, &tmp);
174 	if (idx == -1)
175 		return -1;
176 	return idx + X509_TRUST_COUNT;
177 }
178 
179 int
180 X509_TRUST_set(int *t, int trust)
181 {
182 	if (X509_TRUST_get_by_id(trust) == -1) {
183 		X509error(X509_R_INVALID_TRUST);
184 		return 0;
185 	}
186 	*t = trust;
187 	return 1;
188 }
189 
190 int
191 X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int),
192     const char *name, int arg1, void *arg2)
193 {
194 	int idx;
195 	X509_TRUST *trtmp;
196 	char *name_dup;
197 
198 	/* This is set according to what we change: application can't set it */
199 	flags &= ~X509_TRUST_DYNAMIC;
200 	/* This will always be set for application modified trust entries */
201 	flags |= X509_TRUST_DYNAMIC_NAME;
202 	/* Get existing entry if any */
203 	idx = X509_TRUST_get_by_id(id);
204 	/* Need a new entry */
205 	if (idx == -1) {
206 		if (!(trtmp = malloc(sizeof(X509_TRUST)))) {
207 			X509error(ERR_R_MALLOC_FAILURE);
208 			return 0;
209 		}
210 		trtmp->flags = X509_TRUST_DYNAMIC;
211 	} else {
212 		trtmp = X509_TRUST_get0(idx);
213 		if (trtmp == NULL) {
214 			X509error(X509_R_INVALID_TRUST);
215 			return 0;
216 		}
217 	}
218 
219 	if ((name_dup = strdup(name)) == NULL)
220 		goto err;
221 
222 	/* free existing name if dynamic */
223 	if (trtmp->flags & X509_TRUST_DYNAMIC_NAME)
224 		free(trtmp->name);
225 	/* dup supplied name */
226 	trtmp->name = name_dup;
227 	/* Keep the dynamic flag of existing entry */
228 	trtmp->flags &= X509_TRUST_DYNAMIC;
229 	/* Set all other flags */
230 	trtmp->flags |= flags;
231 
232 	trtmp->trust = id;
233 	trtmp->check_trust = ck;
234 	trtmp->arg1 = arg1;
235 	trtmp->arg2 = arg2;
236 
237 	/* If it's a new entry, manage the dynamic table */
238 	if (idx == -1) {
239 		if (trtable == NULL &&
240 		    (trtable = sk_X509_TRUST_new(tr_cmp)) == NULL)
241 			goto err;
242 		if (sk_X509_TRUST_push(trtable, trtmp) == 0)
243 			goto err;
244 	}
245 	return 1;
246 
247 err:
248 	free(name_dup);
249 	if (idx == -1)
250 		free(trtmp);
251 	X509error(ERR_R_MALLOC_FAILURE);
252 	return 0;
253 }
254 
255 static void
256 trtable_free(X509_TRUST *p)
257 {
258 	if (!p)
259 		return;
260 	if (p->flags & X509_TRUST_DYNAMIC) {
261 		if (p->flags & X509_TRUST_DYNAMIC_NAME)
262 			free(p->name);
263 		free(p);
264 	}
265 }
266 
267 void
268 X509_TRUST_cleanup(void)
269 {
270 	sk_X509_TRUST_pop_free(trtable, trtable_free);
271 	trtable = NULL;
272 }
273 
274 int
275 X509_TRUST_get_flags(const X509_TRUST *xp)
276 {
277 	return xp->flags;
278 }
279 
280 char *
281 X509_TRUST_get0_name(const X509_TRUST *xp)
282 {
283 	return xp->name;
284 }
285 
286 int
287 X509_TRUST_get_trust(const X509_TRUST *xp)
288 {
289 	return xp->trust;
290 }
291 
292 static int
293 trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
294 {
295 	if (x->aux && (x->aux->trust || x->aux->reject))
296 		return obj_trust(trust->arg1, x, flags);
297 	/* we don't have any trust settings: for compatibility
298 	 * we return trusted if it is self signed
299 	 */
300 	return trust_compat(trust, x, flags);
301 }
302 
303 static int
304 trust_1oid(X509_TRUST *trust, X509 *x, int flags)
305 {
306 	if (x->aux)
307 		return obj_trust(trust->arg1, x, flags);
308 	return X509_TRUST_UNTRUSTED;
309 }
310 
311 static int
312 trust_compat(X509_TRUST *trust, X509 *x, int flags)
313 {
314 	X509_check_purpose(x, -1, 0);
315 	if (x->ex_flags & EXFLAG_SS)
316 		return X509_TRUST_TRUSTED;
317 	else
318 		return X509_TRUST_UNTRUSTED;
319 }
320 
321 static int
322 obj_trust(int id, X509 *x, int flags)
323 {
324 	ASN1_OBJECT *obj;
325 	int i;
326 	X509_CERT_AUX *ax;
327 
328 	ax = x->aux;
329 	if (!ax)
330 		return X509_TRUST_UNTRUSTED;
331 	if (ax->reject) {
332 		for (i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
333 			obj = sk_ASN1_OBJECT_value(ax->reject, i);
334 			if (OBJ_obj2nid(obj) == id)
335 				return X509_TRUST_REJECTED;
336 		}
337 	}
338 	if (ax->trust) {
339 		for (i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
340 			obj = sk_ASN1_OBJECT_value(ax->trust, i);
341 			if (OBJ_obj2nid(obj) == id)
342 				return X509_TRUST_TRUSTED;
343 		}
344 	}
345 	return X509_TRUST_UNTRUSTED;
346 }
347