diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index af242455b..863b60c6a 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,60 +1,61 @@ # SPDX-License-Identifier: CC0-1.0 # SPDX-FileCopyrightText: none remove_definitions(-DQT_NO_CAST_FROM_ASCII) include(ECMAddTests) find_package(Qt${QT_MAJOR_VERSION}Test ${REQUIRED_QT_VERSION} CONFIG QUIET) if(NOT TARGET Qt::Test) message(STATUS "Qt${QT_MAJOR_VERSION}Test not found, autotests will not be built.") return() endif() ecm_add_test( flatkeylistmodeltest.cpp abstractkeylistmodeltest.cpp TEST_NAME flatkeylistmodeltest LINK_LIBRARIES KPim${KF_MAJOR_VERSION}::Libkleo Qt::Test ) ecm_add_test( hierarchicalkeylistmodeltest.cpp abstractkeylistmodeltest.cpp abstractkeylistmodeltest.h TEST_NAME hierarchicalkeylistmodeltest LINK_LIBRARIES KPim${KF_MAJOR_VERSION}::Libkleo Qt::Test ) ecm_add_test( keyresolvercoretest.cpp keyresolvercoretest.qrc TEST_NAME keyresolvercoretest LINK_LIBRARIES KPim${KF_MAJOR_VERSION}::Libkleo Qt::Test ) ecm_add_tests( editdirectoryservicedialogtest.cpp LINK_LIBRARIES KPim${KF_MAJOR_VERSION}::Libkleo KF${KF_MAJOR_VERSION}::WidgetsAddons Qt::Widgets Qt::Test ) ecm_add_tests( keyselectioncombotest.cpp keyserverconfigtest.cpp newkeyapprovaldialogtest.cpp LINK_LIBRARIES KPim${KF_MAJOR_VERSION}::Libkleo Qt::Widgets Qt::Test ) ecm_add_test( expirycheckertest.cpp expirycheckertest.qrc + testhelpers.h TEST_NAME expirycheckertest LINK_LIBRARIES KPim${KF_MAJOR_VERSION}::Libkleo Qt::Test ) ecm_add_tests( hextest.cpp LINK_LIBRARIES KPim${KF_MAJOR_VERSION}::Libkleo Qt::Test ) diff --git a/autotests/expirycheckertest.cpp b/autotests/expirycheckertest.cpp index ca5df1850..3c1b78f1d 100644 --- a/autotests/expirycheckertest.cpp +++ b/autotests/expirycheckertest.cpp @@ -1,423 +1,522 @@ /* This file is part of libkleopatra's test suite. SPDX-FileCopyrightText: 2022 Sandro Knauß SPDX-FileCopyrightText: 2023 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: LGPL-2.0-or-later */ +#include "testhelpers.h" + #include #include #include #include #include #include #include #include #include #include using namespace Kleo; using namespace GpgME; using days = Kleo::chrono::days; class FakeTimeProvider : public Kleo::TimeProvider { public: explicit FakeTimeProvider(const QDate &date) : mTime{date.startOfDay(Qt::UTC).toSecsSinceEpoch()} { } time_t getTime() const override { return mTime; } private: time_t mTime; }; class ExpiryCheckerTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase() { qRegisterMetaType(); mGnupgHome = QTest::qExtractTestData(QStringLiteral("/fixtures/expirycheckertest")); qputenv("GNUPGHOME", mGnupgHome->path().toLocal8Bit()); // hold a reference to the key cache to avoid rebuilding while the test is running mKeyCache = KeyCache::instance(); // make sure that the key cache has been populated (void)mKeyCache->keys(); } void cleanupTestCase() { // verify that nobody else holds a reference to the key cache QVERIFY(mKeyCache.use_count() == 1); mKeyCache.reset(); (void)QProcess::execute(QStringLiteral("gpgconf"), {"--kill", "all"}); mGnupgHome.reset(); qunsetenv("GNUPGHOME"); } void valid_data() { QTest::addColumn("key"); QTest::addColumn("fakedate"); // use dates between creation date and expiration date (if there is one) of the test keys/certificates QTest::newRow("neverExpire") << testKey("test@kolab.org", GpgME::OpenPGP) << QDate{2012, 1, 1}; QTest::newRow("openpgp") << testKey("alice@autocrypt.example", GpgME::OpenPGP) << QDate{2020, 1, 1}; QTest::newRow("smime") << testKey("test@example.com", GpgME::CMS) << QDate{2012, 1, 1}; } void valid() { QFETCH(GpgME::Key, key); QFETCH(QDate, fakedate); ExpiryChecker checker(ExpiryCheckerSettings{days{1}, days{1}, days{1}, days{1}}); checker.setTimeProviderForTest(std::make_shared(fakedate)); QSignalSpy spy(&checker, &ExpiryChecker::expiryMessage); - checker.checkKey(key, ExpiryChecker::NoCheckFlags); + const auto result = checker.checkKey(key, ExpiryChecker::NoCheckFlags); + QCOMPARE(result.checkFlags, ExpiryChecker::NoCheckFlags); + QCOMPARE(result.expiration.certificate, key); + QCOMPARE(result.expiration.status, ExpiryChecker::NotNearExpiry); QCOMPARE(spy.count(), 0); } void expired_data() { QTest::addColumn("key"); QTest::addColumn("checkFlags"); QTest::addColumn("fakedate"); + QTest::addColumn("expectedDuration"); QTest::addColumn("expiryInfo"); QTest::addColumn("msg"); QTest::newRow("openpgp - other; < 1 day ago") // << testKey("alice@autocrypt.example", GpgME::OpenPGP) // << ExpiryChecker::CheckFlags{ExpiryChecker::NoCheckFlags} // << QDate{2021, 1, 22} // the day after the expiration date of the key + << days{0} // << ExpiryChecker::OtherKeyExpired << QStringLiteral( "

The OpenPGP key for

alice@autocrypt.example (KeyID 0xF231550C4F47E38E)

expired less than a day " "ago.

"); QTest::newRow("openpgp - own; 1 day ago") // << testKey("alice@autocrypt.example", GpgME::OpenPGP) // << ExpiryChecker::CheckFlags{ExpiryChecker::OwnKey} // << QDate{2021, 1, 23} // the second day after the expiration date of the key + << days{1} // << ExpiryChecker::OwnKeyExpired << QStringLiteral( "

Your OpenPGP encryption key

alice@autocrypt.example (KeyID 0xF231550C4F47E38E)

expired one day " "ago.

