feat: wip omemo + key publish

This commit is contained in:
vae 2021-07-22 23:01:30 +03:00
parent 442ad37300
commit 6c9f1ab964
13 changed files with 147 additions and 96 deletions

View File

@ -22,7 +22,8 @@ using namespace QXmpp::Omemo;
Manager::Manager() Manager::Manager()
: deviceService{new DeviceService(this)}, : deviceService{new DeviceService(this)},
omemoVariant(new Variant::Conversations) { omemoVariant(new Variant::Conversations),
signalContext(new Signal::Context) {
connect(this, &Manager::deviceListReceived, deviceService.get(), &DeviceService::onDeviceListReceived); connect(this, &Manager::deviceListReceived, deviceService.get(), &DeviceService::onDeviceListReceived);
connect(this, &Manager::deviceListNotFound, deviceService.get(), &DeviceService::onDeviceListNotFound); connect(this, &Manager::deviceListNotFound, deviceService.get(), &DeviceService::onDeviceListNotFound);
connect(this, &Manager::deviceListNotFound, this, [this](const QString &jid) { connect(this, &Manager::deviceListNotFound, this, [this](const QString &jid) {
@ -42,7 +43,7 @@ bool QXmpp::Omemo::Manager::handleStanza(const QDomElement &stanza) {
std::cout << str.toStdString(); std::cout << str.toStdString();
if (handleDeviceList(stanza) || handleMissingDeviceList(stanza)) if (handleDeviceList(stanza) || handleMissingDeviceList(stanza) || handleEncryptedMessage(stanza))
return true; return true;
return false; return false;
@ -98,6 +99,19 @@ bool Manager::handleMissingDeviceList(const QDomElement &stanza) {
return true; return true;
} }
bool Manager::handleEncryptedMessage(const QDomElement &stanza) {
if (stanza.tagName() != "message")
return false;
auto encrypted = stanza.firstChildElement("encrypted");
if (encrypted.isNull())
return false;
qDebug() << "!!!! Got encrypted message!!";
return true;
}
void QXmpp::Omemo::Manager::setClient(QXmppClient *client) { void QXmpp::Omemo::Manager::setClient(QXmppClient *client) {
QXmppClientExtension::setClient(client); QXmppClientExtension::setClient(client);
@ -178,7 +192,7 @@ void Manager::generateDeviceForSelfIfNeeded(const DeviceList &currentList) {
qInfo() << "Generating device"; qInfo() << "Generating device";
auto deviceId = QRandomGenerator64{}.bounded(INT32_MAX); auto deviceId = QRandomGenerator64::system()->bounded(INT32_MAX);
db->saveActiveDeviceId(deviceId); db->saveActiveDeviceId(deviceId);
Device device{}; Device device{};
@ -186,11 +200,11 @@ void Manager::generateDeviceForSelfIfNeeded(const DeviceList &currentList) {
auto updatedList = currentList; auto updatedList = currentList;
auto bundle = Bundle();
updatedList.devices.push_back(device); updatedList.devices.push_back(device);
publishDeviceList(updatedList); publishDeviceList(updatedList);
auto bundle = generateAndSaveBundle(deviceId);
publishBundle(deviceId, bundle); publishBundle(deviceId, bundle);
} }
@ -213,6 +227,8 @@ Bundle Manager::generateAndSaveBundle(int deviceId) {
database->saveIdentityKey(deviceId, identityKey); database->saveIdentityKey(deviceId, identityKey);
database->saveIdentityKeySecret(deviceId, identityKeySecret); database->saveIdentityKeySecret(deviceId, identityKeySecret);
result.ik = identityKey;
// Generate SPK // Generate SPK
ec_key_pair *ecSpk; ec_key_pair *ecSpk;
curve_generate_key_pair(signalContext->temporaryGetContextUnsafeForRawAccessThatNeedsToBeWrapped(), &ecSpk); curve_generate_key_pair(signalContext->temporaryGetContextUnsafeForRawAccessThatNeedsToBeWrapped(), &ecSpk);
@ -227,11 +243,15 @@ Bundle Manager::generateAndSaveBundle(int deviceId) {
signal_buffer_len(spkPublic)); signal_buffer_len(spkPublic));
SignedPreKey spk{}; SignedPreKey spk{};
spk.id = 0; spk.id = 1;
spk.key.publicKey = QByteArray((const char *) signal_buffer_const_data(spkPublic), (int) signal_buffer_len(spkPublic)); spk.key.publicKey = QByteArray((const char *) signal_buffer_const_data(spkPublic), (int) signal_buffer_len(spkPublic));
spk.key.secretKey = QByteArray((const char *) signal_buffer_const_data(spkSecret), (int) signal_buffer_len(spkSecret)); spk.key.secretKey = QByteArray((const char *) signal_buffer_const_data(spkSecret), (int) signal_buffer_len(spkSecret));
spk.signature = QByteArray((const char *) signal_buffer_const_data(signature), (int) signal_buffer_len(signature)); spk.signature = QByteArray((const char *) signal_buffer_const_data(signature), (int) signal_buffer_len(signature));
result.spk = spk.key.publicKey;
result.spks = spk.signature;
result.spkId = 1;
// Generate 100 PK // Generate 100 PK
for (auto i = 1; i <= 100; ++i) { for (auto i = 1; i <= 100; ++i) {
ec_key_pair *currentPreKey; ec_key_pair *currentPreKey;
@ -247,6 +267,12 @@ Bundle Manager::generateAndSaveBundle(int deviceId) {
database->savePreKey(deviceId, i, preKey); database->savePreKey(deviceId, i, preKey);
PreKey pk{};
pk.data = preKey.publicKey;
pk.id = i;
result.prekeys.append(pk);
signal_buffer_free(pkPublic); signal_buffer_free(pkPublic);
signal_buffer_free(pkSecret); signal_buffer_free(pkSecret);
SIGNAL_UNREF(currentPreKey); SIGNAL_UNREF(currentPreKey);
@ -261,4 +287,6 @@ Bundle Manager::generateAndSaveBundle(int deviceId) {
signal_buffer_free(spkPublic); signal_buffer_free(spkPublic);
signal_buffer_free(spkSecret); signal_buffer_free(spkSecret);
SIGNAL_UNREF(ecSpk); SIGNAL_UNREF(ecSpk);
return result;
} }

View File

@ -8,6 +8,7 @@
#include "qomemo.h" #include "qomemo.h"
#include "variant/omemo_base.h" #include "variant/omemo_base.h"
#include <memory>
#include <QXmppClientExtension.h> #include <QXmppClientExtension.h>
namespace Signal { namespace Signal {
@ -29,6 +30,8 @@ namespace QXmpp::Omemo {
bool handleMissingDeviceList(const QDomElement& stanza); bool handleMissingDeviceList(const QDomElement& stanza);
bool handleEncryptedMessage(const QDomElement& stanza);
QSharedPointer<DeviceService> getDeviceService(); QSharedPointer<DeviceService> getDeviceService();
Bundle generateAndSaveBundle(int deviceId); Bundle generateAndSaveBundle(int deviceId);
@ -55,7 +58,7 @@ namespace QXmpp::Omemo {
private: private:
QSharedPointer<DeviceService> deviceService; QSharedPointer<DeviceService> deviceService;
QScopedPointer<Variant::Base> omemoVariant; QScopedPointer<Variant::Base> omemoVariant;
QScopedPointer<Signal::Context> signalContext; std::shared_ptr<Signal::Context> signalContext;
}; };
} // namespace QXmpp::Omemo } // namespace QXmpp::Omemo

View File

@ -3,12 +3,18 @@
*/ */
#include "context.h" #include "context.h"
#include "crypto/crypto.h"
using namespace Signal; using namespace Signal;
Context::Context() {} Context::Context() : cryptoProvider{ Signal::Crypto::createProvider() } {
signal_context_create(&ctx, nullptr);
signal_context_set_crypto_provider(ctx, &cryptoProvider);
}
Context::~Context() {} Context::~Context() {
signal_context_destroy(ctx);
}
std::unique_ptr<Crypto::ECKeyPair> Context::generateCurveKeyPair() { std::unique_ptr<Crypto::ECKeyPair> Context::generateCurveKeyPair() {
auto result = std::unique_ptr<Crypto::ECKeyPair>(); auto result = std::unique_ptr<Crypto::ECKeyPair>();

View File

@ -25,6 +25,7 @@ namespace Signal {
signal_context *temporaryGetContextUnsafeForRawAccessThatNeedsToBeWrapped(); signal_context *temporaryGetContextUnsafeForRawAccessThatNeedsToBeWrapped();
private: private:
signal_crypto_provider cryptoProvider{};
signal_context *ctx{nullptr}; signal_context *ctx{nullptr};
}; };

View File

@ -4,26 +4,6 @@
#include "sender_key_store.h" #include "sender_key_store.h"
void Signal::Store::SenderKeyStore::boundToContext(
signal_protocol_store_context *ctx) {
signal_protocol_sender_key_store store{};
store.user_data = nullptr;
store.destroy_func = nullptr;
store.load_sender_key = [](signal_buffer **record, signal_buffer **user_record,
const signal_protocol_sender_key_name *sender_key_name, void *ptr) {
return static_cast<SenderKeyStore *>(ptr)->loadSenderKey(record, user_record, sender_key_name);
};
store.store_sender_key = [](const signal_protocol_sender_key_name *sender_key_name, uint8_t *record,
size_t record_len, uint8_t *user_record, size_t user_record_len, void *ptr) {
return static_cast<SenderKeyStore *>(ptr)->storeSenderKey(sender_key_name, record, record_len, user_record,
user_record_len);
};
signal_protocol_store_context_set_sender_key_store(ctx, &store);
}
int Signal::Store::SenderKeyStore::loadSenderKey(signal_buffer **record, signal_buffer **user_record, int Signal::Store::SenderKeyStore::loadSenderKey(signal_buffer **record, signal_buffer **user_record,
const signal_protocol_sender_key_name *sender_key_name) { const signal_protocol_sender_key_name *sender_key_name) {
return 0; return 0;
@ -34,3 +14,17 @@ int Signal::Store::SenderKeyStore::storeSenderKey(const signal_protocol_sender_k
size_t user_record_len) { size_t user_record_len) {
return 0; return 0;
} }
void Signal::Store::SenderKeyStore::fillCallbacks(signal_protocol_sender_key_store &store) {
store.user_data = nullptr;
store.destroy_func = nullptr;
store.load_sender_key = [](signal_buffer **record, signal_buffer **user_record,
const signal_protocol_sender_key_name *sender_key_name, void *ptr) {
return static_cast<SenderKeyStore *>(ptr)->loadSenderKey(record, user_record, sender_key_name);
};
store.store_sender_key = [](const signal_protocol_sender_key_name *sender_key_name, uint8_t *record,
size_t record_len, uint8_t *user_record, size_t user_record_len, void *ptr) {
return static_cast<SenderKeyStore *>(ptr)->storeSenderKey(sender_key_name, record, record_len, user_record,
user_record_len);
};
}

View File

@ -10,12 +10,12 @@ namespace Signal::Store {
class SenderKeyStore { class SenderKeyStore {
public: public:
static void boundToContext(signal_protocol_store_context *ctx);
int storeSenderKey(const signal_protocol_sender_key_name *sender_key_name, uint8_t *record, size_t record_len, int storeSenderKey(const signal_protocol_sender_key_name *sender_key_name, uint8_t *record, size_t record_len,
uint8_t *user_record, size_t user_record_len); uint8_t *user_record, size_t user_record_len);
int loadSenderKey(signal_buffer **record, signal_buffer **user_record, int loadSenderKey(signal_buffer **record, signal_buffer **user_record,
const signal_protocol_sender_key_name *sender_key_name); const signal_protocol_sender_key_name *sender_key_name);
void fillCallbacks(signal_protocol_sender_key_store &store);
}; };
} // namespace Signal::Store } // namespace Signal::Store

View File

@ -4,38 +4,6 @@
#include "session_store.h" #include "session_store.h"
void Signal::Store::SessionStore::boundToContext(
signal_protocol_store_context *ctx) {
signal_protocol_session_store store{};
store.user_data = nullptr;
store.destroy_func = nullptr;
store.load_session_func = [](signal_buffer **record, signal_buffer **user_record,
const signal_protocol_address *address, void *ptr) {
return static_cast<SessionStore *>(ptr)->loadSession(record, user_record, address);
};
store.get_sub_device_sessions_func = [](signal_int_list **sessions, const char *name, size_t name_len, void *ptr) {
return static_cast<SessionStore *>(ptr)->getSubDeviceSessions(sessions, name, name_len);
};
store.store_session_func = [](const signal_protocol_address *address, uint8_t *record, size_t record_len,
uint8_t *user_record, size_t user_record_len, void *ptr) {
return static_cast<SessionStore *>(ptr)->storeSession(address, record, record_len, user_record,
user_record_len);
};
store.contains_session_func = [](const signal_protocol_address *address, void *ptr) {
return static_cast<SessionStore *>(ptr)->containsSession(address);
};
store.delete_session_func = [](const signal_protocol_address *address, void *ptr) {
return static_cast<SessionStore *>(ptr)->deleteSession(address);
};
store.delete_all_sessions_func = [](const char *name, size_t name_len, void *ptr) {
return static_cast<SessionStore *>(ptr)->deleteAllSessions(name, name_len);
};
signal_protocol_store_context_set_session_store(ctx, &store);
}
int Signal::Store::SessionStore::loadSession(signal_buffer **record, signal_buffer **user_record, int Signal::Store::SessionStore::loadSession(signal_buffer **record, signal_buffer **user_record,
const signal_protocol_address *address) { const signal_protocol_address *address) {
return 0; return 0;
@ -61,3 +29,29 @@ int Signal::Store::SessionStore::deleteSession(const signal_protocol_address *ad
int Signal::Store::SessionStore::deleteAllSessions(const char *name, size_t name_len) { int Signal::Store::SessionStore::deleteAllSessions(const char *name, size_t name_len) {
return 0; return 0;
} }
void Signal::Store::SessionStore::fillCallbacks(signal_protocol_session_store &store) {
store.user_data = nullptr;
store.destroy_func = nullptr;
store.load_session_func = [](signal_buffer **record, signal_buffer **user_record,
const signal_protocol_address *address, void *ptr) {
return static_cast<SessionStore *>(ptr)->loadSession(record, user_record, address);
};
store.get_sub_device_sessions_func = [](signal_int_list **sessions, const char *name, size_t name_len, void *ptr) {
return static_cast<SessionStore *>(ptr)->getSubDeviceSessions(sessions, name, name_len);
};
store.store_session_func = [](const signal_protocol_address *address, uint8_t *record, size_t record_len,
uint8_t *user_record, size_t user_record_len, void *ptr) {
return static_cast<SessionStore *>(ptr)->storeSession(address, record, record_len, user_record,
user_record_len);
};
store.contains_session_func = [](const signal_protocol_address *address, void *ptr) {
return static_cast<SessionStore *>(ptr)->containsSession(address);
};
store.delete_session_func = [](const signal_protocol_address *address, void *ptr) {
return static_cast<SessionStore *>(ptr)->deleteSession(address);
};
store.delete_all_sessions_func = [](const char *name, size_t name_len, void *ptr) {
return static_cast<SessionStore *>(ptr)->deleteAllSessions(name, name_len);
};
}

View File

@ -10,8 +10,6 @@ namespace Signal::Store {
class SessionStore { class SessionStore {
public: public:
static void boundToContext(signal_protocol_store_context *ctx);
int loadSession(signal_buffer **record, signal_buffer **user_record, const signal_protocol_address *address); int loadSession(signal_buffer **record, signal_buffer **user_record, const signal_protocol_address *address);
int getSubDeviceSessions(signal_int_list **sessions, const char *name, size_t name_len); int getSubDeviceSessions(signal_int_list **sessions, const char *name, size_t name_len);
int storeSession(const signal_protocol_address *address, uint8_t *record, size_t record_len, int storeSession(const signal_protocol_address *address, uint8_t *record, size_t record_len,
@ -19,6 +17,8 @@ namespace Signal::Store {
int containsSession(const signal_protocol_address *address); int containsSession(const signal_protocol_address *address);
int deleteSession(const signal_protocol_address *address); int deleteSession(const signal_protocol_address *address);
int deleteAllSessions(const char *name, size_t name_len); int deleteAllSessions(const char *name, size_t name_len);
void fillCallbacks(signal_protocol_session_store &store);
}; };
} // namespace Signal::Store } // namespace Signal::Store

View File

@ -4,33 +4,6 @@
#include "signed_pre_key_store.h" #include "signed_pre_key_store.h"
void Signal::Store::SignedPreKeyStore::boundToContext(
signal_protocol_store_context *ctx) {
signal_protocol_signed_pre_key_store store{};
store.user_data = nullptr;
store.destroy_func = nullptr;
store.load_signed_pre_key = [](signal_buffer **record, uint32_t signed_pre_key_id, void *ptr) {
return static_cast<SignedPreKeyStore *>(ptr)->loadSignedPreKey(
record, signed_pre_key_id);
};
store.store_signed_pre_key = [](uint32_t signed_pre_key_id, uint8_t *record, size_t record_len, void *ptr) {
return static_cast<SignedPreKeyStore *>(ptr)->storeSignedPreKey(
signed_pre_key_id, record, record_len);
};
store.contains_signed_pre_key = [](uint32_t signed_pre_key_id, void *ptr) {
return static_cast<SignedPreKeyStore *>(ptr)->containsSignedPreKey(
signed_pre_key_id);
};
store.remove_signed_pre_key = [](uint32_t signed_pre_key_id, void *ptr) {
return static_cast<SignedPreKeyStore *>(ptr)->removeSignedPreKey(
signed_pre_key_id);
};
signal_protocol_store_context_set_signed_pre_key_store(ctx, &store);
}
int Signal::Store::SignedPreKeyStore::loadSignedPreKey(signal_buffer **record, uint32_t signed_pre_key_id) { int Signal::Store::SignedPreKeyStore::loadSignedPreKey(signal_buffer **record, uint32_t signed_pre_key_id) {
return 0; return 0;
} }
@ -47,3 +20,24 @@ int Signal::Store::SignedPreKeyStore::containsSignedPreKey(uint32_t signed_pre_k
int Signal::Store::SignedPreKeyStore::removeSignedPreKey(uint32_t signed_pre_key_id) { int Signal::Store::SignedPreKeyStore::removeSignedPreKey(uint32_t signed_pre_key_id) {
return 0; return 0;
} }
void Signal::Store::SignedPreKeyStore::fillCallbacks(signal_protocol_signed_pre_key_store &store) {
store.user_data = nullptr;
store.destroy_func = nullptr;
store.load_signed_pre_key = [](signal_buffer **record, uint32_t signed_pre_key_id, void *ptr) {
return static_cast<SignedPreKeyStore *>(ptr)->loadSignedPreKey(
record, signed_pre_key_id);
};
store.store_signed_pre_key = [](uint32_t signed_pre_key_id, uint8_t *record, size_t record_len, void *ptr) {
return static_cast<SignedPreKeyStore *>(ptr)->storeSignedPreKey(
signed_pre_key_id, record, record_len);
};
store.contains_signed_pre_key = [](uint32_t signed_pre_key_id, void *ptr) {
return static_cast<SignedPreKeyStore *>(ptr)->containsSignedPreKey(
signed_pre_key_id);
};
store.remove_signed_pre_key = [](uint32_t signed_pre_key_id, void *ptr) {
return static_cast<SignedPreKeyStore *>(ptr)->removeSignedPreKey(
signed_pre_key_id);
};
}

View File

@ -10,11 +10,12 @@ namespace Signal::Store {
class SignedPreKeyStore { class SignedPreKeyStore {
public: public:
static void boundToContext(signal_protocol_store_context *ctx);
int loadSignedPreKey(signal_buffer **record, uint32_t signed_pre_key_id); int loadSignedPreKey(signal_buffer **record, uint32_t signed_pre_key_id);
int storeSignedPreKey(uint32_t signed_pre_key_id, uint8_t *record, size_t record_len); int storeSignedPreKey(uint32_t signed_pre_key_id, uint8_t *record, size_t record_len);
int containsSignedPreKey(uint32_t signed_pre_key_id); int containsSignedPreKey(uint32_t signed_pre_key_id);
int removeSignedPreKey(uint32_t signed_pre_key_id); int removeSignedPreKey(uint32_t signed_pre_key_id);
void fillCallbacks(signal_protocol_signed_pre_key_store &store);
}; };
} // namespace Signal::Store } // namespace Signal::Store

View File

@ -4,8 +4,20 @@
#include "store_context.h" #include "store_context.h"
Signal::Store::Context::Context(signal_context *global) { Signal::Store::Context::Context(signal_context *global) : identityKeyStore(), preKeyStore(), senderKeyStore(), sessionStore(), signedPreKeyStore() {
signal_protocol_store_context_create(&ctx, global); signal_protocol_store_context_create(&ctx, global);
identityKeyStore->fillCallbacks(iks);
preKeyStore->fillCallbacks(pks);
senderKeyStore->fillCallbacks(sks);
sessionStore->fillCallbacks(ss);
signedPreKeyStore->fillCallbacks(spks);
signal_protocol_store_context_set_identity_key_store(ctx, &iks);
signal_protocol_store_context_set_pre_key_store(ctx, &pks);
signal_protocol_store_context_set_sender_key_store(ctx, &sks);
signal_protocol_store_context_set_session_store(ctx, &ss);
signal_protocol_store_context_set_signed_pre_key_store(ctx, &spks);
} }
Signal::Store::Context::~Context() { Signal::Store::Context::~Context() {

View File

@ -6,6 +6,12 @@
#include <signal/signal_protocol.h> #include <signal/signal_protocol.h>
#include "identity_key_store.h"
#include "pre_key_store.h"
#include "sender_key_store.h"
#include "session_store.h"
#include "signed_pre_key_store.h"
namespace Signal::Store { namespace Signal::Store {
class Context { class Context {
@ -19,6 +25,18 @@ namespace Signal::Store {
private: private:
signal_protocol_store_context *ctx{nullptr}; signal_protocol_store_context *ctx{nullptr};
signal_protocol_identity_key_store iks{};
signal_protocol_pre_key_store pks{};
signal_protocol_sender_key_store sks{};
signal_protocol_session_store ss{};
signal_protocol_signed_pre_key_store spks{};
std::unique_ptr<IdentityKeyStore> identityKeyStore;
std::unique_ptr<PreKeyStore> preKeyStore;
std::unique_ptr<SenderKeyStore> senderKeyStore;
std::unique_ptr<SessionStore> sessionStore;
std::unique_ptr<SignedPreKeyStore> signedPreKeyStore;
}; };
} // namespace Signal::Store } // namespace Signal::Store

View File

@ -103,14 +103,14 @@ QXmppIq Variant::Conversations::bundleSetIq(int deviceId, const Bundle &bundle)
auto publish = createElement("publish"); auto publish = createElement("publish");
publish.setAttribute( publish.setAttribute(
"node", "node",
QString("eu.siacs.conversations.axolotl.bundles:%s").arg(deviceId)); QString("eu.siacs.conversations.axolotl.bundles:%1").arg(deviceId));
publish.appendChild(item); publish.appendChild(item);
auto pubSub = createElement("pubsub", "http://jabber.org/protocol/pubsub"); auto pubSub = createElement("pubsub", "http://jabber.org/protocol/pubsub");
pubSub.appendChild(publish); pubSub.appendChild(publish);
pubSub.appendChild(createOpenPublishOptions()); pubSub.appendChild(createOpenPublishOptions());
iq.extensions().push_back(pubSub); iq.setExtensions({ pubSub });
return iq; return iq;
} }