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