Debian Codegraph @

components/keyrings/keyring_file/backend/backend.cc

raw
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 <fstream>
25 #include <memory>
26
27 #include "backend.h"
28
29 #include <components/keyrings/common/data_file/reader.h>
30 #include <components/keyrings/common/data_file/writer.h>
31 #include <components/keyrings/common/json_data/json_reader.h>
32 #include <components/keyrings/common/json_data/json_writer.h>
33 #include <components/keyrings/common/memstore/cache.h>
34 #include <components/keyrings/common/memstore/iterator.h>
35 #include <components/keyrings/common/utils/utils.h>
36
37 namespace keyring_file {
38
39 namespace backend {
40
41 using keyring_common::data::Data;
42 using keyring_common::data_file::File_reader;
43 using keyring_common::data_file::File_writer;
44 using keyring_common::json_data::Json_data_extension;
45 using keyring_common::json_data::Json_reader;
46 using keyring_common::json_data::Json_writer;
47 using keyring_common::json_data::output_vector;
48 using keyring_common::meta::Metadata;
49 using keyring_common::utils::get_random_data;
50
51 Json_data_extension ext;
52
53 Keyring_file_backend::Keyring_file_backend(const std::string keyring_file_name,
54 bool read_only)
55 : keyring_file_name_(keyring_file_name),
56 read_only_(read_only),
57 json_writer_(),
58 valid_(false) {
59 if (keyring_file_name_.length() == 0) return;
60 std::string data;
61 output_vector elements;
62 create_file_if_missing(keyring_file_name_);
63 {
64 /* Read the file */
65 File_reader file_reader(keyring_file_name_, read_only_, data);
66 if (!file_reader.valid()) return;
67 }
68
69 /* It is possible that file is empty and that's ok. */
70 if (data.length()) {
71 /* Read JSON data - format check */
72 Json_reader json_reader(data);
73 if (!json_reader.valid()) return;
74
75 /* Cache */
76 json_writer_.set_data(data);
77 }
78 valid_ = true;
79 }
80
81 bool Keyring_file_backend::load_cache(
82 keyring_common::operations::Keyring_operations<Keyring_file_backend>
83 &operations) {
84 if (json_writer_.num_elements() == 0) return false;
85 Json_reader json_reader(json_writer_.to_string());
86 if (!json_reader.valid()) return true;
87 if (json_reader.num_elements() != json_writer_.num_elements()) return true;
88
89 for (size_t i = 0; i < json_reader.num_elements(); ++i) {
90 std::unique_ptr<Json_data_extension> data_ext;
91 Metadata metadata;
92 Data data;
93 if (json_reader.get_element(i, metadata, data, data_ext) == true)
94 return true;
95 if (operations.insert(metadata, data) == true) return true;
96 }
97 return false;
98 }
99
100 bool Keyring_file_backend::get(const Metadata &, Data &) const {
101 /* Shouldn't have reached here. */
102 return true;
103 }
104
105 bool Keyring_file_backend::store(const Metadata &metadata, Data &data) {
106 if (!metadata.valid() || !data.valid()) return true;
107 if (json_writer_.add_element(metadata, data, ext)) return true;
108 if (write_to_file()) {
109 /* Erase stored entry */
110 (void)json_writer_.remove_element(metadata, ext);
111 return true;
112 }
113 return false;
114 }
115
116 bool Keyring_file_backend::erase(const Metadata &metadata, Data &data) {
117 if (!metadata.valid()) return true;
118 if (json_writer_.remove_element(metadata, ext)) return true;
119 if (write_to_file()) {
120 /* Add entry back */
121 (void)json_writer_.add_element(metadata, data, ext);
122 return true;
123 }
124 return false;
125 }
126
127 bool Keyring_file_backend::generate(const Metadata &metadata, Data &data,
128 size_t length) {
129 if (!metadata.valid()) return true;
130
131 std::unique_ptr<unsigned char[]> key(new unsigned char[length]);
132 if (!key) return true;
133 if (!get_random_data(key, length)) return true;
134
135 std::string key_str;
136 key_str.assign(reinterpret_cast<const char *>(key.get()), length);
137 data.set_data(key_str);
138
139 return store(metadata, data);
140 }
141
142 bool Keyring_file_backend::write_to_file() {
143 /* Get JSON string from cache and feed it to file writer */
144 File_writer file_writer(keyring_file_name_, json_writer_.to_string());
145 return !file_writer.valid();
146 }
147
148 void Keyring_file_backend::create_file_if_missing(std::string file_name) {
149 std::ifstream f(file_name.c_str());
150 if (f.good())
151 f.close();
152 else {
153 std::ofstream o(file_name.c_str());
154 o.close();
155 }
156 }
157
158 } // namespace backend
159
160 } // namespace keyring_file