Update copyright year
[openssl.git] / crypto / evp / evp_fetch.c
1 /*
2  * Copyright 2019-2022 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 "internal/property.h"
21 #include "crypto/evp.h"    /* evp_local.h needs it */
22 #include "evp_local.h"
23
24 #define NAME_SEPARATOR ':'
25
26 /* Data to be passed through ossl_method_construct() */
27 struct evp_method_data_st {
28     OSSL_LIB_CTX *libctx;
29     int operation_id;            /* For get_evp_method_from_store() */
30     int name_id;                 /* For get_evp_method_from_store() */
31     const char *names;           /* For get_evp_method_from_store() */
32     const char *propquery;       /* For get_evp_method_from_store() */
33
34     OSSL_METHOD_STORE *tmp_store; /* For get_tmp_evp_method_store() */
35
36     unsigned int flag_construct_error_occurred : 1;
37
38     void *(*method_from_algorithm)(int name_id, const OSSL_ALGORITHM *,
39                                    OSSL_PROVIDER *);
40     int (*refcnt_up_method)(void *method);
41     void (*destruct_method)(void *method);
42 };
43
44 /*
45  * Generic routines to fetch / create EVP methods with ossl_method_construct()
46  */
47 static void *get_tmp_evp_method_store(void *data)
48 {
49     struct evp_method_data_st *methdata = data;
50
51     if (methdata->tmp_store == NULL)
52         methdata->tmp_store = ossl_method_store_new(methdata->libctx);
53     return methdata->tmp_store;
54 }
55
56  static void dealloc_tmp_evp_method_store(void *store)
57 {
58     if (store != NULL)
59         ossl_method_store_free(store);
60 }
61
62 static OSSL_METHOD_STORE *get_evp_method_store(OSSL_LIB_CTX *libctx)
63 {
64     return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_EVP_METHOD_STORE_INDEX);
65 }
66
67 /*
68  * To identify the method in the EVP method store, we mix the name identity
69  * with the operation identity, under the assumption that we don't have more
70  * than 2^23 names or more than 2^8 operation types.
71  *
72  * The resulting identity is a 31-bit integer, composed like this:
73  *
74  * +---------23 bits--------+-8 bits-+
75  * |      name identity     | op id  |
76  * +------------------------+--------+
77  *
78  * We limit this composite number to 31 bits, thus leaving the top uint32_t
79  * bit always zero, to avoid negative sign extension when downshifting after
80  * this number happens to be passed to an int (which happens as soon as it's
81  * passed to ossl_method_store_cache_set(), and it's in that form that it
82  * gets passed along to filter_on_operation_id(), defined further down.
83  */
84 #define METHOD_ID_OPERATION_MASK        0x000000FF
85 #define METHOD_ID_OPERATION_MAX         ((1 << 8) - 1)
86 #define METHOD_ID_NAME_MASK             0x7FFFFF00
87 #define METHOD_ID_NAME_OFFSET           8
88 #define METHOD_ID_NAME_MAX              ((1 << 23) - 1)
89 static uint32_t evp_method_id(int name_id, unsigned int operation_id)
90 {
91     if (!ossl_assert(name_id > 0 && name_id <= METHOD_ID_NAME_MAX)
92         || !ossl_assert(operation_id > 0
93                         && operation_id <= METHOD_ID_OPERATION_MAX))
94         return 0;
95     return (((name_id << METHOD_ID_NAME_OFFSET) & METHOD_ID_NAME_MASK)
96             | (operation_id & METHOD_ID_OPERATION_MASK));
97 }
98
99 static void *get_evp_method_from_store(void *store, const OSSL_PROVIDER **prov,
100                                        void *data)
101 {
102     struct evp_method_data_st *methdata = data;
103     void *method = NULL;
104     int name_id = 0;
105     uint32_t meth_id;
106
107     /*
108      * get_evp_method_from_store() is only called to try and get the method
109      * that evp_generic_fetch() is asking for, and the operation id as well
110      * as the name or name id are passed via methdata.
111      */
112     if ((name_id = methdata->name_id) == 0 && methdata->names != NULL) {
113         OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
114         const char *names = methdata->names;
115         const char *q = strchr(names, NAME_SEPARATOR);
116         size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
117
118         if (namemap == 0)
119             return NULL;
120         name_id = ossl_namemap_name2num_n(namemap, names, l);
121     }
122
123     if (name_id == 0
124         || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
125         return NULL;
126
127     if (store == NULL
128         && (store = get_evp_method_store(methdata->libctx)) == NULL)
129         return NULL;
130
131     if (!ossl_method_store_fetch(store, meth_id, methdata->propquery, prov,
132                                  &method))
133         return NULL;
134     return method;
135 }
136
137 static int put_evp_method_in_store(void *store, void *method,
138                                    const OSSL_PROVIDER *prov,
139                                    const char *names, const char *propdef,
140                                    void *data)
141 {
142     struct evp_method_data_st *methdata = data;
143     OSSL_NAMEMAP *namemap;
144     int name_id;
145     uint32_t meth_id;
146     size_t l = 0;
147
148     /*
149      * put_evp_method_in_store() is only called with an EVP method that was
150      * successfully created by construct_method() below, which means that
151      * all the names should already be stored in the namemap with the same
152      * numeric identity, so just use the first to get that identity.
153      */
154     if (names != NULL) {
155         const char *q = strchr(names, NAME_SEPARATOR);
156
157         l = (q == NULL ? strlen(names) : (size_t)(q - names));
158     }
159
160     if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
161         || (name_id = ossl_namemap_name2num_n(namemap, names, l)) == 0
162         || (meth_id = evp_method_id(name_id, methdata->operation_id)) == 0)
163         return 0;
164
165     if (store == NULL
166         && (store = get_evp_method_store(methdata->libctx)) == NULL)
167         return 0;
168
169     return ossl_method_store_add(store, prov, meth_id, propdef, method,
170                                  methdata->refcnt_up_method,
171                                  methdata->destruct_method);
172 }
173
174 /*
175  * The core fetching functionality passes the name of the implementation.
176  * This function is responsible to getting an identity number for it.
177  */
178 static void *construct_evp_method(const OSSL_ALGORITHM *algodef,
179                                   OSSL_PROVIDER *prov, void *data)
180 {
181     /*
182      * This function is only called if get_evp_method_from_store() returned
183      * NULL, so it's safe to say that of all the spots to create a new
184      * namemap entry, this is it.  Should the name already exist there, we
185      * know that ossl_namemap_add_name() will return its corresponding
186      * number.
187      */
188     struct evp_method_data_st *methdata = data;
189     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
190     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
191     const char *names = algodef->algorithm_names;
192     int name_id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
193     void *method;
194
195     if (name_id == 0)
196         return NULL;
197
198     method = methdata->method_from_algorithm(name_id, algodef, prov);
199
200     /*
201      * Flag to indicate that there was actual construction errors.  This
202      * helps inner_evp_generic_fetch() determine what error it should
203      * record on inaccessible algorithms.
204      */
205     if (method == NULL)
206         methdata->flag_construct_error_occurred = 1;
207
208     return method;
209 }
210
211 static void destruct_evp_method(void *method, void *data)
212 {
213     struct evp_method_data_st *methdata = data;
214
215     methdata->destruct_method(method);
216 }
217
218 static void *
219 inner_evp_generic_fetch(struct evp_method_data_st *methdata,
220                         OSSL_PROVIDER *prov, int operation_id,
221                         int name_id, const char *name,
222                         const char *properties,
223                         void *(*new_method)(int name_id,
224                                             const OSSL_ALGORITHM *algodef,
225                                             OSSL_PROVIDER *prov),
226                         int (*up_ref_method)(void *),
227                         void (*free_method)(void *))
228 {
229     OSSL_METHOD_STORE *store = get_evp_method_store(methdata->libctx);
230     OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
231     const char *const propq = properties != NULL ? properties : "";
232     uint32_t meth_id = 0;
233     void *method = NULL;
234     int unsupported = 0;
235
236     if (store == NULL || namemap == NULL) {
237         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
238         return NULL;
239     }
240
241     /*
242      * If there's ever an operation_id == 0 passed, we have an internal
243      * programming error.
244      */
245     if (!ossl_assert(operation_id > 0)) {
246         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
247         return NULL;
248     }
249
250     /*
251      * If we have been passed both a name_id and a name, we have an
252      * internal programming error.
253      */
254     if (!ossl_assert(name_id == 0 || name == NULL)) {
255         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
256         return NULL;
257     }
258
259     /* If we haven't received a name id yet, try to get one for the name */
260     if (name_id == 0 && name != NULL)
261         name_id = ossl_namemap_name2num(namemap, name);
262
263     /*
264      * If we have a name id, calculate a method id with evp_method_id().
265      *
266      * evp_method_id returns 0 if we have too many operations (more than
267      * about 2^8) or too many names (more than about 2^24).  In that case,
268      * we can't create any new method.
269      * For all intents and purposes, this is an internal error.
270      */
271     if (name_id != 0 && (meth_id = evp_method_id(name_id, operation_id)) == 0) {
272         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
273         return NULL;
274     }
275
276     /*
277      * If we haven't found the name yet, chances are that the algorithm to
278      * be fetched is unsupported.
279      */
280     if (name_id == 0)
281         unsupported = 1;
282
283     if (meth_id == 0
284         || !ossl_method_store_cache_get(store, prov, meth_id, propq, &method)) {
285         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
286             get_tmp_evp_method_store,
287             get_evp_method_from_store,
288             put_evp_method_in_store,
289             construct_evp_method,
290             destruct_evp_method
291         };
292
293         methdata->operation_id = operation_id;
294         methdata->name_id = name_id;
295         methdata->names = name;
296         methdata->propquery = propq;
297         methdata->method_from_algorithm = new_method;
298         methdata->refcnt_up_method = up_ref_method;
299         methdata->destruct_method = free_method;
300         methdata->flag_construct_error_occurred = 0;
301         if ((method = ossl_method_construct(methdata->libctx, operation_id,
302                                             &prov, 0 /* !force_cache */,
303                                             &mcm, methdata)) != NULL) {
304             /*
305              * If construction did create a method for us, we know that
306              * there is a correct name_id and meth_id, since those have
307              * already been calculated in get_evp_method_from_store() and
308              * put_evp_method_in_store() above.
309              */
310             if (name_id == 0)
311                 name_id = ossl_namemap_name2num(namemap, name);
312             meth_id = evp_method_id(name_id, operation_id);
313             if (name_id != 0)
314                 ossl_method_store_cache_set(store, prov, meth_id, propq,
315                                             method, up_ref_method, free_method);
316         }
317
318         /*
319          * If we never were in the constructor, the algorithm to be fetched
320          * is unsupported.
321          */
322         unsupported = !methdata->flag_construct_error_occurred;
323     }
324
325     if ((name_id != 0 || name != NULL) && method == NULL) {
326         int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
327
328         if (name == NULL)
329             name = ossl_namemap_num2name(namemap, name_id, 0);
330         ERR_raise_data(ERR_LIB_EVP, code,
331                        "%s, Algorithm (%s : %d), Properties (%s)",
332                        ossl_lib_ctx_get_descriptor(methdata->libctx),
333                        name == NULL ? "<null>" : name, name_id,
334                        properties == NULL ? "<null>" : properties);
335     }
336
337     return method;
338 }
339
340 void *evp_generic_fetch(OSSL_LIB_CTX *libctx, int operation_id,
341                         const char *name, const char *properties,
342                         void *(*new_method)(int name_id,
343                                             const OSSL_ALGORITHM *algodef,
344                                             OSSL_PROVIDER *prov),
345                         int (*up_ref_method)(void *),
346                         void (*free_method)(void *))
347 {
348     struct evp_method_data_st methdata;
349     void *method;
350
351     methdata.libctx = libctx;
352     methdata.tmp_store = NULL;
353     method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
354                                      0, name, properties,
355                                      new_method, up_ref_method, free_method);
356     dealloc_tmp_evp_method_store(methdata.tmp_store);
357     return method;
358 }
359
360 /*
361  * evp_generic_fetch_by_number() is special, and only returns methods for
362  * already known names, i.e. it refuses to work if no name_id can be found
363  * (it's considered an internal programming error).
364  * This is meant to be used when one method needs to fetch an associated
365  * method.
366  */
367 void *evp_generic_fetch_by_number(OSSL_LIB_CTX *libctx, int operation_id,
368                                   int name_id, const char *properties,
369                                   void *(*new_method)(int name_id,
370                                                       const OSSL_ALGORITHM *algodef,
371                                                       OSSL_PROVIDER *prov),
372                                   int (*up_ref_method)(void *),
373                                   void (*free_method)(void *))
374 {
375     struct evp_method_data_st methdata;
376     void *method;
377
378     methdata.libctx = libctx;
379     methdata.tmp_store = NULL;
380     method = inner_evp_generic_fetch(&methdata, NULL, operation_id,
381                                      name_id, NULL, properties,
382                                      new_method, up_ref_method, free_method);
383     dealloc_tmp_evp_method_store(methdata.tmp_store);
384     return method;
385 }
386
387 /*
388  * evp_generic_fetch_from_prov() is special, and only returns methods from
389  * the given provider.
390  * This is meant to be used when one method needs to fetch an associated
391  * method.
392  */
393 void *evp_generic_fetch_from_prov(OSSL_PROVIDER *prov, int operation_id,
394                                   const char *name, const char *properties,
395                                   void *(*new_method)(int name_id,
396                                                       const OSSL_ALGORITHM *algodef,
397                                                       OSSL_PROVIDER *prov),
398                                   int (*up_ref_method)(void *),
399                                   void (*free_method)(void *))
400 {
401     struct evp_method_data_st methdata;
402     void *method;
403
404     methdata.libctx = ossl_provider_libctx(prov);
405     methdata.tmp_store = NULL;
406     method = inner_evp_generic_fetch(&methdata, prov, operation_id,
407                                      0, name, properties,
408                                      new_method, up_ref_method, free_method);
409     dealloc_tmp_evp_method_store(methdata.tmp_store);
410     return method;
411 }
412
413 int evp_method_store_flush(OSSL_LIB_CTX *libctx)
414 {
415     OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
416
417     if (store != NULL)
418         return ossl_method_store_flush_cache(store, 1);
419     return 1;
420 }
421
422 static int evp_set_parsed_default_properties(OSSL_LIB_CTX *libctx,
423                                              OSSL_PROPERTY_LIST *def_prop,
424                                              int loadconfig,
425                                              int mirrored)
426 {
427     OSSL_METHOD_STORE *store = get_evp_method_store(libctx);
428     OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
429
430     if (plp != NULL && store != NULL) {
431 #ifndef FIPS_MODULE
432         char *propstr = NULL;
433         size_t strsz;
434
435         if (mirrored) {
436             if (ossl_global_properties_no_mirrored(libctx))
437                 return 0;
438         } else {
439             /*
440              * These properties have been explicitly set on this libctx, so
441              * don't allow any mirroring from a parent libctx.
442              */
443             ossl_global_properties_stop_mirroring(libctx);
444         }
445
446         strsz = ossl_property_list_to_string(libctx, def_prop, NULL, 0);
447         if (strsz > 0)
448             propstr = OPENSSL_malloc(strsz);
449         if (propstr == NULL) {
450             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
451             return 0;
452         }
453         if (ossl_property_list_to_string(libctx, def_prop, propstr,
454                                          strsz) == 0) {
455             OPENSSL_free(propstr);
456             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
457             return 0;
458         }
459         ossl_provider_default_props_update(libctx, propstr);
460         OPENSSL_free(propstr);
461 #endif
462         ossl_property_free(*plp);
463         *plp = def_prop;
464         if (store != NULL)
465             return ossl_method_store_flush_cache(store, 0);
466     }
467     ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
468     return 0;
469 }
470
471 int evp_set_default_properties_int(OSSL_LIB_CTX *libctx, const char *propq,
472                                    int loadconfig, int mirrored)
473 {
474     OSSL_PROPERTY_LIST *pl = NULL;
475
476     if (propq != NULL && (pl = ossl_parse_query(libctx, propq, 1)) == NULL) {
477         ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
478         return 0;
479     }
480     if (!evp_set_parsed_default_properties(libctx, pl, loadconfig, mirrored)) {
481         ossl_property_free(pl);
482         return 0;
483     }
484     return 1;
485 }
486
487 int EVP_set_default_properties(OSSL_LIB_CTX *libctx, const char *propq)
488 {
489     return evp_set_default_properties_int(libctx, propq, 1, 0);
490 }
491
492 static int evp_default_properties_merge(OSSL_LIB_CTX *libctx, const char *propq,
493                                         int loadconfig)
494 {
495     OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
496     OSSL_PROPERTY_LIST *pl1, *pl2;
497
498     if (propq == NULL)
499         return 1;
500     if (plp == NULL || *plp == NULL)
501         return evp_set_default_properties_int(libctx, propq, 0, 0);
502     if ((pl1 = ossl_parse_query(libctx, propq, 1)) == NULL) {
503         ERR_raise(ERR_LIB_EVP, EVP_R_DEFAULT_QUERY_PARSE_ERROR);
504         return 0;
505     }
506     pl2 = ossl_property_merge(pl1, *plp);
507     ossl_property_free(pl1);
508     if (pl2 == NULL) {
509         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
510         return 0;
511     }
512     if (!evp_set_parsed_default_properties(libctx, pl2, 0, 0)) {
513         ossl_property_free(pl2);
514         return 0;
515     }
516     return 1;
517 }
518
519 static int evp_default_property_is_enabled(OSSL_LIB_CTX *libctx,
520                                            const char *prop_name)
521 {
522     OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, 1);
523
524     return plp != NULL && ossl_property_is_enabled(libctx, prop_name, *plp);
525 }
526
527 int EVP_default_properties_is_fips_enabled(OSSL_LIB_CTX *libctx)
528 {
529     return evp_default_property_is_enabled(libctx, "fips");
530 }
531
532 int evp_default_properties_enable_fips_int(OSSL_LIB_CTX *libctx, int enable,
533                                            int loadconfig)
534 {
535     const char *query = (enable != 0) ? "fips=yes" : "-fips";
536
537     return evp_default_properties_merge(libctx, query, loadconfig);
538 }
539
540 int EVP_default_properties_enable_fips(OSSL_LIB_CTX *libctx, int enable)
541 {
542     return evp_default_properties_enable_fips_int(libctx, enable, 1);
543 }
544
545 char *evp_get_global_properties_str(OSSL_LIB_CTX *libctx, int loadconfig)
546 {
547     OSSL_PROPERTY_LIST **plp = ossl_ctx_global_properties(libctx, loadconfig);
548     char *propstr = NULL;
549     size_t sz;
550
551     if (plp == NULL)
552         return OPENSSL_strdup("");
553
554     sz = ossl_property_list_to_string(libctx, *plp, NULL, 0);
555     if (sz == 0) {
556         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
557         return NULL;
558     }
559
560     propstr = OPENSSL_malloc(sz);
561     if (propstr == NULL) {
562         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
563         return NULL;
564     }
565     if (ossl_property_list_to_string(libctx, *plp, propstr, sz) == 0) {
566         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
567         OPENSSL_free(propstr);
568         return NULL;
569     }
570     return propstr;
571 }
572
573 struct filter_data_st {
574     int operation_id;
575     void (*user_fn)(void *method, void *arg);
576     void *user_arg;
577 };
578
579 static void filter_on_operation_id(int id, void *method, void *arg)
580 {
581     struct filter_data_st *data = arg;
582
583     if ((id & METHOD_ID_OPERATION_MASK) == data->operation_id)
584         data->user_fn(method, data->user_arg);
585 }
586
587 void evp_generic_do_all(OSSL_LIB_CTX *libctx, int operation_id,
588                         void (*user_fn)(void *method, void *arg),
589                         void *user_arg,
590                         void *(*new_method)(int name_id,
591                                             const OSSL_ALGORITHM *algodef,
592                                             OSSL_PROVIDER *prov),
593                         int (*up_ref_method)(void *),
594                         void (*free_method)(void *))
595 {
596     struct evp_method_data_st methdata;
597     struct filter_data_st data;
598
599     methdata.libctx = libctx;
600     methdata.tmp_store = NULL;
601     (void)inner_evp_generic_fetch(&methdata, NULL, operation_id, 0, NULL, NULL,
602                                   new_method, up_ref_method, free_method);
603
604     data.operation_id = operation_id;
605     data.user_fn = user_fn;
606     data.user_arg = user_arg;
607     if (methdata.tmp_store != NULL)
608         ossl_method_store_do_all(methdata.tmp_store, &filter_on_operation_id,
609                                  &data);
610     ossl_method_store_do_all(get_evp_method_store(libctx),
611                              &filter_on_operation_id, &data);
612     dealloc_tmp_evp_method_store(methdata.tmp_store);
613 }
614
615 int evp_is_a(OSSL_PROVIDER *prov, int number,
616              const char *legacy_name, const char *name)
617 {
618     /*
619      * For a |prov| that is NULL, the library context will be NULL
620      */
621     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
622     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
623
624     if (prov == NULL)
625         number = ossl_namemap_name2num(namemap, legacy_name);
626     return ossl_namemap_name2num(namemap, name) == number;
627 }
628
629 int evp_names_do_all(OSSL_PROVIDER *prov, int number,
630                      void (*fn)(const char *name, void *data),
631                      void *data)
632 {
633     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
634     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
635
636     return ossl_namemap_doall_names(namemap, number, fn, data);
637 }