Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[openssl.git] / crypto / encode_decode / encoder_meth.c
1 /*
2  * Copyright 2019-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/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 */
27 static 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
43 int 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
51 void 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 */
67 static void encoder_store_free(void *vstore)
68 {
69     ossl_method_store_free(vstore);
70 }
71
72 static void *encoder_store_new(OSSL_LIB_CTX *ctx)
73 {
74     return ossl_method_store_new(ctx);
75 }
76
77
78 static const OSSL_LIB_CTX_METHOD encoder_store_method = {
79     encoder_store_new,
80     encoder_store_free,
81 };
82
83 /* Data to be passed through ossl_method_construct() */
84 struct encoder_data_st {
85     OSSL_LIB_CTX *libctx;
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() */
90 };
91
92 /*
93  * Generic routines to fetch / create ENCODER methods with
94  * ossl_method_construct()
95  */
96
97 /* Temporary encoder method store, constructor and destructor */
98 static void *alloc_tmp_encoder_store(OSSL_LIB_CTX *ctx)
99 {
100     return ossl_method_store_new(ctx);
101 }
102
103 static void dealloc_tmp_encoder_store(void *store)
104 {
105     if (store != NULL)
106         ossl_method_store_free(store);
107 }
108
109 /* Get the permanent encoder store */
110 static OSSL_METHOD_STORE *get_encoder_store(OSSL_LIB_CTX *libctx)
111 {
112     return ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_ENCODER_STORE_INDEX,
113                                  &encoder_store_method);
114 }
115
116 /* Get encoder methods from a store, or put one in */
117 static void *get_encoder_from_store(OSSL_LIB_CTX *libctx, void *store,
118                                     void *data)
119 {
120     struct encoder_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_encoder_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_encoder_in_store(OSSL_LIB_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_encoder_store(libctx)) == NULL)
152         return 0;
153
154     return ossl_method_store_add(store, prov, id, propdef, method,
155                                  (int (*)(void *))OSSL_ENCODER_up_ref,
156                                  (void (*)(void *))OSSL_ENCODER_free);
157 }
158
159 /* Create and populate a encoder method */
160 static void *encoder_from_dispatch(int id, const OSSL_ALGORITHM *algodef,
161                                    OSSL_PROVIDER *prov)
162 {
163     OSSL_ENCODER *encoder = NULL;
164     const OSSL_DISPATCH *fns = algodef->implementation;
165
166     if ((encoder = ossl_encoder_new()) == NULL)
167         return NULL;
168     encoder->base.id = id;
169     encoder->base.propdef = algodef->property_definition;
170
171     for (; fns->function_id != 0; fns++) {
172         switch (fns->function_id) {
173         case OSSL_FUNC_ENCODER_NEWCTX:
174             if (encoder->newctx == NULL)
175                 encoder->newctx =
176                     OSSL_FUNC_encoder_newctx(fns);
177             break;
178         case OSSL_FUNC_ENCODER_FREECTX:
179             if (encoder->freectx == NULL)
180                 encoder->freectx =
181                     OSSL_FUNC_encoder_freectx(fns);
182             break;
183         case OSSL_FUNC_ENCODER_GET_PARAMS:
184             if (encoder->get_params == NULL)
185                 encoder->get_params =
186                     OSSL_FUNC_encoder_get_params(fns);
187             break;
188         case OSSL_FUNC_ENCODER_GETTABLE_PARAMS:
189             if (encoder->gettable_params == NULL)
190                 encoder->gettable_params =
191                     OSSL_FUNC_encoder_gettable_params(fns);
192             break;
193         case OSSL_FUNC_ENCODER_SET_CTX_PARAMS:
194             if (encoder->set_ctx_params == NULL)
195                 encoder->set_ctx_params =
196                     OSSL_FUNC_encoder_set_ctx_params(fns);
197             break;
198         case OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS:
199             if (encoder->settable_ctx_params == NULL)
200                 encoder->settable_ctx_params =
201                     OSSL_FUNC_encoder_settable_ctx_params(fns);
202             break;
203         case OSSL_FUNC_ENCODER_ENCODE:
204             if (encoder->encode == NULL)
205                 encoder->encode = OSSL_FUNC_encoder_encode(fns);
206             break;
207         case OSSL_FUNC_ENCODER_IMPORT_OBJECT:
208             if (encoder->import_object == NULL)
209                 encoder->import_object =
210                     OSSL_FUNC_encoder_import_object(fns);
211             break;
212         case OSSL_FUNC_ENCODER_FREE_OBJECT:
213             if (encoder->free_object == NULL)
214                 encoder->free_object =
215                     OSSL_FUNC_encoder_free_object(fns);
216             break;
217         }
218     }
219     /*
220      * Try to check that the method is sensible.
221      * If you have a constructor, you must have a destructor and vice versa.
222      * You must have the encoding driver functions.
223      */
224     if (!((encoder->newctx == NULL && encoder->freectx == NULL)
225           || (encoder->newctx != NULL && encoder->freectx != NULL)
226           || (encoder->import_object != NULL && encoder->free_object != NULL)
227           || (encoder->import_object == NULL && encoder->free_object == NULL))
228         || encoder->encode == NULL
229         || encoder->gettable_params == NULL
230         || encoder->get_params == NULL) {
231         OSSL_ENCODER_free(encoder);
232         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
233         return NULL;
234     }
235
236     if (prov != NULL && !ossl_provider_up_ref(prov)) {
237         OSSL_ENCODER_free(encoder);
238         return NULL;
239     }
240
241     encoder->base.prov = prov;
242     return encoder;
243 }
244
245
246 /*
247  * The core fetching functionality passes the names of the implementation.
248  * This function is responsible to getting an identity number for them,
249  * then call encoder_from_dispatch() with that identity number.
250  */
251 static void *construct_encoder(const OSSL_ALGORITHM *algodef,
252                                OSSL_PROVIDER *prov, void *unused)
253 {
254     /*
255      * This function is only called if get_encoder_from_store() returned
256      * NULL, so it's safe to say that of all the spots to create a new
257      * namemap entry, this is it.  Should the name already exist there, we
258      * know that ossl_namemap_add() will return its corresponding number.
259      */
260     OSSL_LIB_CTX *libctx = ossl_provider_library_context(prov);
261     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
262     const char *names = algodef->algorithm_names;
263     int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
264     void *method = NULL;
265
266     if (id != 0)
267         method = encoder_from_dispatch(id, algodef, prov);
268
269     return method;
270 }
271
272 /* Intermediary function to avoid ugly casts, used below */
273 static void destruct_encoder(void *method, void *data)
274 {
275     OSSL_ENCODER_free(method);
276 }
277
278 static int up_ref_encoder(void *method)
279 {
280     return OSSL_ENCODER_up_ref(method);
281 }
282
283 static void free_encoder(void *method)
284 {
285     OSSL_ENCODER_free(method);
286 }
287
288 /* Fetching support.  Can fetch by numeric identity or by name */
289 static OSSL_ENCODER *inner_ossl_encoder_fetch(OSSL_LIB_CTX *libctx,
290                                               int id, const char *name,
291                                               const char *properties)
292 {
293     OSSL_METHOD_STORE *store = get_encoder_store(libctx);
294     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
295     void *method = NULL;
296
297     if (store == NULL || namemap == NULL)
298         return NULL;
299
300     /*
301      * If we have been passed neither a name_id or a name, we have an
302      * internal programming error.
303      */
304     if (!ossl_assert(id != 0 || name != NULL))
305         return NULL;
306
307     if (id == 0)
308         id = ossl_namemap_name2num(namemap, name);
309
310     if (id == 0
311         || !ossl_method_store_cache_get(store, id, properties, &method)) {
312         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
313             alloc_tmp_encoder_store,
314             dealloc_tmp_encoder_store,
315             get_encoder_from_store,
316             put_encoder_in_store,
317             construct_encoder,
318             destruct_encoder
319         };
320         struct encoder_data_st mcmdata;
321
322         mcmdata.libctx = libctx;
323         mcmdata.mcm = &mcm;
324         mcmdata.id = id;
325         mcmdata.names = name;
326         mcmdata.propquery = properties;
327         if ((method = ossl_method_construct(libctx, OSSL_OP_ENCODER,
328                                             0 /* !force_cache */,
329                                             &mcm, &mcmdata)) != NULL) {
330             /*
331              * If construction did create a method for us, we know that
332              * there is a correct name_id and meth_id, since those have
333              * already been calculated in get_encoder_from_store() and
334              * put_encoder_in_store() above.
335              */
336             if (id == 0)
337                 id = ossl_namemap_name2num(namemap, name);
338             ossl_method_store_cache_set(store, id, properties, method,
339                                         up_ref_encoder, free_encoder);
340         }
341     }
342
343     return method;
344 }
345
346 OSSL_ENCODER *OSSL_ENCODER_fetch(OSSL_LIB_CTX *libctx, const char *name,
347                                  const char *properties)
348 {
349     return inner_ossl_encoder_fetch(libctx, 0, name, properties);
350 }
351
352 OSSL_ENCODER *ossl_encoder_fetch_by_number(OSSL_LIB_CTX *libctx, int id,
353                                            const char *properties)
354 {
355     return inner_ossl_encoder_fetch(libctx, id, NULL, properties);
356 }
357
358 /*
359  * Library of basic method functions
360  */
361
362 const OSSL_PROVIDER *OSSL_ENCODER_provider(const OSSL_ENCODER *encoder)
363 {
364     if (!ossl_assert(encoder != NULL)) {
365         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
366         return 0;
367     }
368
369     return encoder->base.prov;
370 }
371
372 const char *OSSL_ENCODER_properties(const OSSL_ENCODER *encoder)
373 {
374     if (!ossl_assert(encoder != NULL)) {
375         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
376         return 0;
377     }
378
379     return encoder->base.propdef;
380 }
381
382 int OSSL_ENCODER_number(const OSSL_ENCODER *encoder)
383 {
384     if (!ossl_assert(encoder != NULL)) {
385         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
386         return 0;
387     }
388
389     return encoder->base.id;
390 }
391
392 int OSSL_ENCODER_is_a(const OSSL_ENCODER *encoder, const char *name)
393 {
394     if (encoder->base.prov != NULL) {
395         OSSL_LIB_CTX *libctx = ossl_provider_library_context(encoder->base.prov);
396         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
397
398         return ossl_namemap_name2num(namemap, name) == encoder->base.id;
399     }
400     return 0;
401 }
402
403 struct encoder_do_all_data_st {
404     void (*user_fn)(void *method, void *arg);
405     void *user_arg;
406 };
407
408 static void encoder_do_one(OSSL_PROVIDER *provider,
409                            const OSSL_ALGORITHM *algodef,
410                            int no_store, void *vdata)
411 {
412     struct encoder_do_all_data_st *data = vdata;
413     OSSL_LIB_CTX *libctx = ossl_provider_library_context(provider);
414     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
415     const char *names = algodef->algorithm_names;
416     int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
417     void *method = NULL;
418
419     if (id != 0)
420         method =
421             encoder_from_dispatch(id, algodef, provider);
422
423     if (method != NULL) {
424         data->user_fn(method, data->user_arg);
425         OSSL_ENCODER_free(method);
426     }
427 }
428
429 void OSSL_ENCODER_do_all_provided(OSSL_LIB_CTX *libctx,
430                                   void (*fn)(OSSL_ENCODER *encoder, void *arg),
431                                   void *arg)
432 {
433     struct encoder_do_all_data_st data;
434
435     data.user_fn = (void (*)(void *, void *))fn;
436     data.user_arg = arg;
437
438     /*
439      * No pre- or post-condition for this call, as this only creates methods
440      * temporarly and then promptly destroys them.
441      */
442     ossl_algorithm_do_all(libctx, OSSL_OP_ENCODER, NULL, NULL,
443                           encoder_do_one, NULL, &data);
444 }
445
446 void OSSL_ENCODER_names_do_all(const OSSL_ENCODER *encoder,
447                                void (*fn)(const char *name, void *data),
448                                void *data)
449 {
450     if (encoder == NULL)
451         return;
452
453     if (encoder->base.prov != NULL) {
454         OSSL_LIB_CTX *libctx = ossl_provider_library_context(encoder->base.prov);
455         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
456
457         ossl_namemap_doall_names(namemap, encoder->base.id, fn, data);
458     }
459 }
460
461 const OSSL_PARAM *
462 OSSL_ENCODER_gettable_params(OSSL_ENCODER *encoder)
463 {
464     if (encoder != NULL && encoder->gettable_params != NULL) {
465         void *provctx = ossl_provider_ctx(OSSL_ENCODER_provider(encoder));
466
467         return encoder->gettable_params(provctx);
468     }
469     return NULL;
470 }
471
472 int OSSL_ENCODER_get_params(OSSL_ENCODER *encoder, OSSL_PARAM params[])
473 {
474     if (encoder != NULL && encoder->get_params != NULL)
475         return encoder->get_params(params);
476     return 0;
477 }
478
479 const OSSL_PARAM *OSSL_ENCODER_settable_ctx_params(OSSL_ENCODER *encoder)
480 {
481     if (encoder != NULL && encoder->settable_ctx_params != NULL) {
482         void *provctx = ossl_provider_ctx(OSSL_ENCODER_provider(encoder));
483
484         return encoder->settable_ctx_params(provctx);
485     }
486     return NULL;
487 }
488
489 /*
490  * Encoder context support
491  */
492
493 OSSL_ENCODER_CTX *OSSL_ENCODER_CTX_new(void)
494 {
495     OSSL_ENCODER_CTX *ctx;
496
497     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
498         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_MALLOC_FAILURE);
499
500     return ctx;
501 }
502
503 int OSSL_ENCODER_CTX_set_params(OSSL_ENCODER_CTX *ctx,
504                                 const OSSL_PARAM params[])
505 {
506     size_t i;
507     size_t l;
508
509     if (!ossl_assert(ctx != NULL)) {
510         ERR_raise(ERR_LIB_OSSL_ENCODER, ERR_R_PASSED_NULL_PARAMETER);
511         return 0;
512     }
513
514     if (ctx->encoder_insts == NULL)
515         return 1;
516
517     l = OSSL_ENCODER_CTX_get_num_encoders(ctx);
518     for (i = 0; i < l; i++) {
519         OSSL_ENCODER_INSTANCE *encoder_inst =
520             sk_OSSL_ENCODER_INSTANCE_value(ctx->encoder_insts, i);
521         OSSL_ENCODER *encoder = OSSL_ENCODER_INSTANCE_get_encoder(encoder_inst);
522         void *encoderctx = OSSL_ENCODER_INSTANCE_get_encoder_ctx(encoder_inst);
523
524         if (encoderctx == NULL || encoder->set_ctx_params == NULL)
525             continue;
526         if (!encoder->set_ctx_params(encoderctx, params))
527             return 0;
528     }
529     return 1;
530 }
531
532 void OSSL_ENCODER_CTX_free(OSSL_ENCODER_CTX *ctx)
533 {
534     if (ctx != NULL) {
535         sk_OSSL_ENCODER_INSTANCE_pop_free(ctx->encoder_insts,
536                                           ossl_encoder_instance_free);
537         OPENSSL_free(ctx->construct_data);
538         ossl_pw_clear_passphrase_data(&ctx->pwdata);
539         OPENSSL_free(ctx);
540     }
541 }