From eb63b18dcbb2ccdf811461a42f120e5d0ab82e88 Mon Sep 17 00:00:00 2001 From: Jonah Brüchert Date: Sat, 24 Sep 2022 17:58:44 +0200 Subject: EncryptedFileSource: Make public (#469) The sources possibly need to be saved by clients. --- src/base/QXmppEncryptedFileSource.cpp | 81 +++++++++++++++++++++++++---------- src/base/QXmppEncryptedFileSource.h | 56 ++++++++++++++++++++++++ src/base/QXmppEncryptedFileSource_p.h | 59 ------------------------- 3 files changed, 115 insertions(+), 81 deletions(-) create mode 100644 src/base/QXmppEncryptedFileSource.h delete mode 100644 src/base/QXmppEncryptedFileSource_p.h (limited to 'src/base') diff --git a/src/base/QXmppEncryptedFileSource.cpp b/src/base/QXmppEncryptedFileSource.cpp index d2c2b592..37170492 100644 --- a/src/base/QXmppEncryptedFileSource.cpp +++ b/src/base/QXmppEncryptedFileSource.cpp @@ -3,8 +3,9 @@ // // SPDX-License-Identifier: LGPL-2.1-or-later +#include "QXmppEncryptedFileSource.h" + #include "QXmppConstants_p.h" -#include "QXmppEncryptedFileSource_p.h" #include "QXmppHttpFileSource.h" #include @@ -13,6 +14,19 @@ #include /// \cond + +class QXmppEncryptedFileSourcePrivate : public QSharedData +{ +public: + QXmppEncryptedFileSource::Cipher cipher = QXmppEncryptedFileSource::Aes128GcmNopadding; + QByteArray key; + QByteArray iv; + QVector hashes; + QVector httpSources; +}; + +QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppEncryptedFileSource) + static QString cipherToString(QXmppEncryptedFileSource::Cipher cipher) { switch (cipher) { @@ -37,64 +51,87 @@ static std::optional cipherFromString(const QS } return {}; } +/// \endcond -QXmppEncryptedFileSource::QXmppEncryptedFileSource() = default; +/// +/// \class QXmppEncryptedFileSource +/// +/// \brief Represents an encrypted file source for file sharing. +/// +/// \since QXmpp 1.5 +/// +QXmppEncryptedFileSource::QXmppEncryptedFileSource() + : d(new QXmppEncryptedFileSourcePrivate()) +{ +} + +/// Returns the cipher that was used to encrypt the data in this file source QXmppEncryptedFileSource::Cipher QXmppEncryptedFileSource::cipher() const { - return m_cipher; + return d->cipher; } +/// Sets the cipher that was used to encrypt the data in this file source void QXmppEncryptedFileSource::setCipher(Cipher newCipher) { - m_cipher = newCipher; + d->cipher = newCipher; } +/// Returns the key that can be used to decrypt the data in this file source const QByteArray &QXmppEncryptedFileSource::key() const { - return m_key; + return d->key; } +/// Sets the key that was used to encrypt the data in this file source void QXmppEncryptedFileSource::setKey(const QByteArray &newKey) { - m_key = newKey; + d->key = newKey; } +/// Returns the Initialization vector that can be used to decrypt the data in this file source const QByteArray &QXmppEncryptedFileSource::iv() const { - return m_iv; + return d->iv; } +/// Sets the initialization vector that was used to encrypt the data in this file source void QXmppEncryptedFileSource::setIv(const QByteArray &newIv) { - m_iv = newIv; + d->iv = newIv; } +/// Returns the hashes of the file contained in this file source const QVector &QXmppEncryptedFileSource::hashes() const { - return m_hashes; + return d->hashes; } +/// Sets the hashes of the file contained in this file source void QXmppEncryptedFileSource::setHashes(const QVector &newHashes) { - m_hashes = newHashes; + d->hashes = newHashes; } +/// Returns the http sources that can be used to retrieve the encrypted data const QVector &QXmppEncryptedFileSource::httpSources() const { - return m_httpSources; + return d->httpSources; } +/// Sets the http sources containing the encrypted data void QXmppEncryptedFileSource::setHttpSources(const QVector &newHttpSources) { - m_httpSources = newHttpSources; + d->httpSources = newHttpSources; } +/// \cond bool QXmppEncryptedFileSource::parse(const QDomElement &el) { QString cipher = el.attribute(QStringLiteral("cipher")); if (auto parsedCipher = cipherFromString(cipher)) { - m_cipher = *parsedCipher; + d->cipher = *parsedCipher; } else { return false; } @@ -103,13 +140,13 @@ bool QXmppEncryptedFileSource::parse(const QDomElement &el) if (keyEl.isNull()) { return false; } - m_key = QByteArray::fromBase64(keyEl.text().toUtf8()); + d->key = QByteArray::fromBase64(keyEl.text().toUtf8()); auto ivEl = el.firstChildElement(QStringLiteral("iv")); if (ivEl.isNull()) { return false; } - m_iv = QByteArray::fromBase64(ivEl.text().toUtf8()); + d->iv = QByteArray::fromBase64(ivEl.text().toUtf8()); for (auto childEl = el.firstChildElement(QStringLiteral("hash")); !childEl.isNull(); @@ -118,7 +155,7 @@ bool QXmppEncryptedFileSource::parse(const QDomElement &el) if (!hash.parse(childEl)) { return false; } - m_hashes.push_back(std::move(hash)); + d->hashes.push_back(std::move(hash)); } auto sourcesEl = el.firstChildElement(QStringLiteral("sources")); @@ -130,7 +167,7 @@ bool QXmppEncryptedFileSource::parse(const QDomElement &el) childEl = childEl.nextSiblingElement(QStringLiteral("url-data"))) { QXmppHttpFileSource source; source.parse(childEl); - m_httpSources.push_back(std::move(source)); + d->httpSources.push_back(std::move(source)); } return true; @@ -140,15 +177,15 @@ void QXmppEncryptedFileSource::toXml(QXmlStreamWriter *writer) const { writer->writeStartElement(QStringLiteral("encrypted")); writer->writeDefaultNamespace(ns_esfs); - writer->writeAttribute(QStringLiteral("cipher"), cipherToString(m_cipher)); - writer->writeTextElement(QStringLiteral("key"), m_key.toBase64()); - writer->writeTextElement(QStringLiteral("iv"), m_iv.toBase64()); - for (const auto &hash : m_hashes) { + writer->writeAttribute(QStringLiteral("cipher"), cipherToString(d->cipher)); + writer->writeTextElement(QStringLiteral("key"), d->key.toBase64()); + writer->writeTextElement(QStringLiteral("iv"), d->iv.toBase64()); + for (const auto &hash : d->hashes) { hash.toXml(writer); } writer->writeStartElement(QStringLiteral("sources")); writer->writeDefaultNamespace(ns_sfs); - for (const auto &source : m_httpSources) { + for (const auto &source : d->httpSources) { source.toXml(writer); } writer->writeEndElement(); diff --git a/src/base/QXmppEncryptedFileSource.h b/src/base/QXmppEncryptedFileSource.h new file mode 100644 index 00000000..3d7a7c90 --- /dev/null +++ b/src/base/QXmppEncryptedFileSource.h @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: 2022 Linus Jahn +// SPDX-FileCopyrightText: 2022 Jonah Brüchert +// +// SPDX-License-Identifier: LGPL-2.1-or-later + +#ifndef QXMPPENCRYPTEDFILESOURCE_H +#define QXMPPENCRYPTEDFILESOURCE_H + +#include "QXmppGlobal.h" +#include "QXmppHash.h" +#include "QXmppHttpFileSource.h" + +#include +#include +#include + +class QXmppEncryptedFileSourcePrivate; + +// exported for tests +class QXMPP_EXPORT QXmppEncryptedFileSource +{ +public: + enum Cipher { + Aes128GcmNopadding, + Aes256GcmNopadding, + Aes256CbcPkcs7, + }; + + QXmppEncryptedFileSource(); + QXMPP_PRIVATE_DECLARE_RULE_OF_SIX(QXmppEncryptedFileSource) + + Cipher cipher() const; + void setCipher(Cipher newCipher); + + const QByteArray &key() const; + void setKey(const QByteArray &newKey); + + const QByteArray &iv() const; + void setIv(const QByteArray &newIv); + + const QVector &hashes() const; + void setHashes(const QVector &newHashes); + + const QVector &httpSources() const; + void setHttpSources(const QVector &newHttpSources); + + /// \cond + bool parse(const QDomElement &el); + void toXml(QXmlStreamWriter *writer) const; + /// \endcond + +private: + QSharedDataPointer d; +}; + +#endif // QXMPPENCRYPTEDFILESOURCE_H diff --git a/src/base/QXmppEncryptedFileSource_p.h b/src/base/QXmppEncryptedFileSource_p.h deleted file mode 100644 index 21270de2..00000000 --- a/src/base/QXmppEncryptedFileSource_p.h +++ /dev/null @@ -1,59 +0,0 @@ -// SPDX-FileCopyrightText: 2022 Linus Jahn -// SPDX-FileCopyrightText: 2022 Jonah Brüchert -// -// SPDX-License-Identifier: LGPL-2.1-or-later - -#ifndef QXMPPENCRYPTEDFILESOURCE_H -#define QXMPPENCRYPTEDFILESOURCE_H - -#include "QXmppGlobal.h" -#include "QXmppHash.h" -#include "QXmppHttpFileSource.h" - -#include -#include -#include - -class QXmppEncryptedFileSourcePrivate; - -// exported for tests -class QXMPP_EXPORT QXmppEncryptedFileSource -{ -public: - enum Cipher { - Aes128GcmNopadding, - Aes256GcmNopadding, - Aes256CbcPkcs7, - }; - - QXmppEncryptedFileSource(); - - Cipher cipher() const; - void setCipher(Cipher newCipher); - - const QByteArray &key() const; - void setKey(const QByteArray &newKey); - - const QByteArray &iv() const; - void setIv(const QByteArray &newIv); - - const QVector &hashes() const; - void setHashes(const QVector &newHashes); - - const QVector &httpSources() const; - void setHttpSources(const QVector &newHttpSources); - - /// \cond - bool parse(const QDomElement &el); - void toXml(QXmlStreamWriter *writer) const; - /// \endcond - -private: - Cipher m_cipher = Aes128GcmNopadding; - QByteArray m_key; - QByteArray m_iv; - QVector m_hashes; - QVector m_httpSources; -}; - -#endif // QXMPPENCRYPTEDFILESOURCE_H -- cgit v1.2.3