]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/evp_fetch.c
Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[thirdparty/openssl.git] / crypto / evp / evp_fetch.c
1 /*
2 * Copyright 2019-2020 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 #define NAME_SEPARATOR ':'
25
26 static void evp_method_store_free(void *vstore)
27 {
28 ossl_method_store_free(vstore);
29 }
30
31 static void *evp_method_store_new(OSSL_LIB_CTX *ctx)
32 {
33 return ossl_method_store_new(ctx);
34 }
35
36
37 static const OSSL_LIB_CTX_METHOD evp_method_store_method = {
38 evp_method_store_new,
39 evp_method_store_free,
40 };
41
42 /* Data to be passed through ossl_method_construct() */
43 struct evp_method_data_st {
44 OSSL_LIB_CTX *libctx;
45 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
46 int operation_id; /* For get_evp_method_from_store() */
47 int name_id; /* For get_evp_method_from_store() */
48 const char *names; /* For get_evp_method_from_store() */
49 const char *propquery; /* For get_evp_method_from_store() */
50
51 unsigned int flag_construct_error_occured : 1;
52
53 void *(*method_from_dispatch)(int name_id, const OSSL_DISPATCH *,
54 OSSL_PROVIDER *);
55 int (*refcnt_up_method)(void *method);
56 void (*destruct_method)(void *method);
57 };
58
59 /*
60 * Generic routines to fetch / create EVP methods with ossl_method_construct()
61 */
62 static void *alloc_tmp_evp_method_store(OSSL_LIB_CTX *ctx)
63 {
64 return ossl_method_store_new(ctx);
65 }
66
67 static void dealloc_tmp_evp_method_store(void *store)
68 {
69 if (store != NULL)
70 ossl_method_store_free(store);
71 }
72
73 static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
74 {
75 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX,
76 &evp_method_store_method);
77 }
78
79 /*
80 * To identify the method in the EVP method store, we mix the name identity
81 * with the operation identity, under the assumption that we don't have more
82 * than 2^24 names or more than 2^8 operation types.
83 *
84 * The resulting identity is a 32-bit integer, composed like this:
85 *
86 * +---------24 bits--------+-8 bits-+
87 * | name identity | op id |
88 * +------------------------+--------+
89 */
90 static uint32_t evp_method_id(int name_id, unsigned int operation_id)
91 {
92 if (!ossl_assert(name_id > 0 && name_id < (1 << 24))
93 || !ossl_assert(operation_id > 0 && operation_id < (1 << 8)))
94 return 0;
95 return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
96 }
97
98 static void *get_evp_method_from_store(OSSL_LIB_CTX *libctx, void *store,
99 void *data)
100 {
101 struct evp_method_data_st *methdata = data;
102 void *method = NULL;
103 int name_id;
104 uint32_t meth_id;
105
106 /*
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.
110 */
111 if ((name_id = methdata->name_id) == 0) {
112 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
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));
116
117 if (namemap == 0)
118 return NULL;
119 name_id = ossl_namemap_name2num_n(namemap, names, l);
120 }
121
122 if (name_id == 0
123 || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
124 return NULL;
125
126 if (store == NULL
127 && (store = get_evp_method_store(libctx)) == NULL)
128 return NULL;
129
130 if (!ossl_method_store_fetch(store, meth_id, methdata->propquery,
131 &method))
132 return NULL;
133 return method;
134 }
135
136 static int put_evp_method_in_store(OSSL_LIB_CTX *libctx, void *store,
137 void *method, const OSSL_PROVIDER *prov,
138 int operation_id, const char *names,
139 const char *propdef, void *data)
140 {
141 struct evp_method_data_st *methdata = data;
142 OSSL_NAMEMAP *namemap;
143 int name_id;
144 uint32_t meth_id;
145 size_t l = 0;
146
147 /*
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.
152 */
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
159 if ((namemap = ossl_namemap_stored(libctx)) == NULL
160 || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
161 || (meth_id = evp_method_id(name_id, operation_id)) == 0)
162 return 0;
163
164 if (store == NULL
165 && (store = get_evp_method_store(libctx)) == NULL)
166 return 0;
167
168 return ossl_method_store_add(store, prov, meth_id, propdef, method,
169 methdata->refcnt_up_method,
170 methdata->destruct_method);
171 }
172
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 */
177 static void *construct_evp_method(const OSSL_ALGORITHM *algodef,
178 OSSL_PROVIDER *prov, void *data)
179 {
180 /*
181 * This function is only called if get_evp_method_from_store() returned
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
184 * know that ossl_namemap_add_name() will return its corresponding
185 * number.
186 */
187 struct evp_method_data_st *methdata = data;
188 OSSL_LIB_CTX *libctx = ossl_provider_library_context(prov);
189 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
190 const char *names = algodef->algorithm_names;
191 int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
192 void *method;
193
194 if (name_id == 0)
195 return NULL;
196
197 method = methdata->method_from_dispatch(name_id, algodef->implementation,
198 prov);
199
200 /*
201 * Flag to indicate that there was actual construction errors. This
202 * helps inner_evp_generic_fetch() determine what error it should
203 * record on inaccessible algorithms.
204 */
205 if (method == NULL)
206 methdata->flag_construct_error_occured = 1;
207
208 return method;
209 }
210
211 static void destruct_evp_method(void *method, void *data)
212 {
213 struct evp_method_data_st *methdata = data;
214
215 methdata->destruct_method(method);
216 }
217
218 static const char *libctx_descriptor(OSSL_LIB_CTX *libctx)
219 {
220 #ifdef FIPS_MODULE
221 return "FIPS internal library context";
222 #else
223 if (ossl_lib_ctx_is_global_default(libctx))
224 return "Global default library context";
225 if (ossl_lib_ctx_is_default(libctx))
226 return "Thread-local default library context";
227 return "Non-default library context";
228 #endif
229 }
230
231 static void *
232 inner_evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
233 int name_id, const char *name,
234 const char *properties,
235 void *(*new_method)(int name_id,
236 const OSSL_DISPATCH *fns,
237 OSSL_PROVIDER *prov),
238 int (*up_ref_method)(void *),
239 void (*free_method)(void *))
240 {
241 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
242 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
243 uint32_t meth_id = 0;
244 void *method = NULL;
245 int unsupported = 0;
246
247 if (store == NULL || namemap == NULL) {
248 ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
249 return NULL;
250 }
251
252 /*
253 * If there's ever an operation_id == 0 passed, we have an internal
254 * programming error.
255 */
256 if (!ossl_assert(operation_id > 0)) {
257 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
258 return NULL;
259 }
260
261 /*
262 * If we have been passed neither a name_id or a name, we have an
263 * internal programming error.
264 */
265 if (!ossl_assert(name_id != 0 || name != NULL)) {
266 ERR_raise(ERR_LIB_CRYPTO, 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 if (name_id == 0)
272 name_id = ossl_namemap_name2num(namemap, name);
273
274 /*
275 * If we have a name id, calculate a method id with evp_method_id().
276 *
277 * evp_method_id returns 0 if we have too many operations (more than
278 * about 2^8) or too many names (more than about 2^24). In that case,
279 * we can't create any new method.
280 * For all intents and purposes, this is an internal error.
281 */
282 if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0) {
283 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
284 return NULL;
285 }
286
287 /*
288 * If we haven't found the name yet, chances are that the algorithm to
289 * be fetched is unsupported.
290 */
291 if (name_id == 0)
292 unsupported = 1;
293
294 if (meth_id == 0
295 || !ossl_method_store_cache_get(store, meth_id, properties, &method)) {
296 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
297 alloc_tmp_evp_method_store,
298 dealloc_tmp_evp_method_store,
299 get_evp_method_from_store,
300 put_evp_method_in_store,
301 construct_evp_method,
302 destruct_evp_method
303 };
304 struct evp_method_data_st mcmdata;
305
306 mcmdata.mcm = &mcm;
307 mcmdata.libctx = libctx;
308 mcmdata.operation_id = operation_id;
309 mcmdata.name_id = name_id;
310 mcmdata.names = name;
311 mcmdata.propquery = properties;
312 mcmdata.method_from_dispatch = new_method;
313 mcmdata.refcnt_up_method = up_ref_method;
314 mcmdata.destruct_method = free_method;
315 mcmdata.flag_construct_error_occured = 0;
316 if ((method = ossl_method_construct(libctx, operation_id,
317 0 /* !force_cache */,
318 &mcm, &mcmdata)) != NULL) {
319 /*
320 * If construction did create a method for us, we know that
321 * there is a correct name_id and meth_id, since those have
322 * already been calculated in get_evp_method_from_store() and
323 * put_evp_method_in_store() above.
324 */
325 if (name_id == 0)
326 name_id = ossl_namemap_name2num(namemap, name);
327 meth_id = evp_method_id(name_id, operation_id);
328 ossl_method_store_cache_set(store, meth_id, properties, method,
329 up_ref_method, free_method);
330 }
331
332 /*
333 * If we never were in the constructor, the algorithm to be fetched
334 * is unsupported.
335 */
336 unsupported = !mcmdata.flag_construct_error_occured;
337 }
338
339 if (method == NULL) {
340 int code =
341 unsupported ? EVP_R_UNSUPPORTED_ALGORITHM : EVP_R_FETCH_FAILED;
342
343 if (name == NULL)
344 name = ossl_namemap_num2name(namemap, name_id, 0);
345 ERR_raise_data(ERR_LIB_EVP, code,
346 "%s, Algorithm (%s : %d), Properties (%s)",
347 libctx_descriptor(libctx),
348 name = NULL ? "<null>" : name, name_id,
349 properties == NULL ? "<null>" : properties);
350 }
351
352 return method;
353 }
354
355 void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
356 const char *name, const char *properties,
357 void *(*new_method)(int name_id,
358 const OSSL_DISPATCH *fns,
359 OSSL_PROVIDER *prov),
360 int (*up_ref_method)(void *),
361 void (*free_method)(void *))
362 {
363 return inner_evp_generic_fetch(libctx,
364 operation_id, 0, name, properties,
365 new_method, up_ref_method, free_method);
366 }
367
368 /*
369 * evp_generic_fetch_by_number() is special, and only returns methods for
370 * already known names, i.e. it refuses to work if no name_id can be found
371 * (it's considered an internal programming error).
372 * This is meant to be used when one method needs to fetch an associated
373 * other method.
374 */
375 void *evp_generic_fetch_by_number(OSSL_LIB_CTX *libctx, int operation_id,
376 int name_id, const char *properties,
377 void *(*new_method)(int name_id,
378 const OSSL_DISPATCH *fns,
379 OSSL_PROVIDER *prov),
380 int (*up_ref_method)(void *),
381 void (*free_method)(void *))
382 {
383 return inner_evp_generic_fetch(libctx,
384 operation_id, name_id, NULL,
385 properties, new_method, up_ref_method,
386 free_method);
387 }
388
389 void evp_method_store_flush(OSSL_LIB_CTX *libctx)
390 {
391 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
392
393 if (store != NULL)
394 ossl_method_store_flush_cache(store, 1);
395 }
396
397 static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
398 OSSL_PROPERTY_LIST *def_prop,
399 int loadconfig)
400 {
401 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
402 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
403
404 if (plp != NULL) {
405 ossl_property_free(*plp);
406 *plp = def_prop;
407 if (store != NULL)
408 ossl_method_store_flush_cache(store, 0);
409 return 1;
410 }
411 EVPerr(0, ERR_R_INTERNAL_ERROR);
412 return 0;
413 }
414
415 int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
416 int loadconfig)
417 {
418 OSSL_PROPERTY_LIST *pl = NULL;
419
420 if (propq != NULL && (pl = ossl_parse_query(libctx, propq)) == NULL) {
421 EVPerr(0, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
422 return 0;
423 }
424 return evp_set_parsed_default_properties(libctx, pl, loadconfig);
425 }
426
427 int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
428 {
429 return evp_set_default_properties_int(libctx, propq, 1);
430 }
431
432 static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq)
433 {
434 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
435 OSSL_PROPERTY_LIST *pl1, *pl2;
436
437 if (propq == NULL)
438 return 1;
439 if (plp == NULL || *plp == NULL)
440 return EVP_set_default_properties(libctx, propq);
441 if ((pl1 = ossl_parse_query(libctx, propq)) == NULL) {
442 EVPerr(0, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
443 return 0;
444 }
445 pl2 = ossl_property_merge(pl1, *plp);
446 ossl_property_free(pl1);
447 if (pl2 == NULL) {
448 EVPerr(0, ERR_R_MALLOC_FAILURE);
449 return 0;
450 }
451 return evp_set_parsed_default_properties(libctx, pl2, 0);
452 }
453
454 static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
455 const char *prop_name)
456 {
457 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
458
459 return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
460 }
461
462 int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
463 {
464 return evp_default_property_is_enabled(libctx, "fips");
465 }
466
467 int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
468 {
469 const char *query = (enable != 0) ? "fips=yes" : "-fips";
470
471 return evp_default_properties_merge(libctx, query);
472 }
473
474
475 struct do_all_data_st {
476 void (*user_fn)(void *method, void *arg);
477 void *user_arg;
478 void *(*new_method)(const int name_id, const OSSL_DISPATCH *fns,
479 OSSL_PROVIDER *prov);
480 void (*free_method)(void *);
481 };
482
483 static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
484 int no_store, void *vdata)
485 {
486 struct do_all_data_st *data = vdata;
487 OSSL_LIB_CTX *libctx = ossl_provider_library_context(provider);
488 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
489 int name_id = ossl_namemap_add_names(namemap, 0, algo->algorithm_names,
490 NAME_SEPARATOR);
491 void *method = NULL;
492
493 if (name_id != 0)
494 method = data->new_method(name_id, algo->implementation, provider);
495
496 if (method != NULL) {
497 data->user_fn(method, data->user_arg);
498 data->free_method(method);
499 }
500 }
501
502 void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
503 void (*user_fn)(void *method, void *arg),
504 void *user_arg,
505 void *(*new_method)(int name_id,
506 const OSSL_DISPATCH *fns,
507 OSSL_PROVIDER *prov),
508 void (*free_method)(void *))
509 {
510 struct do_all_data_st data;
511
512 data.new_method = new_method;
513 data.free_method = free_method;
514 data.user_fn = user_fn;
515 data.user_arg = user_arg;
516
517 /*
518 * No pre- or post-condition for this call, as this only creates methods
519 * temporarly and then promptly destroys them.
520 */
521 ossl_algorithm_do_all(libctx, operation_id, NULL, NULL, do_one, NULL,
522 &data);
523 }
524
525 const char *evp_first_name(const OSSL_PROVIDER *prov, int name_id)
526 {
527 OSSL_LIB_CTX *libctx = ossl_provider_library_context(prov);
528 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
529
530 return ossl_namemap_num2name(namemap, name_id, 0);
531 }
532
533 int evp_is_a(OSSL_PROVIDER *prov, int number,
534 const char *legacy_name, const char *name)
535 {
536 /*
537 * For a |prov| that is NULL, the library context will be NULL
538 */
539 OSSL_LIB_CTX *libctx = ossl_provider_library_context(prov);
540 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
541
542 if (prov == NULL)
543 number = ossl_namemap_name2num(namemap, legacy_name);
544 return ossl_namemap_name2num(namemap, name) == number;
545 }
546
547 void evp_names_do_all(OSSL_PROVIDER *prov, int number,
548 void (*fn)(const char *name, void *data),
549 void *data)
550 {
551 OSSL_LIB_CTX *libctx = ossl_provider_library_context(prov);
552 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
553
554 ossl_namemap_doall_names(namemap, number, fn, data);
555 }