blob: 66831a186cf0e98109b67710932e50fdf9376847 (
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
// SPDX-FileCopyrightText: 2021 Melvin Keskin <melvo@olomono.de>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "QXmppTrustMessages.h"
#include "QXmppConstants_p.h"
#include "QXmppUtils.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)
{
}
/// Copy-constructor.
QXmppTrustMessageElement::QXmppTrustMessageElement(const QXmppTrustMessageElement &other) = default;
/// Move-constructor.
QXmppTrustMessageElement::QXmppTrustMessageElement(QXmppTrustMessageElement &&) = default;
QXmppTrustMessageElement::~QXmppTrustMessageElement() = default;
/// Assignment operator.
QXmppTrustMessageElement &QXmppTrustMessageElement::operator=(const QXmppTrustMessageElement &other) = default;
/// Move-assignment operator.
QXmppTrustMessageElement &QXmppTrustMessageElement::operator=(QXmppTrustMessageElement &&) = 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(QStringLiteral("usage"));
d->encryption = element.attribute(QStringLiteral("encryption"));
for (auto keyOwnerElement = element.firstChildElement(QStringLiteral("key-owner"));
!keyOwnerElement.isNull();
keyOwnerElement = keyOwnerElement.nextSiblingElement(QStringLiteral("key-owner"))) {
if (QXmppTrustMessageKeyOwner::isTrustMessageKeyOwner(keyOwnerElement)) {
QXmppTrustMessageKeyOwner keyOwner;
keyOwner.parse(keyOwnerElement);
d->keyOwners.append(keyOwner);
}
}
}
void QXmppTrustMessageElement::toXml(QXmlStreamWriter *writer) const
{
writer->writeStartElement(QStringLiteral("trust-message"));
writer->writeDefaultNamespace(ns_tm);
writer->writeAttribute(QStringLiteral("usage"), d->usage);
writer->writeAttribute(QStringLiteral("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;
}
///
/// \class QXmppTrustMessageKeyOwner
///
/// \brief The QXmppTrustMessageKeyOwner class represents a key owner of the
/// trust message as defined by \xep{0434, Trust Messages (TM)}.
///
/// \since QXmpp 1.5
///
class QXmppTrustMessageKeyOwnerPrivate : public QSharedData
{
public:
QString jid;
QList<QByteArray> trustedKeys;
QList<QByteArray> distrustedKeys;
};
///
/// Constructs a trust message key owner.
///
QXmppTrustMessageKeyOwner::QXmppTrustMessageKeyOwner()
: d(new QXmppTrustMessageKeyOwnerPrivate)
{
}
/// Copy constructor.
QXmppTrustMessageKeyOwner::QXmppTrustMessageKeyOwner(const QXmppTrustMessageKeyOwner &other) = default;
/// Copy constructor.
QXmppTrustMessageKeyOwner::QXmppTrustMessageKeyOwner(QXmppTrustMessageKeyOwner &&) = default;
QXmppTrustMessageKeyOwner::~QXmppTrustMessageKeyOwner() = default;
/// Assignment operator.
QXmppTrustMessageKeyOwner &QXmppTrustMessageKeyOwner::operator=(const QXmppTrustMessageKeyOwner &other) = default;
/// Assignment operator.
QXmppTrustMessageKeyOwner &QXmppTrustMessageKeyOwner::operator=(QXmppTrustMessageKeyOwner &&) = default;
///
/// Returns the bare JID of the key owner.
///
/// \return the key owner's bare JID
///
QString QXmppTrustMessageKeyOwner::jid() const
{
return d->jid;
}
///
/// Sets the bare JID of the key owner.
///
/// If a full JID is passed, it is converted into a bare JID.
///
/// \param jid key owner's bare JID
///
void QXmppTrustMessageKeyOwner::setJid(const QString &jid)
{
d->jid = QXmppUtils::jidToBareJid(jid);
}
///
/// Returns the IDs of the keys that are trusted.
///
/// \return the IDs of trusted keys
///
QList<QByteArray> QXmppTrustMessageKeyOwner::trustedKeys() const
{
return d->trustedKeys;
}
///
/// Sets the IDs of keys that are trusted.
///
/// \param keyIds IDs of trusted keys
///
void QXmppTrustMessageKeyOwner::setTrustedKeys(const QList<QByteArray> &keyIds)
{
d->trustedKeys = keyIds;
}
///
/// Returns the IDs of the keys that are distrusted.
///
/// \return the IDs of distrusted keys
///
QList<QByteArray> QXmppTrustMessageKeyOwner::distrustedKeys() const
{
return d->distrustedKeys;
}
///
/// Sets the IDs of keys that are distrusted.
///
/// \param keyIds IDs of distrusted keys
///
void QXmppTrustMessageKeyOwner::setDistrustedKeys(const QList<QByteArray> &keyIds)
{
d->distrustedKeys = keyIds;
}
/// \cond
void QXmppTrustMessageKeyOwner::parse(const QDomElement &element)
{
d->jid = element.attribute(QStringLiteral("jid"));
for (auto childElement = element.firstChildElement();
!childElement.isNull();
childElement = childElement.nextSiblingElement()) {
if (const auto tagName = childElement.tagName(); tagName == QStringLiteral("trust")) {
d->trustedKeys.append(QByteArray::fromBase64(childElement.text().toLatin1()));
} else if (tagName == QStringLiteral("distrust")) {
d->distrustedKeys.append(QByteArray::fromBase64(childElement.text().toLatin1()));
}
}
}
void QXmppTrustMessageKeyOwner::toXml(QXmlStreamWriter *writer) const
{
writer->writeStartElement(QStringLiteral("key-owner"));
writer->writeAttribute(QStringLiteral("jid"), d->jid);
for (const auto &keyIdentifier : d->trustedKeys) {
writer->writeTextElement(QStringLiteral("trust"), keyIdentifier.toBase64());
}
for (const auto &keyIdentifier : d->distrustedKeys) {
writer->writeTextElement(QStringLiteral("distrust"), keyIdentifier.toBase64());
}
writer->writeEndElement();
}
/// \endcond
///
/// Determines whether the given DOM element is a trust message key owner.
///
/// \param element DOM element being checked
///
/// \return true if element is a trust message key owner, otherwise false
///
bool QXmppTrustMessageKeyOwner::isTrustMessageKeyOwner(const QDomElement &element)
{
return element.tagName() == QStringLiteral("key-owner") &&
element.namespaceURI() == ns_tm;
}
|