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