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/ossl_typ.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/namemap.h"
19 #include "internal/evp_int.h" /* evp_locl.h needs it */
22 static void default_method_store_free(void *vstore)
24 ossl_method_store_free(vstore);
27 static void *default_method_store_new(OPENSSL_CTX *ctx)
29 return ossl_method_store_new(ctx);
33 static const OPENSSL_CTX_METHOD default_method_store_method = {
34 default_method_store_new,
35 default_method_store_free,
38 /* Data to be passed through ossl_method_construct() */
39 struct method_data_st {
42 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
43 void *(*method_from_dispatch)(const char *, const OSSL_DISPATCH *,
44 OSSL_PROVIDER *, void *);
46 int (*refcnt_up_method)(void *method);
47 void (*destruct_method)(void *method);
51 * Generic routines to fetch / create EVP methods with ossl_method_construct()
53 static void *alloc_tmp_method_store(OPENSSL_CTX *ctx)
55 return ossl_method_store_new(ctx);
58 static void dealloc_tmp_method_store(void *store)
61 ossl_method_store_free(store);
64 static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
66 return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX,
67 &default_method_store_method);
71 * To identity the method in the method store, we mix the name identity
72 * with the operation identity, with the assumption that we don't have
73 * more than 2^24 names or more than 2^8 operation types.
75 * The resulting identity is a 32-bit integer, composed like this:
77 * +---------24 bits--------+-8 bits-+
78 * | name identity | op id |
79 * +------------------------+--------+
81 static uint32_t method_id(unsigned int operation_id, unsigned int name_id)
83 if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8))
84 || !ossl_assert(name_id > 0 && operation_id > 0))
86 return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
89 static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
90 int operation_id, const char *name,
91 const char *propquery, void *data)
93 struct method_data_st *methdata = data;
95 OSSL_NAMEMAP *namemap;
100 && (store = get_default_method_store(libctx)) == NULL)
103 if ((namemap = ossl_namemap_stored(libctx)) == NULL
104 || (nameid = ossl_namemap_name2num(namemap, name)) == 0
105 || (methid = method_id(operation_id, nameid)) == 0)
108 (void)ossl_method_store_fetch(store, methid, propquery, &method);
111 && !methdata->refcnt_up_method(method)) {
117 static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
118 void *method, const OSSL_PROVIDER *prov,
119 int operation_id, const char *name,
120 const char *propdef, void *data)
122 struct method_data_st *methdata = data;
123 OSSL_NAMEMAP *namemap;
127 if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
128 || (nameid = ossl_namemap_add(namemap, 0, name)) == 0
129 || (methid = method_id(operation_id, nameid)) == 0)
133 && (store = get_default_method_store(libctx)) == NULL)
136 return ossl_method_store_add(store, prov, methid, propdef, method,
137 methdata->refcnt_up_method,
138 methdata->destruct_method);
141 static void *construct_method(const char *name, const OSSL_DISPATCH *fns,
142 OSSL_PROVIDER *prov, void *data)
144 struct method_data_st *methdata = data;
146 return methdata->method_from_dispatch(name, fns, prov,
147 methdata->method_data);
150 static void destruct_method(void *method, void *data)
152 struct method_data_st *methdata = data;
154 methdata->destruct_method(method);
157 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
158 const char *name, const char *properties,
159 void *(*new_method)(const char *name,
160 const OSSL_DISPATCH *fns,
164 int (*up_ref_method)(void *),
165 void (*free_method)(void *))
167 OSSL_METHOD_STORE *store = get_default_method_store(libctx);
168 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
173 if (store == NULL || namemap == NULL)
177 * If there's ever an operation_id == 0 passed, we have an internal
180 if (!ossl_assert(operation_id > 0))
184 * method_id returns 0 if we have too many operations (more than
185 * about 2^8) or too many names (more than about 2^24). In that
186 * case, we can't create any new method.
188 if ((nameid = ossl_namemap_name2num(namemap, name)) != 0
189 && (methid = method_id(operation_id, nameid)) == 0)
193 || !ossl_method_store_cache_get(store, methid, properties,
195 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
196 alloc_tmp_method_store,
197 dealloc_tmp_method_store,
198 get_method_from_store,
203 struct method_data_st mcmdata;
206 mcmdata.libctx = libctx;
208 mcmdata.method_from_dispatch = new_method;
209 mcmdata.destruct_method = free_method;
210 mcmdata.refcnt_up_method = up_ref_method;
211 mcmdata.destruct_method = free_method;
212 mcmdata.method_data = method_data;
213 if ((method = ossl_method_construct(libctx, operation_id, name,
214 properties, 0 /* !force_cache */,
215 &mcm, &mcmdata)) != NULL) {
217 * If construction did create a method for us, we know that
218 * there is a correct nameid and methodid, since those have
219 * already been calculated in get_method_from_store() and
220 * put_method_in_store() above.
222 nameid = ossl_namemap_name2num(namemap, name);
223 methid = method_id(operation_id, nameid);
224 ossl_method_store_cache_set(store, methid, properties, method);
227 up_ref_method(method);
233 int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
235 OSSL_METHOD_STORE *store = get_default_method_store(libctx);
238 return ossl_method_store_set_global_properties(store, propq);
239 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
243 struct do_all_data_st {
244 void (*user_fn)(void *method, void *arg);
246 void *(*new_method)(const char *name, const OSSL_DISPATCH *fns,
247 OSSL_PROVIDER *prov, void *method_data);
248 void (*free_method)(void *);
251 static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
252 int no_store, void *vdata)
254 struct do_all_data_st *data = vdata;
255 void *method = data->new_method(algo->algorithm_name,
256 algo->implementation, provider, NULL);
258 if (method != NULL) {
259 data->user_fn(method, data->user_arg);
260 data->free_method(method);
264 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
265 void (*user_fn)(void *method, void *arg),
267 void *(*new_method)(const char *name,
268 const OSSL_DISPATCH *fns,
272 void (*free_method)(void *))
274 struct do_all_data_st data;
276 data.new_method = new_method;
277 data.free_method = free_method;
278 data.user_fn = user_fn;
279 data.user_arg = user_arg;
280 ossl_algorithm_do_all(libctx, operation_id, method_data, do_one, &data);