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