]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/encode_decode/decoder_meth.c
37c6ab2b57540381c581e14afc1568b09690577d
[thirdparty/openssl.git] / crypto / encode_decode / decoder_meth.c
1 /*
2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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/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"
20
21 /*
22 * Decoder can have multiple names, separated with colons in a name string
23 */
24 #define NAME_SEPARATOR ':'
25
26 /* Simple method structure constructor and destructor */
27 static OSSL_DECODER *ossl_decoder_new(void)
28 {
29 OSSL_DECODER *decoder = NULL;
30
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);
35 return NULL;
36 }
37
38 decoder->base.refcnt = 1;
39
40 return decoder;
41 }
42
43 int OSSL_DECODER_up_ref(OSSL_DECODER *decoder)
44 {
45 int ref = 0;
46
47 CRYPTO_UP_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
48 return 1;
49 }
50
51 void OSSL_DECODER_free(OSSL_DECODER *decoder)
52 {
53 int ref = 0;
54
55 if (decoder == NULL)
56 return;
57
58 CRYPTO_DOWN_REF(&decoder->base.refcnt, &ref, decoder->base.lock);
59 if (ref > 0)
60 return;
61 ossl_provider_free(decoder->base.prov);
62 CRYPTO_THREAD_lock_free(decoder->base.lock);
63 OPENSSL_free(decoder);
64 }
65
66 /* Permanent decoder method store, constructor and destructor */
67 static void decoder_store_free(void *vstore)
68 {
69 ossl_method_store_free(vstore);
70 }
71
72 static void *decoder_store_new(OPENSSL_CTX *ctx)
73 {
74 return ossl_method_store_new(ctx);
75 }
76
77
78 static const OPENSSL_CTX_METHOD decoder_store_method = {
79 decoder_store_new,
80 decoder_store_free,
81 };
82
83 /* Data to be passed through ossl_method_construct() */
84 struct decoder_data_st {
85 OPENSSL_CTX *libctx;
86 OSSL_METHOD_CONSTRUCT_METHOD *mcm;
87 int id; /* For get_decoder_from_store() */
88 const char *names; /* For get_decoder_from_store() */
89 const char *propquery; /* For get_decoder_from_store() */
90 };
91
92 /*
93 * Generic routines to fetch / create DECODER methods with
94 * ossl_method_construct()
95 */
96
97 /* Temporary decoder method store, constructor and destructor */
98 static void *alloc_tmp_decoder_store(OPENSSL_CTX *ctx)
99 {
100 return ossl_method_store_new(ctx);
101 }
102
103 static void dealloc_tmp_decoder_store(void *store)
104 {
105 if (store != NULL)
106 ossl_method_store_free(store);
107 }
108
109 /* Get the permanent decoder store */
110 static OSSL_METHOD_STORE *get_decoder_store(OPENSSL_CTX *libctx)
111 {
112 return openssl_ctx_get_data(libctx, OPENSSL_CTX_DECODER_STORE_INDEX,
113 &decoder_store_method);
114 }
115
116 /* Get decoder methods from a store, or put one in */
117 static void *get_decoder_from_store(OPENSSL_CTX *libctx, void *store,
118 void *data)
119 {
120 struct decoder_data_st *methdata = data;
121 void *method = NULL;
122 int id;
123
124 if ((id = methdata->id) == 0) {
125 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
126
127 id = ossl_namemap_name2num(namemap, methdata->names);
128 }
129
130 if (store == NULL
131 && (store = get_decoder_store(libctx)) == NULL)
132 return NULL;
133
134 if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
135 return NULL;
136 return method;
137 }
138
139 static int put_decoder_in_store(OPENSSL_CTX *libctx, void *store,
140 void *method, const OSSL_PROVIDER *prov,
141 int operation_id, const char *names,
142 const char *propdef, void *unused)
143 {
144 OSSL_NAMEMAP *namemap;
145 int id;
146
147 if ((namemap = ossl_namemap_stored(libctx)) == NULL
148 || (id = ossl_namemap_name2num(namemap, names)) == 0)
149 return 0;
150
151 if (store == NULL && (store = get_decoder_store(libctx)) == NULL)
152 return 0;
153
154 return ossl_method_store_add(store, prov, id, propdef, method,
155 (int (*)(void *))OSSL_DECODER_up_ref,
156 (void (*)(void *))OSSL_DECODER_free);
157 }
158
159 /* Create and populate a decoder method */
160 void *ossl_decoder_from_dispatch(int id, const OSSL_ALGORITHM *algodef,
161 OSSL_PROVIDER *prov)
162 {
163 OSSL_DECODER *decoder = NULL;
164 const OSSL_DISPATCH *fns = algodef->implementation;
165
166 if ((decoder = ossl_decoder_new()) == NULL)
167 return NULL;
168 decoder->base.id = id;
169 decoder->base.propdef = algodef->property_definition;
170
171 for (; fns->function_id != 0; fns++) {
172 switch (fns->function_id) {
173 case OSSL_FUNC_DECODER_NEWCTX:
174 if (decoder->newctx == NULL)
175 decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
176 break;
177 case OSSL_FUNC_DECODER_FREECTX:
178 if (decoder->freectx == NULL)
179 decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
180 break;
181 case OSSL_FUNC_DECODER_GET_PARAMS:
182 if (decoder->get_params == NULL)
183 decoder->get_params =
184 OSSL_FUNC_decoder_get_params(fns);
185 break;
186 case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
187 if (decoder->gettable_params == NULL)
188 decoder->gettable_params =
189 OSSL_FUNC_decoder_gettable_params(fns);
190 break;
191 case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
192 if (decoder->set_ctx_params == NULL)
193 decoder->set_ctx_params =
194 OSSL_FUNC_decoder_set_ctx_params(fns);
195 break;
196 case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
197 if (decoder->settable_ctx_params == NULL)
198 decoder->settable_ctx_params =
199 OSSL_FUNC_decoder_settable_ctx_params(fns);
200 break;
201 case OSSL_FUNC_DECODER_DECODE:
202 if (decoder->decode == NULL)
203 decoder->decode = OSSL_FUNC_decoder_decode(fns);
204 break;
205 case OSSL_FUNC_DECODER_EXPORT_OBJECT:
206 if (decoder->export_object == NULL)
207 decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
208 break;
209 }
210 }
211 /*
212 * Try to check that the method is sensible.
213 * If you have a constructor, you must have a destructor and vice versa.
214 * You must have at least one of the encoding driver functions.
215 */
216 if (!((decoder->newctx == NULL && decoder->freectx == NULL)
217 || (decoder->newctx != NULL && decoder->freectx != NULL))
218 || decoder->decode == NULL) {
219 OSSL_DECODER_free(decoder);
220 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
221 return NULL;
222 }
223
224 if (prov != NULL && !ossl_provider_up_ref(prov)) {
225 OSSL_DECODER_free(decoder);
226 return NULL;
227 }
228
229 decoder->base.prov = prov;
230 return decoder;
231 }
232
233
234 /*
235 * The core fetching functionality passes the names of the implementation.
236 * This function is responsible to getting an identity number for them,
237 * then call ossl_decoder_from_dispatch() with that identity number.
238 */
239 static void *construct_decoder(const OSSL_ALGORITHM *algodef,
240 OSSL_PROVIDER *prov, void *unused)
241 {
242 /*
243 * This function is only called if get_decoder_from_store() returned
244 * NULL, so it's safe to say that of all the spots to create a new
245 * namemap entry, this is it. Should the name already exist there, we
246 * know that ossl_namemap_add() will return its corresponding number.
247 */
248 OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
249 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
250 const char *names = algodef->algorithm_names;
251 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
252 void *method = NULL;
253
254 if (id != 0)
255 method = ossl_decoder_from_dispatch(id, algodef, prov);
256
257 return method;
258 }
259
260 /* Intermediary function to avoid ugly casts, used below */
261 static void destruct_decoder(void *method, void *data)
262 {
263 OSSL_DECODER_free(method);
264 }
265
266 static int up_ref_decoder(void *method)
267 {
268 return OSSL_DECODER_up_ref(method);
269 }
270
271 static void free_decoder(void *method)
272 {
273 OSSL_DECODER_free(method);
274 }
275
276 /* Fetching support. Can fetch by numeric identity or by name */
277 static OSSL_DECODER *inner_ossl_decoder_fetch(OPENSSL_CTX *libctx, int id,
278 const char *name,
279 const char *properties)
280 {
281 OSSL_METHOD_STORE *store = get_decoder_store(libctx);
282 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
283 void *method = NULL;
284
285 if (store == NULL || namemap == NULL)
286 return NULL;
287
288 /*
289 * If we have been passed neither a name_id or a name, we have an
290 * internal programming error.
291 */
292 if (!ossl_assert(id != 0 || name != NULL))
293 return NULL;
294
295 if (id == 0)
296 id = ossl_namemap_name2num(namemap, name);
297
298 if (id == 0
299 || !ossl_method_store_cache_get(store, id, properties, &method)) {
300 OSSL_METHOD_CONSTRUCT_METHOD mcm = {
301 alloc_tmp_decoder_store,
302 dealloc_tmp_decoder_store,
303 get_decoder_from_store,
304 put_decoder_in_store,
305 construct_decoder,
306 destruct_decoder
307 };
308 struct decoder_data_st mcmdata;
309
310 mcmdata.libctx = libctx;
311 mcmdata.mcm = &mcm;
312 mcmdata.id = id;
313 mcmdata.names = name;
314 mcmdata.propquery = properties;
315 if ((method = ossl_method_construct(libctx, OSSL_OP_DECODER,
316 0 /* !force_cache */,
317 &mcm, &mcmdata)) != NULL) {
318 /*
319 * If construction did create a method for us, we know that
320 * there is a correct name_id and meth_id, since those have
321 * already been calculated in get_decoder_from_store() and
322 * put_decoder_in_store() above.
323 */
324 if (id == 0)
325 id = ossl_namemap_name2num(namemap, name);
326 ossl_method_store_cache_set(store, id, properties, method,
327 up_ref_decoder, free_decoder);
328 }
329 }
330
331 return method;
332 }
333
334 OSSL_DECODER *OSSL_DECODER_fetch(OPENSSL_CTX *libctx, const char *name,
335 const char *properties)
336 {
337 return inner_ossl_decoder_fetch(libctx, 0, name, properties);
338 }
339
340 OSSL_DECODER *ossl_decoder_fetch_by_number(OPENSSL_CTX *libctx, int id,
341 const char *properties)
342 {
343 return inner_ossl_decoder_fetch(libctx, id, NULL, properties);
344 }
345
346 /*
347 * Library of basic method functions
348 */
349
350 const OSSL_PROVIDER *OSSL_DECODER_provider(const OSSL_DECODER *decoder)
351 {
352 if (!ossl_assert(decoder != NULL)) {
353 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
354 return 0;
355 }
356
357 return decoder->base.prov;
358 }
359
360 const char *OSSL_DECODER_properties(const OSSL_DECODER *decoder)
361 {
362 if (!ossl_assert(decoder != NULL)) {
363 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
364 return 0;
365 }
366
367 return decoder->base.propdef;
368 }
369
370 int OSSL_DECODER_number(const OSSL_DECODER *decoder)
371 {
372 if (!ossl_assert(decoder != NULL)) {
373 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
374 return 0;
375 }
376
377 return decoder->base.id;
378 }
379
380 int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
381 {
382 if (decoder->base.prov != NULL) {
383 OPENSSL_CTX *libctx = ossl_provider_library_context(decoder->base.prov);
384 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
385
386 return ossl_namemap_name2num(namemap, name) == decoder->base.id;
387 }
388 return 0;
389 }
390
391 struct decoder_do_all_data_st {
392 void (*user_fn)(void *method, void *arg);
393 void *user_arg;
394 };
395
396 static void decoder_do_one(OSSL_PROVIDER *provider,
397 const OSSL_ALGORITHM *algodef,
398 int no_store, void *vdata)
399 {
400 struct decoder_do_all_data_st *data = vdata;
401 OPENSSL_CTX *libctx = ossl_provider_library_context(provider);
402 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
403 const char *names = algodef->algorithm_names;
404 int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
405 void *method = NULL;
406
407 if (id != 0)
408 method = ossl_decoder_from_dispatch(id, algodef, provider);
409
410 if (method != NULL) {
411 data->user_fn(method, data->user_arg);
412 OSSL_DECODER_free(method);
413 }
414 }
415
416 void OSSL_DECODER_do_all_provided(OPENSSL_CTX *libctx,
417 void (*fn)(OSSL_DECODER *decoder, void *arg),
418 void *arg)
419 {
420 struct decoder_do_all_data_st data;
421
422 data.user_fn = (void (*)(void *, void *))fn;
423 data.user_arg = arg;
424 ossl_algorithm_do_all(libctx, OSSL_OP_DECODER, NULL,
425 NULL, decoder_do_one, NULL,
426 &data);
427 }
428
429 void OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
430 void (*fn)(const char *name, void *data),
431 void *data)
432 {
433 if (decoder == NULL)
434 return;
435
436 if (decoder->base.prov != NULL) {
437 OPENSSL_CTX *libctx = ossl_provider_library_context(decoder->base.prov);
438 OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
439
440 ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
441 }
442 }
443
444 const OSSL_PARAM *
445 OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
446 {
447 if (decoder != NULL && decoder->gettable_params != NULL) {
448 void *provctx = ossl_provider_ctx(OSSL_DECODER_provider(decoder));
449
450 return decoder->gettable_params(provctx);
451 }
452 return NULL;
453 }
454
455 int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
456 {
457 if (decoder != NULL && decoder->get_params != NULL)
458 return decoder->get_params(params);
459 return 0;
460 }
461
462 const OSSL_PARAM *
463 OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
464 {
465 if (decoder != NULL && decoder->settable_ctx_params != NULL) {
466 void *provctx = ossl_provider_ctx(OSSL_DECODER_provider(decoder));
467
468 return decoder->settable_ctx_params(provctx);
469 }
470 return NULL;
471 }
472
473 /*
474 * Decoder context support
475 */
476
477 /*
478 * |encoder| value NULL is valid, and signifies that there is no decoder.
479 * This is useful to provide fallback mechanisms.
480 * Functions that want to verify if there is a decoder can do so with
481 * OSSL_DECODER_CTX_get_decoder()
482 */
483 OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
484 {
485 OSSL_DECODER_CTX *ctx;
486
487 if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
488 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
489
490 return ctx;
491 }
492
493 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
494 const OSSL_PARAM params[])
495 {
496 size_t i;
497 size_t l;
498
499 if (!ossl_assert(ctx != NULL)) {
500 ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
501 return 0;
502 }
503
504 if (ctx->decoder_insts == NULL)
505 return 1;
506
507 l = OSSL_DECODER_CTX_get_num_decoders(ctx);
508 for (i = 0; i < l; i++) {
509 OSSL_DECODER_INSTANCE *decoder_inst =
510 sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
511 OSSL_DECODER *decoder =
512 OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
513 OSSL_DECODER *decoderctx =
514 OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
515
516 if (decoderctx == NULL || decoder->set_ctx_params == NULL)
517 continue;
518 if (!decoder->set_ctx_params(decoderctx, params))
519 return 0;
520 }
521 return 1;
522 }
523
524 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
525 {
526 if (ctx != NULL) {
527 if (ctx->cleanup != NULL)
528 ctx->cleanup(ctx->construct_data);
529 sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
530 ossl_decoder_instance_free);
531 ossl_pw_clear_passphrase_data(&ctx->pwdata);
532 OPENSSL_free(ctx);
533 }
534 }