]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/encode_decode/encoder_meth.c
Don't hold a lock when calling a callback in ossl_namemap_doall_names
[thirdparty/openssl.git] / crypto / encode_decode / encoder_meth.c
CommitLineData
ece9304c 1/*
4333b89f 2 * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
ece9304c
RL
3 *
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
8 */
9
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
21/*
22 * Encoder can have multiple names, separated with colons in a name string
23 */
24#define NAME_SEPARATOR ':'
25
26/* Simple method structure constructor and destructor */
27static OSSL_ENCODER *ossl_encoder_new(void)
28{
29 OSSL_ENCODER *encoder = NULL;
30
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);
35 return NULL;
36 }
37
38 encoder->base.refcnt = 1;
39
40 return encoder;
41}
42
43int OSSL_ENCODER_up_ref(OSSL_ENCODER *encoder)
44{
45 int ref = 0;
46
47 CRYPTO_UP_REF(&encoder->base.refcnt, &ref, encoder->base.lock);
48 return 1;
49}
50
51void OSSL_ENCODER_free(OSSL_ENCODER *encoder)
52{
53 int ref = 0;
54
55 if (encoder == NULL)
56 return;
57
58 CRYPTO_DOWN_REF(&encoder->base.refcnt, &ref, encoder->base.lock);
59 if (ref > 0)
60 return;
61 ossl_provider_free(encoder->base.prov);
62 CRYPTO_THREAD_lock_free(encoder->base.lock);
63 OPENSSL_free(encoder);
64}
65
66/* Permanent encoder method store, constructor and destructor */
67static void encoder_store_free(void *vstore)
68{
69 ossl_method_store_free(vstore);
70}
71
b4250010 72static void *encoder_store_new(OSSL_LIB_CTX *ctx)
ece9304c
RL
73{
74 return ossl_method_store_new(ctx);
75}
76
77
b4250010 78static const OSSL_LIB_CTX_METHOD encoder_store_method = {
ece9304c
RL
79 encoder_store_new,
80 encoder_store_free,
81};
82
83/* Data to be passed through ossl_method_construct() */
84struct encoder_data_st {
b4250010 85 OSSL_LIB_CTX *libctx;
ece9304c
RL
86 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
87 int id; /* For get_encoder_from_store() */
88 const char *names; /* For get_encoder_from_store() */
89 const char *propquery; /* For get_encoder_from_store() */
d6d42cda
RL
90
91 unsigned int flag_construct_error_occured : 1;
ece9304c
RL
92};
93
94/*
95 * Generic routines to fetch / create ENCODER methods with
96 * ossl_method_construct()
97 */
98
99/* Temporary encoder method store, constructor and destructor */
b4250010 100static void *alloc_tmp_encoder_store(OSSL_LIB_CTX *ctx)
ece9304c
RL
101{
102 return ossl_method_store_new(ctx);
103}
104
105static void dealloc_tmp_encoder_store(void *store)
106{
107 if (store != NULL)
108 ossl_method_store_free(store);
109}
110
111/* Get the permanent encoder store */
b4250010 112static OSSL_METHOD_STORE *get_encoder_store(OSSL_LIB_CTX *libctx)
ece9304c 113{
b4250010
DMSP
114 return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_ENCODER_STORE_INDEX,
115 &encoder_store_method);
ece9304c
RL
116}
117
118/* Get encoder methods from a store, or put one in */
b4250010 119static void *get_encoder_from_store(OSSL_LIB_CTX *libctx, void *store,
ece9304c
RL
120 void *data)
121{
122 struct encoder_data_st *methdata = data;
123 void *method = NULL;
124 int id;
125
126 if ((id = methdata->id) == 0) {
127 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
128
129 id = ossl_namemap_name2num(namemap, methdata->names);
130 }
131
132 if (store == NULL
133 && (store = get_encoder_store(libctx)) == NULL)
134 return NULL;
135
136 if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
137 return NULL;
138 return method;
139}
140
b4250010 141static int put_encoder_in_store(OSSL_LIB_CTX *libctx, void *store,
ece9304c
RL
142 void *method, const OSSL_PROVIDER *prov,
143 int operation_id, const char *names,
144 const char *propdef, void *unused)
145{
146 OSSL_NAMEMAP *namemap;
147 int id;
148
149 if ((namemap = ossl_namemap_stored(libctx)) == NULL
150 || (id = ossl_namemap_name2num(namemap, names)) == 0)
151 return 0;
152
153 if (store == NULL && (store = get_encoder_store(libctx)) == NULL)
154 return 0;
155
156 return ossl_method_store_add(store, prov, id, propdef, method,
157 (int (*)(void *))OSSL_ENCODER_up_ref,
158 (void (*)(void *))OSSL_ENCODER_free);
159}
160
161/* Create and populate a encoder method */
162static void *encoder_from_dispatch(int id, const OSSL_ALGORITHM *algodef,
163 OSSL_PROVIDER *prov)
164{
165 OSSL_ENCODER *encoder = NULL;
166 const OSSL_DISPATCH *fns = algodef->implementation;
167
168 if ((encoder = ossl_encoder_new()) == NULL)
169 return NULL;
170 encoder->base.id = id;
171 encoder->base.propdef = algodef->property_definition;
172
173 for (; fns->function_id != 0; fns++) {
174 switch (fns->function_id) {
175 case OSSL_FUNC_ENCODER_NEWCTX:
176 if (encoder->newctx == NULL)
177 encoder->newctx =
178 OSSL_FUNC_encoder_newctx(fns);
179 break;
180 case OSSL_FUNC_ENCODER_FREECTX:
181 if (encoder->freectx == NULL)
182 encoder->freectx =
183 OSSL_FUNC_encoder_freectx(fns);
184 break;
b8975c68
RL
185 case OSSL_FUNC_ENCODER_GET_PARAMS:
186 if (encoder->get_params == NULL)
187 encoder->get_params =
188 OSSL_FUNC_encoder_get_params(fns);
189 break;
190 case OSSL_FUNC_ENCODER_GETTABLE_PARAMS:
191 if (encoder->gettable_params == NULL)
192 encoder->gettable_params =
193 OSSL_FUNC_encoder_gettable_params(fns);
194 break;
ece9304c
RL
195 case OSSL_FUNC_ENCODER_SET_CTX_PARAMS:
196 if (encoder->set_ctx_params == NULL)
197 encoder->set_ctx_params =
198 OSSL_FUNC_encoder_set_ctx_params(fns);
199 break;
200 case OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS:
201 if (encoder->settable_ctx_params == NULL)
202 encoder->settable_ctx_params =
203 OSSL_FUNC_encoder_settable_ctx_params(fns);
204 break;
cd861ab7
RL
205 case OSSL_FUNC_ENCODER_DOES_SELECTION:
206 if (encoder->does_selection == NULL)
207 encoder->does_selection =
208 OSSL_FUNC_encoder_does_selection(fns);
209 break;
b8975c68
RL
210 case OSSL_FUNC_ENCODER_ENCODE:
211 if (encoder->encode == NULL)
212 encoder->encode = OSSL_FUNC_encoder_encode(fns);
213 break;
214 case OSSL_FUNC_ENCODER_IMPORT_OBJECT:
215 if (encoder->import_object == NULL)
216 encoder->import_object =
217 OSSL_FUNC_encoder_import_object(fns);
ece9304c 218 break;
b8975c68
RL
219 case OSSL_FUNC_ENCODER_FREE_OBJECT:
220 if (encoder->free_object == NULL)
221 encoder->free_object =
222 OSSL_FUNC_encoder_free_object(fns);
ece9304c
RL
223 break;
224 }
225 }
226 /*
227 * Try to check that the method is sensible.
228 * If you have a constructor, you must have a destructor and vice versa.
b8975c68 229 * You must have the encoding driver functions.
ece9304c
RL
230 */
231 if (!((encoder->newctx == NULL && encoder->freectx == NULL)
b8975c68
RL
232 || (encoder->newctx != NULL && encoder->freectx != NULL)
233 || (encoder->import_object != NULL && encoder->free_object != NULL)
234 || (encoder->import_object == NULL && encoder->free_object == NULL))
235 || encoder->encode == NULL
236 || encoder->gettable_params == NULL
237 || encoder->get_params == NULL) {
ece9304c
RL
238 OSSL_ENCODER_free(encoder);
239 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
240 return NULL;
241 }
242
243 if (prov != NULL && !ossl_provider_up_ref(prov)) {
244 OSSL_ENCODER_free(encoder);
245 return NULL;
246 }
247
248 encoder->base.prov = prov;
249 return encoder;
250}
251
252
253/*
254 * The core fetching functionality passes the names of the implementation.
255 * This function is responsible to getting an identity number for them,
256 * then call encoder_from_dispatch() with that identity number.
257 */
258static void *construct_encoder(const OSSL_ALGORITHM *algodef,
d6d42cda 259 OSSL_PROVIDER *prov, void *data)
ece9304c
RL
260{
261 /*
262 * This function is only called if get_encoder_from_store() returned
263 * NULL, so it's safe to say that of all the spots to create a new
264 * namemap entry, this is it. Should the name already exist there, we
265 * know that ossl_namemap_add() will return its corresponding number.
266 */
d6d42cda 267 struct encoder_data_st *methdata = data;
a829b735 268 OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
ece9304c
RL
269 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
270 const char *names = algodef->algorithm_names;
271 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
272 void *method = NULL;
273
274 if (id != 0)
275 method = encoder_from_dispatch(id, algodef, prov);
276
d6d42cda
RL
277 /*
278 * Flag to indicate that there was actual construction errors. This
279 * helps inner_evp_generic_fetch() determine what error it should
280 * record on inaccessible algorithms.
281 */
282 if (method == NULL)
283 methdata->flag_construct_error_occured = 1;
284
ece9304c
RL
285 return method;
286}
287
288/* Intermediary function to avoid ugly casts, used below */
289static void destruct_encoder(void *method, void *data)
290{
291 OSSL_ENCODER_free(method);
292}
293
294static int up_ref_encoder(void *method)
295{
296 return OSSL_ENCODER_up_ref(method);
297}
298
299static void free_encoder(void *method)
300{
301 OSSL_ENCODER_free(method);
302}
303
304/* Fetching support. Can fetch by numeric identity or by name */
b4250010 305static OSSL_ENCODER *inner_ossl_encoder_fetch(OSSL_LIB_CTX *libctx,
ece9304c
RL
306 int id, const char *name,
307 const char *properties)
308{
309 OSSL_METHOD_STORE *store = get_encoder_store(libctx);
310 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
311 void *method = NULL;
d6d42cda 312 int unsupported = 0;
ece9304c 313
d6d42cda
RL
314 if (store == NULL || namemap == NULL) {
315 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_INVALID_ARGUMENT);
ece9304c 316 return NULL;
d6d42cda 317 }
ece9304c
RL
318
319 /*
320 * If we have been passed neither a name_id or a name, we have an
321 * internal programming error.
322 */
d6d42cda
RL
323 if (!ossl_assert(id != 0 || name != NULL)) {
324 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INTERNAL_ERROR);
ece9304c 325 return NULL;
d6d42cda 326 }
ece9304c
RL
327
328 if (id == 0)
329 id = ossl_namemap_name2num(namemap, name);
330
d6d42cda
RL
331 /*
332 * If we haven't found the name yet, chances are that the algorithm to
333 * be fetched is unsupported.
334 */
335 if (id == 0)
336 unsupported = 1;
337
ece9304c
RL
338 if (id == 0
339 || !ossl_method_store_cache_get(store, id, properties, &method)) {
340 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
341 alloc_tmp_encoder_store,
342 dealloc_tmp_encoder_store,
343 get_encoder_from_store,
344 put_encoder_in_store,
345 construct_encoder,
346 destruct_encoder
347 };
348 struct encoder_data_st mcmdata;
349
350 mcmdata.libctx = libctx;
351 mcmdata.mcm = &mcm;
352 mcmdata.id = id;
353 mcmdata.names = name;
354 mcmdata.propquery = properties;
d6d42cda 355 mcmdata.flag_construct_error_occured = 0;
ece9304c
RL
356 if ((method = ossl_method_construct(libctx, OSSL_OP_ENCODER,
357 0 /* !force_cache */,
358 &mcm, &mcmdata)) != NULL) {
359 /*
360 * If construction did create a method for us, we know that
361 * there is a correct name_id and meth_id, since those have
362 * already been calculated in get_encoder_from_store() and
363 * put_encoder_in_store() above.
364 */
365 if (id == 0)
366 id = ossl_namemap_name2num(namemap, name);
367 ossl_method_store_cache_set(store, id, properties, method,
368 up_ref_encoder, free_encoder);
369 }
d6d42cda
RL
370
371 /*
372 * If we never were in the constructor, the algorithm to be fetched
373 * is unsupported.
374 */
375 unsupported = !mcmdata.flag_construct_error_occured;
376 }
377
378 if (method == NULL) {
379 int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
380
381 if (name == NULL)
382 name = ossl_namemap_num2name(namemap, id, 0);
383 ERR_raise_data(ERR_LIB_OSSL_ENCODER, code,
384 "%s, Name (%s : %d), Properties (%s)",
385 ossl_lib_ctx_get_descriptor(libctx),
386 name = NULL ? "<null>" : name, id,
387 properties == NULL ? "<null>" : properties);
ece9304c
RL
388 }
389
390 return method;
391}
392
b4250010 393OSSL_ENCODER *OSSL_ENCODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
ece9304c
RL
394 const char *properties)
395{
396 return inner_ossl_encoder_fetch(libctx, 0, name, properties);
397}
398
b4250010 399OSSL_ENCODER *ossl_encoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
ece9304c
RL
400 const char *properties)
401{
402 return inner_ossl_encoder_fetch(libctx, id, NULL, properties);
403}
404
405/*
406 * Library of basic method functions
407 */
408
409const OSSL_PROVIDER *OSSL_ENCODER_provider(const OSSL_ENCODER *encoder)
410{
411 if (!ossl_assert(encoder != NULL)) {
412 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
413 return 0;
414 }
415
416 return encoder->base.prov;
417}
418
419const char *OSSL_ENCODER_properties(const OSSL_ENCODER *encoder)
420{
421 if (!ossl_assert(encoder != NULL)) {
422 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
423 return 0;
424 }
425
426 return encoder->base.propdef;
427}
428
429int OSSL_ENCODER_number(const OSSL_ENCODER *encoder)
430{
431 if (!ossl_assert(encoder != NULL)) {
432 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
433 return 0;
434 }
435
436 return encoder->base.id;
437}
438
439int OSSL_ENCODER_is_a(const OSSL_ENCODER *encoder, const char *name)
440{
441 if (encoder->base.prov != NULL) {
a829b735 442 OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov);
ece9304c
RL
443 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
444
445 return ossl_namemap_name2num(namemap, name) == encoder->base.id;
446 }
447 return 0;
448}
449
450struct encoder_do_all_data_st {
451 void (*user_fn)(void *method, void *arg);
452 void *user_arg;
453};
454
455static void encoder_do_one(OSSL_PROVIDER *provider,
456 const OSSL_ALGORITHM *algodef,
457 int no_store, void *vdata)
458{
459 struct encoder_do_all_data_st *data = vdata;
a829b735 460 OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
ece9304c
RL
461 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
462 const char *names = algodef->algorithm_names;
463 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
464 void *method = NULL;
465
466 if (id != 0)
467 method =
468 encoder_from_dispatch(id, algodef, provider);
469
470 if (method != NULL) {
471 data->user_fn(method, data->user_arg);
472 OSSL_ENCODER_free(method);
473 }
474}
475
b4250010 476void OSSL_ENCODER_do_all_provided(OSSL_LIB_CTX *libctx,
ece9304c
RL
477 void (*fn)(OSSL_ENCODER *encoder, void *arg),
478 void *arg)
479{
480 struct encoder_do_all_data_st data;
481
482 data.user_fn = (void (*)(void *, void *))fn;
483 data.user_arg = arg;
484
485 /*
486 * No pre- or post-condition for this call, as this only creates methods
487 * temporarly and then promptly destroys them.
488 */
489 ossl_algorithm_do_all(libctx, OSSL_OP_ENCODER, NULL, NULL,
490 encoder_do_one, NULL, &data);
491}
492
d84f5515
MC
493int OSSL_ENCODER_names_do_all(const OSSL_ENCODER *encoder,
494 void (*fn)(const char *name, void *data),
495 void *data)
ece9304c
RL
496{
497 if (encoder == NULL)
d84f5515 498 return 0;
ece9304c
RL
499
500 if (encoder->base.prov != NULL) {
a829b735 501 OSSL_LIB_CTX *libctx = ossl_provider_libctx(encoder->base.prov);
ece9304c
RL
502 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
503
d84f5515 504 return ossl_namemap_doall_names(namemap, encoder->base.id, fn, data);
ece9304c 505 }
d84f5515
MC
506
507 return 1;
ece9304c
RL
508}
509
b8975c68
RL
510const OSSL_PARAM *
511OSSL_ENCODER_gettable_params(OSSL_ENCODER *encoder)
512{
513 if (encoder != NULL && encoder->gettable_params != NULL) {
514 void *provctx = ossl_provider_ctx(OSSL_ENCODER_provider(encoder));
515
516 return encoder->gettable_params(provctx);
517 }
518 return NULL;
519}
520
521int OSSL_ENCODER_get_params(OSSL_ENCODER *encoder, OSSL_PARAM params[])
522{
523 if (encoder != NULL && encoder->get_params != NULL)
524 return encoder->get_params(params);
525 return 0;
526}
527
ece9304c
RL
528const OSSL_PARAM *OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER *encoder)
529{
530 if (encoder != NULL && encoder->settable_ctx_params != NULL) {
531 void *provctx = ossl_provider_ctx(OSSL_ENCODER_provider(encoder));
532
533 return encoder->settable_ctx_params(provctx);
534 }
535 return NULL;
536}
537
538/*
539 * Encoder context support
540 */
541
b8975c68 542OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new(void)
ece9304c
RL
543{
544 OSSL_ENCODER_CTX *ctx;
545
b8975c68 546 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
ece9304c 547 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
ece9304c
RL
548
549 return ctx;
550}
551
b8975c68
RL
552int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX *ctx,
553 const OSSL_PARAM params[])
ece9304c 554{
9096809b 555 int ok = 1;
b8975c68
RL
556 size_t i;
557 size_t l;
558
ece9304c
RL
559 if (!ossl_assert(ctx != NULL)) {
560 ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
561 return 0;
562 }
563
b8975c68
RL
564 if (ctx->encoder_insts == NULL)
565 return 1;
ece9304c 566
b8975c68
RL
567 l = OSSL_ENCODER_CTX_get_num_encoders(ctx);
568 for (i = 0; i < l; i++) {
569 OSSL_ENCODER_INSTANCE *encoder_inst =
570 sk_OSSL_ENCODER_INSTANCE_value(ctx->encoder_insts, i);
571 OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
572 void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst);
ece9304c 573
b8975c68
RL
574 if (encoderctx == NULL || encoder->set_ctx_params == NULL)
575 continue;
576 if (!encoder->set_ctx_params(encoderctx, params))
9096809b 577 ok = 0;
ece9304c 578 }
9096809b 579 return ok;
ece9304c
RL
580}
581
582void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX *ctx)
583{
584 if (ctx != NULL) {
b8975c68
RL
585 sk_OSSL_ENCODER_INSTANCE_pop_free(ctx->encoder_insts,
586 ossl_encoder_instance_free);
587 OPENSSL_free(ctx->construct_data);
a517edec 588 ossl_pw_clear_passphrase_data(&ctx->pwdata);
ece9304c
RL
589 OPENSSL_free(ctx);
590 }
591}