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