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/provider.h"
19 #include "internal/namemap.h"
20 #include "internal/evp_int.h" /* evp_locl.h needs it */
23 static void default_method_store_free(void *vstore
)
25 ossl_method_store_free(vstore
);
28 static void *default_method_store_new(OPENSSL_CTX
*ctx
)
30 return ossl_method_store_new(ctx
);
34 static const OPENSSL_CTX_METHOD default_method_store_method
= {
35 default_method_store_new
,
36 default_method_store_free
,
39 /* Data to be passed through ossl_method_construct() */
40 struct method_data_st
{
42 OSSL_METHOD_CONSTRUCT_METHOD
*mcm
;
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
*,
48 OSSL_PROVIDER
*, void *);
50 int (*refcnt_up_method
)(void *method
);
51 void (*destruct_method
)(void *method
);
55 * Generic routines to fetch / create EVP methods with ossl_method_construct()
57 static void *alloc_tmp_method_store(OPENSSL_CTX
*ctx
)
59 return ossl_method_store_new(ctx
);
62 static void dealloc_tmp_method_store(void *store
)
65 ossl_method_store_free(store
);
68 static OSSL_METHOD_STORE
*get_default_method_store(OPENSSL_CTX
*libctx
)
70 return openssl_ctx_get_data(libctx
, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX
,
71 &default_method_store_method
);
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.
79 * The resulting identity is a 32-bit integer, composed like this:
81 * +---------24 bits--------+-8 bits-+
82 * | name identity | op id |
83 * +------------------------+--------+
85 static uint32_t method_id(unsigned int operation_id
, int name_id
)
87 if (!ossl_assert(name_id
< (1 << 24) || operation_id
< (1 << 8))
88 || !ossl_assert(name_id
> 0 && operation_id
> 0))
90 return ((name_id
<< 8) & 0xFFFFFF00) | (operation_id
& 0x000000FF);
93 static void *get_method_from_store(OPENSSL_CTX
*libctx
, void *store
,
96 struct method_data_st
*methdata
= data
;
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.
106 if ((name_id
= methdata
->name_id
) == 0) {
107 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
111 name_id
= ossl_namemap_name2num(namemap
, methdata
->name
);
115 || (meth_id
= method_id(methdata
->operation_id
, name_id
)) == 0)
119 && (store
= get_default_method_store(libctx
)) == NULL
)
122 (void)ossl_method_store_fetch(store
, meth_id
, methdata
->propquery
,
126 && !methdata
->refcnt_up_method(method
)) {
132 static int put_method_in_store(OPENSSL_CTX
*libctx
, void *store
,
133 void *method
, const OSSL_PROVIDER
*prov
,
134 int operation_id
, const char *name
,
135 const char *propdef
, void *data
)
137 struct method_data_st
*methdata
= data
;
138 OSSL_NAMEMAP
*namemap
;
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.
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)
153 && (store
= get_default_method_store(libctx
)) == NULL
)
156 return ossl_method_store_add(store
, prov
, meth_id
, propdef
, method
,
157 methdata
->refcnt_up_method
,
158 methdata
->destruct_method
);
162 * The core fetching functionality passes the name of the implementation.
163 * This function is responsible to getting an identity number for it.
165 static void *construct_method(const char *name
, const OSSL_DISPATCH
*fns
,
166 OSSL_PROVIDER
*prov
, void *data
)
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.
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.
179 struct method_data_st
*methdata
= data
;
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
);
184 return methdata
->method_from_dispatch(name_id
, fns
, prov
,
185 methdata
->method_data
);
188 static void destruct_method(void *method
, void *data
)
190 struct method_data_st
*methdata
= data
;
192 methdata
->destruct_method(method
);
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
,
203 int (*up_ref_method
)(void *),
204 void (*free_method
)(void *))
206 OSSL_METHOD_STORE
*store
= get_default_method_store(libctx
);
207 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
208 uint32_t meth_id
= 0;
211 if (store
== NULL
|| namemap
== NULL
)
215 * If there's ever an operation_id == 0 passed, we have an internal
218 if (!ossl_assert(operation_id
> 0))
222 * If we have been passed neither a name_id or a name, we have an
223 * internal programming error.
225 if (!ossl_assert(name_id
!= 0 || name
!= NULL
))
228 /* If we haven't received a name id yet, try to get one for the name */
230 name_id
= ossl_namemap_name2num(namemap
, name
);
233 * If we have a name id, calculate a method id with method_id().
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.
239 if (name_id
!= 0 && (meth_id
= method_id(operation_id
, name_id
)) == 0)
243 || !ossl_method_store_cache_get(store
, meth_id
, properties
, &method
)) {
244 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
245 alloc_tmp_method_store
,
246 dealloc_tmp_method_store
,
247 get_method_from_store
,
252 struct method_data_st mcmdata
;
255 mcmdata
.libctx
= libctx
;
256 mcmdata
.operation_id
= operation_id
;
257 mcmdata
.name_id
= name_id
;
259 mcmdata
.propquery
= properties
;
260 mcmdata
.method_from_dispatch
= new_method
;
261 mcmdata
.destruct_method
= free_method
;
262 mcmdata
.refcnt_up_method
= up_ref_method
;
263 mcmdata
.destruct_method
= free_method
;
264 mcmdata
.method_data
= method_data
;
265 if ((method
= ossl_method_construct(libctx
, operation_id
,
266 0 /* !force_cache */,
267 &mcm
, &mcmdata
)) != NULL
) {
269 * If construction did create a method for us, we know that
270 * there is a correct name_id and methodid, since those have
271 * already been calculated in get_method_from_store() and
272 * put_method_in_store() above.
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
);
280 up_ref_method(method
);
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
,
293 int (*up_ref_method
)(void *),
294 void (*free_method
)(void *))
296 return inner_generic_fetch(libctx
,
297 operation_id
, 0, name
, properties
,
298 new_method
, method_data
,
299 up_ref_method
, free_method
);
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
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
,
316 int (*up_ref_method
)(void *),
317 void (*free_method
)(void *))
319 return inner_generic_fetch(libctx
,
320 operation_id
, name_id
, NULL
, properties
,
321 new_method
, method_data
,
322 up_ref_method
, free_method
);
325 int EVP_set_default_properties(OPENSSL_CTX
*libctx
, const char *propq
)
327 OSSL_METHOD_STORE
*store
= get_default_method_store(libctx
);
330 return ossl_method_store_set_global_properties(store
, propq
);
331 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES
, ERR_R_INTERNAL_ERROR
);
335 struct do_all_data_st
{
336 void (*user_fn
)(void *method
, void *arg
);
338 void *(*new_method
)(const int name_id
, const OSSL_DISPATCH
*fns
,
339 OSSL_PROVIDER
*prov
, void *method_data
);
340 void (*free_method
)(void *);
343 static void do_one(OSSL_PROVIDER
*provider
, const OSSL_ALGORITHM
*algo
,
344 int no_store
, void *vdata
)
346 struct do_all_data_st
*data
= vdata
;
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
);
353 method
= data
->new_method(name_id
, algo
->implementation
, provider
,
356 if (method
!= NULL
) {
357 data
->user_fn(method
, data
->user_arg
);
358 data
->free_method(method
);
362 void evp_generic_do_all(OPENSSL_CTX
*libctx
, int operation_id
,
363 void (*user_fn
)(void *method
, void *arg
),
365 void *(*new_method
)(int name_id
,
366 const OSSL_DISPATCH
*fns
,
370 void (*free_method
)(void *))
372 struct do_all_data_st data
;
374 data
.new_method
= new_method
;
375 data
.free_method
= free_method
;
376 data
.user_fn
= user_fn
;
377 data
.user_arg
= user_arg
;
378 ossl_algorithm_do_all(libctx
, operation_id
, method_data
, do_one
, &data
);
381 const char *evp_first_name(OSSL_PROVIDER
*prov
, int name_id
)
383 OPENSSL_CTX
*libctx
= ossl_provider_library_context(prov
);
384 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
386 return ossl_namemap_num2name(namemap
, name_id
, 0);