eng_table.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. {
  61. /* The 'nid' of this algorithm/mode */
  62. int nid;
  63. /* ENGINEs that implement this algorithm/mode. */
  64. STACK_OF(ENGINE) *sk;
  65. /* The default ENGINE to perform this algorithm/mode. */
  66. ENGINE *funct;
  67. /* Zero if 'sk' is newer than the cached 'funct', non-zero otherwise */
  68. int uptodate;
  69. } ENGINE_PILE;
  70. DECLARE_LHASH_OF(ENGINE_PILE);
  71. /* The type exposed in eng_int.h */
  72. struct st_engine_table
  73. {
  74. LHASH_OF(ENGINE_PILE) piles;
  75. }; /* ENGINE_TABLE */
  76. typedef struct st_engine_pile_doall
  77. {
  78. engine_table_doall_cb *cb;
  79. void *arg;
  80. } ENGINE_PILE_DOALL;
  81. /* Global flags (ENGINE_TABLE_FLAG_***). */
  82. static unsigned int table_flags = 0;
  83. /* API function manipulating 'table_flags' */
  84. unsigned int ENGINE_get_table_flags(void)
  85. {
  86. return table_flags;
  87. }
  88. void ENGINE_set_table_flags(unsigned int flags)
  89. {
  90. table_flags = flags;
  91. }
  92. /* Internal functions for the "piles" hash table */
  93. static unsigned long engine_pile_hash(const ENGINE_PILE *c)
  94. {
  95. return c->nid;
  96. }
  97. static int engine_pile_cmp(const ENGINE_PILE *a, const ENGINE_PILE *b)
  98. {
  99. return a->nid - b->nid;
  100. }
  101. static IMPLEMENT_LHASH_HASH_FN(engine_pile, const ENGINE_PILE)
  102. static IMPLEMENT_LHASH_COMP_FN(engine_pile, const ENGINE_PILE)
  103. static int int_table_check(ENGINE_TABLE **t, int create)
  104. {
  105. LHASH_OF(ENGINE_PILE) *lh;
  106. if(*t) return 1;
  107. if(!create) return 0;
  108. if((lh = lh_ENGINE_PILE_new()) == NULL)
  109. return 0;
  110. *t = (ENGINE_TABLE *)lh;
  111. return 1;
  112. }
  113. /* Privately exposed (via eng_int.h) functions for adding and/or removing
  114. * ENGINEs from the implementation table */
  115. int engine_table_register(ENGINE_TABLE **table, ENGINE_CLEANUP_CB *cleanup,
  116. ENGINE *e, const int *nids, int num_nids, int setdefault)
  117. {
  118. int ret = 0, added = 0;
  119. ENGINE_PILE tmplate, *fnd;
  120. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  121. if(!(*table))
  122. added = 1;
  123. if(!int_table_check(table, 1))
  124. goto end;
  125. if(added)
  126. /* The cleanup callback needs to be added */
  127. engine_cleanup_add_first(cleanup);
  128. while(num_nids--)
  129. {
  130. tmplate.nid = *nids;
  131. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  132. if(!fnd)
  133. {
  134. fnd = OPENSSL_malloc(sizeof(ENGINE_PILE));
  135. if(!fnd) goto end;
  136. fnd->uptodate = 1;
  137. fnd->nid = *nids;
  138. fnd->sk = sk_ENGINE_new_null();
  139. if(!fnd->sk)
  140. {
  141. OPENSSL_free(fnd);
  142. goto end;
  143. }
  144. fnd->funct = NULL;
  145. lh_ENGINE_PILE_insert(&(*table)->piles, fnd);
  146. }
  147. /* A registration shouldn't add duplciate entries */
  148. (void)sk_ENGINE_delete_ptr(fnd->sk, e);
  149. /* if 'setdefault', this ENGINE goes to the head of the list */
  150. if(!sk_ENGINE_push(fnd->sk, e))
  151. goto end;
  152. /* "touch" this ENGINE_PILE */
  153. fnd->uptodate = 0;
  154. if(setdefault)
  155. {
  156. if(!engine_unlocked_init(e))
  157. {
  158. ENGINEerr(ENGINE_F_ENGINE_TABLE_REGISTER,
  159. ENGINE_R_INIT_FAILED);
  160. goto end;
  161. }
  162. if(fnd->funct)
  163. engine_unlocked_finish(fnd->funct, 0);
  164. fnd->funct = e;
  165. fnd->uptodate = 1;
  166. }
  167. nids++;
  168. }
  169. ret = 1;
  170. end:
  171. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  172. return ret;
  173. }
  174. static void int_unregister_cb_doall_arg(ENGINE_PILE *pile, ENGINE *e)
  175. {
  176. int n;
  177. /* Iterate the 'c->sk' stack removing any occurance of 'e' */
  178. while((n = sk_ENGINE_find(pile->sk, e)) >= 0)
  179. {
  180. (void)sk_ENGINE_delete(pile->sk, n);
  181. pile->uptodate = 0;
  182. }
  183. if(pile->funct == e)
  184. {
  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. {
  212. lh_ENGINE_PILE_doall(&(*table)->piles,
  213. LHASH_DOALL_FN(int_cleanup_cb));
  214. lh_ENGINE_PILE_free(&(*table)->piles);
  215. *table = NULL;
  216. }
  217. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  218. }
  219. /* return a functional reference for a given 'nid' */
  220. #ifndef ENGINE_TABLE_DEBUG
  221. ENGINE *engine_table_select(ENGINE_TABLE **table, int nid)
  222. #else
  223. ENGINE *engine_table_select_tmp(ENGINE_TABLE **table, int nid, const char *f, int l)
  224. #endif
  225. {
  226. ENGINE *ret = NULL;
  227. ENGINE_PILE tmplate, *fnd=NULL;
  228. int initres, loop = 0;
  229. if(!(*table))
  230. {
  231. #ifdef ENGINE_TABLE_DEBUG
  232. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, nothing "
  233. "registered!\n", f, l, nid);
  234. #endif
  235. return NULL;
  236. }
  237. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  238. /* Check again inside the lock otherwise we could race against cleanup
  239. * operations. But don't worry about a fprintf(stderr). */
  240. if(!int_table_check(table, 0)) goto end;
  241. tmplate.nid = nid;
  242. fnd = lh_ENGINE_PILE_retrieve(&(*table)->piles, &tmplate);
  243. if(!fnd) goto end;
  244. if(fnd->funct && engine_unlocked_init(fnd->funct))
  245. {
  246. #ifdef ENGINE_TABLE_DEBUG
  247. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
  248. "ENGINE '%s' cached\n", f, l, nid, fnd->funct->id);
  249. #endif
  250. ret = fnd->funct;
  251. goto end;
  252. }
  253. if(fnd->uptodate)
  254. {
  255. ret = fnd->funct;
  256. goto end;
  257. }
  258. trynext:
  259. ret = sk_ENGINE_value(fnd->sk, loop++);
  260. if(!ret)
  261. {
  262. #ifdef ENGINE_TABLE_DEBUG
  263. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, no "
  264. "registered implementations would initialise\n",
  265. 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. {
  276. /* Update 'funct' */
  277. if((fnd->funct != ret) && engine_unlocked_init(ret))
  278. {
  279. /* If there was a previous default we release it. */
  280. if(fnd->funct)
  281. engine_unlocked_finish(fnd->funct, 0);
  282. fnd->funct = ret;
  283. #ifdef ENGINE_TABLE_DEBUG
  284. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, "
  285. "setting default to '%s'\n", f, l, nid, ret->id);
  286. #endif
  287. }
  288. #ifdef ENGINE_TABLE_DEBUG
  289. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, using "
  290. "newly initialised '%s'\n", f, l, nid, ret->id);
  291. #endif
  292. goto end;
  293. }
  294. goto trynext;
  295. end:
  296. /* If it failed, it is unlikely to succeed again until some future
  297. * registrations have taken place. In all cases, we cache. */
  298. if(fnd) fnd->uptodate = 1;
  299. #ifdef ENGINE_TABLE_DEBUG
  300. if(ret)
  301. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
  302. "ENGINE '%s'\n", f, l, nid, ret->id);
  303. else
  304. fprintf(stderr, "engine_table_dbg: %s:%d, nid=%d, caching "
  305. "'no matching ENGINE'\n", f, l, nid);
  306. #endif
  307. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  308. /* Whatever happened, any failed init()s are not failures in this
  309. * context, so clear our error state. */
  310. ERR_clear_error();
  311. return ret;
  312. }
  313. /* Table enumeration */
  314. static void int_cb_doall_arg(ENGINE_PILE *pile, ENGINE_PILE_DOALL *dall)
  315. {
  316. dall->cb(pile->nid, pile->sk, pile->funct, dall->arg);
  317. }
  318. static IMPLEMENT_LHASH_DOALL_ARG_FN(int_cb, ENGINE_PILE,ENGINE_PILE_DOALL)
  319. void engine_table_doall(ENGINE_TABLE *table, engine_table_doall_cb *cb,
  320. void *arg)
  321. {
  322. ENGINE_PILE_DOALL dall;
  323. dall.cb = cb;
  324. dall.arg = arg;
  325. lh_ENGINE_PILE_doall_arg(&table->piles, LHASH_DOALL_ARG_FN(int_cb),
  326. ENGINE_PILE_DOALL, &dall);
  327. }