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