// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - The authenticated session planner and pin table #pragma once #include #include #include #include #include #include #include #include #include "TlsChannel.h" #include "core/security/IdentityCertificate.h" namespace koutnet { class CryptoManager; // The authenticated half of the transport, stood up out of NetworkManager: the // TLS channel itself, the station's identity certificate, the pin table (which // address was authenticated by which identity) and the dial-on-advertise book- // keeping. Presence only ever says where a peer listens; this class owns the // pin table which decides whether a dial may happen, and it is the only place // an unsigned address runs into a certificate. // // It owns no peer-table seats beyond its own pin map: the address an identity // is filed under in the observed table (so a frame can be stamped from_ip) is // the presence service's view, reached through the seat-lookup seam. Anything // that has nothing to do with the TLS plane - group keys, call buckets, the // group-key store - stays out; an incoming frame is decoded, seated and handed // to the owner, which routes it. class SessionTransport : public QObject { Q_OBJECT public: explicit SessionTransport(QObject *parent = nullptr); // Build the identity certificate from the crypto seed and bring the channel // up on the standard port (falling back to an ephemeral one). Without a // valid crypto manager nothing is built and the transport stays down. void start(const CryptoManager *crypto); void stop(); bool active() const; // the channel exists and is listening quint16 listenPort() const; // 0 while down // the station's own identity certificate, kept so the voice transport can // wrap its sockets in the same TLS identity the chat channel presents. const IdentityCertificate &identity() const; // Headers the pin table answers: whether the identity is pinned, the SPKI // fingerprint it is pinned to, and which address it authenticated from. bool hasPeer(const QString &peerId) const; QString pinnedFingerprint(const QString &peerId) const; QString identityForAddress(const QString &address) const; // a copy of the whole pin table, for the owner's address queries. QHash seats() const; // Forget a pinned identity so the next contact is trusted on first use // again. Returns true when a pin was removed. bool forgetPin(const QString &peerId); QString pinFile() const; // The voice transport authenticates a peer with the same identity the chat // channel uses; it files the seat here, the one map routing trusts. void seatFromVoice(const QString &address, const QString &peerId); // the address a frame off the channel gets stamped with, from the observed // table, not from the pin: a socket for an identity presence never filed is // kept out of the seating logic by the owner's lookup returning empty. void setSeatLookup(std::function lookup); // Whether messaging to this address can ride the channel: a live channel to // the identity that address was authenticated by. One hash lookup per send. bool canUse(const QString &targetAddress) const; // place a protocol frame on the channel to the identity this address // belongs to; silently nothing when nobody is there. bool sendFrame(const QString &targetAddress, const QJsonObject &payload); // the binary variant keeps byte-string fields (file_data chunks) as bytes // instead of forcing them through JSON's base64 detour. bool sendFrame(const QString &targetAddress, const QCborMap &payload); // send straight to a pinned identity (avatar writer path). bool sendPeerFrame(const QString &peerId, const QJsonObject &payload); // a peer advertises where it listens; dial unless an identity is already // pinned, a dial for that address is already in flight, or the channel is // down, and dedup so a few seconds' presence cycle does not pile up dials. void dial(const QString &peerId, const QString &address, quint16 port); Q_SIGNALS: // a certificate passed the pin gate. The address is already normalized. void peerAuthenticated(QString peerId, QString address); void peerRejected(QString peerId, QString address, QByteArray expectedPin, QByteArray actualFingerprint); // A length-prefixed protocol frame off an authenticated TLS channel, seated // (from_id and from_ip stamped) and peeled into its routing lane: the // file/avatar lanes carry their reassembly data, everything else a plain // decoded frame the owner routes. void frame(QString host, QJsonObject msg); void fileChunk(QString host, QString tid, int idx, int total, QByteArray chunk); void fileAck(QString tid); void avatarFrame(QString host, QJsonObject msg); // the banner lane, AvatarExchange's sibling: bytes the owner hands to // BannerExchange, kept off the avatar lane so a receiver without banners // cannot mistake one for a picture. void bannerFrame(QString host, QJsonObject msg); private Q_SLOTS: void onPeerAuthenticated(const QString &peerId, const QString &address); void onPeerRejected(const QString &peerId, const QString &address, const QByteArray &expectedPin, const QByteArray &actualFingerprint); void onFrame(const QString &peerId, const QByteArray &rawFrame); void onDialFailure(const QString &address); private: TlsChannel *m_tls = nullptr; IdentityCertificate m_ident; quint16 m_listenPort = 0; // address -> identity the TLS channel *authenticated* from that address. // The only map messaging routing trusts: it is filled from the pinned // certificate, never from anything a packet claimed, and never from the // presence plane (its view is the seat-lookup seam). QHash m_seats; // addresses we already dialed and are waiting on to authenticate; a peer's // presence cycles every few seconds and must not pile up dials meanwhile. QSet m_dialing; std::function m_seatLookup; }; } // namespace koutnet