decoder_meth.c 20 KB

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