2 * Copyright 2020-2022 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
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"
23 * Decoder can have multiple names, separated with colons in a name string
25 #define NAME_SEPARATOR ':'
27 /* Simple method structure constructor and destructor */
28 static OSSL_DECODER *ossl_decoder_new(void)
30 OSSL_DECODER *decoder = NULL;
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);
39 decoder->base.refcnt = 1;
44 int OSSL_DECODER_up_ref(OSSL_DECODER *decoder)
48 CRYPTO_UP_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
52 void OSSL_DECODER_free(OSSL_DECODER *decoder)
59 CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
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);
69 /* Data to be passed through ossl_method_construct() */
70 struct decoder_data_st {
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() */
76 OSSL_METHOD_STORE *tmp_store; /* For get_tmp_decoder_store() */
78 unsigned int flag_construct_error_occurred : 1;
82 * Generic routines to fetch / create DECODER methods with
83 * ossl_method_construct()
86 /* Temporary decoder method store, constructor and destructor */
87 static void *get_tmp_decoder_store(void *data)
89 struct decoder_data_st *methdata = data;
91 if (methdata->tmp_store == NULL)
92 methdata->tmp_store = ossl_method_store_new(methdata->libctx);
93 return methdata->tmp_store;
96 static void dealloc_tmp_decoder_store(void *store)
99 ossl_method_store_free(store);
102 /* Get the permanent decoder store */
103 static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
105 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX);
108 /* Get decoder methods from a store, or put one in */
109 static void *get_decoder_from_store(void *store, const OSSL_PROVIDER **prov,
112 struct decoder_data_st *methdata = data;
117 * get_decoder_from_store() is only called to try and get the method
118 * that OSSL_DECODER_fetch() is asking for, and the name or name id are
119 * passed via methdata.
121 if ((id = methdata->id) == 0 && methdata->names != NULL) {
122 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
123 const char *names = methdata->names;
124 const char *q = strchr(names, NAME_SEPARATOR);
125 size_t l = (q == NULL ? strlen(names) : (size_t)(q - names));
129 id = ossl_namemap_name2num_n(namemap, names, l);
136 && (store = get_decoder_store(methdata->libctx)) == NULL)
139 if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method))
144 static int put_decoder_in_store(void *store, void *method,
145 const OSSL_PROVIDER *prov,
146 const char *names, const char *propdef,
149 struct decoder_data_st *methdata = data;
150 OSSL_NAMEMAP *namemap;
155 * put_decoder_in_store() is only called with an OSSL_DECODER method that
156 * was successfully created by construct_decoder() below, which means that
157 * all the names should already be stored in the namemap with the same
158 * numeric identity, so just use the first to get that identity.
161 const char *q = strchr(names, NAME_SEPARATOR);
163 l = (q == NULL ? strlen(names) : (size_t)(q - names));
166 if ((namemap = ossl_namemap_stored(methdata->libctx)) == NULL
167 || (id = ossl_namemap_name2num_n(namemap, names, l)) == 0)
170 if (store == NULL && (store = get_decoder_store(methdata->libctx)) == NULL)
173 return ossl_method_store_add(store, prov, id, propdef, method,
174 (int (*)(void *))OSSL_DECODER_up_ref,
175 (void (*)(void *))OSSL_DECODER_free);
178 /* Create and populate a decoder method */
179 void *ossl_decoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
182 OSSL_DECODER *decoder = NULL;
183 const OSSL_DISPATCH *fns = algodef->implementation;
184 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
186 if ((decoder = ossl_decoder_new()) == NULL)
188 decoder->base.id = id;
189 if ((decoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
190 OSSL_DECODER_free(decoder);
193 decoder->base.algodef = algodef;
194 decoder->base.parsed_propdef
195 = ossl_parse_property(libctx, algodef->property_definition);
197 for (; fns->function_id != 0; fns++) {
198 switch (fns->function_id) {
199 case OSSL_FUNC_DECODER_NEWCTX:
200 if (decoder->newctx == NULL)
201 decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
203 case OSSL_FUNC_DECODER_FREECTX:
204 if (decoder->freectx == NULL)
205 decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
207 case OSSL_FUNC_DECODER_GET_PARAMS:
208 if (decoder->get_params == NULL)
209 decoder->get_params =
210 OSSL_FUNC_decoder_get_params(fns);
212 case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
213 if (decoder->gettable_params == NULL)
214 decoder->gettable_params =
215 OSSL_FUNC_decoder_gettable_params(fns);
217 case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
218 if (decoder->set_ctx_params == NULL)
219 decoder->set_ctx_params =
220 OSSL_FUNC_decoder_set_ctx_params(fns);
222 case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
223 if (decoder->settable_ctx_params == NULL)
224 decoder->settable_ctx_params =
225 OSSL_FUNC_decoder_settable_ctx_params(fns);
227 case OSSL_FUNC_DECODER_DOES_SELECTION:
228 if (decoder->does_selection == NULL)
229 decoder->does_selection =
230 OSSL_FUNC_decoder_does_selection(fns);
232 case OSSL_FUNC_DECODER_DECODE:
233 if (decoder->decode == NULL)
234 decoder->decode = OSSL_FUNC_decoder_decode(fns);
236 case OSSL_FUNC_DECODER_EXPORT_OBJECT:
237 if (decoder->export_object == NULL)
238 decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
243 * Try to check that the method is sensible.
244 * If you have a constructor, you must have a destructor and vice versa.
245 * You must have at least one of the encoding driver functions.
247 if (!((decoder->newctx == NULL && decoder->freectx == NULL)
248 || (decoder->newctx != NULL && decoder->freectx != NULL))
249 || decoder->decode == NULL) {
250 OSSL_DECODER_free(decoder);
251 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
255 if (prov != NULL && !ossl_provider_up_ref(prov)) {
256 OSSL_DECODER_free(decoder);
260 decoder->base.prov = prov;
266 * The core fetching functionality passes the names of the implementation.
267 * This function is responsible to getting an identity number for them,
268 * then call ossl_decoder_from_algorithm() with that identity number.
270 static void *construct_decoder(const OSSL_ALGORITHM *algodef,
271 OSSL_PROVIDER *prov, void *data)
274 * This function is only called if get_decoder_from_store() returned
275 * NULL, so it's safe to say that of all the spots to create a new
276 * namemap entry, this is it. Should the name already exist there, we
277 * know that ossl_namemap_add() will return its corresponding number.
279 struct decoder_data_st *methdata = data;
280 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
281 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
282 const char *names = algodef->algorithm_names;
283 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
287 method = ossl_decoder_from_algorithm(id, algodef, prov);
290 * Flag to indicate that there was actual construction errors. This
291 * helps inner_evp_generic_fetch() determine what error it should
292 * record on inaccessible algorithms.
295 methdata->flag_construct_error_occurred = 1;
300 /* Intermediary function to avoid ugly casts, used below */
301 static void destruct_decoder(void *method, void *data)
303 OSSL_DECODER_free(method);
306 static int up_ref_decoder(void *method)
308 return OSSL_DECODER_up_ref(method);
311 static void free_decoder(void *method)
313 OSSL_DECODER_free(method);
316 /* Fetching support. Can fetch by numeric identity or by name */
317 static OSSL_DECODER *
318 inner_ossl_decoder_fetch(struct decoder_data_st *methdata, int id,
319 const char *name, const char *properties)
321 OSSL_METHOD_STORE *store = get_decoder_store(methdata->libctx);
322 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
323 const char *const propq = properties != NULL ? properties : "";
327 if (store == NULL || namemap == NULL) {
328 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT);
333 * If we have been passed both an id and a name, we have an
334 * internal programming error.
336 if (!ossl_assert(id == 0 || name == NULL)) {
337 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
341 if (id == 0 && name != NULL)
342 id = ossl_namemap_name2num(namemap, name);
345 * If we haven't found the name yet, chances are that the algorithm to
346 * be fetched is unsupported.
352 || !ossl_method_store_cache_get(store, NULL, id, propq, &method)) {
353 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
354 get_tmp_decoder_store,
355 get_decoder_from_store,
356 put_decoder_in_store,
360 OSSL_PROVIDER *prov = NULL;
363 methdata->names = name;
364 methdata->propquery = propq;
365 methdata->flag_construct_error_occurred = 0;
366 if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_DECODER,
367 &prov, 0 /* !force_cache */,
368 &mcm, methdata)) != NULL) {
370 * If construction did create a method for us, we know that
371 * there is a correct name_id and meth_id, since those have
372 * already been calculated in get_decoder_from_store() and
373 * put_decoder_in_store() above.
375 if (id == 0 && name != NULL)
376 id = ossl_namemap_name2num(namemap, name);
378 ossl_method_store_cache_set(store, prov, id, propq, method,
379 up_ref_decoder, free_decoder);
383 * If we never were in the constructor, the algorithm to be fetched
386 unsupported = !methdata->flag_construct_error_occurred;
389 if ((id != 0 || name != NULL) && method == NULL) {
390 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
393 name = ossl_namemap_num2name(namemap, id, 0);
394 ERR_raise_data(ERR_LIB_OSSL_DECODER, code,
395 "%s, Name (%s : %d), Properties (%s)",
396 ossl_lib_ctx_get_descriptor(methdata->libctx),
397 name = NULL ? "<null>" : name, id,
398 properties == NULL ? "<null>" : properties);
404 OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
405 const char *properties)
407 struct decoder_data_st methdata;
410 methdata.libctx = libctx;
411 methdata.tmp_store = NULL;
412 method = inner_ossl_decoder_fetch(&methdata, 0, name, properties);
413 dealloc_tmp_decoder_store(methdata.tmp_store);
417 OSSL_DECODER *ossl_decoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
418 const char *properties)
420 struct decoder_data_st methdata;
423 methdata.libctx = libctx;
424 methdata.tmp_store = NULL;
425 method = inner_ossl_decoder_fetch(&methdata, id, NULL, properties);
426 dealloc_tmp_decoder_store(methdata.tmp_store);
431 * Library of basic method functions
434 const OSSL_PROVIDER *OSSL_DECODER_get0_provider(const OSSL_DECODER *decoder)
436 if (!ossl_assert(decoder != NULL)) {
437 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
441 return decoder->base.prov;
444 const char *OSSL_DECODER_get0_properties(const OSSL_DECODER *decoder)
446 if (!ossl_assert(decoder != NULL)) {
447 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
451 return decoder->base.algodef->property_definition;
454 const OSSL_PROPERTY_LIST *
455 ossl_decoder_parsed_properties(const OSSL_DECODER *decoder)
457 if (!ossl_assert(decoder != NULL)) {
458 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
462 return decoder->base.parsed_propdef;
465 int ossl_decoder_get_number(const OSSL_DECODER *decoder)
467 if (!ossl_assert(decoder != NULL)) {
468 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
472 return decoder->base.id;
475 const char *OSSL_DECODER_get0_name(const OSSL_DECODER *decoder)
477 return decoder->base.name;
480 const char *OSSL_DECODER_get0_description(const OSSL_DECODER *decoder)
482 return decoder->base.algodef->algorithm_description;
485 int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
487 if (decoder->base.prov != NULL) {
488 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
489 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
491 return ossl_namemap_name2num(namemap, name) == decoder->base.id;
496 static int resolve_name(OSSL_DECODER *decoder, const char *name)
498 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
499 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
501 return ossl_namemap_name2num(namemap, name);
504 int ossl_decoder_fast_is_a(OSSL_DECODER *decoder, const char *name, int *id_cache)
509 *id_cache = id = resolve_name(decoder, name);
511 return id > 0 && ossl_decoder_get_number(decoder) == id;
514 struct do_one_data_st {
515 void (*user_fn)(OSSL_DECODER *decoder, void *arg);
519 static void do_one(ossl_unused int id, void *method, void *arg)
521 struct do_one_data_st *data = arg;
523 data->user_fn(method, data->user_arg);
526 void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
527 void (*user_fn)(OSSL_DECODER *decoder,
531 struct decoder_data_st methdata;
532 struct do_one_data_st data;
534 methdata.libctx = libctx;
535 methdata.tmp_store = NULL;
536 (void)inner_ossl_decoder_fetch(&methdata, 0, NULL, NULL /* properties */);
538 data.user_fn = user_fn;
539 data.user_arg = user_arg;
540 if (methdata.tmp_store != NULL)
541 ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
542 ossl_method_store_do_all(get_decoder_store(libctx), &do_one, &data);
543 dealloc_tmp_decoder_store(methdata.tmp_store);
546 int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
547 void (*fn)(const char *name, void *data),
553 if (decoder->base.prov != NULL) {
554 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
555 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
557 return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
564 OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
566 if (decoder != NULL && decoder->gettable_params != NULL) {
567 void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
569 return decoder->gettable_params(provctx);
574 int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
576 if (decoder != NULL && decoder->get_params != NULL)
577 return decoder->get_params(params);
582 OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
584 if (decoder != NULL && decoder->settable_ctx_params != NULL) {
585 void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
587 return decoder->settable_ctx_params(provctx);
593 * Decoder context support
597 * |encoder| value NULL is valid, and signifies that there is no decoder.
598 * This is useful to provide fallback mechanisms.
599 * Functions that want to verify if there is a decoder can do so with
600 * OSSL_DECODER_CTX_get_decoder()
602 OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
604 OSSL_DECODER_CTX *ctx;
606 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
607 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
612 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
613 const OSSL_PARAM params[])
619 if (!ossl_assert(ctx != NULL)) {
620 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
624 if (ctx->decoder_insts == NULL)
627 l = OSSL_DECODER_CTX_get_num_decoders(ctx);
628 for (i = 0; i < l; i++) {
629 OSSL_DECODER_INSTANCE *decoder_inst =
630 sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
631 OSSL_DECODER *decoder =
632 OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
633 OSSL_DECODER *decoderctx =
634 OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
636 if (decoderctx == NULL || decoder->set_ctx_params == NULL)
638 if (!decoder->set_ctx_params(decoderctx, params))
644 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
647 if (ctx->cleanup != NULL)
648 ctx->cleanup(ctx->construct_data);
649 sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
650 ossl_decoder_instance_free);
651 ossl_pw_clear_passphrase_data(&ctx->pwdata);