"); QTest::newRow("openpgp - own signing; 2 days ago") // << testKey("alice@autocrypt.example", GpgME::OpenPGP) // << ExpiryChecker::CheckFlags{ExpiryChecker::OwnSigningKey} // << QDate{2021, 1, 24} // the third day after the expiration date of the key + << days{2} // << ExpiryChecker::OwnKeyExpired << QStringLiteral( "

Your OpenPGP signing key

alice@autocrypt.example (KeyID 0xF231550C4F47E38E)

expired 2 days ago.

"); QTest::newRow("smime - other; < 1 day ago") // << testKey("test@example.com", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::NoCheckFlags} // << QDate{2013, 3, 26} // the day after the expiration date of the key + << days{0} // << ExpiryChecker::OtherKeyExpired << QStringLiteral( "

The S/MIME certificate for

CN=unittest cert,EMAIL=test@example.com,O=KDAB,C=US (serial " "number 00D345203A186385C9)

expired less than a day ago.

"); QTest::newRow("smime - own; 1 day ago") // << testKey("test@example.com", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::OwnKey} // << QDate{2013, 3, 27} // the second day after the expiration date of the key + << days{1} // << ExpiryChecker::OwnKeyExpired << QStringLiteral( "

Your S/MIME encryption certificate

CN=unittest cert,EMAIL=test@example.com,O=KDAB,C=US " "(serial number 00D345203A186385C9)

expired one day ago.

"); QTest::newRow("smime - own signing; 2 days ago") // << testKey("test@example.com", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::OwnSigningKey} // << QDate{2013, 3, 28} // the third day after the expiration date of the key + << days{2} // << ExpiryChecker::OwnKeyExpired << QStringLiteral( "

Your S/MIME signing certificate

CN=unittest cert,EMAIL=test@example.com,O=KDAB,C=US " "(serial number 00D345203A186385C9)

expired 2 days ago.

"); } void expired() { QFETCH(GpgME::Key, key); QFETCH(ExpiryChecker::CheckFlags, checkFlags); QFETCH(QDate, fakedate); + QFETCH(Kleo::chrono::days, expectedDuration); QFETCH(ExpiryChecker::ExpiryInformation, expiryInfo); QFETCH(QString, msg); { ExpiryChecker checker(ExpiryCheckerSettings{days{1}, days{1}, days{1}, days{1}}); checker.setTimeProviderForTest(std::make_shared(fakedate)); QSignalSpy spy(&checker, &ExpiryChecker::expiryMessage); - checker.checkKey(key, checkFlags); + const auto result = checker.checkKey(key, checkFlags); + QCOMPARE(result.checkFlags, checkFlags); + QCOMPARE(result.expiration.certificate, key); + QCOMPARE(result.expiration.status, ExpiryChecker::Expired); + QCOMPARE(result.expiration.duration, expectedDuration); QCOMPARE(spy.count(), 1); QList arguments = spy.takeFirst(); QCOMPARE(arguments.at(0).value().keyID(), key.keyID()); QCOMPARE(arguments.at(1).toString(), msg); QCOMPARE(arguments.at(2).value(), expiryInfo); } } void nearexpiry_data() { QTest::addColumn("key"); QTest::addColumn("fakedate"); + QTest::addColumn("expectedDuration"); QTest::addColumn("msg"); QTest::addColumn("msgOwnKey"); QTest::addColumn("msgOwnSigningKey"); // use the day 5 days before the expiration date of the test keys/certificates as fake date QTest::newRow("openpgp") << testKey("alice@autocrypt.example", GpgME::OpenPGP) // - << QDate{2021, 1, 16} + << QDate{2021, 1, 16} // + << days{5} << QStringLiteral( "

The OpenPGP key for

alice@autocrypt.example (KeyID 0xF231550C4F47E38E)

expires in less than 6 days.

") << QStringLiteral( "

Your OpenPGP encryption key

alice@autocrypt.example (KeyID 0xF231550C4F47E38E)

expires in less than 6 " "days.

") << QStringLiteral( "

Your OpenPGP signing key

alice@autocrypt.example (KeyID 0xF231550C4F47E38E)

expires in less than 6 " "days.

"); QTest::newRow("smime") << testKey("test@example.com", GpgME::CMS) // - << QDate{2013, 3, 20} + << QDate{2013, 3, 20} // + << days{5} << QStringLiteral( "

The S/MIME certificate for

CN=unittest cert,EMAIL=test@example.com,O=KDAB,C=US (serial " "number 00D345203A186385C9)

expires in less than 6 days.

") << QStringLiteral( "

Your S/MIME encryption certificate

CN=unittest cert,EMAIL=test@example.com,O=KDAB,C=US " "(serial number 00D345203A186385C9)

expires in less than 6 days.

") << QStringLiteral( "

Your S/MIME signing certificate

CN=unittest cert,EMAIL=test@example.com,O=KDAB,C=US " "(serial number 00D345203A186385C9)

expires in less than 6 days.

