Commit c20f5956 authored by Sandro Knauß's avatar Sandro Knauß
Browse files

[messagecomposer]: Implement protected headers for SignJob. T742

Test Plan: run autotests successfully

Subscribers: kde-pim

Tags: #kde_pim

Differential Revision: https://phabricator.kde.org/D28449
parent f0efce42
......@@ -41,6 +41,8 @@
QTEST_MAIN(SignJobTest)
using namespace MessageComposer;
void SignJobTest::initTestCase()
{
MessageComposer::Test::setupEnv();
......@@ -217,3 +219,63 @@ void SignJobTest::checkSignJob(MessageComposer::SignJob *sJob)
ComposerTestUtil::verifySignature(result, QString::fromLocal8Bit("one flew over the cuckoo's nest").toUtf8(), Kleo::OpenPGPMIMEFormat, KMime::Headers::CE7Bit);
}
void SignJobTest::testProtectedHeaders_data()
{
QTest::addColumn<bool>("protectedHeaders");
QTest::addColumn<QString>("referenceFile");
QTest::newRow("simple-non-obvoscate") << true << QStringLiteral("protected_headers-non-obvoscate.mbox");
QTest::newRow("non-protected_headers") << false << QStringLiteral("non-protected_headers.mbox");
}
void SignJobTest::testProtectedHeaders()
{
QFETCH(bool,protectedHeaders);
QFETCH(QString, referenceFile);
std::vector< GpgME::Key > keys = Test::getKeys();
Composer composer;
auto sJob = new SignJob(&composer);
QVERIFY(sJob);
const QByteArray data(QString::fromLocal8Bit("one flew over the cuckoo's nest").toUtf8());
const QString subject(QStringLiteral("asdfghjklö"));
auto content = new KMime::Content;
content->contentType(true)->setMimeType("text/plain");
content->setBody(data);
KMime::Message skeletonMessage;
skeletonMessage.contentType(true)->setMimeType("foo/bla");
skeletonMessage.to(true)->from7BitString("to@test.de, to2@test.de");
skeletonMessage.cc(true)->from7BitString("cc@test.de, cc2@test.de");
skeletonMessage.bcc(true)->from7BitString("bcc@test.de, bcc2@test.de");
skeletonMessage.subject(true)->fromUnicodeString(subject, "utf-8");
sJob->setContent(content);
sJob->setCryptoMessageFormat(Kleo::OpenPGPMIMEFormat);
sJob->setSigningKeys(keys);
sJob->setSkeletonMessage(&skeletonMessage);
sJob->setProtectedHeaders(protectedHeaders);
VERIFYEXEC(sJob);
QCOMPARE(skeletonMessage.subject()->asUnicodeString(), subject);
KMime::Content *result = sJob->content();
result->assemble();
QFile f(referenceFile);
QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
const QByteArray signedContent(result->contents().at(0)->encodedContent());
f.write(signedContent);
if (!signedContent.endsWith('\n')) {
f.write("\n");
}
f.close();
Test::compareFile(referenceFile, QStringLiteral(MAIL_DATA_DIR "/")+referenceFile);
}
......@@ -44,6 +44,8 @@ private Q_SLOTS:
void testRecommentationRFC3156();
void testMixedContent();
void testProtectedHeaders_data();
void testProtectedHeaders();
private:
void checkSignJob(MessageComposer::SignJob *sJob);
};
......
......@@ -220,6 +220,9 @@ void ComposerPrivate::composeStep2()
sJob->setCryptoMessageFormat(format);
sJob->setSigningKeys(signers);
sJob->appendSubjob(mainJob);
if (!encrypt) {
sJob->setSkeletonMessage(skeletonMessage);
}
mainJob = sJob;
}
......
......@@ -21,6 +21,7 @@
#include "job/signjob.h"
#include "contentjobbase_p.h"
#include "job/protectedheaders.h"
#include "utils/util_p.h"
#include <Libkleo/Enum>
......@@ -49,9 +50,12 @@ public:
}
KMime::Content *content = nullptr;
KMime::Message *skeletonMessage = nullptr;
std::vector<GpgME::Key> signers;
Kleo::CryptoMessageFormat format;
bool protectedHeaders = true;
// copied from messagecomposer.cpp
bool binaryHint(Kleo::CryptoMessageFormat f)
{
......@@ -118,6 +122,20 @@ void SignJob::setSigningKeys(std::vector<GpgME::Key> &signers)
d->signers = signers;
}
void SignJob::setSkeletonMessage(KMime::Message *skeletonMessage)
{
Q_D(SignJob);
d->skeletonMessage = skeletonMessage;
}
void SignJob::setProtectedHeaders(bool protectedHeaders)
{
Q_D(SignJob);
d->protectedHeaders = protectedHeaders;
}
KMime::Content *SignJob::origContent()
{
Q_D(SignJob);
......@@ -125,6 +143,47 @@ KMime::Content *SignJob::origContent()
return d->content;
}
void SignJob::doStart()
{
Q_D(SignJob);
Q_ASSERT(d->resultContent == nullptr); // Not processed before.
if (d->protectedHeaders && d->skeletonMessage && d->format & Kleo::OpenPGPMIMEFormat) {
ProtectedHeadersJob *pJob = new ProtectedHeadersJob;
pJob->setContent(d->content);
pJob->setSkeletonMessage(d->skeletonMessage);
pJob->setObvoscate(false);
QObject::connect(pJob, &ProtectedHeadersJob::finished, this, [d, pJob](KJob *job) {
if (job->error()) {
return;
}
d->content = pJob->content();
});
appendSubjob(pJob);
}
ContentJobBase::doStart();
}
void SignJob::slotResult(KJob *job)
{
Q_D(SignJob);
if (error()) {
ContentJobBase::slotResult(job);
return;
}
if (subjobs().size() == 2) {
auto pjob = static_cast<ProtectedHeadersJob *>(subjobs().last());
if (pjob) {
auto cjob = dynamic_cast<ContentJobBase *>(job);
Q_ASSERT(cjob);
pjob->setContent(cjob->content());
}
}
ContentJobBase::slotResult(job);
}
void SignJob::process()
{
Q_D(SignJob);
......
......@@ -52,9 +52,14 @@ public:
void setCryptoMessageFormat(Kleo::CryptoMessageFormat format);
void setSigningKeys(std::vector<GpgME::Key> &signers);
void setSkeletonMessage(KMime::Message *skeletonMessage);
void setProtectedHeaders(bool protectedHeaders);
Q_REQUIRED_RESULT KMime::Content *origContent();
protected Q_SLOTS:
void doStart() override;
void slotResult(KJob *job) override;
void process() override;
private:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment