encoder_meth.c 20 KB

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