Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/ssl/qsslpresharedkeyauthenticator.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2014 Governikus GmbH & Co. KG. | - | ||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||
25 | ** | - | ||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||
29 | ** | - | ||||||
30 | ** $QT_END_LICENSE$ | - | ||||||
31 | ** | - | ||||||
32 | ****************************************************************************/ | - | ||||||
33 | - | |||||||
34 | #include "qsslpresharedkeyauthenticator.h" | - | ||||||
35 | #include "qsslpresharedkeyauthenticator_p.h" | - | ||||||
36 | - | |||||||
37 | #include <QSharedData> | - | ||||||
38 | - | |||||||
39 | QT_BEGIN_NAMESPACE | - | ||||||
40 | - | |||||||
41 | /*! | - | ||||||
42 | \internal | - | ||||||
43 | */ | - | ||||||
44 | QSslPreSharedKeyAuthenticatorPrivate::QSslPreSharedKeyAuthenticatorPrivate() | - | ||||||
45 | : maximumIdentityLength(0), | - | ||||||
46 | maximumPreSharedKeyLength(0) | - | ||||||
47 | { | - | ||||||
48 | } never executed: end of block | 0 | ||||||
49 | - | |||||||
50 | /*! | - | ||||||
51 | \class QSslPreSharedKeyAuthenticator | - | ||||||
52 | - | |||||||
53 | \brief The QSslPreSharedKeyAuthenticator class provides authentication data for pre | - | ||||||
54 | shared keys (PSK) ciphersuites. | - | ||||||
55 | - | |||||||
56 | \inmodule QtNetwork | - | ||||||
57 | - | |||||||
58 | \reentrant | - | ||||||
59 | - | |||||||
60 | \ingroup network | - | ||||||
61 | \ingroup ssl | - | ||||||
62 | \ingroup shared | - | ||||||
63 | - | |||||||
64 | \since 5.5 | - | ||||||
65 | - | |||||||
66 | The QSslPreSharedKeyAuthenticator class is used by an SSL socket to provide | - | ||||||
67 | the required authentication data in a pre shared key (PSK) ciphersuite. | - | ||||||
68 | - | |||||||
69 | In a PSK handshake, the client must derive a key, which must match the key | - | ||||||
70 | set on the server. The exact algorithm of deriving the key depends on the | - | ||||||
71 | application; however, for this purpose, the server may send an \e{identity | - | ||||||
72 | hint} to the client. This hint, combined with other information (for | - | ||||||
73 | instance a passphrase), is then used by the client to construct the shared | - | ||||||
74 | key. | - | ||||||
75 | - | |||||||
76 | The QSslPreSharedKeyAuthenticator provides means to client applications for | - | ||||||
77 | completing the PSK handshake. The client application needs to connect a | - | ||||||
78 | slot to the QSslSocket::preSharedKeyAuthenticationRequired() signal: | - | ||||||
79 | - | |||||||
80 | \code | - | ||||||
81 | - | |||||||
82 | connect(socket, &QSslSocket::preSharedKeyAuthenticationRequired, | - | ||||||
83 | this, &AuthManager::handlePreSharedKeyAuthentication); | - | ||||||
84 | - | |||||||
85 | \endcode | - | ||||||
86 | - | |||||||
87 | The signal carries a QSslPreSharedKeyAuthenticator object containing the | - | ||||||
88 | identity hint the server sent to the client, and which must be filled with the | - | ||||||
89 | corresponding client identity and the derived key: | - | ||||||
90 | - | |||||||
91 | \code | - | ||||||
92 | - | |||||||
93 | void AuthManager::handlePreSharedKeyAuthentication(QSslPreSharedKeyAuthenticator *authenticator) | - | ||||||
94 | { | - | ||||||
95 | authenticator->setIdentity("My Qt App"); | - | ||||||
96 | - | |||||||
97 | const QByteArray key = deriveKey(authenticator->identityHint(), passphrase); | - | ||||||
98 | authenticator->setPreSharedKey(key); | - | ||||||
99 | } | - | ||||||
100 | - | |||||||
101 | \endcode | - | ||||||
102 | - | |||||||
103 | \note PSK ciphersuites are supported only when using OpenSSL 1.0.1 (or | - | ||||||
104 | greater) as the SSL backend. | - | ||||||
105 | - | |||||||
106 | \sa QSslSocket | - | ||||||
107 | */ | - | ||||||
108 | - | |||||||
109 | /*! | - | ||||||
110 | Constructs a default QSslPreSharedKeyAuthenticator object. | - | ||||||
111 | - | |||||||
112 | The identity hint, the identity and the key will be initialized to empty | - | ||||||
113 | byte arrays; the maximum length for both the identity and the key will be | - | ||||||
114 | initialized to 0. | - | ||||||
115 | */ | - | ||||||
116 | QSslPreSharedKeyAuthenticator::QSslPreSharedKeyAuthenticator() | - | ||||||
117 | : d(new QSslPreSharedKeyAuthenticatorPrivate) | - | ||||||
118 | { | - | ||||||
119 | } never executed: end of block | 0 | ||||||
120 | - | |||||||
121 | /*! | - | ||||||
122 | Destroys the QSslPreSharedKeyAuthenticator object. | - | ||||||
123 | */ | - | ||||||
124 | QSslPreSharedKeyAuthenticator::~QSslPreSharedKeyAuthenticator() | - | ||||||
125 | { | - | ||||||
126 | } | - | ||||||
127 | - | |||||||
128 | /*! | - | ||||||
129 | Constructs a QSslPreSharedKeyAuthenticator object as a copy of \a authenticator. | - | ||||||
130 | - | |||||||
131 | \sa operator=() | - | ||||||
132 | */ | - | ||||||
133 | QSslPreSharedKeyAuthenticator::QSslPreSharedKeyAuthenticator(const QSslPreSharedKeyAuthenticator &authenticator) | - | ||||||
134 | : d(authenticator.d) | - | ||||||
135 | { | - | ||||||
136 | } never executed: end of block | 0 | ||||||
137 | - | |||||||
138 | /*! | - | ||||||
139 | Assigns the QSslPreSharedKeyAuthenticator object \a authenticator to this object, | - | ||||||
140 | and returns a reference to the copy. | - | ||||||
141 | */ | - | ||||||
142 | QSslPreSharedKeyAuthenticator &QSslPreSharedKeyAuthenticator::operator=(const QSslPreSharedKeyAuthenticator &authenticator) | - | ||||||
143 | { | - | ||||||
144 | d = authenticator.d; | - | ||||||
145 | return *this; never executed: return *this; | 0 | ||||||
146 | } | - | ||||||
147 | - | |||||||
148 | /*! | - | ||||||
149 | \fn QSslPreSharedKeyAuthenticator &QSslPreSharedKeyAuthenticator::operator=(QSslPreSharedKeyAuthenticator &&authenticator) | - | ||||||
150 | - | |||||||
151 | Move-assigns the the QSslPreSharedKeyAuthenticator object \a authenticator to this | - | ||||||
152 | object, and returns a reference to the moved instance. | - | ||||||
153 | */ | - | ||||||
154 | - | |||||||
155 | /*! | - | ||||||
156 | \fn void QSslPreSharedKeyAuthenticator::swap(QSslPreSharedKeyAuthenticator &authenticator) | - | ||||||
157 | - | |||||||
158 | Swaps the QSslPreSharedKeyAuthenticator object \a authenticator with this object. | - | ||||||
159 | This operation is very fast and never fails. | - | ||||||
160 | */ | - | ||||||
161 | - | |||||||
162 | /*! | - | ||||||
163 | Returns the PSK identity hint as provided by the server. The interpretation | - | ||||||
164 | of this hint is left to the application. | - | ||||||
165 | */ | - | ||||||
166 | QByteArray QSslPreSharedKeyAuthenticator::identityHint() const | - | ||||||
167 | { | - | ||||||
168 | return d->identityHint; never executed: return d->identityHint; | 0 | ||||||
169 | } | - | ||||||
170 | - | |||||||
171 | /*! | - | ||||||
172 | Sets the PSK client identity (to be advised to the server) to \a identity. | - | ||||||
173 | - | |||||||
174 | \note it is possible to set an identity whose length is greater than | - | ||||||
175 | maximumIdentityLength(); in this case, only the first maximumIdentityLength() | - | ||||||
176 | bytes will be actually sent to the server. | - | ||||||
177 | - | |||||||
178 | \sa identity(), maximumIdentityLength() | - | ||||||
179 | */ | - | ||||||
180 | void QSslPreSharedKeyAuthenticator::setIdentity(const QByteArray &identity) | - | ||||||
181 | { | - | ||||||
182 | d->identity = identity; | - | ||||||
183 | } never executed: end of block | 0 | ||||||
184 | - | |||||||
185 | /*! | - | ||||||
186 | Returns the PSK client identity. | - | ||||||
187 | - | |||||||
188 | \sa setIdentity() | - | ||||||
189 | */ | - | ||||||
190 | QByteArray QSslPreSharedKeyAuthenticator::identity() const | - | ||||||
191 | { | - | ||||||
192 | return d->identity; never executed: return d->identity; | 0 | ||||||
193 | } | - | ||||||
194 | - | |||||||
195 | - | |||||||
196 | /*! | - | ||||||
197 | Returns the maximum length, in bytes, of the PSK client identity. | - | ||||||
198 | - | |||||||
199 | \note it is possible to set an identity whose length is greater than | - | ||||||
200 | maximumIdentityLength(); in this case, only the first maximumIdentityLength() | - | ||||||
201 | bytes will be actually sent to the server. | - | ||||||
202 | - | |||||||
203 | \sa setIdentity() | - | ||||||
204 | */ | - | ||||||
205 | int QSslPreSharedKeyAuthenticator::maximumIdentityLength() const | - | ||||||
206 | { | - | ||||||
207 | return d->maximumIdentityLength; never executed: return d->maximumIdentityLength; | 0 | ||||||
208 | } | - | ||||||
209 | - | |||||||
210 | - | |||||||
211 | /*! | - | ||||||
212 | Sets the pre shared key to \a preSharedKey. | - | ||||||
213 | - | |||||||
214 | \note it is possible to set a key whose length is greater than the | - | ||||||
215 | maximumPreSharedKeyLength(); in this case, only the first | - | ||||||
216 | maximumPreSharedKeyLength() bytes will be actually sent to the server. | - | ||||||
217 | - | |||||||
218 | \sa preSharedKey(), maximumPreSharedKeyLength(), QByteArray::fromHex() | - | ||||||
219 | */ | - | ||||||
220 | void QSslPreSharedKeyAuthenticator::setPreSharedKey(const QByteArray &preSharedKey) | - | ||||||
221 | { | - | ||||||
222 | d->preSharedKey = preSharedKey; | - | ||||||
223 | } never executed: end of block | 0 | ||||||
224 | - | |||||||
225 | /*! | - | ||||||
226 | Returns the pre shared key. | - | ||||||
227 | - | |||||||
228 | \sa setPreSharedKey() | - | ||||||
229 | */ | - | ||||||
230 | QByteArray QSslPreSharedKeyAuthenticator::preSharedKey() const | - | ||||||
231 | { | - | ||||||
232 | return d->preSharedKey; never executed: return d->preSharedKey; | 0 | ||||||
233 | } | - | ||||||
234 | - | |||||||
235 | /*! | - | ||||||
236 | Returns the maximum length, in bytes, of the pre shared key. | - | ||||||
237 | - | |||||||
238 | \note it is possible to set a key whose length is greater than the | - | ||||||
239 | maximumPreSharedKeyLength(); in this case, only the first | - | ||||||
240 | maximumPreSharedKeyLength() bytes will be actually sent to the server. | - | ||||||
241 | - | |||||||
242 | \sa setPreSharedKey() | - | ||||||
243 | */ | - | ||||||
244 | int QSslPreSharedKeyAuthenticator::maximumPreSharedKeyLength() const | - | ||||||
245 | { | - | ||||||
246 | return d->maximumPreSharedKeyLength; never executed: return d->maximumPreSharedKeyLength; | 0 | ||||||
247 | } | - | ||||||
248 | - | |||||||
249 | /*! | - | ||||||
250 | \relates QSslPreSharedKeyAuthenticator | - | ||||||
251 | \since 5.5 | - | ||||||
252 | - | |||||||
253 | Returns true if the authenticator object \a lhs is equal to \a rhs; false | - | ||||||
254 | otherwise. | - | ||||||
255 | - | |||||||
256 | Two authenticator objects are equal if and only if they have the same | - | ||||||
257 | identity hint, identity, pre shared key, maximum length for the identity | - | ||||||
258 | and maximum length for the pre shared key. | - | ||||||
259 | - | |||||||
260 | */ | - | ||||||
261 | bool operator==(const QSslPreSharedKeyAuthenticator &lhs, const QSslPreSharedKeyAuthenticator &rhs) | - | ||||||
262 | { | - | ||||||
263 | return ((lhs.d == rhs.d) || never executed: return ((lhs.d == rhs.d) || (lhs.d->identityHint == rhs.d->identityHint && lhs.d->identity == rhs.d->identity && lhs.d->maximumIdentityLength == rhs.d->maximumIdentityLength && lhs.d->preSharedKey == rhs.d->preSharedKey && lhs.d->maximumPreSharedKeyLength == rhs.d->maximumPreSharedKeyLength));
| 0 | ||||||
264 | (lhs.d->identityHint == rhs.d->identityHint && never executed: return ((lhs.d == rhs.d) || (lhs.d->identityHint == rhs.d->identityHint && lhs.d->identity == rhs.d->identity && lhs.d->maximumIdentityLength == rhs.d->maximumIdentityLength && lhs.d->preSharedKey == rhs.d->preSharedKey && lhs.d->maximumPreSharedKeyLength == rhs.d->maximumPreSharedKeyLength));
| 0 | ||||||
265 | lhs.d->identity == rhs.d->identity && never executed: return ((lhs.d == rhs.d) || (lhs.d->identityHint == rhs.d->identityHint && lhs.d->identity == rhs.d->identity && lhs.d->maximumIdentityLength == rhs.d->maximumIdentityLength && lhs.d->preSharedKey == rhs.d->preSharedKey && lhs.d->maximumPreSharedKeyLength == rhs.d->maximumPreSharedKeyLength));
| 0 | ||||||
266 | lhs.d->maximumIdentityLength == rhs.d->maximumIdentityLength && never executed: return ((lhs.d == rhs.d) || (lhs.d->identityHint == rhs.d->identityHint && lhs.d->identity == rhs.d->identity && lhs.d->maximumIdentityLength == rhs.d->maximumIdentityLength && lhs.d->preSharedKey == rhs.d->preSharedKey && lhs.d->maximumPreSharedKeyLength == rhs.d->maximumPreSharedKeyLength));
| 0 | ||||||
267 | lhs.d->preSharedKey == rhs.d->preSharedKey && never executed: return ((lhs.d == rhs.d) || (lhs.d->identityHint == rhs.d->identityHint && lhs.d->identity == rhs.d->identity && lhs.d->maximumIdentityLength == rhs.d->maximumIdentityLength && lhs.d->preSharedKey == rhs.d->preSharedKey && lhs.d->maximumPreSharedKeyLength == rhs.d->maximumPreSharedKeyLength));
| 0 | ||||||
268 | lhs.d->maximumPreSharedKeyLength == rhs.d->maximumPreSharedKeyLength)); never executed: return ((lhs.d == rhs.d) || (lhs.d->identityHint == rhs.d->identityHint && lhs.d->identity == rhs.d->identity && lhs.d->maximumIdentityLength == rhs.d->maximumIdentityLength && lhs.d->preSharedKey == rhs.d->preSharedKey && lhs.d->maximumPreSharedKeyLength == rhs.d->maximumPreSharedKeyLength));
| 0 | ||||||
269 | } | - | ||||||
270 | - | |||||||
271 | /*! | - | ||||||
272 | \fn bool operator!=(const QSslPreSharedKeyAuthenticator &lhs, const QSslPreSharedKeyAuthenticator &rhs) | - | ||||||
273 | \relates QSslPreSharedKeyAuthenticator | - | ||||||
274 | \since 5.5 | - | ||||||
275 | - | |||||||
276 | Returns true if the authenticator object \a lhs is different than \a rhs; | - | ||||||
277 | false otherwise. | - | ||||||
278 | - | |||||||
279 | */ | - | ||||||
280 | - | |||||||
281 | QT_END_NAMESPACE | - | ||||||
Source code | Switch to Preprocessed file |