eng_dyn.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "eng_int.h"
  10. #include "internal/dso.h"
  11. #include <openssl/crypto.h>
  12. /*
  13. * Shared libraries implementing ENGINEs for use by the "dynamic" ENGINE
  14. * loader should implement the hook-up functions with the following
  15. * prototypes.
  16. */
  17. /* Our ENGINE handlers */
  18. static int dynamic_init(ENGINE *e);
  19. static int dynamic_finish(ENGINE *e);
  20. static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p,
  21. void (*f) (void));
  22. /* Predeclare our context type */
  23. typedef struct st_dynamic_data_ctx dynamic_data_ctx;
  24. /* The implementation for the important control command */
  25. static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx);
  26. #define DYNAMIC_CMD_SO_PATH ENGINE_CMD_BASE
  27. #define DYNAMIC_CMD_NO_VCHECK (ENGINE_CMD_BASE + 1)
  28. #define DYNAMIC_CMD_ID (ENGINE_CMD_BASE + 2)
  29. #define DYNAMIC_CMD_LIST_ADD (ENGINE_CMD_BASE + 3)
  30. #define DYNAMIC_CMD_DIR_LOAD (ENGINE_CMD_BASE + 4)
  31. #define DYNAMIC_CMD_DIR_ADD (ENGINE_CMD_BASE + 5)
  32. #define DYNAMIC_CMD_LOAD (ENGINE_CMD_BASE + 6)
  33. /* The constants used when creating the ENGINE */
  34. static const char *engine_dynamic_id = "dynamic";
  35. static const char *engine_dynamic_name = "Dynamic engine loading support";
  36. static const ENGINE_CMD_DEFN dynamic_cmd_defns[] = {
  37. {DYNAMIC_CMD_SO_PATH,
  38. "SO_PATH",
  39. "Specifies the path to the new ENGINE shared library",
  40. ENGINE_CMD_FLAG_STRING},
  41. {DYNAMIC_CMD_NO_VCHECK,
  42. "NO_VCHECK",
  43. "Specifies to continue even if version checking fails (boolean)",
  44. ENGINE_CMD_FLAG_NUMERIC},
  45. {DYNAMIC_CMD_ID,
  46. "ID",
  47. "Specifies an ENGINE id name for loading",
  48. ENGINE_CMD_FLAG_STRING},
  49. {DYNAMIC_CMD_LIST_ADD,
  50. "LIST_ADD",
  51. "Whether to add a loaded ENGINE to the internal list (0=no,1=yes,2=mandatory)",
  52. ENGINE_CMD_FLAG_NUMERIC},
  53. {DYNAMIC_CMD_DIR_LOAD,
  54. "DIR_LOAD",
  55. "Specifies whether to load from 'DIR_ADD' directories (0=no,1=yes,2=mandatory)",
  56. ENGINE_CMD_FLAG_NUMERIC},
  57. {DYNAMIC_CMD_DIR_ADD,
  58. "DIR_ADD",
  59. "Adds a directory from which ENGINEs can be loaded",
  60. ENGINE_CMD_FLAG_STRING},
  61. {DYNAMIC_CMD_LOAD,
  62. "LOAD",
  63. "Load up the ENGINE specified by other settings",
  64. ENGINE_CMD_FLAG_NO_INPUT},
  65. {0, NULL, NULL, 0}
  66. };
  67. /*
  68. * Loading code stores state inside the ENGINE structure via the "ex_data"
  69. * element. We load all our state into a single structure and use that as a
  70. * single context in the "ex_data" stack.
  71. */
  72. struct st_dynamic_data_ctx {
  73. /* The DSO object we load that supplies the ENGINE code */
  74. DSO *dynamic_dso;
  75. /*
  76. * The function pointer to the version checking shared library function
  77. */
  78. dynamic_v_check_fn v_check;
  79. /*
  80. * The function pointer to the engine-binding shared library function
  81. */
  82. dynamic_bind_engine bind_engine;
  83. /* The default name/path for loading the shared library */
  84. char *DYNAMIC_LIBNAME;
  85. /* Whether to continue loading on a version check failure */
  86. int no_vcheck;
  87. /* If non-NULL, stipulates the 'id' of the ENGINE to be loaded */
  88. char *engine_id;
  89. /*
  90. * If non-zero, a successfully loaded ENGINE should be added to the
  91. * internal ENGINE list. If 2, the add must succeed or the entire load
  92. * should fail.
  93. */
  94. int list_add_value;
  95. /* The symbol name for the version checking function */
  96. const char *DYNAMIC_F1;
  97. /* The symbol name for the "initialise ENGINE structure" function */
  98. const char *DYNAMIC_F2;
  99. /*
  100. * Whether to never use 'dirs', use 'dirs' as a fallback, or only use
  101. * 'dirs' for loading. Default is to use 'dirs' as a fallback.
  102. */
  103. int dir_load;
  104. /* A stack of directories from which ENGINEs could be loaded */
  105. STACK_OF(OPENSSL_STRING) *dirs;
  106. };
  107. /*
  108. * This is the "ex_data" index we obtain and reserve for use with our context
  109. * structure.
  110. */
  111. static int dynamic_ex_data_idx = -1;
  112. static void int_free_str(char *s)
  113. {
  114. OPENSSL_free(s);
  115. }
  116. /*
  117. * Because our ex_data element may or may not get allocated depending on
  118. * whether a "first-use" occurs before the ENGINE is freed, we have a memory
  119. * leak problem to solve. We can't declare a "new" handler for the ex_data as
  120. * we don't want a dynamic_data_ctx in *all* ENGINE structures of all types
  121. * (this is a bug in the design of CRYPTO_EX_DATA). As such, we just declare
  122. * a "free" handler and that will get called if an ENGINE is being destroyed
  123. * and there was an ex_data element corresponding to our context type.
  124. */
  125. static void dynamic_data_ctx_free_func(void *parent, void *ptr,
  126. CRYPTO_EX_DATA *ad, int idx, long argl,
  127. void *argp)
  128. {
  129. if (ptr) {
  130. dynamic_data_ctx *ctx = (dynamic_data_ctx *)ptr;
  131. DSO_free(ctx->dynamic_dso);
  132. OPENSSL_free(ctx->DYNAMIC_LIBNAME);
  133. OPENSSL_free(ctx->engine_id);
  134. sk_OPENSSL_STRING_pop_free(ctx->dirs, int_free_str);
  135. OPENSSL_free(ctx);
  136. }
  137. }
  138. /*
  139. * Construct the per-ENGINE context. We create it blindly and then use a lock
  140. * to check for a race - if so, all but one of the threads "racing" will have
  141. * wasted their time. The alternative involves creating everything inside the
  142. * lock which is far worse.
  143. */
  144. static int dynamic_set_data_ctx(ENGINE *e, dynamic_data_ctx **ctx)
  145. {
  146. dynamic_data_ctx *c = OPENSSL_zalloc(sizeof(*c));
  147. int ret = 1;
  148. if (c == NULL) {
  149. ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
  150. return 0;
  151. }
  152. c->dirs = sk_OPENSSL_STRING_new_null();
  153. if (c->dirs == NULL) {
  154. ENGINEerr(ENGINE_F_DYNAMIC_SET_DATA_CTX, ERR_R_MALLOC_FAILURE);
  155. OPENSSL_free(c);
  156. return 0;
  157. }
  158. c->DYNAMIC_F1 = "v_check";
  159. c->DYNAMIC_F2 = "bind_engine";
  160. c->dir_load = 1;
  161. CRYPTO_THREAD_write_lock(global_engine_lock);
  162. if ((*ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e,
  163. dynamic_ex_data_idx))
  164. == NULL) {
  165. /* Good, we're the first */
  166. ret = ENGINE_set_ex_data(e, dynamic_ex_data_idx, c);
  167. if (ret) {
  168. *ctx = c;
  169. c = NULL;
  170. }
  171. }
  172. CRYPTO_THREAD_unlock(global_engine_lock);
  173. /*
  174. * If we lost the race to set the context, c is non-NULL and *ctx is the
  175. * context of the thread that won.
  176. */
  177. if (c)
  178. sk_OPENSSL_STRING_free(c->dirs);
  179. OPENSSL_free(c);
  180. return ret;
  181. }
  182. /*
  183. * This function retrieves the context structure from an ENGINE's "ex_data",
  184. * or if it doesn't exist yet, sets it up.
  185. */
  186. static dynamic_data_ctx *dynamic_get_data_ctx(ENGINE *e)
  187. {
  188. dynamic_data_ctx *ctx;
  189. if (dynamic_ex_data_idx < 0) {
  190. /*
  191. * Create and register the ENGINE ex_data, and associate our "free"
  192. * function with it to ensure any allocated contexts get freed when
  193. * an ENGINE goes underground.
  194. */
  195. int new_idx = ENGINE_get_ex_new_index(0, NULL, NULL, NULL,
  196. dynamic_data_ctx_free_func);
  197. if (new_idx == -1) {
  198. ENGINEerr(ENGINE_F_DYNAMIC_GET_DATA_CTX, ENGINE_R_NO_INDEX);
  199. return NULL;
  200. }
  201. CRYPTO_THREAD_write_lock(global_engine_lock);
  202. /* Avoid a race by checking again inside this lock */
  203. if (dynamic_ex_data_idx < 0) {
  204. /* Good, someone didn't beat us to it */
  205. dynamic_ex_data_idx = new_idx;
  206. new_idx = -1;
  207. }
  208. CRYPTO_THREAD_unlock(global_engine_lock);
  209. /*
  210. * In theory we could "give back" the index here if (new_idx>-1), but
  211. * it's not possible and wouldn't gain us much if it were.
  212. */
  213. }
  214. ctx = (dynamic_data_ctx *)ENGINE_get_ex_data(e, dynamic_ex_data_idx);
  215. /* Check if the context needs to be created */
  216. if ((ctx == NULL) && !dynamic_set_data_ctx(e, &ctx))
  217. /* "set_data" will set errors if necessary */
  218. return NULL;
  219. return ctx;
  220. }
  221. static ENGINE *engine_dynamic(void)
  222. {
  223. ENGINE *ret = ENGINE_new();
  224. if (ret == NULL)
  225. return NULL;
  226. if (!ENGINE_set_id(ret, engine_dynamic_id) ||
  227. !ENGINE_set_name(ret, engine_dynamic_name) ||
  228. !ENGINE_set_init_function(ret, dynamic_init) ||
  229. !ENGINE_set_finish_function(ret, dynamic_finish) ||
  230. !ENGINE_set_ctrl_function(ret, dynamic_ctrl) ||
  231. !ENGINE_set_flags(ret, ENGINE_FLAGS_BY_ID_COPY) ||
  232. !ENGINE_set_cmd_defns(ret, dynamic_cmd_defns)) {
  233. ENGINE_free(ret);
  234. return NULL;
  235. }
  236. return ret;
  237. }
  238. void engine_load_dynamic_int(void)
  239. {
  240. ENGINE *toadd = engine_dynamic();
  241. if (!toadd)
  242. return;
  243. ENGINE_add(toadd);
  244. /*
  245. * If the "add" worked, it gets a structural reference. So either way, we
  246. * release our just-created reference.
  247. */
  248. ENGINE_free(toadd);
  249. /*
  250. * If the "add" didn't work, it was probably a conflict because it was
  251. * already added (eg. someone calling ENGINE_load_blah then calling
  252. * ENGINE_load_builtin_engines() perhaps).
  253. */
  254. ERR_clear_error();
  255. }
  256. static int dynamic_init(ENGINE *e)
  257. {
  258. /*
  259. * We always return failure - the "dynamic" engine itself can't be used
  260. * for anything.
  261. */
  262. return 0;
  263. }
  264. static int dynamic_finish(ENGINE *e)
  265. {
  266. /*
  267. * This should never be called on account of "dynamic_init" always
  268. * failing.
  269. */
  270. return 0;
  271. }
  272. static int dynamic_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  273. {
  274. dynamic_data_ctx *ctx = dynamic_get_data_ctx(e);
  275. int initialised;
  276. if (!ctx) {
  277. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_NOT_LOADED);
  278. return 0;
  279. }
  280. initialised = ((ctx->dynamic_dso == NULL) ? 0 : 1);
  281. /* All our control commands require the ENGINE to be uninitialised */
  282. if (initialised) {
  283. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_ALREADY_LOADED);
  284. return 0;
  285. }
  286. switch (cmd) {
  287. case DYNAMIC_CMD_SO_PATH:
  288. /* a NULL 'p' or a string of zero-length is the same thing */
  289. if (p && (strlen((const char *)p) < 1))
  290. p = NULL;
  291. OPENSSL_free(ctx->DYNAMIC_LIBNAME);
  292. if (p)
  293. ctx->DYNAMIC_LIBNAME = OPENSSL_strdup(p);
  294. else
  295. ctx->DYNAMIC_LIBNAME = NULL;
  296. return (ctx->DYNAMIC_LIBNAME ? 1 : 0);
  297. case DYNAMIC_CMD_NO_VCHECK:
  298. ctx->no_vcheck = ((i == 0) ? 0 : 1);
  299. return 1;
  300. case DYNAMIC_CMD_ID:
  301. /* a NULL 'p' or a string of zero-length is the same thing */
  302. if (p && (strlen((const char *)p) < 1))
  303. p = NULL;
  304. OPENSSL_free(ctx->engine_id);
  305. if (p)
  306. ctx->engine_id = OPENSSL_strdup(p);
  307. else
  308. ctx->engine_id = NULL;
  309. return (ctx->engine_id ? 1 : 0);
  310. case DYNAMIC_CMD_LIST_ADD:
  311. if ((i < 0) || (i > 2)) {
  312. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
  313. return 0;
  314. }
  315. ctx->list_add_value = (int)i;
  316. return 1;
  317. case DYNAMIC_CMD_LOAD:
  318. return dynamic_load(e, ctx);
  319. case DYNAMIC_CMD_DIR_LOAD:
  320. if ((i < 0) || (i > 2)) {
  321. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
  322. return 0;
  323. }
  324. ctx->dir_load = (int)i;
  325. return 1;
  326. case DYNAMIC_CMD_DIR_ADD:
  327. /* a NULL 'p' or a string of zero-length is the same thing */
  328. if (!p || (strlen((const char *)p) < 1)) {
  329. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_INVALID_ARGUMENT);
  330. return 0;
  331. }
  332. {
  333. char *tmp_str = OPENSSL_strdup(p);
  334. if (tmp_str == NULL) {
  335. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE);
  336. return 0;
  337. }
  338. if (!sk_OPENSSL_STRING_push(ctx->dirs, tmp_str)) {
  339. OPENSSL_free(tmp_str);
  340. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ERR_R_MALLOC_FAILURE);
  341. return 0;
  342. }
  343. }
  344. return 1;
  345. default:
  346. break;
  347. }
  348. ENGINEerr(ENGINE_F_DYNAMIC_CTRL, ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  349. return 0;
  350. }
  351. static int int_load(dynamic_data_ctx *ctx)
  352. {
  353. int num, loop;
  354. /* Unless told not to, try a direct load */
  355. if ((ctx->dir_load != 2) && (DSO_load(ctx->dynamic_dso,
  356. ctx->DYNAMIC_LIBNAME, NULL,
  357. 0)) != NULL)
  358. return 1;
  359. /* If we're not allowed to use 'dirs' or we have none, fail */
  360. if (!ctx->dir_load || (num = sk_OPENSSL_STRING_num(ctx->dirs)) < 1)
  361. return 0;
  362. for (loop = 0; loop < num; loop++) {
  363. const char *s = sk_OPENSSL_STRING_value(ctx->dirs, loop);
  364. char *merge = DSO_merge(ctx->dynamic_dso, ctx->DYNAMIC_LIBNAME, s);
  365. if (!merge)
  366. return 0;
  367. if (DSO_load(ctx->dynamic_dso, merge, NULL, 0)) {
  368. /* Found what we're looking for */
  369. OPENSSL_free(merge);
  370. return 1;
  371. }
  372. OPENSSL_free(merge);
  373. }
  374. return 0;
  375. }
  376. static int dynamic_load(ENGINE *e, dynamic_data_ctx *ctx)
  377. {
  378. ENGINE cpy;
  379. dynamic_fns fns;
  380. if (ctx->dynamic_dso == NULL)
  381. ctx->dynamic_dso = DSO_new();
  382. if (ctx->dynamic_dso == NULL)
  383. return 0;
  384. if (!ctx->DYNAMIC_LIBNAME) {
  385. if (!ctx->engine_id)
  386. return 0;
  387. DSO_ctrl(ctx->dynamic_dso, DSO_CTRL_SET_FLAGS,
  388. DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
  389. ctx->DYNAMIC_LIBNAME =
  390. DSO_convert_filename(ctx->dynamic_dso, ctx->engine_id);
  391. }
  392. if (!int_load(ctx)) {
  393. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_NOT_FOUND);
  394. DSO_free(ctx->dynamic_dso);
  395. ctx->dynamic_dso = NULL;
  396. return 0;
  397. }
  398. /* We have to find a bind function otherwise it'll always end badly */
  399. if (!
  400. (ctx->bind_engine =
  401. (dynamic_bind_engine) DSO_bind_func(ctx->dynamic_dso,
  402. ctx->DYNAMIC_F2))) {
  403. ctx->bind_engine = NULL;
  404. DSO_free(ctx->dynamic_dso);
  405. ctx->dynamic_dso = NULL;
  406. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_DSO_FAILURE);
  407. return 0;
  408. }
  409. /* Do we perform version checking? */
  410. if (!ctx->no_vcheck) {
  411. unsigned long vcheck_res = 0;
  412. /*
  413. * Now we try to find a version checking function and decide how to
  414. * cope with failure if/when it fails.
  415. */
  416. ctx->v_check =
  417. (dynamic_v_check_fn) DSO_bind_func(ctx->dynamic_dso,
  418. ctx->DYNAMIC_F1);
  419. if (ctx->v_check)
  420. vcheck_res = ctx->v_check(OSSL_DYNAMIC_VERSION);
  421. /*
  422. * We fail if the version checker veto'd the load *or* if it is
  423. * deferring to us (by returning its version) and we think it is too
  424. * old.
  425. */
  426. if (vcheck_res < OSSL_DYNAMIC_OLDEST) {
  427. /* Fail */
  428. ctx->bind_engine = NULL;
  429. ctx->v_check = NULL;
  430. DSO_free(ctx->dynamic_dso);
  431. ctx->dynamic_dso = NULL;
  432. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  433. ENGINE_R_VERSION_INCOMPATIBILITY);
  434. return 0;
  435. }
  436. }
  437. /*
  438. * First binary copy the ENGINE structure so that we can roll back if the
  439. * hand-over fails
  440. */
  441. memcpy(&cpy, e, sizeof(ENGINE));
  442. /*
  443. * Provide the ERR, "ex_data", memory, and locking callbacks so the
  444. * loaded library uses our state rather than its own. FIXME: As noted in
  445. * engine.h, much of this would be simplified if each area of code
  446. * provided its own "summary" structure of all related callbacks. It
  447. * would also increase opaqueness.
  448. */
  449. fns.static_state = ENGINE_get_static_state();
  450. CRYPTO_get_mem_functions(&fns.mem_fns.malloc_fn, &fns.mem_fns.realloc_fn,
  451. &fns.mem_fns.free_fn);
  452. /*
  453. * Now that we've loaded the dynamic engine, make sure no "dynamic"
  454. * ENGINE elements will show through.
  455. */
  456. engine_set_all_null(e);
  457. /* Try to bind the ENGINE onto our own ENGINE structure */
  458. if (!ctx->bind_engine(e, ctx->engine_id, &fns)) {
  459. ctx->bind_engine = NULL;
  460. ctx->v_check = NULL;
  461. DSO_free(ctx->dynamic_dso);
  462. ctx->dynamic_dso = NULL;
  463. ENGINEerr(ENGINE_F_DYNAMIC_LOAD, ENGINE_R_INIT_FAILED);
  464. /* Copy the original ENGINE structure back */
  465. memcpy(e, &cpy, sizeof(ENGINE));
  466. return 0;
  467. }
  468. /* Do we try to add this ENGINE to the internal list too? */
  469. if (ctx->list_add_value > 0) {
  470. if (!ENGINE_add(e)) {
  471. /* Do we tolerate this or fail? */
  472. if (ctx->list_add_value > 1) {
  473. /*
  474. * Fail - NB: By this time, it's too late to rollback, and
  475. * trying to do so allows the bind_engine() code to have
  476. * created leaks. We just have to fail where we are, after
  477. * the ENGINE has changed.
  478. */
  479. ENGINEerr(ENGINE_F_DYNAMIC_LOAD,
  480. ENGINE_R_CONFLICTING_ENGINE_ID);
  481. return 0;
  482. }
  483. /* Tolerate */
  484. ERR_clear_error();
  485. }
  486. }
  487. return 1;
  488. }