eng_table.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #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. if (added)
  84. /* The cleanup callback needs to be added */
  85. engine_cleanup_add_first(cleanup);
  86. while (num_nids--) {
  87. tmplate.nid = *nids;
  88. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  89. if (!fnd) {
  90. fnd = OPENSSL_malloc(sizeof(*fnd));
  91. if (fnd == NULL)
  92. goto end;
  93. fnd->uptodate = 1;
  94. fnd->nid = *nids;
  95. fnd->sk = sk_ENGINE_new_null();
  96. if (!fnd->sk) {
  97. OPENSSL_free(fnd);
  98. goto end;
  99. }
  100. fnd->funct = NULL;
  101. (void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
  102. if (lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate) != fnd) {
  103. sk_ENGINE_free(fnd->sk);
  104. OPENSSL_free(fnd);
  105. goto end;
  106. }
  107. }
  108. /* A registration shouldn't add duplicate entries */
  109. (void)sk_ENGINE_delete_ptr(fnd->sk, e);
  110. /*
  111. * if 'setdefault', this ENGINE goes to the head of the list
  112. */
  113. if (!sk_ENGINE_push(fnd->sk, e))
  114. goto end;
  115. /* "touch" this ENGINE_PILE */
  116. fnd->uptodate = 0;
  117. if (setdefault) {
  118. if (!engine_unlocked_init(e)) {
  119. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_INIT_FAILED);
  120. goto end;
  121. }
  122. if (fnd->funct)
  123. engine_unlocked_finish(fnd->funct, 0);
  124. fnd->funct = e;
  125. fnd->uptodate = 1;
  126. }
  127. nids++;
  128. }
  129. ret = 1;
  130. end:
  131. CRYPTO_THREAD_unlock(global_engine_lock);
  132. return ret;
  133. }
  134. static void int_unregister_cb(ENGINE_PILE *pile, ENGINE *e)
  135. {
  136. int n;
  137. /* Iterate the 'c->sk' stack removing any occurrence of 'e' */
  138. while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) {
  139. (void)sk_ENGINE_delete(pile->sk, n);
  140. pile->uptodate = 0;
  141. }
  142. if (pile->funct == e) {
  143. engine_unlocked_finish(e, 0);
  144. pile->funct = NULL;
  145. }
  146. }
  147. IMPLEMENT_LHASH_DOALL_ARG(ENGINE_PILE, ENGINE);
  148. void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
  149. {
  150. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  151. /* Can't return a value. :( */
  152. return;
  153. if (int_table_check(table, 0))
  154. lh_ENGINE_PILE_doall_ENGINE(&(*table)->piles, int_unregister_cb, e);
  155. CRYPTO_THREAD_unlock(global_engine_lock);
  156. }
  157. static void int_cleanup_cb_doall(ENGINE_PILE *p)
  158. {
  159. if (p == NULL)
  160. return;
  161. sk_ENGINE_free(p->sk);
  162. if (p->funct)
  163. engine_unlocked_finish(p->funct, 0);
  164. OPENSSL_free(p);
  165. }
  166. void engine_table_cleanup(ENGINE_TABLE **table)
  167. {
  168. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  169. return;
  170. if (*table) {
  171. lh_ENGINE_PILE_doall(&(*table)->piles, int_cleanup_cb_doall);
  172. lh_ENGINE_PILE_free(&(*table)->piles);
  173. *table = NULL;
  174. }
  175. CRYPTO_THREAD_unlock(global_engine_lock);
  176. }
  177. /* return a functional reference for a given 'nid' */
  178. ENGINE *ossl_engine_table_select(ENGINE_TABLE **table, int nid,
  179. const char *f, int l)
  180. {
  181. ENGINE *ret = NULL;
  182. ENGINE_PILE tmplate, *fnd = NULL;
  183. int initres, loop = 0;
  184. /* Load the config before trying to check if engines are available */
  185. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  186. if (!(*table)) {
  187. OSSL_TRACE3(ENGINE_TABLE,
  188. "%s:%d, nid=%d, nothing registered!\n",
  189. f, l, nid);
  190. return NULL;
  191. }
  192. ERR_set_mark();
  193. if (!CRYPTO_THREAD_write_lock(global_engine_lock))
  194. goto end;
  195. /*
  196. * Check again inside the lock otherwise we could race against cleanup
  197. * operations. But don't worry about a debug printout
  198. */
  199. if (!int_table_check(table, 0))
  200. goto end;
  201. tmplate.nid = nid;
  202. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  203. if (!fnd)
  204. goto end;
  205. if (fnd->funct && engine_unlocked_init(fnd->funct)) {
  206. OSSL_TRACE4(ENGINE_TABLE,
  207. "%s:%d, nid=%d, using ENGINE '%s' cached\n",
  208. f, l, nid, fnd->funct->id);
  209. ret = fnd->funct;
  210. goto end;
  211. }
  212. if (fnd->uptodate) {
  213. ret = fnd->funct;
  214. goto end;
  215. }
  216. trynext:
  217. ret = sk_ENGINE_value(fnd->sk, loop++);
  218. if (!ret) {
  219. OSSL_TRACE3(ENGINE_TABLE,
  220. "%s:%d, nid=%d, "
  221. "no registered implementations would initialise\n",
  222. f, l, nid);
  223. goto end;
  224. }
  225. /* Try to initialise the ENGINE? */
  226. if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
  227. initres = engine_unlocked_init(ret);
  228. else
  229. initres = 0;
  230. if (initres) {
  231. /* Update 'funct' */
  232. if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
  233. /* If there was a previous default we release it. */
  234. if (fnd->funct)
  235. engine_unlocked_finish(fnd->funct, 0);
  236. fnd->funct = ret;
  237. OSSL_TRACE4(ENGINE_TABLE,
  238. "%s:%d, nid=%d, setting default to '%s'\n",
  239. f, l, nid, ret->id);
  240. }
  241. OSSL_TRACE4(ENGINE_TABLE,
  242. "%s:%d, nid=%d, using newly initialised '%s'\n",
  243. f, l, nid, ret->id);
  244. goto end;
  245. }
  246. goto trynext;
  247. end:
  248. /*
  249. * If it failed, it is unlikely to succeed again until some future
  250. * registrations have taken place. In all cases, we cache.
  251. */
  252. if (fnd)
  253. fnd->uptodate = 1;
  254. if (ret)
  255. OSSL_TRACE4(ENGINE_TABLE,
  256. "%s:%d, nid=%d, caching ENGINE '%s'\n",
  257. f, l, nid, ret->id);
  258. else
  259. OSSL_TRACE3(ENGINE_TABLE,
  260. "%s:%d, nid=%d, caching 'no matching ENGINE'\n",
  261. f, l, nid);
  262. CRYPTO_THREAD_unlock(global_engine_lock);
  263. /*
  264. * Whatever happened, any failed init()s are not failures in this
  265. * context, so clear our error state.
  266. */
  267. ERR_pop_to_mark();
  268. return ret;
  269. }
  270. /* Table enumeration */
  271. static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
  272. {
  273. dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
  274. }
  275. IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL);
  276. void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
  277. void *arg)
  278. {
  279. ENGINE_PILE_DOALL dall;
  280. dall.cb = cb;
  281. dall.arg = arg;
  282. if (table)
  283. lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall);
  284. }