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/types.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 "crypto/evp.h" /* evp_local.h needs it */
21 #include "evp_local.h"
23 #define NAME_SEPARATOR ':'
25 static void evp_method_store_free(void *vstore
)
27 ossl_method_store_free(vstore
);
30 static void *evp_method_store_new(OPENSSL_CTX
*ctx
)
32 return ossl_method_store_new(ctx
);
36 static const OPENSSL_CTX_METHOD evp_method_store_method
= {
38 evp_method_store_free
,
41 /* Data to be passed through ossl_method_construct() */
42 struct evp_method_data_st
{
44 OSSL_METHOD_CONSTRUCT_METHOD
*mcm
;
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() */
49 void *(*method_from_dispatch
)(int name_id
, const OSSL_DISPATCH
*,
51 int (*refcnt_up_method
)(void *method
);
52 void (*destruct_method
)(void *method
);
55 static int add_names_to_namemap(OSSL_NAMEMAP
*namemap
,
62 /* Check that we have a namemap and that there is at least one name */
63 if (namemap
== NULL
) {
64 ERR_raise(ERR_LIB_EVP
, ERR_R_PASSED_NULL_PARAMETER
);
69 * Check that no name is an empty string, and that all names have at
70 * most one numeric identity together.
72 for (p
= names
; *p
!= '\0'; p
= (q
== NULL
? p
+ l
: q
+ 1)) {
75 if ((q
= strchr(p
, NAME_SEPARATOR
)) == NULL
)
76 l
= strlen(p
); /* offset to \0 */
78 l
= q
- p
; /* offset to the next separator */
80 this_id
= ossl_namemap_name2num_n(namemap
, p
, l
);
82 if (*p
== '\0' || *p
== NAME_SEPARATOR
) {
83 ERR_raise(ERR_LIB_EVP
, EVP_R_BAD_ALGORITHM_NAME
);
88 else if (this_id
!= 0 && this_id
!= id
) {
89 ERR_raise_data(ERR_LIB_EVP
, EVP_R_CONFLICTING_ALGORITHM_NAME
,
90 "\"%.*s\" has an existing different identity %d (from \"%s\")",
91 l
, p
, this_id
, names
);
96 /* Now that we have checked, register all names */
97 for (p
= names
; *p
!= '\0'; p
= (q
== NULL
? p
+ l
: q
+ 1)) {
100 if ((q
= strchr(p
, NAME_SEPARATOR
)) == NULL
)
101 l
= strlen(p
); /* offset to \0 */
103 l
= q
- p
; /* offset to the next separator */
105 this_id
= ossl_namemap_add_n(namemap
, id
, p
, l
);
108 else if (this_id
!= id
) {
109 ERR_raise_data(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
,
110 "Got id %d when expecting %d", this_id
, id
);
119 * Generic routines to fetch / create EVP methods with ossl_method_construct()
121 static void *alloc_tmp_evp_method_store(OPENSSL_CTX
*ctx
)
123 return ossl_method_store_new(ctx
);
126 static void dealloc_tmp_evp_method_store(void *store
)
129 ossl_method_store_free(store
);
132 static OSSL_METHOD_STORE
*get_evp_method_store(OPENSSL_CTX
*libctx
)
134 return openssl_ctx_get_data(libctx
, OPENSSL_CTX_EVP_METHOD_STORE_INDEX
,
135 &evp_method_store_method
);
139 * To identity the method in the EVP method store, we mix the name identity
140 * with the operation identity, with the assumption that we don't have more
141 * than 2^24 names or more than 2^8 operation types.
143 * The resulting identity is a 32-bit integer, composed like this:
145 * +---------24 bits--------+-8 bits-+
146 * | name identity | op id |
147 * +------------------------+--------+
149 static uint32_t evp_method_id(unsigned int operation_id
, int name_id
)
151 if (!ossl_assert(name_id
< (1 << 24) || operation_id
< (1 << 8))
152 || !ossl_assert(name_id
> 0 && operation_id
> 0))
154 return ((name_id
<< 8) & 0xFFFFFF00) | (operation_id
& 0x000000FF);
157 static void *get_evp_method_from_store(OPENSSL_CTX
*libctx
, void *store
,
160 struct evp_method_data_st
*methdata
= data
;
166 * get_evp_method_from_store() is only called to try and get the method
167 * that evp_generic_fetch() is asking for, and the operation id as well
168 * as the name or name id are passed via methdata.
170 if ((name_id
= methdata
->name_id
) == 0) {
171 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
172 const char *names
= methdata
->names
;
173 const char *q
= strchr(names
, NAME_SEPARATOR
);
174 size_t l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
178 name_id
= ossl_namemap_name2num_n(namemap
, names
, l
);
182 || (meth_id
= evp_method_id(methdata
->operation_id
, name_id
)) == 0)
186 && (store
= get_evp_method_store(libctx
)) == NULL
)
189 if (!ossl_method_store_fetch(store
, meth_id
, methdata
->propquery
,
195 static int put_evp_method_in_store(OPENSSL_CTX
*libctx
, void *store
,
196 void *method
, const OSSL_PROVIDER
*prov
,
197 int operation_id
, const char *names
,
198 const char *propdef
, void *data
)
200 struct evp_method_data_st
*methdata
= data
;
201 OSSL_NAMEMAP
*namemap
;
207 * put_evp_method_in_store() is only called with an EVP method that was
208 * successfully created by construct_method() below, which means that
209 * all the names should already be stored in the namemap with the same
210 * numeric identity, so just use the first to get that identity.
213 const char *q
= strchr(names
, NAME_SEPARATOR
);
215 l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
218 if ((namemap
= ossl_namemap_stored(libctx
)) == NULL
219 || (name_id
= ossl_namemap_name2num_n(namemap
, names
, l
)) == 0
220 || (meth_id
= evp_method_id(operation_id
, name_id
)) == 0)
224 && (store
= get_evp_method_store(libctx
)) == NULL
)
227 return ossl_method_store_add(store
, prov
, meth_id
, propdef
, method
,
228 methdata
->refcnt_up_method
,
229 methdata
->destruct_method
);
233 * The core fetching functionality passes the name of the implementation.
234 * This function is responsible to getting an identity number for it.
236 static void *construct_evp_method(const char *names
, const OSSL_DISPATCH
*fns
,
237 OSSL_PROVIDER
*prov
, void *data
)
240 * This function is only called if get_evp_method_from_store() returned
241 * NULL, so it's safe to say that of all the spots to create a new
242 * namemap entry, this is it. Should the name already exist there, we
243 * know that ossl_namemap_add() will return its corresponding number.
245 struct evp_method_data_st
*methdata
= data
;
246 OPENSSL_CTX
*libctx
= ossl_provider_library_context(prov
);
247 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
248 int name_id
= add_names_to_namemap(namemap
, names
);
252 return methdata
->method_from_dispatch(name_id
, fns
, prov
);
255 static void destruct_evp_method(void *method
, void *data
)
257 struct evp_method_data_st
*methdata
= data
;
259 methdata
->destruct_method(method
);
263 inner_evp_generic_fetch(OPENSSL_CTX
*libctx
, int operation_id
,
264 int name_id
, const char *name
,
265 const char *properties
,
266 void *(*new_method
)(int name_id
,
267 const OSSL_DISPATCH
*fns
,
268 OSSL_PROVIDER
*prov
),
269 int (*up_ref_method
)(void *),
270 void (*free_method
)(void *))
272 OSSL_METHOD_STORE
*store
= get_evp_method_store(libctx
);
273 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
274 uint32_t meth_id
= 0;
277 if (store
== NULL
|| namemap
== NULL
)
281 * If there's ever an operation_id == 0 passed, we have an internal
284 if (!ossl_assert(operation_id
> 0))
288 * If we have been passed neither a name_id or a name, we have an
289 * internal programming error.
291 if (!ossl_assert(name_id
!= 0 || name
!= NULL
))
294 /* If we haven't received a name id yet, try to get one for the name */
296 name_id
= ossl_namemap_name2num(namemap
, name
);
299 * If we have a name id, calculate a method id with evp_method_id().
301 * evp_method_id returns 0 if we have too many operations (more than
302 * about 2^8) or too many names (more than about 2^24). In that case,
303 * we can't create any new method.
305 if (name_id
!= 0 && (meth_id
= evp_method_id(operation_id
, name_id
)) == 0)
309 || !ossl_method_store_cache_get(store
, meth_id
, properties
, &method
)) {
310 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
311 alloc_tmp_evp_method_store
,
312 dealloc_tmp_evp_method_store
,
313 get_evp_method_from_store
,
314 put_evp_method_in_store
,
315 construct_evp_method
,
318 struct evp_method_data_st mcmdata
;
321 mcmdata
.libctx
= libctx
;
322 mcmdata
.operation_id
= operation_id
;
323 mcmdata
.name_id
= name_id
;
324 mcmdata
.names
= name
;
325 mcmdata
.propquery
= properties
;
326 mcmdata
.method_from_dispatch
= new_method
;
327 mcmdata
.refcnt_up_method
= up_ref_method
;
328 mcmdata
.destruct_method
= free_method
;
329 if ((method
= ossl_method_construct(libctx
, operation_id
,
330 0 /* !force_cache */,
331 &mcm
, &mcmdata
)) != NULL
) {
333 * If construction did create a method for us, we know that
334 * there is a correct name_id and meth_id, since those have
335 * already been calculated in get_evp_method_from_store() and
336 * put_evp_method_in_store() above.
339 name_id
= ossl_namemap_name2num(namemap
, name
);
340 meth_id
= evp_method_id(operation_id
, name_id
);
341 ossl_method_store_cache_set(store
, meth_id
, properties
, method
,
342 up_ref_method
, free_method
);
349 void *evp_generic_fetch(OPENSSL_CTX
*libctx
, int operation_id
,
350 const char *name
, const char *properties
,
351 void *(*new_method
)(int name_id
,
352 const OSSL_DISPATCH
*fns
,
353 OSSL_PROVIDER
*prov
),
354 int (*up_ref_method
)(void *),
355 void (*free_method
)(void *))
357 return inner_evp_generic_fetch(libctx
,
358 operation_id
, 0, name
, properties
,
359 new_method
, up_ref_method
, free_method
);
363 * evp_generic_fetch_by_number() is special, and only returns methods for
364 * already known names, i.e. it refuses to work if no name_id can be found
365 * (it's considered an internal programming error).
366 * This is meant to be used when one method needs to fetch an associated
369 void *evp_generic_fetch_by_number(OPENSSL_CTX
*libctx
, int operation_id
,
370 int name_id
, const char *properties
,
371 void *(*new_method
)(int name_id
,
372 const OSSL_DISPATCH
*fns
,
373 OSSL_PROVIDER
*prov
),
374 int (*up_ref_method
)(void *),
375 void (*free_method
)(void *))
377 return inner_evp_generic_fetch(libctx
,
378 operation_id
, name_id
, NULL
, properties
,
379 new_method
, up_ref_method
, free_method
);
382 int EVP_set_default_properties(OPENSSL_CTX
*libctx
, const char *propq
)
384 OSSL_METHOD_STORE
*store
= get_evp_method_store(libctx
);
387 return ossl_method_store_set_global_properties(store
, propq
);
388 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES
, ERR_R_INTERNAL_ERROR
);
392 struct do_all_data_st
{
393 void (*user_fn
)(void *method
, void *arg
);
395 void *(*new_method
)(const int name_id
, const OSSL_DISPATCH
*fns
,
396 OSSL_PROVIDER
*prov
);
397 void (*free_method
)(void *);
400 static void do_one(OSSL_PROVIDER
*provider
, const OSSL_ALGORITHM
*algo
,
401 int no_store
, void *vdata
)
403 struct do_all_data_st
*data
= vdata
;
404 OPENSSL_CTX
*libctx
= ossl_provider_library_context(provider
);
405 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
406 int name_id
= add_names_to_namemap(namemap
, algo
->algorithm_names
);
410 method
= data
->new_method(name_id
, algo
->implementation
, provider
);
412 if (method
!= NULL
) {
413 data
->user_fn(method
, data
->user_arg
);
414 data
->free_method(method
);
418 void evp_generic_do_all(OPENSSL_CTX
*libctx
, int operation_id
,
419 void (*user_fn
)(void *method
, void *arg
),
421 void *(*new_method
)(int name_id
,
422 const OSSL_DISPATCH
*fns
,
423 OSSL_PROVIDER
*prov
),
424 void (*free_method
)(void *))
426 struct do_all_data_st data
;
428 data
.new_method
= new_method
;
429 data
.free_method
= free_method
;
430 data
.user_fn
= user_fn
;
431 data
.user_arg
= user_arg
;
432 ossl_algorithm_do_all(libctx
, operation_id
, NULL
, do_one
, &data
);
435 const char *evp_first_name(OSSL_PROVIDER
*prov
, int name_id
)
437 OPENSSL_CTX
*libctx
= ossl_provider_library_context(prov
);
438 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
440 return ossl_namemap_num2name(namemap
, name_id
, 0);
443 int evp_is_a(OSSL_PROVIDER
*prov
, int number
, const char *name
)
445 OPENSSL_CTX
*libctx
= ossl_provider_library_context(prov
);
446 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
448 return ossl_namemap_name2num(namemap
, name
) == number
;
451 void evp_names_do_all(OSSL_PROVIDER
*prov
, int number
,
452 void (*fn
)(const char *name
, void *data
),
455 OPENSSL_CTX
*libctx
= ossl_provider_library_context(prov
);
456 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
458 ossl_namemap_doall_names(namemap
, number
, fn
, data
);