decoder_meth.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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/core.h>
  10. #include <openssl/core_dispatch.h>
  11. #include <openssl/decoder.h>
  12. #include <openssl/ui.h>
  13. #include "internal/core.h"
  14. #include "internal/namemap.h"
  15. #include "internal/property.h"
  16. #include "internal/provider.h"
  17. #include "crypto/decoder.h"
  18. #include "encoder_local.h"
  19. /*
  20. * Decoder can have multiple names, separated with colons in a name string
  21. */
  22. #define NAME_SEPARATOR ':'
  23. /* Simple method structure constructor and destructor */
  24. static OSSL_DECODER *ossl_decoder_new(void)
  25. {
  26. OSSL_DECODER *decoder = NULL;
  27. if ((decoder = OPENSSL_zalloc(sizeof(*decoder))) == NULL
  28. || (decoder->base.lock = CRYPTO_THREAD_lock_new()) == NULL) {
  29. OSSL_DECODER_free(decoder);
  30. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  31. return NULL;
  32. }
  33. decoder->base.refcnt = 1;
  34. return decoder;
  35. }
  36. int OSSL_DECODER_up_ref(OSSL_DECODER *decoder)
  37. {
  38. int ref = 0;
  39. CRYPTO_UP_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
  40. return 1;
  41. }
  42. void OSSL_DECODER_free(OSSL_DECODER *decoder)
  43. {
  44. int ref = 0;
  45. if (decoder == NULL)
  46. return;
  47. CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
  48. if (ref > 0)
  49. return;
  50. ossl_provider_free(decoder->base.prov);
  51. CRYPTO_THREAD_lock_free(decoder->base.lock);
  52. OPENSSL_free(decoder);
  53. }
  54. /* Permanent decoder method store, constructor and destructor */
  55. static void decoder_store_free(void *vstore)
  56. {
  57. ossl_method_store_free(vstore);
  58. }
  59. static void *decoder_store_new(OSSL_LIB_CTX *ctx)
  60. {
  61. return ossl_method_store_new(ctx);
  62. }
  63. static const OSSL_LIB_CTX_METHOD decoder_store_method = {
  64. decoder_store_new,
  65. decoder_store_free,
  66. };
  67. /* Data to be passed through ossl_method_construct() */
  68. struct decoder_data_st {
  69. OSSL_LIB_CTX *libctx;
  70. OSSL_METHOD_CONSTRUCT_METHOD *mcm;
  71. int id; /* For get_decoder_from_store() */
  72. const char *names; /* For get_decoder_from_store() */
  73. const char *propquery; /* For get_decoder_from_store() */
  74. };
  75. /*
  76. * Generic routines to fetch / create DECODER methods with
  77. * ossl_method_construct()
  78. */
  79. /* Temporary decoder method store, constructor and destructor */
  80. static void *alloc_tmp_decoder_store(OSSL_LIB_CTX *ctx)
  81. {
  82. return ossl_method_store_new(ctx);
  83. }
  84. static void dealloc_tmp_decoder_store(void *store)
  85. {
  86. if (store != NULL)
  87. ossl_method_store_free(store);
  88. }
  89. /* Get the permanent decoder store */
  90. static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
  91. {
  92. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX,
  93. &decoder_store_method);
  94. }
  95. /* Get decoder methods from a store, or put one in */
  96. static void *get_decoder_from_store(OSSL_LIB_CTX *libctx, void *store,
  97. void *data)
  98. {
  99. struct decoder_data_st *methdata = data;
  100. void *method = NULL;
  101. int id;
  102. if ((id = methdata->id) == 0) {
  103. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  104. id = ossl_namemap_name2num(namemap, methdata->names);
  105. }
  106. if (store == NULL
  107. && (store = get_decoder_store(libctx)) == NULL)
  108. return NULL;
  109. if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
  110. return NULL;
  111. return method;
  112. }
  113. static int put_decoder_in_store(OSSL_LIB_CTX *libctx, void *store,
  114. void *method, const OSSL_PROVIDER *prov,
  115. int operation_id, const char *names,
  116. const char *propdef, void *unused)
  117. {
  118. OSSL_NAMEMAP *namemap;
  119. int id;
  120. if ((namemap = ossl_namemap_stored(libctx)) == NULL
  121. || (id = ossl_namemap_name2num(namemap, names)) == 0)
  122. return 0;
  123. if (store == NULL && (store = get_decoder_store(libctx)) == NULL)
  124. return 0;
  125. return ossl_method_store_add(store, prov, id, propdef, method,
  126. (int (*)(void *))OSSL_DECODER_up_ref,
  127. (void (*)(void *))OSSL_DECODER_free);
  128. }
  129. /* Create and populate a decoder method */
  130. void *ossl_decoder_from_dispatch(int id, const OSSL_ALGORITHM *algodef,
  131. OSSL_PROVIDER *prov)
  132. {
  133. OSSL_DECODER *decoder = NULL;
  134. const OSSL_DISPATCH *fns = algodef->implementation;
  135. if ((decoder = ossl_decoder_new()) == NULL)
  136. return NULL;
  137. decoder->base.id = id;
  138. decoder->base.propdef = algodef->property_definition;
  139. for (; fns->function_id != 0; fns++) {
  140. switch (fns->function_id) {
  141. case OSSL_FUNC_DECODER_NEWCTX:
  142. if (decoder->newctx == NULL)
  143. decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
  144. break;
  145. case OSSL_FUNC_DECODER_FREECTX:
  146. if (decoder->freectx == NULL)
  147. decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
  148. break;
  149. case OSSL_FUNC_DECODER_GET_PARAMS:
  150. if (decoder->get_params == NULL)
  151. decoder->get_params =
  152. OSSL_FUNC_decoder_get_params(fns);
  153. break;
  154. case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
  155. if (decoder->gettable_params == NULL)
  156. decoder->gettable_params =
  157. OSSL_FUNC_decoder_gettable_params(fns);
  158. break;
  159. case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
  160. if (decoder->set_ctx_params == NULL)
  161. decoder->set_ctx_params =
  162. OSSL_FUNC_decoder_set_ctx_params(fns);
  163. break;
  164. case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
  165. if (decoder->settable_ctx_params == NULL)
  166. decoder->settable_ctx_params =
  167. OSSL_FUNC_decoder_settable_ctx_params(fns);
  168. break;
  169. case OSSL_FUNC_DECODER_DECODE:
  170. if (decoder->decode == NULL)
  171. decoder->decode = OSSL_FUNC_decoder_decode(fns);
  172. break;
  173. case OSSL_FUNC_DECODER_EXPORT_OBJECT:
  174. if (decoder->export_object == NULL)
  175. decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
  176. break;
  177. }
  178. }
  179. /*
  180. * Try to check that the method is sensible.
  181. * If you have a constructor, you must have a destructor and vice versa.
  182. * You must have at least one of the encoding driver functions.
  183. */
  184. if (!((decoder->newctx == NULL && decoder->freectx == NULL)
  185. || (decoder->newctx != NULL && decoder->freectx != NULL))
  186. || decoder->decode == NULL) {
  187. OSSL_DECODER_free(decoder);
  188. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
  189. return NULL;
  190. }
  191. if (prov != NULL && !ossl_provider_up_ref(prov)) {
  192. OSSL_DECODER_free(decoder);
  193. return NULL;
  194. }
  195. decoder->base.prov = prov;
  196. return decoder;
  197. }
  198. /*
  199. * The core fetching functionality passes the names of the implementation.
  200. * This function is responsible to getting an identity number for them,
  201. * then call ossl_decoder_from_dispatch() with that identity number.
  202. */
  203. static void *construct_decoder(const OSSL_ALGORITHM *algodef,
  204. OSSL_PROVIDER *prov, void *unused)
  205. {
  206. /*
  207. * This function is only called if get_decoder_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 name already exist there, we
  210. * know that ossl_namemap_add() will return its corresponding number.
  211. */
  212. OSSL_LIB_CTX *libctx = ossl_provider_library_context(prov);
  213. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  214. const char *names = algodef->algorithm_names;
  215. int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
  216. void *method = NULL;
  217. if (id != 0)
  218. method = ossl_decoder_from_dispatch(id, algodef, prov);
  219. return method;
  220. }
  221. /* Intermediary function to avoid ugly casts, used below */
  222. static void destruct_decoder(void *method, void *data)
  223. {
  224. OSSL_DECODER_free(method);
  225. }
  226. static int up_ref_decoder(void *method)
  227. {
  228. return OSSL_DECODER_up_ref(method);
  229. }
  230. static void free_decoder(void *method)
  231. {
  232. OSSL_DECODER_free(method);
  233. }
  234. /* Fetching support. Can fetch by numeric identity or by name */
  235. static OSSL_DECODER *inner_ossl_decoder_fetch(OSSL_LIB_CTX *libctx, int id,
  236. const char *name,
  237. const char *properties)
  238. {
  239. OSSL_METHOD_STORE *store = get_decoder_store(libctx);
  240. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  241. void *method = NULL;
  242. if (store == NULL || namemap == NULL)
  243. return NULL;
  244. /*
  245. * If we have been passed neither a name_id or a name, we have an
  246. * internal programming error.
  247. */
  248. if (!ossl_assert(id != 0 || name != NULL))
  249. return NULL;
  250. if (id == 0)
  251. id = ossl_namemap_name2num(namemap, name);
  252. if (id == 0
  253. || !ossl_method_store_cache_get(store, id, properties, &method)) {
  254. OSSL_METHOD_CONSTRUCT_METHOD mcm = {
  255. alloc_tmp_decoder_store,
  256. dealloc_tmp_decoder_store,
  257. get_decoder_from_store,
  258. put_decoder_in_store,
  259. construct_decoder,
  260. destruct_decoder
  261. };
  262. struct decoder_data_st mcmdata;
  263. mcmdata.libctx = libctx;
  264. mcmdata.mcm = &mcm;
  265. mcmdata.id = id;
  266. mcmdata.names = name;
  267. mcmdata.propquery = properties;
  268. if ((method = ossl_method_construct(libctx, OSSL_OP_DECODER,
  269. 0 /* !force_cache */,
  270. &mcm, &mcmdata)) != NULL) {
  271. /*
  272. * If construction did create a method for us, we know that
  273. * there is a correct name_id and meth_id, since those have
  274. * already been calculated in get_decoder_from_store() and
  275. * put_decoder_in_store() above.
  276. */
  277. if (id == 0)
  278. id = ossl_namemap_name2num(namemap, name);
  279. ossl_method_store_cache_set(store, id, properties, method,
  280. up_ref_decoder, free_decoder);
  281. }
  282. }
  283. return method;
  284. }
  285. OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
  286. const char *properties)
  287. {
  288. return inner_ossl_decoder_fetch(libctx, 0, name, properties);
  289. }
  290. OSSL_DECODER *ossl_decoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
  291. const char *properties)
  292. {
  293. return inner_ossl_decoder_fetch(libctx, id, NULL, properties);
  294. }
  295. /*
  296. * Library of basic method functions
  297. */
  298. const OSSL_PROVIDER *OSSL_DECODER_provider(const OSSL_DECODER *decoder)
  299. {
  300. if (!ossl_assert(decoder != NULL)) {
  301. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  302. return 0;
  303. }
  304. return decoder->base.prov;
  305. }
  306. const char *OSSL_DECODER_properties(const OSSL_DECODER *decoder)
  307. {
  308. if (!ossl_assert(decoder != NULL)) {
  309. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  310. return 0;
  311. }
  312. return decoder->base.propdef;
  313. }
  314. int OSSL_DECODER_number(const OSSL_DECODER *decoder)
  315. {
  316. if (!ossl_assert(decoder != NULL)) {
  317. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  318. return 0;
  319. }
  320. return decoder->base.id;
  321. }
  322. int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
  323. {
  324. if (decoder->base.prov != NULL) {
  325. OSSL_LIB_CTX *libctx = ossl_provider_library_context(decoder->base.prov);
  326. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  327. return ossl_namemap_name2num(namemap, name) == decoder->base.id;
  328. }
  329. return 0;
  330. }
  331. struct decoder_do_all_data_st {
  332. void (*user_fn)(void *method, void *arg);
  333. void *user_arg;
  334. };
  335. static void decoder_do_one(OSSL_PROVIDER *provider,
  336. const OSSL_ALGORITHM *algodef,
  337. int no_store, void *vdata)
  338. {
  339. struct decoder_do_all_data_st *data = vdata;
  340. OSSL_LIB_CTX *libctx = ossl_provider_library_context(provider);
  341. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  342. const char *names = algodef->algorithm_names;
  343. int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
  344. void *method = NULL;
  345. if (id != 0)
  346. method = ossl_decoder_from_dispatch(id, algodef, provider);
  347. if (method != NULL) {
  348. data->user_fn(method, data->user_arg);
  349. OSSL_DECODER_free(method);
  350. }
  351. }
  352. void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
  353. void (*fn)(OSSL_DECODER *decoder, void *arg),
  354. void *arg)
  355. {
  356. struct decoder_do_all_data_st data;
  357. data.user_fn = (void (*)(void *, void *))fn;
  358. data.user_arg = arg;
  359. ossl_algorithm_do_all(libctx, OSSL_OP_DECODER, NULL,
  360. NULL, decoder_do_one, NULL,
  361. &data);
  362. }
  363. void OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
  364. void (*fn)(const char *name, void *data),
  365. void *data)
  366. {
  367. if (decoder == NULL)
  368. return;
  369. if (decoder->base.prov != NULL) {
  370. OSSL_LIB_CTX *libctx = ossl_provider_library_context(decoder->base.prov);
  371. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  372. ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
  373. }
  374. }
  375. const OSSL_PARAM *
  376. OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
  377. {
  378. if (decoder != NULL && decoder->gettable_params != NULL) {
  379. void *provctx = ossl_provider_ctx(OSSL_DECODER_provider(decoder));
  380. return decoder->gettable_params(provctx);
  381. }
  382. return NULL;
  383. }
  384. int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
  385. {
  386. if (decoder != NULL && decoder->get_params != NULL)
  387. return decoder->get_params(params);
  388. return 0;
  389. }
  390. const OSSL_PARAM *
  391. OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
  392. {
  393. if (decoder != NULL && decoder->settable_ctx_params != NULL) {
  394. void *provctx = ossl_provider_ctx(OSSL_DECODER_provider(decoder));
  395. return decoder->settable_ctx_params(provctx);
  396. }
  397. return NULL;
  398. }
  399. /*
  400. * Decoder context support
  401. */
  402. /*
  403. * |encoder| value NULL is valid, and signifies that there is no decoder.
  404. * This is useful to provide fallback mechanisms.
  405. * Functions that want to verify if there is a decoder can do so with
  406. * OSSL_DECODER_CTX_get_decoder()
  407. */
  408. OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
  409. {
  410. OSSL_DECODER_CTX *ctx;
  411. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
  412. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
  413. return ctx;
  414. }
  415. int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
  416. const OSSL_PARAM params[])
  417. {
  418. size_t i;
  419. size_t l;
  420. if (!ossl_assert(ctx != NULL)) {
  421. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  422. return 0;
  423. }
  424. if (ctx->decoder_insts == NULL)
  425. return 1;
  426. l = OSSL_DECODER_CTX_get_num_decoders(ctx);
  427. for (i = 0; i < l; i++) {
  428. OSSL_DECODER_INSTANCE *decoder_inst =
  429. sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
  430. OSSL_DECODER *decoder =
  431. OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
  432. OSSL_DECODER *decoderctx =
  433. OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
  434. if (decoderctx == NULL || decoder->set_ctx_params == NULL)
  435. continue;
  436. if (!decoder->set_ctx_params(decoderctx, params))
  437. return 0;
  438. }
  439. return 1;
  440. }
  441. void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
  442. {
  443. if (ctx != NULL) {
  444. if (ctx->cleanup != NULL)
  445. ctx->cleanup(ctx->construct_data);
  446. sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
  447. ossl_decoder_instance_free);
  448. ossl_pw_clear_passphrase_data(&ctx->pwdata);
  449. OPENSSL_free(ctx);
  450. }
  451. }