]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_fetch.c
provider: disable fall-backs if OSSL_PROVIDER_load() fails.
[thirdparty/openssl.git] / crypto / evp / evp_fetch.c
CommitLineData
c13d2ab4 1/*
33388b44 2 * Copyright 2019-2020 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"
04cb5ec0 20#include "internal/property.h"
706457b7
DMSP
21#include "crypto/evp.h" /* evp_local.h needs it */
22#include "evp_local.h"
c13d2ab4 23
695d195b
RL
24#define NAME_SEPARATOR ':'
25
181ea366 26static void evp_method_store_free(void *vstore)
c13d2ab4
RL
27{
28 ossl_method_store_free(vstore);
29}
30
181ea366 31static void *evp_method_store_new(OPENSSL_CTX *ctx)
c13d2ab4 32{
1aedc35f 33 return ossl_method_store_new(ctx);
c13d2ab4
RL
34}
35
36
181ea366
RL
37static const OPENSSL_CTX_METHOD evp_method_store_method = {
38 evp_method_store_new,
39 evp_method_store_free,
c13d2ab4
RL
40};
41
c13d2ab4 42/* Data to be passed through ossl_method_construct() */
181ea366 43struct evp_method_data_st {
baff732d 44 OPENSSL_CTX *libctx;
c13d2ab4 45 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
181ea366
RL
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() */
f7c16d48 50 void *(*method_from_dispatch)(int name_id, const OSSL_DISPATCH *,
0ddf74bf 51 OSSL_PROVIDER *);
c13d2ab4
RL
52 int (*refcnt_up_method)(void *method);
53 void (*destruct_method)(void *method);
54};
55
56/*
57 * Generic routines to fetch / create EVP methods with ossl_method_construct()
58 */
181ea366 59static void *alloc_tmp_evp_method_store(OPENSSL_CTX *ctx)
c13d2ab4 60{
1aedc35f 61 return ossl_method_store_new(ctx);
c13d2ab4
RL
62}
63
181ea366 64 static void dealloc_tmp_evp_method_store(void *store)
c13d2ab4
RL
65{
66 if (store != NULL)
67 ossl_method_store_free(store);
68}
69
181ea366 70static OSSL_METHOD_STORE *get_evp_method_store(OPENSSL_CTX *libctx)
c13d2ab4 71{
181ea366
RL
72 return openssl_ctx_get_data(libctx, OPENSSL_CTX_EVP_METHOD_STORE_INDEX,
73 &evp_method_store_method);
c13d2ab4
RL
74}
75
2ccb1b4e 76/*
64f849f4
DMSP
77 * To identify the method in the EVP method store, we mix the name identity
78 * with the operation identity, under the assumption that we don't have more
181ea366 79 * than 2^24 names or more than 2^8 operation types.
2ccb1b4e
RL
80 *
81 * The resulting identity is a 32-bit integer, composed like this:
82 *
83 * +---------24 bits--------+-8 bits-+
84 * | name identity | op id |
85 * +------------------------+--------+
86 */
b418980c 87static uint32_t evp_method_id(int name_id, unsigned int operation_id)
2ccb1b4e 88{
64f849f4
DMSP
89 if (!ossl_assert(name_id > 0 && name_id < (1 << 24))
90 || !ossl_assert(operation_id > 0 && operation_id < (1 << 8)))
2ccb1b4e
RL
91 return 0;
92 return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
93}
94
181ea366
RL
95static void *get_evp_method_from_store(OPENSSL_CTX *libctx, void *store,
96 void *data)
c13d2ab4 97{
181ea366 98 struct evp_method_data_st *methdata = data;
c13d2ab4 99 void *method = NULL;
f7c16d48
RL
100 int name_id;
101 uint32_t meth_id;
c13d2ab4 102
f7c16d48 103 /*
181ea366
RL
104 * get_evp_method_from_store() is only called to try and get the method
105 * that evp_generic_fetch() is asking for, and the operation id as well
106 * as the name or name id are passed via methdata.
f7c16d48
RL
107 */
108 if ((name_id = methdata->name_id) == 0) {
109 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
695d195b
RL
110 const char *names = methdata->names;
111 const char *q = strchr(names, NAME_SEPARATOR);
112 size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
f7c16d48
RL
113
114 if (namemap == 0)
115 return NULL;
695d195b 116 name_id = ossl_namemap_name2num_n(namemap, names, l);
f7c16d48
RL
117 }
118
119 if (name_id == 0
b418980c 120 || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
c13d2ab4
RL
121 return NULL;
122
f7c16d48 123 if (store == NULL
181ea366 124 && (store = get_evp_method_store(libctx)) == NULL)
2e49c054
RL
125 return NULL;
126
bdbf2df2
P
127 if (!ossl_method_store_fetch(store, meth_id, methdata->propquery,
128 &method))
129 return NULL;
c13d2ab4
RL
130 return method;
131}
132
181ea366
RL
133static int put_evp_method_in_store(OPENSSL_CTX *libctx, void *store,
134 void *method, const OSSL_PROVIDER *prov,
135 int operation_id, const char *names,
136 const char *propdef, void *data)
c13d2ab4 137{
181ea366 138 struct evp_method_data_st *methdata = data;
2e49c054 139 OSSL_NAMEMAP *namemap;
f7c16d48
RL
140 int name_id;
141 uint32_t meth_id;
695d195b 142 size_t l = 0;
dc46e3dd 143
f7c16d48 144 /*
181ea366
RL
145 * put_evp_method_in_store() is only called with an EVP method that was
146 * successfully created by construct_method() below, which means that
147 * all the names should already be stored in the namemap with the same
148 * numeric identity, so just use the first to get that identity.
f7c16d48 149 */
695d195b
RL
150 if (names != NULL) {
151 const char *q = strchr(names, NAME_SEPARATOR);
152
153 l = (q == NULL ? strlen(names) : (size_t)(q - names));
154 }
155
f7c16d48 156 if ((namemap = ossl_namemap_stored(libctx)) == NULL
695d195b 157 || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
b418980c 158 || (meth_id = evp_method_id(name_id, operation_id)) == 0)
dc46e3dd 159 return 0;
c13d2ab4
RL
160
161 if (store == NULL
181ea366 162 && (store = get_evp_method_store(libctx)) == NULL)
c13d2ab4
RL
163 return 0;
164
f7c16d48 165 return ossl_method_store_add(store, prov, meth_id, propdef, method,
b1d40ddf
RL
166 methdata->refcnt_up_method,
167 methdata->destruct_method);
c13d2ab4
RL
168}
169
f7c16d48
RL
170/*
171 * The core fetching functionality passes the name of the implementation.
172 * This function is responsible to getting an identity number for it.
173 */
36fa4d8a 174static void *construct_evp_method(const OSSL_ALGORITHM *algodef,
181ea366 175 OSSL_PROVIDER *prov, void *data)
c13d2ab4 176{
f7c16d48 177 /*
181ea366 178 * This function is only called if get_evp_method_from_store() returned
f7c16d48
RL
179 * NULL, so it's safe to say that of all the spots to create a new
180 * namemap entry, this is it. Should the name already exist there, we
3d83c735
RL
181 * know that ossl_namemap_add_name() will return its corresponding
182 * number.
f7c16d48 183 */
181ea366 184 struct evp_method_data_st *methdata = data;
f7c16d48
RL
185 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
186 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
36fa4d8a 187 const char *names = algodef->algorithm_names;
3d83c735 188 int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
c13d2ab4 189
695d195b
RL
190 if (name_id == 0)
191 return NULL;
36fa4d8a
RL
192 return methdata->method_from_dispatch(name_id, algodef->implementation,
193 prov);
c13d2ab4
RL
194}
195
181ea366 196static void destruct_evp_method(void *method, void *data)
c13d2ab4 197{
181ea366 198 struct evp_method_data_st *methdata = data;
c13d2ab4
RL
199
200 methdata->destruct_method(method);
201}
202
181ea366
RL
203static void *
204inner_evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
205 int name_id, const char *name,
206 const char *properties,
207 void *(*new_method)(int name_id,
208 const OSSL_DISPATCH *fns,
0ddf74bf 209 OSSL_PROVIDER *prov),
181ea366
RL
210 int (*up_ref_method)(void *),
211 void (*free_method)(void *))
c13d2ab4 212{
181ea366 213 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
437ad983 214 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
f7c16d48 215 uint32_t meth_id = 0;
c13d2ab4
RL
216 void *method = NULL;
217
baff732d 218 if (store == NULL || namemap == NULL)
e019da7b
RL
219 return NULL;
220
2ccb1b4e
RL
221 /*
222 * If there's ever an operation_id == 0 passed, we have an internal
223 * programming error.
224 */
225 if (!ossl_assert(operation_id > 0))
226 return NULL;
227
228 /*
f7c16d48
RL
229 * If we have been passed neither a name_id or a name, we have an
230 * internal programming error.
231 */
232 if (!ossl_assert(name_id != 0 || name != NULL))
233 return NULL;
234
235 /* If we haven't received a name id yet, try to get one for the name */
236 if (name_id == 0)
237 name_id = ossl_namemap_name2num(namemap, name);
238
239 /*
181ea366 240 * If we have a name id, calculate a method id with evp_method_id().
f7c16d48 241 *
181ea366
RL
242 * evp_method_id returns 0 if we have too many operations (more than
243 * about 2^8) or too many names (more than about 2^24). In that case,
244 * we can't create any new method.
2ccb1b4e 245 */
b418980c 246 if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0)
2ccb1b4e
RL
247 return NULL;
248
f7c16d48
RL
249 if (meth_id == 0
250 || !ossl_method_store_cache_get(store, meth_id, properties, &method)) {
c13d2ab4 251 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
181ea366
RL
252 alloc_tmp_evp_method_store,
253 dealloc_tmp_evp_method_store,
254 get_evp_method_from_store,
255 put_evp_method_in_store,
256 construct_evp_method,
257 destruct_evp_method
c13d2ab4 258 };
181ea366 259 struct evp_method_data_st mcmdata;
c13d2ab4 260
c13d2ab4 261 mcmdata.mcm = &mcm;
baff732d 262 mcmdata.libctx = libctx;
f7c16d48
RL
263 mcmdata.operation_id = operation_id;
264 mcmdata.name_id = name_id;
695d195b 265 mcmdata.names = name;
f7c16d48 266 mcmdata.propquery = properties;
c13d2ab4 267 mcmdata.method_from_dispatch = new_method;
7c95390e 268 mcmdata.refcnt_up_method = up_ref_method;
c13d2ab4 269 mcmdata.destruct_method = free_method;
f7c16d48
RL
270 if ((method = ossl_method_construct(libctx, operation_id,
271 0 /* !force_cache */,
08607613 272 &mcm, &mcmdata)) != NULL) {
2ccb1b4e
RL
273 /*
274 * If construction did create a method for us, we know that
181ea366
RL
275 * there is a correct name_id and meth_id, since those have
276 * already been calculated in get_evp_method_from_store() and
277 * put_evp_method_in_store() above.
2ccb1b4e 278 */
f7c16d48
RL
279 if (name_id == 0)
280 name_id = ossl_namemap_name2num(namemap, name);
b418980c 281 meth_id = evp_method_id(name_id, operation_id);
bdbf2df2
P
282 ossl_method_store_cache_set(store, meth_id, properties, method,
283 up_ref_method, free_method);
2ccb1b4e 284 }
c13d2ab4
RL
285 }
286
287 return method;
288}
cb929645 289
cfbd76c1
RL
290#ifndef FIPS_MODULE
291static const char *libctx_descriptor(OPENSSL_CTX *libctx)
292{
293 if (openssl_ctx_is_global_default(libctx))
294 return "Global default library context";
295 if (openssl_ctx_is_default(libctx))
296 return "Thread-local default library context";
297 return "Non-default library context";
298}
299#endif
300
f7c16d48
RL
301void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
302 const char *name, const char *properties,
303 void *(*new_method)(int name_id,
304 const OSSL_DISPATCH *fns,
0ddf74bf 305 OSSL_PROVIDER *prov),
f7c16d48
RL
306 int (*up_ref_method)(void *),
307 void (*free_method)(void *))
308{
6b1e5fa4
MC
309 void *ret = inner_evp_generic_fetch(libctx,
310 operation_id, 0, name, properties,
311 new_method, up_ref_method, free_method);
312
313 if (ret == NULL) {
314 int code = EVP_R_FETCH_FAILED;
315
f844f9eb 316#ifdef FIPS_MODULE
6b1e5fa4
MC
317 ERR_raise(ERR_LIB_EVP, code);
318#else
319 ERR_raise_data(ERR_LIB_EVP, code,
320 "%s, Algorithm (%s), Properties (%s)",
cfbd76c1 321 libctx_descriptor(libctx),
6b1e5fa4
MC
322 name = NULL ? "<null>" : name,
323 properties == NULL ? "<null>" : properties);
324#endif
325 }
326 return ret;
f7c16d48
RL
327}
328
329/*
330 * evp_generic_fetch_by_number() is special, and only returns methods for
331 * already known names, i.e. it refuses to work if no name_id can be found
332 * (it's considered an internal programming error).
333 * This is meant to be used when one method needs to fetch an associated
334 * other method.
335 */
336void *evp_generic_fetch_by_number(OPENSSL_CTX *libctx, int operation_id,
337 int name_id, const char *properties,
338 void *(*new_method)(int name_id,
339 const OSSL_DISPATCH *fns,
0ddf74bf 340 OSSL_PROVIDER *prov),
f7c16d48
RL
341 int (*up_ref_method)(void *),
342 void (*free_method)(void *))
343{
6b1e5fa4
MC
344 void *ret = inner_evp_generic_fetch(libctx,
345 operation_id, name_id, NULL,
346 properties, new_method, up_ref_method,
347 free_method);
348
349 if (ret == NULL) {
350 int code = EVP_R_FETCH_FAILED;
351
f844f9eb 352#ifdef FIPS_MODULE
6b1e5fa4
MC
353 ERR_raise(ERR_LIB_EVP, code);
354#else
355 {
356 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
357 const char *name = (namemap == NULL)
358 ? NULL
359 : ossl_namemap_num2name(namemap, name_id, 0);
360
361 ERR_raise_data(ERR_LIB_EVP, code,
362 "%s, Algorithm (%s), Properties (%s)",
cfbd76c1 363 libctx_descriptor(libctx),
6b1e5fa4
MC
364 name = NULL ? "<null>" : name,
365 properties == NULL ? "<null>" : properties);
366 }
367#endif
368 }
369 return ret;
f7c16d48
RL
370}
371
04cb5ec0
SL
372void evp_method_store_flush(OPENSSL_CTX *libctx)
373{
374 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
375
376 if (store != NULL)
377 ossl_method_store_flush_cache(store, 1);
378}
379
f9e504e8
P
380static int evp_set_default_properties(OPENSSL_CTX *libctx,
381 OSSL_PROPERTY_LIST *def_prop)
cb929645 382{
181ea366 383 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
f9e504e8
P
384 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx);
385
386 if (plp != NULL) {
387 ossl_property_free(*plp);
388 *plp = def_prop;
389 if (store != NULL)
04cb5ec0 390 ossl_method_store_flush_cache(store, 0);
f9e504e8
P
391 return 1;
392 }
e0624f0d 393 EVPerr(0, ERR_R_INTERNAL_ERROR);
cb929645
RL
394 return 0;
395}
3d96a51c 396
f9e504e8
P
397int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
398{
399 OSSL_PROPERTY_LIST *pl = NULL;
400
401 if (propq != NULL && (pl = ossl_parse_query(libctx, propq)) == NULL) {
402 EVPerr(0, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
403 return 0;
404 }
405 return evp_set_default_properties(libctx, pl);
406}
407
e0624f0d
SL
408
409static int evp_default_properties_merge(OPENSSL_CTX *libctx, const char *propq)
410{
f9e504e8
P
411 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx);
412 OSSL_PROPERTY_LIST *pl1, *pl2;
413
414 if (propq == NULL)
415 return 1;
416 if (plp == NULL || *plp == NULL)
417 return EVP_set_default_properties(libctx, propq);
418 if ((pl1 = ossl_parse_query(libctx, propq)) == NULL) {
419 EVPerr(0, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
420 return 0;
421 }
422 pl2 = ossl_property_merge(pl1, *plp);
423 ossl_property_free(pl1);
424 if (pl2 == NULL) {
425 EVPerr(0, ERR_R_MALLOC_FAILURE);
426 return 0;
427 }
428 return evp_set_default_properties(libctx, pl2);
e0624f0d
SL
429}
430
431static int evp_default_property_is_enabled(OPENSSL_CTX *libctx,
432 const char *prop_name)
433{
f9e504e8 434 OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx);
e0624f0d 435
f9e504e8 436 return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
e0624f0d
SL
437}
438
439int EVP_default_properties_is_fips_enabled(OPENSSL_CTX *libctx)
440{
441 return evp_default_property_is_enabled(libctx, "fips");
442}
443
444int EVP_default_properties_enable_fips(OPENSSL_CTX *libctx, int enable)
445{
446 const char *query = (enable != 0) ? "fips=yes" : "-fips";
447
448 return evp_default_properties_merge(libctx, query);
449}
450
451
3d96a51c
RL
452struct do_all_data_st {
453 void (*user_fn)(void *method, void *arg);
454 void *user_arg;
f7c16d48 455 void *(*new_method)(const int name_id, const OSSL_DISPATCH *fns,
0ddf74bf 456 OSSL_PROVIDER *prov);
3d96a51c
RL
457 void (*free_method)(void *);
458};
459
460static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
461 int no_store, void *vdata)
462{
463 struct do_all_data_st *data = vdata;
f7c16d48
RL
464 OPENSSL_CTX *libctx = ossl_provider_library_context(provider);
465 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
3d83c735
RL
466 int name_id = ossl_namemap_add_names(namemap, 0, algo->algorithm_names,
467 NAME_SEPARATOR);
f7c16d48
RL
468 void *method = NULL;
469
470 if (name_id != 0)
0ddf74bf 471 method = data->new_method(name_id, algo->implementation, provider);
3d96a51c
RL
472
473 if (method != NULL) {
474 data->user_fn(method, data->user_arg);
475 data->free_method(method);
476 }
477}
478
479void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
480 void (*user_fn)(void *method, void *arg),
481 void *user_arg,
f7c16d48 482 void *(*new_method)(int name_id,
3d96a51c 483 const OSSL_DISPATCH *fns,
0ddf74bf 484 OSSL_PROVIDER *prov),
3d96a51c
RL
485 void (*free_method)(void *))
486{
487 struct do_all_data_st data;
488
489 data.new_method = new_method;
490 data.free_method = free_method;
491 data.user_fn = user_fn;
492 data.user_arg = user_arg;
5a29b628
RL
493
494 /*
495 * No pre- or post-condition for this call, as this only creates methods
496 * temporarly and then promptly destroys them.
497 */
498 ossl_algorithm_do_all(libctx, operation_id, NULL, NULL, do_one, NULL,
499 &data);
3d96a51c 500}
f7c16d48 501
3f7ce7f1 502const char *evp_first_name(const OSSL_PROVIDER *prov, int name_id)
f7c16d48
RL
503{
504 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
505 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
506
507 return ossl_namemap_num2name(namemap, name_id, 0);
508}
7cfa1717 509
e4a1d023
RL
510int evp_is_a(OSSL_PROVIDER *prov, int number,
511 const char *legacy_name, const char *name)
7cfa1717 512{
e4a1d023
RL
513 /*
514 * For a |prov| that is NULL, the library context will be NULL
515 */
7cfa1717
RL
516 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
517 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
518
e4a1d023
RL
519 if (prov == NULL)
520 number = ossl_namemap_name2num(namemap, legacy_name);
7cfa1717
RL
521 return ossl_namemap_name2num(namemap, name) == number;
522}
32040838 523
f651c727
RL
524void evp_names_do_all(OSSL_PROVIDER *prov, int number,
525 void (*fn)(const char *name, void *data),
526 void *data)
32040838
RL
527{
528 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
529 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
530
531 ossl_namemap_doall_names(namemap, number, fn, data);
532}