]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/store/store_register.c
STORE: Add the base functions to support provider based loaders
[thirdparty/openssl.git] / crypto / store / store_register.c
1 /*
2 * Copyright 2016-2020 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 {
23 registry_lock = CRYPTO_THREAD_lock_new();
24 return registry_lock != NULL;
25 }
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 {
33 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 if (scheme == NULL) {
42 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW,
43 OSSL_STORE_R_INVALID_SCHEME);
44 return NULL;
45 }
46
47 if ((res = OPENSSL_zalloc(sizeof(*res))) == NULL) {
48 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW, ERR_R_MALLOC_FAILURE);
49 return NULL;
50 }
51
52 res->engine = e;
53 res->scheme = scheme;
54 return res;
55 }
56
57 const ENGINE *OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER *loader)
58 {
59 return loader->engine;
60 }
61
62 const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader)
63 {
64 return loader->scheme;
65 }
66
67 int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
68 OSSL_STORE_open_fn open_function)
69 {
70 loader->open = open_function;
71 return 1;
72 }
73
74 int OSSL_STORE_LOADER_set_open_with_libctx
75 (OSSL_STORE_LOADER *loader,
76 OSSL_STORE_open_with_libctx_fn open_with_libctx_function)
77 {
78 loader->open_with_libctx = open_with_libctx_function;
79 return 1;
80 }
81
82 int OSSL_STORE_LOADER_set_attach(OSSL_STORE_LOADER *loader,
83 OSSL_STORE_attach_fn attach_function)
84 {
85 loader->attach = attach_function;
86 return 1;
87 }
88
89 int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
90 OSSL_STORE_ctrl_fn ctrl_function)
91 {
92 loader->ctrl = ctrl_function;
93 return 1;
94 }
95
96 int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER *loader,
97 OSSL_STORE_expect_fn expect_function)
98 {
99 loader->expect = expect_function;
100 return 1;
101 }
102
103 int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER *loader,
104 OSSL_STORE_find_fn find_function)
105 {
106 loader->find = find_function;
107 return 1;
108 }
109
110 int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
111 OSSL_STORE_load_fn load_function)
112 {
113 loader->load = load_function;
114 return 1;
115 }
116
117 int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
118 OSSL_STORE_eof_fn eof_function)
119 {
120 loader->eof = eof_function;
121 return 1;
122 }
123
124 int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
125 OSSL_STORE_error_fn error_function)
126 {
127 loader->error = error_function;
128 return 1;
129 }
130
131 int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
132 OSSL_STORE_close_fn close_function)
133 {
134 loader->close = close_function;
135 return 1;
136 }
137
138 /*
139 * Functions for registering OSSL_STORE_LOADERs
140 */
141
142 static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
143 {
144 return OPENSSL_LH_strhash(v->scheme);
145 }
146
147 static int store_loader_cmp(const OSSL_STORE_LOADER *a,
148 const OSSL_STORE_LOADER *b)
149 {
150 assert(a->scheme != NULL && b->scheme != NULL);
151 return strcmp(a->scheme, b->scheme);
152 }
153
154 static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
155 static int ossl_store_register_init(void)
156 {
157 if (loader_register == NULL) {
158 loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
159 store_loader_cmp);
160 }
161 return loader_register != NULL;
162 }
163
164 int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
165 {
166 const char *scheme = loader->scheme;
167 int ok = 0;
168
169 /*
170 * Check that the given scheme conforms to correct scheme syntax as per
171 * RFC 3986:
172 *
173 * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
174 */
175 if (ossl_isalpha(*scheme))
176 while (*scheme != '\0'
177 && (ossl_isalpha(*scheme)
178 || ossl_isdigit(*scheme)
179 || strchr("+-.", *scheme) != NULL))
180 scheme++;
181 if (*scheme != '\0') {
182 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
183 OSSL_STORE_R_INVALID_SCHEME);
184 ERR_add_error_data(2, "scheme=", loader->scheme);
185 return 0;
186 }
187
188 /* Check that functions we absolutely require are present */
189 if (loader->open == NULL || loader->load == NULL || loader->eof == NULL
190 || loader->error == NULL || loader->close == NULL) {
191 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
192 OSSL_STORE_R_LOADER_INCOMPLETE);
193 return 0;
194 }
195
196 if (!RUN_ONCE(&registry_init, do_registry_init)) {
197 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
198 ERR_R_MALLOC_FAILURE);
199 return 0;
200 }
201 CRYPTO_THREAD_write_lock(registry_lock);
202
203 if (ossl_store_register_init()
204 && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
205 || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
206 ok = 1;
207
208 CRYPTO_THREAD_unlock(registry_lock);
209
210 return ok;
211 }
212 int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
213 {
214 if (!ossl_store_init_once())
215 return 0;
216 return ossl_store_register_loader_int(loader);
217 }
218
219 const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
220 {
221 OSSL_STORE_LOADER template;
222 OSSL_STORE_LOADER *loader = NULL;
223
224 template.scheme = scheme;
225 template.open = NULL;
226 template.load = NULL;
227 template.eof = NULL;
228 template.close = NULL;
229 template.open_with_libctx = NULL;
230
231 if (!ossl_store_init_once())
232 return NULL;
233
234 if (!RUN_ONCE(&registry_init, do_registry_init)) {
235 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
236 ERR_R_MALLOC_FAILURE);
237 return NULL;
238 }
239 CRYPTO_THREAD_write_lock(registry_lock);
240
241 if (!ossl_store_register_init()) {
242 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
243 ERR_R_INTERNAL_ERROR);
244 } else if ((loader = lh_OSSL_STORE_LOADER_retrieve(loader_register,
245 &template)) == NULL) {
246 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
247 OSSL_STORE_R_UNREGISTERED_SCHEME);
248 ERR_add_error_data(2, "scheme=", scheme);
249 }
250
251 CRYPTO_THREAD_unlock(registry_lock);
252
253 return loader;
254 }
255
256 OSSL_STORE_LOADER *ossl_store_unregister_loader_int(const char *scheme)
257 {
258 OSSL_STORE_LOADER template;
259 OSSL_STORE_LOADER *loader = NULL;
260
261 template.scheme = scheme;
262 template.open = NULL;
263 template.load = NULL;
264 template.eof = NULL;
265 template.close = NULL;
266
267 if (!RUN_ONCE(&registry_init, do_registry_init)) {
268 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
269 ERR_R_MALLOC_FAILURE);
270 return NULL;
271 }
272 CRYPTO_THREAD_write_lock(registry_lock);
273
274 if (!ossl_store_register_init()) {
275 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
276 ERR_R_INTERNAL_ERROR);
277 } else if ((loader = lh_OSSL_STORE_LOADER_delete(loader_register,
278 &template)) == NULL) {
279 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
280 OSSL_STORE_R_UNREGISTERED_SCHEME);
281 ERR_add_error_data(2, "scheme=", scheme);
282 }
283
284 CRYPTO_THREAD_unlock(registry_lock);
285
286 return loader;
287 }
288 OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
289 {
290 if (!ossl_store_init_once())
291 return 0;
292 return ossl_store_unregister_loader_int(scheme);
293 }
294
295 void ossl_store_destroy_loaders_int(void)
296 {
297 lh_OSSL_STORE_LOADER_free(loader_register);
298 loader_register = NULL;
299 CRYPTO_THREAD_lock_free(registry_lock);
300 registry_lock = NULL;
301 }
302
303 /*
304 * Functions to list OSSL_STORE loaders
305 */
306
307 IMPLEMENT_LHASH_DOALL_ARG_CONST(OSSL_STORE_LOADER, void);
308 int OSSL_STORE_do_all_loaders(void (*do_function) (const OSSL_STORE_LOADER
309 *loader, void *do_arg),
310 void *do_arg)
311 {
312 if (ossl_store_register_init())
313 lh_OSSL_STORE_LOADER_doall_void(loader_register, do_function, do_arg);
314 return 1;
315 }