2 * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
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
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"
22 * Serializer can have multiple names, separated with colons in a name string
24 #define NAME_SEPARATOR ':'
26 /* Simple method structure constructor and destructor */
27 static OSSL_SERIALIZER
*ossl_serializer_new(void)
29 OSSL_SERIALIZER
*ser
= NULL
;
31 if ((ser
= OPENSSL_zalloc(sizeof(*ser
))) == NULL
32 || (ser
->base
.lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
33 OSSL_SERIALIZER_free(ser
);
34 ERR_raise(ERR_LIB_OSSL_SERIALIZER
, ERR_R_MALLOC_FAILURE
);
43 int OSSL_SERIALIZER_up_ref(OSSL_SERIALIZER
*ser
)
47 CRYPTO_UP_REF(&ser
->base
.refcnt
, &ref
, ser
->base
.lock
);
51 void OSSL_SERIALIZER_free(OSSL_SERIALIZER
*ser
)
58 CRYPTO_DOWN_REF(&ser
->base
.refcnt
, &ref
, ser
->base
.lock
);
61 ossl_provider_free(ser
->base
.prov
);
62 CRYPTO_THREAD_lock_free(ser
->base
.lock
);
66 /* Permanent serializer method store, constructor and destructor */
67 static void serializer_store_free(void *vstore
)
69 ossl_method_store_free(vstore
);
72 static void *serializer_store_new(OPENSSL_CTX
*ctx
)
74 return ossl_method_store_new(ctx
);
78 static const OPENSSL_CTX_METHOD serializer_store_method
= {
80 serializer_store_free
,
83 /* Data to be passed through ossl_method_construct() */
84 struct serializer_data_st
{
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() */
93 * Generic routines to fetch / create SERIALIZER methods with
94 * ossl_method_construct()
97 /* Temporary serializer method store, constructor and destructor */
98 static void *alloc_tmp_serializer_store(OPENSSL_CTX
*ctx
)
100 return ossl_method_store_new(ctx
);
103 static void dealloc_tmp_serializer_store(void *store
)
106 ossl_method_store_free(store
);
109 /* Get the permanent serializer store */
110 static OSSL_METHOD_STORE
*get_serializer_store(OPENSSL_CTX
*libctx
)
112 return openssl_ctx_get_data(libctx
, OPENSSL_CTX_SERIALIZER_STORE_INDEX
,
113 &serializer_store_method
);
116 /* Get serializer methods from a store, or put one in */
117 static void *get_serializer_from_store(OPENSSL_CTX
*libctx
, void *store
,
120 struct serializer_data_st
*methdata
= data
;
124 if ((id
= methdata
->id
) == 0) {
125 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
127 id
= ossl_namemap_name2num(namemap
, methdata
->names
);
131 && (store
= get_serializer_store(libctx
)) == NULL
)
134 if (!ossl_method_store_fetch(store
, id
, methdata
->propquery
, &method
))
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
)
144 OSSL_NAMEMAP
*namemap
;
147 if ((namemap
= ossl_namemap_stored(libctx
)) == NULL
148 || (id
= ossl_namemap_name2num(namemap
, names
)) == 0)
151 if (store
== NULL
&& (store
= get_serializer_store(libctx
)) == NULL
)
154 return ossl_method_store_add(store
, prov
, id
, propdef
, method
,
155 (int (*)(void *))OSSL_SERIALIZER_up_ref
,
156 (void (*)(void *))OSSL_SERIALIZER_free
);
159 /* Create and populate a serializer method */
160 static void *serializer_from_dispatch(int id
, const OSSL_ALGORITHM
*algodef
,
163 OSSL_SERIALIZER
*ser
= NULL
;
164 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
166 if ((ser
= ossl_serializer_new()) == NULL
)
169 ser
->base
.propdef
= algodef
->property_definition
;
171 for (; fns
->function_id
!= 0; fns
++) {
172 switch (fns
->function_id
) {
173 case OSSL_FUNC_SERIALIZER_NEWCTX
:
174 if (ser
->newctx
== NULL
)
176 OSSL_FUNC_serializer_newctx(fns
);
178 case OSSL_FUNC_SERIALIZER_FREECTX
:
179 if (ser
->freectx
== NULL
)
181 OSSL_FUNC_serializer_freectx(fns
);
183 case OSSL_FUNC_SERIALIZER_SET_CTX_PARAMS
:
184 if (ser
->set_ctx_params
== NULL
)
185 ser
->set_ctx_params
=
186 OSSL_FUNC_serializer_set_ctx_params(fns
);
188 case OSSL_FUNC_SERIALIZER_SETTABLE_CTX_PARAMS
:
189 if (ser
->settable_ctx_params
== NULL
)
190 ser
->settable_ctx_params
=
191 OSSL_FUNC_serializer_settable_ctx_params(fns
);
193 case OSSL_FUNC_SERIALIZER_SERIALIZE_DATA
:
194 if (ser
->serialize_data
== NULL
)
195 ser
->serialize_data
=
196 OSSL_FUNC_serializer_serialize_data(fns
);
198 case OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT
:
199 if (ser
->serialize_object
== NULL
)
200 ser
->serialize_object
=
201 OSSL_FUNC_serializer_serialize_object(fns
);
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.
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
);
218 if (prov
!= NULL
&& !ossl_provider_up_ref(prov
)) {
219 OSSL_SERIALIZER_free(ser
);
223 ser
->base
.prov
= prov
;
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.
233 static void *construct_serializer(const OSSL_ALGORITHM
*algodef
,
234 OSSL_PROVIDER
*prov
, void *unused
)
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.
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
);
249 method
= serializer_from_dispatch(id
, algodef
, prov
);
254 /* Intermediary function to avoid ugly casts, used below */
255 static void destruct_serializer(void *method
, void *data
)
257 OSSL_SERIALIZER_free(method
);
260 static int up_ref_serializer(void *method
)
262 return OSSL_SERIALIZER_up_ref(method
);
265 static void free_serializer(void *method
)
267 OSSL_SERIALIZER_free(method
);
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
)
275 OSSL_METHOD_STORE
*store
= get_serializer_store(libctx
);
276 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
279 if (store
== NULL
|| namemap
== NULL
)
283 * If we have been passed neither a name_id or a name, we have an
284 * internal programming error.
286 if (!ossl_assert(id
!= 0 || name
!= NULL
))
290 id
= ossl_namemap_name2num(namemap
, name
);
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
,
302 struct serializer_data_st mcmdata
;
304 mcmdata
.libctx
= libctx
;
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
) {
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.
319 id
= ossl_namemap_name2num(namemap
, name
);
320 ossl_method_store_cache_set(store
, id
, properties
, method
,
321 up_ref_serializer
, free_serializer
);
328 OSSL_SERIALIZER
*OSSL_SERIALIZER_fetch(OPENSSL_CTX
*libctx
, const char *name
,
329 const char *properties
)
331 return inner_ossl_serializer_fetch(libctx
, 0, name
, properties
);
334 OSSL_SERIALIZER
*ossl_serializer_fetch_by_number(OPENSSL_CTX
*libctx
, int id
,
335 const char *properties
)
337 return inner_ossl_serializer_fetch(libctx
, id
, NULL
, properties
);
341 * Library of basic method functions
344 const OSSL_PROVIDER
*OSSL_SERIALIZER_provider(const OSSL_SERIALIZER
*ser
)
346 if (!ossl_assert(ser
!= NULL
)) {
347 ERR_raise(ERR_LIB_OSSL_SERIALIZER
, ERR_R_PASSED_NULL_PARAMETER
);
351 return ser
->base
.prov
;
354 const char *OSSL_SERIALIZER_properties(const OSSL_SERIALIZER
*ser
)
356 if (!ossl_assert(ser
!= NULL
)) {
357 ERR_raise(ERR_LIB_OSSL_SERIALIZER
, ERR_R_PASSED_NULL_PARAMETER
);
361 return ser
->base
.propdef
;
364 int OSSL_SERIALIZER_number(const OSSL_SERIALIZER
*ser
)
366 if (!ossl_assert(ser
!= NULL
)) {
367 ERR_raise(ERR_LIB_OSSL_SERIALIZER
, ERR_R_PASSED_NULL_PARAMETER
);
374 int OSSL_SERIALIZER_is_a(const OSSL_SERIALIZER
*ser
, const char *name
)
376 if (ser
->base
.prov
!= NULL
) {
377 OPENSSL_CTX
*libctx
= ossl_provider_library_context(ser
->base
.prov
);
378 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
380 return ossl_namemap_name2num(namemap
, name
) == ser
->base
.id
;
385 struct serializer_do_all_data_st
{
386 void (*user_fn
)(void *method
, void *arg
);
390 static void serializer_do_one(OSSL_PROVIDER
*provider
,
391 const OSSL_ALGORITHM
*algodef
,
392 int no_store
, void *vdata
)
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
);
403 serializer_from_dispatch(id
, algodef
, provider
);
405 if (method
!= NULL
) {
406 data
->user_fn(method
, data
->user_arg
);
407 OSSL_SERIALIZER_free(method
);
411 void OSSL_SERIALIZER_do_all_provided(OPENSSL_CTX
*libctx
,
412 void (*fn
)(OSSL_SERIALIZER
*ser
,
416 struct serializer_do_all_data_st data
;
418 data
.user_fn
= (void (*)(void *, void *))fn
;
422 * No pre- or post-condition for this call, as this only creates methods
423 * temporarly and then promptly destroys them.
425 ossl_algorithm_do_all(libctx
, OSSL_OP_SERIALIZER
, NULL
, NULL
,
426 serializer_do_one
, NULL
, &data
);
429 void OSSL_SERIALIZER_names_do_all(const OSSL_SERIALIZER
*ser
,
430 void (*fn
)(const char *name
, void *data
),
436 if (ser
->base
.prov
!= NULL
) {
437 OPENSSL_CTX
*libctx
= ossl_provider_library_context(ser
->base
.prov
);
438 OSSL_NAMEMAP
*namemap
= ossl_namemap_stored(libctx
);
440 ossl_namemap_doall_names(namemap
, ser
->base
.id
, fn
, data
);
444 const OSSL_PARAM
*OSSL_SERIALIZER_settable_ctx_params(OSSL_SERIALIZER
*ser
)
446 if (ser
!= NULL
&& ser
->settable_ctx_params
!= NULL
)
447 return ser
->settable_ctx_params();
452 * Serializer context support
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()
461 OSSL_SERIALIZER_CTX
*OSSL_SERIALIZER_CTX_new(OSSL_SERIALIZER
*ser
)
463 OSSL_SERIALIZER_CTX
*ctx
;
465 if ((ctx
= OPENSSL_zalloc(sizeof(*ctx
))) == NULL
) {
466 ERR_raise(ERR_LIB_OSSL_SERIALIZER
, ERR_R_MALLOC_FAILURE
);
471 if (ser
!= NULL
&& ser
->newctx
!= NULL
) {
472 const OSSL_PROVIDER
*prov
= OSSL_SERIALIZER_provider(ser
);
473 void *provctx
= ossl_provider_ctx(prov
);
475 if (OSSL_SERIALIZER_up_ref(ser
)) {
476 ctx
->serctx
= ser
->newctx(provctx
);
478 OSSL_SERIALIZER_free(ser
);
487 const OSSL_SERIALIZER
*
488 OSSL_SERIALIZER_CTX_get_serializer(OSSL_SERIALIZER_CTX
*ctx
)
490 if (!ossl_assert(ctx
!= NULL
)) {
491 ERR_raise(ERR_LIB_OSSL_SERIALIZER
, ERR_R_PASSED_NULL_PARAMETER
);
499 int OSSL_SERIALIZER_CTX_set_params(OSSL_SERIALIZER_CTX
*ctx
,
500 const OSSL_PARAM params
[])
502 if (!ossl_assert(ctx
!= NULL
)) {
503 ERR_raise(ERR_LIB_OSSL_SERIALIZER
, ERR_R_PASSED_NULL_PARAMETER
);
507 if (ctx
->ser
!= NULL
&& ctx
->ser
->set_ctx_params
!= NULL
)
508 return ctx
->ser
->set_ctx_params(ctx
->serctx
, params
);
512 void OSSL_SERIALIZER_CTX_free(OSSL_SERIALIZER_CTX
*ctx
)
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
);