Messagelib

multipartsigned.cpp
1/*
2 SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "multipartsigned.h"
8
9#include "messagepart.h"
10#include "objecttreeparser.h"
11
12#include <KMime/Content>
13
14#include <QGpgME/Protocol>
15
16#include "mimetreeparser_debug.h"
17
18using namespace MimeTreeParser;
19
20const MultiPartSignedBodyPartFormatter *MultiPartSignedBodyPartFormatter::self;
21
22const Interface::BodyPartFormatter *MultiPartSignedBodyPartFormatter::create()
23{
24 if (!self) {
25 self = new MultiPartSignedBodyPartFormatter();
26 }
27 return self;
28}
29
30MessagePart::Ptr MultiPartSignedBodyPartFormatter::process(Interface::BodyPart &part) const
31{
32 KMime::Content *node = part.content();
33 if (node->contents().size() != 2) {
34 qCDebug(MIMETREEPARSER_LOG) << "multipart/signed must have exactly two child parts!" << Qt::endl << "processing as multipart/mixed";
35 if (!node->contents().isEmpty()) {
36 return MessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), node->contents().at(0), false));
37 } else {
38 return {};
39 }
40 }
41
42 KMime::Content *signedData = node->contents().at(0);
43 KMime::Content *signature = node->contents().at(1);
44 Q_ASSERT(signedData);
45 Q_ASSERT(signature);
46
47 QString protocolContentType = node->contentType()->parameter("protocol").toLower();
48 const QString signatureContentType = QLatin1StringView(signature->contentType()->mimeType().toLower());
49 if (protocolContentType.isEmpty()) {
50 qCWarning(MIMETREEPARSER_LOG) << "Message doesn't set the protocol for the multipart/signed content-type, "
51 "using content-type of the signature:"
52 << signatureContentType;
53 protocolContentType = signatureContentType;
54 }
55
56 const QGpgME::Protocol *protocol = nullptr;
57 if (protocolContentType == QLatin1StringView("application/pkcs7-signature") || protocolContentType == QLatin1StringView("application/x-pkcs7-signature")) {
58 protocol = QGpgME::smime();
59 } else if (protocolContentType == QLatin1StringView("application/pgp-signature")
60 || protocolContentType == QLatin1StringView("application/x-pgp-signature")) {
61 protocol = QGpgME::openpgp();
62 }
63
64 if (!protocol) {
65 return MessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), signedData, false));
66 }
67
68 part.nodeHelper()->setNodeProcessed(signature, true);
69
70 part.nodeHelper()->setSignatureState(node, KMMsgFullySigned);
71
72 const QByteArray cleartext = KMime::LFtoCRLF(signedData->encodedContent());
73 QStringDecoder aCodec(part.objectTreeParser()->codecNameFor(signedData).constData());
74
76 new SignedMessagePart(part.objectTreeParser(), aCodec.decode(cleartext), protocol, part.nodeHelper()->fromAsString(node), signedData));
77
78 mp->startVerificationDetached(cleartext, signedData, signature->decodedContent());
79 part.nodeHelper()->registerOverrideHeader(node, mp);
80 return mp;
81}
const Headers::ContentType * contentType() const
QByteArray decodedContent() const
QByteArray encodedContent(bool useCrLf=false) const
QList< Content * > contents()
QByteArray mimeType() const
QString parameter(QByteArrayView key) const
interface of message body parts.
Definition bodypart.h:45
virtual MimeTreeParser::ObjectTreeParser * objectTreeParser() const =0
For making it easier to refactor, add objectTreeParser.
virtual MimeTreeParser::NodeHelper * nodeHelper() const =0
Ok, this is ugly, exposing the node helper here, but there is too much useful stuff in there for real...
virtual KMime::Content * content() const =0
Returns the KMime::Content node represented here.
const char * constData() const const
QByteArray toLower() const const
bool isEmpty() const const
QString toLower() const const
QTextStream & endl(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.