eng_dyn.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* crypto/engine/eng_dyn.c */
  2. /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
  3. * project 2001.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include "eng_int.h"
  59. #include <openssl/dso.h>
  60. /* Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE loader
  61. * should implement the hook-up functions with the following prototypes. */
  62. /* Our ENGINE handlers */
  63. static int dynamic_init(ENGINE *e);
  64. static int dynamic_finish(ENGINE *e);
  65. static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
  66. /* Predeclare our context type */
  67. typedef struct st_dynamic_data_ctx dynamic_data_ctx;
  68. /* The implementation for the important control command */
  69. static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx);
  70. #define DYNAMIC_CMD_SO_PATH ENGINE_CMD_BASE
  71. #define DYNAMIC_CMD_NO_VCHECK (ENGINE_CMD_BASE + 1)
  72. #define DYNAMIC_CMD_ID (ENGINE_CMD_BASE + 2)
  73. #define DYNAMIC_CMD_LIST_ADD (ENGINE_CMD_BASE + 3)
  74. #define DYNAMIC_CMD_DIR_LOAD (ENGINE_CMD_BASE + 4)
  75. #define DYNAMIC_CMD_DIR_ADD (ENGINE_CMD_BASE + 5)
  76. #define DYNAMIC_CMD_LOAD (ENGINE_CMD_BASE + 6)
  77. /* The constants used when creating the ENGINE */
  78. static const char *engine_dynamic_id = "dynamic";
  79. static const char *engine_dynamic_name = "Dynamic engine loading support";
  80. static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = {
  81. {DYNAMIC_CMD_SO_PATH,
  82. "SO_PATH",
  83. "Specifies the path to the new ENGINE shared library",
  84. ENGINE_CMD_FLAG_STRING},
  85. {DYNAMIC_CMD_NO_VCHECK,
  86. "NO_VCHECK",
  87. "Specifies to continue even if version checking fails (boolean)",
  88. ENGINE_CMD_FLAG_NUMERIC},
  89. {DYNAMIC_CMD_ID,
  90. "ID",
  91. "Specifies an ENGINE id name for loading",
  92. ENGINE_CMD_FLAG_STRING},
  93. {DYNAMIC_CMD_LIST_ADD,
  94. "LIST_ADD",
  95. "Whether to add a loaded ENGINE to the internal list (0=no,1=yes,2=mandatory)",
  96. ENGINE_CMD_FLAG_NUMERIC},
  97. {DYNAMIC_CMD_DIR_LOAD,
  98. "DIR_LOAD",
  99. "Specifies whether to load from 'DIR_ADD' directories (0=no,1=yes,2=mandatory)",
  100. ENGINE_CMD_FLAG_NUMERIC},
  101. {DYNAMIC_CMD_DIR_ADD,
  102. "DIR_ADD",
  103. "Adds a directory from which ENGINEs can be loaded",
  104. ENGINE_CMD_FLAG_STRING},
  105. {DYNAMIC_CMD_LOAD,
  106. "LOAD",
  107. "Load up the ENGINE specified by other settings",
  108. ENGINE_CMD_FLAG_NO_INPUT},
  109. {0, NULL, NULL, 0}
  110. };
  111. static const ENGINE_CMD_DEFN dynamic_cmd_defns_empty[] = {
  112. {0, NULL, NULL, 0}
  113. };
  114. /* Loading code stores state inside the ENGINE structure via the "ex_data"
  115. * element. We load all our state into a single structure and use that as a
  116. * single context in the "ex_data" stack. */
  117. struct st_dynamic_data_ctx
  118. {
  119. /* The DSO object we load that supplies the ENGINE code */
  120. DSO *dynamic_dso;
  121. /* The function pointer to the version checking shared library function */
  122. dynamic_v_check_fn v_check;
  123. /* The function pointer to the engine-binding shared library function */
  124. dynamic_bind_engine bind_engine;
  125. /* The default name/path for loading the shared library */
  126. const char *DYNAMIC_LIBNAME;
  127. /* Whether to continue loading on a version check failure */
  128. int no_vcheck;
  129. /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */
  130. const char *engine_id;
  131. /* If non-zero, a successfully loaded ENGINE should be added to the internal
  132. * ENGINE list. If 2, the add must succeed or the entire load should fail. */
  133. int list_add_value;
  134. /* The symbol name for the version checking function */
  135. const char *DYNAMIC_F1;
  136. /* The symbol name for the "initialise ENGINE structure" function */
  137. const char *DYNAMIC_F2;
  138. /* Whether to never use 'dirs', use 'dirs' as a fallback, or only use
  139. * 'dirs' for loading. Default is to use 'dirs' as a fallback. */
  140. int dir_load;
  141. /* A stack of directories from which ENGINEs could be loaded */
  142. STACK *dirs;
  143. };
  144. /* This is the "ex_data" index we obtain and reserve for use with our context
  145. * structure. */
  146. static int dynamic_ex_data_idx = -1;
  147. static void int_free_str(void *s) { OPENSSL_free(s); }
  148. /* Because our ex_data element may or may not get allocated depending on whether
  149. * a "first-use" occurs before the ENGINE is freed, we have a memory leak
  150. * problem to solve. We can't declare a "new" handler for the ex_data as we
  151. * don't want a dynamic_data_ctx in *all* ENGINE structures of all types (this
  152. * is a bug in the design of CRYPTO_EX_DATA). As such, we just declare a "free"
  153. * handler and that will get called if an ENGINE is being destroyed and there
  154. * was an ex_data element corresponding to our context type. */
  155. static void dynamic_data_ctx_free_func(void *parent, void *ptr,
  156. CRYPTO_EX_DATA *ad, int idx, long argl, void *argp)
  157. {
  158. if(ptr)
  159. {
  160. dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr;
  161. if(ctx->dynamic_dso)
  162. DSO_free(ctx->dynamic_dso);
  163. if(ctx->DYNAMIC_LIBNAME)
  164. OPENSSL_free((void*)ctx->DYNAMIC_LIBNAME);
  165. if(ctx->engine_id)
  166. OPENSSL_free((void*)ctx->engine_id);
  167. if(ctx->dirs)
  168. sk_pop_free(ctx->dirs, int_free_str);
  169. OPENSSL_free(ctx);
  170. }
  171. }
  172. /* Construct the per-ENGINE context. We create it blindly and then use a lock to
  173. * check for a race - if so, all but one of the threads "racing" will have
  174. * wasted their time. The alternative involves creating everything inside the
  175. * lock which is far worse. */
  176. static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
  177. {
  178. dynamic_data_ctx *c;
  179. c = OPENSSL_malloc(sizeof(dynamic_data_ctx));
  180. if(!c)
  181. {
  182. ENGINEerr(ENGINE_F_SET_DATA_CTX,ERR_R_MALLOC_FAILURE);
  183. return 0;
  184. }
  185. memset(c, 0, sizeof(dynamic_data_ctx));
  186. c->dynamic_dso = NULL;
  187. c->v_check = NULL;
  188. c->bind_engine = NULL;
  189. c->DYNAMIC_LIBNAME = NULL;
  190. c->no_vcheck = 0;
  191. c->engine_id = NULL;
  192. c->list_add_value = 0;
  193. c->DYNAMIC_F1 = "v_check";
  194. c->DYNAMIC_F2 = "bind_engine";
  195. c->dir_load = 1;
  196. c->dirs = sk_new_null();
  197. if(!c->dirs)
  198. {
  199. ENGINEerr(ENGINE_F_SET_DATA_CTX,ERR_R_MALLOC_FAILURE);
  200. OPENSSL_free(c);
  201. return 0;
  202. }
  203. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  204. if((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e,
  205. dynamic_ex_data_idx)) == NULL)
  206. {
  207. /* Good, we're the first */
  208. ENGINE_set_ex_data(e, dynamic_ex_data_idx, c);
  209. *ctx = c;
  210. c = NULL;
  211. }
  212. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  213. /* If we lost the race to set the context, c is non-NULL and *ctx is the
  214. * context of the thread that won. */
  215. if(c)
  216. OPENSSL_free(c);
  217. return 1;
  218. }
  219. /* This function retrieves the context structure from an ENGINE's "ex_data", or
  220. * if it doesn't exist yet, sets it up. */
  221. static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
  222. {
  223. dynamic_data_ctx *ctx;
  224. if(dynamic_ex_data_idx < 0)
  225. {
  226. /* Create and register the ENGINE ex_data, and associate our
  227. * "free" function with it to ensure any allocated contexts get
  228. * freed when an ENGINE goes underground. */
  229. int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL,
  230. dynamic_data_ctx_free_func);
  231. if(new_idx == -1)
  232. {
  233. ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX,ENGINE_R_NO_INDEX);
  234. return NULL;
  235. }
  236. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  237. /* Avoid a race by checking again inside this lock */
  238. if(dynamic_ex_data_idx < 0)
  239. {
  240. /* Good, someone didn't beat us to it */
  241. dynamic_ex_data_idx = new_idx;
  242. new_idx = -1;
  243. }
  244. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  245. /* In theory we could "give back" the index here if
  246. * (new_idx>-1), but it's not possible and wouldn't gain us much
  247. * if it were. */
  248. }
  249. ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx);
  250. /* Check if the context needs to be created */
  251. if((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx))
  252. /* "set_data" will set errors if necessary */
  253. return NULL;
  254. return ctx;
  255. }
  256. static ENGINE *engine_dynamic(void)
  257. {
  258. ENGINE *ret = ENGINE_new();
  259. if(!ret)
  260. return NULL;
  261. if(!ENGINE_set_id(ret, engine_dynamic_id) ||
  262. !ENGINE_set_name(ret, engine_dynamic_name) ||
  263. !ENGINE_set_init_function(ret, dynamic_init) ||
  264. !ENGINE_set_finish_function(ret, dynamic_finish) ||
  265. !ENGINE_set_ctrl_function(ret, dynamic_ctrl) ||
  266. !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) ||
  267. !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns))
  268. {
  269. ENGINE_free(ret);
  270. return NULL;
  271. }
  272. return ret;
  273. }
  274. void ENGINE_load_dynamic(void)
  275. {
  276. ENGINE *toadd = engine_dynamic();
  277. if(!toadd) return;
  278. ENGINE_add(toadd);
  279. /* If the "add" worked, it gets a structural reference. So either way,
  280. * we release our just-created reference. */
  281. ENGINE_free(toadd);
  282. /* If the "add" didn't work, it was probably a conflict because it was
  283. * already added (eg. someone calling ENGINE_load_blah then calling
  284. * ENGINE_load_builtin_engines() perhaps). */
  285. ERR_clear_error();
  286. }
  287. static int dynamic_init(ENGINE *e)
  288. {
  289. /* We always return failure - the "dyanamic" engine itself can't be used
  290. * for anything. */
  291. return 0;
  292. }
  293. static int dynamic_finish(ENGINE *e)
  294. {
  295. /* This should never be called on account of "dynamic_init" always
  296. * failing. */
  297. return 0;
  298. }
  299. static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  300. {
  301. dynamic_data_ctx *ctx = dynamic_get_data_ctx(e);
  302. int initialised;
  303. if(!ctx)
  304. {
  305. ENGINEerr(ENGINE_F_DYNAMIC_CTRL,ENGINE_R_NOT_LOADED);
  306. return 0;
  307. }
  308. initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1);
  309. /* All our control commands require the ENGINE to be uninitialised */
  310. if(initialised)
  311. {
  312. ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
  313. ENGINE_R_ALREADY_LOADED);
  314. return 0;
  315. }
  316. switch(cmd)
  317. {
  318. case DYNAMIC_CMD_SO_PATH:
  319. /* a NULL 'p' or a string of zero-length is the same thing */
  320. if(p && (strlen((const char *)p) < 1))
  321. p = NULL;
  322. if(ctx->DYNAMIC_LIBNAME)
  323. OPENSSL_free((void*)ctx->DYNAMIC_LIBNAME);
  324. if(p)
  325. ctx->DYNAMIC_LIBNAME = BUF_strdup(p);
  326. else
  327. ctx->DYNAMIC_LIBNAME = NULL;
  328. return (ctx->DYNAMIC_LIBNAME ? 1 : 0);
  329. case DYNAMIC_CMD_NO_VCHECK:
  330. ctx->no_vcheck = ((i == 0) ? 0 : 1);
  331. return 1;
  332. case DYNAMIC_CMD_ID:
  333. /* a NULL 'p' or a string of zero-length is the same thing */
  334. if(p && (strlen((const char *)p) < 1))
  335. p = NULL;
  336. if(ctx->engine_id)
  337. OPENSSL_free((void*)ctx->engine_id);
  338. if(p)
  339. ctx->engine_id = BUF_strdup(p);
  340. else
  341. ctx->engine_id = NULL;
  342. return (ctx->engine_id ? 1 : 0);
  343. case DYNAMIC_CMD_LIST_ADD:
  344. if((i < 0) || (i > 2))
  345. {
  346. ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
  347. ENGINE_R_INVALID_ARGUMENT);
  348. return 0;
  349. }
  350. ctx->list_add_value = (int)i;
  351. return 1;
  352. case DYNAMIC_CMD_LOAD:
  353. return dynamic_load(e, ctx);
  354. case DYNAMIC_CMD_DIR_LOAD:
  355. if((i < 0) || (i > 2))
  356. {
  357. ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
  358. ENGINE_R_INVALID_ARGUMENT);
  359. return 0;
  360. }
  361. ctx->dir_load = (int)i;
  362. return 1;
  363. case DYNAMIC_CMD_DIR_ADD:
  364. /* a NULL 'p' or a string of zero-length is the same thing */
  365. if(!p || (strlen((const char *)p) < 1))
  366. {
  367. ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
  368. ENGINE_R_INVALID_ARGUMENT);
  369. return 0;
  370. }
  371. {
  372. char *tmp_str = BUF_strdup(p);
  373. if(!tmp_str)
  374. {
  375. ENGINEerr(ENGINE_F_DYNAMIC_CTRL,
  376. ERR_R_MALLOC_FAILURE);
  377. return 0;
  378. }
  379. sk_insert(ctx->dirs, tmp_str, -1);
  380. }
  381. return 1;
  382. default:
  383. break;
  384. }
  385. ENGINEerr(ENGINE_F_DYNAMIC_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  386. return 0;
  387. }
  388. static int int_load(dynamic_data_ctx *ctx)
  389. {
  390. int num, loop;
  391. /* Unless told not to, try a direct load */
  392. if((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso,
  393. ctx->DYNAMIC_LIBNAME, NULL, 0)) != NULL)
  394. return 1;
  395. /* If we're not allowed to use 'dirs' or we have none, fail */
  396. if(!ctx->dir_load || ((num = sk_num(ctx->dirs)) < 1))
  397. return 0;
  398. for(loop = 0; loop < num; loop++)
  399. {
  400. const char *s = sk_value(ctx->dirs, loop);
  401. char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s);
  402. if(!merge)
  403. return 0;
  404. if(DSO_load(ctx->dynamic_dso, merge, NULL, 0))
  405. {
  406. /* Found what we're looking for */
  407. OPENSSL_free(merge);
  408. return 1;
  409. }
  410. OPENSSL_free(merge);
  411. }
  412. return 0;
  413. }
  414. static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
  415. {
  416. ENGINE cpy;
  417. dynamic_fns fns;
  418. if(!ctx->dynamic_dso)
  419. ctx->dynamic_dso = DSO_new();
  420. if(!ctx->DYNAMIC_LIBNAME)
  421. {
  422. if(!ctx->engine_id)
  423. return 0;
  424. ctx->DYNAMIC_LIBNAME =
  425. DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id);
  426. }
  427. if(!int_load(ctx))
  428. {
  429. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  430. ENGINE_R_DSO_NOT_FOUND);
  431. DSO_free(ctx->dynamic_dso);
  432. ctx->dynamic_dso = NULL;
  433. return 0;
  434. }
  435. /* We have to find a bind function otherwise it'll always end badly */
  436. if(!(ctx->bind_engine = (dynamic_bind_engine)DSO_bind_func(
  437. ctx->dynamic_dso, ctx->DYNAMIC_F2)))
  438. {
  439. ctx->bind_engine = NULL;
  440. DSO_free(ctx->dynamic_dso);
  441. ctx->dynamic_dso = NULL;
  442. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  443. ENGINE_R_DSO_FAILURE);
  444. return 0;
  445. }
  446. /* Do we perform version checking? */
  447. if(!ctx->no_vcheck)
  448. {
  449. unsigned long vcheck_res = 0;
  450. /* Now we try to find a version checking function and decide how
  451. * to cope with failure if/when it fails. */
  452. ctx->v_check = (dynamic_v_check_fn)DSO_bind_func(
  453. ctx->dynamic_dso, ctx->DYNAMIC_F1);
  454. if(ctx->v_check)
  455. vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION);
  456. /* We fail if the version checker veto'd the load *or* if it is
  457. * deferring to us (by returning its version) and we think it is
  458. * too old. */
  459. if(vcheck_res < OSSL_DYNAMIC_OLDEST)
  460. {
  461. /* Fail */
  462. ctx->bind_engine = NULL;
  463. ctx->v_check = NULL;
  464. DSO_free(ctx->dynamic_dso);
  465. ctx->dynamic_dso = NULL;
  466. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  467. ENGINE_R_VERSION_INCOMPATIBILITY);
  468. return 0;
  469. }
  470. }
  471. /* First binary copy the ENGINE structure so that we can roll back if
  472. * the hand-over fails */
  473. memcpy(&cpy, e, sizeof(ENGINE));
  474. /* Provide the ERR, "ex_data", memory, and locking callbacks so the
  475. * loaded library uses our state rather than its own. FIXME: As noted in
  476. * engine.h, much of this would be simplified if each area of code
  477. * provided its own "summary" structure of all related callbacks. It
  478. * would also increase opaqueness. */
  479. fns.static_state = ENGINE_get_static_state();
  480. fns.err_fns = ERR_get_implementation();
  481. fns.ex_data_fns = CRYPTO_get_ex_data_implementation();
  482. CRYPTO_get_mem_functions(&fns.mem_fns.malloc_cb,
  483. &fns.mem_fns.realloc_cb,
  484. &fns.mem_fns.free_cb);
  485. fns.lock_fns.lock_locking_cb = CRYPTO_get_locking_callback();
  486. fns.lock_fns.lock_add_lock_cb = CRYPTO_get_add_lock_callback();
  487. fns.lock_fns.dynlock_create_cb = CRYPTO_get_dynlock_create_callback();
  488. fns.lock_fns.dynlock_lock_cb = CRYPTO_get_dynlock_lock_callback();
  489. fns.lock_fns.dynlock_destroy_cb = CRYPTO_get_dynlock_destroy_callback();
  490. /* Now that we've loaded the dynamic engine, make sure no "dynamic"
  491. * ENGINE elements will show through. */
  492. engine_set_all_null(e);
  493. /* Try to bind the ENGINE onto our own ENGINE structure */
  494. if(!ctx->bind_engine(e, ctx->engine_id, &fns))
  495. {
  496. ctx->bind_engine = NULL;
  497. ctx->v_check = NULL;
  498. DSO_free(ctx->dynamic_dso);
  499. ctx->dynamic_dso = NULL;
  500. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,ENGINE_R_INIT_FAILED);
  501. /* Copy the original ENGINE structure back */
  502. memcpy(e, &cpy, sizeof(ENGINE));
  503. return 0;
  504. }
  505. /* Do we try to add this ENGINE to the internal list too? */
  506. if(ctx->list_add_value > 0)
  507. {
  508. if(!ENGINE_add(e))
  509. {
  510. /* Do we tolerate this or fail? */
  511. if(ctx->list_add_value > 1)
  512. {
  513. /* Fail - NB: By this time, it's too late to
  514. * rollback, and trying to do so allows the
  515. * bind_engine() code to have created leaks. We
  516. * just have to fail where we are, after the
  517. * ENGINE has changed. */
  518. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  519. ENGINE_R_CONFLICTING_ENGINE_ID);
  520. return 0;
  521. }
  522. /* Tolerate */
  523. ERR_clear_error();
  524. }
  525. }
  526. return 1;
  527. }