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