2 * Copyright 2019-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
11 #include <openssl/crypto.h>
12 #include <openssl/core_dispatch.h>
13 #include <openssl/core_names.h>
14 #include <openssl/provider.h>
15 #include <openssl/evp.h>
16 #include "internal/provider.h"
17 #include "internal/cryptlib.h"
18 #include "crypto/evp.h"
19 #include "crypto/context.h"
21 DEFINE_STACK_OF(OSSL_PROVIDER
)
23 struct child_prov_globals
{
24 const OSSL_CORE_HANDLE
*handle
;
25 const OSSL_CORE_HANDLE
*curr_prov
;
27 OSSL_FUNC_core_get_libctx_fn
*c_get_libctx
;
28 OSSL_FUNC_provider_register_child_cb_fn
*c_provider_register_child_cb
;
29 OSSL_FUNC_provider_deregister_child_cb_fn
*c_provider_deregister_child_cb
;
30 OSSL_FUNC_provider_name_fn
*c_prov_name
;
31 OSSL_FUNC_provider_get0_provider_ctx_fn
*c_prov_get0_provider_ctx
;
32 OSSL_FUNC_provider_get0_dispatch_fn
*c_prov_get0_dispatch
;
33 OSSL_FUNC_provider_up_ref_fn
*c_prov_up_ref
;
34 OSSL_FUNC_provider_free_fn
*c_prov_free
;
37 void *ossl_child_prov_ctx_new(OSSL_LIB_CTX
*libctx
)
39 return OPENSSL_zalloc(sizeof(struct child_prov_globals
));
42 void ossl_child_prov_ctx_free(void *vgbl
)
44 struct child_prov_globals
*gbl
= vgbl
;
46 CRYPTO_THREAD_lock_free(gbl
->lock
);
50 static OSSL_provider_init_fn ossl_child_provider_init
;
52 static int ossl_child_provider_init(const OSSL_CORE_HANDLE
*handle
,
53 const OSSL_DISPATCH
*in
,
54 const OSSL_DISPATCH
**out
,
57 OSSL_FUNC_core_get_libctx_fn
*c_get_libctx
= NULL
;
59 struct child_prov_globals
*gbl
;
61 for (; in
->function_id
!= 0; in
++) {
62 switch (in
->function_id
) {
63 case OSSL_FUNC_CORE_GET_LIBCTX
:
64 c_get_libctx
= OSSL_FUNC_core_get_libctx(in
);
67 /* Just ignore anything we don't understand */
72 if (c_get_libctx
== NULL
)
76 * We need an OSSL_LIB_CTX but c_get_libctx returns OPENSSL_CORE_CTX. We are
77 * a built-in provider and so we can get away with this cast. Normal
78 * providers can't do this.
80 ctx
= (OSSL_LIB_CTX
*)c_get_libctx(handle
);
82 gbl
= ossl_lib_ctx_get_data(ctx
, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX
);
86 *provctx
= gbl
->c_prov_get0_provider_ctx(gbl
->curr_prov
);
87 *out
= gbl
->c_prov_get0_dispatch(gbl
->curr_prov
);
92 static int provider_create_child_cb(const OSSL_CORE_HANDLE
*prov
, void *cbdata
)
94 OSSL_LIB_CTX
*ctx
= cbdata
;
95 struct child_prov_globals
*gbl
;
100 gbl
= ossl_lib_ctx_get_data(ctx
, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX
);
104 if (!CRYPTO_THREAD_write_lock(gbl
->lock
))
107 provname
= gbl
->c_prov_name(prov
);
110 * We're operating under a lock so we can store the "current" provider in
113 gbl
->curr_prov
= prov
;
115 if ((cprov
= ossl_provider_find(ctx
, provname
, 1)) != NULL
) {
117 * We free the newly created ref. We rely on the provider sticking around
118 * in the provider store.
120 ossl_provider_free(cprov
);
123 * The provider already exists. It could be a previously created child,
124 * or it could have been explicitly loaded. If explicitly loaded we
125 * ignore it - i.e. we don't start treating it like a child.
127 if (!ossl_provider_activate(cprov
, 0, 1))
131 * Create it - passing 1 as final param so we don't try and recursively
134 if ((cprov
= ossl_provider_new(ctx
, provname
, ossl_child_provider_init
,
138 if (!ossl_provider_activate(cprov
, 0, 0))
141 if (!ossl_provider_set_child(cprov
, prov
)
142 || !ossl_provider_add_to_store(cprov
, NULL
, 0)) {
143 ossl_provider_deactivate(cprov
, 0);
144 ossl_provider_free(cprov
);
151 CRYPTO_THREAD_unlock(gbl
->lock
);
155 static int provider_remove_child_cb(const OSSL_CORE_HANDLE
*prov
, void *cbdata
)
157 OSSL_LIB_CTX
*ctx
= cbdata
;
158 struct child_prov_globals
*gbl
;
159 const char *provname
;
160 OSSL_PROVIDER
*cprov
;
162 gbl
= ossl_lib_ctx_get_data(ctx
, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX
);
166 provname
= gbl
->c_prov_name(prov
);
167 cprov
= ossl_provider_find(ctx
, provname
, 1);
171 * ossl_provider_find ups the ref count, so we free it again here. We can
172 * rely on the provider store reference count.
174 ossl_provider_free(cprov
);
175 if (ossl_provider_is_child(cprov
)
176 && !ossl_provider_deactivate(cprov
, 1))
182 static int provider_global_props_cb(const char *props
, void *cbdata
)
184 OSSL_LIB_CTX
*ctx
= cbdata
;
186 return evp_set_default_properties_int(ctx
, props
, 0, 1);
189 int ossl_provider_init_as_child(OSSL_LIB_CTX
*ctx
,
190 const OSSL_CORE_HANDLE
*handle
,
191 const OSSL_DISPATCH
*in
)
193 struct child_prov_globals
*gbl
;
198 gbl
= ossl_lib_ctx_get_data(ctx
, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX
);
202 gbl
->handle
= handle
;
203 for (; in
->function_id
!= 0; in
++) {
204 switch (in
->function_id
) {
205 case OSSL_FUNC_CORE_GET_LIBCTX
:
206 gbl
->c_get_libctx
= OSSL_FUNC_core_get_libctx(in
);
208 case OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB
:
209 gbl
->c_provider_register_child_cb
210 = OSSL_FUNC_provider_register_child_cb(in
);
212 case OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB
:
213 gbl
->c_provider_deregister_child_cb
214 = OSSL_FUNC_provider_deregister_child_cb(in
);
216 case OSSL_FUNC_PROVIDER_NAME
:
217 gbl
->c_prov_name
= OSSL_FUNC_provider_name(in
);
219 case OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX
:
220 gbl
->c_prov_get0_provider_ctx
221 = OSSL_FUNC_provider_get0_provider_ctx(in
);
223 case OSSL_FUNC_PROVIDER_GET0_DISPATCH
:
224 gbl
->c_prov_get0_dispatch
= OSSL_FUNC_provider_get0_dispatch(in
);
226 case OSSL_FUNC_PROVIDER_UP_REF
:
228 = OSSL_FUNC_provider_up_ref(in
);
230 case OSSL_FUNC_PROVIDER_FREE
:
231 gbl
->c_prov_free
= OSSL_FUNC_provider_free(in
);
234 /* Just ignore anything we don't understand */
239 if (gbl
->c_get_libctx
== NULL
240 || gbl
->c_provider_register_child_cb
== NULL
241 || gbl
->c_prov_name
== NULL
242 || gbl
->c_prov_get0_provider_ctx
== NULL
243 || gbl
->c_prov_get0_dispatch
== NULL
244 || gbl
->c_prov_up_ref
== NULL
245 || gbl
->c_prov_free
== NULL
)
248 gbl
->lock
= CRYPTO_THREAD_lock_new();
249 if (gbl
->lock
== NULL
)
252 if (!gbl
->c_provider_register_child_cb(gbl
->handle
,
253 provider_create_child_cb
,
254 provider_remove_child_cb
,
255 provider_global_props_cb
,
262 void ossl_provider_deinit_child(OSSL_LIB_CTX
*ctx
)
264 struct child_prov_globals
*gbl
265 = ossl_lib_ctx_get_data(ctx
, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX
);
269 gbl
->c_provider_deregister_child_cb(gbl
->handle
);
273 * ossl_provider_up_ref_parent() and ossl_provider_free_parent() do
274 * nothing in "self-referencing" child providers, i.e. when the parent
275 * of the child provider is the same as the provider where this child
276 * provider was created.
277 * This allows the teardown function in the parent provider to be called
278 * at the correct moment.
279 * For child providers in other providers, the reference count is done to
280 * ensure that cross referencing is recorded. These should be cleared up
281 * through that providers teardown, as part of freeing its child libctx.
284 int ossl_provider_up_ref_parent(OSSL_PROVIDER
*prov
, int activate
)
286 struct child_prov_globals
*gbl
;
287 const OSSL_CORE_HANDLE
*parent_handle
;
289 gbl
= ossl_lib_ctx_get_data(ossl_provider_libctx(prov
),
290 OSSL_LIB_CTX_CHILD_PROVIDER_INDEX
);
294 parent_handle
= ossl_provider_get_parent(prov
);
295 if (parent_handle
== gbl
->handle
)
297 return gbl
->c_prov_up_ref(parent_handle
, activate
);
300 int ossl_provider_free_parent(OSSL_PROVIDER
*prov
, int deactivate
)
302 struct child_prov_globals
*gbl
;
303 const OSSL_CORE_HANDLE
*parent_handle
;
305 gbl
= ossl_lib_ctx_get_data(ossl_provider_libctx(prov
),
306 OSSL_LIB_CTX_CHILD_PROVIDER_INDEX
);
310 parent_handle
= ossl_provider_get_parent(prov
);
311 if (parent_handle
== gbl
->handle
)
313 return gbl
->c_prov_free(ossl_provider_get_parent(prov
), deactivate
);