// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // The TLS half of the LAN/VPN transport. Every connection is a QSslSocket // (TLS 1.3) carrying length-prefixed frames; the peer is identified by the // CN of its self-signed certificate - the KOutNet identity id - and trusted // on first use: the certificate's public-key fingerprint is pinned to the id // the first time it is seen, and every later presentation of that id must // show the same fingerprint or the connection is refused before a byte of it // is delivered. The pin store is one file under AppDataLocation, so a // restart of either side does not shake the trust, while an impostor's // different key is caught on the spot. // // The local certificate comes from buildIdentityCertificate(): a P-256 // keypair deterministically derived from the app's identity seed, so nothing // new has to be stored for this to work. #pragma once #include #include #include #include #include #include #include #include #include #include #include "core/security/IdentityCertificate.h" class QSslSocket; namespace koutnet { class TlsChannel : public QTcpServer { Q_OBJECT public: // A socket that has been accepted but has not finished the handshake is a // descriptor the channel is holding and nothing is reading from it. Both // numbers below exist so that a peer opening connections and then going // quiet cannot sit on them: the timer closes the ones that never finish, // and the count refuses the ones past the limit. On a LAN this is nuisance // rather than an attack, but the listener is on every interface. static constexpr int kMaxHandshakingSockets = 32; static constexpr int kHandshakeTimeoutMs = 15'000; // identity is copied in; the certificate, its DER and the key keep the // same lifetime inside this channel. explicit TlsChannel(const IdentityCertificate &identity, QObject *parent = nullptr); ~TlsChannel() override; // any local interface, like the UDP side of the transport. 0 picks an // ephemeral port, read it back with listenPort(). bool listen(quint16 port); quint16 listenPort() const { return m_listenPort; } // dial a peer. The outcome arrives as peerAuthenticated(), peerRejected() // or connectionFailed(). bool connectTo(const QString &host, quint16 port); void sendPeer(const QString &peerId, const QByteArray &frame); void dropPeer(const QString &peerId); bool hasPeer(const QString &peerId) const; // The SPKI fingerprint pinned to this identity, 64 hex characters, or empty // when the id has never passed the gate. This is the value the user reads // aloud: the same one the TOFU check compares on every reconnect, so a // changed key shows up as a different string. QString pinnedFingerprint(const QString &peerId) const; // Forget a pinned identity so the next contact is trusted on first use // again. Any live connection for it is dropped. Returns true when a pin was // removed. bool forgetPin(const QString &peerId); // how many accepted sockets are still waiting on a handshake. int handshakingCount() const { return m_handshaking.size(); } // close everything, forget nothing (pins stay on disk) void stop(); QString pinFile() const; Q_SIGNALS: // a peer passed the TOFU gate. address is where it actually came from. void peerAuthenticated(const QString &peerId, const QString &address); // a peer presented the identity with a different key: an impostor, or a // reinstall. Nothing was read off that connection. fingerprint is the // using key, expectedPin the one pinned to the id. void peerRejected(const QString &peerId, const QString &address, const QByteArray &expectedPin, const QByteArray &fingerprint); void peerDisconnected(const QString &peerId); // one length-prefixed frame, delivered in order, never interleaved. void frameReceived(const QString &peerId, const QByteArray &frame); void connectionFailed(const QString &host, quint16 port); protected: void incomingConnection(qintptr handle) override; private Q_SLOTS: void onEncrypted(QSslSocket *socket); void onPeerData(QSslSocket *socket); void onPeerClosed(QSslSocket *socket); void onError(QSslSocket *socket); private: // The pin file is state of this channel, not a file to re-read and re-parse // for every handshake. Defined in the .cpp so the store stays opaque here. class TofuPins; void wireSocket(QSslSocket *socket); void adopt(const QString &peerId, QSslSocket *socket); void dropSocket(QSslSocket *socket, bool notify); void startHandshakeClock(QSslSocket *socket); void cancelHandshakeClock(QSslSocket *socket); IdentityCertificate m_identity; QSslConfiguration m_cfg; quint16 m_listenPort = 0; // peerId -> wire state. Replacing a live peer atomically is the reconnect // rule: newest seat wins, the old one is dropped. struct PeerLink { QSslSocket *socket = nullptr; QString address; QByteArray rx; }; QHash m_peers; QHash m_socketIds; QList m_clients; // client-mode sockets before they are adopted // accepted sockets that have not reached "encrypted" yet, each with the // timer that closes it if it never does QHash m_handshaking; std::unique_ptr m_pins; }; } // namespace koutnet