]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/evp/evp_fetch.c
Add support for openssl_ctx_run_once and openssl_ctx_onfree
[thirdparty/openssl.git] / crypto / evp / evp_fetch.c
CommitLineData
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>
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 */
20#include "evp_locl.h"
21
22/* The OpenSSL library context index for the default method store */
23static int default_method_store_index = -1;
24
25static void default_method_store_free(void *vstore)
26{
27 ossl_method_store_free(vstore);
28}
29
30static void *default_method_store_new(void)
31{
32 return ossl_method_store_new();
33}
34
35
36static const OPENSSL_CTX_METHOD default_method_store_method = {
37 default_method_store_new,
38 default_method_store_free,
39};
40
41static int default_method_store_init(void)
42{
43 default_method_store_index =
44 openssl_ctx_new_index(&default_method_store_method);
45
46 return default_method_store_index != -1;
47}
48
49static CRYPTO_ONCE default_method_store_init_flag = CRYPTO_ONCE_STATIC_INIT;
50DEFINE_RUN_ONCE_STATIC(do_default_method_store_init)
51{
52 return OPENSSL_init_crypto(0, NULL)
53 && default_method_store_init();
54}
55
56/* Data to be passed through ossl_method_construct() */
57struct method_data_st {
58 const char *name;
59 int nid;
60 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
61 void *(*method_from_dispatch)(int nid, const OSSL_DISPATCH *,
62 OSSL_PROVIDER *);
63 int (*refcnt_up_method)(void *method);
64 void (*destruct_method)(void *method);
dc46e3dd 65 int (*nid_method)(void *method);
c13d2ab4
RL
66};
67
68/*
69 * Generic routines to fetch / create EVP methods with ossl_method_construct()
70 */
71static void *alloc_tmp_method_store(void)
72{
73 return ossl_method_store_new();
74}
75
76 static void dealloc_tmp_method_store(void *store)
77{
78 if (store != NULL)
79 ossl_method_store_free(store);
80}
81
cb929645 82static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
c13d2ab4
RL
83{
84 if (!RUN_ONCE(&default_method_store_init_flag,
85 do_default_method_store_init))
86 return NULL;
87 return openssl_ctx_get_data(libctx, default_method_store_index);
88}
89
90static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
91 const char *propquery, void *data)
92{
93 struct method_data_st *methdata = data;
94 void *method = NULL;
95
96 if (store == NULL
97 && (store = get_default_method_store(libctx)) == NULL)
98 return NULL;
99
100 (void)ossl_method_store_fetch(store, methdata->nid, propquery, &method);
101
102 if (method != NULL
103 && !methdata->refcnt_up_method(method)) {
104 method = NULL;
105 }
106 return method;
107}
108
109static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
dc46e3dd
MC
110 const char *propdef,
111 void *method, void *data)
c13d2ab4
RL
112{
113 struct method_data_st *methdata = data;
dc46e3dd
MC
114 int nid = methdata->nid_method(method);
115
116 if (nid == NID_undef)
117 return 0;
c13d2ab4
RL
118
119 if (store == NULL
120 && (store = get_default_method_store(libctx)) == NULL)
121 return 0;
122
123 if (methdata->refcnt_up_method(method)
dc46e3dd 124 && ossl_method_store_add(store, nid, propdef, method,
c13d2ab4
RL
125 methdata->destruct_method))
126 return 1;
127 return 0;
128}
129
dc46e3dd
MC
130static void *construct_method(const char *algorithm_name,
131 const OSSL_DISPATCH *fns, OSSL_PROVIDER *prov,
c13d2ab4
RL
132 void *data)
133{
134 struct method_data_st *methdata = data;
135 void *method = NULL;
dc46e3dd 136 int nid = OBJ_sn2nid(algorithm_name);
c13d2ab4 137
dc46e3dd 138 if (nid == NID_undef) {
c13d2ab4
RL
139 /* Create a new NID for that name on the fly */
140 ASN1_OBJECT tmpobj;
141
142 /* This is the same as OBJ_create() but without requiring a OID */
143 tmpobj.nid = OBJ_new_nid(1);
144 tmpobj.sn = tmpobj.ln = methdata->name;
145 tmpobj.flags = ASN1_OBJECT_FLAG_DYNAMIC;
146 tmpobj.length = 0;
147 tmpobj.data = NULL;
148
dc46e3dd 149 nid = OBJ_add_object(&tmpobj);
c13d2ab4
RL
150 }
151
dc46e3dd 152 if (nid == NID_undef)
c13d2ab4
RL
153 return NULL;
154
dc46e3dd 155 method = methdata->method_from_dispatch(nid, fns, prov);
c13d2ab4
RL
156 if (method == NULL)
157 return NULL;
158 return method;
159}
160
161static void destruct_method(void *method, void *data)
162{
163 struct method_data_st *methdata = data;
164
165 methdata->destruct_method(method);
166}
167
168void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
169 const char *algorithm, const char *properties,
170 void *(*new_method)(int nid, const OSSL_DISPATCH *fns,
171 OSSL_PROVIDER *prov),
172 int (*upref_method)(void *),
dc46e3dd
MC
173 void (*free_method)(void *),
174 int (*nid_method)(void *))
c13d2ab4 175{
e019da7b 176 OSSL_METHOD_STORE *store = get_default_method_store(libctx);
c13d2ab4
RL
177 int nid = OBJ_sn2nid(algorithm);
178 void *method = NULL;
179
e019da7b
RL
180 if (store == NULL)
181 return NULL;
182
c13d2ab4 183 if (nid == NID_undef
e019da7b 184 || !ossl_method_store_cache_get(store, nid, properties, &method)) {
c13d2ab4
RL
185 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
186 alloc_tmp_method_store,
187 dealloc_tmp_method_store,
188 get_method_from_store,
189 put_method_in_store,
190 construct_method,
191 destruct_method
192 };
193 struct method_data_st mcmdata;
194
195 mcmdata.nid = nid;
196 mcmdata.mcm = &mcm;
197 mcmdata.method_from_dispatch = new_method;
198 mcmdata.destruct_method = free_method;
199 mcmdata.refcnt_up_method = upref_method;
200 mcmdata.destruct_method = free_method;
dc46e3dd 201 mcmdata.nid_method = nid_method;
c13d2ab4
RL
202 method = ossl_method_construct(libctx, operation_id, algorithm,
203 properties, 0 /* !force_cache */,
204 &mcm, &mcmdata);
e019da7b
RL
205 ossl_method_store_cache_set(store, nid, properties, method);
206 } else {
207 upref_method(method);
c13d2ab4
RL
208 }
209
210 return method;
211}
cb929645
RL
212
213int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
214{
215 OSSL_METHOD_STORE *store = get_default_method_store(libctx);
216
217 if (store != NULL)
218 return ossl_method_store_set_global_properties(store, propq);
219 EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
220 return 0;
221}