2
0

eng_dyn.c 20 KB

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