2 * Copyright 2020-2021 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/store.h>
11 #include <openssl/crypto.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"
18 int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER
*loader
)
22 if (loader
->prov
!= NULL
)
23 CRYPTO_UP_REF(&loader
->refcnt
, &ref
, loader
->lock
);
27 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER
*loader
)
29 if (loader
!= NULL
&& loader
->prov
!= NULL
) {
32 CRYPTO_DOWN_REF(&loader
->refcnt
, &i
, loader
->lock
);
35 ossl_provider_free(loader
->prov
);
36 CRYPTO_THREAD_lock_free(loader
->lock
);
42 * OSSL_STORE_LOADER_new() expects the scheme as a constant string,
43 * which we currently don't have, so we need an alternative allocator.
45 static OSSL_STORE_LOADER
*new_loader(OSSL_PROVIDER
*prov
)
47 OSSL_STORE_LOADER
*loader
;
49 if ((loader
= OPENSSL_zalloc(sizeof(*loader
))) == NULL
50 || (loader
->lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
55 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 /* Permanent loader method store, constructor and destructor */
72 static void loader_store_free(void *vstore
)
74 ossl_method_store_free(vstore
);
77 static void *loader_store_new(OSSL_LIB_CTX
*ctx
)
79 return ossl_method_store_new(ctx
);
83 static const OSSL_LIB_CTX_METHOD loader_store_method
= {
88 /* Data to be passed through ossl_method_construct() */
89 struct loader_data_st
{
91 OSSL_METHOD_CONSTRUCT_METHOD
*mcm
;
92 int scheme_id
; /* For get_loader_from_store() */
93 const char *scheme
; /* For get_loader_from_store() */
94 const char *propquery
; /* For get_loader_from_store() */
96 unsigned int flag_construct_error_occurred
: 1;
100 * Generic routines to fetch / create OSSL_STORE methods with
101 * ossl_method_construct()
104 /* Temporary loader method store, constructor and destructor */
105 static void *alloc_tmp_loader_store(OSSL_LIB_CTX
*ctx
)
107 return ossl_method_store_new(ctx
);
110 static void dealloc_tmp_loader_store(void *store
)
113 ossl_method_store_free(store
);
116 /* Get the permanent loader store */
117 static OSSL_METHOD_STORE
*get_loader_store(OSSL_LIB_CTX
*libctx
)
119 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX
,
120 &loader_store_method
);
123 /* Get loader methods from a store, or put one in */
124 static void *get_loader_from_store(OSSL_LIB_CTX
*libctx
, void *store
,
127 struct loader_data_st
*methdata
= data
;
131 if ((id
= methdata
->scheme_id
) == 0) {
132 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
134 id
= ossl_namemap_name2num(namemap
, methdata
->scheme
);
138 && (store
= get_loader_store(libctx
)) == NULL
)
141 if (!ossl_method_store_fetch(store
, id
, methdata
->propquery
, &method
))
146 static int put_loader_in_store(OSSL_LIB_CTX
*libctx
, void *store
,
147 void *method
, const OSSL_PROVIDER
*prov
,
148 int operation_id
, const char *scheme
,
149 const char *propdef
, void *unused
)
151 OSSL_NAMEMAP
*namemap
;
154 if ((namemap
= ossl_namemap_stored(libctx
)) == NULL
155 || (id
= ossl_namemap_name2num(namemap
, scheme
)) == 0)
158 if (store
== NULL
&& (store
= get_loader_store(libctx
)) == NULL
)
161 return ossl_method_store_add(store
, prov
, id
, propdef
, method
,
162 up_ref_loader
, free_loader
);
165 static void *loader_from_algorithm(int scheme_id
, const OSSL_ALGORITHM
*algodef
,
168 OSSL_STORE_LOADER
*loader
= NULL
;
169 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
171 if ((loader
= new_loader(prov
)) == NULL
)
173 loader
->scheme_id
= scheme_id
;
174 loader
->propdef
= algodef
->property_definition
;
175 loader
->description
= algodef
->algorithm_description
;
177 for (; fns
->function_id
!= 0; fns
++) {
178 switch (fns
->function_id
) {
179 case OSSL_FUNC_STORE_OPEN
:
180 if (loader
->p_open
== NULL
)
181 loader
->p_open
= OSSL_FUNC_store_open(fns
);
183 case OSSL_FUNC_STORE_ATTACH
:
184 if (loader
->p_attach
== NULL
)
185 loader
->p_attach
= OSSL_FUNC_store_attach(fns
);
187 case OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS
:
188 if (loader
->p_settable_ctx_params
== NULL
)
189 loader
->p_settable_ctx_params
=
190 OSSL_FUNC_store_settable_ctx_params(fns
);
192 case OSSL_FUNC_STORE_SET_CTX_PARAMS
:
193 if (loader
->p_set_ctx_params
== NULL
)
194 loader
->p_set_ctx_params
= OSSL_FUNC_store_set_ctx_params(fns
);
196 case OSSL_FUNC_STORE_LOAD
:
197 if (loader
->p_load
== NULL
)
198 loader
->p_load
= OSSL_FUNC_store_load(fns
);
200 case OSSL_FUNC_STORE_EOF
:
201 if (loader
->p_eof
== NULL
)
202 loader
->p_eof
= OSSL_FUNC_store_eof(fns
);
204 case OSSL_FUNC_STORE_CLOSE
:
205 if (loader
->p_close
== NULL
)
206 loader
->p_close
= OSSL_FUNC_store_close(fns
);
208 case OSSL_FUNC_STORE_EXPORT_OBJECT
:
209 if (loader
->p_export_object
== NULL
)
210 loader
->p_export_object
= OSSL_FUNC_store_export_object(fns
);
215 if ((loader
->p_open
== NULL
&& loader
->p_attach
== NULL
)
216 || loader
->p_load
== NULL
217 || loader
->p_eof
== NULL
218 || loader
->p_close
== NULL
) {
219 /* Only set_ctx_params is optionaal */
220 OSSL_STORE_LOADER_free(loader
);
221 ERR_raise(ERR_LIB_OSSL_STORE
, OSSL_STORE_R_LOADER_INCOMPLETE
);
228 * The core fetching functionality passes the scheme of the implementation.
229 * This function is responsible to getting an identity number for them,
230 * then call loader_from_algorithm() with that identity number.
232 static void *construct_loader(const OSSL_ALGORITHM
*algodef
,
233 OSSL_PROVIDER
*prov
, void *data
)
236 * This function is only called if get_loader_from_store() returned
237 * NULL, so it's safe to say that of all the spots to create a new
238 * namemap entry, this is it. Should the scheme already exist there, we
239 * know that ossl_namemap_add() will return its corresponding number.
241 struct loader_data_st
*methdata
= data
;
242 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
243 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
244 const char *scheme
= algodef
->algorithm_names
;
245 int id
= ossl_namemap_add_name(namemap
, 0, scheme
);
249 method
= loader_from_algorithm(id
, algodef
, prov
);
252 * Flag to indicate that there was actual construction errors. This
253 * helps inner_evp_generic_fetch() determine what error it should
254 * record on inaccessible algorithms.
257 methdata
->flag_construct_error_occurred
= 1;
262 /* Intermediary function to avoid ugly casts, used below */
263 static void destruct_loader(void *method
, void *data
)
265 OSSL_STORE_LOADER_free(method
);
268 /* Fetching support. Can fetch by numeric identity or by scheme */
269 static OSSL_STORE_LOADER
*inner_loader_fetch(OSSL_LIB_CTX
*libctx
,
270 int id
, const char *scheme
,
271 const char *properties
)
273 OSSL_METHOD_STORE
*store
= get_loader_store(libctx
);
274 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
278 if (store
== NULL
|| namemap
== NULL
) {
279 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_INVALID_ARGUMENT
);
284 * If we have been passed neither a scheme_id nor a scheme, we have an
285 * internal programming error.
287 if (!ossl_assert(id
!= 0 || scheme
!= NULL
)) {
288 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_INTERNAL_ERROR
);
292 /* If we haven't received a name id yet, try to get one for the name */
294 id
= ossl_namemap_name2num(namemap
, scheme
);
297 * If we haven't found the name yet, chances are that the algorithm to
298 * be fetched is unsupported.
304 || !ossl_method_store_cache_get(store
, id
, properties
, &method
)) {
305 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
306 alloc_tmp_loader_store
,
307 dealloc_tmp_loader_store
,
308 get_loader_from_store
,
313 struct loader_data_st mcmdata
;
315 mcmdata
.libctx
= libctx
;
317 mcmdata
.scheme_id
= id
;
318 mcmdata
.scheme
= scheme
;
319 mcmdata
.propquery
= properties
;
320 mcmdata
.flag_construct_error_occurred
= 0;
321 if ((method
= ossl_method_construct(libctx
, OSSL_OP_STORE
,
322 0 /* !force_cache */,
323 &mcm
, &mcmdata
)) != NULL
) {
325 * If construction did create a method for us, we know that there
326 * is a correct scheme_id, since those have already been calculated
327 * in get_loader_from_store() and put_loader_in_store() above.
330 id
= ossl_namemap_name2num(namemap
, scheme
);
331 ossl_method_store_cache_set(store
, id
, properties
, method
,
332 up_ref_loader
, free_loader
);
336 * If we never were in the constructor, the algorithm to be fetched
339 unsupported
= !mcmdata
.flag_construct_error_occurred
;
342 if (method
== NULL
) {
343 int code
= unsupported
? ERR_R_UNSUPPORTED
: ERR_R_FETCH_FAILED
;
346 scheme
= ossl_namemap_num2name(namemap
, id
, 0);
347 ERR_raise_data(ERR_LIB_OSSL_STORE
, code
,
348 "%s, Scheme (%s : %d), Properties (%s)",
349 ossl_lib_ctx_get_descriptor(libctx
),
350 scheme
= NULL
? "<null>" : scheme
, id
,
351 properties
== NULL
? "<null>" : properties
);
357 OSSL_STORE_LOADER
*OSSL_STORE_LOADER_fetch(const char *scheme
,
358 OSSL_LIB_CTX
*libctx
,
359 const char *properties
)
361 return inner_loader_fetch(libctx
, 0, scheme
, properties
);
364 OSSL_STORE_LOADER
*ossl_store_loader_fetch_by_number(OSSL_LIB_CTX
*libctx
,
366 const char *properties
)
368 return inner_loader_fetch(libctx
, scheme_id
, NULL
, properties
);
372 * Library of basic method functions
375 const OSSL_PROVIDER
*OSSL_STORE_LOADER_provider(const OSSL_STORE_LOADER
*loader
)
377 if (!ossl_assert(loader
!= NULL
)) {
378 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
385 const char *OSSL_STORE_LOADER_properties(const OSSL_STORE_LOADER
*loader
)
387 if (!ossl_assert(loader
!= NULL
)) {
388 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
392 return loader
->propdef
;
395 int OSSL_STORE_LOADER_number(const OSSL_STORE_LOADER
*loader
)
397 if (!ossl_assert(loader
!= NULL
)) {
398 ERR_raise(ERR_LIB_OSSL_STORE
, ERR_R_PASSED_NULL_PARAMETER
);
402 return loader
->scheme_id
;
405 const char *OSSL_STORE_LOADER_description(const OSSL_STORE_LOADER
*loader
)
407 return loader
->description
;
410 int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER
*loader
, const char *name
)
412 if (loader
->prov
!= NULL
) {
413 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(loader
->prov
);
414 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
416 return ossl_namemap_name2num(namemap
, name
) == loader
->scheme_id
;
421 struct loader_do_all_data_st
{
422 void (*user_fn
)(void *method
, void *arg
);
426 static void loader_do_one(OSSL_PROVIDER
*provider
,
427 const OSSL_ALGORITHM
*algodef
,
428 int no_store
, void *vdata
)
430 struct loader_do_all_data_st
*data
= vdata
;
431 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(provider
);
432 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
433 const char *name
= algodef
->algorithm_names
;
434 int id
= ossl_namemap_add_name(namemap
, 0, name
);
439 loader_from_algorithm(id
, algodef
, provider
);
441 if (method
!= NULL
) {
442 data
->user_fn(method
, data
->user_arg
);
443 OSSL_STORE_LOADER_free(method
);
447 void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX
*libctx
,
448 void (*fn
)(OSSL_STORE_LOADER
*loader
,
452 struct loader_do_all_data_st data
;
454 data
.user_fn
= (void (*)(void *, void *))fn
;
456 ossl_algorithm_do_all(libctx
, OSSL_OP_STORE
, NULL
,
457 NULL
, loader_do_one
, NULL
,
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
);