From 20b32e03503f8c3187508d9cd5ba07db2b03f014 Mon Sep 17 00:00:00 2001 From: Ingo Klöcker Date: Thu, 21 Dec 2023 11:53:23 +0100 Subject: qt: Support decryption (with verification) directly to/from files * lang/qt/src/Makefile.am: Add new files. * lang/qt/src/job.cpp (DecryptVerifyJob): Move definition of constructor and destructor and inclusion of the moc file to the corresponding .cpp file. * lang/qt/src/decryptverifyjob.cpp: New. * lang/qt/src/decryptverifyjob.h (DecryptVerifyJob): Add member functions setInputFile, inputFile, setOutputFile, outputFile. * lang/qt/src/decryptverifyjob_p.h: New. * lang/qt/src/qgpgmedecryptverifyjob.cpp (class QGpgMEDecryptVerifyJobPrivate): New. (QGpgMEDecryptVerifyJob::QGpgMEDecryptVerifyJob): Instantiate private job class. (decrypt_verify_from_filename): New. * lang/qt/tests/Makefile.am: Add new test program. * lang/qt/tests/run-decryptverifyjob.cpp: New. -- This makes it possible to tell gpg to read the input and write the output directly to a specified file bypassing GpgME's Data IO when decrypting (and verifying) a file. GnuPG-bug-id: 6550 --- lang/qt/src/qgpgmedecryptverifyjob.cpp | 91 +++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 6 deletions(-) (limited to 'lang/qt/src/qgpgmedecryptverifyjob.cpp') diff --git a/lang/qt/src/qgpgmedecryptverifyjob.cpp b/lang/qt/src/qgpgmedecryptverifyjob.cpp index 256160b5..bc135147 100644 --- a/lang/qt/src/qgpgmedecryptverifyjob.cpp +++ b/lang/qt/src/qgpgmedecryptverifyjob.cpp @@ -39,25 +39,55 @@ #include "qgpgmedecryptverifyjob.h" #include "dataprovider.h" +#include "decryptverifyjob_p.h" +#include "util.h" -#include "context.h" -#include "decryptionresult.h" -#include "verificationresult.h" -#include "data.h" +#include +#include +#include +#include #include #include "qgpgme_debug.h" #include +#include #include using namespace QGpgME; using namespace GpgME; +namespace +{ + +class QGpgMEDecryptVerifyJobPrivate : public DecryptVerifyJobPrivate +{ + QGpgMEDecryptVerifyJob *q = nullptr; + +public: + QGpgMEDecryptVerifyJobPrivate(QGpgMEDecryptVerifyJob *qq) + : q{qq} + { + } + + ~QGpgMEDecryptVerifyJobPrivate() override = default; + +private: + GpgME::Error startIt() override; + + void startNow() override + { + q->run(); + } +}; + +} + QGpgMEDecryptVerifyJob::QGpgMEDecryptVerifyJob(Context *context) : mixin_type(context) { + setJobPrivate(this, std::unique_ptr{new QGpgMEDecryptVerifyJobPrivate{this}}); lateInitialization(); } @@ -112,6 +142,43 @@ static QGpgMEDecryptVerifyJob::result_type decrypt_verify_qba(Context *ctx, cons return decrypt_verify(ctx, nullptr, buffer, std::shared_ptr()); } +static QGpgMEDecryptVerifyJob::result_type decrypt_verify_from_filename(Context *ctx, + const QString &inputFilePath, + const QString &outputFilePath) +{ + Data indata; +#ifdef Q_OS_WIN + indata.setFileName(inputFilePath().toUtf8().constData()); +#else + indata.setFileName(QFile::encodeName(inputFilePath).constData()); +#endif + + PartialFileGuard partFileGuard{outputFilePath}; + if (partFileGuard.tempFileName().isEmpty()) { + return std::make_tuple(DecryptionResult{Error::fromCode(GPG_ERR_EEXIST)}, VerificationResult{Error::fromCode(GPG_ERR_EEXIST)}, QByteArray{}, QString{}, Error{}); + } + + Data outdata; +#ifdef Q_OS_WIN + outdata.setFileName(partFileGuard.tempFileName().toUtf8().constData()); +#else + outdata.setFileName(QFile::encodeName(partFileGuard.tempFileName()).constData()); +#endif + + const auto results = ctx->decryptAndVerify(indata, outdata); + const auto &decryptionResult = results.first; + const auto &verificationResult = results.second; + + if (!decryptionResult.error().code() && !verificationResult.error().code()) { + // the operation succeeded -> save the result under the requested file name + partFileGuard.commit(); + } + + Error ae; + const QString log = _detail::audit_log_as_html(ctx, ae); + return std::make_tuple(decryptionResult, verificationResult, QByteArray{}, log, ae); +} + Error QGpgMEDecryptVerifyJob::start(const QByteArray &cipherText) { run(std::bind(&decrypt_verify_qba, std::placeholders::_1, cipherText)); @@ -132,10 +199,22 @@ QGpgME::QGpgMEDecryptVerifyJob::exec(const QByteArray &cipherText, QByteArray &p return mResult; } -//PENDING(marc) implement showErrorDialog() - void QGpgMEDecryptVerifyJob::resultHook(const result_type &tuple) { mResult = std::make_pair(std::get<0>(tuple), std::get<1>(tuple)); } + +GpgME::Error QGpgMEDecryptVerifyJobPrivate::startIt() +{ + if (m_inputFilePath.isEmpty() || m_outputFilePath.isEmpty()) { + return Error::fromCode(GPG_ERR_INV_VALUE); + } + + q->run([=](Context *ctx) { + return decrypt_verify_from_filename(ctx, m_inputFilePath, m_outputFilePath); + }); + + return {}; +} + #include "qgpgmedecryptverifyjob.moc" -- cgit