/src/openssl34/crypto/encode_decode/decoder_meth.c
Line | Count | Source |
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 | 7.17k | #define NAME_SEPARATOR ':' |
26 | | |
27 | | /* Simple method structure constructor and destructor */ |
28 | | static OSSL_DECODER *ossl_decoder_new(void) |
29 | 3.34k | { |
30 | 3.34k | OSSL_DECODER *decoder = NULL; |
31 | | |
32 | 3.34k | if ((decoder = OPENSSL_zalloc(sizeof(*decoder))) == NULL) |
33 | 0 | return NULL; |
34 | 3.34k | if (!CRYPTO_NEW_REF(&decoder->base.refcnt, 1)) { |
35 | 0 | OSSL_DECODER_free(decoder); |
36 | 0 | return NULL; |
37 | 0 | } |
38 | | |
39 | 3.34k | return decoder; |
40 | 3.34k | } |
41 | | |
42 | | int OSSL_DECODER_up_ref(OSSL_DECODER *decoder) |
43 | 25.4M | { |
44 | 25.4M | int ref = 0; |
45 | | |
46 | 25.4M | CRYPTO_UP_REF(&decoder->base.refcnt, &ref); |
47 | 25.4M | return 1; |
48 | 25.4M | } |
49 | | |
50 | | void OSSL_DECODER_free(OSSL_DECODER *decoder) |
51 | 25.4M | { |
52 | 25.4M | int ref = 0; |
53 | | |
54 | 25.4M | if (decoder == NULL) |
55 | 0 | return; |
56 | | |
57 | 25.4M | CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref); |
58 | 25.4M | if (ref > 0) |
59 | 25.4M | return; |
60 | 2.75k | OPENSSL_free(decoder->base.name); |
61 | 2.75k | ossl_property_free(decoder->base.parsed_propdef); |
62 | 2.75k | ossl_provider_free(decoder->base.prov); |
63 | 2.75k | CRYPTO_FREE_REF(&decoder->base.refcnt); |
64 | 2.75k | OPENSSL_free(decoder); |
65 | 2.75k | } |
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 | 523k | { |
96 | 523k | if (store != NULL) |
97 | 0 | ossl_method_store_free(store); |
98 | 523k | } |
99 | | |
100 | | /* Get the permanent decoder store */ |
101 | | static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx) |
102 | 2.89M | { |
103 | 2.89M | return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX); |
104 | 2.89M | } |
105 | | |
106 | | static int reserve_decoder_store(void *store, void *data) |
107 | 920k | { |
108 | 920k | struct decoder_data_st *methdata = data; |
109 | | |
110 | 920k | if (store == NULL |
111 | 920k | && (store = get_decoder_store(methdata->libctx)) == NULL) |
112 | 0 | return 0; |
113 | | |
114 | 920k | return ossl_method_lock_store(store); |
115 | 920k | } |
116 | | |
117 | | static int unreserve_decoder_store(void *store, void *data) |
118 | 920k | { |
119 | 920k | struct decoder_data_st *methdata = data; |
120 | | |
121 | 920k | if (store == NULL |
122 | 920k | && (store = get_decoder_store(methdata->libctx)) == NULL) |
123 | 0 | return 0; |
124 | | |
125 | 920k | return ossl_method_unlock_store(store); |
126 | 920k | } |
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 | 523k | { |
132 | 523k | struct decoder_data_st *methdata = data; |
133 | 523k | void *method = NULL; |
134 | 523k | 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 | 523k | 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 | 523k | if (id == 0) |
153 | 523k | 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 | 3.58k | { |
169 | 3.58k | struct decoder_data_st *methdata = data; |
170 | 3.58k | OSSL_NAMEMAP *namemap; |
171 | 3.58k | int id; |
172 | 3.58k | 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 | 3.58k | if (names != NULL) { |
181 | 3.58k | const char *q = strchr(names, NAME_SEPARATOR); |
182 | | |
183 | 3.58k | l = (q == NULL ? strlen(names) : (size_t)(q - names)); |
184 | 3.58k | } |
185 | | |
186 | 3.58k | if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL |
187 | 3.58k | || (id = ossl_namemap_name2num_n(namemap, names, l)) == 0) |
188 | 0 | return 0; |
189 | | |
190 | 3.58k | if (store == NULL && (store = get_decoder_store(methdata->libctx)) == NULL) |
191 | 0 | return 0; |
192 | | |
193 | 3.58k | return ossl_method_store_add(store, prov, id, propdef, method, |
194 | 3.58k | (int (*)(void *))OSSL_DECODER_up_ref, |
195 | 3.58k | (void (*)(void *))OSSL_DECODER_free); |
196 | 3.58k | } |
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 | 3.58k | { |
202 | 3.58k | OSSL_DECODER *decoder = NULL; |
203 | 3.58k | const OSSL_DISPATCH *fns = algodef->implementation; |
204 | 3.58k | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
205 | | |
206 | 3.58k | if ((decoder = ossl_decoder_new()) == NULL) |
207 | 0 | return NULL; |
208 | 3.58k | decoder->base.id = id; |
209 | 3.58k | if ((decoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) { |
210 | 0 | OSSL_DECODER_free(decoder); |
211 | 0 | return NULL; |
212 | 0 | } |
213 | 3.58k | decoder->base.algodef = algodef; |
214 | 3.58k | if ((decoder->base.parsed_propdef |
215 | 3.58k | = ossl_parse_property(libctx, algodef->property_definition)) |
216 | 3.58k | == NULL) { |
217 | 0 | OSSL_DECODER_free(decoder); |
218 | 0 | return NULL; |
219 | 0 | } |
220 | | |
221 | 27.6k | for (; fns->function_id != 0; fns++) { |
222 | 24.0k | switch (fns->function_id) { |
223 | 3.58k | case OSSL_FUNC_DECODER_NEWCTX: |
224 | 3.58k | if (decoder->newctx == NULL) |
225 | 3.58k | decoder->newctx = OSSL_FUNC_decoder_newctx(fns); |
226 | 3.58k | break; |
227 | 3.58k | case OSSL_FUNC_DECODER_FREECTX: |
228 | 3.58k | if (decoder->freectx == NULL) |
229 | 3.58k | decoder->freectx = OSSL_FUNC_decoder_freectx(fns); |
230 | 3.58k | break; |
231 | 0 | case OSSL_FUNC_DECODER_GET_PARAMS: |
232 | 0 | if (decoder->get_params == NULL) |
233 | 0 | decoder->get_params = 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 = OSSL_FUNC_decoder_gettable_params(fns); |
238 | 0 | break; |
239 | 3.21k | case OSSL_FUNC_DECODER_SET_CTX_PARAMS: |
240 | 3.21k | if (decoder->set_ctx_params == NULL) |
241 | 3.21k | decoder->set_ctx_params = OSSL_FUNC_decoder_set_ctx_params(fns); |
242 | 3.21k | break; |
243 | 3.21k | case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS: |
244 | 3.21k | if (decoder->settable_ctx_params == NULL) |
245 | 3.21k | decoder->settable_ctx_params = OSSL_FUNC_decoder_settable_ctx_params(fns); |
246 | 3.21k | break; |
247 | 3.40k | case OSSL_FUNC_DECODER_DOES_SELECTION: |
248 | 3.40k | if (decoder->does_selection == NULL) |
249 | 3.40k | decoder->does_selection = OSSL_FUNC_decoder_does_selection(fns); |
250 | 3.40k | break; |
251 | 3.58k | case OSSL_FUNC_DECODER_DECODE: |
252 | 3.58k | if (decoder->decode == NULL) |
253 | 3.58k | decoder->decode = OSSL_FUNC_decoder_decode(fns); |
254 | 3.58k | break; |
255 | 3.40k | case OSSL_FUNC_DECODER_EXPORT_OBJECT: |
256 | 3.40k | if (decoder->export_object == NULL) |
257 | 3.40k | decoder->export_object = OSSL_FUNC_decoder_export_object(fns); |
258 | 3.40k | break; |
259 | 24.0k | } |
260 | 24.0k | } |
261 | | /* |
262 | | * Try to check that the method is sensible. |
263 | | * If you have a constructor, you must have a destructor and vice versa. |
264 | | * You must have at least one of the encoding driver functions. |
265 | | */ |
266 | 3.58k | if (!((decoder->newctx == NULL && decoder->freectx == NULL) |
267 | 3.58k | || (decoder->newctx != NULL && decoder->freectx != NULL)) |
268 | 3.58k | || decoder->decode == NULL) { |
269 | 0 | OSSL_DECODER_free(decoder); |
270 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS); |
271 | 0 | return NULL; |
272 | 0 | } |
273 | | |
274 | 3.58k | if (prov != NULL && !ossl_provider_up_ref(prov)) { |
275 | 0 | OSSL_DECODER_free(decoder); |
276 | 0 | return NULL; |
277 | 0 | } |
278 | | |
279 | 3.58k | decoder->base.prov = prov; |
280 | 3.58k | return decoder; |
281 | 3.58k | } |
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, |
286 | | * then call ossl_decoder_from_algorithm() with that identity number. |
287 | | */ |
288 | | static void *construct_decoder(const OSSL_ALGORITHM *algodef, |
289 | | OSSL_PROVIDER *prov, void *data) |
290 | 3.58k | { |
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 | | */ |
297 | 3.58k | struct decoder_data_st *methdata = data; |
298 | 3.58k | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
299 | 3.58k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
300 | 3.58k | const char *names = algodef->algorithm_names; |
301 | 3.58k | int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR); |
302 | 3.58k | void *method = NULL; |
303 | | |
304 | 3.58k | if (id != 0) |
305 | 3.58k | method = ossl_decoder_from_algorithm(id, algodef, prov); |
306 | | |
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 | 3.58k | if (method == NULL) |
313 | 0 | methdata->flag_construct_error_occurred = 1; |
314 | | |
315 | 3.58k | return method; |
316 | 3.58k | } |
317 | | |
318 | | /* Intermediary function to avoid ugly casts, used below */ |
319 | | static void destruct_decoder(void *method, void *data) |
320 | 3.58k | { |
321 | 3.58k | OSSL_DECODER_free(method); |
322 | 3.58k | } |
323 | | |
324 | | static int up_ref_decoder(void *method) |
325 | 0 | { |
326 | 0 | return OSSL_DECODER_up_ref(method); |
327 | 0 | } |
328 | | |
329 | | static void free_decoder(void *method) |
330 | 0 | { |
331 | 0 | OSSL_DECODER_free(method); |
332 | 0 | } |
333 | | |
334 | | /* Fetching support. Can fetch by numeric identity or by name */ |
335 | | static OSSL_DECODER * |
336 | | inner_ossl_decoder_fetch(struct decoder_data_st *methdata, |
337 | | const char *name, const char *properties) |
338 | 17.4k | { |
339 | 17.4k | OSSL_METHOD_STORE *store = get_decoder_store(methdata->libctx); |
340 | 17.4k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx); |
341 | 17.4k | const char *const propq = properties != NULL ? properties : ""; |
342 | 17.4k | void *method = NULL; |
343 | 17.4k | int unsupported, id; |
344 | | |
345 | 17.4k | if (store == NULL || namemap == NULL) { |
346 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT); |
347 | 0 | return NULL; |
348 | 0 | } |
349 | | |
350 | 17.4k | id = name != NULL ? ossl_namemap_name2num(namemap, name) : 0; |
351 | | |
352 | | /* |
353 | | * If we haven't found the name yet, chances are that the algorithm to |
354 | | * be fetched is unsupported. |
355 | | */ |
356 | 17.4k | unsupported = id == 0; |
357 | | |
358 | 17.4k | if (id == 0 |
359 | 17.4k | || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) { |
360 | 17.4k | OSSL_METHOD_CONSTRUCT_METHOD mcm = { |
361 | 17.4k | get_tmp_decoder_store, |
362 | 17.4k | reserve_decoder_store, |
363 | 17.4k | unreserve_decoder_store, |
364 | 17.4k | get_decoder_from_store, |
365 | 17.4k | put_decoder_in_store, |
366 | 17.4k | construct_decoder, |
367 | 17.4k | destruct_decoder |
368 | 17.4k | }; |
369 | 17.4k | OSSL_PROVIDER *prov = NULL; |
370 | | |
371 | 17.4k | methdata->id = id; |
372 | 17.4k | methdata->names = name; |
373 | 17.4k | methdata->propquery = propq; |
374 | 17.4k | methdata->flag_construct_error_occurred = 0; |
375 | 17.4k | if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_DECODER, |
376 | 17.4k | &prov, 0 /* !force_cache */, |
377 | 17.4k | &mcm, methdata)) |
378 | 17.4k | != NULL) { |
379 | | /* |
380 | | * If construction did create a method for us, we know that |
381 | | * there is a correct name_id and meth_id, since those have |
382 | | * already been calculated in get_decoder_from_store() and |
383 | | * put_decoder_in_store() above. |
384 | | */ |
385 | 0 | if (id == 0 && name != NULL) |
386 | 0 | id = ossl_namemap_name2num(namemap, name); |
387 | 0 | if (id != 0) |
388 | 0 | ossl_method_store_cache_set(store, prov, id, propq, method, |
389 | 0 | up_ref_decoder, free_decoder); |
390 | 0 | } |
391 | | |
392 | | /* |
393 | | * If we never were in the constructor, the algorithm to be fetched |
394 | | * is unsupported. |
395 | | */ |
396 | 17.4k | unsupported = !methdata->flag_construct_error_occurred; |
397 | 17.4k | } |
398 | | |
399 | 17.4k | if ((id != 0 || name != NULL) && method == NULL) { |
400 | 0 | int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED; |
401 | |
|
402 | 0 | if (name == NULL) |
403 | 0 | name = ossl_namemap_num2name(namemap, id, 0); |
404 | 0 | ERR_raise_data(ERR_LIB_OSSL_DECODER, code, |
405 | 0 | "%s, Name (%s : %d), Properties (%s)", |
406 | 0 | ossl_lib_ctx_get_descriptor(methdata->libctx), |
407 | 0 | name == NULL ? "<null>" : name, id, |
408 | 0 | properties == NULL ? "<null>" : properties); |
409 | 0 | } |
410 | | |
411 | 17.4k | return method; |
412 | 17.4k | } |
413 | | |
414 | | OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name, |
415 | | const char *properties) |
416 | 0 | { |
417 | 0 | struct decoder_data_st methdata; |
418 | 0 | void *method; |
419 | |
|
420 | 0 | methdata.libctx = libctx; |
421 | 0 | methdata.tmp_store = NULL; |
422 | 0 | method = inner_ossl_decoder_fetch(&methdata, name, properties); |
423 | 0 | dealloc_tmp_decoder_store(methdata.tmp_store); |
424 | 0 | return method; |
425 | 0 | } |
426 | | |
427 | | int ossl_decoder_store_cache_flush(OSSL_LIB_CTX *libctx) |
428 | 136 | { |
429 | 136 | OSSL_METHOD_STORE *store = get_decoder_store(libctx); |
430 | | |
431 | 136 | if (store != NULL) |
432 | 136 | return ossl_method_store_cache_flush_all(store); |
433 | 0 | return 1; |
434 | 136 | } |
435 | | |
436 | | int ossl_decoder_store_remove_all_provided(const OSSL_PROVIDER *prov) |
437 | 0 | { |
438 | 0 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
439 | 0 | OSSL_METHOD_STORE *store = get_decoder_store(libctx); |
440 | |
|
441 | 0 | if (store != NULL) |
442 | 0 | return ossl_method_store_remove_all_provided(store, prov); |
443 | 0 | return 1; |
444 | 0 | } |
445 | | |
446 | | /* |
447 | | * Library of basic method functions |
448 | | */ |
449 | | |
450 | | const OSSL_PROVIDER *OSSL_DECODER_get0_provider(const OSSL_DECODER *decoder) |
451 | 204M | { |
452 | 204M | if (!ossl_assert(decoder != NULL)) { |
453 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
454 | 0 | return 0; |
455 | 0 | } |
456 | | |
457 | 204M | return decoder->base.prov; |
458 | 204M | } |
459 | | |
460 | | const char *OSSL_DECODER_get0_properties(const OSSL_DECODER *decoder) |
461 | 0 | { |
462 | 0 | if (!ossl_assert(decoder != NULL)) { |
463 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
464 | 0 | return 0; |
465 | 0 | } |
466 | | |
467 | 0 | return decoder->base.algodef->property_definition; |
468 | 0 | } |
469 | | |
470 | | const OSSL_PROPERTY_LIST * |
471 | | ossl_decoder_parsed_properties(const OSSL_DECODER *decoder) |
472 | 3.42M | { |
473 | 3.42M | if (!ossl_assert(decoder != NULL)) { |
474 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
475 | 0 | return 0; |
476 | 0 | } |
477 | | |
478 | 3.42M | return decoder->base.parsed_propdef; |
479 | 3.42M | } |
480 | | |
481 | | int ossl_decoder_get_number(const OSSL_DECODER *decoder) |
482 | 5.18M | { |
483 | 5.18M | if (!ossl_assert(decoder != NULL)) { |
484 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
485 | 0 | return 0; |
486 | 0 | } |
487 | | |
488 | 5.18M | return decoder->base.id; |
489 | 5.18M | } |
490 | | |
491 | | const char *OSSL_DECODER_get0_name(const OSSL_DECODER *decoder) |
492 | 0 | { |
493 | 0 | return decoder->base.name; |
494 | 0 | } |
495 | | |
496 | | const char *OSSL_DECODER_get0_description(const OSSL_DECODER *decoder) |
497 | 0 | { |
498 | 0 | return decoder->base.algodef->algorithm_description; |
499 | 0 | } |
500 | | |
501 | | int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name) |
502 | 231M | { |
503 | 231M | if (decoder->base.prov != NULL) { |
504 | 231M | OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov); |
505 | 231M | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
506 | | |
507 | 231M | return ossl_namemap_name2num(namemap, name) == decoder->base.id; |
508 | 231M | } |
509 | 0 | return 0; |
510 | 231M | } |
511 | | |
512 | | static int resolve_name(OSSL_DECODER *decoder, const char *name) |
513 | 4.78M | { |
514 | 4.78M | OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov); |
515 | 4.78M | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
516 | | |
517 | 4.78M | return ossl_namemap_name2num(namemap, name); |
518 | 4.78M | } |
519 | | |
520 | | int ossl_decoder_fast_is_a(OSSL_DECODER *decoder, const char *name, int *id_cache) |
521 | 5.48M | { |
522 | 5.48M | int id = *id_cache; |
523 | | |
524 | 5.48M | if (id <= 0) |
525 | 4.78M | *id_cache = id = resolve_name(decoder, name); |
526 | | |
527 | 5.48M | return id > 0 && ossl_decoder_get_number(decoder) == id; |
528 | 5.48M | } |
529 | | |
530 | | struct do_one_data_st { |
531 | | void (*user_fn)(OSSL_DECODER *decoder, void *arg); |
532 | | void *user_arg; |
533 | | }; |
534 | | |
535 | | static void do_one(ossl_unused int id, void *method, void *arg) |
536 | 21.3M | { |
537 | 21.3M | struct do_one_data_st *data = arg; |
538 | | |
539 | 21.3M | data->user_fn(method, data->user_arg); |
540 | 21.3M | } |
541 | | |
542 | | void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx, |
543 | | void (*user_fn)(OSSL_DECODER *decoder, |
544 | | void *arg), |
545 | | void *user_arg) |
546 | 523k | { |
547 | 523k | struct decoder_data_st methdata; |
548 | 523k | struct do_one_data_st data; |
549 | | |
550 | 523k | methdata.libctx = libctx; |
551 | 523k | methdata.tmp_store = NULL; |
552 | 523k | (void)inner_ossl_decoder_fetch(&methdata, NULL, NULL /* properties */); |
553 | | |
554 | 523k | data.user_fn = user_fn; |
555 | 523k | data.user_arg = user_arg; |
556 | 523k | if (methdata.tmp_store != NULL) |
557 | 0 | ossl_method_store_do_all(methdata.tmp_store, &do_one, &data); |
558 | 523k | ossl_method_store_do_all(get_decoder_store(libctx), &do_one, &data); |
559 | 523k | dealloc_tmp_decoder_store(methdata.tmp_store); |
560 | 523k | } |
561 | | |
562 | | int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder, |
563 | | void (*fn)(const char *name, void *data), |
564 | | void *data) |
565 | 0 | { |
566 | 0 | if (decoder == NULL) |
567 | 0 | return 0; |
568 | | |
569 | 0 | if (decoder->base.prov != NULL) { |
570 | 0 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov); |
571 | 0 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
572 | |
|
573 | 0 | return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data); |
574 | 0 | } |
575 | | |
576 | 0 | return 1; |
577 | 0 | } |
578 | | |
579 | | const OSSL_PARAM * |
580 | | OSSL_DECODER_gettable_params(OSSL_DECODER *decoder) |
581 | 0 | { |
582 | 0 | if (decoder != NULL && decoder->gettable_params != NULL) { |
583 | 0 | void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder)); |
584 | |
|
585 | 0 | return decoder->gettable_params(provctx); |
586 | 0 | } |
587 | 0 | return NULL; |
588 | 0 | } |
589 | | |
590 | | int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[]) |
591 | 0 | { |
592 | 0 | if (decoder != NULL && decoder->get_params != NULL) |
593 | 0 | return decoder->get_params(params); |
594 | 0 | return 0; |
595 | 0 | } |
596 | | |
597 | | const OSSL_PARAM * |
598 | | OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder) |
599 | 0 | { |
600 | 0 | if (decoder != NULL && decoder->settable_ctx_params != NULL) { |
601 | 0 | void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder)); |
602 | |
|
603 | 0 | return decoder->settable_ctx_params(provctx); |
604 | 0 | } |
605 | 0 | return NULL; |
606 | 0 | } |
607 | | |
608 | | /* |
609 | | * Decoder context support |
610 | | */ |
611 | | |
612 | | /* |
613 | | * |encoder| value NULL is valid, and signifies that there is no decoder. |
614 | | * This is useful to provide fallback mechanisms. |
615 | | * Functions that want to verify if there is a decoder can do so with |
616 | | * OSSL_DECODER_CTX_get_decoder() |
617 | | */ |
618 | | OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void) |
619 | 1.09M | { |
620 | 1.09M | OSSL_DECODER_CTX *ctx; |
621 | | |
622 | 1.09M | ctx = OPENSSL_zalloc(sizeof(*ctx)); |
623 | 1.09M | return ctx; |
624 | 1.09M | } |
625 | | |
626 | | int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx, |
627 | | const OSSL_PARAM params[]) |
628 | 0 | { |
629 | 0 | int ok = 1; |
630 | 0 | size_t i; |
631 | 0 | size_t l; |
632 | |
|
633 | 0 | if (!ossl_assert(ctx != NULL)) { |
634 | 0 | ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER); |
635 | 0 | return 0; |
636 | 0 | } |
637 | | |
638 | 0 | if (ctx->decoder_insts == NULL) |
639 | 0 | return 1; |
640 | | |
641 | 0 | l = OSSL_DECODER_CTX_get_num_decoders(ctx); |
642 | 0 | for (i = 0; i < l; i++) { |
643 | 0 | OSSL_DECODER_INSTANCE *decoder_inst = sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i); |
644 | 0 | OSSL_DECODER *decoder = OSSL_DECODER_INSTANCE_get_decoder(decoder_inst); |
645 | 0 | OSSL_DECODER *decoderctx = OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst); |
646 | |
|
647 | 0 | if (decoderctx == NULL || decoder->set_ctx_params == NULL) |
648 | 0 | continue; |
649 | 0 | if (!decoder->set_ctx_params(decoderctx, params)) |
650 | 0 | ok = 0; |
651 | 0 | } |
652 | 0 | return ok; |
653 | 0 | } |
654 | | |
655 | | void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx) |
656 | 2.31M | { |
657 | 2.31M | if (ctx != NULL) { |
658 | 1.35M | if (ctx->cleanup != NULL) |
659 | 1.24M | ctx->cleanup(ctx->construct_data); |
660 | 1.35M | sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts, |
661 | 1.35M | ossl_decoder_instance_free); |
662 | 1.35M | ossl_pw_clear_passphrase_data(&ctx->pwdata); |
663 | 1.35M | OPENSSL_free(ctx); |
664 | 1.35M | } |
665 | 2.31M | } |