/** * Copyright (C) 2021 Saturneric * * This file is part of GpgFrontend. * * GpgFrontend is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * GpgFrontend is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GpgFrontend. If not, see . * * The initial version of the source code is inherited from * the gpg4usb project, which is under GPL-3.0-or-later. * * All the source code of GpgFrontend was modified and released by * Saturneric starting on May 12, 2021. * * SPDX-License-Identifier: GPL-3.0-or-later * */ #include "GpgFileOpera.h" #include #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 { 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(channel) {} void GpgFileOpera::EncryptFile(std::vector keys, const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { std::vector recipients(keys.begin(), keys.end()); // 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) { 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) { 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) { 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)); } data_object->Swap({ GpgVerifyResult(gpgme_op_verify_result(ctx_.DefaultContext())), }); return err; }, cb, "gpgme_op_verify", "2.1.0"); } void GpgFileOpera::EncryptSignFile(KeyArgsList keys, KeyArgsList signer_keys, const std::filesystem::path& in_path, bool ascii, const std::filesystem::path& out_path, const GpgOperationCallback& cb) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { GpgError err; std::vector 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) { RunGpgOperaAsync( [=](const DataObjectPtr& data_object) -> GpgError { GpgError err; 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) { 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