decoder_meth.c 20 KB

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