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