eng_lib.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "e_os.h"
  10. #include "eng_int.h"
  11. #include <openssl/rand.h>
  12. #include "internal/refcount.h"
  13. CRYPTO_RWLOCK *global_engine_lock;
  14. CRYPTO_ONCE engine_lock_init = CRYPTO_ONCE_STATIC_INIT;
  15. /* The "new"/"free" stuff first */
  16. DEFINE_RUN_ONCE(do_engine_lock_init)
  17. {
  18. if (!OPENSSL_init_crypto(0, NULL))
  19. return 0;
  20. global_engine_lock = CRYPTO_THREAD_lock_new();
  21. return global_engine_lock != NULL;
  22. }
  23. ENGINE *ENGINE_new(void)
  24. {
  25. ENGINE *ret;
  26. if (!RUN_ONCE(&engine_lock_init, do_engine_lock_init)
  27. || (ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
  28. ENGINEerr(ENGINE_F_ENGINE_NEW, ERR_R_MALLOC_FAILURE);
  29. return NULL;
  30. }
  31. ret->struct_ref = 1;
  32. engine_ref_debug(ret, 0, 1);
  33. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_ENGINE, ret, &ret->ex_data)) {
  34. OPENSSL_free(ret);
  35. return NULL;
  36. }
  37. return ret;
  38. }
  39. /*
  40. * Placed here (close proximity to ENGINE_new) so that modifications to the
  41. * elements of the ENGINE structure are more likely to be caught and changed
  42. * here.
  43. */
  44. void engine_set_all_null(ENGINE *e)
  45. {
  46. e->id = NULL;
  47. e->name = NULL;
  48. e->rsa_meth = NULL;
  49. e->dsa_meth = NULL;
  50. e->dh_meth = NULL;
  51. e->rand_meth = NULL;
  52. e->ciphers = NULL;
  53. e->digests = NULL;
  54. e->destroy = NULL;
  55. e->init = NULL;
  56. e->finish = NULL;
  57. e->ctrl = NULL;
  58. e->load_privkey = NULL;
  59. e->load_pubkey = NULL;
  60. e->cmd_defns = NULL;
  61. e->flags = 0;
  62. }
  63. int engine_free_util(ENGINE *e, int not_locked)
  64. {
  65. int i;
  66. if (e == NULL)
  67. return 1;
  68. #ifdef HAVE_ATOMICS
  69. CRYPTO_DOWN_REF(&e->struct_ref, &i, global_engine_lock);
  70. #else
  71. if (not_locked)
  72. CRYPTO_atomic_add(&e->struct_ref, -1, &i, global_engine_lock);
  73. else
  74. i = --e->struct_ref;
  75. #endif
  76. engine_ref_debug(e, 0, -1);
  77. if (i > 0)
  78. return 1;
  79. REF_ASSERT_ISNT(i < 0);
  80. /* Free up any dynamically allocated public key methods */
  81. engine_pkey_meths_free(e);
  82. engine_pkey_asn1_meths_free(e);
  83. /*
  84. * Give the ENGINE a chance to do any structural cleanup corresponding to
  85. * allocation it did in its constructor (eg. unload error strings)
  86. */
  87. if (e->destroy)
  88. e->destroy(e);
  89. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_ENGINE, e, &e->ex_data);
  90. OPENSSL_free(e);
  91. return 1;
  92. }
  93. int ENGINE_free(ENGINE *e)
  94. {
  95. return engine_free_util(e, 1);
  96. }
  97. /* Cleanup stuff */
  98. /*
  99. * engine_cleanup_int() is coded such that anything that does work that will
  100. * need cleanup can register a "cleanup" callback here. That way we don't get
  101. * linker bloat by referring to all *possible* cleanups, but any linker bloat
  102. * into code "X" will cause X's cleanup function to end up here.
  103. */
  104. static STACK_OF(ENGINE_CLEANUP_ITEM) *cleanup_stack = NULL;
  105. static int int_cleanup_check(int create)
  106. {
  107. if (cleanup_stack)
  108. return 1;
  109. if (!create)
  110. return 0;
  111. cleanup_stack = sk_ENGINE_CLEANUP_ITEM_new_null();
  112. return (cleanup_stack ? 1 : 0);
  113. }
  114. static ENGINE_CLEANUP_ITEM *int_cleanup_item(ENGINE_CLEANUP_CB *cb)
  115. {
  116. ENGINE_CLEANUP_ITEM *item;
  117. if ((item = OPENSSL_malloc(sizeof(*item))) == NULL) {
  118. ENGINEerr(ENGINE_F_INT_CLEANUP_ITEM, ERR_R_MALLOC_FAILURE);
  119. return NULL;
  120. }
  121. item->cb = cb;
  122. return item;
  123. }
  124. void engine_cleanup_add_first(ENGINE_CLEANUP_CB *cb)
  125. {
  126. ENGINE_CLEANUP_ITEM *item;
  127. if (!int_cleanup_check(1))
  128. return;
  129. item = int_cleanup_item(cb);
  130. if (item)
  131. sk_ENGINE_CLEANUP_ITEM_insert(cleanup_stack, item, 0);
  132. }
  133. void engine_cleanup_add_last(ENGINE_CLEANUP_CB *cb)
  134. {
  135. ENGINE_CLEANUP_ITEM *item;
  136. if (!int_cleanup_check(1))
  137. return;
  138. item = int_cleanup_item(cb);
  139. if (item)
  140. sk_ENGINE_CLEANUP_ITEM_push(cleanup_stack, item);
  141. }
  142. /* The API function that performs all cleanup */
  143. static void engine_cleanup_cb_free(ENGINE_CLEANUP_ITEM *item)
  144. {
  145. (*(item->cb)) ();
  146. OPENSSL_free(item);
  147. }
  148. void engine_cleanup_int(void)
  149. {
  150. if (int_cleanup_check(0)) {
  151. sk_ENGINE_CLEANUP_ITEM_pop_free(cleanup_stack,
  152. engine_cleanup_cb_free);
  153. cleanup_stack = NULL;
  154. }
  155. CRYPTO_THREAD_lock_free(global_engine_lock);
  156. }
  157. /* Now the "ex_data" support */
  158. int ENGINE_set_ex_data(ENGINE *e, int idx, void *arg)
  159. {
  160. return CRYPTO_set_ex_data(&e->ex_data, idx, arg);
  161. }
  162. void *ENGINE_get_ex_data(const ENGINE *e, int idx)
  163. {
  164. return CRYPTO_get_ex_data(&e->ex_data, idx);
  165. }
  166. /*
  167. * Functions to get/set an ENGINE's elements - mainly to avoid exposing the
  168. * ENGINE structure itself.
  169. */
  170. int ENGINE_set_id(ENGINE *e, const char *id)
  171. {
  172. if (id == NULL) {
  173. ENGINEerr(ENGINE_F_ENGINE_SET_ID, ERR_R_PASSED_NULL_PARAMETER);
  174. return 0;
  175. }
  176. e->id = id;
  177. return 1;
  178. }
  179. int ENGINE_set_name(ENGINE *e, const char *name)
  180. {
  181. if (name == NULL) {
  182. ENGINEerr(ENGINE_F_ENGINE_SET_NAME, ERR_R_PASSED_NULL_PARAMETER);
  183. return 0;
  184. }
  185. e->name = name;
  186. return 1;
  187. }
  188. int ENGINE_set_destroy_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR destroy_f)
  189. {
  190. e->destroy = destroy_f;
  191. return 1;
  192. }
  193. int ENGINE_set_init_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR init_f)
  194. {
  195. e->init = init_f;
  196. return 1;
  197. }
  198. int ENGINE_set_finish_function(ENGINE *e, ENGINE_GEN_INT_FUNC_PTR finish_f)
  199. {
  200. e->finish = finish_f;
  201. return 1;
  202. }
  203. int ENGINE_set_ctrl_function(ENGINE *e, ENGINE_CTRL_FUNC_PTR ctrl_f)
  204. {
  205. e->ctrl = ctrl_f;
  206. return 1;
  207. }
  208. int ENGINE_set_flags(ENGINE *e, int flags)
  209. {
  210. e->flags = flags;
  211. return 1;
  212. }
  213. int ENGINE_set_cmd_defns(ENGINE *e, const ENGINE_CMD_DEFN *defns)
  214. {
  215. e->cmd_defns = defns;
  216. return 1;
  217. }
  218. const char *ENGINE_get_id(const ENGINE *e)
  219. {
  220. return e->id;
  221. }
  222. const char *ENGINE_get_name(const ENGINE *e)
  223. {
  224. return e->name;
  225. }
  226. ENGINE_GEN_INT_FUNC_PTR ENGINE_get_destroy_function(const ENGINE *e)
  227. {
  228. return e->destroy;
  229. }
  230. ENGINE_GEN_INT_FUNC_PTR ENGINE_get_init_function(const ENGINE *e)
  231. {
  232. return e->init;
  233. }
  234. ENGINE_GEN_INT_FUNC_PTR ENGINE_get_finish_function(const ENGINE *e)
  235. {
  236. return e->finish;
  237. }
  238. ENGINE_CTRL_FUNC_PTR ENGINE_get_ctrl_function(const ENGINE *e)
  239. {
  240. return e->ctrl;
  241. }
  242. int ENGINE_get_flags(const ENGINE *e)
  243. {
  244. return e->flags;
  245. }
  246. const ENGINE_CMD_DEFN *ENGINE_get_cmd_defns(const ENGINE *e)
  247. {
  248. return e->cmd_defns;
  249. }
  250. /*
  251. * eng_lib.o is pretty much linked into anything that touches ENGINE already,
  252. * so put the "static_state" hack here.
  253. */
  254. static int internal_static_hack = 0;
  255. void *ENGINE_get_static_state(void)
  256. {
  257. return &internal_static_hack;
  258. }