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