From 12c63eddded9f02369bd4e569f62acfdd95cc723 Mon Sep 17 00:00:00 2001 From: Melvin Keskin Date: Thu, 2 Sep 2021 23:37:24 +0200 Subject: Add QXmppOmemoEnvelope MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Germán Márquez Mejía --- src/base/QXmppOmemoData.cpp | 153 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) (limited to 'src/base/QXmppOmemoData.cpp') diff --git a/src/base/QXmppOmemoData.cpp b/src/base/QXmppOmemoData.cpp index 84170c24..6fb2580d 100644 --- a/src/base/QXmppOmemoData.cpp +++ b/src/base/QXmppOmemoData.cpp @@ -26,6 +26,7 @@ #include "QXmppOmemoDeviceBundle.h" #include "QXmppOmemoDeviceElement.h" #include "QXmppOmemoDeviceList.h" +#include "QXmppOmemoEnvelope.h" #include "QXmppUtils.h" #include @@ -465,3 +466,155 @@ bool QXmppOmemoDeviceBundle::isOmemoDeviceBundle(const QDomElement &element) return element.tagName() == QStringLiteral("bundle") && element.namespaceURI() == ns_omemo_1; } + +/// +/// \class QXmppOmemoEnvelope +/// +/// \brief The QXmppOmemoEnvelope class represents an OMEMO envelope as +/// defined by \xep{0384, OMEMO Encryption}. +/// +/// \since QXmpp 1.5 +/// + +class QXmppOmemoEnvelopePrivate : public QSharedData +{ +public: + uint32_t recipientDeviceId = 0; + bool isUsedForKeyExchange = false; + QByteArray data; +}; + +/// +/// Constructs an OMEMO envelope. +/// +QXmppOmemoEnvelope::QXmppOmemoEnvelope() + : d(new QXmppOmemoEnvelopePrivate) +{ +} + +/// +/// Constructs a copy of \a other. +/// +/// \param other +/// +QXmppOmemoEnvelope::QXmppOmemoEnvelope(const QXmppOmemoEnvelope &other) = default; + +QXmppOmemoEnvelope::~QXmppOmemoEnvelope() = default; + +/// +/// Assigns \a other to this OMEMO envelope. +/// +/// \param other +/// +QXmppOmemoEnvelope &QXmppOmemoEnvelope::operator=(const QXmppOmemoEnvelope &other) = default; + +/// +/// Returns the ID of the recipient's device. +/// +/// The ID is 0 if it is unset. +/// +/// \return the recipient's device ID +/// +uint32_t QXmppOmemoEnvelope::recipientDeviceId() const +{ + return d->recipientDeviceId; +} + +/// +/// Sets the ID of the recipient's device. +/// +/// A valid ID must be at least 1 and at most 2^32-1. +/// +/// \param id recipient's device ID +/// +void QXmppOmemoEnvelope::setRecipientDeviceId(const uint32_t id) +{ + d->recipientDeviceId = id; +} + +/// +/// Returns true if a pre-key was used to prepare this envelope. +/// +/// The default is false. +/// +/// \return true if a pre-key was used to prepare this envelope, otherwise false +/// +bool QXmppOmemoEnvelope::isUsedForKeyExchange() const +{ + return d->isUsedForKeyExchange; +} + +/// +/// Sets whether a pre-key was used to prepare this envelope. +/// +/// \param isUsed whether a pre-key was used to prepare this envelope +/// +void QXmppOmemoEnvelope::setIsUsedForKeyExchange(const bool isUsed) +{ + d->isUsedForKeyExchange = isUsed; +} + +/// +/// Returns the BLOB containing the data for the underlying double ratchet library. +/// +/// It should be treated like an obscure BLOB being passed as is to the ratchet +/// library for further processing. +/// +/// \return the binary data for the ratchet library +/// +QByteArray QXmppOmemoEnvelope::data() const +{ + return d->data; +} + +/// +/// Sets the BLOB containing the data from the underlying double ratchet library. +/// +/// It should be treated like an obscure BLOB produced by the ratchet library. +/// +/// \param data binary data from the ratchet library +/// +void QXmppOmemoEnvelope::setData(const QByteArray &data) +{ + d->data = data; +} + +/// \cond +void QXmppOmemoEnvelope::parse(const QDomElement &element) +{ + d->recipientDeviceId = element.attribute("rid").toInt(); + + const auto isUsedForKeyExchange = element.attribute("kex"); + if (isUsedForKeyExchange == "true" || isUsedForKeyExchange == "1") { + d->isUsedForKeyExchange = true; + } + + d->data = QByteArray::fromBase64(element.text().toLatin1()); +} + +void QXmppOmemoEnvelope::toXml(QXmlStreamWriter *writer) const +{ + writer->writeStartElement("key"); + writer->writeAttribute("rid", QString::number(d->recipientDeviceId)); + + if (d->isUsedForKeyExchange) { + helperToXmlAddAttribute(writer, "kex", "true"); + } + + writer->writeCharacters(d->data.toBase64()); + writer->writeEndElement(); +} +/// \endcond + +/// +/// Determines whether the given DOM element is an OMEMO envelope. +/// +/// \param element DOM element being checked +/// +/// \return true if element is an OMEMO envelope, otherwise false +/// +bool QXmppOmemoEnvelope::isOmemoEnvelope(const QDomElement &element) +{ + return element.tagName() == QStringLiteral("key") && + element.namespaceURI() == ns_omemo_1; +} -- cgit v1.2.3