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/namemap.h"
19 #include "internal/evp_int.h" /* evp_locl.h needs it */
22 static void default_method_store_free(void *vstore
)
24 ossl_method_store_free(vstore
);
27 static void *default_method_store_new(OPENSSL_CTX
*ctx
)
29 return ossl_method_store_new(ctx
);
33 static const OPENSSL_CTX_METHOD default_method_store_method
= {
34 default_method_store_new
,
35 default_method_store_free
,
38 /* Data to be passed through ossl_method_construct() */
39 struct method_data_st
{
43 OSSL_METHOD_CONSTRUCT_METHOD
*mcm
;
44 void *(*method_from_dispatch
)(int nid
, const OSSL_DISPATCH
*,
46 int (*refcnt_up_method
)(void *method
);
47 void (*destruct_method
)(void *method
);
48 int (*nid_method
)(void *method
);
52 * Generic routines to fetch / create EVP methods with ossl_method_construct()
54 static void *alloc_tmp_method_store(OPENSSL_CTX
*ctx
)
56 return ossl_method_store_new(ctx
);
59 static void dealloc_tmp_method_store(void *store
)
62 ossl_method_store_free(store
);
65 static OSSL_METHOD_STORE
*get_default_method_store(OPENSSL_CTX
*libctx
)
67 return openssl_ctx_get_data(libctx
, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX
,
68 &default_method_store_method
);
71 static void *get_method_from_store(OPENSSL_CTX
*libctx
, void *store
,
72 const char *propquery
, void *data
)
74 struct method_data_st
*methdata
= data
;
78 && (store
= get_default_method_store(libctx
)) == NULL
)
81 (void)ossl_method_store_fetch(store
, methdata
->nid
, propquery
, &method
);
84 && !methdata
->refcnt_up_method(method
)) {
90 static int put_method_in_store(OPENSSL_CTX
*libctx
, void *store
,
92 void *method
, void *data
)
94 struct method_data_st
*methdata
= data
;
95 int nid
= methdata
->nid_method(method
);
101 && (store
= get_default_method_store(libctx
)) == NULL
)
104 if (methdata
->refcnt_up_method(method
)
105 && ossl_method_store_add(store
, nid
, propdef
, method
,
106 methdata
->destruct_method
))
111 static void *construct_method(const char *algorithm_name
,
112 const OSSL_DISPATCH
*fns
, OSSL_PROVIDER
*prov
,
115 struct method_data_st
*methdata
= data
;
116 OSSL_NAMEMAP
*namemap
;
119 if ((namemap
= ossl_namemap_stored(methdata
->libctx
)) == NULL
120 || (nid
= ossl_namemap_add(namemap
, algorithm_name
)) == 0)
123 return methdata
->method_from_dispatch(nid
, fns
, prov
);
126 static void destruct_method(void *method
, void *data
)
128 struct method_data_st
*methdata
= data
;
130 methdata
->destruct_method(method
);
133 void *evp_generic_fetch(OPENSSL_CTX
*libctx
, int operation_id
,
134 const char *algorithm
, const char *properties
,
135 void *(*new_method
)(int nid
, const OSSL_DISPATCH
*fns
,
136 OSSL_PROVIDER
*prov
),
137 int (*upref_method
)(void *),
138 void (*free_method
)(void *),
139 int (*nid_method
)(void *))
141 OSSL_METHOD_STORE
*store
= get_default_method_store(libctx
);
142 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
146 if (store
== NULL
|| namemap
== NULL
)
149 if ((nid
= ossl_namemap_number(namemap
, algorithm
)) == 0
150 || !ossl_method_store_cache_get(store
, nid
, properties
, &method
)) {
151 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
152 alloc_tmp_method_store
,
153 dealloc_tmp_method_store
,
154 get_method_from_store
,
159 struct method_data_st mcmdata
;
163 mcmdata
.libctx
= libctx
;
164 mcmdata
.method_from_dispatch
= new_method
;
165 mcmdata
.destruct_method
= free_method
;
166 mcmdata
.refcnt_up_method
= upref_method
;
167 mcmdata
.destruct_method
= free_method
;
168 mcmdata
.nid_method
= nid_method
;
169 method
= ossl_method_construct(libctx
, operation_id
, algorithm
,
170 properties
, 0 /* !force_cache */,
172 ossl_method_store_cache_set(store
, nid
, properties
, method
);
174 upref_method(method
);
180 int EVP_set_default_properties(OPENSSL_CTX
*libctx
, const char *propq
)
182 OSSL_METHOD_STORE
*store
= get_default_method_store(libctx
);
185 return ossl_method_store_set_global_properties(store
, propq
);
186 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES
, ERR_R_INTERNAL_ERROR
);