Coverage Report

Created: 2025-11-07 06:58

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 "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
        return NULL;
48
49
0
    res->engine = e;
50
0
    res->scheme = scheme;
51
0
    return res;
52
0
}
53
54
const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader)
55
0
{
56
0
    return loader->engine;
57
0
}
58
59
const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader)
60
0
{
61
0
    return loader->scheme;
62
0
}
63
64
int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
65
                               OSSL_STORE_open_fn open_function)
66
0
{
67
0
    loader->open = open_function;
68
0
    return 1;
69
0
}
70
71
int OSSL_STORE_LOADER_set_open_ex
72
    (OSSL_STORE_LOADER *loader,
73
     OSSL_STORE_open_ex_fn open_ex_function)
74
0
{
75
0
    loader->open_ex = open_ex_function;
76
0
    return 1;
77
0
}
78
79
int OSSL_STORE_LOADER_set_attach(OSSL_STORE_LOADER *loader,
80
                                 OSSL_STORE_attach_fn attach_function)
81
0
{
82
0
    loader->attach = attach_function;
83
0
    return 1;
84
0
}
85
86
int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
87
                               OSSL_STORE_ctrl_fn ctrl_function)
88
0
{
89
0
    loader->ctrl = ctrl_function;
90
0
    return 1;
91
0
}
92
93
int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
94
                                 OSSL_STORE_expect_fn expect_function)
95
0
{
96
0
    loader->expect = expect_function;
97
0
    return 1;
98
0
}
99
100
int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
101
                               OSSL_STORE_find_fn find_function)
102
0
{
103
0
    loader->find = find_function;
104
0
    return 1;
105
0
}
106
107
int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
108
                               OSSL_STORE_load_fn load_function)
109
0
{
110
0
    loader->load = load_function;
111
0
    return 1;
112
0
}
113
114
int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
115
                              OSSL_STORE_eof_fn eof_function)
116
0
{
117
0
    loader->eof = eof_function;
118
0
    return 1;
119
0
}
120
121
int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
122
                                OSSL_STORE_error_fn error_function)
123
0
{
124
0
    loader->error = error_function;
125
0
    return 1;
126
0
}
127
128
int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
129
                                OSSL_STORE_close_fn close_function)
130
0
{
131
0
    loader->closefn = close_function;
132
0
    return 1;
133
0
}
134
135
/*
136
 *  Functions for registering OSSL_STORE_LOADERs
137
 */
138
139
static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
140
0
{
141
0
    return OPENSSL_LH_strhash(v->scheme);
142
0
}
143
144
static int store_loader_cmp(const OSSL_STORE_LOADER *a,
145
                            const OSSL_STORE_LOADER *b)
146
0
{
147
0
    assert(a->scheme != NULL && b->scheme != NULL);
148
0
    return strcmp(a->scheme, b->scheme);
149
0
}
150
151
static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
152
static int ossl_store_register_init(void)
153
0
{
154
0
    if (loader_register == NULL) {
155
0
        loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
156
0
                                                   store_loader_cmp);
157
0
    }
158
0
    return loader_register != NULL;
159
0
}
160
161
int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
162
0
{
163
0
    const char *scheme = loader->scheme;
164
0
    int ok = 0;
165
166
    /*
167
     * Check that the given scheme conforms to correct scheme syntax as per
168
     * RFC 3986:
169
     *
170
     * scheme        = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
171
     */
172
0
    if (ossl_isalpha(*scheme))
173
0
        while (*scheme != '\0'
174
0
               && (ossl_isalpha(*scheme)
175
0
                   || ossl_isdigit(*scheme)
176
0
                   || strchr("+-.", *scheme) != NULL))
177
0
            scheme++;
178
0
    if (*scheme != '\0') {
179
0
        ERR_raise_data(ERR_LIB_OSSL_STORE, OSSL_STORE_R_INVALID_SCHEME,
180
0
                       "scheme=%s", loader->scheme);
181
0
        return 0;
182
0
    }
183
184
    /* Check that functions we absolutely require are present */
185
0
    if (loader->open == NULL || loader->load == NULL || loader->eof == NULL
186
0
        || loader->error == NULL || loader->closefn == NULL) {
187
0
        ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
188
0
        return 0;
189
0
    }
190
191
0
    if (!RUN_ONCE(&registry_init, do_registry_init)) {
192
        /* Should this error be raised in do_registry_init()? */
193
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
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
        /* Should this error be raised in do_registry_init()? */
227
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
228
0
        return NULL;
229
0
    }
230
0
    if (!CRYPTO_THREAD_write_lock(registry_lock))
231
0
        return NULL;
232
233
0
    if (!ossl_store_register_init())
234
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_INTERNAL_ERROR);
235
0
    else if ((loader = lh_OSSL_STORE_LOADER_retrieve(loader_register,
236
0
                                                     &template)) == 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
        /* Should this error be raised in do_registry_init()? */
258
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_CRYPTO_LIB);
259
0
        return NULL;
260
0
    }
261
0
    if (!CRYPTO_THREAD_write_lock(registry_lock))
262
0
        return NULL;
263
264
0
    if (!ossl_store_register_init())
265
0
        ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_INTERNAL_ERROR);
266
0
    else if ((loader = lh_OSSL_STORE_LOADER_delete(loader_register,
267
0
                                                   &template)) == 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
3
{
282
3
    lh_OSSL_STORE_LOADER_free(loader_register);
283
3
    loader_register = NULL;
284
3
    CRYPTO_THREAD_lock_free(registry_lock);
285
3
    registry_lock = NULL;
286
3
}
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, void *do_arg),
295
                              void *do_arg)
296
0
{
297
0
    if (ossl_store_register_init())
298
0
        lh_OSSL_STORE_LOADER_doall_void(loader_register, do_function, do_arg);
299
0
    return 1;
300
0
}