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