2 * Copyright 2019-2022 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 /* Simple method structure constructor and destructor */
28 static OSSL_ENCODER
*ossl_encoder_new(void)
30 OSSL_ENCODER
*encoder
= NULL
;
32 if ((encoder
= OPENSSL_zalloc(sizeof(*encoder
))) == NULL
33 || (encoder
->base
.lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
34 OSSL_ENCODER_free(encoder
);
35 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_MALLOC_FAILURE
);
39 encoder
->base
.refcnt
= 1;
44 int OSSL_ENCODER_up_ref(OSSL_ENCODER
*encoder
)
48 CRYPTO_UP_REF(&encoder
->base
.refcnt
, &ref
, encoder
->base
.lock
);
52 void OSSL_ENCODER_free(OSSL_ENCODER
*encoder
)
59 CRYPTO_DOWN_REF(&encoder
->base
.refcnt
, &ref
, encoder
->base
.lock
);
62 OPENSSL_free(encoder
->base
.name
);
63 ossl_property_free(encoder
->base
.parsed_propdef
);
64 ossl_provider_free(encoder
->base
.prov
);
65 CRYPTO_THREAD_lock_free(encoder
->base
.lock
);
66 OPENSSL_free(encoder
);
69 /* Data to be passed through ossl_method_construct() */
70 struct encoder_data_st
{
72 int id
; /* For get_encoder_from_store() */
73 const char *names
; /* For get_encoder_from_store() */
74 const char *propquery
; /* For get_encoder_from_store() */
76 OSSL_METHOD_STORE
*tmp_store
; /* For get_tmp_encoder_store() */
78 unsigned int flag_construct_error_occurred
: 1;
82 * Generic routines to fetch / create ENCODER methods with
83 * ossl_method_construct()
86 /* Temporary encoder method store, constructor and destructor */
87 static void *get_tmp_encoder_store(void *data
)
89 struct encoder_data_st
*methdata
= data
;
91 if (methdata
->tmp_store
== NULL
)
92 methdata
->tmp_store
= ossl_method_store_new(methdata
->libctx
);
93 return methdata
->tmp_store
;
96 static void dealloc_tmp_encoder_store(void *store
)
99 ossl_method_store_free(store
);
102 /* Get the permanent encoder store */
103 static OSSL_METHOD_STORE
*get_encoder_store(OSSL_LIB_CTX
*libctx
)
105 return ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_ENCODER_STORE_INDEX
);
108 static int reserve_encoder_store(void *store
, void *data
)
110 struct encoder_data_st
*methdata
= data
;
113 && (store
= get_encoder_store(methdata
->libctx
)) == NULL
)
116 return ossl_method_lock_store(store
);
119 static int unreserve_encoder_store(void *store
, void *data
)
121 struct encoder_data_st
*methdata
= data
;
124 && (store
= get_encoder_store(methdata
->libctx
)) == NULL
)
127 return ossl_method_unlock_store(store
);
130 /* Get encoder methods from a store, or put one in */
131 static void *get_encoder_from_store(void *store
, const OSSL_PROVIDER
**prov
,
134 struct encoder_data_st
*methdata
= data
;
139 * get_encoder_from_store() is only called to try and get the method
140 * that OSSL_ENCODER_fetch() is asking for, and the name or name id are
141 * passed via methdata.
143 if ((id
= methdata
->id
) == 0 && methdata
->names
!= NULL
) {
144 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
145 const char *names
= methdata
->names
;
146 const char *q
= strchr(names
, NAME_SEPARATOR
);
147 size_t l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
151 id
= ossl_namemap_name2num_n(namemap
, methdata
->names
, l
);
158 && (store
= get_encoder_store(methdata
->libctx
)) == NULL
)
161 if (!ossl_method_store_fetch(store
, id
, methdata
->propquery
, prov
, &method
))
166 static int put_encoder_in_store(void *store
, void *method
,
167 const OSSL_PROVIDER
*prov
,
168 const char *names
, const char *propdef
,
171 struct encoder_data_st
*methdata
= data
;
172 OSSL_NAMEMAP
*namemap
;
177 * put_encoder_in_store() is only called with an OSSL_ENCODER method that
178 * was successfully created by construct_encoder() below, which means that
179 * all the names should already be stored in the namemap with the same
180 * numeric identity, so just use the first to get that identity.
183 const char *q
= strchr(names
, NAME_SEPARATOR
);
185 l
= (q
== NULL
? strlen(names
) : (size_t)(q
- names
));
188 if ((namemap
= ossl_namemap_stored(methdata
->libctx
)) == NULL
189 || (id
= ossl_namemap_name2num_n(namemap
, names
, l
)) == 0)
192 if (store
== NULL
&& (store
= get_encoder_store(methdata
->libctx
)) == NULL
)
195 return ossl_method_store_add(store
, prov
, id
, propdef
, method
,
196 (int (*)(void *))OSSL_ENCODER_up_ref
,
197 (void (*)(void *))OSSL_ENCODER_free
);
200 /* Create and populate a encoder method */
201 static void *encoder_from_algorithm(int id
, const OSSL_ALGORITHM
*algodef
,
204 OSSL_ENCODER
*encoder
= NULL
;
205 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
206 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
208 if ((encoder
= ossl_encoder_new()) == NULL
)
210 encoder
->base
.id
= id
;
211 if ((encoder
->base
.name
= ossl_algorithm_get1_first_name(algodef
)) == NULL
) {
212 OSSL_ENCODER_free(encoder
);
215 encoder
->base
.algodef
= algodef
;
216 if ((encoder
->base
.parsed_propdef
217 = ossl_parse_property(libctx
, algodef
->property_definition
)) == NULL
) {
218 OSSL_ENCODER_free(encoder
);
222 for (; fns
->function_id
!= 0; fns
++) {
223 switch (fns
->function_id
) {
224 case OSSL_FUNC_ENCODER_NEWCTX
:
225 if (encoder
->newctx
== NULL
)
227 OSSL_FUNC_encoder_newctx(fns
);
229 case OSSL_FUNC_ENCODER_FREECTX
:
230 if (encoder
->freectx
== NULL
)
232 OSSL_FUNC_encoder_freectx(fns
);
234 case OSSL_FUNC_ENCODER_GET_PARAMS
:
235 if (encoder
->get_params
== NULL
)
236 encoder
->get_params
=
237 OSSL_FUNC_encoder_get_params(fns
);
239 case OSSL_FUNC_ENCODER_GETTABLE_PARAMS
:
240 if (encoder
->gettable_params
== NULL
)
241 encoder
->gettable_params
=
242 OSSL_FUNC_encoder_gettable_params(fns
);
244 case OSSL_FUNC_ENCODER_SET_CTX_PARAMS
:
245 if (encoder
->set_ctx_params
== NULL
)
246 encoder
->set_ctx_params
=
247 OSSL_FUNC_encoder_set_ctx_params(fns
);
249 case OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS
:
250 if (encoder
->settable_ctx_params
== NULL
)
251 encoder
->settable_ctx_params
=
252 OSSL_FUNC_encoder_settable_ctx_params(fns
);
254 case OSSL_FUNC_ENCODER_DOES_SELECTION
:
255 if (encoder
->does_selection
== NULL
)
256 encoder
->does_selection
=
257 OSSL_FUNC_encoder_does_selection(fns
);
259 case OSSL_FUNC_ENCODER_ENCODE
:
260 if (encoder
->encode
== NULL
)
261 encoder
->encode
= OSSL_FUNC_encoder_encode(fns
);
263 case OSSL_FUNC_ENCODER_IMPORT_OBJECT
:
264 if (encoder
->import_object
== NULL
)
265 encoder
->import_object
=
266 OSSL_FUNC_encoder_import_object(fns
);
268 case OSSL_FUNC_ENCODER_FREE_OBJECT
:
269 if (encoder
->free_object
== NULL
)
270 encoder
->free_object
=
271 OSSL_FUNC_encoder_free_object(fns
);
276 * Try to check that the method is sensible.
277 * If you have a constructor, you must have a destructor and vice versa.
278 * You must have the encoding driver functions.
280 if (!((encoder
->newctx
== NULL
&& encoder
->freectx
== NULL
)
281 || (encoder
->newctx
!= NULL
&& encoder
->freectx
!= NULL
)
282 || (encoder
->import_object
!= NULL
&& encoder
->free_object
!= NULL
)
283 || (encoder
->import_object
== NULL
&& encoder
->free_object
== NULL
))
284 || encoder
->encode
== NULL
) {
285 OSSL_ENCODER_free(encoder
);
286 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_INVALID_PROVIDER_FUNCTIONS
);
290 if (prov
!= NULL
&& !ossl_provider_up_ref(prov
)) {
291 OSSL_ENCODER_free(encoder
);
295 encoder
->base
.prov
= prov
;
301 * The core fetching functionality passes the names of the implementation.
302 * This function is responsible to getting an identity number for them,
303 * then call encoder_from_algorithm() with that identity number.
305 static void *construct_encoder(const OSSL_ALGORITHM
*algodef
,
306 OSSL_PROVIDER
*prov
, void *data
)
309 * This function is only called if get_encoder_from_store() returned
310 * NULL, so it's safe to say that of all the spots to create a new
311 * namemap entry, this is it. Should the name already exist there, we
312 * know that ossl_namemap_add() will return its corresponding number.
314 struct encoder_data_st
*methdata
= data
;
315 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
316 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
317 const char *names
= algodef
->algorithm_names
;
318 int id
= ossl_namemap_add_names(namemap
, 0, names
, NAME_SEPARATOR
);
322 method
= encoder_from_algorithm(id
, algodef
, prov
);
325 * Flag to indicate that there was actual construction errors. This
326 * helps inner_evp_generic_fetch() determine what error it should
327 * record on inaccessible algorithms.
330 methdata
->flag_construct_error_occurred
= 1;
335 /* Intermediary function to avoid ugly casts, used below */
336 static void destruct_encoder(void *method
, void *data
)
338 OSSL_ENCODER_free(method
);
341 static int up_ref_encoder(void *method
)
343 return OSSL_ENCODER_up_ref(method
);
346 static void free_encoder(void *method
)
348 OSSL_ENCODER_free(method
);
351 /* Fetching support. Can fetch by numeric identity or by name */
352 static OSSL_ENCODER
*
353 inner_ossl_encoder_fetch(struct encoder_data_st
*methdata
,
354 const char *name
, const char *properties
)
356 OSSL_METHOD_STORE
*store
= get_encoder_store(methdata
->libctx
);
357 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(methdata
->libctx
);
358 const char *const propq
= properties
!= NULL
? properties
: "";
362 if (store
== NULL
|| namemap
== NULL
) {
363 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_INVALID_ARGUMENT
);
367 id
= name
!= NULL
? ossl_namemap_name2num(namemap
, name
) : 0;
370 * If we haven't found the name yet, chances are that the algorithm to
371 * be fetched is unsupported.
373 unsupported
= id
== 0;
376 || !ossl_method_store_cache_get(store
, NULL
, id
, propq
, &method
)) {
377 OSSL_METHOD_CONSTRUCT_METHOD mcm
= {
378 get_tmp_encoder_store
,
379 reserve_encoder_store
,
380 unreserve_encoder_store
,
381 get_encoder_from_store
,
382 put_encoder_in_store
,
386 OSSL_PROVIDER
*prov
= NULL
;
389 methdata
->names
= name
;
390 methdata
->propquery
= propq
;
391 methdata
->flag_construct_error_occurred
= 0;
392 if ((method
= ossl_method_construct(methdata
->libctx
, OSSL_OP_ENCODER
,
393 &prov
, 0 /* !force_cache */,
394 &mcm
, methdata
)) != NULL
) {
396 * If construction did create a method for us, we know that
397 * there is a correct name_id and meth_id, since those have
398 * already been calculated in get_encoder_from_store() and
399 * put_encoder_in_store() above.
402 id
= ossl_namemap_name2num(namemap
, name
);
403 ossl_method_store_cache_set(store
, prov
, id
, propq
, method
,
404 up_ref_encoder
, free_encoder
);
408 * If we never were in the constructor, the algorithm to be fetched
411 unsupported
= !methdata
->flag_construct_error_occurred
;
414 if ((id
!= 0 || name
!= NULL
) && method
== NULL
) {
415 int code
= unsupported
? ERR_R_UNSUPPORTED
: ERR_R_FETCH_FAILED
;
418 name
= ossl_namemap_num2name(namemap
, id
, 0);
419 ERR_raise_data(ERR_LIB_OSSL_ENCODER
, code
,
420 "%s, Name (%s : %d), Properties (%s)",
421 ossl_lib_ctx_get_descriptor(methdata
->libctx
),
422 name
== NULL
? "<null>" : name
, id
,
423 properties
== NULL
? "<null>" : properties
);
429 OSSL_ENCODER
*OSSL_ENCODER_fetch(OSSL_LIB_CTX
*libctx
, const char *name
,
430 const char *properties
)
432 struct encoder_data_st methdata
;
435 methdata
.libctx
= libctx
;
436 methdata
.tmp_store
= NULL
;
437 method
= inner_ossl_encoder_fetch(&methdata
, name
, properties
);
438 dealloc_tmp_encoder_store(methdata
.tmp_store
);
442 int ossl_encoder_store_cache_flush(OSSL_LIB_CTX
*libctx
)
444 OSSL_METHOD_STORE
*store
= get_encoder_store(libctx
);
447 return ossl_method_store_cache_flush_all(store
);
451 int ossl_encoder_store_remove_all_provided(const OSSL_PROVIDER
*prov
)
453 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(prov
);
454 OSSL_METHOD_STORE
*store
= get_encoder_store(libctx
);
457 return ossl_method_store_remove_all_provided(store
, prov
);
462 * Library of basic method functions
465 const OSSL_PROVIDER
*OSSL_ENCODER_get0_provider(const OSSL_ENCODER
*encoder
)
467 if (!ossl_assert(encoder
!= NULL
)) {
468 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
472 return encoder
->base
.prov
;
475 const char *OSSL_ENCODER_get0_properties(const OSSL_ENCODER
*encoder
)
477 if (!ossl_assert(encoder
!= NULL
)) {
478 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
482 return encoder
->base
.algodef
->property_definition
;
485 const OSSL_PROPERTY_LIST
*
486 ossl_encoder_parsed_properties(const OSSL_ENCODER
*encoder
)
488 if (!ossl_assert(encoder
!= NULL
)) {
489 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
493 return encoder
->base
.parsed_propdef
;
496 int ossl_encoder_get_number(const OSSL_ENCODER
*encoder
)
498 if (!ossl_assert(encoder
!= NULL
)) {
499 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
503 return encoder
->base
.id
;
506 const char *OSSL_ENCODER_get0_name(const OSSL_ENCODER
*encoder
)
508 return encoder
->base
.name
;
511 const char *OSSL_ENCODER_get0_description(const OSSL_ENCODER
*encoder
)
513 return encoder
->base
.algodef
->algorithm_description
;
516 int OSSL_ENCODER_is_a(const OSSL_ENCODER
*encoder
, const char *name
)
518 if (encoder
->base
.prov
!= NULL
) {
519 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(encoder
->base
.prov
);
520 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
522 return ossl_namemap_name2num(namemap
, name
) == encoder
->base
.id
;
527 struct do_one_data_st
{
528 void (*user_fn
)(OSSL_ENCODER
*encoder
, void *arg
);
532 static void do_one(ossl_unused
int id
, void *method
, void *arg
)
534 struct do_one_data_st
*data
= arg
;
536 data
->user_fn(method
, data
->user_arg
);
539 void OSSL_ENCODER_do_all_provided(OSSL_LIB_CTX
*libctx
,
540 void (*user_fn
)(OSSL_ENCODER
*encoder
,
544 struct encoder_data_st methdata
;
545 struct do_one_data_st data
;
547 methdata
.libctx
= libctx
;
548 methdata
.tmp_store
= NULL
;
549 (void)inner_ossl_encoder_fetch(&methdata
, NULL
, NULL
/* properties */);
551 data
.user_fn
= user_fn
;
552 data
.user_arg
= user_arg
;
553 if (methdata
.tmp_store
!= NULL
)
554 ossl_method_store_do_all(methdata
.tmp_store
, &do_one
, &data
);
555 ossl_method_store_do_all(get_encoder_store(libctx
), &do_one
, &data
);
556 dealloc_tmp_encoder_store(methdata
.tmp_store
);
559 int OSSL_ENCODER_names_do_all(const OSSL_ENCODER
*encoder
,
560 void (*fn
)(const char *name
, void *data
),
566 if (encoder
->base
.prov
!= NULL
) {
567 OSSL_LIB_CTX
*libctx
= ossl_provider_libctx(encoder
->base
.prov
);
568 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
570 return ossl_namemap_doall_names(namemap
, encoder
->base
.id
, fn
, data
);
577 OSSL_ENCODER_gettable_params(OSSL_ENCODER
*encoder
)
579 if (encoder
!= NULL
&& encoder
->gettable_params
!= NULL
) {
580 void *provctx
= ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder
));
582 return encoder
->gettable_params(provctx
);
587 int OSSL_ENCODER_get_params(OSSL_ENCODER
*encoder
, OSSL_PARAM params
[])
589 if (encoder
!= NULL
&& encoder
->get_params
!= NULL
)
590 return encoder
->get_params(params
);
594 const OSSL_PARAM
*OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER
*encoder
)
596 if (encoder
!= NULL
&& encoder
->settable_ctx_params
!= NULL
) {
597 void *provctx
= ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder
));
599 return encoder
->settable_ctx_params(provctx
);
605 * Encoder context support
608 OSSL_ENCODER_CTX
*OSSL_ENCODER_CTX_new(void)
610 OSSL_ENCODER_CTX
*ctx
;
612 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
)
613 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_MALLOC_FAILURE
);
618 int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX
*ctx
,
619 const OSSL_PARAM params
[])
625 if (!ossl_assert(ctx
!= NULL
)) {
626 ERR_raise(ERR_LIB_OSSL_ENCODER
, ERR_R_PASSED_NULL_PARAMETER
);
630 if (ctx
->encoder_insts
== NULL
)
633 l
= OSSL_ENCODER_CTX_get_num_encoders(ctx
);
634 for (i
= 0; i
< l
; i
++) {
635 OSSL_ENCODER_INSTANCE
*encoder_inst
=
636 sk_OSSL_ENCODER_INSTANCE_value(ctx
->encoder_insts
, i
);
637 OSSL_ENCODER
*encoder
= OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst
);
638 void *encoderctx
= OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst
);
640 if (encoderctx
== NULL
|| encoder
->set_ctx_params
== NULL
)
642 if (!encoder
->set_ctx_params(encoderctx
, params
))
648 void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX
*ctx
)
651 sk_OSSL_ENCODER_INSTANCE_pop_free(ctx
->encoder_insts
,
652 ossl_encoder_instance_free
);
653 OPENSSL_free(ctx
->construct_data
);
654 ossl_pw_clear_passphrase_data(&ctx
->pwdata
);