2 * Copyright 2019-2025 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/encoder.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/encoder.h"
19 #include "encoder_local.h"
20 #include "crypto/context.h"
23 * Encoder can have multiple names, separated with colons in a name string
25 #define NAME_SEPARATOR ':'
27 static void ossl_encoder_free(void *data
)
29 OSSL_ENCODER_free(data
);
32 static int ossl_encoder_up_ref(void *data
)
34 return OSSL_ENCODER_up_ref(data
);
37 /* Simple method structure constructor and destructor */
38 static OSSL_ENCODER
*ossl_encoder_new(void)
40 OSSL_ENCODER
*encoder
= NULL
;
42 if ((encoder
= OPENSSL_zalloc(sizeof(*encoder
))) == NULL
)
44 if (!CRYPTO_NEW_REF(&encoder
->base
.refcnt
, 1)) {
45 OSSL_ENCODER_free(encoder
);
52 int OSSL_ENCODER_up_ref(OSSL_ENCODER
*encoder
)
56 CRYPTO_UP_REF(&encoder
->base
.refcnt
, &ref
);
60 void OSSL_ENCODER_free(OSSL_ENCODER
*encoder
)
67 CRYPTO_DOWN_REF(&encoder
->base
.refcnt
, &ref
);
70 OPENSSL_free(encoder
->base
.name
);
71 ossl_property_free(encoder
->base
.parsed_propdef
);
72 ossl_provider_free(encoder
->base
.prov
);
73 CRYPTO_FREE_REF(&encoder
->base
.refcnt
);
74 OPENSSL_free(encoder
);
77 /* Data to be passed through ossl_method_construct() */
78 struct encoder_data_st
{
80 int id
; /* For get_encoder_from_store() */
81 const char *names
; /* For get_encoder_from_store() */
82 const char *propquery
; /* For get_encoder_from_store() */
84 OSSL_METHOD_STORE
*tmp_store
; /* For get_tmp_encoder_store() */
86 unsigned int flag_construct_error_occurred
: 1;
90 * Generic routines to fetch / create ENCODER methods with
91 * ossl_method_construct()
94 /* Temporary encoder method store, constructor and destructor */
95 static void *get_tmp_encoder_store(void *data
)
97 struct encoder_data_st
*methdata
= data
;
99 if (methdata
->tmp_store
== NULL
)
100 methdata
->tmp_store
= ossl_method_store_new(methdata
->libctx
);
101 return methdata
->tmp_store
;
104 static void dealloc_tmp_encoder_store(void *store
)
107 ossl_method_store_free(store
);
110 /* Get the permanent encoder store */
111 static OSSL_METHOD_STORE
*get_encoder_store(OSSL_LIB_CTX
*libctx
)
113 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_ENCODER_STORE_INDEX
);
116 static int reserve_encoder_store(void *store
, void *data
)
118 struct encoder_data_st
*methdata
= data
;
121 && (store
= get_encoder_store(methdata
->libctx
)) == NULL
)
124 return ossl_method_lock_store(store
);
127 static int unreserve_encoder_store(void *store
, void *data
)
129 struct encoder_data_st
*methdata
= data
;
132 && (store
= get_encoder_store(methdata
->libctx
)) == NULL
)
135 return ossl_method_unlock_store(store
);
138 /* Get encoder methods from a store, or put one in */
139 static void *get_encoder_from_store(void *store
, const OSSL_PROVIDER
**prov
,
142 struct encoder_data_st
*methdata
= data
;
147 * get_encoder_from_store() is only called to try and get the method
148 * that OSSL_ENCODER_fetch() is asking for, and the name or name id are
149 * passed via methdata.
151 if ((id
= methdata
->id
) == 0 && methdata
->names
!= NULL
) {
152 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
153 const char *names
= methdata
->names
;
154 const char *q
= strchr(names
, NAME_SEPARATOR
);
155 size_t l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
159 id
= ossl_namemap_name2num_n(namemap
, methdata
->names
, l
);
166 && (store
= get_encoder_store(methdata
->libctx
)) == NULL
)
169 if (!ossl_method_store_fetch(store
, id
, methdata
->propquery
, prov
, &method
))
174 static int put_encoder_in_store(void *store
, void *method
,
175 const OSSL_PROVIDER
*prov
,
176 const char *names
, const char *propdef
,
179 struct encoder_data_st
*methdata
= data
;
180 OSSL_NAMEMAP
*namemap
;
185 * put_encoder_in_store() is only called with an OSSL_ENCODER method that
186 * was successfully created by construct_encoder() below, which means that
187 * all the names should already be stored in the namemap with the same
188 * numeric identity, so just use the first to get that identity.
191 const char *q
= strchr(names
, NAME_SEPARATOR
);
193 l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
196 if ((namemap
= ossl_namemap_stored(methdata
->libctx
)) == NULL
197 || (id
= ossl_namemap_name2num_n(namemap
, names
, l
)) == 0)
200 if (store
== NULL
&& (store
= get_encoder_store(methdata
->libctx
)) == NULL
)
203 return ossl_method_store_add(store
, prov
, id
, propdef
, method
,
208 /* Create and populate a encoder method */
209 static void *encoder_from_algorithm(int id
, const OSSL_ALGORITHM
*algodef
,
212 OSSL_ENCODER
*encoder
= NULL
;
213 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
214 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
216 if ((encoder
= ossl_encoder_new()) == NULL
)
218 encoder
->base
.id
= id
;
219 if ((encoder
->base
.name
= ossl_algorithm_get1_first_name(algodef
)) == NULL
) {
220 OSSL_ENCODER_free(encoder
);
223 encoder
->base
.algodef
= algodef
;
224 if ((encoder
->base
.parsed_propdef
225 = ossl_parse_property(libctx
, algodef
->property_definition
)) == NULL
) {
226 OSSL_ENCODER_free(encoder
);
230 for (; fns
->function_id
!= 0; fns
++) {
231 switch (fns
->function_id
) {
232 case OSSL_FUNC_ENCODER_NEWCTX
:
233 if (encoder
->newctx
== NULL
)
235 OSSL_FUNC_encoder_newctx(fns
);
237 case OSSL_FUNC_ENCODER_FREECTX
:
238 if (encoder
->freectx
== NULL
)
240 OSSL_FUNC_encoder_freectx(fns
);
242 case OSSL_FUNC_ENCODER_GET_PARAMS
:
243 if (encoder
->get_params
== NULL
)
244 encoder
->get_params
=
245 OSSL_FUNC_encoder_get_params(fns
);
247 case OSSL_FUNC_ENCODER_GETTABLE_PARAMS
:
248 if (encoder
->gettable_params
== NULL
)
249 encoder
->gettable_params
=
250 OSSL_FUNC_encoder_gettable_params(fns
);
252 case OSSL_FUNC_ENCODER_SET_CTX_PARAMS
:
253 if (encoder
->set_ctx_params
== NULL
)
254 encoder
->set_ctx_params
=
255 OSSL_FUNC_encoder_set_ctx_params(fns
);
257 case OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS
:
258 if (encoder
->settable_ctx_params
== NULL
)
259 encoder
->settable_ctx_params
=
260 OSSL_FUNC_encoder_settable_ctx_params(fns
);
262 case OSSL_FUNC_ENCODER_DOES_SELECTION
:
263 if (encoder
->does_selection
== NULL
)
264 encoder
->does_selection
=
265 OSSL_FUNC_encoder_does_selection(fns
);
267 case OSSL_FUNC_ENCODER_ENCODE
:
268 if (encoder
->encode
== NULL
)
269 encoder
->encode
= OSSL_FUNC_encoder_encode(fns
);
271 case OSSL_FUNC_ENCODER_IMPORT_OBJECT
:
272 if (encoder
->import_object
== NULL
)
273 encoder
->import_object
=
274 OSSL_FUNC_encoder_import_object(fns
);
276 case OSSL_FUNC_ENCODER_FREE_OBJECT
:
277 if (encoder
->free_object
== NULL
)
278 encoder
->free_object
=
279 OSSL_FUNC_encoder_free_object(fns
);
284 * Try to check that the method is sensible.
285 * If you have a constructor, you must have a destructor and vice versa.
286 * You must have the encoding driver functions.
288 if (!((encoder
->newctx
== NULL
&& encoder
->freectx
== NULL
)
289 || (encoder
->newctx
!= NULL
&& encoder
->freectx
!= NULL
)
290 || (encoder
->import_object
!= NULL
&& encoder
->free_object
!= NULL
)
291 || (encoder
->import_object
== NULL
&& encoder
->free_object
== NULL
))
292 || encoder
->encode
== NULL
) {
293 OSSL_ENCODER_free(encoder
);
294 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_INVALID_PROVIDER_FUNCTIONS
);
298 if (prov
!= NULL
&& !ossl_provider_up_ref(prov
)) {
299 OSSL_ENCODER_free(encoder
);
303 encoder
->base
.prov
= prov
;
309 * The core fetching functionality passes the names of the implementation.
310 * This function is responsible to getting an identity number for them,
311 * then call encoder_from_algorithm() with that identity number.
313 static void *construct_encoder(const OSSL_ALGORITHM
*algodef
,
314 OSSL_PROVIDER
*prov
, void *data
)
317 * This function is only called if get_encoder_from_store() returned
318 * NULL, so it's safe to say that of all the spots to create a new
319 * namemap entry, this is it. Should the name already exist there, we
320 * know that ossl_namemap_add() will return its corresponding number.
322 struct encoder_data_st
*methdata
= data
;
323 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
324 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
325 const char *names
= algodef
->algorithm_names
;
326 int id
= ossl_namemap_add_names(namemap
, 0, names
, NAME_SEPARATOR
);
330 method
= encoder_from_algorithm(id
, algodef
, prov
);
333 * Flag to indicate that there was actual construction errors. This
334 * helps inner_evp_generic_fetch() determine what error it should
335 * record on inaccessible algorithms.
338 methdata
->flag_construct_error_occurred
= 1;
343 /* Intermediary function to avoid ugly casts, used below */
344 static void destruct_encoder(void *method
, void *data
)
346 OSSL_ENCODER_free(method
);
349 static int up_ref_encoder(void *method
)
351 return OSSL_ENCODER_up_ref(method
);
354 static void free_encoder(void *method
)
356 OSSL_ENCODER_free(method
);
359 /* Fetching support. Can fetch by numeric identity or by name */
360 static OSSL_ENCODER
*
361 inner_ossl_encoder_fetch(struct encoder_data_st
*methdata
,
362 const char *name
, const char *properties
)
364 OSSL_METHOD_STORE
*store
= get_encoder_store(methdata
->libctx
);
365 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
366 const char *const propq
= properties
!= NULL
? properties
: "";
370 if (store
== NULL
|| namemap
== NULL
) {
371 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_INVALID_ARGUMENT
);
375 id
= name
!= NULL
? ossl_namemap_name2num(namemap
, name
) : 0;
378 * If we haven't found the name yet, chances are that the algorithm to
379 * be fetched is unsupported.
381 unsupported
= id
== 0;
384 || !ossl_method_store_cache_get(store
, NULL
, id
, propq
, &method
)) {
385 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
386 get_tmp_encoder_store
,
387 reserve_encoder_store
,
388 unreserve_encoder_store
,
389 get_encoder_from_store
,
390 put_encoder_in_store
,
394 OSSL_PROVIDER
*prov
= NULL
;
397 methdata
->names
= name
;
398 methdata
->propquery
= propq
;
399 methdata
->flag_construct_error_occurred
= 0;
400 if ((method
= ossl_method_construct(methdata
->libctx
, OSSL_OP_ENCODER
,
401 &prov
, 0 /* !force_cache */,
402 &mcm
, methdata
)) != NULL
) {
404 * If construction did create a method for us, we know that
405 * there is a correct name_id and meth_id, since those have
406 * already been calculated in get_encoder_from_store() and
407 * put_encoder_in_store() above.
410 id
= ossl_namemap_name2num(namemap
, name
);
411 ossl_method_store_cache_set(store
, prov
, id
, propq
, method
,
412 up_ref_encoder
, free_encoder
);
416 * If we never were in the constructor, the algorithm to be fetched
419 unsupported
= !methdata
->flag_construct_error_occurred
;
422 if ((id
!= 0 || name
!= NULL
) && method
== NULL
) {
423 int code
= unsupported
? ERR_R_UNSUPPORTED
: ERR_R_FETCH_FAILED
;
426 name
= ossl_namemap_num2name(namemap
, id
, 0);
427 ERR_raise_data(ERR_LIB_OSSL_ENCODER
, code
,
428 "%s, Name (%s : %d), Properties (%s)",
429 ossl_lib_ctx_get_descriptor(methdata
->libctx
),
430 name
== NULL
? "<null>" : name
, id
,
431 properties
== NULL
? "<null>" : properties
);
437 OSSL_ENCODER
*OSSL_ENCODER_fetch(OSSL_LIB_CTX
*libctx
, const char *name
,
438 const char *properties
)
440 struct encoder_data_st methdata
;
443 methdata
.libctx
= libctx
;
444 methdata
.tmp_store
= NULL
;
445 method
= inner_ossl_encoder_fetch(&methdata
, name
, properties
);
446 dealloc_tmp_encoder_store(methdata
.tmp_store
);
450 int ossl_encoder_store_cache_flush(OSSL_LIB_CTX
*libctx
)
452 OSSL_METHOD_STORE
*store
= get_encoder_store(libctx
);
455 return ossl_method_store_cache_flush_all(store
);
459 int ossl_encoder_store_remove_all_provided(const OSSL_PROVIDER
*prov
)
461 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
462 OSSL_METHOD_STORE
*store
= get_encoder_store(libctx
);
465 return ossl_method_store_remove_all_provided(store
, prov
);
470 * Library of basic method functions
473 const OSSL_PROVIDER
*OSSL_ENCODER_get0_provider(const OSSL_ENCODER
*encoder
)
475 if (!ossl_assert(encoder
!= NULL
)) {
476 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
480 return encoder
->base
.prov
;
483 const char *OSSL_ENCODER_get0_properties(const OSSL_ENCODER
*encoder
)
485 if (!ossl_assert(encoder
!= NULL
)) {
486 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
490 return encoder
->base
.algodef
->property_definition
;
493 const OSSL_PROPERTY_LIST
*
494 ossl_encoder_parsed_properties(const OSSL_ENCODER
*encoder
)
496 if (!ossl_assert(encoder
!= NULL
)) {
497 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
501 return encoder
->base
.parsed_propdef
;
504 int ossl_encoder_get_number(const OSSL_ENCODER
*encoder
)
506 if (!ossl_assert(encoder
!= NULL
)) {
507 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
511 return encoder
->base
.id
;
514 const char *OSSL_ENCODER_get0_name(const OSSL_ENCODER
*encoder
)
516 return encoder
->base
.name
;
519 const char *OSSL_ENCODER_get0_description(const OSSL_ENCODER
*encoder
)
521 return encoder
->base
.algodef
->algorithm_description
;
524 int OSSL_ENCODER_is_a(const OSSL_ENCODER
*encoder
, const char *name
)
526 if (encoder
->base
.prov
!= NULL
) {
527 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(encoder
->base
.prov
);
528 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
530 return ossl_namemap_name2num(namemap
, name
) == encoder
->base
.id
;
535 struct do_one_data_st
{
536 void (*user_fn
)(OSSL_ENCODER
*encoder
, void *arg
);
540 static void do_one(ossl_unused
int id
, void *method
, void *arg
)
542 struct do_one_data_st
*data
= arg
;
544 data
->user_fn(method
, data
->user_arg
);
547 void OSSL_ENCODER_do_all_provided(OSSL_LIB_CTX
*libctx
,
548 void (*user_fn
)(OSSL_ENCODER
*encoder
,
552 struct encoder_data_st methdata
;
553 struct do_one_data_st data
;
555 methdata
.libctx
= libctx
;
556 methdata
.tmp_store
= NULL
;
557 (void)inner_ossl_encoder_fetch(&methdata
, NULL
, NULL
/* properties */);
559 data
.user_fn
= user_fn
;
560 data
.user_arg
= user_arg
;
561 if (methdata
.tmp_store
!= NULL
)
562 ossl_method_store_do_all(methdata
.tmp_store
, &do_one
, &data
);
563 ossl_method_store_do_all(get_encoder_store(libctx
), &do_one
, &data
);
564 dealloc_tmp_encoder_store(methdata
.tmp_store
);
567 int OSSL_ENCODER_names_do_all(const OSSL_ENCODER
*encoder
,
568 void (*fn
)(const char *name
, void *data
),
574 if (encoder
->base
.prov
!= NULL
) {
575 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(encoder
->base
.prov
);
576 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
578 return ossl_namemap_doall_names(namemap
, encoder
->base
.id
, fn
, data
);
585 OSSL_ENCODER_gettable_params(OSSL_ENCODER
*encoder
)
587 if (encoder
!= NULL
&& encoder
->gettable_params
!= NULL
) {
588 void *provctx
= ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder
));
590 return encoder
->gettable_params(provctx
);
595 int OSSL_ENCODER_get_params(OSSL_ENCODER
*encoder
, OSSL_PARAM params
[])
597 if (encoder
!= NULL
&& encoder
->get_params
!= NULL
)
598 return encoder
->get_params(params
);
602 const OSSL_PARAM
*OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER
*encoder
)
604 if (encoder
!= NULL
&& encoder
->settable_ctx_params
!= NULL
) {
605 void *provctx
= ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder
));
607 return encoder
->settable_ctx_params(provctx
);
613 * Encoder context support
616 OSSL_ENCODER_CTX
*OSSL_ENCODER_CTX_new(void)
618 OSSL_ENCODER_CTX
*ctx
;
620 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
624 int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX
*ctx
,
625 const OSSL_PARAM params
[])
631 if (!ossl_assert(ctx
!= NULL
)) {
632 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
636 if (ctx
->encoder_insts
== NULL
)
639 l
= OSSL_ENCODER_CTX_get_num_encoders(ctx
);
640 for (i
= 0; i
< l
; i
++) {
641 OSSL_ENCODER_INSTANCE
*encoder_inst
=
642 sk_OSSL_ENCODER_INSTANCE_value(ctx
->encoder_insts
, i
);
643 OSSL_ENCODER
*encoder
= OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst
);
644 void *encoderctx
= OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst
);
646 if (encoderctx
== NULL
|| encoder
->set_ctx_params
== NULL
)
648 if (!encoder
->set_ctx_params(encoderctx
, params
))
654 void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX
*ctx
)
657 sk_OSSL_ENCODER_INSTANCE_pop_free(ctx
->encoder_insts
,
658 ossl_encoder_instance_free
);
659 OPENSSL_free(ctx
->construct_data
);
660 ossl_pw_clear_passphrase_data(&ctx
->pwdata
);