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