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