blob: d2c2b5929edf6d8cd72ef9a402e4ed7bd78ee23d (
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
|
// 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 "QXmppConstants_p.h"
#include "QXmppEncryptedFileSource_p.h"
#include "QXmppHttpFileSource.h"
#include <optional>
#include <QDomElement>
#include <QXmlStreamWriter>
/// \cond
static QString cipherToString(QXmppEncryptedFileSource::Cipher cipher)
{
switch (cipher) {
case QXmppEncryptedFileSource::Aes128GcmNopadding:
return "urn:xmpp:ciphers:aes-128-gcm-nopadding:0";
case QXmppEncryptedFileSource::Aes256GcmNopadding:
return "urn:xmpp:ciphers:aes-256-gcm-nopadding:0";
case QXmppEncryptedFileSource::Aes256CbcPkcs7:
return "urn:xmpp:ciphers:aes-256-cbc-pkcs7:0";
}
Q_UNREACHABLE();
}
static std::optional<QXmppEncryptedFileSource::Cipher> cipherFromString(const QString &cipher)
{
if (cipher == "urn:xmpp:ciphers:aes-128-gcm-nopadding:0") {
return QXmppEncryptedFileSource::Aes128GcmNopadding;
} else if (cipher == "urn:xmpp:ciphers:aes-256-gcm-nopadding:0") {
return QXmppEncryptedFileSource::Aes256GcmNopadding;
} else if (cipher == "urn:xmpp:ciphers:aes-256-cbc-pkcs7:0") {
return QXmppEncryptedFileSource::Aes256CbcPkcs7;
}
return {};
}
QXmppEncryptedFileSource::QXmppEncryptedFileSource() = default;
QXmppEncryptedFileSource::Cipher QXmppEncryptedFileSource::cipher() const
{
return m_cipher;
}
void QXmppEncryptedFileSource::setCipher(Cipher newCipher)
{
m_cipher = newCipher;
}
const QByteArray &QXmppEncryptedFileSource::key() const
{
return m_key;
}
void QXmppEncryptedFileSource::setKey(const QByteArray &newKey)
{
m_key = newKey;
}
const QByteArray &QXmppEncryptedFileSource::iv() const
{
return m_iv;
}
void QXmppEncryptedFileSource::setIv(const QByteArray &newIv)
{
m_iv = newIv;
}
const QVector<QXmppHash> &QXmppEncryptedFileSource::hashes() const
{
return m_hashes;
}
void QXmppEncryptedFileSource::setHashes(const QVector<QXmppHash> &newHashes)
{
m_hashes = newHashes;
}
const QVector<QXmppHttpFileSource> &QXmppEncryptedFileSource::httpSources() const
{
return m_httpSources;
}
void QXmppEncryptedFileSource::setHttpSources(const QVector<QXmppHttpFileSource> &newHttpSources)
{
m_httpSources = newHttpSources;
}
bool QXmppEncryptedFileSource::parse(const QDomElement &el)
{
QString cipher = el.attribute(QStringLiteral("cipher"));
if (auto parsedCipher = cipherFromString(cipher)) {
m_cipher = *parsedCipher;
} else {
return false;
}
auto keyEl = el.firstChildElement(QStringLiteral("key"));
if (keyEl.isNull()) {
return false;
}
m_key = QByteArray::fromBase64(keyEl.text().toUtf8());
auto ivEl = el.firstChildElement(QStringLiteral("iv"));
if (ivEl.isNull()) {
return false;
}
m_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;
}
m_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);
m_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(m_cipher));
writer->writeTextElement(QStringLiteral("key"), m_key.toBase64());
writer->writeTextElement(QStringLiteral("iv"), m_iv.toBase64());
for (const auto &hash : m_hashes) {
hash.toXml(writer);
}
writer->writeStartElement(QStringLiteral("sources"));
writer->writeDefaultNamespace(ns_sfs);
for (const auto &source : m_httpSources) {
source.toXml(writer);
}
writer->writeEndElement();
writer->writeEndElement();
}
/// \endcond
|