A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

438 lines
12 KiB

  1. /*
  2. * Copyright 2019-2020 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 <openssl/crypto.h>
  10. #include <openssl/core_dispatch.h>
  11. #include "crypto/cryptlib.h"
  12. #include "prov/providercommon.h"
  13. #include "internal/thread_once.h"
  14. #ifdef FIPS_MODULE
  15. #include "prov/provider_ctx.h"
  16. /*
  17. * Thread aware code may want to be told about thread stop events. We register
  18. * to hear about those thread stop events when we see a new thread has started.
  19. * We call the ossl_init_thread_start function to do that. In the FIPS provider
  20. * we have our own copy of ossl_init_thread_start, which cascades notifications
  21. * about threads stopping from libcrypto to all the code in the FIPS provider
  22. * that needs to know about it.
  23. *
  24. * The FIPS provider tells libcrypto about which threads it is interested in
  25. * by calling "c_thread_start" which is a function pointer created during
  26. * provider initialisation (i.e. OSSL_init_provider).
  27. */
  28. extern OSSL_FUNC_core_thread_start_fn *c_thread_start;
  29. #endif
  30. typedef struct thread_event_handler_st THREAD_EVENT_HANDLER;
  31. struct thread_event_handler_st {
  32. const void *index;
  33. void *arg;
  34. OSSL_thread_stop_handler_fn handfn;
  35. THREAD_EVENT_HANDLER *next;
  36. };
  37. #ifndef FIPS_MODULE
  38. DEFINE_SPECIAL_STACK_OF(THREAD_EVENT_HANDLER_PTR, THREAD_EVENT_HANDLER *)
  39. typedef struct global_tevent_register_st GLOBAL_TEVENT_REGISTER;
  40. struct global_tevent_register_st {
  41. STACK_OF(THREAD_EVENT_HANDLER_PTR) *skhands;
  42. CRYPTO_RWLOCK *lock;
  43. };
  44. static GLOBAL_TEVENT_REGISTER *glob_tevent_reg = NULL;
  45. static CRYPTO_ONCE tevent_register_runonce = CRYPTO_ONCE_STATIC_INIT;
  46. DEFINE_RUN_ONCE_STATIC(create_global_tevent_register)
  47. {
  48. glob_tevent_reg = OPENSSL_zalloc(sizeof(*glob_tevent_reg));
  49. if (glob_tevent_reg == NULL)
  50. return 0;
  51. glob_tevent_reg->skhands = sk_THREAD_EVENT_HANDLER_PTR_new_null();
  52. glob_tevent_reg->lock = CRYPTO_THREAD_lock_new();
  53. if (glob_tevent_reg->skhands == NULL || glob_tevent_reg->lock == NULL) {
  54. sk_THREAD_EVENT_HANDLER_PTR_free(glob_tevent_reg->skhands);
  55. CRYPTO_THREAD_lock_free(glob_tevent_reg->lock);
  56. OPENSSL_free(glob_tevent_reg);
  57. glob_tevent_reg = NULL;
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. static GLOBAL_TEVENT_REGISTER *get_global_tevent_register(void)
  63. {
  64. if (!RUN_ONCE(&tevent_register_runonce, create_global_tevent_register))
  65. return NULL;
  66. return glob_tevent_reg;
  67. }
  68. #endif
  69. #ifndef FIPS_MODULE
  70. static int init_thread_push_handlers(THREAD_EVENT_HANDLER **hands);
  71. static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin);
  72. static void init_thread_destructor(void *hands);
  73. static int init_thread_deregister(void *arg, int all);
  74. #endif
  75. static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands);
  76. static THREAD_EVENT_HANDLER **
  77. init_get_thread_local(CRYPTO_THREAD_LOCAL *local, int alloc, int keep)
  78. {
  79. THREAD_EVENT_HANDLER **hands = CRYPTO_THREAD_get_local(local);
  80. if (alloc) {
  81. if (hands == NULL) {
  82. if ((hands = OPENSSL_zalloc(sizeof(*hands))) == NULL)
  83. return NULL;
  84. if (!CRYPTO_THREAD_set_local(local, hands)) {
  85. OPENSSL_free(hands);
  86. return NULL;
  87. }
  88. #ifndef FIPS_MODULE
  89. if (!init_thread_push_handlers(hands)) {
  90. CRYPTO_THREAD_set_local(local, NULL);
  91. OPENSSL_free(hands);
  92. return NULL;
  93. }
  94. #endif
  95. }
  96. } else if (!keep) {
  97. CRYPTO_THREAD_set_local(local, NULL);
  98. }
  99. return hands;
  100. }
  101. #ifndef FIPS_MODULE
  102. /*
  103. * Since per-thread-specific-data destructors are not universally
  104. * available, i.e. not on Windows, only below CRYPTO_THREAD_LOCAL key
  105. * is assumed to have destructor associated. And then an effort is made
  106. * to call this single destructor on non-pthread platform[s].
  107. *
  108. * Initial value is "impossible". It is used as guard value to shortcut
  109. * destructor for threads terminating before libcrypto is initialized or
  110. * after it's de-initialized. Access to the key doesn't have to be
  111. * serialized for the said threads, because they didn't use libcrypto
  112. * and it doesn't matter if they pick "impossible" or dereference real
  113. * key value and pull NULL past initialization in the first thread that
  114. * intends to use libcrypto.
  115. */
  116. static union {
  117. long sane;
  118. CRYPTO_THREAD_LOCAL value;
  119. } destructor_key = { -1 };
  120. /*
  121. * The thread event handler list is a thread specific linked list
  122. * of callback functions which are invoked in list order by the
  123. * current thread in case of certain events. (Currently, there is
  124. * only one type of event, the 'thread stop' event.)
  125. *
  126. * We also keep a global reference to that linked list, so that we
  127. * can deregister handlers if necessary before all the threads are
  128. * stopped.
  129. */
  130. static int init_thread_push_handlers(THREAD_EVENT_HANDLER **hands)
  131. {
  132. int ret;
  133. GLOBAL_TEVENT_REGISTER *gtr;
  134. gtr = get_global_tevent_register();
  135. if (gtr == NULL)
  136. return 0;
  137. CRYPTO_THREAD_write_lock(gtr->lock);
  138. ret = (sk_THREAD_EVENT_HANDLER_PTR_push(gtr->skhands, hands) != 0);
  139. CRYPTO_THREAD_unlock(gtr->lock);
  140. return ret;
  141. }
  142. static void init_thread_remove_handlers(THREAD_EVENT_HANDLER **handsin)
  143. {
  144. GLOBAL_TEVENT_REGISTER *gtr;
  145. int i;
  146. gtr = get_global_tevent_register();
  147. if (gtr == NULL)
  148. return;
  149. CRYPTO_THREAD_write_lock(gtr->lock);
  150. for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
  151. THREAD_EVENT_HANDLER **hands
  152. = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
  153. if (hands == handsin) {
  154. sk_THREAD_EVENT_HANDLER_PTR_delete(gtr->skhands, i);
  155. CRYPTO_THREAD_unlock(gtr->lock);
  156. return;
  157. }
  158. }
  159. CRYPTO_THREAD_unlock(gtr->lock);
  160. return;
  161. }
  162. static void init_thread_destructor(void *hands)
  163. {
  164. init_thread_stop(NULL, (THREAD_EVENT_HANDLER **)hands);
  165. init_thread_remove_handlers(hands);
  166. OPENSSL_free(hands);
  167. }
  168. int ossl_init_thread(void)
  169. {
  170. if (!CRYPTO_THREAD_init_local(&destructor_key.value,
  171. init_thread_destructor))
  172. return 0;
  173. return 1;
  174. }
  175. void ossl_cleanup_thread(void)
  176. {
  177. init_thread_deregister(NULL, 1);
  178. CRYPTO_THREAD_cleanup_local(&destructor_key.value);
  179. destructor_key.sane = -1;
  180. }
  181. void OPENSSL_thread_stop_ex(OSSL_LIB_CTX *ctx)
  182. {
  183. ctx = ossl_lib_ctx_get_concrete(ctx);
  184. /*
  185. * TODO(3.0). It would be nice if we could figure out a way to do this on
  186. * all threads that have used the OSSL_LIB_CTX when the context is freed.
  187. * This is currently not possible due to the use of thread local variables.
  188. */
  189. ossl_ctx_thread_stop(ctx);
  190. }
  191. void OPENSSL_thread_stop(void)
  192. {
  193. if (destructor_key.sane != -1) {
  194. THREAD_EVENT_HANDLER **hands
  195. = init_get_thread_local(&destructor_key.value, 0, 0);
  196. init_thread_stop(NULL, hands);
  197. init_thread_remove_handlers(hands);
  198. OPENSSL_free(hands);
  199. }
  200. }
  201. void ossl_ctx_thread_stop(void *arg)
  202. {
  203. if (destructor_key.sane != -1) {
  204. THREAD_EVENT_HANDLER **hands
  205. = init_get_thread_local(&destructor_key.value, 0, 1);
  206. init_thread_stop(arg, hands);
  207. }
  208. }
  209. #else
  210. static void *thread_event_ossl_ctx_new(OSSL_LIB_CTX *libctx)
  211. {
  212. THREAD_EVENT_HANDLER **hands = NULL;
  213. CRYPTO_THREAD_LOCAL *tlocal = OPENSSL_zalloc(sizeof(*tlocal));
  214. if (tlocal == NULL)
  215. return NULL;
  216. if (!CRYPTO_THREAD_init_local(tlocal, NULL)) {
  217. goto err;
  218. }
  219. hands = OPENSSL_zalloc(sizeof(*hands));
  220. if (hands == NULL)
  221. goto err;
  222. if (!CRYPTO_THREAD_set_local(tlocal, hands))
  223. goto err;
  224. return tlocal;
  225. err:
  226. OPENSSL_free(hands);
  227. OPENSSL_free(tlocal);
  228. return NULL;
  229. }
  230. static void thread_event_ossl_ctx_free(void *tlocal)
  231. {
  232. OPENSSL_free(tlocal);
  233. }
  234. static const OSSL_LIB_CTX_METHOD thread_event_ossl_ctx_method = {
  235. thread_event_ossl_ctx_new,
  236. thread_event_ossl_ctx_free,
  237. };
  238. void ossl_ctx_thread_stop(void *arg)
  239. {
  240. THREAD_EVENT_HANDLER **hands;
  241. OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(arg);
  242. CRYPTO_THREAD_LOCAL *local
  243. = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX,
  244. &thread_event_ossl_ctx_method);
  245. if (local == NULL)
  246. return;
  247. hands = init_get_thread_local(local, 0, 0);
  248. init_thread_stop(ctx, hands);
  249. OPENSSL_free(hands);
  250. }
  251. #endif /* FIPS_MODULE */
  252. static void init_thread_stop(void *arg, THREAD_EVENT_HANDLER **hands)
  253. {
  254. THREAD_EVENT_HANDLER *curr, *prev = NULL, *tmp;
  255. /* Can't do much about this */
  256. if (hands == NULL)
  257. return;
  258. curr = *hands;
  259. while (curr != NULL) {
  260. if (arg != NULL && curr->arg != arg) {
  261. prev = curr;
  262. curr = curr->next;
  263. continue;
  264. }
  265. curr->handfn(curr->arg);
  266. if (prev == NULL)
  267. *hands = curr->next;
  268. else
  269. prev->next = curr->next;
  270. tmp = curr;
  271. curr = curr->next;
  272. OPENSSL_free(tmp);
  273. }
  274. }
  275. int ossl_init_thread_start(const void *index, void *arg,
  276. OSSL_thread_stop_handler_fn handfn)
  277. {
  278. THREAD_EVENT_HANDLER **hands;
  279. THREAD_EVENT_HANDLER *hand;
  280. #ifdef FIPS_MODULE
  281. OSSL_LIB_CTX *ctx = arg;
  282. /*
  283. * In FIPS mode the list of THREAD_EVENT_HANDLERs is unique per combination
  284. * of OSSL_LIB_CTX and thread. This is because in FIPS mode each
  285. * OSSL_LIB_CTX gets informed about thread stop events individually.
  286. */
  287. CRYPTO_THREAD_LOCAL *local
  288. = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_THREAD_EVENT_HANDLER_INDEX,
  289. &thread_event_ossl_ctx_method);
  290. #else
  291. /*
  292. * Outside of FIPS mode the list of THREAD_EVENT_HANDLERs is unique per
  293. * thread, but may hold multiple OSSL_LIB_CTXs. We only get told about
  294. * thread stop events globally, so we have to ensure all affected
  295. * OSSL_LIB_CTXs are informed.
  296. */
  297. CRYPTO_THREAD_LOCAL *local = &destructor_key.value;
  298. #endif
  299. hands = init_get_thread_local(local, 1, 0);
  300. if (hands == NULL)
  301. return 0;
  302. #ifdef FIPS_MODULE
  303. if (*hands == NULL) {
  304. /*
  305. * We've not yet registered any handlers for this thread. We need to get
  306. * libcrypto to tell us about later thread stop events. c_thread_start
  307. * is a callback to libcrypto defined in fipsprov.c
  308. */
  309. if (!c_thread_start(FIPS_get_core_handle(ctx), ossl_ctx_thread_stop))
  310. return 0;
  311. }
  312. #endif
  313. hand = OPENSSL_malloc(sizeof(*hand));
  314. if (hand == NULL)
  315. return 0;
  316. hand->handfn = handfn;
  317. hand->arg = arg;
  318. hand->index = index;
  319. hand->next = *hands;
  320. *hands = hand;
  321. return 1;
  322. }
  323. #ifndef FIPS_MODULE
  324. static int init_thread_deregister(void *index, int all)
  325. {
  326. GLOBAL_TEVENT_REGISTER *gtr;
  327. int i;
  328. gtr = get_global_tevent_register();
  329. if (gtr == NULL)
  330. return 0;
  331. if (!all)
  332. CRYPTO_THREAD_write_lock(gtr->lock);
  333. else
  334. glob_tevent_reg = NULL;
  335. for (i = 0; i < sk_THREAD_EVENT_HANDLER_PTR_num(gtr->skhands); i++) {
  336. THREAD_EVENT_HANDLER **hands
  337. = sk_THREAD_EVENT_HANDLER_PTR_value(gtr->skhands, i);
  338. THREAD_EVENT_HANDLER *curr = NULL, *prev = NULL, *tmp;
  339. if (hands == NULL) {
  340. if (!all)
  341. CRYPTO_THREAD_unlock(gtr->lock);
  342. return 0;
  343. }
  344. curr = *hands;
  345. while (curr != NULL) {
  346. if (all || curr->index == index) {
  347. if (prev != NULL)
  348. prev->next = curr->next;
  349. else
  350. *hands = curr->next;
  351. tmp = curr;
  352. curr = curr->next;
  353. OPENSSL_free(tmp);
  354. continue;
  355. }
  356. prev = curr;
  357. curr = curr->next;
  358. }
  359. if (all)
  360. OPENSSL_free(hands);
  361. }
  362. if (all) {
  363. CRYPTO_THREAD_lock_free(gtr->lock);
  364. sk_THREAD_EVENT_HANDLER_PTR_free(gtr->skhands);
  365. OPENSSL_free(gtr);
  366. } else {
  367. CRYPTO_THREAD_unlock(gtr->lock);
  368. }
  369. return 1;
  370. }
  371. int ossl_init_thread_deregister(void *index)
  372. {
  373. return init_thread_deregister(index, 0);
  374. }
  375. #endif