eng_dyn.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /* crypto/engine/eng_dyn.c */
  2. /*
  3. * Written by Geoff Thorpe ([email protected]) 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  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. OPENSSL_free(c);
  238. return 1;
  239. }
  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. */
  244. static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
  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. }
  279. static ENGINE *engine_dynamic(void)
  280. {
  281. ENGINE *ret = ENGINE_new();
  282. if (!ret)
  283. return NULL;
  284. if (!ENGINE_set_id(ret, engine_dynamic_id) ||
  285. !ENGINE_set_name(ret, engine_dynamic_name) ||
  286. !ENGINE_set_init_function(ret, dynamic_init) ||
  287. !ENGINE_set_finish_function(ret, dynamic_finish) ||
  288. !ENGINE_set_ctrl_function(ret, dynamic_ctrl) ||
  289. !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) ||
  290. !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) {
  291. ENGINE_free(ret);
  292. return NULL;
  293. }
  294. return ret;
  295. }
  296. void ENGINE_load_dynamic(void)
  297. {
  298. ENGINE *toadd = engine_dynamic();
  299. if (!toadd)
  300. return;
  301. ENGINE_add(toadd);
  302. /*
  303. * If the "add" worked, it gets a structural reference. So either way, we
  304. * release our just-created reference.
  305. */
  306. ENGINE_free(toadd);
  307. /*
  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. */
  312. ERR_clear_error();
  313. }
  314. static int dynamic_init(ENGINE *e)
  315. {
  316. /*
  317. * We always return failure - the "dyanamic" engine itself can't be used
  318. * for anything.
  319. */
  320. return 0;
  321. }
  322. static int dynamic_finish(ENGINE *e)
  323. {
  324. /*
  325. * This should never be called on account of "dynamic_init" always
  326. * failing.
  327. */
  328. return 0;
  329. }
  330. static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  331. {
  332. dynamic_data_ctx *ctx = dynamic_get_data_ctx(e);
  333. int initialised;
  334. if (!ctx) {
  335. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_NOT_LOADED);
  336. return 0;
  337. }
  338. initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1);
  339. /* All our control commands require the ENGINE to be uninitialised */
  340. if (initialised) {
  341. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_ALREADY_LOADED);
  342. return 0;
  343. }
  344. switch (cmd) {
  345. case DYNAMIC_CMD_SO_PATH:
  346. /* a NULL 'p' or a string of zero-length is the same thing */
  347. if (p && (strlen((const char *)p) < 1))
  348. p = NULL;
  349. if (ctx->DYNAMIC_LIBNAME)
  350. OPENSSL_free((void *)ctx->DYNAMIC_LIBNAME);
  351. if (p)
  352. ctx->DYNAMIC_LIBNAME = BUF_strdup(p);
  353. else
  354. ctx->DYNAMIC_LIBNAME = NULL;
  355. return (ctx->DYNAMIC_LIBNAME ? 1 : 0);
  356. case DYNAMIC_CMD_NO_VCHECK:
  357. ctx->no_vcheck = ((i == 0) ? 0 : 1);
  358. return 1;
  359. case DYNAMIC_CMD_ID:
  360. /* a NULL 'p' or a string of zero-length is the same thing */
  361. if (p && (strlen((const char *)p) < 1))
  362. p = NULL;
  363. if (ctx->engine_id)
  364. OPENSSL_free((void *)ctx->engine_id);
  365. if (p)
  366. ctx->engine_id = BUF_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, ENGINE_R_INVALID_ARGUMENT);
  373. return 0;
  374. }
  375. ctx->list_add_value = (int)i;
  376. return 1;
  377. case DYNAMIC_CMD_LOAD:
  378. return dynamic_load(e, ctx);
  379. case DYNAMIC_CMD_DIR_LOAD:
  380. if ((i < 0) || (i > 2)) {
  381. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
  382. return 0;
  383. }
  384. ctx->dir_load = (int)i;
  385. return 1;
  386. case DYNAMIC_CMD_DIR_ADD:
  387. /* a NULL 'p' or a string of zero-length is the same thing */
  388. if (!p || (strlen((const char *)p) < 1)) {
  389. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
  390. return 0;
  391. }
  392. {
  393. char *tmp_str = BUF_strdup(p);
  394. if (!tmp_str) {
  395. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE);
  396. return 0;
  397. }
  398. sk_OPENSSL_STRING_insert(ctx->dirs, tmp_str, -1);
  399. }
  400. return 1;
  401. default:
  402. break;
  403. }
  404. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  405. return 0;
  406. }
  407. static int int_load(dynamic_data_ctx *ctx)
  408. {
  409. int num, loop;
  410. /* Unless told not to, try a direct load */
  411. if ((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso,
  412. ctx->DYNAMIC_LIBNAME, NULL,
  413. 0)) != NULL)
  414. return 1;
  415. /* If we're not allowed to use 'dirs' or we have none, fail */
  416. if (!ctx->dir_load || (num = sk_OPENSSL_STRING_num(ctx->dirs)) < 1)
  417. return 0;
  418. for (loop = 0; loop < num; loop++) {
  419. const char *s = sk_OPENSSL_STRING_value(ctx->dirs, loop);
  420. char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s);
  421. if (!merge)
  422. return 0;
  423. if (DSO_load(ctx->dynamic_dso, merge, NULL, 0)) {
  424. /* Found what we're looking for */
  425. OPENSSL_free(merge);
  426. return 1;
  427. }
  428. OPENSSL_free(merge);
  429. }
  430. return 0;
  431. }
  432. static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
  433. {
  434. ENGINE cpy;
  435. dynamic_fns fns;
  436. if (!ctx->dynamic_dso)
  437. ctx->dynamic_dso = DSO_new();
  438. if (!ctx->DYNAMIC_LIBNAME) {
  439. if (!ctx->engine_id)
  440. return 0;
  441. ctx->DYNAMIC_LIBNAME =
  442. DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id);
  443. }
  444. if (!int_load(ctx)) {
  445. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_NOT_FOUND);
  446. DSO_free(ctx->dynamic_dso);
  447. ctx->dynamic_dso = NULL;
  448. return 0;
  449. }
  450. /* We have to find a bind function otherwise it'll always end badly */
  451. if (!
  452. (ctx->bind_engine =
  453. (dynamic_bind_engine) DSO_bind_func(ctx->dynamic_dso,
  454. ctx->DYNAMIC_F2))) {
  455. ctx->bind_engine = NULL;
  456. DSO_free(ctx->dynamic_dso);
  457. ctx->dynamic_dso = NULL;
  458. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_FAILURE);
  459. return 0;
  460. }
  461. /* Do we perform version checking? */
  462. if (!ctx->no_vcheck) {
  463. unsigned long vcheck_res = 0;
  464. /*
  465. * Now we try to find a version checking function and decide how to
  466. * cope with failure if/when it fails.
  467. */
  468. ctx->v_check =
  469. (dynamic_v_check_fn) DSO_bind_func(ctx->dynamic_dso,
  470. ctx->DYNAMIC_F1);
  471. if (ctx->v_check)
  472. vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION);
  473. /*
  474. * We fail if the version checker veto'd the load *or* if it is
  475. * deferring to us (by returning its version) and we think it is too
  476. * old.
  477. */
  478. if (vcheck_res < OSSL_DYNAMIC_OLDEST) {
  479. /* Fail */
  480. ctx->bind_engine = NULL;
  481. ctx->v_check = NULL;
  482. DSO_free(ctx->dynamic_dso);
  483. ctx->dynamic_dso = NULL;
  484. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  485. ENGINE_R_VERSION_INCOMPATIBILITY);
  486. return 0;
  487. }
  488. }
  489. /*
  490. * First binary copy the ENGINE structure so that we can roll back if the
  491. * hand-over fails
  492. */
  493. memcpy(&cpy, e, sizeof(ENGINE));
  494. /*
  495. * Provide the ERR, "ex_data", memory, and locking callbacks so the
  496. * loaded library uses our state rather than its own. FIXME: As noted in
  497. * engine.h, much of this would be simplified if each area of code
  498. * provided its own "summary" structure of all related callbacks. It
  499. * would also increase opaqueness.
  500. */
  501. fns.static_state = ENGINE_get_static_state();
  502. fns.err_fns = ERR_get_implementation();
  503. fns.ex_data_fns = CRYPTO_get_ex_data_implementation();
  504. CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb,
  505. &fns.mem_fns.realloc_cb, &fns.mem_fns.free_cb);
  506. fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback();
  507. fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback();
  508. fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback();
  509. fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback();
  510. fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback();
  511. /*
  512. * Now that we've loaded the dynamic engine, make sure no "dynamic"
  513. * ENGINE elements will show through.
  514. */
  515. engine_set_all_null(e);
  516. /* Try to bind the ENGINE onto our own ENGINE structure */
  517. if (!ctx->bind_engine(e, ctx->engine_id, &fns)) {
  518. ctx->bind_engine = NULL;
  519. ctx->v_check = NULL;
  520. DSO_free(ctx->dynamic_dso);
  521. ctx->dynamic_dso = NULL;
  522. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_INIT_FAILED);
  523. /* Copy the original ENGINE structure back */
  524. memcpy(e, &cpy, sizeof(ENGINE));
  525. return 0;
  526. }
  527. /* Do we try to add this ENGINE to the internal list too? */
  528. if (ctx->list_add_value > 0) {
  529. if (!ENGINE_add(e)) {
  530. /* Do we tolerate this or fail? */
  531. if (ctx->list_add_value > 1) {
  532. /*
  533. * Fail - NB: By this time, it's too late to rollback, and
  534. * trying to do so allows the bind_engine() code to have
  535. * created leaks. We just have to fail where we are, after
  536. * the ENGINE has changed.
  537. */
  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. }