xref: /freebsd/crypto/openssl/crypto/store/store_meth.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1 /*
2  * Copyright 2020-2025 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 <openssl/crypto.h>
11 #include "crypto/store.h"
12 #include "internal/core.h"
13 #include "internal/namemap.h"
14 #include "internal/property.h"
15 #include "internal/provider.h"
16 #include "store_local.h"
17 #include "crypto/context.h"
18 
OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER * loader)19 int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER *loader)
20 {
21     int ref = 0;
22 
23     if (loader->prov != NULL)
24         CRYPTO_UP_REF(&loader->refcnt, &ref);
25     return 1;
26 }
27 
OSSL_STORE_LOADER_free(OSSL_STORE_LOADER * loader)28 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
29 {
30     if (loader != NULL && loader->prov != NULL) {
31         int i;
32 
33         CRYPTO_DOWN_REF(&loader->refcnt, &i);
34         if (i > 0)
35             return;
36         ossl_provider_free(loader->prov);
37         CRYPTO_FREE_REF(&loader->refcnt);
38     }
39     OPENSSL_free(loader);
40 }
41 
42 /*
43  * OSSL_STORE_LOADER_new() expects the scheme as a constant string,
44  * which we currently don't have, so we need an alternative allocator.
45  */
new_loader(OSSL_PROVIDER * prov)46 static OSSL_STORE_LOADER *new_loader(OSSL_PROVIDER *prov)
47 {
48     OSSL_STORE_LOADER *loader;
49 
50     if ((loader = OPENSSL_zalloc(sizeof(*loader))) == NULL
51         || !CRYPTO_NEW_REF(&loader->refcnt, 1)
52         || !ossl_provider_up_ref(prov)) {
53         if (loader != NULL)
54             CRYPTO_FREE_REF(&loader->refcnt);
55         OPENSSL_free(loader);
56         return NULL;
57     }
58 
59     loader->prov = prov;
60 
61     return loader;
62 }
63 
up_ref_loader(void * method)64 static int up_ref_loader(void *method)
65 {
66     return OSSL_STORE_LOADER_up_ref(method);
67 }
68 
free_loader(void * method)69 static void free_loader(void *method)
70 {
71     OSSL_STORE_LOADER_free(method);
72 }
73 
74 /* Data to be passed through ossl_method_construct() */
75 struct loader_data_st {
76     OSSL_LIB_CTX *libctx;
77     int scheme_id;               /* For get_loader_from_store() */
78     const char *scheme;          /* For get_loader_from_store() */
79     const char *propquery;       /* For get_loader_from_store() */
80 
81     OSSL_METHOD_STORE *tmp_store; /* For get_tmp_loader_store() */
82 
83     unsigned int flag_construct_error_occurred : 1;
84 };
85 
86 /*
87  * Generic routines to fetch / create OSSL_STORE methods with
88  * ossl_method_construct()
89  */
90 
91 /* Temporary loader method store, constructor and destructor */
get_tmp_loader_store(void * data)92 static void *get_tmp_loader_store(void *data)
93 {
94     struct loader_data_st *methdata = data;
95 
96     if (methdata->tmp_store == NULL)
97         methdata->tmp_store = ossl_method_store_new(methdata->libctx);
98     return methdata->tmp_store;
99 }
100 
dealloc_tmp_loader_store(void * store)101  static void dealloc_tmp_loader_store(void *store)
102 {
103     if (store != NULL)
104         ossl_method_store_free(store);
105 }
106 
107 /* Get the permanent loader store */
get_loader_store(OSSL_LIB_CTX * libctx)108 static OSSL_METHOD_STORE *get_loader_store(OSSL_LIB_CTX *libctx)
109 {
110     return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX);
111 }
112 
reserve_loader_store(void * store,void * data)113 static int reserve_loader_store(void *store, void *data)
114 {
115     struct loader_data_st *methdata = data;
116 
117     if (store == NULL
118         && (store = get_loader_store(methdata->libctx)) == NULL)
119         return 0;
120 
121     return ossl_method_lock_store(store);
122 }
123 
unreserve_loader_store(void * store,void * data)124 static int unreserve_loader_store(void *store, void *data)
125 {
126     struct loader_data_st *methdata = data;
127 
128     if (store == NULL
129         && (store = get_loader_store(methdata->libctx)) == NULL)
130         return 0;
131 
132     return ossl_method_unlock_store(store);
133 }
134 
135 /* Get loader methods from a store, or put one in */
get_loader_from_store(void * store,const OSSL_PROVIDER ** prov,void * data)136 static void *get_loader_from_store(void *store, const OSSL_PROVIDER **prov,
137                                    void *data)
138 {
139     struct loader_data_st *methdata = data;
140     void *method = NULL;
141     int id;
142 
143     if ((id = methdata->scheme_id) == 0) {
144         OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
145 
146         id = ossl_namemap_name2num(namemap, methdata->scheme);
147     }
148 
149     if (store == NULL
150         && (store = get_loader_store(methdata->libctx)) == NULL)
151         return NULL;
152 
153     if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method))
154         return NULL;
155     return method;
156 }
157 
put_loader_in_store(void * store,void * method,const OSSL_PROVIDER * prov,const char * scheme,const char * propdef,void * data)158 static int put_loader_in_store(void *store, void *method,
159                                const OSSL_PROVIDER *prov,
160                                const char *scheme, const char *propdef,
161                                void *data)
162 {
163     struct loader_data_st *methdata = data;
164     OSSL_NAMEMAP *namemap;
165     int id;
166 
167     if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
168         || (id = ossl_namemap_name2num(namemap, scheme)) == 0)
169         return 0;
170 
171     if (store == NULL && (store = get_loader_store(methdata->libctx)) == NULL)
172         return 0;
173 
174     return ossl_method_store_add(store, prov, id, propdef, method,
175                                  up_ref_loader, free_loader);
176 }
177 
loader_from_algorithm(int scheme_id,const OSSL_ALGORITHM * algodef,OSSL_PROVIDER * prov)178 static void *loader_from_algorithm(int scheme_id, const OSSL_ALGORITHM *algodef,
179                                    OSSL_PROVIDER *prov)
180 {
181     OSSL_STORE_LOADER *loader = NULL;
182     const OSSL_DISPATCH *fns = algodef->implementation;
183 
184     if ((loader = new_loader(prov)) == NULL)
185         return NULL;
186     loader->scheme_id = scheme_id;
187     loader->propdef = algodef->property_definition;
188     loader->description = algodef->algorithm_description;
189 
190     for (; fns->function_id != 0; fns++) {
191         switch (fns->function_id) {
192         case OSSL_FUNC_STORE_OPEN:
193             if (loader->p_open == NULL)
194                 loader->p_open = OSSL_FUNC_store_open(fns);
195             break;
196         case OSSL_FUNC_STORE_ATTACH:
197             if (loader->p_attach == NULL)
198                 loader->p_attach = OSSL_FUNC_store_attach(fns);
199             break;
200         case OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS:
201             if (loader->p_settable_ctx_params == NULL)
202                 loader->p_settable_ctx_params =
203                     OSSL_FUNC_store_settable_ctx_params(fns);
204             break;
205         case OSSL_FUNC_STORE_SET_CTX_PARAMS:
206             if (loader->p_set_ctx_params == NULL)
207                 loader->p_set_ctx_params = OSSL_FUNC_store_set_ctx_params(fns);
208             break;
209         case OSSL_FUNC_STORE_LOAD:
210             if (loader->p_load == NULL)
211                 loader->p_load = OSSL_FUNC_store_load(fns);
212             break;
213         case OSSL_FUNC_STORE_EOF:
214             if (loader->p_eof == NULL)
215                 loader->p_eof = OSSL_FUNC_store_eof(fns);
216             break;
217         case OSSL_FUNC_STORE_CLOSE:
218             if (loader->p_close == NULL)
219                 loader->p_close = OSSL_FUNC_store_close(fns);
220             break;
221         case OSSL_FUNC_STORE_EXPORT_OBJECT:
222             if (loader->p_export_object == NULL)
223                 loader->p_export_object = OSSL_FUNC_store_export_object(fns);
224             break;
225         case OSSL_FUNC_STORE_DELETE:
226             if (loader->p_delete == NULL)
227                 loader->p_delete = OSSL_FUNC_store_delete(fns);
228             break;
229         case OSSL_FUNC_STORE_OPEN_EX:
230             if (loader->p_open_ex == NULL)
231                 loader->p_open_ex = OSSL_FUNC_store_open_ex(fns);
232             break;
233         }
234     }
235 
236     if ((loader->p_open == NULL && loader->p_attach == NULL)
237         || loader->p_load == NULL
238         || loader->p_eof == NULL
239         || loader->p_close == NULL) {
240         /* Only set_ctx_params is optional */
241         OSSL_STORE_LOADER_free(loader);
242         ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
243         return NULL;
244     }
245     return loader;
246 }
247 
248 /*
249  * The core fetching functionality passes the scheme of the implementation.
250  * This function is responsible to getting an identity number for them,
251  * then call loader_from_algorithm() with that identity number.
252  */
construct_loader(const OSSL_ALGORITHM * algodef,OSSL_PROVIDER * prov,void * data)253 static void *construct_loader(const OSSL_ALGORITHM *algodef,
254                               OSSL_PROVIDER *prov, void *data)
255 {
256     /*
257      * This function is only called if get_loader_from_store() returned
258      * NULL, so it's safe to say that of all the spots to create a new
259      * namemap entry, this is it.  Should the scheme already exist there, we
260      * know that ossl_namemap_add() will return its corresponding number.
261      */
262     struct loader_data_st *methdata = data;
263     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
264     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
265     const char *scheme = algodef->algorithm_names;
266     int id = ossl_namemap_add_name(namemap, 0, scheme);
267     void *method = NULL;
268 
269     if (id != 0)
270         method = loader_from_algorithm(id, algodef, prov);
271 
272     /*
273      * Flag to indicate that there was actual construction errors.  This
274      * helps inner_loader_fetch() determine what error it should
275      * record on inaccessible algorithms.
276      */
277     if (method == NULL)
278         methdata->flag_construct_error_occurred = 1;
279 
280     return method;
281 }
282 
283 /* Intermediary function to avoid ugly casts, used below */
destruct_loader(void * method,void * data)284 static void destruct_loader(void *method, void *data)
285 {
286     OSSL_STORE_LOADER_free(method);
287 }
288 
289 /* Fetching support.  Can fetch by numeric identity or by scheme */
290 static OSSL_STORE_LOADER *
inner_loader_fetch(struct loader_data_st * methdata,const char * scheme,const char * properties)291 inner_loader_fetch(struct loader_data_st *methdata,
292                    const char *scheme, const char *properties)
293 {
294     OSSL_METHOD_STORE *store = get_loader_store(methdata->libctx);
295     OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
296     const char *const propq = properties != NULL ? properties : "";
297     void *method = NULL;
298     int unsupported, id;
299 
300     if (store == NULL || namemap == NULL) {
301         ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
302         return NULL;
303     }
304 
305     /* If we haven't received a name id yet, try to get one for the name */
306     id = scheme != NULL ? ossl_namemap_name2num(namemap, scheme) : 0;
307 
308     /*
309      * If we haven't found the name yet, chances are that the algorithm to
310      * be fetched is unsupported.
311      */
312     unsupported = id == 0;
313 
314     if (id == 0
315         || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) {
316         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
317             get_tmp_loader_store,
318             reserve_loader_store,
319             unreserve_loader_store,
320             get_loader_from_store,
321             put_loader_in_store,
322             construct_loader,
323             destruct_loader
324         };
325         OSSL_PROVIDER *prov = NULL;
326 
327         methdata->scheme_id = id;
328         methdata->scheme = scheme;
329         methdata->propquery = propq;
330         methdata->flag_construct_error_occurred = 0;
331         if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_STORE,
332                                             &prov, 0 /* !force_cache */,
333                                             &mcm, methdata)) != NULL) {
334             /*
335              * If construction did create a method for us, we know that there
336              * is a correct scheme_id, since those have already been calculated
337              * in get_loader_from_store() and put_loader_in_store() above.
338              */
339             if (id == 0)
340                 id = ossl_namemap_name2num(namemap, scheme);
341             ossl_method_store_cache_set(store, prov, id, propq, method,
342                                         up_ref_loader, free_loader);
343         }
344 
345         /*
346          * If we never were in the constructor, the algorithm to be fetched
347          * is unsupported.
348          */
349         unsupported = !methdata->flag_construct_error_occurred;
350     }
351 
352     if ((id != 0 || scheme != NULL) && method == NULL) {
353         int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
354         const char *helpful_msg =
355             unsupported
356             ? ( "No store loader found. For standard store loaders you need "
357                 "at least one of the default or base providers available. "
358                 "Did you forget to load them? Info: " )
359             : "";
360 
361         if (scheme == NULL)
362             scheme = ossl_namemap_num2name(namemap, id, 0);
363         ERR_raise_data(ERR_LIB_OSSL_STORE, code,
364                        "%s%s, Scheme (%s : %d), Properties (%s)",
365                        helpful_msg,
366                        ossl_lib_ctx_get_descriptor(methdata->libctx),
367                        scheme == NULL ? "<null>" : scheme, id,
368                        properties == NULL ? "<null>" : properties);
369     }
370 
371     return method;
372 }
373 
OSSL_STORE_LOADER_fetch(OSSL_LIB_CTX * libctx,const char * scheme,const char * properties)374 OSSL_STORE_LOADER *OSSL_STORE_LOADER_fetch(OSSL_LIB_CTX *libctx,
375                                            const char *scheme,
376                                            const char *properties)
377 {
378     struct loader_data_st methdata;
379     void *method;
380 
381     methdata.libctx = libctx;
382     methdata.tmp_store = NULL;
383     method = inner_loader_fetch(&methdata, scheme, properties);
384     dealloc_tmp_loader_store(methdata.tmp_store);
385     return method;
386 }
387 
ossl_store_loader_store_cache_flush(OSSL_LIB_CTX * libctx)388 int ossl_store_loader_store_cache_flush(OSSL_LIB_CTX *libctx)
389 {
390     OSSL_METHOD_STORE *store = get_loader_store(libctx);
391 
392     if (store != NULL)
393         return ossl_method_store_cache_flush_all(store);
394     return 1;
395 }
396 
ossl_store_loader_store_remove_all_provided(const OSSL_PROVIDER * prov)397 int ossl_store_loader_store_remove_all_provided(const OSSL_PROVIDER *prov)
398 {
399     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
400     OSSL_METHOD_STORE *store = get_loader_store(libctx);
401 
402     if (store != NULL)
403         return ossl_method_store_remove_all_provided(store, prov);
404     return 1;
405 }
406 
407 /*
408  * Library of basic method functions
409  */
410 
OSSL_STORE_LOADER_get0_provider(const OSSL_STORE_LOADER * loader)411 const OSSL_PROVIDER *OSSL_STORE_LOADER_get0_provider(const OSSL_STORE_LOADER *loader)
412 {
413     if (!ossl_assert(loader != NULL)) {
414         ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
415         return 0;
416     }
417 
418     return loader->prov;
419 }
420 
OSSL_STORE_LOADER_get0_properties(const OSSL_STORE_LOADER * loader)421 const char *OSSL_STORE_LOADER_get0_properties(const OSSL_STORE_LOADER *loader)
422 {
423     if (!ossl_assert(loader != NULL)) {
424         ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
425         return 0;
426     }
427 
428     return loader->propdef;
429 }
430 
ossl_store_loader_get_number(const OSSL_STORE_LOADER * loader)431 int ossl_store_loader_get_number(const OSSL_STORE_LOADER *loader)
432 {
433     if (!ossl_assert(loader != NULL)) {
434         ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
435         return 0;
436     }
437 
438     return loader->scheme_id;
439 }
440 
OSSL_STORE_LOADER_get0_description(const OSSL_STORE_LOADER * loader)441 const char *OSSL_STORE_LOADER_get0_description(const OSSL_STORE_LOADER *loader)
442 {
443     return loader->description;
444 }
445 
OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER * loader,const char * name)446 int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER *loader, const char *name)
447 {
448     if (loader->prov != NULL) {
449         OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
450         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
451 
452         return ossl_namemap_name2num(namemap, name) == loader->scheme_id;
453     }
454     return 0;
455 }
456 
457 struct do_one_data_st {
458     void (*user_fn)(OSSL_STORE_LOADER *loader, void *arg);
459     void *user_arg;
460 };
461 
do_one(ossl_unused int id,void * method,void * arg)462 static void do_one(ossl_unused int id, void *method, void *arg)
463 {
464     struct do_one_data_st *data = arg;
465 
466     data->user_fn(method, data->user_arg);
467 }
468 
OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX * libctx,void (* user_fn)(OSSL_STORE_LOADER * loader,void * arg),void * user_arg)469 void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX *libctx,
470                                        void (*user_fn)(OSSL_STORE_LOADER *loader,
471                                                        void *arg),
472                                        void *user_arg)
473 {
474     struct loader_data_st methdata;
475     struct do_one_data_st data;
476 
477     methdata.libctx = libctx;
478     methdata.tmp_store = NULL;
479     (void)inner_loader_fetch(&methdata, NULL, NULL /* properties */);
480 
481     data.user_fn = user_fn;
482     data.user_arg = user_arg;
483     if (methdata.tmp_store != NULL)
484         ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
485     ossl_method_store_do_all(get_loader_store(libctx), &do_one, &data);
486     dealloc_tmp_loader_store(methdata.tmp_store);
487 }
488 
OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER * loader,void (* fn)(const char * name,void * data),void * data)489 int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER *loader,
490                                    void (*fn)(const char *name, void *data),
491                                    void *data)
492 {
493     if (loader == NULL)
494         return 0;
495 
496     if (loader->prov != NULL) {
497         OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
498         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
499 
500         return ossl_namemap_doall_names(namemap, loader->scheme_id, fn, data);
501     }
502 
503     return 1;
504 }
505