2021-05-12 09:33:35 +00:00
|
|
|
/*
|
|
|
|
* Created by victoria on 2021-05-12.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qomemo.h"
|
2021-05-12 12:00:41 +00:00
|
|
|
#include "sce.h"
|
|
|
|
#include <shared/qxmppfactories.h>
|
2021-05-12 09:33:35 +00:00
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
#include <QBuffer>
|
2021-05-12 09:33:35 +00:00
|
|
|
#include <QDebug>
|
2021-05-12 12:00:41 +00:00
|
|
|
#include <QDomDocument>
|
2021-05-12 09:33:35 +00:00
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
using namespace QXmpp::Factories;
|
2021-05-12 09:33:35 +00:00
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
QXmppElement QXmpp::Omemo::DeviceList::toXml() const {
|
2021-05-12 14:33:34 +00:00
|
|
|
auto element = createElement("list", "eu.siacs.conversations.axolotl");
|
2021-05-12 09:33:35 +00:00
|
|
|
|
|
|
|
for (const auto &d : devices) {
|
|
|
|
element.appendChild(d.toXml());
|
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
QXmppIq QXmpp::Omemo::DeviceList::toIq() const {
|
2021-05-12 09:33:35 +00:00
|
|
|
QXmppIq iq{};
|
|
|
|
|
|
|
|
iq.setType(QXmppIq::Set);
|
|
|
|
|
|
|
|
auto item = createElement("item");
|
|
|
|
item.appendChild(toXml());
|
|
|
|
|
|
|
|
auto publish = createElement("publish");
|
2021-05-12 14:33:34 +00:00
|
|
|
publish.setAttribute("node", "eu.siacs.conversations.axolotl.devicelist");
|
2021-05-12 09:33:35 +00:00
|
|
|
publish.appendChild(item);
|
|
|
|
|
|
|
|
auto pubSub = createElement("pubsub", "http://jabber.org/protocol/pubsub");
|
|
|
|
pubSub.appendChild(publish);
|
|
|
|
pubSub.appendChild(createOpenPublishOptions());
|
|
|
|
|
|
|
|
iq.extensions().push_back(pubSub);
|
|
|
|
|
|
|
|
return iq;
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
void QXmpp::Omemo::DeviceList::fromXml(const QXmppElement &element) {
|
2021-05-12 14:33:34 +00:00
|
|
|
if (!elementMatches(element, "list", "eu.siacs.conversations.axolotl"))
|
2021-05-12 09:33:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
devices.clear();
|
|
|
|
|
|
|
|
auto deviceElement = element.firstChildElement("device");
|
|
|
|
while (!deviceElement.isNull()) {
|
|
|
|
Device device{};
|
|
|
|
device.fromXml(deviceElement);
|
|
|
|
devices.push_back(device);
|
|
|
|
|
|
|
|
deviceElement = deviceElement.nextSiblingElement("device");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
QXmppElement QXmpp::Omemo::Device::toXml() const {
|
2021-05-12 09:33:35 +00:00
|
|
|
auto result = createElement("device");
|
|
|
|
result.setAttribute("id", QString::number(id));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
void QXmpp::Omemo::Device::fromXml(const QXmppElement &element) {
|
2021-05-12 09:33:35 +00:00
|
|
|
if (!elementMatches(element, "device"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
id = element.attribute("id").toInt();
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
QXmppElement QXmpp::Omemo::PreKey::toXml() const {
|
2021-05-12 14:33:34 +00:00
|
|
|
auto pk = createElement("preKeyPublic");
|
|
|
|
pk.setAttribute("preKeyId", QString::number(id));
|
2021-05-12 09:33:35 +00:00
|
|
|
// TODO: Base64
|
|
|
|
pk.setValue(data);
|
|
|
|
|
|
|
|
return pk;
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
void QXmpp::Omemo::PreKey::fromXml(const QXmppElement &element) {
|
2021-05-12 14:33:34 +00:00
|
|
|
if (!elementMatches(element, "preKeyPublic"))
|
2021-05-12 09:33:35 +00:00
|
|
|
return;
|
|
|
|
|
2021-05-12 14:33:34 +00:00
|
|
|
id = element.attribute("preKeyId").toInt();
|
2021-05-12 09:33:35 +00:00
|
|
|
// TODO: Base64
|
|
|
|
data = element.value();
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
QXmppElement QXmpp::Omemo::Bundle::toXml() const {
|
2021-05-12 14:33:34 +00:00
|
|
|
auto spkNode = createElement("signedPreKeyPublic");
|
|
|
|
spkNode.setAttribute("signedPreKeyId", QString::number(spkId));
|
2021-05-12 09:33:35 +00:00
|
|
|
spkNode.setValue(spk);
|
|
|
|
|
2021-05-12 14:33:34 +00:00
|
|
|
auto spksNode = createElement("signedPreKeySignature");
|
2021-05-12 09:33:35 +00:00
|
|
|
spksNode.setValue(spks);
|
|
|
|
|
2021-05-12 14:33:34 +00:00
|
|
|
auto ikNode = createElement("identityKey");
|
2021-05-12 09:33:35 +00:00
|
|
|
ikNode.setValue(ik);
|
|
|
|
|
|
|
|
auto prekeysNode = createElement("prekeys");
|
|
|
|
for (const auto &pk : prekeys) {
|
|
|
|
prekeysNode.appendChild(pk.toXml());
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:33:34 +00:00
|
|
|
auto result = createElement("bundle", "eu.siacs.conversations.axolotl");
|
2021-05-12 09:33:35 +00:00
|
|
|
result.appendChild(spkNode);
|
|
|
|
result.appendChild(spksNode);
|
|
|
|
result.appendChild(ikNode);
|
|
|
|
result.appendChild(prekeysNode);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:33:34 +00:00
|
|
|
QXmppIq QXmpp::Omemo::Bundle::toIq(int deviceId) const {
|
2021-05-12 09:33:35 +00:00
|
|
|
QXmppIq iq{};
|
|
|
|
|
|
|
|
iq.setType(QXmppIq::Set);
|
|
|
|
|
|
|
|
auto item = createElement("item");
|
|
|
|
item.appendChild(toXml());
|
|
|
|
|
|
|
|
auto publish = createElement("publish");
|
2021-05-12 14:33:34 +00:00
|
|
|
publish.setAttribute("node", QString("eu.siacs.conversations.axolotl.bundles:%s").arg(deviceId));
|
2021-05-12 09:33:35 +00:00
|
|
|
publish.appendChild(item);
|
|
|
|
|
|
|
|
auto pubSub = createElement("pubsub", "http://jabber.org/protocol/pubsub");
|
|
|
|
pubSub.appendChild(publish);
|
2021-05-12 14:33:34 +00:00
|
|
|
pubSub.appendChild(createOpenPublishOptions());
|
2021-05-12 09:33:35 +00:00
|
|
|
|
|
|
|
iq.extensions().push_back(pubSub);
|
|
|
|
|
|
|
|
return iq;
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
void QXmpp::Omemo::Bundle::fromXml(const QXmppElement &element) {
|
2021-05-12 14:33:34 +00:00
|
|
|
if (!elementMatches(element, "bundle", "eu.siacs.conversations.axolotl"))
|
2021-05-12 09:33:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
auto spkNode = element.firstChildElement("spk");
|
|
|
|
if (spkNode.isNull()) {
|
|
|
|
qWarning() << "'bundle': missing 'spk'";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
spk = spkNode.value();
|
|
|
|
spkId = spkNode.attribute("id").toInt();
|
|
|
|
|
|
|
|
auto spksNode = element.firstChildElement("spks");
|
|
|
|
if (spksNode.isNull()) {
|
|
|
|
qWarning() << "'bundle': missing 'spks'";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
spks = spksNode.value();
|
|
|
|
|
|
|
|
auto ikNode = element.firstChildElement("ik");
|
|
|
|
if (ikNode.isNull()) {
|
|
|
|
qWarning() << "'bundle': missing 'ik'";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ik = ikNode.value();
|
|
|
|
|
|
|
|
auto prekeysNode = element.firstChildElement("prekeys");
|
|
|
|
auto pkNode = prekeysNode.firstChildElement("pk");
|
|
|
|
|
|
|
|
prekeys.clear();
|
|
|
|
while (!pkNode.isNull()) {
|
|
|
|
PreKey pk{};
|
|
|
|
pk.fromXml(pkNode);
|
|
|
|
prekeys.push_back(pk);
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 12:00:41 +00:00
|
|
|
|
|
|
|
QXmppPubSubIq QXmpp::Omemo::Bundle::fetchDeviceBundleIq(int deviceId) {
|
|
|
|
QXmppPubSubIq iq{};
|
|
|
|
iq.setType(QXmppIq::Get);
|
2021-05-12 14:33:34 +00:00
|
|
|
iq.setQueryNode(QString("eu.siacs.conversations.axolotl.bundles:%1").arg(deviceId));
|
2021-05-12 12:00:41 +00:00
|
|
|
|
|
|
|
QXmppPubSubItem item{};
|
|
|
|
item.setId(QString::number(deviceId));
|
|
|
|
iq.setItems({item});
|
|
|
|
|
|
|
|
return iq;
|
|
|
|
}
|
|
|
|
|
|
|
|
QXmppElement QXmpp::Omemo::EncryptedMessage::header() const {
|
|
|
|
auto result = createElement("header");
|
|
|
|
result.setAttribute("sid", QString::number(fromDeviceId));
|
|
|
|
|
2021-05-12 14:33:34 +00:00
|
|
|
auto ivNode = createElement("iv");
|
|
|
|
ivNode.setValue(iv);
|
|
|
|
|
|
|
|
for (const auto &key : keys) {
|
|
|
|
result.appendChild(key.toXml());
|
|
|
|
}
|
|
|
|
|
2021-05-12 12:00:41 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QXmppElement QXmpp::Omemo::EncryptedMessage::toXml() const {
|
2021-05-12 14:33:34 +00:00
|
|
|
auto result = createElement("encrypted", "eu.siacs.conversations.axolotl");
|
2021-05-12 12:00:41 +00:00
|
|
|
|
|
|
|
result.appendChild(header());
|
2021-05-12 14:33:34 +00:00
|
|
|
// TODO: Payload is optional
|
|
|
|
result.appendChild(payload());
|
2021-05-12 12:00:41 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QXmppElement QXmpp::Omemo::EncryptedMessage::payload() const {
|
|
|
|
QBuffer buffer;
|
|
|
|
buffer.open(QIODevice::ReadWrite);
|
|
|
|
QXmlStreamWriter writer(&buffer);
|
|
|
|
message.toXml(&writer);
|
|
|
|
|
|
|
|
QDomDocument doc;
|
|
|
|
doc.setContent(buffer.data(), true);
|
|
|
|
|
|
|
|
QXmppElement root(doc.documentElement());
|
|
|
|
root.setTagName("payload");
|
|
|
|
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
QXmppElement QXmpp::Omemo::EncryptedMessage::content() const {
|
|
|
|
auto envelope = createElement("content", "urn:xmpp:sce:0");
|
|
|
|
|
|
|
|
envelope.appendChild(payload());
|
|
|
|
|
|
|
|
if (!from.isEmpty()) {
|
|
|
|
auto fromNode = createElement("from");
|
|
|
|
fromNode.setAttribute("jid", from);
|
|
|
|
envelope.appendChild(fromNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!to.isEmpty()) {
|
|
|
|
auto toNode = createElement("to");
|
|
|
|
toNode.setAttribute("jid", to);
|
|
|
|
envelope.appendChild(toNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!timestamp.isNull()) {
|
|
|
|
auto timeNode = createElement("time");
|
|
|
|
timeNode.setAttribute("stamp", timestamp.toString(Qt::DateFormat::ISODate));
|
|
|
|
envelope.appendChild(timeNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto rpad = createElement("rpad");
|
|
|
|
rpad.setValue(QXmpp::Sce::generatePadding());
|
|
|
|
envelope.appendChild(rpad);
|
|
|
|
|
|
|
|
return envelope;
|
|
|
|
}
|
|
|
|
|
|
|
|
QXmppElement QXmpp::Omemo::MessageKey::toXml() const {
|
|
|
|
auto result = createElement("key");
|
|
|
|
|
2021-05-12 14:33:34 +00:00
|
|
|
result.setAttribute("rid", QString::number(receivingDeviceId));
|
|
|
|
if (prekey)
|
|
|
|
result.setAttribute("prekey", "true");
|
2021-05-12 12:00:41 +00:00
|
|
|
|
|
|
|
result.setValue(key);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|