2 * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
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
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"
19 int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER
*loader
)
23 if (loader
->prov
!= NULL
)
24 CRYPTO_UP_REF(&loader
->refcnt
, &ref
);
28 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER
*loader
)
30 if (loader
!= NULL
&& loader
->prov
!= NULL
) {
33 CRYPTO_DOWN_REF(&loader
->refcnt
, &i
);
36 ossl_provider_free(loader
->prov
);
37 CRYPTO_FREE_REF(&loader
->refcnt
);
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.
46 static OSSL_STORE_LOADER
*new_loader(OSSL_PROVIDER
*prov
)
48 OSSL_STORE_LOADER
*loader
;
50 if ((loader
= OPENSSL_zalloc(sizeof(*loader
))) == NULL
51 || !CRYPTO_NEW_REF(&loader
->refcnt
, 1)) {
56 ossl_provider_up_ref(prov
);
61 static int up_ref_loader(void *method
)
63 return OSSL_STORE_LOADER_up_ref(method
);
66 static void free_loader(void *method
)
68 OSSL_STORE_LOADER_free(method
);
71 /* Data to be passed through ossl_method_construct() */
72 struct loader_data_st
{
74 int scheme_id
; /* For get_loader_from_store() */
75 const char *scheme
; /* For get_loader_from_store() */
76 const char *propquery
; /* For get_loader_from_store() */
78 OSSL_METHOD_STORE
*tmp_store
; /* For get_tmp_loader_store() */
80 unsigned int flag_construct_error_occurred
: 1;
84 * Generic routines to fetch / create OSSL_STORE methods with
85 * ossl_method_construct()
88 /* Temporary loader method store, constructor and destructor */
89 static void *get_tmp_loader_store(void *data
)
91 struct loader_data_st
*methdata
= data
;
93 if (methdata
->tmp_store
== NULL
)
94 methdata
->tmp_store
= ossl_method_store_new(methdata
->libctx
);
95 return methdata
->tmp_store
;
98 static void dealloc_tmp_loader_store(void *store
)
101 ossl_method_store_free(store
);
104 /* Get the permanent loader store */
105 static OSSL_METHOD_STORE
*get_loader_store(OSSL_LIB_CTX
*libctx
)
107 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX
);
110 static int reserve_loader_store(void *store
, void *data
)
112 struct loader_data_st
*methdata
= data
;
115 && (store
= get_loader_store(methdata
->libctx
)) == NULL
)
118 return ossl_method_lock_store(store
);
121 static int unreserve_loader_store(void *store
, void *data
)
123 struct loader_data_st
*methdata
= data
;
126 && (store
= get_loader_store(methdata
->libctx
)) == NULL
)
129 return ossl_method_unlock_store(store
);
132 /* Get loader methods from a store, or put one in */
133 static void *get_loader_from_store(void *store
, const OSSL_PROVIDER
**prov
,
136 struct loader_data_st
*methdata
= data
;
140 if ((id
= methdata
->scheme_id
) == 0) {
141 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
143 id
= ossl_namemap_name2num(namemap
, methdata
->scheme
);
147 && (store
= get_loader_store(methdata
->libctx
)) == NULL
)
150 if (!ossl_method_store_fetch(store
, id
, methdata
->propquery
, prov
, &method
))
155 static int put_loader_in_store(void *store
, void *method
,
156 const OSSL_PROVIDER
*prov
,
157 const char *scheme
, const char *propdef
,
160 struct loader_data_st
*methdata
= data
;
161 OSSL_NAMEMAP
*namemap
;
164 if ((namemap
= ossl_namemap_stored(methdata
->libctx
)) == NULL
165 || (id
= ossl_namemap_name2num(namemap
, scheme
)) == 0)
168 if (store
== NULL
&& (store
= get_loader_store(methdata
->libctx
)) == NULL
)
171 return ossl_method_store_add(store
, prov
, id
, propdef
, method
,
172 up_ref_loader
, free_loader
);
175 static void *loader_from_algorithm(int scheme_id
, const OSSL_ALGORITHM
*algodef
,
178 OSSL_STORE_LOADER
*loader
= NULL
;
179 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
181 if ((loader
= new_loader(prov
)) == NULL
)
183 loader
->scheme_id
= scheme_id
;
184 loader
->propdef
= algodef
->property_definition
;
185 loader
->description
= algodef
->algorithm_description
;
187 for (; fns
->function_id
!= 0; fns
++) {
188 switch (fns
->function_id
) {
189 case OSSL_FUNC_STORE_OPEN
:
190 if (loader
->p_open
== NULL
)
191 loader
->p_open
= OSSL_FUNC_store_open(fns
);
193 case OSSL_FUNC_STORE_ATTACH
:
194 if (loader
->p_attach
== NULL
)
195 loader
->p_attach
= OSSL_FUNC_store_attach(fns
);
197 case OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS
:
198 if (loader
->p_settable_ctx_params
== NULL
)
199 loader
->p_settable_ctx_params
=
200 OSSL_FUNC_store_settable_ctx_params(fns
);
202 case OSSL_FUNC_STORE_SET_CTX_PARAMS
:
203 if (loader
->p_set_ctx_params
== NULL
)
204 loader
->p_set_ctx_params
= OSSL_FUNC_store_set_ctx_params(fns
);
206 case OSSL_FUNC_STORE_LOAD
:
207 if (loader
->p_load
== NULL
)
208 loader
->p_load
= OSSL_FUNC_store_load(fns
);
210 case OSSL_FUNC_STORE_EOF
:
211 if (loader
->p_eof
== NULL
)
212 loader
->p_eof
= OSSL_FUNC_store_eof(fns
);
214 case OSSL_FUNC_STORE_CLOSE
:
215 if (loader
->p_close
== NULL
)
216 loader
->p_close
= OSSL_FUNC_store_close(fns
);
218 case OSSL_FUNC_STORE_EXPORT_OBJECT
:
219 if (loader
->p_export_object
== NULL
)
220 loader
->p_export_object
= OSSL_FUNC_store_export_object(fns
);
225 if ((loader
->p_open
== NULL
&& loader
->p_attach
== NULL
)
226 || loader
->p_load
== NULL
227 || loader
->p_eof
== NULL
228 || loader
->p_close
== NULL
) {
229 /* Only set_ctx_params is optionaal */
230 OSSL_STORE_LOADER_free(loader
);
231 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADER_INCOMPLETE
);
238 * The core fetching functionality passes the scheme of the implementation.
239 * This function is responsible to getting an identity number for them,
240 * then call loader_from_algorithm() with that identity number.
242 static void *construct_loader(const OSSL_ALGORITHM
*algodef
,
243 OSSL_PROVIDER
*prov
, void *data
)
246 * This function is only called if get_loader_from_store() returned
247 * NULL, so it's safe to say that of all the spots to create a new
248 * namemap entry, this is it. Should the scheme already exist there, we
249 * know that ossl_namemap_add() will return its corresponding number.
251 struct loader_data_st
*methdata
= data
;
252 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
253 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
254 const char *scheme
= algodef
->algorithm_names
;
255 int id
= ossl_namemap_add_name(namemap
, 0, scheme
);
259 method
= loader_from_algorithm(id
, algodef
, prov
);
262 * Flag to indicate that there was actual construction errors. This
263 * helps inner_loader_fetch() determine what error it should
264 * record on inaccessible algorithms.
267 methdata
->flag_construct_error_occurred
= 1;
272 /* Intermediary function to avoid ugly casts, used below */
273 static void destruct_loader(void *method
, void *data
)
275 OSSL_STORE_LOADER_free(method
);
278 /* Fetching support. Can fetch by numeric identity or by scheme */
279 static OSSL_STORE_LOADER
*
280 inner_loader_fetch(struct loader_data_st
*methdata
,
281 const char *scheme
, const char *properties
)
283 OSSL_METHOD_STORE
*store
= get_loader_store(methdata
->libctx
);
284 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
285 const char *const propq
= properties
!= NULL
? properties
: "";
289 if (store
== NULL
|| namemap
== NULL
) {
290 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_INVALID_ARGUMENT
);
294 /* If we haven't received a name id yet, try to get one for the name */
295 id
= scheme
!= NULL
? ossl_namemap_name2num(namemap
, scheme
) : 0;
298 * If we haven't found the name yet, chances are that the algorithm to
299 * be fetched is unsupported.
301 unsupported
= id
== 0;
304 || !ossl_method_store_cache_get(store
, NULL
, id
, propq
, &method
)) {
305 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
306 get_tmp_loader_store
,
307 reserve_loader_store
,
308 unreserve_loader_store
,
309 get_loader_from_store
,
314 OSSL_PROVIDER
*prov
= NULL
;
316 methdata
->scheme_id
= id
;
317 methdata
->scheme
= scheme
;
318 methdata
->propquery
= propq
;
319 methdata
->flag_construct_error_occurred
= 0;
320 if ((method
= ossl_method_construct(methdata
->libctx
, OSSL_OP_STORE
,
321 &prov
, 0 /* !force_cache */,
322 &mcm
, methdata
)) != NULL
) {
324 * If construction did create a method for us, we know that there
325 * is a correct scheme_id, since those have already been calculated
326 * in get_loader_from_store() and put_loader_in_store() above.
329 id
= ossl_namemap_name2num(namemap
, scheme
);
330 ossl_method_store_cache_set(store
, prov
, id
, propq
, method
,
331 up_ref_loader
, free_loader
);
335 * If we never were in the constructor, the algorithm to be fetched
338 unsupported
= !methdata
->flag_construct_error_occurred
;
341 if ((id
!= 0 || scheme
!= NULL
) && method
== NULL
) {
342 int code
= unsupported
? ERR_R_UNSUPPORTED
: ERR_R_FETCH_FAILED
;
343 const char *helpful_msg
=
345 ? ( "No store loader found. For standard store loaders you need "
346 "at least one of the default or base providers available. "
347 "Did you forget to load them? Info: " )
351 scheme
= ossl_namemap_num2name(namemap
, id
, 0);
352 ERR_raise_data(ERR_LIB_OSSL_STORE
, code
,
353 "%s%s, Scheme (%s : %d), Properties (%s)",
355 ossl_lib_ctx_get_descriptor(methdata
->libctx
),
356 scheme
== NULL
? "<null>" : scheme
, id
,
357 properties
== NULL
? "<null>" : properties
);
363 OSSL_STORE_LOADER
*OSSL_STORE_LOADER_fetch(OSSL_LIB_CTX
*libctx
,
365 const char *properties
)
367 struct loader_data_st methdata
;
370 methdata
.libctx
= libctx
;
371 methdata
.tmp_store
= NULL
;
372 method
= inner_loader_fetch(&methdata
, scheme
, properties
);
373 dealloc_tmp_loader_store(methdata
.tmp_store
);
377 int ossl_store_loader_store_cache_flush(OSSL_LIB_CTX
*libctx
)
379 OSSL_METHOD_STORE
*store
= get_loader_store(libctx
);
382 return ossl_method_store_cache_flush_all(store
);
386 int ossl_store_loader_store_remove_all_provided(const OSSL_PROVIDER
*prov
)
388 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
389 OSSL_METHOD_STORE
*store
= get_loader_store(libctx
);
392 return ossl_method_store_remove_all_provided(store
, prov
);
397 * Library of basic method functions
400 const OSSL_PROVIDER
*OSSL_STORE_LOADER_get0_provider(const OSSL_STORE_LOADER
*loader
)
402 if (!ossl_assert(loader
!= NULL
)) {
403 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
410 const char *OSSL_STORE_LOADER_get0_properties(const OSSL_STORE_LOADER
*loader
)
412 if (!ossl_assert(loader
!= NULL
)) {
413 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
417 return loader
->propdef
;
420 int ossl_store_loader_get_number(const OSSL_STORE_LOADER
*loader
)
422 if (!ossl_assert(loader
!= NULL
)) {
423 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
427 return loader
->scheme_id
;
430 const char *OSSL_STORE_LOADER_get0_description(const OSSL_STORE_LOADER
*loader
)
432 return loader
->description
;
435 int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER
*loader
, const char *name
)
437 if (loader
->prov
!= NULL
) {
438 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(loader
->prov
);
439 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
441 return ossl_namemap_name2num(namemap
, name
) == loader
->scheme_id
;
446 struct do_one_data_st
{
447 void (*user_fn
)(OSSL_STORE_LOADER
*loader
, void *arg
);
451 static void do_one(ossl_unused
int id
, void *method
, void *arg
)
453 struct do_one_data_st
*data
= arg
;
455 data
->user_fn(method
, data
->user_arg
);
458 void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX
*libctx
,
459 void (*user_fn
)(OSSL_STORE_LOADER
*loader
,
463 struct loader_data_st methdata
;
464 struct do_one_data_st data
;
466 methdata
.libctx
= libctx
;
467 methdata
.tmp_store
= NULL
;
468 (void)inner_loader_fetch(&methdata
, NULL
, NULL
/* properties */);
470 data
.user_fn
= user_fn
;
471 data
.user_arg
= user_arg
;
472 if (methdata
.tmp_store
!= NULL
)
473 ossl_method_store_do_all(methdata
.tmp_store
, &do_one
, &data
);
474 ossl_method_store_do_all(get_loader_store(libctx
), &do_one
, &data
);
475 dealloc_tmp_loader_store(methdata
.tmp_store
);
478 int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER
*loader
,
479 void (*fn
)(const char *name
, void *data
),
485 if (loader
->prov
!= NULL
) {
486 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(loader
->prov
);
487 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
489 return ossl_namemap_doall_names(namemap
, loader
->scheme_id
, fn
, data
);