2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
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
14 #include <openssl/err.h>
15 #include <openssl/lhash.h>
16 #include "store_locl.h"
18 static CRYPTO_RWLOCK *registry_lock;
19 static CRYPTO_ONCE registry_init = CRYPTO_ONCE_STATIC_INIT;
21 DEFINE_RUN_ONCE_STATIC(do_registry_init)
23 registry_lock = CRYPTO_THREAD_lock_new();
24 return registry_lock != NULL;
28 * Functions for manipulating OSSL_STORE_LOADERs
31 OSSL_STORE_LOADER *OSSL_STORE_LOADER_new(const char *scheme)
33 OSSL_STORE_LOADER *res = OPENSSL_zalloc(sizeof(*res));
36 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW, ERR_R_MALLOC_FAILURE);
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
47 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW,
48 OSSL_STORE_R_INVALID_SCHEME);
56 const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER *loader)
58 return loader->scheme;
61 int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER *loader,
62 OSSL_STORE_open_fn open_function)
64 loader->open = open_function;
68 int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER *loader,
69 OSSL_STORE_ctrl_fn ctrl_function)
71 loader->ctrl = ctrl_function;
75 int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER *loader,
76 OSSL_STORE_load_fn load_function)
78 loader->load = load_function;
82 int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER *loader,
83 OSSL_STORE_eof_fn eof_function)
85 loader->eof = eof_function;
89 int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER *loader,
90 OSSL_STORE_error_fn error_function)
92 loader->error = error_function;
96 int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER *loader,
97 OSSL_STORE_close_fn close_function)
99 loader->close = close_function;
103 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
105 OPENSSL_free(loader);
109 * Functions for registering OSSL_STORE_LOADERs
112 static unsigned long store_loader_hash(const OSSL_STORE_LOADER *v)
114 return OPENSSL_LH_strhash(v->scheme);
117 static int store_loader_cmp(const OSSL_STORE_LOADER *a,
118 const OSSL_STORE_LOADER *b)
120 if (a->scheme != NULL && b->scheme != NULL)
121 return strcmp(a->scheme, b->scheme);
122 else if (a->scheme == b->scheme)
124 return a->scheme == NULL ? -1 : 1;
127 static LHASH_OF(OSSL_STORE_LOADER) *loader_register = NULL;
129 int ossl_store_register_loader_int(OSSL_STORE_LOADER *loader)
131 const char *scheme = loader->scheme;
135 * Check that the given scheme conforms to correct scheme syntax as per
138 * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
140 if (isalpha(*scheme))
141 while (*scheme != '\0'
144 || strchr("+-.", *scheme) != NULL))
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);
153 if (!RUN_ONCE(®istry_init, do_registry_init)) {
154 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT,
155 ERR_R_MALLOC_FAILURE);
158 CRYPTO_THREAD_write_lock(registry_lock);
160 if (loader_register == NULL) {
161 loader_register = lh_OSSL_STORE_LOADER_new(store_loader_hash,
165 if (loader_register != NULL
166 && (lh_OSSL_STORE_LOADER_insert(loader_register, loader) != NULL
167 || lh_OSSL_STORE_LOADER_error(loader_register) == 0))
170 CRYPTO_THREAD_unlock(registry_lock);
174 int OSSL_STORE_register_loader(OSSL_STORE_LOADER *loader)
176 if (!ossl_store_init_once())
178 return ossl_store_register_loader_int(loader);
181 const OSSL_STORE_LOADER *ossl_store_get0_loader_int(const char *scheme)
183 OSSL_STORE_LOADER template;
184 OSSL_STORE_LOADER *loader = NULL;
186 template.scheme = scheme;
187 template.open = NULL;
188 template.load = NULL;
190 template.close = NULL;
192 if (!ossl_store_init_once())
195 if (!RUN_ONCE(®istry_init, do_registry_init)) {
196 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT,
197 ERR_R_MALLOC_FAILURE);
200 CRYPTO_THREAD_write_lock(registry_lock);
202 loader = lh_OSSL_STORE_LOADER_retrieve(loader_register, &template);
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);
210 CRYPTO_THREAD_unlock(registry_lock);
215 OSSL_STORE_LOADER *ossl_store_unregister_loader_int(const char *scheme)
217 OSSL_STORE_LOADER template;
218 OSSL_STORE_LOADER *loader = NULL;
220 template.scheme = scheme;
221 template.open = NULL;
222 template.load = NULL;
224 template.close = NULL;
226 if (!RUN_ONCE(®istry_init, do_registry_init)) {
227 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT,
228 ERR_R_MALLOC_FAILURE);
231 CRYPTO_THREAD_write_lock(registry_lock);
233 loader = lh_OSSL_STORE_LOADER_delete(loader_register, &template);
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);
241 CRYPTO_THREAD_unlock(registry_lock);
245 OSSL_STORE_LOADER *OSSL_STORE_unregister_loader(const char *scheme)
247 if (!ossl_store_init_once())
249 return ossl_store_unregister_loader_int(scheme);
252 void ossl_store_destroy_loaders_int(void)
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;