provider_child.c 9.5 KB

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