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