2
0

provider_child.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * Copyright 2019-2023 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 <assert.h>
  10. #include <openssl/crypto.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/core_names.h>
  13. #include <openssl/provider.h>
  14. #include <openssl/evp.h>
  15. #include "internal/provider.h"
  16. #include "internal/cryptlib.h"
  17. #include "crypto/evp.h"
  18. #include "crypto/context.h"
  19. DEFINE_STACK_OF(OSSL_PROVIDER)
  20. struct child_prov_globals {
  21. const OSSL_CORE_HANDLE *handle;
  22. const OSSL_CORE_HANDLE *curr_prov;
  23. CRYPTO_RWLOCK *lock;
  24. OSSL_FUNC_core_get_libctx_fn *c_get_libctx;
  25. OSSL_FUNC_provider_register_child_cb_fn *c_provider_register_child_cb;
  26. OSSL_FUNC_provider_deregister_child_cb_fn *c_provider_deregister_child_cb;
  27. OSSL_FUNC_provider_name_fn *c_prov_name;
  28. OSSL_FUNC_provider_get0_provider_ctx_fn *c_prov_get0_provider_ctx;
  29. OSSL_FUNC_provider_get0_dispatch_fn *c_prov_get0_dispatch;
  30. OSSL_FUNC_provider_up_ref_fn *c_prov_up_ref;
  31. OSSL_FUNC_provider_free_fn *c_prov_free;
  32. };
  33. void *ossl_child_prov_ctx_new(OSSL_LIB_CTX *libctx)
  34. {
  35. return OPENSSL_zalloc(sizeof(struct child_prov_globals));
  36. }
  37. void ossl_child_prov_ctx_free(void *vgbl)
  38. {
  39. struct child_prov_globals *gbl = vgbl;
  40. CRYPTO_THREAD_lock_free(gbl->lock);
  41. OPENSSL_free(gbl);
  42. }
  43. static OSSL_provider_init_fn ossl_child_provider_init;
  44. static int ossl_child_provider_init(const OSSL_CORE_HANDLE *handle,
  45. const OSSL_DISPATCH *in,
  46. const OSSL_DISPATCH **out,
  47. void **provctx)
  48. {
  49. OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
  50. OSSL_LIB_CTX *ctx;
  51. struct child_prov_globals *gbl;
  52. for (; in->function_id != 0; in++) {
  53. switch (in->function_id) {
  54. case OSSL_FUNC_CORE_GET_LIBCTX:
  55. c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  56. break;
  57. default:
  58. /* Just ignore anything we don't understand */
  59. break;
  60. }
  61. }
  62. if (c_get_libctx == NULL)
  63. return 0;
  64. /*
  65. * We need an OSSL_LIB_CTX but c_get_libctx returns OPENSSL_CORE_CTX. We are
  66. * a built-in provider and so we can get away with this cast. Normal
  67. * providers can't do this.
  68. */
  69. ctx = (OSSL_LIB_CTX *)c_get_libctx(handle);
  70. gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  71. if (gbl == NULL)
  72. return 0;
  73. *provctx = gbl->c_prov_get0_provider_ctx(gbl->curr_prov);
  74. *out = gbl->c_prov_get0_dispatch(gbl->curr_prov);
  75. return 1;
  76. }
  77. static int provider_create_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata)
  78. {
  79. OSSL_LIB_CTX *ctx = cbdata;
  80. struct child_prov_globals *gbl;
  81. const char *provname;
  82. OSSL_PROVIDER *cprov;
  83. int ret = 0;
  84. gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  85. if (gbl == NULL)
  86. return 0;
  87. if (!CRYPTO_THREAD_write_lock(gbl->lock))
  88. return 0;
  89. provname = gbl->c_prov_name(prov);
  90. /*
  91. * We're operating under a lock so we can store the "current" provider in
  92. * the global data.
  93. */
  94. gbl->curr_prov = prov;
  95. if ((cprov = ossl_provider_find(ctx, provname, 1)) != NULL) {
  96. /*
  97. * We free the newly created ref. We rely on the provider sticking around
  98. * in the provider store.
  99. */
  100. ossl_provider_free(cprov);
  101. /*
  102. * The provider already exists. It could be a previously created child,
  103. * or it could have been explicitly loaded. If explicitly loaded we
  104. * ignore it - i.e. we don't start treating it like a child.
  105. */
  106. if (!ossl_provider_activate(cprov, 0, 1))
  107. goto err;
  108. } else {
  109. /*
  110. * Create it - passing 1 as final param so we don't try and recursively
  111. * init children
  112. */
  113. if ((cprov = ossl_provider_new(ctx, provname, ossl_child_provider_init,
  114. NULL, 1)) == NULL)
  115. goto err;
  116. if (!ossl_provider_activate(cprov, 0, 0)) {
  117. ossl_provider_free(cprov);
  118. goto err;
  119. }
  120. if (!ossl_provider_set_child(cprov, prov)
  121. || !ossl_provider_add_to_store(cprov, NULL, 0)) {
  122. ossl_provider_deactivate(cprov, 0);
  123. ossl_provider_free(cprov);
  124. goto err;
  125. }
  126. }
  127. ret = 1;
  128. err:
  129. CRYPTO_THREAD_unlock(gbl->lock);
  130. return ret;
  131. }
  132. static int provider_remove_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata)
  133. {
  134. OSSL_LIB_CTX *ctx = cbdata;
  135. struct child_prov_globals *gbl;
  136. const char *provname;
  137. OSSL_PROVIDER *cprov;
  138. gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  139. if (gbl == NULL)
  140. return 0;
  141. provname = gbl->c_prov_name(prov);
  142. cprov = ossl_provider_find(ctx, provname, 1);
  143. if (cprov == NULL)
  144. return 0;
  145. /*
  146. * ossl_provider_find ups the ref count, so we free it again here. We can
  147. * rely on the provider store reference count.
  148. */
  149. ossl_provider_free(cprov);
  150. if (ossl_provider_is_child(cprov)
  151. && !ossl_provider_deactivate(cprov, 1))
  152. return 0;
  153. return 1;
  154. }
  155. static int provider_global_props_cb(const char *props, void *cbdata)
  156. {
  157. OSSL_LIB_CTX *ctx = cbdata;
  158. return evp_set_default_properties_int(ctx, props, 0, 1);
  159. }
  160. int ossl_provider_init_as_child(OSSL_LIB_CTX *ctx,
  161. const OSSL_CORE_HANDLE *handle,
  162. const OSSL_DISPATCH *in)
  163. {
  164. struct child_prov_globals *gbl;
  165. if (ctx == NULL)
  166. return 0;
  167. gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  168. if (gbl == NULL)
  169. return 0;
  170. gbl->handle = handle;
  171. for (; in->function_id != 0; in++) {
  172. switch (in->function_id) {
  173. case OSSL_FUNC_CORE_GET_LIBCTX:
  174. gbl->c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  175. break;
  176. case OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB:
  177. gbl->c_provider_register_child_cb
  178. = OSSL_FUNC_provider_register_child_cb(in);
  179. break;
  180. case OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB:
  181. gbl->c_provider_deregister_child_cb
  182. = OSSL_FUNC_provider_deregister_child_cb(in);
  183. break;
  184. case OSSL_FUNC_PROVIDER_NAME:
  185. gbl->c_prov_name = OSSL_FUNC_provider_name(in);
  186. break;
  187. case OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX:
  188. gbl->c_prov_get0_provider_ctx
  189. = OSSL_FUNC_provider_get0_provider_ctx(in);
  190. break;
  191. case OSSL_FUNC_PROVIDER_GET0_DISPATCH:
  192. gbl->c_prov_get0_dispatch = OSSL_FUNC_provider_get0_dispatch(in);
  193. break;
  194. case OSSL_FUNC_PROVIDER_UP_REF:
  195. gbl->c_prov_up_ref
  196. = OSSL_FUNC_provider_up_ref(in);
  197. break;
  198. case OSSL_FUNC_PROVIDER_FREE:
  199. gbl->c_prov_free = OSSL_FUNC_provider_free(in);
  200. break;
  201. default:
  202. /* Just ignore anything we don't understand */
  203. break;
  204. }
  205. }
  206. if (gbl->c_get_libctx == NULL
  207. || gbl->c_provider_register_child_cb == NULL
  208. || gbl->c_prov_name == NULL
  209. || gbl->c_prov_get0_provider_ctx == NULL
  210. || gbl->c_prov_get0_dispatch == NULL
  211. || gbl->c_prov_up_ref == NULL
  212. || gbl->c_prov_free == NULL)
  213. return 0;
  214. gbl->lock = CRYPTO_THREAD_lock_new();
  215. if (gbl->lock == NULL)
  216. return 0;
  217. if (!gbl->c_provider_register_child_cb(gbl->handle,
  218. provider_create_child_cb,
  219. provider_remove_child_cb,
  220. provider_global_props_cb,
  221. ctx))
  222. return 0;
  223. return 1;
  224. }
  225. void ossl_provider_deinit_child(OSSL_LIB_CTX *ctx)
  226. {
  227. struct child_prov_globals *gbl
  228. = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  229. if (gbl == NULL)
  230. return;
  231. gbl->c_provider_deregister_child_cb(gbl->handle);
  232. }
  233. /*
  234. * ossl_provider_up_ref_parent() and ossl_provider_free_parent() do
  235. * nothing in "self-referencing" child providers, i.e. when the parent
  236. * of the child provider is the same as the provider where this child
  237. * provider was created.
  238. * This allows the teardown function in the parent provider to be called
  239. * at the correct moment.
  240. * For child providers in other providers, the reference count is done to
  241. * ensure that cross referencing is recorded. These should be cleared up
  242. * through that providers teardown, as part of freeing its child libctx.
  243. */
  244. int ossl_provider_up_ref_parent(OSSL_PROVIDER *prov, int activate)
  245. {
  246. struct child_prov_globals *gbl;
  247. const OSSL_CORE_HANDLE *parent_handle;
  248. gbl = ossl_lib_ctx_get_data(ossl_provider_libctx(prov),
  249. OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  250. if (gbl == NULL)
  251. return 0;
  252. parent_handle = ossl_provider_get_parent(prov);
  253. if (parent_handle == gbl->handle)
  254. return 1;
  255. return gbl->c_prov_up_ref(parent_handle, activate);
  256. }
  257. int ossl_provider_free_parent(OSSL_PROVIDER *prov, int deactivate)
  258. {
  259. struct child_prov_globals *gbl;
  260. const OSSL_CORE_HANDLE *parent_handle;
  261. gbl = ossl_lib_ctx_get_data(ossl_provider_libctx(prov),
  262. OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  263. if (gbl == NULL)
  264. return 0;
  265. parent_handle = ossl_provider_get_parent(prov);
  266. if (parent_handle == gbl->handle)
  267. return 1;
  268. return gbl->c_prov_free(ossl_provider_get_parent(prov), deactivate);
  269. }