summaryrefslogtreecommitdiffstats
path: root/framework/src
diff options
context:
space:
mode:
Diffstat (limited to 'framework/src')
-rw-r--r--framework/src/domain/mime/mailcrypto.cpp121
-rw-r--r--framework/src/domain/mime/mailcrypto.h3
2 files changed, 68 insertions, 56 deletions
diff --git a/framework/src/domain/mime/mailcrypto.cpp b/framework/src/domain/mime/mailcrypto.cpp
index 19e81b4f..bdfa2a74 100644
--- a/framework/src/domain/mime/mailcrypto.cpp
+++ b/framework/src/domain/mime/mailcrypto.cpp
@@ -22,9 +22,12 @@
22#include "mailcrypto.h" 22#include "mailcrypto.h"
23#include <QGpgME/Protocol> 23#include <QGpgME/Protocol>
24#include <QGpgME/SignJob> 24#include <QGpgME/SignJob>
25#include <QGpgME/EncryptJob>
26#include <QGpgME/SignEncryptJob>
25#include <QGpgME/KeyListJob> 27#include <QGpgME/KeyListJob>
26#include <gpgme++/global.h> 28#include <gpgme++/global.h>
27#include <gpgme++/signingresult.h> 29#include <gpgme++/signingresult.h>
30#include <gpgme++/encryptionresult.h>
28#include <gpgme++/keylistresult.h> 31#include <gpgme++/keylistresult.h>
29#include <QDebug> 32#include <QDebug>
30 33
@@ -335,44 +338,12 @@ KMime::Content *composeHeadersAndBody(KMime::Content *orig, QByteArray encodedBo
335 // } 338 // }
336 // } 339 // }
337 340
338//Hardcoded OpenPGPGMIMEFormat for now 341// replace simple LFs by CRLFs for all MIME supporting CryptPlugs
339KMime::Content *MailCrypto::sign(KMime::Content *content, const std::vector<GpgME::Key> &signers) 342// according to RfC 2633, 3.1.1 Canonicalization
343static QByteArray canonicalizeContent(KMime::Content *content)
340{ 344{
341
342 // if setContent hasn't been called, we assume that a subjob was added
343 // and we want to use that
344 // if (!d->content) {
345 // Q_ASSERT(d->subjobContents.size() == 1);
346 // d->content = d->subjobContents.first();
347 // }
348
349 //d->resultContent = new KMime::Content;
350
351 // const QGpgME::Protocol *proto = nullptr;
352 // if (d->format & Kleo::AnyOpenPGP) {
353 // proto = QGpgME::openpgp();
354 // } else if (d->format & Kleo::AnySMIME) {
355 // proto = QGpgME::smime();
356 // }
357
358 const QGpgME::Protocol *proto = QGpgME::openpgp();
359 Q_ASSERT(proto);
360
361 qDebug() << "creating signJob from:" << proto->name() << proto->displayName();
362 // std::unique_ptr<QGpgME::SignJob> job(proto->signJob(!d->binaryHint(d->format), d->format == Kleo::InlineOpenPGPFormat));
363 bool armor = true;
364 bool textMode = false;
365 std::unique_ptr<QGpgME::SignJob> job(proto->signJob(armor, textMode));
366 // for now just do the main recipients
367 QByteArray signature;
368
369 content->assemble();
370
371 // replace simple LFs by CRLFs for all MIME supporting CryptPlugs
372 // according to RfC 2633, 3.1.1 Canonicalization
373 QByteArray contentData;
374 // if (d->format & Kleo::InlineOpenPGPFormat) { 345 // if (d->format & Kleo::InlineOpenPGPFormat) {
375 // content = d->content->body(); 346 // return d->content->body();
376 // } else if (!(d->format & Kleo::SMIMEOpaqueFormat)) { 347 // } else if (!(d->format & Kleo::SMIMEOpaqueFormat)) {
377 348
378 // replace "From " and "--" at the beginning of lines 349 // replace "From " and "--" at the beginning of lines
@@ -425,33 +396,73 @@ KMime::Content *MailCrypto::sign(KMime::Content *content, const std::vector<GpgM
425 } 396 }
426 } 397 }
427 398
428 contentData = KMime::LFtoCRLF(content->encodedContent()); 399 return KMime::LFtoCRLF(content->encodedContent());
429 // } else { // SMimeOpaque doesn't need LFtoCRLF, else it gets munged 400 // } else { // SMimeOpaque doesn't need LFtoCRLF, else it gets munged
430 // contentData = content->encodedContent(); 401 // return content->encodedContent();
431 // } 402 // }
432 403
433 auto signingMode = GpgME::Detached; 404}
434 405
435 // FIXME: Make this async 406KMime::Content *MailCrypto::processCrypto(KMime::Content *content, const std::vector<GpgME::Key> &signingKeys, const std::vector<GpgME::Key> &encryptionKeys, MailCrypto::Protocol protocol)
436 GpgME::SigningResult res = job->exec(signers, 407{
437 contentData, 408 const QGpgME::Protocol *const proto = protocol == MailCrypto::SMIME ? QGpgME::smime() : QGpgME::openpgp();
438 signingMode, 409 Q_ASSERT(proto);
439 signature);
440 410
441 // exec'ed jobs don't delete themselves 411 qDebug() << "creating signJob from:" << proto->name() << proto->displayName();
442 job->deleteLater(); 412 // for now just do the main recipients
413
414 content->assemble();
443 415
444 if (res.error().code()) { 416 auto signingMode = GpgME::Detached;
445 qWarning() << "signing failed:" << res.error().asString(); 417 bool armor = true;
446 // job->showErrorDialog( globalPart()->parentWidgetForGui() ); 418 bool textMode = false;
447 // setError(res.error().code()); 419 const bool sign = !signingKeys.empty();
448 // setErrorText(QString::fromLocal8Bit(res.error().asString())); 420 const bool encrypt = !encryptionKeys.empty();
421
422 QByteArray resultContent;
423 QByteArray hashAlgo;
424 //Trust provided keys and don't check them for validity
425 bool alwaysTrust = true;
426 if (sign && encrypt) {
427 std::unique_ptr<QGpgME::SignEncryptJob> job(proto->signEncryptJob(armor, textMode));
428 const auto res = job->exec(signingKeys, encryptionKeys, canonicalizeContent(content), alwaysTrust, resultContent);
429 if (res.first.error().code()) {
430 qWarning() << "Signing failed:" << res.first.error().asString();
431 return nullptr;
432 } else {
433 hashAlgo = res.first.createdSignature(0).hashAlgorithmAsString();
434 }
435 if (res.second.error().code()) {
436 qWarning() << "Encryption failed:" << res.second.error().asString();
437 return nullptr;
438 }
439 } else if (sign) {
440 std::unique_ptr<QGpgME::SignJob> job(proto->signJob(armor, textMode));
441 auto result = job->exec(signingKeys, canonicalizeContent(content), signingMode, resultContent);
442 if (result.error().code()) {
443 qWarning() << "Signing failed:" << result.error().asString();
444 return nullptr;
445 }
446 hashAlgo = result.createdSignature(0).hashAlgorithmAsString();
447 } else if (encrypt) {
448 std::unique_ptr<QGpgME::EncryptJob> job(proto->encryptJob(armor, textMode));
449 const auto result = job->exec(encryptionKeys, canonicalizeContent(content), alwaysTrust, resultContent);
450 if (result.error().code()) {
451 qWarning() << "Encryption failed:" << result.error().asString();
452 return nullptr;
453 }
454 hashAlgo = "pgp-sha1";
449 } else { 455 } else {
450 QByteArray signatureHashAlgo = res.createdSignature(0).hashAlgorithmAsString(); 456 qWarning() << "Not signing or encrypting";
451 bool sign = true; 457 return nullptr;
452 return composeHeadersAndBody(content, signature, sign, signatureHashAlgo);
453 } 458 }
454 return nullptr; 459
460 return composeHeadersAndBody(content, resultContent, sign, hashAlgo);
461}
462
463KMime::Content *MailCrypto::sign(KMime::Content *content, const std::vector<GpgME::Key> &signers)
464{
465 return processCrypto(content, signers, {}, OPENPGP);
455} 466}
456 467
457std::vector<GpgME::Key> MailCrypto::findKeys(const QStringList &filter, bool findPrivate, Protocol protocol) 468std::vector<GpgME::Key> MailCrypto::findKeys(const QStringList &filter, bool findPrivate, Protocol protocol)
diff --git a/framework/src/domain/mime/mailcrypto.h b/framework/src/domain/mime/mailcrypto.h
index badf1005..ed362ddc 100644
--- a/framework/src/domain/mime/mailcrypto.h
+++ b/framework/src/domain/mime/mailcrypto.h
@@ -30,6 +30,7 @@ namespace MailCrypto
30 OPENPGP, 30 OPENPGP,
31 SMIME 31 SMIME
32 }; 32 };
33 KMime::Content *processCrypto(KMime::Content *content, const std::vector<GpgME::Key> &signingKeys, const std::vector<GpgME::Key> &encryptionKeys, MailCrypto::Protocol protocol);
33 KMime::Content *sign(KMime::Content *content, const std::vector<GpgME::Key> &signers); 34 KMime::Content *sign(KMime::Content *content, const std::vector<GpgME::Key> &signers);
34 std::vector<GpgME::Key> findKeys(const QStringList &filter, bool findPrivate = false, Protocol protocol = OPENPGP); 35 std::vector<GpgME::Key> findKeys(const QStringList &filter, bool findPrivate = false, Protocol protocol = OPENPGP);
35}; 36};