"); } void nearexpiry() { QFETCH(GpgME::Key, key); QFETCH(QDate, fakedate); + QFETCH(Kleo::chrono::days, expectedDuration); QFETCH(QString, msg); QFETCH(QString, msgOwnKey); QFETCH(QString, msgOwnSigningKey); { ExpiryChecker checker(ExpiryCheckerSettings{days{1}, days{10}, days{1}, days{1}}); checker.setTimeProviderForTest(std::make_shared(fakedate)); QSignalSpy spy(&checker, &ExpiryChecker::expiryMessage); - // Test if the correct treshold is taken - checker.checkKey(key, ExpiryChecker::NoCheckFlags); - checker.checkKey(key, ExpiryChecker::OwnKey); - checker.checkKey(key, ExpiryChecker::OwnSigningKey); - QCOMPARE(spy.count(), 1); + // Test if the correct threshold is taken + { + const auto result = checker.checkKey(key, ExpiryChecker::NoCheckFlags); + QCOMPARE(result.checkFlags, ExpiryChecker::NoCheckFlags); + QCOMPARE(result.expiration.certificate, key); + QCOMPARE(result.expiration.status, ExpiryChecker::ExpiresSoon); + QCOMPARE(result.expiration.duration, expectedDuration); + QCOMPARE(spy.count(), 1); + } + { + const auto result = checker.checkKey(key, ExpiryChecker::OwnKey); + QCOMPARE(result.checkFlags, ExpiryChecker::OwnKey); + QCOMPARE(result.expiration.certificate, key); + QCOMPARE(result.expiration.status, ExpiryChecker::NotNearExpiry); + QCOMPARE(result.expiration.duration, expectedDuration); + QCOMPARE(spy.count(), 1); + } + { + const auto result = checker.checkKey(key, ExpiryChecker::OwnSigningKey); + QCOMPARE(result.checkFlags, ExpiryChecker::OwnSigningKey); + QCOMPARE(result.expiration.certificate, key); + QCOMPARE(result.expiration.status, ExpiryChecker::NotNearExpiry); + QCOMPARE(result.expiration.duration, expectedDuration); + QCOMPARE(spy.count(), 1); + } QList arguments = spy.takeFirst(); QCOMPARE(arguments.at(0).value().keyID(), key.keyID()); QCOMPARE(arguments.at(1).toString(), msg); QCOMPARE(arguments.at(2).value(), ExpiryChecker::OtherKeyNearExpiry); } { ExpiryChecker checker(ExpiryCheckerSettings{days{10}, days{1}, days{1}, days{1}}); checker.setTimeProviderForTest(std::make_shared(fakedate)); QSignalSpy spy(&checker, &ExpiryChecker::expiryMessage); // Test if the correct treshold is taken checker.checkKey(key, ExpiryChecker::NoCheckFlags); checker.checkKey(key, ExpiryChecker::OwnKey); QCOMPARE(spy.count(), 1); QList arguments = spy.takeFirst(); QCOMPARE(arguments.at(0).value().keyID(), key.keyID()); QCOMPARE(arguments.at(1).toString(), msgOwnKey); QCOMPARE(arguments.at(2).value(), ExpiryChecker::OwnKeyNearExpiry); } { ExpiryChecker checker(ExpiryCheckerSettings{days{10}, days{1}, days{1}, days{1}}); checker.setTimeProviderForTest(std::make_shared(fakedate)); QSignalSpy spy(&checker, &ExpiryChecker::expiryMessage); // Test if the correct treshold is taken checker.checkKey(key, ExpiryChecker::NoCheckFlags); checker.checkKey(key, ExpiryChecker::OwnSigningKey); QCOMPARE(spy.count(), 1); QList arguments = spy.takeFirst(); QCOMPARE(arguments.at(0).value().keyID(), key.keyID()); QCOMPARE(arguments.at(1).toString(), msgOwnSigningKey); QCOMPARE(arguments.at(2).value(), ExpiryChecker::OwnKeyNearExpiry); } } void certificateChain_data() { QTest::addColumn("key"); QTest::addColumn("checkFlags"); QTest::addColumn("fakedate"); + QTest::addColumn("expectedStatus"); + QTest::addColumn("expectedDuration"); + QTest::addColumn("expectedChainResults"); + QTest::addColumn("expectedChainCertificate"); + QTest::addColumn("expectedChainStatus"); + QTest::addColumn("expectedChainDuration"); QTest::addColumn("emissions"); QTest::addColumn("keyID"); QTest::addColumn("msg"); QTest::newRow("certificate near expiry; issuer okay") // << testKey("3193786A48BDF2D4D20B8FC6501F4DE8BE231B05", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::CheckChain} // << QDate{2019, 6, 19} // 5 days before expiration date of the certificate + << ExpiryChecker::ExpiresSoon // + << days{5} // + << 0 // no expired or expiring certificates in issuer chain + << Key{} // ignored + << ExpiryChecker::ExpirationStatus{} // ignored + << days{} // ignored << 1 // expect 1 signal emission because of a 2-certificate chain with 1 cert near expiry << QByteArray{"501F4DE8BE231B05"} // first signal emission references the certificate << QStringLiteral( "

The S/MIME certificate for

CN=AddTrust External CA Root,OU=AddTrust External TTP Network,O=AddTrust AB,C=SE " "(serial number 51260A931CE27F9CC3A55F79E072AE82)

expires in less than 6 days.

"); QTest::newRow("certificate near expiry; issuer not checked") // << testKey("3193786A48BDF2D4D20B8FC6501F4DE8BE231B05", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::NoCheckFlags} // << QDate{2019, 6, 19} // 5 days before expiration date of the certificate + << ExpiryChecker::ExpiresSoon // + << days{5} // + << 0 // issuer chain not checked + << Key{} // ignored + << ExpiryChecker::ExpirationStatus{} // ignored + << days{} // ignored << 1 // expect 1 signal emission because certificate is near expiry << QByteArray{"501F4DE8BE231B05"} // signal emission references the certificate << QStringLiteral( "

The S/MIME certificate for

CN=AddTrust External CA Root,OU=AddTrust External TTP Network,O=AddTrust AB,C=SE " "(serial number 51260A931CE27F9CC3A55F79E072AE82)

expires in less than 6 days.

"); QTest::newRow("certificate okay; issuer near expiry") // << testKey("9E99817D12280C9677674430492EDA1DCE2E4C63", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::CheckChain} // << QDate{2019, 6, 19} // 5 days before expiration date of the issuer certificate + << ExpiryChecker::NotNearExpiry // + << days{346} // + << 1 // one expiring certificate in issuer chain + << testKey("3193786A48BDF2D4D20B8FC6501F4DE8BE231B05", GpgME::CMS) // + << ExpiryChecker::ExpiresSoon // + << days{5} // << 1 // expect 1 signal emission because of a 2-certificate chain with 1 cert near expiry << QByteArray{"501F4DE8BE231B05"} // first signal emission references the isser certificate << QStringLiteral( "

The intermediate CA certificate

CN=AddTrust External CA Root,OU=AddTrust External TTP Network,O=AddTrust " "AB,C=SE

for S/MIME certificate

CN=UTN - DATACorp SGC,L=Salt Lake " "City,SP=UT,OU=http://www.usertrust.com,O=The USERTRUST Network,C=US (serial number 46EAF096054CC5E3FA65EA6E9F42C664)

expires in " "less than 6 days.

"); QTest::newRow("certificate okay; issuer not checked") // << testKey("9E99817D12280C9677674430492EDA1DCE2E4C63", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::NoCheckFlags} // << QDate{2019, 6, 19} // 5 days before expiration date of the issuer certificate + << ExpiryChecker::NotNearExpiry // + << days{346} // + << 0 // issuer chain not checked + << Key{} // ignored + << ExpiryChecker::ExpirationStatus{} // ignored + << days{} // ignored << 0 // expect 0 signal emission because certificate is not near expiry << QByteArray{} // << QString{}; QTest::newRow("certificate near expiry; issuer expired") // << testKey("9E99817D12280C9677674430492EDA1DCE2E4C63", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::CheckChain} // << QDate{2020, 5, 25} // 5 days before expiration date of the certificate + << ExpiryChecker::ExpiresSoon // + << days{5} // + << 1 // one expired certificate in issuer chain + << testKey("3193786A48BDF2D4D20B8FC6501F4DE8BE231B05", GpgME::CMS) // + << ExpiryChecker::Expired // + << days{335} // << 2 // expect 2 signal emissions because both certificates in the 2-certificate chain are either expired or near expiry << QByteArray{"492EDA1DCE2E4C63"} // first signal emission references the certificate << QStringLiteral( "

