Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/store/store_register.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2021 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 <string.h>
11
#include <assert.h>
12
13
#include <openssl/err.h>
14
#include <openssl/lhash.h>
15
#include "internal/common.h"
16
#include "store_local.h"
17
18
static CRYPTO_RWLOCK *registry_lock;
19
static CRYPTO_ONCE registry_init = CRYPTO_ONCE_STATIC_INIT;
20
21
DEFINE_RUN_ONCE_STATIC(do_registry_init)
22
0
{
23
0
    registry_lock = CRYPTO_THREAD_lock_new();
24
0
    return registry_lock != NULL;
25
0
}
26
27
/*
28
 *  Functions for manipulating OSSL_STORE_LOADERs
29
 */
30
31
OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(ENGINE *e, const char *scheme)
32
0
{
33
0
    OSSL_STORE_LOADER *res = NULL;
34
35
0
    if (!ossl_assert(e == NULL))
36
0
        return NULL;
37
    /*
38
     * We usually don't check NULL arguments.  For loaders, though, the
39
     * scheme is crucial and must never be NULL, or the user will get
40
     * mysterious errors when trying to register the created loader
41
     * later on.
42
     */
43
0
    if (scheme == NULL) {
44
0
        ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_INVALID_SCHEME);
45
0
        return NULL;
46
0
    }
47
48
0
    if ((res = OPENSSL_zalloc(sizeof(*res))) == NULL)
49
0
        return NULL;
50
51
0
    res->scheme = scheme;
52
0
    return res;
53
0
}
54
55
const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader)
56
0
{
57
0
    return loader->scheme;
58
0
}
59
60
int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
61
    OSSL_STORE_open_fn open_function)
62
0
{
63
0
    loader->open = open_function;
64
0
    return 1;
65
0
}
66
67
int OSSL_STORE_LOADER_set_open_ex(OSSL_STORE_LOADER *loader,
68
    OSSL_STORE_open_ex_fn open_ex_function)
69
0
{
70
0
    loader->open_ex = open_ex_function;
71
0
    return 1;
72
0
}
73
74
int OSSL_STORE_LOADER_set_attach(OSSL_STORE_LOADER *loader,
75
    OSSL_STORE_attach_fn attach_function)
76
0
{
77
0
    loader->attach = attach_function;
78
0
    return 1;
79
0
}
80
81
int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
82
    OSSL_STORE_ctrl_fn ctrl_function)
83
0
{
84
0
    loader->ctrl = ctrl_function;
85
0
    return 1;
86
0
}
87
88
int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
89
    OSSL_STORE_expect_fn expect_function)
90
0
{
91
0
    loader->expect = expect_function;
92
0
    return 1;
93
0
}
94
95
int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
96
    OSSL_STORE_find_fn find_function)
97
0
{
98
0
    loader->find = find_function;
99
0
    return 1;
100
0
}
101
102
int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
103
    OSSL_STORE_load_fn load_function)
104
0
{
105
0
    loader->load = load_function;
106
0
    return 1;
107
0
}
108
109
int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
110
    OSSL_STORE_eof_fn eof_function)
111
0
{
112
0
    loader->eof = eof_function;
113
0
    return 1;
114
0
}
115
116
int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
117
    OSSL_STORE_error_fn error_function)
118
0
{
119
0
    loader->error = error_function;
120
0
    return 1;
121
0
}
122
123
int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
124
    OSSL_STORE_close_fn close_function)
125
0
{
126
0
    loader->closefn = close_function;
127
0
    return 1;
128
0
}
129
130
/*
131
 *  Functions for registering OSSL_STORE_LOADERs
132
 */
133
134
static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
135
0
{
136
0
    return OPENSSL_LH_strhash(v->scheme);
137
0
}
138
139
static int store_loader_cmp(const OSSL_STORE_LOADER *a,
140
    const OSSL_STORE_LOADER *b)
141
0
{
142
0
    assert(a->scheme != NULL && b->scheme != NULL);
143
0
    return strcmp(a->scheme, b->scheme);
144
0
}
145
146
static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
147
static int ossl_store_register_init(void)
148
0
{
149
0
    if (loader_register == NULL) {
150
0
        loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
151
0
            store_loader_cmp);
152
0
    }
153
0
    return loader_register != NULL;
154
0
}
155
156
int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
157
0
{
158
0
    const char *scheme = loader->scheme;
159
0
    int ok = 0;
160
161
    /*
162
     * Check that the given scheme conforms to correct scheme syntax as per
163
     * RFC 3986:
164
     *
165
     * scheme        = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
166
     */
167
0
    OSSL_SKIP_SCHEME(scheme);
168
0
    if (*loader->scheme == '\0' || *scheme != '\0') {
169
0
        ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_INVALID_SCHEME,
170
0
            "scheme=%s", loader->scheme);
171
0
        return 0;
172
0
    }
