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/asn1_int.h"
17 #include "internal/property.h"
18 #include "internal/core.h"
19 #include "internal/evp_int.h" /* evp_locl.h needs it */
22 /* The OpenSSL library context index for the default method store */
23 static int default_method_store_index = -1;
25 static void default_method_store_free(void *vstore)
27 ossl_method_store_free(vstore);
30 static void *default_method_store_new(void)
32 return ossl_method_store_new();
36 static const OPENSSL_CTX_METHOD default_method_store_method = {
37 default_method_store_new,
38 default_method_store_free,
41 static int default_method_store_init(void)
43 default_method_store_index =
44 openssl_ctx_new_index(&default_method_store_method);
46 return default_method_store_index != -1;
49 static CRYPTO_ONCE default_method_store_init_flag = CRYPTO_ONCE_STATIC_INIT;
50 DEFINE_RUN_ONCE_STATIC(do_default_method_store_init)
52 return OPENSSL_init_crypto(0, NULL)
53 && default_method_store_init();
56 /* Data to be passed through ossl_method_construct() */
57 struct method_data_st {
60 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
61 void *(*method_from_dispatch)(int nid, const OSSL_DISPATCH *,
63 int (*refcnt_up_method)(void *method);
64 void (*destruct_method)(void *method);
68 * Generic routines to fetch / create EVP methods with ossl_method_construct()
70 static void *alloc_tmp_method_store(void)
72 return ossl_method_store_new();
75 static void dealloc_tmp_method_store(void *store)
78 ossl_method_store_free(store);
81 static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
83 if (!RUN_ONCE(&default_method_store_init_flag,
84 do_default_method_store_init))
86 return openssl_ctx_get_data(libctx, default_method_store_index);
89 static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
90 const char *propquery, void *data)
92 struct method_data_st *methdata = data;
96 && (store = get_default_method_store(libctx)) == NULL)
99 (void)ossl_method_store_fetch(store, methdata->nid, propquery, &method);
102 && !methdata->refcnt_up_method(method)) {
108 static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
109 const char *propdef, void *method,
112 struct method_data_st *methdata = data;
115 && (store = get_default_method_store(libctx)) == NULL)
118 if (methdata->refcnt_up_method(method)
119 && ossl_method_store_add(store, methdata->nid, propdef, method,
120 methdata->destruct_method))
125 static void *construct_method(const OSSL_DISPATCH *fns, OSSL_PROVIDER *prov,
128 struct method_data_st *methdata = data;
131 if (methdata->nid == NID_undef) {
132 /* Create a new NID for that name on the fly */
135 /* This is the same as OBJ_create() but without requiring a OID */
136 tmpobj.nid = OBJ_new_nid(1);
137 tmpobj.sn = tmpobj.ln = methdata->name;
138 tmpobj.flags = ASN1_OBJECT_FLAG_DYNAMIC;
142 methdata->nid = OBJ_add_object(&tmpobj);
145 if (methdata->nid == NID_undef)
148 method = methdata->method_from_dispatch(methdata->nid, fns, prov);
154 static void destruct_method(void *method, void *data)
156 struct method_data_st *methdata = data;
158 methdata->destruct_method(method);
161 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
162 const char *algorithm, const char *properties,
163 void *(*new_method)(int nid, const OSSL_DISPATCH *fns,
164 OSSL_PROVIDER *prov),
165 int (*upref_method)(void *),
166 void (*free_method)(void *))
168 int nid = OBJ_sn2nid(algorithm);
172 || !ossl_method_store_cache_get(NULL, nid, properties, &method)) {
173 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
174 alloc_tmp_method_store,
175 dealloc_tmp_method_store,
176 get_method_from_store,
181 struct method_data_st mcmdata;
185 mcmdata.method_from_dispatch = new_method;
186 mcmdata.destruct_method = free_method;
187 mcmdata.refcnt_up_method = upref_method;
188 mcmdata.destruct_method = free_method;
189 method = ossl_method_construct(libctx, operation_id, algorithm,
190 properties, 0 /* !force_cache */,
192 ossl_method_store_cache_set(NULL, nid, properties, method);
198 int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
200 OSSL_METHOD_STORE *store = get_default_method_store(libctx);
203 return ossl_method_store_set_global_properties(store, propq);
204 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);