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

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "verifyopaquebodypartmemento.h"
0008 
0009 #include <QGpgME/KeyListJob>
0010 #include <QGpgME/VerifyOpaqueJob>
0011 
0012 #include <gpgme++/keylistresult.h>
0013 
0014 #include <cassert>
0015 
0016 using namespace QGpgME;
0017 using namespace GpgME;
0018 using namespace MimeTreeParser;
0019 
0020 VerifyOpaqueBodyPartMemento::VerifyOpaqueBodyPartMemento(VerifyOpaqueJob *job, KeyListJob *klj, const QByteArray &signature)
0021     : CryptoBodyPartMemento()
0022     , m_signature(signature)
0023     , m_job(job)
0024     , m_keylistjob(klj)
0025 {
0026     assert(m_job);
0027 }
0028 
0029 VerifyOpaqueBodyPartMemento::~VerifyOpaqueBodyPartMemento()
0030 {
0031     if (m_job) {
0032         m_job->slotCancel();
0033     }
0034     if (m_keylistjob) {
0035         m_keylistjob->slotCancel();
0036     }
0037 }
0038 
0039 bool VerifyOpaqueBodyPartMemento::start()
0040 {
0041     assert(m_job);
0042 #ifdef DEBUG_SIGNATURE
0043     qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento started";
0044 #endif
0045     if (const Error err = m_job->start(m_signature)) {
0046         m_vr = VerificationResult(err);
0047 #ifdef DEBUG_SIGNATURE
0048         qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento stopped with error";
0049 #endif
0050         return false;
0051     }
0052     connect(m_job.data(), &VerifyOpaqueJob::result, this, &VerifyOpaqueBodyPartMemento::slotResult);
0053     setRunning(true);
0054     return true;
0055 }
0056 
0057 void VerifyOpaqueBodyPartMemento::exec()
0058 {
0059     assert(m_job);
0060     setRunning(true);
0061     QByteArray plainText;
0062 #ifdef DEBUG_SIGNATURE
0063     qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento execed";
0064 #endif
0065     saveResult(m_job->exec(m_signature, plainText), plainText);
0066 #ifdef DEBUG_SIGNATURE
0067     qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento after execed";
0068 #endif
0069     m_job->deleteLater(); // exec'ed jobs don't delete themselves
0070     m_job = nullptr;
0071     if (canStartKeyListJob()) {
0072         std::vector<GpgME::Key> keys;
0073         m_keylistjob->exec(keyListPattern(), /*secretOnly=*/false, keys);
0074         if (!keys.empty()) {
0075             m_key = keys.back();
0076         }
0077     }
0078     if (m_keylistjob) {
0079         m_keylistjob->deleteLater(); // exec'ed jobs don't delete themselves
0080     }
0081     m_keylistjob = nullptr;
0082     setRunning(false);
0083 }
0084 
0085 bool VerifyOpaqueBodyPartMemento::canStartKeyListJob() const
0086 {
0087     if (!m_keylistjob) {
0088         return false;
0089     }
0090     const char *const fpr = m_vr.signature(0).fingerprint();
0091     return fpr && *fpr;
0092 }
0093 
0094 QStringList VerifyOpaqueBodyPartMemento::keyListPattern() const
0095 {
0096     assert(canStartKeyListJob());
0097     return QStringList(QString::fromLatin1(m_vr.signature(0).fingerprint()));
0098 }
0099 
0100 void VerifyOpaqueBodyPartMemento::saveResult(const VerificationResult &vr, const QByteArray &plainText)
0101 {
0102     assert(m_job);
0103 #ifdef DEBUG_SIGNATURE
0104     qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::saveResult called";
0105 #endif
0106     m_vr = vr;
0107     m_plainText = plainText;
0108     setAuditLog(m_job->auditLogError(), m_job->auditLogAsHtml());
0109 }
0110 
0111 void VerifyOpaqueBodyPartMemento::slotResult(const VerificationResult &vr, const QByteArray &plainText)
0112 {
0113 #ifdef DEBUG_SIGNATURE
0114     qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotResult called";
0115 #endif
0116     saveResult(vr, plainText);
0117     m_job = nullptr;
0118     if (canStartKeyListJob() && startKeyListJob()) {
0119 #ifdef DEBUG_SIGNATURE
0120         qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento: canStartKeyListJob && startKeyListJob";
0121 #endif
0122         return;
0123     }
0124     if (m_keylistjob) {
0125         m_keylistjob->deleteLater();
0126     }
0127     m_keylistjob = nullptr;
0128     setRunning(false);
0129     notify();
0130 }
0131 
0132 bool VerifyOpaqueBodyPartMemento::startKeyListJob()
0133 {
0134     assert(canStartKeyListJob());
0135     if (const GpgME::Error err = m_keylistjob->start(keyListPattern())) {
0136         return false;
0137     }
0138     connect(m_keylistjob.data(), &Job::done, this, &VerifyOpaqueBodyPartMemento::slotKeyListJobDone);
0139     connect(m_keylistjob.data(), &KeyListJob::nextKey, this, &VerifyOpaqueBodyPartMemento::slotNextKey);
0140     return true;
0141 }
0142 
0143 void VerifyOpaqueBodyPartMemento::slotNextKey(const GpgME::Key &key)
0144 {
0145 #ifdef DEBUG_SIGNATURE
0146     qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotNextKey called";
0147 #endif
0148     m_key = key;
0149 }
0150 
0151 void VerifyOpaqueBodyPartMemento::slotKeyListJobDone()
0152 {
0153 #ifdef DEBUG_SIGNATURE
0154     qCDebug(MIMETREEPARSER_LOG) << "tokoe: VerifyOpaqueBodyPartMemento::slotKeyListJobDone called";
0155 #endif
0156     m_keylistjob = nullptr;
0157     setRunning(false);
0158     notify();
0159 }
0160 
0161 #include "moc_verifyopaquebodypartmemento.cpp"