File indexing completed on 2024-06-23 05:19:20

0001 /*
0002    SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "multipartsigned.h"
0008 
0009 #include "messagepart.h"
0010 #include "objecttreeparser.h"
0011 
0012 #include <KMime/Content>
0013 
0014 #include <QGpgME/Protocol>
0015 
0016 #include "mimetreeparser_debug.h"
0017 
0018 using namespace MimeTreeParser;
0019 
0020 const MultiPartSignedBodyPartFormatter *MultiPartSignedBodyPartFormatter::self;
0021 
0022 const Interface::BodyPartFormatter *MultiPartSignedBodyPartFormatter::create()
0023 {
0024     if (!self) {
0025         self = new MultiPartSignedBodyPartFormatter();
0026     }
0027     return self;
0028 }
0029 
0030 MessagePart::Ptr MultiPartSignedBodyPartFormatter::process(Interface::BodyPart &part) const
0031 {
0032     KMime::Content *node = part.content();
0033     if (node->contents().size() != 2) {
0034         qCDebug(MIMETREEPARSER_LOG) << "multipart/signed must have exactly two child parts!" << Qt::endl << "processing as multipart/mixed";
0035         if (!node->contents().isEmpty()) {
0036             return MessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), node->contents().at(0), false));
0037         } else {
0038             return {};
0039         }
0040     }
0041 
0042     KMime::Content *signedData = node->contents().at(0);
0043     KMime::Content *signature = node->contents().at(1);
0044     Q_ASSERT(signedData);
0045     Q_ASSERT(signature);
0046 
0047     QString protocolContentType = node->contentType()->parameter(QStringLiteral("protocol")).toLower();
0048     const QString signatureContentType = QLatin1StringView(signature->contentType()->mimeType().toLower());
0049     if (protocolContentType.isEmpty()) {
0050         qCWarning(MIMETREEPARSER_LOG) << "Message doesn't set the protocol for the multipart/signed content-type, "
0051                                          "using content-type of the signature:"
0052                                       << signatureContentType;
0053         protocolContentType = signatureContentType;
0054     }
0055 
0056     const QGpgME::Protocol *protocol = nullptr;
0057     if (protocolContentType == QLatin1StringView("application/pkcs7-signature") || protocolContentType == QLatin1StringView("application/x-pkcs7-signature")) {
0058         protocol = QGpgME::smime();
0059     } else if (protocolContentType == QLatin1StringView("application/pgp-signature")
0060                || protocolContentType == QLatin1StringView("application/x-pgp-signature")) {
0061         protocol = QGpgME::openpgp();
0062     }
0063 
0064     if (!protocol) {
0065         return MessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), signedData, false));
0066     }
0067 
0068     part.nodeHelper()->setNodeProcessed(signature, true);
0069 
0070     part.nodeHelper()->setSignatureState(node, KMMsgFullySigned);
0071 
0072     const QByteArray cleartext = KMime::LFtoCRLF(signedData->encodedContent());
0073     QStringDecoder aCodec(part.objectTreeParser()->codecNameFor(signedData).constData());
0074 
0075     SignedMessagePart::Ptr mp(
0076         new SignedMessagePart(part.objectTreeParser(), aCodec.decode(cleartext), protocol, part.nodeHelper()->fromAsString(node), signedData));
0077 
0078     mp->startVerificationDetached(cleartext, signedData, signature->decodedContent());
0079     part.nodeHelper()->registerOverrideHeader(node, mp);
0080     return mp;
0081 }