eng_dyn.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. static const ENGINE_CMD_DEFN dynamic_cmd_defns_empty[] = {
  117. {0, NULL, NULL, 0}
  118. };
  119. /*
  120. * Loading code stores state inside the ENGINE structure via the "ex_data"
  121. * element. We load all our state into a single structure and use that as a
  122. * single context in the "ex_data" stack.
  123. */
  124. struct st_dynamic_data_ctx {
  125. /* The DSO object we load that supplies the ENGINE code */
  126. DSO *dynamic_dso;
  127. /*
  128. * The function pointer to the version checking shared library function
  129. */
  130. dynamic_v_check_fn v_check;
  131. /*
  132. * The function pointer to the engine-binding shared library function
  133. */
  134. dynamic_bind_engine bind_engine;
  135. /* The default name/path for loading the shared library */
  136. const char *DYNAMIC_LIBNAME;
  137. /* Whether to continue loading on a version check failure */
  138. int no_vcheck;
  139. /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */
  140. const char *engine_id;
  141. /*
  142. * If non-zero, a successfully loaded ENGINE should be added to the
  143. * internal ENGINE list. If 2, the add must succeed or the entire load
  144. * should fail.
  145. */
  146. int list_add_value;
  147. /* The symbol name for the version checking function */
  148. const char *DYNAMIC_F1;
  149. /* The symbol name for the "initialise ENGINE structure" function */
  150. const char *DYNAMIC_F2;
  151. /*
  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. */
  155. int dir_load;
  156. /* A stack of directories from which ENGINEs could be loaded */
  157. STACK *dirs;
  158. };
  159. /*
  160. * This is the "ex_data" index we obtain and reserve for use with our context
  161. * structure.
  162. */
  163. static int dynamic_ex_data_idx = -1;
  164. static void int_free_str(void *s)
  165. {
  166. OPENSSL_free(s);
  167. }
  168. /*
  169. * Because our ex_data element may or may not get allocated depending on
  170. * whether a "first-use" occurs before the ENGINE is freed, we have a memory
  171. * leak problem to solve. We can't declare a "new" handler for the ex_data as
  172. * we don't want a dynamic_data_ctx in *all* ENGINE structures of all types
  173. * (this is a bug in the design of CRYPTO_EX_DATA). As such, we just declare
  174. * a "free" handler and that will get called if an ENGINE is being destroyed
  175. * and there was an ex_data element corresponding to our context type.
  176. */
  177. static void dynamic_data_ctx_free_func(void *parent, void *ptr,
  178. CRYPTO_EX_DATA *ad, int idx, long argl,
  179. void *argp)
  180. {
  181. if (ptr) {
  182. dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr;
  183. if (ctx->dynamic_dso)
  184. DSO_free(ctx->dynamic_dso);
  185. if (ctx->DYNAMIC_LIBNAME)
  186. OPENSSL_free((void *)ctx->DYNAMIC_LIBNAME);
  187. if (ctx->engine_id)
  188. OPENSSL_free((void *)ctx->engine_id);
  189. if (ctx->dirs)
  190. sk_pop_free(ctx->dirs, int_free_str);
  191. OPENSSL_free(ctx);
  192. }
  193. }
  194. /*
  195. * Construct the per-ENGINE context. We create it blindly and then use a lock
  196. * to check for a race - if so, all but one of the threads "racing" will have
  197. * wasted their time. The alternative involves creating everything inside the
  198. * lock which is far worse.
  199. */
  200. static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
  201. {
  202. dynamic_data_ctx *c;
  203. c = OPENSSL_malloc(sizeof(dynamic_data_ctx));
  204. if (!c) {
  205. ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
  206. return 0;
  207. }
  208. memset(c, 0, sizeof(dynamic_data_ctx));
  209. c->dynamic_dso = NULL;
  210. c->v_check = NULL;
  211. c->bind_engine = NULL;
  212. c->DYNAMIC_LIBNAME = NULL;
  213. c->no_vcheck = 0;
  214. c->engine_id = NULL;
  215. c->list_add_value = 0;
  216. c->DYNAMIC_F1 = "v_check";
  217. c->DYNAMIC_F2 = "bind_engine";
  218. c->dir_load = 1;
  219. c->dirs = sk_new_null();
  220. if (!c->dirs) {
  221. ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
  222. OPENSSL_free(c);
  223. return 0;
  224. }
  225. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  226. if ((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e,
  227. dynamic_ex_data_idx))
  228. == NULL) {
  229. /* Good, we're the first */
  230. ENGINE_set_ex_data(e, dynamic_ex_data_idx, c);
  231. *ctx = c;
  232. c = NULL;
  233. }
  234. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  235. /*
  236. * If we lost the race to set the context, c is non-NULL and *ctx is the
  237. * context of the thread that won.
  238. */
  239. if (c)
  240. OPENSSL_free(c);
  241. return 1;
  242. }
  243. /*
  244. * This function retrieves the context structure from an ENGINE's "ex_data",
  245. * or if it doesn't exist yet, sets it up.
  246. */
  247. static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
  248. {
  249. dynamic_data_ctx *ctx;
  250. if (dynamic_ex_data_idx < 0) {
  251. /*
  252. * Create and register the ENGINE ex_data, and associate our "free"
  253. * function with it to ensure any allocated contexts get freed when
  254. * an ENGINE goes underground.
  255. */
  256. int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL,
  257. dynamic_data_ctx_free_func);
  258. if (new_idx == -1) {
  259. ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX, ENGINE_R_NO_INDEX);
  260. return NULL;
  261. }
  262. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  263. /* Avoid a race by checking again inside this lock */
  264. if (dynamic_ex_data_idx < 0) {
  265. /* Good, someone didn't beat us to it */
  266. dynamic_ex_data_idx = new_idx;
  267. new_idx = -1;
  268. }
  269. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  270. /*
  271. * In theory we could "give back" the index here if (new_idx>-1), but
  272. * it's not possible and wouldn't gain us much if it were.
  273. */
  274. }
  275. ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx);
  276. /* Check if the context needs to be created */
  277. if ((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx))
  278. /* "set_data" will set errors if necessary */
  279. return NULL;
  280. return ctx;
  281. }
  282. static ENGINE *engine_dynamic(void)
  283. {
  284. ENGINE *ret = ENGINE_new();
  285. if (!ret)
  286. return NULL;
  287. if (!ENGINE_set_id(ret, engine_dynamic_id) ||
  288. !ENGINE_set_name(ret, engine_dynamic_name) ||
  289. !ENGINE_set_init_function(ret, dynamic_init) ||
  290. !ENGINE_set_finish_function(ret, dynamic_finish) ||
  291. !ENGINE_set_ctrl_function(ret, dynamic_ctrl) ||
  292. !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) ||
  293. !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) {
  294. ENGINE_free(ret);
  295. return NULL;
  296. }
  297. return ret;
  298. }
  299. void ENGINE_load_dynamic(void)
  300. {
  301. ENGINE *toadd = engine_dynamic();
  302. if (!toadd)
  303. return;
  304. ENGINE_add(toadd);
  305. /*
  306. * If the "add" worked, it gets a structural reference. So either way, we
  307. * release our just-created reference.
  308. */
  309. ENGINE_free(toadd);
  310. /*
  311. * If the "add" didn't work, it was probably a conflict because it was
  312. * already added (eg. someone calling ENGINE_load_blah then calling
  313. * ENGINE_load_builtin_engines() perhaps).
  314. */
  315. ERR_clear_error();
  316. }
  317. static int dynamic_init(ENGINE *e)
  318. {
  319. /*
  320. * We always return failure - the "dyanamic" engine itself can't be used
  321. * for anything.
  322. */
  323. return 0;
  324. }
  325. static int dynamic_finish(ENGINE *e)
  326. {
  327. /*
  328. * This should never be called on account of "dynamic_init" always
  329. * failing.
  330. */
  331. return 0;
  332. }
  333. static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  334. {
  335. dynamic_data_ctx *ctx = dynamic_get_data_ctx(e);
  336. int initialised;
  337. if (!ctx) {
  338. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_NOT_LOADED);
  339. return 0;
  340. }
  341. initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1);
  342. /* All our control commands require the ENGINE to be uninitialised */
  343. if (initialised) {
  344. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_ALREADY_LOADED);
  345. return 0;
  346. }
  347. switch (cmd) {
  348. case DYNAMIC_CMD_SO_PATH:
  349. /* a NULL 'p' or a string of zero-length is the same thing */
  350. if (p && (strlen((const char *)p) < 1))
  351. p = NULL;
  352. if (ctx->DYNAMIC_LIBNAME)
  353. OPENSSL_free((void *)ctx->DYNAMIC_LIBNAME);
  354. if (p)
  355. ctx->DYNAMIC_LIBNAME = BUF_strdup(p);
  356. else
  357. ctx->DYNAMIC_LIBNAME = NULL;
  358. return (ctx->DYNAMIC_LIBNAME ? 1 : 0);
  359. case DYNAMIC_CMD_NO_VCHECK:
  360. ctx->no_vcheck = ((i == 0) ? 0 : 1);
  361. return 1;
  362. case DYNAMIC_CMD_ID:
  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->engine_id)
  367. OPENSSL_free((void *)ctx->engine_id);
  368. if (p)
  369. ctx->engine_id = BUF_strdup(p);
  370. else
  371. ctx->engine_id = NULL;
  372. return (ctx->engine_id ? 1 : 0);
  373. case DYNAMIC_CMD_LIST_ADD:
  374. if ((i < 0) || (i > 2)) {
  375. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
  376. return 0;
  377. }
  378. ctx->list_add_value = (int)i;
  379. return 1;
  380. case DYNAMIC_CMD_LOAD:
  381. return dynamic_load(e, ctx);
  382. case DYNAMIC_CMD_DIR_LOAD:
  383. if ((i < 0) || (i > 2)) {
  384. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
  385. return 0;
  386. }
  387. ctx->dir_load = (int)i;
  388. return 1;
  389. case DYNAMIC_CMD_DIR_ADD:
  390. /* a NULL 'p' or a string of zero-length is the same thing */
  391. if (!p || (strlen((const char *)p) < 1)) {
  392. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
  393. return 0;
  394. }
  395. {
  396. char *tmp_str = BUF_strdup(p);
  397. if (!tmp_str) {
  398. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE);
  399. return 0;
  400. }
  401. sk_insert(ctx->dirs, tmp_str, -1);
  402. }
  403. return 1;
  404. default:
  405. break;
  406. }
  407. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  408. return 0;
  409. }
  410. static int int_load(dynamic_data_ctx *ctx)
  411. {
  412. int num, loop;
  413. /* Unless told not to, try a direct load */
  414. if ((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso,
  415. ctx->DYNAMIC_LIBNAME, NULL,
  416. 0)) != NULL)
  417. return 1;
  418. /* If we're not allowed to use 'dirs' or we have none, fail */
  419. if (!ctx->dir_load || ((num = sk_num(ctx->dirs)) < 1))
  420. return 0;
  421. for (loop = 0; loop < num; loop++) {
  422. const char *s = sk_value(ctx->dirs, loop);
  423. char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s);
  424. if (!merge)
  425. return 0;
  426. if (DSO_load(ctx->dynamic_dso, merge, NULL, 0)) {
  427. /* Found what we're looking for */
  428. OPENSSL_free(merge);
  429. return 1;
  430. }
  431. OPENSSL_free(merge);
  432. }
  433. return 0;
  434. }
  435. static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
  436. {
  437. ENGINE cpy;
  438. dynamic_fns fns;
  439. if (!ctx->dynamic_dso)
  440. ctx->dynamic_dso = DSO_new();
  441. if (!ctx->DYNAMIC_LIBNAME) {
  442. if (!ctx->engine_id)
  443. return 0;
  444. ctx->DYNAMIC_LIBNAME =
  445. DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id);
  446. }
  447. if (!int_load(ctx)) {
  448. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_NOT_FOUND);
  449. DSO_free(ctx->dynamic_dso);
  450. ctx->dynamic_dso = NULL;
  451. return 0;
  452. }
  453. /* We have to find a bind function otherwise it'll always end badly */
  454. if (!
  455. (ctx->bind_engine =
  456. (dynamic_bind_engine) DSO_bind_func(ctx->dynamic_dso,
  457. ctx->DYNAMIC_F2))) {
  458. ctx->bind_engine = NULL;
  459. DSO_free(ctx->dynamic_dso);
  460. ctx->dynamic_dso = NULL;
  461. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_FAILURE);
  462. return 0;
  463. }
  464. /* Do we perform version checking? */
  465. if (!ctx->no_vcheck) {
  466. unsigned long vcheck_res = 0;
  467. /*
  468. * Now we try to find a version checking function and decide how to
  469. * cope with failure if/when it fails.
  470. */
  471. ctx->v_check =
  472. (dynamic_v_check_fn) DSO_bind_func(ctx->dynamic_dso,
  473. ctx->DYNAMIC_F1);
  474. if (ctx->v_check)
  475. vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION);
  476. /*
  477. * We fail if the version checker veto'd the load *or* if it is
  478. * deferring to us (by returning its version) and we think it is too
  479. * old.
  480. */
  481. if (vcheck_res < OSSL_DYNAMIC_OLDEST) {
  482. /* Fail */
  483. ctx->bind_engine = NULL;
  484. ctx->v_check = NULL;
  485. DSO_free(ctx->dynamic_dso);
  486. ctx->dynamic_dso = NULL;
  487. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  488. ENGINE_R_VERSION_INCOMPATIBILITY);
  489. return 0;
  490. }
  491. }
  492. /*
  493. * First binary copy the ENGINE structure so that we can roll back if the
  494. * hand-over fails
  495. */
  496. memcpy(&cpy, e, sizeof(ENGINE));
  497. /*
  498. * Provide the ERR, "ex_data", memory, and locking callbacks so the
  499. * loaded library uses our state rather than its own. FIXME: As noted in
  500. * engine.h, much of this would be simplified if each area of code
  501. * provided its own "summary" structure of all related callbacks. It
  502. * would also increase opaqueness.
  503. */
  504. fns.static_state = ENGINE_get_static_state();
  505. fns.err_fns = ERR_get_implementation();
  506. fns.ex_data_fns = CRYPTO_get_ex_data_implementation();
  507. CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb,
  508. &fns.mem_fns.realloc_cb, &fns.mem_fns.free_cb);
  509. fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback();
  510. fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback();
  511. fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback();
  512. fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback();
  513. fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback();
  514. /*
  515. * Now that we've loaded the dynamic engine, make sure no "dynamic"
  516. * ENGINE elements will show through.
  517. */
  518. engine_set_all_null(e);
  519. /* Try to bind the ENGINE onto our own ENGINE structure */
  520. if (!ctx->bind_engine(e, ctx->engine_id, &fns)) {
  521. ctx->bind_engine = NULL;
  522. ctx->v_check = NULL;
  523. DSO_free(ctx->dynamic_dso);
  524. ctx->dynamic_dso = NULL;
  525. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_INIT_FAILED);
  526. /* Copy the original ENGINE structure back */
  527. memcpy(e, &cpy, sizeof(ENGINE));
  528. return 0;
  529. }
  530. /* Do we try to add this ENGINE to the internal list too? */
  531. if (ctx->list_add_value > 0) {
  532. if (!ENGINE_add(e)) {
  533. /* Do we tolerate this or fail? */
  534. if (ctx->list_add_value > 1) {
  535. /*
  536. * Fail - NB: By this time, it's too late to rollback, and
  537. * trying to do so allows the bind_engine() code to have
  538. * created leaks. We just have to fail where we are, after
  539. * the ENGINE has changed.
  540. */
  541. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  542. ENGINE_R_CONFLICTING_ENGINE_ID);
  543. return 0;
  544. }
  545. /* Tolerate */
  546. ERR_clear_error();
  547. }
  548. }
  549. return 1;
  550. }