VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/crypto/encode_decode/decoder_meth.c@ 101211

Last change on this file since 101211 was 101211, checked in by vboxsync, 17 months ago

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette