File indexing completed on 2024-06-23 05:19:20

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "qgpgmejobexecutor.h"
0008 #include "mimetreeparser_debug.h"
0009 
0010 #include <QGpgME/DecryptVerifyJob>
0011 #include <QGpgME/ImportJob>
0012 #include <QGpgME/VerifyDetachedJob>
0013 #include <QGpgME/VerifyOpaqueJob>
0014 
0015 #include <QEventLoop>
0016 
0017 #include <cassert>
0018 
0019 using namespace GpgME;
0020 using namespace MimeTreeParser;
0021 
0022 QGpgMEJobExecutor::QGpgMEJobExecutor(QObject *parent)
0023     : QObject(parent)
0024     , mEventLoop(new QEventLoop(this))
0025 {
0026     setObjectName(QLatin1StringView("KleoJobExecutor"));
0027 }
0028 
0029 GpgME::VerificationResult QGpgMEJobExecutor::exec(QGpgME::VerifyDetachedJob *job, const QByteArray &signature, const QByteArray &signedData)
0030 {
0031     qCDebug(MIMETREEPARSER_LOG) << "Starting detached verification job";
0032     connect(job, &QGpgME::VerifyDetachedJob::result, this, qOverload<const GpgME::VerificationResult &>(&QGpgMEJobExecutor::verificationResult));
0033     GpgME::Error err = job->start(signature, signedData);
0034     if (err) {
0035         return VerificationResult(err);
0036     }
0037     mEventLoop->exec(QEventLoop::ExcludeUserInputEvents);
0038     return mVerificationResult;
0039 }
0040 
0041 GpgME::VerificationResult QGpgMEJobExecutor::exec(QGpgME::VerifyOpaqueJob *job, const QByteArray &signedData, QByteArray &plainText)
0042 {
0043     qCDebug(MIMETREEPARSER_LOG) << "Starting opaque verification job";
0044     connect(job,
0045             &QGpgME::VerifyOpaqueJob::result,
0046             this,
0047             qOverload<const GpgME::VerificationResult &, const QByteArray &>(&QGpgMEJobExecutor::verificationResult));
0048     GpgME::Error err = job->start(signedData);
0049     if (err) {
0050         plainText.clear();
0051         return VerificationResult(err);
0052     }
0053     mEventLoop->exec(QEventLoop::ExcludeUserInputEvents);
0054     plainText = mData;
0055     return mVerificationResult;
0056 }
0057 
0058 std::pair<GpgME::DecryptionResult, GpgME::VerificationResult>
0059 QGpgMEJobExecutor::exec(QGpgME::DecryptVerifyJob *job, const QByteArray &cipherText, QByteArray &plainText)
0060 {
0061     qCDebug(MIMETREEPARSER_LOG) << "Starting decryption job";
0062     connect(job, &QGpgME::DecryptVerifyJob::result, this, &QGpgMEJobExecutor::decryptResult);
0063     GpgME::Error err = job->start(cipherText);
0064     if (err) {
0065         plainText.clear();
0066         return std::make_pair(DecryptionResult(err), VerificationResult(err));
0067     }
0068     mEventLoop->exec(QEventLoop::ExcludeUserInputEvents);
0069     plainText = mData;
0070     return std::make_pair(mDecryptResult, mVerificationResult);
0071 }
0072 
0073 GpgME::ImportResult QGpgMEJobExecutor::exec(QGpgME::ImportJob *job, const QByteArray &certData)
0074 {
0075     connect(job, &QGpgME::AbstractImportJob::result, this, &QGpgMEJobExecutor::importResult);
0076     GpgME::Error err = job->start(certData);
0077     if (err) {
0078         return ImportResult(err);
0079     }
0080     mEventLoop->exec(QEventLoop::ExcludeUserInputEvents);
0081     return mImportResult;
0082 }
0083 
0084 Error QGpgMEJobExecutor::auditLogError() const
0085 {
0086     return mAuditLogError;
0087 }
0088 
0089 void QGpgMEJobExecutor::verificationResult(const GpgME::VerificationResult &result)
0090 {
0091     qCDebug(MIMETREEPARSER_LOG) << "Detached verification job finished";
0092     auto job = qobject_cast<QGpgME::Job *>(sender());
0093     assert(job);
0094     mVerificationResult = result;
0095     mAuditLogError = job->auditLogError();
0096     mAuditLog = job->auditLogAsHtml();
0097     mEventLoop->quit();
0098 }
0099 
0100 void QGpgMEJobExecutor::verificationResult(const GpgME::VerificationResult &result, const QByteArray &plainText)
0101 {
0102     qCDebug(MIMETREEPARSER_LOG) << "Opaque verification job finished";
0103     auto job = qobject_cast<QGpgME::Job *>(sender());
0104     assert(job);
0105     mVerificationResult = result;
0106     mData = plainText;
0107     mAuditLogError = job->auditLogError();
0108     mAuditLog = job->auditLogAsHtml();
0109     mEventLoop->quit();
0110 }
0111 
0112 void QGpgMEJobExecutor::decryptResult(const GpgME::DecryptionResult &decryptionresult,
0113                                       const GpgME::VerificationResult &verificationresult,
0114                                       const QByteArray &plainText)
0115 {
0116     qCDebug(MIMETREEPARSER_LOG) << "Decryption job finished";
0117     auto job = qobject_cast<QGpgME::Job *>(sender());
0118     assert(job);
0119     mVerificationResult = verificationresult;
0120     mDecryptResult = decryptionresult;
0121     mData = plainText;
0122     mAuditLogError = job->auditLogError();
0123     mAuditLog = job->auditLogAsHtml();
0124     mEventLoop->quit();
0125 }
0126 
0127 void QGpgMEJobExecutor::importResult(const GpgME::ImportResult &result)
0128 {
0129     auto job = qobject_cast<QGpgME::Job *>(sender());
0130     assert(job);
0131     mImportResult = result;
0132     mAuditLogError = job->auditLogError();
0133     mAuditLog = job->auditLogAsHtml();
0134     mEventLoop->quit();
0135 }
0136 
0137 QString QGpgMEJobExecutor::auditLogAsHtml() const
0138 {
0139     return mAuditLog;
0140 }
0141 
0142 #include "moc_qgpgmejobexecutor.cpp"