decoder_meth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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/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. #include "crypto/context.h"
  20. /*
  21. * Decoder can have multiple names, separated with colons in a name string
  22. */
  23. #define NAME_SEPARATOR ':'
  24. /* Simple method structure constructor and destructor */
  25. static OSSL_DECODER *ossl_decoder_new(void)
  26. {
  27. OSSL_DECODER *decoder = NULL;
  28. if ((decoder = OPENSSL_zalloc(sizeof(*decoder))) == NULL)
  29. return NULL;
  30. if ((decoder->base.lock = CRYPTO_THREAD_lock_new()) == NULL) {
  31. OSSL_DECODER_free(decoder);
  32. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_CRYPTO_LIB);
  33. return NULL;
  34. }
  35. decoder->base.refcnt = 1;
  36. return decoder;
  37. }
  38. int OSSL_DECODER_up_ref(OSSL_DECODER *decoder)
  39. {
  40. int ref = 0;
  41. CRYPTO_UP_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
  42. return 1;
  43. }
  44. void OSSL_DECODER_free(OSSL_DECODER *decoder)
  45. {
  46. int ref = 0;
  47. if (decoder == NULL)
  48. return;
  49. CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
  50. if (ref > 0)
  51. return;
  52. OPENSSL_free(decoder->base.name);
  53. ossl_property_free(decoder->base.parsed_propdef);
  54. ossl_provider_free(decoder->base.prov);
  55. CRYPTO_THREAD_lock_free(decoder->base.lock);
  56. OPENSSL_free(decoder);
  57. }
  58. /* Data to be passed through ossl_method_construct() */
  59. struct decoder_data_st {
  60. OSSL_LIB_CTX *libctx;
  61. int id; /* For get_decoder_from_store() */
  62. const char *names; /* For get_decoder_from_store() */
  63. const char *propquery; /* For get_decoder_from_store() */
  64. OSSL_METHOD_STORE *tmp_store; /* For get_tmp_decoder_store() */
  65. unsigned int flag_construct_error_occurred : 1;
  66. };
  67. /*
  68. * Generic routines to fetch / create DECODER methods with
  69. * ossl_method_construct()
  70. */
  71. /* Temporary decoder method store, constructor and destructor */
  72. static void *get_tmp_decoder_store(void *data)
  73. {
  74. struct decoder_data_st *methdata = data;
  75. if (methdata->tmp_store == NULL)
  76. methdata->tmp_store = ossl_method_store_new(methdata->libctx);
  77. return methdata->tmp_store;
  78. }
  79. static void dealloc_tmp_decoder_store(void *store)
  80. {
  81. if (store != NULL)
  82. ossl_method_store_free(store);
  83. }
  84. /* Get the permanent decoder store */
  85. static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
  86. {
  87. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX);
  88. }
  89. static int reserve_decoder_store(void *store, void *data)
  90. {
  91. struct decoder_data_st *methdata = data;
  92. if (store == NULL
  93. && (store = get_decoder_store(methdata->libctx)) == NULL)
  94. return 0;
  95. return ossl_method_lock_store(store);
  96. }
  97. static int unreserve_decoder_store(void *store, void *data)
  98. {
  99. struct decoder_data_st *methdata = data;
  100. if (store == NULL
  101. && (store = get_decoder_store(methdata->libctx)) == NULL)
  102. return 0;
  103. return ossl_method_unlock_store(store);
  104. }
  105. /* Get decoder methods from a store, or put one in */
  106. static void *get_decoder_from_store(void *store, const OSSL_PROVIDER **prov,
  107. void *data)
  108. {
  109. struct decoder_data_st *methdata = data;
  110. void *method = NULL;
  111. int id;
  112. /*
  113. * get_decoder_from_store() is only called to try and get the method
  114. * that OSSL_DECODER_fetch() is asking for, and the name or name id are
  115. * passed via methdata.
  116. */
  117. if ((id = methdata->id) == 0 && methdata->names != NULL) {
  118. OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
  119. const char *names = methdata->names;
  120. const char *q = strchr(names, NAME_SEPARATOR);
  121. size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
  122. if (namemap == 0)
  123. return NULL;
  124. id = ossl_namemap_name2num_n(namemap, names, l);
  125. }
  126. if (id == 0)
  127. return NULL;
  128. if (store == NULL
  129. && (store = get_decoder_store(methdata->libctx)) == NULL)
  130. return NULL;
  131. if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method))
  132. return NULL;
  133. return method;
  134. }
  135. static int put_decoder_in_store(void *store, void *method,
  136. const OSSL_PROVIDER *prov,
  137. const char *names, const char *propdef,
  138. void *data)
  139. {
  140. struct decoder_data_st *methdata = data;
  141. OSSL_NAMEMAP *namemap;
  142. int id;
  143. size_t l = 0;
  144. /*
  145. * put_decoder_in_store() is only called with an OSSL_DECODER method that
  146. * was successfully created by construct_decoder() below, which means that
  147. * all the names should already be stored in the namemap with the same
  148. * numeric identity, so just use the first to get that identity.
  149. */
  150. if (names != NULL) {
  151. const char *q = strchr(names, NAME_SEPARATOR);
  152. l = (q == NULL ? strlen(names) : (size_t)(q - names));
  153. }
  154. if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
  155. || (id = ossl_namemap_name2num_n(namemap, names, l)) == 0)
  156. return 0;
  157. if (store == NULL && (store = get_decoder_store(methdata->libctx)) == NULL)
  158. return 0;
  159. return ossl_method_store_add(store, prov, id, propdef, method,
  160. (int (*)(void *))OSSL_DECODER_up_ref,
  161. (void (*)(void *))OSSL_DECODER_free);
  162. }
  163. /* Create and populate a decoder method */
  164. void *ossl_decoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
  165. OSSL_PROVIDER *prov)
  166. {
  167. OSSL_DECODER *decoder = NULL;
  168. const OSSL_DISPATCH *fns = algodef->implementation;
  169. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  170. if ((decoder = ossl_decoder_new()) == NULL)
  171. return NULL;
  172. decoder->base.id = id;
  173. if ((decoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  174. OSSL_DECODER_free(decoder);
  175. return NULL;
  176. }
  177. decoder->base.algodef = algodef;
  178. if ((decoder->base.parsed_propdef
  179. = ossl_parse_property(libctx, algodef->property_definition)) == NULL) {
  180. OSSL_DECODER_free(decoder);
  181. return NULL;
  182. }
  183. for (; fns->function_id != 0; fns++) {
  184. switch (fns->function_id) {
  185. case OSSL_FUNC_DECODER_NEWCTX:
  186. if (decoder->newctx == NULL)
  187. decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
  188. break;
  189. case OSSL_FUNC_DECODER_FREECTX:
  190. if (decoder->freectx == NULL)
  191. decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
  192. break;
  193. case OSSL_FUNC_DECODER_GET_PARAMS:
  194. if (decoder->get_params == NULL)
  195. decoder->get_params =
  196. OSSL_FUNC_decoder_get_params(fns);
  197. break;
  198. case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
  199. if (decoder->gettable_params == NULL)
  200. decoder->gettable_params =
  201. OSSL_FUNC_decoder_gettable_params(fns);
  202. break;
  203. case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
  204. if (decoder->set_ctx_params == NULL)
  205. decoder->set_ctx_params =
  206. OSSL_FUNC_decoder_set_ctx_params(fns);
  207. break;
  208. case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
  209. if (decoder->settable_ctx_params == NULL)
  210. decoder->settable_ctx_params =
  211. OSSL_FUNC_decoder_settable_ctx_params(fns);
  212. break;
  213. case OSSL_FUNC_DECODER_DOES_SELECTION:
  214. if (decoder->does_selection == NULL)
  215. decoder->does_selection =
  216. OSSL_FUNC_decoder_does_selection(fns);
  217. break;
  218. case OSSL_FUNC_DECODER_DECODE:
  219. if (decoder->decode == NULL)
  220. decoder->decode = OSSL_FUNC_decoder_decode(fns);
  221. break;
  222. case OSSL_FUNC_DECODER_EXPORT_OBJECT:
  223. if (decoder->export_object == NULL)
  224. decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
  225. break;
  226. }
  227. }
  228. /*
  229. * Try to check that the method is sensible.
  230. * If you have a constructor, you must have a destructor and vice versa.
  231. * You must have at least one of the encoding driver functions.
  232. */
  233. if (!((decoder->newctx == NULL && decoder->freectx == NULL)
  234. || (decoder->newctx != NULL && decoder->freectx != NULL))
  235. || decoder->decode == NULL) {
  236. OSSL_DECODER_free(decoder);
  237. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
  238. return NULL;
  239. }
  240. if (prov != NULL && !ossl_provider_up_ref(prov)) {
  241. OSSL_DECODER_free(decoder);
  242. return NULL;
  243. }
  244. decoder->base.prov = prov;
  245. return decoder;
  246. }
  247. /*
  248. * The core fetching functionality passes the names of the implementation.
  249. * This function is responsible to getting an identity number for them,
  250. * then call ossl_decoder_from_algorithm() with that identity number.
  251. */
  252. static void *construct_decoder(const OSSL_ALGORITHM *algodef,
  253. OSSL_PROVIDER *prov, void *data)
  254. {
  255. /*
  256. * This function is only called if get_decoder_from_store() returned
  257. * NULL, so it's safe to say that of all the spots to create a new
  258. * namemap entry, this is it. Should the name already exist there, we
  259. * know that ossl_namemap_add() will return its corresponding number.
  260. */
  261. struct decoder_data_st *methdata = data;
  262. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  263. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  264. const char *names = algodef->algorithm_names;
  265. int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
  266. void *method = NULL;
  267. if (id != 0)
  268. method = ossl_decoder_from_algorithm(id, algodef, prov);
  269. /*
  270. * Flag to indicate that there was actual construction errors. This
  271. * helps inner_evp_generic_fetch() determine what error it should
  272. * record on inaccessible algorithms.
  273. */
  274. if (method == NULL)
  275. methdata->flag_construct_error_occurred = 1;
  276. return method;
  277. }
  278. /* Intermediary function to avoid ugly casts, used below */
  279. static void destruct_decoder(void *method, void *data)
  280. {
  281. OSSL_DECODER_free(method);
  282. }
  283. static int up_ref_decoder(void *method)
  284. {
  285. return OSSL_DECODER_up_ref(method);
  286. }
  287. static void free_decoder(void *method)
  288. {
  289. OSSL_DECODER_free(method);
  290. }
  291. /* Fetching support. Can fetch by numeric identity or by name */
  292. static OSSL_DECODER *
  293. inner_ossl_decoder_fetch(struct decoder_data_st *methdata,
  294. const char *name, const char *properties)
  295. {
  296. OSSL_METHOD_STORE *store = get_decoder_store(methdata->libctx);
  297. OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
  298. const char *const propq = properties != NULL ? properties : "";
  299. void *method = NULL;
  300. int unsupported, id;
  301. if (store == NULL || namemap == NULL) {
  302. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT);
  303. return NULL;
  304. }
  305. id = name != NULL ? ossl_namemap_name2num(namemap, name) : 0;
  306. /*
  307. * If we haven't found the name yet, chances are that the algorithm to
  308. * be fetched is unsupported.
  309. */
  310. unsupported = id == 0;
  311. if (id == 0
  312. || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) {
  313. OSSL_METHOD_CONSTRUCT_METHOD mcm = {
  314. get_tmp_decoder_store,
  315. reserve_decoder_store,
  316. unreserve_decoder_store,
  317. get_decoder_from_store,
  318. put_decoder_in_store,
  319. construct_decoder,
  320. destruct_decoder
  321. };
  322. OSSL_PROVIDER *prov = NULL;
  323. methdata->id = id;
  324. methdata->names = name;
  325. methdata->propquery = propq;
  326. methdata->flag_construct_error_occurred = 0;
  327. if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_DECODER,
  328. &prov, 0 /* !force_cache */,
  329. &mcm, methdata)) != NULL) {
  330. /*
  331. * If construction did create a method for us, we know that
  332. * there is a correct name_id and meth_id, since those have
  333. * already been calculated in get_decoder_from_store() and
  334. * put_decoder_in_store() above.
  335. */
  336. if (id == 0 && name != NULL)
  337. id = ossl_namemap_name2num(namemap, name);
  338. if (id != 0)
  339. ossl_method_store_cache_set(store, prov, id, propq, method,
  340. up_ref_decoder, free_decoder);
  341. }
  342. /*
  343. * If we never were in the constructor, the algorithm to be fetched
  344. * is unsupported.
  345. */
  346. unsupported = !methdata->flag_construct_error_occurred;
  347. }
  348. if ((id != 0 || name != NULL) && method == NULL) {
  349. int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
  350. if (name == NULL)
  351. name = ossl_namemap_num2name(namemap, id, 0);
  352. ERR_raise_data(ERR_LIB_OSSL_DECODER, code,
  353. "%s, Name (%s : %d), Properties (%s)",
  354. ossl_lib_ctx_get_descriptor(methdata->libctx),
  355. name == NULL ? "<null>" : name, id,
  356. properties == NULL ? "<null>" : properties);
  357. }
  358. return method;
  359. }
  360. OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
  361. const char *properties)
  362. {
  363. struct decoder_data_st methdata;
  364. void *method;
  365. methdata.libctx = libctx;
  366. methdata.tmp_store = NULL;
  367. method = inner_ossl_decoder_fetch(&methdata, name, properties);
  368. dealloc_tmp_decoder_store(methdata.tmp_store);
  369. return method;
  370. }
  371. int ossl_decoder_store_cache_flush(OSSL_LIB_CTX *libctx)
  372. {
  373. OSSL_METHOD_STORE *store = get_decoder_store(libctx);
  374. if (store != NULL)
  375. return ossl_method_store_cache_flush_all(store);
  376. return 1;
  377. }
  378. int ossl_decoder_store_remove_all_provided(const OSSL_PROVIDER *prov)
  379. {
  380. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  381. OSSL_METHOD_STORE *store = get_decoder_store(libctx);
  382. if (store != NULL)
  383. return ossl_method_store_remove_all_provided(store, prov);
  384. return 1;
  385. }
  386. /*
  387. * Library of basic method functions
  388. */
  389. const OSSL_PROVIDER *OSSL_DECODER_get0_provider(const OSSL_DECODER *decoder)
  390. {
  391. if (!ossl_assert(decoder != NULL)) {
  392. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  393. return 0;
  394. }
  395. return decoder->base.prov;
  396. }
  397. const char *OSSL_DECODER_get0_properties(const OSSL_DECODER *decoder)
  398. {
  399. if (!ossl_assert(decoder != NULL)) {
  400. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  401. return 0;
  402. }
  403. return decoder->base.algodef->property_definition;
  404. }
  405. const OSSL_PROPERTY_LIST *
  406. ossl_decoder_parsed_properties(const OSSL_DECODER *decoder)
  407. {
  408. if (!ossl_assert(decoder != NULL)) {
  409. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  410. return 0;
  411. }
  412. return decoder->base.parsed_propdef;
  413. }
  414. int ossl_decoder_get_number(const OSSL_DECODER *decoder)
  415. {
  416. if (!ossl_assert(decoder != NULL)) {
  417. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  418. return 0;
  419. }
  420. return decoder->base.id;
  421. }
  422. const char *OSSL_DECODER_get0_name(const OSSL_DECODER *decoder)
  423. {
  424. return decoder->base.name;
  425. }
  426. const char *OSSL_DECODER_get0_description(const OSSL_DECODER *decoder)
  427. {
  428. return decoder->base.algodef->algorithm_description;
  429. }
  430. int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
  431. {
  432. if (decoder->base.prov != NULL) {
  433. OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
  434. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  435. return ossl_namemap_name2num(namemap, name) == decoder->base.id;
  436. }
  437. return 0;
  438. }
  439. static int resolve_name(OSSL_DECODER *decoder, const char *name)
  440. {
  441. OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
  442. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  443. return ossl_namemap_name2num(namemap, name);
  444. }
  445. int ossl_decoder_fast_is_a(OSSL_DECODER *decoder, const char *name, int *id_cache)
  446. {
  447. int id = *id_cache;
  448. if (id <= 0)
  449. *id_cache = id = resolve_name(decoder, name);
  450. return id > 0 && ossl_decoder_get_number(decoder) == id;
  451. }
  452. struct do_one_data_st {
  453. void (*user_fn)(OSSL_DECODER *decoder, void *arg);
  454. void *user_arg;
  455. };
  456. static void do_one(ossl_unused int id, void *method, void *arg)
  457. {
  458. struct do_one_data_st *data = arg;
  459. data->user_fn(method, data->user_arg);
  460. }
  461. void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
  462. void (*user_fn)(OSSL_DECODER *decoder,
  463. void *arg),
  464. void *user_arg)
  465. {
  466. struct decoder_data_st methdata;
  467. struct do_one_data_st data;
  468. methdata.libctx = libctx;
  469. methdata.tmp_store = NULL;
  470. (void)inner_ossl_decoder_fetch(&methdata, NULL, NULL /* properties */);
  471. data.user_fn = user_fn;
  472. data.user_arg = user_arg;
  473. if (methdata.tmp_store != NULL)
  474. ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
  475. ossl_method_store_do_all(get_decoder_store(libctx), &do_one, &data);
  476. dealloc_tmp_decoder_store(methdata.tmp_store);
  477. }
  478. int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
  479. void (*fn)(const char *name, void *data),
  480. void *data)
  481. {
  482. if (decoder == NULL)
  483. return 0;
  484. if (decoder->base.prov != NULL) {
  485. OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
  486. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  487. return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
  488. }
  489. return 1;
  490. }
  491. const OSSL_PARAM *
  492. OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
  493. {
  494. if (decoder != NULL && decoder->gettable_params != NULL) {
  495. void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
  496. return decoder->gettable_params(provctx);
  497. }
  498. return NULL;
  499. }
  500. int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
  501. {
  502. if (decoder != NULL && decoder->get_params != NULL)
  503. return decoder->get_params(params);
  504. return 0;
  505. }
  506. const OSSL_PARAM *
  507. OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
  508. {
  509. if (decoder != NULL && decoder->settable_ctx_params != NULL) {
  510. void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
  511. return decoder->settable_ctx_params(provctx);
  512. }
  513. return NULL;
  514. }
  515. /*
  516. * Decoder context support
  517. */
  518. /*
  519. * |encoder| value NULL is valid, and signifies that there is no decoder.
  520. * This is useful to provide fallback mechanisms.
  521. * Functions that want to verify if there is a decoder can do so with
  522. * OSSL_DECODER_CTX_get_decoder()
  523. */
  524. OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
  525. {
  526. OSSL_DECODER_CTX *ctx;
  527. ctx = OPENSSL_zalloc(sizeof(*ctx));
  528. return ctx;
  529. }
  530. int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
  531. const OSSL_PARAM params[])
  532. {
  533. int ok = 1;
  534. size_t i;
  535. size_t l;
  536. if (!ossl_assert(ctx != NULL)) {
  537. ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
  538. return 0;
  539. }
  540. if (ctx->decoder_insts == NULL)
  541. return 1;
  542. l = OSSL_DECODER_CTX_get_num_decoders(ctx);
  543. for (i = 0; i < l; i++) {
  544. OSSL_DECODER_INSTANCE *decoder_inst =
  545. sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
  546. OSSL_DECODER *decoder =
  547. OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
  548. OSSL_DECODER *decoderctx =
  549. OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
  550. if (decoderctx == NULL || decoder->set_ctx_params == NULL)
  551. continue;
  552. if (!decoder->set_ctx_params(decoderctx, params))
  553. ok = 0;
  554. }
  555. return ok;
  556. }
  557. void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
  558. {
  559. if (ctx != NULL) {
  560. if (ctx->cleanup != NULL)
  561. ctx->cleanup(ctx->construct_data);
  562. sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
  563. ossl_decoder_instance_free);
  564. ossl_pw_clear_passphrase_data(&ctx->pwdata);
  565. OPENSSL_free(ctx);
  566. }
  567. }