1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtNetwork module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 |
|
---|
43 | //#define QSSLSOCKET_DEBUG
|
---|
44 |
|
---|
45 | /*!
|
---|
46 | \class QSslSocket
|
---|
47 | \brief The QSslSocket class provides an SSL encrypted socket for both
|
---|
48 | clients and servers.
|
---|
49 | \since 4.3
|
---|
50 |
|
---|
51 | \reentrant
|
---|
52 | \ingroup network
|
---|
53 | \ingroup ssl
|
---|
54 | \inmodule QtNetwork
|
---|
55 |
|
---|
56 | QSslSocket establishes a secure, encrypted TCP connection you can
|
---|
57 | use for transmitting encrypted data. It can operate in both client
|
---|
58 | and server mode, and it supports modern SSL protocols, including
|
---|
59 | SSLv3 and TLSv1. By default, QSslSocket uses SSLv3, but you can
|
---|
60 | change the SSL protocol by calling setProtocol() as long as you do
|
---|
61 | it before the handshake has started.
|
---|
62 |
|
---|
63 | SSL encryption operates on top of the existing TCP stream after
|
---|
64 | the socket enters the ConnectedState. There are two simple ways to
|
---|
65 | establish a secure connection using QSslSocket: With an immediate
|
---|
66 | SSL handshake, or with a delayed SSL handshake occurring after the
|
---|
67 | connection has been established in unencrypted mode.
|
---|
68 |
|
---|
69 | The most common way to use QSslSocket is to construct an object
|
---|
70 | and start a secure connection by calling connectToHostEncrypted().
|
---|
71 | This method starts an immediate SSL handshake once the connection
|
---|
72 | has been established.
|
---|
73 |
|
---|
74 | \snippet doc/src/snippets/code/src_network_ssl_qsslsocket.cpp 0
|
---|
75 |
|
---|
76 | As with a plain QTcpSocket, QSslSocket enters the HostLookupState,
|
---|
77 | ConnectingState, and finally the ConnectedState, if the connection
|
---|
78 | is successful. The handshake then starts automatically, and if it
|
---|
79 | succeeds, the encrypted() signal is emitted to indicate the socket
|
---|
80 | has entered the encrypted state and is ready for use.
|
---|
81 |
|
---|
82 | Note that data can be written to the socket immediately after the
|
---|
83 | return from connectToHostEncrypted() (i.e., before the encrypted()
|
---|
84 | signal is emitted). The data is queued in QSslSocket until after
|
---|
85 | the encrypted() signal is emitted.
|
---|
86 |
|
---|
87 | An example of using the delayed SSL handshake to secure an
|
---|
88 | existing connection is the case where an SSL server secures an
|
---|
89 | incoming connection. Suppose you create an SSL server class as a
|
---|
90 | subclass of QTcpServer. You would override
|
---|
91 | QTcpServer::incomingConnection() with something like the example
|
---|
92 | below, which first constructs an instance of QSslSocket and then
|
---|
93 | calls setSocketDescriptor() to set the new socket's descriptor to
|
---|
94 | the existing one passed in. It then initiates the SSL handshake
|
---|
95 | by calling startServerEncryption().
|
---|
96 |
|
---|
97 | \snippet doc/src/snippets/code/src_network_ssl_qsslsocket.cpp 1
|
---|
98 |
|
---|
99 | If an error occurs, QSslSocket emits the sslErrors() signal. In this
|
---|
100 | case, if no action is taken to ignore the error(s), the connection
|
---|
101 | is dropped. To continue, despite the occurrence of an error, you
|
---|
102 | can call ignoreSslErrors(), either from within this slot after the
|
---|
103 | error occurs, or any time after construction of the QSslSocket and
|
---|
104 | before the connection is attempted. This will allow QSslSocket to
|
---|
105 | ignore the errors it encounters when establishing the identity of
|
---|
106 | the peer. Ignoring errors during an SSL handshake should be used
|
---|
107 | with caution, since a fundamental characteristic of secure
|
---|
108 | connections is that they should be established with a successful
|
---|
109 | handshake.
|
---|
110 |
|
---|
111 | Once encrypted, you use QSslSocket as a regular QTcpSocket. When
|
---|
112 | readyRead() is emitted, you can call read(), canReadLine() and
|
---|
113 | readLine(), or getChar() to read decrypted data from QSslSocket's
|
---|
114 | internal buffer, and you can call write() or putChar() to write
|
---|
115 | data back to the peer. QSslSocket will automatically encrypt the
|
---|
116 | written data for you, and emit encryptedBytesWritten() once
|
---|
117 | the data has been written to the peer.
|
---|
118 |
|
---|
119 | As a convenience, QSslSocket supports QTcpSocket's blocking
|
---|
120 | functions waitForConnected(), waitForReadyRead(),
|
---|
121 | waitForBytesWritten(), and waitForDisconnected(). It also provides
|
---|
122 | waitForEncrypted(), which will block the calling thread until an
|
---|
123 | encrypted connection has been established.
|
---|
124 |
|
---|
125 | \snippet doc/src/snippets/code/src_network_ssl_qsslsocket.cpp 2
|
---|
126 |
|
---|
127 | QSslSocket provides an extensive, easy-to-use API for handling
|
---|
128 | cryptographic ciphers, private keys, and local, peer, and
|
---|
129 | Certification Authority (CA) certificates. It also provides an API
|
---|
130 | for handling errors that occur during the handshake phase.
|
---|
131 |
|
---|
132 | The following features can also be customized:
|
---|
133 |
|
---|
134 | \list
|
---|
135 | \o The socket's cryptographic cipher suite can be customized before
|
---|
136 | the handshake phase with setCiphers() and setDefaultCiphers().
|
---|
137 | \o The socket's local certificate and private key can be customized
|
---|
138 | before the handshake phase with setLocalCertificate() and
|
---|
139 | setPrivateKey().
|
---|
140 | \o The CA certificate database can be extended and customized with
|
---|
141 | addCaCertificate(), addCaCertificates(), setCaCertificates(),
|
---|
142 | addDefaultCaCertificate(), addDefaultCaCertificates(), and
|
---|
143 | setDefaultCaCertificates().
|
---|
144 | \endlist
|
---|
145 |
|
---|
146 | For more information about ciphers and certificates, refer to QSslCipher and
|
---|
147 | QSslCertificate.
|
---|
148 |
|
---|
149 | This product includes software developed by the OpenSSL Project
|
---|
150 | for use in the OpenSSL Toolkit (\l{http://www.openssl.org/}).
|
---|
151 |
|
---|
152 | \note Be aware of the difference between the bytesWritten() signal and
|
---|
153 | the encryptedBytesWritten() signal. For a QTcpSocket, bytesWritten()
|
---|
154 | will get emitted as soon as data has been written to the TCP socket.
|
---|
155 | For a QSslSocket, bytesWritten() will get emitted when the data
|
---|
156 | is being encrypted and encryptedBytesWritten()
|
---|
157 | will get emitted as soon as data has been written to the TCP socket.
|
---|
158 |
|
---|
159 | \section1 Symbian Platform Security Requirements
|
---|
160 |
|
---|
161 | On Symbian, processes which use this class must have the
|
---|
162 | \c NetworkServices platform security capability. If the client
|
---|
163 | process lacks this capability, operations will fail.
|
---|
164 |
|
---|
165 | Platform security capabilities are added via the
|
---|
166 | \l{qmake-variable-reference.html#target-capability}{TARGET.CAPABILITY}
|
---|
167 | qmake variable.
|
---|
168 |
|
---|
169 | \sa QSslCertificate, QSslCipher, QSslError
|
---|
170 | */
|
---|
171 |
|
---|
172 | /*!
|
---|
173 | \enum QSslSocket::SslMode
|
---|
174 |
|
---|
175 | Describes the connection modes available for QSslSocket.
|
---|
176 |
|
---|
177 | \value UnencryptedMode The socket is unencrypted. Its
|
---|
178 | behavior is identical to QTcpSocket.
|
---|
179 |
|
---|
180 | \value SslClientMode The socket is a client-side SSL socket.
|
---|
181 | It is either alreayd encrypted, or it is in the SSL handshake
|
---|
182 | phase (see QSslSocket::isEncrypted()).
|
---|
183 |
|
---|
184 | \value SslServerMode The socket is a server-side SSL socket.
|
---|
185 | It is either already encrypted, or it is in the SSL handshake
|
---|
186 | phase (see QSslSocket::isEncrypted()).
|
---|
187 | */
|
---|
188 |
|
---|
189 | /*!
|
---|
190 | \enum QSslSocket::PeerVerifyMode
|
---|
191 | \since 4.4
|
---|
192 |
|
---|
193 | Describes the peer verification modes for QSslSocket. The default mode is
|
---|
194 | AutoVerifyPeer, which selects an appropriate mode depending on the
|
---|
195 | socket's QSocket::SslMode.
|
---|
196 |
|
---|
197 | \value VerifyNone QSslSocket will not request a certificate from the
|
---|
198 | peer. You can set this mode if you are not interested in the identity of
|
---|
199 | the other side of the connection. The connection will still be encrypted,
|
---|
200 | and your socket will still send its local certificate to the peer if it's
|
---|
201 | requested.
|
---|
202 |
|
---|
203 | \value QueryPeer QSslSocket will request a certificate from the peer, but
|
---|
204 | does not require this certificate to be valid. This is useful when you
|
---|
205 | want to display peer certificate details to the user without affecting the
|
---|
206 | actual SSL handshake. This mode is the default for servers.
|
---|
207 |
|
---|
208 | \value VerifyPeer QSslSocket will request a certificate from the peer
|
---|
209 | during the SSL handshake phase, and requires that this certificate is
|
---|
210 | valid. On failure, QSslSocket will emit the QSslSocket::sslErrors()
|
---|
211 | signal. This mode is the default for clients.
|
---|
212 |
|
---|
213 | \value AutoVerifyPeer QSslSocket will automaticaly use QueryPeer for
|
---|
214 | server sockets and VerifyPeer for client sockets.
|
---|
215 |
|
---|
216 | \sa QSslSocket::peerVerifyMode()
|
---|
217 | */
|
---|
218 |
|
---|
219 | /*!
|
---|
220 | \fn QSslSocket::encrypted()
|
---|
221 |
|
---|
222 | This signal is emitted when QSslSocket enters encrypted mode. After this
|
---|
223 | signal has been emitted, QSslSocket::isEncrypted() will return true, and
|
---|
224 | all further transmissions on the socket will be encrypted.
|
---|
225 |
|
---|
226 | \sa QSslSocket::connectToHostEncrypted(), QSslSocket::isEncrypted()
|
---|
227 | */
|
---|
228 |
|
---|
229 | /*!
|
---|
230 | \fn QSslSocket::modeChanged(QSslSocket::SslMode mode)
|
---|
231 |
|
---|
232 | This signal is emitted when QSslSocket changes from \l
|
---|
233 | QSslSocket::UnencryptedMode to either \l QSslSocket::SslClientMode or \l
|
---|
234 | QSslSocket::SslServerMode. \a mode is the new mode.
|
---|
235 |
|
---|
236 | \sa QSslSocket::mode()
|
---|
237 | */
|
---|
238 |
|
---|
239 | /*!
|
---|
240 | \fn QSslSocket::encryptedBytesWritten(qint64 written)
|
---|
241 | \since 4.4
|
---|
242 |
|
---|
243 | This signal is emitted when QSslSocket writes its encrypted data to the
|
---|
244 | network. The \a written parameter contains the number of bytes that were
|
---|
245 | successfully written.
|
---|
246 |
|
---|
247 | \sa QIODevice::bytesWritten()
|
---|
248 | */
|
---|
249 |
|
---|
250 | /*!
|
---|
251 | \fn void QSslSocket::peerVerifyError(const QSslError &error)
|
---|
252 | \since 4.4
|
---|
253 |
|
---|
254 | QSslSocket can emit this signal several times during the SSL handshake,
|
---|
255 | before encryption has been established, to indicate that an error has
|
---|
256 | occurred while establishing the identity of the peer. The \a error is
|
---|
257 | usually an indication that QSslSocket is unable to securely identify the
|
---|
258 | peer.
|
---|
259 |
|
---|
260 | This signal provides you with an early indication when something's wrong.
|
---|
261 | By connecting to this signal, you can manually choose to tear down the
|
---|
262 | connection from inside the connected slot before the handshake has
|
---|
263 | completed. If no action is taken, QSslSocket will proceed to emitting
|
---|
264 | QSslSocket::sslErrors().
|
---|
265 |
|
---|
266 | \sa sslErrors()
|
---|
267 | */
|
---|
268 |
|
---|
269 | /*!
|
---|
270 | \fn void QSslSocket::sslErrors(const QList<QSslError> &errors);
|
---|
271 |
|
---|
272 | QSslSocket emits this signal after the SSL handshake to indicate that one
|
---|
273 | or more errors have occurred while establishing the identity of the
|
---|
274 | peer. The errors are usually an indication that QSslSocket is unable to
|
---|
275 | securely identify the peer. Unless any action is taken, the connection
|
---|
276 | will be dropped after this signal has been emitted.
|
---|
277 |
|
---|
278 | If you want to continue connecting despite the errors that have occurred,
|
---|
279 | you must call QSslSocket::ignoreSslErrors() from inside a slot connected to
|
---|
280 | this signal. If you need to access the error list at a later point, you
|
---|
281 | can call sslErrors() (without arguments).
|
---|
282 |
|
---|
283 | \a errors contains one or more errors that prevent QSslSocket from
|
---|
284 | verifying the identity of the peer.
|
---|
285 |
|
---|
286 | Note: You cannot use Qt::QueuedConnection when connecting to this signal,
|
---|
287 | or calling QSslSocket::ignoreSslErrors() will have no effect.
|
---|
288 |
|
---|
289 | \sa peerVerifyError()
|
---|
290 | */
|
---|
291 |
|
---|
292 | #include "qsslcipher.h"
|
---|
293 | #include "qsslsocket.h"
|
---|
294 | #include "qsslsocket_openssl_p.h"
|
---|
295 | #include "qsslconfiguration_p.h"
|
---|
296 |
|
---|
297 | #include <QtCore/qdebug.h>
|
---|
298 | #include <QtCore/qdir.h>
|
---|
299 | #include <QtCore/qdatetime.h>
|
---|
300 | #include <QtCore/qmutex.h>
|
---|
301 | #include <QtNetwork/qhostaddress.h>
|
---|
302 | #include <QtNetwork/qhostinfo.h>
|
---|
303 |
|
---|
304 | QT_BEGIN_NAMESPACE
|
---|
305 |
|
---|
306 | /*
|
---|
307 | Returns the difference between msecs and elapsed. If msecs is -1,
|
---|
308 | however, -1 is returned.
|
---|
309 | */
|
---|
310 | static int qt_timeout_value(int msecs, int elapsed)
|
---|
311 | {
|
---|
312 | if (msecs == -1)
|
---|
313 | return -1;
|
---|
314 |
|
---|
315 | int timeout = msecs - elapsed;
|
---|
316 | return timeout < 0 ? 0 : timeout;
|
---|
317 | }
|
---|
318 |
|
---|
319 | class QSslSocketGlobalData
|
---|
320 | {
|
---|
321 | public:
|
---|
322 | QSslSocketGlobalData() : config(new QSslConfigurationPrivate) {}
|
---|
323 |
|
---|
324 | QMutex mutex;
|
---|
325 | QList<QSslCipher> supportedCiphers;
|
---|
326 | QExplicitlySharedDataPointer<QSslConfigurationPrivate> config;
|
---|
327 | };
|
---|
328 | Q_GLOBAL_STATIC(QSslSocketGlobalData, globalData)
|
---|
329 |
|
---|
330 | /*!
|
---|
331 | Constructs a QSslSocket object. \a parent is passed to QObject's
|
---|
332 | constructor. The new socket's \l {QSslCipher} {cipher} suite is
|
---|
333 | set to the one returned by the static method defaultCiphers().
|
---|
334 | */
|
---|
335 | QSslSocket::QSslSocket(QObject *parent)
|
---|
336 | : QTcpSocket(*new QSslSocketBackendPrivate, parent)
|
---|
337 | {
|
---|
338 | Q_D(QSslSocket);
|
---|
339 | #ifdef QSSLSOCKET_DEBUG
|
---|
340 | qDebug() << "QSslSocket::QSslSocket(" << parent << "), this =" << (void *)this;
|
---|
341 | #endif
|
---|
342 | d->q_ptr = this;
|
---|
343 | d->init();
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*!
|
---|
347 | Destroys the QSslSocket.
|
---|
348 | */
|
---|
349 | QSslSocket::~QSslSocket()
|
---|
350 | {
|
---|
351 | Q_D(QSslSocket);
|
---|
352 | #ifdef QSSLSOCKET_DEBUG
|
---|
353 | qDebug() << "QSslSocket::~QSslSocket(), this =" << (void *)this;
|
---|
354 | #endif
|
---|
355 | delete d->plainSocket;
|
---|
356 | d->plainSocket = 0;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /*!
|
---|
360 | Starts an encrypted connection to the device \a hostName on \a
|
---|
361 | port, using \a mode as the \l OpenMode. This is equivalent to
|
---|
362 | calling connectToHost() to establish the connection, followed by a
|
---|
363 | call to startClientEncryption().
|
---|
364 |
|
---|
365 | QSslSocket first enters the HostLookupState. Then, after entering
|
---|
366 | either the event loop or one of the waitFor...() functions, it
|
---|
367 | enters the ConnectingState, emits connected(), and then initiates
|
---|
368 | the SSL client handshake. At each state change, QSslSocket emits
|
---|
369 | signal stateChanged().
|
---|
370 |
|
---|
371 | After initiating the SSL client handshake, if the identity of the
|
---|
372 | peer can't be established, signal sslErrors() is emitted. If you
|
---|
373 | want to ignore the errors and continue connecting, you must call
|
---|
374 | ignoreSslErrors(), either from inside a slot function connected to
|
---|
375 | the sslErrors() signal, or prior to entering encrypted mode. If
|
---|
376 | ignoreSslErrors() is not called, the connection is dropped, signal
|
---|
377 | disconnected() is emitted, and QSslSocket returns to the
|
---|
378 | UnconnectedState.
|
---|
379 |
|
---|
380 | If the SSL handshake is successful, QSslSocket emits encrypted().
|
---|
381 |
|
---|
382 | \snippet doc/src/snippets/code/src_network_ssl_qsslsocket.cpp 3
|
---|
383 |
|
---|
384 | \bold{Note:} The example above shows that text can be written to
|
---|
385 | the socket immediately after requesting the encrypted connection,
|
---|
386 | before the encrypted() signal has been emitted. In such cases, the
|
---|
387 | text is queued in the object and written to the socket \e after
|
---|
388 | the connection is established and the encrypted() signal has been
|
---|
389 | emitted.
|
---|
390 |
|
---|
391 | The default for \a mode is \l ReadWrite.
|
---|
392 |
|
---|
393 | If you want to create a QSslSocket on the server side of a connection, you
|
---|
394 | should instead call startServerEncryption() upon receiving the incoming
|
---|
395 | connection through QTcpServer.
|
---|
396 |
|
---|
397 | \sa connectToHost(), startClientEncryption(), waitForConnected(), waitForEncrypted()
|
---|
398 | */
|
---|
399 | void QSslSocket::connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode mode)
|
---|
400 | {
|
---|
401 | Q_D(QSslSocket);
|
---|
402 | if (d->state == ConnectedState || d->state == ConnectingState) {
|
---|
403 | qWarning("QSslSocket::connectToHostEncrypted() called when already connecting/connected");
|
---|
404 | return;
|
---|
405 | }
|
---|
406 |
|
---|
407 | d->init();
|
---|
408 | d->autoStartHandshake = true;
|
---|
409 | d->initialized = true;
|
---|
410 |
|
---|
411 | // Note: When connecting to localhost, some platforms (e.g., HP-UX and some BSDs)
|
---|
412 | // establish the connection immediately (i.e., first attempt).
|
---|
413 | connectToHost(hostName, port, mode);
|
---|
414 | }
|
---|
415 |
|
---|
416 | /*!
|
---|
417 | \since 4.6
|
---|
418 | \overload
|
---|
419 |
|
---|
420 | In addition to the original behaviour of connectToHostEncrypted,
|
---|
421 | this overloaded method enables the usage of a different hostname
|
---|
422 | (\a sslPeerName) for the certificate validation instead of
|
---|
423 | the one used for the TCP connection (\a hostName).
|
---|
424 |
|
---|
425 | \sa connectToHostEncrypted()
|
---|
426 | */
|
---|
427 | void QSslSocket::connectToHostEncrypted(const QString &hostName, quint16 port,
|
---|
428 | const QString &sslPeerName, OpenMode mode)
|
---|
429 | {
|
---|
430 | Q_D(QSslSocket);
|
---|
431 | if (d->state == ConnectedState || d->state == ConnectingState) {
|
---|
432 | qWarning("QSslSocket::connectToHostEncrypted() called when already connecting/connected");
|
---|
433 | return;
|
---|
434 | }
|
---|
435 |
|
---|
436 | d->init();
|
---|
437 | d->autoStartHandshake = true;
|
---|
438 | d->initialized = true;
|
---|
439 | d->verificationPeerName = sslPeerName;
|
---|
440 |
|
---|
441 | // Note: When connecting to localhost, some platforms (e.g., HP-UX and some BSDs)
|
---|
442 | // establish the connection immediately (i.e., first attempt).
|
---|
443 | connectToHost(hostName, port, mode);
|
---|
444 | }
|
---|
445 |
|
---|
446 | /*!
|
---|
447 | Initializes QSslSocket with the native socket descriptor \a
|
---|
448 | socketDescriptor. Returns true if \a socketDescriptor is accepted
|
---|
449 | as a valid socket descriptor; otherwise returns false.
|
---|
450 | The socket is opened in the mode specified by \a openMode, and
|
---|
451 | enters the socket state specified by \a state.
|
---|
452 |
|
---|
453 | \bold{Note:} It is not possible to initialize two sockets with the same
|
---|
454 | native socket descriptor.
|
---|
455 |
|
---|
456 | \sa socketDescriptor()
|
---|
457 | */
|
---|
458 | bool QSslSocket::setSocketDescriptor(int socketDescriptor, SocketState state, OpenMode openMode)
|
---|
459 | {
|
---|
460 | Q_D(QSslSocket);
|
---|
461 | #ifdef QSSLSOCKET_DEBUG
|
---|
462 | qDebug() << "QSslSocket::setSocketDescriptor(" << socketDescriptor << ','
|
---|
463 | << state << ',' << openMode << ')';
|
---|
464 | #endif
|
---|
465 | if (!d->plainSocket)
|
---|
466 | d->createPlainSocket(openMode);
|
---|
467 | bool retVal = d->plainSocket->setSocketDescriptor(socketDescriptor, state, openMode);
|
---|
468 | d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();
|
---|
469 | setSocketError(d->plainSocket->error());
|
---|
470 | setSocketState(state);
|
---|
471 | setOpenMode(openMode);
|
---|
472 | setLocalPort(d->plainSocket->localPort());
|
---|
473 | setLocalAddress(d->plainSocket->localAddress());
|
---|
474 | setPeerPort(d->plainSocket->peerPort());
|
---|
475 | setPeerAddress(d->plainSocket->peerAddress());
|
---|
476 | setPeerName(d->plainSocket->peerName());
|
---|
477 | return retVal;
|
---|
478 | }
|
---|
479 |
|
---|
480 | /*!
|
---|
481 | \since 4.6
|
---|
482 | Sets the given \a option to the value described by \a value.
|
---|
483 |
|
---|
484 | \sa socketOption()
|
---|
485 | */
|
---|
486 | void QSslSocket::setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value)
|
---|
487 | {
|
---|
488 | Q_D(QSslSocket);
|
---|
489 | if (d->plainSocket)
|
---|
490 | d->plainSocket->setSocketOption(option, value);
|
---|
491 | }
|
---|
492 |
|
---|
493 | /*!
|
---|
494 | \since 4.6
|
---|
495 | Returns the value of the \a option option.
|
---|
496 |
|
---|
497 | \sa setSocketOption()
|
---|
498 | */
|
---|
499 | QVariant QSslSocket::socketOption(QAbstractSocket::SocketOption option)
|
---|
500 | {
|
---|
501 | Q_D(QSslSocket);
|
---|
502 | if (d->plainSocket)
|
---|
503 | return d->plainSocket->socketOption(option);
|
---|
504 | else
|
---|
505 | return QVariant();
|
---|
506 | }
|
---|
507 |
|
---|
508 | /*!
|
---|
509 | Returns the current mode for the socket; either UnencryptedMode, where
|
---|
510 | QSslSocket behaves identially to QTcpSocket, or one of SslClientMode or
|
---|
511 | SslServerMode, where the client is either negotiating or in encrypted
|
---|
512 | mode.
|
---|
513 |
|
---|
514 | When the mode changes, QSslSocket emits modeChanged()
|
---|
515 |
|
---|
516 | \sa SslMode
|
---|
517 | */
|
---|
518 | QSslSocket::SslMode QSslSocket::mode() const
|
---|
519 | {
|
---|
520 | Q_D(const QSslSocket);
|
---|
521 | return d->mode;
|
---|
522 | }
|
---|
523 |
|
---|
524 | /*!
|
---|
525 | Returns true if the socket is encrypted; otherwise, false is returned.
|
---|
526 |
|
---|
527 | An encrypted socket encrypts all data that is written by calling write()
|
---|
528 | or putChar() before the data is written to the network, and decrypts all
|
---|
529 | incoming data as the data is received from the network, before you call
|
---|
530 | read(), readLine() or getChar().
|
---|
531 |
|
---|
532 | QSslSocket emits encrypted() when it enters encrypted mode.
|
---|
533 |
|
---|
534 | You can call sessionCipher() to find which cryptographic cipher is used to
|
---|
535 | encrypt and decrypt your data.
|
---|
536 |
|
---|
537 | \sa mode()
|
---|
538 | */
|
---|
539 | bool QSslSocket::isEncrypted() const
|
---|
540 | {
|
---|
541 | Q_D(const QSslSocket);
|
---|
542 | return d->connectionEncrypted;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /*!
|
---|
546 | Returns the socket's SSL protocol. By default, \l QSsl::SslV3 is used.
|
---|
547 |
|
---|
548 | \sa setProtocol()
|
---|
549 | */
|
---|
550 | QSsl::SslProtocol QSslSocket::protocol() const
|
---|
551 | {
|
---|
552 | Q_D(const QSslSocket);
|
---|
553 | return d->configuration.protocol;
|
---|
554 | }
|
---|
555 |
|
---|
556 | /*!
|
---|
557 | Sets the socket's SSL protocol to \a protocol. This will affect the next
|
---|
558 | initiated handshake; calling this function on an already-encrypted socket
|
---|
559 | will not affect the socket's protocol.
|
---|
560 | */
|
---|
561 | void QSslSocket::setProtocol(QSsl::SslProtocol protocol)
|
---|
562 | {
|
---|
563 | Q_D(QSslSocket);
|
---|
564 | d->configuration.protocol = protocol;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /*!
|
---|
568 | \since 4.4
|
---|
569 |
|
---|
570 | Returns the socket's verify mode. This mode mode decides whether
|
---|
571 | QSslSocket should request a certificate from the peer (i.e., the client
|
---|
572 | requests a certificate from the server, or a server requesting a
|
---|
573 | certificate from the client), and whether it should require that this
|
---|
574 | certificate is valid.
|
---|
575 |
|
---|
576 | The default mode is AutoVerifyPeer, which tells QSslSocket to use
|
---|
577 | VerifyPeer for clients, QueryPeer for clients.
|
---|
578 |
|
---|
579 | \sa setPeerVerifyMode(), peerVerifyDepth(), mode()
|
---|
580 | */
|
---|
581 | QSslSocket::PeerVerifyMode QSslSocket::peerVerifyMode() const
|
---|
582 | {
|
---|
583 | Q_D(const QSslSocket);
|
---|
584 | return d->configuration.peerVerifyMode;
|
---|
585 | }
|
---|
586 |
|
---|
587 | /*!
|
---|
588 | \since 4.4
|
---|
589 |
|
---|
590 | Sets the socket's verify mode to \a mode. This mode decides whether
|
---|
591 | QSslSocket should request a certificate from the peer (i.e., the client
|
---|
592 | requests a certificate from the server, or a server requesting a
|
---|
593 | certificate from the client), and whether it should require that this
|
---|
594 | certificate is valid.
|
---|
595 |
|
---|
596 | The default mode is AutoVerifyPeer, which tells QSslSocket to use
|
---|
597 | VerifyPeer for clients, QueryPeer for clients.
|
---|
598 |
|
---|
599 | Setting this mode after encryption has started has no effect on the
|
---|
600 | current connection.
|
---|
601 |
|
---|
602 | \sa peerVerifyMode(), setPeerVerifyDepth(), mode()
|
---|
603 | */
|
---|
604 | void QSslSocket::setPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
|
---|
605 | {
|
---|
606 | Q_D(QSslSocket);
|
---|
607 | d->configuration.peerVerifyMode = mode;
|
---|
608 | }
|
---|
609 |
|
---|
610 | /*!
|
---|
611 | \since 4.4
|
---|
612 |
|
---|
613 | Returns the maximum number of certificates in the peer's certificate chain
|
---|
614 | to be checked during the SSL handshake phase, or 0 (the default) if no
|
---|
615 | maximum depth has been set, indicating that the whole certificate chain
|
---|
616 | should be checked.
|
---|
617 |
|
---|
618 | The certificates are checked in issuing order, starting with the peer's
|
---|
619 | own certificate, then its issuer's certificate, and so on.
|
---|
620 |
|
---|
621 | \sa setPeerVerifyDepth(), peerVerifyMode()
|
---|
622 | */
|
---|
623 | int QSslSocket::peerVerifyDepth() const
|
---|
624 | {
|
---|
625 | Q_D(const QSslSocket);
|
---|
626 | return d->configuration.peerVerifyDepth;
|
---|
627 | }
|
---|
628 |
|
---|
629 | /*!
|
---|
630 | \since 4.4
|
---|
631 |
|
---|
632 | Sets the maximum number of certificates in the peer's certificate chain to
|
---|
633 | be checked during the SSL handshake phase, to \a depth. Setting a depth of
|
---|
634 | 0 means that no maximum depth is set, indicating that the whole
|
---|
635 | certificate chain should be checked.
|
---|
636 |
|
---|
637 | The certificates are checked in issuing order, starting with the peer's
|
---|
638 | own certificate, then its issuer's certificate, and so on.
|
---|
639 |
|
---|
640 | \sa peerVerifyDepth(), setPeerVerifyMode()
|
---|
641 | */
|
---|
642 | void QSslSocket::setPeerVerifyDepth(int depth)
|
---|
643 | {
|
---|
644 | Q_D(QSslSocket);
|
---|
645 | if (depth < 0) {
|
---|
646 | qWarning("QSslSocket::setPeerVerifyDepth: cannot set negative depth of %d", depth);
|
---|
647 | return;
|
---|
648 | }
|
---|
649 | d->configuration.peerVerifyDepth = depth;
|
---|
650 | }
|
---|
651 |
|
---|
652 | /*!
|
---|
653 | \reimp
|
---|
654 |
|
---|
655 | Returns the number of decrypted bytes that are immediately available for
|
---|
656 | reading.
|
---|
657 | */
|
---|
658 | qint64 QSslSocket::bytesAvailable() const
|
---|
659 | {
|
---|
660 | Q_D(const QSslSocket);
|
---|
661 | if (d->mode == UnencryptedMode)
|
---|
662 | return QIODevice::bytesAvailable() + (d->plainSocket ? d->plainSocket->bytesAvailable() : 0);
|
---|
663 | return QIODevice::bytesAvailable() + d->readBuffer.size();
|
---|
664 | }
|
---|
665 |
|
---|
666 | /*!
|
---|
667 | \reimp
|
---|
668 |
|
---|
669 | Returns the number of unencrypted bytes that are waiting to be encrypted
|
---|
670 | and written to the network.
|
---|
671 | */
|
---|
672 | qint64 QSslSocket::bytesToWrite() const
|
---|
673 | {
|
---|
674 | Q_D(const QSslSocket);
|
---|
675 | if (d->mode == UnencryptedMode)
|
---|
676 | return d->plainSocket ? d->plainSocket->bytesToWrite() : 0;
|
---|
677 | return d->writeBuffer.size();
|
---|
678 | }
|
---|
679 |
|
---|
680 | /*!
|
---|
681 | \since 4.4
|
---|
682 |
|
---|
683 | Returns the number of encrypted bytes that are awaiting decryption.
|
---|
684 | Normally, this function will return 0 because QSslSocket decrypts its
|
---|
685 | incoming data as soon as it can.
|
---|
686 | */
|
---|
687 | qint64 QSslSocket::encryptedBytesAvailable() const
|
---|
688 | {
|
---|
689 | Q_D(const QSslSocket);
|
---|
690 | if (d->mode == UnencryptedMode)
|
---|
691 | return 0;
|
---|
692 | return d->plainSocket->bytesAvailable();
|
---|
693 | }
|
---|
694 |
|
---|
695 | /*!
|
---|
696 | \since 4.4
|
---|
697 |
|
---|
698 | Returns the number of encrypted bytes that are waiting to be written to
|
---|
699 | the network.
|
---|
700 | */
|
---|
701 | qint64 QSslSocket::encryptedBytesToWrite() const
|
---|
702 | {
|
---|
703 | Q_D(const QSslSocket);
|
---|
704 | if (d->mode == UnencryptedMode)
|
---|
705 | return 0;
|
---|
706 | return d->plainSocket->bytesToWrite();
|
---|
707 | }
|
---|
708 |
|
---|
709 | /*!
|
---|
710 | \reimp
|
---|
711 |
|
---|
712 | Returns true if you can read one while line (terminated by a single ASCII
|
---|
713 | '\n' character) of decrypted characters; otherwise, false is returned.
|
---|
714 | */
|
---|
715 | bool QSslSocket::canReadLine() const
|
---|
716 | {
|
---|
717 | Q_D(const QSslSocket);
|
---|
718 | if (d->mode == UnencryptedMode)
|
---|
719 | return QIODevice::canReadLine() || (d->plainSocket && d->plainSocket->canReadLine());
|
---|
720 | return QIODevice::canReadLine() || (!d->readBuffer.isEmpty() && d->readBuffer.canReadLine());
|
---|
721 | }
|
---|
722 |
|
---|
723 | /*!
|
---|
724 | \reimp
|
---|
725 | */
|
---|
726 | void QSslSocket::close()
|
---|
727 | {
|
---|
728 | #ifdef QSSLSOCKET_DEBUG
|
---|
729 | qDebug() << "QSslSocket::close()";
|
---|
730 | #endif
|
---|
731 | Q_D(QSslSocket);
|
---|
732 | if (d->plainSocket)
|
---|
733 | d->plainSocket->close();
|
---|
734 | QTcpSocket::close();
|
---|
735 |
|
---|
736 | // must be cleared, reading/writing not possible on closed socket:
|
---|
737 | d->readBuffer.clear();
|
---|
738 | d->writeBuffer.clear();
|
---|
739 | // for QTcpSocket this is already done because it uses the readBuffer/writeBuffer
|
---|
740 | // if the QIODevice it is based on
|
---|
741 | // ### FIXME QSslSocket should probably do similar instead of having
|
---|
742 | // its own readBuffer/writeBuffer
|
---|
743 | }
|
---|
744 |
|
---|
745 | /*!
|
---|
746 | \reimp
|
---|
747 | */
|
---|
748 | bool QSslSocket::atEnd() const
|
---|
749 | {
|
---|
750 | Q_D(const QSslSocket);
|
---|
751 | if (d->mode == UnencryptedMode)
|
---|
752 | return QIODevice::atEnd() && (!d->plainSocket || d->plainSocket->atEnd());
|
---|
753 | return QIODevice::atEnd() && d->readBuffer.isEmpty();
|
---|
754 | }
|
---|
755 |
|
---|
756 | /*!
|
---|
757 | This function writes as much as possible from the internal write buffer to
|
---|
758 | the underlying network socket, without blocking. If any data was written,
|
---|
759 | this function returns true; otherwise false is returned.
|
---|
760 |
|
---|
761 | Call this function if you need QSslSocket to start sending buffered data
|
---|
762 | immediately. The number of bytes successfully written depends on the
|
---|
763 | operating system. In most cases, you do not need to call this function,
|
---|
764 | because QAbstractSocket will start sending data automatically once control
|
---|
765 | goes back to the event loop. In the absence of an event loop, call
|
---|
766 | waitForBytesWritten() instead.
|
---|
767 |
|
---|
768 | \sa write(), waitForBytesWritten()
|
---|
769 | */
|
---|
770 | // Note! docs copied from QAbstractSocket::flush()
|
---|
771 | bool QSslSocket::flush()
|
---|
772 | {
|
---|
773 | Q_D(QSslSocket);
|
---|
774 | #ifdef QSSLSOCKET_DEBUG
|
---|
775 | qDebug() << "QSslSocket::flush()";
|
---|
776 | #endif
|
---|
777 | if (d->mode != UnencryptedMode)
|
---|
778 | // encrypt any unencrypted bytes in our buffer
|
---|
779 | d->transmit();
|
---|
780 |
|
---|
781 | return d->plainSocket ? d->plainSocket->flush() : false;
|
---|
782 | }
|
---|
783 |
|
---|
784 | /*!
|
---|
785 | \since 4.4
|
---|
786 |
|
---|
787 | Sets the size of QSslSocket's internal read buffer to be \a size bytes.
|
---|
788 | */
|
---|
789 | void QSslSocket::setReadBufferSize(qint64 size)
|
---|
790 | {
|
---|
791 | Q_D(QSslSocket);
|
---|
792 | d->readBufferMaxSize = size;
|
---|
793 |
|
---|
794 | // set the plain socket's buffer size to 1k if we have a limit
|
---|
795 | // see also the same logic in QSslSocketPrivate::createPlainSocket
|
---|
796 | if (d->plainSocket) {
|
---|
797 | if (d->mode == UnencryptedMode)
|
---|
798 | d->plainSocket->setReadBufferSize(size);
|
---|
799 | else
|
---|
800 | d->plainSocket->setReadBufferSize(size ? 1024 : 0);
|
---|
801 | }
|
---|
802 | }
|
---|
803 |
|
---|
804 | /*!
|
---|
805 | Aborts the current connection and resets the socket. Unlike
|
---|
806 | disconnectFromHost(), this function immediately closes the socket,
|
---|
807 | clearing any pending data in the write buffer.
|
---|
808 |
|
---|
809 | \sa disconnectFromHost(), close()
|
---|
810 | */
|
---|
811 | void QSslSocket::abort()
|
---|
812 | {
|
---|
813 | Q_D(QSslSocket);
|
---|
814 | #ifdef QSSLSOCKET_DEBUG
|
---|
815 | qDebug() << "QSslSocket::abort()";
|
---|
816 | #endif
|
---|
817 | if (d->plainSocket)
|
---|
818 | d->plainSocket->abort();
|
---|
819 | close();
|
---|
820 | }
|
---|
821 |
|
---|
822 | /*!
|
---|
823 | \since 4.4
|
---|
824 |
|
---|
825 | Returns the socket's SSL configuration state. The default SSL
|
---|
826 | configuration of a socket is to use the default ciphers,
|
---|
827 | default CA certificates, no local private key or certificate.
|
---|
828 |
|
---|
829 | The SSL configuration also contains fields that can change with
|
---|
830 | time without notice.
|
---|
831 |
|
---|
832 | \sa localCertificate(), peerCertificate(), peerCertificateChain(),
|
---|
833 | sessionCipher(), privateKey(), ciphers(), caCertificates()
|
---|
834 | */
|
---|
835 | QSslConfiguration QSslSocket::sslConfiguration() const
|
---|
836 | {
|
---|
837 | Q_D(const QSslSocket);
|
---|
838 |
|
---|
839 | // create a deep copy of our configuration
|
---|
840 | QSslConfigurationPrivate *copy = new QSslConfigurationPrivate(d->configuration);
|
---|
841 | copy->ref = 0; // the QSslConfiguration constructor refs up
|
---|
842 | copy->sessionCipher = d->sessionCipher();
|
---|
843 |
|
---|
844 | return QSslConfiguration(copy);
|
---|
845 | }
|
---|
846 |
|
---|
847 | /*!
|
---|
848 | \since 4.4
|
---|
849 |
|
---|
850 | Sets the socket's SSL configuration to be the contents of \a configuration.
|
---|
851 | This function sets the local certificate, the ciphers, the private key and the CA
|
---|
852 | certificates to those stored in \a configuration.
|
---|
853 |
|
---|
854 | It is not possible to set the SSL-state related fields.
|
---|
855 |
|
---|
856 | \sa setLocalCertificate(), setPrivateKey(), setCaCertificates(), setCiphers()
|
---|
857 | */
|
---|
858 | void QSslSocket::setSslConfiguration(const QSslConfiguration &configuration)
|
---|
859 | {
|
---|
860 | Q_D(QSslSocket);
|
---|
861 | d->configuration.localCertificate = configuration.localCertificate();
|
---|
862 | d->configuration.privateKey = configuration.privateKey();
|
---|
863 | d->configuration.ciphers = configuration.ciphers();
|
---|
864 | d->configuration.caCertificates = configuration.caCertificates();
|
---|
865 | d->configuration.peerVerifyDepth = configuration.peerVerifyDepth();
|
---|
866 | d->configuration.peerVerifyMode = configuration.peerVerifyMode();
|
---|
867 | d->configuration.protocol = configuration.protocol();
|
---|
868 | }
|
---|
869 |
|
---|
870 | /*!
|
---|
871 | Sets the socket's local certificate to \a certificate. The local
|
---|
872 | certificate is necessary if you need to confirm your identity to the
|
---|
873 | peer. It is used together with the private key; if you set the local
|
---|
874 | certificate, you must also set the private key.
|
---|
875 |
|
---|
876 | The local certificate and private key are always necessary for server
|
---|
877 | sockets, but are also rarely used by client sockets if the server requires
|
---|
878 | the client to authenticate.
|
---|
879 |
|
---|
880 | \sa localCertificate(), setPrivateKey()
|
---|
881 | */
|
---|
882 | void QSslSocket::setLocalCertificate(const QSslCertificate &certificate)
|
---|
883 | {
|
---|
884 | Q_D(QSslSocket);
|
---|
885 | d->configuration.localCertificate = certificate;
|
---|
886 | }
|
---|
887 |
|
---|
888 | /*!
|
---|
889 | \overload
|
---|
890 |
|
---|
891 | Sets the socket's local \l {QSslCertificate} {certificate} to the
|
---|
892 | first one found in file \a path, which is parsed according to the
|
---|
893 | specified \a format.
|
---|
894 | */
|
---|
895 | void QSslSocket::setLocalCertificate(const QString &path,
|
---|
896 | QSsl::EncodingFormat format)
|
---|
897 | {
|
---|
898 | Q_D(QSslSocket);
|
---|
899 | QFile file(path);
|
---|
900 | if (file.open(QIODevice::ReadOnly | QIODevice::Text))
|
---|
901 | d->configuration.localCertificate = QSslCertificate(file.readAll(), format);
|
---|
902 | }
|
---|
903 |
|
---|
904 | /*!
|
---|
905 | Returns the socket's local \l {QSslCertificate} {certificate}, or
|
---|
906 | an empty certificate if no local certificate has been assigned.
|
---|
907 |
|
---|
908 | \sa setLocalCertificate(), privateKey()
|
---|
909 | */
|
---|
910 | QSslCertificate QSslSocket::localCertificate() const
|
---|
911 | {
|
---|
912 | Q_D(const QSslSocket);
|
---|
913 | return d->configuration.localCertificate;
|
---|
914 | }
|
---|
915 |
|
---|
916 | /*!
|
---|
917 | Returns the peer's digital certificate (i.e., the immediate
|
---|
918 | certificate of the host you are connected to), or a null
|
---|
919 | certificate, if the peer has not assigned a certificate.
|
---|
920 |
|
---|
921 | The peer certificate is checked automatically during the
|
---|
922 | handshake phase, so this function is normally used to fetch
|
---|
923 | the certificate for display or for connection diagnostic
|
---|
924 | purposes. It contains information about the peer, including
|
---|
925 | its host name, the certificate issuer, and the peer's public
|
---|
926 | key.
|
---|
927 |
|
---|
928 | Because the peer certificate is set during the handshake phase, it
|
---|
929 | is safe to access the peer certificate from a slot connected to
|
---|
930 | the sslErrors() signal or the encrypted() signal.
|
---|
931 |
|
---|
932 | If a null certificate is returned, it can mean the SSL handshake
|
---|
933 | failed, or it can mean the host you are connected to doesn't have
|
---|
934 | a certificate, or it can mean there is no connection.
|
---|
935 |
|
---|
936 | If you want to check the peer's complete chain of certificates,
|
---|
937 | use peerCertificateChain() to get them all at once.
|
---|
938 |
|
---|
939 | \sa peerCertificateChain()
|
---|
940 | */
|
---|
941 | QSslCertificate QSslSocket::peerCertificate() const
|
---|
942 | {
|
---|
943 | Q_D(const QSslSocket);
|
---|
944 | return d->configuration.peerCertificate;
|
---|
945 | }
|
---|
946 |
|
---|
947 | /*!
|
---|
948 | Returns the peer's chain of digital certificates, or an empty list
|
---|
949 | of certificates.
|
---|
950 |
|
---|
951 | Peer certificates are checked automatically during the handshake
|
---|
952 | phase. This function is normally used to fetch certificates for
|
---|
953 | display, or for performing connection diagnostics. Certificates
|
---|
954 | contain information about the peer and the certificate issuers,
|
---|
955 | including host name, issuer names, and issuer public keys.
|
---|
956 |
|
---|
957 | The peer certificates are set in QSslSocket during the handshake
|
---|
958 | phase, so it is safe to call this function from a slot connected
|
---|
959 | to the sslErrors() signal or the encrypted() signal.
|
---|
960 |
|
---|
961 | If an empty list is returned, it can mean the SSL handshake
|
---|
962 | failed, or it can mean the host you are connected to doesn't have
|
---|
963 | a certificate, or it can mean there is no connection.
|
---|
964 |
|
---|
965 | If you want to get only the peer's immediate certificate, use
|
---|
966 | peerCertificate().
|
---|
967 |
|
---|
968 | \sa peerCertificate()
|
---|
969 | */
|
---|
970 | QList<QSslCertificate> QSslSocket::peerCertificateChain() const
|
---|
971 | {
|
---|
972 | Q_D(const QSslSocket);
|
---|
973 | return d->configuration.peerCertificateChain;
|
---|
974 | }
|
---|
975 |
|
---|
976 | /*!
|
---|
977 | Returns the socket's cryptographic \l {QSslCipher} {cipher}, or a
|
---|
978 | null cipher if the connection isn't encrypted. The socket's cipher
|
---|
979 | for the session is set during the handshake phase. The cipher is
|
---|
980 | used to encrypt and decrypt data transmitted through the socket.
|
---|
981 |
|
---|
982 | QSslSocket also provides functions for setting the ordered list of
|
---|
983 | ciphers from which the handshake phase will eventually select the
|
---|
984 | session cipher. This ordered list must be in place before the
|
---|
985 | handshake phase begins.
|
---|
986 |
|
---|
987 | \sa ciphers(), setCiphers(), setDefaultCiphers(), defaultCiphers(),
|
---|
988 | supportedCiphers()
|
---|
989 | */
|
---|
990 | QSslCipher QSslSocket::sessionCipher() const
|
---|
991 | {
|
---|
992 | Q_D(const QSslSocket);
|
---|
993 | return d->sessionCipher();
|
---|
994 | }
|
---|
995 |
|
---|
996 | /*!
|
---|
997 | Sets the socket's private \l {QSslKey} {key} to \a key. The
|
---|
998 | private key and the local \l {QSslCertificate} {certificate} are
|
---|
999 | used by clients and servers that must prove their identity to
|
---|
1000 | SSL peers.
|
---|
1001 |
|
---|
1002 | Both the key and the local certificate are required if you are
|
---|
1003 | creating an SSL server socket. If you are creating an SSL client
|
---|
1004 | socket, the key and local certificate are required if your client
|
---|
1005 | must identify itself to an SSL server.
|
---|
1006 |
|
---|
1007 | \sa privateKey(), setLocalCertificate()
|
---|
1008 | */
|
---|
1009 | void QSslSocket::setPrivateKey(const QSslKey &key)
|
---|
1010 | {
|
---|
1011 | Q_D(QSslSocket);
|
---|
1012 | d->configuration.privateKey = key;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | /*!
|
---|
1016 | \overload
|
---|
1017 |
|
---|
1018 | Reads the string in file \a fileName and decodes it using
|
---|
1019 | a specified \a algorithm and encoding \a format to construct
|
---|
1020 | an \l {QSslKey} {SSL key}. If the encoded key is encrypted,
|
---|
1021 | \a passPhrase is used to decrypt it.
|
---|
1022 |
|
---|
1023 | The socket's private key is set to the constructed key. The
|
---|
1024 | private key and the local \l {QSslCertificate} {certificate} are
|
---|
1025 | used by clients and servers that must prove their identity to SSL
|
---|
1026 | peers.
|
---|
1027 |
|
---|
1028 | Both the key and the local certificate are required if you are
|
---|
1029 | creating an SSL server socket. If you are creating an SSL client
|
---|
1030 | socket, the key and local certificate are required if your client
|
---|
1031 | must identify itself to an SSL server.
|
---|
1032 |
|
---|
1033 | \sa privateKey(), setLocalCertificate()
|
---|
1034 | */
|
---|
1035 | void QSslSocket::setPrivateKey(const QString &fileName, QSsl::KeyAlgorithm algorithm,
|
---|
1036 | QSsl::EncodingFormat format, const QByteArray &passPhrase)
|
---|
1037 | {
|
---|
1038 | Q_D(QSslSocket);
|
---|
1039 | QFile file(fileName);
|
---|
1040 | if (file.open(QIODevice::ReadOnly)) {
|
---|
1041 | d->configuration.privateKey = QSslKey(file.readAll(), algorithm,
|
---|
1042 | format, QSsl::PrivateKey, passPhrase);
|
---|
1043 | }
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | /*!
|
---|
1047 | Returns this socket's private key.
|
---|
1048 |
|
---|
1049 | \sa setPrivateKey(), localCertificate()
|
---|
1050 | */
|
---|
1051 | QSslKey QSslSocket::privateKey() const
|
---|
1052 | {
|
---|
1053 | Q_D(const QSslSocket);
|
---|
1054 | return d->configuration.privateKey;
|
---|
1055 | }
|
---|
1056 |
|
---|
1057 | /*!
|
---|
1058 | Returns this socket's current cryptographic cipher suite. This
|
---|
1059 | list is used during the socket's handshake phase for choosing a
|
---|
1060 | session cipher. The returned list of ciphers is ordered by
|
---|
1061 | descending preference. (i.e., the first cipher in the list is the
|
---|
1062 | most preferred cipher). The session cipher will be the first one
|
---|
1063 | in the list that is also supported by the peer.
|
---|
1064 |
|
---|
1065 | By default, the handshake phase can choose any of the ciphers
|
---|
1066 | supported by this system's SSL libraries, which may vary from
|
---|
1067 | system to system. The list of ciphers supported by this system's
|
---|
1068 | SSL libraries is returned by supportedCiphers(). You can restrict
|
---|
1069 | the list of ciphers used for choosing the session cipher for this
|
---|
1070 | socket by calling setCiphers() with a subset of the supported
|
---|
1071 | ciphers. You can revert to using the entire set by calling
|
---|
1072 | setCiphers() with the list returned by supportedCiphers().
|
---|
1073 |
|
---|
1074 | You can restrict the list of ciphers used for choosing the session
|
---|
1075 | cipher for \e all sockets by calling setDefaultCiphers() with a
|
---|
1076 | subset of the supported ciphers. You can revert to using the
|
---|
1077 | entire set by calling setCiphers() with the list returned by
|
---|
1078 | supportedCiphers().
|
---|
1079 |
|
---|
1080 | \sa setCiphers(), defaultCiphers(), setDefaultCiphers(), supportedCiphers()
|
---|
1081 | */
|
---|
1082 | QList<QSslCipher> QSslSocket::ciphers() const
|
---|
1083 | {
|
---|
1084 | Q_D(const QSslSocket);
|
---|
1085 | return d->configuration.ciphers;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /*!
|
---|
1089 | Sets the cryptographic cipher suite for this socket to \a ciphers,
|
---|
1090 | which must contain a subset of the ciphers in the list returned by
|
---|
1091 | supportedCiphers().
|
---|
1092 |
|
---|
1093 | Restricting the cipher suite must be done before the handshake
|
---|
1094 | phase, where the session cipher is chosen.
|
---|
1095 |
|
---|
1096 | \sa ciphers(), setDefaultCiphers(), supportedCiphers()
|
---|
1097 | */
|
---|
1098 | void QSslSocket::setCiphers(const QList<QSslCipher> &ciphers)
|
---|
1099 | {
|
---|
1100 | Q_D(QSslSocket);
|
---|
1101 | d->configuration.ciphers = ciphers;
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | /*!
|
---|
1105 | Sets the cryptographic cipher suite for this socket to \a ciphers, which
|
---|
1106 | is a colon-separated list of cipher suite names. The ciphers are listed in
|
---|
1107 | order of preference, starting with the most preferred cipher. For example:
|
---|
1108 |
|
---|
1109 | \snippet doc/src/snippets/code/src_network_ssl_qsslsocket.cpp 4
|
---|
1110 |
|
---|
1111 | Each cipher name in \a ciphers must be the name of a cipher in the
|
---|
1112 | list returned by supportedCiphers(). Restricting the cipher suite
|
---|
1113 | must be done before the handshake phase, where the session cipher
|
---|
1114 | is chosen.
|
---|
1115 |
|
---|
1116 | \sa ciphers(), setDefaultCiphers(), supportedCiphers()
|
---|
1117 | */
|
---|
1118 | void QSslSocket::setCiphers(const QString &ciphers)
|
---|
1119 | {
|
---|
1120 | Q_D(QSslSocket);
|
---|
1121 | d->configuration.ciphers.clear();
|
---|
1122 | foreach (const QString &cipherName, ciphers.split(QLatin1String(":"),QString::SkipEmptyParts)) {
|
---|
1123 | for (int i = 0; i < 3; ++i) {
|
---|
1124 | // ### Crude
|
---|
1125 | QSslCipher cipher(cipherName, QSsl::SslProtocol(i));
|
---|
1126 | if (!cipher.isNull())
|
---|
1127 | d->configuration.ciphers << cipher;
|
---|
1128 | }
|
---|
1129 | }
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | /*!
|
---|
1133 | Sets the default cryptographic cipher suite for all sockets in
|
---|
1134 | this application to \a ciphers, which must contain a subset of the
|
---|
1135 | ciphers in the list returned by supportedCiphers().
|
---|
1136 |
|
---|
1137 | Restricting the default cipher suite only affects SSL sockets
|
---|
1138 | that perform their handshake phase after the default cipher
|
---|
1139 | suite has been changed.
|
---|
1140 |
|
---|
1141 | \sa setCiphers(), defaultCiphers(), supportedCiphers()
|
---|
1142 | */
|
---|
1143 | void QSslSocket::setDefaultCiphers(const QList<QSslCipher> &ciphers)
|
---|
1144 | {
|
---|
1145 | QSslSocketPrivate::setDefaultCiphers(ciphers);
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | /*!
|
---|
1149 | Returns the default cryptographic cipher suite for all sockets in
|
---|
1150 | this application. This list is used during the socket's handshake
|
---|
1151 | phase when negotiating with the peer to choose a session cipher.
|
---|
1152 | The list is ordered by preference (i.e., the first cipher in the
|
---|
1153 | list is the most preferred cipher).
|
---|
1154 |
|
---|
1155 | By default, the handshake phase can choose any of the ciphers
|
---|
1156 | supported by this system's SSL libraries, which may vary from
|
---|
1157 | system to system. The list of ciphers supported by this system's
|
---|
1158 | SSL libraries is returned by supportedCiphers().
|
---|
1159 |
|
---|
1160 | \sa supportedCiphers()
|
---|
1161 | */
|
---|
1162 | QList<QSslCipher> QSslSocket::defaultCiphers()
|
---|
1163 | {
|
---|
1164 | return QSslSocketPrivate::defaultCiphers();
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | /*!
|
---|
1168 | Returns the list of cryptographic ciphers supported by this
|
---|
1169 | system. This list is set by the system's SSL libraries and may
|
---|
1170 | vary from system to system.
|
---|
1171 |
|
---|
1172 | \sa defaultCiphers(), ciphers(), setCiphers()
|
---|
1173 | */
|
---|
1174 | QList<QSslCipher> QSslSocket::supportedCiphers()
|
---|
1175 | {
|
---|
1176 | return QSslSocketPrivate::supportedCiphers();
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | /*!
|
---|
1180 | Searches all files in the \a path for certificates encoded in the
|
---|
1181 | specified \a format and adds them to this socket's CA certificate
|
---|
1182 | database. \a path can be explicit, or it can contain wildcards in
|
---|
1183 | the format specified by \a syntax. Returns true if one or more
|
---|
1184 | certificates are added to the socket's CA certificate database;
|
---|
1185 | otherwise returns false.
|
---|
1186 |
|
---|
1187 | The CA certificate database is used by the socket during the
|
---|
1188 | handshake phase to validate the peer's certificate.
|
---|
1189 |
|
---|
1190 | For more precise control, use addCaCertificate().
|
---|
1191 |
|
---|
1192 | \sa addCaCertificate(), QSslCertificate::fromPath()
|
---|
1193 | */
|
---|
1194 | bool QSslSocket::addCaCertificates(const QString &path, QSsl::EncodingFormat format,
|
---|
1195 | QRegExp::PatternSyntax syntax)
|
---|
1196 | {
|
---|
1197 | Q_D(QSslSocket);
|
---|
1198 | QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax);
|
---|
1199 | if (certs.isEmpty())
|
---|
1200 | return false;
|
---|
1201 |
|
---|
1202 | d->configuration.caCertificates += certs;
|
---|
1203 | return true;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | /*!
|
---|
1207 | Adds the \a certificate to this socket's CA certificate database.
|
---|
1208 | The CA certificate database is used by the socket during the
|
---|
1209 | handshake phase to validate the peer's certificate.
|
---|
1210 |
|
---|
1211 | To add multiple certificates, use addCaCertificates().
|
---|
1212 |
|
---|
1213 | \sa caCertificates(), setCaCertificates()
|
---|
1214 | */
|
---|
1215 | void QSslSocket::addCaCertificate(const QSslCertificate &certificate)
|
---|
1216 | {
|
---|
1217 | Q_D(QSslSocket);
|
---|
1218 | d->configuration.caCertificates += certificate;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | /*!
|
---|
1222 | Adds the \a certificates to this socket's CA certificate database.
|
---|
1223 | The CA certificate database is used by the socket during the
|
---|
1224 | handshake phase to validate the peer's certificate.
|
---|
1225 |
|
---|
1226 | For more precise control, use addCaCertificate().
|
---|
1227 |
|
---|
1228 | \sa caCertificates(), addDefaultCaCertificate()
|
---|
1229 | */
|
---|
1230 | void QSslSocket::addCaCertificates(const QList<QSslCertificate> &certificates)
|
---|
1231 | {
|
---|
1232 | Q_D(QSslSocket);
|
---|
1233 | d->configuration.caCertificates += certificates;
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | /*!
|
---|
1237 | Sets this socket's CA certificate database to be \a certificates.
|
---|
1238 | The certificate database must be set prior to the SSL handshake.
|
---|
1239 | The CA certificate database is used by the socket during the
|
---|
1240 | handshake phase to validate the peer's certificate.
|
---|
1241 |
|
---|
1242 | The CA certificate database can be reset to the current default CA
|
---|
1243 | certificate database by calling this function with the list of CA
|
---|
1244 | certificates returned by defaultCaCertificates().
|
---|
1245 |
|
---|
1246 | \sa defaultCaCertificates()
|
---|
1247 | */
|
---|
1248 | void QSslSocket::setCaCertificates(const QList<QSslCertificate> &certificates)
|
---|
1249 | {
|
---|
1250 | Q_D(QSslSocket);
|
---|
1251 | d->configuration.caCertificates = certificates;
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | /*!
|
---|
1255 | Returns this socket's CA certificate database. The CA certificate
|
---|
1256 | database is used by the socket during the handshake phase to
|
---|
1257 | validate the peer's certificate. It can be moodified prior to the
|
---|
1258 | handshake with addCaCertificate(), addCaCertificates(), and
|
---|
1259 | setCaCertificates().
|
---|
1260 |
|
---|
1261 | \sa addCaCertificate(), addCaCertificates(), setCaCertificates()
|
---|
1262 | */
|
---|
1263 | QList<QSslCertificate> QSslSocket::caCertificates() const
|
---|
1264 | {
|
---|
1265 | Q_D(const QSslSocket);
|
---|
1266 | return d->configuration.caCertificates;
|
---|
1267 | }
|
---|
1268 |
|
---|
1269 | /*!
|
---|
1270 | Searches all files in the \a path for certificates with the
|
---|
1271 | specified \a encoding and adds them to the default CA certificate
|
---|
1272 | database. \a path can be an explicit file, or it can contain
|
---|
1273 | wildcards in the format specified by \a syntax. Returns true if
|
---|
1274 | any CA certificates are added to the default database.
|
---|
1275 |
|
---|
1276 | Each SSL socket's CA certificate database is initialized to the
|
---|
1277 | default CA certificate database.
|
---|
1278 |
|
---|
1279 | \sa defaultCaCertificates(), addCaCertificates(), addDefaultCaCertificate()
|
---|
1280 | */
|
---|
1281 | bool QSslSocket::addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat encoding,
|
---|
1282 | QRegExp::PatternSyntax syntax)
|
---|
1283 | {
|
---|
1284 | return QSslSocketPrivate::addDefaultCaCertificates(path, encoding, syntax);
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | /*!
|
---|
1288 | Adds \a certificate to the default CA certificate database. Each
|
---|
1289 | SSL socket's CA certificate database is initialized to the default
|
---|
1290 | CA certificate database.
|
---|
1291 |
|
---|
1292 | \sa defaultCaCertificates(), addCaCertificates()
|
---|
1293 | */
|
---|
1294 | void QSslSocket::addDefaultCaCertificate(const QSslCertificate &certificate)
|
---|
1295 | {
|
---|
1296 | QSslSocketPrivate::addDefaultCaCertificate(certificate);
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | /*!
|
---|
1300 | Adds \a certificates to the default CA certificate database. Each
|
---|
1301 | SSL socket's CA certificate database is initialized to the default
|
---|
1302 | CA certificate database.
|
---|
1303 |
|
---|
1304 | \sa defaultCaCertificates(), addCaCertificates()
|
---|
1305 | */
|
---|
1306 | void QSslSocket::addDefaultCaCertificates(const QList<QSslCertificate> &certificates)
|
---|
1307 | {
|
---|
1308 | QSslSocketPrivate::addDefaultCaCertificates(certificates);
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | /*!
|
---|
1312 | Sets the default CA certificate database to \a certificates. The
|
---|
1313 | default CA certificate database is originally set to your system's
|
---|
1314 | default CA certificate database. If no system default database is
|
---|
1315 | found, Qt will provide its own default database. You can override
|
---|
1316 | the default CA certificate database with your own CA certificate
|
---|
1317 | database using this function.
|
---|
1318 |
|
---|
1319 | Each SSL socket's CA certificate database is initialized to the
|
---|
1320 | default CA certificate database.
|
---|
1321 |
|
---|
1322 | \sa addDefaultCaCertificate()
|
---|
1323 | */
|
---|
1324 | void QSslSocket::setDefaultCaCertificates(const QList<QSslCertificate> &certificates)
|
---|
1325 | {
|
---|
1326 | QSslSocketPrivate::setDefaultCaCertificates(certificates);
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | /*!
|
---|
1330 | Returns the current default CA certificate database. This database
|
---|
1331 | is originally set to your system's default CA certificate database.
|
---|
1332 | If no system default database is found, Qt will provide its own
|
---|
1333 | default database. You can override the default CA certificate database
|
---|
1334 | with your own CA certificate database using setDefaultCaCertificates().
|
---|
1335 |
|
---|
1336 | Each SSL socket's CA certificate database is initialized to the
|
---|
1337 | default CA certificate database.
|
---|
1338 |
|
---|
1339 | \sa caCertificates()
|
---|
1340 | */
|
---|
1341 | QList<QSslCertificate> QSslSocket::defaultCaCertificates()
|
---|
1342 | {
|
---|
1343 | return QSslSocketPrivate::defaultCaCertificates();
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | /*!
|
---|
1347 | This function provides a default CA certificate database
|
---|
1348 | shipped together with Qt. The CA certificate database
|
---|
1349 | returned by this function is used to initialize the database
|
---|
1350 | returned by defaultCaCertificates(). You can replace that database
|
---|
1351 | with your own with setDefaultCaCertificates().
|
---|
1352 |
|
---|
1353 | \sa caCertificates(), defaultCaCertificates(), setDefaultCaCertificates()
|
---|
1354 | */
|
---|
1355 | QList<QSslCertificate> QSslSocket::systemCaCertificates()
|
---|
1356 | {
|
---|
1357 | QSslSocketPrivate::ensureInitialized();
|
---|
1358 | return QSslSocketPrivate::systemCaCertificates();
|
---|
1359 | }
|
---|
1360 |
|
---|
1361 | /*!
|
---|
1362 | Waits until the socket is connected, or \a msecs milliseconds,
|
---|
1363 | whichever happens first. If the connection has been established,
|
---|
1364 | this function returns true; otherwise it returns false.
|
---|
1365 |
|
---|
1366 | \sa QAbstractSocket::waitForConnected()
|
---|
1367 | */
|
---|
1368 | bool QSslSocket::waitForConnected(int msecs)
|
---|
1369 | {
|
---|
1370 | Q_D(QSslSocket);
|
---|
1371 | if (!d->plainSocket)
|
---|
1372 | return false;
|
---|
1373 | bool retVal = d->plainSocket->waitForConnected(msecs);
|
---|
1374 | if (!retVal) {
|
---|
1375 | setSocketState(d->plainSocket->state());
|
---|
1376 | setSocketError(d->plainSocket->error());
|
---|
1377 | setErrorString(d->plainSocket->errorString());
|
---|
1378 | }
|
---|
1379 | return retVal;
|
---|
1380 | }
|
---|
1381 |
|
---|
1382 | /*!
|
---|
1383 | Waits until the socket has completed the SSL handshake and has
|
---|
1384 | emitted encrypted(), or \a msecs milliseconds, whichever comes
|
---|
1385 | first. If encrypted() has been emitted, this function returns
|
---|
1386 | true; otherwise (e.g., the socket is disconnected, or the SSL
|
---|
1387 | handshake fails), false is returned.
|
---|
1388 |
|
---|
1389 | The following example waits up to one second for the socket to be
|
---|
1390 | encrypted:
|
---|
1391 |
|
---|
1392 | \snippet doc/src/snippets/code/src_network_ssl_qsslsocket.cpp 5
|
---|
1393 |
|
---|
1394 | If msecs is -1, this function will not time out.
|
---|
1395 |
|
---|
1396 | \sa startClientEncryption(), startServerEncryption(), encrypted(), isEncrypted()
|
---|
1397 | */
|
---|
1398 | bool QSslSocket::waitForEncrypted(int msecs)
|
---|
1399 | {
|
---|
1400 | Q_D(QSslSocket);
|
---|
1401 | if (!d->plainSocket || d->connectionEncrypted)
|
---|
1402 | return false;
|
---|
1403 | if (d->mode == UnencryptedMode && !d->autoStartHandshake)
|
---|
1404 | return false;
|
---|
1405 |
|
---|
1406 | QTime stopWatch;
|
---|
1407 | stopWatch.start();
|
---|
1408 |
|
---|
1409 | if (d->plainSocket->state() != QAbstractSocket::ConnectedState) {
|
---|
1410 | // Wait until we've entered connected state.
|
---|
1411 | if (!d->plainSocket->waitForConnected(msecs))
|
---|
1412 | return false;
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | while (!d->connectionEncrypted) {
|
---|
1416 | // Start the handshake, if this hasn't been started yet.
|
---|
|
---|