aboutsummaryrefslogtreecommitdiff
path: root/src/base/QXmppOmemoData.cpp
diff options
context:
space:
mode:
authorMelvin Keskin <melvo@olomono.de>2021-09-02 23:37:24 +0200
committerLinus Jahn <lnj@kaidan.im>2021-09-03 16:16:14 +0200
commit12c63eddded9f02369bd4e569f62acfdd95cc723 (patch)
treede008ca97accd962c5d2ca54ba9302c2f952a0ea /src/base/QXmppOmemoData.cpp
parent11db6293ced5c4665630ad3d728aba5751a3f658 (diff)
downloadqxmpp-12c63eddded9f02369bd4e569f62acfdd95cc723.tar.gz
Add QXmppOmemoEnvelope
Co-authored-by: Germán Márquez Mejía <mancho@olomono.de>
Diffstat (limited to 'src/base/QXmppOmemoData.cpp')
-rw-r--r--src/base/QXmppOmemoData.cpp153
1 files changed, 153 insertions, 0 deletions
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 <QDomElement>
@@ -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;
+}