diff options
| author | Melvin Keskin <melvo@olomono.de> | 2021-06-30 17:38:47 +0200 |
|---|---|---|
| committer | Linus Jahn <lnj@kaidan.im> | 2021-07-06 22:35:53 +0200 |
| commit | 0b947c682c27091dd4afd5900d8e339175e1aba0 (patch) | |
| tree | d880db35de8d3eaefb01ead0043b07901c5cda50 | |
| parent | 92427f63b3458fac76f64f2993db81d8c4c5d84c (diff) | |
| download | qxmpp-0b947c682c27091dd4afd5900d8e339175e1aba0.tar.gz | |
Add QXmppTrustMessageElement
| -rw-r--r-- | src/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/base/QXmppMessage.cpp | 35 | ||||
| -rw-r--r-- | src/base/QXmppMessage.h | 5 | ||||
| -rw-r--r-- | src/base/QXmppTrustMessageElement.cpp | 188 | ||||
| -rw-r--r-- | src/base/QXmppTrustMessageElement.h | 69 | ||||
| -rw-r--r-- | tests/qxmppmessage/tst_qxmppmessage.cpp | 30 | ||||
| -rw-r--r-- | tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp | 70 |
7 files changed, 399 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index cee15612..edd92556 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -60,6 +60,7 @@ set(INSTALL_HEADER_FILES base/QXmppStream.h base/QXmppStreamFeatures.h base/QXmppStun.h + base/QXmppTrustMessageElement.h base/QXmppTrustMessageKeyOwner.h base/QXmppUtils.h base/QXmppVCardIq.h @@ -147,6 +148,7 @@ set(SOURCE_FILES base/QXmppStreamInitiationIq.cpp base/QXmppStreamManagement.cpp base/QXmppStun.cpp + base/QXmppTrustMessageElement.cpp base/QXmppTrustMessageKeyOwner.cpp base/QXmppUtils.cpp base/QXmppVCardIq.cpp diff --git a/src/base/QXmppMessage.cpp b/src/base/QXmppMessage.cpp index 5186692d..f0e74735 100644 --- a/src/base/QXmppMessage.cpp +++ b/src/base/QXmppMessage.cpp @@ -28,6 +28,7 @@ #include "QXmppBitsOfBinaryDataList.h" #include "QXmppConstants_p.h" #include "QXmppMixInvitation.h" +#include "QXmppTrustMessageElement.h" #include "QXmppUtils.h" #include <optional> @@ -175,6 +176,9 @@ public: // XEP-0428: Fallback Indication bool isFallback; + + // XEP-0434: Trust Messages (TM) + std::optional<QXmppTrustMessageElement> trustMessageElement; }; QXmppMessagePrivate::QXmppMessagePrivate() @@ -1131,6 +1135,27 @@ void QXmppMessage::setIsFallback(bool isFallback) d->isFallback = isFallback; } +/// +/// Returns an included trust message element as defined by +/// \xep{0434, Trust Messages (TM)}. +/// +/// \since QXmpp 1.5 +/// +std::optional<QXmppTrustMessageElement> QXmppMessage::trustMessageElement() const +{ + return d->trustMessageElement; +} + +/// +/// Sets a trust message element as defined by \xep{0434, Trust Messages (TM)}. +/// +/// \since QXmpp 1.5 +/// +void QXmppMessage::setTrustMessageElement(const std::optional<QXmppTrustMessageElement> &trustMessageElement) +{ + d->trustMessageElement = trustMessageElement; +} + /// \cond void QXmppMessage::parse(const QDomElement &element) { @@ -1328,6 +1353,11 @@ bool QXmppMessage::parseExtension(const QDomElement &element) } else if (checkElement(element, QStringLiteral("fallback"), ns_fallback_indication)) { // XEP-0428: Fallback Indication d->isFallback = true; + } else if (QXmppTrustMessageElement::isTrustMessageElement(element)) { + // XEP-0434: Trust Messages (TM) + QXmppTrustMessageElement trustMessageElement; + trustMessageElement.parse(element); + d->trustMessageElement = trustMessageElement; } else { return false; } @@ -1524,4 +1554,9 @@ void QXmppMessage::serializeExtensions(QXmlStreamWriter *xmlWriter) const xmlWriter->writeDefaultNamespace(ns_fallback_indication); xmlWriter->writeEndElement(); } + + // XEP-0434: Trust Messages (TM) + if (d->trustMessageElement) { + d->trustMessageElement->toXml(xmlWriter); + } } diff --git a/src/base/QXmppMessage.h b/src/base/QXmppMessage.h index d717b226..edd0c5c1 100644 --- a/src/base/QXmppMessage.h +++ b/src/base/QXmppMessage.h @@ -36,6 +36,7 @@ class QXmppMessagePrivate; class QXmppBitsOfBinaryDataList; class QXmppMixInvitation; +class QXmppTrustMessageElement; /// /// \brief The QXmppMessage class represents an XMPP message. @@ -254,6 +255,10 @@ public: bool isFallback() const; void setIsFallback(bool isFallback); + // XEP-0434: Trust Messages (TM) + std::optional<QXmppTrustMessageElement> trustMessageElement() const; + void setTrustMessageElement(const std::optional<QXmppTrustMessageElement> &trustMessageElement); + /// \cond void parse(const QDomElement &element) override; void toXml(QXmlStreamWriter *writer) const override; diff --git a/src/base/QXmppTrustMessageElement.cpp b/src/base/QXmppTrustMessageElement.cpp new file mode 100644 index 00000000..b15dc6fe --- /dev/null +++ b/src/base/QXmppTrustMessageElement.cpp @@ -0,0 +1,188 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Author: + * 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. + * + */ + +#include "QXmppTrustMessageElement.h" + +#include "QXmppConstants_p.h" +#include "QXmppTrustMessageKeyOwner.h" + +#include <QDomElement> + +/// +/// \class QXmppTrustMessageElement +/// +/// \brief The QXmppTrustMessageElement class represents a trust message element +/// as defined by \xep{0434, Trust Messages (TM)}. +/// +/// \since QXmpp 1.5 +/// + +class QXmppTrustMessageElementPrivate : public QSharedData +{ +public: + QString usage; + QString encryption; + QList<QXmppTrustMessageKeyOwner> keyOwners; +}; + +/// +/// Constructs a trust message element. +/// +QXmppTrustMessageElement::QXmppTrustMessageElement() + : d(new QXmppTrustMessageElementPrivate) +{ +} + +/// +/// Constructs a copy of \a other. +/// +/// \param other +/// +QXmppTrustMessageElement::QXmppTrustMessageElement(const QXmppTrustMessageElement &other) = default; + +QXmppTrustMessageElement::~QXmppTrustMessageElement() = default; + +/// +/// Assigns \a other to this trust message element. +/// +/// \param other +/// +QXmppTrustMessageElement &QXmppTrustMessageElement::operator=(const QXmppTrustMessageElement &other) = default; + +/// +/// Returns the namespace of the trust management protocol. +/// +/// \return the trust management protocol namespace +/// +QString QXmppTrustMessageElement::usage() const +{ + return d->usage; +} + +/// +/// Sets the namespace of the trust management protocol. +/// +/// \param usage trust management protocol namespace +/// +void QXmppTrustMessageElement::setUsage(const QString &usage) +{ + d->usage = usage; +} + +/// +/// Returns the namespace of the keys' encryption protocol. +/// +/// \return the encryption protocol namespace +/// +QString QXmppTrustMessageElement::encryption() const +{ + return d->encryption; +} + +/// +/// Sets the namespace of the keys' encryption protocol. +/// +/// \param encryption encryption protocol namespace +/// +void QXmppTrustMessageElement::setEncryption(const QString &encryption) +{ + d->encryption = encryption; +} + +/// +/// Returns the key owners containing the corresponding information for +/// trusting or distrusting their keys. +/// +/// \return the owners of the keys for trusting or distrusting +/// +QList<QXmppTrustMessageKeyOwner> QXmppTrustMessageElement::keyOwners() const +{ + return d->keyOwners; +} + +/// +/// Sets the key owners containing the corresponding information for trusting or +/// distrusting their keys. +/// +/// \param keyOwners owners of the keys for trusting or distrusting +/// +void QXmppTrustMessageElement::setKeyOwners(const QList<QXmppTrustMessageKeyOwner> &keyOwners) +{ + d->keyOwners = keyOwners; +} + +/// +/// Adds a key owner containing the corresponding information for trusting or +/// distrusting the owners keys. +/// +/// \param keyOwner owner of the keys for trusting or distrusting +/// +void QXmppTrustMessageElement::addKeyOwner(const QXmppTrustMessageKeyOwner &keyOwner) +{ + d->keyOwners.append(keyOwner); +} + +/// \cond +void QXmppTrustMessageElement::parse(const QDomElement &element) +{ + d->usage = element.attribute("usage"); + d->encryption = element.attribute("encryption"); + + for (auto keyOwnerElement = element.firstChildElement("key-owner"); + !keyOwnerElement.isNull(); + keyOwnerElement = keyOwnerElement.nextSiblingElement("key-owner")) { + if (QXmppTrustMessageKeyOwner::isTrustMessageKeyOwner(keyOwnerElement)) { + QXmppTrustMessageKeyOwner keyOwner; + keyOwner.parse(keyOwnerElement); + d->keyOwners.append(keyOwner); + } + } +} + +void QXmppTrustMessageElement::toXml(QXmlStreamWriter *writer) const +{ + writer->writeStartElement("trust-message"); + writer->writeDefaultNamespace(ns_tm); + writer->writeAttribute("usage", d->usage); + writer->writeAttribute("encryption", d->encryption); + + for (const auto &keyOwner : d->keyOwners) { + keyOwner.toXml(writer); + } + + writer->writeEndElement(); +} +/// \endcond + +/// +/// Determines whether the given DOM element is a trust message element. +/// +/// \param element DOM element being checked +/// +/// \return true if element is a trust message element, otherwise false +/// +bool QXmppTrustMessageElement::isTrustMessageElement(const QDomElement &element) +{ + return element.tagName() == QStringLiteral("trust-message") && + element.namespaceURI() == ns_tm; +} diff --git a/src/base/QXmppTrustMessageElement.h b/src/base/QXmppTrustMessageElement.h new file mode 100644 index 00000000..6cb3b3bc --- /dev/null +++ b/src/base/QXmppTrustMessageElement.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2008-2021 The QXmpp developers + * + * Author: + * 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 QXMPPTRUSTMESSAGEELEMENT_H +#define QXMPPTRUSTMESSAGEELEMENT_H + +#include "QXmppGlobal.h" + +#include <QDomElement> +#include <QList> +#include <QSharedDataPointer> +#include <QXmlStreamWriter> + +class QXmppTrustMessageElementPrivate; +class QXmppTrustMessageKeyOwner; + +class QXMPP_EXPORT QXmppTrustMessageElement +{ +public: + QXmppTrustMessageElement(); + QXmppTrustMessageElement(const QXmppTrustMessageElement &other); + ~QXmppTrustMessageElement(); + + QXmppTrustMessageElement &operator=(const QXmppTrustMessageElement &other); + + QString usage() const; + void setUsage(const QString &usage); + + QString encryption() const; + void setEncryption(const QString &encryption); + + QList<QXmppTrustMessageKeyOwner> keyOwners() const; + void setKeyOwners(const QList<QXmppTrustMessageKeyOwner> &keyOwners); + void addKeyOwner(const QXmppTrustMessageKeyOwner &keyOwner); + + /// \cond + void parse(const QDomElement &element); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + + static bool isTrustMessageElement(const QDomElement &element); + +private: + QSharedDataPointer<QXmppTrustMessageElementPrivate> d; +}; + +Q_DECLARE_TYPEINFO(QXmppTrustMessageElement, Q_MOVABLE_TYPE); + +#endif // QXMPPTRUSTMESSAGEELEMENT_H diff --git a/tests/qxmppmessage/tst_qxmppmessage.cpp b/tests/qxmppmessage/tst_qxmppmessage.cpp index 42255c57..93ab7722 100644 --- a/tests/qxmppmessage/tst_qxmppmessage.cpp +++ b/tests/qxmppmessage/tst_qxmppmessage.cpp @@ -26,6 +26,7 @@ #include "QXmppBitsOfBinaryDataList.h" #include "QXmppMessage.h" #include "QXmppMixInvitation.h" +#include "QXmppTrustMessageElement.h" #include <optional> @@ -67,6 +68,7 @@ private slots: void testSlashMe_data(); void testSlashMe(); void testMixInvitation(); + void testTrustMessageElement(); }; void tst_QXmppMessage::testBasic_data() @@ -1101,5 +1103,33 @@ void tst_QXmppMessage::testMixInvitation() serializePacket(message, xml); } +void tst_QXmppMessage::testTrustMessageElement() +{ + const QByteArray xml( + "<message id=\"1\" to=\"alice@example.org/\" from=\"alice@example.org/A2\" type=\"chat\">" + "<store xmlns=\"urn:xmpp:hints\"/>" + "<trust-message xmlns=\"urn:xmpp:tm:0\" usage=\"urn:xmpp:atm:1\" encryption=\"urn:xmpp:omemo:1\">" + "<key-owner jid=\"alice@example.org\">" + "<trust>6850019d7ed0feb6d3823072498ceb4f616c6025586f8f666dc6b9c81ef7e0a4</trust>" + "<trust>221a4f8e228b72182b006e5ca527d3bddccf8d9e6feaf4ce96e1c451e8648020</trust>" + "</key-owner>" + "<key-owner jid=\"bob@example.com\">" + "<trust>623548d3835c6d33ef5cb680f7944ef381cf712bf23a0119dabe5c4f252cd02f</trust>" + "<distrust>b423f5088de9a924d51b31581723d850c7cc67d0a4fe6b267c3d301ff56d2413</distrust>" + "<distrust>d9f849b6b828309c5f2c8df4f38fd891887da5aaa24a22c50d52f69b4a80817e</distrust>" + "</key-owner>" + "</trust-message>" + "</message>"); + + QXmppMessage message1; + parsePacket(message1, xml); + QVERIFY(message1.trustMessageElement()); + serializePacket(message1, xml); + + QXmppMessage message2; + message2.setTrustMessageElement(QXmppTrustMessageElement()); + QVERIFY(message2.trustMessageElement()); +} + QTEST_MAIN(tst_QXmppMessage) #include "tst_qxmppmessage.moc" diff --git a/tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp b/tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp index 2eff6c1c..f8afb940 100644 --- a/tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp +++ b/tests/qxmpptrustmessages/tst_qxmpptrustmessages.cpp @@ -36,6 +36,9 @@ private slots: void testIsTrustMessageKeyOwner(); void testTrustMessageKeyOwner_data(); void testTrustMessageKeyOwner(); + void testIsTrustMessageElement_data(); + void testIsTrustMessageElement(); + void testTrustMessageElement(); }; void tst_QXmppTrustMessages::testIsTrustMessageKeyOwner_data() @@ -126,5 +129,72 @@ void tst_QXmppTrustMessages::testTrustMessageKeyOwner() serializePacket(keyOwner2, xml); } +void tst_QXmppTrustMessages::testIsTrustMessageElement_data() +{ + QTest::addColumn<QByteArray>("xml"); + QTest::addColumn<bool>("isValid"); + + QTest::newRow("valid") + << QByteArrayLiteral("<trust-message xmlns=\"urn:xmpp:tm:0\"/>") + << true; + QTest::newRow("invalidTag") + << QByteArrayLiteral("<invalid xmlns=\"urn:xmpp:tm:0\"/>") + << false; + QTest::newRow("invalidNamespace") + << QByteArrayLiteral("<trust-message xmlns=\"invalid\"/>") + << false; +} + +void tst_QXmppTrustMessages::testIsTrustMessageElement() +{ + QFETCH(QByteArray, xml); + QFETCH(bool, isValid); + + QCOMPARE(QXmppTrustMessageElement::isTrustMessageElement(xmlToDom(xml)), isValid); +} + +void tst_QXmppTrustMessages::testTrustMessageElement() +{ + const QByteArray xml(QByteArrayLiteral( + "<trust-message xmlns=\"urn:xmpp:tm:0\" usage=\"urn:xmpp:atm:1\" encryption=\"urn:xmpp:omemo:1\">" + "<key-owner jid=\"alice@example.org\"/>" + "<key-owner jid=\"bob@example.com\"/>" + "</trust-message>")); + + QXmppTrustMessageElement trustMessageElement1; + parsePacket(trustMessageElement1, xml); + QCOMPARE(trustMessageElement1.usage(), QStringLiteral("urn:xmpp:atm:1")); + QCOMPARE(trustMessageElement1.encryption(), QStringLiteral("urn:xmpp:omemo:1")); + QCOMPARE(trustMessageElement1.keyOwners().at(0).jid(), QStringLiteral("alice@example.org")); + QCOMPARE(trustMessageElement1.keyOwners().at(1).jid(), QStringLiteral("bob@example.com")); + serializePacket(trustMessageElement1, xml); + + QXmppTrustMessageKeyOwner keyOwner1; + keyOwner1.setJid(QStringLiteral("alice@example.org")); + QXmppTrustMessageKeyOwner keyOwner2; + keyOwner2.setJid(QStringLiteral("bob@example.com")); + + QXmppTrustMessageElement trustMessageElement2; + trustMessageElement2.setUsage(QStringLiteral("urn:xmpp:atm:1")); + trustMessageElement2.setEncryption(QStringLiteral("urn:xmpp:omemo:1")); + trustMessageElement2.setKeyOwners({ keyOwner1, keyOwner2 }); + QCOMPARE(trustMessageElement2.usage(), QStringLiteral("urn:xmpp:atm:1")); + QCOMPARE(trustMessageElement2.encryption(), QStringLiteral("urn:xmpp:omemo:1")); + QCOMPARE(trustMessageElement2.keyOwners().at(0).jid(), QStringLiteral("alice@example.org")); + QCOMPARE(trustMessageElement2.keyOwners().at(1).jid(), QStringLiteral("bob@example.com")); + serializePacket(trustMessageElement2, xml); + + QXmppTrustMessageElement trustMessageElement3; + trustMessageElement3.setUsage(QStringLiteral("urn:xmpp:atm:1")); + trustMessageElement3.setEncryption(QStringLiteral("urn:xmpp:omemo:1")); + trustMessageElement3.addKeyOwner(keyOwner1); + trustMessageElement3.addKeyOwner(keyOwner2); + QCOMPARE(trustMessageElement3.usage(), QStringLiteral("urn:xmpp:atm:1")); + QCOMPARE(trustMessageElement3.encryption(), QStringLiteral("urn:xmpp:omemo:1")); + QCOMPARE(trustMessageElement3.keyOwners().at(0).jid(), QStringLiteral("alice@example.org")); + QCOMPARE(trustMessageElement3.keyOwners().at(1).jid(), QStringLiteral("bob@example.com")); + serializePacket(trustMessageElement3, xml); +} + QTEST_MAIN(tst_QXmppTrustMessages) #include "tst_qxmpptrustmessages.moc" |
