| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// check_signature_basics.cpp |
| 4 |
// |
| 5 |
// This file is licensed under the GPLv2 or later |
| 6 |
// |
| 7 |
// Copyright 2023-2025 g10 Code GmbH, Author: Sune Stolborg Vuorela <[email protected]> |
| 8 |
// Copyright 2025 Albert Astals Cid <[email protected]> |
| 9 |
//======================================================================== |
| 10 |
|
| 11 |
// Simple tests of reading signatures |
| 12 |
// |
| 13 |
// Note that this does not check the actual validity because |
| 14 |
// that will have an expiry date, and adding time bombs to unit tests is |
| 15 |
// probably not a good idea. |
| 16 |
#include <QtTest/QTest> |
| 17 |
#include <QTemporaryDir> |
| 18 |
#include "PDFDoc.h" |
| 19 |
#include "GlobalParams.h" |
| 20 |
#include "SignatureInfo.h" |
| 21 |
#include "CryptoSignBackend.h" |
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
class TestSignatureBasics : public QObject |
| 25 |
{ |
| 26 |
Q_OBJECT |
| 27 |
public: |
| 28 |
explicit TestSignatureBasics(QObject *parent = nullptr) : QObject(parent) { } |
| 29 |
|
| 30 |
std::unique_ptr<PDFDoc> doc; |
| 31 |
|
| 32 |
static std::unique_ptr<QTemporaryDir> tmpdir; |
| 33 |
static void initMain() |
| 34 |
{ |
| 35 |
tmpdir = std::make_unique<QTemporaryDir>(); |
| 36 |
qputenv("GNUPGHOME", tmpdir->path().toLocal8Bit().data()); |
| 37 |
} |
| 38 |
|
| 39 |
private Q_SLOTS: |
| 40 |
void init(); |
| 41 |
void initTestCase_data(); |
| 42 |
void initTestCase() { } |
| 43 |
void cleanupTestCase(); |
| 44 |
void testSignatureCount(); |
| 45 |
void testSignatureSizes(); |
| 46 |
void testSignerInfo(); // names and stuff |
| 47 |
void testSignedRanges(); |
| 48 |
}; |
| 49 |
|
| 50 |
std::unique_ptr<QTemporaryDir> TestSignatureBasics::tmpdir; |
| 51 |
|
| 52 |
void TestSignatureBasics::init() |
| 53 |
{ |
| 54 |
#if ENABLE_SIGNATURES |
| 55 |
QFETCH_GLOBAL(CryptoSign::Backend::Type, backend); |
| 56 |
CryptoSign::Factory::setPreferredBackend(backend); |
| 57 |
QCOMPARE(CryptoSign::Factory::getActive(), backend); |
| 58 |
#endif |
| 59 |
|
| 60 |
QFETCH_GLOBAL(std::string, filename); |
| 61 |
|
| 62 |
globalParams = std::make_unique<GlobalParams>(); |
| 63 |
doc = std::make_unique<PDFDoc>(std::make_unique<GooString>(filename)); |
| 64 |
QVERIFY(doc); |
| 65 |
QVERIFY(doc->isOk()); |
| 66 |
} |
| 67 |
|
| 68 |
void TestSignatureBasics::initTestCase_data() |
| 69 |
{ |
| 70 |
|
| 71 |
#if ENABLE_SIGNATURES |
| 72 |
const auto availableBackends = CryptoSign::Factory::getAvailable(); |
| 73 |
QTest::addColumn<CryptoSign::Backend::Type>("backend"); |
| 74 |
#endif |
| 75 |
QTest::addColumn<std::string>("filename"); |
| 76 |
|
| 77 |
for (auto document : { TESTDATADIR "/unittestcases/pdf-signature-sample-2sigs.pdf", TESTDATADIR "/unittestcases/pdf-signature-sample-2sigs-randompadded.pdf" }) { |
| 78 |
#if ENABLE_SIGNATURES |
| 79 |
|
| 80 |
# if ENABLE_NSS3 |
| 81 |
if (std::ranges::find(availableBackends, CryptoSign::Backend::Type::NSS3) != availableBackends.end()) { |
| 82 |
QTest::addRow("nss %s", document) << CryptoSign::Backend::Type::NSS3 << std::string { document }; |
| 83 |
} else { |
| 84 |
QWARN("Compiled with NSS3, but NSS not functional"); |
| 85 |
} |
| 86 |
# endif |
| 87 |
# if ENABLE_GPGME |
| 88 |
if (std::ranges::find(availableBackends, CryptoSign::Backend::Type::GPGME) != availableBackends.end()) { |
| 89 |
QTest::addRow("gpg %s", document) << CryptoSign::Backend::Type::GPGME << std::string { document }; |
| 90 |
} else { |
| 91 |
QWARN("Compiled with GPGME, but GPGME not functional"); |
| 92 |
} |
| 93 |
# endif |
| 94 |
#else |
| 95 |
QTest::addRow("%s", document) << std::string { document }; |
| 96 |
#endif |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
void TestSignatureBasics::cleanupTestCase() |
| 101 |
{ |
| 102 |
globalParams.reset(); |
| 103 |
} |
| 104 |
|
| 105 |
void TestSignatureBasics::testSignatureCount() |
| 106 |
{ |
| 107 |
QVERIFY(doc); |
| 108 |
auto signatureFields = doc->getSignatureFields(); |
| 109 |
QCOMPARE(signatureFields.size(), 4); |
| 110 |
// count active signatures |
| 111 |
QVERIFY(!signatureFields[0]->getSignature().empty()); |
| 112 |
QVERIFY(!signatureFields[1]->getSignature().empty()); |
| 113 |
QVERIFY(signatureFields[2]->getSignature().empty()); |
| 114 |
QVERIFY(signatureFields[3]->getSignature().empty()); |
| 115 |
} |
| 116 |
|
| 117 |
void TestSignatureBasics::testSignatureSizes() |
| 118 |
{ |
| 119 |
auto signatureFields = doc->getSignatureFields(); |
| 120 |
// These are not the actual signature lengths, but rather |
| 121 |
// the length of the signature field, which is likely |
| 122 |
// a padded field. At least the pdf specification suggest to pad |
| 123 |
// the field. |
| 124 |
// Poppler before 23.04 did not have a padded field, later versions do. |
| 125 |
QCOMPARE(signatureFields[0]->getSignature().size(), 10230); // Signature data size is 2340 |
| 126 |
QCOMPARE(signatureFields[1]->getSignature().size(), 10196); // Signature data size is 2340 |
| 127 |
} |
| 128 |
|
| 129 |
void TestSignatureBasics::testSignerInfo() |
| 130 |
{ |
| 131 |
auto signatureFields = doc->getSignatureFields(); |
| 132 |
QCOMPARE(signatureFields[0]->getCreateWidget()->getField()->getFullyQualifiedName()->toStr(), std::string { "P2.AnA_Signature0_B_" }); |
| 133 |
QCOMPARE(signatureFields[0]->getSignatureType(), CryptoSign::SignatureType::ETSI_CAdES_detached); |
| 134 |
auto siginfo0 = signatureFields[0]->validateSignatureAsync(false, false, -1 /* now */, false, false, {}); |
| 135 |
signatureFields[0]->validateSignatureResult(); |
| 136 |
#if ENABLE_SIGNATURES |
| 137 |
QCOMPARE(siginfo0->getSignerName(), std::string { "Koch, Werner" }); |
| 138 |
QCOMPARE(siginfo0->getHashAlgorithm(), HashAlgorithm::Sha256); |
| 139 |
QCOMPARE(siginfo0->getCertificateInfo()->getPublicKeyInfo().publicKeyStrength, 2048 / 8); |
| 140 |
#else |
| 141 |
QCOMPARE(siginfo0->getSignerName(), std::string {}); |
| 142 |
QCOMPARE(siginfo0->getHashAlgorithm(), HashAlgorithm::Unknown); |
| 143 |
#endif |
| 144 |
QCOMPARE(siginfo0->getSigningTime(), time_t(1677570911)); |
| 145 |
|
| 146 |
QCOMPARE(signatureFields[1]->getCreateWidget()->getField()->getFullyQualifiedName()->toStr(), std::string { "P2.AnA_Signature1_B_" }); |
| 147 |
QCOMPARE(signatureFields[1]->getSignatureType(), CryptoSign::SignatureType::ETSI_CAdES_detached); |
| 148 |
auto siginfo1 = signatureFields[1]->validateSignatureAsync(false, false, -1 /* now */, false, false, {}); |
| 149 |
signatureFields[1]->validateSignatureResult(); |
| 150 |
#if ENABLE_SIGNATURES |
| 151 |
QCOMPARE(siginfo1->getSignerName(), std::string { "Koch, Werner" }); |
| 152 |
QCOMPARE(siginfo1->getHashAlgorithm(), HashAlgorithm::Sha256); |
| 153 |
QFETCH_GLOBAL(CryptoSign::Backend::Type, backend); |
| 154 |
if (backend == CryptoSign::Backend::Type::GPGME) { |
| 155 |
QCOMPARE(siginfo1->getCertificateInfo()->getPublicKeyInfo().publicKeyStrength, 2048 / 8); |
| 156 |
} else if (backend == CryptoSign::Backend::Type::NSS3) { |
| 157 |
// Not fully sure why it is zero here, but it seems to be. |
| 158 |
QCOMPARE(siginfo1->getCertificateInfo()->getPublicKeyInfo().publicKeyStrength, 0); |
| 159 |
} |
| 160 |
#else |
| 161 |
QCOMPARE(siginfo1->getSignerName(), std::string {}); |
| 162 |
QCOMPARE(siginfo1->getHashAlgorithm(), HashAlgorithm::Unknown); |
| 163 |
#endif |
| 164 |
QCOMPARE(siginfo1->getSigningTime(), time_t(1677840601)); |
| 165 |
|
| 166 |
QCOMPARE(signatureFields[2]->getCreateWidget()->getField()->getFullyQualifiedName()->toStr(), std::string { "P2.AnA_Signature2_B_" }); |
| 167 |
QCOMPARE(signatureFields[2]->getSignatureType(), CryptoSign::SignatureType::unsigned_signature_field); |
| 168 |
QCOMPARE(signatureFields[3]->getCreateWidget()->getField()->getFullyQualifiedName()->toStr(), std::string { "P2.AnA_Signature3_B_" }); |
| 169 |
QCOMPARE(signatureFields[3]->getSignatureType(), CryptoSign::SignatureType::unsigned_signature_field); |
| 170 |
} |
| 171 |
|
| 172 |
void TestSignatureBasics::testSignedRanges() |
| 173 |
{ |
| 174 |
auto signatureFields = doc->getSignatureFields(); |
| 175 |
|
| 176 |
auto [sig0, size0] = signatureFields[0]->getCheckedSignature(); |
| 177 |
QVERIFY(sig0); |
| 178 |
auto ranges0 = signatureFields[0]->getSignedRangeBounds(); |
| 179 |
QCOMPARE(ranges0.size(), 4); |
| 180 |
QCOMPARE(ranges0[0], 0); |
| 181 |
QCOMPARE(ranges0[1], 24890); |
| 182 |
QCOMPARE(ranges0[2], 45352); |
| 183 |
QCOMPARE(ranges0[3], 58529); |
| 184 |
QVERIFY(ranges0[3] != size0); // signature does not cover all of it |
| 185 |
|
| 186 |
auto [sig1, size1] = signatureFields[1]->getCheckedSignature(); |
| 187 |
QVERIFY(sig1); |
| 188 |
auto ranges1 = signatureFields[1]->getSignedRangeBounds(); |
| 189 |
QCOMPARE(ranges1.size(), 4); |
| 190 |
QCOMPARE(ranges1[0], 0); |
| 191 |
QCOMPARE(ranges1[1], 59257); |
| 192 |
QCOMPARE(ranges1[2], 79651); |
| 193 |
QCOMPARE(ranges1[3], 92773); |
| 194 |
QCOMPARE(ranges1[3], size1); // signature does cover all of it |
| 195 |
} |
| 196 |
|
| 197 |
QTEST_GUILESS_MAIN(TestSignatureBasics) |
| 198 |
#include "check_signature_basics.moc" |