662195e4de08275c1d38f12b7800c2980e84e6e4
[openssl.git] / crypto / evp / evp_fetch.c
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/property.h"
17 #include "internal/core.h"
18 #include "internal/namemap.h"
19 #include "internal/evp_int.h"    /* evp_locl.h needs it */
20 #include "evp_locl.h"
21
22 static void default_method_store_free(void *vstore)
23 {
24     ossl_method_store_free(vstore);
25 }
26
27 static void *default_method_store_new(OPENSSL_CTX *ctx)
28 {
29     return ossl_method_store_new(ctx);
30 }
31
32
33 static const OPENSSL_CTX_METHOD default_method_store_method = {
34     default_method_store_new,
35     default_method_store_free,
36 };
37
38 /* Data to be passed through ossl_method_construct() */
39 struct method_data_st {
40     OPENSSL_CTX *libctx;
41     const char *name;
42     OSSL_METHOD_CONSTRUCT_METHOD *mcm;
43     void *(*method_from_dispatch)(const char *, const OSSL_DISPATCH *,
44                                   OSSL_PROVIDER *, void *);
45     void *method_data;
46     int (*refcnt_up_method)(void *method);
47     void (*destruct_method)(void *method);
48 };
49
50 /*
51  * Generic routines to fetch / create EVP methods with ossl_method_construct()
52  */
53 static void *alloc_tmp_method_store(OPENSSL_CTX *ctx)
54 {
55     return ossl_method_store_new(ctx);
56 }
57
58  static void dealloc_tmp_method_store(void *store)
59 {
60     if (store != NULL)
61         ossl_method_store_free(store);
62 }
63
64 static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
65 {
66     return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX,
67                                 &default_method_store_method);
68 }
69
70 /*
71  * To identity the method in the method store, we mix the name identity
72  * with the operation identity, with the assumption that we don't have
73  * more than 2^24 names or more than 2^8 operation types.
74  *
75  * The resulting identity is a 32-bit integer, composed like this:
76  *
77  * +---------24 bits--------+-8 bits-+
78  * |      name identity     | op id  |
79  * +------------------------+--------+
80  */
81 static uint32_t method_id(unsigned int operation_id, unsigned int name_id)
82 {
83     if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8))
84         || !ossl_assert(name_id > 0 && operation_id > 0))
85         return 0;
86     return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
87 }
88
89 static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
90                                    int operation_id, const char *name,
91                                    const char *propquery, void *data)
92 {
93     struct method_data_st *methdata = data;
94     void *method = NULL;
95     OSSL_NAMEMAP *namemap;
96     int nameid;
97     uint32_t methid;
98
99     if (store == NULL
100         && (store = get_default_method_store(libctx)) == NULL)
101         return NULL;
102
103     if ((namemap = ossl_namemap_stored(libctx)) == NULL
104         || (nameid = ossl_namemap_name2num(namemap, name)) == 0
105         || (methid = method_id(operation_id, nameid)) == 0)
106         return NULL;
107
108     (void)ossl_method_store_fetch(store, methid, propquery, &method);
109
110     if (method != NULL
111         && !methdata->refcnt_up_method(method)) {
112         method = NULL;
113     }
114     return method;
115 }
116
117 static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
118                                void *method, const OSSL_PROVIDER *prov,
119                                int operation_id, const char *name,
120                                const char *propdef, void *data)
121 {
122     struct method_data_st *methdata = data;
123     OSSL_NAMEMAP *namemap;
124     int nameid;
125     uint32_t methid;
126
127     if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
128         || (nameid = ossl_namemap_add(namemap, 0, name)) == 0
129         || (methid = method_id(operation_id, nameid)) == 0)
130         return 0;
131
132     if (store == NULL
133         && (store = get_default_method_store(libctx)) == NULL)
134         return 0;
135
136     return ossl_method_store_add(store, prov, methid, propdef, method,
137                                  methdata->refcnt_up_method,
138                                  methdata->destruct_method);
139 }
140
141 static void *construct_method(const char *name, const OSSL_DISPATCH *fns,
142                               OSSL_PROVIDER *prov, void *data)
143 {
144     struct method_data_st *methdata = data;
145
146     return methdata->method_from_dispatch(name, fns, prov,
147                                           methdata->method_data);
148 }
149
150 static void destruct_method(void *method, void *data)
151 {
152     struct method_data_st *methdata = data;
153
154     methdata->destruct_method(method);
155 }
156
157 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
158                         const char *name, const char *properties,
159                         void *(*new_method)(const char *name,
160                                             const OSSL_DISPATCH *fns,
161                                             OSSL_PROVIDER *prov,
162                                             void *method_data),
163                         void *method_data,
164                         int (*up_ref_method)(void *),
165                         void (*free_method)(void *))
166 {
167     OSSL_METHOD_STORE *store = get_default_method_store(libctx);
168     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
169     int nameid = 0;
170     uint32_t methid = 0;
171     void *method = NULL;
172
173     if (store == NULL || namemap == NULL)
174         return NULL;
175
176     /*
177      * If there's ever an operation_id == 0 passed, we have an internal
178      * programming error.
179      */
180     if (!ossl_assert(operation_id > 0))
181         return NULL;
182
183     /*
184      * method_id returns 0 if we have too many operations (more than
185      * about 2^8) or too many names (more than about 2^24).  In that
186      * case, we can't create any new method.
187      */
188     if ((nameid = ossl_namemap_name2num(namemap, name)) != 0
189         && (methid = method_id(operation_id, nameid)) == 0)
190         return NULL;
191
192     if (nameid == 0
193         || !ossl_method_store_cache_get(store, methid, properties,
194                                         &method)) {
195         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
196             alloc_tmp_method_store,
197             dealloc_tmp_method_store,
198             get_method_from_store,
199             put_method_in_store,
200             construct_method,
201             destruct_method
202         };
203         struct method_data_st mcmdata;
204
205         mcmdata.mcm = &mcm;
206         mcmdata.libctx = libctx;
207         mcmdata.name = name;
208         mcmdata.method_from_dispatch = new_method;
209         mcmdata.destruct_method = free_method;
210         mcmdata.refcnt_up_method = up_ref_method;
211         mcmdata.destruct_method = free_method;
212         mcmdata.method_data = method_data;
213         if ((method = ossl_method_construct(libctx, operation_id, name,
214                                             properties, 0 /* !force_cache */,
215                                             &mcm, &mcmdata)) != NULL) {
216             /*
217              * If construction did create a method for us, we know that
218              * there is a correct nameid and methodid, since those have
219              * already been calculated in get_method_from_store() and
220              * put_method_in_store() above.
221              */
222             nameid = ossl_namemap_name2num(namemap, name);
223             methid = method_id(operation_id, nameid);
224             ossl_method_store_cache_set(store, methid, properties, method);
225         }
226     } else {
227         up_ref_method(method);
228     }
229
230     return method;
231 }
232
233 int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
234 {
235     OSSL_METHOD_STORE *store = get_default_method_store(libctx);
236
237     if (store != NULL)
238         return ossl_method_store_set_global_properties(store, propq);
239     EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
240     return 0;
241 }
242
243 struct do_all_data_st {
244     void (*user_fn)(void *method, void *arg);
245     void *user_arg;
246     void *(*new_method)(const char *name, const OSSL_DISPATCH *fns,
247                         OSSL_PROVIDER *prov, void *method_data);
248     void (*free_method)(void *);
249 };
250
251 static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
252                    int no_store, void *vdata)
253 {
254     struct do_all_data_st *data = vdata;
255     void *method = data->new_method(algo->algorithm_name,
256                                     algo->implementation, provider, NULL);
257
258     if (method != NULL) {
259         data->user_fn(method, data->user_arg);
260         data->free_method(method);
261     }
262 }
263
264 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
265                         void (*user_fn)(void *method, void *arg),
266                         void *user_arg,
267                         void *(*new_method)(const char *name,
268                                             const OSSL_DISPATCH *fns,
269                                             OSSL_PROVIDER *prov,
270                                             void *method_data),
271                         void *method_data,
272                         void (*free_method)(void *))
273 {
274     struct do_all_data_st data;
275
276     data.new_method = new_method;
277     data.free_method = free_method;
278     data.user_fn = user_fn;
279     data.user_arg = user_arg;
280     ossl_algorithm_do_all(libctx, operation_id, method_data, do_one, &data);
281 }