eng_dyn.c 17 KB

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