Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl30/crypto/provider_child.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2023 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 <assert.h>
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
20
DEFINE_STACK_OF(OSSL_PROVIDER)
21
22
struct child_prov_globals {
23
    const OSSL_CORE_HANDLE *handle;
24
    const OSSL_CORE_HANDLE *curr_prov;
25
    CRYPTO_RWLOCK *lock;
26
    OSSL_FUNC_core_get_libctx_fn *c_get_libctx;
27
    OSSL_FUNC_provider_register_child_cb_fn *c_provider_register_child_cb;
28
    OSSL_FUNC_provider_deregister_child_cb_fn *c_provider_deregister_child_cb;
29
    OSSL_FUNC_provider_name_fn *c_prov_name;
30
    OSSL_FUNC_provider_get0_provider_ctx_fn *c_prov_get0_provider_ctx;
31
    OSSL_FUNC_provider_get0_dispatch_fn *c_prov_get0_dispatch;
32
    OSSL_FUNC_provider_up_ref_fn *c_prov_up_ref;
33
    OSSL_FUNC_provider_free_fn *c_prov_free;
34
};
35
36
static void *child_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
37
0
{
38
0
    return OPENSSL_zalloc(sizeof(struct child_prov_globals));
39
0
}
40
41
static void child_prov_ossl_ctx_free(void *vgbl)
42
0
{
43
0
    struct child_prov_globals *gbl = vgbl;
44
45
0
    CRYPTO_THREAD_lock_free(gbl->lock);
46
0
    OPENSSL_free(gbl);
47
0
}
48
49
static const OSSL_LIB_CTX_METHOD child_prov_ossl_ctx_method = {
50
    OSSL_LIB_CTX_METHOD_LOW_PRIORITY,
51
    child_prov_ossl_ctx_new,
52
    child_prov_ossl_ctx_free,
53
};
54
55
static OSSL_provider_init_fn ossl_child_provider_init;
56
57
static int ossl_child_provider_init(const OSSL_CORE_HANDLE *handle,
58
    const OSSL_DISPATCH *in,
59
    const OSSL_DISPATCH **out,
60
    void **provctx)
61
0
{
62
0
    OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
63
0
    OSSL_LIB_CTX *ctx;
64
0
    struct child_prov_globals *gbl;
65
66
0
    for (; in->function_id != 0; in++) {
67
0
        switch (in->function_id) {
68
0
        case OSSL_FUNC_CORE_GET_LIBCTX:
69
0
            c_get_libctx = OSSL_FUNC_core_get_libctx(in);
70
0
            break;
71
0
        default:
72
            /* Just ignore anything we don't understand */
73
0
            break;
74
0
        }
75
0
    }
76
77
0
    if (c_get_libctx == NULL)
78
0
        return 0;
79
80
    /*
81
     * We need an OSSL_LIB_CTX but c_get_libctx returns OPENSSL_CORE_CTX. We are
82
     * a built-in provider and so we can get away with this cast. Normal
83
     * providers can't do this.
84
     */
85
0
    ctx = (OSSL_LIB_CTX *)c_get_libctx(handle);
86
87
0
    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
88
0
        &child_prov_ossl_ctx_method);
89
0
    if (gbl == NULL)
90
0
        return 0;
91
92
0
    *provctx = gbl->c_prov_get0_provider_ctx(gbl->curr_prov);
93
0
    *out = gbl->c_prov_get0_dispatch(gbl->curr_prov);
94
95
0
    return 1;
96
0
}
97
98
static int provider_create_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata)
99
0
{
100
0
    OSSL_LIB_CTX *ctx = cbdata;
101
0
    struct child_prov_globals *gbl;
102
0
    const char *provname;
103
0
    OSSL_PROVIDER *cprov;
104
0
    int ret = 0;
105
106
0
    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
107
0
        &child_prov_ossl_ctx_method);
108
0
    if (gbl == NULL)
109
0
        return 0;
110
111
0
    if (!CRYPTO_THREAD_write_lock(gbl->lock))
112
0
        return 0;
113
114
0
    provname = gbl->c_prov_name(prov);
115
116
    /*
117
     * We're operating under a lock so we can store the "current" provider in
118
     * the global data.
119
     */
120
0
    gbl->curr_prov = prov;
