5aa1319ce1554d5cc8ca984b3388282d11462ad9
[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/provider.h"
19 #include "internal/namemap.h"
20 #include "crypto/evp.h"    /* evp_local.h needs it */
21 #include "evp_local.h"
22
23 static void default_method_store_free(void *vstore)
24 {
25     ossl_method_store_free(vstore);
26 }
27
28 static void *default_method_store_new(OPENSSL_CTX *ctx)
29 {
30     return ossl_method_store_new(ctx);
31 }
32
33
34 static const OPENSSL_CTX_METHOD default_method_store_method = {
35     default_method_store_new,
36     default_method_store_free,
37 };
38
39 /* Data to be passed through ossl_method_construct() */
40 struct method_data_st {
41     OPENSSL_CTX *libctx;
42     OSSL_METHOD_CONSTRUCT_METHOD *mcm;
43     int operation_id;            /* For get_method_from_store() */
44     int name_id;                 /* For get_method_from_store() */
45     const char *name;            /* For get_method_from_store() */
46     const char *propquery;       /* For get_method_from_store() */
47     void *(*method_from_dispatch)(int name_id, const OSSL_DISPATCH *,
48                                   OSSL_PROVIDER *, void *);
49     void *method_data;
50     int (*refcnt_up_method)(void *method);
51     void (*destruct_method)(void *method);
52 };
53
54 /*
55  * Generic routines to fetch / create EVP methods with ossl_method_construct()
56  */
57 static void *alloc_tmp_method_store(OPENSSL_CTX *ctx)
58 {
59     return ossl_method_store_new(ctx);
60 }
61
62  static void dealloc_tmp_method_store(void *store)
63 {
64     if (store != NULL)
65         ossl_method_store_free(store);
66 }
67
68 static OSSL_METHOD_STORE *get_default_method_store(OPENSSL_CTX *libctx)
69 {
70     return openssl_ctx_get_data(libctx, OPENSSL_CTX_DEFAULT_METHOD_STORE_INDEX,
71                                 &default_method_store_method);
72 }
73
74 /*
75  * To identity the method in the method store, we mix the name identity
76  * with the operation identity, with the assumption that we don't have
77  * more than 2^24 names or more than 2^8 operation types.
78  *
79  * The resulting identity is a 32-bit integer, composed like this:
80  *
81  * +---------24 bits--------+-8 bits-+
82  * |      name identity     | op id  |
83  * +------------------------+--------+
84  */
85 static uint32_t method_id(unsigned int operation_id, int name_id)
86 {
87     if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8))
88         || !ossl_assert(name_id > 0 && operation_id > 0))
89         return 0;
90     return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
91 }
92
93 static void *get_method_from_store(OPENSSL_CTX *libctx, void *store,
94                                    void *data)
95 {
96     struct method_data_st *methdata = data;
97     void *method = NULL;
98     int name_id;
99     uint32_t meth_id;
100
101     /*
102      * get_method_from_store() is only called to try and get the method
103      * that evp_generic_fetch() is asking for, and the operation id as
104      * well as the name or name id are passed via methdata.
105      */
106     if ((name_id = methdata->name_id) == 0) {
107         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
108
109         if (namemap == 0)
110             return NULL;
111         name_id = ossl_namemap_name2num(namemap, methdata->name);
112     }
113
114     if (name_id == 0
115         || (meth_id = method_id(methdata->operation_id, name_id)) == 0)
116         return NULL;
117
118     if (store == NULL
119         && (store = get_default_method_store(libctx)) == NULL)
120         return NULL;
121
122     (void)ossl_method_store_fetch(store, meth_id, methdata->propquery,
123                                   &method);
124
125     if (method != NULL
126         && !methdata->refcnt_up_method(method)) {
127         method = NULL;
128     }
129     return method;
130 }
131
132 static int put_method_in_store(OPENSSL_CTX *libctx, void *store,
133                                void *method, const OSSL_PROVIDER *prov,
134                                int operation_id, const char *name,
135                                const char *propdef, void *data)
136 {
137     struct method_data_st *methdata = data;
138     OSSL_NAMEMAP *namemap;
139     int name_id;
140     uint32_t meth_id;
141
142     /*
143      * put_method_in_store() is only called with a method that was
144      * successfully created by construct_method() below, which means
145      * the name should already be stored in the namemap, so just use it.
146      */
147     if ((namemap = ossl_namemap_stored(libctx)) == NULL
148         || (name_id = ossl_namemap_name2num(namemap, name)) == 0
149         || (meth_id = method_id(operation_id, name_id)) == 0)
150         return 0;
151
152     if (store == NULL
153         && (store = get_default_method_store(libctx)) == NULL)
154         return 0;
155
156     return ossl_method_store_add(store, prov, meth_id, propdef, method,
157                                  methdata->refcnt_up_method,
158                                  methdata->destruct_method);
159 }
160
161 /*
162  * The core fetching functionality passes the name of the implementation.
163  * This function is responsible to getting an identity number for it.
164  */
165 static void *construct_method(const char *name, const OSSL_DISPATCH *fns,
166                               OSSL_PROVIDER *prov, void *data)
167 {
168     /*
169      * This function is only called if get_method_from_store() returned
170      * NULL, so it's safe to say that of all the spots to create a new
171      * namemap entry, this is it.  Should the name already exist there, we
172      * know that ossl_namemap_add() will return its corresponding number.
173      *
174      * TODO(3.0): If this function gets an array of names instead of just
175      * one, we need to check through all the names to see if at least one
176      * of them has an associated number, and use that.  If several names
177      * have associated numbers that differ from each other, it's an error.
178      */
179     struct method_data_st *methdata = data;
180     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
181     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
182     int name_id = ossl_namemap_add(namemap, 0, name);
183
184     return methdata->method_from_dispatch(name_id, fns, prov,
185                                           methdata->method_data);
186 }
187
188 static void destruct_method(void *method, void *data)
189 {
190     struct method_data_st *methdata = data;
191
192     methdata->destruct_method(method);
193 }
194
195 static void *inner_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
196                                  int name_id, const char *name,
197                                  const char *properties,
198                                  void *(*new_method)(int name_id,
199                                                      const OSSL_DISPATCH *fns,
200                                                      OSSL_PROVIDER *prov,
201                                                      void *method_data),
202                                  void *method_data,
203                                  int (*up_ref_method)(void *),
204                                  void (*free_method)(void *))
205 {
206     OSSL_METHOD_STORE *store = get_default_method_store(libctx);
207     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
208     uint32_t meth_id = 0;
209     void *method = NULL;
210
211     if (store == NULL || namemap == NULL)
212         return NULL;
213
214     /*
215      * If there's ever an operation_id == 0 passed, we have an internal
216      * programming error.
217      */
218     if (!ossl_assert(operation_id > 0))
219         return NULL;
220
221     /*
222      * If we have been passed neither a name_id or a name, we have an
223      * internal programming error.
224      */
225     if (!ossl_assert(name_id != 0 || name != NULL))
226         return NULL;
227
228     /* If we haven't received a name id yet, try to get one for the name */
229     if (name_id == 0)
230         name_id = ossl_namemap_name2num(namemap, name);
231
232     /*
233      * If we have a name id, calculate a method id with method_id().
234      *
235      * method_id returns 0 if we have too many operations (more than
236      * about 2^8) or too many names (more than about 2^24).  In that
237      * case, we can't create any new method.
238      */
239     if (name_id != 0 && (meth_id = method_id(operation_id, name_id)) == 0)
240         return NULL;
241
242     if (meth_id == 0
243         || !ossl_method_store_cache_get(store, meth_id, properties, &method)) {
244         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
245             alloc_tmp_method_store,
246             dealloc_tmp_method_store,
247             get_method_from_store,
248             put_method_in_store,
249             construct_method,
250             destruct_method
251         };
252         struct method_data_st mcmdata;
253
254         mcmdata.mcm = &mcm;
255         mcmdata.libctx = libctx;
256         mcmdata.operation_id = operation_id;
257         mcmdata.name_id = name_id;
258         mcmdata.name = name;
259         mcmdata.propquery = properties;
260         mcmdata.method_from_dispatch = new_method;
261         mcmdata.destruct_method = free_method;
262         mcmdata.refcnt_up_method = up_ref_method;
263         mcmdata.destruct_method = free_method;
264         mcmdata.method_data = method_data;
265         if ((method = ossl_method_construct(libctx, operation_id,
266                                             0 /* !force_cache */,
267                                             &mcm, &mcmdata)) != NULL) {
268             /*
269              * If construction did create a method for us, we know that
270              * there is a correct name_id and methodid, since those have
271              * already been calculated in get_method_from_store() and
272              * put_method_in_store() above.
273              */
274             if (name_id == 0)
275                 name_id = ossl_namemap_name2num(namemap, name);
276             meth_id = method_id(operation_id, name_id);
277             ossl_method_store_cache_set(store, meth_id, properties, method);
278         }
279     } else {
280         up_ref_method(method);
281     }
282
283     return method;
284 }
285
286 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
287                         const char *name, const char *properties,
288                         void *(*new_method)(int name_id,
289                                             const OSSL_DISPATCH *fns,
290                                             OSSL_PROVIDER *prov,
291                                             void *method_data),
292                         void *method_data,
293                         int (*up_ref_method)(void *),
294                         void (*free_method)(void *))
295 {
296     return inner_generic_fetch(libctx,
297                                operation_id, 0, name, properties,
298                                new_method, method_data,
299                                up_ref_method, free_method);
300 }
301
302 /*
303  * evp_generic_fetch_by_number() is special, and only returns methods for
304  * already known names, i.e. it refuses to work if no name_id can be found
305  * (it's considered an internal programming error).
306  * This is meant to be used when one method needs to fetch an associated
307  * other method.
308  */
309 void *evp_generic_fetch_by_number(OPENSSL_CTX *libctx, int operation_id,
310                                   int name_id, const char *properties,
311                                   void *(*new_method)(int name_id,
312                                                       const OSSL_DISPATCH *fns,
313                                                       OSSL_PROVIDER *prov,
314                                                       void *method_data),
315                                   void *method_data,
316                                   int (*up_ref_method)(void *),
317                                   void (*free_method)(void *))
318 {
319     return inner_generic_fetch(libctx,
320                                operation_id, name_id, NULL, properties,
321                                new_method, method_data,
322                                up_ref_method, free_method);
323 }
324
325 int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
326 {
327     OSSL_METHOD_STORE *store = get_default_method_store(libctx);
328
329     if (store != NULL)
330         return ossl_method_store_set_global_properties(store, propq);
331     EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
332     return 0;
333 }
334
335 struct do_all_data_st {
336     void (*user_fn)(void *method, void *arg);
337     void *user_arg;
338     void *(*new_method)(const int name_id, const OSSL_DISPATCH *fns,
339                         OSSL_PROVIDER *prov, void *method_data);
340     void (*free_method)(void *);
341 };
342
343 static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
344                    int no_store, void *vdata)
345 {
346     struct do_all_data_st *data = vdata;
347     OPENSSL_CTX *libctx = ossl_provider_library_context(provider);
348     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
349     int name_id = ossl_namemap_add(namemap, 0, algo->algorithm_name);
350     void *method = NULL;
351
352     if (name_id != 0)
353         method = data->new_method(name_id, algo->implementation, provider,
354                                   NULL);
355
356     if (method != NULL) {
357         data->user_fn(method, data->user_arg);
358         data->free_method(method);
359     }
360 }
361
362 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
363                         void (*user_fn)(void *method, void *arg),
364                         void *user_arg,
365                         void *(*new_method)(int name_id,
366                                             const OSSL_DISPATCH *fns,
367                                             OSSL_PROVIDER *prov,
368                                             void *method_data),
369                         void *method_data,
370                         void (*free_method)(void *))
371 {
372     struct do_all_data_st data;
373
374     data.new_method = new_method;
375     data.free_method = free_method;
376     data.user_fn = user_fn;
377     data.user_arg = user_arg;
378     ossl_algorithm_do_all(libctx, operation_id, method_data, do_one, &data);
379 }
380
381 const char *evp_first_name(OSSL_PROVIDER *prov, int name_id)
382 {
383     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
384     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
385
386     return ossl_namemap_num2name(namemap, name_id, 0);
387 }
388
389 int evp_is_a(OSSL_PROVIDER *prov, int number, const char *name)
390 {
391     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
392     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
393
394     return ossl_namemap_name2num(namemap, name) == number;
395 }
396
397 void evp_doall_names(OSSL_PROVIDER *prov, int number,
398                      void (*fn)(const char *name, void *data),
399                      void *data)
400 {
401     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
402     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
403
404     ossl_namemap_doall_names(namemap, number, fn, data);
405 }