Coverage Report

Created: 2025-12-31 06:58

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