]>
Commit | Line | Data |
---|---|---|
c13d2ab4 RL |
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" | |
c13d2ab4 RL |
16 | #include "internal/property.h" |
17 | #include "internal/core.h" | |
f7c16d48 | 18 | #include "internal/provider.h" |
baff732d | 19 | #include "internal/namemap.h" |
25f2138b | 20 | #include "crypto/evp.h" /* evp_locl.h needs it */ |
c13d2ab4 RL |
21 | #include "evp_locl.h" |
22 | ||
c13d2ab4 RL |
23 | static void default_method_store_free(void *vstore) |
24 | { | |
25 | ossl_method_store_free(vstore); | |
26 | } | |
27 | ||
1aedc35f | 28 | static void *default_method_store_new(OPENSSL_CTX *ctx) |
c13d2ab4 | 29 | { |
1aedc35f | 30 | return ossl_method_store_new(ctx); |
c13d2ab4 RL |
31 | } |
32 | ||
33 | ||
34 | static const OPENSSL_CTX_METHOD default_method_store_method = { | |
35 | default_method_store_new, | |
36 | default_method_store_free, | |
37 | }; | |
38 | ||
c13d2ab4 RL |
39 | /* Data to be passed through ossl_method_construct() */ |
40 | struct method_data_st { | |
baff732d | 41 | OPENSSL_CTX *libctx; |
c13d2ab4 | 42 | OSSL_METHOD_CONSTRUCT_METHOD *mcm; |
f7c16d48 RL |
43 | int operation_id; /* For get_method_from_store() */ |
44 | int name_id; /* For get_method_from_store() */ | |
45 | const char *name; /* For get_method_from_store() */ | |
46 | const char *propquery; /* For get_method_from_store() */ | |
47 | void *(*method_from_dispatch)(int name_id, const OSSL_DISPATCH *, | |
3ca9d210 RL |
48 | OSSL_PROVIDER *, void *); |
49 | void *method_data; | |
c13d2ab4 RL |
50 | int (*refcnt_up_method)(void *method); |
51 | void (*destruct_method)(void *method); | |
52 | }; | |
53 | ||
54 | /* | |
55 | * Generic routines to fetch / create EVP methods with ossl_method_construct() | |
56 | */ | |
1aedc35f | 57 | static void *alloc_tmp_method_store(OPENSSL_CTX *ctx) |
c13d2ab4 | 58 | { |
1aedc35f | 59 | return ossl_method_store_new(ctx); |
c13d2ab4 RL |
60 | } |
61 | ||
62 | static void dealloc_tmp_method_store(void *store) | |
63 | { | |
64 | if (store != NULL) | |
65 | ossl_method_store_free(store); | |
66 | } | |
67 | ||
cb929645 | 68 | static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx) |
c13d2ab4 | 69 | { |
1aedc35f MC |
70 | return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX, |
71 | &default_method_store_method); | |
c13d2ab4 RL |
72 | } |
73 | ||
2ccb1b4e RL |
74 | /* |
75 | * To identity the method in the method store, we mix the name identity | |
76 | * with the operation identity, with the assumption that we don't have | |
77 | * more than 2^24 names or more than 2^8 operation types. | |
78 | * | |
79 | * The resulting identity is a 32-bit integer, composed like this: | |
80 | * | |
81 | * +---------24 bits--------+-8 bits-+ | |
82 | * | name identity | op id | | |
83 | * +------------------------+--------+ | |
84 | */ | |
f7c16d48 | 85 | static uint32_t method_id(unsigned int operation_id, int name_id) |
2ccb1b4e RL |
86 | { |
87 | if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8)) | |
88 | || !ossl_assert(name_id > 0 && operation_id > 0)) | |
89 | return 0; | |
90 | return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF); | |
91 | } | |
92 | ||
c13d2ab4 | 93 | static void *get_method_from_store(OPENSSL_CTX *libctx, void *store, |
f7c16d48 | 94 | void *data) |
c13d2ab4 RL |
95 | { |
96 | struct method_data_st *methdata = data; | |
97 | void *method = NULL; | |
f7c16d48 RL |
98 | int name_id; |
99 | uint32_t meth_id; | |
c13d2ab4 | 100 | |
f7c16d48 RL |
101 | /* |
102 | * get_method_from_store() is only called to try and get the method | |
103 | * that evp_generic_fetch() is asking for, and the operation id as | |
104 | * well as the name or name id are passed via methdata. | |
105 | */ | |
106 | if ((name_id = methdata->name_id) == 0) { | |
107 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
108 | ||
109 | if (namemap == 0) | |
110 | return NULL; | |
111 | name_id = ossl_namemap_name2num(namemap, methdata->name); | |
112 | } | |
113 | ||
114 | if (name_id == 0 | |
115 | || (meth_id = method_id(methdata->operation_id, name_id)) == 0) | |
c13d2ab4 RL |
116 | return NULL; |
117 | ||
f7c16d48 RL |
118 | if (store == NULL |
119 | && (store = get_default_method_store(libctx)) == NULL) | |
2e49c054 RL |
120 | return NULL; |
121 | ||
f7c16d48 RL |
122 | (void)ossl_method_store_fetch(store, meth_id, methdata->propquery, |
123 | &method); | |
c13d2ab4 RL |
124 | |
125 | if (method != NULL | |
126 | && !methdata->refcnt_up_method(method)) { | |
127 | method = NULL; | |
128 | } | |
129 | return method; | |
130 | } | |
131 | ||
132 | static int put_method_in_store(OPENSSL_CTX *libctx, void *store, | |
c1d56231 RL |
133 | void *method, const OSSL_PROVIDER *prov, |
134 | int operation_id, const char *name, | |
135 | const char *propdef, void *data) | |
c13d2ab4 RL |
136 | { |
137 | struct method_data_st *methdata = data; | |
2e49c054 | 138 | OSSL_NAMEMAP *namemap; |
f7c16d48 RL |
139 | int name_id; |
140 | uint32_t meth_id; | |
dc46e3dd | 141 | |
f7c16d48 RL |
142 | /* |
143 | * put_method_in_store() is only called with a method that was | |
144 | * successfully created by construct_method() below, which means | |
145 | * the name should already be stored in the namemap, so just use it. | |
146 | */ | |
147 | if ((namemap = ossl_namemap_stored(libctx)) == NULL | |
148 | || (name_id = ossl_namemap_name2num(namemap, name)) == 0 | |
149 | || (meth_id = method_id(operation_id, name_id)) == 0) | |
dc46e3dd | 150 | return 0; |
c13d2ab4 RL |
151 | |
152 | if (store == NULL | |
153 | && (store = get_default_method_store(libctx)) == NULL) | |
154 | return 0; | |
155 | ||
f7c16d48 | 156 | return ossl_method_store_add(store, prov, meth_id, propdef, method, |
b1d40ddf RL |
157 | methdata->refcnt_up_method, |
158 | methdata->destruct_method); | |
c13d2ab4 RL |
159 | } |
160 | ||
f7c16d48 RL |
161 | /* |
162 | * The core fetching functionality passes the name of the implementation. | |
163 | * This function is responsible to getting an identity number for it. | |
164 | */ | |
2e49c054 RL |
165 | static void *construct_method(const char *name, const OSSL_DISPATCH *fns, |
166 | OSSL_PROVIDER *prov, void *data) | |
c13d2ab4 | 167 | { |
f7c16d48 RL |
168 | /* |
169 | * This function is only called if get_method_from_store() returned | |
170 | * NULL, so it's safe to say that of all the spots to create a new | |
171 | * namemap entry, this is it. Should the name already exist there, we | |
172 | * know that ossl_namemap_add() will return its corresponding number. | |
173 | * | |
174 | * TODO(3.0): If this function gets an array of names instead of just | |
175 | * one, we need to check through all the names to see if at least one | |
176 | * of them has an associated number, and use that. If several names | |
177 | * have associated numbers that differ from each other, it's an error. | |
178 | */ | |
c13d2ab4 | 179 | struct method_data_st *methdata = data; |
f7c16d48 RL |
180 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); |
181 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
182 | int name_id = ossl_namemap_add(namemap, 0, name); | |
c13d2ab4 | 183 | |
f7c16d48 | 184 | return methdata->method_from_dispatch(name_id, fns, prov, |
3ca9d210 | 185 | methdata->method_data); |
c13d2ab4 RL |
186 | } |
187 | ||
188 | static void destruct_method(void *method, void *data) | |
189 | { | |
190 | struct method_data_st *methdata = data; | |
191 | ||
192 | methdata->destruct_method(method); | |
193 | } | |
194 | ||
f7c16d48 RL |
195 | static void *inner_generic_fetch(OPENSSL_CTX *libctx, int operation_id, |
196 | int name_id, const char *name, | |
197 | const char *properties, | |
198 | void *(*new_method)(int name_id, | |
199 | const OSSL_DISPATCH *fns, | |
200 | OSSL_PROVIDER *prov, | |
201 | void *method_data), | |
202 | void *method_data, | |
203 | int (*up_ref_method)(void *), | |
204 | void (*free_method)(void *)) | |
c13d2ab4 | 205 | { |
e019da7b | 206 | OSSL_METHOD_STORE *store = get_default_method_store(libctx); |
baff732d | 207 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
f7c16d48 | 208 | uint32_t meth_id = 0; |
c13d2ab4 RL |
209 | void *method = NULL; |
210 | ||
baff732d | 211 | if (store == NULL || namemap == NULL) |
e019da7b RL |
212 | return NULL; |
213 | ||
2ccb1b4e RL |
214 | /* |
215 | * If there's ever an operation_id == 0 passed, we have an internal | |
216 | * programming error. | |
217 | */ | |
218 | if (!ossl_assert(operation_id > 0)) | |
219 | return NULL; | |
220 | ||
221 | /* | |
f7c16d48 RL |
222 | * If we have been passed neither a name_id or a name, we have an |
223 | * internal programming error. | |
224 | */ | |
225 | if (!ossl_assert(name_id != 0 || name != NULL)) | |
226 | return NULL; | |
227 | ||
228 | /* If we haven't received a name id yet, try to get one for the name */ | |
229 | if (name_id == 0) | |
230 | name_id = ossl_namemap_name2num(namemap, name); | |
231 | ||
232 | /* | |
233 | * If we have a name id, calculate a method id with method_id(). | |
234 | * | |
2ccb1b4e RL |
235 | * method_id returns 0 if we have too many operations (more than |
236 | * about 2^8) or too many names (more than about 2^24). In that | |
237 | * case, we can't create any new method. | |
238 | */ | |
f7c16d48 | 239 | if (name_id != 0 && (meth_id = method_id(operation_id, name_id)) == 0) |
2ccb1b4e RL |
240 | return NULL; |
241 | ||
f7c16d48 RL |
242 | if (meth_id == 0 |
243 | || !ossl_method_store_cache_get(store, meth_id, properties, &method)) { | |
c13d2ab4 RL |
244 | OSSL_METHOD_CONSTRUCT_METHOD mcm = { |
245 | alloc_tmp_method_store, | |
246 | dealloc_tmp_method_store, | |
247 | get_method_from_store, | |
248 | put_method_in_store, | |
249 | construct_method, | |
250 | destruct_method | |
251 | }; | |
252 | struct method_data_st mcmdata; | |
253 | ||
c13d2ab4 | 254 | mcmdata.mcm = &mcm; |
baff732d | 255 | mcmdata.libctx = libctx; |
f7c16d48 RL |
256 | mcmdata.operation_id = operation_id; |
257 | mcmdata.name_id = name_id; | |
d5e5e2ff | 258 | mcmdata.name = name; |
f7c16d48 | 259 | mcmdata.propquery = properties; |
c13d2ab4 RL |
260 | mcmdata.method_from_dispatch = new_method; |
261 | mcmdata.destruct_method = free_method; | |
7c95390e | 262 | mcmdata.refcnt_up_method = up_ref_method; |
c13d2ab4 | 263 | mcmdata.destruct_method = free_method; |
3ca9d210 | 264 | mcmdata.method_data = method_data; |
f7c16d48 RL |
265 | if ((method = ossl_method_construct(libctx, operation_id, |
266 | 0 /* !force_cache */, | |
08607613 | 267 | &mcm, &mcmdata)) != NULL) { |
2ccb1b4e RL |
268 | /* |
269 | * If construction did create a method for us, we know that | |
f7c16d48 | 270 | * there is a correct name_id and methodid, since those have |
2ccb1b4e RL |
271 | * already been calculated in get_method_from_store() and |
272 | * put_method_in_store() above. | |
273 | */ | |
f7c16d48 RL |
274 | if (name_id == 0) |
275 | name_id = ossl_namemap_name2num(namemap, name); | |
276 | meth_id = method_id(operation_id, name_id); | |
277 | ossl_method_store_cache_set(store, meth_id, properties, method); | |
2ccb1b4e | 278 | } |
e019da7b | 279 | } else { |
7c95390e | 280 | up_ref_method(method); |
c13d2ab4 RL |
281 | } |
282 | ||
283 | return method; | |
284 | } | |
cb929645 | 285 | |
f7c16d48 RL |
286 | void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id, |
287 | const char *name, const char *properties, | |
288 | void *(*new_method)(int name_id, | |
289 | const OSSL_DISPATCH *fns, | |
290 | OSSL_PROVIDER *prov, | |
291 | void *method_data), | |
292 | void *method_data, | |
293 | int (*up_ref_method)(void *), | |
294 | void (*free_method)(void *)) | |
295 | { | |
296 | return inner_generic_fetch(libctx, | |
297 | operation_id, 0, name, properties, | |
298 | new_method, method_data, | |
299 | up_ref_method, free_method); | |
300 | } | |
301 | ||
302 | /* | |
303 | * evp_generic_fetch_by_number() is special, and only returns methods for | |
304 | * already known names, i.e. it refuses to work if no name_id can be found | |
305 | * (it's considered an internal programming error). | |
306 | * This is meant to be used when one method needs to fetch an associated | |
307 | * other method. | |
308 | */ | |
309 | void *evp_generic_fetch_by_number(OPENSSL_CTX *libctx, int operation_id, | |
310 | int name_id, const char *properties, | |
311 | void *(*new_method)(int name_id, | |
312 | const OSSL_DISPATCH *fns, | |
313 | OSSL_PROVIDER *prov, | |
314 | void *method_data), | |
315 | void *method_data, | |
316 | int (*up_ref_method)(void *), | |
317 | void (*free_method)(void *)) | |
318 | { | |
319 | return inner_generic_fetch(libctx, | |
320 | operation_id, name_id, NULL, properties, | |
321 | new_method, method_data, | |
322 | up_ref_method, free_method); | |
323 | } | |
324 | ||
cb929645 RL |
325 | int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq) |
326 | { | |
327 | OSSL_METHOD_STORE *store = get_default_method_store(libctx); | |
328 | ||
329 | if (store != NULL) | |
330 | return ossl_method_store_set_global_properties(store, propq); | |
331 | EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR); | |
332 | return 0; | |
333 | } | |
3d96a51c RL |
334 | |
335 | struct do_all_data_st { | |
336 | void (*user_fn)(void *method, void *arg); | |
337 | void *user_arg; | |
f7c16d48 | 338 | void *(*new_method)(const int name_id, const OSSL_DISPATCH *fns, |
3ca9d210 | 339 | OSSL_PROVIDER *prov, void *method_data); |
3d96a51c RL |
340 | void (*free_method)(void *); |
341 | }; | |
342 | ||
343 | static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo, | |
344 | int no_store, void *vdata) | |
345 | { | |
346 | struct do_all_data_st *data = vdata; | |
f7c16d48 RL |
347 | OPENSSL_CTX *libctx = ossl_provider_library_context(provider); |
348 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
349 | int name_id = ossl_namemap_add(namemap, 0, algo->algorithm_name); | |
350 | void *method = NULL; | |
351 | ||
352 | if (name_id != 0) | |
353 | method = data->new_method(name_id, algo->implementation, provider, | |
354 | NULL); | |
3d96a51c RL |
355 | |
356 | if (method != NULL) { | |
357 | data->user_fn(method, data->user_arg); | |
358 | data->free_method(method); | |
359 | } | |
360 | } | |
361 | ||
362 | void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id, | |
363 | void (*user_fn)(void *method, void *arg), | |
364 | void *user_arg, | |
f7c16d48 | 365 | void *(*new_method)(int name_id, |
3d96a51c | 366 | const OSSL_DISPATCH *fns, |
3ca9d210 RL |
367 | OSSL_PROVIDER *prov, |
368 | void *method_data), | |
369 | void *method_data, | |
3d96a51c RL |
370 | void (*free_method)(void *)) |
371 | { | |
372 | struct do_all_data_st data; | |
373 | ||
374 | data.new_method = new_method; | |
375 | data.free_method = free_method; | |
376 | data.user_fn = user_fn; | |
377 | data.user_arg = user_arg; | |
3ca9d210 | 378 | ossl_algorithm_do_all(libctx, operation_id, method_data, do_one, &data); |
3d96a51c | 379 | } |
f7c16d48 RL |
380 | |
381 | const char *evp_first_name(OSSL_PROVIDER *prov, int name_id) | |
382 | { | |
383 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); | |
384 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
385 | ||
386 | return ossl_namemap_num2name(namemap, name_id, 0); | |
387 | } | |
7cfa1717 RL |
388 | |
389 | int evp_is_a(OSSL_PROVIDER *prov, int number, const char *name) | |
390 | { | |
391 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); | |
392 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
393 | ||
394 | return ossl_namemap_name2num(namemap, name) == number; | |
395 | } | |
32040838 RL |
396 | |
397 | void evp_doall_names(OSSL_PROVIDER *prov, int number, | |
398 | void (*fn)(const char *name, void *data), | |
399 | void *data) | |
400 | { | |
401 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); | |
402 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
403 | ||
404 | ossl_namemap_doall_names(namemap, number, fn, data); | |
405 | } |