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