// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // The TLS transport is two real sockets on loopback - no packet sniffing, no // fake peers - so the trust model itself is what is under test: a peer is // pinned on first sight, an impostor with a different key for the same id is // refused before any frame is delivered, and the pin outlives a restart of // either side. identityFingerprint() checking determinism keeps the whole // story honest: what pins and compares must be the key, not the certificate // (whose ECDSA signature is freshly random each process). #include #include #include #include #include #include #include "../core/security/IdentityCertificate.h" #include "../network/TlsChannel.h" using koutnet::buildIdentityCertificate; using koutnet::IdentityCertificate; using koutnet::identityFingerprint; using koutnet::TlsChannel; namespace koutnet { class TlsChannelTest : public QObject { Q_OBJECT private Q_SLOTS: void identityCertificateIsDeterministic(); void tlsChannelsAuthenticateEachOtherAndExchangeFrames(); void anImpostorFailsThePinnedIdentity(); void thePinSurvivesARestart(); // a socket that is accepted and then says nothing must not be held: the // channel caps how many it waits on and times the rest out void silentSocketsAreRefusedAndTimedOut(); private: static IdentityCertificate identity(int tag, const QString &id) { QByteArray seed(32, '\0'); for (int i = 0; i < seed.size(); ++i) { seed[i] = char(tag + (i & 0x1F)); } return buildIdentityCertificate(seed, id); } }; void TlsChannelTest::identityCertificateIsDeterministic() { const IdentityCertificate one = identity(11, QStringLiteral("ta_eternal")); const IdentityCertificate two = identity(11, QStringLiteral("ta_eternal")); QVERIFY(one.isValid()); QVERIFY(two.isValid()); QCOMPARE(one.cert.subjectInfo(QSslCertificate::CommonName).value(0, QString()), QStringLiteral("ta_eternal")); // same seed, same serial and same public key across construction runs; the // certificate bytes themselves differ only in the random signature, which // is exactly why the fingerprint is taken off the key. QCOMPARE(one.cert.serialNumber(), two.cert.serialNumber()); QCOMPARE(identityFingerprint(one.cert), identityFingerprint(two.cert)); const IdentityCertificate other = identity(12, QStringLiteral("ta_eternal")); QVERIFY(other.isValid()); QVERIFY(identityFingerprint(other.cert) != identityFingerprint(one.cert)); // a garbage seed shape must not half-build a certificate QVERIFY(!buildIdentityCertificate(QByteArray(16, '\x01'), QStringLiteral("ta_short")).isValid()); } void TlsChannelTest::tlsChannelsAuthenticateEachOtherAndExchangeFrames() { const IdentityCertificate a = identity(21, QStringLiteral("tb_a")); const IdentityCertificate b = identity(22, QStringLiteral("tb_b")); TlsChannel ca(a); TlsChannel cb(b); QVERIFY(ca.listen(0)); QSignalSpy authA(&ca, &TlsChannel::peerAuthenticated); QSignalSpy authB(&cb, &TlsChannel::peerAuthenticated); QSignalSpy rejectA(&ca, &TlsChannel::peerRejected); QVERIFY(cb.connectTo(QStringLiteral("127.0.0.1"), ca.listenPort())); QTRY_VERIFY_WITH_TIMEOUT(authA.count() == 1 && authB.count() == 1, 30000); QVERIFY(rejectA.isEmpty()); QCOMPARE(authA.at(0).at(0).toString(), QStringLiteral("tb_b")); QCOMPARE(authB.at(0).at(0).toString(), QStringLiteral("tb_a")); QVERIFY(ca.hasPeer(QStringLiteral("tb_b"))); QVERIFY(cb.hasPeer(QStringLiteral("tb_a"))); QSignalSpy framesA(&ca, &TlsChannel::frameReceived); QSignalSpy framesB(&cb, &TlsChannel::frameReceived); const QByteArray payload("hello over tls"); ca.sendPeer(QStringLiteral("tb_b"), payload); cb.sendPeer(QStringLiteral("tb_a"), QByteArrayLiteral("back at you")); QTRY_COMPARE_WITH_TIMEOUT(framesA.count(), 1, 15000); QTRY_COMPARE_WITH_TIMEOUT(framesB.count(), 1, 15000); QCOMPARE(framesA.at(0).at(1).toByteArray(), QByteArrayLiteral("back at you")); QCOMPARE(framesB.at(0).at(1).toByteArray(), payload); // a binary frame carrying non-text bytes and a big-ish payload QByteArray blob(30 * 1024, '\0'); for (int i = 0; i < blob.size(); ++i) { blob[i] = char(i & 0xFF); } ca.sendPeer(QStringLiteral("tb_b"), blob); QTRY_COMPARE_WITH_TIMEOUT(framesB.count(), 2, 15000); QCOMPARE(framesB.at(1).at(1).toByteArray(), blob); } void TlsChannelTest::anImpostorFailsThePinnedIdentity() { const IdentityCertificate a = identity(31, QStringLiteral("tc_a")); const IdentityCertificate b = identity(32, QStringLiteral("tc_b")); TlsChannel ca(a); TlsChannel cb(b); QVERIFY(ca.listen(0)); QSignalSpy authA(&ca, &TlsChannel::peerAuthenticated); QSignalSpy authB(&cb, &TlsChannel::peerAuthenticated); QVERIFY(cb.connectTo(QStringLiteral("127.0.0.1"), ca.listenPort())); QTRY_VERIFY_WITH_TIMEOUT(authA.count() == 1 && authB.count() == 1, 30000); // now another key speaks for the same id tc_b const IdentityCertificate impostor = identity(33, QStringLiteral("tc_b")); TlsChannel ci(impostor); QSignalSpy rejectA(&ca, &TlsChannel::peerRejected); QVERIFY(ci.connectTo(QStringLiteral("127.0.0.1"), ca.listenPort())); QTRY_VERIFY_WITH_TIMEOUT(rejectA.count() == 1, 30000); QCOMPARE(rejectA.at(0).at(0).toString(), QStringLiteral("tc_b")); // the pin A holds for tc_b is B's key, and the pretender is the impostor's QCOMPARE(rejectA.at(0).at(2).toByteArray(), identityFingerprint(b.cert)); QCOMPARE(rejectA.at(0).at(3).toByteArray(), identityFingerprint(impostor.cert)); // the impostor never became a peer on A's side and the real one is untouched // (A keeps exactly the one wire that presented B's pinned key) QVERIFY(ca.hasPeer(QStringLiteral("tc_b"))); // and the real wire still carries frames afterwards QSignalSpy framesB(&cb, &TlsChannel::frameReceived); ca.sendPeer(QStringLiteral("tc_b"), QByteArrayLiteral("still here")); QTRY_COMPARE_WITH_TIMEOUT(framesB.count(), 1, 15000); QCOMPARE(framesB.at(0).at(1).toByteArray(), QByteArrayLiteral("still here")); } void TlsChannelTest::thePinSurvivesARestart() { const IdentityCertificate a = identity(41, QStringLiteral("td_restart_a")); const IdentityCertificate b = identity(42, QStringLiteral("td_restart_b")); { TlsChannel ca(a); TlsChannel cb(b); QVERIFY(ca.listen(0)); QSignalSpy authA(&ca, &TlsChannel::peerAuthenticated); QSignalSpy authB(&cb, &TlsChannel::peerAuthenticated); QVERIFY(cb.connectTo(QStringLiteral("127.0.0.1"), ca.listenPort())); QTRY_VERIFY_WITH_TIMEOUT(authA.count() == 1 && authB.count() == 1, 30000); } // both channels are gone; the pins they wrote must carry the trust over to // the next instance of each side. TlsChannel ca2(a); TlsChannel cb2(b); QSignalSpy authA2(&ca2, &TlsChannel::peerAuthenticated); QSignalSpy authB2(&cb2, &TlsChannel::peerAuthenticated); QSignalSpy rejectA2(&ca2, &TlsChannel::peerRejected); QSignalSpy rejectB2(&cb2, &TlsChannel::peerRejected); QVERIFY(ca2.listen(0)); QVERIFY(cb2.connectTo(QStringLiteral("127.0.0.1"), ca2.listenPort())); QTRY_VERIFY_WITH_TIMEOUT(authA2.count() == 1 && authB2.count() == 1, 30000); QVERIFY(rejectA2.isEmpty()); QVERIFY(rejectB2.isEmpty()); QVERIFY(ca2.hasPeer(QStringLiteral("td_restart_b"))); QVERIFY(cb2.hasPeer(QStringLiteral("td_restart_a"))); } void TlsChannelTest::silentSocketsAreRefusedAndTimedOut() { const IdentityCertificate a = identity(51, QStringLiteral("te_silent_a")); TlsChannel ca(a); QVERIFY(ca.listen(0)); const quint16 port = ca.listenPort(); QSignalSpy auth(&ca, &TlsChannel::peerAuthenticated); // Plain TCP sockets that connect and then never write a ClientHello. Each // one is a descriptor the channel holds with nothing to read. const int overTheLimit = TlsChannel::kMaxHandshakingSockets + 8; QList silent; for (int i = 0; i < overTheLimit; ++i) { auto *s = new QTcpSocket; silent.append(s); s->connectToHost(QStringLiteral("127.0.0.1"), port); } // the accepted ones are waiting on a handshake; the rest were refused at the // door, so the number held never climbs past the cap QTRY_VERIFY_WITH_TIMEOUT(ca.handshakingCount() > 0, 5000); QTRY_VERIFY_WITH_TIMEOUT(ca.handshakingCount() <= TlsChannel::kMaxHandshakingSockets, 5000); QCOMPARE(ca.handshakingCount(), TlsChannel::kMaxHandshakingSockets); QVERIFY(auth.isEmpty()); qInfo("handshake timeout is %d ms; not waiting it out in a unit test", TlsChannel::kHandshakeTimeoutMs); QVERIFY(TlsChannel::kHandshakeTimeoutMs > 0); // the refused ones are already gone, and nothing ever authenticated for (QTcpSocket *s : std::as_const(silent)) { s->abort(); s->deleteLater(); } QVERIFY(auth.isEmpty()); ca.stop(); QCOMPARE(ca.handshakingCount(), 0); } } // namespace koutnet int main(int argc, char *argv[]) { // the pin file lives under AppDataLocation; test mode keeps this run away // from any pins a real app session would have written. QStandardPaths::setTestModeEnabled(true); QCoreApplication app(argc, argv); QCoreApplication::setOrganizationName(QStringLiteral("koutnet-tests")); QCoreApplication::setApplicationName(QStringLiteral("tls-channel")); koutnet::TlsChannelTest tc; QTEST_SET_MAIN_SOURCE_PATH return QTest::qExec(&tc, argc, argv); } #include "TlsChannelTest.moc"