eng_dyn.c 17 KB

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