2 * Copyright 2020 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 ossl_provider_free(decoder->base.prov);
62 CRYPTO_THREAD_lock_free(decoder->base.lock);
63 OPENSSL_free(decoder);
66 /* Permanent decoder method store, constructor and destructor */
67 static void decoder_store_free(void *vstore)
69 ossl_method_store_free(vstore);
72 static void *decoder_store_new(OSSL_LIB_CTX *ctx)
74 return ossl_method_store_new(ctx);
78 static const OSSL_LIB_CTX_METHOD decoder_store_method = {
83 /* Data to be passed through ossl_method_construct() */
84 struct decoder_data_st {
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() */
93 * Generic routines to fetch / create DECODER methods with
94 * ossl_method_construct()
97 /* Temporary decoder method store, constructor and destructor */
98 static void *alloc_tmp_decoder_store(OSSL_LIB_CTX *ctx)
100 return ossl_method_store_new(ctx);
103 static void dealloc_tmp_decoder_store(void *store)
106 ossl_method_store_free(store);
109 /* Get the permanent decoder store */
110 static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
112 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX,
113 &decoder_store_method);
116 /* Get decoder methods from a store, or put one in */
117 static void *get_decoder_from_store(OSSL_LIB_CTX *libctx, void *store,
120 struct decoder_data_st *methdata = data;
124 if ((id = methdata->id) == 0) {
125 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
127 id = ossl_namemap_name2num(namemap, methdata->names);
131 && (store = get_decoder_store(libctx)) == NULL)
134 if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
139 static int put_decoder_in_store(OSSL_LIB_CTX *libctx, void *store,
140 void *method, const OSSL_PROVIDER *prov,
141 int operation_id, const char *names,
142 const char *propdef, void *unused)
144 OSSL_NAMEMAP *namemap;
147 if ((namemap = ossl_namemap_stored(libctx)) == NULL
148 || (id = ossl_namemap_name2num(namemap, names)) == 0)
151 if (store == NULL && (store = get_decoder_store(libctx)) == NULL)
154 return ossl_method_store_add(store, prov, id, propdef, method,
155 (int (*)(void *))OSSL_DECODER_up_ref,
156 (void (*)(void *))OSSL_DECODER_free);
159 /* Create and populate a decoder method */
160 void *ossl_decoder_from_dispatch(int id, const OSSL_ALGORITHM *algodef,
163 OSSL_DECODER *decoder = NULL;
164 const OSSL_DISPATCH *fns = algodef->implementation;
166 if ((decoder = ossl_decoder_new()) == NULL)
168 decoder->base.id = id;
169 decoder->base.propdef = algodef->property_definition;
171 for (; fns->function_id != 0; fns++) {
172 switch (fns->function_id) {
173 case OSSL_FUNC_DECODER_NEWCTX:
174 if (decoder->newctx == NULL)
175 decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
177 case OSSL_FUNC_DECODER_FREECTX:
178 if (decoder->freectx == NULL)
179 decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
181 case OSSL_FUNC_DECODER_GET_PARAMS:
182 if (decoder->get_params == NULL)
183 decoder->get_params =
184 OSSL_FUNC_decoder_get_params(fns);
186 case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
187 if (decoder->gettable_params == NULL)
188 decoder->gettable_params =
189 OSSL_FUNC_decoder_gettable_params(fns);
191 case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
192 if (decoder->set_ctx_params == NULL)
193 decoder->set_ctx_params =
194 OSSL_FUNC_decoder_set_ctx_params(fns);
196 case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
197 if (decoder->settable_ctx_params == NULL)
198 decoder->settable_ctx_params =
199 OSSL_FUNC_decoder_settable_ctx_params(fns);
201 case OSSL_FUNC_DECODER_DECODE:
202 if (decoder->decode == NULL)
203 decoder->decode = OSSL_FUNC_decoder_decode(fns);
205 case OSSL_FUNC_DECODER_EXPORT_OBJECT:
206 if (decoder->export_object == NULL)
207 decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
212 * Try to check that the method is sensible.
213 * If you have a constructor, you must have a destructor and vice versa.
214 * You must have at least one of the encoding driver functions.
216 if (!((decoder->newctx == NULL && decoder->freectx == NULL)
217 || (decoder->newctx != NULL && decoder->freectx != NULL))
218 || decoder->decode == NULL) {
219 OSSL_DECODER_free(decoder);
220 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
224 if (prov != NULL && !ossl_provider_up_ref(prov)) {
225 OSSL_DECODER_free(decoder);
229 decoder->base.prov = prov;
235 * The core fetching functionality passes the names of the implementation.
236 * This function is responsible to getting an identity number for them,
237 * then call ossl_decoder_from_dispatch() with that identity number.
239 static void *construct_decoder(const OSSL_ALGORITHM *algodef,
240 OSSL_PROVIDER *prov, void *unused)
243 * This function is only called if get_decoder_from_store() returned
244 * NULL, so it's safe to say that of all the spots to create a new
245 * namemap entry, this is it. Should the name already exist there, we
246 * know that ossl_namemap_add() will return its corresponding number.
248 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
249 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
250 const char *names = algodef->algorithm_names;
251 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
255 method = ossl_decoder_from_dispatch(id, algodef, prov);
260 /* Intermediary function to avoid ugly casts, used below */
261 static void destruct_decoder(void *method, void *data)
263 OSSL_DECODER_free(method);
266 static int up_ref_decoder(void *method)
268 return OSSL_DECODER_up_ref(method);
271 static void free_decoder(void *method)
273 OSSL_DECODER_free(method);
276 /* Fetching support. Can fetch by numeric identity or by name */
277 static OSSL_DECODER *inner_ossl_decoder_fetch(OSSL_LIB_CTX *libctx, int id,
279 const char *properties)
281 OSSL_METHOD_STORE *store = get_decoder_store(libctx);
282 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
285 if (store == NULL || namemap == NULL)
289 * If we have been passed neither a name_id or a name, we have an
290 * internal programming error.
292 if (!ossl_assert(id != 0 || name != NULL))
296 id = ossl_namemap_name2num(namemap, name);
299 || !ossl_method_store_cache_get(store, id, properties, &method)) {
300 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
301 alloc_tmp_decoder_store,
302 dealloc_tmp_decoder_store,
303 get_decoder_from_store,
304 put_decoder_in_store,
308 struct decoder_data_st mcmdata;
310 mcmdata.libctx = libctx;
313 mcmdata.names = name;
314 mcmdata.propquery = properties;
315 if ((method = ossl_method_construct(libctx, OSSL_OP_DECODER,
316 0 /* !force_cache */,
317 &mcm, &mcmdata)) != NULL) {
319 * If construction did create a method for us, we know that
320 * there is a correct name_id and meth_id, since those have
321 * already been calculated in get_decoder_from_store() and
322 * put_decoder_in_store() above.
325 id = ossl_namemap_name2num(namemap, name);
326 ossl_method_store_cache_set(store, id, properties, method,
327 up_ref_decoder, free_decoder);
334 OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
335 const char *properties)
337 return inner_ossl_decoder_fetch(libctx, 0, name, properties);
340 OSSL_DECODER *ossl_decoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
341 const char *properties)
343 return inner_ossl_decoder_fetch(libctx, id, NULL, properties);
347 * Library of basic method functions
350 const OSSL_PROVIDER *OSSL_DECODER_provider(const OSSL_DECODER *decoder)
352 if (!ossl_assert(decoder != NULL)) {
353 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
357 return decoder->base.prov;
360 const char *OSSL_DECODER_properties(const OSSL_DECODER *decoder)
362 if (!ossl_assert(decoder != NULL)) {
363 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
367 return decoder->base.propdef;
370 int OSSL_DECODER_number(const OSSL_DECODER *decoder)
372 if (!ossl_assert(decoder != NULL)) {
373 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
377 return decoder->base.id;
380 int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
382 if (decoder->base.prov != NULL) {
383 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
384 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
386 return ossl_namemap_name2num(namemap, name) == decoder->base.id;
391 struct decoder_do_all_data_st {
392 void (*user_fn)(void *method, void *arg);
396 static void decoder_do_one(OSSL_PROVIDER *provider,
397 const OSSL_ALGORITHM *algodef,
398 int no_store, void *vdata)
400 struct decoder_do_all_data_st *data = vdata;
401 OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
402 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
403 const char *names = algodef->algorithm_names;
404 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
408 method = ossl_decoder_from_dispatch(id, algodef, provider);
410 if (method != NULL) {
411 data->user_fn(method, data->user_arg);
412 OSSL_DECODER_free(method);
416 void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
417 void (*fn)(OSSL_DECODER *decoder, void *arg),
420 struct decoder_do_all_data_st data;
422 data.user_fn = (void (*)(void *, void *))fn;
424 ossl_algorithm_do_all(libctx, OSSL_OP_DECODER, NULL,
425 NULL, decoder_do_one, NULL,
429 void OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
430 void (*fn)(const char *name, void *data),
436 if (decoder->base.prov != NULL) {
437 OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
438 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
440 ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
445 OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
447 if (decoder != NULL && decoder->gettable_params != NULL) {
448 void *provctx = ossl_provider_ctx(OSSL_DECODER_provider(decoder));
450 return decoder->gettable_params(provctx);
455 int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
457 if (decoder != NULL && decoder->get_params != NULL)
458 return decoder->get_params(params);
463 OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
465 if (decoder != NULL && decoder->settable_ctx_params != NULL) {
466 void *provctx = ossl_provider_ctx(OSSL_DECODER_provider(decoder));
468 return decoder->settable_ctx_params(provctx);
474 * Decoder context support
478 * |encoder| value NULL is valid, and signifies that there is no decoder.
479 * This is useful to provide fallback mechanisms.
480 * Functions that want to verify if there is a decoder can do so with
481 * OSSL_DECODER_CTX_get_decoder()
483 OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
485 OSSL_DECODER_CTX *ctx;
487 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
488 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
493 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
494 const OSSL_PARAM params[])
499 if (!ossl_assert(ctx != NULL)) {
500 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
504 if (ctx->decoder_insts == NULL)
507 l = OSSL_DECODER_CTX_get_num_decoders(ctx);
508 for (i = 0; i < l; i++) {
509 OSSL_DECODER_INSTANCE *decoder_inst =
510 sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
511 OSSL_DECODER *decoder =
512 OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
513 OSSL_DECODER *decoderctx =
514 OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
516 if (decoderctx == NULL || decoder->set_ctx_params == NULL)
518 if (!decoder->set_ctx_params(decoderctx, params))
524 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
527 if (ctx->cleanup != NULL)
528 ctx->cleanup(ctx->construct_data);
529 sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
530 ossl_decoder_instance_free);
531 ossl_pw_clear_passphrase_data(&ctx->pwdata);