encoder_meth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*
  2. * Copyright 2019-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/encoder.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/encoder.h"
  18. #include "encoder_local.h"
  19. #include "crypto/context.h"
  20. /*
  21. * Encoder can have multiple names, separated with colons in a name string
  22. */
  23. #define NAME_SEPARATOR ':'
  24. static void ossl_encoder_free(void *data)
  25. {
  26. OSSL_ENCODER_free(data);
  27. }
  28. static int ossl_encoder_up_ref(void *data)
  29. {
  30. return OSSL_ENCODER_up_ref(data);
  31. }
  32. /* Simple method structure constructor and destructor */
  33. static OSSL_ENCODER *ossl_encoder_new(void)
  34. {
  35. OSSL_ENCODER *encoder = NULL;
  36. if ((encoder = OPENSSL_zalloc(sizeof(*encoder))) == NULL)
  37. return NULL;
  38. if (!CRYPTO_NEW_REF(&encoder->base.refcnt, 1)) {
  39. OSSL_ENCODER_free(encoder);
  40. return NULL;
  41. }
  42. return encoder;
  43. }
  44. int OSSL_ENCODER_up_ref(OSSL_ENCODER *encoder)
  45. {
  46. int ref = 0;
  47. CRYPTO_UP_REF(&encoder->base.refcnt, &ref);
  48. return 1;
  49. }
  50. void OSSL_ENCODER_free(OSSL_ENCODER *encoder)
  51. {
  52. int ref = 0;
  53. if (encoder == NULL)
  54. return;
  55. CRYPTO_DOWN_REF(&encoder->base.refcnt, &ref);
  56. if (ref > 0)
  57. return;
  58. OPENSSL_free(encoder->base.name);
  59. ossl_property_free(encoder->base.parsed_propdef);
  60. ossl_provider_free(encoder->base.prov);
  61. CRYPTO_FREE_REF(&encoder->base.refcnt);
  62. OPENSSL_free(encoder);
  63. }
  64. /* Data to be passed through ossl_method_construct() */
  65. struct encoder_data_st {
  66. OSSL_LIB_CTX *libctx;
  67. int id; /* For get_encoder_from_store() */
  68. const char *names; /* For get_encoder_from_store() */
  69. const char *propquery; /* For get_encoder_from_store() */
  70. OSSL_METHOD_STORE *tmp_store; /* For get_tmp_encoder_store() */
  71. unsigned int flag_construct_error_occurred : 1;
  72. };
  73. /*
  74. * Generic routines to fetch / create ENCODER methods with
  75. * ossl_method_construct()
  76. */
  77. /* Temporary encoder method store, constructor and destructor */
  78. static void *get_tmp_encoder_store(void *data)
  79. {
  80. struct encoder_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_encoder_store(void *store)
  86. {
  87. if (store != NULL)
  88. ossl_method_store_free(store);
  89. }
  90. /* Get the permanent encoder store */
  91. static OSSL_METHOD_STORE *get_encoder_store(OSSL_LIB_CTX *libctx)
  92. {
  93. return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_ENCODER_STORE_INDEX);
  94. }
  95. static int reserve_encoder_store(void *store, void *data)
  96. {
  97. struct encoder_data_st *methdata = data;
  98. if (store == NULL
  99. && (store = get_encoder_store(methdata->libctx)) == NULL)
  100. return 0;
  101. return ossl_method_lock_store(store);
  102. }
  103. static int unreserve_encoder_store(void *store, void *data)
  104. {
  105. struct encoder_data_st *methdata = data;
  106. if (store == NULL
  107. && (store = get_encoder_store(methdata->libctx)) == NULL)
  108. return 0;
  109. return ossl_method_unlock_store(store);
  110. }
  111. /* Get encoder methods from a store, or put one in */
  112. static void *get_encoder_from_store(void *store, const OSSL_PROVIDER **prov,
  113. void *data)
  114. {
  115. struct encoder_data_st *methdata = data;
  116. void *method = NULL;
  117. int id;
  118. /*
  119. * get_encoder_from_store() is only called to try and get the method
  120. * that OSSL_ENCODER_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, methdata->names, l);
  131. }
  132. if (id == 0)
  133. return NULL;
  134. if (store == NULL
  135. && (store = get_encoder_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_encoder_in_store(void *store, void *method,
  142. const OSSL_PROVIDER *prov,
  143. const char *names, const char *propdef,
  144. void *data)
  145. {
  146. struct encoder_data_st *methdata = data;
  147. OSSL_NAMEMAP *namemap;
  148. int id;
  149. size_t l = 0;
  150. /*
  151. * put_encoder_in_store() is only called with an OSSL_ENCODER method that
  152. * was successfully created by construct_encoder() 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_encoder_store(methdata->libctx)) == NULL)
  164. return 0;
  165. return ossl_method_store_add(store, prov, id, propdef, method,
  166. ossl_encoder_up_ref,
  167. ossl_encoder_free);
  168. }
  169. /* Create and populate a encoder method */
  170. static void *encoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
  171. OSSL_PROVIDER *prov)
  172. {
  173. OSSL_ENCODER *encoder = NULL;
  174. const OSSL_DISPATCH *fns = algodef->implementation;
  175. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  176. if ((encoder = ossl_encoder_new()) == NULL)
  177. return NULL;
  178. encoder->base.id = id;
  179. if ((encoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
  180. OSSL_ENCODER_free(encoder);
  181. return NULL;
  182. }
  183. encoder->base.algodef = algodef;
  184. if ((encoder->base.parsed_propdef
  185. = ossl_parse_property(libctx, algodef->property_definition)) == NULL) {
  186. OSSL_ENCODER_free(encoder);
  187. return NULL;
  188. }
  189. for (; fns->function_id != 0; fns++) {
  190. switch (fns->function_id) {
  191. case OSSL_FUNC_ENCODER_NEWCTX:
  192. if (encoder->newctx == NULL)
  193. encoder->newctx =
  194. OSSL_FUNC_encoder_newctx(fns);
  195. break;
  196. case OSSL_FUNC_ENCODER_FREECTX:
  197. if (encoder->freectx == NULL)
  198. encoder->freectx =
  199. OSSL_FUNC_encoder_freectx(fns);
  200. break;
  201. case OSSL_FUNC_ENCODER_GET_PARAMS:
  202. if (encoder->get_params == NULL)
  203. encoder->get_params =
  204. OSSL_FUNC_encoder_get_params(fns);
  205. break;
  206. case OSSL_FUNC_ENCODER_GETTABLE_PARAMS:
  207. if (encoder->gettable_params == NULL)
  208. encoder->gettable_params =
  209. OSSL_FUNC_encoder_gettable_params(fns);
  210. break;
  211. case OSSL_FUNC_ENCODER_SET_CTX_PARAMS:
  212. if (encoder->set_ctx_params == NULL)
  213. encoder->set_ctx_params =
  214. OSSL_FUNC_encoder_set_ctx_params(fns);
  215. break;
  216. case OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS:
  217. if (encoder->settable_ctx_params == NULL)
  218. encoder->settable_ctx_params =
  219. OSSL_FUNC_encoder_settable_ctx_params(fns);
  220. break;
  221. case OSSL_FUNC_ENCODER_DOES_SELECTION:
  222. if (encoder->does_selection == NULL)
  223. encoder->does_selection =
  224. OSSL_FUNC_encoder_does_selection(fns);
  225. break;
  226. case OSSL_FUNC_ENCODER_ENCODE:
  227. if (encoder->encode == NULL)
  228. encoder->encode = OSSL_FUNC_encoder_encode(fns);
  229. break;
  230. case OSSL_FUNC_ENCODER_IMPORT_OBJECT:
  231. if (encoder->import_object == NULL)
  232. encoder->import_object =
  233. OSSL_FUNC_encoder_import_object(fns);
  234. break;
  235. case OSSL_FUNC_ENCODER_FREE_OBJECT:
  236. if (encoder->free_object == NULL)
  237. encoder->free_object =
  238. OSSL_FUNC_encoder_free_object(fns);
  239. break;
  240. }
  241. }
  242. /*
  243. * Try to check that the method is sensible.
  244. * If you have a constructor, you must have a destructor and vice versa.
  245. * You must have the encoding driver functions.
  246. */
  247. if (!((encoder->newctx == NULL && encoder->freectx == NULL)
  248. || (encoder->newctx != NULL && encoder->freectx != NULL)
  249. || (encoder->import_object != NULL && encoder->free_object != NULL)
  250. || (encoder->import_object == NULL && encoder->free_object == NULL))
  251. || encoder->encode == NULL) {
  252. OSSL_ENCODER_free(encoder);
  253. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
  254. return NULL;
  255. }
  256. if (prov != NULL && !ossl_provider_up_ref(prov)) {
  257. OSSL_ENCODER_free(encoder);
  258. return NULL;
  259. }
  260. encoder->base.prov = prov;
  261. return encoder;
  262. }
  263. /*
  264. * The core fetching functionality passes the names of the implementation.
  265. * This function is responsible to getting an identity number for them,
  266. * then call encoder_from_algorithm() with that identity number.
  267. */
  268. static void *construct_encoder(const OSSL_ALGORITHM *algodef,
  269. OSSL_PROVIDER *prov, void *data)
  270. {
  271. /*
  272. * This function is only called if get_encoder_from_store() returned
  273. * NULL, so it's safe to say that of all the spots to create a new
  274. * namemap entry, this is it. Should the name already exist there, we
  275. * know that ossl_namemap_add() will return its corresponding number.
  276. */
  277. struct encoder_data_st *methdata = data;
  278. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  279. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  280. const char *names = algodef->algorithm_names;
  281. int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
  282. void *method = NULL;
  283. if (id != 0)
  284. method = encoder_from_algorithm(id, algodef, prov);
  285. /*
  286. * Flag to indicate that there was actual construction errors. This
  287. * helps inner_evp_generic_fetch() determine what error it should
  288. * record on inaccessible algorithms.
  289. */
  290. if (method == NULL)
  291. methdata->flag_construct_error_occurred = 1;
  292. return method;
  293. }
  294. /* Intermediary function to avoid ugly casts, used below */
  295. static void destruct_encoder(void *method, void *data)
  296. {
  297. OSSL_ENCODER_free(method);
  298. }
  299. static int up_ref_encoder(void *method)
  300. {
  301. return OSSL_ENCODER_up_ref(method);
  302. }
  303. static void free_encoder(void *method)
  304. {
  305. OSSL_ENCODER_free(method);
  306. }
  307. /* Fetching support. Can fetch by numeric identity or by name */
  308. static OSSL_ENCODER *
  309. inner_ossl_encoder_fetch(struct encoder_data_st *methdata,
  310. const char *name, const char *properties)
  311. {
  312. OSSL_METHOD_STORE *store = get_encoder_store(methdata->libctx);
  313. OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
  314. const char *const propq = properties != NULL ? properties : "";
  315. void *method = NULL;
  316. int unsupported, id;
  317. if (store == NULL || namemap == NULL) {
  318. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
  319. return NULL;
  320. }
  321. id = name != NULL ? ossl_namemap_name2num(namemap, name) : 0;
  322. /*
  323. * If we haven't found the name yet, chances are that the algorithm to
  324. * be fetched is unsupported.
  325. */
  326. unsupported = id == 0;
  327. if (id == 0
  328. || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) {
  329. OSSL_METHOD_CONSTRUCT_METHOD mcm = {
  330. get_tmp_encoder_store,
  331. reserve_encoder_store,
  332. unreserve_encoder_store,
  333. get_encoder_from_store,
  334. put_encoder_in_store,
  335. construct_encoder,
  336. destruct_encoder
  337. };
  338. OSSL_PROVIDER *prov = NULL;
  339. methdata->id = id;
  340. methdata->names = name;
  341. methdata->propquery = propq;
  342. methdata->flag_construct_error_occurred = 0;
  343. if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_ENCODER,
  344. &prov, 0 /* !force_cache */,
  345. &mcm, methdata)) != NULL) {
  346. /*
  347. * If construction did create a method for us, we know that
  348. * there is a correct name_id and meth_id, since those have
  349. * already been calculated in get_encoder_from_store() and
  350. * put_encoder_in_store() above.
  351. */
  352. if (id == 0)
  353. id = ossl_namemap_name2num(namemap, name);
  354. ossl_method_store_cache_set(store, prov, id, propq, method,
  355. up_ref_encoder, free_encoder);
  356. }
  357. /*
  358. * If we never were in the constructor, the algorithm to be fetched
  359. * is unsupported.
  360. */
  361. unsupported = !methdata->flag_construct_error_occurred;
  362. }
  363. if ((id != 0 || name != NULL) && method == NULL) {
  364. int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
  365. if (name == NULL)
  366. name = ossl_namemap_num2name(namemap, id, 0);
  367. ERR_raise_data(ERR_LIB_OSSL_ENCODER, code,
  368. "%s, Name (%s : %d), Properties (%s)",
  369. ossl_lib_ctx_get_descriptor(methdata->libctx),
  370. name == NULL ? "<null>" : name, id,
  371. properties == NULL ? "<null>" : properties);
  372. }
  373. return method;
  374. }
  375. OSSL_ENCODER *OSSL_ENCODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
  376. const char *properties)
  377. {
  378. struct encoder_data_st methdata;
  379. void *method;
  380. methdata.libctx = libctx;
  381. methdata.tmp_store = NULL;
  382. method = inner_ossl_encoder_fetch(&methdata, name, properties);
  383. dealloc_tmp_encoder_store(methdata.tmp_store);
  384. return method;
  385. }
  386. int ossl_encoder_store_cache_flush(OSSL_LIB_CTX *libctx)
  387. {
  388. OSSL_METHOD_STORE *store = get_encoder_store(libctx);
  389. if (store != NULL)
  390. return ossl_method_store_cache_flush_all(store);
  391. return 1;
  392. }
  393. int ossl_encoder_store_remove_all_provided(const OSSL_PROVIDER *prov)
  394. {
  395. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  396. OSSL_METHOD_STORE *store = get_encoder_store(libctx);
  397. if (store != NULL)
  398. return ossl_method_store_remove_all_provided(store, prov);
  399. return 1;
  400. }
  401. /*
  402. * Library of basic method functions
  403. */
  404. const OSSL_PROVIDER *OSSL_ENCODER_get0_provider(const OSSL_ENCODER *encoder)
  405. {
  406. if (!ossl_assert(encoder != NULL)) {
  407. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  408. return 0;
  409. }
  410. return encoder->base.prov;
  411. }
  412. const char *OSSL_ENCODER_get0_properties(const OSSL_ENCODER *encoder)
  413. {
  414. if (!ossl_assert(encoder != NULL)) {
  415. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  416. return 0;
  417. }
  418. return encoder->base.algodef->property_definition;
  419. }
  420. const OSSL_PROPERTY_LIST *
  421. ossl_encoder_parsed_properties(const OSSL_ENCODER *encoder)
  422. {
  423. if (!ossl_assert(encoder != NULL)) {
  424. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  425. return 0;
  426. }
  427. return encoder->base.parsed_propdef;
  428. }
  429. int ossl_encoder_get_number(const OSSL_ENCODER *encoder)
  430. {
  431. if (!ossl_assert(encoder != NULL)) {
  432. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  433. return 0;
  434. }
  435. return encoder->base.id;
  436. }
  437. const char *OSSL_ENCODER_get0_name(const OSSL_ENCODER *encoder)
  438. {
  439. return encoder->base.name;
  440. }
  441. const char *OSSL_ENCODER_get0_description(const OSSL_ENCODER *encoder)
  442. {
  443. return encoder->base.algodef->algorithm_description;
  444. }
  445. int OSSL_ENCODER_is_a(const OSSL_ENCODER *encoder, const char *name)
  446. {
  447. if (encoder->base.prov != NULL) {
  448. OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov);
  449. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  450. return ossl_namemap_name2num(namemap, name) == encoder->base.id;
  451. }
  452. return 0;
  453. }
  454. struct do_one_data_st {
  455. void (*user_fn)(OSSL_ENCODER *encoder, void *arg);
  456. void *user_arg;
  457. };
  458. static void do_one(ossl_unused int id, void *method, void *arg)
  459. {
  460. struct do_one_data_st *data = arg;
  461. data->user_fn(method, data->user_arg);
  462. }
  463. void OSSL_ENCODER_do_all_provided(OSSL_LIB_CTX *libctx,
  464. void (*user_fn)(OSSL_ENCODER *encoder,
  465. void *arg),
  466. void *user_arg)
  467. {
  468. struct encoder_data_st methdata;
  469. struct do_one_data_st data;
  470. methdata.libctx = libctx;
  471. methdata.tmp_store = NULL;
  472. (void)inner_ossl_encoder_fetch(&methdata, NULL, NULL /* properties */);
  473. data.user_fn = user_fn;
  474. data.user_arg = user_arg;
  475. if (methdata.tmp_store != NULL)
  476. ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
  477. ossl_method_store_do_all(get_encoder_store(libctx), &do_one, &data);
  478. dealloc_tmp_encoder_store(methdata.tmp_store);
  479. }
  480. int OSSL_ENCODER_names_do_all(const OSSL_ENCODER *encoder,
  481. void (*fn)(const char *name, void *data),
  482. void *data)
  483. {
  484. if (encoder == NULL)
  485. return 0;
  486. if (encoder->base.prov != NULL) {
  487. OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov);
  488. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  489. return ossl_namemap_doall_names(namemap, encoder->base.id, fn, data);
  490. }
  491. return 1;
  492. }
  493. const OSSL_PARAM *
  494. OSSL_ENCODER_gettable_params(OSSL_ENCODER *encoder)
  495. {
  496. if (encoder != NULL && encoder->gettable_params != NULL) {
  497. void *provctx = ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder));
  498. return encoder->gettable_params(provctx);
  499. }
  500. return NULL;
  501. }
  502. int OSSL_ENCODER_get_params(OSSL_ENCODER *encoder, OSSL_PARAM params[])
  503. {
  504. if (encoder != NULL && encoder->get_params != NULL)
  505. return encoder->get_params(params);
  506. return 0;
  507. }
  508. const OSSL_PARAM *OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER *encoder)
  509. {
  510. if (encoder != NULL && encoder->settable_ctx_params != NULL) {
  511. void *provctx = ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder));
  512. return encoder->settable_ctx_params(provctx);
  513. }
  514. return NULL;
  515. }
  516. /*
  517. * Encoder context support
  518. */
  519. OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new(void)
  520. {
  521. OSSL_ENCODER_CTX *ctx;
  522. ctx = OPENSSL_zalloc(sizeof(*ctx));
  523. return ctx;
  524. }
  525. int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX *ctx,
  526. const OSSL_PARAM params[])
  527. {
  528. int ok = 1;
  529. size_t i;
  530. size_t l;
  531. if (!ossl_assert(ctx != NULL)) {
  532. ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
  533. return 0;
  534. }
  535. if (ctx->encoder_insts == NULL)
  536. return 1;
  537. l = OSSL_ENCODER_CTX_get_num_encoders(ctx);
  538. for (i = 0; i < l; i++) {
  539. OSSL_ENCODER_INSTANCE *encoder_inst =
  540. sk_OSSL_ENCODER_INSTANCE_value(ctx->encoder_insts, i);
  541. OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
  542. void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst);
  543. if (encoderctx == NULL || encoder->set_ctx_params == NULL)
  544. continue;
  545. if (!encoder->set_ctx_params(encoderctx, params))
  546. ok = 0;
  547. }
  548. return ok;
  549. }
  550. void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX *ctx)
  551. {
  552. if (ctx != NULL) {
  553. sk_OSSL_ENCODER_INSTANCE_pop_free(ctx->encoder_insts,
  554. ossl_encoder_instance_free);
  555. OPENSSL_free(ctx->construct_data);
  556. ossl_pw_clear_passphrase_data(&ctx->pwdata);
  557. OPENSSL_free(ctx);
  558. }
  559. }