b95bd14a51b527517019c9e70ff77a4dd08a9f75
[oweals/openssl.git] / crypto / serializer / serializer_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/serializer.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/serializer.h"
19 #include "serializer_local.h"
20
21 /*
22  * Serializer 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_SERIALIZER *ossl_serializer_new(void)
28 {
29     OSSL_SERIALIZER *ser = NULL;
30
31     if ((ser = OPENSSL_zalloc(sizeof(*ser))) == NULL
32         || (ser->lock = CRYPTO_THREAD_lock_new()) == NULL) {
33         OSSL_SERIALIZER_free(ser);
34         ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_MALLOC_FAILURE);
35         return NULL;
36     }
37
38     ser->refcnt = 1;
39
40     return ser;
41 }
42
43 int OSSL_SERIALIZER_up_ref(OSSL_SERIALIZER *ser)
44 {
45     int ref = 0;
46
47     CRYPTO_UP_REF(&ser->refcnt, &ref, ser->lock);
48     return 1;
49 }
50
51 void OSSL_SERIALIZER_free(OSSL_SERIALIZER *ser)
52 {
53     int ref = 0;
54
55     if (ser == NULL)
56         return;
57
58     CRYPTO_DOWN_REF(&ser->refcnt, &ref, ser->lock);
59     if (ref > 0)
60         return;
61     ossl_provider_free(ser->prov);
62     CRYPTO_THREAD_lock_free(ser->lock);
63     OPENSSL_free(ser);
64 }
65
66 /* Permanent serializer method store, constructor and destructor */
67 static void serializer_store_free(void *vstore)
68 {
69     ossl_method_store_free(vstore);
70 }
71
72 static void *serializer_store_new(OPENSSL_CTX *ctx)
73 {
74     return ossl_method_store_new(ctx);
75 }
76
77
78 static const OPENSSL_CTX_METHOD serializer_store_method = {
79     serializer_store_new,
80     serializer_store_free,
81 };
82
83 /* Data to be passed through ossl_method_construct() */
84 struct serializer_data_st {
85     OPENSSL_CTX *libctx;
86     OSSL_METHOD_CONSTRUCT_METHOD *mcm;
87     int id;                      /* For get_serializer_from_store() */
88     const char *names;           /* For get_serializer_from_store() */
89     const char *propquery;       /* For get_serializer_from_store() */
90 };
91
92 /*
93  * Generic routines to fetch / create SERIALIZER methods with
94  * ossl_method_construct()
95  */
96
97 /* Temporary serializer method store, constructor and destructor */
98 static void *alloc_tmp_serializer_store(OPENSSL_CTX *ctx)
99 {
100     return ossl_method_store_new(ctx);
101 }
102
103  static void dealloc_tmp_serializer_store(void *store)
104 {
105     if (store != NULL)
106         ossl_method_store_free(store);
107 }
108
109 /* Get the permanent serializer store */
110 static OSSL_METHOD_STORE *get_serializer_store(OPENSSL_CTX *libctx)
111 {
112     return openssl_ctx_get_data(libctx, OPENSSL_CTX_SERIALIZER_STORE_INDEX,
113                                 &serializer_store_method);
114 }
115
116 /* Get serializer methods from a store, or put one in */
117 static void *get_serializer_from_store(OPENSSL_CTX *libctx, void *store,
118                                        void *data)
119 {
120     struct serializer_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_serializer_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_serializer_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_serializer_store(libctx)) == NULL)
152         return 0;
153
154     return ossl_method_store_add(store, prov, id, propdef, method,
155                                  (int (*)(void *))OSSL_SERIALIZER_up_ref,
156                                  (void (*)(void *))OSSL_SERIALIZER_free);
157 }
158
159 /* Create and populate a serializer method */
160 static void *serializer_from_dispatch(int id, const OSSL_ALGORITHM *algodef,
161                                       OSSL_PROVIDER *prov)
162 {
163     OSSL_SERIALIZER *ser = NULL;
164     const OSSL_DISPATCH *fns = algodef->implementation;
165
166     if ((ser = ossl_serializer_new()) == NULL)
167         return NULL;
168     ser->id = id;
169     ser->propdef = algodef->property_definition;
170
171     for (; fns->function_id != 0; fns++) {
172         switch (fns->function_id) {
173         case OSSL_FUNC_SERIALIZER_NEWCTX:
174             if (ser->newctx == NULL)
175                 ser->newctx =
176                     OSSL_get_OP_serializer_newctx(fns);
177             break;
178         case OSSL_FUNC_SERIALIZER_FREECTX:
179             if (ser->freectx == NULL)
180                 ser->freectx =
181                     OSSL_get_OP_serializer_freectx(fns);
182             break;
183         case OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS:
184             if (ser->set_ctx_params == NULL)
185                 ser->set_ctx_params =
186                     OSSL_get_OP_serializer_set_ctx_params(fns);
187             break;
188         case OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS:
189             if (ser->settable_ctx_params == NULL)
190                 ser->settable_ctx_params =
191                     OSSL_get_OP_serializer_settable_ctx_params(fns);
192             break;
193         case OSSL_FUNC_SERIALIZER_SERIALIZE_DATA:
194             if (ser->serialize_data == NULL)
195                 ser->serialize_data =
196                     OSSL_get_OP_serializer_serialize_data(fns);
197             break;
198         case OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT:
199             if (ser->serialize_object == NULL)
200                 ser->serialize_object =
201                     OSSL_get_OP_serializer_serialize_object(fns);
202             break;
203         }
204     }
205     /*
206      * Try to check that the method is sensible.
207      * If you have a constructor, you must have a destructor and vice versa.
208      * You must have at least one of the serializing driver functions.
209      */
210     if (!((ser->newctx == NULL && ser->freectx == NULL)
211           || (ser->newctx != NULL && ser->freectx != NULL))
212         || (ser->serialize_data == NULL && ser->serialize_object == NULL)) {
213         OSSL_SERIALIZER_free(ser);
214         ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_INVALID_PROVIDER_FUNCTIONS);
215         return NULL;
216     }
217
218     if (prov != NULL && !ossl_provider_up_ref(prov)) {
219         OSSL_SERIALIZER_free(ser);
220         return NULL;
221     }
222
223     ser->prov = prov;
224     return ser;
225 }
226
227
228 /*
229  * The core fetching functionality passes the names of the implementation.
230  * This function is responsible to getting an identity number for them,
231  * then call serializer_from_dispatch() with that identity number.
232  */
233 static void *construct_serializer(const OSSL_ALGORITHM *algodef,
234                                   OSSL_PROVIDER *prov, void *unused)
235 {
236     /*
237      * This function is only called if get_serializer_from_store() returned
238      * NULL, so it's safe to say that of all the spots to create a new
239      * namemap entry, this is it.  Should the name already exist there, we
240      * know that ossl_namemap_add() will return its corresponding number.
241      */
242     OPENSSL_CTX *libctx = ossl_provider_library_context(prov);
243     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
244     const char *names = algodef->algorithm_names;
245     int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
246     void *method = NULL;
247
248     if (id != 0)
249         method = serializer_from_dispatch(id, algodef, prov);
250
251     return method;
252 }
253
254 /* Intermediary function to avoid ugly casts, used below */
255 static void destruct_serializer(void *method, void *data)
256 {
257     OSSL_SERIALIZER_free(method);
258 }
259
260 static int up_ref_serializer(void *method)
261 {
262     return OSSL_SERIALIZER_up_ref(method);
263 }
264
265 static void free_serializer(void *method)
266 {
267     OSSL_SERIALIZER_free(method);
268 }
269
270 /* Fetching support.  Can fetch by numeric identity or by name */
271 static OSSL_SERIALIZER *inner_ossl_serializer_fetch(OPENSSL_CTX *libctx,
272                                                     int id, const char *name,
273                                                     const char *properties)
274 {
275     OSSL_METHOD_STORE *store = get_serializer_store(libctx);
276     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
277     void *method = NULL;
278
279     if (store == NULL || namemap == NULL)
280         return NULL;
281
282     /*
283      * If we have been passed neither a name_id or a name, we have an
284      * internal programming error.
285      */
286     if (!ossl_assert(id != 0 || name != NULL))
287         return NULL;
288
289     if (id == 0)
290         id = ossl_namemap_name2num(namemap, name);
291
292     if (id == 0
293         || !ossl_method_store_cache_get(store, id, properties, &method)) {
294         OSSL_METHOD_CONSTRUCT_METHOD mcm = {
295             alloc_tmp_serializer_store,
296             dealloc_tmp_serializer_store,
297             get_serializer_from_store,
298             put_serializer_in_store,
299             construct_serializer,
300             destruct_serializer
301         };
302         struct serializer_data_st mcmdata;
303
304         mcmdata.libctx = libctx;
305         mcmdata.mcm = &mcm;
306         mcmdata.id = id;
307         mcmdata.names = name;
308         mcmdata.propquery = properties;
309         if ((method = ossl_method_construct(libctx, OSSL_OP_SERIALIZER,
310                                             0 /* !force_cache */,
311                                             &mcm, &mcmdata)) != NULL) {
312             /*
313              * If construction did create a method for us, we know that
314              * there is a correct name_id and meth_id, since those have
315              * already been calculated in get_serializer_from_store() and
316              * put_serializer_in_store() above.
317              */
318             if (id == 0)
319                 id = ossl_namemap_name2num(namemap, name);
320             ossl_method_store_cache_set(store, id, properties, method,
321                                         up_ref_serializer, free_serializer);
322         }
323     }
324
325     return method;
326 }
327
328 OSSL_SERIALIZER *OSSL_SERIALIZER_fetch(OPENSSL_CTX *libctx, const char *name,
329                                        const char *properties)
330 {
331     return inner_ossl_serializer_fetch(libctx, 0, name, properties);
332 }
333
334 OSSL_SERIALIZER *ossl_serializer_fetch_by_number(OPENSSL_CTX *libctx, int id,
335                                                  const char *properties)
336 {
337     return inner_ossl_serializer_fetch(libctx, id, NULL, properties);
338 }
339
340 /*
341  * Library of basic method functions
342  */
343
344 const OSSL_PROVIDER *OSSL_SERIALIZER_provider(const OSSL_SERIALIZER *ser)
345 {
346     if (!ossl_assert(ser != NULL)) {
347         ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
348         return 0;
349     }
350
351     return ser->prov;
352 }
353
354 const char *OSSL_SERIALIZER_properties(const OSSL_SERIALIZER *ser)
355 {
356     if (!ossl_assert(ser != NULL)) {
357         ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
358         return 0;
359     }
360
361     return ser->propdef;
362 }
363
364 int OSSL_SERIALIZER_number(const OSSL_SERIALIZER *ser)
365 {
366     if (!ossl_assert(ser != NULL)) {
367         ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
368         return 0;
369     }
370
371     return ser->id;
372 }
373
374 int OSSL_SERIALIZER_is_a(const OSSL_SERIALIZER *ser, const char *name)
375 {
376     if (ser->prov != NULL) {
377         OPENSSL_CTX *libctx = ossl_provider_library_context(ser->prov);
378         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
379
380         return ossl_namemap_name2num(namemap, name) == ser->id;
381     }
382     return 0;
383 }
384
385 struct serializer_do_all_data_st {
386     void (*user_fn)(void *method, void *arg);
387     void *user_arg;
388 };
389
390 static void serializer_do_one(OSSL_PROVIDER *provider,
391                               const OSSL_ALGORITHM *algodef,
392                               int no_store, void *vdata)
393 {
394     struct serializer_do_all_data_st *data = vdata;
395     OPENSSL_CTX *libctx = ossl_provider_library_context(provider);
396     OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
397     const char *names = algodef->algorithm_names;
398     int id = ossl_namemap_add_names(namemap, 0, names, NAME_SEPARATOR);
399     void *method = NULL;
400
401     if (id != 0)
402         method =
403             serializer_from_dispatch(id, algodef, provider);
404
405     if (method != NULL) {
406         data->user_fn(method, data->user_arg);
407         OSSL_SERIALIZER_free(method);
408     }
409 }
410
411 void OSSL_SERIALIZER_do_all_provided(OPENSSL_CTX *libctx,
412                                      void (*fn)(OSSL_SERIALIZER *ser,
413                                                 void *arg),
414                                      void *arg)
415 {
416     struct serializer_do_all_data_st data;
417
418     data.user_fn = (void (*)(void *, void *))fn;
419     data.user_arg = arg;
420
421     /*
422      * No pre- or post-condition for this call, as this only creates methods
423      * temporarly and then promptly destroys them.
424      */
425     ossl_algorithm_do_all(libctx, OSSL_OP_SERIALIZER, NULL, NULL,
426                           serializer_do_one, NULL, &data);
427 }
428
429 void OSSL_SERIALIZER_names_do_all(const OSSL_SERIALIZER *ser,
430                                   void (*fn)(const char *name, void *data),
431                                   void *data)
432 {
433     if (ser == NULL)
434         return;
435
436     if (ser->prov != NULL) {
437         OPENSSL_CTX *libctx = ossl_provider_library_context(ser->prov);
438         OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
439
440         ossl_namemap_doall_names(namemap, ser->id, fn, data);
441     }
442 }
443
444 const OSSL_PARAM *OSSL_SERIALIZER_settable_ctx_params(OSSL_SERIALIZER *ser)
445 {
446     if (ser != NULL && ser->settable_ctx_params != NULL)
447         return ser->settable_ctx_params();
448     return NULL;
449 }
450
451 /*
452  * Serializer context support
453  */
454
455 /*
456  * |ser| value NULL is valid, and signifies that there is no serializer.
457  * This is useful to provide fallback mechanisms.
458  *  Functions that want to verify if there is a serializer can do so with
459  * OSSL_SERIALIZER_CTX_get_serializer()
460  */
461 OSSL_SERIALIZER_CTX *OSSL_SERIALIZER_CTX_new(OSSL_SERIALIZER *ser)
462 {
463     OSSL_SERIALIZER_CTX *ctx;
464
465     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
466         ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_MALLOC_FAILURE);
467         return NULL;
468     }
469
470     ctx->ser = ser;
471     if (ser != NULL && ser->newctx != NULL) {
472         const OSSL_PROVIDER *prov = OSSL_SERIALIZER_provider(ser);
473         void *provctx = ossl_provider_ctx(prov);
474
475         if (OSSL_SERIALIZER_up_ref(ser)) {
476             ctx->serctx = ser->newctx(provctx);
477         } else {
478             OSSL_SERIALIZER_free(ser);
479             OPENSSL_free(ctx);
480             ctx = NULL;
481         }
482     }
483
484     return ctx;
485 }
486
487 const OSSL_SERIALIZER *
488 OSSL_SERIALIZER_CTX_get_serializer(OSSL_SERIALIZER_CTX *ctx)
489 {
490     if (!ossl_assert(ctx != NULL)) {
491         ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
492         return 0;
493     }
494
495     return ctx->ser;
496 }
497
498
499 int OSSL_SERIALIZER_CTX_set_params(OSSL_SERIALIZER_CTX *ctx,
500                                    const OSSL_PARAM params[])
501 {
502     if (!ossl_assert(ctx != NULL)) {
503         ERR_raise(ERR_LIB_OSSL_SERIALIZER, ERR_R_PASSED_NULL_PARAMETER);
504         return 0;
505     }
506
507     if (ctx->ser != NULL && ctx->ser->set_ctx_params != NULL)
508         return ctx->ser->set_ctx_params(ctx->serctx, params);
509     return 0;
510 }
511
512 void OSSL_SERIALIZER_CTX_free(OSSL_SERIALIZER_CTX *ctx)
513 {
514     if (ctx != NULL) {
515         if (ctx->ser != NULL && ctx->ser->freectx != NULL)
516             ctx->ser->freectx(ctx->serctx);
517         OSSL_SERIALIZER_free(ctx->ser);
518         UI_destroy_method(ctx->allocated_ui_method);
519         OPENSSL_free(ctx);
520     }
521 }