diff options
| author | Melvin Keskin <melvo@olomono.de> | 2021-09-02 23:37:24 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2021-09-03 16:16:14 +0200 |
| commit | 12c63eddded9f02369bd4e569f62acfdd95cc723 (patch) | |
| tree | de008ca97accd962c5d2ca54ba9302c2f952a0ea /src/base | |
| parent | 11db6293ced5c4665630ad3d728aba5751a3f658 (diff) | |
| download | qxmpp-12c63eddded9f02369bd4e569f62acfdd95cc723.tar.gz | |
Add QXmppOmemoEnvelope
Co-authored-by: Germán Márquez Mejía <mancho@olomono.de>
Diffstat (limited to 'src/base')
| -rw-r--r-- | src/base/QXmppOmemoData.cpp | 153 | ||||
| -rw-r--r-- | src/base/QXmppOmemoEnvelope.h | 67 |
2 files changed, 220 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; +} diff --git a/src/base/QXmppOmemoEnvelope.h b/src/base/QXmppOmemoEnvelope.h new file mode 100644 index 00000000..a8f0a0c1 --- /dev/null +++ b/src/base/QXmppOmemoEnvelope.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Author: + * Germán Márquez Mejía + * Melvin Keskin + * + * Source: + * https://github.com/qxmpp-project/qxmpp + * + * This file is a part of QXmpp library. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + */ + +#ifndef QXMPPOMEMOENVELOPE_H +#define QXMPPOMEMOENVELOPE_H + +#include "QXmppGlobal.h" + +#include <QSharedDataPointer> + +class QDomElement; +class QXmppOmemoEnvelopePrivate; +class QXmlStreamWriter; + +class QXMPP_EXPORT QXmppOmemoEnvelope +{ +public: + QXmppOmemoEnvelope(); + QXmppOmemoEnvelope(const QXmppOmemoEnvelope &other); + ~QXmppOmemoEnvelope(); + + QXmppOmemoEnvelope &operator=(const QXmppOmemoEnvelope &other); + + uint32_t recipientDeviceId() const; + void setRecipientDeviceId(uint32_t id); + + bool isUsedForKeyExchange() const; + void setIsUsedForKeyExchange(bool isUsed); + + QByteArray data() const; + void setData(const QByteArray &data); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isOmemoEnvelope(const QDomElement &element); + +private: + QSharedDataPointer<QXmppOmemoEnvelopePrivate> d; +}; + +Q_DECLARE_TYPEINFO(QXmppOmemoEnvelope, Q_MOVABLE_TYPE); + +#endif // QXMPPOMEMOENVELOPE_H |
