| 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_GENERATOR_SERVICE_IMPL_TEMPLATE_INCLUDED |
| 25 |
#define KEYRING_GENERATOR_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/meta.h> |
| 36 |
#include <components/keyrings/common/operations/operations.h> |
| 37 |
|
| 38 |
namespace keyring_common { |
| 39 |
namespace service_implementation { |
| 40 |
|
| 41 |
using keyring_common::meta::Metadata; |
| 42 |
using keyring_common::operations::Keyring_operations; |
| 43 |
|
| 44 |
/** |
| 45 |
Generate data and store in keyring |
| 46 |
|
| 47 |
@param [in] data_id Data Identifier |
| 48 |
@param [in] auth_id Authorization ID |
| 49 |
@param [in] data_type Type of data. Assumed null terminated. |
| 50 |
@param [in] data_size Size of the data to be generated |
| 51 |
@param [in] keyring_operations Reference to the object |
| 52 |
that handles cache and backend |
| 53 |
@param [in] callbacks Handle to component specific callbacks |
| 54 |
|
| 55 |
@returns status of the operation |
| 56 |
@retval false Success - Key generated and stored in keyring. |
| 57 |
@retval true Failure |
| 58 |
*/ |
| 59 |
template <typename Backend, typename Data_extension = data::Data> |
| 60 |
bool generate_template( |
| 61 |
const char *data_id, const char *auth_id, const char *data_type, |
| 62 |
size_t data_size, |
| 63 |
Keyring_operations<Backend, Data_extension> &keyring_operations, |
| 64 |
Component_callbacks &callbacks) { |
| 65 |
try { |
| 66 |
if (callbacks.keyring_initialized() == false) { |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 70 |
if (data_id == nullptr || !*data_id) { |
| 71 |
assert(false); |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
if (data_size > keyring_operations.maximum_data_length()) { |
| 76 |
LogComponentErr(INFORMATION_LEVEL, |
| 77 |
ER_NOTE_KEYRING_COMPONENT_WRITE_MAXIMUM_DATA_LENGTH, |
| 78 |
keyring_operations.maximum_data_length()); |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
Metadata metadata(data_id, auth_id); |
| 83 |
if (keyring_operations.generate(metadata, data_type, data_size) == true) { |
| 84 |
LogComponentErr(INFORMATION_LEVEL, |
| 85 |
ER_NOTE_KEYRING_COMPONENT_GENERATE_FAILED, data_id, |
| 86 |
(auth_id == nullptr || !*auth_id) ? "NULL" : auth_id); |
| 87 |
return true; |
| 88 |
} |
| 89 |
return false; |
| 90 |
} catch (...) { |
| 91 |
LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "generate", |
| 92 |
"keyring_generate"); |
| 93 |
return true; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
} // namespace service_implementation |
| 98 |
} // namespace keyring_common |
| 99 |
|
| 100 |
#endif // !KEYRING_GENERATOR_SERVICE_IMPL_TEMPLATE_INCLUDED |