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

0001 /*
0002    SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "applicationpgpencrypted.h"
0008 
0009 #include "utils.h"
0010 
0011 #include "messagepart.h"
0012 #include "objecttreeparser.h"
0013 
0014 #include <QGpgME/Protocol>
0015 
0016 #include <KMime/Content>
0017 
0018 #include "mimetreeparser_debug.h"
0019 
0020 using namespace MimeTreeParser;
0021 
0022 const ApplicationPGPEncryptedBodyPartFormatter *ApplicationPGPEncryptedBodyPartFormatter::self;
0023 
0024 const Interface::BodyPartFormatter *ApplicationPGPEncryptedBodyPartFormatter::create()
0025 {
0026     if (!self) {
0027         self = new ApplicationPGPEncryptedBodyPartFormatter();
0028     }
0029     return self;
0030 }
0031 
0032 MessagePart::Ptr ApplicationPGPEncryptedBodyPartFormatter::process(Interface::BodyPart &part) const
0033 {
0034     KMime::Content *node(part.content());
0035 
0036     if (node->decodedContent().trimmed() != "Version: 1") {
0037         qCWarning(MIMETREEPARSER_LOG) << "Unknown PGP Version String:" << node->decodedContent().trimmed();
0038     }
0039 
0040     if (!part.content()->parent()) {
0041         return {};
0042     }
0043 
0044     KMime::Content *data = findTypeInDirectChilds(part.content()->parent(), "application/octet-stream");
0045 
0046     if (!data) {
0047         return {}; // new MimeMessagePart(part.objectTreeParser(), node, false));
0048     }
0049 
0050     part.nodeHelper()->setEncryptionState(node, KMMsgFullyEncrypted);
0051 
0052     EncryptedMessagePart::Ptr mp(
0053         new EncryptedMessagePart(part.objectTreeParser(), data->decodedText(), QGpgME::openpgp(), part.nodeHelper()->fromAsString(data), node));
0054     mp->setIsEncrypted(true);
0055     mp->setDecryptMessage(part.source()->decryptMessage());
0056     PartMetaData *messagePart(mp->partMetaData());
0057     if (!part.source()->decryptMessage()) {
0058         part.nodeHelper()->setNodeProcessed(data, false); // Set the data node to done to prevent it from being processed
0059     } else if (KMime::Content *newNode = part.nodeHelper()->decryptedNodeForContent(data)) {
0060         part.nodeHelper()->registerOverrideHeader(data->parent(), mp);
0061         // if we already have a decrypted node for this encrypted node, don't do the decryption again
0062         return MessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), newNode, false));
0063     } else {
0064         mp->startDecryption(data);
0065         if (!messagePart->inProgress) {
0066             part.nodeHelper()->registerOverrideHeader(data->parent(), mp);
0067             part.nodeHelper()->setNodeProcessed(data, false); // Set the data node to done to prevent it from being processed
0068             if (messagePart->isDecryptable && messagePart->isSigned) {
0069                 part.nodeHelper()->setSignatureState(node, KMMsgFullySigned);
0070             }
0071         }
0072     }
0073     return mp;
0074 }