The S/MIME certificate for

CN=UTN - DATACorp SGC,L=Salt Lake City,SP=UT,OU=http://www.usertrust.com,O=The " "USERTRUST Network,C=US (serial number 46EAF096054CC5E3FA65EA6E9F42C664)

expires in less than 6 days.

"); QTest::newRow("certificate near expiry; issuer not checked") << testKey("9E99817D12280C9677674430492EDA1DCE2E4C63", GpgME::CMS) // << ExpiryChecker::CheckFlags{ExpiryChecker::NoCheckFlags} // << QDate{2020, 5, 25} // 5 days before expiration date of the certificate + << ExpiryChecker::ExpiresSoon // + << days{5} // + << 0 // issuer chain not checked + << Key{} // ignored + << ExpiryChecker::ExpirationStatus{} // ignored + << days{} // ignored << 1 // expect 1 signal emission because certificate is near expiry << QByteArray{"492EDA1DCE2E4C63"} // first signal emission references the certificate << QStringLiteral( "

The S/MIME certificate for

CN=UTN - DATACorp SGC,L=Salt Lake City,SP=UT,OU=http://www.usertrust.com,O=The " "USERTRUST Network,C=US (serial number 46EAF096054CC5E3FA65EA6E9F42C664)

expires in less than 6 days.

