summaryrefslogtreecommitdiffstats
path: root/crypto/provider_child.c
blob: fa1d0041227a897ebc1d753169e40ec1dd6ba806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/crypto.h>
#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/provider.h>
#include "internal/provider.h"
#include "internal/cryptlib.h"

DEFINE_STACK_OF(OSSL_PROVIDER)

struct child_prov_globals {
    const OSSL_CORE_HANDLE *handle;
    OSSL_CORE_PROVIDER *curr_prov;
    STACK_OF(OSSL_PROVIDER) *childprovs;
    unsigned int isinited:1;
    CRYPTO_RWLOCK *lock;
    OSSL_FUNC_core_get_libctx_fn *c_get_libctx;
    OSSL_FUNC_core_provider_do_all_fn *c_prov_do_all;
    OSSL_FUNC_core_provider_name_fn *c_prov_name;
    OSSL_FUNC_core_provider_get0_provider_ctx_fn *c_prov_get0_provider_ctx;
    OSSL_FUNC_core_provider_get0_dispatch_fn *c_prov_get0_dispatch;
};

static void *child_prov_ossl_ctx_new(OSSL_LIB_CTX *libctx)
{
    return OPENSSL_zalloc(sizeof(struct child_prov_globals));
}

/* Wrapper with a void return type for use with sk_OSSL_PROVIDER_pop_free */
static void ossl_prov_free(OSSL_PROVIDER *prov)
{
    OSSL_PROVIDER_unload(prov);
}

static void child_prov_ossl_ctx_free(void *vgbl)
{
    struct child_prov_globals *gbl = vgbl;

    sk_OSSL_PROVIDER_pop_free(gbl->childprovs, ossl_prov_free);
    CRYPTO_THREAD_lock_free(gbl->lock);
    OPENSSL_free(gbl);
}

static const OSSL_LIB_CTX_METHOD child_prov_ossl_ctx_method = {
    OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
    child_prov_ossl_ctx_new,
    child_prov_ossl_ctx_free,
};

static OSSL_provider_init_fn ossl_child_provider_init;

static int ossl_child_provider_init(const OSSL_CORE_HANDLE *handle,
                                    const OSSL_DISPATCH *in,
                                    const OSSL_DISPATCH **out,
                                    void **provctx)
{
    OSSL_FUNC_core_get_libctx_fn *c_get_libctx = NULL;
    OSSL_LIB_CTX *ctx;
    struct child_prov_globals *gbl;

    for (; in->function_id != 0; in++) {
        switch (in->function_id) {
        case OSSL_FUNC_CORE_GET_LIBCTX:
            c_get_libctx = OSSL_FUNC_core_get_libctx(in);
            break;
        default:
            /* Just ignore anything we don't understand */
            break;
        }
    }

    if (c_get_libctx == NULL)
        return 0;

    /*
     * We need an OSSL_LIB_CTX but c_get_libctx returns OPENSSL_CORE_CTX. We are
     * a built-in provider and so we can get away with this cast. Normal
     * providers can't do this.
     */
    ctx = (OSSL_LIB_CTX *)c_get_libctx(handle);

    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
                                &child_prov_ossl_ctx_method);
    if (gbl == NULL)
        return 0;

    *provctx = gbl->c_prov_get0_provider_ctx(gbl->curr_prov);
    *out = gbl->c_prov_get0_dispatch(gbl->curr_prov);

    return 1;
}

static int provider_create_child_cb(OSSL_CORE_PROVIDER *prov, void *cbdata)
{
    OSSL_LIB_CTX *ctx = cbdata;
    struct child_prov_globals *gbl;
    const char *provname;
    OSSL_PROVIDER *cprov;

    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
                                &child_prov_ossl_ctx_method);
    if (gbl == NULL)
        return 0;

    provname = gbl->c_prov_name(prov);

    /*
     * We're operating under a lock so we can store the "current" provider in
     * the global data.
     */
    gbl->curr_prov = prov;

    /*
     * Create it - passing 1 as final param so we don't try and recursively init
     * children
     */
    if ((cprov = ossl_provider_new(ctx, provname, ossl_child_provider_init,
                                   1)) == NULL)
        return 0;

    if (!ossl_provider_activate(cprov, 0)) {
        ossl_provider_free(cprov);
        return 0;
    }
    ossl_provider_set_child(cprov);

    if (!sk_OSSL_PROVIDER_push(gbl->childprovs, cprov)) {
        OSSL_PROVIDER_unload(cprov);
        return 0;
    }

    return 1;
}

int ossl_provider_init_child_providers(OSSL_LIB_CTX *ctx)
{
    struct child_prov_globals *gbl;

    /* Should never happen */
    if (ctx == NULL)
        return 0;

    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
                                &child_prov_ossl_ctx_method);
    if (gbl == NULL)
        return 0;

    if (!CRYPTO_THREAD_read_lock(gbl->lock))
        return 0;
    if (gbl->isinited) {
        CRYPTO_THREAD_unlock(gbl->lock);
        return 1;
    }
    CRYPTO_THREAD_unlock(gbl->lock);

    if (!CRYPTO_THREAD_write_lock(gbl->lock))
        return 0;
    if (!gbl->isinited) {
        if (!gbl->c_prov_do_all(gbl->c_get_libctx(gbl->handle),
                                provider_create_child_cb, ctx)) {
            CRYPTO_THREAD_unlock(gbl->lock);
            return 0;
        }
        gbl->isinited = 1;
    }
    CRYPTO_THREAD_unlock(gbl->lock);

    return 1;
}

int ossl_provider_init_as_child(OSSL_LIB_CTX *ctx,
                                const OSSL_CORE_HANDLE *handle,
                                const OSSL_DISPATCH *in)
{
    struct child_prov_globals *gbl;

    if (ctx == NULL)
        return 0;

    gbl = ossl_lib_ctx_get_data(ctx, OSSL_LIB_CTX_CHILD_PROVIDER_INDEX,
                                &child_prov_ossl_ctx_method);
    if (gbl == NULL)
        return 0;

    gbl->handle = handle;
    for (; in->function_id != 0; in++) {
        switch (in->function_id) {
        case OSSL_FUNC_CORE_GET_LIBCTX:
            gbl->c_get_libctx = OSSL_FUNC_core_get_libctx(in);
            break;
        case OSSL_FUNC_CORE_PROVIDER_DO_ALL:
            gbl->c_prov_do_all = OSSL_FUNC_core_provider_do_all(in);
            break;
        case OSSL_FUNC_CORE_PROVIDER_NAME:
            gbl->c_prov_name = OSSL_FUNC_core_provider_name(in);
            break;
        case OSSL_FUNC_CORE_PROVIDER_GET0_PROVIDER_CTX:
            gbl->c_prov_get0_provider_ctx
                = OSSL_FUNC_core_provider_get0_provider_ctx(in);
            break;
        case OSSL_FUNC_CORE_PROVIDER_GET0_DISPATCH:
            gbl->c_prov_get0_dispatch = OSSL_FUNC_core_provider_get0_dispatch(in);
            break;
        default:
            /* Just ignore anything we don't understand */
            break;
        }
    }

    if (gbl->c_prov_do_all == NULL
            || gbl->c_prov_name == NULL
            || gbl->c_prov_get0_provider_ctx == NULL
            || gbl->c_prov_get0_dispatch == NULL)
        return 0;

    gbl->childprovs = sk_OSSL_PROVIDER_new_null();
    if (gbl->childprovs == NULL)
        return 0;
    gbl->lock = CRYPTO_THREAD_lock_new();
    if (gbl->lock == NULL)
        return 0;

    return 1;
}