bde190e78eb7af15765266cfdd402f63820e9b93
[openssl.git] / crypto / store / store_register.c
1 /*
2  * Copyright 2016-2017 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 <ctype.h>
12 #include <assert.h>
13
14 #include <openssl/err.h>
15 #include <openssl/lhash.h>
16 #include "store_locl.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 = OPENSSL_zalloc(sizeof(*res));
34
35     if (res == NULL) {
36         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW, ERR_R_MALLOC_FAILURE);
37         return NULL;
38     }
39
40     /*
41      * We usually don't check NULL arguments.  For loaders, though, the
42      * scheme is crucial and must never be NULL, or the user will get
43      * mysterious errors when trying to register the created loader
44      * later on.
45      */
46     if (scheme == NULL) {
47         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW,
48                       OSSL_STORE_R_INVALID_SCHEME);
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_ctrl(OSSL_STORE_LOADER *loader,
75                                OSSL_STORE_ctrl_fn ctrl_function)
76 {
77     loader->ctrl = ctrl_function;
78     return 1;
79 }
80
81 int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
82                                OSSL_STORE_load_fn load_function)
83 {
84     loader->load = load_function;
85     return 1;
86 }
87
88 int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
89                               OSSL_STORE_eof_fn eof_function)
90 {
91     loader->eof = eof_function;
92     return 1;
93 }
94
95 int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
96                                 OSSL_STORE_error_fn error_function)
97 {
98     loader->error = error_function;
99     return 1;
100 }
101
102 int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
103                                 OSSL_STORE_close_fn close_function)
104 {
105     loader->close = close_function;
106     return 1;
107 }
108
109 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
110 {
111     OPENSSL_free(loader);
112 }
113
114 /*
115  *  Functions for registering OSSL_STORE_LOADERs
116  */
117
118 static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
119 {
120     return OPENSSL_LH_strhash(v->scheme);
121 }
122
123 static int store_loader_cmp(const OSSL_STORE_LOADER *a,
124                             const OSSL_STORE_LOADER *b)
125 {
126     if (a->scheme != NULL && b->scheme != NULL)
127         return strcmp(a->scheme, b->scheme);
128     else if (a->scheme == b->scheme)
129         return 0;
130     return a->scheme == NULL ? -1 : 1;
131 }
132
133 static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
134
135 int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
136 {
137     const char *scheme = loader->scheme;
138     int ok = 0;
139
140     /*
141      * Check that the given scheme conforms to correct scheme syntax as per
142      * RFC 3986:
143      *
144      * scheme        = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
145      */
146     if (isalpha(*scheme))
147         while (*scheme != '\0'
148                && (isalpha(*scheme)
149                    || isdigit(*scheme)
150                    || strchr("+-.", *scheme) != NULL))
151             scheme++;
152     if (*scheme != '\0') {
153         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
154                       OSSL_STORE_R_INVALID_SCHEME);
155         ERR_add_error_data(4, "scheme=", loader->scheme);
156         return 0;
157     }
158
159     if (!RUN_ONCE(&registry_init, do_registry_init)) {
160         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
161                       ERR_R_MALLOC_FAILURE);
162         return 0;
163     }
164     CRYPTO_THREAD_write_lock(registry_lock);
165
166     if (loader_register == NULL) {
167         loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
168                                                    store_loader_cmp);
169     }
170
171     if (loader_register != NULL
172         && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
173             || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
174         ok = 1;
175
176     CRYPTO_THREAD_unlock(registry_lock);
177
178     return ok;
179 }
180 int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
181 {
182     if (!ossl_store_init_once())
183         return 0;
184     return ossl_store_register_loader_int(loader);
185 }
186
187 const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
188 {
189     OSSL_STORE_LOADER template;
190     OSSL_STORE_LOADER *loader = NULL;
191
192     template.scheme = scheme;
193     template.open = NULL;
194     template.load = NULL;
195     template.eof = NULL;
196     template.close = NULL;
197
198     if (!ossl_store_init_once())
199         return NULL;
200
201     if (!RUN_ONCE(&registry_init, do_registry_init)) {
202         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
203                       ERR_R_MALLOC_FAILURE);
204         return NULL;
205     }
206     CRYPTO_THREAD_write_lock(registry_lock);
207
208     loader = lh_OSSL_STORE_LOADER_retrieve(loader_register, &template);
209
210     if (loader == NULL) {
211         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
212                       OSSL_STORE_R_UNREGISTERED_SCHEME);
213         ERR_add_error_data(2, "scheme=", scheme);
214     }
215
216     CRYPTO_THREAD_unlock(registry_lock);
217
218     return loader;
219 }
220
221 OSSL_STORE_LOADER *ossl_store_unregister_loader_int(const char *scheme)
222 {
223     OSSL_STORE_LOADER template;
224     OSSL_STORE_LOADER *loader = NULL;
225
226     template.scheme = scheme;
227     template.open = NULL;
228     template.load = NULL;
229     template.eof = NULL;
230     template.close = NULL;
231
232     if (!RUN_ONCE(&registry_init, do_registry_init)) {
233         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
234                       ERR_R_MALLOC_FAILURE);
235         return NULL;
236     }
237     CRYPTO_THREAD_write_lock(registry_lock);
238
239     loader = lh_OSSL_STORE_LOADER_delete(loader_register, &template);
240
241     if (loader == NULL) {
242         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
243                       OSSL_STORE_R_UNREGISTERED_SCHEME);
244         ERR_add_error_data(2, "scheme=", scheme);
245     }
246
247     CRYPTO_THREAD_unlock(registry_lock);
248
249     return loader;
250 }
251 OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
252 {
253     if (!ossl_store_init_once())
254         return 0;
255     return ossl_store_unregister_loader_int(scheme);
256 }
257
258 void ossl_store_destroy_loaders_int(void)
259 {
260     assert(lh_OSSL_STORE_LOADER_num_items(loader_register) == 0);
261     lh_OSSL_STORE_LOADER_free(loader_register);
262     loader_register = NULL;
263     CRYPTO_THREAD_lock_free(registry_lock);
264     registry_lock = NULL;
265 }
266
267 /*
268  *  Functions to list OSSL_STORE loaders
269  */
270
271 IMPLEMENT_LHASH_DOALL_ARG_CONST(OSSL_STORE_LOADER, void);
272 int OSSL_STORE_do_all_loaders(void (*do_function) (const OSSL_STORE_LOADER
273                                                    *loader, void *do_arg),
274                               void *do_arg)
275 {
276     lh_OSSL_STORE_LOADER_doall_void(loader_register, do_function, do_arg);
277     return 1;
278 }