"); } void certificateChain() { QFETCH(GpgME::Key, key); QFETCH(ExpiryChecker::CheckFlags, checkFlags); QFETCH(QDate, fakedate); + QFETCH(ExpiryChecker::ExpirationStatus, expectedStatus); + QFETCH(Kleo::chrono::days, expectedDuration); + QFETCH(int, expectedChainResults); + QFETCH(GpgME::Key, expectedChainCertificate); + QFETCH(ExpiryChecker::ExpirationStatus, expectedChainStatus); + QFETCH(Kleo::chrono::days, expectedChainDuration); QFETCH(int, emissions); QFETCH(QByteArray, keyID); QFETCH(QString, msg); { ExpiryChecker checker(ExpiryCheckerSettings{days{1}, days{10}, days{10}, days{10}}); checker.setTimeProviderForTest(std::make_shared(fakedate)); QSignalSpy spy(&checker, &ExpiryChecker::expiryMessage); - checker.checkKey(key, checkFlags); + const auto result = checker.checkKey(key, checkFlags); + QCOMPARE(result.checkFlags, checkFlags); + QCOMPARE(result.expiration.certificate, key); + QCOMPARE(result.expiration.status, expectedStatus); + QCOMPARE(result.expiration.duration, expectedDuration); + QCOMPARE(result.chainExpiration.size(), expectedChainResults); + if (result.chainExpiration.size() > 0) { + const auto issuerExpiration = result.chainExpiration.front(); + QCOMPARE(issuerExpiration.status, expectedChainStatus); + QCOMPARE(issuerExpiration.duration, expectedChainDuration); + } QCOMPARE(spy.count(), emissions); if (emissions > 0) { QList arguments = spy.takeFirst(); QCOMPARE(arguments.at(0).value().keyID(), keyID); QCOMPARE(arguments.at(1).toString(), msg); QCOMPARE(arguments.at(2).value(), ExpiryChecker::OtherKeyNearExpiry); } } } private: // OpenPGP keys // // pub rsa2048 2009-11-13 [SC] // 1BA323932B3FAA826132C79E8D9860C58F246DE6 // uid [ultimate] unittest key (no password) // sub rsa2048 2009-11-13 [E] // // pub ed25519 2019-01-22 [SC] [expired: 2021-01-21] // EB85BB5FA33A75E15E944E63F231550C4F47E38E // uid [ expired] alice@autocrypt.example // // S/MIME certificates // // ID: 0x212B49DC // S/N: 00D345203A186385C9 // (dec): 15223609549285197257 // Issuer: /CN=unittest cert/O=KDAB/C=US/EMail=test@example.com // Subject: /CN=unittest cert/O=KDAB/C=US/EMail=test@example.com // validity: 2010-06-29 13:48:23 through 2013-03-25 13:48:23 // key type: rsa1024 // chain length: unlimited // sha1 fpr: 24:D2:FC:A2:2E:B3:B8:0A:1E:37:71:D1:4C:C6:58:E3:21:2B:49:DC // sha2 fpr: 62:4B:A4:B8:7D:8F:99:AA:6B:46:E3:C8:C5:BE:BF:30:29:B6:EC:4E:CC:7D:1F:9F:A8:39:B6:CE:03:6F:C7:FB // // S/MIME certificates building a circular chain // // ID: 0xBE231B05 // S/N: 51260A931CE27F9CC3A55F79E072AE82 // (dec): 107864989418777835411218143713715990146 // Issuer: /CN=UTN - DATACorp SGC/OU=http:\x2f\x2fwww.usertrust.com/O=The USERTRUST Network/L=Salt Lake City/ST=UT/C=US // Subject: /CN=AddTrust External CA Root/OU=AddTrust External TTP Network/O=AddTrust AB/C=SE // validity: 2005-06-07 08:09:10 through 2019-06-24 19:06:30 // key type: rsa2048 // key usage: certSign crlSign // ext key usage: ms-serverGatedCrypto (suggested), serverGatedCrypto.ns (suggested) // chain length: unlimited // sha1 fpr: 31:93:78:6A:48:BD:F2:D4:D2:0B:8F:C6:50:1F:4D:E8:BE:23:1B:05 // sha2 fpr: 92:5E:4B:37:2B:A3:2E:5E:87:30:22:84:B2:D7:C9:DF:BF:82:00:FF:CB:A0:D1:66:03:A1:A0:6F:F7:6C:D3:53 // // ID: 0xCE2E4C63 // S/N: 46EAF096054CC5E3FA65EA6E9F42C664 // (dec): 94265836834010752231943569188608722532 // Issuer: /CN=AddTrust External CA Root/OU=AddTrust External TTP Network/O=AddTrust AB/C=SE // Subject: /CN=UTN - DATACorp SGC/OU=http:\x2f\x2fwww.usertrust.com/O=The USERTRUST Network/L=Salt Lake City/ST=UT/C=US // validity: 2005-06-07 08:09:10 through 2020-05-30 10:48:38 // key type: rsa2048 // key usage: certSign crlSign // ext key usage: ms-serverGatedCrypto (suggested), serverGatedCrypto.ns (suggested) // policies: 2.5.29.32.0:N: // chain length: unlimited // sha1 fpr: 9E:99:81:7D:12:28:0C:96:77:67:44:30:49:2E:DA:1D:CE:2E:4C:63 // sha2 fpr: 21:3F:AD:03:B1:C5:23:47:E9:A8:0F:29:9A:F0:89:9B:CA:FF:3F:62:B3:4E:B0:60:66:F4:D7:EE:A5:EE:1A:73 Key testKey(const char *pattern, Protocol protocol = UnknownProtocol) { const std::vector keys = KeyCache::instance()->findByEMailAddress(pattern); for (const auto &key : keys) { if (protocol == UnknownProtocol || key.protocol() == protocol) { return key; } } const auto key = KeyCache::instance()->findByKeyIDOrFingerprint(pattern); if (key.isNull()) { qWarning() << "No" << Formatting::displayName(protocol) << "test key found for" << pattern; } return key; } private: QSharedPointer mGnupgHome; std::shared_ptr mKeyCache; }; QTEST_MAIN(ExpiryCheckerTest) #include "expirycheckertest.moc" diff --git a/autotests/testhelpers.h b/autotests/testhelpers.h new file mode 100644 index 000000000..9f2292b8d --- /dev/null +++ b/autotests/testhelpers.h @@ -0,0 +1,52 @@ +/* + This file is part of libkleopatra's test suite. + SPDX-FileCopyrightText: 2023 g10 Code GmbH + SPDX-FileContributor: Ingo Klöcker + + SPDX-License-Identifier: LGPL-2.0-or-later +*/ + +#pragma once + +#include +#include + +#include + +#include + +namespace QTest +{ + +inline bool qCompare(const GpgME::Key &key1, const GpgME::Key &key2, const char *actual, const char *expected, const char *file, int line) +{ + return qCompare(key1.primaryFingerprint(), key2.primaryFingerprint(), actual, expected, file, line); +} + +template<> +inline char *toString(const Kleo::chrono::days &days) +{ + return QTest::toString(days.count() == 1 ? QByteArray{"1 day"} : QByteArray::number(qlonglong{days.count()}) + " days"); +} + +// helpers to compare CheckFlags with CheckFlag; the generic template falls back to integer comparison which doesn't give nice value output +inline bool qCompare(const Kleo::ExpiryChecker::CheckFlags &t1, // + Kleo::ExpiryChecker::CheckFlag t2, + const char *actual, + const char *expected, + const char *file, + int line) +{ + return qCompare(t1, Kleo::ExpiryChecker::CheckFlags{t2}, actual, expected, file, line); +} +inline bool qCompare(Kleo::ExpiryChecker::CheckFlag t1, // + const Kleo::ExpiryChecker::CheckFlags &t2, + const char *actual, + const char *expected, + const char *file, + int line) +{ + return qCompare(Kleo::ExpiryChecker::CheckFlags{t1}, t2, actual, expected, file, line); +} + +} diff --git a/src/kleo/expirychecker.cpp b/src/kleo/expirychecker.cpp index 22aea440d..781751b22 100644 --- a/src/kleo/expirychecker.cpp +++ b/src/kleo/expirychecker.cpp @@ -1,454 +1,452 @@ /* This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB SPDX-FileCopyrightText: 2021 Sandro Knauß SPDX-FileCopyrightText: 2023 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker Based on kpgp.h Copyright (C) 2001,2002 the KPGP authors See file libkdenetwork/AUTHORS.kpgp for details SPDX-License-Identifier: LGPL-2.0-or-later */ #include "expirychecker.h" #include "debug.h" #include "dn.h" #include "expirycheckersettings.h" #include #include #include #include #include #include #include #include #include #include using namespace Kleo; -namespace -{ -struct Expiration { - enum Status { - NotNearExpiry, - ExpiresSoon, - Expired, - Expires = ExpiresSoon, // alias for temporary status - } status; - // duration is full days until expiry if status is Expires, - // full days since expiry if status is Expired, - // undefined if status is NotNearExpiry - Kleo::chrono::days duration; -}; -} - class Kleo::ExpiryCheckerPrivate { Kleo::ExpiryChecker *q; public: ExpiryCheckerPrivate(ExpiryChecker *qq, const ExpiryCheckerSettings &settings_) : q{qq} , settings{settings_} { } - Expiration calculateExpiration(const GpgME::Subkey &key) const; - Expiration checkForExpiration(const GpgME::Key &key, Kleo::chrono::days threshold) const; + ExpiryChecker::Expiration calculateExpiration(const GpgME::Key &key) const; + ExpiryChecker::Expiration checkForExpiration(const GpgME::Key &key, Kleo::chrono::days threshold) const; - void checkKeyNearExpiry(const GpgME::Key &key, ExpiryChecker::CheckFlags flags); + ExpiryChecker::Result checkKeyNearExpiry(const GpgME::Key &key, ExpiryChecker::CheckFlags flags); ExpiryCheckerSettings settings; std::set alreadyWarnedFingerprints; std::shared_ptr timeProvider; }; ExpiryChecker::ExpiryChecker(const ExpiryCheckerSettings &settings) : d{new ExpiryCheckerPrivate{this, settings}} { } ExpiryChecker::~ExpiryChecker() = default; ExpiryCheckerSettings ExpiryChecker::settings() const { return d->settings; } -QString formatOpenPGPMessage(const GpgME::Key &key, Expiration expiration, ExpiryChecker::CheckFlags flags) +QString formatOpenPGPMessage(ExpiryChecker::Expiration expiration, ExpiryChecker::CheckFlags flags) { + const GpgME::Key key = expiration.certificate; const bool isOwnKey = flags & ExpiryChecker::OwnKey; const bool isSigningKey = flags & ExpiryChecker::SigningKey; const auto keyInfo = ki18nc("User ID of key (KeyID key ID of key in hex notation)", "%1 (KeyID 0x%2)") .subs(QString::fromUtf8(key.userID(0).id())) .subs(QString::fromLatin1(key.keyID())); - if (expiration.status == Expiration::Expired) { + if (expiration.status == ExpiryChecker::Expired) { qCDebug(LIBKLEO_LOG) << "Key" << key << "expired" << expiration.duration.count() << "days ago"; if (expiration.duration.count() == 0) { KLocalizedString msg; if (isSigningKey) { msg = ki18n("

