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 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() */
91 unsigned int flag_construct_error_occured
: 1;
95 * Generic routines to fetch / create DECODER methods with
96 * ossl_method_construct()
99 /* Temporary decoder method store, constructor and destructor */
100 static void *alloc_tmp_decoder_store(OSSL_LIB_CTX
*ctx
)
102 return ossl_method_store_new(ctx
);
105 static void dealloc_tmp_decoder_store(void *store
)
108 ossl_method_store_free(store
);
111 /* Get the permanent decoder store */
112 static OSSL_METHOD_STORE
*get_decoder_store(OSSL_LIB_CTX
*libctx
)
114 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_DECODER_STORE_INDEX
,
115 &decoder_store_method
);
118 /* Get decoder methods from a store, or put one in */
119 static void *get_decoder_from_store(OSSL_LIB_CTX
*libctx
, void *store
,
122 struct decoder_data_st
*methdata
= data
;
126 if ((id
= methdata
->id
) == 0) {
127 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
129 id
= ossl_namemap_name2num(namemap
, methdata
->names
);
133 && (store
= get_decoder_store(libctx
)) == NULL
)
136 if (!ossl_method_store_fetch(store
, id
, methdata
->propquery
, &method
))
141 static int put_decoder_in_store(OSSL_LIB_CTX
*libctx
, void *store
,
142 void *method
, const OSSL_PROVIDER
*prov
,
143 int operation_id
, const char *names
,
144 const char *propdef
, void *unused
)
146 OSSL_NAMEMAP
*namemap
;
149 if ((namemap
= ossl_namemap_stored(libctx
)) == NULL
150 || (id
= ossl_namemap_name2num(namemap
, names
)) == 0)
153 if (store
== NULL
&& (store
= get_decoder_store(libctx
)) == NULL
)
156 return ossl_method_store_add(store
, prov
, id
, propdef
, method
,
157 (int (*)(void *))OSSL_DECODER_up_ref
,
158 (void (*)(void *))OSSL_DECODER_free
);
161 /* Create and populate a decoder method */
162 void *ossl_decoder_from_dispatch(int id
, const OSSL_ALGORITHM
*algodef
,
165 OSSL_DECODER
*decoder
= NULL
;
166 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
168 if ((decoder
= ossl_decoder_new()) == NULL
)
170 decoder
->base
.id
= id
;
171 decoder
->base
.propdef
= algodef
->property_definition
;
173 for (; fns
->function_id
!= 0; fns
++) {
174 switch (fns
->function_id
) {
175 case OSSL_FUNC_DECODER_NEWCTX
:
176 if (decoder
->newctx
== NULL
)
177 decoder
->newctx
= OSSL_FUNC_decoder_newctx(fns
);
179 case OSSL_FUNC_DECODER_FREECTX
:
180 if (decoder
->freectx
== NULL
)
181 decoder
->freectx
= OSSL_FUNC_decoder_freectx(fns
);
183 case OSSL_FUNC_DECODER_GET_PARAMS
:
184 if (decoder
->get_params
== NULL
)
185 decoder
->get_params
=
186 OSSL_FUNC_decoder_get_params(fns
);
188 case OSSL_FUNC_DECODER_GETTABLE_PARAMS
:
189 if (decoder
->gettable_params
== NULL
)
190 decoder
->gettable_params
=
191 OSSL_FUNC_decoder_gettable_params(fns
);
193 case OSSL_FUNC_DECODER_SET_CTX_PARAMS
:
194 if (decoder
->set_ctx_params
== NULL
)
195 decoder
->set_ctx_params
=
196 OSSL_FUNC_decoder_set_ctx_params(fns
);
198 case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS
:
199 if (decoder
->settable_ctx_params
== NULL
)
200 decoder
->settable_ctx_params
=
201 OSSL_FUNC_decoder_settable_ctx_params(fns
);
203 case OSSL_FUNC_DECODER_DOES_SELECTION
:
204 if (decoder
->does_selection
== NULL
)
205 decoder
->does_selection
=
206 OSSL_FUNC_decoder_does_selection(fns
);
208 case OSSL_FUNC_DECODER_DECODE
:
209 if (decoder
->decode
== NULL
)
210 decoder
->decode
= OSSL_FUNC_decoder_decode(fns
);
212 case OSSL_FUNC_DECODER_EXPORT_OBJECT
:
213 if (decoder
->export_object
== NULL
)
214 decoder
->export_object
= OSSL_FUNC_decoder_export_object(fns
);
219 * Try to check that the method is sensible.
220 * If you have a constructor, you must have a destructor and vice versa.
221 * You must have at least one of the encoding driver functions.
223 if (!((decoder
->newctx
== NULL
&& decoder
->freectx
== NULL
)
224 || (decoder
->newctx
!= NULL
&& decoder
->freectx
!= NULL
))
225 || decoder
->decode
== NULL
) {
226 OSSL_DECODER_free(decoder
);
227 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_INVALID_PROVIDER_FUNCTIONS
);
231 if (prov
!= NULL
&& !ossl_provider_up_ref(prov
)) {
232 OSSL_DECODER_free(decoder
);
236 decoder
->base
.prov
= prov
;
242 * The core fetching functionality passes the names of the implementation.
243 * This function is responsible to getting an identity number for them,
244 * then call ossl_decoder_from_dispatch() with that identity number.
246 static void *construct_decoder(const OSSL_ALGORITHM
*algodef
,
247 OSSL_PROVIDER
*prov
, void *data
)
250 * This function is only called if get_decoder_from_store() returned
251 * NULL, so it's safe to say that of all the spots to create a new
252 * namemap entry, this is it. Should the name already exist there, we
253 * know that ossl_namemap_add() will return its corresponding number.
255 struct decoder_data_st
*methdata
= data
;
256 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
257 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
258 const char *names
= algodef
->algorithm_names
;
259 int id
= ossl_namemap_add_names(namemap
, 0, names
, NAME_SEPARATOR
);
263 method
= ossl_decoder_from_dispatch(id
, algodef
, prov
);
266 * Flag to indicate that there was actual construction errors. This
267 * helps inner_evp_generic_fetch() determine what error it should
268 * record on inaccessible algorithms.
271 methdata
->flag_construct_error_occured
= 1;
276 /* Intermediary function to avoid ugly casts, used below */
277 static void destruct_decoder(void *method
, void *data
)
279 OSSL_DECODER_free(method
);
282 static int up_ref_decoder(void *method
)
284 return OSSL_DECODER_up_ref(method
);
287 static void free_decoder(void *method
)
289 OSSL_DECODER_free(method
);
292 /* Fetching support. Can fetch by numeric identity or by name */
293 static OSSL_DECODER
*inner_ossl_decoder_fetch(OSSL_LIB_CTX
*libctx
, int id
,
295 const char *properties
)
297 OSSL_METHOD_STORE
*store
= get_decoder_store(libctx
);
298 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
302 if (store
== NULL
|| namemap
== NULL
) {
303 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_INVALID_ARGUMENT
);
308 * If we have been passed neither a name_id or a name, we have an
309 * internal programming error.
311 if (!ossl_assert(id
!= 0 || name
!= NULL
)) {
312 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_INTERNAL_ERROR
);
317 id
= ossl_namemap_name2num(namemap
, name
);
320 * If we haven't found the name yet, chances are that the algorithm to
321 * be fetched is unsupported.
327 || !ossl_method_store_cache_get(store
, id
, properties
, &method
)) {
328 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
329 alloc_tmp_decoder_store
,
330 dealloc_tmp_decoder_store
,
331 get_decoder_from_store
,
332 put_decoder_in_store
,
336 struct decoder_data_st mcmdata
;
338 mcmdata
.libctx
= libctx
;
341 mcmdata
.names
= name
;
342 mcmdata
.propquery
= properties
;
343 mcmdata
.flag_construct_error_occured
= 0;
344 if ((method
= ossl_method_construct(libctx
, OSSL_OP_DECODER
,
345 0 /* !force_cache */,
346 &mcm
, &mcmdata
)) != NULL
) {
348 * If construction did create a method for us, we know that
349 * there is a correct name_id and meth_id, since those have
350 * already been calculated in get_decoder_from_store() and
351 * put_decoder_in_store() above.
354 id
= ossl_namemap_name2num(namemap
, name
);
355 ossl_method_store_cache_set(store
, id
, properties
, method
,
356 up_ref_decoder
, free_decoder
);
360 * If we never were in the constructor, the algorithm to be fetched
363 unsupported
= !mcmdata
.flag_construct_error_occured
;
366 if (method
== NULL
) {
367 int code
= unsupported
? ERR_R_UNSUPPORTED
: ERR_R_FETCH_FAILED
;
370 name
= ossl_namemap_num2name(namemap
, id
, 0);
371 ERR_raise_data(ERR_LIB_OSSL_DECODER
, code
,
372 "%s, Name (%s : %d), Properties (%s)",
373 ossl_lib_ctx_get_descriptor(libctx
),
374 name
= NULL
? "<null>" : name
, id
,
375 properties
== NULL
? "<null>" : properties
);
381 OSSL_DECODER
*OSSL_DECODER_fetch(OSSL_LIB_CTX
*libctx
, const char *name
,
382 const char *properties
)
384 return inner_ossl_decoder_fetch(libctx
, 0, name
, properties
);
387 OSSL_DECODER
*ossl_decoder_fetch_by_number(OSSL_LIB_CTX
*libctx
, int id
,
388 const char *properties
)
390 return inner_ossl_decoder_fetch(libctx
, id
, NULL
, properties
);
394 * Library of basic method functions
397 const OSSL_PROVIDER
*OSSL_DECODER_provider(const OSSL_DECODER
*decoder
)
399 if (!ossl_assert(decoder
!= NULL
)) {
400 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
404 return decoder
->base
.prov
;
407 const char *OSSL_DECODER_properties(const OSSL_DECODER
*decoder
)
409 if (!ossl_assert(decoder
!= NULL
)) {
410 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
414 return decoder
->base
.propdef
;
417 int OSSL_DECODER_number(const OSSL_DECODER
*decoder
)
419 if (!ossl_assert(decoder
!= NULL
)) {
420 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
424 return decoder
->base
.id
;
427 int OSSL_DECODER_is_a(const OSSL_DECODER
*decoder
, const char *name
)
429 if (decoder
->base
.prov
!= NULL
) {
430 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(decoder
->base
.prov
);
431 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
433 return ossl_namemap_name2num(namemap
, name
) == decoder
->base
.id
;
438 struct decoder_do_all_data_st
{
439 void (*user_fn
)(void *method
, void *arg
);
443 static void decoder_do_one(OSSL_PROVIDER
*provider
,
444 const OSSL_ALGORITHM
*algodef
,
445 int no_store
, void *vdata
)
447 struct decoder_do_all_data_st
*data
= vdata
;
448 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(provider
);
449 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
450 const char *names
= algodef
->algorithm_names
;
451 int id
= ossl_namemap_add_names(namemap
, 0, names
, NAME_SEPARATOR
);
455 method
= ossl_decoder_from_dispatch(id
, algodef
, provider
);
457 if (method
!= NULL
) {
458 data
->user_fn(method
, data
->user_arg
);
459 OSSL_DECODER_free(method
);
463 void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX
*libctx
,
464 void (*fn
)(OSSL_DECODER
*decoder
, void *arg
),
467 struct decoder_do_all_data_st data
;
469 data
.user_fn
= (void (*)(void *, void *))fn
;
471 ossl_algorithm_do_all(libctx
, OSSL_OP_DECODER
, NULL
,
472 NULL
, decoder_do_one
, NULL
,
476 int OSSL_DECODER_names_do_all(const OSSL_DECODER
*decoder
,
477 void (*fn
)(const char *name
, void *data
),
483 if (decoder
->base
.prov
!= NULL
) {
484 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(decoder
->base
.prov
);
485 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
487 return ossl_namemap_doall_names(namemap
, decoder
->base
.id
, fn
, data
);
494 OSSL_DECODER_gettable_params(OSSL_DECODER
*decoder
)
496 if (decoder
!= NULL
&& decoder
->gettable_params
!= NULL
) {
497 void *provctx
= ossl_provider_ctx(OSSL_DECODER_provider(decoder
));
499 return decoder
->gettable_params(provctx
);
504 int OSSL_DECODER_get_params(OSSL_DECODER
*decoder
, OSSL_PARAM params
[])
506 if (decoder
!= NULL
&& decoder
->get_params
!= NULL
)
507 return decoder
->get_params(params
);
512 OSSL_DECODER_settable_ctx_params(OSSL_DECODER
*decoder
)
514 if (decoder
!= NULL
&& decoder
->settable_ctx_params
!= NULL
) {
515 void *provctx
= ossl_provider_ctx(OSSL_DECODER_provider(decoder
));
517 return decoder
->settable_ctx_params(provctx
);
523 * Decoder context support
527 * |encoder| value NULL is valid, and signifies that there is no decoder.
528 * This is useful to provide fallback mechanisms.
529 * Functions that want to verify if there is a decoder can do so with
530 * OSSL_DECODER_CTX_get_decoder()
532 OSSL_DECODER_CTX
*OSSL_DECODER_CTX_new(void)
534 OSSL_DECODER_CTX
*ctx
;
536 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
)
537 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_MALLOC_FAILURE
);
542 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX
*ctx
,
543 const OSSL_PARAM params
[])
549 if (!ossl_assert(ctx
!= NULL
)) {
550 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
554 if (ctx
->decoder_insts
== NULL
)
557 l
= OSSL_DECODER_CTX_get_num_decoders(ctx
);
558 for (i
= 0; i
< l
; i
++) {
559 OSSL_DECODER_INSTANCE
*decoder_inst
=
560 sk_OSSL_DECODER_INSTANCE_value(ctx
->decoder_insts
, i
);
561 OSSL_DECODER
*decoder
=
562 OSSL_DECODER_INSTANCE_get_decoder(decoder_inst
);
563 OSSL_DECODER
*decoderctx
=
564 OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst
);
566 if (decoderctx
== NULL
|| decoder
->set_ctx_params
== NULL
)
568 if (!decoder
->set_ctx_params(decoderctx
, params
))
574 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX
*ctx
)
577 if (ctx
->cleanup
!= NULL
)
578 ctx
->cleanup(ctx
->construct_data
);
579 sk_OSSL_DECODER_INSTANCE_pop_free(ctx
->decoder_insts
,
580 ossl_decoder_instance_free
);
581 ossl_pw_clear_passphrase_data(&ctx
->pwdata
);