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