store_meth.c 15 KB

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