121
122
0
    if ((cprov = ossl_provider_find(ctx, provname, 1)) != NULL) {
123
        /*
124
         * We free the newly created ref. We rely on the provider sticking around
125
         * in the provider store.
126
         */
127
0
        ossl_provider_free(cprov);
128
129
        /*
130
         * The provider already exists. It could be a previously created child,
131
         * or it could have been explicitly loaded. If explicitly loaded we
132
         * ignore it - i.e. we don't start treating it like a child.
133
         */
134
0
        if (!ossl_provider_activate(cprov, 0, 1))
135
0
            goto err;
136
0
    } else {
137
        /*
138
         * Create it - passing 1 as final param so we don't try and recursively
139
         * init children
140
         */
141
0
        if ((cprov = ossl_provider_new(ctx, provname, ossl_child_provider_init,
142
0
                 1))
143
0
            == NULL)
144
0
            goto err;
145
146
0
        if (!ossl_provider_activate(cprov, 0, 0)) {
147
0
            ossl_provider_free(cprov);
148
0
            goto err;
149
0
        }
150
151
0
        if (!ossl_provider_set_child(cprov, prov)
152
0
            || !ossl_provider_add_to_store(cprov, NULL, 0)) {
153
0
            ossl_provider_deactivate(cprov, 0);
154
0
            ossl_provider_free(cprov);
155
0
            goto err;
156
0
        }
157
0
    }
158
159
0
    ret = 1;
160
0
err:
161
0
    CRYPTO_THREAD_unlock(gbl->lock);
162
0
    return ret;
163
0
}
164
165
static int provider_remove_child_cb(const OSSL_CORE_HANDLE *prov, void *cbdata)
166
0
{
167
0
    OSSL_LIB_CTX *ctx = cbdata;
168
0
    struct child_prov_globals *gbl;
169
0
    const char *provname;
170
0
    OSSL_PROVIDER *cprov;
171
172
0
    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
173
0
        &child_prov_ossl_ctx_method);
174
0
    if (gbl == NULL)
175
0
        return 0;
176
177
0
    provname = gbl->c_prov_name(prov);
178
0
    cprov = ossl_provider_find(ctx, provname, 1);
179
0
    if (cprov == NULL)
180
0
        return 0;
181
    /*
182
     * ossl_provider_find ups the ref count, so we free it again here. We can
183
     * rely on the provider store reference count.
184
     */
185
0
    ossl_provider_free(cprov);
186
0
    if (ossl_provider_is_child(cprov)
187
0
        && !ossl_provider_deactivate(cprov, 1))
188
0
        return 0;
189
190
0
    return 1;
191
0
}
192
193
static int provider_global_props_cb(const char *props, void *cbdata)
194
0
{
195
0
    OSSL_LIB_CTX *ctx = cbdata;
196
197
0
    return evp_set_default_properties_int(ctx, props, 0, 1);
198
0
}
199
200
int ossl_provider_init_as_child(OSSL_LIB_CTX *ctx,
201
    const OSSL_CORE_HANDLE *handle,
202
    const OSSL_DISPATCH *in)
203
0
{
204
0
    struct child_prov_globals *gbl;
205
206
0
    if (ctx == NULL)
207
0
        return 0;
208
209
0
    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
210
0
        &child_prov_ossl_ctx_method);
211
0
    if (gbl == NULL)
212
0
        return 0;
213
214
0
    gbl->handle = handle;
