24#ifndef KEYRING_ENCRYPTION_SERVICE_IMPL_TEMPLATE_INCLUDED
25#define KEYRING_ENCRYPTION_SERVICE_IMPL_TEMPLATE_INCLUDED
36#include <mysqld_error.h>
49using aes_encryption::Aes_operation_context;
54using operations::Keyring_operations;
56namespace service_implementation {
70 size_t block_size,
size_t *out_size) {
72 if (
mode ==
nullptr || block_size == 0) {
74 ER_NOTE_KEYRING_COMPONENT_AES_INVALID_MODE_BLOCK_SIZE);
80 if (!context.valid())
return true;
84 LogComponentErr(
ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION,
"get_size",
113template <
typename Backend,
typename Data_extension = Data>
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,
126 if (
mode ==
nullptr || block_size == 0) {
128 ER_NOTE_KEYRING_COMPONENT_AES_INVALID_MODE_BLOCK_SIZE);
132 if (data_id ==
nullptr) {
134 ER_NOTE_KEYRING_COMPONENT_AES_DATA_IDENTIFIER_EMPTY);
141 const size_t required_out_buffer_size =
143 if (out_buffer ==
nullptr || required_out_buffer_size > out_buffer_length) {
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);
154 (void)deinit_reader_template<Backend, Data_extension>(
155 it, keyring_operations, callbacks);
161 if (fetch_length_template<Backend, Data_extension>(
162 it, &key_length, &key_type_length, keyring_operations, callbacks)) {
167 const std::unique_ptr<unsigned char[]> key_buffer =
168 std::make_unique<unsigned char[]>(key_length);
169 if (key_buffer ==
nullptr) {
170 LogComponentErr(
ERROR_LEVEL, ER_KEYRING_COMPONENT_MEMORY_ALLOCATION_ERROR,
171 "key buffer",
"encrypt",
"keyring_aes");
174 char key_type_buffer[32] = {0};
175 size_t dummy_key_buffer_size, dummy_key_type_buffer_size;
177 if (fetch_template<Backend, Data_extension>(
178 it, key_buffer.get(), key_length, &dummy_key_buffer_size,
179 key_type_buffer, 32, &dummy_key_type_buffer_size,
180 keyring_operations, callbacks)) {
185 std::string
key_type(key_type_buffer);
190 ER_NOTE_KEYRING_COMPONENT_AES_INVALID_KEY, data_id,
191 (auth_id ==
nullptr || !*auth_id) ?
"NULL" : auth_id);
196 aes_encrypt(data_buffer, (
unsigned int)data_buffer_length, out_buffer,
197 key_buffer.get(), (
unsigned int)key_length, opmode, iv,
198 padding, out_length);
201 std::stringstream ss;
204 ss <<
"'Output size buffer is NULL'";
207 ss <<
"'Key transformation error'";
210 ss <<
"'Failed to allocate memory for encryption context'";
213 ss <<
"'Invalid block mode'";
216 ss <<
"'IV is empty'";
219 ss <<
"'Could not complete operation'";
222 ss <<
"'Unknown error number: '" << ret;
225 const std::string ss_str = ss.str();
227 ER_NOTE_KEYRING_COMPONENT_AES_OPERATION_ERROR,
228 ss_str.c_str(),
"encrypt", data_id,
229 (auth_id ==
nullptr || *auth_id) ?
"NULL" : auth_id);
236 LogComponentErr(
ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION,
"encrypt",
265template <
typename Backend,
typename Data_extension = Data>
267 const char *data_id,
const char *auth_id,
const char *
mode,
268 size_t block_size,
const unsigned char *iv,
bool padding,
269 const unsigned char *data_buffer,
size_t data_buffer_length,
270 unsigned char *out_buffer,
size_t out_buffer_length,
size_t *out_length,
278 if (
mode ==
nullptr || block_size == 0) {
280 ER_NOTE_KEYRING_COMPONENT_AES_INVALID_MODE_BLOCK_SIZE);
284 if (data_id ==
nullptr) {
286 ER_NOTE_KEYRING_COMPONENT_AES_DATA_IDENTIFIER_EMPTY);
294 if (out_buffer ==
nullptr || data_buffer_length > out_buffer_length) {
299 size_t key_length = 0;
300 size_t key_type_length = 0;
301 std::unique_ptr<Iterator<Data_extension>> it;
302 int retval = init_reader_template<Backend, Data_extension>(
303 data_id, auth_id, it, keyring_operations, callbacks);
305 (void)deinit_reader_template<Backend, Data_extension>(
306 it, keyring_operations, callbacks);
312 if (fetch_length_template<Backend, Data_extension>(
313 it, &key_length, &key_type_length, keyring_operations, callbacks)) {
318 std::unique_ptr<unsigned char[]> key_buffer =
319 std::make_unique<unsigned char[]>(key_length);
320 if (key_buffer.get() ==
nullptr) {
321 LogComponentErr(
ERROR_LEVEL, ER_KEYRING_COMPONENT_MEMORY_ALLOCATION_ERROR,
322 "key buffer",
"decrypt",
"keyring_aes");
325 char key_type_buffer[32] = {0};
326 size_t dummy_key_buffer_size, dummy_key_type_buffer_size;
328 if (fetch_template<Backend, Data_extension>(
329 it, key_buffer.get(), key_length, &dummy_key_buffer_size,
330 key_type_buffer, 32, &dummy_key_type_buffer_size,
331 keyring_operations, callbacks)) {
336 std::string
key_type(key_type_buffer);
341 ER_NOTE_KEYRING_COMPONENT_AES_INVALID_KEY, data_id,
342 (auth_id ==
nullptr || !*auth_id) ?
"NULL" : auth_id);
347 aes_decrypt(data_buffer, (
unsigned int)data_buffer_length, out_buffer,
348 key_buffer.get(), (
unsigned int)key_length, opmode, iv,
349 padding, out_length);
352 std::stringstream ss;
355 ss <<
"'Output size buffer is NULL'";
358 ss <<
"'Key transformation error'";
361 ss <<
"'Failed to allocate memory for encryption context'";
364 ss <<
"'Invalid block mode'";
367 ss <<
"'IV is empty'";
370 ss <<
"'Could not complete operation'";
373 ss <<
"'Unknown error number: '" << ret;
376 const std::string ss_str = ss.str();
378 ER_NOTE_KEYRING_COMPONENT_AES_OPERATION_ERROR,
379 ss_str.c_str(),
"decrypt", data_id,
380 (auth_id ==
nullptr || *auth_id) ?
"NULL" : auth_id);
387 LogComponentErr(
ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION,
"decrypt",
Keyring_aes_opmode opmode() const
Definition: aes.h:68
Keyring operations A class to perform operations on keyring.
Definition: operations.h:482
Definition: service_requirements.h:35
bool keyring_initialized()
Keyring component status.
Definition: component_callbacks.cc:28
@ ERROR_LEVEL
Definition: my_loglevel.h:43
@ INFORMATION_LEVEL
Definition: my_loglevel.h:45
bool transform(const dd::Spatial_reference_system *source_srs, const Geometry &in, const dd::Spatial_reference_system *target_srs, const char *func_name, std::unique_ptr< Geometry > *out) noexcept
Transforms a geometry from one SRS to another.
Definition: transform.cc:216
int key_type
Definition: method.h:38
aes_return_status
Definition: aes.h:47
@ AES_OP_OK
Definition: aes.h:48
@ AES_DECRYPTION_ERROR
Definition: aes.h:55
@ AES_IV_EMPTY
Definition: aes.h:53
@ AES_CTX_ALLOCATION_ERROR
Definition: aes.h:51
@ AES_INVALID_BLOCK_MODE
Definition: aes.h:52
@ AES_KEY_TRANSFORMATION_ERROR
Definition: aes.h:50
@ AES_OUTPUT_SIZE_NULL
Definition: aes.h:49
@ AES_ENCRYPTION_ERROR
Definition: aes.h:54
aes_return_status aes_decrypt(const unsigned char *source, unsigned int source_length, unsigned char *dest, const unsigned char *key, unsigned int key_length, enum Keyring_aes_opmode mode, const unsigned char *iv, bool padding, size_t *decrypted_length)
Definition: aes.cc:187
aes_return_status aes_encrypt(const unsigned char *source, unsigned int source_length, unsigned char *dest, const unsigned char *key, unsigned int key_length, Keyring_aes_opmode mode, const unsigned char *iv, bool padding, size_t *encrypted_length)
Definition: aes.cc:137
Keyring_aes_opmode
Supported AES cipher/block mode combos.
Definition: aes.h:37
size_t get_ciphertext_size(size_t input_size, const Keyring_aes_opmode mode)
Definition: aes.cc:127
bool aes_decrypt_template(const char *data_id, const char *auth_id, const char *mode, size_t block_size, const unsigned char *iv, bool padding, const unsigned char *data_buffer, size_t data_buffer_length, unsigned char *out_buffer, size_t out_buffer_length, size_t *out_length, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Decrypt given piece ciphertext.
Definition: keyring_encryption_service_impl_template.h:266
bool aes_get_encrypted_size_template(size_t input_length, const char *mode, size_t block_size, size_t *out_size)
Retrieve required out buffer length information.
Definition: keyring_encryption_service_impl_template.h:69
bool aes_encrypt_template(const char *data_id, const char *auth_id, const char *mode, size_t block_size, const unsigned char *iv, bool padding, const unsigned char *data_buffer, size_t data_buffer_length, unsigned char *out_buffer, size_t out_buffer_length, size_t *out_length, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Encrypt given piece of plaintext.
Definition: keyring_encryption_service_impl_template.h:114
Definition: keyring_encryption_service_definition.h:32
char tolower(const char &ch)
Definition: parsing_helpers.h:41
mode
Definition: file_handle.h:61
Scope_guard< TLambda > create_scope_guard(const TLambda rollback_lambda)
Create a scope guard object.
Definition: scope_guard.h:113