store_meth.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright 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/store.h>
  10. #include <openssl/crypto.h>
  11. #include "internal/core.h"
  12. #include "internal/namemap.h"
  13. #include "internal/property.h"
  14. #include "internal/provider.h"
  15. #include "store_local.h"
  16. int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER *loader)
  17. {
  18. int ref = 0;
  19. if (loader->prov != NULL)
  20. CRYPTO_UP_REF(&loader->refcnt, &ref, loader->lock);
  21. return 1;
  22. }
  23. void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
  24. {
  25. if (loader != NULL && loader->prov != NULL) {
  26. int i;
  27. CRYPTO_DOWN_REF(&loader->refcnt, &i, loader->lock);
  28. if (i > 0)
  29. return;
  30. ossl_provider_free(loader->prov);
  31. CRYPTO_THREAD_lock_free(loader->lock);
  32. }
  33. OPENSSL_free(loader);
  34. }
  35. /*
  36. * OSSL_STORE_LOADER_new() expects the scheme as a constant string,
  37. * which we currently don't have, so we need an alternative allocator.
  38. */
  39. static OSSL_STORE_LOADER *new_loader(OSSL_PROVIDER *prov)
  40. {
  41. OSSL_STORE_LOADER *loader;
  42. if ((loader = OPENSSL_zalloc(sizeof(*loader))) == NULL
  43. || (loader->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  44. OPENSSL_free(loader);
  45. return NULL;
  46. }
  47. loader->prov = prov;
  48. ossl_provider_up_ref(prov);
  49. loader->refcnt = 1;
  50. return loader;
  51. }
  52. static int up_ref_loader(void *method)
  53. {
  54. return OSSL_STORE_LOADER_up_ref(method);
  55. }
  56. static void free_loader(void *method)
  57. {
  58. OSSL_STORE_LOADER_free(method);
  59. }
  60. /* Permanent loader method store, constructor and destructor */
  61. static void loader_store_free(void *vstore)
  62. {
  63. ossl_method_store_free(vstore);
  64. }
  65. static void *loader_store_new(OSSL_LIB_CTX *ctx)
  66. {
  67. return ossl_method_store_new(ctx);
  68. }
  69. static const OSSL_LIB_CTX_METHOD loader_store_method = {
  70. loader_store_new,
  71. loader_store_free,
  72. };
  73. /* Data to be passed through ossl_method_construct() */
  74. struct loader_data_st {
  75. OSSL_LIB_CTX *libctx;
  76. OSSL_METHOD_CONSTRUCT_METHOD *mcm;
  77. int scheme_id; /* For get_loader_from_store() */
  78. const char *scheme; /* For get_loader_from_store() */
  79. const char *propquery; /* For get_loader_from_store() */
  80. };
  81. /*
  82. * Generic routines to fetch / create OSSL_STORE methods with
  83. * ossl_method_construct()
  84. */
  85. /* Temporary loader method store, constructor and destructor */
  86. static void *alloc_tmp_loader_store(OSSL_LIB_CTX *ctx)
  87. {
  88. return ossl_method_store_new(ctx);
  89. }
  90. static void dealloc_tmp_loader_store(void *store)
  91. {
  92. if (store != NULL)
  93. ossl_method_store_free(store);
  94. }
  95. /* Get the permanent loader store */
  96. static OSSL_METHOD_STORE *get_loader_store(OSSL_LIB_CTX *libctx)
  97. {
  98. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX,
  99. &loader_store_method);
  100. }
  101. /* Get loader methods from a store, or put one in */
  102. static void *get_loader_from_store(OSSL_LIB_CTX *libctx, void *store,
  103. void *data)
  104. {
  105. struct loader_data_st *methdata = data;
  106. void *method = NULL;
  107. int id;
  108. if ((id = methdata->scheme_id) == 0) {
  109. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  110. id = ossl_namemap_name2num(namemap, methdata->scheme);
  111. }
  112. if (store == NULL
  113. && (store = get_loader_store(libctx)) == NULL)
  114. return NULL;
  115. if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
  116. return NULL;
  117. return method;
  118. }
  119. static int put_loader_in_store(OSSL_LIB_CTX *libctx, void *store,
  120. void *method, const OSSL_PROVIDER *prov,
  121. int operation_id, const char *scheme,
  122. const char *propdef, void *unused)
  123. {
  124. OSSL_NAMEMAP *namemap;
  125. int id;
  126. if ((namemap = ossl_namemap_stored(libctx)) == NULL
  127. || (id = ossl_namemap_name2num(namemap, scheme)) == 0)
  128. return 0;
  129. if (store == NULL && (store = get_loader_store(libctx)) == NULL)
  130. return 0;
  131. return ossl_method_store_add(store, prov, id, propdef, method,
  132. up_ref_loader, free_loader);
  133. }
  134. static void *loader_from_dispatch(int scheme_id, const OSSL_ALGORITHM *algodef,
  135. OSSL_PROVIDER *prov)
  136. {
  137. OSSL_STORE_LOADER *loader = NULL;
  138. const OSSL_DISPATCH *fns = algodef->implementation;
  139. if ((loader = new_loader(prov)) == NULL)
  140. return NULL;
  141. loader->scheme_id = scheme_id;
  142. loader->propdef = algodef->property_definition;
  143. for (; fns->function_id != 0; fns++) {
  144. switch (fns->function_id) {
  145. case OSSL_FUNC_STORE_OPEN:
  146. if (loader->p_open == NULL)
  147. loader->p_open = OSSL_FUNC_store_open(fns);
  148. break;
  149. case OSSL_FUNC_STORE_ATTACH:
  150. if (loader->p_attach == NULL)
  151. loader->p_attach = OSSL_FUNC_store_attach(fns);
  152. break;
  153. case OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS:
  154. if (loader->p_settable_ctx_params == NULL)
  155. loader->p_settable_ctx_params =
  156. OSSL_FUNC_store_settable_ctx_params(fns);
  157. break;
  158. case OSSL_FUNC_STORE_SET_CTX_PARAMS:
  159. if (loader->p_set_ctx_params == NULL)
  160. loader->p_set_ctx_params = OSSL_FUNC_store_set_ctx_params(fns);
  161. break;
  162. case OSSL_FUNC_STORE_LOAD:
  163. if (loader->p_load == NULL)
  164. loader->p_load = OSSL_FUNC_store_load(fns);
  165. break;
  166. case OSSL_FUNC_STORE_EOF:
  167. if (loader->p_eof == NULL)
  168. loader->p_eof = OSSL_FUNC_store_eof(fns);
  169. break;
  170. case OSSL_FUNC_STORE_CLOSE:
  171. if (loader->p_close == NULL)
  172. loader->p_close = OSSL_FUNC_store_close(fns);
  173. break;
  174. case OSSL_FUNC_STORE_EXPORT_OBJECT:
  175. if (loader->p_export_object == NULL)
  176. loader->p_export_object = OSSL_FUNC_store_export_object(fns);
  177. break;
  178. }
  179. }
  180. if ((loader->p_open == NULL && loader->p_attach == NULL)
  181. || loader->p_load == NULL
  182. || loader->p_eof == NULL
  183. || loader->p_close == NULL) {
  184. /* Only set_ctx_params is optionaal */
  185. OSSL_STORE_LOADER_free(loader);
  186. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
  187. return NULL;
  188. }
  189. return loader;
  190. }
  191. /*
  192. * The core fetching functionality passes the scheme of the implementation.
  193. * This function is responsible to getting an identity number for them,
  194. * then call loader_from_dispatch() with that identity number.
  195. */
  196. static void *construct_loader(const OSSL_ALGORITHM *algodef,
  197. OSSL_PROVIDER *prov, void *unused)
  198. {
  199. /*
  200. * This function is only called if get_loader_from_store() returned
  201. * NULL, so it's safe to say that of all the spots to create a new
  202. * namemap entry, this is it. Should the scheme already exist there, we
  203. * know that ossl_namemap_add() will return its corresponding number.
  204. */
  205. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  206. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  207. const char *scheme = algodef->algorithm_names;
  208. int id = ossl_namemap_add_name(namemap, 0, scheme);
  209. void *method = NULL;
  210. if (id != 0)
  211. method = loader_from_dispatch(id, algodef, prov);
  212. return method;
  213. }
  214. /* Intermediary function to avoid ugly casts, used below */
  215. static void destruct_loader(void *method, void *data)
  216. {
  217. OSSL_STORE_LOADER_free(method);
  218. }
  219. /* Fetching support. Can fetch by numeric identity or by scheme */
  220. static OSSL_STORE_LOADER *inner_loader_fetch(OSSL_LIB_CTX *libctx,
  221. int id, const char *scheme,
  222. const char *properties)
  223. {
  224. OSSL_METHOD_STORE *store = get_loader_store(libctx);
  225. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  226. void *method = NULL;
  227. if (store == NULL || namemap == NULL)
  228. return NULL;
  229. /*
  230. * If we have been passed neither a scheme_id or a scheme, we have an
  231. * internal programming error.
  232. */
  233. if (!ossl_assert(id != 0 || scheme != NULL))
  234. return NULL;
  235. if (id == 0)
  236. id = ossl_namemap_name2num(namemap, scheme);
  237. if (id == 0
  238. || !ossl_method_store_cache_get(store, id, properties, &method)) {
  239. OSSL_METHOD_CONSTRUCT_METHOD mcm = {
  240. alloc_tmp_loader_store,
  241. dealloc_tmp_loader_store,
  242. get_loader_from_store,
  243. put_loader_in_store,
  244. construct_loader,
  245. destruct_loader
  246. };
  247. struct loader_data_st mcmdata;
  248. mcmdata.libctx = libctx;
  249. mcmdata.mcm = &mcm;
  250. mcmdata.scheme_id = id;
  251. mcmdata.scheme = scheme;
  252. mcmdata.propquery = properties;
  253. if ((method = ossl_method_construct(libctx, OSSL_OP_STORE,
  254. 0 /* !force_cache */,
  255. &mcm, &mcmdata)) != NULL) {
  256. /*
  257. * If construction did create a method for us, we know that there
  258. * is a correct scheme_id, since those have already been calculated
  259. * in get_loader_from_store() and put_loader_in_store() above.
  260. */
  261. if (id == 0)
  262. id = ossl_namemap_name2num(namemap, scheme);
  263. ossl_method_store_cache_set(store, id, properties, method,
  264. up_ref_loader, free_loader);
  265. }
  266. }
  267. return method;
  268. }
  269. OSSL_STORE_LOADER *OSSL_STORE_LOADER_fetch(const char *scheme,
  270. OSSL_LIB_CTX *libctx,
  271. const char *properties)
  272. {
  273. return inner_loader_fetch(libctx, 0, scheme, properties);
  274. }
  275. OSSL_STORE_LOADER *ossl_store_loader_fetch_by_number(OSSL_LIB_CTX *libctx,
  276. int scheme_id,
  277. const char *properties)
  278. {
  279. return inner_loader_fetch(libctx, scheme_id, NULL, properties);
  280. }
  281. /*
  282. * Library of basic method functions
  283. */
  284. const OSSL_PROVIDER *OSSL_STORE_LOADER_provider(const OSSL_STORE_LOADER *loader)
  285. {
  286. if (!ossl_assert(loader != NULL)) {
  287. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  288. return 0;
  289. }
  290. return loader->prov;
  291. }
  292. const char *OSSL_STORE_LOADER_properties(const OSSL_STORE_LOADER *loader)
  293. {
  294. if (!ossl_assert(loader != NULL)) {
  295. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  296. return 0;
  297. }
  298. return loader->propdef;
  299. }
  300. int OSSL_STORE_LOADER_number(const OSSL_STORE_LOADER *loader)
  301. {
  302. if (!ossl_assert(loader != NULL)) {
  303. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  304. return 0;
  305. }
  306. return loader->scheme_id;
  307. }
  308. int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER *loader, const char *name)
  309. {
  310. if (loader->prov != NULL) {
  311. OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
  312. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  313. return ossl_namemap_name2num(namemap, name) == loader->scheme_id;
  314. }
  315. return 0;
  316. }
  317. struct loader_do_all_data_st {
  318. void (*user_fn)(void *method, void *arg);
  319. void *user_arg;
  320. };
  321. static void loader_do_one(OSSL_PROVIDER *provider,
  322. const OSSL_ALGORITHM *algodef,
  323. int no_store, void *vdata)
  324. {
  325. struct loader_do_all_data_st *data = vdata;
  326. OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
  327. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  328. const char *name = algodef->algorithm_names;
  329. int id = ossl_namemap_add_name(namemap, 0, name);
  330. void *method = NULL;
  331. if (id != 0)
  332. method =
  333. loader_from_dispatch(id, algodef, provider);
  334. if (method != NULL) {
  335. data->user_fn(method, data->user_arg);
  336. OSSL_STORE_LOADER_free(method);
  337. }
  338. }
  339. void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX *libctx,
  340. void (*fn)(OSSL_STORE_LOADER *loader,
  341. void *arg),
  342. void *arg)
  343. {
  344. struct loader_do_all_data_st data;
  345. data.user_fn = (void (*)(void *, void *))fn;
  346. data.user_arg = arg;
  347. ossl_algorithm_do_all(libctx, OSSL_OP_STORE, NULL,
  348. NULL, loader_do_one, NULL,
  349. &data);
  350. }
  351. void OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER *loader,
  352. void (*fn)(const char *name, void *data),
  353. void *data)
  354. {
  355. if (loader == NULL)
  356. return;
  357. if (loader->prov != NULL) {
  358. OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
  359. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  360. ossl_namemap_doall_names(namemap, loader->scheme_id, fn, data);
  361. }
  362. }