diff options
Diffstat (limited to '')
-rw-r--r-- | src/core/function/gpg/GpgBasicOperator.cpp | 84 | ||||
-rw-r--r-- | src/core/function/gpg/GpgBasicOperator.h | 17 | ||||
-rw-r--r-- | src/core/function/gpg/GpgFileOpera.cpp | 52 | ||||
-rw-r--r-- | src/core/model/GpgData.cpp | 2 | ||||
-rw-r--r-- | src/core/model/GpgData.h | 2 | ||||
-rw-r--r-- | src/core/typedef/CoreTypedef.h | 1 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 149 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotUI.cpp | 6 |
8 files changed, 155 insertions, 158 deletions
diff --git a/src/core/function/gpg/GpgBasicOperator.cpp b/src/core/function/gpg/GpgBasicOperator.cpp index d8a45489..63def65f 100644 --- a/src/core/function/gpg/GpgBasicOperator.cpp +++ b/src/core/function/gpg/GpgBasicOperator.cpp @@ -28,47 +28,51 @@ #include "GpgBasicOperator.h" +#include <gpg-error.h> + #include "core/GpgModel.h" +#include "core/utils/AsyncUtils.h" #include "core/utils/GpgUtils.h" +namespace GpgFrontend { + GpgFrontend::GpgBasicOperator::GpgBasicOperator(int channel) : SingletonFunctionObject<GpgBasicOperator>(channel) {} -auto GpgFrontend::GpgBasicOperator::Encrypt( - KeyListPtr keys, GpgFrontend::BypeArrayRef in_buffer, - GpgFrontend::ByteArrayPtr& out_buffer, GpgFrontend::GpgEncrResult& result) - -> GpgFrontend::GpgError { - // gpgme_encrypt_result_t e_result; - gpgme_key_t recipients[keys->size() + 1]; - - int index = 0; - for (const auto& key : *keys) { - recipients[index++] = static_cast<gpgme_key_t>(key); - } - - // Last entry data_in array has to be nullptr - recipients[keys->size()] = nullptr; - - GpgData data_in(in_buffer.data(), in_buffer.size()); - GpgData data_out; - - gpgme_error_t err = CheckGpgError( - gpgme_op_encrypt(ctx_.DefaultContext(), recipients, - GPGME_ENCRYPT_ALWAYS_TRUST, data_in, data_out)); - - auto temp_data_out = data_out.Read2Buffer(); - std::swap(temp_data_out, out_buffer); - - auto temp_result = NewResult(gpgme_op_encrypt_result(ctx_.DefaultContext())); - std::swap(result, temp_result); - - return err; +void GpgFrontend::GpgBasicOperator::Encrypt(KeyListPtr keys, + ConstBypeArrayRef in_buffer, + const GpgOperationCallback& cb) { + RunGpgOperaAsync( + [&](const DataObjectPtr& data_object) -> GpgError { + SPDLOG_DEBUG("key size: {}", keys->size()); + std::vector<gpgme_key_t> recipients(keys->size() + 1); + + for (const auto& key : *keys) { + recipients.emplace_back(key); + } + + // Last entry data_in array has to be nullptr + recipients.emplace_back(nullptr); + + GpgData data_in(in_buffer.data(), in_buffer.size()); + GpgData data_out; + + auto err = CheckGpgError( + gpgme_op_encrypt(ctx_.DefaultContext(), recipients.data(), + GPGME_ENCRYPT_ALWAYS_TRUST, data_in, data_out)); + data_object->Swap( + {NewResult(gpgme_op_encrypt_result(ctx_.DefaultContext())), + data_out.Read2Buffer()}); + + return err; + }, + cb, "gpgme_op_encrypt", "2.1.0"); } auto GpgFrontend::GpgBasicOperator::Decrypt( BypeArrayRef in_buffer, GpgFrontend::ByteArrayPtr& out_buffer, GpgFrontend::GpgDecrResult& result) -> GpgFrontend::GpgError { - gpgme_error_t err; + GpgError err; GpgData data_in(in_buffer.data(), in_buffer.size()); GpgData data_out; @@ -88,7 +92,7 @@ auto GpgFrontend::GpgBasicOperator::Verify(BypeArrayRef& in_buffer, ByteArrayPtr& sig_buffer, GpgVerifyResult& result) const -> GpgFrontend::GpgError { - gpgme_error_t err; + GpgError err; GpgData data_in(in_buffer.data(), in_buffer.size()); GpgData data_out; @@ -111,7 +115,7 @@ auto GpgFrontend::GpgBasicOperator::Verify(BypeArrayRef& in_buffer, auto GpgFrontend::GpgBasicOperator::Sign( KeyListPtr signers, BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, gpgme_sig_mode_t mode, GpgSignResult& result) -> GpgFrontend::GpgError { - gpgme_error_t err; + GpgError err; // Set Singers of this opera SetSigners(*signers); @@ -134,9 +138,8 @@ auto GpgFrontend::GpgBasicOperator::Sign( auto GpgFrontend::GpgBasicOperator::DecryptVerify( BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, - GpgDecrResult& decrypt_result, GpgVerifyResult& verify_result) - -> gpgme_error_t { - gpgme_error_t err; + GpgDecrResult& decrypt_result, GpgVerifyResult& verify_result) -> GpgError { + GpgError err; GpgData data_in(in_buffer.data(), in_buffer.size()); GpgData data_out; @@ -161,8 +164,8 @@ auto GpgFrontend::GpgBasicOperator::DecryptVerify( auto GpgFrontend::GpgBasicOperator::EncryptSign( KeyListPtr keys, KeyListPtr signers, BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, GpgEncrResult& encr_result, - GpgSignResult& sign_result) -> gpgme_error_t { - gpgme_error_t err; + GpgSignResult& sign_result) -> GpgError { + GpgError err; SetSigners(*signers); // gpgme_encrypt_result_t e_result; @@ -230,9 +233,9 @@ auto GpgFrontend::GpgBasicOperator::EncryptSymmetric( GpgData data_in(in_buffer.data(), in_buffer.size()); GpgData data_out; - gpgme_error_t err = CheckGpgError( - gpgme_op_encrypt(ctx_.DefaultContext(), nullptr, GPGME_ENCRYPT_SYMMETRIC, - data_in, data_out)); + GpgError err = CheckGpgError(gpgme_op_encrypt(ctx_.DefaultContext(), nullptr, + GPGME_ENCRYPT_SYMMETRIC, + data_in, data_out)); auto temp_data_out = data_out.Read2Buffer(); std::swap(temp_data_out, out_buffer); @@ -246,3 +249,4 @@ auto GpgFrontend::GpgBasicOperator::EncryptSymmetric( return err; } +} // namespace GpgFrontend diff --git a/src/core/function/gpg/GpgBasicOperator.h b/src/core/function/gpg/GpgBasicOperator.h index e72db861..72cd0347 100644 --- a/src/core/function/gpg/GpgBasicOperator.h +++ b/src/core/function/gpg/GpgBasicOperator.h @@ -63,8 +63,7 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * @param result the result of the operation * @return error code */ - auto Encrypt(KeyListPtr keys, BypeArrayRef in_buffer, - ByteArrayPtr& out_buffer, GpgEncrResult& result) -> gpg_error_t; + void Encrypt(KeyListPtr, ConstBypeArrayRef, const GpgOperationCallback&); /** * @brief Call the interface provided by GPGME to symmetrical encryption @@ -72,10 +71,10 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * @param in_buffer Data for encryption * @param out_buffer Encrypted data * @param result Encrypted results - * @return gpg_error_t + * @return GpgError */ auto EncryptSymmetric(BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, - GpgEncrResult& result) -> gpg_error_t; + GpgEncrResult& result) -> GpgError; /** * @@ -92,7 +91,7 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator */ auto EncryptSign(KeyListPtr keys, KeyListPtr signers, BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, GpgEncrResult& encr_result, - GpgSignResult& sign_result) -> gpgme_error_t; + GpgSignResult& sign_result) -> GpgError; /** * @brief Call the interface provided by gpgme for decryption operation @@ -103,7 +102,7 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * @return error code */ auto Decrypt(BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, - GpgDecrResult& result) -> gpgme_error_t; + GpgDecrResult& result) -> GpgError; /** * @brief Call the interface provided by gpgme to perform decryption and @@ -117,7 +116,7 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator */ auto DecryptVerify(BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, GpgDecrResult& decrypt_result, - GpgVerifyResult& verify_result) -> gpgme_error_t; + GpgVerifyResult& verify_result) -> GpgError; /** * @brief Call the interface provided by gpgme for verification operation @@ -128,7 +127,7 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator * @return error code */ auto Verify(BypeArrayRef in_buffer, ByteArrayPtr& sig_buffer, - GpgVerifyResult& result) const -> gpgme_error_t; + GpgVerifyResult& result) const -> GpgError; /** * @brief Call the interface provided by gpgme for signing operation @@ -152,7 +151,7 @@ class GPGFRONTEND_CORE_EXPORT GpgBasicOperator */ auto Sign(KeyListPtr signers, BypeArrayRef in_buffer, ByteArrayPtr& out_buffer, gpgme_sig_mode_t mode, - GpgSignResult& result) -> gpg_error_t; + GpgSignResult& result) -> GpgError; /** * @brief Set the private key for signatures, this operation is a global diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp index 8d5e3369..bce3fcd8 100644 --- a/src/core/function/gpg/GpgFileOpera.cpp +++ b/src/core/function/gpg/GpgFileOpera.cpp @@ -36,32 +36,32 @@ auto GpgFrontend::GpgFileOpera::EncryptFile(KeyListPtr keys, const std::string& out_path, GpgEncrResult& result, int _channel) -> GpgFrontend::GpgError { -#ifdef WINDOWS - auto in_path_std = - std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); - auto out_path_std = - std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); -#else - auto in_path_std = std::filesystem::path(in_path); - auto out_path_std = std::filesystem::path(out_path); -#endif - - std::string in_buffer; - if (!ReadFileStd(in_path_std, in_buffer)) { - throw std::runtime_error("read file error"); - } - - ByteArrayPtr out_buffer = nullptr; - - auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( - std::move(keys), in_buffer, out_buffer, result); - - if (CheckGpgError(err) == GPG_ERR_NO_ERROR) - if (!WriteFileStd(out_path_std, *out_buffer)) { - throw std::runtime_error("WriteBufferToFile error"); - }; - - return err; + // #ifdef WINDOWS + // auto in_path_std = + // std::filesystem::path(QString::fromStdString(in_path).toStdU16String()); + // auto out_path_std = + // std::filesystem::path(QString::fromStdString(out_path).toStdU16String()); + // #else + // auto in_path_std = std::filesystem::path(in_path); + // auto out_path_std = std::filesystem::path(out_path); + // #endif + + // std::string in_buffer; + // if (!ReadFileStd(in_path_std, in_buffer)) { + // throw std::runtime_error("read file error"); + // } + + // ByteArrayPtr out_buffer = nullptr; + + // auto err = GpgBasicOperator::GetInstance(_channel).Encrypt( + // std::move(keys), in_buffer, out_buffer, result); + + // if (CheckGpgError(err) == GPG_ERR_NO_ERROR) + // if (!WriteFileStd(out_path_std, *out_buffer)) { + // throw std::runtime_error("WriteBufferToFile error"); + // }; + + // return err; } auto GpgFrontend::GpgFileOpera::DecryptFile(const std::string& in_path, diff --git a/src/core/model/GpgData.cpp b/src/core/model/GpgData.cpp index b4ebff18..60e6ace2 100644 --- a/src/core/model/GpgData.cpp +++ b/src/core/model/GpgData.cpp @@ -37,7 +37,7 @@ GpgFrontend::GpgData::GpgData() { data_ref_ = std::unique_ptr<struct gpgme_data, DataRefDeleter>(data); } -GpgFrontend::GpgData::GpgData(void* buffer, size_t size, bool copy) { +GpgFrontend::GpgData::GpgData(const void* buffer, size_t size, bool copy) { gpgme_data_t data; auto err = gpgme_data_new_from_mem(&data, static_cast<const char*>(buffer), diff --git a/src/core/model/GpgData.h b/src/core/model/GpgData.h index fa4910d5..695f821f 100644 --- a/src/core/model/GpgData.h +++ b/src/core/model/GpgData.h @@ -50,7 +50,7 @@ class GpgData { * @param size * @param copy */ - GpgData(void* buffer, size_t size, bool copy = true); + GpgData(const void* buffer, size_t size, bool copy = true); /** * @brief diff --git a/src/core/typedef/CoreTypedef.h b/src/core/typedef/CoreTypedef.h index fe2ed87b..ccb63bc7 100644 --- a/src/core/typedef/CoreTypedef.h +++ b/src/core/typedef/CoreTypedef.h @@ -34,6 +34,7 @@ using ByteArray = std::string; ///< using ByteArrayPtr = std::shared_ptr<ByteArray>; ///< using StdBypeArrayPtr = std::shared_ptr<ByteArray>; ///< using BypeArrayRef = ByteArray&; ///< +using ConstBypeArrayRef = const ByteArray&; ///< using BypeArrayConstRef = const ByteArray&; ///< using StringArgsPtr = std::unique_ptr<std::vector<std::string>>; ///< using StringArgsRef = std::vector<std::string>&; ///< diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 7d0833c6..eea2075c 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -42,8 +42,10 @@ #include "core/function/result_analyse/GpgVerifyResultAnalyse.h" #include "core/model/DataObject.h" #include "core/module/ModuleManager.h" +#include "core/typedef/GpgTypedef.h" #include "core/utils/CommonUtils.h" #include "core/utils/GpgUtils.h" +#include "spdlog/spdlog.h" #include "ui/UserInterfaceUtils.h" #include "ui/dialog/SignersPicker.h" #include "ui/dialog/help/AboutDialog.h" @@ -53,6 +55,7 @@ #include "ui/widgets/FindWidget.h" namespace GpgFrontend::UI { + /** * Encrypt Entry(Text & File) */ @@ -63,37 +66,7 @@ void MainWindow::slot_encrypt() { } auto key_ids = m_key_list_->GetChecked(); - - // data to transfer into task - auto data_object = TransferParams( - edit_->CurTextPage()->GetTextPage()->toPlainText().toStdString()); - - // the callback function - auto result_callback = [this](int rtn, const DataObjectPtr& data_object) { - if (rtn == 0) { - if (!data_object->Check<GpgError, GpgEncrResult, ByteArrayPtr>()) - throw std::runtime_error("data object doesn't pass checking"); - auto error = ExtractParams<GpgError>(data_object, 0); - auto result = ExtractParams<GpgEncrResult>(data_object, 1); - auto tmp = ExtractParams<ByteArrayPtr>(data_object, 2); - - auto resultAnalyse = GpgEncryptResultAnalyse(error, std::move(result)); - resultAnalyse.Analyse(); - process_result_analyse(edit_, info_board_, resultAnalyse); - - if (CheckGpgError(error) == GPG_ERR_NO_ERROR) - edit_->SlotFillTextEditWithText(QString::fromStdString(*tmp)); - info_board_->ResetOptionActionsMenu(); - } else { - QMessageBox::critical(this, _("Error"), - _("An error occurred during operation.")); - return; - } - }; - - Thread::Task::TaskRunnable encrypt_runner = nullptr; - - std::string encrypt_type; + auto buffer = edit_->CurTextPage()->GetTextPage()->toPlainText(); if (key_ids->empty()) { // Symmetric Encrypt @@ -105,24 +78,50 @@ void MainWindow::slot_encrypt() { if (ret == QMessageBox::Cancel) return; - encrypt_type = _("Symmetrically Encrypting"); - encrypt_runner = [](const DataObjectPtr& data_object) -> int { - if (data_object == nullptr || !data_object->Check<std::string>()) - throw std::runtime_error("data object doesn't pass checking"); - auto buffer = ExtractParams<std::string>(data_object, 0); - try { - GpgEncrResult result = nullptr; - auto tmp = GpgFrontend::SecureCreateSharedObject<ByteArray>(); - GpgError error = - GpgFrontend::GpgBasicOperator::GetInstance().EncryptSymmetric( - buffer, tmp, result); - - data_object->Swap(DataObject{error, std::move(result), std::move(tmp)}); - } catch (const std::runtime_error& e) { - return -1; - } - return 0; - }; + // encrypt_type = ; + // encrypt_runner = [](const DataObjectPtr& data_object) -> int { + // if (data_object == nullptr || !data_object->Check<std::string>()) { + // throw std::runtime_error("data object doesn't pass checking"); + // } + // auto buffer = ExtractParams<std::string>(data_object, 0); + // try { + // GpgEncrResult result = nullptr; + // auto tmp = GpgFrontend::SecureCreateSharedObject<ByteArray>(); + // GpgError error = + // GpgFrontend::GpgBasicOperator::GetInstance().EncryptSymmetric( + // buffer, tmp, result); + + // data_object->Swap(DataObject{error, std::move(result), + // std::move(tmp)}); + // } catch (const std::runtime_error& e) { + // return -1; + // } + // return 0; + // }; + + // CommonUtils::WaitForOpera( + // this, _("Symmetrically Encrypting"), + // [this, keys, buffer](const OperaWaitingHd& hd) { + // GpgFrontend::GpgBasicOperator::GetInstance().Encrypt( + // std::move(keys), buffer.toStdString(), + // [this, hd](GpgError err, const DataObjectPtr& data_obj) { + // auto result = ExtractParams<GpgEncrResult>(data_obj, 0); + // auto buffer = ExtractParams<ByteArrayPtr>(data_obj, 1); + + // auto resultAnalyse = + // GpgEncryptResultAnalyse(err, std::move(result)); + // resultAnalyse.Analyse(); + // process_result_analyse(edit_, info_board_, resultAnalyse); + + // if (CheckGpgError(err) == GPG_ERR_NO_ERROR) + // edit_->SlotFillTextEditWithText( + // QString::fromStdString(*buffer)); + // info_board_->ResetOptionActionsMenu(); + + // // stop waiting + // hd(); + // }); + // }); } else { auto keys = GpgKeyGetter::GetInstance().GetKeys(key_ids); @@ -139,35 +138,31 @@ void MainWindow::slot_encrypt() { } } - // push the keys into data object - data_object->AppendObject(std::move(keys)); - - // Asymmetric Encrypt - encrypt_type = _("Encrypting"); - encrypt_runner = [](DataObjectPtr data_object) -> int { - // check the size of the data object - if (data_object == nullptr || - !data_object->Check<std::string, KeyListPtr>()) - throw std::runtime_error("data object doesn't pass checking"); - - auto buffer = ExtractParams<std::string>(data_object, 0); - auto keys = ExtractParams<KeyListPtr>(data_object, 1); - try { - GpgEncrResult result = nullptr; - auto tmp = GpgFrontend::SecureCreateSharedObject<ByteArray>(); - GpgError error = GpgFrontend::GpgBasicOperator::GetInstance().Encrypt( - std::move(keys), buffer, tmp, result); - - data_object->Swap({error, result, tmp}); - } catch (const std::runtime_error& e) { - return -1; - } - return 0; - }; + CommonUtils::WaitForOpera( + this, _("Encrypting"), [this, keys, buffer](const OperaWaitingHd& hd) { + SPDLOG_DEBUG("[*] size of gpg key list: {}", keys->size()); + GpgFrontend::GpgBasicOperator::GetInstance().Encrypt( + keys, buffer.toStdString(), + [this, hd](GpgError err, const DataObjectPtr& data_obj) { + auto result = ExtractParams<GpgEncrResult>(data_obj, 0); + auto buffer = ExtractParams<ByteArrayPtr>(data_obj, 1); + + auto result_analyse = + GpgEncryptResultAnalyse(err, std::move(result)); + result_analyse.Analyse(); + process_result_analyse(edit_, info_board_, result_analyse); + + if (CheckGpgError(err) == GPG_ERR_NO_ERROR) { + edit_->SlotFillTextEditWithText( + QString::fromStdString(*buffer)); + } + info_board_->ResetOptionActionsMenu(); + + // stop waiting + hd(); + }); + }); } - // Do the task - process_operation(this, _("Encrypting"), std::move(encrypt_runner), - std::move(result_callback), data_object); } void MainWindow::slot_sign() { diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp index e0459955..37eb688b 100644 --- a/src/ui/main_window/MainWindowSlotUI.cpp +++ b/src/ui/main_window/MainWindowSlotUI.cpp @@ -26,16 +26,14 @@ * */ -#include <qnamespace.h> - #include "MainWindow.h" -#include "UISignalStation.h" #include "core/GpgConstants.h" #include "core/function/CoreSignalStation.h" #include "core/function/GlobalSettingStation.h" -#include "function/RaisePinentry.h" +#include "ui/UISignalStation.h" #include "ui/UserInterfaceUtils.h" #include "ui/dialog/Wizard.h" +#include "ui/function/RaisePinentry.h" #include "ui/main_window/KeyMgmt.h" #include "ui/struct/SettingsObject.h" |