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