diff options
Diffstat (limited to 'framework/src/domain/mimetreeparser/otp/multipartsigned.cpp')
-rw-r--r-- | framework/src/domain/mimetreeparser/otp/multipartsigned.cpp | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/framework/src/domain/mimetreeparser/otp/multipartsigned.cpp b/framework/src/domain/mimetreeparser/otp/multipartsigned.cpp new file mode 100644 index 00000000..cb0def6c --- /dev/null +++ b/framework/src/domain/mimetreeparser/otp/multipartsigned.cpp | |||
@@ -0,0 +1,114 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Sandro Knauß <sknauss@kde.org> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #include "multipartsigned.h" | ||
21 | |||
22 | #include "objecttreeparser.h" | ||
23 | #include "messagepart.h" | ||
24 | |||
25 | #include <KMime/Content> | ||
26 | |||
27 | #include <QGpgME/Protocol> | ||
28 | |||
29 | #include "mimetreeparser_debug.h" | ||
30 | |||
31 | #include <QTextCodec> | ||
32 | |||
33 | using namespace MimeTreeParser; | ||
34 | |||
35 | const MultiPartSignedBodyPartFormatter *MultiPartSignedBodyPartFormatter::self; | ||
36 | |||
37 | const Interface::BodyPartFormatter *MultiPartSignedBodyPartFormatter::create() | ||
38 | { | ||
39 | if (!self) { | ||
40 | self = new MultiPartSignedBodyPartFormatter(); | ||
41 | } | ||
42 | return self; | ||
43 | } | ||
44 | Interface::BodyPartFormatter::Result MultiPartSignedBodyPartFormatter::format(Interface::BodyPart *part, HtmlWriter *writer) const | ||
45 | { | ||
46 | Q_UNUSED(writer) | ||
47 | const auto p = process(*part); | ||
48 | const auto mp = static_cast<MessagePart *>(p.data()); | ||
49 | if (mp) { | ||
50 | mp->html(false); | ||
51 | return Ok; | ||
52 | } | ||
53 | return Failed; | ||
54 | } | ||
55 | |||
56 | Interface::MessagePart::Ptr MultiPartSignedBodyPartFormatter::process(Interface::BodyPart &part) const | ||
57 | { | ||
58 | KMime::Content *node = part.content(); | ||
59 | if (node->contents().size() != 2) { | ||
60 | qCDebug(MIMETREEPARSER_LOG) << "mulitpart/signed must have exactly two child parts!" << endl | ||
61 | << "processing as multipart/mixed"; | ||
62 | if (!node->contents().isEmpty()) { | ||
63 | return MessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), node->contents().at(0), false)); | ||
64 | } else { | ||
65 | return MessagePart::Ptr(); | ||
66 | } | ||
67 | } | ||
68 | |||
69 | KMime::Content *signedData = node->contents().at(0); | ||
70 | KMime::Content *signature = node->contents().at(1); | ||
71 | Q_ASSERT(signedData); | ||
72 | Q_ASSERT(signature); | ||
73 | |||
74 | QString protocolContentType = node->contentType()->parameter(QStringLiteral("protocol")).toLower(); | ||
75 | const QString signatureContentType = QLatin1String(signature->contentType()->mimeType().toLower()); | ||
76 | if (protocolContentType.isEmpty()) { | ||
77 | qCWarning(MIMETREEPARSER_LOG) << "Message doesn't set the protocol for the multipart/signed content-type, " | ||
78 | "using content-type of the signature:" << signatureContentType; | ||
79 | protocolContentType = signatureContentType; | ||
80 | } | ||
81 | |||
82 | const QGpgME::Protocol *protocol = nullptr; | ||
83 | if (protocolContentType == QLatin1String("application/pkcs7-signature") || | ||
84 | protocolContentType == QLatin1String("application/x-pkcs7-signature")) { | ||
85 | protocol = QGpgME::smime(); | ||
86 | } else if (protocolContentType == QLatin1String("application/pgp-signature") || | ||
87 | protocolContentType == QLatin1String("application/x-pgp-signature")) { | ||
88 | protocol = QGpgME::openpgp(); | ||
89 | } | ||
90 | |||
91 | if (!protocol) { | ||
92 | return MessagePart::Ptr(new MimeMessagePart(part.objectTreeParser(), signedData, false)); | ||
93 | } | ||
94 | |||
95 | part.nodeHelper()->setNodeProcessed(signature, true); | ||
96 | |||
97 | part.nodeHelper()->setSignatureState(node, KMMsgFullySigned); | ||
98 | |||
99 | const QByteArray cleartext = KMime::LFtoCRLF(signedData->encodedContent()); | ||
100 | const QTextCodec *aCodec(part.objectTreeParser()->codecFor(signedData)); | ||
101 | |||
102 | SignedMessagePart::Ptr mp(new SignedMessagePart(part.objectTreeParser(), | ||
103 | aCodec->toUnicode(cleartext), protocol, | ||
104 | part.nodeHelper()->fromAsString(node), signature)); | ||
105 | PartMetaData *messagePart(mp->partMetaData()); | ||
106 | |||
107 | if (protocol) { | ||
108 | mp->startVerificationDetached(cleartext, signedData, signature->decodedContent()); | ||
109 | } else { | ||
110 | messagePart->auditLogError = GpgME::Error(GPG_ERR_NOT_IMPLEMENTED); | ||
111 | } | ||
112 | |||
113 | return mp; | ||
114 | } | ||