store_meth.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
  71. loader_store_new,
  72. loader_store_free,
  73. };
  74. /* Data to be passed through ossl_method_construct() */
  75. struct loader_data_st {
  76. OSSL_LIB_CTX *libctx;
  77. OSSL_METHOD_CONSTRUCT_METHOD *mcm;
  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(OSSL_LIB_CTX *libctx, void *store,
  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(libctx);
  116. id = ossl_namemap_name2num(namemap, methdata->scheme);
  117. }
  118. if (store == NULL
  119. && (store = get_loader_store(libctx)) == NULL)
  120. return NULL;
  121. if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
  122. return NULL;
  123. return method;
  124. }
  125. static int put_loader_in_store(OSSL_LIB_CTX *libctx, void *store,
  126. void *method, const OSSL_PROVIDER *prov,
  127. int operation_id, const char *scheme,
  128. const char *propdef, void *unused)
  129. {
  130. OSSL_NAMEMAP *namemap;
  131. int id;
  132. if ((namemap = ossl_namemap_stored(libctx)) == NULL
  133. || (id = ossl_namemap_name2num(namemap, scheme)) == 0)
  134. return 0;
  135. if (store == NULL && (store = get_loader_store(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->mcm = &mcm;
  274. methdata->scheme_id = id;
  275. methdata->scheme = scheme;
  276. methdata->propquery = properties;
  277. methdata->flag_construct_error_occurred = 0;
  278. if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_STORE,
  279. 0 /* !force_cache */,
  280. &mcm, methdata)) != NULL) {
  281. /*
  282. * If construction did create a method for us, we know that there
  283. * is a correct scheme_id, since those have already been calculated
  284. * in get_loader_from_store() and put_loader_in_store() above.
  285. */
  286. if (id == 0)
  287. id = ossl_namemap_name2num(namemap, scheme);
  288. ossl_method_store_cache_set(store, id, properties, method,
  289. up_ref_loader, free_loader);
  290. }
  291. /*
  292. * If we never were in the constructor, the algorithm to be fetched
  293. * is unsupported.
  294. */
  295. unsupported = !methdata->flag_construct_error_occurred;
  296. }
  297. if ((id != 0 || scheme != NULL) && method == NULL) {
  298. int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
  299. if (scheme == NULL)
  300. scheme = ossl_namemap_num2name(namemap, id, 0);
  301. ERR_raise_data(ERR_LIB_OSSL_STORE, code,
  302. "%s, Scheme (%s : %d), Properties (%s)",
  303. ossl_lib_ctx_get_descriptor(methdata->libctx),
  304. scheme = NULL ? "<null>" : scheme, id,
  305. properties == NULL ? "<null>" : properties);
  306. }
  307. return method;
  308. }
  309. OSSL_STORE_LOADER *OSSL_STORE_LOADER_fetch(OSSL_LIB_CTX *libctx,
  310. const char *scheme,
  311. const char *properties)
  312. {
  313. struct loader_data_st methdata;
  314. void *method;
  315. methdata.libctx = libctx;
  316. methdata.tmp_store = NULL;
  317. method = inner_loader_fetch(&methdata, 0, scheme, properties);
  318. dealloc_tmp_loader_store(methdata.tmp_store);
  319. return method;
  320. }
  321. OSSL_STORE_LOADER *ossl_store_loader_fetch_by_number(OSSL_LIB_CTX *libctx,
  322. int scheme_id,
  323. const char *properties)
  324. {
  325. struct loader_data_st methdata;
  326. void *method;
  327. methdata.libctx = libctx;
  328. methdata.tmp_store = NULL;
  329. method = inner_loader_fetch(&methdata, scheme_id, NULL, properties);
  330. dealloc_tmp_loader_store(methdata.tmp_store);
  331. return method;
  332. }
  333. /*
  334. * Library of basic method functions
  335. */
  336. const OSSL_PROVIDER *OSSL_STORE_LOADER_get0_provider(const OSSL_STORE_LOADER *loader)
  337. {
  338. if (!ossl_assert(loader != NULL)) {
  339. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  340. return 0;
  341. }
  342. return loader->prov;
  343. }
  344. const char *OSSL_STORE_LOADER_get0_properties(const OSSL_STORE_LOADER *loader)
  345. {
  346. if (!ossl_assert(loader != NULL)) {
  347. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  348. return 0;
  349. }
  350. return loader->propdef;
  351. }
  352. int ossl_store_loader_get_number(const OSSL_STORE_LOADER *loader)
  353. {
  354. if (!ossl_assert(loader != NULL)) {
  355. ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
  356. return 0;
  357. }
  358. return loader->scheme_id;
  359. }
  360. const char *OSSL_STORE_LOADER_get0_description(const OSSL_STORE_LOADER *loader)
  361. {
  362. return loader->description;
  363. }
  364. int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER *loader, const char *name)
  365. {
  366. if (loader->prov != NULL) {
  367. OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
  368. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  369. return ossl_namemap_name2num(namemap, name) == loader->scheme_id;
  370. }
  371. return 0;
  372. }
  373. struct do_one_data_st {
  374. void (*user_fn)(OSSL_STORE_LOADER *loader, void *arg);
  375. void *user_arg;
  376. };
  377. static void do_one(ossl_unused int id, void *method, void *arg)
  378. {
  379. struct do_one_data_st *data = arg;
  380. data->user_fn(method, data->user_arg);
  381. }
  382. void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX *libctx,
  383. void (*user_fn)(OSSL_STORE_LOADER *loader,
  384. void *arg),
  385. void *user_arg)
  386. {
  387. struct loader_data_st methdata;
  388. struct do_one_data_st data;
  389. methdata.libctx = libctx;
  390. methdata.tmp_store = NULL;
  391. (void)inner_loader_fetch(&methdata, 0, NULL, NULL /* properties */);
  392. data.user_fn = user_fn;
  393. data.user_arg = user_arg;
  394. if (methdata.tmp_store != NULL)
  395. ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
  396. ossl_method_store_do_all(get_loader_store(libctx), &do_one, &data);
  397. dealloc_tmp_loader_store(methdata.tmp_store);
  398. }
  399. int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER *loader,
  400. void (*fn)(const char *name, void *data),
  401. void *data)
  402. {
  403. if (loader == NULL)
  404. return 0;
  405. if (loader->prov != NULL) {
  406. OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
  407. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  408. return ossl_namemap_doall_names(namemap, loader->scheme_id, fn, data);
  409. }
  410. return 1;
  411. }