2
0

eng_lib.c 6.6 KB

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