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