Debian Codegraph @

src/network/ssl/qssldiffiehellmanparameters.cpp

raw
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 Mikkel Krautz <[email protected]>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://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 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40
41 /*!
42 \class QSslDiffieHellmanParameters
43 \brief The QSslDiffieHellmanParameters class provides an interface for Diffie-Hellman parameters for servers.
44 \since 5.8
45
46 \reentrant
47 \ingroup network
48 \ingroup ssl
49 \ingroup shared
50 \inmodule QtNetwork
51
52 QSslDiffieHellmanParameters provides an interface for setting Diffie-Hellman parameters to servers based on QSslSocket.
53
54 \sa QSslSocket, QSslCipher, QSslConfiguration
55 */
56
57 #include "qssldiffiehellmanparameters.h"
58 #include "qssldiffiehellmanparameters_p.h"
59 #include "qsslsocket.h"
60 #include "qsslsocket_p.h"
61
62 #include <QtCore/qcoreapplication.h>
63 #include <QtCore/qatomic.h>
64 #include <QtCore/qbytearray.h>
65 #include <QtCore/qbytearraymatcher.h>
66 #include <QtCore/qiodevice.h>
67 #include <QtCore/qdebug.h>
68
69 QT_BEGIN_NAMESPACE
70
71 // The 2048-bit MODP group from RFC 3526
72 Q_AUTOTEST_EXPORT const char *qssl_dhparams_default_base64 =
73 "MIIBCAKCAQEA///////////JD9qiIWjCNMTGYouA3BzRKQJOCIpnzHQCC76mOxObIlFKCHmO"
74 "NATd75UZs806QxswKwpt8l8UN0/hNW1tUcJF5IW1dmJefsb0TELppjftawv/XLb0Brft7jhr"
75 "+1qJn6WunyQRfEsf5kkoZlHs5Fs9wgB8uKFjvwWY2kg2HFXTmmkWP6j9JM9fg2VdI9yjrZYc"
76 "YvNWIIVSu57VKQdwlpZtZww1Tkq8mATxdGwIyhghfDKQXkYuNs474553LBgOhgObJ4Oi7Aei"
77 "j7XFXfBvTFLJ3ivL9pVYFxg5lUl86pVq5RXSJhiY+gUQFXKOWoqsqmj//////////wIBAg==";
78
79 /*!
80 Returns the default QSslDiffieHellmanParameters used by QSslSocket.
81
82 This is currently the 2048-bit MODP group from RFC 3526.
83 */
84 QSslDiffieHellmanParameters QSslDiffieHellmanParameters::defaultParameters()
85 {
86 QSslDiffieHellmanParameters def;
87 def.d->derData = QByteArray::fromBase64(QByteArray(qssl_dhparams_default_base64));
88 return def;
89 }
90
91 /*!
92 Constructs an empty QSslDiffieHellmanParameters instance.
93
94 If an empty QSslDiffieHellmanParameters instance is set on a
95 QSslConfiguration object, Diffie-Hellman negotiation will
96 be disabled.
97
98 \sa isValid()
99 \sa QSslConfiguration
100 */
101 QSslDiffieHellmanParameters::QSslDiffieHellmanParameters()
102 : d(new QSslDiffieHellmanParametersPrivate)
103 {
104 d->ref.ref();
105 }
106
107 /*!
108 Constructs a QSslDiffieHellmanParameters object using
109 the byte array \a encoded in either PEM or DER form as specified by \a encoding.
110
111 Use the isValid() method on the returned object to
112 check whether the Diffie-Hellman parameters were valid and
113 loaded correctly.
114
115 \sa isValid()
116 \sa QSslConfiguration
117 */
118 QSslDiffieHellmanParameters QSslDiffieHellmanParameters::fromEncoded(const QByteArray &encoded, QSsl::EncodingFormat encoding)
119 {
120 QSslDiffieHellmanParameters result;
121 switch (encoding) {
122 case QSsl::Der:
123 result.d->decodeDer(encoded);
124 break;
125 case QSsl::Pem:
126 result.d->decodePem(encoded);
127 break;
128 }
129 return result;
130 }
131
132 /*!
133 Constructs a QSslDiffieHellmanParameters object by
134 reading from \a device in either PEM or DER form as specified by \a encoding.
135
136 Use the isValid() method on the returned object
137 to check whether the Diffie-Hellman parameters were valid
138 and loaded correctly.
139
140 In particular, if \a device is \nullptr or not open for reading, an invalid
141 object will be returned.
142
143 \sa isValid()
144 \sa QSslConfiguration
145 */
146 QSslDiffieHellmanParameters QSslDiffieHellmanParameters::fromEncoded(QIODevice *device, QSsl::EncodingFormat encoding)
147 {
148 if (device)
149 return fromEncoded(device->readAll(), encoding);
150 else
151 return QSslDiffieHellmanParameters();
152 }
153
154 /*!
155 Constructs an identical copy of \a other.
156 */
157 QSslDiffieHellmanParameters::QSslDiffieHellmanParameters(const QSslDiffieHellmanParameters &other)
158 : d(other.d)
159 {
160 if (d)
161 d->ref.ref();
162 }
163
164 /*!
165 \fn QSslDiffieHellmanParameters::QSslDiffieHellmanParameters(QSslDiffieHellmanParameters &&other)
166
167 Move-constructs from \a other.
168
169 \note The moved-from object \a other is placed in a partially-formed state, in which
170 the only valid operations are destruction and assignment of a new value.
171 */
172
173 /*!
174 Destroys the QSslDiffieHellmanParameters object.
175 */
176 QSslDiffieHellmanParameters::~QSslDiffieHellmanParameters()
177 {
178 if (d && !d->ref.deref())
179 delete d;
180 }
181
182 /*!
183 Copies the contents of \a other into this QSslDiffieHellmanParameters, making the two QSslDiffieHellmanParameters
184 identical.
185
186 Returns a reference to this QSslDiffieHellmanParameters.
187 */
188 QSslDiffieHellmanParameters &QSslDiffieHellmanParameters::operator=(const QSslDiffieHellmanParameters &other)
189 {
190 QSslDiffieHellmanParameters copy(other);
191 swap(copy);
192 return *this;
193 }
194
195 /*!
196 \fn QSslDiffieHellmanParameters &QSslDiffieHellmanParameters::operator=(QSslDiffieHellmanParameters &&other)
197
198 Move-assigns \a other to this QSslDiffieHellmanParameters instance.
199
200 \note The moved-from object \a other is placed in a partially-formed state, in which
201 the only valid operations are destruction and assignment of a new value.
202 */
203
204 /*!
205 \fn void QSslDiffieHellmanParameters::swap(QSslDiffieHellmanParameters &other)
206
207 Swaps this QSslDiffieHellmanParameters with \a other. This function is very fast and
208 never fails.
209 */
210
211 /*!
212 Returns \c true if this is a an empty QSslDiffieHellmanParameters instance.
213
214 Setting an empty QSslDiffieHellmanParameters instance on a QSslSocket-based
215 server will disable Diffie-Hellman key exchange.
216 */
217 bool QSslDiffieHellmanParameters::isEmpty() const noexcept
218 {
219 return d->derData.isNull() && d->error == QSslDiffieHellmanParameters::NoError;
220 }
221
222 /*!
223 Returns \c true if this is a valid QSslDiffieHellmanParameters; otherwise false.
224
225 This method should be used after constructing a QSslDiffieHellmanParameters
226 object to determine its validity.
227
228 If a QSslDiffieHellmanParameters object is not valid, you can use the error()
229 method to determine what error prevented the object from being constructed.
230
231 \sa error()
232 */
233 bool QSslDiffieHellmanParameters::isValid() const noexcept
234 {
235 return d->error == QSslDiffieHellmanParameters::NoError;
236 }
237
238 /*!
239 \enum QSslDiffieHellmanParameters::Error
240
241 Describes a QSslDiffieHellmanParameters error.
242
243 \value NoError No error occurred.
244
245 \value InvalidInputDataError The given input data could not be used to
246 construct a QSslDiffieHellmanParameters
247 object.
248
249 \value UnsafeParametersError The Diffie-Hellman parameters are unsafe
250 and should not be used.
251 */
252
253 /*!
254 Returns the error that caused the QSslDiffieHellmanParameters object
255 to be invalid.
256 */
257 QSslDiffieHellmanParameters::Error QSslDiffieHellmanParameters::error() const noexcept
258 {
259 return d->error;
260 }
261
262 /*!
263 Returns a human-readable description of the error that caused the
264 QSslDiffieHellmanParameters object to be invalid.
265 */
266 QString QSslDiffieHellmanParameters::errorString() const noexcept
267 {
268 switch (d->error) {
269 case QSslDiffieHellmanParameters::NoError:
270 return QCoreApplication::translate("QSslDiffieHellmanParameter", "No error");
271 case QSslDiffieHellmanParameters::InvalidInputDataError:
272 return QCoreApplication::translate("QSslDiffieHellmanParameter", "Invalid input data");
273 case QSslDiffieHellmanParameters::UnsafeParametersError:
274 return QCoreApplication::translate("QSslDiffieHellmanParameter", "The given Diffie-Hellman parameters are deemed unsafe");
275 }
276
277 Q_UNREACHABLE();
278 return QString();
279 }
280
281 /*!
282 \since 5.8
283 \relates QSslDiffieHellmanParameters
284
285 Returns \c true if \a lhs is equal to \a rhs; otherwise returns \c false.
286 */
287 bool operator==(const QSslDiffieHellmanParameters &lhs, const QSslDiffieHellmanParameters &rhs) noexcept
288 {
289 return lhs.d->derData == rhs.d->derData;
290 }
291
292 #ifndef QT_NO_DEBUG_STREAM
293 /*!
294 \since 5.8
295 \relates QSslDiffieHellmanParameters
296
297 Writes the set of Diffie-Hellman parameters in \a dhparam into the debug object \a debug for
298 debugging purposes.
299
300 The Diffie-Hellman parameters will be represented in Base64-encoded DER form.
301
302 \sa {Debugging Techniques}
303 */
304 QDebug operator<<(QDebug debug, const QSslDiffieHellmanParameters &dhparam)
305 {
306 QDebugStateSaver saver(debug);
307 debug.resetFormat().nospace();
308 debug << "QSslDiffieHellmanParameters(" << dhparam.d->derData.toBase64() << ')';
309 return debug;
310 }
311 #endif
312
313 /*!
314 \since 5.8
315 \relates QSslDiffieHellmanParameters
316
317 Returns an hash value for \a dhparam, using \a seed to seed
318 the calculation.
319 */
320 uint qHash(const QSslDiffieHellmanParameters &dhparam, uint seed) noexcept
321 {
322 return qHash(dhparam.d->derData, seed);
323 }
324
325 QT_END_NAMESPACE