diff --git a/repertory/librepertory/include/comm/curl/curl_comm.hpp b/repertory/librepertory/include/comm/curl/curl_comm.hpp index 8ab68076..0ada9500 100644 --- a/repertory/librepertory/include/comm/curl/curl_comm.hpp +++ b/repertory/librepertory/include/comm/curl/curl_comm.hpp @@ -26,7 +26,7 @@ #include "comm/i_http_comm.hpp" #include "events/event_system.hpp" #include "events/events.hpp" -#include "utils/encrypt.hpp" +#include "utils/encryption.hpp" #include "utils/utils.hpp" namespace repertory { @@ -92,32 +92,31 @@ public: const auto key = utils::encryption::generate_key( request.decryption_token.value()); - const auto result = utils::encryption::read_encrypted_range( - request.range.value(), key, - [&](data_buffer &ct, std::uint64_t start_offset, - std::uint64_t end_offset) -> api_error { - auto encrypted_request = request; - encrypted_request.decryption_token = std::nullopt; - encrypted_request.range = {{start_offset, end_offset}}; - encrypted_request.response_handler = [&ct](const auto &encrypted_data, - long /*response_code*/) { - ct = encrypted_data; - }; - encrypted_request.total_size = std::nullopt; + if (not utils::encryption::read_encrypted_range( + request.range.value(), key, + [&](data_buffer &ct, std::uint64_t start_offset, + std::uint64_t end_offset) -> bool { + auto encrypted_request = request; + encrypted_request.decryption_token = std::nullopt; + encrypted_request.range = {{start_offset, end_offset}}; + encrypted_request.response_handler = + [&ct](const auto &encrypted_data, long /*response_code*/) { + ct = encrypted_data; + }; + encrypted_request.total_size = std::nullopt; - if (not make_request(cfg, encrypted_request, response_code, - stop_requested)) { - return api_error::comm_error; - } + if (not make_request(cfg, encrypted_request, response_code, + stop_requested)) { + return false; + } - if (response_code != 200) { - return api_error::comm_error; - } + if (response_code != 200) { + return false; + } - return api_error::success; - }, - request.total_size.value(), data); - if (result != api_error::success) { + return true; + }, + request.total_size.value(), data)) { return false; } diff --git a/repertory/librepertory/include/utils/encrypt.hpp b/repertory/librepertory/include/utils/encrypt.hpp deleted file mode 100644 index 840855c4..00000000 --- a/repertory/librepertory/include/utils/encrypt.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - Copyright <2018-2024> - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -#ifndef INCLUDE_UTILS_ENCRYPT_HPP_ -#define INCLUDE_UTILS_ENCRYPT_HPP_ - -#include "types/repertory.hpp" -#include "utils/encryption.hpp" - -namespace repertory::utils::encryption { -using reader_func = std::function; - -[[nodiscard]] auto -read_encrypted_range(const http_range &range, - const utils::encryption::hash_256_t &key, - reader_func reader, std::uint64_t total_size, - data_buffer &data) -> api_error; -} // namespace repertory::utils::encryption - -#endif // INCLUDE_UTILS_ENCRYPT_HPP_ diff --git a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp index 62baef4a..937e72f0 100644 --- a/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp +++ b/repertory/librepertory/src/providers/encrypt/encrypt_provider.cpp @@ -28,8 +28,8 @@ #include "events/events.hpp" #include "types/repertory.hpp" #include "utils/collection.hpp" -#include "utils/encrypt.hpp" #include "utils/encrypting_reader.hpp" +#include "utils/encryption.hpp" #include "utils/file_utils.hpp" #include "utils/path.hpp" #include "utils/polling.hpp" diff --git a/repertory/librepertory/src/providers/s3/s3_provider.cpp b/repertory/librepertory/src/providers/s3/s3_provider.cpp index 7240e099..3f0ad674 100644 --- a/repertory/librepertory/src/providers/s3/s3_provider.cpp +++ b/repertory/librepertory/src/providers/s3/s3_provider.cpp @@ -29,8 +29,8 @@ #include "types/s3.hpp" #include "types/startup_exception.hpp" #include "utils/collection.hpp" -#include "utils/encrypt.hpp" #include "utils/encrypting_reader.hpp" +#include "utils/encryption.hpp" #include "utils/error_utils.hpp" #include "utils/file_utils.hpp" #include "utils/path.hpp" @@ -732,18 +732,25 @@ auto s3_provider::read_file_bytes(const std::string &api_path, std::size_t size, if (res != api_error::success) { return res; } - const auto total_size = utils::string::to_uint64(temp); - return utils::encryption::read_encrypted_range( - {offset, offset + size - 1U}, - utils::encryption::generate_key( - cfg.encryption_token), - [&](data_buffer &ct_buffer, std::uint64_t start_offset, - std::uint64_t end_offset) -> api_error { - return read_bytes((end_offset - start_offset + 1U), start_offset, - ct_buffer); - }, - total_size, data); + auto total_size = utils::string::to_uint64(temp); + + res = api_error::success; + if (utils::encryption::read_encrypted_range( + {offset, offset + size - 1U}, + utils::encryption::generate_key( + cfg.encryption_token), + [&](data_buffer &ct_buffer, std::uint64_t start_offset, + std::uint64_t end_offset) -> api_error { + res = read_bytes((end_offset - start_offset + 1U), start_offset, + ct_buffer); + return res == api_error::success; + }, + total_size, data)) { + return api_error::success; + } + + return res == api_error::success ? api_error::decryption_error : res; } return read_bytes(size, offset, data); diff --git a/repertory/librepertory/src/utils/encrypt.cpp b/repertory/librepertory/src/utils/encrypt.cpp deleted file mode 100644 index 80402210..00000000 --- a/repertory/librepertory/src/utils/encrypt.cpp +++ /dev/null @@ -1,80 +0,0 @@ -/* - Copyright <2018-2024> - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated documentation files (the "Software"), to deal - in the Software without restriction, including without limitation the rights - to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - copies of the Software, and to permit persons to whom the Software is - furnished to do so, subject to the following conditions: - - The above copyright notice and this permission notice shall be included in all - copies or substantial portions of the Software. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - SOFTWARE. -*/ -#include "utils/encrypt.hpp" - -#include "events/event_system.hpp" -#include "events/events.hpp" -#include "types/repertory.hpp" -#include "utils/collection.hpp" -#include "utils/encrypting_reader.hpp" -#include "utils/encryption.hpp" -#include "utils/utils.hpp" - -namespace repertory::utils::encryption { -auto read_encrypted_range(const http_range &range, - const utils::encryption::hash_256_t &key, - reader_func reader, std::uint64_t total_size, - data_buffer &data) -> api_error { - const auto encrypted_chunk_size = - utils::encryption::encrypting_reader::get_encrypted_chunk_size(); - const auto data_chunk_size = - utils::encryption::encrypting_reader::get_data_chunk_size(); - - const auto start_chunk = - static_cast(range.begin / data_chunk_size); - const auto end_chunk = static_cast(range.end / data_chunk_size); - auto remain = range.end - range.begin + 1U; - auto source_offset = static_cast(range.begin % data_chunk_size); - - for (std::size_t chunk = start_chunk; chunk <= end_chunk; chunk++) { - data_buffer cypher; - const auto start_offset = chunk * encrypted_chunk_size; - const auto end_offset = std::min( - start_offset + (total_size - (chunk * data_chunk_size)) + - encryption_header_size - 1U, - static_cast(start_offset + encrypted_chunk_size - 1U)); - - const auto result = reader(cypher, start_offset, end_offset); - if (result != api_error::success) { - return result; - } - - data_buffer source_buffer; - if (not utils::encryption::decrypt_data(key, cypher, source_buffer)) { - return api_error::decryption_error; - } - cypher.clear(); - - const auto data_size = static_cast(std::min( - remain, static_cast(data_chunk_size - source_offset))); - std::copy(std::next(source_buffer.begin(), - static_cast(source_offset)), - std::next(source_buffer.begin(), - static_cast(source_offset + data_size)), - std::back_inserter(data)); - remain -= data_size; - source_offset = 0U; - } - - return api_error::success; -} -} // namespace repertory::utils::encryption diff --git a/support/include/utils/encryption.hpp b/support/include/utils/encryption.hpp index 07c9f306..9834d305 100644 --- a/support/include/utils/encryption.hpp +++ b/support/include/utils/encryption.hpp @@ -178,6 +178,18 @@ encrypt_data(const std::array(buf.data()), buf.size(), res); } + +#if defined(PROJECT_ENABLE_CURL) +using reader_func_t = + std::function; + +[[nodiscard]] auto +read_encrypted_range(const http_range &range, + const utils::encryption::hash_256_t &key, + reader_func_t reader_func, std::uint64_t total_size, + data_buffer &data) -> bool; +#endif // defined(PROJECT_ENABLE_CURL) #endif // defined(PROJECT_ENABLE_BOOST) template diff --git a/support/src/utils/encryption.cpp b/support/src/utils/encryption.cpp index c60b2d87..eec96407 100644 --- a/support/src/utils/encryption.cpp +++ b/support/src/utils/encryption.cpp @@ -62,6 +62,55 @@ auto decrypt_file_name(std::string_view encryption_token, return true; } + +#if defined(PROJECT_ENABLE_CURL) +auto read_encrypted_range(const http_range &range, + const utils::encryption::hash_256_t &key, + reader_func_t reader_func, std::uint64_t total_size, + data_buffer &data) -> bool { + const auto encrypted_chunk_size = + utils::encryption::encrypting_reader::get_encrypted_chunk_size(); + const auto data_chunk_size = + utils::encryption::encrypting_reader::get_data_chunk_size(); + + const auto start_chunk = + static_cast(range.begin / data_chunk_size); + const auto end_chunk = static_cast(range.end / data_chunk_size); + auto remain = range.end - range.begin + 1U; + auto source_offset = static_cast(range.begin % data_chunk_size); + + for (std::size_t chunk = start_chunk; chunk <= end_chunk; chunk++) { + data_buffer cypher; + const auto start_offset = chunk * encrypted_chunk_size; + const auto end_offset = std::min( + start_offset + (total_size - (chunk * data_chunk_size)) + + encryption_header_size - 1U, + static_cast(start_offset + encrypted_chunk_size - 1U)); + + if (not reader_func(cypher, start_offset, end_offset)) { + return false; + } + + data_buffer source_buffer; + if (not utils::encryption::decrypt_data(key, cypher, source_buffer)) { + return false; + } + cypher.clear(); + + const auto data_size = static_cast(std::min( + remain, static_cast(data_chunk_size - source_offset))); + std::copy(std::next(source_buffer.begin(), + static_cast(source_offset)), + std::next(source_buffer.begin(), + static_cast(source_offset + data_size)), + std::back_inserter(data)); + remain -= data_size; + source_offset = 0U; + } + + return false; +} +#endif // defined(PROJECT_ENABLE_CURL) } // namespace repertory::utils::encryption #endif // defined(PROJECT_ENABLE_LIBSODIUM) && defined (PROJECT_ENABLE_BOOST)