eng_table.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* ====================================================================
  2. * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * licensing@OpenSSL.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This product includes cryptographic software written by Eric Young
  50. * (eay@cryptsoft.com). This product includes software written by Tim
  51. * Hudson (tjh@cryptsoft.com).
  52. *
  53. */
  54. #include "cryptlib.h"
  55. #include <openssl/evp.h>
  56. #include <openssl/lhash.h>
  57. #include "eng_int.h"
  58. /* The type of the items in the table */
  59. typedef struct st_engine_pile {
  60. /* The 'nid' of this algorithm/mode */
  61. int nid;
  62. /* ENGINEs that implement this algorithm/mode. */
  63. STACK_OF(ENGINE) *sk;
  64. /* The default ENGINE to perform this algorithm/mode. */
  65. ENGINE *funct;
  66. /*
  67. * Zero if 'sk' is newer than the cached 'funct', non-zero otherwise
  68. */
  69. int uptodate;
  70. } ENGINE_PILE;
  71. DECLARE_LHASH_OF(ENGINE_PILE);
  72. /* The type exposed in eng_int.h */
  73. struct st_engine_table {
  74. LHASH_OF(ENGINE_PILE) piles;
  75. }; /* ENGINE_TABLE */
  76. typedef struct st_engine_pile_doall {
  77. engine_table_doall_cb *cb;
  78. void *arg;
  79. } ENGINE_PILE_DOALL;
  80. /* Global flags (ENGINE_TABLE_FLAG_***). */
  81. static unsigned int table_flags = 0;
  82. /* API function manipulating 'table_flags' */
  83. unsigned int ENGINE_get_table_flags(void)
  84. {
  85. return table_flags;
  86. }
  87. void ENGINE_set_table_flags(unsigned int flags)
  88. {
  89. table_flags = flags;
  90. }
  91. /* Internal functions for the "piles" hash table */
  92. static unsigned long engine_pile_hash(const ENGINE_PILE *c)
  93. {
  94. return c->nid;
  95. }
  96. static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
  97. {
  98. return a->nid - b->nid;
  99. }
  100. static IMPLEMENT_LHASH_HASH_FN(engine_pile, ENGINE_PILE)
  101. static IMPLEMENT_LHASH_COMP_FN(engine_pile, ENGINE_PILE)
  102. static int int_table_check(ENGINE_TABLE **t, int create)
  103. {
  104. LHASH_OF(ENGINE_PILE) *lh;
  105. if (*t)
  106. return 1;
  107. if (!create)
  108. return 0;
  109. if ((lh = lh_ENGINE_PILE_new()) == NULL)
  110. return 0;
  111. *t = (ENGINE_TABLE *)lh;
  112. return 1;
  113. }
  114. /*
  115. * Privately exposed (via eng_int.h) functions for adding and/or removing
  116. * ENGINEs from the implementation table
  117. */
  118. int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
  119. ENGINE *e, const int *nids, int num_nids,
  120. int setdefault)
  121. {
  122. int ret = 0, added = 0;
  123. ENGINE_PILE tmplate, *fnd;
  124. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  125. if (!(*table))
  126. added = 1;
  127. if (!int_table_check(table, 1))
  128. goto end;
  129. if (added)
  130. /* The cleanup callback needs to be added */
  131. engine_cleanup_add_first(cleanup);
  132. while (num_nids--) {
  133. tmplate.nid = *nids;
  134. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  135. if (!fnd) {
  136. fnd = OPENSSL_malloc(sizeof(ENGINE_PILE));
  137. if (!fnd)
  138. goto end;
  139. fnd->uptodate = 1;
  140. fnd->nid = *nids;
  141. fnd->sk = sk_ENGINE_new_null();
  142. if (!fnd->sk) {
  143. OPENSSL_free(fnd);
  144. goto end;
  145. }
  146. fnd->funct = NULL;
  147. (void)lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
  148. }
  149. /* A registration shouldn't add duplciate entries */
  150. (void)sk_ENGINE_delete_ptr(fnd->sk, e);
  151. /*
  152. * if 'setdefault', this ENGINE goes to the head of the list
  153. */
  154. if (!sk_ENGINE_push(fnd->sk, e))
  155. goto end;
  156. /* "touch" this ENGINE_PILE */
  157. fnd->uptodate = 0;
  158. if (setdefault) {
  159. if (!engine_unlocked_init(e)) {
  160. ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER,
  161. ENGINE_R_INIT_FAILED);
  162. goto end;
  163. }
  164. if (fnd->funct)
  165. engine_unlocked_finish(fnd->funct, 0);
  166. fnd->funct = e;
  167. fnd->uptodate = 1;
  168. }
  169. nids++;
  170. }
  171. ret = 1;
  172. end:
  173. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  174. return ret;
  175. }
  176. static void int_unregister_cb_doall_arg(ENGINE_PILE *pile, ENGINE *e)
  177. {
  178. int n;
  179. /* Iterate the 'c->sk' stack removing any occurance of 'e' */
  180. while ((n = sk_ENGINE_find(pile->sk, e)) >= 0) {
  181. (void)sk_ENGINE_delete(pile->sk, n);
  182. pile->uptodate = 0;
  183. }
  184. if (pile->funct == e) {
  185. engine_unlocked_finish(e, 0);
  186. pile->funct = NULL;
  187. }
  188. }
  189. static IMPLEMENT_LHASH_DOALL_ARG_FN(int_unregister_cb, ENGINE_PILE, ENGINE)
  190. void engine_table_unregister(ENGINE_TABLE **table, ENGINE *e)
  191. {
  192. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  193. if (int_table_check(table, 0))
  194. lh_ENGINE_PILE_doall_arg(&(*table)->piles,
  195. LHASH_DOALL_ARG_FN(int_unregister_cb),
  196. ENGINE, e);
  197. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  198. }
  199. static void int_cleanup_cb_doall(ENGINE_PILE *p)
  200. {
  201. sk_ENGINE_free(p->sk);
  202. if (p->funct)
  203. engine_unlocked_finish(p->funct, 0);
  204. OPENSSL_free(p);
  205. }
  206. static IMPLEMENT_LHASH_DOALL_FN(int_cleanup_cb, ENGINE_PILE)
  207. void engine_table_cleanup(ENGINE_TABLE **table)
  208. {
  209. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  210. if (*table) {
  211. lh_ENGINE_PILE_doall(&(*table)->piles,
  212. LHASH_DOALL_FN(int_cleanup_cb));
  213. lh_ENGINE_PILE_free(&(*table)->piles);
  214. *table = NULL;
  215. }
  216. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  217. }
  218. /* return a functional reference for a given 'nid' */
  219. #ifndef ENGINE_TABLE_DEBUG
  220. ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)
  221. #else
  222. ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f,
  223. int l)
  224. #endif
  225. {
  226. ENGINE *ret = NULL;
  227. ENGINE_PILE tmplate, *fnd = NULL;
  228. int initres, loop = 0;
  229. if (!(*table)) {
  230. #ifdef ENGINE_TABLE_DEBUG
  231. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "
  232. "registered!\n", f, l, nid);
  233. #endif
  234. return NULL;
  235. }
  236. ERR_set_mark();
  237. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  238. /*
  239. * Check again inside the lock otherwise we could race against cleanup
  240. * operations. But don't worry about a fprintf(stderr).
  241. */
  242. if (!int_table_check(table, 0))
  243. goto end;
  244. tmplate.nid = nid;
  245. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  246. if (!fnd)
  247. goto end;
  248. if (fnd->funct && engine_unlocked_init(fnd->funct)) {
  249. #ifdef ENGINE_TABLE_DEBUG
  250. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
  251. "ENGINE '%s' cached\n", f, l, nid, fnd->funct->id);
  252. #endif
  253. ret = fnd->funct;
  254. goto end;
  255. }
  256. if (fnd->uptodate) {
  257. ret = fnd->funct;
  258. goto end;
  259. }
  260. trynext:
  261. ret = sk_ENGINE_value(fnd->sk, loop++);
  262. if (!ret) {
  263. #ifdef ENGINE_TABLE_DEBUG
  264. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "
  265. "registered implementations would initialise\n", f, l, nid);
  266. #endif
  267. goto end;
  268. }
  269. /* Try to initialise the ENGINE? */
  270. if ((ret->funct_ref > 0) || !(table_flags & ENGINE_TABLE_FLAG_NOINIT))
  271. initres = engine_unlocked_init(ret);
  272. else
  273. initres = 0;
  274. if (initres) {
  275. /* Update 'funct' */
  276. if ((fnd->funct != ret) && engine_unlocked_init(ret)) {
  277. /* If there was a previous default we release it. */
  278. if (fnd->funct)
  279. engine_unlocked_finish(fnd->funct, 0);
  280. fnd->funct = ret;
  281. #ifdef ENGINE_TABLE_DEBUG
  282. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "
  283. "setting default to '%s'\n", f, l, nid, ret->id);
  284. #endif
  285. }
  286. #ifdef ENGINE_TABLE_DEBUG
  287. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
  288. "newly initialised '%s'\n", f, l, nid, ret->id);
  289. #endif
  290. goto end;
  291. }
  292. goto trynext;
  293. end:
  294. /*
  295. * If it failed, it is unlikely to succeed again until some future
  296. * registrations have taken place. In all cases, we cache.
  297. */
  298. if (fnd)
  299. fnd->uptodate = 1;
  300. #ifdef ENGINE_TABLE_DEBUG
  301. if (ret)
  302. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
  303. "ENGINE '%s'\n", f, l, nid, ret->id);
  304. else
  305. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
  306. "'no matching ENGINE'\n", f, l, nid);
  307. #endif
  308. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  309. /*
  310. * Whatever happened, any failed init()s are not failures in this
  311. * context, so clear our error state.
  312. */
  313. ERR_pop_to_mark();
  314. return ret;
  315. }
  316. /* Table enumeration */
  317. static void int_cb_doall_arg(ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
  318. {
  319. dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
  320. }
  321. static IMPLEMENT_LHASH_DOALL_ARG_FN(int_cb, ENGINE_PILE, ENGINE_PILE_DOALL)
  322. void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
  323. void *arg)
  324. {
  325. ENGINE_PILE_DOALL dall;
  326. dall.cb = cb;
  327. dall.arg = arg;
  328. if (table)
  329. lh_ENGINE_PILE_doall_arg(&table->piles,
  330. LHASH_DOALL_ARG_FN(int_cb),
  331. ENGINE_PILE_DOALL, &dall);
  332. }