eng_table.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 2001-2023 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 "internal/cryptlib.h"
  10. #include <openssl/evp.h>
  11. #include <openssl/lhash.h>
  12. #include <openssl/trace.h>
  13. #include "eng_local.h"
  14. /* The type of the items in the table */
  15. struct st_engine_pile {
  16. /* The 'nid' of this algorithm/mode */
  17. int nid;
  18. /* ENGINEs that implement this algorithm/mode. */
  19. STACK_OF(ENGINE) *sk;
  20. /* The default ENGINE to perform this algorithm/mode. */
  21. ENGINE *funct;
  22. /*
  23. * Zero if 'sk' is newer than the cached 'funct', non-zero otherwise
  24. */
  25. int uptodate;
  26. };
  27. /* The type exposed in eng_local.h */
  28. struct st_engine_table {
  29. LHASH_OF(ENGINE_PILE) piles;
  30. }; /* ENGINE_TABLE */
  31. typedef struct st_engine_pile_doall {
  32. engine_table_doall_cb *cb;
  33. void *arg;
  34. } ENGINE_PILE_DOALL;
  35. /* Global flags (ENGINE_TABLE_FLAG_***). */
  36. static unsigned int table_flags = 0;
  37. /* API function manipulating 'table_flags' */
  38. unsigned int ENGINE_get_table_flags(void)
  39. {
  40. return table_flags;
  41. }
  42. void ENGINE_set_table_flags(unsigned int flags)
  43. {
  44. table_flags = flags;
  45. }
  46. /* Internal functions for the "piles" hash table */
  47. static unsigned long engine_pile_hash(const ENGINE_PILE *c)
  48. {
  49. return c->nid;
  50. }
  51. static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
  52. {
  53. return a->nid - b->nid;
  54. }
  55. static int int_table_check(ENGINE_TABLE **t, int create)
  56. {
  57. LHASH_OF(ENGINE_PILE) *lh;
  58. if (*t)
  59. return 1;
  60. if (!create)
  61. return 0;
  62. if ((lh = lh_ENGINE_PILE_new(engine_pile_hash, engine_pile_cmp)) == NULL)
  63. return 0;
  64. *t = (ENGINE_TABLE *)lh;
  65. return 1;
  66. }
  67. /*
  68. * Privately exposed (via eng_local.h) functions for adding and/or removing
  69. * ENGINEs from the implementation table
  70. */
  71. int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
  72. ENGINE *e, const int *nids, int num_nids,
  73. int setdefault)
  74. {
  75. int ret = 0, added = 0;
  76. ENGINE_PILE tmplate, *fnd;
  77. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  78. return 0;
  79. if (!(*table))
  80. added = 1;
  81. if (!int_table_check(table, 1))
  82. goto end;
  83. /* The cleanup callback needs to be added */
  84. if (added && !engine_cleanup_add_first(cleanup)) {
  85. lh_ENGINE_PILE_free(&(*table)->piles);
  86. *table = NULL;
  87. goto end;
  88. }
  89. while (num_nids--) {
  90. tmplate.nid = *nids;
  91. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  92. if (!fnd) {
  93. fnd = OPENSSL_malloc(sizeof(*fnd));
  94. if (fnd == NULL)
  95. goto end;
  96. fnd->uptodate = 1;
  97. fnd->nid = *nids;
  98. fnd->sk = sk_ENGINE_new_null();
  99. if (!fnd->sk) {
  100. OPENSSL_free(fnd);
  101. goto end;
  102. }
  103. fnd->funct = NULL;
  104. (void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
  105. if (lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate) != fnd) {
  106. sk_ENGINE_free(fnd->sk);
  107. OPENSSL_free(fnd);
  108. goto end;
  109. }
  110. }
  111. /* A registration shouldn't add duplicate entries */
  112. (void)sk_ENGINE_delete_ptr(fnd->sk, e);
  113. /*
  114. * if 'setdefault', this ENGINE goes to the head of the list
  115. */
  116. if (!sk_ENGINE_push(fnd->sk, e))
  117. goto end;
  118. /* "touch" this ENGINE_PILE */
  119. fnd->uptodate = 0;
  120. if (setdefault) {
  121. if (!engine_unlocked_init(e)) {
  122. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INIT_FAILED);
  123. goto end;
  124. }
  125. if (fnd->funct)
  126. engine_unlocked_finish(fnd->funct, 0);
  127. fnd->funct = e;
  128. fnd->uptodate = 1;
  129. }
  130. nids++;
  131. }
  132. ret = 1;
  133. end:
  134. CRYPTO_THREAD_unlock(global_engine_lock);
  135. return ret;
  136. }
  137. static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e)
  138. {
  139. int n;
  140. /* Iterate the 'c->sk' stack removing any occurrence of 'e' */
  141. while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) {
  142. (void)sk_ENGINE_delete(pile->sk, n);
  143. pile->uptodate = 0;
  144. }
  145. if (pile->funct == e) {
  146. engine_unlocked_finish(e, 0);
  147. pile->funct = NULL;
  148. }
  149. }
  150. IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE);
  151. void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
  152. {
  153. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  154. /* Can't return a value. :( */
  155. return;
  156. if (int_table_check(table, 0))
  157. lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e);
  158. CRYPTO_THREAD_unlock(global_engine_lock);
  159. }
  160. static void int_cleanup_cb_doall(ENGINE_PILE *p)
  161. {
  162. if (p == NULL)
  163. return;
  164. sk_ENGINE_free(p->sk);
  165. if (p->funct)
  166. engine_unlocked_finish(p->funct, 0);
  167. OPENSSL_free(p);
  168. }
  169. void engine_table_cleanup(ENGINE_TABLE **table)
  170. {
  171. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  172. return;
  173. if (*table) {
  174. lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall);
  175. lh_ENGINE_PILE_free(&(*table)->piles);
  176. *table = NULL;
  177. }
  178. CRYPTO_THREAD_unlock(global_engine_lock);
  179. }
  180. /* return a functional reference for a given 'nid' */
  181. ENGINE *ossl_engine_table_select(ENGINE_TABLE **table, int nid,
  182. const char *f, int l)
  183. {
  184. ENGINE *ret = NULL;
  185. ENGINE_PILE tmplate, *fnd = NULL;
  186. int initres, loop = 0;
  187. #ifndef OPENSSL_NO_AUTOLOAD_CONFIG
  188. /* Load the config before trying to check if engines are available */
  189. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  190. #endif
  191. if (!(*table)) {
  192. OSSL_TRACE3(ENGINE_TABLE,
  193. "%s:%d, nid=%d, nothing registered!\n",
  194. f, l, nid);
  195. return NULL;
  196. }
  197. ERR_set_mark();
  198. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  199. goto end;
  200. /*
  201. * Check again inside the lock otherwise we could race against cleanup
  202. * operations. But don't worry about a debug printout
  203. */
  204. if (!int_table_check(table, 0))
  205. goto end;
  206. tmplate.nid = nid;
  207. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  208. if (!fnd)
  209. goto end;
  210. if (fnd->funct && engine_unlocked_init(fnd->funct)) {
  211. OSSL_TRACE4(ENGINE_TABLE,
  212. "%s:%d, nid=%d, using ENGINE '%s' cached\n",
  213. f, l, nid, fnd->funct->id);
  214. ret = fnd->funct;
  215. goto end;
  216. }
  217. if (fnd->uptodate) {
  218. ret = fnd->funct;
  219. goto end;
  220. }
  221. trynext:
  222. ret = sk_ENGINE_value(fnd->sk, loop++);
  223. if (!ret) {
  224. OSSL_TRACE3(ENGINE_TABLE,
  225. "%s:%d, nid=%d, "
  226. "no registered implementations would initialise\n",
  227. f, l, nid);
  228. goto end;
  229. }
  230. /* Try to initialise the ENGINE? */
  231. if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
  232. initres = engine_unlocked_init(ret);
  233. else
  234. initres = 0;
  235. if (initres) {
  236. /* Update 'funct' */
  237. if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
  238. /* If there was a previous default we release it. */
  239. if (fnd->funct)
  240. engine_unlocked_finish(fnd->funct, 0);
  241. fnd->funct = ret;
  242. OSSL_TRACE4(ENGINE_TABLE,
  243. "%s:%d, nid=%d, setting default to '%s'\n",
  244. f, l, nid, ret->id);
  245. }
  246. OSSL_TRACE4(ENGINE_TABLE,
  247. "%s:%d, nid=%d, using newly initialised '%s'\n",
  248. f, l, nid, ret->id);
  249. goto end;
  250. }
  251. goto trynext;
  252. end:
  253. /*
  254. * If it failed, it is unlikely to succeed again until some future
  255. * registrations have taken place. In all cases, we cache.
  256. */
  257. if (fnd)
  258. fnd->uptodate = 1;
  259. if (ret)
  260. OSSL_TRACE4(ENGINE_TABLE,
  261. "%s:%d, nid=%d, caching ENGINE '%s'\n",
  262. f, l, nid, ret->id);
  263. else
  264. OSSL_TRACE3(ENGINE_TABLE,
  265. "%s:%d, nid=%d, caching 'no matching ENGINE'\n",
  266. f, l, nid);
  267. CRYPTO_THREAD_unlock(global_engine_lock);
  268. /*
  269. * Whatever happened, any failed init()s are not failures in this
  270. * context, so clear our error state.
  271. */
  272. ERR_pop_to_mark();
  273. return ret;
  274. }
  275. /* Table enumeration */
  276. static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
  277. {
  278. dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
  279. }
  280. IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL);
  281. void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
  282. void *arg)
  283. {
  284. ENGINE_PILE_DOALL dall;
  285. dall.cb = cb;
  286. dall.arg = arg;
  287. if (table)
  288. lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall);
  289. }