diff options
author | saturneric <[email protected]> | 2024-11-27 22:42:38 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-11-27 22:42:38 +0000 |
commit | af6abea57702e95c513dc1f0a7c4e5b622552806 (patch) | |
tree | b7393fe9130f3600e8e507aaff3cad39888d2a55 | |
parent | feat: improve ui logic and support more email operations (diff) | |
download | GpgFrontend-af6abea57702e95c513dc1f0a7c4e5b622552806.tar.gz GpgFrontend-af6abea57702e95c513dc1f0a7c4e5b622552806.zip |
feat: support encrypting email
m--------- | modules | 0 | ||||
-rw-r--r-- | src/sdk/GFSDKGpg.cpp | 35 | ||||
-rw-r--r-- | src/sdk/GFSDKGpg.h | 18 | ||||
-rw-r--r-- | src/ui/main_window/MainWindow.h | 6 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotFunction.cpp | 55 | ||||
-rw-r--r-- | src/ui/main_window/MainWindowSlotUI.cpp | 6 |
6 files changed, 117 insertions, 3 deletions
diff --git a/modules b/modules -Subproject 4b846455b7c232b7db63f95c6a1943ccbc7ce15 +Subproject da105c4ae189f63b74e4f2df96031caffba68af diff --git a/src/sdk/GFSDKGpg.cpp b/src/sdk/GFSDKGpg.cpp index ae467b7b..638c8aa1 100644 --- a/src/sdk/GFSDKGpg.cpp +++ b/src/sdk/GFSDKGpg.cpp @@ -33,6 +33,7 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/model/DataObject.h" +#include "core/model/GpgEncryptResult.h" #include "core/model/GpgSignResult.h" #include "core/typedef/GpgTypedef.h" @@ -113,3 +114,37 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgKeyPrimaryUID(int channel, char* key_id, s->comment = GFStrDup(primary_uid.GetComment()); return 0; } + +auto GPGFRONTEND_MODULE_SDK_EXPORT +GFGpgEncryptData(int channel, char** key_ids, int key_ids_size, char* data, + int ascii, GFGpgEncryptionResult** ps) -> int { + auto encrypt_key_ids = CharArrayToQList(key_ids, key_ids_size); + + GpgFrontend::KeyArgsList encrypt_keys; + for (const auto& encrypt_key_id : encrypt_key_ids) { + auto key = + GpgFrontend::GpgKeyGetter::GetInstance(channel).GetKey(encrypt_key_id); + if (key.IsGood()) encrypt_keys.push_back(key); + } + + if (encrypt_keys.empty()) return -1; + + auto in_buffer = GpgFrontend::GFBuffer(GFUnStrDup(data).toUtf8()); + + auto [err, data_object] = + GpgFrontend::GpgBasicOperator::GetInstance(channel).EncryptSync( + encrypt_keys, in_buffer, ascii != 0); + + if (GpgFrontend::CheckGpgError(err) != GPG_ERR_NO_ERROR) return -1; + + auto result = + GpgFrontend::ExtractParams<GpgFrontend::GpgEncryptResult>(data_object, 0); + auto out_buffer = + GpgFrontend::ExtractParams<GpgFrontend::GFBuffer>(data_object, 1); + + *ps = static_cast<GFGpgEncryptionResult*>( + GFAllocateMemory(sizeof(GFGpgEncryptionResult))); + auto* s = *ps; + s->encrypted_data = GFStrDup(out_buffer.ConvertToQByteArray()); + return 0; +} diff --git a/src/sdk/GFSDKGpg.h b/src/sdk/GFSDKGpg.h index b7b40324..fa7325fc 100644 --- a/src/sdk/GFSDKGpg.h +++ b/src/sdk/GFSDKGpg.h @@ -37,6 +37,10 @@ struct GFGpgSignResult { char* hash_algo; }; +struct GFGpgEncryptionResult { + char* encrypted_data; +}; + struct GFGpgKeyUID { char* name; char* email; @@ -59,6 +63,20 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgSignData(int channel, char** key_ids, /** * @brief * + * @param channel + * @param key_ids + * @param key_ids_size + * @param data + * @param ascii + * @return int + */ +auto GPGFRONTEND_MODULE_SDK_EXPORT +GFGpgEncryptData(int channel, char** key_ids, int key_ids_size, char* data, + int ascii, GFGpgEncryptionResult**) -> int; + +/** + * @brief + * * @param key_id * @param data * @param mode diff --git a/src/ui/main_window/MainWindow.h b/src/ui/main_window/MainWindow.h index 6c6886d4..cd0babd3 100644 --- a/src/ui/main_window/MainWindow.h +++ b/src/ui/main_window/MainWindow.h @@ -183,6 +183,12 @@ class MainWindow : public GeneralMainWindow { void SlotSignEML(); /** + * @brief + * + */ + void SlotEncryptEML(); + + /** * @details decrypt and verify the text of currently active textedit-page * with the currently checked keys */ diff --git a/src/ui/main_window/MainWindowSlotFunction.cpp b/src/ui/main_window/MainWindowSlotFunction.cpp index 8bfb4289..e3225d6c 100644 --- a/src/ui/main_window/MainWindowSlotFunction.cpp +++ b/src/ui/main_window/MainWindowSlotFunction.cpp @@ -689,19 +689,68 @@ void MainWindow::slot_decrypt_email_by_eml_data_result_helper( }); } +void MainWindow::SlotEncryptEML() { + if (edit_->TabCount() == 0 || edit_->CurEMailPage() == nullptr) return; + auto checked_keys = m_key_list_->GetCheckedKeys(); + + if (checked_keys.isEmpty()) { + QMessageBox::warning(this, tr("No Key Selected"), + tr("Please select a key for encrypt the EML.")); + return; + } + auto buffer = edit_->CurPlainText().toUtf8(); + + Module::TriggerEvent( + "EMAIL_ENCRYPT_EML_DATA", + { + {"body_data", QString::fromLatin1(buffer.toBase64())}, + {"channel", + QString::number(m_key_list_->GetCurrentGpgContextChannel())}, + {"encrypt_keys", checked_keys.join(';')}, + }, + [=](Module::EventIdentifier i, Module::Event::ListenerIdentifier ei, + Module::Event::Params p) { + LOG_D() << "EMAIL_DECRYPT_EML_DATA callback: " << i << ei; + if (p["ret"] != "0" || !p["err"].isEmpty()) { + LOG_E() << "An error occurred trying to decrypt email, " + << "error message: " << p["err"]; + + return; + } + + if (!p["eml_data"].isEmpty()) { + edit_->SlotSetText2CurEMailPage(p.value("eml_data", "")); + } + + LOG_E() << "mime or signature data is missing"; + }); +} + void MainWindow::SlotSignEML() { if (edit_->TabCount() == 0 || edit_->CurEMailPage() == nullptr) return; - if (m_key_list_->GetCheckedKeys().isEmpty()) return; + auto checked_keys = m_key_list_->GetCheckedKeys(); + + if (checked_keys.isEmpty()) { + QMessageBox::warning(this, tr("No Key Selected"), + tr("Please select a key for signing the EML.")); + return; + } + + if (checked_keys.size() > 1) { + QMessageBox::warning(this, tr("Multiple Keys Selected"), + tr("Please select only one key to sign the EML.")); + return; + } auto buffer = edit_->CurPlainText().toUtf8(); Module::TriggerEvent( - "EMAIL_EXPORT_EML_DATA", + "EMAIL_SIGN_EML_DATA", { {"body_data", QString::fromLatin1(buffer.toBase64())}, {"channel", QString::number(m_key_list_->GetCurrentGpgContextChannel())}, - {"sign_key", m_key_list_->GetCheckedKeys().front()}, + {"sign_key", checked_keys.front()}, }, [=](Module::EventIdentifier i, Module::Event::ListenerIdentifier ei, Module::Event::Params p) { diff --git a/src/ui/main_window/MainWindowSlotUI.cpp b/src/ui/main_window/MainWindowSlotUI.cpp index d99325ec..576700bc 100644 --- a/src/ui/main_window/MainWindowSlotUI.cpp +++ b/src/ui/main_window/MainWindowSlotUI.cpp @@ -217,6 +217,12 @@ void MainWindow::SlotGeneralEncrypt(bool) { this->SlotDirectoryEncrypt(path); } } + + if (edit_->CurEMailPage() != nullptr) { + this->SlotEncryptEML(); + return; + } + if (edit_->SlotCurPageTextEdit() != nullptr) { this->SlotEncrypt(); } |