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