store_meth.c 15 KB

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