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