]>
Commit | Line | Data |
---|---|---|
0f113f3e MC |
1 | /* |
2 | * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project | |
3 | * 2001. | |
9391f977 GT |
4 | */ |
5 | /* ==================================================================== | |
2b671586 | 6 | * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. |
9391f977 GT |
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 | |
0f113f3e | 13 | * notice, this list of conditions and the following disclaimer. |
9391f977 GT |
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 | */ | |
58 | ||
14cfde9c | 59 | #include "eng_int.h" |
9391f977 | 60 | #include <openssl/dso.h> |
7b9f8f7f | 61 | #include <openssl/crypto.h> |
9391f977 | 62 | |
0f113f3e MC |
63 | /* |
64 | * Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE | |
65 | * loader should implement the hook-up functions with the following | |
66 | * prototypes. | |
67 | */ | |
9391f977 GT |
68 | |
69 | /* Our ENGINE handlers */ | |
70 | static int dynamic_init(ENGINE *e); | |
71 | static int dynamic_finish(ENGINE *e); | |
0f113f3e MC |
72 | static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, |
73 | void (*f) (void)); | |
9391f977 GT |
74 | /* Predeclare our context type */ |
75 | typedef struct st_dynamic_data_ctx dynamic_data_ctx; | |
76 | /* The implementation for the important control command */ | |
77 | static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx); | |
78 | ||
0f113f3e MC |
79 | #define DYNAMIC_CMD_SO_PATH ENGINE_CMD_BASE |
80 | #define DYNAMIC_CMD_NO_VCHECK (ENGINE_CMD_BASE + 1) | |
81 | #define DYNAMIC_CMD_ID (ENGINE_CMD_BASE + 2) | |
82 | #define DYNAMIC_CMD_LIST_ADD (ENGINE_CMD_BASE + 3) | |
83 | #define DYNAMIC_CMD_DIR_LOAD (ENGINE_CMD_BASE + 4) | |
84 | #define DYNAMIC_CMD_DIR_ADD (ENGINE_CMD_BASE + 5) | |
85 | #define DYNAMIC_CMD_LOAD (ENGINE_CMD_BASE + 6) | |
9391f977 GT |
86 | |
87 | /* The constants used when creating the ENGINE */ | |
88 | static const char *engine_dynamic_id = "dynamic"; | |
89 | static const char *engine_dynamic_name = "Dynamic engine loading support"; | |
90 | static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = { | |
0f113f3e MC |
91 | {DYNAMIC_CMD_SO_PATH, |
92 | "SO_PATH", | |
93 | "Specifies the path to the new ENGINE shared library", | |
94 | ENGINE_CMD_FLAG_STRING}, | |
95 | {DYNAMIC_CMD_NO_VCHECK, | |
96 | "NO_VCHECK", | |
97 | "Specifies to continue even if version checking fails (boolean)", | |
98 | ENGINE_CMD_FLAG_NUMERIC}, | |
99 | {DYNAMIC_CMD_ID, | |
100 | "ID", | |
101 | "Specifies an ENGINE id name for loading", | |
102 | ENGINE_CMD_FLAG_STRING}, | |
103 | {DYNAMIC_CMD_LIST_ADD, | |
104 | "LIST_ADD", | |
105 | "Whether to add a loaded ENGINE to the internal list (0=no,1=yes,2=mandatory)", | |
106 | ENGINE_CMD_FLAG_NUMERIC}, | |
107 | {DYNAMIC_CMD_DIR_LOAD, | |
108 | "DIR_LOAD", | |
109 | "Specifies whether to load from 'DIR_ADD' directories (0=no,1=yes,2=mandatory)", | |
110 | ENGINE_CMD_FLAG_NUMERIC}, | |
111 | {DYNAMIC_CMD_DIR_ADD, | |
112 | "DIR_ADD", | |
113 | "Adds a directory from which ENGINEs can be loaded", | |
114 | ENGINE_CMD_FLAG_STRING}, | |
115 | {DYNAMIC_CMD_LOAD, | |
116 | "LOAD", | |
117 | "Load up the ENGINE specified by other settings", | |
118 | ENGINE_CMD_FLAG_NO_INPUT}, | |
119 | {0, NULL, NULL, 0} | |
120 | }; | |
9391f977 | 121 | |
0f113f3e MC |
122 | /* |
123 | * Loading code stores state inside the ENGINE structure via the "ex_data" | |
9391f977 | 124 | * element. We load all our state into a single structure and use that as a |
0f113f3e MC |
125 | * single context in the "ex_data" stack. |
126 | */ | |
127 | struct st_dynamic_data_ctx { | |
128 | /* The DSO object we load that supplies the ENGINE code */ | |
129 | DSO *dynamic_dso; | |
130 | /* | |
131 | * The function pointer to the version checking shared library function | |
132 | */ | |
133 | dynamic_v_check_fn v_check; | |
134 | /* | |
135 | * The function pointer to the engine-binding shared library function | |
136 | */ | |
137 | dynamic_bind_engine bind_engine; | |
138 | /* The default name/path for loading the shared library */ | |
b548a1f1 | 139 | char *DYNAMIC_LIBNAME; |
0f113f3e MC |
140 | /* Whether to continue loading on a version check failure */ |
141 | int no_vcheck; | |
142 | /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */ | |
b548a1f1 | 143 | char *engine_id; |
0f113f3e MC |
144 | /* |
145 | * If non-zero, a successfully loaded ENGINE should be added to the | |
146 | * internal ENGINE list. If 2, the add must succeed or the entire load | |
147 | * should fail. | |
148 | */ | |
149 | int list_add_value; | |
150 | /* The symbol name for the version checking function */ | |
151 | const char *DYNAMIC_F1; | |
152 | /* The symbol name for the "initialise ENGINE structure" function */ | |
153 | const char *DYNAMIC_F2; | |
154 | /* | |
155 | * Whether to never use 'dirs', use 'dirs' as a fallback, or only use | |
156 | * 'dirs' for loading. Default is to use 'dirs' as a fallback. | |
157 | */ | |
158 | int dir_load; | |
159 | /* A stack of directories from which ENGINEs could be loaded */ | |
160 | STACK_OF(OPENSSL_STRING) *dirs; | |
161 | }; | |
9391f977 | 162 | |
0f113f3e MC |
163 | /* |
164 | * This is the "ex_data" index we obtain and reserve for use with our context | |
165 | * structure. | |
166 | */ | |
9391f977 GT |
167 | static int dynamic_ex_data_idx = -1; |
168 | ||
0f113f3e MC |
169 | static void int_free_str(char *s) |
170 | { | |
171 | OPENSSL_free(s); | |
172 | } | |
173 | ||
174 | /* | |
175 | * Because our ex_data element may or may not get allocated depending on | |
176 | * whether a "first-use" occurs before the ENGINE is freed, we have a memory | |
177 | * leak problem to solve. We can't declare a "new" handler for the ex_data as | |
178 | * we don't want a dynamic_data_ctx in *all* ENGINE structures of all types | |
179 | * (this is a bug in the design of CRYPTO_EX_DATA). As such, we just declare | |
180 | * a "free" handler and that will get called if an ENGINE is being destroyed | |
181 | * and there was an ex_data element corresponding to our context type. | |
182 | */ | |
9391f977 | 183 | static void dynamic_data_ctx_free_func(void *parent, void *ptr, |
0f113f3e MC |
184 | CRYPTO_EX_DATA *ad, int idx, long argl, |
185 | void *argp) | |
186 | { | |
187 | if (ptr) { | |
188 | dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr; | |
efa7dd64 | 189 | DSO_free(ctx->dynamic_dso); |
b548a1f1 RS |
190 | OPENSSL_free(ctx->DYNAMIC_LIBNAME); |
191 | OPENSSL_free(ctx->engine_id); | |
25aaa98a | 192 | sk_OPENSSL_STRING_pop_free(ctx->dirs, int_free_str); |
0f113f3e MC |
193 | OPENSSL_free(ctx); |
194 | } | |
195 | } | |
9391f977 | 196 | |
0f113f3e MC |
197 | /* |
198 | * Construct the per-ENGINE context. We create it blindly and then use a lock | |
199 | * to check for a race - if so, all but one of the threads "racing" will have | |
9391f977 | 200 | * wasted their time. The alternative involves creating everything inside the |
0f113f3e MC |
201 | * lock which is far worse. |
202 | */ | |
9391f977 | 203 | static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx) |
0f113f3e | 204 | { |
b51bce94 | 205 | dynamic_data_ctx *c = OPENSSL_zalloc(sizeof(*c)); |
b4faea50 | 206 | |
90945fa3 | 207 | if (c == NULL) { |
0f113f3e MC |
208 | ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE); |
209 | return 0; | |
210 | } | |
0f113f3e | 211 | c->dirs = sk_OPENSSL_STRING_new_null(); |
90945fa3 | 212 | if (c->dirs == NULL) { |
0f113f3e MC |
213 | ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE); |
214 | OPENSSL_free(c); | |
215 | return 0; | |
216 | } | |
64b25758 RS |
217 | c->DYNAMIC_F1 = "v_check"; |
218 | c->DYNAMIC_F2 = "bind_engine"; | |
219 | c->dir_load = 1; | |
0f113f3e MC |
220 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); |
221 | if ((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, | |
222 | dynamic_ex_data_idx)) | |
223 | == NULL) { | |
224 | /* Good, we're the first */ | |
225 | ENGINE_set_ex_data(e, dynamic_ex_data_idx, c); | |
226 | *ctx = c; | |
227 | c = NULL; | |
228 | } | |
229 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); | |
230 | /* | |
231 | * If we lost the race to set the context, c is non-NULL and *ctx is the | |
232 | * context of the thread that won. | |
233 | */ | |
98637bd3 F |
234 | if (c) |
235 | sk_OPENSSL_STRING_free(c->dirs); | |
b548a1f1 | 236 | OPENSSL_free(c); |
0f113f3e MC |
237 | return 1; |
238 | } | |
9391f977 | 239 | |
0f113f3e MC |
240 | /* |
241 | * This function retrieves the context structure from an ENGINE's "ex_data", | |
242 | * or if it doesn't exist yet, sets it up. | |
243 | */ | |
9391f977 | 244 | static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e) |
0f113f3e MC |
245 | { |
246 | dynamic_data_ctx *ctx; | |
247 | if (dynamic_ex_data_idx < 0) { | |
248 | /* | |
249 | * Create and register the ENGINE ex_data, and associate our "free" | |
250 | * function with it to ensure any allocated contexts get freed when | |
251 | * an ENGINE goes underground. | |
252 | */ | |
253 | int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, | |
254 | dynamic_data_ctx_free_func); | |
255 | if (new_idx == -1) { | |
256 | ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX, ENGINE_R_NO_INDEX); | |
257 | return NULL; | |
258 | } | |
259 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); | |
260 | /* Avoid a race by checking again inside this lock */ | |
261 | if (dynamic_ex_data_idx < 0) { | |
262 | /* Good, someone didn't beat us to it */ | |
263 | dynamic_ex_data_idx = new_idx; | |
264 | new_idx = -1; | |
265 | } | |
266 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); | |
267 | /* | |
268 | * In theory we could "give back" the index here if (new_idx>-1), but | |
269 | * it's not possible and wouldn't gain us much if it were. | |
270 | */ | |
271 | } | |
272 | ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx); | |
273 | /* Check if the context needs to be created */ | |
274 | if ((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx)) | |
275 | /* "set_data" will set errors if necessary */ | |
276 | return NULL; | |
277 | return ctx; | |
278 | } | |
9391f977 | 279 | |
b6d1e52d | 280 | static ENGINE *engine_dynamic(void) |
0f113f3e MC |
281 | { |
282 | ENGINE *ret = ENGINE_new(); | |
90945fa3 | 283 | if (ret == NULL) |
0f113f3e MC |
284 | return NULL; |
285 | if (!ENGINE_set_id(ret, engine_dynamic_id) || | |
286 | !ENGINE_set_name(ret, engine_dynamic_name) || | |
287 | !ENGINE_set_init_function(ret, dynamic_init) || | |
288 | !ENGINE_set_finish_function(ret, dynamic_finish) || | |
289 | !ENGINE_set_ctrl_function(ret, dynamic_ctrl) || | |
290 | !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) || | |
291 | !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) { | |
292 | ENGINE_free(ret); | |
293 | return NULL; | |
294 | } | |
295 | return ret; | |
296 | } | |
9391f977 | 297 | |
7b9f8f7f | 298 | void engine_load_dynamic_internal(void) |
0f113f3e MC |
299 | { |
300 | ENGINE *toadd = engine_dynamic(); | |
301 | if (!toadd) | |
302 | return; | |
303 | ENGINE_add(toadd); | |
304 | /* | |
305 | * If the "add" worked, it gets a structural reference. So either way, we | |
306 | * release our just-created reference. | |
307 | */ | |
308 | ENGINE_free(toadd); | |
309 | /* | |
310 | * If the "add" didn't work, it was probably a conflict because it was | |
311 | * already added (eg. someone calling ENGINE_load_blah then calling | |
312 | * ENGINE_load_builtin_engines() perhaps). | |
313 | */ | |
314 | ERR_clear_error(); | |
315 | } | |
b6d1e52d | 316 | |
9391f977 | 317 | static int dynamic_init(ENGINE *e) |
0f113f3e MC |
318 | { |
319 | /* | |
0d4fb843 | 320 | * We always return failure - the "dynamic" engine itself can't be used |
0f113f3e MC |
321 | * for anything. |
322 | */ | |
323 | return 0; | |
324 | } | |
9391f977 GT |
325 | |
326 | static int dynamic_finish(ENGINE *e) | |
0f113f3e MC |
327 | { |
328 | /* | |
329 | * This should never be called on account of "dynamic_init" always | |
330 | * failing. | |
331 | */ | |
332 | return 0; | |
333 | } | |
334 | ||
335 | static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) | |
336 | { | |
337 | dynamic_data_ctx *ctx = dynamic_get_data_ctx(e); | |
338 | int initialised; | |
9391f977 | 339 | |
0f113f3e MC |
340 | if (!ctx) { |
341 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_NOT_LOADED); | |
342 | return 0; | |
343 | } | |
344 | initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1); | |
345 | /* All our control commands require the ENGINE to be uninitialised */ | |
346 | if (initialised) { | |
347 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_ALREADY_LOADED); | |
348 | return 0; | |
349 | } | |
350 | switch (cmd) { | |
351 | case DYNAMIC_CMD_SO_PATH: | |
352 | /* a NULL 'p' or a string of zero-length is the same thing */ | |
353 | if (p && (strlen((const char *)p) < 1)) | |
354 | p = NULL; | |
b548a1f1 | 355 | OPENSSL_free(ctx->DYNAMIC_LIBNAME); |
0f113f3e | 356 | if (p) |
7644a9ae | 357 | ctx->DYNAMIC_LIBNAME = OPENSSL_strdup(p); |
0f113f3e MC |
358 | else |
359 | ctx->DYNAMIC_LIBNAME = NULL; | |
360 | return (ctx->DYNAMIC_LIBNAME ? 1 : 0); | |
361 | case DYNAMIC_CMD_NO_VCHECK: | |
362 | ctx->no_vcheck = ((i == 0) ? 0 : 1); | |
363 | return 1; | |
364 | case DYNAMIC_CMD_ID: | |
365 | /* a NULL 'p' or a string of zero-length is the same thing */ | |
366 | if (p && (strlen((const char *)p) < 1)) | |
367 | p = NULL; | |
b548a1f1 | 368 | OPENSSL_free(ctx->engine_id); |
0f113f3e | 369 | if (p) |
7644a9ae | 370 | ctx->engine_id = OPENSSL_strdup(p); |
0f113f3e MC |
371 | else |
372 | ctx->engine_id = NULL; | |
373 | return (ctx->engine_id ? 1 : 0); | |
374 | case DYNAMIC_CMD_LIST_ADD: | |
375 | if ((i < 0) || (i > 2)) { | |
376 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT); | |
377 | return 0; | |
378 | } | |
379 | ctx->list_add_value = (int)i; | |
380 | return 1; | |
381 | case DYNAMIC_CMD_LOAD: | |
382 | return dynamic_load(e, ctx); | |
383 | case DYNAMIC_CMD_DIR_LOAD: | |
384 | if ((i < 0) || (i > 2)) { | |
385 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT); | |
386 | return 0; | |
387 | } | |
388 | ctx->dir_load = (int)i; | |
389 | return 1; | |
390 | case DYNAMIC_CMD_DIR_ADD: | |
391 | /* a NULL 'p' or a string of zero-length is the same thing */ | |
392 | if (!p || (strlen((const char *)p) < 1)) { | |
393 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT); | |
394 | return 0; | |
395 | } | |
396 | { | |
7644a9ae | 397 | char *tmp_str = OPENSSL_strdup(p); |
0f113f3e MC |
398 | if (!tmp_str) { |
399 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE); | |
400 | return 0; | |
401 | } | |
402 | sk_OPENSSL_STRING_insert(ctx->dirs, tmp_str, -1); | |
403 | } | |
404 | return 1; | |
405 | default: | |
406 | break; | |
407 | } | |
408 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED); | |
409 | return 0; | |
410 | } | |
9391f977 | 411 | |
02acf140 | 412 | static int int_load(dynamic_data_ctx *ctx) |
0f113f3e MC |
413 | { |
414 | int num, loop; | |
415 | /* Unless told not to, try a direct load */ | |
416 | if ((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso, | |
417 | ctx->DYNAMIC_LIBNAME, NULL, | |
418 | 0)) != NULL) | |
419 | return 1; | |
420 | /* If we're not allowed to use 'dirs' or we have none, fail */ | |
421 | if (!ctx->dir_load || (num = sk_OPENSSL_STRING_num(ctx->dirs)) < 1) | |
422 | return 0; | |
423 | for (loop = 0; loop < num; loop++) { | |
424 | const char *s = sk_OPENSSL_STRING_value(ctx->dirs, loop); | |
425 | char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s); | |
426 | if (!merge) | |
427 | return 0; | |
428 | if (DSO_load(ctx->dynamic_dso, merge, NULL, 0)) { | |
429 | /* Found what we're looking for */ | |
430 | OPENSSL_free(merge); | |
431 | return 1; | |
432 | } | |
433 | OPENSSL_free(merge); | |
434 | } | |
435 | return 0; | |
436 | } | |
02acf140 | 437 | |
9391f977 | 438 | static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx) |
0f113f3e MC |
439 | { |
440 | ENGINE cpy; | |
441 | dynamic_fns fns; | |
9391f977 | 442 | |
90945fa3 | 443 | if (ctx->dynamic_dso == NULL) |
0f113f3e | 444 | ctx->dynamic_dso = DSO_new(); |
90945fa3 MC |
445 | if (ctx->dynamic_dso == NULL) |
446 | return 0; | |
0f113f3e MC |
447 | if (!ctx->DYNAMIC_LIBNAME) { |
448 | if (!ctx->engine_id) | |
449 | return 0; | |
450 | ctx->DYNAMIC_LIBNAME = | |
451 | DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id); | |
452 | } | |
453 | if (!int_load(ctx)) { | |
454 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_NOT_FOUND); | |
455 | DSO_free(ctx->dynamic_dso); | |
456 | ctx->dynamic_dso = NULL; | |
457 | return 0; | |
458 | } | |
459 | /* We have to find a bind function otherwise it'll always end badly */ | |
460 | if (! | |
461 | (ctx->bind_engine = | |
462 | (dynamic_bind_engine) DSO_bind_func(ctx->dynamic_dso, | |
463 | ctx->DYNAMIC_F2))) { | |
464 | ctx->bind_engine = NULL; | |
465 | DSO_free(ctx->dynamic_dso); | |
466 | ctx->dynamic_dso = NULL; | |
467 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_FAILURE); | |
468 | return 0; | |
469 | } | |
470 | /* Do we perform version checking? */ | |
471 | if (!ctx->no_vcheck) { | |
472 | unsigned long vcheck_res = 0; | |
473 | /* | |
474 | * Now we try to find a version checking function and decide how to | |
475 | * cope with failure if/when it fails. | |
476 | */ | |
477 | ctx->v_check = | |
478 | (dynamic_v_check_fn) DSO_bind_func(ctx->dynamic_dso, | |
479 | ctx->DYNAMIC_F1); | |
480 | if (ctx->v_check) | |
481 | vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION); | |
482 | /* | |
483 | * We fail if the version checker veto'd the load *or* if it is | |
484 | * deferring to us (by returning its version) and we think it is too | |
485 | * old. | |
486 | */ | |
487 | if (vcheck_res < OSSL_DYNAMIC_OLDEST) { | |
488 | /* Fail */ | |
489 | ctx->bind_engine = NULL; | |
490 | ctx->v_check = NULL; | |
491 | DSO_free(ctx->dynamic_dso); | |
492 | ctx->dynamic_dso = NULL; | |
493 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, | |
494 | ENGINE_R_VERSION_INCOMPATIBILITY); | |
495 | return 0; | |
496 | } | |
497 | } | |
498 | /* | |
499 | * First binary copy the ENGINE structure so that we can roll back if the | |
500 | * hand-over fails | |
501 | */ | |
502 | memcpy(&cpy, e, sizeof(ENGINE)); | |
503 | /* | |
504 | * Provide the ERR, "ex_data", memory, and locking callbacks so the | |
505 | * loaded library uses our state rather than its own. FIXME: As noted in | |
506 | * engine.h, much of this would be simplified if each area of code | |
507 | * provided its own "summary" structure of all related callbacks. It | |
508 | * would also increase opaqueness. | |
509 | */ | |
510 | fns.static_state = ENGINE_get_static_state(); | |
0f113f3e MC |
511 | fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback(); |
512 | fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback(); | |
513 | fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback(); | |
514 | fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback(); | |
515 | fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback(); | |
516 | /* | |
517 | * Now that we've loaded the dynamic engine, make sure no "dynamic" | |
518 | * ENGINE elements will show through. | |
519 | */ | |
520 | engine_set_all_null(e); | |
5b8a57ec | 521 | |
0f113f3e MC |
522 | /* Try to bind the ENGINE onto our own ENGINE structure */ |
523 | if (!ctx->bind_engine(e, ctx->engine_id, &fns)) { | |
524 | ctx->bind_engine = NULL; | |
525 | ctx->v_check = NULL; | |
526 | DSO_free(ctx->dynamic_dso); | |
527 | ctx->dynamic_dso = NULL; | |
528 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_INIT_FAILED); | |
529 | /* Copy the original ENGINE structure back */ | |
530 | memcpy(e, &cpy, sizeof(ENGINE)); | |
531 | return 0; | |
532 | } | |
533 | /* Do we try to add this ENGINE to the internal list too? */ | |
534 | if (ctx->list_add_value > 0) { | |
535 | if (!ENGINE_add(e)) { | |
536 | /* Do we tolerate this or fail? */ | |
537 | if (ctx->list_add_value > 1) { | |
538 | /* | |
539 | * Fail - NB: By this time, it's too late to rollback, and | |
540 | * trying to do so allows the bind_engine() code to have | |
541 | * created leaks. We just have to fail where we are, after | |
542 | * the ENGINE has changed. | |
543 | */ | |
544 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, | |
545 | ENGINE_R_CONFLICTING_ENGINE_ID); | |
546 | return 0; | |
547 | } | |
548 | /* Tolerate */ | |
549 | ERR_clear_error(); | |
550 | } | |
551 | } | |
552 | return 1; | |
553 | } |