Disable mem leak checking for the self test lock
[oweals/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 #ifndef FIPS_MODE
119 /* Creates an initial namemap with names found in the legacy method db */
120 static void get_legacy_evp_names(const char *main_name, const char *alias,
121                                  void *arg)
122 {
123     int main_id = ossl_namemap_add(arg, 0, main_name);
124
125     /*
126      * We could check that the returned value is the same as main_id,
127      * but since this is a void function, there's no sane way to report
128      * the error.  The best we can do is trust ourselve to keep the legacy
129      * method database conflict free.
130      *
131      * This registers any alias with the same number as the main name.
132      * Should it be that the current |on| *has* the main name, this is
133      * simply a no-op.
134      */
135     if (alias != NULL)
136         (void)ossl_namemap_add(arg, main_id, alias);
137 }
138
139 static void get_legacy_cipher_names(const OBJ_NAME *on, void *arg)
140 {
141     const EVP_CIPHER *cipher = (void *)OBJ_NAME_get(on->name, on->type);
142
143     get_legacy_evp_names(EVP_CIPHER_name(cipher), on->name, arg);
144 }
145
146 static void get_legacy_md_names(const OBJ_NAME *on, void *arg)
147 {
148     const EVP_MD *md = (void *)OBJ_NAME_get(on->name, on->type);
149     /* We don't want the pkey_type names, so we need some extra care */
150     int snid, lnid;
151
152     snid = OBJ_sn2nid(on->name);
153     lnid = OBJ_ln2nid(on->name);
154     if (snid != EVP_MD_pkey_type(md) && lnid != EVP_MD_pkey_type(md))
155         get_legacy_evp_names(EVP_MD_name(md), on->name, arg);
156     else
157         get_legacy_evp_names(EVP_MD_name(md), NULL, arg);
158 }
159 #endif
160
161 static OSSL_NAMEMAP *get_prepopulated_namemap(OPENSSL_CTX *libctx)
162 {
163     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
164
165 #ifndef FIPS_MODE
166     if (namemap != NULL && ossl_namemap_empty(namemap)) {
167         /* Before pilfering, we make sure the legacy database is populated */
168         OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
169                             |OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
170
171         OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH,
172                         get_legacy_cipher_names, namemap);
173         OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH,
174                         get_legacy_md_names, namemap);
175     }
176 #endif
177
178     return namemap;
179 }
180
181 /*
182  * Generic routines to fetch / create EVP methods with ossl_method_construct()
183  */
184 static void *alloc_tmp_evp_method_store(OPENSSL_CTX *ctx)
185 {
186     return ossl_method_store_new(ctx);
187 }
188
189  static void dealloc_tmp_evp_method_store(void *store)
190 {
191     if (store != NULL)
192         ossl_method_store_free(store);
193 }
194
195 static OSSL_METHOD_STORE *get_evp_method_store(OPENSSL_CTX *libctx)
196 {
197     return openssl_ctx_get_data(libctx, OPENSSL_CTX_EVP_METHOD_STORE_INDEX,
198                                 &evp_method_store_method);
199 }
200
201 /*
202  * To identity the method in the EVP method store, we mix the name identity
203  * with the operation identity, with the assumption that we don't have more
204  * than 2^24 names or more than 2^8 operation types.
205  *
206  * The resulting identity is a 32-bit integer, composed like this:
207  *
208  * +---------24 bits--------+-8 bits-+
209  * |      name identity     | op id  |
210  * +------------------------+--------+
211  */
212 static uint32_t evp_method_id(unsigned int operation_id, int name_id)
213 {
214     if (!ossl_assert(name_id < (1 << 24) || operation_id < (1 << 8))
215         || !ossl_assert(name_id > 0 && operation_id > 0))
216         return 0;
217     return ((name_id << 8) & 0xFFFFFF00) | (operation_id & 0x000000FF);
218 }
219
220 static void *get_evp_method_from_store(OPENSSL_CTX *libctx, void *store,
221                                        void *data)
222 {
223     struct evp_method_data_st *methdata = data;
224     void *method = NULL;
225     int name_id;
226     uint32_t meth_id;
227
228     /*
229      * get_evp_method_from_store() is only called to try and get the method
230      * that evp_generic_fetch() is asking for, and the operation id as well
231      * as the name or name id are passed via methdata.
232      */
233     if ((name_id = methdata->name_id) == 0) {
234         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
235         const char *names = methdata->names;
236         const char *q = strchr(names, NAME_SEPARATOR);
237         size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
238
239         if (namemap == 0)
240             return NULL;
241         name_id = ossl_namemap_name2num_n(namemap, names, l);
242     }
243
244     if (name_id == 0
245         || (meth_id = evp_method_id(methdata->operation_id, name_id)) == 0)
246         return NULL;
247
248     if (store == NULL
249         && (store = get_evp_method_store(libctx)) == NULL)
250         return NULL;
251
252     if (!ossl_method_store_fetch(store, meth_id, methdata->propquery,
253                                  &method))
254         return NULL;
255     return method;
256 }
257
258 static int put_evp_method_in_store(OPENSSL_CTX *libctx, void *store,
259                                    void *method, const OSSL_PROVIDER *prov,
260                                    int operation_id, const char *names,
261                                    const char *propdef, void *data)
262 {
263     struct evp_method_data_st *methdata = data;
264     OSSL_NAMEMAP *namemap;
265     int name_id;
266     uint32_t meth_id;
267     size_t l = 0;
268
269     /*
270      * put_evp_method_in_store() is only called with an EVP method that was
271      * successfully created by construct_method() below, which means that
272      * all the names should already be stored in the namemap with the same
273      * numeric identity, so just use the first to get that identity.
274      */
275     if (names != NULL) {
276         const char *q = strchr(names, NAME_SEPARATOR);
277
278         l = (q == NULL ? strlen(names) : (size_t)(q - names));
279     }
280
281     if ((namemap = ossl_namemap_stored(libctx)) == NULL
282         || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
283         || (meth_id = evp_method_id(operation_id, name_id)) == 0)
284         return 0;
285
286     if (store == NULL
287         && (store = get_evp_method_store(libctx)) == NULL)
288         return 0;
289
290     return ossl_method_store_add(store, prov, meth_id, propdef, method,
291                                  methdata->refcnt_up_method,
292                                  methdata->destruct_method);
293 }
294
295 /*
296  * The core fetching functionality passes the name of the implementation.
297  * This function is responsible to getting an identity number for it.
298  */
299 static void *construct_evp_method(const char *names, const OSSL_DISPATCH *fns,
300                                   OSSL_PROVIDER *prov, void *data)
301 {
302     /*
303      * This function is only called if get_evp_method_from_store() returned
304      * NULL, so it's safe to say that of all the spots to create a new
305      * namemap entry, this is it.  Should the name already exist there, we
306      * know that ossl_namemap_add() will return its corresponding number.
307      */
308     struct evp_method_data_st *methdata = data;
309     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
310     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
311     int name_id = add_names_to_namemap(namemap, names);
312
313     if (name_id == 0)
314         return NULL;
315     return methdata->method_from_dispatch(name_id, fns, prov);
316 }
317
318 static void destruct_evp_method(void *method, void *data)
319 {
320     struct evp_method_data_st *methdata = data;
321
322     methdata->destruct_method(method);
323 }
324
325 static void *
326 inner_evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
327                         int name_id, const char *name,
328                         const char *properties,
329                         void *(*new_method)(int name_id,
330                                             const OSSL_DISPATCH *fns,
331                                             OSSL_PROVIDER *prov),
332                         int (*up_ref_method)(void *),
333                         void (*free_method)(void *))
334 {
335     OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
336     OSSL_NAMEMAP *namemap = get_prepopulated_namemap(libctx);
337     uint32_t meth_id = 0;
338     void *method = NULL;
339
340     if (store == NULL || namemap == NULL)
341         return NULL;
342
343     /*
344      * If there's ever an operation_id == 0 passed, we have an internal
345      * programming error.
346      */
347     if (!ossl_assert(operation_id > 0))
348         return NULL;
349
350     /*
351      * If we have been passed neither a name_id or a name, we have an
352      * internal programming error.
353      */
354     if (!ossl_assert(name_id != 0 || name != NULL))
355         return NULL;
356
357     /* If we haven't received a name id yet, try to get one for the name */
358     if (name_id == 0)
359         name_id = ossl_namemap_name2num(namemap, name);
360
361     /*
362      * If we have a name id, calculate a method id with evp_method_id().
363      *
364      * evp_method_id returns 0 if we have too many operations (more than
365      * about 2^8) or too many names (more than about 2^24).  In that case,
366      * we can't create any new method.
367      */
368     if (name_id != 0 && (meth_id = evp_method_id(operation_id, name_id)) == 0)
369         return NULL;
370
371     if (meth_id == 0
372         || !ossl_method_store_cache_get(store, meth_id, properties, &method)) {
373         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
374             alloc_tmp_evp_method_store,
375             dealloc_tmp_evp_method_store,
376             get_evp_method_from_store,
377             put_evp_method_in_store,
378             construct_evp_method,
379             destruct_evp_method
380         };
381         struct evp_method_data_st mcmdata;
382
383         mcmdata.mcm = &mcm;
384         mcmdata.libctx = libctx;
385         mcmdata.operation_id = operation_id;
386         mcmdata.name_id = name_id;
387         mcmdata.names = name;
388         mcmdata.propquery = properties;
389         mcmdata.method_from_dispatch = new_method;
390         mcmdata.refcnt_up_method = up_ref_method;
391         mcmdata.destruct_method = free_method;
392         if ((method = ossl_method_construct(libctx, operation_id,
393                                             0 /* !force_cache */,
394                                             &mcm, &mcmdata)) != NULL) {
395             /*
396              * If construction did create a method for us, we know that
397              * there is a correct name_id and meth_id, since those have
398              * already been calculated in get_evp_method_from_store() and
399              * put_evp_method_in_store() above.
400              */
401             if (name_id == 0)
402                 name_id = ossl_namemap_name2num(namemap, name);
403             meth_id = evp_method_id(operation_id, name_id);
404             ossl_method_store_cache_set(store, meth_id, properties, method,
405                                         up_ref_method, free_method);
406         }
407     }
408
409     return method;
410 }
411
412 void *evp_generic_fetch(OPENSSL_CTX *libctx, int operation_id,
413                         const char *name, const char *properties,
414                         void *(*new_method)(int name_id,
415                                             const OSSL_DISPATCH *fns,
416                                             OSSL_PROVIDER *prov),
417                         int (*up_ref_method)(void *),
418                         void (*free_method)(void *))
419 {
420     return inner_evp_generic_fetch(libctx,
421                                    operation_id, 0, name, properties,
422                                    new_method, up_ref_method, free_method);
423 }
424
425 /*
426  * evp_generic_fetch_by_number() is special, and only returns methods for
427  * already known names, i.e. it refuses to work if no name_id can be found
428  * (it's considered an internal programming error).
429  * This is meant to be used when one method needs to fetch an associated
430  * other method.
431  */
432 void *evp_generic_fetch_by_number(OPENSSL_CTX *libctx, int operation_id,
433                                   int name_id, const char *properties,
434                                   void *(*new_method)(int name_id,
435                                                       const OSSL_DISPATCH *fns,
436                                                       OSSL_PROVIDER *prov),
437                                   int (*up_ref_method)(void *),
438                                   void (*free_method)(void *))
439 {
440     return inner_evp_generic_fetch(libctx,
441                                    operation_id, name_id, NULL, properties,
442                                    new_method, up_ref_method, free_method);
443 }
444
445 int EVP_set_default_properties(OPENSSL_CTX *libctx, const char *propq)
446 {
447     OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
448
449     if (store != NULL)
450         return ossl_method_store_set_global_properties(store, propq);
451     EVPerr(EVP_F_EVP_SET_DEFAULT_PROPERTIES, ERR_R_INTERNAL_ERROR);
452     return 0;
453 }
454
455 struct do_all_data_st {
456     void (*user_fn)(void *method, void *arg);
457     void *user_arg;
458     void *(*new_method)(const int name_id, const OSSL_DISPATCH *fns,
459                         OSSL_PROVIDER *prov);
460     void (*free_method)(void *);
461 };
462
463 static void do_one(OSSL_PROVIDER *provider, const OSSL_ALGORITHM *algo,
464                    int no_store, void *vdata)
465 {
466     struct do_all_data_st *data = vdata;
467     OPENSSL_CTX *libctx = ossl_provider_library_context(provider);
468     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
469     int name_id = add_names_to_namemap(namemap, algo->algorithm_names);
470     void *method = NULL;
471
472     if (name_id != 0)
473         method = data->new_method(name_id, algo->implementation, provider);
474
475     if (method != NULL) {
476         data->user_fn(method, data->user_arg);
477         data->free_method(method);
478     }
479 }
480
481 void evp_generic_do_all(OPENSSL_CTX *libctx, int operation_id,
482                         void (*user_fn)(void *method, void *arg),
483                         void *user_arg,
484                         void *(*new_method)(int name_id,
485                                             const OSSL_DISPATCH *fns,
486                                             OSSL_PROVIDER *prov),
487                         void (*free_method)(void *))
488 {
489     struct do_all_data_st data;
490
491     data.new_method = new_method;
492     data.free_method = free_method;
493     data.user_fn = user_fn;
494     data.user_arg = user_arg;
495     ossl_algorithm_do_all(libctx, operation_id, NULL, do_one, &data);
496 }
497
498 const char *evp_first_name(OSSL_PROVIDER *prov, int name_id)
499 {
500     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
501     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
502
503     return ossl_namemap_num2name(namemap, name_id, 0);
504 }
505
506 int evp_is_a(OSSL_PROVIDER *prov, int number, const char *name)
507 {
508     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
509     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
510
511     return ossl_namemap_name2num(namemap, name) == number;
512 }
513
514 void evp_names_do_all(OSSL_PROVIDER *prov, int number,
515                       void (*fn)(const char *name, void *data),
516                       void *data)
517 {
518     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
519     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
520
521     ossl_namemap_doall_names(namemap, number, fn, data);
522 }