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