eng_table.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_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. 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 == NULL)
  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. /* Load the config before trying to check if engines are available */
  182. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
  183. if (!(*table)) {
  184. OSSL_TRACE3(ENGINE_TABLE,
  185. "%s:%d, nid=%d, nothing registered!\n",
  186. f, l, nid);
  187. return NULL;
  188. }
  189. ERR_set_mark();
  190. CRYPTO_THREAD_write_lock(global_engine_lock);
  191. /*
  192. * Check again inside the lock otherwise we could race against cleanup
  193. * operations. But don't worry about a debug printout
  194. */
  195. if (!int_table_check(table, 0))
  196. goto end;
  197. tmplate.nid = nid;
  198. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  199. if (!fnd)
  200. goto end;
  201. if (fnd->funct && engine_unlocked_init(fnd->funct)) {
  202. OSSL_TRACE4(ENGINE_TABLE,
  203. "%s:%d, nid=%d, using ENGINE '%s' cached\n",
  204. f, l, nid, fnd->funct->id);
  205. ret = fnd->funct;
  206. goto end;
  207. }
  208. if (fnd->uptodate) {
  209. ret = fnd->funct;
  210. goto end;
  211. }
  212. trynext:
  213. ret = sk_ENGINE_value(fnd->sk, loop++);
  214. if (!ret) {
  215. OSSL_TRACE3(ENGINE_TABLE,
  216. "%s:%d, nid=%d, "
  217. "no registered implementations would initialise\n",
  218. f, l, nid);
  219. goto end;
  220. }
  221. /* Try to initialise the ENGINE? */
  222. if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
  223. initres = engine_unlocked_init(ret);
  224. else
  225. initres = 0;
  226. if (initres) {
  227. /* Update 'funct' */
  228. if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
  229. /* If there was a previous default we release it. */
  230. if (fnd->funct)
  231. engine_unlocked_finish(fnd->funct, 0);
  232. fnd->funct = ret;
  233. OSSL_TRACE4(ENGINE_TABLE,
  234. "%s:%d, nid=%d, setting default to '%s'\n",
  235. f, l, nid, ret->id);
  236. }
  237. OSSL_TRACE4(ENGINE_TABLE,
  238. "%s:%d, nid=%d, using newly initialised '%s'\n",
  239. f, l, nid, ret->id);
  240. goto end;
  241. }
  242. goto trynext;
  243. end:
  244. /*
  245. * If it failed, it is unlikely to succeed again until some future
  246. * registrations have taken place. In all cases, we cache.
  247. */
  248. if (fnd)
  249. fnd->uptodate = 1;
  250. if (ret)
  251. OSSL_TRACE4(ENGINE_TABLE,
  252. "%s:%d, nid=%d, caching ENGINE '%s'\n",
  253. f, l, nid, ret->id);
  254. else
  255. OSSL_TRACE3(ENGINE_TABLE,
  256. "%s:%d, nid=%d, caching 'no matching ENGINE'\n",
  257. f, l, nid);
  258. CRYPTO_THREAD_unlock(global_engine_lock);
  259. /*
  260. * Whatever happened, any failed init()s are not failures in this
  261. * context, so clear our error state.
  262. */
  263. ERR_pop_to_mark();
  264. return ret;
  265. }
  266. /* Table enumeration */
  267. static void int_dall(const ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
  268. {
  269. dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
  270. }
  271. IMPLEMENT_LHASH_DOALL_ARG_CONST(ENGINE_PILE, ENGINE_PILE_DOALL);
  272. void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
  273. void *arg)
  274. {
  275. ENGINE_PILE_DOALL dall;
  276. dall.cb = cb;
  277. dall.arg = arg;
  278. if (table)
  279. lh_ENGINE_PILE_doall_ENGINE_PILE_DOALL(&table->piles, int_dall, &dall);
  280. }