]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/encode_decode/decoder_meth.c
Don't hold a lock when calling a callback in ossl_namemap_doall_names
[thirdparty/openssl.git] / crypto / encode_decode / decoder_meth.c
1 /*
2 * Copyright 2020-2021 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/core.h>
11 #include <openssl/core_dispatch.h>
12 #include <openssl/decoder.h>
13 #include <openssl/ui.h>
14 #include "internal/core.h"
15 #include "internal/namemap.h"
16 #include "internal/property.h"
17 #include "internal/provider.h"
18 #include "crypto/decoder.h"
19 #include "encoder_local.h"
20
21 /*
22 * Decoder can have multiple names, separated with colons in a name string
23 */
24 #define NAME_SEPARATOR ':'
25
26 /* Simple method structure constructor and destructor */
27 static OSSL_DECODER *ossl_decoder_new(void)
28 {
29 OSSL_DECODER *decoder = NULL;
30
31 if ((decoder = OPENSSL_zalloc(sizeof(*decoder))) == NULL
32 || (decoder->base.lock = CRYPTO_THREAD_lock_new()) == NULL) {
33 OSSL_DECODER_free(decoder);
34 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
35 return NULL;
36 }
37
38 decoder->base.refcnt = 1;
39
40 return decoder;
41 }
42
43 int OSSL_DECODER_up_ref(OSSL_DECODER *decoder)
44 {
45 int ref = 0;
46
47 CRYPTO_UP_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
48 return 1;
49 }
50
51 void OSSL_DECODER_free(OSSL_DECODER *decoder)
52 {
53 int ref = 0;
54
55 if (decoder == NULL)
56 return;
57
58 CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
59 if (ref > 0)
60 return;
61 ossl_provider_free(decoder->base.prov);
62 CRYPTO_THREAD_lock_free(decoder->base.lock);
63 OPENSSL_free(decoder);
64 }
65
66 /* Permanent decoder method store, constructor and destructor */
67 static void decoder_store_free(void *vstore)
68 {
69 ossl_method_store_free(vstore);
70 }
71
72 static void *decoder_store_new(OSSL_LIB_CTX *ctx)
73 {
74 return ossl_method_store_new(ctx);
75 }
76
77
78 static const OSSL_LIB_CTX_METHOD decoder_store_method = {
79 decoder_store_new,
80 decoder_store_free,
81 };
82
83 /* Data to be passed through ossl_method_construct() */
84 struct decoder_data_st {
85 OSSL_LIB_CTX *libctx;
86 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
87 int id; /* For get_decoder_from_store() */
88 const char *names; /* For get_decoder_from_store() */
89 const char *propquery; /* For get_decoder_from_store() */
90
91 unsigned int flag_construct_error_occured : 1;
92 };
93
94 /*
95 * Generic routines to fetch / create DECODER methods with
96 * ossl_method_construct()
97 */
98
99 /* Temporary decoder method store, constructor and destructor */
100 static void *alloc_tmp_decoder_store(OSSL_LIB_CTX *ctx)
101 {
102 return ossl_method_store_new(ctx);
103 }
104
105 static void dealloc_tmp_decoder_store(void *store)
106 {
107 if (store != NULL)
108 ossl_method_store_free(store);
109 }
110
111 /* Get the permanent decoder store */
112 static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
113 {
114 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX,
115 &decoder_store_method);
116 }
117
118 /* Get decoder methods from a store, or put one in */
119 static void *get_decoder_from_store(OSSL_LIB_CTX *libctx, void *store,
120 void *data)
121 {
122 struct decoder_data_st *methdata = data;
123 void *method = NULL;
124 int id;
125
126 if ((id = methdata->id) == 0) {
127 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
128
129 id = ossl_namemap_name2num(namemap, methdata->names);
130 }
131
132 if (store == NULL
133 && (store = get_decoder_store(libctx)) == NULL)
134 return NULL;
135
136 if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
137 return NULL;
138 return method;
139 }
140
141 static int put_decoder_in_store(OSSL_LIB_CTX *libctx, void *store,
142 void *method, const OSSL_PROVIDER *prov,
143 int operation_id, const char *names,
144 const char *propdef, void *unused)
145 {
146 OSSL_NAMEMAP *namemap;
147 int id;
148
149 if ((namemap = ossl_namemap_stored(libctx)) == NULL
150 || (id = ossl_namemap_name2num(namemap, names)) == 0)
151 return 0;
152
153 if (store == NULL && (store = get_decoder_store(libctx)) == NULL)
154 return 0;
155
156 return ossl_method_store_add(store, prov, id, propdef, method,
157 (int (*)(void *))OSSL_DECODER_up_ref,
158 (void (*)(void *))OSSL_DECODER_free);
159 }
160
161 /* Create and populate a decoder method */
162 void *ossl_decoder_from_dispatch(int id, const OSSL_ALGORITHM *algodef,
163 OSSL_PROVIDER *prov)
164 {
165 OSSL_DECODER *decoder = NULL;
166 const OSSL_DISPATCH *fns = algodef->implementation;
167
168 if ((decoder = ossl_decoder_new()) == NULL)
169 return NULL;
170 decoder->base.id = id;
171 decoder->base.propdef = algodef->property_definition;
172
173 for (; fns->function_id != 0; fns++) {
174 switch (fns->function_id) {
175 case OSSL_FUNC_DECODER_NEWCTX:
176 if (decoder->newctx == NULL)
177 decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
178 break;
179 case OSSL_FUNC_DECODER_FREECTX:
180 if (decoder->freectx == NULL)
181 decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
182 break;
183 case OSSL_FUNC_DECODER_GET_PARAMS:
184 if (decoder->get_params == NULL)
185 decoder->get_params =
186 OSSL_FUNC_decoder_get_params(fns);
187 break;
188 case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
189 if (decoder->gettable_params == NULL)
190 decoder->gettable_params =
191 OSSL_FUNC_decoder_gettable_params(fns);
192 break;
193 case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
194 if (decoder->set_ctx_params == NULL)
195 decoder->set_ctx_params =
196 OSSL_FUNC_decoder_set_ctx_params(fns);
197 break;
198 case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
199 if (decoder->settable_ctx_params == NULL)
200 decoder->settable_ctx_params =
201 OSSL_FUNC_decoder_settable_ctx_params(fns);
202 break;
203 case OSSL_FUNC_DECODER_DOES_SELECTION:
204 if (decoder->does_selection == NULL)
205 decoder->does_selection =
206 OSSL_FUNC_decoder_does_selection(fns);
207 break;
208 case OSSL_FUNC_DECODER_DECODE:
209 if (decoder->decode == NULL)
210 decoder->decode = OSSL_FUNC_decoder_decode(fns);
211 break;
212 case OSSL_FUNC_DECODER_EXPORT_OBJECT:
213 if (decoder->export_object == NULL)
214 decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
215 break;
216 }
217 }
218 /*
219 * Try to check that the method is sensible.
220 * If you have a constructor, you must have a destructor and vice versa.
221 * You must have at least one of the encoding driver functions.
222 */
223 if (!((decoder->newctx == NULL && decoder->freectx == NULL)
224 || (decoder->newctx != NULL && decoder->freectx != NULL))
225 || decoder->decode == NULL) {
226 OSSL_DECODER_free(decoder);
227 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
228 return NULL;
229 }
230
231 if (prov != NULL && !ossl_provider_up_ref(prov)) {
232 OSSL_DECODER_free(decoder);
233 return NULL;
234 }
235
236 decoder->base.prov = prov;
237 return decoder;
238 }
239
240
241 /*
242 * The core fetching functionality passes the names of the implementation.
243 * This function is responsible to getting an identity number for them,
244 * then call ossl_decoder_from_dispatch() with that identity number.
245 */
246 static void *construct_decoder(const OSSL_ALGORITHM *algodef,
247 OSSL_PROVIDER *prov, void *data)
248 {
249 /*
250 * This function is only called if get_decoder_from_store() returned
251 * NULL, so it's safe to say that of all the spots to create a new
252 * namemap entry, this is it. Should the name already exist there, we
253 * know that ossl_namemap_add() will return its corresponding number.
254 */
255 struct decoder_data_st *methdata = data;
256 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
257 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
258 const char *names = algodef->algorithm_names;
259 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
260 void *method = NULL;
261
262 if (id != 0)
263 method = ossl_decoder_from_dispatch(id, algodef, prov);
264
265 /*
266 * Flag to indicate that there was actual construction errors. This
267 * helps inner_evp_generic_fetch() determine what error it should
268 * record on inaccessible algorithms.
269 */
270 if (method == NULL)
271 methdata->flag_construct_error_occured = 1;
272
273 return method;
274 }
275
276 /* Intermediary function to avoid ugly casts, used below */
277 static void destruct_decoder(void *method, void *data)
278 {
279 OSSL_DECODER_free(method);
280 }
281
282 static int up_ref_decoder(void *method)
283 {
284 return OSSL_DECODER_up_ref(method);
285 }
286
287 static void free_decoder(void *method)
288 {
289 OSSL_DECODER_free(method);
290 }
291
292 /* Fetching support. Can fetch by numeric identity or by name */
293 static OSSL_DECODER *inner_ossl_decoder_fetch(OSSL_LIB_CTX *libctx, int id,
294 const char *name,
295 const char *properties)
296 {
297 OSSL_METHOD_STORE *store = get_decoder_store(libctx);
298 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
299 void *method = NULL;
300 int unsupported = 0;
301
302 if (store == NULL || namemap == NULL) {
303 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT);
304 return NULL;
305 }
306
307 /*
308 * If we have been passed neither a name_id or a name, we have an
309 * internal programming error.
310 */
311 if (!ossl_assert(id != 0 || name != NULL)) {
312 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
313 return NULL;
314 }
315
316 if (id == 0)
317 id = ossl_namemap_name2num(namemap, name);
318
319 /*
320 * If we haven't found the name yet, chances are that the algorithm to
321 * be fetched is unsupported.
322 */
323 if (id == 0)
324 unsupported = 1;
325
326 if (id == 0
327 || !ossl_method_store_cache_get(store, id, properties, &method)) {
328 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
329 alloc_tmp_decoder_store,
330 dealloc_tmp_decoder_store,
331 get_decoder_from_store,
332 put_decoder_in_store,
333 construct_decoder,
334 destruct_decoder
335 };
336 struct decoder_data_st mcmdata;
337
338 mcmdata.libctx = libctx;
339 mcmdata.mcm = &mcm;
340 mcmdata.id = id;
341 mcmdata.names = name;
342 mcmdata.propquery = properties;
343 mcmdata.flag_construct_error_occured = 0;
344 if ((method = ossl_method_construct(libctx, OSSL_OP_DECODER,
345 0 /* !force_cache */,
346 &mcm, &mcmdata)) != NULL) {
347 /*
348 * If construction did create a method for us, we know that
349 * there is a correct name_id and meth_id, since those have
350 * already been calculated in get_decoder_from_store() and
351 * put_decoder_in_store() above.
352 */
353 if (id == 0)
354 id = ossl_namemap_name2num(namemap, name);
355 ossl_method_store_cache_set(store, id, properties, method,
356 up_ref_decoder, free_decoder);
357 }
358
359 /*
360 * If we never were in the constructor, the algorithm to be fetched
361 * is unsupported.
362 */
363 unsupported = !mcmdata.flag_construct_error_occured;
364 }
365
366 if (method == NULL) {
367 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
368
369 if (name == NULL)
370 name = ossl_namemap_num2name(namemap, id, 0);
371 ERR_raise_data(ERR_LIB_OSSL_DECODER, code,
372 "%s, Name (%s : %d), Properties (%s)",
373 ossl_lib_ctx_get_descriptor(libctx),
374 name = NULL ? "<null>" : name, id,
375 properties == NULL ? "<null>" : properties);
376 }
377
378 return method;
379 }
380
381 OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
382 const char *properties)
383 {
384 return inner_ossl_decoder_fetch(libctx, 0, name, properties);
385 }
386
387 OSSL_DECODER *ossl_decoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
388 const char *properties)
389 {
390 return inner_ossl_decoder_fetch(libctx, id, NULL, properties);
391 }
392
393 /*
394 * Library of basic method functions
395 */
396
397 const OSSL_PROVIDER *OSSL_DECODER_provider(const OSSL_DECODER *decoder)
398 {
399 if (!ossl_assert(decoder != NULL)) {
400 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
401 return 0;
402 }
403
404 return decoder->base.prov;
405 }
406
407 const char *OSSL_DECODER_properties(const OSSL_DECODER *decoder)
408 {
409 if (!ossl_assert(decoder != NULL)) {
410 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
411 return 0;
412 }
413
414 return decoder->base.propdef;
415 }
416
417 int OSSL_DECODER_number(const OSSL_DECODER *decoder)
418 {
419 if (!ossl_assert(decoder != NULL)) {
420 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
421 return 0;
422 }
423
424 return decoder->base.id;
425 }
426
427 int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
428 {
429 if (decoder->base.prov != NULL) {
430 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
431 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
432
433 return ossl_namemap_name2num(namemap, name) == decoder->base.id;
434 }
435 return 0;
436 }
437
438 struct decoder_do_all_data_st {
439 void (*user_fn)(void *method, void *arg);
440 void *user_arg;
441 };
442
443 static void decoder_do_one(OSSL_PROVIDER *provider,
444 const OSSL_ALGORITHM *algodef,
445 int no_store, void *vdata)
446 {
447 struct decoder_do_all_data_st *data = vdata;
448 OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
449 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
450 const char *names = algodef->algorithm_names;
451 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
452 void *method = NULL;
453
454 if (id != 0)
455 method = ossl_decoder_from_dispatch(id, algodef, provider);
456
457 if (method != NULL) {
458 data->user_fn(method, data->user_arg);
459 OSSL_DECODER_free(method);
460 }
461 }
462
463 void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
464 void (*fn)(OSSL_DECODER *decoder, void *arg),
465 void *arg)
466 {
467 struct decoder_do_all_data_st data;
468
469 data.user_fn = (void (*)(void *, void *))fn;
470 data.user_arg = arg;
471 ossl_algorithm_do_all(libctx, OSSL_OP_DECODER, NULL,
472 NULL, decoder_do_one, NULL,
473 &data);
474 }
475
476 int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
477 void (*fn)(const char *name, void *data),
478 void *data)
479 {
480 if (decoder == NULL)
481 return 0;
482
483 if (decoder->base.prov != NULL) {
484 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
485 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
486
487 return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
488 }
489
490 return 1;
491 }
492
493 const OSSL_PARAM *
494 OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
495 {
496 if (decoder != NULL && decoder->gettable_params != NULL) {
497 void *provctx = ossl_provider_ctx(OSSL_DECODER_provider(decoder));
498
499 return decoder->gettable_params(provctx);
500 }
501 return NULL;
502 }
503
504 int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
505 {
506 if (decoder != NULL && decoder->get_params != NULL)
507 return decoder->get_params(params);
508 return 0;
509 }
510
511 const OSSL_PARAM *
512 OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
513 {
514 if (decoder != NULL && decoder->settable_ctx_params != NULL) {
515 void *provctx = ossl_provider_ctx(OSSL_DECODER_provider(decoder));
516
517 return decoder->settable_ctx_params(provctx);
518 }
519 return NULL;
520 }
521
522 /*
523 * Decoder context support
524 */
525
526 /*
527 * |encoder| value NULL is valid, and signifies that there is no decoder.
528 * This is useful to provide fallback mechanisms.
529 * Functions that want to verify if there is a decoder can do so with
530 * OSSL_DECODER_CTX_get_decoder()
531 */
532 OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
533 {
534 OSSL_DECODER_CTX *ctx;
535
536 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
537 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
538
539 return ctx;
540 }
541
542 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
543 const OSSL_PARAM params[])
544 {
545 int ok = 1;
546 size_t i;
547 size_t l;
548
549 if (!ossl_assert(ctx != NULL)) {
550 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
551 return 0;
552 }
553
554 if (ctx->decoder_insts == NULL)
555 return 1;
556
557 l = OSSL_DECODER_CTX_get_num_decoders(ctx);
558 for (i = 0; i < l; i++) {
559 OSSL_DECODER_INSTANCE *decoder_inst =
560 sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
561 OSSL_DECODER *decoder =
562 OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
563 OSSL_DECODER *decoderctx =
564 OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
565
566 if (decoderctx == NULL || decoder->set_ctx_params == NULL)
567 continue;
568 if (!decoder->set_ctx_params(decoderctx, params))
569 ok = 0;
570 }
571 return ok;
572 }
573
574 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
575 {
576 if (ctx != NULL) {
577 if (ctx->cleanup != NULL)
578 ctx->cleanup(ctx->construct_data);
579 sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
580 ossl_decoder_instance_free);
581 ossl_pw_clear_passphrase_data(&ctx->pwdata);
582 OPENSSL_free(ctx);
583 }
584 }