provider_child.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 2019-2022 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. 1)) == NULL)
  115. goto err;
  116. if (!ossl_provider_activate(cprov, 0, 0))
  117. goto err;
  118. if (!ossl_provider_set_child(cprov, prov)
  119. || !ossl_provider_add_to_store(cprov, NULL, 0)) {
  120. ossl_provider_deactivate(cprov, 0);
  121. ossl_provider_free(cprov);
  122. goto err;
  123. }
  124. }
  125. ret = 1;
  126. err:
  127. CRYPTO_THREAD_unlock(gbl->lock);
  128. return ret;
  129. }
  130. static int provider_remove_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata)
  131. {
  132. OSSL_LIB_CTX *ctx = cbdata;
  133. struct child_prov_globals *gbl;
  134. const char *provname;
  135. OSSL_PROVIDER *cprov;
  136. gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  137. if (gbl == NULL)
  138. return 0;
  139. provname = gbl->c_prov_name(prov);
  140. cprov = ossl_provider_find(ctx, provname, 1);
  141. if (cprov == NULL)
  142. return 0;
  143. /*
  144. * ossl_provider_find ups the ref count, so we free it again here. We can
  145. * rely on the provider store reference count.
  146. */
  147. ossl_provider_free(cprov);
  148. if (ossl_provider_is_child(cprov)
  149. && !ossl_provider_deactivate(cprov, 1))
  150. return 0;
  151. return 1;
  152. }
  153. static int provider_global_props_cb(const char *props, void *cbdata)
  154. {
  155. OSSL_LIB_CTX *ctx = cbdata;
  156. return evp_set_default_properties_int(ctx, props, 0, 1);
  157. }
  158. int ossl_provider_init_as_child(OSSL_LIB_CTX *ctx,
  159. const OSSL_CORE_HANDLE *handle,
  160. const OSSL_DISPATCH *in)
  161. {
  162. struct child_prov_globals *gbl;
  163. if (ctx == NULL)
  164. return 0;
  165. gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  166. if (gbl == NULL)
  167. return 0;
  168. gbl->handle = handle;
  169. for (; in->function_id != 0; in++) {
  170. switch (in->function_id) {
  171. case OSSL_FUNC_CORE_GET_LIBCTX:
  172. gbl->c_get_libctx = OSSL_FUNC_core_get_libctx(in);
  173. break;
  174. case OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB:
  175. gbl->c_provider_register_child_cb
  176. = OSSL_FUNC_provider_register_child_cb(in);
  177. break;
  178. case OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB:
  179. gbl->c_provider_deregister_child_cb
  180. = OSSL_FUNC_provider_deregister_child_cb(in);
  181. break;
  182. case OSSL_FUNC_PROVIDER_NAME:
  183. gbl->c_prov_name = OSSL_FUNC_provider_name(in);
  184. break;
  185. case OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX:
  186. gbl->c_prov_get0_provider_ctx
  187. = OSSL_FUNC_provider_get0_provider_ctx(in);
  188. break;
  189. case OSSL_FUNC_PROVIDER_GET0_DISPATCH:
  190. gbl->c_prov_get0_dispatch = OSSL_FUNC_provider_get0_dispatch(in);
  191. break;
  192. case OSSL_FUNC_PROVIDER_UP_REF:
  193. gbl->c_prov_up_ref
  194. = OSSL_FUNC_provider_up_ref(in);
  195. break;
  196. case OSSL_FUNC_PROVIDER_FREE:
  197. gbl->c_prov_free = OSSL_FUNC_provider_free(in);
  198. break;
  199. default:
  200. /* Just ignore anything we don't understand */
  201. break;
  202. }
  203. }
  204. if (gbl->c_get_libctx == NULL
  205. || gbl->c_provider_register_child_cb == NULL
  206. || gbl->c_prov_name == NULL
  207. || gbl->c_prov_get0_provider_ctx == NULL
  208. || gbl->c_prov_get0_dispatch == NULL
  209. || gbl->c_prov_up_ref == NULL
  210. || gbl->c_prov_free == NULL)
  211. return 0;
  212. gbl->lock = CRYPTO_THREAD_lock_new();
  213. if (gbl->lock == NULL)
  214. return 0;
  215. if (!gbl->c_provider_register_child_cb(gbl->handle,
  216. provider_create_child_cb,
  217. provider_remove_child_cb,
  218. provider_global_props_cb,
  219. ctx))
  220. return 0;
  221. return 1;
  222. }
  223. void ossl_provider_deinit_child(OSSL_LIB_CTX *ctx)
  224. {
  225. struct child_prov_globals *gbl
  226. = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  227. if (gbl == NULL)
  228. return;
  229. gbl->c_provider_deregister_child_cb(gbl->handle);
  230. }
  231. /*
  232. * ossl_provider_up_ref_parent() and ossl_provider_free_parent() do
  233. * nothing in "self-referencing" child providers, i.e. when the parent
  234. * of the child provider is the same as the provider where this child
  235. * provider was created.
  236. * This allows the teardown function in the parent provider to be called
  237. * at the correct moment.
  238. * For child providers in other providers, the reference count is done to
  239. * ensure that cross referencing is recorded. These should be cleared up
  240. * through that providers teardown, as part of freeing its child libctx.
  241. */
  242. int ossl_provider_up_ref_parent(OSSL_PROVIDER *prov, int activate)
  243. {
  244. struct child_prov_globals *gbl;
  245. const OSSL_CORE_HANDLE *parent_handle;
  246. gbl = ossl_lib_ctx_get_data(ossl_provider_libctx(prov),
  247. OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  248. if (gbl == NULL)
  249. return 0;
  250. parent_handle = ossl_provider_get_parent(prov);
  251. if (parent_handle == gbl->handle)
  252. return 1;
  253. return gbl->c_prov_up_ref(parent_handle, activate);
  254. }
  255. int ossl_provider_free_parent(OSSL_PROVIDER *prov, int deactivate)
  256. {
  257. struct child_prov_globals *gbl;
  258. const OSSL_CORE_HANDLE *parent_handle;
  259. gbl = ossl_lib_ctx_get_data(ossl_provider_libctx(prov),
  260. OSSL_LIB_CTX_CHILD_PROVIDER_INDEX);
  261. if (gbl == NULL)
  262. return 0;
  263. parent_handle = ossl_provider_get_parent(prov);
  264. if (parent_handle == gbl->handle)
  265. return 1;
  266. return gbl->c_prov_free(ossl_provider_get_parent(prov), deactivate);
  267. }