2 * Copyright 2020-2021 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"
22 * Decoder can have multiple names, separated with colons in a name string
24 #define NAME_SEPARATOR ':'
26 /* Simple method structure constructor and destructor */
27 static OSSL_DECODER *ossl_decoder_new(void)
29 OSSL_DECODER *decoder = NULL;
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);
38 decoder->base.refcnt = 1;
43 int OSSL_DECODER_up_ref(OSSL_DECODER *decoder)
47 CRYPTO_UP_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
51 void OSSL_DECODER_free(OSSL_DECODER *decoder)
58 CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
61 OPENSSL_free(decoder->base.name);
62 ossl_property_free(decoder->base.parsed_propdef);
63 ossl_provider_free(decoder->base.prov);
64 CRYPTO_THREAD_lock_free(decoder->base.lock);
65 OPENSSL_free(decoder);
68 /* Permanent decoder method store, constructor and destructor */
69 static void decoder_store_free(void *vstore)
71 ossl_method_store_free(vstore);
74 static void *decoder_store_new(OSSL_LIB_CTX *ctx)
76 return ossl_method_store_new(ctx);
80 static const OSSL_LIB_CTX_METHOD decoder_store_method = {
81 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
86 /* Data to be passed through ossl_method_construct() */
87 struct decoder_data_st {
89 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
90 int id; /* For get_decoder_from_store() */
91 const char *names; /* For get_decoder_from_store() */
92 const char *propquery; /* For get_decoder_from_store() */
94 unsigned int flag_construct_error_occurred : 1;
98 * Generic routines to fetch / create DECODER methods with
99 * ossl_method_construct()
102 /* Temporary decoder method store, constructor and destructor */
103 static void *alloc_tmp_decoder_store(OSSL_LIB_CTX *ctx)
105 return ossl_method_store_new(ctx);
108 static void dealloc_tmp_decoder_store(void *store)
111 ossl_method_store_free(store);
114 /* Get the permanent decoder store */
115 static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
117 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX,
118 &decoder_store_method);
121 /* Get decoder methods from a store, or put one in */
122 static void *get_decoder_from_store(OSSL_LIB_CTX *libctx, void *store,
125 struct decoder_data_st *methdata = data;
129 if ((id = methdata->id) == 0) {
130 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
132 id = ossl_namemap_name2num(namemap, methdata->names);
136 && (store = get_decoder_store(libctx)) == NULL)
139 if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
144 static int put_decoder_in_store(OSSL_LIB_CTX *libctx, void *store,
145 void *method, const OSSL_PROVIDER *prov,
146 int operation_id, const char *names,
147 const char *propdef, void *unused)
149 OSSL_NAMEMAP *namemap;
152 if ((namemap = ossl_namemap_stored(libctx)) == NULL
153 || (id = ossl_namemap_name2num(namemap, names)) == 0)
156 if (store == NULL && (store = get_decoder_store(libctx)) == NULL)
159 return ossl_method_store_add(store, prov, id, propdef, method,
160 (int (*)(void *))OSSL_DECODER_up_ref,
161 (void (*)(void *))OSSL_DECODER_free);
164 /* Create and populate a decoder method */
165 void *ossl_decoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
168 OSSL_DECODER *decoder = NULL;
169 const OSSL_DISPATCH *fns = algodef->implementation;
170 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
172 if ((decoder = ossl_decoder_new()) == NULL)
174 decoder->base.id = id;
175 if ((decoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
176 OSSL_DECODER_free(decoder);
179 decoder->base.propdef = algodef->property_definition;
180 decoder->base.description = algodef->algorithm_description;
181 decoder->base.parsed_propdef
182 = ossl_parse_property(libctx, algodef->property_definition);
184 for (; fns->function_id != 0; fns++) {
185 switch (fns->function_id) {
186 case OSSL_FUNC_DECODER_NEWCTX:
187 if (decoder->newctx == NULL)
188 decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
190 case OSSL_FUNC_DECODER_FREECTX:
191 if (decoder->freectx == NULL)
192 decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
194 case OSSL_FUNC_DECODER_GET_PARAMS:
195 if (decoder->get_params == NULL)
196 decoder->get_params =
197 OSSL_FUNC_decoder_get_params(fns);
199 case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
200 if (decoder->gettable_params == NULL)
201 decoder->gettable_params =
202 OSSL_FUNC_decoder_gettable_params(fns);
204 case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
205 if (decoder->set_ctx_params == NULL)
206 decoder->set_ctx_params =
207 OSSL_FUNC_decoder_set_ctx_params(fns);
209 case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
210 if (decoder->settable_ctx_params == NULL)
211 decoder->settable_ctx_params =
212 OSSL_FUNC_decoder_settable_ctx_params(fns);
214 case OSSL_FUNC_DECODER_DOES_SELECTION:
215 if (decoder->does_selection == NULL)
216 decoder->does_selection =
217 OSSL_FUNC_decoder_does_selection(fns);
219 case OSSL_FUNC_DECODER_DECODE:
220 if (decoder->decode == NULL)
221 decoder->decode = OSSL_FUNC_decoder_decode(fns);
223 case OSSL_FUNC_DECODER_EXPORT_OBJECT:
224 if (decoder->export_object == NULL)
225 decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
230 * Try to check that the method is sensible.
231 * If you have a constructor, you must have a destructor and vice versa.
232 * You must have at least one of the encoding driver functions.
234 if (!((decoder->newctx == NULL && decoder->freectx == NULL)
235 || (decoder->newctx != NULL && decoder->freectx != NULL))
236 || decoder->decode == NULL) {
237 OSSL_DECODER_free(decoder);
238 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
242 if (prov != NULL && !ossl_provider_up_ref(prov)) {
243 OSSL_DECODER_free(decoder);
247 decoder->base.prov = prov;
253 * The core fetching functionality passes the names of the implementation.
254 * This function is responsible to getting an identity number for them,
255 * then call ossl_decoder_from_algorithm() with that identity number.
257 static void *construct_decoder(const OSSL_ALGORITHM *algodef,
258 OSSL_PROVIDER *prov, void *data)
261 * This function is only called if get_decoder_from_store() returned
262 * NULL, so it's safe to say that of all the spots to create a new
263 * namemap entry, this is it. Should the name already exist there, we
264 * know that ossl_namemap_add() will return its corresponding number.
266 struct decoder_data_st *methdata = data;
267 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
268 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
269 const char *names = algodef->algorithm_names;
270 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
274 method = ossl_decoder_from_algorithm(id, algodef, prov);
277 * Flag to indicate that there was actual construction errors. This
278 * helps inner_evp_generic_fetch() determine what error it should
279 * record on inaccessible algorithms.
282 methdata->flag_construct_error_occurred = 1;
287 /* Intermediary function to avoid ugly casts, used below */
288 static void destruct_decoder(void *method, void *data)
290 OSSL_DECODER_free(method);
293 static int up_ref_decoder(void *method)
295 return OSSL_DECODER_up_ref(method);
298 static void free_decoder(void *method)
300 OSSL_DECODER_free(method);
303 /* Fetching support. Can fetch by numeric identity or by name */
304 static OSSL_DECODER *inner_ossl_decoder_fetch(OSSL_LIB_CTX *libctx, int id,
306 const char *properties)
308 OSSL_METHOD_STORE *store = get_decoder_store(libctx);
309 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
313 if (store == NULL || namemap == NULL) {
314 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT);
319 * If we have been passed neither a name_id or a name, we have an
320 * internal programming error.
322 if (!ossl_assert(id != 0 || name != NULL)) {
323 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
328 id = ossl_namemap_name2num(namemap, name);
331 * If we haven't found the name yet, chances are that the algorithm to
332 * be fetched is unsupported.
338 || !ossl_method_store_cache_get(store, id, properties, &method)) {
339 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
340 alloc_tmp_decoder_store,
341 dealloc_tmp_decoder_store,
342 get_decoder_from_store,
343 put_decoder_in_store,
347 struct decoder_data_st mcmdata;
349 mcmdata.libctx = libctx;
352 mcmdata.names = name;
353 mcmdata.propquery = properties;
354 mcmdata.flag_construct_error_occurred = 0;
355 if ((method = ossl_method_construct(libctx, OSSL_OP_DECODER,
356 0 /* !force_cache */,
357 &mcm, &mcmdata)) != NULL) {
359 * If construction did create a method for us, we know that
360 * there is a correct name_id and meth_id, since those have
361 * already been calculated in get_decoder_from_store() and
362 * put_decoder_in_store() above.
365 id = ossl_namemap_name2num(namemap, name);
366 ossl_method_store_cache_set(store, id, properties, method,
367 up_ref_decoder, free_decoder);
371 * If we never were in the constructor, the algorithm to be fetched
374 unsupported = !mcmdata.flag_construct_error_occurred;
377 if (method == NULL) {
378 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
381 name = ossl_namemap_num2name(namemap, id, 0);
382 ERR_raise_data(ERR_LIB_OSSL_DECODER, code,
383 "%s, Name (%s : %d), Properties (%s)",
384 ossl_lib_ctx_get_descriptor(libctx),
385 name = NULL ? "<null>" : name, id,
386 properties == NULL ? "<null>" : properties);
392 OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
393 const char *properties)
395 return inner_ossl_decoder_fetch(libctx, 0, name, properties);
398 OSSL_DECODER *ossl_decoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
399 const char *properties)
401 return inner_ossl_decoder_fetch(libctx, id, NULL, properties);
405 * Library of basic method functions
408 const OSSL_PROVIDER *OSSL_DECODER_get0_provider(const OSSL_DECODER *decoder)
410 if (!ossl_assert(decoder != NULL)) {
411 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
415 return decoder->base.prov;
418 const char *OSSL_DECODER_get0_properties(const OSSL_DECODER *decoder)
420 if (!ossl_assert(decoder != NULL)) {
421 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
425 return decoder->base.propdef;
428 const OSSL_PROPERTY_LIST *
429 ossl_decoder_parsed_properties(const OSSL_DECODER *decoder)
431 if (!ossl_assert(decoder != NULL)) {
432 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
436 return decoder->base.parsed_propdef;
439 int ossl_decoder_get_number(const OSSL_DECODER *decoder)
441 if (!ossl_assert(decoder != NULL)) {
442 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
446 return decoder->base.id;
449 const char *OSSL_DECODER_get0_name(const OSSL_DECODER *decoder)
451 return decoder->base.name;
454 const char *OSSL_DECODER_get0_description(const OSSL_DECODER *decoder)
456 return decoder->base.description;
459 int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
461 if (decoder->base.prov != NULL) {
462 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
463 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
465 return ossl_namemap_name2num(namemap, name) == decoder->base.id;
470 struct decoder_do_all_data_st {
471 void (*user_fn)(void *method, void *arg);
475 static void decoder_do_one(OSSL_PROVIDER *provider,
476 const OSSL_ALGORITHM *algodef,
477 int no_store, void *vdata)
479 struct decoder_do_all_data_st *data = vdata;
480 OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
481 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
482 const char *names = algodef->algorithm_names;
483 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
487 method = ossl_decoder_from_algorithm(id, algodef, provider);
489 if (method != NULL) {
490 data->user_fn(method, data->user_arg);
491 OSSL_DECODER_free(method);
495 void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
496 void (*fn)(OSSL_DECODER *decoder, void *arg),
499 struct decoder_do_all_data_st data;
501 data.user_fn = (void (*)(void *, void *))fn;
503 ossl_algorithm_do_all(libctx, OSSL_OP_DECODER, NULL,
504 NULL, decoder_do_one, NULL,
508 int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
509 void (*fn)(const char *name, void *data),
515 if (decoder->base.prov != NULL) {
516 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
517 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
519 return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
526 OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
528 if (decoder != NULL && decoder->gettable_params != NULL) {
529 void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
531 return decoder->gettable_params(provctx);
536 int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
538 if (decoder != NULL && decoder->get_params != NULL)
539 return decoder->get_params(params);
544 OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
546 if (decoder != NULL && decoder->settable_ctx_params != NULL) {
547 void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
549 return decoder->settable_ctx_params(provctx);
555 * Decoder context support
559 * |encoder| value NULL is valid, and signifies that there is no decoder.
560 * This is useful to provide fallback mechanisms.
561 * Functions that want to verify if there is a decoder can do so with
562 * OSSL_DECODER_CTX_get_decoder()
564 OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
566 OSSL_DECODER_CTX *ctx;
568 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
569 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
574 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
575 const OSSL_PARAM params[])
581 if (!ossl_assert(ctx != NULL)) {
582 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
586 if (ctx->decoder_insts == NULL)
589 l = OSSL_DECODER_CTX_get_num_decoders(ctx);
590 for (i = 0; i < l; i++) {
591 OSSL_DECODER_INSTANCE *decoder_inst =
592 sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
593 OSSL_DECODER *decoder =
594 OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
595 OSSL_DECODER *decoderctx =
596 OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
598 if (decoderctx == NULL || decoder->set_ctx_params == NULL)
600 if (!decoder->set_ctx_params(decoderctx, params))
606 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
609 if (ctx->cleanup != NULL)
610 ctx->cleanup(ctx->construct_data);
611 sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
612 ossl_decoder_instance_free);
613 ossl_pw_clear_passphrase_data(&ctx->pwdata);