| 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 <memory> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
#include "mysql/components/library_mysys/my_hex_tools.h" |
| 28 |
|
| 29 |
#include "json_reader.h" |
| 30 |
|
| 31 |
namespace keyring_common { |
| 32 |
namespace json_data { |
| 33 |
|
| 34 |
static std::string schema_version_1_0 = |
| 35 |
"{" |
| 36 |
" \"title\": \"Key store validator version 1.0\"," |
| 37 |
" \"description\": \"Expected schema for version 1.0\"," |
| 38 |
" \"type\": \"object\"," |
| 39 |
" \"properties\": {" |
| 40 |
" \"version\": {" |
| 41 |
" \"description\": \"The file format version\"," |
| 42 |
" \"type\": \"string\"" |
| 43 |
" }," |
| 44 |
" \"elements\": {" |
| 45 |
" \"description\": \"Array of stored data\"," |
| 46 |
" \"type\": \"array\"," |
| 47 |
" \"items\": {" |
| 48 |
" \"type\": \"object\"," |
| 49 |
" \"properties\": {" |
| 50 |
" \"user\": { \"type\": \"string\" }," |
| 51 |
" \"data_id\": { \"type\": \"string\" }," |
| 52 |
" \"data_type\": { \"type\": \"string\" }," |
| 53 |
" \"data\": { \"type\": \"string\" }," |
| 54 |
" \"extension\" : { \"type\": \"array\" }" |
| 55 |
" }," |
| 56 |
" \"required\": [" |
| 57 |
" \"user\"," |
| 58 |
" \"data_id\"," |
| 59 |
" \"data_type\"," |
| 60 |
" \"data\"," |
| 61 |
" \"extension\"" |
| 62 |
" ]" |
| 63 |
" }" |
| 64 |
" }" |
| 65 |
" }," |
| 66 |
" \"required\": [" |
| 67 |
" \"version\"," |
| 68 |
" \"elements\"" |
| 69 |
" ]" |
| 70 |
"}"; |
| 71 |
|
| 72 |
/** |
| 73 |
Constructor |
| 74 |
|
| 75 |
@param [in] schema JSON schema in string format |
| 76 |
@param [in] data JSON data in string format |
| 77 |
@param [in] version_key JSON schema version information |
| 78 |
@param [in] array_key Key for array of elements |
| 79 |
*/ |
| 80 |
Json_reader::Json_reader(const std::string schema, const std::string data, |
| 81 |
const std::string version_key, |
| 82 |
const std::string array_key) |
| 83 |
: document_(), |
| 84 |
version_key_(version_key), |
| 85 |
array_key_(array_key), |
| 86 |
valid_(false) { |
| 87 |
rapidjson::Document schema_json; |
| 88 |
if (schema_json.Parse(schema).HasParseError()) return; |
| 89 |
if (document_.Parse(data).HasParseError()) return; |
| 90 |
|
| 91 |
rapidjson::SchemaDocument sd(schema_json); |
| 92 |
rapidjson::SchemaValidator validator(sd); |
| 93 |
if (!document_.Accept(validator)) return; |
| 94 |
|
| 95 |
valid_ = true; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
Constructor |
| 100 |
|
| 101 |
@param [in] data JSON Data in string format |
| 102 |
|
| 103 |
Initializes JSON document with given data |
| 104 |
and sets validity state. |
| 105 |
*/ |
| 106 |
Json_reader::Json_reader(const std::string data) |
| 107 |
: Json_reader(schema_version_1_0, data) {} |
| 108 |
|
| 109 |
/* Default constructor - creates empty document */ |
| 110 |
Json_reader::Json_reader() : Json_reader(schema_version_1_0, "") {} |
| 111 |
|
| 112 |
/** |
| 113 |
Get version info |
| 114 |
|
| 115 |
@returns version string in case same is |
| 116 |
present, empty string otherwise. |
| 117 |
*/ |
| 118 |
std::string Json_reader::version() const { |
| 119 |
if (!valid_) return {}; |
| 120 |
return document_[version_key_.c_str()].Get<std::string>(); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
Get number of elements in the document |
| 125 |
|
| 126 |
@returns number elements in the document |
| 127 |
*/ |
| 128 |
size_t Json_reader::num_elements() const { |
| 129 |
if (!valid_) return 0; |
| 130 |
return static_cast<size_t>(document_[array_key_.c_str()].Size()); |
| 131 |
} |
| 132 |
|
| 133 |
bool Json_reader::get_element( |
| 134 |
size_t index, meta::Metadata &metadata, data::Data &data, |
| 135 |
std::unique_ptr<Json_data_extension> &json_data_extension) const { |
| 136 |
if (!valid_ || index >= num_elements()) return true; |
| 137 |
const rapidjson::Value &elements = document_[array_key_.c_str()]; |
| 138 |
if (!elements.IsArray()) return true; |
| 139 |
metadata = {elements[index]["data_id"].Get<std::string>(), |
| 140 |
elements[index]["user"].Get<std::string>()}; |
| 141 |
std::string hex_data(elements[index]["data"].Get<std::string>()); |
| 142 |
std::string unhex_data(hex_data.length() * 2, '\0'); |
| 143 |
unsigned long length = unhex_string( |
| 144 |
hex_data.data(), hex_data.data() + hex_data.size(), &unhex_data[0]); |
| 145 |
unhex_data.resize(length); |
| 146 |
data = {unhex_data, elements[index]["data_type"].Get<std::string>()}; |
| 147 |
json_data_extension = std::make_unique<Json_data_extension>(); |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
bool Json_reader::get_elements(output_vector &output) const { |
| 152 |
if (!valid_) return true; |
| 153 |
const rapidjson::Value &elements = document_[array_key_.c_str()]; |
| 154 |
if (!elements.IsArray()) return true; |
| 155 |
for (rapidjson::SizeType t = 0; t < elements.Size(); ++t) { |
| 156 |
meta::Metadata metadata; |
| 157 |
data::Data secret_data; |
| 158 |
std::unique_ptr<Json_data_extension> ext; |
| 159 |
if (get_element(t, metadata, secret_data, ext) == true) { |
| 160 |
output.clear(); |
| 161 |
return true; |
| 162 |
} |
| 163 |
output.push_back( |
| 164 |
std::make_pair(std::make_pair(metadata, secret_data), std::move(ext))); |
| 165 |
} |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
} // namespace json_data |
| 170 |
} // namespace keyring_common |