2022-12-19 15:43:24 +00:00
|
|
|
// Squawk messenger.
|
|
|
|
// Copyright (C) 2019 Yury Gubich <blue@macaw.me>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "trusthandler.h"
|
|
|
|
#include "core/account.h"
|
2023-01-29 17:26:54 +00:00
|
|
|
#include "core/adapterfunctions.h"
|
2022-12-19 15:43:24 +00:00
|
|
|
|
|
|
|
Core::TrustHandler::TrustHandler(Account* account):
|
2023-03-17 20:59:51 +00:00
|
|
|
QObject(),
|
|
|
|
QXmppTrustStorage(),
|
2022-12-19 15:43:24 +00:00
|
|
|
acc(account),
|
|
|
|
db(acc->getName() + "/trust"),
|
|
|
|
protocols(db.createDirectory() + "/protocols"),
|
|
|
|
securityPolicies(db.addCache<QString, uint8_t>("securityPolicies")),
|
|
|
|
ownKeys(db.addCache<QString, QByteArray>("ownKeys")),
|
|
|
|
keysByProtocol()
|
|
|
|
{
|
2023-11-10 22:26:16 +00:00
|
|
|
if (!protocols.open(QIODevice::ReadWrite | QIODevice::Text)) //never supposed to happen since I have just created a directory;
|
2023-03-27 18:45:29 +00:00
|
|
|
throw LMDBAL::Directory(protocols.fileName().toStdString());
|
2022-12-19 15:43:24 +00:00
|
|
|
|
|
|
|
QTextStream in(&protocols);
|
|
|
|
while(!in.atEnd()) {
|
|
|
|
QString protocol = in.readLine();
|
|
|
|
|
|
|
|
if (protocol.size() > 1) { //I'm afraid of reading some nonsence like separately standing \n or EF or BOM, so... let it be at least 2 chars long
|
|
|
|
KeyCache* cache = db.addCache<QString, Keys>(protocol.toStdString());
|
|
|
|
keysByProtocol.insert(std::make_pair(protocol, cache));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protocols.close();
|
|
|
|
db.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::TrustHandler::~TrustHandler() {
|
|
|
|
protocols.close();
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::TrustHandler::KeyCache * Core::TrustHandler::getCache(const QString& encryption) {
|
|
|
|
std::map<QString, KeyCache*>::iterator itr = keysByProtocol.find(encryption);
|
2023-11-10 22:26:16 +00:00
|
|
|
if (itr == keysByProtocol.end())
|
2022-12-19 15:43:24 +00:00
|
|
|
return createNewCache(encryption);
|
2023-11-10 22:26:16 +00:00
|
|
|
else
|
2022-12-19 15:43:24 +00:00
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::TrustHandler::KeyCache * Core::TrustHandler::createNewCache(const QString& encryption) {
|
|
|
|
db.close();
|
|
|
|
KeyCache* cache = db.addCache<QString, Keys>(encryption.toStdString());
|
|
|
|
keysByProtocol.insert(std::make_pair(encryption, cache));
|
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
if (!protocols.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
|
2023-03-27 18:45:29 +00:00
|
|
|
throw LMDBAL::Directory(protocols.fileName().toStdString());
|
2023-11-10 22:26:16 +00:00
|
|
|
|
2022-12-19 15:43:24 +00:00
|
|
|
QTextStream out(&protocols);
|
|
|
|
out << encryption + "\n";
|
|
|
|
protocols.close();
|
|
|
|
|
|
|
|
db.open();
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::resetAll(const QString& encryption) {
|
2022-12-19 15:43:24 +00:00
|
|
|
securityPolicies->removeRecord(encryption);
|
|
|
|
ownKeys->removeRecord(encryption);
|
2023-03-17 23:50:04 +00:00
|
|
|
std::map<QString, KeyCache*>::const_iterator itr = keysByProtocol.find(encryption);
|
|
|
|
if (itr == keysByProtocol.end())
|
|
|
|
return Core::makeReadyTask();
|
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
2023-03-17 23:50:04 +00:00
|
|
|
KeyCache* cache = itr->second;
|
2023-11-10 22:26:16 +00:00
|
|
|
std::map<QString, Keys> keys = cache->readAll(txn);
|
|
|
|
cache->drop(txn);
|
2023-03-17 23:50:04 +00:00
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
txn.commit();
|
2023-03-17 23:50:04 +00:00
|
|
|
for (const std::pair<const QString, Keys>& pair : keys) {
|
|
|
|
bool empty = true;
|
|
|
|
for (const std::pair<const QByteArray, Shared::TrustLevel>& trust : pair.second) {
|
|
|
|
if (trust.second != Shared::TrustLevel::undecided) {
|
|
|
|
empty = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty)
|
|
|
|
emit trustLevelsChanged(pair.first, getSummary(pair.first));
|
|
|
|
}
|
2022-12-19 15:43:24 +00:00
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<QXmpp::TrustLevel> Core::TrustHandler::trustLevel(
|
2022-12-19 15:43:24 +00:00
|
|
|
const QString& encryption,
|
|
|
|
const QString& keyOwnerJid,
|
2023-11-10 22:26:16 +00:00
|
|
|
const QByteArray& keyId
|
|
|
|
) {
|
2023-03-10 18:43:31 +00:00
|
|
|
KeyCache* cache = getCache(encryption);
|
|
|
|
Shared::TrustLevel level = Shared::TrustLevel::undecided;
|
|
|
|
try {
|
|
|
|
Keys map = cache->getRecord(keyOwnerJid);
|
2023-03-11 16:46:23 +00:00
|
|
|
Keys::const_iterator itr = map.find(keyId);
|
|
|
|
if (itr != map.end())
|
|
|
|
level = itr->second;
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(convert(level)));
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<QHash<QString, QMultiHash<QString, QByteArray>>> Core::TrustHandler::setTrustLevel(
|
2022-12-19 15:43:24 +00:00
|
|
|
const QString& encryption,
|
|
|
|
const QList<QString>& keyOwnerJids,
|
|
|
|
QXmpp::TrustLevel oldTrustLevel,
|
2023-11-10 22:26:16 +00:00
|
|
|
QXmpp::TrustLevel newTrustLevel
|
|
|
|
) {
|
2022-12-19 15:43:24 +00:00
|
|
|
QHash<QString, QMultiHash<QString, QByteArray>> modifiedKeys;
|
|
|
|
Shared::TrustLevel oldLevel = convert(oldTrustLevel);
|
|
|
|
Shared::TrustLevel newLevel = convert(newTrustLevel);
|
2023-03-17 23:50:04 +00:00
|
|
|
std::set<QString> modifiedJids;
|
2022-12-19 15:43:24 +00:00
|
|
|
KeyCache* cache = getCache(encryption);
|
2023-11-10 22:26:16 +00:00
|
|
|
|
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
2022-12-19 15:43:24 +00:00
|
|
|
for (const QString& keyOwnerJid : keyOwnerJids) {
|
2023-11-10 22:26:16 +00:00
|
|
|
Keys map = cache->getRecord(keyOwnerJid, txn);
|
2022-12-19 15:43:24 +00:00
|
|
|
uint count = 0;
|
|
|
|
for (std::pair<const QByteArray, Shared::TrustLevel>& pair : map) {
|
|
|
|
Shared::TrustLevel& current = pair.second;
|
|
|
|
if (current == oldLevel) {
|
|
|
|
current = newLevel;
|
|
|
|
modifiedKeys[encryption].insert(keyOwnerJid, pair.first);
|
2023-03-17 23:50:04 +00:00
|
|
|
modifiedJids.insert(keyOwnerJid);
|
2022-12-19 15:43:24 +00:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
2023-03-17 23:50:04 +00:00
|
|
|
if (count > 0)
|
2023-11-10 22:26:16 +00:00
|
|
|
cache->changeRecord(keyOwnerJid, map, txn);
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
2023-11-10 22:26:16 +00:00
|
|
|
txn.commit();
|
2023-03-17 23:50:04 +00:00
|
|
|
for (const QString& jid : modifiedJids)
|
|
|
|
emit trustLevelsChanged(jid, getSummary(jid));
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(modifiedKeys));
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<QHash<QString, QMultiHash<QString, QByteArray>>> Core::TrustHandler::setTrustLevel(
|
2022-12-19 15:43:24 +00:00
|
|
|
const QString& encryption,
|
|
|
|
const QMultiHash<QString, QByteArray>& keyIds,
|
2023-11-10 22:26:16 +00:00
|
|
|
QXmpp::TrustLevel trustLevel
|
|
|
|
) {
|
2022-12-19 15:43:24 +00:00
|
|
|
QHash<QString, QMultiHash<QString, QByteArray>> modifiedKeys;
|
|
|
|
Shared::TrustLevel level = convert(trustLevel);
|
2023-03-17 23:50:04 +00:00
|
|
|
std::set<QString> modifiedJids;
|
2022-12-19 15:43:24 +00:00
|
|
|
KeyCache* cache = getCache(encryption);
|
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
2022-12-19 15:43:24 +00:00
|
|
|
for (MultySB::const_iterator itr = keyIds.begin(), end = keyIds.end(); itr != end; ++itr) {
|
|
|
|
const QString& keyOwnerJid = itr.key();
|
|
|
|
const QByteArray& keyId = itr.value();
|
2023-03-10 18:43:31 +00:00
|
|
|
try {
|
2023-11-10 22:26:16 +00:00
|
|
|
Keys map = cache->getRecord(keyOwnerJid, txn);
|
2023-03-10 18:43:31 +00:00
|
|
|
std::pair<Keys::iterator, bool> result = map.insert(std::make_pair(keyId, level));
|
|
|
|
bool changed = result.second;
|
|
|
|
if (!changed && result.first->second != level) {
|
|
|
|
result.first->second = level;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
modifiedKeys[encryption].insert(keyOwnerJid, keyId);
|
2023-03-17 23:50:04 +00:00
|
|
|
modifiedJids.insert(keyOwnerJid);
|
2023-11-10 22:26:16 +00:00
|
|
|
cache->changeRecord(keyOwnerJid, map, txn);
|
2023-03-10 18:43:31 +00:00
|
|
|
}
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {
|
2023-03-10 18:43:31 +00:00
|
|
|
Keys map({{keyId, level}});
|
2022-12-19 15:43:24 +00:00
|
|
|
modifiedKeys[encryption].insert(keyOwnerJid, keyId);
|
2023-03-17 23:50:04 +00:00
|
|
|
modifiedJids.insert(keyOwnerJid);
|
2023-11-10 22:26:16 +00:00
|
|
|
cache->addRecord(keyOwnerJid, map, txn);
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 22:26:16 +00:00
|
|
|
txn.commit();
|
2023-03-17 23:50:04 +00:00
|
|
|
|
|
|
|
for (const QString& jid : modifiedJids)
|
|
|
|
emit trustLevelsChanged(jid, getSummary(jid));
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(modifiedKeys));
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
QXmppTask<bool> Core::TrustHandler::hasKey(
|
|
|
|
const QString& encryption,
|
|
|
|
const QString& keyOwnerJid,
|
|
|
|
QXmpp::TrustLevels trustLevels
|
|
|
|
) {
|
2022-12-19 15:43:24 +00:00
|
|
|
KeyCache* cache = getCache(encryption);
|
|
|
|
bool found = false;
|
|
|
|
try {
|
|
|
|
Keys map = cache->getRecord(keyOwnerJid);
|
|
|
|
for (const std::pair<const QByteArray, Shared::TrustLevel>& pair : map) {
|
|
|
|
if (trustLevels.testFlag(convert(pair.second))) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(found));
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<QHash<QString, QHash<QByteArray, QXmpp::TrustLevel>>> Core::TrustHandler::keys(
|
2022-12-19 15:43:24 +00:00
|
|
|
const QString& encryption,
|
|
|
|
const QList<QString>& keyOwnerJids,
|
2023-11-10 22:26:16 +00:00
|
|
|
QXmpp::TrustLevels trustLevels
|
|
|
|
) {
|
2022-12-19 15:43:24 +00:00
|
|
|
HSHBTL res;
|
|
|
|
|
|
|
|
KeyCache* cache = getCache(encryption);
|
|
|
|
for (const QString& keyOwnerJid : keyOwnerJids) {
|
|
|
|
try {
|
|
|
|
Keys map = cache->getRecord(keyOwnerJid);
|
|
|
|
QHash<QByteArray, QXmpp::TrustLevel>& pRes = res[keyOwnerJid];
|
|
|
|
for (const std::pair<const QByteArray, Shared::TrustLevel>& pair : map) {
|
|
|
|
QXmpp::TrustLevel level = convert(pair.second);
|
|
|
|
if (!trustLevels || trustLevels.testFlag(level)) {
|
|
|
|
pRes.insert(pair.first, level);
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(res));
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<QHash<QXmpp::TrustLevel, QMultiHash<QString, QByteArray>>> Core::TrustHandler::keys(
|
2022-12-19 15:43:24 +00:00
|
|
|
const QString& encryption,
|
2023-11-10 22:26:16 +00:00
|
|
|
QXmpp::TrustLevels trustLevels
|
|
|
|
) {
|
2022-12-19 15:43:24 +00:00
|
|
|
QHash<TL, MultySB> res;
|
|
|
|
KeyCache* cache = getCache(encryption);
|
|
|
|
std::map<QString, Keys> storage = cache->readAll();
|
|
|
|
for (const std::pair<const QString, Keys>& value : storage) {
|
|
|
|
for (const std::pair<const QByteArray, Shared::TrustLevel>& pair : value.second) {
|
|
|
|
QXmpp::TrustLevel level = convert(pair.second);
|
2023-03-10 18:43:31 +00:00
|
|
|
if (!trustLevels || trustLevels.testFlag(level))
|
2022-12-19 15:43:24 +00:00
|
|
|
res[level].insert(value.first, pair.first);
|
|
|
|
}
|
|
|
|
}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(res));
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::removeKeys(const QString& encryption) {
|
2023-03-17 23:50:04 +00:00
|
|
|
std::map<QString, KeyCache*>::const_iterator itr = keysByProtocol.find(encryption);
|
|
|
|
if (itr == keysByProtocol.end())
|
|
|
|
return Core::makeReadyTask();
|
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
2023-03-17 23:50:04 +00:00
|
|
|
KeyCache* cache = itr->second;
|
2023-11-10 22:26:16 +00:00
|
|
|
std::map<QString, Keys> keys = cache->readAll(txn);
|
|
|
|
cache->drop(txn);
|
|
|
|
txn.commit();
|
2023-03-17 23:50:04 +00:00
|
|
|
|
|
|
|
for (const std::pair<const QString, Keys>& pair : keys) {
|
|
|
|
bool empty = true;
|
|
|
|
for (const std::pair<const QByteArray, Shared::TrustLevel>& trust : pair.second) {
|
|
|
|
if (trust.second != Shared::TrustLevel::undecided) {
|
|
|
|
empty = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!empty)
|
|
|
|
emit trustLevelsChanged(pair.first, getSummary(pair.first));
|
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::removeKeys(const QString& encryption, const QString& keyOwnerJid) {
|
2023-03-17 23:50:04 +00:00
|
|
|
std::map<QString, KeyCache*>::const_iterator itr = keysByProtocol.find(encryption);
|
|
|
|
if (itr == keysByProtocol.end())
|
|
|
|
return Core::makeReadyTask();
|
|
|
|
|
|
|
|
KeyCache* cache = itr->second;
|
|
|
|
try {
|
|
|
|
cache->removeRecord(keyOwnerJid);
|
|
|
|
emit trustLevelsChanged(keyOwnerJid, getSummary(keyOwnerJid)); //TODO there is a probability of notification without the actial change
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {} //if the movin entry was empty or if it consisted of Undecided keys
|
2023-03-17 23:50:04 +00:00
|
|
|
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::removeKeys(const QString& encryption, const QList<QByteArray>& keyIds) {
|
2022-12-19 15:43:24 +00:00
|
|
|
std::set<QByteArray> set;
|
2023-03-10 18:43:31 +00:00
|
|
|
for (const QByteArray& keyId : keyIds)
|
2022-12-19 15:43:24 +00:00
|
|
|
set.insert(keyId);
|
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
2022-12-19 15:43:24 +00:00
|
|
|
KeyCache* cache = getCache(encryption);
|
2023-11-10 22:26:16 +00:00
|
|
|
std::map<QString, Keys> data = cache->readAll(txn);
|
2023-03-17 23:50:04 +00:00
|
|
|
std::set<QString> modifiedJids;
|
|
|
|
|
2022-12-19 15:43:24 +00:00
|
|
|
for (std::map<QString, Keys>::iterator cItr = data.begin(), cEnd = data.end(); cItr != cEnd; /*no increment*/) {
|
|
|
|
Keys& byOwner = cItr->second;
|
|
|
|
for (Keys::const_iterator itr = byOwner.begin(), end = byOwner.end(); itr != end; /*no increment*/) {
|
|
|
|
const QByteArray& keyId = itr->first;
|
|
|
|
if (set.erase(keyId)) {
|
|
|
|
byOwner.erase(itr++);
|
2023-03-17 23:50:04 +00:00
|
|
|
modifiedJids.insert(cItr->first);
|
2023-03-10 18:43:31 +00:00
|
|
|
} else
|
2022-12-19 15:43:24 +00:00
|
|
|
++itr;
|
|
|
|
}
|
2023-03-10 18:43:31 +00:00
|
|
|
if (byOwner.size() > 0)
|
2022-12-19 15:43:24 +00:00
|
|
|
data.erase(cItr++);
|
2023-03-10 18:43:31 +00:00
|
|
|
else
|
2022-12-19 15:43:24 +00:00
|
|
|
++cItr;
|
|
|
|
}
|
2023-11-10 22:26:16 +00:00
|
|
|
if (modifiedJids.size() > 0)
|
|
|
|
cache->replaceAll(data, txn);
|
2023-03-17 23:50:04 +00:00
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
txn.commit();
|
2023-03-17 23:50:04 +00:00
|
|
|
for (const QString& jid : modifiedJids)
|
|
|
|
emit trustLevelsChanged(jid, getSummary(jid));
|
2022-12-19 15:43:24 +00:00
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::addKeys(
|
2022-12-19 15:43:24 +00:00
|
|
|
const QString& encryption,
|
|
|
|
const QString& keyOwnerJid,
|
|
|
|
const QList<QByteArray>& keyIds,
|
2023-11-10 22:26:16 +00:00
|
|
|
QXmpp::TrustLevel trustLevel
|
|
|
|
) {
|
2022-12-19 15:43:24 +00:00
|
|
|
KeyCache* cache = getCache(encryption);
|
|
|
|
Shared::TrustLevel level = convert(trustLevel);
|
|
|
|
Keys data;
|
|
|
|
bool had = false;
|
2023-11-10 22:26:16 +00:00
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
2022-12-19 15:43:24 +00:00
|
|
|
try {
|
2023-11-10 22:26:16 +00:00
|
|
|
data = cache->getRecord(keyOwnerJid, txn);
|
2022-12-19 15:43:24 +00:00
|
|
|
had = true;
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2022-12-19 15:43:24 +00:00
|
|
|
for (const QByteArray& keyId : keyIds) {
|
|
|
|
std::pair<Keys::iterator, bool> result = data.insert(std::make_pair(keyId, level));
|
2023-03-10 18:43:31 +00:00
|
|
|
if (!result.second)
|
2022-12-19 15:43:24 +00:00
|
|
|
result.first->second = level;
|
|
|
|
}
|
|
|
|
|
2023-03-17 23:50:04 +00:00
|
|
|
if (had)
|
2023-11-10 22:26:16 +00:00
|
|
|
cache->changeRecord(keyOwnerJid, data, txn);
|
2023-03-17 23:50:04 +00:00
|
|
|
else
|
2023-11-10 22:26:16 +00:00
|
|
|
cache->addRecord(keyOwnerJid, data, txn);
|
2023-03-17 23:50:04 +00:00
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
txn.commit();
|
2023-03-17 23:50:04 +00:00
|
|
|
emit trustLevelsChanged(keyOwnerJid, getSummary(keyOwnerJid));
|
2022-12-19 15:43:24 +00:00
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<QByteArray> Core::TrustHandler::ownKey(const QString& encryption) {
|
2022-12-19 15:43:24 +00:00
|
|
|
QByteArray res;
|
|
|
|
try {
|
|
|
|
res = ownKeys->getRecord(encryption);
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(res));
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::resetOwnKey(const QString& encryption) {
|
2022-12-19 15:43:24 +00:00
|
|
|
try {
|
|
|
|
ownKeys->removeRecord(encryption);
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2022-12-19 15:43:24 +00:00
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::setOwnKey(const QString& encryption, const QByteArray& keyId) {
|
2022-12-19 15:43:24 +00:00
|
|
|
ownKeys->forceRecord(encryption, keyId);
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<QXmpp::TrustSecurityPolicy> Core::TrustHandler::securityPolicy(const QString& encryption) {
|
2022-12-19 15:43:24 +00:00
|
|
|
QXmpp::TrustSecurityPolicy res;
|
|
|
|
try {
|
|
|
|
res = static_cast<QXmpp::TrustSecurityPolicy>(securityPolicies->getRecord(encryption));
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(res));
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::resetSecurityPolicy(const QString& encryption) {
|
2022-12-19 15:43:24 +00:00
|
|
|
try {
|
|
|
|
securityPolicies->removeRecord(encryption);
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
QXmppTask<void> Core::TrustHandler::setSecurityPolicy(
|
2022-12-19 15:43:24 +00:00
|
|
|
const QString& encryption,
|
|
|
|
QXmpp::TrustSecurityPolicy securityPolicy)
|
|
|
|
{
|
|
|
|
uint8_t pol = securityPolicy;
|
|
|
|
securityPolicies->forceRecord(encryption, pol);
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
Core::TrustHandler::Keys Core::TrustHandler::getKeys(Shared::EncryptionProtocol protocol, const QString& jid) const {
|
|
|
|
const QString& prt = Shared::TrustSummary::protocolKeys.at(protocol);
|
|
|
|
std::map<QString, KeyCache*>::const_iterator itr = keysByProtocol.find(prt);
|
2023-03-02 18:17:06 +00:00
|
|
|
if (itr != keysByProtocol.end()) {
|
|
|
|
try {
|
|
|
|
Keys map = itr->second->getRecord(jid);
|
|
|
|
return map;
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {
|
2023-03-02 18:17:06 +00:00
|
|
|
return Keys();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Keys();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 20:59:51 +00:00
|
|
|
Shared::TrustSummary Core::TrustHandler::getSummary(const QString& jid) const {
|
|
|
|
Shared::TrustSummary result;
|
|
|
|
for (const std::pair<const QString, KeyCache*>& pair : keysByProtocol) {
|
|
|
|
try {
|
|
|
|
Keys keys = pair.second->getRecord(jid);
|
|
|
|
Shared::EncryptionProtocol protocol = Shared::TrustSummary::protocolValues.at(pair.first);
|
2023-11-10 22:26:16 +00:00
|
|
|
for (const std::pair<const QByteArray, Shared::TrustLevel>& trust : keys)
|
2023-03-17 20:59:51 +00:00
|
|
|
result.increment(protocol, trust.second);
|
2023-11-10 22:26:16 +00:00
|
|
|
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {}
|
2023-03-17 20:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-03-14 19:49:58 +00:00
|
|
|
Shared::TrustLevel Core::TrustHandler::convert(Core::TrustHandler::TL level) {
|
2022-12-19 15:43:24 +00:00
|
|
|
switch (level) {
|
2023-01-01 17:25:51 +00:00
|
|
|
case QXmpp::TrustLevel::Undecided: return Shared::TrustLevel::undecided;
|
|
|
|
case QXmpp::TrustLevel::AutomaticallyDistrusted: return Shared::TrustLevel::automaticallyDistrusted;
|
|
|
|
case QXmpp::TrustLevel::ManuallyDistrusted: return Shared::TrustLevel::manuallyDistrusted;
|
|
|
|
case QXmpp::TrustLevel::AutomaticallyTrusted: return Shared::TrustLevel::automaticallyTrusted;
|
|
|
|
case QXmpp::TrustLevel::ManuallyTrusted: return Shared::TrustLevel::manuallyTrusted;
|
|
|
|
case QXmpp::TrustLevel::Authenticated: return Shared::TrustLevel::authenticated;
|
2023-03-14 19:49:58 +00:00
|
|
|
default: throw 2413; //never supposed to get here, switch case if complete, this line is just to suppress a warning
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 19:49:58 +00:00
|
|
|
Core::TrustHandler::TL Core::TrustHandler::convert(Shared::TrustLevel level) {
|
2022-12-19 15:43:24 +00:00
|
|
|
switch (level) {
|
2023-01-01 17:25:51 +00:00
|
|
|
case Shared::TrustLevel::undecided: return QXmpp::TrustLevel::Undecided;
|
|
|
|
case Shared::TrustLevel::automaticallyDistrusted: return QXmpp::TrustLevel::AutomaticallyDistrusted;
|
|
|
|
case Shared::TrustLevel::manuallyDistrusted: return QXmpp::TrustLevel::ManuallyDistrusted;
|
|
|
|
case Shared::TrustLevel::automaticallyTrusted: return QXmpp::TrustLevel::AutomaticallyTrusted;
|
|
|
|
case Shared::TrustLevel::manuallyTrusted: return QXmpp::TrustLevel::ManuallyTrusted;
|
|
|
|
case Shared::TrustLevel::authenticated: return QXmpp::TrustLevel::Authenticated;
|
2023-03-14 19:49:58 +00:00
|
|
|
default: throw 2413; //never supposed to get here, switch case if complete, this line is just to suppress a warning
|
2022-12-19 15:43:24 +00:00
|
|
|
}
|
|
|
|
}
|