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_provider_free(decoder->base.prov);
63 CRYPTO_THREAD_lock_free(decoder->base.lock);
64 OPENSSL_free(decoder);
67 /* Permanent decoder method store, constructor and destructor */
68 static void decoder_store_free(void *vstore)
70 ossl_method_store_free(vstore);
73 static void *decoder_store_new(OSSL_LIB_CTX *ctx)
75 return ossl_method_store_new(ctx);
79 static const OSSL_LIB_CTX_METHOD decoder_store_method = {
80 OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
85 /* Data to be passed through ossl_method_construct() */
86 struct decoder_data_st {
88 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
89 int id; /* For get_decoder_from_store() */
90 const char *names; /* For get_decoder_from_store() */
91 const char *propquery; /* For get_decoder_from_store() */
93 unsigned int flag_construct_error_occurred : 1;
97 * Generic routines to fetch / create DECODER methods with
98 * ossl_method_construct()
101 /* Temporary decoder method store, constructor and destructor */
102 static void *alloc_tmp_decoder_store(OSSL_LIB_CTX *ctx)
104 return ossl_method_store_new(ctx);
107 static void dealloc_tmp_decoder_store(void *store)
110 ossl_method_store_free(store);
113 /* Get the permanent decoder store */
114 static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
116 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX,
117 &decoder_store_method);
120 /* Get decoder methods from a store, or put one in */
121 static void *get_decoder_from_store(OSSL_LIB_CTX *libctx, void *store,
124 struct decoder_data_st *methdata = data;
128 if ((id = methdata->id) == 0) {
129 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
131 id = ossl_namemap_name2num(namemap, methdata->names);
135 && (store = get_decoder_store(libctx)) == NULL)
138 if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
143 static int put_decoder_in_store(OSSL_LIB_CTX *libctx, void *store,
144 void *method, const OSSL_PROVIDER *prov,
145 int operation_id, const char *names,
146 const char *propdef, void *unused)
148 OSSL_NAMEMAP *namemap;
151 if ((namemap = ossl_namemap_stored(libctx)) == NULL
152 || (id = ossl_namemap_name2num(namemap, names)) == 0)
155 if (store == NULL && (store = get_decoder_store(libctx)) == NULL)
158 return ossl_method_store_add(store, prov, id, propdef, method,
159 (int (*)(void *))OSSL_DECODER_up_ref,
160 (void (*)(void *))OSSL_DECODER_free);
163 /* Create and populate a decoder method */
164 void *ossl_decoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
167 OSSL_DECODER *decoder = NULL;
168 const OSSL_DISPATCH *fns = algodef->implementation;
170 if ((decoder = ossl_decoder_new()) == NULL)
172 decoder->base.id = id;
173 if ((decoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
174 OSSL_DECODER_free(decoder);
177 decoder->base.propdef = algodef->property_definition;
178 decoder->base.description = algodef->algorithm_description;
180 for (; fns->function_id != 0; fns++) {
181 switch (fns->function_id) {
182 case OSSL_FUNC_DECODER_NEWCTX:
183 if (decoder->newctx == NULL)
184 decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
186 case OSSL_FUNC_DECODER_FREECTX:
187 if (decoder->freectx == NULL)
188 decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
190 case OSSL_FUNC_DECODER_GET_PARAMS:
191 if (decoder->get_params == NULL)
192 decoder->get_params =
193 OSSL_FUNC_decoder_get_params(fns);
195 case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
196 if (decoder->gettable_params == NULL)
197 decoder->gettable_params =
198 OSSL_FUNC_decoder_gettable_params(fns);
200 case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
201 if (decoder->set_ctx_params == NULL)
202 decoder->set_ctx_params =
203 OSSL_FUNC_decoder_set_ctx_params(fns);
205 case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
206 if (decoder->settable_ctx_params == NULL)
207 decoder->settable_ctx_params =
208 OSSL_FUNC_decoder_settable_ctx_params(fns);
210 case OSSL_FUNC_DECODER_DOES_SELECTION:
211 if (decoder->does_selection == NULL)
212 decoder->does_selection =
213 OSSL_FUNC_decoder_does_selection(fns);
215 case OSSL_FUNC_DECODER_DECODE:
216 if (decoder->decode == NULL)
217 decoder->decode = OSSL_FUNC_decoder_decode(fns);
219 case OSSL_FUNC_DECODER_EXPORT_OBJECT:
220 if (decoder->export_object == NULL)
221 decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
226 * Try to check that the method is sensible.
227 * If you have a constructor, you must have a destructor and vice versa.
228 * You must have at least one of the encoding driver functions.
230 if (!((decoder->newctx == NULL && decoder->freectx == NULL)
231 || (decoder->newctx != NULL && decoder->freectx != NULL))
232 || decoder->decode == NULL) {
233 OSSL_DECODER_free(decoder);
234 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
238 if (prov != NULL && !ossl_provider_up_ref(prov)) {
239 OSSL_DECODER_free(decoder);
243 decoder->base.prov = prov;
249 * The core fetching functionality passes the names of the implementation.
250 * This function is responsible to getting an identity number for them,
251 * then call ossl_decoder_from_algorithm() with that identity number.
253 static void *construct_decoder(const OSSL_ALGORITHM *algodef,
254 OSSL_PROVIDER *prov, void *data)
257 * This function is only called if get_decoder_from_store() returned
258 * NULL, so it's safe to say that of all the spots to create a new
259 * namemap entry, this is it. Should the name already exist there, we
260 * know that ossl_namemap_add() will return its corresponding number.
262 struct decoder_data_st *methdata = data;
263 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
264 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
265 const char *names = algodef->algorithm_names;
266 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
270 method = ossl_decoder_from_algorithm(id, algodef, prov);
273 * Flag to indicate that there was actual construction errors. This
274 * helps inner_evp_generic_fetch() determine what error it should
275 * record on inaccessible algorithms.
278 methdata->flag_construct_error_occurred = 1;
283 /* Intermediary function to avoid ugly casts, used below */
284 static void destruct_decoder(void *method, void *data)
286 OSSL_DECODER_free(method);
289 static int up_ref_decoder(void *method)
291 return OSSL_DECODER_up_ref(method);
294 static void free_decoder(void *method)
296 OSSL_DECODER_free(method);
299 /* Fetching support. Can fetch by numeric identity or by name */
300 static OSSL_DECODER *inner_ossl_decoder_fetch(OSSL_LIB_CTX *libctx, int id,
302 const char *properties)
304 OSSL_METHOD_STORE *store = get_decoder_store(libctx);
305 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
309 if (store == NULL || namemap == NULL) {
310 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT);
315 * If we have been passed neither a name_id or a name, we have an
316 * internal programming error.
318 if (!ossl_assert(id != 0 || name != NULL)) {
319 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
324 id = ossl_namemap_name2num(namemap, name);
327 * If we haven't found the name yet, chances are that the algorithm to
328 * be fetched is unsupported.
334 || !ossl_method_store_cache_get(store, id, properties, &method)) {
335 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
336 alloc_tmp_decoder_store,
337 dealloc_tmp_decoder_store,
338 get_decoder_from_store,
339 put_decoder_in_store,
343 struct decoder_data_st mcmdata;
345 mcmdata.libctx = libctx;
348 mcmdata.names = name;
349 mcmdata.propquery = properties;
350 mcmdata.flag_construct_error_occurred = 0;
351 if ((method = ossl_method_construct(libctx, OSSL_OP_DECODER,
352 0 /* !force_cache */,
353 &mcm, &mcmdata)) != NULL) {
355 * If construction did create a method for us, we know that
356 * there is a correct name_id and meth_id, since those have
357 * already been calculated in get_decoder_from_store() and
358 * put_decoder_in_store() above.
361 id = ossl_namemap_name2num(namemap, name);
362 ossl_method_store_cache_set(store, id, properties, method,
363 up_ref_decoder, free_decoder);
367 * If we never were in the constructor, the algorithm to be fetched
370 unsupported = !mcmdata.flag_construct_error_occurred;
373 if (method == NULL) {
374 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
377 name = ossl_namemap_num2name(namemap, id, 0);
378 ERR_raise_data(ERR_LIB_OSSL_DECODER, code,
379 "%s, Name (%s : %d), Properties (%s)",
380 ossl_lib_ctx_get_descriptor(libctx),
381 name = NULL ? "<null>" : name, id,
382 properties == NULL ? "<null>" : properties);
388 OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
389 const char *properties)
391 return inner_ossl_decoder_fetch(libctx, 0, name, properties);
394 OSSL_DECODER *ossl_decoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
395 const char *properties)
397 return inner_ossl_decoder_fetch(libctx, id, NULL, properties);
401 * Library of basic method functions
404 const OSSL_PROVIDER *OSSL_DECODER_get0_provider(const OSSL_DECODER *decoder)
406 if (!ossl_assert(decoder != NULL)) {
407 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
411 return decoder->base.prov;
414 const char *OSSL_DECODER_get0_properties(const OSSL_DECODER *decoder)
416 if (!ossl_assert(decoder != NULL)) {
417 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
421 return decoder->base.propdef;
424 int ossl_decoder_get_number(const OSSL_DECODER *decoder)
426 if (!ossl_assert(decoder != NULL)) {
427 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
431 return decoder->base.id;
434 const char *OSSL_DECODER_get0_name(const OSSL_DECODER *decoder)
436 return decoder->base.name;
439 const char *OSSL_DECODER_get0_description(const OSSL_DECODER *decoder)
441 return decoder->base.description;
444 int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
446 if (decoder->base.prov != NULL) {
447 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
448 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
450 return ossl_namemap_name2num(namemap, name) == decoder->base.id;
455 struct decoder_do_all_data_st {
456 void (*user_fn)(void *method, void *arg);
460 static void decoder_do_one(OSSL_PROVIDER *provider,
461 const OSSL_ALGORITHM *algodef,
462 int no_store, void *vdata)
464 struct decoder_do_all_data_st *data = vdata;
465 OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
466 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
467 const char *names = algodef->algorithm_names;
468 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
472 method = ossl_decoder_from_algorithm(id, algodef, provider);
474 if (method != NULL) {
475 data->user_fn(method, data->user_arg);
476 OSSL_DECODER_free(method);
480 void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
481 void (*fn)(OSSL_DECODER *decoder, void *arg),
484 struct decoder_do_all_data_st data;
486 data.user_fn = (void (*)(void *, void *))fn;
488 ossl_algorithm_do_all(libctx, OSSL_OP_DECODER, NULL,
489 NULL, decoder_do_one, NULL,
493 int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
494 void (*fn)(const char *name, void *data),
500 if (decoder->base.prov != NULL) {
501 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
502 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
504 return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
511 OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
513 if (decoder != NULL && decoder->gettable_params != NULL) {
514 void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
516 return decoder->gettable_params(provctx);
521 int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
523 if (decoder != NULL && decoder->get_params != NULL)
524 return decoder->get_params(params);
529 OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
531 if (decoder != NULL && decoder->settable_ctx_params != NULL) {
532 void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
534 return decoder->settable_ctx_params(provctx);
540 * Decoder context support
544 * |encoder| value NULL is valid, and signifies that there is no decoder.
545 * This is useful to provide fallback mechanisms.
546 * Functions that want to verify if there is a decoder can do so with
547 * OSSL_DECODER_CTX_get_decoder()
549 OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
551 OSSL_DECODER_CTX *ctx;
553 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
554 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
559 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
560 const OSSL_PARAM params[])
566 if (!ossl_assert(ctx != NULL)) {
567 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
571 if (ctx->decoder_insts == NULL)
574 l = OSSL_DECODER_CTX_get_num_decoders(ctx);
575 for (i = 0; i < l; i++) {
576 OSSL_DECODER_INSTANCE *decoder_inst =
577 sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
578 OSSL_DECODER *decoder =
579 OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
580 OSSL_DECODER *decoderctx =
581 OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
583 if (decoderctx == NULL || decoder->set_ctx_params == NULL)
585 if (!decoder->set_ctx_params(decoderctx, params))
591 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
594 if (ctx->cleanup != NULL)
595 ctx->cleanup(ctx->construct_data);
596 sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
597 ossl_decoder_instance_free);
598 ossl_pw_clear_passphrase_data(&ctx->pwdata);