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 /* We want decoder_store to be cleaned up before the provider store */
82 OSSL_LIB_CTX_METHOD_PRIORITY_2
,
87 /* Data to be passed through ossl_method_construct() */
88 struct decoder_data_st
{
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 OSSL_METHOD_STORE
*tmp_store
; /* For get_tmp_decoder_store() */
96 unsigned int flag_construct_error_occurred
: 1;
100 * Generic routines to fetch / create DECODER methods with
101 * ossl_method_construct()
104 /* Temporary decoder method store, constructor and destructor */
105 static void *get_tmp_decoder_store(void *data
)
107 struct decoder_data_st
*methdata
= data
;
109 if (methdata
->tmp_store
== NULL
)
110 methdata
->tmp_store
= ossl_method_store_new(methdata
->libctx
);
111 return methdata
->tmp_store
;
114 static void dealloc_tmp_decoder_store(void *store
)
117 ossl_method_store_free(store
);
120 /* Get the permanent decoder store */
121 static OSSL_METHOD_STORE
*get_decoder_store(OSSL_LIB_CTX
*libctx
)
123 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_DECODER_STORE_INDEX
,
124 &decoder_store_method
);
127 /* Get decoder methods from a store, or put one in */
128 static void *get_decoder_from_store(void *store
, const OSSL_PROVIDER
**prov
,
131 struct decoder_data_st
*methdata
= data
;
136 * get_decoder_from_store() is only called to try and get the method
137 * that OSSL_DECODER_fetch() is asking for, and the name or name id are
138 * passed via methdata.
140 if ((id
= methdata
->id
) == 0 && methdata
->names
!= NULL
) {
141 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
142 const char *names
= methdata
->names
;
143 const char *q
= strchr(names
, NAME_SEPARATOR
);
144 size_t l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
148 id
= ossl_namemap_name2num_n(namemap
, names
, l
);
155 && (store
= get_decoder_store(methdata
->libctx
)) == NULL
)
158 if (!ossl_method_store_fetch(store
, id
, methdata
->propquery
, prov
, &method
))
163 static int put_decoder_in_store(void *store
, void *method
,
164 const OSSL_PROVIDER
*prov
,
165 const char *names
, const char *propdef
,
168 struct decoder_data_st
*methdata
= data
;
169 OSSL_NAMEMAP
*namemap
;
174 * put_decoder_in_store() is only called with an OSSL_DECODER method that
175 * was successfully created by construct_decoder() below, which means that
176 * all the names should already be stored in the namemap with the same
177 * numeric identity, so just use the first to get that identity.
180 const char *q
= strchr(names
, NAME_SEPARATOR
);
182 l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
185 if ((namemap
= ossl_namemap_stored(methdata
->libctx
)) == NULL
186 || (id
= ossl_namemap_name2num_n(namemap
, names
, l
)) == 0)
189 if (store
== NULL
&& (store
= get_decoder_store(methdata
->libctx
)) == NULL
)
192 return ossl_method_store_add(store
, prov
, id
, propdef
, method
,
193 (int (*)(void *))OSSL_DECODER_up_ref
,
194 (void (*)(void *))OSSL_DECODER_free
);
197 /* Create and populate a decoder method */
198 void *ossl_decoder_from_algorithm(int id
, const OSSL_ALGORITHM
*algodef
,
201 OSSL_DECODER
*decoder
= NULL
;
202 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
203 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
205 if ((decoder
= ossl_decoder_new()) == NULL
)
207 decoder
->base
.id
= id
;
208 if ((decoder
->base
.name
= ossl_algorithm_get1_first_name(algodef
)) == NULL
) {
209 OSSL_DECODER_free(decoder
);
212 decoder
->base
.algodef
= algodef
;
213 decoder
->base
.parsed_propdef
214 = ossl_parse_property(libctx
, algodef
->property_definition
);
216 for (; fns
->function_id
!= 0; fns
++) {
217 switch (fns
->function_id
) {
218 case OSSL_FUNC_DECODER_NEWCTX
:
219 if (decoder
->newctx
== NULL
)
220 decoder
->newctx
= OSSL_FUNC_decoder_newctx(fns
);
222 case OSSL_FUNC_DECODER_FREECTX
:
223 if (decoder
->freectx
== NULL
)
224 decoder
->freectx
= OSSL_FUNC_decoder_freectx(fns
);
226 case OSSL_FUNC_DECODER_GET_PARAMS
:
227 if (decoder
->get_params
== NULL
)
228 decoder
->get_params
=
229 OSSL_FUNC_decoder_get_params(fns
);
231 case OSSL_FUNC_DECODER_GETTABLE_PARAMS
:
232 if (decoder
->gettable_params
== NULL
)
233 decoder
->gettable_params
=
234 OSSL_FUNC_decoder_gettable_params(fns
);
236 case OSSL_FUNC_DECODER_SET_CTX_PARAMS
:
237 if (decoder
->set_ctx_params
== NULL
)
238 decoder
->set_ctx_params
=
239 OSSL_FUNC_decoder_set_ctx_params(fns
);
241 case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS
:
242 if (decoder
->settable_ctx_params
== NULL
)
243 decoder
->settable_ctx_params
=
244 OSSL_FUNC_decoder_settable_ctx_params(fns
);
246 case OSSL_FUNC_DECODER_DOES_SELECTION
:
247 if (decoder
->does_selection
== NULL
)
248 decoder
->does_selection
=
249 OSSL_FUNC_decoder_does_selection(fns
);
251 case OSSL_FUNC_DECODER_DECODE
:
252 if (decoder
->decode
== NULL
)
253 decoder
->decode
= OSSL_FUNC_decoder_decode(fns
);
255 case OSSL_FUNC_DECODER_EXPORT_OBJECT
:
256 if (decoder
->export_object
== NULL
)
257 decoder
->export_object
= OSSL_FUNC_decoder_export_object(fns
);
262 * Try to check that the method is sensible.
263 * If you have a constructor, you must have a destructor and vice versa.
264 * You must have at least one of the encoding driver functions.
266 if (!((decoder
->newctx
== NULL
&& decoder
->freectx
== NULL
)
267 || (decoder
->newctx
!= NULL
&& decoder
->freectx
!= NULL
))
268 || decoder
->decode
== NULL
) {
269 OSSL_DECODER_free(decoder
);
270 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_INVALID_PROVIDER_FUNCTIONS
);
274 if (prov
!= NULL
&& !ossl_provider_up_ref(prov
)) {
275 OSSL_DECODER_free(decoder
);
279 decoder
->base
.prov
= prov
;
285 * The core fetching functionality passes the names of the implementation.
286 * This function is responsible to getting an identity number for them,
287 * then call ossl_decoder_from_algorithm() with that identity number.
289 static void *construct_decoder(const OSSL_ALGORITHM
*algodef
,
290 OSSL_PROVIDER
*prov
, void *data
)
293 * This function is only called if get_decoder_from_store() returned
294 * NULL, so it's safe to say that of all the spots to create a new
295 * namemap entry, this is it. Should the name already exist there, we
296 * know that ossl_namemap_add() will return its corresponding number.
298 struct decoder_data_st
*methdata
= data
;
299 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
300 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
301 const char *names
= algodef
->algorithm_names
;
302 int id
= ossl_namemap_add_names(namemap
, 0, names
, NAME_SEPARATOR
);
306 method
= ossl_decoder_from_algorithm(id
, algodef
, prov
);
309 * Flag to indicate that there was actual construction errors. This
310 * helps inner_evp_generic_fetch() determine what error it should
311 * record on inaccessible algorithms.
314 methdata
->flag_construct_error_occurred
= 1;
319 /* Intermediary function to avoid ugly casts, used below */
320 static void destruct_decoder(void *method
, void *data
)
322 OSSL_DECODER_free(method
);
325 static int up_ref_decoder(void *method
)
327 return OSSL_DECODER_up_ref(method
);
330 static void free_decoder(void *method
)
332 OSSL_DECODER_free(method
);
335 /* Fetching support. Can fetch by numeric identity or by name */
336 static OSSL_DECODER
*
337 inner_ossl_decoder_fetch(struct decoder_data_st
*methdata
, int id
,
338 const char *name
, const char *properties
)
340 OSSL_METHOD_STORE
*store
= get_decoder_store(methdata
->libctx
);
341 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
345 if (store
== NULL
|| namemap
== NULL
) {
346 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_INVALID_ARGUMENT
);
351 * If we have been passed both an id and a name, we have an
352 * internal programming error.
354 if (!ossl_assert(id
== 0 || name
== NULL
)) {
355 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_INTERNAL_ERROR
);
359 if (id
== 0 && name
!= NULL
)
360 id
= ossl_namemap_name2num(namemap
, name
);
363 * If we haven't found the name yet, chances are that the algorithm to
364 * be fetched is unsupported.
370 || !ossl_method_store_cache_get(store
, NULL
, id
, properties
, &method
)) {
371 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
372 get_tmp_decoder_store
,
373 get_decoder_from_store
,
374 put_decoder_in_store
,
380 methdata
->names
= name
;
381 methdata
->propquery
= properties
;
382 methdata
->flag_construct_error_occurred
= 0;
383 if ((method
= ossl_method_construct(methdata
->libctx
, OSSL_OP_DECODER
,
384 NULL
, 0 /* !force_cache */,
385 &mcm
, methdata
)) != NULL
) {
387 * If construction did create a method for us, we know that
388 * there is a correct name_id and meth_id, since those have
389 * already been calculated in get_decoder_from_store() and
390 * put_decoder_in_store() above.
392 if (id
== 0 && name
!= NULL
)
393 id
= ossl_namemap_name2num(namemap
, name
);
395 ossl_method_store_cache_set(store
, NULL
, id
, properties
, method
,
396 up_ref_decoder
, free_decoder
);
400 * If we never were in the constructor, the algorithm to be fetched
403 unsupported
= !methdata
->flag_construct_error_occurred
;
406 if ((id
!= 0 || name
!= NULL
) && method
== NULL
) {
407 int code
= unsupported
? ERR_R_UNSUPPORTED
: ERR_R_FETCH_FAILED
;
410 name
= ossl_namemap_num2name(namemap
, id
, 0);
411 ERR_raise_data(ERR_LIB_OSSL_DECODER
, code
,
412 "%s, Name (%s : %d), Properties (%s)",
413 ossl_lib_ctx_get_descriptor(methdata
->libctx
),
414 name
= NULL
? "<null>" : name
, id
,
415 properties
== NULL
? "<null>" : properties
);
421 OSSL_DECODER
*OSSL_DECODER_fetch(OSSL_LIB_CTX
*libctx
, const char *name
,
422 const char *properties
)
424 struct decoder_data_st methdata
;
427 methdata
.libctx
= libctx
;
428 methdata
.tmp_store
= NULL
;
429 method
= inner_ossl_decoder_fetch(&methdata
, 0, name
, properties
);
430 dealloc_tmp_decoder_store(methdata
.tmp_store
);
434 OSSL_DECODER
*ossl_decoder_fetch_by_number(OSSL_LIB_CTX
*libctx
, int id
,
435 const char *properties
)
437 struct decoder_data_st methdata
;
440 methdata
.libctx
= libctx
;
441 methdata
.tmp_store
= NULL
;
442 method
= inner_ossl_decoder_fetch(&methdata
, id
, NULL
, properties
);
443 dealloc_tmp_decoder_store(methdata
.tmp_store
);
448 * Library of basic method functions
451 const OSSL_PROVIDER
*OSSL_DECODER_get0_provider(const OSSL_DECODER
*decoder
)
453 if (!ossl_assert(decoder
!= NULL
)) {
454 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
458 return decoder
->base
.prov
;
461 const char *OSSL_DECODER_get0_properties(const OSSL_DECODER
*decoder
)
463 if (!ossl_assert(decoder
!= NULL
)) {
464 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
468 return decoder
->base
.algodef
->property_definition
;
471 const OSSL_PROPERTY_LIST
*
472 ossl_decoder_parsed_properties(const OSSL_DECODER
*decoder
)
474 if (!ossl_assert(decoder
!= NULL
)) {
475 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
479 return decoder
->base
.parsed_propdef
;
482 int ossl_decoder_get_number(const OSSL_DECODER
*decoder
)
484 if (!ossl_assert(decoder
!= NULL
)) {
485 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
489 return decoder
->base
.id
;
492 const char *OSSL_DECODER_get0_name(const OSSL_DECODER
*decoder
)
494 return decoder
->base
.name
;
497 const char *OSSL_DECODER_get0_description(const OSSL_DECODER
*decoder
)
499 return decoder
->base
.algodef
->algorithm_description
;
502 int OSSL_DECODER_is_a(const OSSL_DECODER
*decoder
, const char *name
)
504 if (decoder
->base
.prov
!= NULL
) {
505 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(decoder
->base
.prov
);
506 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
508 return ossl_namemap_name2num(namemap
, name
) == decoder
->base
.id
;
513 struct do_one_data_st
{
514 void (*user_fn
)(OSSL_DECODER
*decoder
, void *arg
);
518 static void do_one(ossl_unused
int id
, void *method
, void *arg
)
520 struct do_one_data_st
*data
= arg
;
522 data
->user_fn(method
, data
->user_arg
);
525 void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX
*libctx
,
526 void (*user_fn
)(OSSL_DECODER
*decoder
,
530 struct decoder_data_st methdata
;
531 struct do_one_data_st data
;
533 methdata
.libctx
= libctx
;
534 methdata
.tmp_store
= NULL
;
535 (void)inner_ossl_decoder_fetch(&methdata
, 0, NULL
, NULL
/* properties */);
537 data
.user_fn
= user_fn
;
538 data
.user_arg
= user_arg
;
539 if (methdata
.tmp_store
!= NULL
)
540 ossl_method_store_do_all(methdata
.tmp_store
, &do_one
, &data
);
541 ossl_method_store_do_all(get_decoder_store(libctx
), &do_one
, &data
);
542 dealloc_tmp_decoder_store(methdata
.tmp_store
);
545 int OSSL_DECODER_names_do_all(const OSSL_DECODER
*decoder
,
546 void (*fn
)(const char *name
, void *data
),
552 if (decoder
->base
.prov
!= NULL
) {
553 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(decoder
->base
.prov
);
554 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
556 return ossl_namemap_doall_names(namemap
, decoder
->base
.id
, fn
, data
);
563 OSSL_DECODER_gettable_params(OSSL_DECODER
*decoder
)
565 if (decoder
!= NULL
&& decoder
->gettable_params
!= NULL
) {
566 void *provctx
= ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder
));
568 return decoder
->gettable_params(provctx
);
573 int OSSL_DECODER_get_params(OSSL_DECODER
*decoder
, OSSL_PARAM params
[])
575 if (decoder
!= NULL
&& decoder
->get_params
!= NULL
)
576 return decoder
->get_params(params
);
581 OSSL_DECODER_settable_ctx_params(OSSL_DECODER
*decoder
)
583 if (decoder
!= NULL
&& decoder
->settable_ctx_params
!= NULL
) {
584 void *provctx
= ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder
));
586 return decoder
->settable_ctx_params(provctx
);
592 * Decoder context support
596 * |encoder| value NULL is valid, and signifies that there is no decoder.
597 * This is useful to provide fallback mechanisms.
598 * Functions that want to verify if there is a decoder can do so with
599 * OSSL_DECODER_CTX_get_decoder()
601 OSSL_DECODER_CTX
*OSSL_DECODER_CTX_new(void)
603 OSSL_DECODER_CTX
*ctx
;
605 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
)
606 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_MALLOC_FAILURE
);
611 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX
*ctx
,
612 const OSSL_PARAM params
[])
618 if (!ossl_assert(ctx
!= NULL
)) {
619 ERR_raise(ERR_LIB_OSSL_DECODER
, ERR_R_PASSED_NULL_PARAMETER
);
623 if (ctx
->decoder_insts
== NULL
)
626 l
= OSSL_DECODER_CTX_get_num_decoders(ctx
);
627 for (i
= 0; i
< l
; i
++) {
628 OSSL_DECODER_INSTANCE
*decoder_inst
=
629 sk_OSSL_DECODER_INSTANCE_value(ctx
->decoder_insts
, i
);
630 OSSL_DECODER
*decoder
=
631 OSSL_DECODER_INSTANCE_get_decoder(decoder_inst
);
632 OSSL_DECODER
*decoderctx
=
633 OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst
);
635 if (decoderctx
== NULL
|| decoder
->set_ctx_params
== NULL
)
637 if (!decoder
->set_ctx_params(decoderctx
, params
))
643 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX
*ctx
)
646 if (ctx
->cleanup
!= NULL
)
647 ctx
->cleanup(ctx
->construct_data
);
648 sk_OSSL_DECODER_INSTANCE_pop_free(ctx
->decoder_insts
,
649 ossl_decoder_instance_free
);
650 ossl_pw_clear_passphrase_data(&ctx
->pwdata
);