173
174
    /* Check that functions we absolutely require are present */
175
0
    if (loader->open == NULL || loader->load == NULL || loader->eof == NULL
176
0
        || loader->error == NULL || loader->closefn == NULL) {
177
0
        ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
178
0
        return 0;
179
0
    }
180
181
0
    if (!RUN_ONCE(&registry_init, do_registry_init)) {
182
        /* Should this error be raised in do_registry_init()? */
183
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
184
0
        return 0;
185
0
    }
186
0
    if (!CRYPTO_THREAD_write_lock(registry_lock))
187
0
        return 0;
188
189
0
    if (ossl_store_register_init()
190
0
        && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
191
0
            || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
192
0
        ok = 1;
193
194
0
    CRYPTO_THREAD_unlock(registry_lock);
195
196
0
    return ok;
197
0
}
198
int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
199
0
{
200
0
    return ossl_store_register_loader_int(loader);
201
0
}
202
203
const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
204
0
{
205
0
    OSSL_STORE_LOADER template;
206
0
    OSSL_STORE_LOADER *loader = NULL;
207
208
0
    template.scheme = scheme;
209
0
    template.open = NULL;
210
0
    template.load = NULL;
211
0
    template.eof = NULL;
212
0
    template.closefn = NULL;
213
0
    template.open_ex = NULL;
214
215
0
    if (!RUN_ONCE(&registry_init, do_registry_init)) {
216
        /* Should this error be raised in do_registry_init()? */
217
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
218
0
        return NULL;
219
0
    }
220
0
    if (!CRYPTO_THREAD_write_lock(registry_lock))
221
0
        return NULL;
222
223
0
    if (!ossl_store_register_init())
224
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_INTERNAL_ERROR);
225
0
    else if ((loader = lh_OSSL_STORE_LOADER_retrieve(loader_register,
226
0
                  &template))
227
0
        == NULL)
228
0
        ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNREGISTERED_SCHEME,
229
0
            "scheme=%s", scheme);
230
231
0
    CRYPTO_THREAD_unlock(registry_lock);
232
233
0
    return loader;
234
0
}
235
236
OSSL_STORE_LOADER *ossl_store_unregister_loader_int(const char *scheme)
237
0
{
238
0
    OSSL_STORE_LOADER template;
239
0
    OSSL_STORE_LOADER *loader = NULL;
240
241
0
    template.scheme = scheme;
242
0
    template.open = NULL;
243
0
    template.load = NULL;
244
0
    template.eof = NULL;
245
0
    template.closefn = NULL;
246
247
0
    if (!RUN_ONCE(&registry_init, do_registry_init)) {
248
        /* Should this error be raised in do_registry_init()? */
249
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
250
0
        return NULL;
251
0
    }
252
0
    if (!CRYPTO_THREAD_write_lock(registry_lock))
253
0
        return NULL;
254
255
0
    if (!ossl_store_register_init())
256
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_INTERNAL_ERROR);
257
0
    else if ((loader = lh_OSSL_STORE_LOADER_delete(loader_register,
258
0
                  &template))
259
0
        == NULL)
260
0
        ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_UNREGISTERED_SCHEME,
261
0
            "scheme=%s", scheme);
262
263
0
    CRYPTO_THREAD_unlock(registry_lock);
264
265
0
    return loader;
266
0
}
267
OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
268
0
{
269
0
    return ossl_store_unregister_loader_int(scheme);
270
0
}
271
272
void ossl_store_destroy_loaders_int(void)
273
154
{
274
154
    lh_OSSL_STORE_LOADER_free(loader_register);
275
154
    loader_register = NULL;
276
154
    CRYPTO_THREAD_lock_free(registry_lock);
277
154
    registry_lock = NULL;
278
154
}
279
280
/*
281
 *  Functions to list OSSL_STORE loaders
282
 */
283
284
IMPLEMENT_LHASH_DOALL_ARG_CONST(OSSL_STORE_LOADER, void);
285
int OSSL_STORE_do_all_loaders(void (*do_function)(const OSSL_STORE_LOADER
286
                                                      *loader,
287
                                  void *do_arg),
288
    void *do_arg)
289
0
{
290
0
    if (ossl_store_register_init())
291
0
        lh_OSSL_STORE_LOADER_doall_void(loader_register, do_function, do_arg);
292
0
    return 1;
293
0
}