store_meth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. * Copyright 2020-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 <openssl/crypto.h>
  10. #include "crypto/store.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. #include "crypto/context.h"
  17. int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER *loader)
  18. {
  19. int ref = 0;
  20. if (loader->prov != NULL)
  21. CRYPTO_UP_REF(&loader->refcnt, &ref, loader->lock);
  22. return 1;
  23. }
  24. void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
  25. {
  26. if (loader != NULL && loader->prov != NULL) {
  27. int i;
  28. CRYPTO_DOWN_REF(&loader->refcnt, &i, loader->lock);
  29. if (i > 0)
  30. return;
  31. ossl_provider_free(loader->prov);
  32. CRYPTO_THREAD_lock_free(loader->lock);
  33. }
  34. OPENSSL_free(loader);
  35. }
  36. /*
  37. * OSSL_STORE_LOADER_new() expects the scheme as a constant string,
  38. * which we currently don't have, so we need an alternative allocator.
  39. */
  40. static OSSL_STORE_LOADER *new_loader(OSSL_PROVIDER *prov)
  41. {
  42. OSSL_STORE_LOADER *loader;
  43. if ((loader = OPENSSL_zalloc(sizeof(*loader))) == NULL
  44. || (loader->lock = CRYPTO_THREAD_lock_new()) == NULL) {
  45. OPENSSL_free(loader);
  46. return NULL;
  47. }
  48. loader->prov = prov;
  49. ossl_provider_up_ref(prov);
  50. loader->refcnt = 1;
  51. return loader;
  52. }
  53. static int up_ref_loader(void *method)
  54. {
  55. return OSSL_STORE_LOADER_up_ref(method);
  56. }
  57. static void free_loader(void *method)
  58. {
  59. OSSL_STORE_LOADER_free(method);
  60. }
  61. /* Data to be passed through ossl_method_construct() */
  62. struct loader_data_st {
  63. OSSL_LIB_CTX *libctx;
  64. int scheme_id; /* For get_loader_from_store() */
  65. const char *scheme; /* For get_loader_from_store() */
  66. const char *propquery; /* For get_loader_from_store() */
  67. OSSL_METHOD_STORE *tmp_store; /* For get_tmp_loader_store() */
  68. unsigned int flag_construct_error_occurred : 1;
  69. };
  70. /*
  71. * Generic routines to fetch / create OSSL_STORE methods with
  72. * ossl_method_construct()
  73. */
  74. /* Temporary loader method store, constructor and destructor */
  75. static void *get_tmp_loader_store(void *data)
  76. {
  77. struct loader_data_st *methdata = data;
  78. if (methdata->tmp_store == NULL)
  79. methdata->tmp_store = ossl_method_store_new(methdata->libctx);
  80. return methdata->tmp_store;
  81. }
  82. static void dealloc_tmp_loader_store(void *store)
  83. {
  84. if (store != NULL)
  85. ossl_method_store_free(store);
  86. }
  87. /* Get the permanent loader store */
  88. static OSSL_METHOD_STORE *get_loader_store(OSSL_LIB_CTX *libctx)
  89. {
  90. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX);
  91. }
  92. static int reserve_loader_store(void *store, void *data)
  93. {
  94. struct loader_data_st *methdata = data;
  95. if (store == NULL
  96. && (store = get_loader_store(methdata->libctx)) == NULL)
  97. return 0;
  98. return ossl_method_lock_store(store);
  99. }
  100. static int unreserve_loader_store(void *store, void *data)
  101. {
  102. struct loader_data_st *methdata = data;
  103. if (store == NULL
  104. && (store = get_loader_store(methdata->libctx)) == NULL)
  105. return 0;
  106. return ossl_method_unlock_store(store);
  107. }
  108. /* Get loader methods from a store, or put one in */
  109. static void *get_loader_from_store(void *store, const OSSL_PROVIDER **prov,
  110. void *data)
  111. {
  112. struct loader_data_st *methdata = data;
  113. void *method = NULL;
  114. int id;
  115. if ((id = methdata->scheme_id) == 0) {
  116. OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
  117. id = ossl_namemap_name2num(namemap, methdata->scheme);
  118. }
  119. if (store == NULL
  120. && (store = get_loader_store(methdata->libctx)) == NULL)
  121. return NULL;
  122. if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method))
  123. return NULL;
  124. return method;
  125. }
  126. static int put_loader_in_store(void *store, void *method,
  127. const OSSL_PROVIDER *prov,
  128. const char *scheme, const char *propdef,
  129. void *data)
  130. {
  131. struct loader_data_st *methdata = data;
  132. OSSL_NAMEMAP *namemap;
  133. int id;
  134. if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
  135. || (id = ossl_namemap_name2num(namemap, scheme)) == 0)
  136. return 0;
  137. if (store == NULL && (store = get_loader_store(methdata->libctx)) == NULL)
  138. return 0;
  139. return ossl_method_store_add(store, prov, id, propdef, method,
  140. up_ref_loader, free_loader);
  141. }
  142. static void *loader_from_algorithm(int scheme_id, const OSSL_ALGORITHM *algodef,
  143. OSSL_PROVIDER *prov)
  144. {
  145. OSSL_STORE_LOADER *loader = NULL;
  146. const OSSL_DISPATCH *fns = algodef->implementation;
  147. if ((loader = new_loader(prov)) == NULL)
  148. return NULL;
  149. loader->scheme_id = scheme_id;
  150. loader->propdef = algodef->property_definition;
  151. loader->description = algodef->algorithm_description;
  152. for (; fns->function_id != 0; fns++) {
  153. switch (fns->function_id) {
  154. case OSSL_FUNC_STORE_OPEN:
  155. if (loader->p_open == NULL)
  156. loader->p_open = OSSL_FUNC_store_open(fns);
  157. break;
  158. case OSSL_FUNC_STORE_ATTACH:
  159. if (loader->p_attach == NULL)
  160. loader->p_attach = OSSL_FUNC_store_attach(fns);
  161. break;
  162. case OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS:
  163. if (loader->p_settable_ctx_params == NULL)
  164. loader->p_settable_ctx_params =
  165. OSSL_FUNC_store_settable_ctx_params(fns);
  166. break;
  167. case OSSL_FUNC_STORE_SET_CTX_PARAMS:
  168. if (loader->p_set_ctx_params == NULL)
  169. loader->p_set_ctx_params = OSSL_FUNC_store_set_ctx_params(fns);
  170. break;
  171. case OSSL_FUNC_STORE_LOAD:
  172. if (loader->p_load == NULL)
  173. loader->p_load = OSSL_FUNC_store_load(fns);
  174. break;
  175. case OSSL_FUNC_STORE_EOF:
  176. if (loader->p_eof == NULL)
  177. loader->p_eof = OSSL_FUNC_store_eof(fns);
  178. break;
  179. case OSSL_FUNC_STORE_CLOSE:
  180. if (loader->p_close == NULL)
  181. loader->p_close = OSSL_FUNC_store_close(fns);
  182. break;
  183. case OSSL_FUNC_STORE_EXPORT_OBJECT:
  184. if (loader->p_export_object == NULL)
  185. loader->p_export_object = OSSL_FUNC_store_export_object(fns);
  186. break;
  187. }
  188. }
  189. if ((loader->p_open == NULL && loader->p_attach == NULL)
  190. || loader->p_load == NULL
  191. || loader->p_eof == NULL
  192. || loader->p_close == NULL) {
  193. /* Only set_ctx_params is optionaal */
  194. OSSL_STORE_LOADER_free(loader);
  195. ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
  196. return NULL;
  197. }
  198. return loader;
  199. }
  200. /*
  201. * The core fetching functionality passes the scheme of the implementation.
  202. * This function is responsible to getting an identity number for them,
  203. * then call loader_from_algorithm() with that identity number.
  204. */
  205. static void *construct_loader(const OSSL_ALGORITHM *algodef,
  206. OSSL_PROVIDER *prov, void *data)
  207. {
  208. /*
  209. * This function is only called if get_loader_from_store() returned
  210. * NULL, so it's safe to say that of all the spots to create a new
  211. * namemap entry, this is it. Should the scheme already exist there, we
  212. * know that ossl_namemap_add() will return its corresponding number.
  213. */
  214. struct loader_data_st *methdata = data;
  215. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  216. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  217. const char *scheme = algodef->algorithm_names;
  218. int id = ossl_namemap_add_name(namemap, 0, scheme);
  219. void *method = NULL;
  220. if (id != 0)
  221. method = loader_from_algorithm(id, algodef, prov);
  222. /*
  223. * Flag to indicate that there was actual construction errors. This
  224. * helps inner_loader_fetch() determine what error it should
  225. * record on inaccessible algorithms.
  226. */
  227. if (method == NULL)
  228. methdata->flag_construct_error_occurred = 1;
  229. return method;
  230. }
  231. /* Intermediary function to avoid ugly casts, used below */
  232. static void destruct_loader(void *method, void *data)
  233. {
  234. OSSL_STORE_LOADER_free(method);
  235. }
  236. /* Fetching support. Can fetch by numeric identity or by scheme */
  237. static OSSL_STORE_LOADER *
  238. inner_loader_fetch(struct loader_data_st *methdata,
  239. const char *scheme, const char *properties)
  240. {
  241. OSSL_METHOD_STORE *store = get_loader_store(methdata->libctx);
  242. OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
  243. const char *const propq = properties != NULL ? properties : "";
  244. void *method = NULL;
  245. int unsupported, id;
  246. if (store == NULL || namemap == NULL) {
  247. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
  248. return NULL;
  249. }
  250. /* If we haven't received a name id yet, try to get one for the name */
  251. id = scheme != NULL ? ossl_namemap_name2num(namemap, scheme) : 0;
  252. /*
  253. * If we haven't found the name yet, chances are that the algorithm to
  254. * be fetched is unsupported.
  255. */
  256. unsupported = id == 0;
  257. if (id == 0
  258. || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) {
  259. OSSL_METHOD_CONSTRUCT_METHOD mcm = {
  260. get_tmp_loader_store,
  261. reserve_loader_store,
  262. unreserve_loader_store,
  263. get_loader_from_store,
  264. put_loader_in_store,
  265. construct_loader,
  266. destruct_loader
  267. };
  268. OSSL_PROVIDER *prov = NULL;
  269. methdata->scheme_id = id;
  270. methdata->scheme = scheme;
  271. methdata->propquery = propq;
  272. methdata->flag_construct_error_occurred = 0;
  273. if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_STORE,
  274. &prov, 0 /* !force_cache */,
  275. &mcm, methdata)) != NULL) {
  276. /*
  277. * If construction did create a method for us, we know that there
  278. * is a correct scheme_id, since those have already been calculated
  279. * in get_loader_from_store() and put_loader_in_store() above.
  280. */
  281. if (id == 0)
  282. id = ossl_namemap_name2num(namemap, scheme);
  283. ossl_method_store_cache_set(store, prov, id, propq, method,
  284. up_ref_loader, free_loader);
  285. }
  286. /*
  287. * If we never were in the constructor, the algorithm to be fetched
  288. * is unsupported.
  289. */
  290. unsupported = !methdata->flag_construct_error_occurred;
  291. }
  292. if ((id != 0 || scheme != NULL) && method == NULL) {
  293. int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
  294. const char *helpful_msg =
  295. unsupported
  296. ? ( "No store loader found. For standard store loaders you need "
  297. "at least one of the default or base providers available. "
  298. "Did you forget to load them? Info: " )
  299. : "";
  300. if (scheme == NULL)
  301. scheme = ossl_namemap_num2name(namemap, id, 0);
  302. ERR_raise_data(ERR_LIB_OSSL_STORE, code,
  303. "%s%s, Scheme (%s : %d), Properties (%s)",
  304. helpful_msg,
  305. ossl_lib_ctx_get_descriptor(methdata->libctx),
  306. scheme == NULL ? "<null>" : scheme, id,
  307. properties == NULL ? "<null>" : properties);
  308. }
  309. return method;
  310. }
  311. OSSL_STORE_LOADER *OSSL_STORE_LOADER_fetch(OSSL_LIB_CTX *libctx,
  312. const char *scheme,
  313. const char *properties)
  314. {
  315. struct loader_data_st methdata;
  316. void *method;
  317. methdata.libctx = libctx;
  318. methdata.tmp_store = NULL;
  319. method = inner_loader_fetch(&methdata, scheme, properties);
  320. dealloc_tmp_loader_store(methdata.tmp_store);
  321. return method;
  322. }
  323. int ossl_store_loader_store_cache_flush(OSSL_LIB_CTX *libctx)
  324. {
  325. OSSL_METHOD_STORE *store = get_loader_store(libctx);
  326. if (store != NULL)
  327. return ossl_method_store_cache_flush_all(store);
  328. return 1;
  329. }
  330. int ossl_store_loader_store_remove_all_provided(const OSSL_PROVIDER *prov)
  331. {
  332. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  333. OSSL_METHOD_STORE *store = get_loader_store(libctx);
  334. if (store != NULL)
  335. return ossl_method_store_remove_all_provided(store, prov);
  336. return 1;
  337. }
  338. /*
  339. * Library of basic method functions
  340. */
  341. const OSSL_PROVIDER *OSSL_STORE_LOADER_get0_provider(const OSSL_STORE_LOADER *loader)
  342. {
  343. if (!ossl_assert(loader != NULL)) {
  344. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  345. return 0;
  346. }
  347. return loader->prov;
  348. }
  349. const char *OSSL_STORE_LOADER_get0_properties(const OSSL_STORE_LOADER *loader)
  350. {
  351. if (!ossl_assert(loader != NULL)) {
  352. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  353. return 0;
  354. }
  355. return loader->propdef;
  356. }
  357. int ossl_store_loader_get_number(const OSSL_STORE_LOADER *loader)
  358. {
  359. if (!ossl_assert(loader != NULL)) {
  360. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  361. return 0;
  362. }
  363. return loader->scheme_id;
  364. }
  365. const char *OSSL_STORE_LOADER_get0_description(const OSSL_STORE_LOADER *loader)
  366. {
  367. return loader->description;
  368. }
  369. int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER *loader, const char *name)
  370. {
  371. if (loader->prov != NULL) {
  372. OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
  373. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  374. return ossl_namemap_name2num(namemap, name) == loader->scheme_id;
  375. }
  376. return 0;
  377. }
  378. struct do_one_data_st {
  379. void (*user_fn)(OSSL_STORE_LOADER *loader, void *arg);
  380. void *user_arg;
  381. };
  382. static void do_one(ossl_unused int id, void *method, void *arg)
  383. {
  384. struct do_one_data_st *data = arg;
  385. data->user_fn(method, data->user_arg);
  386. }
  387. void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX *libctx,
  388. void (*user_fn)(OSSL_STORE_LOADER *loader,
  389. void *arg),
  390. void *user_arg)
  391. {
  392. struct loader_data_st methdata;
  393. struct do_one_data_st data;
  394. methdata.libctx = libctx;
  395. methdata.tmp_store = NULL;
  396. (void)inner_loader_fetch(&methdata, NULL, NULL /* properties */);
  397. data.user_fn = user_fn;
  398. data.user_arg = user_arg;
  399. if (methdata.tmp_store != NULL)
  400. ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
  401. ossl_method_store_do_all(get_loader_store(libctx), &do_one, &data);
  402. dealloc_tmp_loader_store(methdata.tmp_store);
  403. }
  404. int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER *loader,
  405. void (*fn)(const char *name, void *data),
  406. void *data)
  407. {
  408. if (loader == NULL)
  409. return 0;
  410. if (loader->prov != NULL) {
  411. OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
  412. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  413. return ossl_namemap_doall_names(namemap, loader->scheme_id, fn, data);
  414. }
  415. return 1;
  416. }