eng_table.c 8.6 KB

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