Debian Codegraph @

Cryptlib/OpenSSL/crypto/x509/x509_trs.c

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