Your OpenPGP signing key

%1

expired less than a day ago.

"); } else if (isOwnKey) { msg = ki18n("

Your OpenPGP encryption key

%1

expired less than a day ago.

"); } else { msg = ki18n("

The OpenPGP key for

%1

expired less than a day ago.

"); } return msg.subs(keyInfo).toString(); } KLocalizedString msg; if (isSigningKey) { msg = ki18np( "

Your OpenPGP signing key

%2

" "

expired one day ago.

", "

Your OpenPGP signing key

%2

" "

expired %1 days ago.

"); } else if (isOwnKey) { msg = ki18np( "

Your OpenPGP encryption key

%2

" "

expired one day ago.

", "

Your OpenPGP encryption key

%2

" "

expired %1 days ago.

"); } else { msg = ki18np( "

The OpenPGP key for

%2

" "

expired one day ago.

", "

The OpenPGP key for

%2

" "

expired %1 days ago.

"); } return msg.subs(expiration.duration.count()).subs(keyInfo).toString(); } qCDebug(LIBKLEO_LOG) << "Key" << key << "expires in less than" << expiration.duration.count() + 1 << "days"; KLocalizedString msg; if (isSigningKey) { msg = ki18np( "

Your OpenPGP signing key

%2

" "

expires in less than a day.

", "

Your OpenPGP signing key

%2

" "

expires in less than %1 days.

"); } else if (isOwnKey) { msg = ki18np( "

Your OpenPGP encryption key

%2

" "

expires in less than a day.

", "

Your OpenPGP encryption key

%2

" "

expires in less than %1 days.

"); } else { msg = ki18np( "

The OpenPGP key for

%2

" "

expires in less than a day.

", "

The OpenPGP key for

%2

" "

expires in less than %1 days.

"); } return msg.subs(expiration.duration.count() + 1).subs(keyInfo).toString(); } -QString formatSMIMEMessage(const GpgME::Key &key, const GpgME::Key &orig_key, Expiration expiration, ExpiryChecker::CheckFlags flags, bool ca) +QString formatSMIMEMessage(const GpgME::Key &orig_key, ExpiryChecker::Expiration expiration, ExpiryChecker::CheckFlags flags, bool ca) { + const GpgME::Key key = expiration.certificate; const bool isOwnKey = flags & ExpiryChecker::OwnKey; const bool isSigningKey = flags & ExpiryChecker::SigningKey; const auto userCert = orig_key.isNull() ? key : orig_key; const auto userCertInfo = ki18nc("User ID of certificate (serial number serial no. of certificate)", "%1 (serial number %2)") .subs(Kleo::DN(userCert.userID(0).id()).prettyDN()) .subs(QString::fromLatin1(userCert.issuerSerial())); - if (expiration.status == Expiration::Expired) { + if (expiration.status == ExpiryChecker::Expired) { qCDebug(LIBKLEO_LOG) << "Certificate" << key << "expired" << expiration.duration.count() << "days ago"; if (ca) { if (key.isRoot()) { if (expiration.duration.count() == 0) { KLocalizedString msg; if (isSigningKey) { msg = ki18n( "

The root certificate

%2

" "

for your S/MIME signing certificate

%1

" "

expired less than a day ago.

"); } else if (isOwnKey) { msg = ki18n( "

The root certificate

%2

" "

for your S/MIME encryption certificate

%1

" "

expired less than a day ago.

"); } else { msg = ki18n( "

The root certificate

%2

" "

for S/MIME certificate

%1

" "

expired less than a day ago.

"); } return msg.subs(userCertInfo).subs(Kleo::DN(key.userID(0).id()).prettyDN()).toString(); } KLocalizedString msg; if (isSigningKey) { msg = ki18np( "

The root certificate

%3

" "

for your S/MIME signing certificate

%2

" "

expired one day ago.

", "

The root certificate

%3

" "

for your S/MIME signing certificate

%2

" "

expired %1 days ago.

"); } else if (isOwnKey) { msg = ki18np( "

The root certificate

%3

" "

for your S/MIME encryption certificate

%2

" "

expired one day ago.

", "

The root certificate

%3

" "

for your S/MIME encryption certificate

%2

" "

expired %1 days ago.

"); } else { msg = ki18np( "

The root certificate

%3

" "

for S/MIME certificate

%2

" "

expired one day ago.

", "

The root certificate

%3

" "

for S/MIME certificate

%2

" "

expired %1 days ago.

"); } return msg.subs(expiration.duration.count()).subs(userCertInfo).subs(Kleo::DN(key.userID(0).id()).prettyDN()).toString(); } else { if (expiration.duration.count() == 0) { KLocalizedString msg; if (isSigningKey) { msg = ki18n( "

The intermediate CA certificate

%2

" "

for your S/MIME signing certificate

%1

" "

expired less than a day ago.

"); } else if (isOwnKey) { msg = ki18n( "

The intermediate CA certificate

%2

" "

for your S/MIME encryption certificate

%1

" "

expired less than a day ago.

"); } else { msg = ki18n( "

The intermediate CA certificate

%2

" "

for S/MIME certificate

%1

" "

expired less than a day ago.

"); } return msg.subs(userCertInfo).subs(Kleo::DN(key.userID(0).id()).prettyDN()).toString(); } KLocalizedString msg; if (isSigningKey) { msg = ki18np( "

The intermediate CA certificate

%3

" "

for your S/MIME signing certificate

%2

" "

expired one day ago.

", "

The intermediate CA certificate

%3

" "

for your S/MIME signing certificate

%2

" "

expired %1 days ago.

"); } else if (isOwnKey) { msg = ki18np( "

The intermediate CA certificate

%3

" "

for your S/MIME encryption certificate

%2

" "

expired one day ago.

", "

The intermediate CA certificate

%3

" "

for your S/MIME encryption certificate

%2

" "

expired %1 days ago.

"); } else { msg = ki18np( "

The intermediate CA certificate

%3

" "

for S/MIME certificate

%2

" "

expired one day ago.

", "

The intermediate CA certificate

%3

" "

for S/MIME certificate

%2

" "

expired %1 days ago.

"); } return msg.subs(expiration.duration.count()).subs(userCertInfo).subs(Kleo::DN(key.userID(0).id()).prettyDN()).toString(); } } else { if (expiration.duration.count() == 0) { KLocalizedString msg; if (isSigningKey) { msg = ki18n("

Your S/MIME signing certificate

%1

expired less than a day ago.

"); } else if (isOwnKey) { msg = ki18n("

