DECODER: use property definitions instead of getting implementation parameters
[openssl.git] / crypto / encode_decode / decoder_meth.c
1 /*
2  * Copyright 2020-2021 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     OPENSSL_free(decoder->base.name);
62     ossl_property_free(decoder->base.parsed_propdef);
63     ossl_provider_free(decoder->base.prov);
64     CRYPTO_THREAD_lock_free(decoder->base.lock);
65     OPENSSL_free(decoder);
66 }
67
68 /* Permanent decoder method store, constructor and destructor */
69 static void decoder_store_free(void *vstore)
70 {
71     ossl_method_store_free(vstore);
72 }
73
74 static void *decoder_store_new(OSSL_LIB_CTX *ctx)
75 {
76     return ossl_method_store_new(ctx);
77 }
78
79
80 static const OSSL_LIB_CTX_METHOD decoder_store_method = {
81     OSSL_LIB_CTX_METHOD_DEFAULT_PRIORITY,
82     decoder_store_new,
83     decoder_store_free,
84 };
85
86 /* Data to be passed through ossl_method_construct() */
87 struct decoder_data_st {
88     OSSL_LIB_CTX *libctx;
89     OSSL_METHOD_CONSTRUCT_METHOD *mcm;
90     int id;                      /* For get_decoder_from_store() */
91     const char *names;           /* For get_decoder_from_store() */
92     const char *propquery;       /* For get_decoder_from_store() */
93
94     unsigned int flag_construct_error_occurred : 1;
95 };
96
97 /*
98  * Generic routines to fetch / create DECODER methods with
99  * ossl_method_construct()
100  */
101
102 /* Temporary decoder method store, constructor and destructor */
103 static void *alloc_tmp_decoder_store(OSSL_LIB_CTX *ctx)
104 {
105     return ossl_method_store_new(ctx);
106 }
107
108 static void dealloc_tmp_decoder_store(void *store)
109 {
110     if (store != NULL)
111         ossl_method_store_free(store);
112 }
113
114 /* Get the permanent decoder store */
115 static OSSL_METHOD_STORE *get_decoder_store(OSSL_LIB_CTX *libctx)
116 {
117     return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_DECODER_STORE_INDEX,
118                                  &decoder_store_method);
119 }
120
121 /* Get decoder methods from a store, or put one in */
122 static void *get_decoder_from_store(OSSL_LIB_CTX *libctx, void *store,
123                                     void *data)
124 {
125     struct decoder_data_st *methdata = data;
126     void *method = NULL;
127     int id;
128
129     if ((id = methdata->id) == 0) {
130         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
131
132         id = ossl_namemap_name2num(namemap, methdata->names);
133     }
134
135     if (store == NULL
136         && (store = get_decoder_store(libctx)) == NULL)
137         return NULL;
138
139     if (!ossl_method_store_fetch(store, id, methdata->propquery, &method))
140         return NULL;
141     return method;
142 }
143
144 static int put_decoder_in_store(OSSL_LIB_CTX *libctx, void *store,
145                                 void *method, const OSSL_PROVIDER *prov,
146                                 int operation_id, const char *names,
147                                 const char *propdef, void *unused)
148 {
149     OSSL_NAMEMAP *namemap;
150     int id;
151
152     if ((namemap = ossl_namemap_stored(libctx)) == NULL
153         || (id = ossl_namemap_name2num(namemap, names)) == 0)
154         return 0;
155
156     if (store == NULL && (store = get_decoder_store(libctx)) == NULL)
157         return 0;
158
159     return ossl_method_store_add(store, prov, id, propdef, method,
160                                  (int (*)(void *))OSSL_DECODER_up_ref,
161                                  (void (*)(void *))OSSL_DECODER_free);
162 }
163
164 /* Create and populate a decoder method */
165 void *ossl_decoder_from_algorithm(int id, const OSSL_ALGORITHM *algodef,
166                                   OSSL_PROVIDER *prov)
167 {
168     OSSL_DECODER *decoder = NULL;
169     const OSSL_DISPATCH *fns = algodef->implementation;
170     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
171
172     if ((decoder = ossl_decoder_new()) == NULL)
173         return NULL;
174     decoder->base.id = id;
175     if ((decoder->base.name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
176         OSSL_DECODER_free(decoder);
177         return NULL;
178     }
179     decoder->base.propdef = algodef->property_definition;
180     decoder->base.description = algodef->algorithm_description;
181     decoder->base.parsed_propdef
182         = ossl_parse_property(libctx, algodef->property_definition);
183
184     for (; fns->function_id != 0; fns++) {
185         switch (fns->function_id) {
186         case OSSL_FUNC_DECODER_NEWCTX:
187             if (decoder->newctx == NULL)
188                 decoder->newctx = OSSL_FUNC_decoder_newctx(fns);
189             break;
190         case OSSL_FUNC_DECODER_FREECTX:
191             if (decoder->freectx == NULL)
192                 decoder->freectx = OSSL_FUNC_decoder_freectx(fns);
193             break;
194         case OSSL_FUNC_DECODER_GET_PARAMS:
195             if (decoder->get_params == NULL)
196                 decoder->get_params =
197                     OSSL_FUNC_decoder_get_params(fns);
198             break;
199         case OSSL_FUNC_DECODER_GETTABLE_PARAMS:
200             if (decoder->gettable_params == NULL)
201                 decoder->gettable_params =
202                     OSSL_FUNC_decoder_gettable_params(fns);
203             break;
204         case OSSL_FUNC_DECODER_SET_CTX_PARAMS:
205             if (decoder->set_ctx_params == NULL)
206                 decoder->set_ctx_params =
207                     OSSL_FUNC_decoder_set_ctx_params(fns);
208             break;
209         case OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS:
210             if (decoder->settable_ctx_params == NULL)
211                 decoder->settable_ctx_params =
212                     OSSL_FUNC_decoder_settable_ctx_params(fns);
213             break;
214         case OSSL_FUNC_DECODER_DOES_SELECTION:
215             if (decoder->does_selection == NULL)
216                 decoder->does_selection =
217                     OSSL_FUNC_decoder_does_selection(fns);
218             break;
219         case OSSL_FUNC_DECODER_DECODE:
220             if (decoder->decode == NULL)
221                 decoder->decode = OSSL_FUNC_decoder_decode(fns);
222             break;
223         case OSSL_FUNC_DECODER_EXPORT_OBJECT:
224             if (decoder->export_object == NULL)
225                 decoder->export_object = OSSL_FUNC_decoder_export_object(fns);
226             break;
227         }
228     }
229     /*
230      * Try to check that the method is sensible.
231      * If you have a constructor, you must have a destructor and vice versa.
232      * You must have at least one of the encoding driver functions.
233      */
234     if (!((decoder->newctx == NULL && decoder->freectx == NULL)
235           || (decoder->newctx != NULL && decoder->freectx != NULL))
236         || decoder->decode == NULL) {
237         OSSL_DECODER_free(decoder);
238         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
239         return NULL;
240     }
241
242     if (prov != NULL && !ossl_provider_up_ref(prov)) {
243         OSSL_DECODER_free(decoder);
244         return NULL;
245     }
246
247     decoder->base.prov = prov;
248     return decoder;
249 }
250
251
252 /*
253  * The core fetching functionality passes the names of the implementation.
254  * This function is responsible to getting an identity number for them,
255  * then call ossl_decoder_from_algorithm() with that identity number.
256  */
257 static void *construct_decoder(const OSSL_ALGORITHM *algodef,
258                                OSSL_PROVIDER *prov, void *data)
259 {
260     /*
261      * This function is only called if get_decoder_from_store() returned
262      * NULL, so it's safe to say that of all the spots to create a new
263      * namemap entry, this is it.  Should the name already exist there, we
264      * know that ossl_namemap_add() will return its corresponding number.
265      */
266     struct decoder_data_st *methdata = data;
267     OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
268     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
269     const char *names = algodef->algorithm_names;
270     int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
271     void *method = NULL;
272
273     if (id != 0)
274         method = ossl_decoder_from_algorithm(id, algodef, prov);
275
276     /*
277      * Flag to indicate that there was actual construction errors.  This
278      * helps inner_evp_generic_fetch() determine what error it should
279      * record on inaccessible algorithms.
280      */
281     if (method == NULL)
282         methdata->flag_construct_error_occurred = 1;
283
284     return method;
285 }
286
287 /* Intermediary function to avoid ugly casts, used below */
288 static void destruct_decoder(void *method, void *data)
289 {
290     OSSL_DECODER_free(method);
291 }
292
293 static int up_ref_decoder(void *method)
294 {
295     return OSSL_DECODER_up_ref(method);
296 }
297
298 static void free_decoder(void *method)
299 {
300     OSSL_DECODER_free(method);
301 }
302
303 /* Fetching support.  Can fetch by numeric identity or by name */
304 static OSSL_DECODER *inner_ossl_decoder_fetch(OSSL_LIB_CTX *libctx, int id,
305                                               const char *name,
306                                               const char *properties)
307 {
308     OSSL_METHOD_STORE *store = get_decoder_store(libctx);
309     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
310     void *method = NULL;
311     int unsupported = 0;
312
313     if (store == NULL || namemap == NULL) {
314         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_INVALID_ARGUMENT);
315         return NULL;
316     }
317
318     /*
319      * If we have been passed neither a name_id or a name, we have an
320      * internal programming error.
321      */
322     if (!ossl_assert(id != 0 || name != NULL)) {
323         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_INTERNAL_ERROR);
324         return NULL;
325     }
326
327     if (id == 0)
328         id = ossl_namemap_name2num(namemap, name);
329
330     /*
331      * If we haven't found the name yet, chances are that the algorithm to
332      * be fetched is unsupported.
333      */
334     if (id == 0)
335         unsupported = 1;
336
337     if (id == 0
338         || !ossl_method_store_cache_get(store, id, properties, &method)) {
339         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
340             alloc_tmp_decoder_store,
341             dealloc_tmp_decoder_store,
342             get_decoder_from_store,
343             put_decoder_in_store,
344             construct_decoder,
345             destruct_decoder
346         };
347         struct decoder_data_st mcmdata;
348
349         mcmdata.libctx = libctx;
350         mcmdata.mcm = &mcm;
351         mcmdata.id = id;
352         mcmdata.names = name;
353         mcmdata.propquery = properties;
354         mcmdata.flag_construct_error_occurred = 0;
355         if ((method = ossl_method_construct(libctx, OSSL_OP_DECODER,
356                                             0 /* !force_cache */,
357                                             &mcm, &mcmdata)) != NULL) {
358             /*
359              * If construction did create a method for us, we know that
360              * there is a correct name_id and meth_id, since those have
361              * already been calculated in get_decoder_from_store() and
362              * put_decoder_in_store() above.
363              */
364             if (id == 0)
365                 id = ossl_namemap_name2num(namemap, name);
366             ossl_method_store_cache_set(store, id, properties, method,
367                                         up_ref_decoder, free_decoder);
368         }
369
370         /*
371          * If we never were in the constructor, the algorithm to be fetched
372          * is unsupported.
373          */
374         unsupported = !mcmdata.flag_construct_error_occurred;
375     }
376
377     if (method == NULL) {
378         int code = unsupported ? ERR_R_UNSUPPORTED : ERR_R_FETCH_FAILED;
379
380         if (name == NULL)
381             name = ossl_namemap_num2name(namemap, id, 0);
382         ERR_raise_data(ERR_LIB_OSSL_DECODER, code,
383                        "%s, Name (%s : %d), Properties (%s)",
384                        ossl_lib_ctx_get_descriptor(libctx),
385                        name = NULL ? "<null>" : name, id,
386                        properties == NULL ? "<null>" : properties);
387     }
388
389     return method;
390 }
391
392 OSSL_DECODER *OSSL_DECODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
393                                  const char *properties)
394 {
395     return inner_ossl_decoder_fetch(libctx, 0, name, properties);
396 }
397
398 OSSL_DECODER *ossl_decoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
399                                            const char *properties)
400 {
401     return inner_ossl_decoder_fetch(libctx, id, NULL, properties);
402 }
403
404 /*
405  * Library of basic method functions
406  */
407
408 const OSSL_PROVIDER *OSSL_DECODER_get0_provider(const OSSL_DECODER *decoder)
409 {
410     if (!ossl_assert(decoder != NULL)) {
411         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
412         return 0;
413     }
414
415     return decoder->base.prov;
416 }
417
418 const char *OSSL_DECODER_get0_properties(const OSSL_DECODER *decoder)
419 {
420     if (!ossl_assert(decoder != NULL)) {
421         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
422         return 0;
423     }
424
425     return decoder->base.propdef;
426 }
427
428 const OSSL_PROPERTY_LIST *
429 ossl_decoder_parsed_properties(const OSSL_DECODER *decoder)
430 {
431     if (!ossl_assert(decoder != NULL)) {
432         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
433         return 0;
434     }
435
436     return decoder->base.parsed_propdef;
437 }
438
439 int ossl_decoder_get_number(const OSSL_DECODER *decoder)
440 {
441     if (!ossl_assert(decoder != NULL)) {
442         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
443         return 0;
444     }
445
446     return decoder->base.id;
447 }
448
449 const char *OSSL_DECODER_get0_name(const OSSL_DECODER *decoder)
450 {
451     return decoder->base.name;
452 }
453
454 const char *OSSL_DECODER_get0_description(const OSSL_DECODER *decoder)
455 {
456     return decoder->base.description;
457 }
458
459 int OSSL_DECODER_is_a(const OSSL_DECODER *decoder, const char *name)
460 {
461     if (decoder->base.prov != NULL) {
462         OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
463         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
464
465         return ossl_namemap_name2num(namemap, name) == decoder->base.id;
466     }
467     return 0;
468 }
469
470 struct decoder_do_all_data_st {
471     void (*user_fn)(void *method, void *arg);
472     void *user_arg;
473 };
474
475 static void decoder_do_one(OSSL_PROVIDER *provider,
476                            const OSSL_ALGORITHM *algodef,
477                            int no_store, void *vdata)
478 {
479     struct decoder_do_all_data_st *data = vdata;
480     OSSL_LIB_CTX *libctx = ossl_provider_libctx(provider);
481     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
482     const char *names = algodef->algorithm_names;
483     int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
484     void *method = NULL;
485
486     if (id != 0)
487         method = ossl_decoder_from_algorithm(id, algodef, provider);
488
489     if (method != NULL) {
490         data->user_fn(method, data->user_arg);
491         OSSL_DECODER_free(method);
492     }
493 }
494
495 void OSSL_DECODER_do_all_provided(OSSL_LIB_CTX *libctx,
496                                   void (*fn)(OSSL_DECODER *decoder, void *arg),
497                                   void *arg)
498 {
499     struct decoder_do_all_data_st data;
500
501     data.user_fn = (void (*)(void *, void *))fn;
502     data.user_arg = arg;
503     ossl_algorithm_do_all(libctx, OSSL_OP_DECODER, NULL,
504                           NULL, decoder_do_one, NULL,
505                           &data);
506 }
507
508 int OSSL_DECODER_names_do_all(const OSSL_DECODER *decoder,
509                               void (*fn)(const char *name, void *data),
510                               void *data)
511 {
512     if (decoder == NULL)
513         return 0;
514
515     if (decoder->base.prov != NULL) {
516         OSSL_LIB_CTX *libctx = ossl_provider_libctx(decoder->base.prov);
517         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
518
519         return ossl_namemap_doall_names(namemap, decoder->base.id, fn, data);
520     }
521
522     return 1;
523 }
524
525 const OSSL_PARAM *
526 OSSL_DECODER_gettable_params(OSSL_DECODER *decoder)
527 {
528     if (decoder != NULL && decoder->gettable_params != NULL) {
529         void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
530
531         return decoder->gettable_params(provctx);
532     }
533     return NULL;
534 }
535
536 int OSSL_DECODER_get_params(OSSL_DECODER *decoder, OSSL_PARAM params[])
537 {
538     if (decoder != NULL && decoder->get_params != NULL)
539         return decoder->get_params(params);
540     return 0;
541 }
542
543 const OSSL_PARAM *
544 OSSL_DECODER_settable_ctx_params(OSSL_DECODER *decoder)
545 {
546     if (decoder != NULL && decoder->settable_ctx_params != NULL) {
547         void *provctx = ossl_provider_ctx(OSSL_DECODER_get0_provider(decoder));
548
549         return decoder->settable_ctx_params(provctx);
550     }
551     return NULL;
552 }
553
554 /*
555  * Decoder context support
556  */
557
558 /*
559  * |encoder| value NULL is valid, and signifies that there is no decoder.
560  * This is useful to provide fallback mechanisms.
561  *  Functions that want to verify if there is a decoder can do so with
562  * OSSL_DECODER_CTX_get_decoder()
563  */
564 OSSL_DECODER_CTX *OSSL_DECODER_CTX_new(void)
565 {
566     OSSL_DECODER_CTX *ctx;
567
568     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
569         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_MALLOC_FAILURE);
570
571     return ctx;
572 }
573
574 int OSSL_DECODER_CTX_set_params(OSSL_DECODER_CTX *ctx,
575                                 const OSSL_PARAM params[])
576 {
577     int ok = 1;
578     size_t i;
579     size_t l;
580
581     if (!ossl_assert(ctx != NULL)) {
582         ERR_raise(ERR_LIB_OSSL_DECODER, ERR_R_PASSED_NULL_PARAMETER);
583         return 0;
584     }
585
586     if (ctx->decoder_insts == NULL)
587         return 1;
588
589     l = OSSL_DECODER_CTX_get_num_decoders(ctx);
590     for (i = 0; i < l; i++) {
591         OSSL_DECODER_INSTANCE *decoder_inst =
592             sk_OSSL_DECODER_INSTANCE_value(ctx->decoder_insts, i);
593         OSSL_DECODER *decoder =
594             OSSL_DECODER_INSTANCE_get_decoder(decoder_inst);
595         OSSL_DECODER *decoderctx =
596             OSSL_DECODER_INSTANCE_get_decoder_ctx(decoder_inst);
597
598         if (decoderctx == NULL || decoder->set_ctx_params == NULL)
599             continue;
600         if (!decoder->set_ctx_params(decoderctx, params))
601             ok = 0;
602     }
603     return ok;
604 }
605
606 void OSSL_DECODER_CTX_free(OSSL_DECODER_CTX *ctx)
607 {
608     if (ctx != NULL) {
609         if (ctx->cleanup != NULL)
610             ctx->cleanup(ctx->construct_data);
611         sk_OSSL_DECODER_INSTANCE_pop_free(ctx->decoder_insts,
612                                           ossl_decoder_instance_free);
613         ossl_pw_clear_passphrase_data(&ctx->pwdata);
614         OPENSSL_free(ctx);
615     }
616 }