diff options
author | saturneric <[email protected]> | 2023-12-27 03:20:11 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2023-12-27 03:20:11 +0000 |
commit | 566d5e41a34e08b06a5c84fd4ad9f944b6b31feb (patch) | |
tree | 996184047486e1484b3101e68ebc0b43b4ccf11e /src/core | |
parent | fix: add test cases and test file basical operations (diff) | |
download | GpgFrontend-566d5e41a34e08b06a5c84fd4ad9f944b6b31feb.tar.gz GpgFrontend-566d5e41a34e08b06a5c84fd4ad9f944b6b31feb.zip |
feat: avoid reading entire file to memory
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/function/gpg/GpgFileOpera.cpp | 296 | ||||
-rw-r--r-- | src/core/function/gpg/GpgFileOpera.h | 63 | ||||
-rw-r--r-- | src/core/model/GpgData.cpp | 32 | ||||
-rw-r--r-- | src/core/model/GpgData.h | 22 |
4 files changed, 239 insertions, 174 deletions
diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 45286df2..7c247b95 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -31,120 +31,129 @@ #include "core/function/gpg/GpgBasicOperator.h" #include "core/model/GFBuffer.h" +#include "core/model/GpgData.h" #include "core/model/GpgDecryptResult.h" #include "core/model/GpgEncryptResult.h" #include "core/model/GpgKey.h" #include "core/model/GpgSignResult.h" #include "core/model/GpgVerifyResult.h" +#include "core/utils/AsyncUtils.h" #include "core/utils/GpgUtils.h" #include "core/utils/IOUtils.h" namespace GpgFrontend { +auto PathPreCheck(const std::filesystem::path& path, bool read) + -> std::tuple<bool, std::string> { + QFileInfo const file_info(path); + QFileInfo const path_info(file_info.absolutePath()); + + if (!path_info.exists()) { + return {false, _("")}; + } + if (read ? !file_info.isReadable() : !path_info.isWritable()) { + return {false, _("")}; + } + return {true, _("")}; +} + +GpgFileOpera::GpgFileOpera(int channel) + : SingletonFunctionObject<GpgFileOpera>(channel) {} + void GpgFileOpera::EncryptFile(std::vector<GpgKey> keys, const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + std::vector<gpgme_key_t> recipients(keys.begin(), keys.end()); - GpgBasicOperator::GetInstance().Encrypt( - std::move(keys), std::get<1>(read_result), ascii, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check<GpgEncryptResult, GFBuffer>()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams<GpgEncryptResult>(data_object, 0); - auto buffer = ExtractParams<GFBuffer>(data_object, 1); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - } - cb(err, TransferParams(result)); - }); + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); + data_object->Swap({GpgEncryptResult(gpgme_op_encrypt_result(ctx))}); + + return err; + }, + cb, "gpgme_op_encrypt", "2.1.0"); } void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - - GpgBasicOperator::GetInstance().Decrypt( - std::get<1>(read_result), - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check<GpgDecryptResult, GFBuffer>()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams<GpgDecryptResult>(data_object, 0); - auto buffer = ExtractParams<GFBuffer>(data_object, 1); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR && - !WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - - cb(err, TransferParams(result)); - }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto err = CheckGpgError( + gpgme_op_decrypt(ctx_.DefaultContext(), data_in, data_out)); + data_object->Swap( + {GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext()))}); + + return err; + }, + cb, "gpgme_op_decrypt", "2.1.0"); } void GpgFileOpera::SignFile(KeyArgsList keys, const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - - GpgBasicOperator::GetInstance().Sign( - std::move(keys), std::get<1>(read_result), GPGME_SIG_MODE_DETACH, ascii, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check<GpgSignResult, GFBuffer>()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams<GpgSignResult>(data_object, 0); - auto buffer = ExtractParams<GFBuffer>(data_object, 1); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR && - !WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - cb(err, TransferParams(result)); - }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + + // Set Singers of this opera + GpgBasicOperator::GetInstance().SetSigners(keys, ascii); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError( + gpgme_op_sign(ctx, data_in, data_out, GPGME_SIG_MODE_DETACH)); + + data_object->Swap({ + GpgSignResult(gpgme_op_sign_result(ctx)), + }); + return err; + }, + cb, "gpgme_op_sign", "2.1.0"); } void GpgFileOpera::VerifyFile(const std::filesystem::path& data_path, const std::filesystem::path& sign_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(data_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + + GpgData data_in(data_path, true); + GpgData data_out; + if (!sign_path.empty()) { + GpgData sig_data(sign_path, true); + err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), sig_data, + data_in, nullptr)); + } else { + err = CheckGpgError(gpgme_op_verify(ctx_.DefaultContext(), data_in, + nullptr, data_out)); + } - GFBuffer sign_buffer; - if (!sign_path.empty()) { - auto read_result = ReadFileGFBuffer(sign_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - sign_buffer = std::get<1>(read_result); - } + data_object->Swap({ + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + }); - GpgBasicOperator::GetInstance().Verify( - std::get<1>(read_result), sign_buffer, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check<GpgVerifyResult>()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams<GpgVerifyResult>(data_object, 0); - cb(err, TransferParams(result)); - }); + return err; + }, + cb, "gpgme_op_verify", "2.1.0"); } void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, @@ -152,83 +161,74 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - - GpgBasicOperator::GetInstance().EncryptSign( - std::move(keys), std::move(signer_keys), std::get<1>(read_result), ascii, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check<GpgEncryptResult, GpgSignResult, GFBuffer>()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto encrypt_result = ExtractParams<GpgEncryptResult>(data_object, 0); - auto sign_result = ExtractParams<GpgSignResult>(data_object, 1); - auto buffer = ExtractParams<GFBuffer>(data_object, 2); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - } - cb(err, TransferParams(encrypt_result, sign_result)); - }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; + std::vector<gpgme_key_t> recipients(keys.begin(), keys.end()); + + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgBasicOperator::GetInstance().SetSigners(signer_keys, ascii); + + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + err = CheckGpgError(gpgme_op_encrypt_sign(ctx, recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, + data_in, data_out)); + + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + GpgSignResult(gpgme_op_sign_result(ctx)), + }); + return err; + }, + cb, "gpgme_op_encrypt_sign", "2.1.0"); } void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgError err; - GpgBasicOperator::GetInstance().DecryptVerify( - std::get<1>(read_result), - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object - ->Check<GpgDecryptResult, GpgVerifyResult, GFBuffer>()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto decrypt_result = ExtractParams<GpgDecryptResult>(data_object, 0); - auto verify_result = ExtractParams<GpgVerifyResult>(data_object, 1); - auto buffer = ExtractParams<GFBuffer>(data_object, 2); - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { - if (!WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - } - cb(err, TransferParams(decrypt_result, verify_result)); - }); + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + err = CheckGpgError( + gpgme_op_decrypt_verify(ctx_.DefaultContext(), data_in, data_out)); + + data_object->Swap({ + GpgDecryptResult(gpgme_op_decrypt_result(ctx_.DefaultContext())), + GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), + }); + + return err; + }, + cb, "gpgme_op_decrypt_verify", "2.1.0"); } + void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { - auto read_result = ReadFileGFBuffer(in_path); - if (!std::get<0>(read_result)) { - throw std::runtime_error("read file error"); - } - - GpgBasicOperator::GetInstance().EncryptSymmetric( - std::get<1>(read_result), ascii, - [=](GpgError err, const DataObjectPtr& data_object) { - if (!data_object->Check<GpgEncryptResult, GFBuffer>()) { - throw std::runtime_error("data object transfers wrong arguments"); - } - auto result = ExtractParams<GpgEncryptResult>(data_object, 0); - auto buffer = ExtractParams<GFBuffer>(data_object, 1); - if (CheckGpgError(err) != GPG_ERR_NO_ERROR) { - cb(err, TransferParams(result)); - return; - } - - if (!WriteFileGFBuffer(out_path, buffer)) { - throw std::runtime_error("write buffer to file error"); - } - - cb(err, TransferParams(result)); - }); + RunGpgOperaAsync( + [=](const DataObjectPtr& data_object) -> GpgError { + GpgData data_in(in_path, true); + GpgData data_out(out_path, false); + + auto* ctx = ascii ? ctx_.DefaultContext() : ctx_.BinaryContext(); + auto err = CheckGpgError(gpgme_op_encrypt( + ctx, nullptr, GPGME_ENCRYPT_SYMMETRIC, data_in, data_out)); + data_object->Swap({ + GpgEncryptResult(gpgme_op_encrypt_result(ctx)), + }); + + return err; + }, + cb, "gpgme_op_encrypt_symmetric", "2.1.0"); } - } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/function/gpg/GpgFileOpera.h b/src/core/function/gpg/GpgFileOpera.h index a358b60a..73649d47 100644 --- a/src/core/function/gpg/GpgFileOpera.h +++ b/src/core/function/gpg/GpgFileOpera.h @@ -28,6 +28,8 @@ #pragma once +#include "core/function/basic/GpgFunctionObject.h" +#include "core/function/gpg/GpgContext.h" #include "core/function/result_analyse/GpgResultAnalyse.h" #include "core/typedef/GpgTypedef.h" @@ -38,9 +40,18 @@ namespace GpgFrontend { * * @class class: GpgBasicOperator */ -class GPGFRONTEND_CORE_EXPORT GpgFileOpera { +class GPGFRONTEND_CORE_EXPORT GpgFileOpera + : public SingletonFunctionObject<GpgFileOpera> { public: /** + * @brief Construct a new Gpg File Opera object + * + * @param channel + */ + explicit GpgFileOpera( + int channel = SingletonFunctionObject::GetDefaultChannel()); + + /** * @brief Encrypted file with public key * * @param keys Used public key @@ -50,10 +61,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel Channel in context * @return unsigned int error code */ - static void EncryptFile(KeyArgsList keys, - const std::filesystem::path& in_path, bool ascii, - const std::filesystem::path& out_path, - const GpgOperationCallback& cb); + void EncryptFile(KeyArgsList keys, const std::filesystem::path& in_path, + bool ascii, const std::filesystem::path& out_path, + const GpgOperationCallback& cb); /** * @brief Encrypted file symmetrically (with password) @@ -64,10 +74,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel * @return unsigned int */ - static void EncryptFileSymmetric(const std::filesystem::path& in_path, - bool ascii, - const std::filesystem::path& out_path, - const GpgOperationCallback& cb); + void EncryptFileSymmetric(const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, + const GpgOperationCallback& cb); /** * @brief @@ -77,9 +86,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param result * @return GpgError */ - static void DecryptFile(const std::filesystem::path& in_path, - const std::filesystem::path& out_path, - const GpgOperationCallback& cb); + void DecryptFile(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, + const GpgOperationCallback& cb); /** * @brief Sign file with private key @@ -91,9 +100,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel * @return GpgError */ - static void SignFile(KeyArgsList keys, const std::filesystem::path& in_path, - bool ascii, const std::filesystem::path& out_path, - const GpgOperationCallback& cb); + void SignFile(KeyArgsList keys, const std::filesystem::path& in_path, + bool ascii, const std::filesystem::path& out_path, + const GpgOperationCallback& cb); /** * @brief Verify file with public key @@ -104,9 +113,9 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel Channel in context * @return GpgError */ - static void VerifyFile(const std::filesystem::path& data_path, - const std::filesystem::path& sign_path, - const GpgOperationCallback& cb); + void VerifyFile(const std::filesystem::path& data_path, + const std::filesystem::path& sign_path, + const GpgOperationCallback& cb); /** * @brief Encrypt and sign file with public key and private key @@ -120,10 +129,10 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param channel * @return GpgError */ - static void EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, - const std::filesystem::path& in_path, bool ascii, - const std::filesystem::path& out_path, - const GpgOperationCallback& cb); + void EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, + const std::filesystem::path& in_path, bool ascii, + const std::filesystem::path& out_path, + const GpgOperationCallback& cb); /** * @brief @@ -134,9 +143,13 @@ class GPGFRONTEND_CORE_EXPORT GpgFileOpera { * @param verify_res * @return GpgError */ - static void DecryptVerifyFile(const std::filesystem::path& in_path, - const std::filesystem::path& out_path, - const GpgOperationCallback& cb); + void DecryptVerifyFile(const std::filesystem::path& in_path, + const std::filesystem::path& out_path, + const GpgOperationCallback& cb); + + private: + GpgContext& ctx_ = GpgContext::GetInstance( + SingletonFunctionObject::GetChannel()); ///< Corresponding context }; } // namespace GpgFrontend diff --git a/src/core/model/GpgData.cpp b/src/core/model/GpgData.cpp index 19f45e22..6a6105eb 100644 --- a/src/core/model/GpgData.cpp +++ b/src/core/model/GpgData.cpp @@ -28,6 +28,8 @@ #include "core/model/GpgData.h" +#include <unistd.h> + #include "core/typedef/GpgTypedef.h" namespace GpgFrontend { @@ -63,6 +65,35 @@ GpgData::GpgData(const void* buffer, size_t size, bool copy) { data_ref_ = std::unique_ptr<struct gpgme_data, DataRefDeleter>(data); } +GpgData::GpgData(int fd) : fd_(fd) { + gpgme_data_t data; + + auto err = gpgme_data_new_from_fd(&data, fd); + assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + + data_ref_ = std::unique_ptr<struct gpgme_data, DataRefDeleter>(data); +} + +GpgData::GpgData(const std::filesystem::path& path, bool read) { + gpgme_data_t data; + + fp_ = fopen(path.string().c_str(), read ? "rb" : "wb"); + auto err = gpgme_data_new_from_stream(&data, fp_); + assert(gpgme_err_code(err) == GPG_ERR_NO_ERROR); + + data_ref_ = std::unique_ptr<struct gpgme_data, DataRefDeleter>(data); +} + +GpgData::~GpgData() { + if (fp_ != nullptr) { + fclose(fp_); + } + + if (fd_ >= 0) { + close(fd_); + } +} + auto GpgData::Read2Buffer() -> ByteArrayPtr { gpgme_off_t ret = gpgme_data_seek(*this, 0, SEEK_SET); ByteArrayPtr out_buffer = std::make_unique<std::string>(); @@ -110,5 +141,4 @@ auto GpgData::Read2GFBuffer() -> GFBuffer { } GpgData::operator gpgme_data_t() { return data_ref_.get(); } - } // namespace GpgFrontend
\ No newline at end of file diff --git a/src/core/model/GpgData.h b/src/core/model/GpgData.h index cd2e193e..4abde270 100644 --- a/src/core/model/GpgData.h +++ b/src/core/model/GpgData.h @@ -57,10 +57,30 @@ class GPGFRONTEND_CORE_EXPORT GpgData { /** * @brief Construct a new Gpg Data object * + * @param fd + */ + explicit GpgData(int fd); + + /** + * @brief Construct a new Gpg Data object + * + * @param path + */ + explicit GpgData(const std::filesystem::path& path, bool read); + + /** + * @brief Construct a new Gpg Data object + * */ explicit GpgData(GFBuffer); /** + * @brief Destroy the Gpg Data object + * + */ + ~GpgData(); + + /** * @brief * * @return gpgme_data_t @@ -95,6 +115,8 @@ class GPGFRONTEND_CORE_EXPORT GpgData { GFBuffer cached_buffer_; std::unique_ptr<struct gpgme_data, DataRefDeleter> data_ref_ = nullptr; ///< + FILE* fp_ = nullptr; + int fd_ = -1; }; } // namespace GpgFrontend |