a138edc6362b91bf321ca9a040daaafae856ae62
[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(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->scheme = scheme;
53     return res;
54 }
55
56 const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader)
57 {
58     return loader->scheme;
59 }
60
61 int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
62                                OSSL_STORE_open_fn open_function)
63 {
64     loader->open = open_function;
65     return 1;
66 }
67
68 int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
69                                OSSL_STORE_ctrl_fn ctrl_function)
70 {
71     loader->ctrl = ctrl_function;
72     return 1;
73 }
74
75 int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
76                                OSSL_STORE_load_fn load_function)
77 {
78     loader->load = load_function;
79     return 1;
80 }
81
82 int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
83                               OSSL_STORE_eof_fn eof_function)
84 {
85     loader->eof = eof_function;
86     return 1;
87 }
88
89 int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
90                                 OSSL_STORE_error_fn error_function)
91 {
92     loader->error = error_function;
93     return 1;
94 }
95
96 int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
97                                 OSSL_STORE_close_fn close_function)
98 {
99     loader->close = close_function;
100     return 1;
101 }
102
103 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
104 {
105     OPENSSL_free(loader);
106 }
107
108 /*
109  *  Functions for registering OSSL_STORE_LOADERs
110  */
111
112 static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
113 {
114     return OPENSSL_LH_strhash(v->scheme);
115 }
116
117 static int store_loader_cmp(const OSSL_STORE_LOADER *a,
118                             const OSSL_STORE_LOADER *b)
119 {
120     if (a->scheme != NULL && b->scheme != NULL)
121         return strcmp(a->scheme, b->scheme);
122     else if (a->scheme == b->scheme)
123         return 0;
124     return a->scheme == NULL ? -1 : 1;
125 }
126
127 static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
128
129 int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
130 {
131     const char *scheme = loader->scheme;
132     int ok = 0;
133
134     /*
135      * Check that the given scheme conforms to correct scheme syntax as per
136      * RFC 3986:
137      *
138      * scheme        = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
139      */
140     if (isalpha(*scheme))
141         while (*scheme != '\0'
142                && (isalpha(*scheme)
143                    || isdigit(*scheme)
144                    || strchr("+-.", *scheme) != NULL))
145             scheme++;
146     if (*scheme != '\0') {
147         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
148                       OSSL_STORE_R_INVALID_SCHEME);
149         ERR_add_error_data(4, "scheme=", loader->scheme);
150         return 0;
151     }
152
153     if (!RUN_ONCE(&registry_init, do_registry_init)) {
154         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
155                       ERR_R_MALLOC_FAILURE);
156         return 0;
157     }
158     CRYPTO_THREAD_write_lock(registry_lock);
159
160     if (loader_register == NULL) {
161         loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
162                                                    store_loader_cmp);
163     }
164
165     if (loader_register != NULL
166         && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
167             || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
168         ok = 1;
169
170     CRYPTO_THREAD_unlock(registry_lock);
171
172     return ok;
173 }
174 int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
175 {
176     if (!ossl_store_init_once())
177         return 0;
178     return ossl_store_register_loader_int(loader);
179 }
180
181 const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
182 {
183     OSSL_STORE_LOADER template;
184     OSSL_STORE_LOADER *loader = NULL;
185
186     template.scheme = scheme;
187     template.open = NULL;
188     template.load = NULL;
189     template.eof = NULL;
190     template.close = NULL;
191
192     if (!ossl_store_init_once())
193         return NULL;
194
195     if (!RUN_ONCE(&registry_init, do_registry_init)) {
196         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
197                       ERR_R_MALLOC_FAILURE);
198         return NULL;
199     }
200     CRYPTO_THREAD_write_lock(registry_lock);
201
202     loader = lh_OSSL_STORE_LOADER_retrieve(loader_register, &template);
203
204     if (loader == NULL) {
205         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
206                       OSSL_STORE_R_UNREGISTERED_SCHEME);
207         ERR_add_error_data(2, "scheme=", scheme);
208     }
209
210     CRYPTO_THREAD_unlock(registry_lock);
211
212     return loader;
213 }
214
215 OSSL_STORE_LOADER *ossl_store_unregister_loader_int(const char *scheme)
216 {
217     OSSL_STORE_LOADER template;
218     OSSL_STORE_LOADER *loader = NULL;
219
220     template.scheme = scheme;
221     template.open = NULL;
222     template.load = NULL;
223     template.eof = NULL;
224     template.close = NULL;
225
226     if (!RUN_ONCE(&registry_init, do_registry_init)) {
227         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
228                       ERR_R_MALLOC_FAILURE);
229         return NULL;
230     }
231     CRYPTO_THREAD_write_lock(registry_lock);
232
233     loader = lh_OSSL_STORE_LOADER_delete(loader_register, &template);
234
235     if (loader == NULL) {
236         OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
237                       OSSL_STORE_R_UNREGISTERED_SCHEME);
238         ERR_add_error_data(2, "scheme=", scheme);
239     }
240
241     CRYPTO_THREAD_unlock(registry_lock);
242
243     return loader;
244 }
245 OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
246 {
247     if (!ossl_store_init_once())
248         return 0;
249     return ossl_store_unregister_loader_int(scheme);
250 }
251
252 void ossl_store_destroy_loaders_int(void)
253 {
254     assert(lh_OSSL_STORE_LOADER_num_items(loader_register) == 0);
255     lh_OSSL_STORE_LOADER_free(loader_register);
256     loader_register = NULL;
257     CRYPTO_THREAD_lock_free(registry_lock);
258     registry_lock = NULL;
259 }