| 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_ENCRYPTION_SERVICE_IMPL_TEMPLATE_INCLUDED |
| 25 |
#define KEYRING_ENCRYPTION_SERVICE_IMPL_TEMPLATE_INCLUDED |
| 26 |
|
| 27 |
#include <algorithm> /* std::transform */ |
| 28 |
#include <cctype> |
| 29 |
#include <cstring> |
| 30 |
#include <functional> |
| 31 |
#include <memory> /* std::unique_ptr */ |
| 32 |
#include <sstream> /* std::stringstream */ |
| 33 |
|
| 34 |
#include <my_dbug.h> |
| 35 |
#include <mysql/components/services/log_builtins.h> /* LogComponentErr */ |
| 36 |
#include <mysqld_error.h> |
| 37 |
#include <scope_guard.h> |
| 38 |
|
| 39 |
#include <components/keyrings/common/component_helpers/include/keyring_reader_service_impl_template.h> |
| 40 |
#include <components/keyrings/common/component_helpers/include/service_requirements.h> |
| 41 |
#include <components/keyrings/common/data/data.h> |
| 42 |
#include <components/keyrings/common/encryption/aes.h> /* AES encryption */ |
| 43 |
#include <components/keyrings/common/operations/operations.h> |
| 44 |
|
| 45 |
namespace keyring_common { |
| 46 |
|
| 47 |
using aes_encryption::aes_decrypt; |
| 48 |
using aes_encryption::aes_encrypt; |
| 49 |
using aes_encryption::Aes_operation_context; |
| 50 |
using aes_encryption::aes_return_status; |
| 51 |
using aes_encryption::get_ciphertext_size; |
| 52 |
using aes_encryption::Keyring_aes_opmode; |
| 53 |
using data::Data; |
| 54 |
using operations::Keyring_operations; |
| 55 |
|
| 56 |
namespace service_implementation { |
| 57 |
/** |
| 58 |
Retrieve required out buffer length information |
| 59 |
|
| 60 |
@param [in] input_length Length of input text |
| 61 |
@param [in] mode Block encryption mode |
| 62 |
@param [in] block_size AES block size |
| 63 |
@param [out] out_size Out buffer length |
| 64 |
|
| 65 |
@returns Output buffer length or error |
| 66 |
@retval false Success |
| 67 |
@retval true Error processing given mode and/or block size |
| 68 |
*/ |
| 69 |
bool aes_get_encrypted_size_template(size_t input_length, const char *mode, |
| 70 |
size_t block_size, size_t *out_size) { |
| 71 |
try { |
| 72 |
if (mode == nullptr || block_size == 0) { |
| 73 |
LogComponentErr(ERROR_LEVEL, |
| 74 |
ER_NOTE_KEYRING_COMPONENT_AES_INVALID_MODE_BLOCK_SIZE); |
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
Aes_operation_context context(std::string{}, std::string{}, mode, |
| 79 |
block_size); |
| 80 |
if (context.valid() == false) return true; |
| 81 |
*out_size = get_ciphertext_size(input_length, context.opmode()); |
| 82 |
return false; |
| 83 |
} catch (...) { |
| 84 |
LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "get_size", |
| 85 |
"keyring_aes"); |
| 86 |
return true; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
Encrypt given piece of plaintext |
| 92 |
|
| 93 |
@param [in] data_id Name of the key |
| 94 |
@param [in] auth_id Owner of the key |
| 95 |
@param [in] mode AES mode |
| 96 |
@param [in] block_size AES block size information |
| 97 |
@param [in] iv Initialization vector |
| 98 |
@param [in] padding padding preference |
| 99 |
@param [in] data_buffer Input buffer |
| 100 |
@param [in] data_buffer_length Input buffer length |
| 101 |
@param [out] out_buffer Output buffer |
| 102 |
@param [in] out_buffer_length Output buffer length |
| 103 |
@param [out] out_length Length of encrypted data |
| 104 |
@param [in] keyring_operations Reference to the object |
| 105 |
that handles cache and backend |
| 106 |
@param [in] callbacks Handle to component specific callbacks |
| 107 |
|
| 108 |
@returns status of the operation |
| 109 |
@retval false Success |
| 110 |
@retval true Failure |
| 111 |
|
| 112 |
*/ |
| 113 |
template <typename Backend, typename Data_extension = Data> |
| 114 |
bool aes_encrypt_template( |
| 115 |
const char *data_id, const char *auth_id, const char *mode, |
| 116 |
size_t block_size, const unsigned char *iv, bool padding, |
| 117 |
const unsigned char *data_buffer, size_t data_buffer_length, |
| 118 |
unsigned char *out_buffer, size_t out_buffer_length, size_t *out_length, |
| 119 |
Keyring_operations<Backend, Data_extension> &keyring_operations, |
| 120 |
Component_callbacks &callbacks) { |
| 121 |
try { |
| 122 |
if (callbacks.keyring_initialized() == false) { |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
if (mode == nullptr || block_size == 0) { |
| 127 |
LogComponentErr(ERROR_LEVEL, |
| 128 |
ER_NOTE_KEYRING_COMPONENT_AES_INVALID_MODE_BLOCK_SIZE); |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
if (data_id == nullptr) { |
| 133 |
LogComponentErr(INFORMATION_LEVEL, |
| 134 |
ER_NOTE_KEYRING_COMPONENT_AES_DATA_IDENTIFIER_EMPTY); |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
Aes_operation_context context(data_id, auth_id, mode, block_size); |
| 139 |
|
| 140 |
Keyring_aes_opmode opmode = context.opmode(); |
| 141 |
size_t required_out_buffer_size = |
| 142 |
get_ciphertext_size(data_buffer_length, opmode); |
| 143 |
if (out_buffer == nullptr || required_out_buffer_size > out_buffer_length) { |
| 144 |
assert(false); |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
size_t key_length = 0; |
| 149 |
size_t key_type_length = 0; |
| 150 |
std::unique_ptr<Iterator<Data_extension>> it; |
| 151 |
int retval = init_reader_template<Backend, Data_extension>( |
| 152 |
data_id, auth_id, it, keyring_operations, callbacks); |
| 153 |
auto cleanup_guard = create_scope_guard([&] { |
| 154 |
(void)deinit_reader_template<Backend, Data_extension>( |
| 155 |
it, keyring_operations, callbacks); |
| 156 |
}); |
| 157 |
if (retval < 1) { |
| 158 |
// Error would have been raised |
| 159 |
return true; |
| 160 |
} |
| 161 |
if (fetch_length_template<Backend, Data_extension>( |
| 162 |
it, &key_length, &key_type_length, keyring_operations, callbacks) == |
| 163 |
true) { |
| 164 |
// Error would have been raised |
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
std::unique_ptr<unsigned char[]> key_buffer = |
| 169 |
std::make_unique<unsigned char[]>(key_length); |
| 170 |
if (key_buffer.get() == nullptr) { |
| 171 |
LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_MEMORY_ALLOCATION_ERROR, |
| 172 |
"key buffer", "encrypt", "keyring_aes"); |
| 173 |
} |
| 174 |
|
| 175 |
char key_type_buffer[32] = {0}; |
| 176 |
size_t dummy_key_buffer_size, dummy_key_type_buffer_size; |
| 177 |
|
| 178 |
if (fetch_template<Backend, Data_extension>( |
| 179 |
it, key_buffer.get(), key_length, &dummy_key_buffer_size, |
| 180 |
key_type_buffer, 32, &dummy_key_type_buffer_size, |
| 181 |
keyring_operations, callbacks) == true) { |
| 182 |
// Error would have been raised |
| 183 |
return true; |
| 184 |
} |
| 185 |
|
| 186 |
std::string key_type(key_type_buffer); |
| 187 |
std::transform(key_type.begin(), key_type.end(), key_type.begin(), |
| 188 |
::tolower); |
| 189 |
if (key_type != "aes") { |
| 190 |
LogComponentErr(INFORMATION_LEVEL, |
| 191 |
ER_NOTE_KEYRING_COMPONENT_AES_INVALID_KEY, data_id, |
| 192 |
(auth_id == nullptr || !*auth_id) ? "NULL" : auth_id); |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
aes_return_status ret = |
| 197 |
aes_encrypt(data_buffer, (unsigned int)data_buffer_length, out_buffer, |
| 198 |
key_buffer.get(), (unsigned int)key_length, opmode, iv, |
| 199 |
padding, out_length); |
| 200 |
|
| 201 |
if (ret != keyring_common::aes_encryption::AES_OP_OK) { |
| 202 |
std::stringstream ss; |
| 203 |
switch (ret) { |
| 204 |
case keyring_common::aes_encryption::AES_OUTPUT_SIZE_NULL: |
| 205 |
ss << "'Output size buffer is NULL'"; |
| 206 |
break; |
| 207 |
case keyring_common::aes_encryption::AES_KEY_TRANSFORMATION_ERROR: |
| 208 |
ss << "'Key transformation error'"; |
| 209 |
break; |
| 210 |
case keyring_common::aes_encryption::AES_CTX_ALLOCATION_ERROR: |
| 211 |
ss << "'Failed to allocate memory for encryption context'"; |
| 212 |
break; |
| 213 |
case keyring_common::aes_encryption::AES_INVALID_BLOCK_MODE: |
| 214 |
ss << "'Invalid block mode'"; |
| 215 |
break; |
| 216 |
case keyring_common::aes_encryption::AES_IV_EMPTY: |
| 217 |
ss << "'IV is empty'"; |
| 218 |
break; |
| 219 |
case keyring_common::aes_encryption::AES_ENCRYPTION_ERROR: |
| 220 |
ss << "'Could not complete operation'"; |
| 221 |
break; |
| 222 |
default: |
| 223 |
ss << "'Unknown error number: '" << ret; |
| 224 |
break; |
| 225 |
} |
| 226 |
std::string ss_str = ss.str(); |
| 227 |
LogComponentErr(INFORMATION_LEVEL, |
| 228 |
ER_NOTE_KEYRING_COMPONENT_AES_OPERATION_ERROR, |
| 229 |
ss_str.c_str(), "encrypt", data_id, |
| 230 |
(auth_id == nullptr || *auth_id) ? "NULL" : auth_id); |
| 231 |
return true; |
| 232 |
} |
| 233 |
|
| 234 |
/* All is well */ |
| 235 |
return false; |
| 236 |
} catch (...) { |
| 237 |
LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "encrypt", |
| 238 |
"keyring_aes"); |
| 239 |
return true; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
Decrypt given piece ciphertext |
| 245 |
|
| 246 |
@param [in] data_id Name of the key |
| 247 |
@param [in] auth_id Owner of the key |
| 248 |
@param [in] mode AES mode |
| 249 |
@param [in] block_size AES block size information |
| 250 |
@param [in] iv Initialization vector |
| 251 |
@param [in] padding padding preference |
| 252 |
@param [in] data_buffer Input buffer |
| 253 |
@param [in] data_buffer_length Input buffer length |
| 254 |
@param [out] out_buffer Output buffer |
| 255 |
@param [in] out_buffer_length Output buffer length |
| 256 |
@param [out] out_length Length of decrypted data |
| 257 |
@param [in] keyring_operations Reference to the object |
| 258 |
that handles cache and backend |
| 259 |
@param [in] callbacks Handle to component specific callbacks |
| 260 |
|
| 261 |
@returns status of the operation |
| 262 |
@retval false Success |
| 263 |
@retval true Failure |
| 264 |
|
| 265 |
*/ |
| 266 |
template <typename Backend, typename Data_extension = Data> |
| 267 |
bool aes_decrypt_template( |
| 268 |
const char *data_id, const char *auth_id, const char *mode, |
| 269 |
size_t block_size, const unsigned char *iv, bool padding, |
| 270 |
const unsigned char *data_buffer, size_t data_buffer_length, |
| 271 |
unsigned char *out_buffer, size_t out_buffer_length, size_t *out_length, |
| 272 |
Keyring_operations<Backend, Data_extension> &keyring_operations, |
| 273 |
Component_callbacks &callbacks) { |
| 274 |
try { |
| 275 |
if (callbacks.keyring_initialized() == false) { |
| 276 |
return true; |
| 277 |
} |
| 278 |
|
| 279 |
if (mode == nullptr || block_size == 0) { |
| 280 |
LogComponentErr(ERROR_LEVEL, |
| 281 |
ER_NOTE_KEYRING_COMPONENT_AES_INVALID_MODE_BLOCK_SIZE); |
| 282 |
return true; |
| 283 |
} |
| 284 |
|
| 285 |
if (data_id == nullptr) { |
| 286 |
LogComponentErr(INFORMATION_LEVEL, |
| 287 |
ER_NOTE_KEYRING_COMPONENT_AES_DATA_IDENTIFIER_EMPTY); |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
Aes_operation_context context(data_id, auth_id, mode, block_size); |
| 292 |
|
| 293 |
Keyring_aes_opmode opmode = context.opmode(); |
| 294 |
|
| 295 |
if (out_buffer == nullptr || data_buffer_length > out_buffer_length) { |
| 296 |
assert(false); |
| 297 |
return true; |
| 298 |
} |
| 299 |
|
| 300 |
size_t key_length = 0; |
| 301 |
size_t key_type_length = 0; |
| 302 |
std::unique_ptr<Iterator<Data_extension>> it; |
| 303 |
int retval = init_reader_template<Backend, Data_extension>( |
| 304 |
data_id, auth_id, it, keyring_operations, callbacks); |
| 305 |
auto cleanup_guard = create_scope_guard([&] { |
| 306 |
(void)deinit_reader_template<Backend, Data_extension>( |
| 307 |
it, keyring_operations, callbacks); |
| 308 |
}); |
| 309 |
if (retval < 1) { |
| 310 |
// Error would have been raised |
| 311 |
return true; |
| 312 |
} |
| 313 |
if (fetch_length_template<Backend, Data_extension>( |
| 314 |
it, &key_length, &key_type_length, keyring_operations, callbacks) == |
| 315 |
true) { |
| 316 |
// Error would have been raised |
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
std::unique_ptr<unsigned char[]> key_buffer = |
| 321 |
std::make_unique<unsigned char[]>(key_length); |
| 322 |
if (key_buffer.get() == nullptr) { |
| 323 |
LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_MEMORY_ALLOCATION_ERROR, |
| 324 |
"key buffer", "decrypt", "keyring_aes"); |
| 325 |
} |
| 326 |
|
| 327 |
char key_type_buffer[32] = {0}; |
| 328 |
size_t dummy_key_buffer_size, dummy_key_type_buffer_size; |
| 329 |
|
| 330 |
if (fetch_template<Backend, Data_extension>( |
| 331 |
it, key_buffer.get(), key_length, &dummy_key_buffer_size, |
| 332 |
key_type_buffer, 32, &dummy_key_type_buffer_size, |
| 333 |
keyring_operations, callbacks) == true) { |
| 334 |
// Error would have been raised |
| 335 |
return true; |
| 336 |
} |
| 337 |
|
| 338 |
std::string key_type(key_type_buffer); |
| 339 |
std::transform(key_type.begin(), key_type.end(), key_type.begin(), |
| 340 |
::tolower); |
| 341 |
if (key_type != "aes") { |
| 342 |
LogComponentErr(INFORMATION_LEVEL, |
| 343 |
ER_NOTE_KEYRING_COMPONENT_AES_INVALID_KEY, data_id, |
| 344 |
(auth_id == nullptr || !*auth_id) ? "NULL" : auth_id); |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
aes_return_status ret = |
| 349 |
aes_decrypt(data_buffer, (unsigned int)data_buffer_length, out_buffer, |
| 350 |
key_buffer.get(), (unsigned int)key_length, opmode, iv, |
| 351 |
padding, out_length); |
| 352 |
|
| 353 |
if (ret != keyring_common::aes_encryption::AES_OP_OK) { |
| 354 |
std::stringstream ss; |
| 355 |
switch (ret) { |
| 356 |
case keyring_common::aes_encryption::AES_OUTPUT_SIZE_NULL: |
| 357 |
ss << "'Output size buffer is NULL'"; |
| 358 |
break; |
| 359 |
case keyring_common::aes_encryption::AES_KEY_TRANSFORMATION_ERROR: |
| 360 |
ss << "'Key transformation error'"; |
| 361 |
break; |
| 362 |
case keyring_common::aes_encryption::AES_CTX_ALLOCATION_ERROR: |
| 363 |
ss << "'Failed to allocate memory for encryption context'"; |
| 364 |
break; |
| 365 |
case keyring_common::aes_encryption::AES_INVALID_BLOCK_MODE: |
| 366 |
ss << "'Invalid block mode'"; |
| 367 |
break; |
| 368 |
case keyring_common::aes_encryption::AES_IV_EMPTY: |
| 369 |
ss << "'IV is empty'"; |
| 370 |
break; |
| 371 |
case keyring_common::aes_encryption::AES_DECRYPTION_ERROR: |
| 372 |
ss << "'Could not complete operation'"; |
| 373 |
break; |
| 374 |
default: |
| 375 |
ss << "'Unknown error number: '" << ret; |
| 376 |
break; |
| 377 |
} |
| 378 |
std::string ss_str = ss.str(); |
| 379 |
LogComponentErr(INFORMATION_LEVEL, |
| 380 |
ER_NOTE_KEYRING_COMPONENT_AES_OPERATION_ERROR, |
| 381 |
ss_str.c_str(), "decrypt", data_id, |
| 382 |
(auth_id == nullptr || *auth_id) ? "NULL" : auth_id); |
| 383 |
return true; |
| 384 |
} |
| 385 |
|
| 386 |
/* All is well */ |
| 387 |
return false; |
| 388 |
} catch (...) { |
| 389 |
LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "decrypt", |
| 390 |
"keyring_aes"); |
| 391 |
return true; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
} // namespace service_implementation |
| 396 |
} // namespace keyring_common |
| 397 |
|
| 398 |
#endif // !KEYRING_ENCRYPTION_SERVICE_IMPL_TEMPLATE_INCLUDED |