]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/evp_fetch.c
Add internal function evp_generic_do_all()
[thirdparty/openssl.git] / crypto / evp / evp_fetch.c
1 /*
2 * Copyright 2019 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/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 */
20 #include "evp_locl.h"
21
22 static void default_method_store_free(void *vstore)
23 {
24 ossl_method_store_free(vstore);
25 }
26
27 static void *default_method_store_new(OPENSSL_CTX *ctx)
28 {
29 return ossl_method_store_new(ctx);
30 }
31
32
33 static const OPENSSL_CTX_METHOD default_method_store_method = {
34 default_method_store_new,
35 default_method_store_free,
36 };
37
38 /* Data to be passed through ossl_method_construct() */
39 struct method_data_st {
40 OPENSSL_CTX *libctx;
41 const char *name;
42 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
43 void *(*method_from_dispatch)(const char *, const OSSL_DISPATCH *,
44 OSSL_PROVIDER *);
45 int (*refcnt_up_method)(void *method);
46 void (*destruct_method)(void *method);
47 };
48
49 /*
50 * Generic routines to fetch / create EVP methods with ossl_method_construct()
51 */
52 static void *alloc_tmp_method_store(OPENSSL_CTX *ctx)
53 {
54 return ossl_method_store_new(ctx);
55 }
56
57 static void dealloc_tmp_method_store(void *store)
58 {
59 if (store != NULL)
60 ossl_method_store_free(store);
61 }
62
63 static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
64 {
65 return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX,
66 &default_method_store_method);
67 }
68
69 /*
70 * To identity the method in the method store, we mix the name identity
71 * with the operation identity, with the assumption that we don't have
72 * more than 2^24 names or more than 2^8 operation types.
73 *
74 * The resulting identity is a 32-bit integer, composed like this:
75 *
76 * +---------24 bits--------+-8 bits-+
77 * | name identity | op id |
78 * +------------------------+--------+
79 */
80 static uint32_t method_id(unsigned int operation_id, unsigned int name_id)
81 {
82 if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8))
83 || !ossl_assert(name_id > 0 && operation_id > 0))
84 return 0;
85 return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
86 }
87
88 static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
89 int operation_id, const char *name,
90 const char *propquery, void *data)
91 {
92 struct method_data_st *methdata = data;
93 void *method = NULL;
94 OSSL_NAMEMAP *namemap;
95 int nameid;
96 uint32_t methid;
97
98 if (store == NULL
99 && (store = get_default_method_store(libctx)) == NULL)
100 return NULL;
101
102 if ((namemap = ossl_namemap_stored(libctx)) == NULL
103 || (nameid = ossl_namemap_name2num(namemap, name)) == 0
104 || (methid = method_id(operation_id, nameid)) == 0)
105 return NULL;
106
107 (void)ossl_method_store_fetch(store, methid, propquery, &method);
108
109 if (method != NULL
110 && !methdata->refcnt_up_method(method)) {
111 method = NULL;
112 }
113 return method;
114 }
115
116 static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
117 void *method, int operation_id,
118 const char *name, const char *propdef,
119 void *data)
120 {
121 struct method_data_st *methdata = data;
122 OSSL_NAMEMAP *namemap;
123 int nameid;
124 uint32_t methid;
125
126 if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
127 || (nameid = ossl_namemap_add(namemap, 0, name)) == 0
128 || (methid = method_id(operation_id, nameid)) == 0)
129 return 0;
130
131 if (store == NULL
132 && (store = get_default_method_store(libctx)) == NULL)
133 return 0;
134
135 if (methdata->refcnt_up_method(method)
136 && ossl_method_store_add(store, methid, propdef, method,
137 methdata->destruct_method))
138 return 1;
139 return 0;
140 }
141
142 static void *construct_method(const char *name, const OSSL_DISPATCH *fns,
143 OSSL_PROVIDER *prov, void *data)
144 {
145 struct method_data_st *methdata = data;
146
147 return methdata->method_from_dispatch(name, fns, prov);
148 }
149
150 static void destruct_method(void *method, void *data)
151 {
152 struct method_data_st *methdata = data;
153
154 methdata->destruct_method(method);
155 }
156
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,
161 OSSL_PROVIDER *prov),
162 int (*up_ref_method)(void *),
163 void (*free_method)(void *))
164 {
165 OSSL_METHOD_STORE *store = get_default_method_store(libctx);
166 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
167 int nameid = 0;
168 uint32_t methid = 0;
169 void *method = NULL;
170
171 if (store == NULL || namemap == NULL)
172 return NULL;
173
174 /*
175 * If there's ever an operation_id == 0 passed, we have an internal
176 * programming error.
177 */
178 if (!ossl_assert(operation_id > 0))
179 return NULL;
180
181 /*
182 * method_id returns 0 if we have too many operations (more than
183 * about 2^8) or too many names (more than about 2^24). In that
184 * case, we can't create any new method.
185 */
186 if ((nameid = ossl_namemap_name2num(namemap, name)) != 0
187 && (methid = method_id(operation_id, nameid)) == 0)
188 return NULL;
189
190 if (nameid == 0
191 || !ossl_method_store_cache_get(store, methid, properties,
192 &method)) {
193 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
194 alloc_tmp_method_store,
195 dealloc_tmp_method_store,
196 get_method_from_store,
197 put_method_in_store,
198 construct_method,
199 destruct_method
200 };
201 struct method_data_st mcmdata;
202
203 mcmdata.mcm = &mcm;
204 mcmdata.libctx = libctx;
205 mcmdata.name = name;
206 mcmdata.method_from_dispatch = new_method;
207 mcmdata.destruct_method = free_method;
208 mcmdata.refcnt_up_method = up_ref_method;
209 mcmdata.destruct_method = free_method;
210 if ((method = ossl_method_construct(libctx, operation_id, name,
211 properties, 0 /* !force_cache */,
212 &mcm, &mcmdata)) != NULL) {
213 /*
214 * If construction did create a method for us, we know that
215 * there is a correct nameid and methodid, since those have
216 * already been calculated in get_method_from_store() and
217 * put_method_in_store() above.
218 */
219 nameid = ossl_namemap_name2num(namemap, name);
220 methid = method_id(operation_id, nameid);
221 ossl_method_store_cache_set(store, methid, properties, method);
222 }
223 } else {
224 up_ref_method(method);
225 }
226
227 return method;
228 }
229
230 int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
231 {
232 OSSL_METHOD_STORE *store = get_default_method_store(libctx);
233
234 if (store != NULL)
235 return ossl_method_store_set_global_properties(store, propq);
236 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
237 return 0;
238 }
239
240 struct do_all_data_st {
241 void (*user_fn)(void *method, void *arg);
242 void *user_arg;
243 void *(*new_method)(const char *name, const OSSL_DISPATCH *fns,
244 OSSL_PROVIDER *prov);
245 void (*free_method)(void *);
246 };
247
248 static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
249 int no_store, void *vdata)
250 {
251 struct do_all_data_st *data = vdata;
252 void *method = data->new_method(algo->algorithm_name,
253 algo->implementation, provider);
254
255 if (method != NULL) {
256 data->user_fn(method, data->user_arg);
257 data->free_method(method);
258 }
259 }
260
261 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
262 void (*user_fn)(void *method, void *arg),
263 void *user_arg,
264 void *(*new_method)(const char *name,
265 const OSSL_DISPATCH *fns,
266 OSSL_PROVIDER *prov),
267 void (*free_method)(void *))
268 {
269 struct do_all_data_st data;
270
271 data.new_method = new_method;
272 data.free_method = free_method;
273 data.user_fn = user_fn;
274 data.user_arg = user_arg;
275 ossl_algorithm_do_all(libctx, operation_id, NULL, do_one, &data);
276 }