File indexing completed on 2024-06-16 04:55:57

0001 /* -*- mode: c++; c-basic-offset:4 -*-
0002     decryptverifytask.h
0003 
0004     This file is part of Kleopatra, the KDE keymanager
0005     SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #pragma once
0011 
0012 #include "task.h"
0013 
0014 #include <utils/types.h>
0015 
0016 #include <gpgme++/verificationresult.h>
0017 
0018 #include <memory>
0019 
0020 namespace KMime
0021 {
0022 namespace Types
0023 {
0024 class Mailbox;
0025 }
0026 }
0027 namespace GpgME
0028 {
0029 class DecryptionResult;
0030 class VerificationResult;
0031 class Key;
0032 class Signature;
0033 }
0034 
0035 namespace QGpgME
0036 {
0037 class Job;
0038 }
0039 
0040 namespace Kleo
0041 {
0042 class Input;
0043 class Output;
0044 class AuditLogEntry;
0045 }
0046 
0047 namespace Kleo
0048 {
0049 namespace Crypto
0050 {
0051 
0052 class DecryptVerifyResult;
0053 
0054 class AbstractDecryptVerifyTask : public Task
0055 {
0056     Q_OBJECT
0057 public:
0058     explicit AbstractDecryptVerifyTask(QObject *parent = nullptr);
0059     ~AbstractDecryptVerifyTask() override;
0060     virtual void autodetectProtocolFromInput() = 0;
0061 
0062     KMime::Types::Mailbox informativeSender() const;
0063     void setInformativeSender(const KMime::Types::Mailbox &senders);
0064 
0065     virtual QString inputLabel() const = 0;
0066     virtual QString outputLabel() const = 0;
0067 
0068 public Q_SLOTS:
0069     void cancel() override;
0070 
0071 protected:
0072     std::shared_ptr<DecryptVerifyResult> fromDecryptResult(const GpgME::DecryptionResult &dr, const QByteArray &plaintext, const AuditLogEntry &auditLog);
0073     std::shared_ptr<DecryptVerifyResult> fromDecryptResult(const GpgME::Error &err, const QString &details, const AuditLogEntry &auditLog);
0074     std::shared_ptr<DecryptVerifyResult> fromDecryptVerifyResult(const GpgME::DecryptionResult &dr,
0075                                                                  const GpgME::VerificationResult &vr,
0076                                                                  const QByteArray &plaintext,
0077                                                                  const QString &fileName,
0078                                                                  const AuditLogEntry &auditLog);
0079     std::shared_ptr<DecryptVerifyResult> fromDecryptVerifyResult(const GpgME::Error &err, const QString &what, const AuditLogEntry &auditLog);
0080     std::shared_ptr<DecryptVerifyResult>
0081     fromVerifyOpaqueResult(const GpgME::VerificationResult &vr, const QByteArray &plaintext, const AuditLogEntry &auditLog);
0082     std::shared_ptr<DecryptVerifyResult> fromVerifyOpaqueResult(const GpgME::Error &err, const QString &details, const AuditLogEntry &auditLog);
0083     std::shared_ptr<DecryptVerifyResult> fromVerifyDetachedResult(const GpgME::VerificationResult &vr, const AuditLogEntry &auditLog);
0084     std::shared_ptr<DecryptVerifyResult> fromVerifyDetachedResult(const GpgME::Error &err, const QString &details, const AuditLogEntry &auditLog);
0085 
0086 public:
0087     // public to allow access from the Private classes of the concrete tasks
0088     QGpgME::Job *job() const;
0089     void setJob(QGpgME::Job *job);
0090 
0091 private:
0092     class Private;
0093     kdtools::pimpl_ptr<Private> d;
0094 };
0095 
0096 class DecryptTask : public AbstractDecryptVerifyTask
0097 {
0098     Q_OBJECT
0099 public:
0100     explicit DecryptTask(QObject *parent = nullptr);
0101     ~DecryptTask() override;
0102 
0103     void setInput(const std::shared_ptr<Input> &input);
0104     void setOutput(const std::shared_ptr<Output> &output);
0105 
0106     void setProtocol(GpgME::Protocol prot);
0107     void autodetectProtocolFromInput() override;
0108 
0109     QString label() const override;
0110 
0111     GpgME::Protocol protocol() const override;
0112 
0113     QString inputLabel() const override;
0114     QString outputLabel() const override;
0115 
0116 private:
0117     void doStart() override;
0118     unsigned long long inputSize() const override;
0119 
0120 private:
0121     class Private;
0122     kdtools::pimpl_ptr<Private> d;
0123     Q_PRIVATE_SLOT(d, void slotResult(GpgME::DecryptionResult, QByteArray))
0124 };
0125 
0126 class VerifyDetachedTask : public AbstractDecryptVerifyTask
0127 {
0128     Q_OBJECT
0129 public:
0130     explicit VerifyDetachedTask(QObject *parent = nullptr);
0131     ~VerifyDetachedTask() override;
0132 
0133     void setInput(const std::shared_ptr<Input> &input);
0134     void setSignedData(const std::shared_ptr<Input> &signedData);
0135 
0136     void setSignatureFile(const QString &path);
0137     void setSignedFile(const QString &path);
0138 
0139     void setProtocol(GpgME::Protocol prot);
0140     void autodetectProtocolFromInput() override;
0141 
0142     QString label() const override;
0143 
0144     GpgME::Protocol protocol() const override;
0145 
0146     QString inputLabel() const override;
0147     QString outputLabel() const override;
0148 
0149 private:
0150     void doStart() override;
0151     unsigned long long inputSize() const override;
0152 
0153 private:
0154     class Private;
0155     kdtools::pimpl_ptr<Private> d;
0156     Q_PRIVATE_SLOT(d, void slotResult(GpgME::VerificationResult))
0157 };
0158 
0159 class VerifyOpaqueTask : public AbstractDecryptVerifyTask
0160 {
0161     Q_OBJECT
0162 public:
0163     explicit VerifyOpaqueTask(QObject *parent = nullptr);
0164     ~VerifyOpaqueTask() override;
0165 
0166     void setInput(const std::shared_ptr<Input> &input);
0167     void setOutput(const std::shared_ptr<Output> &output);
0168 
0169     void setProtocol(GpgME::Protocol prot);
0170     void autodetectProtocolFromInput() override;
0171 
0172     QString label() const override;
0173     GpgME::Protocol protocol() const override;
0174 
0175     void setExtractArchive(bool extract);
0176     void setInputFile(const QString &path);
0177     // for files
0178     void setOutputFile(const QString &path);
0179     // for archives
0180     void setOutputDirectory(const QString &directory);
0181 
0182     QString inputLabel() const override;
0183     QString outputLabel() const override;
0184 
0185 private:
0186     void doStart() override;
0187     unsigned long long inputSize() const override;
0188 
0189 private:
0190     class Private;
0191     kdtools::pimpl_ptr<Private> d;
0192     Q_PRIVATE_SLOT(d, void slotResult(GpgME::VerificationResult, QByteArray))
0193 };
0194 
0195 class DecryptVerifyTask : public AbstractDecryptVerifyTask
0196 {
0197     Q_OBJECT
0198 public:
0199     explicit DecryptVerifyTask(QObject *parent = nullptr);
0200     ~DecryptVerifyTask() override;
0201 
0202     void setInput(const std::shared_ptr<Input> &input);
0203     void setOutput(const std::shared_ptr<Output> &output);
0204 
0205     void setProtocol(GpgME::Protocol prot);
0206     void autodetectProtocolFromInput() override;
0207 
0208     QString label() const override;
0209 
0210     GpgME::Protocol protocol() const override;
0211 
0212     void setIgnoreMDCError(bool value);
0213     void setExtractArchive(bool extract);
0214     void setInputFile(const QString &path);
0215     // for files
0216     void setOutputFile(const QString &path);
0217     // for archives
0218     void setOutputDirectory(const QString &directory);
0219 
0220     QString inputLabel() const override;
0221     QString outputLabel() const override;
0222 
0223 private:
0224     void doStart() override;
0225     unsigned long long inputSize() const override;
0226 
0227 private:
0228     class Private;
0229     kdtools::pimpl_ptr<Private> d;
0230     Q_PRIVATE_SLOT(d, void slotResult(GpgME::DecryptionResult, GpgME::VerificationResult, QByteArray))
0231 };
0232 
0233 class DecryptVerifyResult : public Task::Result
0234 {
0235     friend class ::Kleo::Crypto::AbstractDecryptVerifyTask;
0236 
0237 public:
0238     class SenderInfo;
0239 
0240     QString overview() const override;
0241     QString details() const override;
0242     GpgME::Error error() const override;
0243     QString errorString() const override;
0244     VisualCode code() const override;
0245     AuditLogEntry auditLog() const override;
0246     QPointer<Task> parentTask() const override;
0247     Task::Result::ContentType viewableContentType() const override;
0248 
0249     GpgME::VerificationResult verificationResult() const;
0250     GpgME::DecryptionResult decryptionResult() const;
0251     QString fileName() const;
0252 
0253 private:
0254     DecryptVerifyResult();
0255     DecryptVerifyResult(const DecryptVerifyResult &);
0256     DecryptVerifyResult &operator=(const DecryptVerifyResult &other);
0257 
0258     DecryptVerifyResult(DecryptVerifyOperation op,
0259                         const GpgME::VerificationResult &vr,
0260                         const GpgME::DecryptionResult &dr,
0261                         const QByteArray &stuff,
0262                         const QString &fileName,
0263                         const GpgME::Error &error,
0264                         const QString &errString,
0265                         const QString &inputLabel,
0266                         const QString &outputLabel,
0267                         const AuditLogEntry &auditLog,
0268                         Task *parentTask,
0269                         const KMime::Types::Mailbox &informativeSender);
0270 
0271 private:
0272     class Private;
0273     kdtools::pimpl_ptr<Private> d;
0274 };
0275 }
0276 }