Page MenuHome GnuPG

No OneTemporary

diff --git a/src/core/autotests/cryptohelpertest.cpp b/src/core/autotests/cryptohelpertest.cpp
index cdc112a..ba6889e 100644
--- a/src/core/autotests/cryptohelpertest.cpp
+++ b/src/core/autotests/cryptohelpertest.cpp
@@ -1,133 +1,133 @@
-// Copyright 2015 Sandro Knauß <knauss@kolabsys.com>
+// SPDX-FileCopyrightText: 2015 Sandro Knauß <knauss@kolabsys.com>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#include "cryptohelpertest.h"
#include "cryptohelper.h"
#include <QTest>
using namespace MimeTreeParser;
void CryptoHelperTest::testPMFDEmpty()
{
QCOMPARE(prepareMessageForDecryption("").count(), 0);
}
void CryptoHelperTest::testPMFDWithNoPGPBlock()
{
const QByteArray text = "testblabla";
- const QList<Block> blocks = prepareMessageForDecryption(text);
+ const QVector<Block> blocks = prepareMessageForDecryption(text);
QCOMPARE(blocks.count(), 1);
QCOMPARE(blocks[0].text(), text);
QCOMPARE(blocks[0].type(), NoPgpBlock);
}
void CryptoHelperTest::testPGPBlockType()
{
const QString blockText = QStringLiteral("text");
const QString preString = QStringLiteral("before\n");
for (int i = 1; i <= PrivateKeyBlock; ++i) {
QString name;
switch (i) {
case PgpMessageBlock:
name = QStringLiteral("MESSAGE");
break;
case MultiPgpMessageBlock:
name = QStringLiteral("MESSAGE PART");
break;
case SignatureBlock:
name = QStringLiteral("SIGNATURE");
break;
case ClearsignedBlock:
name = QStringLiteral("SIGNED MESSAGE");
break;
case PublicKeyBlock:
name = QStringLiteral("PUBLIC KEY BLOCK");
break;
case PrivateKeyBlock:
name = QStringLiteral("PRIVATE KEY BLOCK");
break;
}
- QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1String("\n") + blockText;
- QList<Block> blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
+ QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1Char('\n') + blockText;
+ QVector<Block> blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
QCOMPARE(blocks.count(), 1);
QCOMPARE(blocks[0].type(), UnknownBlock);
- text += QLatin1String("\n-----END PGP ") + name + QLatin1String("\n");
+ text += QLatin1String("\n-----END PGP ") + name + QLatin1Char('\n');
blocks = prepareMessageForDecryption(preString.toLatin1() + text.toLatin1());
QCOMPARE(blocks.count(), 2);
QCOMPARE(blocks[1].text(), text.toLatin1());
QCOMPARE(blocks[1].type(), static_cast<PGPBlockType>(i));
}
}
void CryptoHelperTest::testDeterminePGPBlockType()
{
const QString blockText = QStringLiteral("text");
for (int i = 1; i <= PrivateKeyBlock; ++i) {
QString name;
switch (i) {
case PgpMessageBlock:
name = QStringLiteral("MESSAGE");
break;
case MultiPgpMessageBlock:
name = QStringLiteral("MESSAGE PART");
break;
case SignatureBlock:
name = QStringLiteral("SIGNATURE");
break;
case ClearsignedBlock:
name = QStringLiteral("SIGNED MESSAGE");
break;
case PublicKeyBlock:
name = QStringLiteral("PUBLIC KEY BLOCK");
break;
case PrivateKeyBlock:
name = QStringLiteral("PRIVATE KEY BLOCK");
break;
}
- const QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1String("\n") + blockText + QLatin1String("\n");
+ const QString text = QLatin1String("-----BEGIN PGP ") + name + QLatin1Char('\n') + blockText + QLatin1Char('\n');
const Block block = Block(text.toLatin1());
QCOMPARE(block.text(), text.toLatin1());
QCOMPARE(block.type(), static_cast<PGPBlockType>(i));
}
}
void CryptoHelperTest::testEmbededPGPBlock()
{
const QByteArray text = QByteArray("before\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\nafter");
- const QList<Block> blocks = prepareMessageForDecryption(text);
+ const QVector<Block> blocks = prepareMessageForDecryption(text);
QCOMPARE(blocks.count(), 3);
QCOMPARE(blocks[0].text(), QByteArray("before\n"));
QCOMPARE(blocks[1].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"));
QCOMPARE(blocks[2].text(), QByteArray("after"));
}
void CryptoHelperTest::testClearSignedMessage()
{
const QByteArray text = QByteArray(
"before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\nafter");
- const QList<Block> blocks = prepareMessageForDecryption(text);
+ const QVector<Block> blocks = prepareMessageForDecryption(text);
QCOMPARE(blocks.count(), 3);
QCOMPARE(blocks[0].text(), QByteArray("before\n"));
QCOMPARE(blocks[1].text(),
QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n"));
QCOMPARE(blocks[2].text(), QByteArray("after"));
}
void CryptoHelperTest::testMultipleBlockMessage()
{
const QByteArray text = QByteArray(
"before\n-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP "
"SIGNATURE-----\nafter\n-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n");
- const QList<Block> blocks = prepareMessageForDecryption(text);
+ const QVector<Block> blocks = prepareMessageForDecryption(text);
QCOMPARE(blocks.count(), 4);
QCOMPARE(blocks[0].text(), QByteArray("before\n"));
QCOMPARE(blocks[1].text(),
QByteArray("-----BEGIN PGP SIGNED MESSAGE-----\nsigned content\n-----BEGIN PGP SIGNATURE-----\nfancy signature\n-----END PGP SIGNATURE-----\n"));
QCOMPARE(blocks[2].text(), QByteArray("after\n"));
QCOMPARE(blocks[3].text(), QByteArray("-----BEGIN PGP MESSAGE-----\ncrypted - you see :)\n-----END PGP MESSAGE-----\n"));
}
QTEST_APPLESS_MAIN(CryptoHelperTest)
diff --git a/src/core/autotests/cryptohelpertest.h b/src/core/autotests/cryptohelpertest.h
index 8d45c59..e6faf2e 100644
--- a/src/core/autotests/cryptohelpertest.h
+++ b/src/core/autotests/cryptohelpertest.h
@@ -1,25 +1,23 @@
-// Copyright 2009 Thomas McGuire <mcguire@kde.org>
+// SPDX-FileCopyrightText: 2009 Thomas McGuire <mcguire@kde.org>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
#pragma once
#include <QObject>
namespace MimeTreeParser
{
-
class CryptoHelperTest : public QObject
{
Q_OBJECT
private Q_SLOTS:
void testPMFDEmpty();
void testPMFDWithNoPGPBlock();
void testPGPBlockType();
void testDeterminePGPBlockType();
void testEmbededPGPBlock();
void testClearSignedMessage();
void testMultipleBlockMessage();
};
-
}
diff --git a/src/core/cryptohelper.cpp b/src/core/cryptohelper.cpp
index b1ed365..d9bc7b0 100644
--- a/src/core/cryptohelper.cpp
+++ b/src/core/cryptohelper.cpp
@@ -1,137 +1,145 @@
// SPDX-FileCopyrightText: 2015 Sandro Knauß <knauss@kolabsys.com>
// SPDX-FileCopyrightText: 2001,2002 the KPGP authors
// SPDX-License-Identifier: GPL-2.0-or-later
#include "cryptohelper.h"
using namespace MimeTreeParser;
PGPBlockType Block::determineType() const
{
const QByteArray data = text();
- if (data.startsWith("-----BEGIN PGP SIGNED")) {
+ if (data.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----")) {
+ return NoPgpBlock;
+ } else if (data.startsWith("-----BEGIN PGP SIGNED")) {
return ClearsignedBlock;
} else if (data.startsWith("-----BEGIN PGP SIGNATURE")) {
return SignatureBlock;
} else if (data.startsWith("-----BEGIN PGP PUBLIC")) {
return PublicKeyBlock;
} else if (data.startsWith("-----BEGIN PGP PRIVATE") || data.startsWith("-----BEGIN PGP SECRET")) {
return PrivateKeyBlock;
} else if (data.startsWith("-----BEGIN PGP MESSAGE")) {
if (data.startsWith("-----BEGIN PGP MESSAGE PART")) {
return MultiPgpMessageBlock;
} else {
return PgpMessageBlock;
}
} else if (data.startsWith("-----BEGIN PGP ARMORED FILE")) {
return PgpMessageBlock;
} else if (data.startsWith("-----BEGIN PGP ")) {
return UnknownBlock;
} else {
return NoPgpBlock;
}
}
-QList<Block> MimeTreeParser::prepareMessageForDecryption(const QByteArray &msg)
+QVector<Block> MimeTreeParser::prepareMessageForDecryption(const QByteArray &msg)
{
PGPBlockType pgpBlock = NoPgpBlock;
- QList<Block> blocks;
+ QVector<Block> blocks;
int start = -1; // start of the current PGP block
int lastEnd = -1; // end of the last PGP block
const int length = msg.length();
if (msg.isEmpty()) {
return blocks;
}
+ if (msg.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----")) {
+ return blocks;
+ }
if (msg.startsWith("-----BEGIN PGP ")) {
start = 0;
} else {
start = msg.indexOf("\n-----BEGIN PGP ") + 1;
if (start == 0) {
blocks.append(Block(msg, NoPgpBlock));
return blocks;
}
}
while (start != -1) {
- int nextEnd, nextStart;
+ int nextEnd;
+ int nextStart;
// is the PGP block a clearsigned block?
if (!strncmp(msg.constData() + start + 15, "SIGNED", 6)) {
pgpBlock = ClearsignedBlock;
} else {
pgpBlock = UnknownBlock;
}
nextEnd = msg.indexOf("\n-----END PGP ", start + 15);
nextStart = msg.indexOf("\n-----BEGIN PGP ", start + 15);
if (nextEnd == -1) { // Missing END PGP line
if (lastEnd != -1) {
blocks.append(Block(msg.mid(lastEnd + 1), UnknownBlock));
} else {
blocks.append(Block(msg.mid(start), UnknownBlock));
}
break;
}
if ((nextStart == -1) || (nextEnd < nextStart) || (pgpBlock == ClearsignedBlock)) {
// most likely we found a PGP block (but we don't check if it's valid)
// store the preceding non-PGP block
if (start - lastEnd - 1 > 0) {
blocks.append(Block(msg.mid(lastEnd + 1, start - lastEnd - 1), NoPgpBlock));
}
lastEnd = msg.indexOf("\n", nextEnd + 14);
if (lastEnd == -1) {
if (start < length) {
blocks.append(Block(msg.mid(start)));
}
break;
} else {
blocks.append(Block(msg.mid(start, lastEnd + 1 - start)));
if ((nextStart != -1) && (nextEnd > nextStart)) {
nextStart = msg.indexOf("\n-----BEGIN PGP ", lastEnd + 1);
}
}
}
start = nextStart;
if (start == -1) {
if (lastEnd + 1 < length) {
// rest of mail is no PGP Block
blocks.append(Block(msg.mid(lastEnd + 1), NoPgpBlock));
}
break;
} else {
start++; // move start behind the '\n'
}
}
return blocks;
}
+Block::Block() = default;
+
Block::Block(const QByteArray &m)
: msg(m)
{
mType = determineType();
}
Block::Block(const QByteArray &m, PGPBlockType t)
: msg(m)
, mType(t)
{
}
QByteArray MimeTreeParser::Block::text() const
{
return msg;
}
PGPBlockType Block::type() const
{
return mType;
}
diff --git a/src/core/cryptohelper.h b/src/core/cryptohelper.h
index 88d939f..b3ff4b8 100644
--- a/src/core/cryptohelper.h
+++ b/src/core/cryptohelper.h
@@ -1,46 +1,45 @@
// SPDX-FileCopyrightText: 2015 Sandro Knauß <knauss@kolabsys.com>
// SPDX-FileCopyrightText: 2001,2002 the KPGP authors
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <QByteArray>
-#include <QList>
+#include <QVector>
namespace MimeTreeParser
{
-
enum PGPBlockType {
UnknownBlock = -1, // BEGIN PGP ???
NoPgpBlock = 0,
PgpMessageBlock = 1, // BEGIN PGP MESSAGE
MultiPgpMessageBlock = 2, // BEGIN PGP MESSAGE, PART X[/Y]
SignatureBlock = 3, // BEGIN PGP SIGNATURE
ClearsignedBlock = 4, // BEGIN PGP SIGNED MESSAGE
PublicKeyBlock = 5, // BEGIN PGP PUBLIC KEY BLOCK
- PrivateKeyBlock = 6 // BEGIN PGP PRIVATE KEY BLOCK (PGP 2.x: ...SECRET...)
+ PrivateKeyBlock = 6, // BEGIN PGP PRIVATE KEY BLOCK (PGP 2.x: ...SECRET...)
};
class Block
{
public:
- explicit Block(const QByteArray &m);
+ Block();
+ Block(const QByteArray &m);
Block(const QByteArray &m, PGPBlockType t);
- QByteArray text() const;
- PGPBlockType type() const;
- PGPBlockType determineType() const;
+ Q_REQUIRED_RESULT QByteArray text() const;
+ Q_REQUIRED_RESULT PGPBlockType type() const;
+ Q_REQUIRED_RESULT PGPBlockType determineType() const;
QByteArray msg;
- PGPBlockType mType;
+ PGPBlockType mType = UnknownBlock;
};
/** Parses the given message and splits it into OpenPGP blocks and
Non-OpenPGP blocks.
*/
-QList<Block> prepareMessageForDecryption(const QByteArray &msg);
-
+Q_REQUIRED_RESULT QVector<Block> prepareMessageForDecryption(const QByteArray &msg);
} // namespace MimeTreeParser
Q_DECLARE_TYPEINFO(MimeTreeParser::Block, Q_MOVABLE_TYPE);

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jul 17, 12:51 AM (1 d, 13 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
60/02/a2aed6392cac2518c587f075ff67

Event Timeline