context.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright 2019 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 "internal/cryptlib.h"
  10. #include "internal/thread_once.h"
  11. struct openssl_ctx_onfree_list_st {
  12. openssl_ctx_onfree_fn *fn;
  13. struct openssl_ctx_onfree_list_st *next;
  14. };
  15. struct openssl_ctx_st {
  16. CRYPTO_RWLOCK *lock;
  17. CRYPTO_EX_DATA data;
  18. /*
  19. * For most data in the OPENSSL_CTX we just use ex_data to store it. But
  20. * that doesn't work for ex_data itself - so we store that directly.
  21. */
  22. OSSL_EX_DATA_GLOBAL global;
  23. /* Map internal static indexes to dynamically created indexes */
  24. int dyn_indexes[OPENSSL_CTX_MAX_INDEXES];
  25. CRYPTO_RWLOCK *oncelock;
  26. int run_once_done[OPENSSL_CTX_MAX_RUN_ONCE];
  27. int run_once_ret[OPENSSL_CTX_MAX_RUN_ONCE];
  28. struct openssl_ctx_onfree_list_st *onfreelist;
  29. };
  30. #ifndef FIPS_MODE
  31. static OPENSSL_CTX default_context_int;
  32. /* Always points at default_context_int if it has been initialised */
  33. static OPENSSL_CTX *default_context = NULL;
  34. #endif
  35. static int context_init(OPENSSL_CTX *ctx)
  36. {
  37. size_t i;
  38. ctx->lock = CRYPTO_THREAD_lock_new();
  39. if (ctx->lock == NULL)
  40. return 0;
  41. ctx->oncelock = CRYPTO_THREAD_lock_new();
  42. if (ctx->oncelock == NULL)
  43. goto err;
  44. for (i = 0; i < OPENSSL_CTX_MAX_INDEXES; i++)
  45. ctx->dyn_indexes[i] = -1;
  46. if (!do_ex_data_init(ctx))
  47. goto err;
  48. if (!crypto_new_ex_data_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
  49. &ctx->data)) {
  50. crypto_cleanup_all_ex_data_int(ctx);
  51. goto err;
  52. }
  53. return 1;
  54. err:
  55. CRYPTO_THREAD_lock_free(ctx->oncelock);
  56. CRYPTO_THREAD_lock_free(ctx->lock);
  57. ctx->lock = NULL;
  58. return 0;
  59. }
  60. static int context_deinit(OPENSSL_CTX *ctx)
  61. {
  62. struct openssl_ctx_onfree_list_st *tmp, *onfree;
  63. if (ctx == NULL)
  64. return 1;
  65. onfree = ctx->onfreelist;
  66. while (onfree != NULL) {
  67. onfree->fn(ctx);
  68. tmp = onfree;
  69. onfree = onfree->next;
  70. OPENSSL_free(tmp);
  71. }
  72. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL, &ctx->data);
  73. crypto_cleanup_all_ex_data_int(ctx);
  74. CRYPTO_THREAD_lock_free(ctx->oncelock);
  75. CRYPTO_THREAD_lock_free(ctx->lock);
  76. ctx->lock = NULL;
  77. return 1;
  78. }
  79. #ifndef FIPS_MODE
  80. void openssl_ctx_default_deinit(void)
  81. {
  82. context_deinit(default_context);
  83. }
  84. static CRYPTO_ONCE default_context_init = CRYPTO_ONCE_STATIC_INIT;
  85. DEFINE_RUN_ONCE_STATIC(do_default_context_init)
  86. {
  87. if (context_init(&default_context_int))
  88. default_context = &default_context_int;
  89. return 1;
  90. }
  91. #endif
  92. OPENSSL_CTX *OPENSSL_CTX_new(void)
  93. {
  94. OPENSSL_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  95. if (ctx != NULL && !context_init(ctx)) {
  96. OPENSSL_CTX_free(ctx);
  97. ctx = NULL;
  98. }
  99. return ctx;
  100. }
  101. void OPENSSL_CTX_free(OPENSSL_CTX *ctx)
  102. {
  103. if (ctx != NULL)
  104. context_deinit(ctx);
  105. OPENSSL_free(ctx);
  106. }
  107. static void openssl_ctx_generic_new(void *parent_ign, void *ptr_ign,
  108. CRYPTO_EX_DATA *ad, int index,
  109. long argl_ign, void *argp)
  110. {
  111. const OPENSSL_CTX_METHOD *meth = argp;
  112. void *ptr = meth->new_func(crypto_ex_data_get_openssl_ctx(ad));
  113. if (ptr != NULL)
  114. CRYPTO_set_ex_data(ad, index, ptr);
  115. }
  116. static void openssl_ctx_generic_free(void *parent_ign, void *ptr,
  117. CRYPTO_EX_DATA *ad, int index,
  118. long argl_ign, void *argp)
  119. {
  120. const OPENSSL_CTX_METHOD *meth = argp;
  121. meth->free_func(ptr);
  122. }
  123. /* Non-static so we can use it in context_internal_test */
  124. static int openssl_ctx_init_index(OPENSSL_CTX *ctx, int static_index,
  125. const OPENSSL_CTX_METHOD *meth)
  126. {
  127. int idx;
  128. #ifndef FIPS_MODE
  129. if (ctx == NULL) {
  130. if (!RUN_ONCE(&default_context_init, do_default_context_init))
  131. return 0;
  132. ctx = default_context;
  133. }
  134. #endif
  135. if (ctx == NULL)
  136. return 0;
  137. idx = crypto_get_ex_new_index_ex(ctx, CRYPTO_EX_INDEX_OPENSSL_CTX, 0,
  138. (void *)meth,
  139. openssl_ctx_generic_new,
  140. NULL, openssl_ctx_generic_free);
  141. if (idx < 0)
  142. return 0;
  143. ctx->dyn_indexes[static_index] = idx;
  144. return 1;
  145. }
  146. void *openssl_ctx_get_data(OPENSSL_CTX *ctx, int index,
  147. const OPENSSL_CTX_METHOD *meth)
  148. {
  149. void *data = NULL;
  150. #ifndef FIPS_MODE
  151. if (ctx == NULL) {
  152. if (!RUN_ONCE(&default_context_init, do_default_context_init))
  153. return NULL;
  154. ctx = default_context;
  155. }
  156. #endif
  157. if (ctx == NULL)
  158. return NULL;
  159. CRYPTO_THREAD_read_lock(ctx->lock);
  160. if (ctx->dyn_indexes[index] == -1
  161. && !openssl_ctx_init_index(ctx, index, meth)) {
  162. CRYPTO_THREAD_unlock(ctx->lock);
  163. return NULL;
  164. }
  165. /* The alloc call ensures there's a value there */
  166. if (CRYPTO_alloc_ex_data(CRYPTO_EX_INDEX_OPENSSL_CTX, NULL,
  167. &ctx->data, ctx->dyn_indexes[index]))
  168. data = CRYPTO_get_ex_data(&ctx->data, ctx->dyn_indexes[index]);
  169. CRYPTO_THREAD_unlock(ctx->lock);
  170. return data;
  171. }
  172. OSSL_EX_DATA_GLOBAL *openssl_ctx_get_ex_data_global(OPENSSL_CTX *ctx)
  173. {
  174. /*
  175. * The default context code is not needed in FIPS_MODE and ctx should never
  176. * be NULL in the FIPS provider. However we compile this code out to ensure
  177. * we fail immediately if ctx == NULL in FIPS_MODE
  178. */
  179. #ifndef FIPS_MODE
  180. if (ctx == NULL) {
  181. if (!RUN_ONCE(&default_context_init, do_default_context_init))
  182. return NULL;
  183. ctx = default_context;
  184. }
  185. #endif
  186. if (ctx == NULL)
  187. return NULL;
  188. return &ctx->global;
  189. }
  190. int openssl_ctx_run_once(OPENSSL_CTX *ctx, unsigned int idx,
  191. openssl_ctx_run_once_fn run_once_fn)
  192. {
  193. int done = 0, ret = 0;
  194. #ifndef FIPS_MODE
  195. if (ctx == NULL) {
  196. if (!RUN_ONCE(&default_context_init, do_default_context_init))
  197. return 0;
  198. ctx = default_context;
  199. }
  200. #endif
  201. if (ctx == NULL)
  202. return 0;
  203. CRYPTO_THREAD_read_lock(ctx->oncelock);
  204. done = ctx->run_once_done[idx];
  205. if (done)
  206. ret = ctx->run_once_ret[idx];
  207. CRYPTO_THREAD_unlock(ctx->oncelock);
  208. if (done)
  209. return ret;
  210. CRYPTO_THREAD_write_lock(ctx->oncelock);
  211. if (ctx->run_once_done[idx]) {
  212. ret = ctx->run_once_ret[idx];
  213. CRYPTO_THREAD_unlock(ctx->oncelock);
  214. return ret;
  215. }
  216. ret = run_once_fn(ctx);
  217. ctx->run_once_done[idx] = 1;
  218. ctx->run_once_ret[idx] = ret;
  219. CRYPTO_THREAD_unlock(ctx->oncelock);
  220. return ret;
  221. }
  222. int openssl_ctx_onfree(OPENSSL_CTX *ctx, openssl_ctx_onfree_fn onfreefn)
  223. {
  224. struct openssl_ctx_onfree_list_st *newonfree
  225. = OPENSSL_malloc(sizeof(*newonfree));
  226. if (newonfree == NULL)
  227. return 0;
  228. newonfree->fn = onfreefn;
  229. newonfree->next = ctx->onfreelist;
  230. ctx->onfreelist = newonfree;
  231. return 1;
  232. }