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