| 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 |
#include <cstring> |
| 25 |
#include <memory> |
| 26 |
|
| 27 |
#include "keyring_file.h" |
| 28 |
|
| 29 |
/* Keyring_encryption_service_impl */ |
| 30 |
#include <components/keyrings/common/component_helpers/include/keyring_encryption_service_definition.h> |
| 31 |
/* Keyring_generator_service_impl */ |
| 32 |
#include <components/keyrings/common/component_helpers/include/keyring_generator_service_definition.h> |
| 33 |
/* Keyring_load_service_impl */ |
| 34 |
#include <components/keyrings/common/component_helpers/include/keyring_load_service_definition.h> |
| 35 |
/* Keyring_keys_metadata_iterator_service_impl */ |
| 36 |
#include <components/keyrings/common/component_helpers/include/keyring_keys_metadata_iterator_service_definition.h> |
| 37 |
/* Log_builtins_keyring */ |
| 38 |
#include <components/keyrings/common/component_helpers/include/keyring_log_builtins_definition.h> |
| 39 |
/* Keyring_metadata_query_service_impl */ |
| 40 |
#include <components/keyrings/common/component_helpers/include/keyring_metadata_query_service_definition.h> |
| 41 |
/* Keyring_reader_service_impl */ |
| 42 |
#include <components/keyrings/common/component_helpers/include/keyring_reader_service_definition.h> |
| 43 |
/* Keyring_writer_service_impl */ |
| 44 |
#include <components/keyrings/common/component_helpers/include/keyring_writer_service_definition.h> |
| 45 |
|
| 46 |
/* clang-format off */ |
| 47 |
/** |
| 48 |
@page PAGE_COMPONENT_KEYRING_FILE component_keyring_file |
| 49 |
|
| 50 |
This is keyring component services' implementation with file as backend to |
| 51 |
store data. This component implements following keyring services: |
| 52 |
|
| 53 |
- keyring_aes |
| 54 |
- keyring_generate |
| 55 |
- keyring_keys_metadata_iterator |
| 56 |
- keyring_component_status |
| 57 |
- keyring_metadata_query |
| 58 |
- keyring_reader |
| 59 |
- keyring_reload |
| 60 |
- keyring_writer |
| 61 |
|
| 62 |
Data is stored in JSON format. |
| 63 |
@code |
| 64 |
{ |
| 65 |
"version": "1.0", |
| 66 |
"elements": [ |
| 67 |
{ |
| 68 |
"user": "<user_name>", |
| 69 |
"data_id": "<name>", |
| 70 |
"data_type": "<data_type>", |
| 71 |
"data": "<hex_of_data>", |
| 72 |
"extension": [] |
| 73 |
}, |
| 74 |
... |
| 75 |
... |
| 76 |
] |
| 77 |
} |
| 78 |
@endcode |
| 79 |
|
| 80 |
For most parts, component_keyring_file relies on keyring_common library |
| 81 |
for implementation. |
| 82 |
|
| 83 |
The component relies on component_keyring_file.cnf file for configuration. |
| 84 |
|
| 85 |
Location of this configuration file is same directory where component_keyring_file |
| 86 |
shared library is located. This configuration file should contain information |
| 87 |
in one of the following formats. |
| 88 |
|
| 89 |
1. Signal component to read configuration from current working directory |
| 90 |
@code |
| 91 |
{ |
| 92 |
"read_local_config": true |
| 93 |
} |
| 94 |
@endcode |
| 95 |
|
| 96 |
2. Details of data file and nature of keyring |
| 97 |
@code |
| 98 |
{ |
| 99 |
"path": <path to data file>, |
| 100 |
"read_only": <boolean value to signal state of the keyring> |
| 101 |
} |
| 102 |
@endcode |
| 103 |
|
| 104 |
If configuration file co-located with shared library signals to read |
| 105 |
configuration locally, current working directory is searched for |
| 106 |
component_keyring_file.cnf and expected format is 2. |
| 107 |
|
| 108 |
The component exposes following status information through |
| 109 |
keyring_metadata_query service implementation. |
| 110 |
|
| 111 |
1. Name of the keyring |
| 112 |
2. Author |
| 113 |
3. Implementation name |
| 114 |
4. Version |
| 115 |
5. Component status |
| 116 |
6. Data file location |
| 117 |
7. Read only status |
| 118 |
|
| 119 |
<b> |
| 120 |
Note: Implementation does not provide concurrency control. |
| 121 |
That is responsibility of users of the services. |
| 122 |
</b> |
| 123 |
*/ |
| 124 |
/* clang-format on */ |
| 125 |
|
| 126 |
using keyring_common::operations::Keyring_operations; |
| 127 |
using keyring_file::backend::Keyring_file_backend; |
| 128 |
using keyring_file::config::Config_pod; |
| 129 |
using keyring_file::config::g_component_path; |
| 130 |
using keyring_file::config::g_instance_path; |
| 131 |
|
| 132 |
/** Dependencies */ |
| 133 |
REQUIRES_SERVICE_PLACEHOLDER(log_builtins); |
| 134 |
REQUIRES_SERVICE_PLACEHOLDER(log_builtins_string); |
| 135 |
|
| 136 |
SERVICE_TYPE(log_builtins) * log_bi; |
| 137 |
SERVICE_TYPE(log_builtins_string) * log_bs; |
| 138 |
|
| 139 |
namespace keyring_file { |
| 140 |
/** Keyring operations object */ |
| 141 |
Keyring_operations<Keyring_file_backend> *g_keyring_operations = nullptr; |
| 142 |
|
| 143 |
/** Keyring data source */ |
| 144 |
Config_pod *g_config_pod = nullptr; |
| 145 |
|
| 146 |
/** Keyring state */ |
| 147 |
bool g_keyring_file_inited = false; |
| 148 |
|
| 149 |
/** |
| 150 |
Set path to component |
| 151 |
|
| 152 |
@param [in] component_path Path to component library |
| 153 |
@param [in] instance_path Path to instance specific config |
| 154 |
|
| 155 |
@returns initialization status |
| 156 |
@retval false Successful initialization |
| 157 |
@retval true Error |
| 158 |
*/ |
| 159 |
bool set_paths(const char *component_path, const char *instance_path) { |
| 160 |
char *save_c = g_component_path; |
| 161 |
char *save_i = g_instance_path; |
| 162 |
g_component_path = strdup(component_path != nullptr ? component_path : ""); |
| 163 |
g_instance_path = strdup(instance_path != nullptr ? instance_path : ""); |
| 164 |
if (g_component_path == nullptr || g_instance_path == nullptr) { |
| 165 |
g_component_path = save_c; |
| 166 |
g_instance_path = save_i; |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
if (save_c != nullptr) free(save_c); |
| 171 |
if (save_i != nullptr) free(save_i); |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
Initialize or re-initialize keyring. |
| 177 |
1. Read configuration file |
| 178 |
2. Read keyring file |
| 179 |
3. Initialize internal cache |
| 180 |
|
| 181 |
@returns Status of read operations |
| 182 |
@retval false Read config and data |
| 183 |
@retval true Error reading config or data. |
| 184 |
Existing data remains as it is. |
| 185 |
*/ |
| 186 |
bool init_or_reinit_keyring() { |
| 187 |
/* Get config */ |
| 188 |
std::unique_ptr<Config_pod> new_config_pod; |
| 189 |
if (keyring_file::config::find_and_read_config_file(new_config_pod)) |
| 190 |
return true; |
| 191 |
|
| 192 |
/* Initialize backend handler */ |
| 193 |
std::unique_ptr<Keyring_file_backend> new_backend = |
| 194 |
std::make_unique<Keyring_file_backend>( |
| 195 |
new_config_pod.get()->config_file_path_, |
| 196 |
new_config_pod.get()->read_only_); |
| 197 |
if (!new_backend || !new_backend.get()->valid()) return true; |
| 198 |
|
| 199 |
/* Create new operations class */ |
| 200 |
Keyring_operations<Keyring_file_backend> *new_operations = new (std::nothrow) |
| 201 |
Keyring_operations<Keyring_file_backend>(true, new_backend.release()); |
| 202 |
if (new_operations == nullptr) return true; |
| 203 |
|
| 204 |
if (!new_operations->valid()) { |
| 205 |
delete new_operations; |
| 206 |
return true; |
| 207 |
} |
| 208 |
|
| 209 |
std::swap(g_keyring_operations, new_operations); |
| 210 |
Config_pod *current = g_config_pod; |
| 211 |
g_config_pod = new_config_pod.release(); |
| 212 |
if (current != nullptr) delete current; |
| 213 |
if (new_operations != nullptr) delete new_operations; |
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
Initialization function for component - Used when loading the component |
| 219 |
*/ |
| 220 |
static mysql_service_status_t keyring_file_init() { |
| 221 |
log_bi = mysql_service_log_builtins; |
| 222 |
log_bs = mysql_service_log_builtins_string; |
| 223 |
|
| 224 |
g_component_callbacks = new (std::nothrow) |
| 225 |
keyring_common::service_implementation::Component_callbacks(); |
| 226 |
|
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
De-initialization function for component - Used when unloading the component |
| 232 |
*/ |
| 233 |
static mysql_service_status_t keyring_file_deinit() { |
| 234 |
g_keyring_file_inited = false; |
| 235 |
if (g_component_path) free(g_component_path); |
| 236 |
g_component_path = nullptr; |
| 237 |
if (g_instance_path) free(g_instance_path); |
| 238 |
g_instance_path = nullptr; |
| 239 |
|
| 240 |
if (g_keyring_operations != nullptr) delete g_keyring_operations; |
| 241 |
g_keyring_operations = nullptr; |
| 242 |
|
| 243 |
if (g_config_pod) delete g_config_pod; |
| 244 |
g_config_pod = nullptr; |
| 245 |
|
| 246 |
if (g_component_callbacks) delete g_component_callbacks; |
| 247 |
g_component_callbacks = nullptr; |
| 248 |
|
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
} // namespace keyring_file |
| 253 |
|
| 254 |
/** ======================================================================= */ |
| 255 |
|
| 256 |
/** Component declaration related stuff */ |
| 257 |
|
| 258 |
/** This component provides implementation of following component services */ |
| 259 |
KEYRING_AES_IMPLEMENTOR(component_keyring_file); |
| 260 |
KEYRING_GENERATOR_IMPLEMENTOR(component_keyring_file); |
| 261 |
KEYRING_LOAD_IMPLEMENTOR(component_keyring_file); |
| 262 |
KEYRING_KEYS_METADATA_FORWARD_ITERATOR_IMPLEMENTOR(component_keyring_file); |
| 263 |
KEYRING_COMPONENT_STATUS_IMPLEMENTOR(component_keyring_file); |
| 264 |
KEYRING_COMPONENT_METADATA_QUERY_IMPLEMENTOR(component_keyring_file); |
| 265 |
KEYRING_READER_IMPLEMENTOR(component_keyring_file); |
| 266 |
KEYRING_WRITER_IMPLEMENTOR(component_keyring_file); |
| 267 |
/* Used if log_builtins is not available */ |
| 268 |
KEYRING_LOG_BUILTINS_IMPLEMENTOR(component_keyring_file); |
| 269 |
KEYRING_LOG_BUILTINS_STRING_IMPLEMENTOR(component_keyring_file); |
| 270 |
|
| 271 |
/** Component provides */ |
| 272 |
BEGIN_COMPONENT_PROVIDES(component_keyring_file) |
| 273 |
PROVIDES_SERVICE(component_keyring_file, keyring_aes), |
| 274 |
PROVIDES_SERVICE(component_keyring_file, keyring_generator), |
| 275 |
PROVIDES_SERVICE(component_keyring_file, keyring_load), |
| 276 |
PROVIDES_SERVICE(component_keyring_file, keyring_keys_metadata_iterator), |
| 277 |
PROVIDES_SERVICE(component_keyring_file, keyring_component_status), |
| 278 |
PROVIDES_SERVICE(component_keyring_file, keyring_component_metadata_query), |
| 279 |
PROVIDES_SERVICE(component_keyring_file, keyring_reader_with_status), |
| 280 |
PROVIDES_SERVICE(component_keyring_file, keyring_writer), |
| 281 |
PROVIDES_SERVICE(component_keyring_file, log_builtins), |
| 282 |
PROVIDES_SERVICE(component_keyring_file, log_builtins_string), |
| 283 |
END_COMPONENT_PROVIDES(); |
| 284 |
|
| 285 |
/** List of dependencies */ |
| 286 |
BEGIN_COMPONENT_REQUIRES(component_keyring_file) |
| 287 |
REQUIRES_SERVICE(registry), REQUIRES_SERVICE(log_builtins), |
| 288 |
REQUIRES_SERVICE(log_builtins_string), END_COMPONENT_REQUIRES(); |
| 289 |
|
| 290 |
/** Component description */ |
| 291 |
BEGIN_COMPONENT_METADATA(component_keyring_file) |
| 292 |
METADATA("mysql.author", "Oracle Corporation"), |
| 293 |
METADATA("mysql.license", "GPL"), |
| 294 |
METADATA("component_keyring_file_service", "1"), END_COMPONENT_METADATA(); |
| 295 |
|
| 296 |
/** Component declaration */ |
| 297 |
DECLARE_COMPONENT(component_keyring_file, "component_keyring_file") |
| 298 |
keyring_file::keyring_file_init, |
| 299 |
keyring_file::keyring_file_deinit END_DECLARE_COMPONENT(); |
| 300 |
|
| 301 |
/** Component contained in this library */ |
| 302 |
DECLARE_LIBRARY_COMPONENTS &COMPONENT_REF(component_keyring_file) |
| 303 |
END_DECLARE_LIBRARY_COMPONENTS |