// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // KOutNet - The keys that seal group traffic #pragma once #include #include #include namespace koutnet { // Everything about who-gets-which-key for group chat, lifted out of // NetworkManager: the shared passphrase, the per-group keys, and the rule for // picking which one seals a conversation. The store behind it (SecretStore) // is durable, and the in-memory cache means a group chat does not hit it on // every decrypted message. // // The rule the sealing paths share: a group that was given its own key uses // it, everything else falls back to the shared passphrase, and no key at all // means the conversation is not sent (it would go out in the clear). A group // that predates per-group keys keeps working on the passphrase, and both // directions fall back the same way. class GroupKeyStore : public QObject { Q_OBJECT public: explicit GroupKeyStore(QObject *parent = nullptr); // The passphrase everyone in a broadcast shares. Empty means cleartext is // refused rather than risked. void setPassphrase(const QString &passphrase); QString passphrase() const { return m_passphrase; } // The key that seals or opens a protected conversation: the pinned group // key when the group has one, otherwise the shared passphrase, otherwise // empty. Both the encrypt side and the decrypt side resolve to this one // rule, so a message never leaves under one key and opens under another. QString cipherKeyFor(const QString &gid) const; // The pinned key of a group, empty when it was never given one. QString keyFor(const QString &gid) const; // Pin (and persist) a group's key, or clear it with an empty key. void setKey(const QString &gid, const QString &key); void removeKey(const QString &gid); // The key that will seal a group before somebody is invited into it: the // pinned one if there is any, a freshly minted persisted one otherwise. QString ensureKey(const QString &gid); private: static QString storeKey(const QString &gid); QString m_passphrase; // in-memory cache of pinned group keys; the cache is filled lazily inside // the const keyFor(), which is why it is mutable mutable QHash m_groupKeys; }; } // namespace koutnet