]>
Commit | Line | Data |
---|---|---|
2b671586 | 1 | /* crypto/engine/eng_dyn.c */ |
ae5c8664 MC |
2 | /* |
3 | * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project | |
4 | * 2001. | |
9391f977 GT |
5 | */ |
6 | /* ==================================================================== | |
2b671586 | 7 | * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved. |
9391f977 GT |
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 | |
ae5c8664 | 14 | * notice, this list of conditions and the following disclaimer. |
9391f977 GT |
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 | * licensing@OpenSSL.org. | |
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 | * (eay@cryptsoft.com). This product includes software written by Tim | |
56 | * Hudson (tjh@cryptsoft.com). | |
57 | * | |
58 | */ | |
59 | ||
14cfde9c | 60 | #include "eng_int.h" |
9391f977 GT |
61 | #include <openssl/dso.h> |
62 | ||
ae5c8664 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); | |
ae5c8664 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 | ||
ae5c8664 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[] = { | |
ae5c8664 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 | |
ae5c8664 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 |
ae5c8664 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 */ | |
139 | const char *DYNAMIC_LIBNAME; | |
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 */ | |
143 | const char *engine_id; | |
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 | |
ae5c8664 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 | ||
ae5c8664 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, |
ae5c8664 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; | |
189 | if (ctx->dynamic_dso) | |
190 | DSO_free(ctx->dynamic_dso); | |
191 | if (ctx->DYNAMIC_LIBNAME) | |
192 | OPENSSL_free((void *)ctx->DYNAMIC_LIBNAME); | |
193 | if (ctx->engine_id) | |
194 | OPENSSL_free((void *)ctx->engine_id); | |
195 | if (ctx->dirs) | |
196 | sk_OPENSSL_STRING_pop_free(ctx->dirs, int_free_str); | |
197 | OPENSSL_free(ctx); | |
198 | } | |
199 | } | |
9391f977 | 200 | |
ae5c8664 MC |
201 | /* |
202 | * Construct the per-ENGINE context. We create it blindly and then use a lock | |
203 | * to check for a race - if so, all but one of the threads "racing" will have | |
9391f977 | 204 | * wasted their time. The alternative involves creating everything inside the |
ae5c8664 MC |
205 | * lock which is far worse. |
206 | */ | |
9391f977 | 207 | static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx) |
ae5c8664 MC |
208 | { |
209 | dynamic_data_ctx *c; | |
210 | c = OPENSSL_malloc(sizeof(dynamic_data_ctx)); | |
211 | if (!c) { | |
212 | ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE); | |
213 | return 0; | |
214 | } | |
215 | memset(c, 0, sizeof(dynamic_data_ctx)); | |
216 | c->dynamic_dso = NULL; | |
217 | c->v_check = NULL; | |
218 | c->bind_engine = NULL; | |
219 | c->DYNAMIC_LIBNAME = NULL; | |
220 | c->no_vcheck = 0; | |
221 | c->engine_id = NULL; | |
222 | c->list_add_value = 0; | |
223 | c->DYNAMIC_F1 = "v_check"; | |
224 | c->DYNAMIC_F2 = "bind_engine"; | |
225 | c->dir_load = 1; | |
226 | c->dirs = sk_OPENSSL_STRING_new_null(); | |
227 | if (!c->dirs) { | |
228 | ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE); | |
229 | OPENSSL_free(c); | |
230 | return 0; | |
231 | } | |
232 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); | |
233 | if ((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, | |
234 | dynamic_ex_data_idx)) | |
235 | == NULL) { | |
236 | /* Good, we're the first */ | |
237 | ENGINE_set_ex_data(e, dynamic_ex_data_idx, c); | |
238 | *ctx = c; | |
239 | c = NULL; | |
240 | } | |
241 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); | |
242 | /* | |
243 | * If we lost the race to set the context, c is non-NULL and *ctx is the | |
244 | * context of the thread that won. | |
245 | */ | |
246 | if (c) | |
247 | OPENSSL_free(c); | |
248 | return 1; | |
249 | } | |
9391f977 | 250 | |
ae5c8664 MC |
251 | /* |
252 | * This function retrieves the context structure from an ENGINE's "ex_data", | |
253 | * or if it doesn't exist yet, sets it up. | |
254 | */ | |
9391f977 | 255 | static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e) |
ae5c8664 MC |
256 | { |
257 | dynamic_data_ctx *ctx; | |
258 | if (dynamic_ex_data_idx < 0) { | |
259 | /* | |
260 | * Create and register the ENGINE ex_data, and associate our "free" | |
261 | * function with it to ensure any allocated contexts get freed when | |
262 | * an ENGINE goes underground. | |
263 | */ | |
264 | int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL, | |
265 | dynamic_data_ctx_free_func); | |
266 | if (new_idx == -1) { | |
267 | ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX, ENGINE_R_NO_INDEX); | |
268 | return NULL; | |
269 | } | |
270 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); | |
271 | /* Avoid a race by checking again inside this lock */ | |
272 | if (dynamic_ex_data_idx < 0) { | |
273 | /* Good, someone didn't beat us to it */ | |
274 | dynamic_ex_data_idx = new_idx; | |
275 | new_idx = -1; | |
276 | } | |
277 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); | |
278 | /* | |
279 | * In theory we could "give back" the index here if (new_idx>-1), but | |
280 | * it's not possible and wouldn't gain us much if it were. | |
281 | */ | |
282 | } | |
283 | ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx); | |
284 | /* Check if the context needs to be created */ | |
285 | if ((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx)) | |
286 | /* "set_data" will set errors if necessary */ | |
287 | return NULL; | |
288 | return ctx; | |
289 | } | |
9391f977 | 290 | |
b6d1e52d | 291 | static ENGINE *engine_dynamic(void) |
ae5c8664 MC |
292 | { |
293 | ENGINE *ret = ENGINE_new(); | |
294 | if (!ret) | |
295 | return NULL; | |
296 | if (!ENGINE_set_id(ret, engine_dynamic_id) || | |
297 | !ENGINE_set_name(ret, engine_dynamic_name) || | |
298 | !ENGINE_set_init_function(ret, dynamic_init) || | |
299 | !ENGINE_set_finish_function(ret, dynamic_finish) || | |
300 | !ENGINE_set_ctrl_function(ret, dynamic_ctrl) || | |
301 | !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) || | |
302 | !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) { | |
303 | ENGINE_free(ret); | |
304 | return NULL; | |
305 | } | |
306 | return ret; | |
307 | } | |
9391f977 | 308 | |
b6d1e52d | 309 | void ENGINE_load_dynamic(void) |
ae5c8664 MC |
310 | { |
311 | ENGINE *toadd = engine_dynamic(); | |
312 | if (!toadd) | |
313 | return; | |
314 | ENGINE_add(toadd); | |
315 | /* | |
316 | * If the "add" worked, it gets a structural reference. So either way, we | |
317 | * release our just-created reference. | |
318 | */ | |
319 | ENGINE_free(toadd); | |
320 | /* | |
321 | * If the "add" didn't work, it was probably a conflict because it was | |
322 | * already added (eg. someone calling ENGINE_load_blah then calling | |
323 | * ENGINE_load_builtin_engines() perhaps). | |
324 | */ | |
325 | ERR_clear_error(); | |
326 | } | |
b6d1e52d | 327 | |
9391f977 | 328 | static int dynamic_init(ENGINE *e) |
ae5c8664 MC |
329 | { |
330 | /* | |
331 | * We always return failure - the "dyanamic" engine itself can't be used | |
332 | * for anything. | |
333 | */ | |
334 | return 0; | |
335 | } | |
9391f977 GT |
336 | |
337 | static int dynamic_finish(ENGINE *e) | |
ae5c8664 MC |
338 | { |
339 | /* | |
340 | * This should never be called on account of "dynamic_init" always | |
341 | * failing. | |
342 | */ | |
343 | return 0; | |
344 | } | |
345 | ||
346 | static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void)) | |
347 | { | |
348 | dynamic_data_ctx *ctx = dynamic_get_data_ctx(e); | |
349 | int initialised; | |
9391f977 | 350 | |
ae5c8664 MC |
351 | if (!ctx) { |
352 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_NOT_LOADED); | |
353 | return 0; | |
354 | } | |
355 | initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1); | |
356 | /* All our control commands require the ENGINE to be uninitialised */ | |
357 | if (initialised) { | |
358 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_ALREADY_LOADED); | |
359 | return 0; | |
360 | } | |
361 | switch (cmd) { | |
362 | case DYNAMIC_CMD_SO_PATH: | |
363 | /* a NULL 'p' or a string of zero-length is the same thing */ | |
364 | if (p && (strlen((const char *)p) < 1)) | |
365 | p = NULL; | |
366 | if (ctx->DYNAMIC_LIBNAME) | |
367 | OPENSSL_free((void *)ctx->DYNAMIC_LIBNAME); | |
368 | if (p) | |
369 | ctx->DYNAMIC_LIBNAME = BUF_strdup(p); | |
370 | else | |
371 | ctx->DYNAMIC_LIBNAME = NULL; | |
372 | return (ctx->DYNAMIC_LIBNAME ? 1 : 0); | |
373 | case DYNAMIC_CMD_NO_VCHECK: | |
374 | ctx->no_vcheck = ((i == 0) ? 0 : 1); | |
375 | return 1; | |
376 | case DYNAMIC_CMD_ID: | |
377 | /* a NULL 'p' or a string of zero-length is the same thing */ | |
378 | if (p && (strlen((const char *)p) < 1)) | |
379 | p = NULL; | |
380 | if (ctx->engine_id) | |
381 | OPENSSL_free((void *)ctx->engine_id); | |
382 | if (p) | |
383 | ctx->engine_id = BUF_strdup(p); | |
384 | else | |
385 | ctx->engine_id = NULL; | |
386 | return (ctx->engine_id ? 1 : 0); | |
387 | case DYNAMIC_CMD_LIST_ADD: | |
388 | if ((i < 0) || (i > 2)) { | |
389 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT); | |
390 | return 0; | |
391 | } | |
392 | ctx->list_add_value = (int)i; | |
393 | return 1; | |
394 | case DYNAMIC_CMD_LOAD: | |
395 | return dynamic_load(e, ctx); | |
396 | case DYNAMIC_CMD_DIR_LOAD: | |
397 | if ((i < 0) || (i > 2)) { | |
398 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT); | |
399 | return 0; | |
400 | } | |
401 | ctx->dir_load = (int)i; | |
402 | return 1; | |
403 | case DYNAMIC_CMD_DIR_ADD: | |
404 | /* a NULL 'p' or a string of zero-length is the same thing */ | |
405 | if (!p || (strlen((const char *)p) < 1)) { | |
406 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT); | |
407 | return 0; | |
408 | } | |
409 | { | |
410 | char *tmp_str = BUF_strdup(p); | |
411 | if (!tmp_str) { | |
412 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE); | |
413 | return 0; | |
414 | } | |
415 | sk_OPENSSL_STRING_insert(ctx->dirs, tmp_str, -1); | |
416 | } | |
417 | return 1; | |
418 | default: | |
419 | break; | |
420 | } | |
421 | ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED); | |
422 | return 0; | |
423 | } | |
9391f977 | 424 | |
02acf140 | 425 | static int int_load(dynamic_data_ctx *ctx) |
ae5c8664 MC |
426 | { |
427 | int num, loop; | |
428 | /* Unless told not to, try a direct load */ | |
429 | if ((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso, | |
430 | ctx->DYNAMIC_LIBNAME, NULL, | |
431 | 0)) != NULL) | |
432 | return 1; | |
433 | /* If we're not allowed to use 'dirs' or we have none, fail */ | |
434 | if (!ctx->dir_load || (num = sk_OPENSSL_STRING_num(ctx->dirs)) < 1) | |
435 | return 0; | |
436 | for (loop = 0; loop < num; loop++) { | |
437 | const char *s = sk_OPENSSL_STRING_value(ctx->dirs, loop); | |
438 | char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s); | |
439 | if (!merge) | |
440 | return 0; | |
441 | if (DSO_load(ctx->dynamic_dso, merge, NULL, 0)) { | |
442 | /* Found what we're looking for */ | |
443 | OPENSSL_free(merge); | |
444 | return 1; | |
445 | } | |
446 | OPENSSL_free(merge); | |
447 | } | |
448 | return 0; | |
449 | } | |
02acf140 | 450 | |
9391f977 | 451 | static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx) |
ae5c8664 MC |
452 | { |
453 | ENGINE cpy; | |
454 | dynamic_fns fns; | |
9391f977 | 455 | |
ae5c8664 MC |
456 | if (!ctx->dynamic_dso) |
457 | ctx->dynamic_dso = DSO_new(); | |
458 | if (!ctx->DYNAMIC_LIBNAME) { | |
459 | if (!ctx->engine_id) | |
460 | return 0; | |
461 | ctx->DYNAMIC_LIBNAME = | |
462 | DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id); | |
463 | } | |
464 | if (!int_load(ctx)) { | |
465 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_NOT_FOUND); | |
466 | DSO_free(ctx->dynamic_dso); | |
467 | ctx->dynamic_dso = NULL; | |
468 | return 0; | |
469 | } | |
470 | /* We have to find a bind function otherwise it'll always end badly */ | |
471 | if (! | |
472 | (ctx->bind_engine = | |
473 | (dynamic_bind_engine) DSO_bind_func(ctx->dynamic_dso, | |
474 | ctx->DYNAMIC_F2))) { | |
475 | ctx->bind_engine = NULL; | |
476 | DSO_free(ctx->dynamic_dso); | |
477 | ctx->dynamic_dso = NULL; | |
478 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_FAILURE); | |
479 | return 0; | |
480 | } | |
481 | /* Do we perform version checking? */ | |
482 | if (!ctx->no_vcheck) { | |
483 | unsigned long vcheck_res = 0; | |
484 | /* | |
485 | * Now we try to find a version checking function and decide how to | |
486 | * cope with failure if/when it fails. | |
487 | */ | |
488 | ctx->v_check = | |
489 | (dynamic_v_check_fn) DSO_bind_func(ctx->dynamic_dso, | |
490 | ctx->DYNAMIC_F1); | |
491 | if (ctx->v_check) | |
492 | vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION); | |
493 | /* | |
494 | * We fail if the version checker veto'd the load *or* if it is | |
495 | * deferring to us (by returning its version) and we think it is too | |
496 | * old. | |
497 | */ | |
498 | if (vcheck_res < OSSL_DYNAMIC_OLDEST) { | |
499 | /* Fail */ | |
500 | ctx->bind_engine = NULL; | |
501 | ctx->v_check = NULL; | |
502 | DSO_free(ctx->dynamic_dso); | |
503 | ctx->dynamic_dso = NULL; | |
504 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, | |
505 | ENGINE_R_VERSION_INCOMPATIBILITY); | |
506 | return 0; | |
507 | } | |
508 | } | |
509 | /* | |
510 | * First binary copy the ENGINE structure so that we can roll back if the | |
511 | * hand-over fails | |
512 | */ | |
513 | memcpy(&cpy, e, sizeof(ENGINE)); | |
514 | /* | |
515 | * Provide the ERR, "ex_data", memory, and locking callbacks so the | |
516 | * loaded library uses our state rather than its own. FIXME: As noted in | |
517 | * engine.h, much of this would be simplified if each area of code | |
518 | * provided its own "summary" structure of all related callbacks. It | |
519 | * would also increase opaqueness. | |
520 | */ | |
521 | fns.static_state = ENGINE_get_static_state(); | |
522 | fns.err_fns = ERR_get_implementation(); | |
523 | fns.ex_data_fns = CRYPTO_get_ex_data_implementation(); | |
524 | CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb, | |
525 | &fns.mem_fns.realloc_cb, &fns.mem_fns.free_cb); | |
526 | fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback(); | |
527 | fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback(); | |
528 | fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback(); | |
529 | fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback(); | |
530 | fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback(); | |
531 | /* | |
532 | * Now that we've loaded the dynamic engine, make sure no "dynamic" | |
533 | * ENGINE elements will show through. | |
534 | */ | |
535 | engine_set_all_null(e); | |
5b8a57ec | 536 | |
ae5c8664 MC |
537 | /* Try to bind the ENGINE onto our own ENGINE structure */ |
538 | if (!ctx->bind_engine(e, ctx->engine_id, &fns)) { | |
539 | ctx->bind_engine = NULL; | |
540 | ctx->v_check = NULL; | |
541 | DSO_free(ctx->dynamic_dso); | |
542 | ctx->dynamic_dso = NULL; | |
543 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_INIT_FAILED); | |
544 | /* Copy the original ENGINE structure back */ | |
545 | memcpy(e, &cpy, sizeof(ENGINE)); | |
546 | return 0; | |
547 | } | |
548 | /* Do we try to add this ENGINE to the internal list too? */ | |
549 | if (ctx->list_add_value > 0) { | |
550 | if (!ENGINE_add(e)) { | |
551 | /* Do we tolerate this or fail? */ | |
552 | if (ctx->list_add_value > 1) { | |
553 | /* | |
554 | * Fail - NB: By this time, it's too late to rollback, and | |
555 | * trying to do so allows the bind_engine() code to have | |
556 | * created leaks. We just have to fail where we are, after | |
557 | * the ENGINE has changed. | |
558 | */ | |
559 | ENGINEerr(ENGINE_F_DYNAMIC_LOAD, | |
560 | ENGINE_R_CONFLICTING_ENGINE_ID); | |
561 | return 0; | |
562 | } | |
563 | /* Tolerate */ | |
564 | ERR_clear_error(); | |
565 | } | |
566 | } | |
567 | return 1; | |
568 | } |