aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/gpg/GpgFileOpera.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2023-12-31 06:10:31 +0000
committersaturneric <[email protected]>2023-12-31 06:10:31 +0000
commit5030380bc5e155252655e2ad71e63c15271054c4 (patch)
tree80a4592451267c83da6f8569b3999539884d08e9 /src/core/function/gpg/GpgFileOpera.cpp
parentfeat: do not load entire data to memory in libarchive operations (diff)
downloadGpgFrontend-5030380bc5e155252655e2ad71e63c15271054c4.tar.gz
GpgFrontend-5030380bc5e155252655e2ad71e63c15271054c4.zip
feat: fully test basical operations of archive and file
Diffstat (limited to 'src/core/function/gpg/GpgFileOpera.cpp')
-rw-r--r--src/core/function/gpg/GpgFileOpera.cpp103
1 files changed, 101 insertions, 2 deletions
diff --git a/src/core/function/gpg/GpgFileOpera.cpp b/src/core/function/gpg/GpgFileOpera.cpp
index a8e33725..ecb292e0 100644
--- a/src/core/function/gpg/GpgFileOpera.cpp
+++ b/src/core/function/gpg/GpgFileOpera.cpp
@@ -42,6 +42,8 @@
namespace GpgFrontend {
+constexpr ssize_t kDataExchangerSize = 8192;
+
GpgFileOpera::GpgFileOpera(int channel)
: SingletonFunctionObject<GpgFileOpera>(channel) {}
@@ -75,7 +77,7 @@ void GpgFileOpera::EncryptDirectory(std::vector<GpgKey> keys,
bool ascii,
const std::filesystem::path& out_path,
const GpgOperationCallback& cb) {
- std::shared_ptr<GFDataExchanger> ex = std::make_shared<GFDataExchanger>(8192);
+ auto ex = std::make_shared<GFDataExchanger>(kDataExchangerSize);
RunGpgOperaAsync(
[=](const DataObjectPtr& data_object) -> GpgError {
@@ -127,7 +129,7 @@ void GpgFileOpera::DecryptFile(const std::filesystem::path& in_path,
void GpgFileOpera::DecryptArchive(const std::filesystem::path& in_path,
const std::filesystem::path& out_path,
const GpgOperationCallback& cb) {
- std::shared_ptr<GFDataExchanger> ex = std::make_shared<GFDataExchanger>(8192);
+ auto ex = std::make_shared<GFDataExchanger>(kDataExchangerSize);
ArchiveFileOperator::ExtractArchiveFromDataExchanger(
ex, out_path, [](GFError err, const DataObjectPtr&) {
@@ -234,6 +236,46 @@ void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys,
cb, "gpgme_op_encrypt_sign", "2.1.0");
}
+void GpgFileOpera::EncryptSignDirectory(KeyArgsList keys,
+ KeyArgsList signer_keys,
+ const std::filesystem::path& in_path,
+ bool ascii,
+ const std::filesystem::path& out_path,
+ const GpgOperationCallback& cb) {
+ auto ex = std::make_shared<GFDataExchanger>(kDataExchangerSize);
+
+ 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(ex);
+ 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");
+
+ ArchiveFileOperator::NewArchive2DataExchanger(
+ in_path, ex, [=](GFError err, const DataObjectPtr&) {
+ SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err);
+ });
+}
+
void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path,
const std::filesystem::path& out_path,
const GpgOperationCallback& cb) {
@@ -257,6 +299,36 @@ void GpgFileOpera::DecryptVerifyFile(const std::filesystem::path& in_path,
cb, "gpgme_op_decrypt_verify", "2.1.0");
}
+void GpgFileOpera::DecryptVerifyArchive(const std::filesystem::path& in_path,
+ const std::filesystem::path& out_path,
+ const GpgOperationCallback& cb) {
+ auto ex = std::make_shared<GFDataExchanger>(kDataExchangerSize);
+
+ ArchiveFileOperator::ExtractArchiveFromDataExchanger(
+ ex, out_path, [](GFError err, const DataObjectPtr&) {
+ SPDLOG_DEBUG("extract archive from ex operation, err: {}", err);
+ });
+
+ RunGpgOperaAsync(
+ [=](const DataObjectPtr& data_object) -> GpgError {
+ GpgError err;
+
+ GpgData data_in(in_path, true);
+ GpgData data_out(ex);
+
+ 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,
@@ -277,4 +349,31 @@ void GpgFileOpera::EncryptFileSymmetric(const std::filesystem::path& in_path,
},
cb, "gpgme_op_encrypt_symmetric", "2.1.0");
}
+
+void GpgFileOpera::EncryptDerectorySymmetric(
+ const std::filesystem::path& in_path, bool ascii,
+ const std::filesystem::path& out_path, const GpgOperationCallback& cb) {
+ auto ex = std::make_shared<GFDataExchanger>(kDataExchangerSize);
+
+ RunGpgOperaAsync(
+ [=](const DataObjectPtr& data_object) -> GpgError {
+ GpgData data_in(ex);
+ 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");
+
+ ArchiveFileOperator::NewArchive2DataExchanger(
+ in_path, ex, [=](GFError err, const DataObjectPtr&) {
+ SPDLOG_DEBUG("new archive 2 fd operation, err: {}", err);
+ });
+}
} // namespace GpgFrontend \ No newline at end of file