Your S/MIME encryption certificate

%1

expired less than a day ago.

"); } else { msg = ki18n("

The S/MIME certificate for

%1

expired less than a day ago.

"); } return msg.subs(userCertInfo).toString(); } KLocalizedString msg; if (isSigningKey) { msg = ki18np( "

Your S/MIME signing certificate

%2

" "

expired one day ago.

", "

Your S/MIME signing certificate

%2

" "

expired %1 days ago.

"); } else if (isOwnKey) { msg = ki18np( "

Your S/MIME encryption certificate

%2

" "

expired one day ago.

", "

Your S/MIME encryption certificate

%2

" "

expired %1 days ago.

"); } else { msg = ki18np( "

The S/MIME certificate for

%2

" "

expired one day ago.

", "

The S/MIME certificate for

%2

" "

expired %1 days ago.

"); } return msg.subs(expiration.duration.count()).subs(userCertInfo).toString(); } } qCDebug(LIBKLEO_LOG) << "Certificate" << key << "expires in less than" << expiration.duration.count() + 1 << "days"; KLocalizedString msg; if (ca) { if (key.isRoot()) { if (isSigningKey) { msg = ki18np( "

The root certificate

%3

" "

for your S/MIME signing certificate

%2

" "

expires in less than a day.

", "

The root certificate

%3

" "

for your S/MIME signing certificate

%2

" "

expires in less than %1 days.

"); } else if (isOwnKey) { msg = ki18np( "

The root certificate

%3

" "

for your S/MIME encryption certificate

%2

" "

expires in less than a day.

", "

The root certificate

%3

" "

for your S/MIME encryption certificate

%2

" "

expires in less than %1 days.

"); } else { msg = ki18np( "

The root certificate

%3

" "

for S/MIME certificate

%2

" "

expires in less than a day.

", "

The root certificate

%3

" "

for S/MIME certificate

%2

" "

expires in less than %1 days.

"); } } else { if (isSigningKey) { msg = ki18np( "

The intermediate CA certificate

%3

" "

for your S/MIME signing certificate

%2

" "

expires in less than a day.

", "

The intermediate CA certificate

%3

" "

for your S/MIME signing certificate

%2

" "

expires in less than %1 days.

"); } else if (isOwnKey) { msg = ki18np( "

The intermediate CA certificate

%3

" "

for your S/MIME encryption certificate

%2

" "

expires in less than a day.

", "

The intermediate CA certificate

%3

" "

for your S/MIME encryption certificate

%2

" "

expires in less than %1 days.

"); } else { msg = ki18np( "

The intermediate CA certificate

%3

" "

for S/MIME certificate

%2

" "

expires in less than a day.

", "

The intermediate CA certificate

%3

" "

for S/MIME certificate

%2

" "

expires in less than %1 days.

"); } } return msg.subs(expiration.duration.count() + 1).subs(userCertInfo).subs(Kleo::DN(key.userID(0).id()).prettyDN()).toString(); } if (isSigningKey) { msg = ki18np( "

Your S/MIME signing certificate

%2

" "

expires in less than a day.

", "

Your S/MIME signing certificate

%2

" "

expires in less than %1 days.

"); } else if (isOwnKey) { msg = ki18np( "

Your S/MIME encryption certificate

%2

" "

expires in less than a day.

", "

Your S/MIME encryption certificate

%2

" "

expires in less than %1 days.

"); } else { msg = ki18np( "

The S/MIME certificate for

%2

" "

expires in less than a day.

", "

The S/MIME certificate for

%2

" "

expires in less than %1 days.

