blob: 3b718cdab3a1489ea9b4b8567bb2f07b6ce286e9 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppEncryptedFileSource.h"
#include "QXmppConstants_p.h"
#include "QXmppHttpFileSource.h"
#include <optional>
#include <QDomElement>
#include <QXmlStreamWriter>
using namespace QXmpp;
/// \cond
class QXmppEncryptedFileSourcePrivate : public QSharedData
{
public:
Cipher cipher = Aes128GcmNoPad;
QByteArray key;
QByteArray iv;
QVector<QXmppHash> hashes;
QVector<QXmppHttpFileSource> httpSources;
};
QXMPP_PRIVATE_DEFINE_RULE_OF_SIX(QXmppEncryptedFileSource)
static QString cipherToString(Cipher cipher)
{
switch (cipher) {
case Aes128GcmNoPad:
return "urn:xmpp:ciphers:aes-128-gcm-nopadding:0";
case Aes256GcmNoPad:
return "urn:xmpp:ciphers:aes-256-gcm-nopadding:0";
case Aes256CbcPkcs7:
return "urn:xmpp:ciphers:aes-256-cbc-pkcs7:0";
}
Q_UNREACHABLE();
}
static std::optional<Cipher> cipherFromString(const QString &cipher)
{
if (cipher == "urn:xmpp:ciphers:aes-128-gcm-nopadding:0") {
return Aes128GcmNoPad;
} else if (cipher == "urn:xmpp:ciphers:aes-256-gcm-nopadding:0") {
return Aes256GcmNoPad;
} else if (cipher == "urn:xmpp:ciphers:aes-256-cbc-pkcs7:0") {
return Aes256CbcPkcs7;
}
return {};
}
/// \endcond
///
/// \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
Cipher QXmppEncryptedFileSource::cipher() const
{
return d->cipher;
}
/// Sets the cipher that was used to encrypt the data in this file source
void QXmppEncryptedFileSource::setCipher(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 d->key;
}
/// Sets the key that was used to encrypt the data in this file source
void QXmppEncryptedFileSource::setKey(const QByteArray &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 d->iv;
}
/// Sets the initialization vector that was used to encrypt the data in this file source
void QXmppEncryptedFileSource::setIv(const QByteArray &newIv)
{
d->iv = newIv;
}
/// Returns the hashes of the file contained in this file source
const QVector<QXmppHash> &QXmppEncryptedFileSource::hashes() const
{
return d->hashes;
}
/// Sets the hashes of the file contained in this file source
void QXmppEncryptedFileSource::setHashes(const QVector<QXmppHash> &newHashes)
{
d->hashes = newHashes;
}
/// Returns the http sources that can be used to retrieve the encrypted data
const QVector<QXmppHttpFileSource> &QXmppEncryptedFileSource::httpSources() const
{
return d->httpSources;
}
/// Sets the http sources containing the encrypted data
void QXmppEncryptedFileSource::setHttpSources(const QVector<QXmppHttpFileSource> &newHttpSources)
{
d->httpSources = newHttpSources;
}
/// \cond
bool QXmppEncryptedFileSource::parse(const QDomElement &el)
{
QString cipher = el.attribute(QStringLiteral("cipher"));
if (auto parsedCipher = cipherFromString(cipher)) {
d->cipher = *parsedCipher;
} else {
return false;
}
auto keyEl = el.firstChildElement(QStringLiteral("key"));
if (keyEl.isNull()) {
return false;
}
d->key = QByteArray::fromBase64(keyEl.text().toUtf8());
auto ivEl = el.firstChildElement(QStringLiteral("iv"));
if (ivEl.isNull()) {
return false;
}
d->iv = QByteArray::fromBase64(ivEl.text().toUtf8());
for (auto childEl = el.firstChildElement(QStringLiteral("hash"));
!childEl.isNull();
childEl = childEl.nextSiblingElement(QStringLiteral("hash"))) {
QXmppHash hash;
if (!hash.parse(childEl)) {
return false;
}
d->hashes.push_back(std::move(hash));
}
auto sourcesEl = el.firstChildElement(QStringLiteral("sources"));
if (sourcesEl.isNull()) {
return false;
}
for (auto childEl = sourcesEl.firstChildElement(QStringLiteral("url-data"));
!childEl.isNull();
childEl = childEl.nextSiblingElement(QStringLiteral("url-data"))) {
QXmppHttpFileSource source;
source.parse(childEl);
d->httpSources.push_back(std::move(source));
}
return true;
}
void QXmppEncryptedFileSource::toXml(QXmlStreamWriter *writer) const
{
writer->writeStartElement(QStringLiteral("encrypted"));
writer->writeDefaultNamespace(ns_esfs);
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 : d->httpSources) {
source.toXml(writer);
}
writer->writeEndElement();
writer->writeEndElement();
}
/// \endcond
|