MySQL 9.5.0
Source Code Documentation
keyring_writer_service_impl_template.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2025, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef KEYRING_WRITER_SERVICE_IMPL_TEMPLATE_INCLUDED
25#define KEYRING_WRITER_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
38
40
44
45/**
46 Store data in keyring
47
48 @param [in] data_id Data Identifier
49 @param [in] auth_id Authorization ID
50 @param [in] data Data to be stored
51 @param [in] data_size Size of data to be stored
52 @param [in] data_type Type of data
53 @param [in] keyring_operations Reference to the object
54 that handles cache and backend
55 @param [in] callbacks Handle to component specific callbacks
56
57 @returns status of the operation
58 @retval false Success
59 @retval true Failure
60*/
61
62template <typename Backend, typename Data_extension = data::Data>
64 const char *data_id, const char *auth_id, const unsigned char *data,
65 size_t data_size, const char *data_type,
67 Component_callbacks &callbacks) {
68 try {
69 if (!callbacks.keyring_initialized()) {
70 LogComponentErr(INFORMATION_LEVEL,
71 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
72 return true;
73 }
74
75 if (data_id == nullptr || !*data_id) {
76 LogComponentErr(INFORMATION_LEVEL,
77 ER_NOTE_KEYRING_COMPONENT_EMPTY_DATA_ID);
78 return true;
79 }
80
81 if (data_size > keyring_operations.maximum_data_length()) {
82 LogComponentErr(INFORMATION_LEVEL,
83 ER_NOTE_KEYRING_COMPONENT_WRITE_MAXIMUM_DATA_LENGTH,
84 keyring_operations.maximum_data_length());
85 return true;
86 }
87
88 Metadata metadata(data_id, auth_id);
89 Data data_to_be_stored({reinterpret_cast<const char *>(data), data_size},
90 {data_type, data_type ? strlen(data_type) : 0});
91 if (keyring_operations.store(metadata, data_to_be_stored)) {
92 LogComponentErr(INFORMATION_LEVEL, ER_NOTE_KEYRING_COMPONENT_STORE_FAILED,
93 data_id,
94 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
95 return true;
96 }
97 return false;
98 } catch (...) {
99 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "store",
100 "keyring_writer");
101 return true;
102 }
103}
104
105/**
106 Remove data from keyring
107
108 @param [in] data_id Data Identifier
109 @param [in] auth_id Authorization ID
110 @param [in] keyring_operations Reference to the object
111 that handles cache and backend
112 @param [in] callbacks Handle to component specific callbacks
113
114 @returns status of the operation
115 @retval false Success - Key removed successfully or key not present.
116 @retval true Failure
117*/
118template <typename Backend, typename Data_extension = data::Data>
120 const char *data_id, const char *auth_id,
122 Component_callbacks &callbacks) {
123 try {
124 if (!callbacks.keyring_initialized()) {
125 LogComponentErr(INFORMATION_LEVEL,
126 ER_NOTE_KEYRING_COMPONENT_NOT_INITIALIZED);
127 return true;
128 }
129
130 if (data_id == nullptr || !*data_id) {
131 LogComponentErr(INFORMATION_LEVEL,
132 ER_NOTE_KEYRING_COMPONENT_EMPTY_DATA_ID);
133 return true;
134 }
135
136 Metadata metadata(data_id, auth_id);
137 if (keyring_operations.erase(metadata)) {
138 LogComponentErr(INFORMATION_LEVEL,
139 ER_NOTE_KEYRING_COMPONENT_REMOVE_FAILED, data_id,
140 (auth_id == nullptr || !*auth_id) ? "NULL" : auth_id);
141 return true;
142 }
143 return false;
144 } catch (...) {
145 LogComponentErr(ERROR_LEVEL, ER_KEYRING_COMPONENT_EXCEPTION, "remove",
146 "keyring_writer");
147 return true;
148 }
149}
150
151} // namespace keyring_common::service_implementation
152
153#endif // !KEYRING_WRITER_SERVICE_IMPL_TEMPLATE_INCLUDED
Sensitive data storage.
Definition: data.h:39
Common metadata.
Definition: meta.h:38
Keyring operations A class to perform operations on keyring.
Definition: operations.h:482
bool erase(const meta::Metadata &metadata)
Remove API.
Definition: operations.h:609
bool store(const meta::Metadata &metadata, const data::Data &data)
Store API.
Definition: operations.h:570
size_t maximum_data_length() const
Maximum data length supported.
Definition: operations.h:786
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
Definition: keyring_encryption_service_impl_template.h:56
bool store_template(const char *data_id, const char *auth_id, const unsigned char *data, size_t data_size, const char *data_type, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Store data in keyring.
Definition: keyring_writer_service_impl_template.h:63
bool remove_template(const char *data_id, const char *auth_id, Keyring_operations< Backend, Data_extension > &keyring_operations, Component_callbacks &callbacks)
Remove data from keyring.
Definition: keyring_writer_service_impl_template.h:119