]>
Commit | Line | Data |
---|---|---|
b6d1e52d | 1 | /* ==================================================================== |
3a8a0a39 | 2 | * Copyright (c) 2001 The OpenSSL Project. All rights reserved. |
b6d1e52d GT |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without | |
5 | * modification, are permitted provided that the following conditions | |
6 | * are met: | |
7 | * | |
8 | * 1. Redistributions of source code must retain the above copyright | |
0f113f3e | 9 | * notice, this list of conditions and the following disclaimer. |
b6d1e52d GT |
10 | * |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in | |
13 | * the documentation and/or other materials provided with the | |
14 | * distribution. | |
15 | * | |
16 | * 3. All advertising materials mentioning features or use of this | |
17 | * software must display the following acknowledgment: | |
18 | * "This product includes software developed by the OpenSSL Project | |
19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | |
20 | * | |
21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
22 | * endorse or promote products derived from this software without | |
23 | * prior written permission. For written permission, please contact | |
24 | * licensing@OpenSSL.org. | |
25 | * | |
26 | * 5. Products derived from this software may not be called "OpenSSL" | |
27 | * nor may "OpenSSL" appear in their names without prior written | |
28 | * permission of the OpenSSL Project. | |
29 | * | |
30 | * 6. Redistributions of any form whatsoever must retain the following | |
31 | * acknowledgment: | |
32 | * "This product includes software developed by the OpenSSL Project | |
33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | |
34 | * | |
35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
46 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
47 | * ==================================================================== | |
48 | * | |
49 | * This product includes cryptographic software written by Eric Young | |
50 | * (eay@cryptsoft.com). This product includes software written by Tim | |
51 | * Hudson (tjh@cryptsoft.com). | |
52 | * | |
53 | */ | |
54 | ||
b39fc560 | 55 | #include "internal/cryptlib.h" |
b6d1e52d | 56 | #include <openssl/evp.h> |
3a87a9b9 | 57 | #include <openssl/lhash.h> |
b6d1e52d GT |
58 | #include "eng_int.h" |
59 | ||
b6d1e52d | 60 | /* The type of the items in the table */ |
e6b5c341 | 61 | struct st_engine_pile { |
0f113f3e MC |
62 | /* The 'nid' of this algorithm/mode */ |
63 | int nid; | |
64 | /* ENGINEs that implement this algorithm/mode. */ | |
65 | STACK_OF(ENGINE) *sk; | |
66 | /* The default ENGINE to perform this algorithm/mode. */ | |
67 | ENGINE *funct; | |
68 | /* | |
69 | * Zero if 'sk' is newer than the cached 'funct', non-zero otherwise | |
70 | */ | |
71 | int uptodate; | |
e6b5c341 | 72 | }; |
3c1d6bbc | 73 | |
340f5856 | 74 | /* The type exposed in eng_int.h */ |
0f113f3e MC |
75 | struct st_engine_table { |
76 | LHASH_OF(ENGINE_PILE) piles; | |
77 | }; /* ENGINE_TABLE */ | |
b6d1e52d | 78 | |
0f113f3e MC |
79 | typedef struct st_engine_pile_doall { |
80 | engine_table_doall_cb *cb; | |
81 | void *arg; | |
82 | } ENGINE_PILE_DOALL; | |
2f0550c4 | 83 | |
340f5856 | 84 | /* Global flags (ENGINE_TABLE_FLAG_***). */ |
b6d1e52d GT |
85 | static unsigned int table_flags = 0; |
86 | ||
87 | /* API function manipulating 'table_flags' */ | |
88 | unsigned int ENGINE_get_table_flags(void) | |
0f113f3e MC |
89 | { |
90 | return table_flags; | |
91 | } | |
3c1d6bbc | 92 | |
b6d1e52d | 93 | void ENGINE_set_table_flags(unsigned int flags) |
0f113f3e MC |
94 | { |
95 | table_flags = flags; | |
96 | } | |
b6d1e52d GT |
97 | |
98 | /* Internal functions for the "piles" hash table */ | |
99 | static unsigned long engine_pile_hash(const ENGINE_PILE *c) | |
0f113f3e MC |
100 | { |
101 | return c->nid; | |
102 | } | |
3c1d6bbc | 103 | |
b6d1e52d | 104 | static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b) |
0f113f3e MC |
105 | { |
106 | return a->nid - b->nid; | |
107 | } | |
108 | ||
b6d1e52d | 109 | static int int_table_check(ENGINE_TABLE **t, int create) |
0f113f3e MC |
110 | { |
111 | LHASH_OF(ENGINE_PILE) *lh; | |
3c1d6bbc | 112 | |
0f113f3e MC |
113 | if (*t) |
114 | return 1; | |
115 | if (!create) | |
116 | return 0; | |
62d0577e | 117 | if ((lh = lh_ENGINE_PILE_new(engine_pile_hash, engine_pile_cmp)) == NULL) |
0f113f3e MC |
118 | return 0; |
119 | *t = (ENGINE_TABLE *)lh; | |
120 | return 1; | |
121 | } | |
b6d1e52d | 122 | |
0f113f3e MC |
123 | /* |
124 | * Privately exposed (via eng_int.h) functions for adding and/or removing | |
125 | * ENGINEs from the implementation table | |
126 | */ | |
752f2b67 | 127 | int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup, |
0f113f3e MC |
128 | ENGINE *e, const int *nids, int num_nids, |
129 | int setdefault) | |
130 | { | |
131 | int ret = 0, added = 0; | |
132 | ENGINE_PILE tmplate, *fnd; | |
133 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); | |
134 | if (!(*table)) | |
135 | added = 1; | |
136 | if (!int_table_check(table, 1)) | |
137 | goto end; | |
138 | if (added) | |
139 | /* The cleanup callback needs to be added */ | |
140 | engine_cleanup_add_first(cleanup); | |
141 | while (num_nids--) { | |
142 | tmplate.nid = *nids; | |
143 | fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate); | |
144 | if (!fnd) { | |
b4faea50 | 145 | fnd = OPENSSL_malloc(sizeof(*fnd)); |
90945fa3 | 146 | if (fnd == NULL) |
0f113f3e MC |
147 | goto end; |
148 | fnd->uptodate = 1; | |
149 | fnd->nid = *nids; | |
150 | fnd->sk = sk_ENGINE_new_null(); | |
151 | if (!fnd->sk) { | |
152 | OPENSSL_free(fnd); | |
153 | goto end; | |
154 | } | |
155 | fnd->funct = NULL; | |
156 | (void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd); | |
157 | } | |
158 | /* A registration shouldn't add duplciate entries */ | |
159 | (void)sk_ENGINE_delete_ptr(fnd->sk, e); | |
160 | /* | |
161 | * if 'setdefault', this ENGINE goes to the head of the list | |
162 | */ | |
163 | if (!sk_ENGINE_push(fnd->sk, e)) | |
164 | goto end; | |
165 | /* "touch" this ENGINE_PILE */ | |
166 | fnd->uptodate = 0; | |
167 | if (setdefault) { | |
168 | if (!engine_unlocked_init(e)) { | |
169 | ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER, | |
170 | ENGINE_R_INIT_FAILED); | |
171 | goto end; | |
172 | } | |
173 | if (fnd->funct) | |
174 | engine_unlocked_finish(fnd->funct, 0); | |
175 | fnd->funct = e; | |
176 | fnd->uptodate = 1; | |
177 | } | |
178 | nids++; | |
179 | } | |
180 | ret = 1; | |
181 | end: | |
182 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); | |
183 | return ret; | |
184 | } | |
185 | ||
2a056de8 | 186 | static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e) |
0f113f3e MC |
187 | { |
188 | int n; | |
189 | /* Iterate the 'c->sk' stack removing any occurrence of 'e' */ | |
190 | while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) { | |
191 | (void)sk_ENGINE_delete(pile->sk, n); | |
192 | pile->uptodate = 0; | |
193 | } | |
194 | if (pile->funct == e) { | |
195 | engine_unlocked_finish(e, 0); | |
196 | pile->funct = NULL; | |
197 | } | |
198 | } | |
199 | ||
2a056de8 | 200 | IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE); |
3c1d6bbc | 201 | |
b6d1e52d | 202 | void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e) |
0f113f3e MC |
203 | { |
204 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); | |
205 | if (int_table_check(table, 0)) | |
2a056de8 | 206 | lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e); |
0f113f3e MC |
207 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); |
208 | } | |
b6d1e52d | 209 | |
3c1d6bbc | 210 | static void int_cleanup_cb_doall(ENGINE_PILE *p) |
0f113f3e | 211 | { |
efa7dd64 RS |
212 | if (!p) |
213 | return; | |
0f113f3e MC |
214 | sk_ENGINE_free(p->sk); |
215 | if (p->funct) | |
216 | engine_unlocked_finish(p->funct, 0); | |
217 | OPENSSL_free(p); | |
218 | } | |
219 | ||
b6d1e52d | 220 | void engine_table_cleanup(ENGINE_TABLE **table) |
0f113f3e MC |
221 | { |
222 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); | |
223 | if (*table) { | |
63c75cd6 | 224 | lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall); |
0f113f3e MC |
225 | lh_ENGINE_PILE_free(&(*table)->piles); |
226 | *table = NULL; | |
227 | } | |
228 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); | |
229 | } | |
b6d1e52d | 230 | |
340f5856 | 231 | /* return a functional reference for a given 'nid' */ |
b6d1e52d GT |
232 | #ifndef ENGINE_TABLE_DEBUG |
233 | ENGINE *engine_table_select(ENGINE_TABLE **table, int nid) | |
234 | #else | |
0f113f3e MC |
235 | ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f, |
236 | int l) | |
b6d1e52d | 237 | #endif |
0f113f3e MC |
238 | { |
239 | ENGINE *ret = NULL; | |
240 | ENGINE_PILE tmplate, *fnd = NULL; | |
241 | int initres, loop = 0; | |
b6d1e52d | 242 | |
0f113f3e | 243 | if (!(*table)) { |
b6d1e52d | 244 | #ifdef ENGINE_TABLE_DEBUG |
0f113f3e MC |
245 | fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing " |
246 | "registered!\n", f, l, nid); | |
b6d1e52d | 247 | #endif |
0f113f3e MC |
248 | return NULL; |
249 | } | |
250 | ERR_set_mark(); | |
251 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); | |
252 | /* | |
253 | * Check again inside the lock otherwise we could race against cleanup | |
254 | * operations. But don't worry about a fprintf(stderr). | |
255 | */ | |
256 | if (!int_table_check(table, 0)) | |
257 | goto end; | |
258 | tmplate.nid = nid; | |
259 | fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate); | |
260 | if (!fnd) | |
261 | goto end; | |
262 | if (fnd->funct && engine_unlocked_init(fnd->funct)) { | |
b6d1e52d | 263 | #ifdef ENGINE_TABLE_DEBUG |
0f113f3e MC |
264 | fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using " |
265 | "ENGINE '%s' cached\n", f, l, nid, fnd->funct->id); | |
b6d1e52d | 266 | #endif |
0f113f3e MC |
267 | ret = fnd->funct; |
268 | goto end; | |
269 | } | |
270 | if (fnd->uptodate) { | |
271 | ret = fnd->funct; | |
272 | goto end; | |
273 | } | |
274 | trynext: | |
275 | ret = sk_ENGINE_value(fnd->sk, loop++); | |
276 | if (!ret) { | |
b6d1e52d | 277 | #ifdef ENGINE_TABLE_DEBUG |
0f113f3e MC |
278 | fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no " |
279 | "registered implementations would initialise\n", f, l, nid); | |
b6d1e52d | 280 | #endif |
0f113f3e MC |
281 | goto end; |
282 | } | |
283 | /* Try to initialise the ENGINE? */ | |
284 | if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT)) | |
285 | initres = engine_unlocked_init(ret); | |
286 | else | |
287 | initres = 0; | |
288 | if (initres) { | |
289 | /* Update 'funct' */ | |
290 | if ((fnd->funct != ret) && engine_unlocked_init(ret)) { | |
291 | /* If there was a previous default we release it. */ | |
292 | if (fnd->funct) | |
293 | engine_unlocked_finish(fnd->funct, 0); | |
294 | fnd->funct = ret; | |
b6d1e52d | 295 | #ifdef ENGINE_TABLE_DEBUG |
0f113f3e MC |
296 | fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, " |
297 | "setting default to '%s'\n", f, l, nid, ret->id); | |
b6d1e52d | 298 | #endif |
0f113f3e | 299 | } |
b6d1e52d | 300 | #ifdef ENGINE_TABLE_DEBUG |
0f113f3e MC |
301 | fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using " |
302 | "newly initialised '%s'\n", f, l, nid, ret->id); | |
b6d1e52d | 303 | #endif |
0f113f3e MC |
304 | goto end; |
305 | } | |
306 | goto trynext; | |
307 | end: | |
308 | /* | |
309 | * If it failed, it is unlikely to succeed again until some future | |
310 | * registrations have taken place. In all cases, we cache. | |
311 | */ | |
312 | if (fnd) | |
313 | fnd->uptodate = 1; | |
b6d1e52d | 314 | #ifdef ENGINE_TABLE_DEBUG |
0f113f3e MC |
315 | if (ret) |
316 | fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching " | |
317 | "ENGINE '%s'\n", f, l, nid, ret->id); | |
318 | else | |
319 | fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching " | |
320 | "'no matching ENGINE'\n", f, l, nid); | |
b6d1e52d | 321 | #endif |
0f113f3e MC |
322 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); |
323 | /* | |
324 | * Whatever happened, any failed init()s are not failures in this | |
325 | * context, so clear our error state. | |
326 | */ | |
327 | ERR_pop_to_mark(); | |
328 | return ret; | |
329 | } | |
2f0550c4 DSH |
330 | |
331 | /* Table enumeration */ | |
332 | ||
2a056de8 | 333 | static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall) |
0f113f3e MC |
334 | { |
335 | dall->cb(pile->nid, pile->sk, pile->funct, dall->arg); | |
336 | } | |
337 | ||
2a056de8 | 338 | IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL); |
2f0550c4 | 339 | |
2f0550c4 | 340 | void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb, |
0f113f3e MC |
341 | void *arg) |
342 | { | |
343 | ENGINE_PILE_DOALL dall; | |
344 | dall.cb = cb; | |
345 | dall.arg = arg; | |
346 | if (table) | |
2a056de8 | 347 | lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall); |
0f113f3e | 348 | } |