2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include "crypto/ctype.h"
14 #include <openssl/err.h>
15 #include <openssl/lhash.h>
16 #include "store_local.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(ENGINE
*e
, const char *scheme
)
33 OSSL_STORE_LOADER
*res
= NULL
;
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
42 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW
,
43 OSSL_STORE_R_INVALID_SCHEME
);
47 if ((res
= OPENSSL_zalloc(sizeof(*res
))) == NULL
) {
48 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_LOADER_NEW
, ERR_R_MALLOC_FAILURE
);
57 const ENGINE
*OSSL_STORE_LOADER_get0_engine(const OSSL_STORE_LOADER
*loader
)
59 return loader
->engine
;
62 const char *OSSL_STORE_LOADER_get0_scheme(const OSSL_STORE_LOADER
*loader
)
64 return loader
->scheme
;
67 int OSSL_STORE_LOADER_set_open(OSSL_STORE_LOADER
*loader
,
68 OSSL_STORE_open_fn open_function
)
70 loader
->open
= open_function
;
74 int OSSL_STORE_LOADER_set_attach(OSSL_STORE_LOADER
*loader
,
75 OSSL_STORE_attach_fn attach_function
)
77 loader
->attach
= attach_function
;
81 int OSSL_STORE_LOADER_set_ctrl(OSSL_STORE_LOADER
*loader
,
82 OSSL_STORE_ctrl_fn ctrl_function
)
84 loader
->ctrl
= ctrl_function
;
88 int OSSL_STORE_LOADER_set_expect(OSSL_STORE_LOADER
*loader
,
89 OSSL_STORE_expect_fn expect_function
)
91 loader
->expect
= expect_function
;
95 int OSSL_STORE_LOADER_set_find(OSSL_STORE_LOADER
*loader
,
96 OSSL_STORE_find_fn find_function
)
98 loader
->find
= find_function
;
102 int OSSL_STORE_LOADER_set_load(OSSL_STORE_LOADER
*loader
,
103 OSSL_STORE_load_fn load_function
)
105 loader
->load
= load_function
;
109 int OSSL_STORE_LOADER_set_eof(OSSL_STORE_LOADER
*loader
,
110 OSSL_STORE_eof_fn eof_function
)
112 loader
->eof
= eof_function
;
116 int OSSL_STORE_LOADER_set_error(OSSL_STORE_LOADER
*loader
,
117 OSSL_STORE_error_fn error_function
)
119 loader
->error
= error_function
;
123 int OSSL_STORE_LOADER_set_close(OSSL_STORE_LOADER
*loader
,
124 OSSL_STORE_close_fn close_function
)
126 loader
->close
= close_function
;
130 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER
*loader
)
132 OPENSSL_free(loader
);
136 * Functions for registering OSSL_STORE_LOADERs
139 static unsigned long store_loader_hash(const OSSL_STORE_LOADER
*v
)
141 return OPENSSL_LH_strhash(v
->scheme
);
144 static int store_loader_cmp(const OSSL_STORE_LOADER
*a
,
145 const OSSL_STORE_LOADER
*b
)
147 assert(a
->scheme
!= NULL
&& b
->scheme
!= NULL
);
148 return strcmp(a
->scheme
, b
->scheme
);
151 static LHASH_OF(OSSL_STORE_LOADER
) *loader_register
= NULL
;
153 int ossl_store_register_loader_int(OSSL_STORE_LOADER
*loader
)
155 const char *scheme
= loader
->scheme
;
159 * Check that the given scheme conforms to correct scheme syntax as per
162 * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
164 if (ossl_isalpha(*scheme
))
165 while (*scheme
!= '\0'
166 && (ossl_isalpha(*scheme
)
167 || ossl_isdigit(*scheme
)
168 || strchr("+-.", *scheme
) != NULL
))
170 if (*scheme
!= '\0') {
171 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT
,
172 OSSL_STORE_R_INVALID_SCHEME
);
173 ERR_add_error_data(2, "scheme=", loader
->scheme
);
177 /* Check that functions we absolutely require are present */
178 if (loader
->open
== NULL
|| loader
->load
== NULL
|| loader
->eof
== NULL
179 || loader
->error
== NULL
|| loader
->close
== NULL
) {
180 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT
,
181 OSSL_STORE_R_LOADER_INCOMPLETE
);
185 if (!RUN_ONCE(®istry_init
, do_registry_init
)) {
186 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_REGISTER_LOADER_INT
,
187 ERR_R_MALLOC_FAILURE
);
190 CRYPTO_THREAD_write_lock(registry_lock
);
192 if (loader_register
== NULL
) {
193 loader_register
= lh_OSSL_STORE_LOADER_new(store_loader_hash
,
197 if (loader_register
!= NULL
198 && (lh_OSSL_STORE_LOADER_insert(loader_register
, loader
) != NULL
199 || lh_OSSL_STORE_LOADER_error(loader_register
) == 0))
202 CRYPTO_THREAD_unlock(registry_lock
);
206 int OSSL_STORE_register_loader(OSSL_STORE_LOADER
*loader
)
208 if (!ossl_store_init_once())
210 return ossl_store_register_loader_int(loader
);
213 const OSSL_STORE_LOADER
*ossl_store_get0_loader_int(const char *scheme
)
215 OSSL_STORE_LOADER
template;
216 OSSL_STORE_LOADER
*loader
= NULL
;
218 template.scheme
= scheme
;
219 template.open
= NULL
;
220 template.load
= NULL
;
222 template.close
= NULL
;
224 if (!ossl_store_init_once())
227 if (!RUN_ONCE(®istry_init
, do_registry_init
)) {
228 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT
,
229 ERR_R_MALLOC_FAILURE
);
232 CRYPTO_THREAD_write_lock(registry_lock
);
234 loader
= lh_OSSL_STORE_LOADER_retrieve(loader_register
, &template);
236 if (loader
== NULL
) {
237 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_GET0_LOADER_INT
,
238 OSSL_STORE_R_UNREGISTERED_SCHEME
);
239 ERR_add_error_data(2, "scheme=", scheme
);
242 CRYPTO_THREAD_unlock(registry_lock
);
247 OSSL_STORE_LOADER
*ossl_store_unregister_loader_int(const char *scheme
)
249 OSSL_STORE_LOADER
template;
250 OSSL_STORE_LOADER
*loader
= NULL
;
252 template.scheme
= scheme
;
253 template.open
= NULL
;
254 template.load
= NULL
;
256 template.close
= NULL
;
258 if (!RUN_ONCE(®istry_init
, do_registry_init
)) {
259 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT
,
260 ERR_R_MALLOC_FAILURE
);
263 CRYPTO_THREAD_write_lock(registry_lock
);
265 loader
= lh_OSSL_STORE_LOADER_delete(loader_register
, &template);
267 if (loader
== NULL
) {
268 OSSL_STOREerr(OSSL_STORE_F_OSSL_STORE_UNREGISTER_LOADER_INT
,
269 OSSL_STORE_R_UNREGISTERED_SCHEME
);
270 ERR_add_error_data(2, "scheme=", scheme
);
273 CRYPTO_THREAD_unlock(registry_lock
);
277 OSSL_STORE_LOADER
*OSSL_STORE_unregister_loader(const char *scheme
)
279 if (!ossl_store_init_once())
281 return ossl_store_unregister_loader_int(scheme
);
284 void ossl_store_destroy_loaders_int(void)
286 assert(lh_OSSL_STORE_LOADER_num_items(loader_register
) == 0);
287 lh_OSSL_STORE_LOADER_free(loader_register
);
288 loader_register
= NULL
;
289 CRYPTO_THREAD_lock_free(registry_lock
);
290 registry_lock
= NULL
;
294 * Functions to list OSSL_STORE loaders
297 IMPLEMENT_LHASH_DOALL_ARG_CONST(OSSL_STORE_LOADER
, void);
298 int OSSL_STORE_do_all_loaders(void (*do_function
) (const OSSL_STORE_LOADER
299 *loader
, void *do_arg
),
302 lh_OSSL_STORE_LOADER_doall_void(loader_register
, do_function
, do_arg
);