Debian Codegraph @

components/keyrings/common/component_helpers/include/keyring_reader_service_impl_template.h

raw
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #ifndef KEYRING_READER_SERVICE_IMPL_TEMPLATE_INCLUDED
25 #define KEYRING_READER_SERVICE_IMPL_TEMPLATE_INCLUDED
26
27 #include <functional> /* std::function */
28 #include <sstream>
29
30 #include <my_dbug.h>
31 #include <mysql/components/services/log_builtins.h> /* LogComponentErr */
32 #include <mysqld_error.h>
33
34 #include <components/keyrings/common/component_helpers/include/service_requirements.h>
35 #include <components/keyrings/common/data/data.h>
36 #include <components/keyrings/common/data/meta.h>
37 #include <components/keyrings/common/memstore/iterator.h>
38 #include <components/keyrings/common/operations/operations.h>
39
40 using keyring_common::data::Data;
41 using keyring_common::iterator::Iterator;
42 using keyring_common::meta::Metadata;
43 using keyring_common::operations::Keyring_operations;
44
45 namespace keyring_common {
46 namespace service_implementation {
47
48 /**
49 Initialize reader
50
51 @param [in] data_id Data Identifier
52 @param [in] auth_id Authorization ID
53 @param [out] it Iterator
54 @param [in] keyring_operations Reference to the object
55 that handles cache and backend
56 @param [in] callbacks Handle to component specific callbacks
57
58 @returns status of the operation
59 @retval -1 Keyring error. reader_object will not be created.
60 @retval 0 Key not found OR error fetching keys.
61 reader_object will not be created.
62 @retval 1 Key found, check out parameters
63 */
64 template <typename Backend, typename Data_extension = data::Data>
65 int init_reader_template(
66 const char *data_id, const char *auth_id,
67 std::unique_ptr<Iterator<Data_extension>> &it,
68 Keyring_operations<Backend, Data_extension> &keyring_operations,
69 Component_callbacks &callbacks) {
70 try {
71 if (callbacks.keyring_initialized() == false) {
72 return -1;
73 }
74
75 if (data_id == nullptr || !*data_id) {
76 assert(false);
77 return 0;
78 }
79
80 Metadata metadata(data_id, auth_id);
81 if (keyring_operations.init_read_iterator(it, metadata) == true) {
82 return 0;
83 }
84
85 if (keyring_operations.is_valid(it) == false) {
86 LogComponentErr(INFORMATION_LEVEL,
87 ER_NOTE_KEYRING_COMPONENT_READ_DATA_NOT_FOUND, data_id,
88 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
89 keyring_operations.deinit_forward_iterator(it);
90 return 0;
91 }
92
93 return 1;
94 } catch (...) {
95 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "init",
96 "keyring_reader_with_status");
97 return -1;
98 }
99 }
100
101 /**
102 Deinitialize reader
103
104 @param [in, out] it Iterator
105 @param [in] keyring_operations Reference to the object
106 that handles cache and backend
107 @param [in] callbacks Handle to component specific callbacks
108
109 @returns status of the operation
110 @retval false Success
111 @retval true Failure
112 */
113
114 template <typename Backend, typename Data_extension = data::Data>
115 bool deinit_reader_template(
116 std::unique_ptr<Iterator<Data_extension>> &it,
117 Keyring_operations<Backend, Data_extension> &keyring_operations,
118 Component_callbacks &callbacks) {
119 try {
120 if (callbacks.keyring_initialized() == false) {
121 return true;
122 }
123 keyring_operations.deinit_forward_iterator(it);
124 return false;
125 } catch (...) {
126 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "deinit",
127 "keyring_reader_with_status");
128 return true;
129 }
130 }
131
132 /**
133 Fetch length of the data
134
135 @param [in] it Iterator
136 @param [out] data_size Size of fetched data
137 @param [out] data_type_size Size of data type
138 @param [in] keyring_operations Reference to the object
139 that handles cache and backend
140 @param [in] callbacks Handle to component specific callbacks
141 @returns status of the operation
142 @retval false Success
143 @retval true Failure
144 */
145 template <typename Backend, typename Data_extension = data::Data>
146 bool fetch_length_template(
147 std::unique_ptr<Iterator<Data_extension>> &it, size_t *data_size,
148 size_t *data_type_size,
149 Keyring_operations<Backend, Data_extension> &keyring_operations,
150 Component_callbacks &callbacks) {
151 try {
152 if (callbacks.keyring_initialized() == false) {
153 return true;
154 }
155
156 if (data_size == nullptr || data_type_size == nullptr) {
157 assert(false);
158 return true;
159 }
160
161 Data_extension data;
162 Metadata metadata;
163 if (keyring_operations.get_iterator_data(it, metadata, data) == true) {
164 return true;
165 }
166
167 *data_size = data.data().length();
168 *data_type_size = data.type().length();
169 return false;
170 } catch (...) {
171 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "fetch_length",
172 "keyring_reader_with_status");
173 return true;
174 }
175 }
176
177 /**
178 Fetches data from keyring
179
180 @param [in] it Iterator
181 @param [out] data_buffer Out buffer for data
182 @param [in] data_buffer_length Length of out buffer
183 @param [out] data_size Size of fetched data
184 @param [out] data_type_buffer Type of data
185 @param [in] data_type_buffer_length Length of data type buffer
186 @param [out] data_type_size Size of data type
187 @param [in] keyring_operations Reference to the object
188 that handles cache and backend
189 @param [in] callbacks Handle to component specific callbacks
190
191 @returns status of the operation
192 @retval false Success
193 @retval true Failure
194 */
195 template <typename Backend, typename Data_extension = data::Data>
196 bool fetch_template(
197 std::unique_ptr<Iterator<Data_extension>> &it, unsigned char *data_buffer,
198 size_t data_buffer_length, size_t *data_size, char *data_type_buffer,
199 size_t data_type_buffer_length, size_t *data_type_size,
200 Keyring_operations<Backend, Data_extension> &keyring_operations,
201 Component_callbacks &callbacks) {
202 try {
203 if (callbacks.keyring_initialized() == false) {
204 return true;
205 }
206
207 Data_extension data;
208 Metadata metadata;
209 if (keyring_operations.get_iterator_data(it, metadata, data) == true) {
210 return true;
211 }
212
213 if (data_buffer_length < data.data().length() || data_buffer == nullptr) {
214 assert(false);
215 return true;
216 }
217
218 if (data_type_buffer_length < data.type().length() ||
219 data_type_buffer == nullptr) {
220 assert(false);
221 return true;
222 }
223
224 memset(data_buffer, 0, data_buffer_length);
225 memset(data_type_buffer, 0, data_type_buffer_length);
226
227 memcpy(data_buffer, data.data().c_str(), data.data().length());
228 *data_size = data.data().length();
229
230 memcpy(data_type_buffer, data.type().c_str(), data.type().length());
231 *data_type_size = data.type().length();
232
233 return false;
234 } catch (...) {
235 memset(data_buffer, 0, data_buffer_length);
236 memset(data_type_buffer, 0, data_type_buffer_length);
237 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "fetch",
238 "keyring_reader_with_status");
239 return true;
240 }
241 }
242
243 } // namespace service_implementation
244 } // namespace keyring_common
245
246 #endif // KEYRING_READER_SERVICE_IMPL_TEMPLATE_INCLUDED