diff options
| author | Melvin Keskin <melvo@olomono.de> | 2022-04-19 20:05:16 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2022-05-06 13:36:43 +0200 |
| commit | 4c565dd7dd78340a393ccbf4bc3bed9f6ce360f2 (patch) | |
| tree | bc8f94f355a90b3c5b0380993ed13fd5d2e02032 /src/client | |
| parent | 2000431680f4ddf58f3fbe9ea138dd18bc9a1b8c (diff) | |
| download | qxmpp-4c565dd7dd78340a393ccbf4bc3bed9f6ce360f2.tar.gz | |
Add QXmppTrustManager as base of trust managers such as QXmppAtmManager
Diffstat (limited to 'src/client')
| -rw-r--r-- | src/client/QXmppAtmManager.cpp | 23 | ||||
| -rw-r--r-- | src/client/QXmppAtmManager.h | 9 | ||||
| -rw-r--r-- | src/client/QXmppTrustManager.cpp | 282 | ||||
| -rw-r--r-- | src/client/QXmppTrustManager.h | 59 | ||||
| -rw-r--r-- | src/client/QXmppTrustMemoryStorage.cpp | 26 | ||||
| -rw-r--r-- | src/client/QXmppTrustMemoryStorage.h | 4 | ||||
| -rw-r--r-- | src/client/QXmppTrustStorage.cpp | 11 | ||||
| -rw-r--r-- | src/client/QXmppTrustStorage.h | 4 |
8 files changed, 388 insertions, 30 deletions
diff --git a/src/client/QXmppAtmManager.cpp b/src/client/QXmppAtmManager.cpp index ca5b5f9e..b7ea5660 100644 --- a/src/client/QXmppAtmManager.cpp +++ b/src/client/QXmppAtmManager.cpp @@ -61,8 +61,8 @@ using namespace QXmpp::Private; /// \param trustStorage trust storage implementation /// QXmppAtmManager::QXmppAtmManager(QXmppAtmTrustStorage *trustStorage) + : QXmppTrustManager(trustStorage) { - m_trustStorage = trustStorage; } /// @@ -79,7 +79,7 @@ QFuture<void> QXmppAtmManager::makeTrustDecisions(const QString &encryption, con { QFutureInterface<void> interface(QFutureInterfaceBase::Started); - auto future = m_trustStorage->keys(encryption, QXmppTrustStorage::Authenticated | QXmppTrustStorage::ManuallyDistrusted); + auto future = keys(encryption, QXmppTrustStorage::Authenticated | QXmppTrustStorage::ManuallyDistrusted); await(future, this, [=](const QHash<QXmppTrustStorage::TrustLevel, QMultiHash<QString, QByteArray>> &&keys) mutable { const auto authenticatedKeys = keys.value(QXmppTrustStorage::Authenticated); const auto manuallyDistrustedKeys = keys.value(QXmppTrustStorage::ManuallyDistrusted); @@ -301,7 +301,7 @@ QFuture<void> QXmppAtmManager::handleMessage(const QXmppMessage &message) const auto senderKey = e2eeMetadata ? e2eeMetadata->senderKey() : QByteArray(); const auto encryption = trustMessageElement->encryption(); - auto future = m_trustStorage->trustLevel(encryption, senderJid, senderKey); + auto future = trustLevel(encryption, senderJid, senderKey); await(future, this, [=](const auto &&senderKeyTrustLevel) mutable { const auto isSenderKeyAuthenticated = senderKeyTrustLevel == QXmppTrustStorage::Authenticated; @@ -347,7 +347,7 @@ QFuture<void> QXmppAtmManager::handleMessage(const QXmppMessage &message) } } - auto future = m_trustStorage->addKeysForPostponedTrustDecisions(encryption, senderKey, keyOwnersForPostponedTrustDecisions); + auto future = trustStorage()->addKeysForPostponedTrustDecisions(encryption, senderKey, keyOwnersForPostponedTrustDecisions); await(future, this, [=]() mutable { auto future = makeTrustDecisions(encryption, keysBeingAuthenticated, keysBeingDistrusted); await(future, this, [=]() mutable { @@ -380,9 +380,10 @@ QFuture<void> QXmppAtmManager::authenticate(const QString &encryption, const QMu QFutureInterface<void> interface(QFutureInterfaceBase::Started); - auto future = m_trustStorage->setTrustLevel(encryption, keyIds, QXmppTrustStorage::Authenticated); + auto future = setTrustLevel(encryption, keyIds, QXmppTrustStorage::Authenticated); await(future, this, [=]() mutable { - await(m_trustStorage->securityPolicy(encryption), this, [=](const auto securityPolicy) mutable { + auto future = securityPolicy(encryption); + await(future, this, [=](auto securityPolicy) mutable { if (securityPolicy == QXmppTrustStorage::Toakafa) { auto future = distrustAutomaticallyTrustedKeys(encryption, keyIds.uniqueKeys()); await(future, this, [=]() mutable { @@ -417,9 +418,9 @@ QFuture<void> QXmppAtmManager::distrust(const QString &encryption, const QMultiH QFutureInterface<void> interface(QFutureInterfaceBase::Started); - auto future = m_trustStorage->setTrustLevel(encryption, keyIds, QXmppTrustStorage::ManuallyDistrusted); + auto future = setTrustLevel(encryption, keyIds, QXmppTrustStorage::ManuallyDistrusted); await(future, this, [=]() mutable { - auto future = m_trustStorage->removeKeysForPostponedTrustDecisions(encryption, keyIds.values()); + auto future = trustStorage()->removeKeysForPostponedTrustDecisions(encryption, keyIds.values()); await(future, this, [=]() mutable { interface.reportFinished(); }); @@ -437,7 +438,7 @@ QFuture<void> QXmppAtmManager::distrust(const QString &encryption, const QMultiH /// QFuture<void> QXmppAtmManager::distrustAutomaticallyTrustedKeys(const QString &encryption, const QList<QString> &keyOwnerJids) { - return m_trustStorage->setTrustLevel(encryption, keyOwnerJids, QXmppTrustStorage::AutomaticallyTrusted, QXmppTrustStorage::AutomaticallyDistrusted); + return setTrustLevel(encryption, keyOwnerJids, QXmppTrustStorage::AutomaticallyTrusted, QXmppTrustStorage::AutomaticallyDistrusted); } /// @@ -454,13 +455,13 @@ QFuture<void> QXmppAtmManager::makePostponedTrustDecisions(const QString &encryp { QFutureInterface<void> interface(QFutureInterfaceBase::Started); - auto future = m_trustStorage->keysForPostponedTrustDecisions(encryption, senderKeyIds); + auto future = trustStorage()->keysForPostponedTrustDecisions(encryption, senderKeyIds); await(future, this, [=](const QHash<bool, QMultiHash<QString, QByteArray>> &&keysForPostponedTrustDecisions) mutable { // JIDs of key owners mapped to the IDs of their keys const auto keysBeingAuthenticated = keysForPostponedTrustDecisions.value(true); const auto keysBeingDistrusted = keysForPostponedTrustDecisions.value(false); - auto future = m_trustStorage->removeKeysForPostponedTrustDecisions(encryption, keysBeingAuthenticated.values(), keysBeingDistrusted.values()); + auto future = trustStorage()->removeKeysForPostponedTrustDecisions(encryption, keysBeingAuthenticated.values(), keysBeingDistrusted.values()); await(future, this, [=]() mutable { auto future = makeTrustDecisions(encryption, keysBeingAuthenticated, keysBeingDistrusted); await(future, this, [=]() mutable { diff --git a/src/client/QXmppAtmManager.h b/src/client/QXmppAtmManager.h index 042d1342..d4145d06 100644 --- a/src/client/QXmppAtmManager.h +++ b/src/client/QXmppAtmManager.h @@ -6,13 +6,13 @@ #define QXMPPATMMANAGER_H #include "QXmppAtmTrustStorage.h" -#include "QXmppClientExtension.h" #include "QXmppSendResult.h" +#include "QXmppTrustManager.h" class QXmppMessage; class QXmppTrustMessageKeyOwner; -class QXMPP_EXPORT QXmppAtmManager : public QXmppClientExtension +class QXMPP_EXPORT QXmppAtmManager : public QXmppTrustManager { Q_OBJECT @@ -42,7 +42,10 @@ private: QFuture<QXmpp::SendResult> sendTrustMessage(const QString &encryption, const QList<QXmppTrustMessageKeyOwner> &keyOwners, const QString &recipientJid); - QXmppAtmTrustStorage *m_trustStorage; + inline QXmppAtmTrustStorage *trustStorage() const + { + return dynamic_cast<QXmppAtmTrustStorage *>(QXmppTrustManager::trustStorage()); + } friend class tst_QXmppAtmManager; }; diff --git a/src/client/QXmppTrustManager.cpp b/src/client/QXmppTrustManager.cpp new file mode 100644 index 00000000..094ed2f2 --- /dev/null +++ b/src/client/QXmppTrustManager.cpp @@ -0,0 +1,282 @@ +// SPDX-FileCopyrightText: 2022 Melvin Keskin <melvo@olomono.de> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "QXmppTrustManager.h" + +#include "QXmppFutureUtils_p.h" + +using namespace QXmpp::Private; + +/// +/// \class QXmppTrustManager +/// +/// \brief The QXmppTrustManager manages end-to-end encryption trust decisions. +/// +/// \warning THIS API IS NOT FINALIZED YET! +/// +/// \ingroup Managers +/// +/// \since QXmpp 1.5 +/// + +/// +/// Constructs a trust manager. +/// +/// \param trustStorage trust storage implementation +/// +QXmppTrustManager::QXmppTrustManager(QXmppTrustStorage *trustStorage) + : m_trustStorage(trustStorage) +{ +} + +QXmppTrustManager::~QXmppTrustManager() = default; + +/// +/// Sets the security policy for an encryption protocol. +/// +/// \param encryption encryption protocol namespace +/// \param securityPolicy security policy being applied +/// +QFuture<void> QXmppTrustManager::setSecurityPolicy(const QString &encryption, QXmppTrustStorage::SecurityPolicy securityPolicy) +{ + return m_trustStorage->setSecurityPolicy(encryption, securityPolicy); +} + +/// +/// Resets the security policy for an encryption protocol. +/// +/// \param encryption encryption protocol namespace +/// +QFuture<void> QXmppTrustManager::resetSecurityPolicy(const QString &encryption) +{ + return m_trustStorage->resetSecurityPolicy(encryption); +} + +/// +/// Returns the security policy for an encryption protocol. +/// +/// \param encryption encryption protocol namespace +/// +/// \return the set security policy +/// +QFuture<QXmppTrustStorage::SecurityPolicy> QXmppTrustManager::securityPolicy(const QString &encryption) +{ + return m_trustStorage->securityPolicy(encryption); +} + +/// +/// Sets the own key (i.e., the key used by this client instance) for an +/// encryption protocol. +/// +/// \param encryption encryption protocol namespace +/// \param keyId ID of the key +/// +QFuture<void> QXmppTrustManager::setOwnKey(const QString &encryption, const QByteArray &keyId) +{ + return m_trustStorage->setOwnKey(encryption, keyId); +} + +/// +/// Resets the own key (i.e., the key used by this client instance) for an +/// encryption protocol. +/// +/// \param encryption encryption protocol namespace +/// +QFuture<void> QXmppTrustManager::resetOwnKey(const QString &encryption) +{ + return m_trustStorage->resetOwnKey(encryption); +} + +/// +/// Returns the own key (i.e., the key used by this client instance) for an +/// encryption protocol. +/// +/// \param encryption encryption protocol namespace +/// +/// \return the ID of the own key +/// +QFuture<QByteArray> QXmppTrustManager::ownKey(const QString &encryption) +{ + return m_trustStorage->ownKey(encryption); +} + +/// +/// Adds keys. +/// +/// \param encryption encryption protocol namespace +/// \param keyOwnerJid key owner's bare JID +/// \param keyIds IDs of the keys +/// \param trustLevel trust level of the keys +/// +QFuture<void> QXmppTrustManager::addKeys(const QString &encryption, const QString &keyOwnerJid, const QList<QByteArray> &keyIds, QXmppTrustStorage::TrustLevel trustLevel) +{ + return m_trustStorage->addKeys(encryption, keyOwnerJid, keyIds, trustLevel); +} + +/// +/// Removes keys. +/// +/// \param encryption encryption protocol namespace +/// \param keyIds IDs of the keys +/// +QFuture<void> QXmppTrustManager::removeKeys(const QString &encryption, const QList<QByteArray> &keyIds) +{ + return m_trustStorage->removeKeys(encryption, keyIds); +} + +/// +/// Removes all keys of a key owner. +/// +/// \param encryption encryption protocol namespace +/// \param keyOwnerJid key owner's bare JID +/// +QFuture<void> QXmppTrustManager::removeKeys(const QString &encryption, const QString &keyOwnerJid) +{ + return m_trustStorage->removeKeys(encryption, keyOwnerJid); +} + +/// +/// Removes all keys for encryption. +/// +/// \param encryption encryption protocol namespace +/// +QFuture<void> QXmppTrustManager::removeKeys(const QString &encryption) +{ + return m_trustStorage->removeKeys(encryption); +} + +/// +/// Returns the JIDs of all key owners mapped to the IDs of their keys with +/// specific trust levels. +/// +/// If no trust levels are passed, all keys for encryption are returned. +/// +/// \param encryption encryption protocol namespace +/// \param trustLevels trust levels of the keys +/// +/// \return the key owner JIDs mapped to their keys with specific trust levels +/// +QFuture<QHash<QXmppTrustStorage::TrustLevel, QMultiHash<QString, QByteArray>>> QXmppTrustManager::keys(const QString &encryption, QXmppTrustStorage::TrustLevels trustLevels) +{ + return m_trustStorage->keys(encryption, trustLevels); +} + +/// +/// Returns the IDs of keys mapped to their trust levels for specific key +/// owners. +/// +/// If no trust levels are passed, all keys for encryption and keyOwnerJids are +/// returned. +/// +/// \param encryption encryption protocol namespace +/// \param keyOwnerJids key owners' bare JIDs +/// \param trustLevels trust levels of the keys +/// +/// \return the key IDs mapped to their trust levels for specific key owners +/// +QFuture<QHash<QString, QHash<QByteArray, QXmppTrustStorage::TrustLevel>>> QXmppTrustManager::keys(const QString &encryption, const QList<QString> &keyOwnerJids, QXmppTrustStorage::TrustLevels trustLevels) +{ + return m_trustStorage->keys(encryption, keyOwnerJids, trustLevels); +} + +/// +/// Returns whether at least one key of a key owner with a specific trust level +/// is stored. +/// +/// \param encryption encryption protocol namespace +/// \param keyOwnerJid key owner's bare JID +/// \param trustLevels possible trust levels of the key +/// +/// \return whether a key of the key owner with a passed trust level is stored +/// +QFuture<bool> QXmppTrustManager::hasKey(const QString &encryption, const QString &keyOwnerJid, QXmppTrustStorage::TrustLevels trustLevels) +{ + return m_trustStorage->hasKey(encryption, keyOwnerJid, trustLevels); +} + +/// +/// Sets the trust level of keys. +/// +/// If a key is not stored, it is added to the storage. +/// +/// \param encryption encryption protocol namespace +/// \param keyIds key owners' bare JIDs mapped to the IDs of their keys +/// \param trustLevel trust level being set +/// +QFuture<void> QXmppTrustManager::setTrustLevel(const QString &encryption, const QMultiHash<QString, QByteArray> &keyIds, QXmppTrustStorage::TrustLevel trustLevel) +{ + QFutureInterface<void> interface(QFutureInterfaceBase::Started); + + auto future = m_trustStorage->setTrustLevel(encryption, keyIds, trustLevel); + await(future, this, [=](QHash<QString, QMultiHash<QString, QByteArray>> modifiedKeys) mutable { + emit trustLevelsChanged(modifiedKeys); + interface.reportFinished(); + }); + + return interface.future(); +} + +/// +/// Sets the trust level of keys specified by their key owner and trust level. +/// +/// \param encryption encryption protocol namespace +/// \param keyOwnerJids key owners' bare JIDs +/// \param oldTrustLevel trust level being changed +/// \param newTrustLevel trust level being set +/// +QFuture<void> QXmppTrustManager::setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, QXmppTrustStorage::TrustLevel oldTrustLevel, QXmppTrustStorage::TrustLevel newTrustLevel) +{ + QFutureInterface<void> interface(QFutureInterfaceBase::Started); + + auto future = m_trustStorage->setTrustLevel(encryption, keyOwnerJids, oldTrustLevel, newTrustLevel); + await(future, this, [=](QHash<QString, QMultiHash<QString, QByteArray>> modifiedKeys) mutable { + emit trustLevelsChanged(modifiedKeys); + interface.reportFinished(); + }); + + return interface.future(); +} + +/// +/// Returns the trust level of a key. +/// +/// If the key is not stored, the trust in that key is undecided. +/// +/// \param encryption encryption protocol namespace +/// \param keyOwnerJid key owner's bare JID +/// \param keyId ID of the key +/// +/// \return the key's trust level +/// +QFuture<QXmppTrustStorage::TrustLevel> QXmppTrustManager::trustLevel(const QString &encryption, const QString &keyOwnerJid, const QByteArray &keyId) +{ + return m_trustStorage->trustLevel(encryption, keyOwnerJid, keyId); +} + +/// +/// Resets all data for encryption. +/// +/// \param encryption encryption protocol namespace +/// +QFuture<void> QXmppTrustManager::resetAll(const QString &encryption) +{ + return m_trustStorage->resetAll(encryption); +} + +/// \cond +bool QXmppTrustManager::handleStanza(const QDomElement &stanza) +{ + return false; +} +/// \endcond + +/// +/// \fn QXmppTrustManager::trustLevelsChanged(const QHash<QString, QMultiHash<QString, QByteArray>> &modifiedKeys) +/// +/// Emitted when the trust levels of keys changed because \c setTrustLevel() +/// added a new key or modified an existing one. +/// +/// \param modifiedKeys key owners' bare JIDs mapped to their modified keys for +/// specific encryption protocol namespaces +/// diff --git a/src/client/QXmppTrustManager.h b/src/client/QXmppTrustManager.h new file mode 100644 index 00000000..9081af92 --- /dev/null +++ b/src/client/QXmppTrustManager.h @@ -0,0 +1,59 @@ +// SPDX-FileCopyrightText: 2022 Melvin Keskin <melvo@olomono.de> +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPTRUSTMANAGER_H +#define QXMPPTRUSTMANAGER_H + +#include "QXmppClientExtension.h" +#include "QXmppTrustStorage.h" + +class QXMPP_EXPORT QXmppTrustManager : public QXmppClientExtension +{ + Q_OBJECT + +public: + QXmppTrustManager(QXmppTrustStorage *trustStorage); + ~QXmppTrustManager(); + + QFuture<void> setSecurityPolicy(const QString &encryption, QXmppTrustStorage::SecurityPolicy securityPolicy); + QFuture<void> resetSecurityPolicy(const QString &encryption); + QFuture<QXmppTrustStorage::SecurityPolicy> securityPolicy(const QString &encryption); + + QFuture<void> setOwnKey(const QString &encryption, const QByteArray &keyId); + QFuture<void> resetOwnKey(const QString &encryption); + QFuture<QByteArray> ownKey(const QString &encryption); + + QFuture<void> addKeys(const QString &encryption, const QString &keyOwnerJid, const QList<QByteArray> &keyIds, QXmppTrustStorage::TrustLevel trustLevel = QXmppTrustStorage::AutomaticallyDistrusted); + QFuture<void> removeKeys(const QString &encryption, const QList<QByteArray> &keyIds); + QFuture<void> removeKeys(const QString &encryption, const QString &keyOwnerJid); + QFuture<void> removeKeys(const QString &encryption); + QFuture<QHash<QXmppTrustStorage::TrustLevel, QMultiHash<QString, QByteArray>>> keys(const QString &encryption, QXmppTrustStorage::TrustLevels trustLevels = {}); + QFuture<QHash<QString, QHash<QByteArray, QXmppTrustStorage::TrustLevel>>> keys(const QString &encryption, const QList<QString> &keyOwnerJids, QXmppTrustStorage::TrustLevels trustLevels = {}); + QFuture<bool> hasKey(const QString &encryption, const QString &keyOwnerJid, QXmppTrustStorage::TrustLevels trustLevels); + + QFuture<void> setTrustLevel(const QString &encryption, const QMultiHash<QString, QByteArray> &keyIds, QXmppTrustStorage::TrustLevel trustLevel); + QFuture<void> setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, QXmppTrustStorage::TrustLevel oldTrustLevel, QXmppTrustStorage::TrustLevel newTrustLevel); + QFuture<QXmppTrustStorage::TrustLevel> trustLevel(const QString &encryption, const QString &keyOwnerJid, const QByteArray &keyId); + + QFuture<void> resetAll(const QString &encryption); + + /// \cond + bool handleStanza(const QDomElement &stanza) override; + /// \endcond + + Q_SIGNAL void trustLevelsChanged(const QHash<QString, QMultiHash<QString, QByteArray>> &modifiedKeys); + +protected: + /// \cond + inline QXmppTrustStorage *trustStorage() const + { + return m_trustStorage; + } + /// \endcond + +private: + QXmppTrustStorage *m_trustStorage; +}; + +#endif // QXMPPTRUSTMANAGER_H diff --git a/src/client/QXmppTrustMemoryStorage.cpp b/src/client/QXmppTrustMemoryStorage.cpp index c9e2f37f..a94768d2 100644 --- a/src/client/QXmppTrustMemoryStorage.cpp +++ b/src/client/QXmppTrustMemoryStorage.cpp @@ -50,7 +50,7 @@ QXmppTrustMemoryStorage::QXmppTrustMemoryStorage() QXmppTrustMemoryStorage::~QXmppTrustMemoryStorage() = default; /// \cond -QFuture<void> QXmppTrustMemoryStorage::setSecurityPolicy(const QString &encryption, const QXmppTrustStorage::SecurityPolicy securityPolicy) +QFuture<void> QXmppTrustMemoryStorage::setSecurityPolicy(const QString &encryption, QXmppTrustStorage::SecurityPolicy securityPolicy) { d->securityPolicies.insert(encryption, securityPolicy); return makeReadyFuture(); @@ -85,7 +85,7 @@ QFuture<QByteArray> QXmppTrustMemoryStorage::ownKey(const QString &encryption) return makeReadyFuture(std::move(key)); } -QFuture<void> QXmppTrustMemoryStorage::addKeys(const QString &encryption, const QString &keyOwnerJid, const QList<QByteArray> &keyIds, const QXmppTrustStorage::TrustLevel trustLevel) +QFuture<void> QXmppTrustMemoryStorage::addKeys(const QString &encryption, const QString &keyOwnerJid, const QList<QByteArray> &keyIds, QXmppTrustStorage::TrustLevel trustLevel) { for (const auto &keyId : keyIds) { Key key; @@ -132,7 +132,7 @@ QFuture<void> QXmppTrustMemoryStorage::removeKeys(const QString &encryption) return makeReadyFuture(); } -QFuture<QHash<QXmppTrustStorage::TrustLevel, QMultiHash<QString, QByteArray>>> QXmppTrustMemoryStorage::keys(const QString &encryption, const TrustLevels trustLevels) +QFuture<QHash<QXmppTrustStorage::TrustLevel, QMultiHash<QString, QByteArray>>> QXmppTrustMemoryStorage::keys(const QString &encryption, TrustLevels trustLevels) { QHash<TrustLevel, QMultiHash<QString, QByteArray>> keys; @@ -175,8 +175,10 @@ QFuture<bool> QXmppTrustMemoryStorage::hasKey(const QString &encryption, const Q return makeReadyFuture(std::move(false)); } -QFuture<void> QXmppTrustMemoryStorage::setTrustLevel(const QString &encryption, const QMultiHash<QString, QByteArray> &keyIds, const TrustLevel trustLevel) +QFuture<QHash<QString, QMultiHash<QString, QByteArray>>> QXmppTrustMemoryStorage::setTrustLevel(const QString &encryption, const QMultiHash<QString, QByteArray> &keyIds, TrustLevel trustLevel) { + QHash<QString, QMultiHash<QString, QByteArray>> modifiedKeys; + for (auto itr = keyIds.constBegin(); itr != keyIds.constEnd(); ++itr) { const auto keyOwnerJid = itr.key(); const auto keyId = itr.value(); @@ -191,6 +193,7 @@ QFuture<void> QXmppTrustMemoryStorage::setTrustLevel(const QString &encryption, // Update the stored trust level if it differs from the new one. if (key.trustLevel != trustLevel) { key.trustLevel = trustLevel; + modifiedKeys[encryption].insert(keyOwnerJid, keyId); } isKeyFound = true; @@ -205,22 +208,27 @@ QFuture<void> QXmppTrustMemoryStorage::setTrustLevel(const QString &encryption, key.ownerJid = keyOwnerJid; key.trustLevel = trustLevel; d->keys.insert(encryption, key); + modifiedKeys[encryption].insert(keyOwnerJid, keyId); } } - return makeReadyFuture(); + return makeReadyFuture(std::move(modifiedKeys)); } -QFuture<void> QXmppTrustMemoryStorage::setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, const QXmppTrustStorage::TrustLevel oldTrustLevel, const QXmppTrustStorage::TrustLevel newTrustLevel) +QFuture<QHash<QString, QMultiHash<QString, QByteArray>>> QXmppTrustMemoryStorage::setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, TrustLevel oldTrustLevel, TrustLevel newTrustLevel) { + QHash<QString, QMultiHash<QString, QByteArray>> modifiedKeys; + for (auto itr = d->keys.find(encryption); itr != d->keys.end() && itr.key() == encryption; ++itr) { auto &key = itr.value(); - if (keyOwnerJids.contains(key.ownerJid) && key.trustLevel == oldTrustLevel) { + auto keyOwnerJid = key.ownerJid; + if (keyOwnerJids.contains(keyOwnerJid) && key.trustLevel == oldTrustLevel) { key.trustLevel = newTrustLevel; + modifiedKeys[encryption].insert(keyOwnerJid, key.id); } } - return makeReadyFuture(); + return makeReadyFuture(std::move(modifiedKeys)); } QFuture<QXmppTrustStorage::TrustLevel> QXmppTrustMemoryStorage::trustLevel(const QString &encryption, const QString &keyOwnerJid, const QByteArray &keyId) @@ -228,7 +236,7 @@ QFuture<QXmppTrustStorage::TrustLevel> QXmppTrustMemoryStorage::trustLevel(const const auto keys = d->keys.values(encryption); for (const auto &key : keys) { if (key.id == keyId && key.ownerJid == keyOwnerJid) { - return makeReadyFuture(std::move(QXmppTrustStorage::TrustLevel(key.trustLevel))); + return makeReadyFuture(std::move(TrustLevel(key.trustLevel))); } } diff --git a/src/client/QXmppTrustMemoryStorage.h b/src/client/QXmppTrustMemoryStorage.h index c9d15fe9..1d0e2561 100644 --- a/src/client/QXmppTrustMemoryStorage.h +++ b/src/client/QXmppTrustMemoryStorage.h @@ -34,8 +34,8 @@ public: QFuture<QHash<QString, QHash<QByteArray, TrustLevel>>> keys(const QString &encryption, const QList<QString> &keyOwnerJids, TrustLevels trustLevels = {}) override; QFuture<bool> hasKey(const QString &encryption, const QString &keyOwnerJid, TrustLevels trustLevels) override; - QFuture<void> setTrustLevel(const QString &encryption, const QMultiHash<QString, QByteArray> &keyIds, TrustLevel trustLevel) override; - QFuture<void> setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, TrustLevel oldTrustLevel, TrustLevel newTrustLevel) override; + QFuture<QHash<QString, QMultiHash<QString, QByteArray>>> setTrustLevel(const QString &encryption, const QMultiHash<QString, QByteArray> &keyIds, TrustLevel trustLevel) override; + QFuture<QHash<QString, QMultiHash<QString, QByteArray>>> setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, TrustLevel oldTrustLevel, TrustLevel newTrustLevel) override; QFuture<TrustLevel> trustLevel(const QString &encryption, const QString &keyOwnerJid, const QByteArray &keyId) override; QFuture<void> resetAll(const QString &encryption) override; diff --git a/src/client/QXmppTrustStorage.cpp b/src/client/QXmppTrustStorage.cpp index 5cfe5b95..18637fab 100644 --- a/src/client/QXmppTrustStorage.cpp +++ b/src/client/QXmppTrustStorage.cpp @@ -5,8 +5,7 @@ /// /// \class QXmppTrustStorage /// -/// \brief The QXmppTrustStorage class stores trust data for end-to-end -/// encryption. +/// \brief The QXmppTrustStorage class stores end-to-end encryption trust data. /// /// The term "key" is used for a public long-term key. /// @@ -73,7 +72,7 @@ /// /// -/// \fn QXmppTrustStorage::addKeys(const QString &encryption, const QString &keyOwnerJid, const QList<QByteArray> &keyIds, const QXmppTrustStorage::TrustLevel trustLevel) +/// \fn QXmppTrustStorage::addKeys(const QString &encryption, const QString &keyOwnerJid, const QList<QByteArray> &keyIds, QXmppTrustStorage::TrustLevel trustLevel) /// /// Adds keys. /// @@ -163,6 +162,9 @@ /// \param keyIds key owners' bare JIDs mapped to the IDs of their keys /// \param trustLevel trust level being set /// +/// \return the key owner JIDs mapped to their modified keys for specific +/// encryption protocols +/// /// /// \fn QXmppTrustStorage::setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, TrustLevel oldTrustLevel, TrustLevel newTrustLevel) @@ -174,6 +176,9 @@ /// \param oldTrustLevel trust level being changed /// \param newTrustLevel trust level being set /// +/// \return the key owner JIDs mapped to their modified keys for specific +/// encryption protocols +/// /// /// \fn QXmppTrustStorage::trustLevel(const QString &encryption, const QString &keyOwnerJid, const QByteArray &keyId) diff --git a/src/client/QXmppTrustStorage.h b/src/client/QXmppTrustStorage.h index c1db773d..b93eae7b 100644 --- a/src/client/QXmppTrustStorage.h +++ b/src/client/QXmppTrustStorage.h @@ -53,8 +53,8 @@ public: virtual QFuture<QHash<QString, QHash<QByteArray, TrustLevel>>> keys(const QString &encryption, const QList<QString> &keyOwnerJids, TrustLevels trustLevels = {}) = 0; virtual QFuture<bool> hasKey(const QString &encryption, const QString &keyOwnerJid, TrustLevels trustLevels) = 0; - virtual QFuture<void> setTrustLevel(const QString &encryption, const QMultiHash<QString, QByteArray> &keyIds, TrustLevel trustLevel) = 0; - virtual QFuture<void> setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, TrustLevel oldTrustLevel, TrustLevel newTrustLevel) = 0; + virtual QFuture<QHash<QString, QMultiHash<QString, QByteArray>>> setTrustLevel(const QString &encryption, const QMultiHash<QString, QByteArray> &keyIds, TrustLevel trustLevel) = 0; + virtual QFuture<QHash<QString, QMultiHash<QString, QByteArray>>> setTrustLevel(const QString &encryption, const QList<QString> &keyOwnerJids, TrustLevel oldTrustLevel, TrustLevel newTrustLevel) = 0; virtual QFuture<TrustLevel> trustLevel(const QString &encryption, const QString &keyOwnerJid, const QByteArray &keyId) = 0; virtual QFuture<void> resetAll(const QString &encryption) = 0; |
