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
|
/*
* Copyright (C) 2008-2021 The QXmpp developers
*
* Author:
* Linus Jahn
*
* 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 "QXmppBitsOfBinaryContentId.h"
#include <QMap>
#include <QSharedData>
#include <QString>
#define CONTENTID_URL QStringLiteral("cid:")
#define CONTENTID_URL_LENGTH 4
#define CONTENTID_POSTFIX QStringLiteral("@bob.xmpp.org")
#define CONTENTID_POSTFIX_LENGTH 13
#define CONTENTID_HASH_SEPARATOR QStringLiteral("+")
static const QMap<QCryptographicHash::Algorithm, QString> HASH_ALGORITHMS = {
{ QCryptographicHash::Sha1, QStringLiteral("sha1") },
#ifndef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
{ QCryptographicHash::Md4, QStringLiteral("md4") },
{ QCryptographicHash::Md5, QStringLiteral("md5") },
{ QCryptographicHash::Sha224, QStringLiteral("sha224") },
{ QCryptographicHash::Sha256, QStringLiteral("sha256") },
{ QCryptographicHash::Sha384, QStringLiteral("sha384") },
{ QCryptographicHash::Sha512, QStringLiteral("sha512") },
{ QCryptographicHash::Sha3_224, QStringLiteral("sha3-224") },
{ QCryptographicHash::Sha3_256, QStringLiteral("sha3-256") },
{ QCryptographicHash::Sha3_384, QStringLiteral("sha3-384") },
{ QCryptographicHash::Sha3_512, QStringLiteral("sha3-512") },
#endif
};
class QXmppBitsOfBinaryContentIdPrivate : public QSharedData
{
public:
QXmppBitsOfBinaryContentIdPrivate();
QCryptographicHash::Algorithm algorithm;
QByteArray hash;
};
QXmppBitsOfBinaryContentIdPrivate::QXmppBitsOfBinaryContentIdPrivate()
: algorithm(QCryptographicHash::Sha1)
{
}
///
/// \class QXmppBitsOfBinaryContentId
///
/// QXmppBitsOfBinaryContentId represents a link to or an identifier of
/// \xep{0231, Bits of Binary} data.
///
/// \since QXmpp 1.2
///
///
/// Parses a \c QXmppBitsOfBinaryContentId from a \xep{0231, Bits of Binary}
/// \c cid: URL
///
/// In case parsing failed, the returned \c QXmppBitsOfBinaryContentId is
/// empty.
///
/// \see QXmppBitsOfBinaryContentId::fromContentId
///
QXmppBitsOfBinaryContentId QXmppBitsOfBinaryContentId::fromCidUrl(const QString &input)
{
if (input.startsWith(CONTENTID_URL))
return fromContentId(input.mid(CONTENTID_URL_LENGTH));
return {};
}
///
/// Parses a \c QXmppBitsOfBinaryContentId from a \xep{0231, Bits of Binary}
/// content id
///
/// In case parsing failed, the returned \c QXmppBitsOfBinaryContentId is
/// empty.
///
/// \note This does not allow \c cid: URLs to be passed. Use
/// \c QXmppBitsOfBinaryContentId::fromCidUrl for that purpose.
///
/// \see QXmppBitsOfBinaryContentId::fromCidUrl
///
QXmppBitsOfBinaryContentId QXmppBitsOfBinaryContentId::fromContentId(const QString &input)
{
if (input.startsWith(CONTENTID_URL) || !input.endsWith(CONTENTID_POSTFIX))
return {};
// remove '@bob.xmpp.org'
QString hashAndAlgoStr = input.left(input.size() - CONTENTID_POSTFIX_LENGTH);
// get size of hash algo id
QStringList algoAndHash = hashAndAlgoStr.split(CONTENTID_HASH_SEPARATOR);
if (algoAndHash.size() != 2)
return {};
QCryptographicHash::Algorithm algo = HASH_ALGORITHMS.key(algoAndHash.first(), QCryptographicHash::Algorithm(-1));
if (int(algo) == -1)
return {};
QXmppBitsOfBinaryContentId cid;
cid.setAlgorithm(algo);
cid.setHash(QByteArray::fromHex(algoAndHash.last().toUtf8()));
return cid;
}
///
/// Default contructor
///
QXmppBitsOfBinaryContentId::QXmppBitsOfBinaryContentId()
: d(new QXmppBitsOfBinaryContentIdPrivate)
{
}
///
/// Returns true, if two \c QXmppBitsOfBinaryContentId equal
///
bool QXmppBitsOfBinaryContentId::operator==(const QXmppBitsOfBinaryContentId &other) const
{
return d->algorithm == other.algorithm() && d->hash == other.hash();
}
/// Default destructor
QXmppBitsOfBinaryContentId::~QXmppBitsOfBinaryContentId() = default;
/// Default copy-constructor
QXmppBitsOfBinaryContentId::QXmppBitsOfBinaryContentId(const QXmppBitsOfBinaryContentId &cid) = default;
/// Default assignment operator
QXmppBitsOfBinaryContentId &QXmppBitsOfBinaryContentId::operator=(const QXmppBitsOfBinaryContentId &other) = default;
///
/// Returns a \xep{0231, Bits of Binary} content id
///
QString QXmppBitsOfBinaryContentId::toContentId() const
{
if (!isValid())
return {};
return HASH_ALGORITHMS.value(d->algorithm) +
CONTENTID_HASH_SEPARATOR +
d->hash.toHex() +
CONTENTID_POSTFIX;
}
///
/// Returns a \xep{0231, Bits of Binary} \c cid: URL
///
QString QXmppBitsOfBinaryContentId::toCidUrl() const
{
if (!isValid())
return {};
return toContentId().prepend(CONTENTID_URL);
}
///
/// Returns the hash value in binary form
///
QByteArray QXmppBitsOfBinaryContentId::hash() const
{
return d->hash;
}
///
/// Sets the hash value in binary form
///
void QXmppBitsOfBinaryContentId::setHash(const QByteArray &hash)
{
d->hash = hash;
}
///
/// Returns the hash algorithm used to calculate the \c hash value
///
/// The default value is \c QCryptographicHash::Sha1.
///
/// This currently supports MD4, MD5, SHA-1, SHA-2 (SHA224 - SHA512) and SHA-3
/// (SHA3-224 - SHA3-512).
///
QCryptographicHash::Algorithm QXmppBitsOfBinaryContentId::algorithm() const
{
return d->algorithm;
}
///
/// Sets the hash algorithm used to calculate the \c hash value
///
/// The default value is \c QCryptographicHash::Sha1.
///
/// This currently supports MD4, MD5, SHA-1, SHA-2 (SHA224 - SHA512) and SHA-3
/// (SHA3-224 - SHA3-512).
///
/// \note Only change this, if you know what you do. The XEP allows other
/// hashing algorithms than SHA-1 to be used, but not all clients support this.
/// Since in most cases the content id is not security relevant it is not a
/// problem to continue using SHA-1.
///
void QXmppBitsOfBinaryContentId::setAlgorithm(QCryptographicHash::Algorithm algo)
{
d->algorithm = algo;
}
///
/// Checks whether the content id is valid and can be serialized into a string.
///
/// \note Checking the hash length requires QXmpp to be built with Qt 5.12.0 or
/// later.
///
/// \returns True, if the set hashing algorithm is supported, a hash value is
/// set and its length is correct, false otherwise.
///
bool QXmppBitsOfBinaryContentId::isValid() const
{
#if QT_VERSION >= QT_VERSION_CHECK(5, 12, 0)
return !d->hash.isEmpty() &&
HASH_ALGORITHMS.contains(d->algorithm) &&
d->hash.length() == QCryptographicHash::hashLength(d->algorithm);
#else
return !d->hash.isEmpty() && HASH_ALGORITHMS.contains(d->algorithm);
#endif
}
///
/// Checks whether \c input is a Bits of Binary content id or \c cid: URL
///
/// \param input The string to be checked.
/// \param checkIsCidUrl If true, it only accepts \c cid: URLs.
///
/// \returns True, if \c input is valid.
///
bool QXmppBitsOfBinaryContentId::isBitsOfBinaryContentId(const QString &input, bool checkIsCidUrl)
{
return input.endsWith(CONTENTID_POSTFIX) &&
input.contains(CONTENTID_HASH_SEPARATOR) &&
(!checkIsCidUrl || input.startsWith(CONTENTID_URL));
}
|