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_dispatch.h>
11 #include <openssl/types.h>
12 #include <openssl/safestack.h>
13 #include <openssl/serializer.h>
14 #include <openssl/deserializer.h>
15 #include "internal/cryptlib.h"
16 #include "internal/refcount.h"
18 struct ossl_serdes_base_st {
23 CRYPTO_REF_COUNT refcnt;
27 struct ossl_serializer_st {
28 struct ossl_serdes_base_st base;
29 OSSL_FUNC_serializer_newctx_fn *newctx;
30 OSSL_FUNC_serializer_freectx_fn *freectx;
31 OSSL_FUNC_serializer_set_ctx_params_fn *set_ctx_params;
32 OSSL_FUNC_serializer_settable_ctx_params_fn *settable_ctx_params;
33 OSSL_FUNC_serializer_serialize_data_fn *serialize_data;
34 OSSL_FUNC_serializer_serialize_object_fn *serialize_object;
37 struct ossl_deserializer_st {
38 struct ossl_serdes_base_st base;
39 OSSL_FUNC_deserializer_newctx_fn *newctx;
40 OSSL_FUNC_deserializer_freectx_fn *freectx;
41 OSSL_FUNC_deserializer_get_params_fn *get_params;
42 OSSL_FUNC_deserializer_gettable_params_fn *gettable_params;
43 OSSL_FUNC_deserializer_set_ctx_params_fn *set_ctx_params;
44 OSSL_FUNC_deserializer_settable_ctx_params_fn *settable_ctx_params;
45 OSSL_FUNC_deserializer_deserialize_fn *deserialize;
46 OSSL_FUNC_deserializer_export_object_fn *export_object;
49 struct ossl_serializer_ctx_st {
56 * Output / serializing data, used by OSSL_SERIALIZER_to_{bio,fp}
58 * |object| is the libcrypto object to handle.
59 * |do_output| performs the actual serialization.
61 * |do_output| must have intimate knowledge of |object|.
64 int (*do_output)(OSSL_SERIALIZER_CTX *ctx, BIO *out);
66 /* For any function that needs a passphrase reader */
67 const UI_METHOD *ui_method;
70 * if caller used OSSL_SERIALIZER_CTX_set_passphrase_cb(), we need
71 * intermediary storage.
73 UI_METHOD *allocated_ui_method;
76 struct ossl_deserializer_instance_st {
77 OSSL_DESERIALIZER *deser; /* Never NULL */
78 void *deserctx; /* Never NULL */
79 const char *input_type; /* Never NULL */
82 DEFINE_STACK_OF(OSSL_DESERIALIZER_INSTANCE)
84 struct ossl_deserializer_ctx_st {
86 * The caller may know the input type of the data they pass. If not,
87 * this will remain NULL and the deserializing functionality will start
88 * with trying to deserialize with any desserializer in |deser_insts|,
89 * regardless of their respective input type.
91 const char *start_input_type;
94 * Deserializers that are components of any current deserialization path.
96 STACK_OF(OSSL_DESERIALIZER_INSTANCE) *deser_insts;
99 * The finalizer of a deserialization, and its caller argument.
101 OSSL_DESERIALIZER_FINALIZER *finalizer;
102 OSSL_DESERIALIZER_CLEANER *cleaner;
105 /* For any function that needs a passphrase reader */
106 const UI_METHOD *ui_method;
109 * if caller used OSSL_SERIALIZER_CTX_set_passphrase_cb(), we need
110 * intermediary storage.
112 UI_METHOD *allocated_ui_method;
114 * Because the same input may pass through more than one deserializer,
115 * we cache any passphrase passed to us. The desrializing processor
116 * must clear this at the end of a run.
118 unsigned char *cached_passphrase;
119 size_t cached_passphrase_len;
122 /* Passphrase callbacks, found in serdes_pass.c */
125 * Serializers typically want to get an outgoing passphrase, while
126 * deserializers typically want to get en incoming passphrase.
128 OSSL_PASSPHRASE_CALLBACK ossl_serializer_passphrase_out_cb;
129 OSSL_PASSPHRASE_CALLBACK ossl_deserializer_passphrase_in_cb;