2 * Copyright 2019-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/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"
22 * Encoder 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_ENCODER *ossl_encoder_new(void)
29 OSSL_ENCODER *encoder = NULL;
31 if ((encoder = OPENSSL_zalloc(sizeof(*encoder))) == NULL
32 || (encoder->base.lock = CRYPTO_THREAD_lock_new()) == NULL) {
33 OSSL_ENCODER_free(encoder);
34 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
38 encoder->base.refcnt = 1;
43 int OSSL_ENCODER_up_ref(OSSL_ENCODER *encoder)
47 CRYPTO_UP_REF(&encoder->base.refcnt, &ref, encoder->base.lock);
51 void OSSL_ENCODER_free(OSSL_ENCODER *encoder)
58 CRYPTO_DOWN_REF(&encoder->base.refcnt, &ref, encoder->base.lock);
61 OPENSSL_free(encoder->base.name);
62 ossl_property_free(encoder->base.parsed_propdef);
63 ossl_provider_free(encoder->base.prov);
64 CRYPTO_THREAD_lock_free(encoder->base.lock);
65 OPENSSL_free(encoder);
68 /* Permanent encoder method store, constructor and destructor */
69 static void encoder_store_free(void *vstore)
71 ossl_method_store_free(vstore);
74 static void *encoder_store_new(OSSL_LIB_CTX *ctx)
76 return ossl_method_store_new(ctx);
80 static const OSSL_LIB_CTX_METHOD encoder_store_method = {
81 /* We want encoder_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 encoder_data_st {
90 int id; /* For get_encoder_from_store() */
91 const char *names; /* For get_encoder_from_store() */
92 const char *propquery; /* For get_encoder_from_store() */
94 OSSL_METHOD_STORE *tmp_store; /* For get_tmp_encoder_store() */
96 unsigned int flag_construct_error_occurred : 1;
100 * Generic routines to fetch / create ENCODER methods with
101 * ossl_method_construct()
104 /* Temporary encoder method store, constructor and destructor */
105 static void *get_tmp_encoder_store(void *data)
107 struct encoder_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_encoder_store(void *store)
117 ossl_method_store_free(store);
120 /* Get the permanent encoder store */
121 static OSSL_METHOD_STORE *get_encoder_store(OSSL_LIB_CTX *libctx)
123 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_ENCODER_STORE_INDEX,
124 &encoder_store_method);
127 /* Get encoder methods from a store, or put one in */
128 static void *get_encoder_from_store(void *store, const OSSL_PROVIDER **prov,
131 struct encoder_data_st *methdata = data;
136 * get_encoder_from_store() is only called to try and get the method
137 * that OSSL_ENCODER_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, methdata->names, l);
155 && (store = get_encoder_store(methdata->libctx)) == NULL)
158 if (!ossl_method_store_fetch(store, id, methdata->propquery, prov, &method))
163 static int put_encoder_in_store(void *store, void *method,
164 const OSSL_PROVIDER *prov,
165 const char *names, const char *propdef,
168 struct encoder_data_st *methdata = data;
169 OSSL_NAMEMAP *namemap;
174 * put_encoder_in_store() is only called with an OSSL_ENCODER method that
175 * was successfully created by construct_encoder() 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_encoder_store(methdata->libctx)) == NULL)
192 return ossl_method_store_add(store, prov, id, propdef, method,
193 (int (*)(void *))OSSL_ENCODER_up_ref,
194 (void (*)(void *))OSSL_ENCODER_free);
197 /* Create and populate a encoder method */
198 static void *encoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
201 OSSL_ENCODER *encoder = NULL;
202 const OSSL_DISPATCH *fns = algodef->implementation;
203 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
205 if ((encoder = ossl_encoder_new()) == NULL)
207 encoder->base.id = id;
208 if ((encoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
209 OSSL_ENCODER_free(encoder);
212 encoder->base.algodef = algodef;
213 encoder->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_ENCODER_NEWCTX:
219 if (encoder->newctx == NULL)
221 OSSL_FUNC_encoder_newctx(fns);
223 case OSSL_FUNC_ENCODER_FREECTX:
224 if (encoder->freectx == NULL)
226 OSSL_FUNC_encoder_freectx(fns);
228 case OSSL_FUNC_ENCODER_GET_PARAMS:
229 if (encoder->get_params == NULL)
230 encoder->get_params =
231 OSSL_FUNC_encoder_get_params(fns);
233 case OSSL_FUNC_ENCODER_GETTABLE_PARAMS:
234 if (encoder->gettable_params == NULL)
235 encoder->gettable_params =
236 OSSL_FUNC_encoder_gettable_params(fns);
238 case OSSL_FUNC_ENCODER_SET_CTX_PARAMS:
239 if (encoder->set_ctx_params == NULL)
240 encoder->set_ctx_params =
241 OSSL_FUNC_encoder_set_ctx_params(fns);
243 case OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS:
244 if (encoder->settable_ctx_params == NULL)
245 encoder->settable_ctx_params =
246 OSSL_FUNC_encoder_settable_ctx_params(fns);
248 case OSSL_FUNC_ENCODER_DOES_SELECTION:
249 if (encoder->does_selection == NULL)
250 encoder->does_selection =
251 OSSL_FUNC_encoder_does_selection(fns);
253 case OSSL_FUNC_ENCODER_ENCODE:
254 if (encoder->encode == NULL)
255 encoder->encode = OSSL_FUNC_encoder_encode(fns);
257 case OSSL_FUNC_ENCODER_IMPORT_OBJECT:
258 if (encoder->import_object == NULL)
259 encoder->import_object =
260 OSSL_FUNC_encoder_import_object(fns);
262 case OSSL_FUNC_ENCODER_FREE_OBJECT:
263 if (encoder->free_object == NULL)
264 encoder->free_object =
265 OSSL_FUNC_encoder_free_object(fns);
270 * Try to check that the method is sensible.
271 * If you have a constructor, you must have a destructor and vice versa.
272 * You must have the encoding driver functions.
274 if (!((encoder->newctx == NULL && encoder->freectx == NULL)
275 || (encoder->newctx != NULL && encoder->freectx != NULL)
276 || (encoder->import_object != NULL && encoder->free_object != NULL)
277 || (encoder->import_object == NULL && encoder->free_object == NULL))
278 || encoder->encode == NULL) {
279 OSSL_ENCODER_free(encoder);
280 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
284 if (prov != NULL && !ossl_provider_up_ref(prov)) {
285 OSSL_ENCODER_free(encoder);
289 encoder->base.prov = prov;
295 * The core fetching functionality passes the names of the implementation.
296 * This function is responsible to getting an identity number for them,
297 * then call encoder_from_algorithm() with that identity number.
299 static void *construct_encoder(const OSSL_ALGORITHM *algodef,
300 OSSL_PROVIDER *prov, void *data)
303 * This function is only called if get_encoder_from_store() returned
304 * NULL, so it's safe to say that of all the spots to create a new
305 * namemap entry, this is it. Should the name already exist there, we
306 * know that ossl_namemap_add() will return its corresponding number.
308 struct encoder_data_st *methdata = data;
309 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
310 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
311 const char *names = algodef->algorithm_names;
312 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
316 method = encoder_from_algorithm(id, algodef, prov);
319 * Flag to indicate that there was actual construction errors. This
320 * helps inner_evp_generic_fetch() determine what error it should
321 * record on inaccessible algorithms.
324 methdata->flag_construct_error_occurred = 1;
329 /* Intermediary function to avoid ugly casts, used below */
330 static void destruct_encoder(void *method, void *data)
332 OSSL_ENCODER_free(method);
335 static int up_ref_encoder(void *method)
337 return OSSL_ENCODER_up_ref(method);
340 static void free_encoder(void *method)
342 OSSL_ENCODER_free(method);
345 /* Fetching support. Can fetch by numeric identity or by name */
346 static OSSL_ENCODER *
347 inner_ossl_encoder_fetch(struct encoder_data_st *methdata, int id,
348 const char *name, const char *properties)
350 OSSL_METHOD_STORE *store = get_encoder_store(methdata->libctx);
351 OSSL_NAMEMAP *namemap = ossl_namemap_stored(methdata->libctx);
355 if (store == NULL || namemap == NULL) {
356 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
361 * If we have been passed both an id and a name, we have an
362 * internal programming error.
364 if (!ossl_assert(id == 0 || name == NULL)) {
365 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
370 id = ossl_namemap_name2num(namemap, name);
373 * If we haven't found the name yet, chances are that the algorithm to
374 * be fetched is unsupported.
380 || !ossl_method_store_cache_get(store, NULL, id, properties, &method)) {
381 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
382 get_tmp_encoder_store,
383 get_encoder_from_store,
384 put_encoder_in_store,
390 methdata->names = name;
391 methdata->propquery = properties;
392 methdata->flag_construct_error_occurred = 0;
393 if ((method = ossl_method_construct(methdata->libctx, OSSL_OP_ENCODER,
394 NULL, 0 /* !force_cache */,
395 &mcm, methdata)) != NULL) {
397 * If construction did create a method for us, we know that
398 * there is a correct name_id and meth_id, since those have
399 * already been calculated in get_encoder_from_store() and
400 * put_encoder_in_store() above.
403 id = ossl_namemap_name2num(namemap, name);
404 ossl_method_store_cache_set(store, NULL, id, properties, method,
405 up_ref_encoder, free_encoder);
409 * If we never were in the constructor, the algorithm to be fetched
412 unsupported = !methdata->flag_construct_error_occurred;
415 if ((id != 0 || name != NULL) && method == NULL) {
416 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
419 name = ossl_namemap_num2name(namemap, id, 0);
420 ERR_raise_data(ERR_LIB_OSSL_ENCODER, code,
421 "%s, Name (%s : %d), Properties (%s)",
422 ossl_lib_ctx_get_descriptor(methdata->libctx),
423 name = NULL ? "<null>" : name, id,
424 properties == NULL ? "<null>" : properties);
430 OSSL_ENCODER *OSSL_ENCODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
431 const char *properties)
433 struct encoder_data_st methdata;
436 methdata.libctx = libctx;
437 methdata.tmp_store = NULL;
438 method = inner_ossl_encoder_fetch(&methdata, 0, name, properties);
439 dealloc_tmp_encoder_store(methdata.tmp_store);
443 OSSL_ENCODER *ossl_encoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
444 const char *properties)
446 struct encoder_data_st methdata;
449 methdata.libctx = libctx;
450 methdata.tmp_store = NULL;
451 method = inner_ossl_encoder_fetch(&methdata, id, NULL, properties);
452 dealloc_tmp_encoder_store(methdata.tmp_store);
457 * Library of basic method functions
460 const OSSL_PROVIDER *OSSL_ENCODER_get0_provider(const OSSL_ENCODER *encoder)
462 if (!ossl_assert(encoder != NULL)) {
463 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
467 return encoder->base.prov;
470 const char *OSSL_ENCODER_get0_properties(const OSSL_ENCODER *encoder)
472 if (!ossl_assert(encoder != NULL)) {
473 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
477 return encoder->base.algodef->property_definition;
480 const OSSL_PROPERTY_LIST *
481 ossl_encoder_parsed_properties(const OSSL_ENCODER *encoder)
483 if (!ossl_assert(encoder != NULL)) {
484 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
488 return encoder->base.parsed_propdef;
491 int ossl_encoder_get_number(const OSSL_ENCODER *encoder)
493 if (!ossl_assert(encoder != NULL)) {
494 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
498 return encoder->base.id;
501 const char *OSSL_ENCODER_get0_name(const OSSL_ENCODER *encoder)
503 return encoder->base.name;
506 const char *OSSL_ENCODER_get0_description(const OSSL_ENCODER *encoder)
508 return encoder->base.algodef->algorithm_description;
511 int OSSL_ENCODER_is_a(const OSSL_ENCODER *encoder, const char *name)
513 if (encoder->base.prov != NULL) {
514 OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov);
515 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
517 return ossl_namemap_name2num(namemap, name) == encoder->base.id;
522 struct do_one_data_st {
523 void (*user_fn)(OSSL_ENCODER *encoder, void *arg);
527 static void do_one(ossl_unused int id, void *method, void *arg)
529 struct do_one_data_st *data = arg;
531 data->user_fn(method, data->user_arg);
534 void OSSL_ENCODER_do_all_provided(OSSL_LIB_CTX *libctx,
535 void (*user_fn)(OSSL_ENCODER *encoder,
539 struct encoder_data_st methdata;
540 struct do_one_data_st data;
542 methdata.libctx = libctx;
543 methdata.tmp_store = NULL;
544 (void)inner_ossl_encoder_fetch(&methdata, 0, NULL, NULL /* properties */);
546 data.user_fn = user_fn;
547 data.user_arg = user_arg;
548 if (methdata.tmp_store != NULL)
549 ossl_method_store_do_all(methdata.tmp_store, &do_one, &data);
550 ossl_method_store_do_all(get_encoder_store(libctx), &do_one, &data);
551 dealloc_tmp_encoder_store(methdata.tmp_store);
554 int OSSL_ENCODER_names_do_all(const OSSL_ENCODER *encoder,
555 void (*fn)(const char *name, void *data),
561 if (encoder->base.prov != NULL) {
562 OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov);
563 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
565 return ossl_namemap_doall_names(namemap, encoder->base.id, fn, data);
572 OSSL_ENCODER_gettable_params(OSSL_ENCODER *encoder)
574 if (encoder != NULL && encoder->gettable_params != NULL) {
575 void *provctx = ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder));
577 return encoder->gettable_params(provctx);
582 int OSSL_ENCODER_get_params(OSSL_ENCODER *encoder, OSSL_PARAM params[])
584 if (encoder != NULL && encoder->get_params != NULL)
585 return encoder->get_params(params);
589 const OSSL_PARAM *OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER *encoder)
591 if (encoder != NULL && encoder->settable_ctx_params != NULL) {
592 void *provctx = ossl_provider_ctx(OSSL_ENCODER_get0_provider(encoder));
594 return encoder->settable_ctx_params(provctx);
600 * Encoder context support
603 OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new(void)
605 OSSL_ENCODER_CTX *ctx;
607 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
608 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
613 int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX *ctx,
614 const OSSL_PARAM params[])
620 if (!ossl_assert(ctx != NULL)) {
621 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
625 if (ctx->encoder_insts == NULL)
628 l = OSSL_ENCODER_CTX_get_num_encoders(ctx);
629 for (i = 0; i < l; i++) {
630 OSSL_ENCODER_INSTANCE *encoder_inst =
631 sk_OSSL_ENCODER_INSTANCE_value(ctx->encoder_insts, i);
632 OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
633 void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst);
635 if (encoderctx == NULL || encoder->set_ctx_params == NULL)
637 if (!encoder->set_ctx_params(encoderctx, params))
643 void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX *ctx)
646 sk_OSSL_ENCODER_INSTANCE_pop_free(ctx->encoder_insts,
647 ossl_encoder_instance_free);
648 OPENSSL_free(ctx->construct_data);
649 ossl_pw_clear_passphrase_data(&ctx->pwdata);