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