2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
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
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"
23 #define NAME_SEPARATOR ':'
25 static void evp_method_store_free(void *vstore)
27 ossl_method_store_free(vstore);
30 static void *evp_method_store_new(OPENSSL_CTX *ctx)
32 return ossl_method_store_new(ctx);
36 static const OPENSSL_CTX_METHOD evp_method_store_method = {
38 evp_method_store_free,
41 /* Data to be passed through ossl_method_construct() */
42 struct evp_method_data_st {
44 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
45 int operation_id; /* For get_evp_method_from_store() */
46 int name_id; /* For get_evp_method_from_store() */
47 const char *names; /* For get_evp_method_from_store() */
48 const char *propquery; /* For get_evp_method_from_store() */
49 void *(*method_from_dispatch)(int name_id, const OSSL_DISPATCH *,
50 OSSL_PROVIDER *, void *);
52 int (*refcnt_up_method)(void *method);
53 void (*destruct_method)(void *method);
56 static int add_names_to_namemap(OSSL_NAMEMAP *namemap,
63 /* Check that we have a namemap and that there is at least one name */
64 if (namemap == NULL) {
65 ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
70 * Check that no name is an empty string, and that all names have at
71 * most one numeric identity together.
73 for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
76 if ((q = strchr(p, NAME_SEPARATOR)) == NULL)
77 l = strlen(p); /* offset to \0 */
79 l = q - p; /* offset to the next separator */
81 this_id = ossl_namemap_name2num_n(namemap, p, l);
83 if (*p == '\0' || *p == NAME_SEPARATOR) {
84 ERR_raise(ERR_LIB_EVP, EVP_R_BAD_ALGORITHM_NAME);
89 else if (this_id != 0 && this_id != id) {
90 ERR_raise_data(ERR_LIB_EVP, EVP_R_CONFLICTING_ALGORITHM_NAME,
91 "\"%.*s\" has an existing different identity %d (from \"%s\")",
92 l, p, this_id, names);
97 /* Now that we have checked, register all names */
98 for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
101 if ((q = strchr(p, NAME_SEPARATOR)) == NULL)
102 l = strlen(p); /* offset to \0 */
104 l = q - p; /* offset to the next separator */
106 this_id = ossl_namemap_add_n(namemap, id, p, l);
109 else if (this_id != id) {
110 ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
111 "Got id %d when expecting %d", this_id, id);
120 * Generic routines to fetch / create EVP methods with ossl_method_construct()
122 static void *alloc_tmp_evp_method_store(OPENSSL_CTX *ctx)
124 return ossl_method_store_new(ctx);
127 static void dealloc_tmp_evp_method_store(void *store)
130 ossl_method_store_free(store);
133 static OSSL_METHOD_STORE *get_evp_method_store(OPENSSL_CTX *libctx)
135 return openssl_ctx_get_data(libctx, OPENSSL_CTX_EVP_METHOD_STORE_INDEX,
136 &evp_method_store_method);
140 * To identity the method in the EVP method store, we mix the name identity
141 * with the operation identity, with the assumption that we don't have more
142 * than 2^24 names or more than 2^8 operation types.
144 * The resulting identity is a 32-bit integer, composed like this:
146 * +---------24 bits--------+-8 bits-+
147 * | name identity | op id |
148 * +------------------------+--------+
150 static uint32_t evp_method_id(unsigned int operation_id, int name_id)
152 if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8))
153 || !ossl_assert(name_id > 0 && operation_id > 0))
155 return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
158 static void *get_evp_method_from_store(OPENSSL_CTX *libctx, void *store,
161 struct evp_method_data_st *methdata = data;
167 * get_evp_method_from_store() is only called to try and get the method
168 * that evp_generic_fetch() is asking for, and the operation id as well
169 * as the name or name id are passed via methdata.
171 if ((name_id = methdata->name_id) == 0) {
172 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
173 const char *names = methdata->names;
174 const char *q = strchr(names, NAME_SEPARATOR);
175 size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
179 name_id = ossl_namemap_name2num_n(namemap, names, l);
183 || (meth_id = evp_method_id(methdata->operation_id, name_id)) == 0)
187 && (store = get_evp_method_store(libctx)) == NULL)
190 (void)ossl_method_store_fetch(store, meth_id, methdata->propquery,
194 && !methdata->refcnt_up_method(method)) {
200 static int put_evp_method_in_store(OPENSSL_CTX *libctx, void *store,
201 void *method, const OSSL_PROVIDER *prov,
202 int operation_id, const char *names,
203 const char *propdef, void *data)
205 struct evp_method_data_st *methdata = data;
206 OSSL_NAMEMAP *namemap;
212 * put_evp_method_in_store() is only called with an EVP method that was
213 * successfully created by construct_method() below, which means that
214 * all the names should already be stored in the namemap with the same
215 * numeric identity, so just use the first to get that identity.
218 const char *q = strchr(names, NAME_SEPARATOR);
220 l = (q == NULL ? strlen(names) : (size_t)(q - names));
223 if ((namemap = ossl_namemap_stored(libctx)) == NULL
224 || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
225 || (meth_id = evp_method_id(operation_id, name_id)) == 0)
229 && (store = get_evp_method_store(libctx)) == NULL)
232 return ossl_method_store_add(store, prov, meth_id, propdef, method,
233 methdata->refcnt_up_method,
234 methdata->destruct_method);
238 * The core fetching functionality passes the name of the implementation.
239 * This function is responsible to getting an identity number for it.
241 static void *construct_evp_method(const char *names, const OSSL_DISPATCH *fns,
242 OSSL_PROVIDER *prov, void *data)
245 * This function is only called if get_evp_method_from_store() returned
246 * NULL, so it's safe to say that of all the spots to create a new
247 * namemap entry, this is it. Should the name already exist there, we
248 * know that ossl_namemap_add() will return its corresponding number.
250 struct evp_method_data_st *methdata = data;
251 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
252 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
253 int name_id = add_names_to_namemap(namemap, names);
257 return methdata->method_from_dispatch(name_id, fns, prov,
258 methdata->method_data);
261 static void destruct_evp_method(void *method, void *data)
263 struct evp_method_data_st *methdata = data;
265 methdata->destruct_method(method);
269 inner_evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
270 int name_id, const char *name,
271 const char *properties,
272 void *(*new_method)(int name_id,
273 const OSSL_DISPATCH *fns,
277 int (*up_ref_method)(void *),
278 void (*free_method)(void *))
280 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
281 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
282 uint32_t meth_id = 0;
285 if (store == NULL || namemap == NULL)
289 * If there's ever an operation_id == 0 passed, we have an internal
292 if (!ossl_assert(operation_id > 0))
296 * If we have been passed neither a name_id or a name, we have an
297 * internal programming error.
299 if (!ossl_assert(name_id != 0 || name != NULL))
302 /* If we haven't received a name id yet, try to get one for the name */
304 name_id = ossl_namemap_name2num(namemap, name);
307 * If we have a name id, calculate a method id with evp_method_id().
309 * evp_method_id returns 0 if we have too many operations (more than
310 * about 2^8) or too many names (more than about 2^24). In that case,
311 * we can't create any new method.
313 if (name_id != 0 && (meth_id = evp_method_id(operation_id, name_id)) == 0)
317 || !ossl_method_store_cache_get(store, meth_id, properties, &method)) {
318 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
319 alloc_tmp_evp_method_store,
320 dealloc_tmp_evp_method_store,
321 get_evp_method_from_store,
322 put_evp_method_in_store,
323 construct_evp_method,
326 struct evp_method_data_st mcmdata;
329 mcmdata.libctx = libctx;
330 mcmdata.operation_id = operation_id;
331 mcmdata.name_id = name_id;
332 mcmdata.names = name;
333 mcmdata.propquery = properties;
334 mcmdata.method_from_dispatch = new_method;
335 mcmdata.destruct_method = free_method;
336 mcmdata.refcnt_up_method = up_ref_method;
337 mcmdata.destruct_method = free_method;
338 mcmdata.method_data = method_data;
339 if ((method = ossl_method_construct(libctx, operation_id,
340 0 /* !force_cache */,
341 &mcm, &mcmdata)) != NULL) {
343 * If construction did create a method for us, we know that
344 * there is a correct name_id and meth_id, since those have
345 * already been calculated in get_evp_method_from_store() and
346 * put_evp_method_in_store() above.
349 name_id = ossl_namemap_name2num(namemap, name);
350 meth_id = evp_method_id(operation_id, name_id);
351 ossl_method_store_cache_set(store, meth_id, properties, method);
354 up_ref_method(method);
360 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
361 const char *name, const char *properties,
362 void *(*new_method)(int name_id,
363 const OSSL_DISPATCH *fns,
367 int (*up_ref_method)(void *),
368 void (*free_method)(void *))
370 return inner_evp_generic_fetch(libctx,
371 operation_id, 0, name, properties,
372 new_method, method_data,
373 up_ref_method, free_method);
377 * evp_generic_fetch_by_number() is special, and only returns methods for
378 * already known names, i.e. it refuses to work if no name_id can be found
379 * (it's considered an internal programming error).
380 * This is meant to be used when one method needs to fetch an associated
383 void *evp_generic_fetch_by_number(OPENSSL_CTX *libctx, int operation_id,
384 int name_id, const char *properties,
385 void *(*new_method)(int name_id,
386 const OSSL_DISPATCH *fns,
390 int (*up_ref_method)(void *),
391 void (*free_method)(void *))
393 return inner_evp_generic_fetch(libctx,
394 operation_id, name_id, NULL, properties,
395 new_method, method_data,
396 up_ref_method, free_method);
399 int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
401 OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
404 return ossl_method_store_set_global_properties(store, propq);
405 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
409 struct do_all_data_st {
410 void (*user_fn)(void *method, void *arg);
412 void *(*new_method)(const int name_id, const OSSL_DISPATCH *fns,
413 OSSL_PROVIDER *prov, void *method_data);
415 void (*free_method)(void *);
418 static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
419 int no_store, void *vdata)
421 struct do_all_data_st *data = vdata;
422 OPENSSL_CTX *libctx = ossl_provider_library_context(provider);
423 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
424 int name_id = add_names_to_namemap(namemap, algo->algorithm_names);
428 method = data->new_method(name_id, algo->implementation, provider,
431 if (method != NULL) {
432 data->user_fn(method, data->user_arg);
433 data->free_method(method);
437 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
438 void (*user_fn)(void *method, void *arg),
440 void *(*new_method)(int name_id,
441 const OSSL_DISPATCH *fns,
445 void (*free_method)(void *))
447 struct do_all_data_st data;
449 data.new_method = new_method;
450 data.method_data = method_data;
451 data.free_method = free_method;
452 data.user_fn = user_fn;
453 data.user_arg = user_arg;
454 ossl_algorithm_do_all(libctx, operation_id, NULL, do_one, &data);
457 const char *evp_first_name(OSSL_PROVIDER *prov, int name_id)
459 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
460 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
462 return ossl_namemap_num2name(namemap, name_id, 0);
465 int evp_is_a(OSSL_PROVIDER *prov, int number, const char *name)
467 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
468 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
470 return ossl_namemap_name2num(namemap, name) == number;
473 void evp_names_do_all(OSSL_PROVIDER *prov, int number,
474 void (*fn)(const char *name, void *data),
477 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
478 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
480 ossl_namemap_doall_names(namemap, number, fn, data);