]>
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> | |
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 | ||
c13d2ab4 RL |
25 | static void default_method_store_free(void *vstore) |
26 | { | |
27 | ossl_method_store_free(vstore); | |
28 | } | |
29 | ||
1aedc35f | 30 | static void *default_method_store_new(OPENSSL_CTX *ctx) |
c13d2ab4 | 31 | { |
1aedc35f | 32 | return ossl_method_store_new(ctx); |
c13d2ab4 RL |
33 | } |
34 | ||
35 | ||
36 | static const OPENSSL_CTX_METHOD default_method_store_method = { | |
37 | default_method_store_new, | |
38 | default_method_store_free, | |
39 | }; | |
40 | ||
c13d2ab4 RL |
41 | /* Data to be passed through ossl_method_construct() */ |
42 | struct method_data_st { | |
baff732d | 43 | OPENSSL_CTX *libctx; |
c13d2ab4 | 44 | OSSL_METHOD_CONSTRUCT_METHOD *mcm; |
f7c16d48 RL |
45 | int operation_id; /* For get_method_from_store() */ |
46 | int name_id; /* For get_method_from_store() */ | |
695d195b | 47 | const char *names; /* For get_method_from_store() */ |
f7c16d48 RL |
48 | const char *propquery; /* For get_method_from_store() */ |
49 | void *(*method_from_dispatch)(int name_id, const OSSL_DISPATCH *, | |
3ca9d210 RL |
50 | OSSL_PROVIDER *, void *); |
51 | void *method_data; | |
c13d2ab4 RL |
52 | int (*refcnt_up_method)(void *method); |
53 | void (*destruct_method)(void *method); | |
54 | }; | |
55 | ||
695d195b RL |
56 | static int add_names_to_namemap(OSSL_NAMEMAP *namemap, |
57 | const char *names) | |
58 | { | |
59 | const char *p, *q; | |
60 | size_t l; | |
61 | int id = 0; | |
62 | ||
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); | |
66 | return 0; | |
67 | } | |
68 | ||
69 | /* | |
70 | * Check that no name is an empty string, and that all names have at | |
71 | * most one numeric identity together. | |
72 | */ | |
73 | for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) { | |
74 | int this_id; | |
75 | ||
76 | if ((q = strchr(p, NAME_SEPARATOR)) == NULL) | |
77 | l = strlen(p); /* offset to \0 */ | |
78 | else | |
79 | l = q - p; /* offset to the next separator */ | |
80 | ||
81 | this_id = ossl_namemap_name2num_n(namemap, p, l); | |
82 | ||
83 | if (*p == '\0' || *p == NAME_SEPARATOR) { | |
84 | ERR_raise(ERR_LIB_EVP, EVP_R_BAD_ALGORITHM_NAME); | |
85 | return 0; | |
86 | } | |
87 | if (id == 0) | |
88 | id = this_id; | |
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); | |
93 | return 0; | |
94 | } | |
95 | } | |
96 | ||
97 | /* Now that we have checked, register all names */ | |
98 | for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) { | |
99 | int this_id; | |
100 | ||
101 | if ((q = strchr(p, NAME_SEPARATOR)) == NULL) | |
102 | l = strlen(p); /* offset to \0 */ | |
103 | else | |
104 | l = q - p; /* offset to the next separator */ | |
105 | ||
106 | this_id = ossl_namemap_add_n(namemap, id, p, l); | |
107 | if (id == 0) | |
108 | id = this_id; | |
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); | |
112 | return 0; | |
113 | } | |
114 | } | |
115 | ||
116 | return id; | |
117 | } | |
118 | ||
c13d2ab4 RL |
119 | /* |
120 | * Generic routines to fetch / create EVP methods with ossl_method_construct() | |
121 | */ | |
1aedc35f | 122 | static void *alloc_tmp_method_store(OPENSSL_CTX *ctx) |
c13d2ab4 | 123 | { |
1aedc35f | 124 | return ossl_method_store_new(ctx); |
c13d2ab4 RL |
125 | } |
126 | ||
127 | static void dealloc_tmp_method_store(void *store) | |
128 | { | |
129 | if (store != NULL) | |
130 | ossl_method_store_free(store); | |
131 | } | |
132 | ||
cb929645 | 133 | static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx) |
c13d2ab4 | 134 | { |
1aedc35f MC |
135 | return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX, |
136 | &default_method_store_method); | |
c13d2ab4 RL |
137 | } |
138 | ||
2ccb1b4e RL |
139 | /* |
140 | * To identity the method in the method store, we mix the name identity | |
141 | * with the operation identity, with the assumption that we don't have | |
142 | * more than 2^24 names or more than 2^8 operation types. | |
143 | * | |
144 | * The resulting identity is a 32-bit integer, composed like this: | |
145 | * | |
146 | * +---------24 bits--------+-8 bits-+ | |
147 | * | name identity | op id | | |
148 | * +------------------------+--------+ | |
149 | */ | |
f7c16d48 | 150 | static uint32_t method_id(unsigned int operation_id, int name_id) |
2ccb1b4e RL |
151 | { |
152 | if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8)) | |
153 | || !ossl_assert(name_id > 0 && operation_id > 0)) | |
154 | return 0; | |
155 | return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF); | |
156 | } | |
157 | ||
c13d2ab4 | 158 | static void *get_method_from_store(OPENSSL_CTX *libctx, void *store, |
f7c16d48 | 159 | void *data) |
c13d2ab4 RL |
160 | { |
161 | struct method_data_st *methdata = data; | |
162 | void *method = NULL; | |
f7c16d48 RL |
163 | int name_id; |
164 | uint32_t meth_id; | |
c13d2ab4 | 165 | |
f7c16d48 RL |
166 | /* |
167 | * get_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 | |
169 | * well as the name or name id are passed via methdata. | |
170 | */ | |
171 | if ((name_id = methdata->name_id) == 0) { | |
172 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
695d195b RL |
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)); | |
f7c16d48 RL |
176 | |
177 | if (namemap == 0) | |
178 | return NULL; | |
695d195b | 179 | name_id = ossl_namemap_name2num_n(namemap, names, l); |
f7c16d48 RL |
180 | } |
181 | ||
182 | if (name_id == 0 | |
183 | || (meth_id = method_id(methdata->operation_id, name_id)) == 0) | |
c13d2ab4 RL |
184 | return NULL; |
185 | ||
f7c16d48 RL |
186 | if (store == NULL |
187 | && (store = get_default_method_store(libctx)) == NULL) | |
2e49c054 RL |
188 | return NULL; |
189 | ||
f7c16d48 RL |
190 | (void)ossl_method_store_fetch(store, meth_id, methdata->propquery, |
191 | &method); | |
c13d2ab4 RL |
192 | |
193 | if (method != NULL | |
194 | && !methdata->refcnt_up_method(method)) { | |
195 | method = NULL; | |
196 | } | |
197 | return method; | |
198 | } | |
199 | ||
200 | static int put_method_in_store(OPENSSL_CTX *libctx, void *store, | |
c1d56231 | 201 | void *method, const OSSL_PROVIDER *prov, |
695d195b | 202 | int operation_id, const char *names, |
c1d56231 | 203 | const char *propdef, void *data) |
c13d2ab4 RL |
204 | { |
205 | struct method_data_st *methdata = data; | |
2e49c054 | 206 | OSSL_NAMEMAP *namemap; |
f7c16d48 RL |
207 | int name_id; |
208 | uint32_t meth_id; | |
695d195b | 209 | size_t l = 0; |
dc46e3dd | 210 | |
f7c16d48 RL |
211 | /* |
212 | * put_method_in_store() is only called with a method that was | |
213 | * successfully created by construct_method() below, which means | |
695d195b RL |
214 | * that all the names should already be stored in the namemap with |
215 | * the same numeric identity, so just use the first to get that | |
216 | * identity. | |
f7c16d48 | 217 | */ |
695d195b RL |
218 | if (names != NULL) { |
219 | const char *q = strchr(names, NAME_SEPARATOR); | |
220 | ||
221 | l = (q == NULL ? strlen(names) : (size_t)(q - names)); | |
222 | } | |
223 | ||
f7c16d48 | 224 | if ((namemap = ossl_namemap_stored(libctx)) == NULL |
695d195b | 225 | || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0 |
f7c16d48 | 226 | || (meth_id = method_id(operation_id, name_id)) == 0) |
dc46e3dd | 227 | return 0; |
c13d2ab4 RL |
228 | |
229 | if (store == NULL | |
230 | && (store = get_default_method_store(libctx)) == NULL) | |
231 | return 0; | |
232 | ||
f7c16d48 | 233 | return ossl_method_store_add(store, prov, meth_id, propdef, method, |
b1d40ddf RL |
234 | methdata->refcnt_up_method, |
235 | methdata->destruct_method); | |
c13d2ab4 RL |
236 | } |
237 | ||
f7c16d48 RL |
238 | /* |
239 | * The core fetching functionality passes the name of the implementation. | |
240 | * This function is responsible to getting an identity number for it. | |
241 | */ | |
695d195b | 242 | static void *construct_method(const char *names, const OSSL_DISPATCH *fns, |
2e49c054 | 243 | OSSL_PROVIDER *prov, void *data) |
c13d2ab4 | 244 | { |
f7c16d48 RL |
245 | /* |
246 | * This function is only called if get_method_from_store() returned | |
247 | * NULL, so it's safe to say that of all the spots to create a new | |
248 | * namemap entry, this is it. Should the name already exist there, we | |
249 | * know that ossl_namemap_add() will return its corresponding number. | |
f7c16d48 | 250 | */ |
c13d2ab4 | 251 | struct method_data_st *methdata = data; |
f7c16d48 RL |
252 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); |
253 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
695d195b | 254 | int name_id = add_names_to_namemap(namemap, names); |
c13d2ab4 | 255 | |
695d195b RL |
256 | if (name_id == 0) |
257 | return NULL; | |
f7c16d48 | 258 | return methdata->method_from_dispatch(name_id, fns, prov, |
3ca9d210 | 259 | methdata->method_data); |
c13d2ab4 RL |
260 | } |
261 | ||
262 | static void destruct_method(void *method, void *data) | |
263 | { | |
264 | struct method_data_st *methdata = data; | |
265 | ||
266 | methdata->destruct_method(method); | |
267 | } | |
268 | ||
f7c16d48 RL |
269 | static void *inner_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, | |
274 | OSSL_PROVIDER *prov, | |
275 | void *method_data), | |
276 | void *method_data, | |
277 | int (*up_ref_method)(void *), | |
278 | void (*free_method)(void *)) | |
c13d2ab4 | 279 | { |
e019da7b | 280 | OSSL_METHOD_STORE *store = get_default_method_store(libctx); |
baff732d | 281 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); |
f7c16d48 | 282 | uint32_t meth_id = 0; |
c13d2ab4 RL |
283 | void *method = NULL; |
284 | ||
baff732d | 285 | if (store == NULL || namemap == NULL) |
e019da7b RL |
286 | return NULL; |
287 | ||
2ccb1b4e RL |
288 | /* |
289 | * If there's ever an operation_id == 0 passed, we have an internal | |
290 | * programming error. | |
291 | */ | |
292 | if (!ossl_assert(operation_id > 0)) | |
293 | return NULL; | |
294 | ||
295 | /* | |
f7c16d48 RL |
296 | * If we have been passed neither a name_id or a name, we have an |
297 | * internal programming error. | |
298 | */ | |
299 | if (!ossl_assert(name_id != 0 || name != NULL)) | |
300 | return NULL; | |
301 | ||
302 | /* If we haven't received a name id yet, try to get one for the name */ | |
303 | if (name_id == 0) | |
304 | name_id = ossl_namemap_name2num(namemap, name); | |
305 | ||
306 | /* | |
307 | * If we have a name id, calculate a method id with method_id(). | |
308 | * | |
2ccb1b4e RL |
309 | * 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 | |
311 | * case, we can't create any new method. | |
312 | */ | |
f7c16d48 | 313 | if (name_id != 0 && (meth_id = method_id(operation_id, name_id)) == 0) |
2ccb1b4e RL |
314 | return NULL; |
315 | ||
f7c16d48 RL |
316 | if (meth_id == 0 |
317 | || !ossl_method_store_cache_get(store, meth_id, properties, &method)) { | |
c13d2ab4 RL |
318 | OSSL_METHOD_CONSTRUCT_METHOD mcm = { |
319 | alloc_tmp_method_store, | |
320 | dealloc_tmp_method_store, | |
321 | get_method_from_store, | |
322 | put_method_in_store, | |
323 | construct_method, | |
324 | destruct_method | |
325 | }; | |
326 | struct method_data_st mcmdata; | |
327 | ||
c13d2ab4 | 328 | mcmdata.mcm = &mcm; |
baff732d | 329 | mcmdata.libctx = libctx; |
f7c16d48 RL |
330 | mcmdata.operation_id = operation_id; |
331 | mcmdata.name_id = name_id; | |
695d195b | 332 | mcmdata.names = name; |
f7c16d48 | 333 | mcmdata.propquery = properties; |
c13d2ab4 RL |
334 | mcmdata.method_from_dispatch = new_method; |
335 | mcmdata.destruct_method = free_method; | |
7c95390e | 336 | mcmdata.refcnt_up_method = up_ref_method; |
c13d2ab4 | 337 | mcmdata.destruct_method = free_method; |
3ca9d210 | 338 | mcmdata.method_data = method_data; |
f7c16d48 RL |
339 | if ((method = ossl_method_construct(libctx, operation_id, |
340 | 0 /* !force_cache */, | |
08607613 | 341 | &mcm, &mcmdata)) != NULL) { |
2ccb1b4e RL |
342 | /* |
343 | * If construction did create a method for us, we know that | |
f7c16d48 | 344 | * there is a correct name_id and methodid, since those have |
2ccb1b4e RL |
345 | * already been calculated in get_method_from_store() and |
346 | * put_method_in_store() above. | |
347 | */ | |
f7c16d48 RL |
348 | if (name_id == 0) |
349 | name_id = ossl_namemap_name2num(namemap, name); | |
350 | meth_id = method_id(operation_id, name_id); | |
351 | ossl_method_store_cache_set(store, meth_id, properties, method); | |
2ccb1b4e | 352 | } |
e019da7b | 353 | } else { |
7c95390e | 354 | up_ref_method(method); |
c13d2ab4 RL |
355 | } |
356 | ||
357 | return method; | |
358 | } | |
cb929645 | 359 | |
f7c16d48 RL |
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, | |
364 | OSSL_PROVIDER *prov, | |
365 | void *method_data), | |
366 | void *method_data, | |
367 | int (*up_ref_method)(void *), | |
368 | void (*free_method)(void *)) | |
369 | { | |
370 | return inner_generic_fetch(libctx, | |
371 | operation_id, 0, name, properties, | |
372 | new_method, method_data, | |
373 | up_ref_method, free_method); | |
374 | } | |
375 | ||
376 | /* | |
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 | |
381 | * other method. | |
382 | */ | |
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, | |
387 | OSSL_PROVIDER *prov, | |
388 | void *method_data), | |
389 | void *method_data, | |
390 | int (*up_ref_method)(void *), | |
391 | void (*free_method)(void *)) | |
392 | { | |
393 | return inner_generic_fetch(libctx, | |
394 | operation_id, name_id, NULL, properties, | |
395 | new_method, method_data, | |
396 | up_ref_method, free_method); | |
397 | } | |
398 | ||
cb929645 RL |
399 | int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq) |
400 | { | |
401 | OSSL_METHOD_STORE *store = get_default_method_store(libctx); | |
402 | ||
403 | if (store != NULL) | |
404 | return ossl_method_store_set_global_properties(store, propq); | |
405 | EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR); | |
406 | return 0; | |
407 | } | |
3d96a51c RL |
408 | |
409 | struct do_all_data_st { | |
410 | void (*user_fn)(void *method, void *arg); | |
411 | void *user_arg; | |
f7c16d48 | 412 | void *(*new_method)(const int name_id, const OSSL_DISPATCH *fns, |
3ca9d210 | 413 | OSSL_PROVIDER *prov, void *method_data); |
3d96a51c RL |
414 | void (*free_method)(void *); |
415 | }; | |
416 | ||
417 | static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo, | |
418 | int no_store, void *vdata) | |
419 | { | |
420 | struct do_all_data_st *data = vdata; | |
f7c16d48 RL |
421 | OPENSSL_CTX *libctx = ossl_provider_library_context(provider); |
422 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
695d195b | 423 | int name_id = add_names_to_namemap(namemap, algo->algorithm_names); |
f7c16d48 RL |
424 | void *method = NULL; |
425 | ||
426 | if (name_id != 0) | |
427 | method = data->new_method(name_id, algo->implementation, provider, | |
428 | NULL); | |
3d96a51c RL |
429 | |
430 | if (method != NULL) { | |
431 | data->user_fn(method, data->user_arg); | |
432 | data->free_method(method); | |
433 | } | |
434 | } | |
435 | ||
436 | void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id, | |
437 | void (*user_fn)(void *method, void *arg), | |
438 | void *user_arg, | |
f7c16d48 | 439 | void *(*new_method)(int name_id, |
3d96a51c | 440 | const OSSL_DISPATCH *fns, |
3ca9d210 RL |
441 | OSSL_PROVIDER *prov, |
442 | void *method_data), | |
443 | void *method_data, | |
3d96a51c RL |
444 | void (*free_method)(void *)) |
445 | { | |
446 | struct do_all_data_st data; | |
447 | ||
448 | data.new_method = new_method; | |
449 | data.free_method = free_method; | |
450 | data.user_fn = user_fn; | |
451 | data.user_arg = user_arg; | |
3ca9d210 | 452 | ossl_algorithm_do_all(libctx, operation_id, method_data, do_one, &data); |
3d96a51c | 453 | } |
f7c16d48 RL |
454 | |
455 | const char *evp_first_name(OSSL_PROVIDER *prov, int name_id) | |
456 | { | |
457 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); | |
458 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
459 | ||
460 | return ossl_namemap_num2name(namemap, name_id, 0); | |
461 | } | |
7cfa1717 RL |
462 | |
463 | int evp_is_a(OSSL_PROVIDER *prov, int number, const char *name) | |
464 | { | |
465 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); | |
466 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
467 | ||
468 | return ossl_namemap_name2num(namemap, name) == number; | |
469 | } | |
32040838 | 470 | |
f651c727 RL |
471 | void evp_names_do_all(OSSL_PROVIDER *prov, int number, |
472 | void (*fn)(const char *name, void *data), | |
473 | void *data) | |
32040838 RL |
474 | { |
475 | OPENSSL_CTX *libctx = ossl_provider_library_context(prov); | |
476 | OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx); | |
477 | ||
478 | ossl_namemap_doall_names(namemap, number, fn, data); | |
479 | } |