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