DESERIALIZER: Implement decryption of password protected objects
[openssl.git] / crypto / serializer / serializer_local.h
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_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"
17
18 struct ossl_serdes_base_st {
19     OSSL_PROVIDER *prov;
20     int id;
21     const char *propdef;
22
23     CRYPTO_REF_COUNT refcnt;
24     CRYPTO_RWLOCK *lock;
25 };
26
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;
35 };
36
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;
47 };
48
49 struct ossl_serializer_ctx_st {
50     OSSL_SERIALIZER *ser;
51     void *serctx;
52
53     int selection;
54
55     /*-
56      * Output / serializing data, used by OSSL_SERIALIZER_to_{bio,fp}
57      *
58      * |object|         is the libcrypto object to handle.
59      * |do_output|      performs the actual serialization.
60      *
61      * |do_output| must have intimate knowledge of |object|.
62      */
63     const void *object;
64     int (*do_output)(OSSL_SERIALIZER_CTX *ctx, BIO *out);
65
66     /* For any function that needs a passphrase reader */
67     const UI_METHOD *ui_method;
68     void *ui_data;
69     /*
70      * if caller used OSSL_SERIALIZER_CTX_set_passphrase_cb(), we need
71      * intermediary storage.
72      */
73     UI_METHOD *allocated_ui_method;
74 };
75
76 struct ossl_deserializer_instance_st {
77     OSSL_DESERIALIZER *deser;    /* Never NULL */
78     void *deserctx;              /* Never NULL */
79     const char *input_type;      /* Never NULL */
80 };
81
82 DEFINE_STACK_OF(OSSL_DESERIALIZER_INSTANCE)
83
84 struct ossl_deserializer_ctx_st {
85     /*
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.
90      */
91     const char *start_input_type;
92
93     /*
94      * Deserializers that are components of any current deserialization path.
95      */
96     STACK_OF(OSSL_DESERIALIZER_INSTANCE) *deser_insts;
97
98     /*
99      * The finalizer of a deserialization, and its caller argument.
100      */
101     OSSL_DESERIALIZER_FINALIZER *finalizer;
102     OSSL_DESERIALIZER_CLEANER *cleaner;
103     void *finalize_arg;
104
105     /* For any function that needs a passphrase reader */
106     const UI_METHOD *ui_method;
107     void *ui_data;
108     /*
109      * if caller used OSSL_SERIALIZER_CTX_set_passphrase_cb(), we need
110      * intermediary storage.
111      */
112     UI_METHOD *allocated_ui_method;
113     /*
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.
117      */
118     unsigned char *cached_passphrase;
119     size_t cached_passphrase_len;
120 };
121
122 /* Passphrase callbacks, found in serdes_pass.c */
123
124 /*
125  * Serializers typically want to get an outgoing passphrase, while
126  * deserializers typically want to get en incoming passphrase.
127  */
128 OSSL_PASSPHRASE_CALLBACK ossl_serializer_passphrase_out_cb;
129 OSSL_PASSPHRASE_CALLBACK ossl_deserializer_passphrase_in_cb;