2 * Copyright 2020-2022 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
, loader
->lock
);
28 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER
*loader
)
30 if (loader
!= NULL
&& loader
->prov
!= NULL
) {
33 CRYPTO_DOWN_REF(&loader
->refcnt
, &i
, loader
->lock
);
36 ossl_provider_free(loader
->prov
);
37 CRYPTO_THREAD_lock_free(loader
->lock
);
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 || (loader
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
56 ossl_provider_up_ref(prov
);
62 static int up_ref_loader(void *method
)
64 return OSSL_STORE_LOADER_up_ref(method
);
67 static void free_loader(void *method
)
69 OSSL_STORE_LOADER_free(method
);
72 /* Data to be passed through ossl_method_construct() */
73 struct loader_data_st
{
75 int scheme_id
; /* For get_loader_from_store() */
76 const char *scheme
; /* For get_loader_from_store() */
77 const char *propquery
; /* For get_loader_from_store() */
79 OSSL_METHOD_STORE
*tmp_store
; /* For get_tmp_loader_store() */
81 unsigned int flag_construct_error_occurred
: 1;
85 * Generic routines to fetch / create OSSL_STORE methods with
86 * ossl_method_construct()
89 /* Temporary loader method store, constructor and destructor */
90 static void *get_tmp_loader_store(void *data
)
92 struct loader_data_st
*methdata
= data
;
94 if (methdata
->tmp_store
== NULL
)
95 methdata
->tmp_store
= ossl_method_store_new(methdata
->libctx
);
96 return methdata
->tmp_store
;
99 static void dealloc_tmp_loader_store(void *store
)
102 ossl_method_store_free(store
);
105 /* Get the permanent loader store */
106 static OSSL_METHOD_STORE
*get_loader_store(OSSL_LIB_CTX
*libctx
)
108 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX
);
111 /* Get loader methods from a store, or put one in */
112 static void *get_loader_from_store(void *store
, const OSSL_PROVIDER
**prov
,
115 struct loader_data_st
*methdata
= data
;
119 if ((id
= methdata
->scheme_id
) == 0) {
120 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
122 id
= ossl_namemap_name2num(namemap
, methdata
->scheme
);
126 && (store
= get_loader_store(methdata
->libctx
)) == NULL
)
129 if (!ossl_method_store_fetch(store
, id
, methdata
->propquery
, prov
, &method
))
134 static int put_loader_in_store(void *store
, void *method
,
135 const OSSL_PROVIDER
*prov
,
136 const char *scheme
, const char *propdef
,
139 struct loader_data_st
*methdata
= data
;
140 OSSL_NAMEMAP
*namemap
;
143 if ((namemap
= ossl_namemap_stored(methdata
->libctx
)) == NULL
144 || (id
= ossl_namemap_name2num(namemap
, scheme
)) == 0)
147 if (store
== NULL
&& (store
= get_loader_store(methdata
->libctx
)) == NULL
)
150 return ossl_method_store_add(store
, prov
, id
, propdef
, method
,
151 up_ref_loader
, free_loader
);
154 static void *loader_from_algorithm(int scheme_id
, const OSSL_ALGORITHM
*algodef
,
157 OSSL_STORE_LOADER
*loader
= NULL
;
158 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
160 if ((loader
= new_loader(prov
)) == NULL
)
162 loader
->scheme_id
= scheme_id
;
163 loader
->propdef
= algodef
->property_definition
;
164 loader
->description
= algodef
->algorithm_description
;
166 for (; fns
->function_id
!= 0; fns
++) {
167 switch (fns
->function_id
) {
168 case OSSL_FUNC_STORE_OPEN
:
169 if (loader
->p_open
== NULL
)
170 loader
->p_open
= OSSL_FUNC_store_open(fns
);
172 case OSSL_FUNC_STORE_ATTACH
:
173 if (loader
->p_attach
== NULL
)
174 loader
->p_attach
= OSSL_FUNC_store_attach(fns
);
176 case OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS
:
177 if (loader
->p_settable_ctx_params
== NULL
)
178 loader
->p_settable_ctx_params
=
179 OSSL_FUNC_store_settable_ctx_params(fns
);
181 case OSSL_FUNC_STORE_SET_CTX_PARAMS
:
182 if (loader
->p_set_ctx_params
== NULL
)
183 loader
->p_set_ctx_params
= OSSL_FUNC_store_set_ctx_params(fns
);
185 case OSSL_FUNC_STORE_LOAD
:
186 if (loader
->p_load
== NULL
)
187 loader
->p_load
= OSSL_FUNC_store_load(fns
);
189 case OSSL_FUNC_STORE_EOF
:
190 if (loader
->p_eof
== NULL
)
191 loader
->p_eof
= OSSL_FUNC_store_eof(fns
);
193 case OSSL_FUNC_STORE_CLOSE
:
194 if (loader
->p_close
== NULL
)
195 loader
->p_close
= OSSL_FUNC_store_close(fns
);
197 case OSSL_FUNC_STORE_EXPORT_OBJECT
:
198 if (loader
->p_export_object
== NULL
)
199 loader
->p_export_object
= OSSL_FUNC_store_export_object(fns
);
204 if ((loader
->p_open
== NULL
&& loader
->p_attach
== NULL
)
205 || loader
->p_load
== NULL
206 || loader
->p_eof
== NULL
207 || loader
->p_close
== NULL
) {
208 /* Only set_ctx_params is optionaal */
209 OSSL_STORE_LOADER_free(loader
);
210 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADER_INCOMPLETE
);
217 * The core fetching functionality passes the scheme of the implementation.
218 * This function is responsible to getting an identity number for them,
219 * then call loader_from_algorithm() with that identity number.
221 static void *construct_loader(const OSSL_ALGORITHM
*algodef
,
222 OSSL_PROVIDER
*prov
, void *data
)
225 * This function is only called if get_loader_from_store() returned
226 * NULL, so it's safe to say that of all the spots to create a new
227 * namemap entry, this is it. Should the scheme already exist there, we
228 * know that ossl_namemap_add() will return its corresponding number.
230 struct loader_data_st
*methdata
= data
;
231 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
232 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
233 const char *scheme
= algodef
->algorithm_names
;
234 int id
= ossl_namemap_add_name(namemap
, 0, scheme
);
238 method
= loader_from_algorithm(id
, algodef
, prov
);
241 * Flag to indicate that there was actual construction errors. This
242 * helps inner_loader_fetch() determine what error it should
243 * record on inaccessible algorithms.
246 methdata
->flag_construct_error_occurred
= 1;
251 /* Intermediary function to avoid ugly casts, used below */
252 static void destruct_loader(void *method
, void *data
)
254 OSSL_STORE_LOADER_free(method
);
257 /* Fetching support. Can fetch by numeric identity or by scheme */
258 static OSSL_STORE_LOADER
*
259 inner_loader_fetch(struct loader_data_st
*methdata
, int id
,
260 const char *scheme
, const char *properties
)
262 OSSL_METHOD_STORE
*store
= get_loader_store(methdata
->libctx
);
263 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
264 const char *const propq
= properties
!= NULL
? properties
: "";
268 if (store
== NULL
|| namemap
== NULL
) {
269 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_INVALID_ARGUMENT
);
274 * If we have been passed both an id and a scheme, we have an
275 * internal programming error.
277 if (!ossl_assert(id
== 0 || scheme
== NULL
)) {
278 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_INTERNAL_ERROR
);
282 /* If we haven't received a name id yet, try to get one for the name */
283 if (id
== 0 && scheme
!= NULL
)
284 id
= ossl_namemap_name2num(namemap
, scheme
);
287 * If we haven't found the name yet, chances are that the algorithm to
288 * be fetched is unsupported.
294 || !ossl_method_store_cache_get(store
, NULL
, id
, propq
, &method
)) {
295 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
296 get_tmp_loader_store
,
297 get_loader_from_store
,
302 OSSL_PROVIDER
*prov
= NULL
;
304 methdata
->scheme_id
= id
;
305 methdata
->scheme
= scheme
;
306 methdata
->propquery
= propq
;
307 methdata
->flag_construct_error_occurred
= 0;
308 if ((method
= ossl_method_construct(methdata
->libctx
, OSSL_OP_STORE
,
309 &prov
, 0 /* !force_cache */,
310 &mcm
, methdata
)) != NULL
) {
312 * If construction did create a method for us, we know that there
313 * is a correct scheme_id, since those have already been calculated
314 * in get_loader_from_store() and put_loader_in_store() above.
317 id
= ossl_namemap_name2num(namemap
, scheme
);
318 ossl_method_store_cache_set(store
, prov
, id
, propq
, method
,
319 up_ref_loader
, free_loader
);
323 * If we never were in the constructor, the algorithm to be fetched
326 unsupported
= !methdata
->flag_construct_error_occurred
;
329 if ((id
!= 0 || scheme
!= NULL
) && method
== NULL
) {
330 int code
= unsupported
? ERR_R_UNSUPPORTED
: ERR_R_FETCH_FAILED
;
331 const char *helpful_msg
=
333 ? ( "No store loader found. For standard store loaders you need "
334 "at least one of the default or base providers available. "
335 "Did you forget to load them? Info: " )
339 scheme
= ossl_namemap_num2name(namemap
, id
, 0);
340 ERR_raise_data(ERR_LIB_OSSL_STORE
, code
,
341 "%s%s, Scheme (%s : %d), Properties (%s)",
343 ossl_lib_ctx_get_descriptor(methdata
->libctx
),
344 scheme
= NULL
? "<null>" : scheme
, id
,
345 properties
== NULL
? "<null>" : properties
);
351 OSSL_STORE_LOADER
*OSSL_STORE_LOADER_fetch(OSSL_LIB_CTX
*libctx
,
353 const char *properties
)
355 struct loader_data_st methdata
;
358 methdata
.libctx
= libctx
;
359 methdata
.tmp_store
= NULL
;
360 method
= inner_loader_fetch(&methdata
, 0, scheme
, properties
);
361 dealloc_tmp_loader_store(methdata
.tmp_store
);
365 OSSL_STORE_LOADER
*ossl_store_loader_fetch_by_number(OSSL_LIB_CTX
*libctx
,
367 const char *properties
)
369 struct loader_data_st methdata
;
372 methdata
.libctx
= libctx
;
373 methdata
.tmp_store
= NULL
;
374 method
= inner_loader_fetch(&methdata
, scheme_id
, NULL
, properties
);
375 dealloc_tmp_loader_store(methdata
.tmp_store
);
380 * Library of basic method functions
383 const OSSL_PROVIDER
*OSSL_STORE_LOADER_get0_provider(const OSSL_STORE_LOADER
*loader
)
385 if (!ossl_assert(loader
!= NULL
)) {
386 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
393 const char *OSSL_STORE_LOADER_get0_properties(const OSSL_STORE_LOADER
*loader
)
395 if (!ossl_assert(loader
!= NULL
)) {
396 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
400 return loader
->propdef
;
403 int ossl_store_loader_get_number(const OSSL_STORE_LOADER
*loader
)
405 if (!ossl_assert(loader
!= NULL
)) {
406 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
410 return loader
->scheme_id
;
413 const char *OSSL_STORE_LOADER_get0_description(const OSSL_STORE_LOADER
*loader
)
415 return loader
->description
;
418 int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER
*loader
, const char *name
)
420 if (loader
->prov
!= NULL
) {
421 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(loader
->prov
);
422 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
424 return ossl_namemap_name2num(namemap
, name
) == loader
->scheme_id
;
429 struct do_one_data_st
{
430 void (*user_fn
)(OSSL_STORE_LOADER
*loader
, void *arg
);
434 static void do_one(ossl_unused
int id
, void *method
, void *arg
)
436 struct do_one_data_st
*data
= arg
;
438 data
->user_fn(method
, data
->user_arg
);
441 void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX
*libctx
,
442 void (*user_fn
)(OSSL_STORE_LOADER
*loader
,
446 struct loader_data_st methdata
;
447 struct do_one_data_st data
;
449 methdata
.libctx
= libctx
;
450 methdata
.tmp_store
= NULL
;
451 (void)inner_loader_fetch(&methdata
, 0, NULL
, NULL
/* properties */);
453 data
.user_fn
= user_fn
;
454 data
.user_arg
= user_arg
;
455 if (methdata
.tmp_store
!= NULL
)
456 ossl_method_store_do_all(methdata
.tmp_store
, &do_one
, &data
);
457 ossl_method_store_do_all(get_loader_store(libctx
), &do_one
, &data
);
458 dealloc_tmp_loader_store(methdata
.tmp_store
);
461 int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER
*loader
,
462 void (*fn
)(const char *name
, void *data
),
468 if (loader
->prov
!= NULL
) {
469 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(loader
->prov
);
470 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
472 return ossl_namemap_doall_names(namemap
, loader
->scheme_id
, fn
, data
);