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