215
0
    for (; in->function_id != 0; in++) {
216
0
        switch (in->function_id) {
217
0
        case OSSL_FUNC_CORE_GET_LIBCTX:
218
0
            gbl->c_get_libctx = OSSL_FUNC_core_get_libctx(in);
219
0
            break;
220
0
        case OSSL_FUNC_PROVIDER_REGISTER_CHILD_CB:
221
0
            gbl->c_provider_register_child_cb
222
0
                = OSSL_FUNC_provider_register_child_cb(in);
223
0
            break;
224
0
        case OSSL_FUNC_PROVIDER_DEREGISTER_CHILD_CB:
225
0
            gbl->c_provider_deregister_child_cb
226
0
                = OSSL_FUNC_provider_deregister_child_cb(in);
227
0
            break;
228
0
        case OSSL_FUNC_PROVIDER_NAME:
229
0
            gbl->c_prov_name = OSSL_FUNC_provider_name(in);
230
0
            break;
231
0
        case OSSL_FUNC_PROVIDER_GET0_PROVIDER_CTX:
232
0
            gbl->c_prov_get0_provider_ctx
233
0
                = OSSL_FUNC_provider_get0_provider_ctx(in);
234
0
            break;
235
0
        case OSSL_FUNC_PROVIDER_GET0_DISPATCH:
236
0
            gbl->c_prov_get0_dispatch = OSSL_FUNC_provider_get0_dispatch(in);
237
0
            break;
238
0
        case OSSL_FUNC_PROVIDER_UP_REF:
239
0
            gbl->c_prov_up_ref
240
0
                = OSSL_FUNC_provider_up_ref(in);
241
0
            break;
242
0
        case OSSL_FUNC_PROVIDER_FREE:
243
0
            gbl->c_prov_free = OSSL_FUNC_provider_free(in);
244
0
            break;
245
0
        default:
246
            /* Just ignore anything we don't understand */
247
0
            break;
248
0
        }
249
0
    }
250
251
0
    if (gbl->c_get_libctx == NULL
252
0
        || gbl->c_provider_register_child_cb == NULL
253
0
        || gbl->c_prov_name == NULL
254
0
        || gbl->c_prov_get0_provider_ctx == NULL
255
0
        || gbl->c_prov_get0_dispatch == NULL
256
0
        || gbl->c_prov_up_ref == NULL
257
0
        || gbl->c_prov_free == NULL)
258
0
        return 0;
259
260
0
    gbl->lock = CRYPTO_THREAD_lock_new();
261
0
    if (gbl->lock == NULL)
262
0
        return 0;
263
264
0
    if (!gbl->c_provider_register_child_cb(gbl->handle,
265
0
            provider_create_child_cb,
266
0
            provider_remove_child_cb,
267
0
            provider_global_props_cb,
268
0
            ctx))
269
0
        return 0;
270
271
0
    return 1;
272
0
}
273
274
void ossl_provider_deinit_child(OSSL_LIB_CTX *ctx)
275
0
{
276
0
    struct child_prov_globals *gbl
277
0
        = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
278
0
            &child_prov_ossl_ctx_method);
279
0
    if (gbl == NULL)
280
0
        return;
281
282
0
    gbl->c_provider_deregister_child_cb(gbl->handle);
283
0
}
284
285
/*
286
 * ossl_provider_up_ref_parent() and ossl_provider_free_parent() do
287
 * nothing in "self-referencing" child providers, i.e. when the parent
288
 * of the child provider is the same as the provider where this child
289
 * provider was created.
290
 * This allows the teardown function in the parent provider to be called
291
 * at the correct moment.
292
 * For child providers in other providers, the reference count is done to
293
 * ensure that cross referencing is recorded.  These should be cleared up
294
 * through that providers teardown, as part of freeing its child libctx.
295
 */
296
297
int ossl_provider_up_ref_parent(OSSL_PROVIDER *prov, int activate)
298
0
{
299
0
    struct child_prov_globals *gbl;
300
0
    const OSSL_CORE_HANDLE *parent_handle;
301
302
0
    gbl = ossl_lib_ctx_get_data(ossl_provider_libctx(prov),
303
0
        OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
304
0
        &child_prov_ossl_ctx_method);
305
0
    if (gbl == NULL)
306
0
        return 0;
307
308
0
    parent_handle = ossl_provider_get_parent(prov);
309
0
    if (parent_handle == gbl->handle)
310
0
        return 1;
311
0
    return gbl->c_prov_up_ref(parent_handle, activate);
312
0
}
313
314
int ossl_provider_free_parent(OSSL_PROVIDER *prov, int deactivate)
315
0
{
316
0
    struct child_prov_globals *gbl;
317
0
    const OSSL_CORE_HANDLE *parent_handle;
318
319
0
    gbl = ossl_lib_ctx_get_data(ossl_provider_libctx(prov),
320
0
        OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
321
0
        &child_prov_ossl_ctx_method);
322
0
    if (gbl == NULL)
323
0
        return 0;
324
325
0
    parent_handle = ossl_provider_get_parent(prov);
326
0
    if (parent_handle == gbl->handle)
327
0
        return 1;
328
0
    return gbl->c_prov_free(ossl_provider_get_parent(prov), deactivate);
329
0
}