1 /* crypto/engine/eng_dyn.c */
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
61 #include <openssl/dso.h>
64 * Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE
65 * loader should implement the hook-up functions with the following
69 /* Our ENGINE handlers */
70 static int dynamic_init(ENGINE *e);
71 static int dynamic_finish(ENGINE *e);
72 static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p,
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);
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)
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[] = {
93 "Specifies the path to the new ENGINE shared library",
94 ENGINE_CMD_FLAG_STRING},
95 {DYNAMIC_CMD_NO_VCHECK,
97 "Specifies to continue even if version checking fails (boolean)",
98 ENGINE_CMD_FLAG_NUMERIC},
101 "Specifies an ENGINE id name for loading",
102 ENGINE_CMD_FLAG_STRING},
103 {DYNAMIC_CMD_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,
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,
113 "Adds a directory from which ENGINEs can be loaded",
114 ENGINE_CMD_FLAG_STRING},
117 "Load up the ENGINE specified by other settings",
118 ENGINE_CMD_FLAG_NO_INPUT},
123 * Loading code stores state inside the ENGINE structure via the "ex_data"
124 * element. We load all our state into a single structure and use that as a
125 * single context in the "ex_data" stack.
127 struct st_dynamic_data_ctx {
128 /* The DSO object we load that supplies the ENGINE code */
131 * The function pointer to the version checking shared library function
133 dynamic_v_check_fn v_check;
135 * The function pointer to the engine-binding shared library function
137 dynamic_bind_engine bind_engine;
138 /* The default name/path for loading the shared library */
139 char *DYNAMIC_LIBNAME;
140 /* Whether to continue loading on a version check failure */
142 /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */
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
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;
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.
159 /* A stack of directories from which ENGINEs could be loaded */
160 STACK_OF(OPENSSL_STRING) *dirs;
164 * This is the "ex_data" index we obtain and reserve for use with our context
167 static int dynamic_ex_data_idx = -1;
169 static void int_free_str(char *s)
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.
183 static void dynamic_data_ctx_free_func(void *parent, void *ptr,
184 CRYPTO_EX_DATA *ad, int idx, long argl,
188 dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr;
189 DSO_free(ctx->dynamic_dso);
190 OPENSSL_free(ctx->DYNAMIC_LIBNAME);
191 OPENSSL_free(ctx->engine_id);
192 sk_OPENSSL_STRING_pop_free(ctx->dirs, int_free_str);
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
200 * wasted their time. The alternative involves creating everything inside the
201 * lock which is far worse.
203 static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
205 dynamic_data_ctx *c = OPENSSL_zalloc(sizeof(*c));
208 ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
211 c->dynamic_dso = NULL;
213 c->bind_engine = NULL;
214 c->DYNAMIC_LIBNAME = NULL;
217 c->list_add_value = 0;
218 c->DYNAMIC_F1 = "v_check";
219 c->DYNAMIC_F2 = "bind_engine";
221 c->dirs = sk_OPENSSL_STRING_new_null();
223 ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
227 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
228 if ((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e,
229 dynamic_ex_data_idx))
231 /* Good, we're the first */
232 ENGINE_set_ex_data(e, dynamic_ex_data_idx, c);
236 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
238 * If we lost the race to set the context, c is non-NULL and *ctx is the
239 * context of the thread that won.
246 * This function retrieves the context structure from an ENGINE's "ex_data",
247 * or if it doesn't exist yet, sets it up.
249 static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
251 dynamic_data_ctx *ctx;
252 if (dynamic_ex_data_idx < 0) {
254 * Create and register the ENGINE ex_data, and associate our "free"
255 * function with it to ensure any allocated contexts get freed when
256 * an ENGINE goes underground.
258 int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL,
259 dynamic_data_ctx_free_func);
261 ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX, ENGINE_R_NO_INDEX);
264 CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
265 /* Avoid a race by checking again inside this lock */
266 if (dynamic_ex_data_idx < 0) {
267 /* Good, someone didn't beat us to it */
268 dynamic_ex_data_idx = new_idx;
271 CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
273 * In theory we could "give back" the index here if (new_idx>-1), but
274 * it's not possible and wouldn't gain us much if it were.
277 ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx);
278 /* Check if the context needs to be created */
279 if ((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx))
280 /* "set_data" will set errors if necessary */
285 static ENGINE *engine_dynamic(void)
287 ENGINE *ret = ENGINE_new();
290 if (!ENGINE_set_id(ret, engine_dynamic_id) ||
291 !ENGINE_set_name(ret, engine_dynamic_name) ||
292 !ENGINE_set_init_function(ret, dynamic_init) ||
293 !ENGINE_set_finish_function(ret, dynamic_finish) ||
294 !ENGINE_set_ctrl_function(ret, dynamic_ctrl) ||
295 !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) ||
296 !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) {
303 void ENGINE_load_dynamic(void)
305 ENGINE *toadd = engine_dynamic();
310 * If the "add" worked, it gets a structural reference. So either way, we
311 * release our just-created reference.
315 * If the "add" didn't work, it was probably a conflict because it was
316 * already added (eg. someone calling ENGINE_load_blah then calling
317 * ENGINE_load_builtin_engines() perhaps).
322 static int dynamic_init(ENGINE *e)
325 * We always return failure - the "dyanamic" engine itself can't be used
331 static int dynamic_finish(ENGINE *e)
334 * This should never be called on account of "dynamic_init" always
340 static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
342 dynamic_data_ctx *ctx = dynamic_get_data_ctx(e);
346 ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_NOT_LOADED);
349 initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1);
350 /* All our control commands require the ENGINE to be uninitialised */
352 ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_ALREADY_LOADED);
356 case DYNAMIC_CMD_SO_PATH:
357 /* a NULL 'p' or a string of zero-length is the same thing */
358 if (p && (strlen((const char *)p) < 1))
360 OPENSSL_free(ctx->DYNAMIC_LIBNAME);
362 ctx->DYNAMIC_LIBNAME = BUF_strdup(p);
364 ctx->DYNAMIC_LIBNAME = NULL;
365 return (ctx->DYNAMIC_LIBNAME ? 1 : 0);
366 case DYNAMIC_CMD_NO_VCHECK:
367 ctx->no_vcheck = ((i == 0) ? 0 : 1);
370 /* a NULL 'p' or a string of zero-length is the same thing */
371 if (p && (strlen((const char *)p) < 1))
373 OPENSSL_free(ctx->engine_id);
375 ctx->engine_id = BUF_strdup(p);
377 ctx->engine_id = NULL;
378 return (ctx->engine_id ? 1 : 0);
379 case DYNAMIC_CMD_LIST_ADD:
380 if ((i < 0) || (i > 2)) {
381 ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
384 ctx->list_add_value = (int)i;
386 case DYNAMIC_CMD_LOAD:
387 return dynamic_load(e, ctx);
388 case DYNAMIC_CMD_DIR_LOAD:
389 if ((i < 0) || (i > 2)) {
390 ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
393 ctx->dir_load = (int)i;
395 case DYNAMIC_CMD_DIR_ADD:
396 /* a NULL 'p' or a string of zero-length is the same thing */
397 if (!p || (strlen((const char *)p) < 1)) {
398 ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
402 char *tmp_str = BUF_strdup(p);
404 ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE);
407 sk_OPENSSL_STRING_insert(ctx->dirs, tmp_str, -1);
413 ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
417 static int int_load(dynamic_data_ctx *ctx)
420 /* Unless told not to, try a direct load */
421 if ((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso,
422 ctx->DYNAMIC_LIBNAME, NULL,
425 /* If we're not allowed to use 'dirs' or we have none, fail */
426 if (!ctx->dir_load || (num = sk_OPENSSL_STRING_num(ctx->dirs)) < 1)
428 for (loop = 0; loop < num; loop++) {
429 const char *s = sk_OPENSSL_STRING_value(ctx->dirs, loop);
430 char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s);
433 if (DSO_load(ctx->dynamic_dso, merge, NULL, 0)) {
434 /* Found what we're looking for */
443 static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
448 if (!ctx->dynamic_dso)
449 ctx->dynamic_dso = DSO_new();
450 if (!ctx->DYNAMIC_LIBNAME) {
453 ctx->DYNAMIC_LIBNAME =
454 DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id);
456 if (!int_load(ctx)) {
457 ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_NOT_FOUND);
458 DSO_free(ctx->dynamic_dso);
459 ctx->dynamic_dso = NULL;
462 /* We have to find a bind function otherwise it'll always end badly */
465 (dynamic_bind_engine) DSO_bind_func(ctx->dynamic_dso,
467 ctx->bind_engine = NULL;
468 DSO_free(ctx->dynamic_dso);
469 ctx->dynamic_dso = NULL;
470 ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_FAILURE);
473 /* Do we perform version checking? */
474 if (!ctx->no_vcheck) {
475 unsigned long vcheck_res = 0;
477 * Now we try to find a version checking function and decide how to
478 * cope with failure if/when it fails.
481 (dynamic_v_check_fn) DSO_bind_func(ctx->dynamic_dso,
484 vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION);
486 * We fail if the version checker veto'd the load *or* if it is
487 * deferring to us (by returning its version) and we think it is too
490 if (vcheck_res < OSSL_DYNAMIC_OLDEST) {
492 ctx->bind_engine = NULL;
494 DSO_free(ctx->dynamic_dso);
495 ctx->dynamic_dso = NULL;
496 ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
497 ENGINE_R_VERSION_INCOMPATIBILITY);
502 * First binary copy the ENGINE structure so that we can roll back if the
505 memcpy(&cpy, e, sizeof(ENGINE));
507 * Provide the ERR, "ex_data", memory, and locking callbacks so the
508 * loaded library uses our state rather than its own. FIXME: As noted in
509 * engine.h, much of this would be simplified if each area of code
510 * provided its own "summary" structure of all related callbacks. It
511 * would also increase opaqueness.
513 fns.static_state = ENGINE_get_static_state();
514 CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb,
515 &fns.mem_fns.realloc_cb, &fns.mem_fns.free_cb);
516 fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback();
517 fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback();
518 fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback();
519 fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback();
520 fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback();
522 * Now that we've loaded the dynamic engine, make sure no "dynamic"
523 * ENGINE elements will show through.
525 engine_set_all_null(e);
527 /* Try to bind the ENGINE onto our own ENGINE structure */
528 if (!ctx->bind_engine(e, ctx->engine_id, &fns)) {
529 ctx->bind_engine = NULL;
531 DSO_free(ctx->dynamic_dso);
532 ctx->dynamic_dso = NULL;
533 ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_INIT_FAILED);
534 /* Copy the original ENGINE structure back */
535 memcpy(e, &cpy, sizeof(ENGINE));
538 /* Do we try to add this ENGINE to the internal list too? */
539 if (ctx->list_add_value > 0) {
540 if (!ENGINE_add(e)) {
541 /* Do we tolerate this or fail? */
542 if (ctx->list_add_value > 1) {
544 * Fail - NB: By this time, it's too late to rollback, and
545 * trying to do so allows the bind_engine() code to have
546 * created leaks. We just have to fail where we are, after
547 * the ENGINE has changed.
549 ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
550 ENGINE_R_CONFLICTING_ENGINE_ID);