1 /*
2   SPDX-FileCopyrightText: 2009 Klaralvdalens Datakonsult AB, a KDAB Group company, info@kdab.net
3   SPDX-FileCopyrightText: 2009 Leo Franchi <lfranchi@kde.org>
4 
5   SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "job/signencryptjob.h"
9 
10 #include "contentjobbase_p.h"
11 #include "job/protectedheadersjob.h"
12 #include "utils/util_p.h"
13 
14 #include <QGpgME/Protocol>
15 #include <QGpgME/SignEncryptJob>
16 
17 #include "messagecomposer_debug.h"
18 #include <KMime/Content>
19 #include <KMime/Headers>
20 #include <KMime/KMimeMessage>
21 
22 #include <gpgme++/encryptionresult.h>
23 #include <gpgme++/global.h>
24 #include <gpgme++/signingresult.h>
25 #include <sstream>
26 
27 using namespace MessageComposer;
28 
29 class MessageComposer::SignEncryptJobPrivate : public ContentJobBasePrivate
30 {
31 public:
SignEncryptJobPrivate(SignEncryptJob * qq)32     SignEncryptJobPrivate(SignEncryptJob *qq)
33         : ContentJobBasePrivate(qq)
34     {
35     }
36 
37     std::vector<GpgME::Key> signers;
38 
39     std::vector<GpgME::Key> encKeys;
40     QStringList recipients;
41     Kleo::CryptoMessageFormat format;
42     KMime::Content *content = nullptr;
43     KMime::Message *skeletonMessage = nullptr;
44 
45     bool protectedHeaders = true;
46     bool protectedHeadersObvoscate = false;
47 
48     // copied from messagecomposer.cpp
binaryHint(Kleo::CryptoMessageFormat f)49     bool binaryHint(Kleo::CryptoMessageFormat f)
50     {
51         switch (f) {
52         case Kleo::SMIMEFormat:
53         case Kleo::SMIMEOpaqueFormat:
54             return true;
55         default:
56         case Kleo::OpenPGPMIMEFormat:
57         case Kleo::InlineOpenPGPFormat:
58             return false;
59         }
60     }
61 
62     Q_DECLARE_PUBLIC(SignEncryptJob)
63 };
64 
SignEncryptJob(QObject * parent)65 SignEncryptJob::SignEncryptJob(QObject *parent)
66     : ContentJobBase(*new SignEncryptJobPrivate(this), parent)
67 {
68 }
69 
~SignEncryptJob()70 SignEncryptJob::~SignEncryptJob()
71 {
72 }
73 
setContent(KMime::Content * content)74 void SignEncryptJob::setContent(KMime::Content *content)
75 {
76     Q_D(SignEncryptJob);
77 
78     Q_ASSERT(content);
79 
80     d->content = content;
81 }
82 
setCryptoMessageFormat(Kleo::CryptoMessageFormat format)83 void SignEncryptJob::setCryptoMessageFormat(Kleo::CryptoMessageFormat format)
84 {
85     Q_D(SignEncryptJob);
86 
87     // There *must* be a concrete format set at this point.
88     Q_ASSERT(format == Kleo::OpenPGPMIMEFormat || format == Kleo::InlineOpenPGPFormat || format == Kleo::SMIMEFormat || format == Kleo::SMIMEOpaqueFormat);
89     d->format = format;
90 }
91 
setSigningKeys(const std::vector<GpgME::Key> & signers)92 void SignEncryptJob::setSigningKeys(const std::vector<GpgME::Key> &signers)
93 {
94     Q_D(SignEncryptJob);
95 
96     d->signers = signers;
97 }
98 
origContent()99 KMime::Content *SignEncryptJob::origContent()
100 {
101     Q_D(SignEncryptJob);
102 
103     return d->content;
104 }
105 
setEncryptionKeys(const std::vector<GpgME::Key> & keys)106 void SignEncryptJob::setEncryptionKeys(const std::vector<GpgME::Key> &keys)
107 {
108     Q_D(SignEncryptJob);
109 
110     d->encKeys = keys;
111 }
112 
setRecipients(const QStringList & recipients)113 void SignEncryptJob::setRecipients(const QStringList &recipients)
114 {
115     Q_D(SignEncryptJob);
116 
117     d->recipients = recipients;
118 }
119 
setSkeletonMessage(KMime::Message * skeletonMessage)120 void SignEncryptJob::setSkeletonMessage(KMime::Message *skeletonMessage)
121 {
122     Q_D(SignEncryptJob);
123 
124     d->skeletonMessage = skeletonMessage;
125 }
126 
setProtectedHeaders(bool protectedHeaders)127 void SignEncryptJob::setProtectedHeaders(bool protectedHeaders)
128 {
129     Q_D(SignEncryptJob);
130 
131     d->protectedHeaders = protectedHeaders;
132 }
133 
setProtectedHeadersObvoscate(bool protectedHeadersObvoscate)134 void SignEncryptJob::setProtectedHeadersObvoscate(bool protectedHeadersObvoscate)
135 {
136     Q_D(SignEncryptJob);
137 
138     d->protectedHeadersObvoscate = protectedHeadersObvoscate;
139 }
140 
recipients() const141 QStringList SignEncryptJob::recipients() const
142 {
143     Q_D(const SignEncryptJob);
144 
145     return d->recipients;
146 }
147 
encryptionKeys() const148 std::vector<GpgME::Key> SignEncryptJob::encryptionKeys() const
149 {
150     Q_D(const SignEncryptJob);
151 
152     return d->encKeys;
153 }
154 
doStart()155 void SignEncryptJob::doStart()
156 {
157     Q_D(SignEncryptJob);
158     Q_ASSERT(d->resultContent == nullptr); // Not processed before.
159 
160     if (d->protectedHeaders && d->skeletonMessage && d->format & Kleo::OpenPGPMIMEFormat) {
161         auto pJob = new ProtectedHeadersJob;
162         pJob->setContent(d->content);
163         pJob->setSkeletonMessage(d->skeletonMessage);
164         pJob->setObvoscate(d->protectedHeadersObvoscate);
165         QObject::connect(pJob, &ProtectedHeadersJob::finished, this, [d, pJob](KJob *job) {
166             if (job->error()) {
167                 return;
168             }
169             d->content = pJob->content();
170         });
171         appendSubjob(pJob);
172     }
173 
174     ContentJobBase::doStart();
175 }
176 
slotResult(KJob * job)177 void SignEncryptJob::slotResult(KJob *job)
178 {
179     // Q_D(SignEncryptJob);
180     if (error() || job->error()) {
181         ContentJobBase::slotResult(job);
182         return;
183     }
184     if (subjobs().size() == 2) {
185         auto pjob = static_cast<ProtectedHeadersJob *>(subjobs().last());
186         if (pjob) {
187             auto cjob = qobject_cast<ContentJobBase *>(job);
188             Q_ASSERT(cjob);
189             pjob->setContent(cjob->content());
190         }
191     }
192 
193     ContentJobBase::slotResult(job);
194 }
195 
process()196 void SignEncryptJob::process()
197 {
198     Q_D(SignEncryptJob);
199     Q_ASSERT(d->resultContent == nullptr); // Not processed before.
200 
201     // if setContent hasn't been called, we assume that a subjob was added
202     // and we want to use that
203     if (!d->content || !d->content->hasContent()) {
204         Q_ASSERT(d->subjobContents.size() == 1);
205         d->content = d->subjobContents.constFirst();
206     }
207 
208     const QGpgME::Protocol *proto = nullptr;
209     if (d->format & Kleo::AnyOpenPGP) {
210         proto = QGpgME::openpgp();
211     } else if (d->format & Kleo::AnySMIME) {
212         proto = QGpgME::smime();
213     } else {
214         return;
215     }
216     Q_ASSERT(proto);
217     // d->resultContent = new KMime::Content;
218 
219     qCDebug(MESSAGECOMPOSER_LOG) << "creating signencrypt from:" << proto->name() << proto->displayName();
220 
221     QByteArray encBody;
222     d->content->assemble();
223 
224     // replace simple LFs by CRLFs for all MIME supporting CryptPlugs
225     // according to RfC 2633, 3.1.1 Canonicalization
226     QByteArray content;
227     if (d->format & Kleo::InlineOpenPGPFormat) {
228         content = d->content->body();
229     } else if (!(d->format & Kleo::SMIMEOpaqueFormat)) {
230         content = KMime::LFtoCRLF(d->content->encodedContent());
231     } else { // SMimeOpaque doesn't need LFtoCRLF, else it gets munged
232         content = d->content->encodedContent();
233     }
234 
235     QGpgME::SignEncryptJob *job(proto->signEncryptJob(!d->binaryHint(d->format), d->format == Kleo::InlineOpenPGPFormat));
236     QObject::connect(job,
237                      &QGpgME::SignEncryptJob::result,
238                      this,
239                      [this, d](const GpgME::SigningResult &signingResult,
240                                const GpgME::EncryptionResult &encryptionResult,
241                                const QByteArray &cipherText,
242                                const QString &auditLogAsHtml,
243                                const GpgME::Error &auditLogError) {
244                          Q_UNUSED(auditLogAsHtml)
245                          Q_UNUSED(auditLogError)
246                          if (signingResult.error()) {
247                              qCDebug(MESSAGECOMPOSER_LOG) << "signing failed:" << signingResult.error().asString();
248                              setError(signingResult.error().code());
249                              setErrorText(QString::fromLocal8Bit(signingResult.error().asString()));
250                              emitResult();
251                              return;
252                          }
253                          if (encryptionResult.error()) {
254                              qCDebug(MESSAGECOMPOSER_LOG) << "encrypting failed:" << encryptionResult.error().asString();
255                              setError(encryptionResult.error().code());
256                              setErrorText(QString::fromLocal8Bit(encryptionResult.error().asString()));
257                              emitResult();
258                              return;
259                          }
260 
261                          QByteArray signatureHashAlgo = signingResult.createdSignature(0).hashAlgorithmAsString();
262                          d->resultContent = MessageComposer::Util::composeHeadersAndBody(d->content, cipherText, d->format, false, signatureHashAlgo);
263 
264                          emitResult();
265                      });
266 
267     const auto error = job->start(d->signers, d->encKeys, content, false);
268     if (error.code()) {
269         job->deleteLater();
270         setError(error.code());
271         setErrorText(QString::fromLocal8Bit(error.asString()));
272         emitResult();
273     }
274 }
275