"); } return msg.subs(expiration.duration.count() + 1).subs(userCertInfo).toString(); } -Expiration ExpiryCheckerPrivate::calculateExpiration(const GpgME::Subkey &subkey) const +ExpiryChecker::Expiration ExpiryCheckerPrivate::calculateExpiration(const GpgME::Key &key) const { + const GpgME::Subkey subkey = key.subkey(0); if (subkey.neverExpires()) { - return {Expiration::NotNearExpiry, Kleo::chrono::days::zero()}; + return {key, ExpiryChecker::NotNearExpiry, Kleo::chrono::days::zero()}; } const time_t t = timeProvider ? timeProvider->getTime() : std::time(nullptr); // casting the double-valued difference (returned by std::difftime) of two non-negative time_t to a time_t is no problem; // negative values for expiration time and current time can be safely ignored const time_t secsTillExpiry = static_cast(std::difftime(subkey.expirationTime(), t)); - return {secsTillExpiry <= 0 ? Expiration::Expired : Expiration::Expires, + return {key, + secsTillExpiry <= 0 ? ExpiryChecker::Expired : ExpiryChecker::Expires, std::chrono::duration_cast(std::chrono::seconds{std::abs(secsTillExpiry)})}; } -Expiration ExpiryCheckerPrivate::checkForExpiration(const GpgME::Key &key, Kleo::chrono::days threshold) const +ExpiryChecker::Expiration ExpiryCheckerPrivate::checkForExpiration(const GpgME::Key &key, Kleo::chrono::days threshold) const { - Expiration expiration = calculateExpiration(key.subkey(0)); - if ((expiration.status == Expiration::Expires) && (expiration.duration > threshold)) { + ExpiryChecker::Expiration expiration = calculateExpiration(key); + if ((expiration.status == ExpiryChecker::Expires) && (expiration.duration > threshold)) { // key expires, but not too soon - expiration.status = Expiration::NotNearExpiry; + expiration.status = ExpiryChecker::NotNearExpiry; } return expiration; } -void ExpiryCheckerPrivate::checkKeyNearExpiry(const GpgME::Key &orig_key, ExpiryChecker::CheckFlags flags) +ExpiryChecker::Result ExpiryCheckerPrivate::checkKeyNearExpiry(const GpgME::Key &orig_key, ExpiryChecker::CheckFlags flags) { static const int maximumCertificateChainLength = 100; const bool isOwnKey = flags & ExpiryChecker::OwnKey; + ExpiryChecker::Result result; + result.checkFlags = flags; + result.expiration.certificate = orig_key; + // use vector instead of set because certificate chains are usually very short std::vector checkedCertificates; auto key = orig_key; for (int chainCount = 0; chainCount < maximumCertificateChainLength; ++chainCount) { checkedCertificates.push_back(key.primaryFingerprint()); const GpgME::Subkey subkey = key.subkey(0); const bool newMessage = !alreadyWarnedFingerprints.count(subkey.fingerprint()); const auto threshold = chainCount > 0 // ? (key.isRoot() ? settings.rootCertThreshold() : settings.chainCertThreshold()) // : (isOwnKey ? settings.ownKeyThreshold() : settings.otherKeyThreshold()); const auto expiration = checkForExpiration(key, threshold); - if (expiration.status == Expiration::Expired) { + if (chainCount == 0) { + result.expiration = expiration; + } else if (expiration.status != ExpiryChecker::NotNearExpiry) { + result.chainExpiration.push_back(expiration); + } + if (expiration.status == ExpiryChecker::Expired) { const QString msg = key.protocol() == GpgME::OpenPGP // - ? formatOpenPGPMessage(key, expiration, flags) - : formatSMIMEMessage(key, orig_key, expiration, flags, chainCount > 0); + ? formatOpenPGPMessage(expiration, flags) + : formatSMIMEMessage(orig_key, expiration, flags, chainCount > 0); alreadyWarnedFingerprints.insert(subkey.fingerprint()); Q_EMIT q->expiryMessage(key, msg, isOwnKey ? ExpiryChecker::OwnKeyExpired : ExpiryChecker::OtherKeyExpired, newMessage); - } else if (expiration.status == Expiration::ExpiresSoon) { + } else if (expiration.status == ExpiryChecker::ExpiresSoon) { const QString msg = key.protocol() == GpgME::OpenPGP // - ? formatOpenPGPMessage(key, expiration, flags) - : formatSMIMEMessage(key, orig_key, expiration, flags, chainCount > 0); + ? formatOpenPGPMessage(expiration, flags) + : formatSMIMEMessage(orig_key, expiration, flags, chainCount > 0); alreadyWarnedFingerprints.insert(subkey.fingerprint()); Q_EMIT q->expiryMessage(key, msg, isOwnKey ? ExpiryChecker::OwnKeyNearExpiry : ExpiryChecker::OtherKeyNearExpiry, newMessage); } if (!(flags & ExpiryChecker::CheckChain) || key.isRoot() || (key.protocol() != GpgME::CMS)) { break; } const auto keys = KeyCache::instance()->findIssuers(key, KeyCache::NoOption); if (keys.empty()) { break; } key = keys.front(); if (Kleo::contains(checkedCertificates, key.primaryFingerprint())) { break; // this certificate was already checked (looks like a circle in the chain) } } + return result; } -void ExpiryChecker::checkKey(const GpgME::Key &key, CheckFlags flags) const +ExpiryChecker::Result ExpiryChecker::checkKey(const GpgME::Key &key, CheckFlags flags) const { - d->checkKeyNearExpiry(key, flags); + return d->checkKeyNearExpiry(key, flags); } void ExpiryChecker::setTimeProviderForTest(const std::shared_ptr &timeProvider) { d->timeProvider = timeProvider; } diff --git a/src/kleo/expirychecker.h b/src/kleo/expirychecker.h index 1046467b0..1e1680428 100644 --- a/src/kleo/expirychecker.h +++ b/src/kleo/expirychecker.h @@ -1,85 +1,110 @@ /* This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2004 Klarälvdalens Datakonsult AB SPDX-FileCopyrightText: 2021 Sandro Knauß SPDX-FileCopyrightText: 2023 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker Based on kpgp.h Copyright (C) 2001,2002 the KPGP authors See file libkdenetwork/AUTHORS.kpgp for details SPDX-License-Identifier: LGPL-2.0-or-later */ #pragma once #include "kleo_export.h" #include #include #include #include namespace Kleo { class ExpiryCheckerPrivate; class ExpiryCheckerSettings; class KLEO_EXPORT TimeProvider { public: virtual ~TimeProvider() = default; virtual time_t getTime() const = 0; }; class KLEO_EXPORT ExpiryChecker : public QObject { Q_OBJECT public: enum CheckFlag { NoCheckFlags = 0, OwnKey = 1, OwnEncryptionKey = OwnKey, SigningKey = 2, OwnSigningKey = OwnKey | SigningKey, CheckChain = 4, }; + Q_FLAG(CheckFlag) Q_DECLARE_FLAGS(CheckFlags, CheckFlag) + Q_FLAG(CheckFlags) + + enum ExpirationStatus { + NotNearExpiry, + ExpiresSoon, + Expired, + Expires = ExpiresSoon, // alias used internally + }; + Q_ENUM(ExpirationStatus) + + struct Expiration { + GpgME::Key certificate; + ExpirationStatus status; + // duration is full days until expiry if status is Expires, + // full days since expiry if status is Expired, + // undefined if status is NotNearExpiry + Kleo::chrono::days duration; + }; + + struct Result { + CheckFlags checkFlags; + Expiration expiration; // result for the checked certificate + std::vector chainExpiration; // results for expired or soon expiring chain certificates + }; explicit ExpiryChecker(const ExpiryCheckerSettings &settings); ~ExpiryChecker() override; Q_REQUIRED_RESULT ExpiryCheckerSettings settings() const; enum ExpiryInformation { OwnKeyExpired, OwnKeyNearExpiry, OtherKeyExpired, OtherKeyNearExpiry, }; Q_ENUM(ExpiryInformation) - void checkKey(const GpgME::Key &key, CheckFlags flags) const; + Result checkKey(const GpgME::Key &key, CheckFlags flags) const; Q_SIGNALS: void expiryMessage(const GpgME::Key &key, QString msg, Kleo::ExpiryChecker::ExpiryInformation info, bool isNewMessage) const; public: void setTimeProviderForTest(const std::shared_ptr &); private: std::unique_ptr const d; }; Q_DECLARE_OPERATORS_FOR_FLAGS(ExpiryChecker::CheckFlags) } Q_DECLARE_METATYPE(Kleo::ExpiryChecker::CheckFlags) Q_DECLARE_METATYPE(Kleo::ExpiryChecker::ExpiryInformation) Q_DECLARE_METATYPE(GpgME::Key) diff --git a/src/utils/chrono.h b/src/utils/chrono.h index 0e29c8dee..cb5e43c54 100644 --- a/src/utils/chrono.h +++ b/src/utils/chrono.h @@ -1,19 +1,23 @@ /* utils/chrono.h This file is part of libkleopatra, the KDE keymanagement library SPDX-FileCopyrightText: 2023 g10 Code GmbH SPDX-FileContributor: Ingo Klöcker SPDX-License-Identifier: GPL-2.0-or-later */ #pragma once +#include + #include namespace Kleo::chrono { // typedef for duration in days (defined in C++20) using days = std::chrono::duration>; } + +Q_DECLARE_METATYPE(Kleo::chrono::days)