/src/openssl/crypto/evp/evp_fetch.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2019-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 <stddef.h> |
11 | | #include <openssl/types.h> |
12 | | #include <openssl/evp.h> |
13 | | #include <openssl/core.h> |
14 | | #include "internal/cryptlib.h" |
15 | | #include "internal/thread_once.h" |
16 | | #include "internal/property.h" |
17 | | #include "internal/core.h" |
18 | | #include "internal/provider.h" |
19 | | #include "internal/namemap.h" |
20 | | #include "crypto/decoder.h" |
21 | | #include "crypto/evp.h" /* evp_local.h needs it */ |
22 | | #include "evp_local.h" |
23 | | |
24 | 60.3k | #define NAME_SEPARATOR ':' |
25 | | |
26 | | /* Data to be passed through ossl_method_construct() */ |
27 | | struct evp_method_data_st { |
28 | | OSSL_LIB_CTX *libctx; |
29 | | int operation_id; /* For get_evp_method_from_store() */ |
30 | | int name_id; /* For get_evp_method_from_store() */ |
31 | | const char *names; /* For get_evp_method_from_store() */ |
32 | | const char *propquery; /* For get_evp_method_from_store() */ |
33 | | |
34 | | OSSL_METHOD_STORE *tmp_store; /* For get_tmp_evp_method_store() */ |
35 | | |
36 | | unsigned int flag_construct_error_occurred : 1; |
37 | | |
38 | | void *(*method_from_algorithm)(int name_id, const OSSL_ALGORITHM *, |
39 | | OSSL_PROVIDER *); |
40 | | int (*refcnt_up_method)(void *method); |
41 | | void (*destruct_method)(void *method); |
42 | | }; |
43 | | |
44 | | /* |
45 | | * Generic routines to fetch / create EVP methods with ossl_method_construct() |
46 | | */ |
47 | | static void *get_tmp_evp_method_store(void *data) |
48 | 0 | { |
49 | 0 | struct evp_method_data_st *methdata = data; |
50 | |
|
51 | 0 | if (methdata->tmp_store == NULL) { |
52 | 0 | methdata->tmp_store = ossl_method_store_new(methdata->libctx); |
53 | 0 | OSSL_TRACE1(QUERY, "Allocating a new tmp_store %p\n", (void *)methdata->tmp_store); |
54 | 0 | } else { |
55 | 0 | OSSL_TRACE1(QUERY, "Using the existing tmp_store %p\n", (void *)methdata->tmp_store); |
56 | 0 | } |
57 | 0 | return methdata->tmp_store; |
58 | 0 | } |
59 | | |
60 | | static void dealloc_tmp_evp_method_store(void *store) |
61 | 848k | { |
62 | 848k | OSSL_TRACE1(QUERY, "Deallocating the tmp_store %p\n", store); |
63 | 848k | if (store != NULL) |
64 | 0 | ossl_method_store_free(store); |
65 | 848k | } |
66 | | |
67 | | static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx) |
68 | 1.22M | { |
69 | 1.22M | return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX); |
70 | 1.22M | } |
71 | | |
72 | | static int reserve_evp_method_store(void *store, void *data) |
73 | 173k | { |
74 | 173k | struct evp_method_data_st *methdata = data; |
75 | | |
76 | 173k | if (store == NULL |
77 | 173k | && (store = get_evp_method_store(methdata->libctx)) == NULL) |
78 | 0 | return 0; |
79 | | |
80 | 173k | return ossl_method_lock_store(store); |
81 | 173k | } |
82 | | |
83 | | static int unreserve_evp_method_store(void *store, void *data) |
84 | 173k | { |
85 | 173k | struct evp_method_data_st *methdata = data; |
86 | | |
87 | 173k | if (store == NULL |
88 | 173k | && (store = get_evp_method_store(methdata->libctx)) == NULL) |
89 | 0 | return 0; |
90 | | |
91 | 173k | return ossl_method_unlock_store(store); |
92 | 173k | } |
93 | | |
94 | | /* |
95 | | * To identify the method in the EVP method store, we mix the name identity |
96 | | * with the operation identity, under the assumption that we don't have more |
97 | | * than 2^23 names or more than 2^8 operation types. |
98 | | * |
99 | | * The resulting identity is a 31-bit integer, composed like this: |
100 | | * |
101 | | * +---------23 bits--------+-8 bits-+ |
102 | | * | name identity | op id | |
103 | | * +------------------------+--------+ |
104 | | * |
105 | | * We limit this composite number to 31 bits, thus leaving the top uint32_t |
106 | | * bit always zero, to avoid negative sign extension when downshifting after |
107 | | * this number happens to be passed to an int (which happens as soon as it's |
108 | | * passed to ossl_method_store_cache_set(), and it's in that form that it |
109 | | * gets passed along to filter_on_operation_id(), defined further down. |
110 | | */ |
111 | 817k | #define METHOD_ID_OPERATION_MASK 0x000000FF |
112 | | #define METHOD_ID_OPERATION_MAX ((1 << 8) - 1) |
113 | 816k | #define METHOD_ID_NAME_MASK 0x7FFFFF00 |
114 | 816k | #define METHOD_ID_NAME_OFFSET 8 |
115 | | #define METHOD_ID_NAME_MAX ((1 << 23) - 1) |
116 | | static uint32_t evp_method_id(int name_id, unsigned int operation_id) |
117 | 816k | { |
118 | 816k | if (!ossl_assert(name_id > 0 && name_id <= METHOD_ID_NAME_MAX) |
119 | 816k | || !ossl_assert(operation_id > 0 |
120 | 816k | && operation_id <= METHOD_ID_OPERATION_MAX)) |
121 | 0 | return 0; |
122 | 816k | return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK) |
123 | 816k | | (operation_id & METHOD_ID_OPERATION_MASK)); |
124 | 816k | } |
125 | | |
126 | | static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov, |
127 | | void *data) |
128 | 86.8k | { |
129 | 86.8k | struct evp_method_data_st *methdata = data; |
130 | 86.8k | void *method = NULL; |
131 | 86.8k | int name_id; |
132 | 86.8k | uint32_t meth_id; |
133 | | |
134 | | /* |
135 | | * get_evp_method_from_store() is only called to try and get the method |
136 | | * that evp_generic_fetch() is asking for, and the operation id as well |
137 | | * as the name or name id are passed via methdata. |
138 | | */ |
139 | 86.8k | if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) { |
140 | 59.6k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx); |
141 | 59.6k | const char *names = methdata->names; |
142 | 59.6k | const char *q = strchr(names, NAME_SEPARATOR); |
143 | 59.6k | size_t l = (q == NULL ? strlen(names) : (size_t)(q - names)); |
144 | | |
145 | 59.6k | if (namemap == 0) |
146 | 0 | return NULL; |
147 | 59.6k | name_id = ossl_namemap_name2num_n(namemap, names, l); |
148 | 59.6k | } |
149 | | |
150 | 86.8k | if (name_id == 0 |
151 | 86.8k | || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0) |
152 | 59.7k | return NULL; |
153 | | |
154 | 27.1k | if (store == NULL |
155 | 27.1k | && (store = get_evp_method_store(methdata->libctx)) == NULL) |
156 | 0 | return NULL; |
157 | | |
158 | 27.1k | if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov, |
159 | 27.1k | &method)) |
160 | 27.1k | return NULL; |
161 | 63 | return method; |
162 | 27.1k | } |
163 | | |
164 | | static int put_evp_method_in_store(void *store, void *method, |
165 | | const OSSL_PROVIDER *prov, |
166 | | const char *names, const char *propdef, |
167 | | void *data) |
168 | 307 | { |
169 | 307 | struct evp_method_data_st *methdata = data; |
170 | 307 | OSSL_NAMEMAP *namemap; |
171 | 307 | int name_id; |
172 | 307 | uint32_t meth_id; |
173 | 307 | size_t l = 0; |
174 | | |
175 | | /* |
176 | | * put_evp_method_in_store() is only called with an EVP method that was |
177 | | * successfully created by construct_method() below, which means that |
178 | | * all the names should already be stored in the namemap with the same |
179 | | * numeric identity, so just use the first to get that identity. |
180 | | */ |
181 | 307 | if (names != NULL) { |
182 | 307 | const char *q = strchr(names, NAME_SEPARATOR); |
183 | | |
184 | 307 | l = (q == NULL ? strlen(names) : (size_t)(q - names)); |
185 | 307 | } |
186 | | |
187 | 307 | if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL |
188 | 307 | || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0 |
189 | 307 | || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0) |
190 | 0 | return 0; |
191 | | |
192 | 307 | OSSL_TRACE1(QUERY, "put_evp_method_in_store: original store: %p\n", store); |
193 | 307 | if (store == NULL |
194 | 307 | && (store = get_evp_method_store(methdata->libctx)) == NULL) |
195 | 0 | return 0; |
196 | | |
197 | 307 | OSSL_TRACE5(QUERY, |
198 | 307 | "put_evp_method_in_store: " |
199 | 307 | "store: %p, names: %s, operation_id %d, method_id: %d, properties: %s\n", |
200 | 307 | store, names, methdata->operation_id, meth_id, propdef ? propdef : "<null>"); |
201 | 307 | return ossl_method_store_add(store, prov, meth_id, propdef, method, |
202 | 307 | methdata->refcnt_up_method, |
203 | 307 | methdata->destruct_method); |
204 | 307 | } |
205 | | |
206 | | /* |
207 | | * The core fetching functionality passes the name of the implementation. |
208 | | * This function is responsible to getting an identity number for it. |
209 | | */ |
210 | | static void *construct_evp_method(const OSSL_ALGORITHM *algodef, |
211 | | OSSL_PROVIDER *prov, void *data) |
212 | 307 | { |
213 | | /* |
214 | | * This function is only called if get_evp_method_from_store() returned |
215 | | * NULL, so it's safe to say that of all the spots to create a new |
216 | | * namemap entry, this is it. Should the name already exist there, we |
217 | | * know that ossl_namemap_add_name() will return its corresponding |
218 | | * number. |
219 | | */ |
220 | 307 | struct evp_method_data_st *methdata = data; |
221 | 307 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
222 | 307 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
223 | 307 | const char *names = algodef->algorithm_names; |
224 | 307 | int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR); |
225 | 307 | void *method; |
226 | | |
227 | 307 | if (name_id == 0) |
228 | 0 | return NULL; |
229 | | |
230 | 307 | method = methdata->method_from_algorithm(name_id, algodef, prov); |
231 | | |
232 | | /* |
233 | | * Flag to indicate that there was actual construction errors. This |
234 | | * helps inner_evp_generic_fetch() determine what error it should |
235 | | * record on inaccessible algorithms. |
236 | | */ |
237 | 307 | if (method == NULL) |
238 | 0 | methdata->flag_construct_error_occurred = 1; |
239 | | |
240 | 307 | return method; |
241 | 307 | } |
242 | | |
243 | | static void destruct_evp_method(void *method, void *data) |
244 | 307 | { |
245 | 307 | struct evp_method_data_st *methdata = data; |
246 | | |
247 | 307 | methdata->destruct_method(method); |
248 | 307 | } |
249 | | |
250 | | static void * |
251 | | inner_evp_generic_fetch(struct evp_method_data_st *methdata, |
252 | | OSSL_PROVIDER *prov, int operation_id, |
253 | | const char *name, ossl_unused const char *properties, |
254 | | void *(*new_method)(int name_id, |
255 | | const OSSL_ALGORITHM *algodef, |
256 | | OSSL_PROVIDER *prov), |
257 | | int (*up_ref_method)(void *), |
258 | | void (*free_method)(void *)) |
259 | 848k | { |
260 | 848k | OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx); |
261 | 848k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx); |
262 | | #ifdef FIPS_MODULE |
263 | | /* |
264 | | * The FIPS provider has its own internal library context where only it |
265 | | * is loaded. Consequently, property queries aren't relevant because |
266 | | * there is only one fetchable algorithm and it is assumed that the |
267 | | * FIPS-ness is handled by the using algorithm. |
268 | | */ |
269 | | const char *const propq = ""; |
270 | | #else |
271 | 848k | const char *const propq = properties != NULL ? properties : ""; |
272 | 848k | #endif /* FIPS_MODULE */ |
273 | 848k | uint32_t meth_id = 0; |
274 | 848k | void *method = NULL; |
275 | 848k | int unsupported, name_id; |
276 | | |
277 | 848k | if (store == NULL || namemap == NULL) { |
278 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT); |
279 | 0 | return NULL; |
280 | 0 | } |
281 | | |
282 | | /* |
283 | | * If there's ever an operation_id == 0 passed, we have an internal |
284 | | * programming error. |
285 | | */ |
286 | 848k | if (!ossl_assert(operation_id > 0)) { |
287 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
288 | 0 | return NULL; |
289 | 0 | } |
290 | | |
291 | | /* If we haven't received a name id yet, try to get one for the name */ |
292 | 848k | name_id = name != NULL ? ossl_namemap_name2num(namemap, name) : 0; |
293 | | |
294 | | /* |
295 | | * If we have a name id, calculate a method id with evp_method_id(). |
296 | | * |
297 | | * evp_method_id returns 0 if we have too many operations (more than |
298 | | * about 2^8) or too many names (more than about 2^24). In that case, |
299 | | * we can't create any new method. |
300 | | * For all intents and purposes, this is an internal error. |
301 | | */ |
302 | 848k | if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0) { |
303 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
304 | 0 | return NULL; |
305 | 0 | } |
306 | | |
307 | | /* |
308 | | * If we haven't found the name yet, chances are that the algorithm to |
309 | | * be fetched is unsupported. |
310 | | */ |
311 | 848k | unsupported = name_id == 0; |
312 | | |
313 | 848k | if (meth_id == 0 |
314 | 848k | || !ossl_method_store_cache_get(store, prov, meth_id, propq, &method)) { |
315 | 86.8k | OSSL_METHOD_CONSTRUCT_METHOD mcm = { |
316 | 86.8k | get_tmp_evp_method_store, |
317 | 86.8k | reserve_evp_method_store, |
318 | 86.8k | unreserve_evp_method_store, |
319 | 86.8k | get_evp_method_from_store, |
320 | 86.8k | put_evp_method_in_store, |
321 | 86.8k | construct_evp_method, |
322 | 86.8k | destruct_evp_method |
323 | 86.8k | }; |
324 | | |
325 | 86.8k | methdata->operation_id = operation_id; |
326 | 86.8k | methdata->name_id = name_id; |
327 | 86.8k | methdata->names = name; |
328 | 86.8k | methdata->propquery = propq; |
329 | 86.8k | methdata->method_from_algorithm = new_method; |
330 | 86.8k | methdata->refcnt_up_method = up_ref_method; |
331 | 86.8k | methdata->destruct_method = free_method; |
332 | 86.8k | methdata->flag_construct_error_occurred = 0; |
333 | 86.8k | if ((method = ossl_method_construct(methdata->libctx, operation_id, |
334 | 86.8k | &prov, 0 /* !force_cache */, |
335 | 86.8k | &mcm, methdata)) != NULL) { |
336 | | /* |
337 | | * If construction did create a method for us, we know that |
338 | | * there is a correct name_id and meth_id, since those have |
339 | | * already been calculated in get_evp_method_from_store() and |
340 | | * put_evp_method_in_store() above. |
341 | | * Note that there is a corner case here, in which, if a user |
342 | | * passes a name of the form name1:name2:..., then the construction |
343 | | * will create a method against all names, but the lookup will fail |
344 | | * as ossl_namemap_name2num treats the name string as a single name |
345 | | * rather than introducing new features where in the EVP_<obj>_fetch |
346 | | * parses the string and queries for each, return an error. |
347 | | */ |
348 | 63 | if (name_id == 0) |
349 | 2 | name_id = ossl_namemap_name2num(namemap, name); |
350 | 63 | if (name_id == 0) { |
351 | 0 | ERR_raise_data(ERR_LIB_EVP, ERR_R_FETCH_FAILED, |
352 | 0 | "Algorithm %s cannot be found", name); |
353 | 0 | free_method(method); |
354 | 0 | method = NULL; |
355 | 63 | } else { |
356 | 63 | meth_id = evp_method_id(name_id, operation_id); |
357 | 63 | if (meth_id != 0) |
358 | 63 | ossl_method_store_cache_set(store, prov, meth_id, propq, |
359 | 63 | method, up_ref_method, free_method); |
360 | 63 | } |
361 | 63 | } |
362 | | |
363 | | /* |
364 | | * If we never were in the constructor, the algorithm to be fetched |
365 | | * is unsupported. |
366 | | */ |
367 | 86.8k | unsupported = !methdata->flag_construct_error_occurred; |
368 | 86.8k | } |
369 | | |
370 | 848k | if ((name_id != 0 || name != NULL) && method == NULL) { |
371 | 86.8k | int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED; |
372 | | |
373 | 86.8k | if (name == NULL) |
374 | 0 | name = ossl_namemap_num2name(namemap, name_id, 0); |
375 | 86.8k | ERR_raise_data(ERR_LIB_EVP, code, |
376 | 86.8k | "%s, Algorithm (%s : %d), Properties (%s)", |
377 | 86.8k | ossl_lib_ctx_get_descriptor(methdata->libctx), |
378 | 86.8k | name == NULL ? "<null>" : name, name_id, |
379 | 86.8k | properties == NULL ? "<null>" : properties); |
380 | 761k | } else { |
381 | 761k | OSSL_TRACE4(QUERY, "%s, Algorithm (%s : %d), Properties (%s)\n", |
382 | 761k | ossl_lib_ctx_get_descriptor(methdata->libctx), |
383 | 761k | name == NULL ? "<null>" : name, name_id, |
384 | 761k | properties == NULL ? "<null>" : properties); |
385 | 761k | } |
386 | | |
387 | 848k | return method; |
388 | 848k | } |
389 | | |
390 | | void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id, |
391 | | const char *name, const char *properties, |
392 | | void *(*new_method)(int name_id, |
393 | | const OSSL_ALGORITHM *algodef, |
394 | | OSSL_PROVIDER *prov), |
395 | | int (*up_ref_method)(void *), |
396 | | void (*free_method)(void *)) |
397 | 834k | { |
398 | 834k | struct evp_method_data_st methdata; |
399 | 834k | void *method; |
400 | | |
401 | 834k | methdata.libctx = libctx; |
402 | 834k | methdata.tmp_store = NULL; |
403 | 834k | method = inner_evp_generic_fetch(&methdata, NULL, operation_id, |
404 | 834k | name, properties, |
405 | 834k | new_method, up_ref_method, free_method); |
406 | 834k | dealloc_tmp_evp_method_store(methdata.tmp_store); |
407 | 834k | return method; |
408 | 834k | } |
409 | | |
410 | | /* |
411 | | * evp_generic_fetch_from_prov() is special, and only returns methods from |
412 | | * the given provider. |
413 | | * This is meant to be used when one method needs to fetch an associated |
414 | | * method. |
415 | | */ |
416 | | void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id, |
417 | | const char *name, const char *properties, |
418 | | void *(*new_method)(int name_id, |
419 | | const OSSL_ALGORITHM *algodef, |
420 | | OSSL_PROVIDER *prov), |
421 | | int (*up_ref_method)(void *), |
422 | | void (*free_method)(void *)) |
423 | 14.6k | { |
424 | 14.6k | struct evp_method_data_st methdata; |
425 | 14.6k | void *method; |
426 | | |
427 | 14.6k | methdata.libctx = ossl_provider_libctx(prov); |
428 | 14.6k | methdata.tmp_store = NULL; |
429 | 14.6k | method = inner_evp_generic_fetch(&methdata, prov, operation_id, |
430 | 14.6k | name, properties, |
431 | 14.6k | new_method, up_ref_method, free_method); |
432 | 14.6k | dealloc_tmp_evp_method_store(methdata.tmp_store); |
433 | 14.6k | return method; |
434 | 14.6k | } |
435 | | |
436 | | int evp_method_store_cache_flush(OSSL_LIB_CTX *libctx) |
437 | 2 | { |
438 | 2 | OSSL_METHOD_STORE *store = get_evp_method_store(libctx); |
439 | | |
440 | 2 | if (store != NULL) |
441 | 2 | return ossl_method_store_cache_flush_all(store); |
442 | 0 | return 1; |
443 | 2 | } |
444 | | |
445 | | int evp_method_store_remove_all_provided(const OSSL_PROVIDER *prov) |
446 | 0 | { |
447 | 0 | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
448 | 0 | OSSL_METHOD_STORE *store = get_evp_method_store(libctx); |
449 | |
|
450 | 0 | if (store != NULL) |
451 | 0 | return ossl_method_store_remove_all_provided(store, prov); |
452 | 0 | return 1; |
453 | 0 | } |
454 | | |
455 | | static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx, |
456 | | OSSL_PROPERTY_LIST *def_prop, |
457 | | int loadconfig, |
458 | | int mirrored) |
459 | 0 | { |
460 | 0 | OSSL_METHOD_STORE *store = get_evp_method_store(libctx); |
461 | 0 | OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig); |
462 | |
|
463 | 0 | if (plp != NULL && store != NULL) { |
464 | 0 | int ret; |
465 | 0 | #ifndef FIPS_MODULE |
466 | 0 | char *propstr = NULL; |
467 | 0 | size_t strsz; |
468 | |
|
469 | 0 | if (mirrored) { |
470 | 0 | if (ossl_global_properties_no_mirrored(libctx)) |
471 | 0 | return 0; |
472 | 0 | } else { |
473 | | /* |
474 | | * These properties have been explicitly set on this libctx, so |
475 | | * don't allow any mirroring from a parent libctx. |
476 | | */ |
477 | 0 | ossl_global_properties_stop_mirroring(libctx); |
478 | 0 | } |
479 | | |
480 | 0 | strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0); |
481 | 0 | if (strsz > 0) |
482 | 0 | propstr = OPENSSL_malloc(strsz); |
483 | 0 | if (propstr == NULL) { |
484 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
485 | 0 | return 0; |
486 | 0 | } |
487 | 0 | if (ossl_property_list_to_string(libctx, def_prop, propstr, |
488 | 0 | strsz) == 0) { |
489 | 0 | OPENSSL_free(propstr); |
490 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
491 | 0 | return 0; |
492 | 0 | } |
493 | 0 | ossl_provider_default_props_update(libctx, propstr); |
494 | 0 | OPENSSL_free(propstr); |
495 | 0 | #endif |
496 | 0 | ossl_property_free(*plp); |
497 | 0 | *plp = def_prop; |
498 | |
|
499 | 0 | ret = ossl_method_store_cache_flush_all(store); |
500 | 0 | #ifndef FIPS_MODULE |
501 | 0 | ossl_decoder_cache_flush(libctx); |
502 | 0 | #endif |
503 | 0 | return ret; |
504 | 0 | } |
505 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
506 | 0 | return 0; |
507 | 0 | } |
508 | | |
509 | | int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq, |
510 | | int loadconfig, int mirrored) |
511 | 0 | { |
512 | 0 | OSSL_PROPERTY_LIST *pl = NULL; |
513 | |
|
514 | 0 | if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) { |
515 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR); |
516 | 0 | return 0; |
517 | 0 | } |
518 | 0 | if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) { |
519 | 0 | ossl_property_free(pl); |
520 | 0 | return 0; |
521 | 0 | } |
522 | 0 | return 1; |
523 | 0 | } |
524 | | |
525 | | int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq) |
526 | 0 | { |
527 | 0 | return evp_set_default_properties_int(libctx, propq, 1, 0); |
528 | 0 | } |
529 | | |
530 | | static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq, |
531 | | int loadconfig) |
532 | 0 | { |
533 | 0 | OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig); |
534 | 0 | OSSL_PROPERTY_LIST *pl1, *pl2; |
535 | |
|
536 | 0 | if (propq == NULL) |
537 | 0 | return 1; |
538 | 0 | if (plp == NULL || *plp == NULL) |
539 | 0 | return evp_set_default_properties_int(libctx, propq, 0, 0); |
540 | 0 | if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) { |
541 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR); |
542 | 0 | return 0; |
543 | 0 | } |
544 | 0 | pl2 = ossl_property_merge(pl1, *plp); |
545 | 0 | ossl_property_free(pl1); |
546 | 0 | if (pl2 == NULL) { |
547 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
548 | 0 | return 0; |
549 | 0 | } |
550 | 0 | if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) { |
551 | 0 | ossl_property_free(pl2); |
552 | 0 | return 0; |
553 | 0 | } |
554 | 0 | return 1; |
555 | 0 | } |
556 | | |
557 | | static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx, |
558 | | const char *prop_name) |
559 | 0 | { |
560 | 0 | OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1); |
561 | |
|
562 | 0 | return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp); |
563 | 0 | } |
564 | | |
565 | | int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx) |
566 | 0 | { |
567 | 0 | return evp_default_property_is_enabled(libctx, "fips"); |
568 | 0 | } |
569 | | |
570 | | int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable, |
571 | | int loadconfig) |
572 | 0 | { |
573 | 0 | const char *query = (enable != 0) ? "fips=yes" : "-fips"; |
574 | |
|
575 | 0 | return evp_default_properties_merge(libctx, query, loadconfig); |
576 | 0 | } |
577 | | |
578 | | int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable) |
579 | 0 | { |
580 | 0 | return evp_default_properties_enable_fips_int(libctx, enable, 1); |
581 | 0 | } |
582 | | |
583 | | char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig) |
584 | 0 | { |
585 | 0 | OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig); |
586 | 0 | char *propstr = NULL; |
587 | 0 | size_t sz; |
588 | |
|
589 | 0 | if (plp == NULL) |
590 | 0 | return OPENSSL_strdup(""); |
591 | | |
592 | 0 | sz = ossl_property_list_to_string(libctx, *plp, NULL, 0); |
593 | 0 | if (sz == 0) { |
594 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
595 | 0 | return NULL; |
596 | 0 | } |
597 | | |
598 | 0 | propstr = OPENSSL_malloc(sz); |
599 | 0 | if (propstr == NULL) |
600 | 0 | return NULL; |
601 | 0 | if (ossl_property_list_to_string(libctx, *plp, propstr, sz) == 0) { |
602 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
603 | 0 | OPENSSL_free(propstr); |
604 | 0 | return NULL; |
605 | 0 | } |
606 | 0 | return propstr; |
607 | 0 | } |
608 | | |
609 | | char *EVP_get1_default_properties(OSSL_LIB_CTX *libctx) |
610 | 0 | { |
611 | 0 | return evp_get_global_properties_str(libctx, ossl_lib_ctx_is_global_default(libctx)); |
612 | 0 | } |
613 | | |
614 | | struct filter_data_st { |
615 | | int operation_id; |
616 | | void (*user_fn)(void *method, void *arg); |
617 | | void *user_arg; |
618 | | }; |
619 | | |
620 | | static void filter_on_operation_id(int id, void *method, void *arg) |
621 | 1.07k | { |
622 | 1.07k | struct filter_data_st *data = arg; |
623 | | |
624 | 1.07k | if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id) |
625 | 160 | data->user_fn(method, data->user_arg); |
626 | 1.07k | } |
627 | | |
628 | | void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id, |
629 | | void (*user_fn)(void *method, void *arg), |
630 | | void *user_arg, |
631 | | void *(*new_method)(int name_id, |
632 | | const OSSL_ALGORITHM *algodef, |
633 | | OSSL_PROVIDER *prov), |
634 | | int (*up_ref_method)(void *), |
635 | | void (*free_method)(void *)) |
636 | 4 | { |
637 | 4 | struct evp_method_data_st methdata; |
638 | 4 | struct filter_data_st data; |
639 | | |
640 | 4 | methdata.libctx = libctx; |
641 | 4 | methdata.tmp_store = NULL; |
642 | 4 | (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, NULL, NULL, |
643 | 4 | new_method, up_ref_method, free_method); |
644 | | |
645 | 4 | data.operation_id = operation_id; |
646 | 4 | data.user_fn = user_fn; |
647 | 4 | data.user_arg = user_arg; |
648 | 4 | if (methdata.tmp_store != NULL) |
649 | 0 | ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id, |
650 | 0 | &data); |
651 | 4 | ossl_method_store_do_all(get_evp_method_store(libctx), |
652 | 4 | &filter_on_operation_id, &data); |
653 | 4 | dealloc_tmp_evp_method_store(methdata.tmp_store); |
654 | 4 | } |
655 | | |
656 | | int evp_is_a(OSSL_PROVIDER *prov, int number, |
657 | | const char *legacy_name, const char *name) |
658 | 253k | { |
659 | | /* |
660 | | * For a |prov| that is NULL, the library context will be NULL |
661 | | */ |
662 | 253k | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
663 | 253k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
664 | | |
665 | 253k | if (prov == NULL) |
666 | 16.2k | number = ossl_namemap_name2num(namemap, legacy_name); |
667 | 253k | return ossl_namemap_name2num(namemap, name) == number; |
668 | 253k | } |
669 | | |
670 | | int evp_names_do_all(OSSL_PROVIDER *prov, int number, |
671 | | void (*fn)(const char *name, void *data), |
672 | | void *data) |
673 | 31.3k | { |
674 | 31.3k | OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov); |
675 | 31.3k | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
676 | | |
677 | 31.3k | return ossl_namemap_doall_names(namemap, number, fn, data); |
678 | 31.3k | } |