• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
2  * project 1999. */
3 /* ====================================================================
4  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. All advertising materials mentioning features or use of this
19  *    software must display the following acknowledgment:
20  *    "This product includes software developed by the OpenSSL Project
21  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
22  *
23  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24  *    endorse or promote products derived from this software without
25  *    prior written permission. For written permission, please contact
26  *    licensing@OpenSSL.org.
27  *
28  * 5. Products derived from this software may not be called "OpenSSL"
29  *    nor may "OpenSSL" appear in their names without prior written
30  *    permission of the OpenSSL Project.
31  *
32  * 6. Redistributions of any form whatsoever must retain the following
33  *    acknowledgment:
34  *    "This product includes software developed by the OpenSSL Project
35  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48  * OF THE POSSIBILITY OF SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This product includes cryptographic software written by Eric Young
52  * (eay@cryptsoft.com).  This product includes software written by Tim
53  * Hudson (tjh@cryptsoft.com). */
54 
55 #include <openssl/buf.h>
56 #include <openssl/err.h>
57 #include <openssl/mem.h>
58 #include <openssl/obj.h>
59 #include <openssl/x509v3.h>
60 
61 
62 static int tr_cmp(const X509_TRUST **a,
63 		const X509_TRUST **b);
64 static void trtable_free(X509_TRUST *p);
65 
66 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags);
67 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags);
68 static int trust_compat(X509_TRUST *trust, X509 *x, int flags);
69 
70 static int obj_trust(int id, X509 *x, int flags);
71 static int (*default_trust)(int id, X509 *x, int flags) = obj_trust;
72 
73 /* WARNING: the following table should be kept in order of trust
74  * and without any gaps so we can just subtract the minimum trust
75  * value to get an index into the table
76  */
77 
78 static X509_TRUST trstandard[] = {
79 {X509_TRUST_COMPAT, 0, trust_compat, (char *) "compatible", 0, NULL},
80 {X509_TRUST_SSL_CLIENT, 0, trust_1oidany, (char *) "SSL Client", NID_client_auth, NULL},
81 {X509_TRUST_SSL_SERVER, 0, trust_1oidany, (char *) "SSL Server", NID_server_auth, NULL},
82 {X509_TRUST_EMAIL, 0, trust_1oidany, (char *) "S/MIME email", NID_email_protect, NULL},
83 {X509_TRUST_OBJECT_SIGN, 0, trust_1oidany, (char *) "Object Signer", NID_code_sign, NULL},
84 {X509_TRUST_OCSP_SIGN, 0, trust_1oid, (char *) "OCSP responder", NID_OCSP_sign, NULL},
85 {X509_TRUST_OCSP_REQUEST, 0, trust_1oid, (char *) "OCSP request", NID_ad_OCSP, NULL},
86 {X509_TRUST_TSA, 0, trust_1oidany, (char *) "TSA server", NID_time_stamp, NULL}
87 };
88 
89 #define X509_TRUST_COUNT	(sizeof(trstandard)/sizeof(X509_TRUST))
90 
91 static STACK_OF(X509_TRUST) *trtable = NULL;
92 
tr_cmp(const X509_TRUST ** a,const X509_TRUST ** b)93 static int tr_cmp(const X509_TRUST **a,
94 		const X509_TRUST **b)
95 {
96 	return (*a)->trust - (*b)->trust;
97 }
98 
X509_TRUST_set_default(int (* trust)(int,X509 *,int))99 int (*X509_TRUST_set_default(int (*trust)(int , X509 *, int)))(int, X509 *, int)
100 {
101 	int (*oldtrust)(int , X509 *, int);
102 	oldtrust = default_trust;
103 	default_trust = trust;
104 	return oldtrust;
105 }
106 
107 
X509_check_trust(X509 * x,int id,int flags)108 int X509_check_trust(X509 *x, int id, int flags)
109 {
110 	X509_TRUST *pt;
111 	int idx;
112 	if(id == -1) return 1;
113 	/* We get this as a default value */
114 	if (id == 0)
115 		{
116 		int rv;
117 		rv = obj_trust(NID_anyExtendedKeyUsage, x, 0);
118 		if (rv != X509_TRUST_UNTRUSTED)
119 			return rv;
120 		return trust_compat(NULL, x, 0);
121 		}
122 	idx = X509_TRUST_get_by_id(id);
123 	if(idx == -1) return default_trust(id, x, flags);
124 	pt = X509_TRUST_get0(idx);
125 	return pt->check_trust(pt, x, flags);
126 }
127 
X509_TRUST_get_count(void)128 int X509_TRUST_get_count(void)
129 {
130 	if(!trtable) return X509_TRUST_COUNT;
131 	return sk_X509_TRUST_num(trtable) + X509_TRUST_COUNT;
132 }
133 
X509_TRUST_get0(int idx)134 X509_TRUST * X509_TRUST_get0(int idx)
135 {
136 	if(idx < 0) return NULL;
137 	if(idx < (int)X509_TRUST_COUNT) return trstandard + idx;
138 	return sk_X509_TRUST_value(trtable, idx - X509_TRUST_COUNT);
139 }
140 
X509_TRUST_get_by_id(int id)141 int X509_TRUST_get_by_id(int id)
142 {
143 	X509_TRUST tmp;
144 	size_t idx;
145 
146 	if((id >= X509_TRUST_MIN) && (id <= X509_TRUST_MAX))
147 				 return id - X509_TRUST_MIN;
148 	tmp.trust = id;
149 	if(!trtable) return -1;
150 	if (!sk_X509_TRUST_find(trtable, &idx, &tmp)) {
151 		return -1;
152 	}
153 	return idx + X509_TRUST_COUNT;
154 }
155 
X509_TRUST_set(int * t,int trust)156 int X509_TRUST_set(int *t, int trust)
157 {
158 	if(X509_TRUST_get_by_id(trust) == -1) {
159 		OPENSSL_PUT_ERROR(X509, X509_TRUST_set, X509_R_INVALID_TRUST);
160 		return 0;
161 	}
162 	*t = trust;
163 	return 1;
164 }
165 
X509_TRUST_add(int id,int flags,int (* ck)(X509_TRUST *,X509 *,int),char * name,int arg1,void * arg2)166 int X509_TRUST_add(int id, int flags, int (*ck)(X509_TRUST *, X509 *, int),
167 					char *name, int arg1, void *arg2)
168 {
169 	int idx;
170 	X509_TRUST *trtmp;
171 	/* This is set according to what we change: application can't set it */
172 	flags &= ~X509_TRUST_DYNAMIC;
173 	/* This will always be set for application modified trust entries */
174 	flags |= X509_TRUST_DYNAMIC_NAME;
175 	/* Get existing entry if any */
176 	idx = X509_TRUST_get_by_id(id);
177 	/* Need a new entry */
178 	if(idx == -1) {
179 		if(!(trtmp = OPENSSL_malloc(sizeof(X509_TRUST)))) {
180 			OPENSSL_PUT_ERROR(X509, X509_TRUST_add, ERR_R_MALLOC_FAILURE);
181 			return 0;
182 		}
183 		trtmp->flags = X509_TRUST_DYNAMIC;
184 	} else trtmp = X509_TRUST_get0(idx);
185 
186 	/* OPENSSL_free existing name if dynamic */
187 	if(trtmp->flags & X509_TRUST_DYNAMIC_NAME) OPENSSL_free(trtmp->name);
188 	/* dup supplied name */
189 	if(!(trtmp->name = BUF_strdup(name))) {
190 		OPENSSL_PUT_ERROR(X509, X509_TRUST_add, ERR_R_MALLOC_FAILURE);
191 		return 0;
192 	}
193 	/* Keep the dynamic flag of existing entry */
194 	trtmp->flags &= X509_TRUST_DYNAMIC;
195 	/* Set all other flags */
196 	trtmp->flags |= flags;
197 
198 	trtmp->trust = id;
199 	trtmp->check_trust = ck;
200 	trtmp->arg1 = arg1;
201 	trtmp->arg2 = arg2;
202 
203 	/* If its a new entry manage the dynamic table */
204 	if(idx == -1) {
205 		if(!trtable && !(trtable = sk_X509_TRUST_new(tr_cmp))) {
206 			OPENSSL_PUT_ERROR(X509, X509_TRUST_add, ERR_R_MALLOC_FAILURE);
207 			return 0;
208 		}
209 		if (!sk_X509_TRUST_push(trtable, trtmp)) {
210 			OPENSSL_PUT_ERROR(X509, X509_TRUST_add, ERR_R_MALLOC_FAILURE);
211 			return 0;
212 		}
213 	}
214 	return 1;
215 }
216 
trtable_free(X509_TRUST * p)217 static void trtable_free(X509_TRUST *p)
218 	{
219 	if(!p) return;
220 	if (p->flags & X509_TRUST_DYNAMIC)
221 		{
222 		if (p->flags & X509_TRUST_DYNAMIC_NAME)
223 			OPENSSL_free(p->name);
224 		OPENSSL_free(p);
225 		}
226 	}
227 
X509_TRUST_cleanup(void)228 void X509_TRUST_cleanup(void)
229 {
230 	unsigned int i;
231 	for(i = 0; i < X509_TRUST_COUNT; i++) trtable_free(trstandard + i);
232 	sk_X509_TRUST_pop_free(trtable, trtable_free);
233 	trtable = NULL;
234 }
235 
X509_TRUST_get_flags(X509_TRUST * xp)236 int X509_TRUST_get_flags(X509_TRUST *xp)
237 {
238 	return xp->flags;
239 }
240 
X509_TRUST_get0_name(X509_TRUST * xp)241 char *X509_TRUST_get0_name(X509_TRUST *xp)
242 {
243 	return xp->name;
244 }
245 
X509_TRUST_get_trust(X509_TRUST * xp)246 int X509_TRUST_get_trust(X509_TRUST *xp)
247 {
248 	return xp->trust;
249 }
250 
trust_1oidany(X509_TRUST * trust,X509 * x,int flags)251 static int trust_1oidany(X509_TRUST *trust, X509 *x, int flags)
252 {
253 	if(x->aux && (x->aux->trust || x->aux->reject))
254 		return obj_trust(trust->arg1, x, flags);
255 	/* we don't have any trust settings: for compatibility
256 	 * we return trusted if it is self signed
257 	 */
258 	return trust_compat(trust, x, flags);
259 }
260 
trust_1oid(X509_TRUST * trust,X509 * x,int flags)261 static int trust_1oid(X509_TRUST *trust, X509 *x, int flags)
262 {
263 	if(x->aux) return obj_trust(trust->arg1, x, flags);
264 	return X509_TRUST_UNTRUSTED;
265 }
266 
trust_compat(X509_TRUST * trust,X509 * x,int flags)267 static int trust_compat(X509_TRUST *trust, X509 *x, int flags)
268 {
269 	X509_check_purpose(x, -1, 0);
270 	if(x->ex_flags & EXFLAG_SS) return X509_TRUST_TRUSTED;
271 	else return X509_TRUST_UNTRUSTED;
272 }
273 
obj_trust(int id,X509 * x,int flags)274 static int obj_trust(int id, X509 *x, int flags)
275 {
276 	ASN1_OBJECT *obj;
277 	size_t i;
278 	X509_CERT_AUX *ax;
279 	ax = x->aux;
280 	if(!ax) return X509_TRUST_UNTRUSTED;
281 	if(ax->reject) {
282 		for(i = 0; i < sk_ASN1_OBJECT_num(ax->reject); i++) {
283 			obj = sk_ASN1_OBJECT_value(ax->reject, i);
284 			if(OBJ_obj2nid(obj) == id) return X509_TRUST_REJECTED;
285 		}
286 	}
287 	if(ax->trust) {
288 		for(i = 0; i < sk_ASN1_OBJECT_num(ax->trust); i++) {
289 			obj = sk_ASN1_OBJECT_value(ax->trust, i);
290 			if(OBJ_obj2nid(obj) == id) return X509_TRUST_TRUSTED;
291 		}
292 	}
293 	return X509_TRUST_UNTRUSTED;
294 }
295 
296