]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/store/store_meth.c
Copyright year updates
[thirdparty/openssl.git] / crypto / store / store_meth.c
1 /*
2 * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <openssl/crypto.h>
11 #include "crypto/store.h"
12 #include "internal/core.h"
13 #include "internal/namemap.h"
14 #include "internal/property.h"
15 #include "internal/provider.h"
16 #include "store_local.h"
17 #include "crypto/context.h"
18
19 int OSSL_STORE_LOADER_up_ref(OSSL_STORE_LOADER *loader)
20 {
21 int ref = 0;
22
23 if (loader->prov != NULL)
24 CRYPTO_UP_REF(&loader->refcnt, &ref);
25 return 1;
26 }
27
28 void OSSL_STORE_LOADER_free(OSSL_STORE_LOADER *loader)
29 {
30 if (loader != NULL && loader->prov != NULL) {
31 int i;
32
33 CRYPTO_DOWN_REF(&loader->refcnt, &i);
34 if (i > 0)
35 return;
36 ossl_provider_free(loader->prov);
37 CRYPTO_FREE_REF(&loader->refcnt);
38 }
39 OPENSSL_free(loader);
40 }
41
42 /*
43 * OSSL_STORE_LOADER_new() expects the scheme as a constant string,
44 * which we currently don't have, so we need an alternative allocator.
45 */
46 static OSSL_STORE_LOADER *new_loader(OSSL_PROVIDER *prov)
47 {
48 OSSL_STORE_LOADER *loader;
49
50 if ((loader = OPENSSL_zalloc(sizeof(*loader))) == NULL
51 || !CRYPTO_NEW_REF(&loader->refcnt, 1)) {
52 OPENSSL_free(loader);
53 return NULL;
54 }
55 loader->prov = prov;
56 ossl_provider_up_ref(prov);
57
58 return loader;
59 }
60
61 static int up_ref_loader(void *method)
62 {
63 return OSSL_STORE_LOADER_up_ref(method);
64 }
65
66 static void free_loader(void *method)
67 {
68 OSSL_STORE_LOADER_free(method);
69 }
70
71 /* Data to be passed through ossl_method_construct() */
72 struct loader_data_st {
73 OSSL_LIB_CTX *libctx;
74 int scheme_id; /* For get_loader_from_store() */
75 const char *scheme; /* For get_loader_from_store() */
76 const char *propquery; /* For get_loader_from_store() */
77
78 OSSL_METHOD_STORE *tmp_store; /* For get_tmp_loader_store() */
79
80 unsigned int flag_construct_error_occurred : 1;
81 };
82
83 /*
84 * Generic routines to fetch / create OSSL_STORE methods with
85 * ossl_method_construct()
86 */
87
88 /* Temporary loader method store, constructor and destructor */
89 static void *get_tmp_loader_store(void *data)
90 {
91 struct loader_data_st *methdata = data;
92
93 if (methdata->tmp_store == NULL)
94 methdata->tmp_store = ossl_method_store_new(methdata->libctx);
95 return methdata->tmp_store;
96 }
97
98 static void dealloc_tmp_loader_store(void *store)
99 {
100 if (store != NULL)
101 ossl_method_store_free(store);
102 }
103
104 /* Get the permanent loader store */
105 static OSSL_METHOD_STORE *get_loader_store(OSSL_LIB_CTX *libctx)
106 {
107 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_STORE_LOADER_STORE_INDEX);
108 }
109
110 static int reserve_loader_store(void *store, void *data)
111 {
112 struct loader_data_st *methdata = data;
113
114 if (store == NULL
115 && (store = get_loader_store(methdata->libctx)) == NULL)
116 return 0;
117
118 return ossl_method_lock_store(store);
119 }
120
121 static int unreserve_loader_store(void *store, void *data)
122 {
123 struct loader_data_st *methdata = data;
124
125 if (store == NULL
126 && (store = get_loader_store(methdata->libctx)) == NULL)
127 return 0;
128
129 return ossl_method_unlock_store(store);
130 }
131
132 /* Get loader methods from a store, or put one in */
133 static void *get_loader_from_store(void *store, const OSSL_PROVIDER **prov,
134 void *data)
135 {
136 struct loader_data_st *methdata = data;
137 void *method = NULL;
138 int id;
139
140 if ((id = methdata->scheme_id) == 0) {
141 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
142
143 id = ossl_namemap_name2num(namemap, methdata->scheme);
144 }
145
146 if (store == NULL
147 && (store = get_loader_store(methdata->libctx)) == NULL)
148 return NULL;
149
150 if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method))
151 return NULL;
152 return method;
153 }
154
155 static int put_loader_in_store(void *store, void *method,
156 const OSSL_PROVIDER *prov,
157 const char *scheme, const char *propdef,
158 void *data)
159 {
160 struct loader_data_st *methdata = data;
161 OSSL_NAMEMAP *namemap;
162 int id;
163
164 if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
165 || (id = ossl_namemap_name2num(namemap, scheme)) == 0)
166 return 0;
167
168 if (store == NULL && (store = get_loader_store(methdata->libctx)) == NULL)
169 return 0;
170
171 return ossl_method_store_add(store, prov, id, propdef, method,
172 up_ref_loader, free_loader);
173 }
174
175 static void *loader_from_algorithm(int scheme_id, const OSSL_ALGORITHM *algodef,
176 OSSL_PROVIDER *prov)
177 {
178 OSSL_STORE_LOADER *loader = NULL;
179 const OSSL_DISPATCH *fns = algodef->implementation;
180
181 if ((loader = new_loader(prov)) == NULL)
182 return NULL;
183 loader->scheme_id = scheme_id;
184 loader->propdef = algodef->property_definition;
185 loader->description = algodef->algorithm_description;
186
187 for (; fns->function_id != 0; fns++) {
188 switch (fns->function_id) {
189 case OSSL_FUNC_STORE_OPEN:
190 if (loader->p_open == NULL)
191 loader->p_open = OSSL_FUNC_store_open(fns);
192 break;
193 case OSSL_FUNC_STORE_ATTACH:
194 if (loader->p_attach == NULL)
195 loader->p_attach = OSSL_FUNC_store_attach(fns);
196 break;
197 case OSSL_FUNC_STORE_SETTABLE_CTX_PARAMS:
198 if (loader->p_settable_ctx_params == NULL)
199 loader->p_settable_ctx_params =
200 OSSL_FUNC_store_settable_ctx_params(fns);
201 break;
202 case OSSL_FUNC_STORE_SET_CTX_PARAMS:
203 if (loader->p_set_ctx_params == NULL)
204 loader->p_set_ctx_params = OSSL_FUNC_store_set_ctx_params(fns);
205 break;
206 case OSSL_FUNC_STORE_LOAD:
207 if (loader->p_load == NULL)
208 loader->p_load = OSSL_FUNC_store_load(fns);
209 break;
210 case OSSL_FUNC_STORE_EOF:
211 if (loader->p_eof == NULL)
212 loader->p_eof = OSSL_FUNC_store_eof(fns);
213 break;
214 case OSSL_FUNC_STORE_CLOSE:
215 if (loader->p_close == NULL)
216 loader->p_close = OSSL_FUNC_store_close(fns);
217 break;
218 case OSSL_FUNC_STORE_EXPORT_OBJECT:
219 if (loader->p_export_object == NULL)
220 loader->p_export_object = OSSL_FUNC_store_export_object(fns);
221 break;
222 }
223 }
224
225 if ((loader->p_open == NULL && loader->p_attach == NULL)
226 || loader->p_load == NULL
227 || loader->p_eof == NULL
228 || loader->p_close == NULL) {
229 /* Only set_ctx_params is optionaal */
230 OSSL_STORE_LOADER_free(loader);
231 ERR_raise(ERR_LIB_OSSL_STORE, OSSL_STORE_R_LOADER_INCOMPLETE);
232 return NULL;
233 }
234 return loader;
235 }
236
237 /*
238 * The core fetching functionality passes the scheme of the implementation.
239 * This function is responsible to getting an identity number for them,
240 * then call loader_from_algorithm() with that identity number.
241 */
242 static void *construct_loader(const OSSL_ALGORITHM *algodef,
243 OSSL_PROVIDER *prov, void *data)
244 {
245 /*
246 * This function is only called if get_loader_from_store() returned
247 * NULL, so it's safe to say that of all the spots to create a new
248 * namemap entry, this is it. Should the scheme already exist there, we
249 * know that ossl_namemap_add() will return its corresponding number.
250 */
251 struct loader_data_st *methdata = data;
252 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
253 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
254 const char *scheme = algodef->algorithm_names;
255 int id = ossl_namemap_add_name(namemap, 0, scheme);
256 void *method = NULL;
257
258 if (id != 0)
259 method = loader_from_algorithm(id, algodef, prov);
260
261 /*
262 * Flag to indicate that there was actual construction errors. This
263 * helps inner_loader_fetch() determine what error it should
264 * record on inaccessible algorithms.
265 */
266 if (method == NULL)
267 methdata->flag_construct_error_occurred = 1;
268
269 return method;
270 }
271
272 /* Intermediary function to avoid ugly casts, used below */
273 static void destruct_loader(void *method, void *data)
274 {
275 OSSL_STORE_LOADER_free(method);
276 }
277
278 /* Fetching support. Can fetch by numeric identity or by scheme */
279 static OSSL_STORE_LOADER *
280 inner_loader_fetch(struct loader_data_st *methdata,
281 const char *scheme, const char *properties)
282 {
283 OSSL_METHOD_STORE *store = get_loader_store(methdata->libctx);
284 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
285 const char *const propq = properties != NULL ? properties : "";
286 void *method = NULL;
287 int unsupported, id;
288
289 if (store == NULL || namemap == NULL) {
290 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_INVALID_ARGUMENT);
291 return NULL;
292 }
293
294 /* If we haven't received a name id yet, try to get one for the name */
295 id = scheme != NULL ? ossl_namemap_name2num(namemap, scheme) : 0;
296
297 /*
298 * If we haven't found the name yet, chances are that the algorithm to
299 * be fetched is unsupported.
300 */
301 unsupported = id == 0;
302
303 if (id == 0
304 || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) {
305 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
306 get_tmp_loader_store,
307 reserve_loader_store,
308 unreserve_loader_store,
309 get_loader_from_store,
310 put_loader_in_store,
311 construct_loader,
312 destruct_loader
313 };
314 OSSL_PROVIDER *prov = NULL;
315
316 methdata->scheme_id = id;
317 methdata->scheme = scheme;
318 methdata->propquery = propq;
319 methdata->flag_construct_error_occurred = 0;
320 if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_STORE,
321 &prov, 0 /* !force_cache */,
322 &mcm, methdata)) != NULL) {
323 /*
324 * If construction did create a method for us, we know that there
325 * is a correct scheme_id, since those have already been calculated
326 * in get_loader_from_store() and put_loader_in_store() above.
327 */
328 if (id == 0)
329 id = ossl_namemap_name2num(namemap, scheme);
330 ossl_method_store_cache_set(store, prov, id, propq, method,
331 up_ref_loader, free_loader);
332 }
333
334 /*
335 * If we never were in the constructor, the algorithm to be fetched
336 * is unsupported.
337 */
338 unsupported = !methdata->flag_construct_error_occurred;
339 }
340
341 if ((id != 0 || scheme != NULL) && method == NULL) {
342 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
343 const char *helpful_msg =
344 unsupported
345 ? ( "No store loader found. For standard store loaders you need "
346 "at least one of the default or base providers available. "
347 "Did you forget to load them? Info: " )
348 : "";
349
350 if (scheme == NULL)
351 scheme = ossl_namemap_num2name(namemap, id, 0);
352 ERR_raise_data(ERR_LIB_OSSL_STORE, code,
353 "%s%s, Scheme (%s : %d), Properties (%s)",
354 helpful_msg,
355 ossl_lib_ctx_get_descriptor(methdata->libctx),
356 scheme == NULL ? "<null>" : scheme, id,
357 properties == NULL ? "<null>" : properties);
358 }
359
360 return method;
361 }
362
363 OSSL_STORE_LOADER *OSSL_STORE_LOADER_fetch(OSSL_LIB_CTX *libctx,
364 const char *scheme,
365 const char *properties)
366 {
367 struct loader_data_st methdata;
368 void *method;
369
370 methdata.libctx = libctx;
371 methdata.tmp_store = NULL;
372 method = inner_loader_fetch(&methdata, scheme, properties);
373 dealloc_tmp_loader_store(methdata.tmp_store);
374 return method;
375 }
376
377 int ossl_store_loader_store_cache_flush(OSSL_LIB_CTX *libctx)
378 {
379 OSSL_METHOD_STORE *store = get_loader_store(libctx);
380
381 if (store != NULL)
382 return ossl_method_store_cache_flush_all(store);
383 return 1;
384 }
385
386 int ossl_store_loader_store_remove_all_provided(const OSSL_PROVIDER *prov)
387 {
388 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
389 OSSL_METHOD_STORE *store = get_loader_store(libctx);
390
391 if (store != NULL)
392 return ossl_method_store_remove_all_provided(store, prov);
393 return 1;
394 }
395
396 /*
397 * Library of basic method functions
398 */
399
400 const OSSL_PROVIDER *OSSL_STORE_LOADER_get0_provider(const OSSL_STORE_LOADER *loader)
401 {
402 if (!ossl_assert(loader != NULL)) {
403 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
404 return 0;
405 }
406
407 return loader->prov;
408 }
409
410 const char *OSSL_STORE_LOADER_get0_properties(const OSSL_STORE_LOADER *loader)
411 {
412 if (!ossl_assert(loader != NULL)) {
413 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
414 return 0;
415 }
416
417 return loader->propdef;
418 }
419
420 int ossl_store_loader_get_number(const OSSL_STORE_LOADER *loader)
421 {
422 if (!ossl_assert(loader != NULL)) {
423 ERR_raise(ERR_LIB_OSSL_STORE, ERR_R_PASSED_NULL_PARAMETER);
424 return 0;
425 }
426
427 return loader->scheme_id;
428 }
429
430 const char *OSSL_STORE_LOADER_get0_description(const OSSL_STORE_LOADER *loader)
431 {
432 return loader->description;
433 }
434
435 int OSSL_STORE_LOADER_is_a(const OSSL_STORE_LOADER *loader, const char *name)
436 {
437 if (loader->prov != NULL) {
438 OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
439 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
440
441 return ossl_namemap_name2num(namemap, name) == loader->scheme_id;
442 }
443 return 0;
444 }
445
446 struct do_one_data_st {
447 void (*user_fn)(OSSL_STORE_LOADER *loader, void *arg);
448 void *user_arg;
449 };
450
451 static void do_one(ossl_unused int id, void *method, void *arg)
452 {
453 struct do_one_data_st *data = arg;
454
455 data->user_fn(method, data->user_arg);
456 }
457
458 void OSSL_STORE_LOADER_do_all_provided(OSSL_LIB_CTX *libctx,
459 void (*user_fn)(OSSL_STORE_LOADER *loader,
460 void *arg),
461 void *user_arg)
462 {
463 struct loader_data_st methdata;
464 struct do_one_data_st data;
465
466 methdata.libctx = libctx;
467 methdata.tmp_store = NULL;
468 (void)inner_loader_fetch(&methdata, NULL, NULL /* properties */);
469
470 data.user_fn = user_fn;
471 data.user_arg = user_arg;
472 if (methdata.tmp_store != NULL)
473 ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
474 ossl_method_store_do_all(get_loader_store(libctx), &do_one, &data);
475 dealloc_tmp_loader_store(methdata.tmp_store);
476 }
477
478 int OSSL_STORE_LOADER_names_do_all(const OSSL_STORE_LOADER *loader,
479 void (*fn)(const char *name, void *data),
480 void *data)
481 {
482 if (loader == NULL)
483 return 0;
484
485 if (loader->prov != NULL) {
486 OSSL_LIB_CTX *libctx = ossl_provider_libctx(loader->prov);
487 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
488
489 return ossl_namemap_doall_names(namemap, loader->scheme_id, fn, data);
490 }
491
492 return 1;
493 }