ref(omemo): reformat qomemo/

This commit is contained in:
vae 2021-05-13 17:54:37 +03:00
parent 574210f5d9
commit bbeeee4c8a
Signed by: vae
GPG Key ID: A9A33351400E00E5
23 changed files with 447 additions and 455 deletions

View File

@ -1,12 +0,0 @@
* Generate device w/ keys
* PubSub set urn:xmpp:omemo:1:devices to announce new device
* PubSub set urn:xmpp:omemo:1:bundles to announce new key bundles
* PubSub get urn:xmpp:omemo:1:bundles to get user bundles
Sending a message:
* Create urn:xmpp:sce:0 with padding and content
* Add <encrypted xmlns='urn:xmpp:omemo:1'> with header (key list) and payload (b64)
Receiving a message:
* Check <keys> => <key>
* Decrypt (TODO)

View File

@ -8,23 +8,25 @@
#include <QString> #include <QString>
class QXmppPubSubIq; class QXmppPubSubIq;
class QXmppElement; class QXmppElement;
class QXmppIq; class QXmppIq;
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class PreKey { class PreKey {
public: public:
[[nodiscard]] QXmppElement toXml() const; [[nodiscard]] QXmppElement toXml() const;
/// Expects a <pk> /// Expects a <pk>
void fromXml(const QXmppElement &element); void fromXml(const QXmppElement &element);
int id; int id;
QString data; QString data;
}; };
class Bundle { class Bundle {
public: public:
[[nodiscard]] static QXmppPubSubIq fetchDeviceBundleIq(int deviceId); [[nodiscard]] static QXmppPubSubIq fetchDeviceBundleIq(int deviceId);
[[nodiscard]] QXmppElement toXml() const; [[nodiscard]] QXmppElement toXml() const;
@ -36,6 +38,6 @@ public:
QString spks; QString spks;
QString ik; QString ik;
QList<PreKey> prekeys; QList<PreKey> prekeys;
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -74,7 +74,7 @@ bool Database::saveIdentityKeySecret(int deviceId,
int Database::loadActiveDeviceId() { int Database::loadActiveDeviceId() {
MDB_val key, value; MDB_val key, value;
key.mv_data = (void *)"active"; key.mv_data = (void *) "active";
key.mv_size = sizeof("active"); key.mv_size = sizeof("active");
MDB_txn *txn; MDB_txn *txn;
@ -101,7 +101,7 @@ int Database::loadActiveDeviceId() {
bool Database::saveActiveDeviceId(int deviceId) { bool Database::saveActiveDeviceId(int deviceId) {
MDB_val key, value; MDB_val key, value;
key.mv_data = (void *)"active"; key.mv_data = (void *) "active";
key.mv_size = sizeof("active"); key.mv_size = sizeof("active");
value.mv_data = &deviceId; value.mv_data = &deviceId;

View File

@ -10,8 +10,8 @@
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class Database { class Database {
public: public:
explicit Database(QString jid); explicit Database(QString jid);
~Database(); ~Database();
Database(const Database &) = delete; Database(const Database &) = delete;
@ -29,12 +29,12 @@ public:
const QString jid; const QString jid;
private: private:
MDB_env *env{}; MDB_env *env{};
MDB_dbi dbiDevices{}; MDB_dbi dbiDevices{};
MDB_dbi dbiKeys{}; MDB_dbi dbiKeys{};
MDB_dbi dbiPreKeys{}; MDB_dbi dbiPreKeys{};
MDB_dbi dbiIdentityKeys{}; MDB_dbi dbiIdentityKeys{};
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -7,18 +7,19 @@
#include <QList> #include <QList>
class QXmppElement; class QXmppElement;
class QXmppIq; class QXmppIq;
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class Device { class Device {
public: public:
int id; int id;
}; };
class DeviceList { class DeviceList {
public: public:
QList<Device> devices; QList<Device> devices;
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -11,5 +11,4 @@ int QXmpp::Omemo::DeviceKeyStorage::generateDeviceId() {
return 1 + random.bounded(INT32_MAX - 1); return 1 + random.bounded(INT32_MAX - 1);
} }
QXmpp::Omemo::DeviceKeyStorage::DeviceKeyStorage(int deviceId) QXmpp::Omemo::DeviceKeyStorage::DeviceKeyStorage(int deviceId) : deviceId(deviceId) {}
: deviceId(deviceId) {}

View File

@ -6,13 +6,13 @@
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class DeviceKeyStorage { class DeviceKeyStorage {
public: public:
static int generateDeviceId(); static int generateDeviceId();
explicit DeviceKeyStorage(int deviceId); explicit DeviceKeyStorage(int deviceId);
const int deviceId; const int deviceId;
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -7,8 +7,7 @@
QXmpp::Omemo::DeviceService::DeviceService(QObject *parent) : QObject(parent) {} QXmpp::Omemo::DeviceService::DeviceService(QObject *parent) : QObject(parent) {}
void QXmpp::Omemo::DeviceService::onDeviceListReceived( void QXmpp::Omemo::DeviceService::onDeviceListReceived(const QString &jid, const QXmpp::Omemo::DeviceList &list) {
const QString &jid, const QXmpp::Omemo::DeviceList &list) {
for (const auto &device : list.devices) { for (const auto &device : list.devices) {
qInfo() << "Got device for" << jid << ":" << device.id; qInfo() << "Got device for" << jid << ":" << device.id;

View File

@ -11,19 +11,19 @@
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class DeviceList; class DeviceList;
class DeviceService : public QObject { class DeviceService : public QObject {
Q_OBJECT Q_OBJECT
public: public:
explicit DeviceService(QObject *parent); explicit DeviceService(QObject *parent);
public slots: public slots:
void onDeviceListReceived(const QString &jid, const DeviceList &list); void onDeviceListReceived(const QString &jid, const QXmpp::Omemo::DeviceList &list);
private: private:
QMap<QString, UserDeviceList> device_lists{}; QMap<QString, UserDeviceList> device_lists{};
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -11,17 +11,17 @@
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class MessageKey { class MessageKey {
public: public:
[[nodiscard]] QXmppElement toXml() const; [[nodiscard]] QXmppElement toXml() const;
int receivingDeviceId{}; int receivingDeviceId{};
bool prekey{}; bool prekey{};
QString key{}; QString key{};
}; };
class EncryptedMessage { class EncryptedMessage {
public: public:
[[nodiscard]] QXmppElement header() const; [[nodiscard]] QXmppElement header() const;
[[nodiscard]] QXmppElement content() const; [[nodiscard]] QXmppElement content() const;
[[nodiscard]] QXmppElement toXml() const; [[nodiscard]] QXmppElement toXml() const;
@ -37,6 +37,6 @@ public:
QString iv{}; QString iv{};
QXmppMessage message{}; QXmppMessage message{};
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -18,8 +18,7 @@ using namespace QXmpp::Omemo;
Manager::Manager() Manager::Manager()
: deviceService(new DeviceService(this)), : deviceService(new DeviceService(this)),
omemoVariant(new Variant::Conversations) { omemoVariant(new Variant::Conversations) {
connect(this, &Manager::deviceListReceived, deviceService.get(), connect(this, &Manager::deviceListReceived, deviceService.get(), &DeviceService::onDeviceListReceived);
&DeviceService::onDeviceListReceived);
} }
bool QXmpp::Omemo::Manager::handleStanza(const QDomElement &stanza) { bool QXmpp::Omemo::Manager::handleStanza(const QDomElement &stanza) {

View File

@ -12,27 +12,27 @@
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class Manager : public QXmppClientExtension { class Manager : public QXmppClientExtension {
Q_OBJECT; Q_OBJECT;
public: public:
Manager(); Manager();
~Manager() override = default; ~Manager() override = default;
bool handleStanza(const QDomElement &stanza) override; bool handleStanza(const QDomElement &stanza) override;
public slots: public slots:
void fetchOwnDevices(); void fetchOwnDevices();
signals: signals:
void deviceListReceived(const QString &jid, const DeviceList &list); void deviceListReceived(const QString &jid, const QXmpp::Omemo::DeviceList &list);
protected: protected:
void setClient(QXmppClient *client) override; void setClient(QXmppClient *client) override;
private: private:
QScopedPointer<DeviceService> deviceService; QScopedPointer<DeviceService> deviceService;
QScopedPointer<Variant::Base> omemoVariant; QScopedPointer<Variant::Base> omemoVariant;
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -6,12 +6,14 @@
#include <QRandomGenerator> #include <QRandomGenerator>
#define RPAD_ALPHABET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
constexpr int RPAD_MAX_LENGTH = 200; constexpr int RPAD_MAX_LENGTH = 200;
QString QXmpp::Sce::generatePadding() { QString QXmpp::Sce::generatePadding() {
QRandomGenerator random{}; QRandomGenerator random{};
QString result{}; QString result{};
QString alphabet{ QStringLiteral("!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~") }; QString alphabet{QStringLiteral(RPAD_ALPHABET)};
auto length = random.bounded(RPAD_MAX_LENGTH); auto length = random.bounded(RPAD_MAX_LENGTH);
result.resize(length); result.resize(length);

View File

@ -9,6 +9,6 @@
namespace QXmpp::Sce { namespace QXmpp::Sce {
QString generatePadding(); QString generatePadding();
} }

View File

@ -35,7 +35,8 @@ int Signal::Store::SignedPreKeyStore::loadSignedPreKey(signal_buffer **record, u
return 0; return 0;
} }
int Signal::Store::SignedPreKeyStore::storeSignedPreKey(uint32_t signed_pre_key_id, uint8_t *record, size_t record_len) { int
Signal::Store::SignedPreKeyStore::storeSignedPreKey(uint32_t signed_pre_key_id, uint8_t *record, size_t record_len) {
return 0; return 0;
} }

View File

@ -6,5 +6,4 @@
#include <utility> #include <utility>
QXmpp::Omemo::UserDeviceList::UserDeviceList(QString jid) QXmpp::Omemo::UserDeviceList::UserDeviceList(QString jid) : jid(std::move(jid)) {}
: jid(std::move(jid)) {}

View File

@ -8,11 +8,11 @@
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class UserDeviceList { class UserDeviceList {
public: public:
explicit UserDeviceList(QString jid); explicit UserDeviceList(QString jid);
const QString jid; const QString jid;
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -8,8 +8,8 @@
namespace QXmpp::Omemo::Variant { namespace QXmpp::Omemo::Variant {
class Conversations : public Base { class Conversations : public Base {
public: public:
~Conversations() override = default; ~Conversations() override = default;
QXmppElement deviceToXml(const Device &device) override; QXmppElement deviceToXml(const Device &device) override;
@ -18,6 +18,6 @@ public:
QXmppElement deviceListToXml(const DeviceList &deviceList) override; QXmppElement deviceListToXml(const DeviceList &deviceList) override;
DeviceList deviceListFromXml(const QXmppElement &xml) override; DeviceList deviceListFromXml(const QXmppElement &xml) override;
QXmppIq deviceListSetIq(const DeviceList &deviceList) override; QXmppIq deviceListSetIq(const DeviceList &deviceList) override;
}; };
} // namespace QXmpp::Omemo::Variant } // namespace QXmpp::Omemo::Variant

View File

@ -5,17 +5,19 @@
#pragma once #pragma once
class QXmppElement; class QXmppElement;
class QXmppIq; class QXmppIq;
namespace QXmpp::Omemo { namespace QXmpp::Omemo {
class Device; class Device;
class DeviceList;
namespace Variant { class DeviceList;
class Base { namespace Variant {
public:
class Base {
public:
virtual ~Base() = default; virtual ~Base() = default;
virtual QXmppElement deviceToXml(const Device &device) = 0; virtual QXmppElement deviceToXml(const Device &device) = 0;
@ -24,8 +26,8 @@ public:
virtual QXmppElement deviceListToXml(const DeviceList &deviceList) = 0; virtual QXmppElement deviceListToXml(const DeviceList &deviceList) = 0;
virtual DeviceList deviceListFromXml(const QXmppElement &xml) = 0; virtual DeviceList deviceListFromXml(const QXmppElement &xml) = 0;
virtual QXmppIq deviceListSetIq(const DeviceList &deviceList) = 0; virtual QXmppIq deviceListSetIq(const DeviceList &deviceList) = 0;
}; };
} // namespace Variant } // namespace Variant
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo