2023-11-13 22:05:26 +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/>.
|
|
|
|
*/
|
2022-09-03 11:39:42 +00:00
|
|
|
|
2022-12-14 23:08:08 +00:00
|
|
|
#include <QDebug>
|
2022-09-03 11:39:42 +00:00
|
|
|
#include "omemohandler.h"
|
2022-12-14 23:08:08 +00:00
|
|
|
#include "core/account.h"
|
2023-01-29 17:26:54 +00:00
|
|
|
#include "core/adapterfunctions.h"
|
2022-12-14 23:08:08 +00:00
|
|
|
|
|
|
|
Core::OmemoHandler::OmemoHandler(Account* account) :
|
2023-03-10 18:43:31 +00:00
|
|
|
QObject(),
|
2022-12-14 23:08:08 +00:00
|
|
|
QXmppOmemoStorage(),
|
|
|
|
acc(account),
|
|
|
|
ownDevice(std::nullopt),
|
2022-12-19 15:43:24 +00:00
|
|
|
db(acc->getName() + "/omemo"),
|
2022-12-14 23:08:08 +00:00
|
|
|
meta(db.addCache<QString, QVariant>("meta")),
|
|
|
|
devices(db.addCache<QString, QHash<uint32_t, Device>>("devices")),
|
|
|
|
preKeyPairs(db.addCache<uint32_t, QByteArray>("preKeyPairs")),
|
2022-12-28 22:41:59 +00:00
|
|
|
signedPreKeyPairs(db.addCache<uint32_t, SignedPreKeyPair>("signedPreKeyPairs"))
|
2022-12-14 23:08:08 +00:00
|
|
|
{
|
|
|
|
db.open();
|
|
|
|
try {
|
|
|
|
QVariant own = meta->getRecord("ownDevice");
|
2022-12-26 22:01:01 +00:00
|
|
|
ownDevice = own.value<OwnDevice>();
|
2022-12-14 23:08:08 +00:00
|
|
|
qDebug() << "Successfully found own device omemo data for account" << acc->getName();
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& e) {
|
2022-12-14 23:08:08 +00:00
|
|
|
qDebug() << "No device omemo data was found for account" << acc->getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Core::OmemoHandler::~OmemoHandler() {
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
2022-12-26 22:01:01 +00:00
|
|
|
bool Core::OmemoHandler::hasOwnDevice() {
|
|
|
|
return ownDevice.has_value();
|
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<QXmppOmemoStorage::OmemoData> Core::OmemoHandler::allData() {
|
2022-12-14 23:08:08 +00:00
|
|
|
OmemoData data;
|
|
|
|
data.ownDevice = ownDevice;
|
|
|
|
|
2023-11-14 23:23:39 +00:00
|
|
|
LMDBAL::Transaction txn = db.beginReadOnlyTransaction();
|
|
|
|
std::map<uint32_t, QByteArray> pkeys = preKeyPairs->readAll(txn);
|
|
|
|
for (const std::pair<const uint32_t, QByteArray>& pair : pkeys)
|
2022-12-14 23:08:08 +00:00
|
|
|
data.preKeyPairs.insert(pair.first, pair.second);
|
|
|
|
|
2023-11-14 23:23:39 +00:00
|
|
|
std::map<uint32_t, SignedPreKeyPair> spre = signedPreKeyPairs->readAll(txn);
|
2022-12-28 22:41:59 +00:00
|
|
|
for (const std::pair<const uint32_t, SignedPreKeyPair>& pair : spre) {
|
|
|
|
QXmppOmemoStorage::SignedPreKeyPair qxpair = {pair.second.first, pair.second.second};
|
|
|
|
data.signedPreKeyPairs.insert(pair.first, qxpair);
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-11-14 23:23:39 +00:00
|
|
|
std::map<QString, QHash<uint32_t, Device>> devs = devices->readAll(txn);
|
|
|
|
for (const std::pair<const QString, QHash<uint32_t, Device>>& pair : devs)
|
2022-12-14 23:08:08 +00:00
|
|
|
data.devices.insert(pair.first, pair.second);
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask(std::move(data));
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::addDevice(const QString& jid, uint32_t deviceId, const QXmppOmemoStorage::Device& device) {
|
2022-12-14 23:08:08 +00:00
|
|
|
QHash<uint32_t, Device> devs;
|
2023-11-10 22:26:16 +00:00
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
2022-12-14 23:08:08 +00:00
|
|
|
bool had = true;
|
|
|
|
try {
|
2023-11-10 22:26:16 +00:00
|
|
|
devices->getRecord(jid, devs, txn);
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& error) {
|
2022-12-14 23:08:08 +00:00
|
|
|
had = false;
|
|
|
|
}
|
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
devs.insert(deviceId, device); //overwrites
|
|
|
|
if (had)
|
|
|
|
devices->changeRecord(jid, devs, txn);
|
|
|
|
else
|
|
|
|
devices->addRecord(jid, devs, txn);
|
2022-12-14 23:08:08 +00:00
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
txn.commit();
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::addPreKeyPairs(const QHash<uint32_t, QByteArray>& keyPairs) {
|
2023-11-10 22:26:16 +00:00
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
|
|
|
for (QHash<uint32_t, QByteArray>::const_iterator itr = keyPairs.begin(), end = keyPairs.end(); itr != end; ++itr)
|
|
|
|
preKeyPairs->forceRecord(itr.key(), itr.value(), txn);
|
2022-12-14 23:08:08 +00:00
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
txn.commit();
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::addSignedPreKeyPair(uint32_t keyId, const QXmppOmemoStorage::SignedPreKeyPair& keyPair) {
|
2022-12-28 22:41:59 +00:00
|
|
|
signedPreKeyPairs->forceRecord(keyId, std::make_pair(keyPair.creationDate, keyPair.data));
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::removeDevice(const QString& jid, uint32_t deviceId) {
|
2023-11-10 22:26:16 +00:00
|
|
|
LMDBAL::WriteTransaction txn = db.beginTransaction();
|
|
|
|
QHash<uint32_t, Device> devs = devices->getRecord(jid, txn);
|
2022-12-14 23:08:08 +00:00
|
|
|
devs.remove(deviceId);
|
2023-11-10 22:26:16 +00:00
|
|
|
if (devs.isEmpty())
|
|
|
|
devices->removeRecord(jid, txn);
|
|
|
|
else
|
|
|
|
devices->changeRecord(jid, devs, txn);
|
|
|
|
|
|
|
|
txn.commit();
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::removeDevices(const QString& jid) {
|
2022-12-14 23:08:08 +00:00
|
|
|
devices->removeRecord(jid);
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::removePreKeyPair(uint32_t keyId) {
|
2023-11-10 22:26:16 +00:00
|
|
|
try {
|
|
|
|
preKeyPairs->removeRecord(keyId);
|
|
|
|
} catch (const LMDBAL::NotFound& e) {
|
|
|
|
qDebug() << "Couldn't remove preKeyPair " << e.what();
|
|
|
|
}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::removeSignedPreKeyPair(uint32_t keyId) {
|
|
|
|
try {
|
|
|
|
signedPreKeyPairs->removeRecord(keyId);
|
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-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::setOwnDevice(const std::optional<OwnDevice>& device) {
|
2022-12-14 23:08:08 +00:00
|
|
|
bool had = ownDevice.has_value();
|
|
|
|
ownDevice = device;
|
|
|
|
if (ownDevice.has_value()) {
|
2023-11-10 22:26:16 +00:00
|
|
|
if (had)
|
2022-12-14 23:08:08 +00:00
|
|
|
meta->changeRecord("ownDevice", QVariant::fromValue(ownDevice.value()));
|
2023-11-10 22:26:16 +00:00
|
|
|
else
|
2022-12-14 23:08:08 +00:00
|
|
|
meta->addRecord("ownDevice", QVariant::fromValue(ownDevice.value()));
|
2023-11-10 22:26:16 +00:00
|
|
|
} else if (had) {
|
|
|
|
meta->removeRecord("ownDevice");
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
QXmppTask<void> Core::OmemoHandler::resetAll() {
|
2022-12-14 23:08:08 +00:00
|
|
|
ownDevice = std::nullopt;
|
|
|
|
db.drop();
|
|
|
|
|
2023-01-29 17:26:54 +00:00
|
|
|
return Core::makeReadyTask();
|
2022-12-14 23:08:08 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 18:17:06 +00:00
|
|
|
void Core::OmemoHandler::getDevices(const QString& jid, std::list<Shared::KeyInfo>& out) const {
|
|
|
|
QHash<uint32_t, Device> devs;
|
|
|
|
try {
|
2023-11-10 22:26:16 +00:00
|
|
|
devices->getRecord(jid, devs);
|
2023-03-27 18:45:29 +00:00
|
|
|
} catch (const LMDBAL::NotFound& error) {}
|
2023-03-02 18:17:06 +00:00
|
|
|
|
|
|
|
for (QHash<uint32_t, Device>::const_iterator itr = devs.begin(), end = devs.end(); itr != end; ++itr) {
|
|
|
|
const Device& dev = itr.value();
|
2023-11-12 22:55:32 +00:00
|
|
|
out.emplace_back(
|
|
|
|
itr.key(),
|
|
|
|
dev.keyId,
|
|
|
|
dev.label,
|
|
|
|
dev.removalFromDeviceListDate,
|
|
|
|
Shared::TrustLevel::undecided,
|
|
|
|
Shared::EncryptionProtocol::omemo2,
|
|
|
|
false
|
|
|
|
);
|
2023-03-02 18:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 18:43:31 +00:00
|
|
|
void Core::OmemoHandler::requestBundles(const QString& jid) {
|
|
|
|
QXmppTask<void> task = acc->om->buildMissingSessions({jid});
|
2023-11-10 22:26:16 +00:00
|
|
|
Contact* cnt = acc->rh->getContact(jid);
|
|
|
|
if (cnt)
|
|
|
|
cnt->omemoBundles = Shared::Possible::discovering;
|
|
|
|
|
2023-03-10 18:43:31 +00:00
|
|
|
task.then(this, std::bind(&OmemoHandler::onBundlesReceived, this, jid));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::OmemoHandler::requestOwnBundles() {
|
|
|
|
QXmppTask<void> task = acc->om->buildMissingSessions({acc->getBareJid()});
|
|
|
|
task.then(this, std::bind(&OmemoHandler::onOwnBundlesReceived, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::OmemoHandler::onBundlesReceived(const QString& jid) {
|
2023-11-13 22:05:26 +00:00
|
|
|
std::list<Shared::KeyInfo> keys = readKeys(jid);
|
2023-03-10 18:43:31 +00:00
|
|
|
|
2023-11-10 22:26:16 +00:00
|
|
|
Contact* cnt = acc->rh->getContact(jid);
|
|
|
|
if (cnt)
|
|
|
|
cnt->omemoBundles = Shared::Possible::present;
|
|
|
|
|
2023-03-10 18:43:31 +00:00
|
|
|
acc->delay->receivedBundles(jid, keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Core::OmemoHandler::onOwnBundlesReceived() {
|
2023-11-13 22:05:26 +00:00
|
|
|
std::list<Shared::KeyInfo> keys = readKeys(acc->getBareJid());
|
|
|
|
if (ownDevice)
|
|
|
|
keys.emplace_front(
|
|
|
|
ownDevice->id,
|
|
|
|
ownDevice->publicIdentityKey,
|
|
|
|
ownDevice->label,
|
|
|
|
QDateTime::currentDateTime(),
|
|
|
|
Shared::TrustLevel::authenticated,
|
|
|
|
Shared::EncryptionProtocol::omemo2,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
|
|
|
|
acc->delay->receivedOwnBundles(keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::list<Shared::KeyInfo> Core::OmemoHandler::readKeys(const QString& jid) {
|
2023-03-10 18:43:31 +00:00
|
|
|
std::list<Shared::KeyInfo> keys;
|
2023-11-13 22:05:26 +00:00
|
|
|
getDevices(jid, keys);
|
2023-03-17 20:59:51 +00:00
|
|
|
std::map<QByteArray, Shared::TrustLevel> trustLevels = acc->th->getKeys(Shared::EncryptionProtocol::omemo2, jid);
|
2023-03-10 18:43:31 +00:00
|
|
|
|
|
|
|
for (Shared::KeyInfo& key : keys) {
|
|
|
|
std::map<QByteArray, Shared::TrustLevel>::const_iterator itr = trustLevels.find(key.fingerPrint);
|
2023-11-13 22:05:26 +00:00
|
|
|
if (itr != trustLevels.end())
|
2023-03-10 18:43:31 +00:00
|
|
|
key.trustLevel = itr->second;
|
|
|
|
}
|
|
|
|
|
2023-11-13 22:05:26 +00:00
|
|
|
return keys;
|
2023-03-10 18:43:31 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 18:17:44 +00:00
|
|
|
void Core::OmemoHandler::onOmemoDeviceAdded(const QString& jid, uint32_t id) {
|
2023-08-15 15:28:25 +00:00
|
|
|
SHARED_UNUSED(id);
|
2023-03-15 18:17:44 +00:00
|
|
|
qDebug() << "OMEMO device added for" << jid;
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:43:24 +00:00
|
|
|
QDataStream & operator >> (QDataStream& in, QXmppOmemoStorage::Device& device) {
|
|
|
|
in >> device.label;
|
|
|
|
in >> device.keyId;
|
|
|
|
in >> device.session;
|
|
|
|
in >> device.unrespondedSentStanzasCount;
|
|
|
|
in >> device.unrespondedReceivedStanzasCount;
|
|
|
|
in >> device.removalFromDeviceListDate;
|
2022-12-14 23:08:08 +00:00
|
|
|
|
2022-12-19 15:43:24 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream & operator << (QDataStream& out, const QXmppOmemoStorage::Device& device) {
|
|
|
|
out << device.label;
|
|
|
|
out << device.keyId;
|
|
|
|
out << device.session;
|
|
|
|
out << device.unrespondedSentStanzasCount;
|
|
|
|
out << device.unrespondedReceivedStanzasCount;
|
|
|
|
out << device.removalFromDeviceListDate;
|
2022-12-14 23:08:08 +00:00
|
|
|
|
2022-12-19 15:43:24 +00:00
|
|
|
return out;
|
|
|
|
}
|
2022-12-14 23:08:08 +00:00
|
|
|
|
2022-12-19 15:43:24 +00:00
|
|
|
QDataStream & operator >> (QDataStream& in, QXmppOmemoStorage::OwnDevice& device) {
|
|
|
|
in >> device.id;
|
|
|
|
in >> device.label;
|
|
|
|
in >> device.privateIdentityKey;
|
|
|
|
in >> device.publicIdentityKey;
|
|
|
|
in >> device.latestSignedPreKeyId;
|
|
|
|
in >> device.latestPreKeyId;
|
2022-12-14 23:08:08 +00:00
|
|
|
|
2022-12-19 15:43:24 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream & operator << (QDataStream& out, const QXmppOmemoStorage::OwnDevice& device) {
|
|
|
|
out << device.id;
|
|
|
|
out << device.label;
|
|
|
|
out << device.privateIdentityKey;
|
|
|
|
out << device.publicIdentityKey;
|
|
|
|
out << device.latestSignedPreKeyId;
|
|
|
|
out << device.latestPreKeyId;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|