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