diff options
author | saturneric <[email protected]> | 2024-11-28 16:19:46 +0000 |
---|---|---|
committer | saturneric <[email protected]> | 2024-11-28 16:19:46 +0000 |
commit | 564b8099189b20b9cf2fa78bfa2620d42f514b72 (patch) | |
tree | d35bd61198618ce6897ae4565a8470597e896983 /src/sdk/GFSDKGpg.cpp | |
parent | feat: support encrypting email (diff) | |
download | GpgFrontend-564b8099189b20b9cf2fa78bfa2620d42f514b72.tar.gz GpgFrontend-564b8099189b20b9cf2fa78bfa2620d42f514b72.zip |
feat: support decrypt & verify email
Diffstat (limited to 'src/sdk/GFSDKGpg.cpp')
-rw-r--r-- | src/sdk/GFSDKGpg.cpp | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/src/sdk/GFSDKGpg.cpp b/src/sdk/GFSDKGpg.cpp index 638c8aa1..4dd23417 100644 --- a/src/sdk/GFSDKGpg.cpp +++ b/src/sdk/GFSDKGpg.cpp @@ -33,12 +33,15 @@ #include "core/function/gpg/GpgKeyGetter.h" #include "core/function/gpg/GpgKeyImportExporter.h" #include "core/model/DataObject.h" +#include "core/model/GpgDecryptResult.h" #include "core/model/GpgEncryptResult.h" #include "core/model/GpgSignResult.h" +#include "core/model/GpgVerifyResult.h" #include "core/typedef/GpgTypedef.h" +#include "core/utils/GpgUtils.h" +#include "ui/UIModuleManager.h" // -#include "core/utils/GpgUtils.h" #include "private/GFSDKPrivat.h" auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgSignData(int channel, char** key_ids, @@ -72,11 +75,15 @@ auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgSignData(int channel, char** key_ids, auto out_buffer = GpgFrontend::ExtractParams<GpgFrontend::GFBuffer>(data_object, 1); + auto capsule_id = + GpgFrontend::UI::UIModuleManager::GetInstance().MakeCapsule(result); + *ps = static_cast<GFGpgSignResult*>(GFAllocateMemory(sizeof(GFGpgSignResult))); auto* s = *ps; s->signature = GFStrDup(out_buffer.ConvertToQByteArray()); s->hash_algo = GFStrDup(result.HashAlgo()); + s->capsule_id = GFStrDup(capsule_id); return 0; } @@ -142,9 +149,63 @@ GFGpgEncryptData(int channel, char** key_ids, int key_ids_size, char* data, auto out_buffer = GpgFrontend::ExtractParams<GpgFrontend::GFBuffer>(data_object, 1); + auto capsule_id = + GpgFrontend::UI::UIModuleManager::GetInstance().MakeCapsule(result); + *ps = static_cast<GFGpgEncryptionResult*>( GFAllocateMemory(sizeof(GFGpgEncryptionResult))); auto* s = *ps; s->encrypted_data = GFStrDup(out_buffer.ConvertToQByteArray()); + s->capsule_id = GFStrDup(capsule_id); + return 0; +} + +auto GPGFRONTEND_MODULE_SDK_EXPORT +GFGpgDecryptData(int channel, char* data, GFGpgDecryptResult** ps) -> int { + auto in_buffer = GpgFrontend::GFBuffer(GFUnStrDup(data).toUtf8()); + + auto [err, data_object] = + GpgFrontend::GpgBasicOperator::GetInstance(channel).DecryptSync( + in_buffer); + + if (GpgFrontend::CheckGpgError(err) != GPG_ERR_NO_ERROR) return -1; + + auto result = + GpgFrontend::ExtractParams<GpgFrontend::GpgDecryptResult>(data_object, 0); + auto out_buffer = + GpgFrontend::ExtractParams<GpgFrontend::GFBuffer>(data_object, 1); + + auto capsule_id = + GpgFrontend::UI::UIModuleManager::GetInstance().MakeCapsule(result); + + *ps = static_cast<GFGpgDecryptResult*>( + GFAllocateMemory(sizeof(GFGpgDecryptResult))); + auto* s = *ps; + s->decrypted_data = GFStrDup(out_buffer.ConvertToQByteArray()); + s->capsule_id = GFStrDup(capsule_id); + return 0; +} + +auto GPGFRONTEND_MODULE_SDK_EXPORT GFGpgVerifyData( + int channel, char* data, char* signature, GFGpgVerifyResult** ps) -> int { + auto in_buffer = GpgFrontend::GFBuffer(GFUnStrDup(data).toUtf8()); + auto sig_buffer = GpgFrontend::GFBuffer(GFUnStrDup(signature).toUtf8()); + + auto [err, data_object] = + GpgFrontend::GpgBasicOperator::GetInstance(channel).VerifySync( + in_buffer, sig_buffer); + + if (GpgFrontend::CheckGpgError(err) != GPG_ERR_NO_ERROR) return -1; + + auto result = + GpgFrontend::ExtractParams<GpgFrontend::GpgVerifyResult>(data_object, 0); + + auto capsule_id = + GpgFrontend::UI::UIModuleManager::GetInstance().MakeCapsule(result); + + *ps = static_cast<GFGpgVerifyResult*>( + GFAllocateMemory(sizeof(GFGpgVerifyResult))); + auto* s = *ps; + s->capsule_id = GFStrDup(capsule_id); return 0; } |