Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/ssl/qsslsocket_openssl.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||
2 | ** | - | ||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
4 | ** Copyright (C) 2014 Governikus GmbH & Co. KG | - | ||||||||||||||||||
5 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
6 | ** | - | ||||||||||||||||||
7 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||||||||
8 | ** | - | ||||||||||||||||||
9 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
10 | ** Commercial License Usage | - | ||||||||||||||||||
11 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
12 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
13 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
14 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
15 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
16 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
17 | ** | - | ||||||||||||||||||
18 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
19 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
20 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
21 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
22 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
23 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
24 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
25 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
26 | ** | - | ||||||||||||||||||
27 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
28 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
29 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
30 | ** | - | ||||||||||||||||||
31 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
32 | ** | - | ||||||||||||||||||
33 | ****************************************************************************/ | - | ||||||||||||||||||
34 | - | |||||||||||||||||||
35 | /**************************************************************************** | - | ||||||||||||||||||
36 | ** | - | ||||||||||||||||||
37 | ** In addition, as a special exception, the copyright holders listed above give | - | ||||||||||||||||||
38 | ** permission to link the code of its release of Qt with the OpenSSL project's | - | ||||||||||||||||||
39 | ** "OpenSSL" library (or modified versions of the "OpenSSL" library that use the | - | ||||||||||||||||||
40 | ** same license as the original version), and distribute the linked executables. | - | ||||||||||||||||||
41 | ** | - | ||||||||||||||||||
42 | ** You must comply with the GNU General Public License version 2 in all | - | ||||||||||||||||||
43 | ** respects for all of the code used other than the "OpenSSL" code. If you | - | ||||||||||||||||||
44 | ** modify this file, you may extend this exception to your version of the file, | - | ||||||||||||||||||
45 | ** but you are not obligated to do so. If you do not wish to do so, delete | - | ||||||||||||||||||
46 | ** this exception statement from your version of this file. | - | ||||||||||||||||||
47 | ** | - | ||||||||||||||||||
48 | ****************************************************************************/ | - | ||||||||||||||||||
49 | - | |||||||||||||||||||
50 | //#define QSSLSOCKET_DEBUG | - | ||||||||||||||||||
51 | //#define QT_DECRYPT_SSL_TRAFFIC | - | ||||||||||||||||||
52 | - | |||||||||||||||||||
53 | #include "qssl_p.h" | - | ||||||||||||||||||
54 | #include "qsslsocket_openssl_p.h" | - | ||||||||||||||||||
55 | #include "qsslsocket_openssl_symbols_p.h" | - | ||||||||||||||||||
56 | #include "qsslsocket.h" | - | ||||||||||||||||||
57 | #include "qsslcertificate_p.h" | - | ||||||||||||||||||
58 | #include "qsslcipher_p.h" | - | ||||||||||||||||||
59 | #include "qsslkey_p.h" | - | ||||||||||||||||||
60 | #include "qsslellipticcurve.h" | - | ||||||||||||||||||
61 | #include "qsslpresharedkeyauthenticator.h" | - | ||||||||||||||||||
62 | #include "qsslpresharedkeyauthenticator_p.h" | - | ||||||||||||||||||
63 | - | |||||||||||||||||||
64 | #include <QtCore/qdatetime.h> | - | ||||||||||||||||||
65 | #include <QtCore/qdebug.h> | - | ||||||||||||||||||
66 | #include <QtCore/qdir.h> | - | ||||||||||||||||||
67 | #include <QtCore/qdiriterator.h> | - | ||||||||||||||||||
68 | #include <QtCore/qelapsedtimer.h> | - | ||||||||||||||||||
69 | #include <QtCore/qfile.h> | - | ||||||||||||||||||
70 | #include <QtCore/qfileinfo.h> | - | ||||||||||||||||||
71 | #include <QtCore/qmutex.h> | - | ||||||||||||||||||
72 | #include <QtCore/qthread.h> | - | ||||||||||||||||||
73 | #include <QtCore/qurl.h> | - | ||||||||||||||||||
74 | #include <QtCore/qvarlengtharray.h> | - | ||||||||||||||||||
75 | #include <QLibrary> // for loading the security lib for the CA store | - | ||||||||||||||||||
76 | - | |||||||||||||||||||
77 | #include <string.h> | - | ||||||||||||||||||
78 | - | |||||||||||||||||||
79 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
80 | - | |||||||||||||||||||
81 | #if defined(Q_OS_MACX) | - | ||||||||||||||||||
82 | #define kSecTrustSettingsDomainSystem 2 // so we do not need to include the header file | - | ||||||||||||||||||
83 | PtrSecCertificateCopyData QSslSocketPrivate::ptrSecCertificateCopyData = 0; | - | ||||||||||||||||||
84 | PtrSecTrustSettingsCopyCertificates QSslSocketPrivate::ptrSecTrustSettingsCopyCertificates = 0; | - | ||||||||||||||||||
85 | PtrSecTrustCopyAnchorCertificates QSslSocketPrivate::ptrSecTrustCopyAnchorCertificates = 0; | - | ||||||||||||||||||
86 | #elif defined(Q_OS_WIN) | - | ||||||||||||||||||
87 | PtrCertOpenSystemStoreW QSslSocketPrivate::ptrCertOpenSystemStoreW = 0; | - | ||||||||||||||||||
88 | PtrCertFindCertificateInStore QSslSocketPrivate::ptrCertFindCertificateInStore = 0; | - | ||||||||||||||||||
89 | PtrCertCloseStore QSslSocketPrivate::ptrCertCloseStore = 0; | - | ||||||||||||||||||
90 | #endif | - | ||||||||||||||||||
91 | - | |||||||||||||||||||
92 | bool QSslSocketPrivate::s_libraryLoaded = false; | - | ||||||||||||||||||
93 | bool QSslSocketPrivate::s_loadedCiphersAndCerts = false; | - | ||||||||||||||||||
94 | bool QSslSocketPrivate::s_loadRootCertsOnDemand = false; | - | ||||||||||||||||||
95 | - | |||||||||||||||||||
96 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L | - | ||||||||||||||||||
97 | int QSslSocketBackendPrivate::s_indexForSSLExtraData = -1; | - | ||||||||||||||||||
98 | #endif | - | ||||||||||||||||||
99 | - | |||||||||||||||||||
100 | /* \internal | - | ||||||||||||||||||
101 | - | |||||||||||||||||||
102 | From OpenSSL's thread(3) manual page: | - | ||||||||||||||||||
103 | - | |||||||||||||||||||
104 | OpenSSL can safely be used in multi-threaded applications provided that at | - | ||||||||||||||||||
105 | least two callback functions are set. | - | ||||||||||||||||||
106 | - | |||||||||||||||||||
107 | locking_function(int mode, int n, const char *file, int line) is needed to | - | ||||||||||||||||||
108 | perform locking on shared data structures. (Note that OpenSSL uses a | - | ||||||||||||||||||
109 | number of global data structures that will be implicitly shared | - | ||||||||||||||||||
110 | whenever multiple threads use OpenSSL.) Multi-threaded | - | ||||||||||||||||||
111 | applications will crash at random if it is not set. ... | - | ||||||||||||||||||
112 | ... | - | ||||||||||||||||||
113 | id_function(void) is a function that returns a thread ID. It is not | - | ||||||||||||||||||
114 | needed on Windows nor on platforms where getpid() returns a different | - | ||||||||||||||||||
115 | ID for each thread (most notably Linux) | - | ||||||||||||||||||
116 | */ | - | ||||||||||||||||||
117 | class QOpenSslLocks | - | ||||||||||||||||||
118 | { | - | ||||||||||||||||||
119 | public: | - | ||||||||||||||||||
120 | inline QOpenSslLocks() | - | ||||||||||||||||||
121 | : initLocker(QMutex::Recursive), | - | ||||||||||||||||||
122 | locksLocker(QMutex::Recursive) | - | ||||||||||||||||||
123 | { | - | ||||||||||||||||||
124 | QMutexLocker locker(&locksLocker); | - | ||||||||||||||||||
125 | int numLocks = q_CRYPTO_num_locks(); | - | ||||||||||||||||||
126 | locks = new QMutex *[numLocks]; | - | ||||||||||||||||||
127 | memset(locks, 0, numLocks * sizeof(QMutex *)); | - | ||||||||||||||||||
128 | } executed 16 times by 16 tests: end of block Executed by:
| 16 | ||||||||||||||||||
129 | inline ~QOpenSslLocks() | - | ||||||||||||||||||
130 | { | - | ||||||||||||||||||
131 | QMutexLocker locker(&locksLocker); | - | ||||||||||||||||||
132 | for (int i = 0; i < q_CRYPTO_num_locks(); ++i)
| 16-656 | ||||||||||||||||||
133 | delete locks[i]; executed 656 times by 16 tests: delete locks[i]; Executed by:
| 656 | ||||||||||||||||||
134 | delete [] locks; | - | ||||||||||||||||||
135 | - | |||||||||||||||||||
136 | QSslSocketPrivate::deinitialize(); | - | ||||||||||||||||||
137 | } executed 16 times by 16 tests: end of block Executed by:
| 16 | ||||||||||||||||||
138 | inline QMutex *lock(int num) | - | ||||||||||||||||||
139 | { | - | ||||||||||||||||||
140 | QMutexLocker locker(&locksLocker); | - | ||||||||||||||||||
141 | QMutex *tmp = locks[num]; | - | ||||||||||||||||||
142 | if (!tmp)
| 224-2345162 | ||||||||||||||||||
143 | tmp = locks[num] = new QMutex(QMutex::Recursive); executed 224 times by 17 tests: tmp = locks[num] = new QMutex(QMutex::Recursive); Executed by:
| 224 | ||||||||||||||||||
144 | return tmp; executed 2345386 times by 18 tests: return tmp; Executed by:
| 2345386 | ||||||||||||||||||
145 | } | - | ||||||||||||||||||
146 | - | |||||||||||||||||||
147 | QMutex *globalLock() | - | ||||||||||||||||||
148 | { | - | ||||||||||||||||||
149 | return &locksLocker; never executed: return &locksLocker; | 0 | ||||||||||||||||||
150 | } | - | ||||||||||||||||||
151 | - | |||||||||||||||||||
152 | QMutex *initLock() | - | ||||||||||||||||||
153 | { | - | ||||||||||||||||||
154 | return &initLocker; executed 186598 times by 26 tests: return &initLocker; Executed by:
| 186598 | ||||||||||||||||||
155 | } | - | ||||||||||||||||||
156 | - | |||||||||||||||||||
157 | private: | - | ||||||||||||||||||
158 | QMutex initLocker; | - | ||||||||||||||||||
159 | QMutex locksLocker; | - | ||||||||||||||||||
160 | QMutex **locks; | - | ||||||||||||||||||
161 | }; | - | ||||||||||||||||||
162 | Q_GLOBAL_STATIC(QOpenSslLocks, openssl_locks) executed 16 times by 16 tests: end of block Executed by:
executed 16 times by 16 tests: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 2531984 times by 26 tests: return &holder.value; Executed by:
| 0-2531984 | ||||||||||||||||||
163 | - | |||||||||||||||||||
164 | QString QSslSocketBackendPrivate::getErrorsFromOpenSsl() | - | ||||||||||||||||||
165 | { | - | ||||||||||||||||||
166 | QString errorString; | - | ||||||||||||||||||
167 | unsigned long errNum; | - | ||||||||||||||||||
168 | while ((errNum = q_ERR_get_error())) {
| 25-31 | ||||||||||||||||||
169 | if (! errorString.isEmpty())
| 14-17 | ||||||||||||||||||
170 | errorString.append(QLatin1String(", ")); executed 14 times by 1 test: errorString.append(QLatin1String(", ")); Executed by:
| 14 | ||||||||||||||||||
171 | const char *error = q_ERR_error_string(errNum, NULL); | - | ||||||||||||||||||
172 | errorString.append(QString::fromLatin1(error)); // error is ascii according to man ERR_error_string | - | ||||||||||||||||||
173 | } executed 31 times by 3 tests: end of block Executed by:
| 31 | ||||||||||||||||||
174 | return errorString; executed 25 times by 3 tests: return errorString; Executed by:
| 25 | ||||||||||||||||||
175 | } | - | ||||||||||||||||||
176 | - | |||||||||||||||||||
177 | extern "C" { | - | ||||||||||||||||||
178 | static void locking_function(int mode, int lockNumber, const char *, int) | - | ||||||||||||||||||
179 | { | - | ||||||||||||||||||
180 | QMutex *mutex = openssl_locks()->lock(lockNumber); | - | ||||||||||||||||||
181 | - | |||||||||||||||||||
182 | // Lock or unlock it | - | ||||||||||||||||||
183 | if (mode & CRYPTO_LOCK)
| 1172693 | ||||||||||||||||||
184 | mutex->lock(); executed 1172693 times by 18 tests: mutex->lock(); Executed by:
| 1172693 | ||||||||||||||||||
185 | else | - | ||||||||||||||||||
186 | mutex->unlock(); executed 1172693 times by 18 tests: mutex->unlock(); Executed by:
| 1172693 | ||||||||||||||||||
187 | } | - | ||||||||||||||||||
188 | static unsigned long id_function() | - | ||||||||||||||||||
189 | { | - | ||||||||||||||||||
190 | return (quintptr)QThread::currentThreadId(); executed 57026 times by 17 tests: return (quintptr)QThread::currentThreadId(); Executed by:
| 57026 | ||||||||||||||||||
191 | } | - | ||||||||||||||||||
192 | - | |||||||||||||||||||
193 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L && !defined(OPENSSL_NO_PSK) | - | ||||||||||||||||||
194 | static unsigned int q_ssl_psk_client_callback(SSL *ssl, | - | ||||||||||||||||||
195 | const char *hint, | - | ||||||||||||||||||
196 | char *identity, unsigned int max_identity_len, | - | ||||||||||||||||||
197 | unsigned char *psk, unsigned int max_psk_len) | - | ||||||||||||||||||
198 | { | - | ||||||||||||||||||
199 | QSslSocketBackendPrivate *d = reinterpret_cast<QSslSocketBackendPrivate *>(q_SSL_get_ex_data(ssl, QSslSocketBackendPrivate::s_indexForSSLExtraData)); | - | ||||||||||||||||||
200 | Q_ASSERT(d); | - | ||||||||||||||||||
201 | return d->tlsPskClientCallback(hint, identity, max_identity_len, psk, max_psk_len); never executed: return d->tlsPskClientCallback(hint, identity, max_identity_len, psk, max_psk_len); | 0 | ||||||||||||||||||
202 | } | - | ||||||||||||||||||
203 | #endif | - | ||||||||||||||||||
204 | } // extern "C" | - | ||||||||||||||||||
205 | - | |||||||||||||||||||
206 | QSslSocketBackendPrivate::QSslSocketBackendPrivate() | - | ||||||||||||||||||
207 | : ssl(0), | - | ||||||||||||||||||
208 | readBio(0), | - | ||||||||||||||||||
209 | writeBio(0), | - | ||||||||||||||||||
210 | session(0) | - | ||||||||||||||||||
211 | { | - | ||||||||||||||||||
212 | // Calls SSL_library_init(). | - | ||||||||||||||||||
213 | ensureInitialized(); | - | ||||||||||||||||||
214 | } executed 713 times by 8 tests: end of block Executed by:
| 713 | ||||||||||||||||||
215 | - | |||||||||||||||||||
216 | QSslSocketBackendPrivate::~QSslSocketBackendPrivate() | - | ||||||||||||||||||
217 | { | - | ||||||||||||||||||
218 | destroySslContext(); | - | ||||||||||||||||||
219 | } executed 707 times by 9 tests: end of block Executed by:
| 707 | ||||||||||||||||||
220 | - | |||||||||||||||||||
221 | QSslCipher QSslSocketBackendPrivate::QSslCipher_from_SSL_CIPHER(SSL_CIPHER *cipher) | - | ||||||||||||||||||
222 | { | - | ||||||||||||||||||
223 | QSslCipher ciph; | - | ||||||||||||||||||
224 | - | |||||||||||||||||||
225 | char buf [256]; | - | ||||||||||||||||||
226 | QString descriptionOneLine = QString::fromLatin1(q_SSL_CIPHER_description(cipher, buf, sizeof(buf))); | - | ||||||||||||||||||
227 | - | |||||||||||||||||||
228 | QStringList descriptionList = descriptionOneLine.split(QLatin1Char(' '), QString::SkipEmptyParts); | - | ||||||||||||||||||
229 | if (descriptionList.size() > 5) {
| 0-1777 | ||||||||||||||||||
230 | // ### crude code. | - | ||||||||||||||||||
231 | ciph.d->isNull = false; | - | ||||||||||||||||||
232 | ciph.d->name = descriptionList.at(0); | - | ||||||||||||||||||
233 | - | |||||||||||||||||||
234 | QString protoString = descriptionList.at(1); | - | ||||||||||||||||||
235 | ciph.d->protocolString = protoString; | - | ||||||||||||||||||
236 | ciph.d->protocol = QSsl::UnknownProtocol; | - | ||||||||||||||||||
237 | if (protoString == QLatin1String("SSLv3"))
| 737-1040 | ||||||||||||||||||
238 | ciph.d->protocol = QSsl::SslV3; executed 737 times by 15 tests: ciph.d->protocol = QSsl::SslV3; Executed by:
| 737 | ||||||||||||||||||
239 | else if (protoString == QLatin1String("SSLv2"))
| 0-1040 | ||||||||||||||||||
240 | ciph.d->protocol = QSsl::SslV2; never executed: ciph.d->protocol = QSsl::SslV2; | 0 | ||||||||||||||||||
241 | else if (protoString == QLatin1String("TLSv1"))
| 0-1040 | ||||||||||||||||||
242 | ciph.d->protocol = QSsl::TlsV1_0; never executed: ciph.d->protocol = QSsl::TlsV1_0; | 0 | ||||||||||||||||||
243 | else if (protoString == QLatin1String("TLSv1.1"))
| 0-1040 | ||||||||||||||||||
244 | ciph.d->protocol = QSsl::TlsV1_1; never executed: ciph.d->protocol = QSsl::TlsV1_1; | 0 | ||||||||||||||||||
245 | else if (protoString == QLatin1String("TLSv1.2"))
| 0-1040 | ||||||||||||||||||
246 | ciph.d->protocol = QSsl::TlsV1_2; executed 1040 times by 16 tests: ciph.d->protocol = QSsl::TlsV1_2; Executed by:
| 1040 | ||||||||||||||||||
247 | - | |||||||||||||||||||
248 | if (descriptionList.at(2).startsWith(QLatin1String("Kx=")))
| 0-1777 | ||||||||||||||||||
249 | ciph.d->keyExchangeMethod = descriptionList.at(2).mid(3); executed 1777 times by 16 tests: ciph.d->keyExchangeMethod = descriptionList.at(2).mid(3); Executed by:
| 1777 | ||||||||||||||||||
250 | if (descriptionList.at(3).startsWith(QLatin1String("Au=")))
| 0-1777 | ||||||||||||||||||
251 | ciph.d->authenticationMethod = descriptionList.at(3).mid(3); executed 1777 times by 16 tests: ciph.d->authenticationMethod = descriptionList.at(3).mid(3); Executed by:
| 1777 | ||||||||||||||||||
252 | if (descriptionList.at(4).startsWith(QLatin1String("Enc=")))
| 0-1777 | ||||||||||||||||||
253 | ciph.d->encryptionMethod = descriptionList.at(4).mid(4); executed 1777 times by 16 tests: ciph.d->encryptionMethod = descriptionList.at(4).mid(4); Executed by:
| 1777 | ||||||||||||||||||
254 | ciph.d->exportable = (descriptionList.size() > 6 && descriptionList.at(6) == QLatin1String("export"));
| 0-1762 | ||||||||||||||||||
255 | - | |||||||||||||||||||
256 | ciph.d->bits = q_SSL_CIPHER_get_bits(cipher, &ciph.d->supportedBits); | - | ||||||||||||||||||
257 | } executed 1777 times by 16 tests: end of block Executed by:
| 1777 | ||||||||||||||||||
258 | return ciph; executed 1777 times by 16 tests: return ciph; Executed by:
| 1777 | ||||||||||||||||||
259 | } | - | ||||||||||||||||||
260 | - | |||||||||||||||||||
261 | // ### This list is shared between all threads, and protected by a | - | ||||||||||||||||||
262 | // mutex. Investigate using thread local storage instead. | - | ||||||||||||||||||
263 | struct QSslErrorList | - | ||||||||||||||||||
264 | { | - | ||||||||||||||||||
265 | QMutex mutex; | - | ||||||||||||||||||
266 | QList<QPair<int, int> > errors; | - | ||||||||||||||||||
267 | }; | - | ||||||||||||||||||
268 | Q_GLOBAL_STATIC(QSslErrorList, _q_sslErrorList) executed 7 times by 7 tests: end of block Executed by:
executed 7 times by 7 tests: guard.store(QtGlobalStatic::Destroyed); Executed by:
executed 12800 times by 8 tests: return &holder.value; Executed by:
| 0-12800 | ||||||||||||||||||
269 | - | |||||||||||||||||||
270 | int q_X509Callback(int ok, X509_STORE_CTX *ctx) | - | ||||||||||||||||||
271 | { | - | ||||||||||||||||||
272 | if (!ok) {
| 369-375 | ||||||||||||||||||
273 | // Store the error and at which depth the error was detected. | - | ||||||||||||||||||
274 | _q_sslErrorList()->errors << qMakePair<int, int>(q_X509_STORE_CTX_get_error(ctx), q_X509_STORE_CTX_get_error_depth(ctx)); | - | ||||||||||||||||||
275 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
276 | qCDebug(lcSsl) << "verification error: dumping bad certificate"; | - | ||||||||||||||||||
277 | qCDebug(lcSsl) << QSslCertificatePrivate::QSslCertificate_from_X509(q_X509_STORE_CTX_get_current_cert(ctx)).toPem(); | - | ||||||||||||||||||
278 | qCDebug(lcSsl) << "dumping chain"; | - | ||||||||||||||||||
279 | foreach (QSslCertificate cert, QSslSocketBackendPrivate::STACKOFX509_to_QSslCertificates(q_X509_STORE_CTX_get_chain(ctx))) { | - | ||||||||||||||||||
280 | qCDebug(lcSsl) << "Issuer:" << "O=" << cert.issuerInfo(QSslCertificate::Organization) | - | ||||||||||||||||||
281 | << "CN=" << cert.issuerInfo(QSslCertificate::CommonName) | - | ||||||||||||||||||
282 | << "L=" << cert.issuerInfo(QSslCertificate::LocalityName) | - | ||||||||||||||||||
283 | << "OU=" << cert.issuerInfo(QSslCertificate::OrganizationalUnitName) | - | ||||||||||||||||||
284 | << "C=" << cert.issuerInfo(QSslCertificate::CountryName) | - | ||||||||||||||||||
285 | << "ST=" << cert.issuerInfo(QSslCertificate::StateOrProvinceName); | - | ||||||||||||||||||
286 | qCDebug(lcSsl) << "Subject:" << "O=" << cert.subjectInfo(QSslCertificate::Organization) | - | ||||||||||||||||||
287 | << "CN=" << cert.subjectInfo(QSslCertificate::CommonName) | - | ||||||||||||||||||
288 | << "L=" << cert.subjectInfo(QSslCertificate::LocalityName) | - | ||||||||||||||||||
289 | << "OU=" << cert.subjectInfo(QSslCertificate::OrganizationalUnitName) | - | ||||||||||||||||||
290 | << "C=" << cert.subjectInfo(QSslCertificate::CountryName) | - | ||||||||||||||||||
291 | << "ST=" << cert.subjectInfo(QSslCertificate::StateOrProvinceName); | - | ||||||||||||||||||
292 | qCDebug(lcSsl) << "Valid:" << cert.effectiveDate() << '-' << cert.expiryDate(); | - | ||||||||||||||||||
293 | } | - | ||||||||||||||||||
294 | #endif | - | ||||||||||||||||||
295 | } executed 375 times by 8 tests: end of block Executed by:
| 375 | ||||||||||||||||||
296 | // Always return OK to allow verification to continue. We're handle the | - | ||||||||||||||||||
297 | // errors gracefully after collecting all errors, after verification has | - | ||||||||||||||||||
298 | // completed. | - | ||||||||||||||||||
299 | return 1; executed 744 times by 8 tests: return 1; Executed by:
| 744 | ||||||||||||||||||
300 | } | - | ||||||||||||||||||
301 | - | |||||||||||||||||||
302 | long QSslSocketBackendPrivate::setupOpenSslOptions(QSsl::SslProtocol protocol, QSsl::SslOptions sslOptions) | - | ||||||||||||||||||
303 | { | - | ||||||||||||||||||
304 | long options; | - | ||||||||||||||||||
305 | if (protocol == QSsl::TlsV1SslV3)
| 18-466 | ||||||||||||||||||
306 | options = SSL_OP_ALL|SSL_OP_NO_SSLv2; executed 18 times by 1 test: options = 0x80000BFFL|0x01000000L; Executed by:
| 18 | ||||||||||||||||||
307 | else if (protocol == QSsl::SecureProtocols)
| 110-356 | ||||||||||||||||||
308 | options = SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3; executed 356 times by 7 tests: options = 0x80000BFFL|0x01000000L|0x02000000L; Executed by:
| 356 | ||||||||||||||||||
309 | else if (protocol == QSsl::TlsV1_0OrLater)
| 0-110 | ||||||||||||||||||
310 | options = SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3; never executed: options = 0x80000BFFL|0x01000000L|0x02000000L; | 0 | ||||||||||||||||||
311 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L | - | ||||||||||||||||||
312 | // Choosing Tlsv1_1OrLater or TlsV1_2OrLater on OpenSSL < 1.0.1 | - | ||||||||||||||||||
313 | // will cause an error in QSslContext::fromConfiguration, meaning | - | ||||||||||||||||||
314 | // we will never get here. | - | ||||||||||||||||||
315 | else if (protocol == QSsl::TlsV1_1OrLater)
| 0-110 | ||||||||||||||||||
316 | options = SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1; never executed: options = 0x80000BFFL|0x01000000L|0x02000000L|0x04000000L; | 0 | ||||||||||||||||||
317 | else if (protocol == QSsl::TlsV1_2OrLater)
| 0-110 | ||||||||||||||||||
318 | options = SSL_OP_ALL|SSL_OP_NO_SSLv2|SSL_OP_NO_SSLv3|SSL_OP_NO_TLSv1|SSL_OP_NO_TLSv1_1; never executed: options = 0x80000BFFL|0x01000000L|0x02000000L|0x04000000L|0x10000000L; | 0 | ||||||||||||||||||
319 | #endif | - | ||||||||||||||||||
320 | else | - | ||||||||||||||||||
321 | options = SSL_OP_ALL; executed 110 times by 2 tests: options = 0x80000BFFL; Executed by:
| 110 | ||||||||||||||||||
322 | - | |||||||||||||||||||
323 | // This option is disabled by default, so we need to be able to clear it | - | ||||||||||||||||||
324 | if (sslOptions & QSsl::SslOptionDisableEmptyFragments)
| 5-479 | ||||||||||||||||||
325 | options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; executed 479 times by 7 tests: options |= 0x00000800L; Executed by:
| 479 | ||||||||||||||||||
326 | else | - | ||||||||||||||||||
327 | options &= ~SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS; executed 5 times by 1 test: options &= ~0x00000800L; Executed by:
| 5 | ||||||||||||||||||
328 | - | |||||||||||||||||||
329 | #ifdef SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION | - | ||||||||||||||||||
330 | // This option is disabled by default, so we need to be able to clear it | - | ||||||||||||||||||
331 | if (sslOptions & QSsl::SslOptionDisableLegacyRenegotiation)
| 5-479 | ||||||||||||||||||
332 | options &= ~SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION; executed 479 times by 7 tests: options &= ~0x00040000L; Executed by:
| 479 | ||||||||||||||||||
333 | else | - | ||||||||||||||||||
334 | options |= SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION; executed 5 times by 1 test: options |= 0x00040000L; Executed by:
| 5 | ||||||||||||||||||
335 | #endif | - | ||||||||||||||||||
336 | - | |||||||||||||||||||
337 | #ifdef SSL_OP_NO_TICKET | - | ||||||||||||||||||
338 | if (sslOptions & QSsl::SslOptionDisableSessionTickets)
| 10-474 | ||||||||||||||||||
339 | options |= SSL_OP_NO_TICKET; executed 10 times by 1 test: options |= 0x00004000L; Executed by:
| 10 | ||||||||||||||||||
340 | #endif | - | ||||||||||||||||||
341 | #ifdef SSL_OP_NO_COMPRESSION | - | ||||||||||||||||||
342 | if (sslOptions & QSsl::SslOptionDisableCompression)
| 20-464 | ||||||||||||||||||
343 | options |= SSL_OP_NO_COMPRESSION; executed 464 times by 7 tests: options |= 0x00020000L; Executed by:
| 464 | ||||||||||||||||||
344 | #endif | - | ||||||||||||||||||
345 | - | |||||||||||||||||||
346 | if (!(sslOptions & QSsl::SslOptionDisableServerCipherPreference))
| 1-483 | ||||||||||||||||||
347 | options |= SSL_OP_CIPHER_SERVER_PREFERENCE; executed 483 times by 7 tests: options |= 0x00400000L; Executed by:
| 483 | ||||||||||||||||||
348 | - | |||||||||||||||||||
349 | return options; executed 484 times by 7 tests: return options; Executed by:
| 484 | ||||||||||||||||||
350 | } | - | ||||||||||||||||||
351 | - | |||||||||||||||||||
352 | bool QSslSocketBackendPrivate::initSslContext() | - | ||||||||||||||||||
353 | { | - | ||||||||||||||||||
354 | Q_Q(QSslSocket); | - | ||||||||||||||||||
355 | - | |||||||||||||||||||
356 | // If no external context was set (e.g. bei QHttpNetworkConnection) we will create a default context | - | ||||||||||||||||||
357 | if (!sslContextPointer) {
| 25-454 | ||||||||||||||||||
358 | // create a deep copy of our configuration | - | ||||||||||||||||||
359 | QSslConfigurationPrivate *configurationCopy = new QSslConfigurationPrivate(configuration); | - | ||||||||||||||||||
360 | configurationCopy->ref.store(0); // the QSslConfiguration constructor refs up | - | ||||||||||||||||||
361 | sslContextPointer = QSharedPointer<QSslContext>( | - | ||||||||||||||||||
362 | QSslContext::fromConfiguration(mode, configurationCopy, allowRootCertOnDemandLoading)); | - | ||||||||||||||||||
363 | } executed 454 times by 7 tests: end of block Executed by:
| 454 | ||||||||||||||||||
364 | - | |||||||||||||||||||
365 | if (sslContextPointer->error() != QSslError::NoError) {
| 8-471 | ||||||||||||||||||
366 | setErrorAndEmit(QAbstractSocket::SslInvalidUserDataError, sslContextPointer->errorString()); | - | ||||||||||||||||||
367 | sslContextPointer.clear(); // deletes the QSslContext | - | ||||||||||||||||||
368 | return false; executed 8 times by 2 tests: return false; Executed by:
| 8 | ||||||||||||||||||
369 | } | - | ||||||||||||||||||
370 | - | |||||||||||||||||||
371 | // Create and initialize SSL session | - | ||||||||||||||||||
372 | if (!(ssl = sslContextPointer->createSsl())) {
| 0-471 | ||||||||||||||||||
373 | // ### Bad error code | - | ||||||||||||||||||
374 | setErrorAndEmit(QAbstractSocket::SslInternalError, | - | ||||||||||||||||||
375 | QSslSocket::tr("Error creating SSL session, %1").arg(getErrorsFromOpenSsl())); | - | ||||||||||||||||||
376 | return false; never executed: return false; | 0 | ||||||||||||||||||
377 | } | - | ||||||||||||||||||
378 | - | |||||||||||||||||||
379 | if (configuration.protocol != QSsl::SslV2 &&
| 0-471 | ||||||||||||||||||
380 | configuration.protocol != QSsl::SslV3 &&
| 19-452 | ||||||||||||||||||
381 | configuration.protocol != QSsl::UnknownProtocol &&
| 0-452 | ||||||||||||||||||
382 | mode == QSslSocket::SslClientMode && q_SSLeay() >= 0x00090806fL) {
| 0-395 | ||||||||||||||||||
383 | // Set server hostname on TLS extension. RFC4366 section 3.1 requires it in ACE format. | - | ||||||||||||||||||
384 | QString tlsHostName = verificationPeerName.isEmpty() ? q->peerName() : verificationPeerName;
| 5-390 | ||||||||||||||||||
385 | if (tlsHostName.isEmpty())
| 0-395 | ||||||||||||||||||
386 | tlsHostName = hostName; never executed: tlsHostName = hostName; | 0 | ||||||||||||||||||
387 | QByteArray ace = QUrl::toAce(tlsHostName); | - | ||||||||||||||||||
388 | // only send the SNI header if the URL is valid and not an IP | - | ||||||||||||||||||
389 | if (!ace.isEmpty()
| 0-395 | ||||||||||||||||||
390 | && !QHostAddress().setAddress(tlsHostName)
| 71-324 | ||||||||||||||||||
391 | && !(configuration.sslOptions & QSsl::SslOptionDisableServerNameIndication)) {
| 0-324 | ||||||||||||||||||
392 | // We don't send the trailing dot from the host header if present see | - | ||||||||||||||||||
393 | // https://tools.ietf.org/html/rfc6066#section-3 | - | ||||||||||||||||||
394 | if (ace.endsWith('.'))
| 0-324 | ||||||||||||||||||
395 | ace.chop(1); never executed: ace.chop(1); | 0 | ||||||||||||||||||
396 | if (!q_SSL_ctrl(ssl, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name, ace.data()))
| 0-324 | ||||||||||||||||||
397 | qCWarning(lcSsl, "could not set SSL_CTRL_SET_TLSEXT_HOSTNAME, Server Name Indication disabled"); never executed: QMessageLogger(__FILE__, 397, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("could not set SSL_CTRL_SET_TLSEXT_HOSTNAME, Server Name Indication disabled");
| 0 | ||||||||||||||||||
398 | } executed 324 times by 7 tests: end of block Executed by:
| 324 | ||||||||||||||||||
399 | } executed 395 times by 7 tests: end of block Executed by:
| 395 | ||||||||||||||||||
400 | - | |||||||||||||||||||
401 | // Clear the session. | - | ||||||||||||||||||
402 | errorList.clear(); | - | ||||||||||||||||||
403 | - | |||||||||||||||||||
404 | // Initialize memory BIOs for encryption and decryption. | - | ||||||||||||||||||
405 | readBio = q_BIO_new(q_BIO_s_mem()); | - | ||||||||||||||||||
406 | writeBio = q_BIO_new(q_BIO_s_mem()); | - | ||||||||||||||||||
407 | if (!readBio || !writeBio) {
| 0-471 | ||||||||||||||||||
408 | setErrorAndEmit(QAbstractSocket::SslInternalError, | - | ||||||||||||||||||
409 | QSslSocket::tr("Error creating SSL session: %1").arg(getErrorsFromOpenSsl())); | - | ||||||||||||||||||
410 | return false; never executed: return false; | 0 | ||||||||||||||||||
411 | } | - | ||||||||||||||||||
412 | - | |||||||||||||||||||
413 | // Assign the bios. | - | ||||||||||||||||||
414 | q_SSL_set_bio(ssl, readBio, writeBio); | - | ||||||||||||||||||
415 | - | |||||||||||||||||||
416 | if (mode == QSslSocket::SslClientMode)
| 62-409 | ||||||||||||||||||
417 | q_SSL_set_connect_state(ssl); executed 409 times by 7 tests: q_SSL_set_connect_state(ssl); Executed by:
| 409 | ||||||||||||||||||
418 | else | - | ||||||||||||||||||
419 | q_SSL_set_accept_state(ssl); executed 62 times by 2 tests: q_SSL_set_accept_state(ssl); Executed by:
| 62 | ||||||||||||||||||
420 | - | |||||||||||||||||||
421 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L | - | ||||||||||||||||||
422 | // Save a pointer to this object into the SSL structure. | - | ||||||||||||||||||
423 | if (q_SSLeay() >= 0x10001000L)
| 0-471 | ||||||||||||||||||
424 | q_SSL_set_ex_data(ssl, s_indexForSSLExtraData, this); executed 471 times by 7 tests: q_SSL_set_ex_data(ssl, s_indexForSSLExtraData, this); Executed by:
| 471 | ||||||||||||||||||
425 | #endif | - | ||||||||||||||||||
426 | - | |||||||||||||||||||
427 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L && !defined(OPENSSL_NO_PSK) | - | ||||||||||||||||||
428 | // Set the client callback for PSK | - | ||||||||||||||||||
429 | if (q_SSLeay() >= 0x10001000L && mode == QSslSocket::SslClientMode)
| 0-471 | ||||||||||||||||||
430 | q_SSL_set_psk_client_callback(ssl, &q_ssl_psk_client_callback); executed 409 times by 7 tests: q_SSL_set_psk_client_callback(ssl, &q_ssl_psk_client_callback); Executed by:
| 409 | ||||||||||||||||||
431 | #endif | - | ||||||||||||||||||
432 | - | |||||||||||||||||||
433 | return true; executed 471 times by 7 tests: return true; Executed by:
| 471 | ||||||||||||||||||
434 | } | - | ||||||||||||||||||
435 | - | |||||||||||||||||||
436 | void QSslSocketBackendPrivate::destroySslContext() | - | ||||||||||||||||||
437 | { | - | ||||||||||||||||||
438 | if (ssl) {
| 470-916 | ||||||||||||||||||
439 | q_SSL_free(ssl); | - | ||||||||||||||||||
440 | ssl = 0; | - | ||||||||||||||||||
441 | } executed 470 times by 8 tests: end of block Executed by:
| 470 | ||||||||||||||||||
442 | sslContextPointer.clear(); | - | ||||||||||||||||||
443 | } executed 1386 times by 9 tests: end of block Executed by:
| 1386 | ||||||||||||||||||
444 | - | |||||||||||||||||||
445 | /*! | - | ||||||||||||||||||
446 | \internal | - | ||||||||||||||||||
447 | */ | - | ||||||||||||||||||
448 | void QSslSocketPrivate::deinitialize() | - | ||||||||||||||||||
449 | { | - | ||||||||||||||||||
450 | q_CRYPTO_set_id_callback(0); | - | ||||||||||||||||||
451 | q_CRYPTO_set_locking_callback(0); | - | ||||||||||||||||||
452 | q_ERR_free_strings(); | - | ||||||||||||||||||
453 | } executed 16 times by 16 tests: end of block Executed by:
| 16 | ||||||||||||||||||
454 | - | |||||||||||||||||||
455 | /*! | - | ||||||||||||||||||
456 | \internal | - | ||||||||||||||||||
457 | - | |||||||||||||||||||
458 | Does the minimum amount of initialization to determine whether SSL | - | ||||||||||||||||||
459 | is supported or not. | - | ||||||||||||||||||
460 | */ | - | ||||||||||||||||||
461 | - | |||||||||||||||||||
462 | bool QSslSocketPrivate::supportsSsl() | - | ||||||||||||||||||
463 | { | - | ||||||||||||||||||
464 | return ensureLibraryLoaded(); executed 112700 times by 26 tests: return ensureLibraryLoaded(); Executed by:
| 112700 | ||||||||||||||||||
465 | } | - | ||||||||||||||||||
466 | - | |||||||||||||||||||
467 | bool QSslSocketPrivate::ensureLibraryLoaded() | - | ||||||||||||||||||
468 | { | - | ||||||||||||||||||
469 | if (!q_resolveOpenSslSymbols())
| 0-112700 | ||||||||||||||||||
470 | return false; never executed: return false; | 0 | ||||||||||||||||||
471 | - | |||||||||||||||||||
472 | // Check if the library itself needs to be initialized. | - | ||||||||||||||||||
473 | QMutexLocker locker(openssl_locks()->initLock()); | - | ||||||||||||||||||
474 | - | |||||||||||||||||||
475 | if (!s_libraryLoaded) {
| 16-112684 | ||||||||||||||||||
476 | s_libraryLoaded = true; | - | ||||||||||||||||||
477 | - | |||||||||||||||||||
478 | // Initialize OpenSSL. | - | ||||||||||||||||||
479 | q_CRYPTO_set_id_callback(id_function); | - | ||||||||||||||||||
480 | q_CRYPTO_set_locking_callback(locking_function); | - | ||||||||||||||||||
481 | if (q_SSL_library_init() != 1)
| 0-16 | ||||||||||||||||||
482 | return false; never executed: return false; | 0 | ||||||||||||||||||
483 | q_SSL_load_error_strings(); | - | ||||||||||||||||||
484 | q_OpenSSL_add_all_algorithms(); | - | ||||||||||||||||||
485 | - | |||||||||||||||||||
486 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L | - | ||||||||||||||||||
487 | if (q_SSLeay() >= 0x10001000L)
| 0-16 | ||||||||||||||||||
488 | QSslSocketBackendPrivate::s_indexForSSLExtraData = q_SSL_get_ex_new_index(0L, NULL, NULL, NULL, NULL); executed 16 times by 16 tests: QSslSocketBackendPrivate::s_indexForSSLExtraData = q_SSL_get_ex_new_index(0L, __null, __null, __null, __null); Executed by:
| 16 | ||||||||||||||||||
489 | #endif | - | ||||||||||||||||||
490 | - | |||||||||||||||||||
491 | // Initialize OpenSSL's random seed. | - | ||||||||||||||||||
492 | if (!q_RAND_status()) {
| 0-16 | ||||||||||||||||||
493 | qWarning("Random number generator not seeded, disabling SSL support"); | - | ||||||||||||||||||
494 | return false; never executed: return false; | 0 | ||||||||||||||||||
495 | } | - | ||||||||||||||||||
496 | } executed 16 times by 16 tests: end of block Executed by:
| 16 | ||||||||||||||||||
497 | return true; executed 112700 times by 26 tests: return true; Executed by:
| 112700 | ||||||||||||||||||
498 | } | - | ||||||||||||||||||
499 | - | |||||||||||||||||||
500 | void QSslSocketPrivate::ensureCiphersAndCertsLoaded() | - | ||||||||||||||||||
501 | { | - | ||||||||||||||||||
502 | QMutexLocker locker(openssl_locks()->initLock()); | - | ||||||||||||||||||
503 | if (s_loadedCiphersAndCerts)
| 15-73883 | ||||||||||||||||||
504 | return; executed 73883 times by 16 tests: return; Executed by:
| 73883 | ||||||||||||||||||
505 | s_loadedCiphersAndCerts = true; | - | ||||||||||||||||||
506 | - | |||||||||||||||||||
507 | resetDefaultCiphers(); | - | ||||||||||||||||||
508 | resetDefaultEllipticCurves(); | - | ||||||||||||||||||
509 | - | |||||||||||||||||||
510 | #ifndef QT_NO_LIBRARY | - | ||||||||||||||||||
511 | //load symbols needed to receive certificates from system store | - | ||||||||||||||||||
512 | #if defined(Q_OS_MACX) | - | ||||||||||||||||||
513 | QLibrary securityLib("/System/Library/Frameworks/Security.framework/Versions/Current/Security"); | - | ||||||||||||||||||
514 | if (securityLib.load()) { | - | ||||||||||||||||||
515 | ptrSecCertificateCopyData = (PtrSecCertificateCopyData) securityLib.resolve("SecCertificateCopyData"); | - | ||||||||||||||||||
516 | if (!ptrSecCertificateCopyData) | - | ||||||||||||||||||
517 | qCWarning(lcSsl, "could not resolve symbols in security library"); // should never happen | - | ||||||||||||||||||
518 | - | |||||||||||||||||||
519 | ptrSecTrustSettingsCopyCertificates = (PtrSecTrustSettingsCopyCertificates) securityLib.resolve("SecTrustSettingsCopyCertificates"); | - | ||||||||||||||||||
520 | if (!ptrSecTrustSettingsCopyCertificates) { // method was introduced in Leopard, use legacy method if it's not there | - | ||||||||||||||||||
521 | ptrSecTrustCopyAnchorCertificates = (PtrSecTrustCopyAnchorCertificates) securityLib.resolve("SecTrustCopyAnchorCertificates"); | - | ||||||||||||||||||
522 | if (!ptrSecTrustCopyAnchorCertificates) | - | ||||||||||||||||||
523 | qCWarning(lcSsl, "could not resolve symbols in security library"); // should never happen | - | ||||||||||||||||||
524 | } | - | ||||||||||||||||||
525 | } else { | - | ||||||||||||||||||
526 | qCWarning(lcSsl, "could not load security library"); | - | ||||||||||||||||||
527 | } | - | ||||||||||||||||||
528 | #elif defined(Q_OS_WIN) | - | ||||||||||||||||||
529 | HINSTANCE hLib = LoadLibraryW(L"Crypt32"); | - | ||||||||||||||||||
530 | if (hLib) { | - | ||||||||||||||||||
531 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
532 | ptrCertOpenSystemStoreW = (PtrCertOpenSystemStoreW)GetProcAddress(hLib, L"CertOpenStore"); | - | ||||||||||||||||||
533 | ptrCertFindCertificateInStore = (PtrCertFindCertificateInStore)GetProcAddress(hLib, L"CertFindCertificateInStore"); | - | ||||||||||||||||||
534 | ptrCertCloseStore = (PtrCertCloseStore)GetProcAddress(hLib, L"CertCloseStore"); | - | ||||||||||||||||||
535 | #else | - | ||||||||||||||||||
536 | ptrCertOpenSystemStoreW = (PtrCertOpenSystemStoreW)GetProcAddress(hLib, "CertOpenSystemStoreW"); | - | ||||||||||||||||||
537 | ptrCertFindCertificateInStore = (PtrCertFindCertificateInStore)GetProcAddress(hLib, "CertFindCertificateInStore"); | - | ||||||||||||||||||
538 | ptrCertCloseStore = (PtrCertCloseStore)GetProcAddress(hLib, "CertCloseStore"); | - | ||||||||||||||||||
539 | #endif | - | ||||||||||||||||||
540 | if (!ptrCertOpenSystemStoreW || !ptrCertFindCertificateInStore || !ptrCertCloseStore) | - | ||||||||||||||||||
541 | qCWarning(lcSsl, "could not resolve symbols in crypt32 library"); // should never happen | - | ||||||||||||||||||
542 | } else { | - | ||||||||||||||||||
543 | qCWarning(lcSsl, "could not load crypt32 library"); // should never happen | - | ||||||||||||||||||
544 | } | - | ||||||||||||||||||
545 | #elif defined(Q_OS_QNX) | - | ||||||||||||||||||
546 | s_loadRootCertsOnDemand = true; | - | ||||||||||||||||||
547 | #elif defined(Q_OS_UNIX) && !defined(Q_OS_MAC) | - | ||||||||||||||||||
548 | // check whether we can enable on-demand root-cert loading (i.e. check whether the sym links are there) | - | ||||||||||||||||||
549 | QList<QByteArray> dirs = unixRootCertDirectories(); | - | ||||||||||||||||||
550 | QStringList symLinkFilter; | - | ||||||||||||||||||
551 | symLinkFilter << QLatin1String("[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f].[0-9]"); | - | ||||||||||||||||||
552 | for (int a = 0; a < dirs.count(); ++a) {
| 0-15 | ||||||||||||||||||
553 | QDirIterator iterator(QLatin1String(dirs.at(a)), symLinkFilter, QDir::Files); | - | ||||||||||||||||||
554 | if (iterator.hasNext()) {
| 0-15 | ||||||||||||||||||
555 | s_loadRootCertsOnDemand = true; | - | ||||||||||||||||||
556 | break; executed 15 times by 15 tests: break; Executed by:
| 15 | ||||||||||||||||||
557 | } | - | ||||||||||||||||||
558 | } never executed: end of block | 0 | ||||||||||||||||||
559 | #endif | - | ||||||||||||||||||
560 | #endif //QT_NO_LIBRARY | - | ||||||||||||||||||
561 | // if on-demand loading was not enabled, load the certs now | - | ||||||||||||||||||
562 | if (!s_loadRootCertsOnDemand)
| 0-15 | ||||||||||||||||||
563 | setDefaultCaCertificates(systemCaCertificates()); never executed: setDefaultCaCertificates(systemCaCertificates()); | 0 | ||||||||||||||||||
564 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
565 | //Enabled for fetching additional root certs from windows update on windows 6+ | - | ||||||||||||||||||
566 | //This flag is set false by setDefaultCaCertificates() indicating the app uses | - | ||||||||||||||||||
567 | //its own cert bundle rather than the system one. | - | ||||||||||||||||||
568 | //Same logic that disables the unix on demand cert loading. | - | ||||||||||||||||||
569 | //Unlike unix, we do preload the certificates from the cert store. | - | ||||||||||||||||||
570 | if ((QSysInfo::windowsVersion() & QSysInfo::WV_NT_based) >= QSysInfo::WV_6_0) | - | ||||||||||||||||||
571 | s_loadRootCertsOnDemand = true; | - | ||||||||||||||||||
572 | #endif | - | ||||||||||||||||||
573 | } executed 15 times by 15 tests: end of block Executed by:
| 15 | ||||||||||||||||||
574 | - | |||||||||||||||||||
575 | /*! | - | ||||||||||||||||||
576 | \internal | - | ||||||||||||||||||
577 | - | |||||||||||||||||||
578 | Declared static in QSslSocketPrivate, makes sure the SSL libraries have | - | ||||||||||||||||||
579 | been initialized. | - | ||||||||||||||||||
580 | */ | - | ||||||||||||||||||
581 | - | |||||||||||||||||||
582 | void QSslSocketPrivate::ensureInitialized() | - | ||||||||||||||||||
583 | { | - | ||||||||||||||||||
584 | if (!supportsSsl())
| 0-73898 | ||||||||||||||||||
585 | return; never executed: return; | 0 | ||||||||||||||||||
586 | - | |||||||||||||||||||
587 | ensureCiphersAndCertsLoaded(); | - | ||||||||||||||||||
588 | } executed 73898 times by 16 tests: end of block Executed by:
| 73898 | ||||||||||||||||||
589 | - | |||||||||||||||||||
590 | long QSslSocketPrivate::sslLibraryVersionNumber() | - | ||||||||||||||||||
591 | { | - | ||||||||||||||||||
592 | if (!supportsSsl())
| 0-6 | ||||||||||||||||||
593 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
594 | - | |||||||||||||||||||
595 | return q_SSLeay(); executed 6 times by 1 test: return q_SSLeay(); Executed by:
| 6 | ||||||||||||||||||
596 | } | - | ||||||||||||||||||
597 | - | |||||||||||||||||||
598 | QString QSslSocketPrivate::sslLibraryVersionString() | - | ||||||||||||||||||
599 | { | - | ||||||||||||||||||
600 | if (!supportsSsl())
| 0-6 | ||||||||||||||||||
601 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
602 | - | |||||||||||||||||||
603 | const char *versionString = q_SSLeay_version(SSLEAY_VERSION); | - | ||||||||||||||||||
604 | if (!versionString)
| 0-6 | ||||||||||||||||||
605 | return QString(); never executed: return QString(); | 0 | ||||||||||||||||||
606 | - | |||||||||||||||||||
607 | return QString::fromLatin1(versionString); executed 6 times by 1 test: return QString::fromLatin1(versionString); Executed by:
| 6 | ||||||||||||||||||
608 | } | - | ||||||||||||||||||
609 | - | |||||||||||||||||||
610 | long QSslSocketPrivate::sslLibraryBuildVersionNumber() | - | ||||||||||||||||||
611 | { | - | ||||||||||||||||||
612 | return OPENSSL_VERSION_NUMBER; never executed: return 0x1000114fL; | 0 | ||||||||||||||||||
613 | } | - | ||||||||||||||||||
614 | - | |||||||||||||||||||
615 | QString QSslSocketPrivate::sslLibraryBuildVersionString() | - | ||||||||||||||||||
616 | { | - | ||||||||||||||||||
617 | // Using QStringLiteral to store the version string as unicode and | - | ||||||||||||||||||
618 | // avoid false positives from Google searching the playstore for old | - | ||||||||||||||||||
619 | // SSL versions. See QTBUG-46265 | - | ||||||||||||||||||
620 | return QStringLiteral(OPENSSL_VERSION_TEXT); never executed: return ([]() -> QString { enum { Size = sizeof(u"" "OpenSSL 1.0.1t 3 May 2016")/2 - 1 }; static const QStaticStringData<Size> qstring_literal = { { { { -1 } }, Size, 0, 0, sizeof(QStringData) }, u"" "OpenSSL 1.0.1t 3 May 2016" }; QStringDataPtr holder = { qstring_literal.data_ptr() }; const QString qstring_literal_temp(holder); return qstring_literal_temp; }()); never executed: return qstring_literal_temp; | 0 | ||||||||||||||||||
621 | } | - | ||||||||||||||||||
622 | - | |||||||||||||||||||
623 | /*! | - | ||||||||||||||||||
624 | \internal | - | ||||||||||||||||||
625 | - | |||||||||||||||||||
626 | Declared static in QSslSocketPrivate, backend-dependent loading of | - | ||||||||||||||||||
627 | application-wide global ciphers. | - | ||||||||||||||||||
628 | */ | - | ||||||||||||||||||
629 | void QSslSocketPrivate::resetDefaultCiphers() | - | ||||||||||||||||||
630 | { | - | ||||||||||||||||||
631 | SSL_CTX *myCtx = q_SSL_CTX_new(q_SSLv23_client_method()); | - | ||||||||||||||||||
632 | SSL *mySsl = q_SSL_new(myCtx); | - | ||||||||||||||||||
633 | - | |||||||||||||||||||
634 | QList<QSslCipher> ciphers; | - | ||||||||||||||||||
635 | QList<QSslCipher> defaultCiphers; | - | ||||||||||||||||||
636 | - | |||||||||||||||||||
637 | STACK_OF(SSL_CIPHER) *supportedCiphers = q_SSL_get_ciphers(mySsl); | - | ||||||||||||||||||
638 | for (int i = 0; i < q_sk_SSL_CIPHER_num(supportedCiphers); ++i) {
| 15-1155 | ||||||||||||||||||
639 | if (SSL_CIPHER *cipher = q_sk_SSL_CIPHER_value(supportedCiphers, i)) {
| 0-1155 | ||||||||||||||||||
640 | QSslCipher ciph = QSslSocketBackendPrivate::QSslCipher_from_SSL_CIPHER(cipher); | - | ||||||||||||||||||
641 | if (!ciph.isNull()) {
| 0-1155 | ||||||||||||||||||
642 | // Unconditionally exclude ADH and AECDH ciphers since they offer no MITM protection | - | ||||||||||||||||||
643 | if (!ciph.name().toLower().startsWith(QLatin1String("adh")) &&
| 0-1155 | ||||||||||||||||||
644 | !ciph.name().toLower().startsWith(QLatin1String("exp-adh")) &&
| 0-1155 | ||||||||||||||||||
645 | !ciph.name().toLower().startsWith(QLatin1String("aecdh"))) {
| 0-1155 | ||||||||||||||||||
646 | ciphers << ciph; | - | ||||||||||||||||||
647 | - | |||||||||||||||||||
648 | if (ciph.usedBits() >= 128)
| 165-990 | ||||||||||||||||||
649 | defaultCiphers << ciph; executed 990 times by 15 tests: defaultCiphers << ciph; Executed by:
| 990 | ||||||||||||||||||
650 | } executed 1155 times by 15 tests: end of block Executed by:
| 1155 | ||||||||||||||||||
651 | } executed 1155 times by 15 tests: end of block Executed by:
| 1155 | ||||||||||||||||||
652 | } executed 1155 times by 15 tests: end of block Executed by:
| 1155 | ||||||||||||||||||
653 | } executed 1155 times by 15 tests: end of block Executed by:
| 1155 | ||||||||||||||||||
654 | - | |||||||||||||||||||
655 | q_SSL_CTX_free(myCtx); | - | ||||||||||||||||||
656 | q_SSL_free(mySsl); | - | ||||||||||||||||||
657 | - | |||||||||||||||||||
658 | setDefaultSupportedCiphers(ciphers); | - | ||||||||||||||||||
659 | setDefaultCiphers(defaultCiphers); | - | ||||||||||||||||||
660 | } executed 15 times by 15 tests: end of block Executed by:
| 15 | ||||||||||||||||||
661 | - | |||||||||||||||||||
662 | void QSslSocketPrivate::resetDefaultEllipticCurves() | - | ||||||||||||||||||
663 | { | - | ||||||||||||||||||
664 | QVector<QSslEllipticCurve> curves; | - | ||||||||||||||||||
665 | - | |||||||||||||||||||
666 | #ifndef OPENSSL_NO_EC | - | ||||||||||||||||||
667 | const size_t curveCount = q_EC_get_builtin_curves(NULL, 0); | - | ||||||||||||||||||
668 | - | |||||||||||||||||||
669 | QVarLengthArray<EC_builtin_curve> builtinCurves(static_cast<int>(curveCount)); | - | ||||||||||||||||||
670 | - | |||||||||||||||||||
671 | if (q_EC_get_builtin_curves(builtinCurves.data(), curveCount) == curveCount) {
| 0-15 | ||||||||||||||||||
672 | curves.reserve(int(curveCount)); | - | ||||||||||||||||||
673 | for (size_t i = 0; i < curveCount; ++i) {
| 15-1005 | ||||||||||||||||||
674 | QSslEllipticCurve curve; | - | ||||||||||||||||||
675 | curve.id = builtinCurves[int(i)].nid; | - | ||||||||||||||||||
676 | curves.append(curve); | - | ||||||||||||||||||
677 | } executed 1005 times by 15 tests: end of block Executed by:
| 1005 | ||||||||||||||||||
678 | } executed 15 times by 15 tests: end of block Executed by:
| 15 | ||||||||||||||||||
679 | #endif // OPENSSL_NO_EC | - | ||||||||||||||||||
680 | - | |||||||||||||||||||
681 | // set the list of supported ECs, but not the list | - | ||||||||||||||||||
682 | // of *default* ECs. OpenSSL doesn't like forcing an EC for the wrong | - | ||||||||||||||||||
683 | // ciphersuite, so don't try it -- leave the empty list to mean | - | ||||||||||||||||||
684 | // "the implementation will choose the most suitable one". | - | ||||||||||||||||||
685 | setDefaultSupportedEllipticCurves(curves); | - | ||||||||||||||||||
686 | } executed 15 times by 15 tests: end of block Executed by:
| 15 | ||||||||||||||||||
687 | - | |||||||||||||||||||
688 | QList<QSslCertificate> QSslSocketPrivate::systemCaCertificates() | - | ||||||||||||||||||
689 | { | - | ||||||||||||||||||
690 | ensureInitialized(); | - | ||||||||||||||||||
691 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
692 | QElapsedTimer timer; | - | ||||||||||||||||||
693 | timer.start(); | - | ||||||||||||||||||
694 | #endif | - | ||||||||||||||||||
695 | QList<QSslCertificate> systemCerts; | - | ||||||||||||||||||
696 | #if defined(Q_OS_MACX) | - | ||||||||||||||||||
697 | CFArrayRef cfCerts; | - | ||||||||||||||||||
698 | OSStatus status = 1; | - | ||||||||||||||||||
699 | - | |||||||||||||||||||
700 | CFDataRef SecCertificateCopyData ( | - | ||||||||||||||||||
701 | SecCertificateRef certificate | - | ||||||||||||||||||
702 | ); | - | ||||||||||||||||||
703 | - | |||||||||||||||||||
704 | if (ptrSecCertificateCopyData) { | - | ||||||||||||||||||
705 | if (ptrSecTrustSettingsCopyCertificates) | - | ||||||||||||||||||
706 | status = ptrSecTrustSettingsCopyCertificates(kSecTrustSettingsDomainSystem, &cfCerts); | - | ||||||||||||||||||
707 | else if (ptrSecTrustCopyAnchorCertificates) | - | ||||||||||||||||||
708 | status = ptrSecTrustCopyAnchorCertificates(&cfCerts); | - | ||||||||||||||||||
709 | if (!status) { | - | ||||||||||||||||||
710 | CFIndex size = CFArrayGetCount(cfCerts); | - | ||||||||||||||||||
711 | for (CFIndex i = 0; i < size; ++i) { | - | ||||||||||||||||||
712 | SecCertificateRef cfCert = (SecCertificateRef)CFArrayGetValueAtIndex(cfCerts, i); | - | ||||||||||||||||||
713 | CFDataRef data; | - | ||||||||||||||||||
714 | - | |||||||||||||||||||
715 | data = ptrSecCertificateCopyData(cfCert); | - | ||||||||||||||||||
716 | - | |||||||||||||||||||
717 | if (data == NULL) { | - | ||||||||||||||||||
718 | qCWarning(lcSsl, "error retrieving a CA certificate from the system store"); | - | ||||||||||||||||||
719 | } else { | - | ||||||||||||||||||
720 | QByteArray rawCert = QByteArray::fromRawData((const char *)CFDataGetBytePtr(data), CFDataGetLength(data)); | - | ||||||||||||||||||
721 | systemCerts.append(QSslCertificate::fromData(rawCert, QSsl::Der)); | - | ||||||||||||||||||
722 | CFRelease(data); | - | ||||||||||||||||||
723 | } | - | ||||||||||||||||||
724 | } | - | ||||||||||||||||||
725 | CFRelease(cfCerts); | - | ||||||||||||||||||
726 | } | - | ||||||||||||||||||
727 | else { | - | ||||||||||||||||||
728 | // no detailed error handling here | - | ||||||||||||||||||
729 | qCWarning(lcSsl, "could not retrieve system CA certificates"); | - | ||||||||||||||||||
730 | } | - | ||||||||||||||||||
731 | } | - | ||||||||||||||||||
732 | #elif defined(Q_OS_WIN) | - | ||||||||||||||||||
733 | if (ptrCertOpenSystemStoreW && ptrCertFindCertificateInStore && ptrCertCloseStore) { | - | ||||||||||||||||||
734 | HCERTSTORE hSystemStore; | - | ||||||||||||||||||
735 | #if defined(Q_OS_WINCE) | - | ||||||||||||||||||
736 | hSystemStore = ptrCertOpenSystemStoreW(CERT_STORE_PROV_SYSTEM_W, | - | ||||||||||||||||||
737 | 0, | - | ||||||||||||||||||
738 | 0, | - | ||||||||||||||||||
739 | CERT_STORE_NO_CRYPT_RELEASE_FLAG|CERT_SYSTEM_STORE_CURRENT_USER, | - | ||||||||||||||||||
740 | L"ROOT"); | - | ||||||||||||||||||
741 | #else | - | ||||||||||||||||||
742 | hSystemStore = ptrCertOpenSystemStoreW(0, L"ROOT"); | - | ||||||||||||||||||
743 | #endif | - | ||||||||||||||||||
744 | if(hSystemStore) { | - | ||||||||||||||||||
745 | PCCERT_CONTEXT pc = NULL; | - | ||||||||||||||||||
746 | while(1) { | - | ||||||||||||||||||
747 | pc = ptrCertFindCertificateInStore( hSystemStore, X509_ASN_ENCODING, 0, CERT_FIND_ANY, NULL, pc); | - | ||||||||||||||||||
748 | if(!pc) | - | ||||||||||||||||||
749 | break; | - | ||||||||||||||||||
750 | QByteArray der((const char *)(pc->pbCertEncoded), static_cast<int>(pc->cbCertEncoded)); | - | ||||||||||||||||||
751 | QSslCertificate cert(der, QSsl::Der); | - | ||||||||||||||||||
752 | systemCerts.append(cert); | - | ||||||||||||||||||
753 | } | - | ||||||||||||||||||
754 | ptrCertCloseStore(hSystemStore, 0); | - | ||||||||||||||||||
755 | } | - | ||||||||||||||||||
756 | } | - | ||||||||||||||||||
757 | #elif defined(Q_OS_UNIX) | - | ||||||||||||||||||
758 | QSet<QString> certFiles; | - | ||||||||||||||||||
759 | QDir currentDir; | - | ||||||||||||||||||
760 | QStringList nameFilters; | - | ||||||||||||||||||
761 | QList<QByteArray> directories; | - | ||||||||||||||||||
762 | QSsl::EncodingFormat platformEncodingFormat; | - | ||||||||||||||||||
763 | # ifndef Q_OS_ANDROID | - | ||||||||||||||||||
764 | directories = unixRootCertDirectories(); | - | ||||||||||||||||||
765 | nameFilters << QLatin1String("*.pem") << QLatin1String("*.crt"); | - | ||||||||||||||||||
766 | platformEncodingFormat = QSsl::Pem; | - | ||||||||||||||||||
767 | # else | - | ||||||||||||||||||
768 | // Q_OS_ANDROID | - | ||||||||||||||||||
769 | QByteArray ministroPath = qgetenv("MINISTRO_SSL_CERTS_PATH"); // Set by Ministro | - | ||||||||||||||||||
770 | directories << ministroPath; | - | ||||||||||||||||||
771 | nameFilters << QLatin1String("*.der"); | - | ||||||||||||||||||
772 | platformEncodingFormat = QSsl::Der; | - | ||||||||||||||||||
773 | # ifndef Q_OS_ANDROID_NO_SDK | - | ||||||||||||||||||
774 | if (ministroPath.isEmpty()) { | - | ||||||||||||||||||
775 | QList<QByteArray> certificateData = fetchSslCertificateData(); | - | ||||||||||||||||||
776 | for (int i = 0; i < certificateData.size(); ++i) { | - | ||||||||||||||||||
777 | systemCerts.append(QSslCertificate::fromData(certificateData.at(i), QSsl::Der)); | - | ||||||||||||||||||
778 | } | - | ||||||||||||||||||
779 | } else | - | ||||||||||||||||||
780 | # endif //Q_OS_ANDROID_NO_SDK | - | ||||||||||||||||||
781 | # endif //Q_OS_ANDROID | - | ||||||||||||||||||
782 | { | - | ||||||||||||||||||
783 | currentDir.setNameFilters(nameFilters); | - | ||||||||||||||||||
784 | for (int a = 0; a < directories.count(); a++) {
| 54-486 | ||||||||||||||||||
785 | currentDir.setPath(QLatin1String(directories.at(a))); | - | ||||||||||||||||||
786 | QDirIterator it(currentDir); | - | ||||||||||||||||||
787 | while (it.hasNext()) {
| 486-19008 | ||||||||||||||||||
788 | it.next(); | - | ||||||||||||||||||
789 | // use canonical path here to not load the same certificate twice if symlinked | - | ||||||||||||||||||
790 | certFiles.insert(it.fileInfo().canonicalFilePath()); | - | ||||||||||||||||||
791 | } executed 19008 times by 3 tests: end of block Executed by:
| 19008 | ||||||||||||||||||
792 | } executed 486 times by 3 tests: end of block Executed by:
| 486 | ||||||||||||||||||
793 | QSetIterator<QString> it(certFiles); | - | ||||||||||||||||||
794 | while (it.hasNext())
| 54-9504 | ||||||||||||||||||
795 | systemCerts.append(QSslCertificate::fromPath(it.next(), platformEncodingFormat)); executed 9504 times by 3 tests: systemCerts.append(QSslCertificate::fromPath(it.next(), platformEncodingFormat)); Executed by:
| 9504 | ||||||||||||||||||
796 | # ifndef Q_OS_ANDROID | - | ||||||||||||||||||
797 | systemCerts.append(QSslCertificate::fromPath(QLatin1String("/etc/pki/tls/certs/ca-bundle.crt"), QSsl::Pem)); // Fedora, Mandriva | - | ||||||||||||||||||
798 | systemCerts.append(QSslCertificate::fromPath(QLatin1String("/usr/local/share/certs/ca-root-nss.crt"), QSsl::Pem)); // FreeBSD's ca_root_nss | - | ||||||||||||||||||
799 | # endif | - | ||||||||||||||||||
800 | } | - | ||||||||||||||||||
801 | #endif | - | ||||||||||||||||||
802 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
803 | qCDebug(lcSsl) << "systemCaCertificates retrieval time " << timer.elapsed() << "ms"; | - | ||||||||||||||||||
804 | qCDebug(lcSsl) << "imported " << systemCerts.count() << " certificates"; | - | ||||||||||||||||||
805 | #endif | - | ||||||||||||||||||
806 | - | |||||||||||||||||||
807 | return systemCerts; executed 54 times by 3 tests: return systemCerts; Executed by:
| 54 | ||||||||||||||||||
808 | } | - | ||||||||||||||||||
809 | - | |||||||||||||||||||
810 | void QSslSocketBackendPrivate::startClientEncryption() | - | ||||||||||||||||||
811 | { | - | ||||||||||||||||||
812 | if (!initSslContext()) {
| 7-409 | ||||||||||||||||||
813 | setErrorAndEmit(QAbstractSocket::SslInternalError, | - | ||||||||||||||||||
814 | QSslSocket::tr("Unable to init SSL Context: %1").arg(getErrorsFromOpenSsl())); | - | ||||||||||||||||||
815 | return; executed 7 times by 2 tests: return; Executed by:
| 7 | ||||||||||||||||||
816 | } | - | ||||||||||||||||||
817 | - | |||||||||||||||||||
818 | // Start connecting. This will place outgoing data in the BIO, so we | - | ||||||||||||||||||
819 | // follow up with calling transmit(). | - | ||||||||||||||||||
820 | startHandshake(); | - | ||||||||||||||||||
821 | transmit(); | - | ||||||||||||||||||
822 | } executed 409 times by 7 tests: end of block Executed by:
| 409 | ||||||||||||||||||
823 | - | |||||||||||||||||||
824 | void QSslSocketBackendPrivate::startServerEncryption() | - | ||||||||||||||||||
825 | { | - | ||||||||||||||||||
826 | if (!initSslContext()) {
| 1-62 | ||||||||||||||||||
827 | setErrorAndEmit(QAbstractSocket::SslInternalError, | - | ||||||||||||||||||
828 | QSslSocket::tr("Unable to init SSL Context: %1").arg(getErrorsFromOpenSsl())); | - | ||||||||||||||||||
829 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||||||||
830 | } | - | ||||||||||||||||||
831 | - | |||||||||||||||||||
832 | // Start connecting. This will place outgoing data in the BIO, so we | - | ||||||||||||||||||
833 | // follow up with calling transmit(). | - | ||||||||||||||||||
834 | startHandshake(); | - | ||||||||||||||||||
835 | transmit(); | - | ||||||||||||||||||
836 | } executed 62 times by 2 tests: end of block Executed by:
| 62 | ||||||||||||||||||
837 | - | |||||||||||||||||||
838 | /*! | - | ||||||||||||||||||
839 | \internal | - | ||||||||||||||||||
840 | - | |||||||||||||||||||
841 | Transmits encrypted data between the BIOs and the socket. | - | ||||||||||||||||||
842 | */ | - | ||||||||||||||||||
843 | void QSslSocketBackendPrivate::transmit() | - | ||||||||||||||||||
844 | { | - | ||||||||||||||||||
845 | Q_Q(QSslSocket); | - | ||||||||||||||||||
846 | - | |||||||||||||||||||
847 | // If we don't have any SSL context, don't bother transmitting. | - | ||||||||||||||||||
848 | if (!ssl)
| 44-7626 | ||||||||||||||||||
849 | return; executed 44 times by 3 tests: return; Executed by:
| 44 | ||||||||||||||||||
850 | - | |||||||||||||||||||
851 | bool transmitting; | - | ||||||||||||||||||
852 | do { | - | ||||||||||||||||||
853 | transmitting = false; | - | ||||||||||||||||||
854 | - | |||||||||||||||||||
855 | // If the connection is secure, we can transfer data from the write | - | ||||||||||||||||||
856 | // buffer (in plain text) to the write BIO through SSL_write. | - | ||||||||||||||||||
857 | if (connectionEncrypted && !writeBuffer.isEmpty()) {
| 2630-12973 | ||||||||||||||||||
858 | qint64 totalBytesWritten = 0; | - | ||||||||||||||||||
859 | int nextDataBlockSize; | - | ||||||||||||||||||
860 | while ((nextDataBlockSize = writeBuffer.nextDataBlockSize()) > 0) {
| 4297-4375 | ||||||||||||||||||
861 | int writtenBytes = q_SSL_write(ssl, writeBuffer.readPointer(), nextDataBlockSize); | - | ||||||||||||||||||
862 | if (writtenBytes <= 0) {
| 0-4375 | ||||||||||||||||||
863 | int error = q_SSL_get_error(ssl, writtenBytes); | - | ||||||||||||||||||
864 | //write can result in a want_write_error - not an error - continue transmitting | - | ||||||||||||||||||
865 | if (error == SSL_ERROR_WANT_WRITE) {
| 0 | ||||||||||||||||||
866 | transmitting = true; | - | ||||||||||||||||||
867 | break; never executed: break; | 0 | ||||||||||||||||||
868 | } else if (error == SSL_ERROR_WANT_READ) {
| 0 | ||||||||||||||||||
869 | //write can result in a want_read error, possibly due to renegotiation - not an error - stop transmitting | - | ||||||||||||||||||
870 | transmitting = false; | - | ||||||||||||||||||
871 | break; never executed: break; | 0 | ||||||||||||||||||
872 | } else { | - | ||||||||||||||||||
873 | // ### Better error handling. | - | ||||||||||||||||||
874 | setErrorAndEmit(QAbstractSocket::SslInternalError, | - | ||||||||||||||||||
875 | QSslSocket::tr("Unable to write data: %1").arg( | - | ||||||||||||||||||
876 | getErrorsFromOpenSsl())); | - | ||||||||||||||||||
877 | return; never executed: return; | 0 | ||||||||||||||||||
878 | } | - | ||||||||||||||||||
879 | } | - | ||||||||||||||||||
880 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
881 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: encrypted" << writtenBytes << "bytes"; | - | ||||||||||||||||||
882 | #endif | - | ||||||||||||||||||
883 | writeBuffer.free(writtenBytes); | - | ||||||||||||||||||
884 | totalBytesWritten += writtenBytes; | - | ||||||||||||||||||
885 | - | |||||||||||||||||||
886 | if (writtenBytes < nextDataBlockSize) {
| 0-4375 | ||||||||||||||||||
887 | // break out of the writing loop and try again after we had read | - | ||||||||||||||||||
888 | transmitting = true; | - | ||||||||||||||||||
889 | break; never executed: break; | 0 | ||||||||||||||||||
890 | } | - | ||||||||||||||||||
891 | } executed 4375 times by 5 tests: end of block Executed by:
| 4375 | ||||||||||||||||||
892 | - | |||||||||||||||||||
893 | if (totalBytesWritten > 0) {
| 0-4297 | ||||||||||||||||||
894 | // Don't emit bytesWritten() recursively. | - | ||||||||||||||||||
895 | if (!emittedBytesWritten) {
| 0-4297 | ||||||||||||||||||
896 | emittedBytesWritten = true; | - | ||||||||||||||||||
897 | emit q->bytesWritten(totalBytesWritten); | - | ||||||||||||||||||
898 | emittedBytesWritten = false; | - | ||||||||||||||||||
899 | } executed 4297 times by 5 tests: end of block Executed by:
| 4297 | ||||||||||||||||||
900 | } executed 4297 times by 5 tests: end of block Executed by:
| 4297 | ||||||||||||||||||
901 | } executed 4297 times by 5 tests: end of block Executed by:
| 4297 | ||||||||||||||||||
902 | - | |||||||||||||||||||
903 | // Check if we've got any data to be written to the socket. | - | ||||||||||||||||||
904 | QVarLengthArray<char, 4096> data; | - | ||||||||||||||||||
905 | int pendingBytes; | - | ||||||||||||||||||
906 | while (plainSocket->isValid() && (pendingBytes = q_BIO_pending(writeBio)) > 0) {
| 0-20834 | ||||||||||||||||||
907 | // Read encrypted data from the write BIO into a buffer. | - | ||||||||||||||||||
908 | data.resize(pendingBytes); | - | ||||||||||||||||||
909 | int encryptedBytesRead = q_BIO_read(writeBio, data.data(), pendingBytes); | - | ||||||||||||||||||
910 | - | |||||||||||||||||||
911 | // Write encrypted data from the buffer to the socket. | - | ||||||||||||||||||
912 | qint64 actualWritten = plainSocket->write(data.constData(), encryptedBytesRead); | - | ||||||||||||||||||
913 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
914 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: wrote" << encryptedBytesRead << "encrypted bytes to the socket" << actualWritten << "actual."; | - | ||||||||||||||||||
915 | #endif | - | ||||||||||||||||||
916 | if (actualWritten < 0) {
| 5-5231 | ||||||||||||||||||
917 | //plain socket write fails if it was in the pending close state. | - | ||||||||||||||||||
918 | setErrorAndEmit(plainSocket->error(), plainSocket->errorString()); | - | ||||||||||||||||||
919 | return; executed 5 times by 1 test: return; Executed by:
| 5 | ||||||||||||||||||
920 | } | - | ||||||||||||||||||
921 | transmitting = true; | - | ||||||||||||||||||
922 | } executed 5231 times by 7 tests: end of block Executed by:
| 5231 | ||||||||||||||||||
923 | - | |||||||||||||||||||
924 | // Check if we've got any data to be read from the socket. | - | ||||||||||||||||||
925 | if (!connectionEncrypted || !readBufferMaxSize || buffer.size() < readBufferMaxSize)
| 19-12968 | ||||||||||||||||||
926 | while ((pendingBytes = plainSocket->bytesAvailable()) > 0) {
| 2889-15579 | ||||||||||||||||||
927 | // Read encrypted data from the socket into a buffer. | - | ||||||||||||||||||
928 | data.resize(pendingBytes); | - | ||||||||||||||||||
929 | // just peek() here because q_BIO_write could write less data than expected | - | ||||||||||||||||||
930 | int encryptedBytesRead = plainSocket->peek(data.data(), pendingBytes); | - | ||||||||||||||||||
931 | - | |||||||||||||||||||
932 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
933 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: read" << encryptedBytesRead << "encrypted bytes from the socket"; | - | ||||||||||||||||||
934 | #endif | - | ||||||||||||||||||
935 | // Write encrypted data from the buffer into the read BIO. | - | ||||||||||||||||||
936 | int writtenToBio = q_BIO_write(readBio, data.constData(), encryptedBytesRead); | - | ||||||||||||||||||
937 | - | |||||||||||||||||||
938 | // do the actual read() here and throw away the results. | - | ||||||||||||||||||
939 | if (writtenToBio > 0) {
| 0-2889 | ||||||||||||||||||
940 | // ### TODO: make this cheaper by not making it memcpy. E.g. make it work with data=0x0 or make it work with seek | - | ||||||||||||||||||
941 | plainSocket->read(data.data(), writtenToBio); | - | ||||||||||||||||||
942 | } else { executed 2889 times by 7 tests: end of block Executed by:
| 2889 | ||||||||||||||||||
943 | // ### Better error handling. | - | ||||||||||||||||||
944 | setErrorAndEmit(QAbstractSocket::SslInternalError, | - | ||||||||||||||||||
945 | QSslSocket::tr("Unable to decrypt data: %1").arg( | - | ||||||||||||||||||
946 | getErrorsFromOpenSsl())); | - | ||||||||||||||||||
947 | return; never executed: return; | 0 | ||||||||||||||||||
948 | } | - | ||||||||||||||||||
949 | - | |||||||||||||||||||
950 | transmitting = true; | - | ||||||||||||||||||
951 | } executed 2889 times by 7 tests: end of block Executed by:
| 2889 | ||||||||||||||||||
952 | - | |||||||||||||||||||
953 | // If the connection isn't secured yet, this is the time to retry the | - | ||||||||||||||||||
954 | // connect / accept. | - | ||||||||||||||||||
955 | if (!connectionEncrypted) {
| 2630-12968 | ||||||||||||||||||
956 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
957 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: testing encryption"; | - | ||||||||||||||||||
958 | #endif | - | ||||||||||||||||||
959 | if (startHandshake()) {
| 340-2290 | ||||||||||||||||||
960 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
961 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: encryption established"; | - | ||||||||||||||||||
962 | #endif | - | ||||||||||||||||||
963 | connectionEncrypted = true; | - | ||||||||||||||||||
964 | transmitting = true; | - | ||||||||||||||||||
965 | } else if (plainSocket->state() != QAbstractSocket::ConnectedState) { executed 340 times by 7 tests: end of block Executed by:
| 84-2206 | ||||||||||||||||||
966 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
967 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: connection lost"; | - | ||||||||||||||||||
968 | #endif | - | ||||||||||||||||||
969 | break; executed 84 times by 6 tests: break; Executed by:
| 84 | ||||||||||||||||||
970 | } else if (paused) {
| 20-2186 | ||||||||||||||||||
971 | // just wait until the user continues | - | ||||||||||||||||||
972 | return; executed 20 times by 1 test: return; Executed by:
| 20 | ||||||||||||||||||
973 | } else { | - | ||||||||||||||||||
974 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
975 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: encryption not done yet"; | - | ||||||||||||||||||
976 | #endif | - | ||||||||||||||||||
977 | } executed 2186 times by 7 tests: end of block Executed by:
| 2186 | ||||||||||||||||||
978 | } | - | ||||||||||||||||||
979 | - | |||||||||||||||||||
980 | // If the request is small and the remote host closes the transmission | - | ||||||||||||||||||
981 | // after sending, there's a chance that startHandshake() will already | - | ||||||||||||||||||
982 | // have triggered a shutdown. | - | ||||||||||||||||||
983 | if (!ssl)
| 0-15494 | ||||||||||||||||||
984 | continue; never executed: continue; | 0 | ||||||||||||||||||
985 | - | |||||||||||||||||||
986 | // We always read everything from the SSL decryption buffers, even if | - | ||||||||||||||||||
987 | // we have a readBufferMaxSize. There's no point in leaving data there | - | ||||||||||||||||||
988 | // just so that readBuffer.size() == readBufferMaxSize. | - | ||||||||||||||||||
989 | int readBytes = 0; | - | ||||||||||||||||||
990 | data.resize(4096); | - | ||||||||||||||||||
991 | ::memset(data.data(), 0, data.size()); | - | ||||||||||||||||||
992 | do { | - | ||||||||||||||||||
993 | // Don't use SSL_pending(). It's very unreliable. | - | ||||||||||||||||||
994 | if ((readBytes = q_SSL_read(ssl, data.data(), data.size())) > 0) {
| 6076-15482 | ||||||||||||||||||
995 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
996 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: decrypted" << readBytes << "bytes"; | - | ||||||||||||||||||
997 | #endif | - | ||||||||||||||||||
998 | char *ptr = buffer.reserve(readBytes); | - | ||||||||||||||||||
999 | ::memcpy(ptr, data.data(), readBytes); | - | ||||||||||||||||||
1000 | - | |||||||||||||||||||
1001 | if (readyReadEmittedPointer)
| 38-6038 | ||||||||||||||||||
1002 | *readyReadEmittedPointer = true; executed 38 times by 3 tests: *readyReadEmittedPointer = true; Executed by:
| 38 | ||||||||||||||||||
1003 | emit q->readyRead(); | - | ||||||||||||||||||
1004 | transmitting = true; | - | ||||||||||||||||||
1005 | continue; executed 6076 times by 5 tests: continue; Executed by:
| 6076 | ||||||||||||||||||
1006 | } | - | ||||||||||||||||||
1007 | - | |||||||||||||||||||
1008 | // Error. | - | ||||||||||||||||||
1009 | switch (q_SSL_get_error(ssl, readBytes)) { | - | ||||||||||||||||||
1010 | case SSL_ERROR_WANT_READ: executed 15470 times by 7 tests: case 2: Executed by:
| 15470 | ||||||||||||||||||
1011 | case SSL_ERROR_WANT_WRITE: never executed: case 3: | 0 | ||||||||||||||||||
1012 | // Out of data. | - | ||||||||||||||||||
1013 | break; executed 15470 times by 7 tests: break; Executed by:
| 15470 | ||||||||||||||||||
1014 | case SSL_ERROR_ZERO_RETURN: executed 12 times by 2 tests: case 6: Executed by:
| 12 | ||||||||||||||||||
1015 | // The remote host closed the connection. | - | ||||||||||||||||||
1016 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
1017 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::transmit: remote disconnect"; | - | ||||||||||||||||||
1018 | #endif | - | ||||||||||||||||||
1019 | shutdown = true; // the other side shut down, make sure we do not send shutdown ourselves | - | ||||||||||||||||||
1020 | setErrorAndEmit(QAbstractSocket::RemoteHostClosedError, | - | ||||||||||||||||||
1021 | QSslSocket::tr("The TLS/SSL connection has been closed")); | - | ||||||||||||||||||
1022 | return; executed 12 times by 2 tests: return; Executed by:
| 12 | ||||||||||||||||||
1023 | case SSL_ERROR_SYSCALL: // some IO error never executed: case 5: | 0 | ||||||||||||||||||
1024 | case SSL_ERROR_SSL: // error in the SSL library never executed: case 1: | 0 | ||||||||||||||||||
1025 | // we do not know exactly what the error is, nor whether we can recover from it, | - | ||||||||||||||||||
1026 | // so just return to prevent an endless loop in the outer "while" statement | - | ||||||||||||||||||
1027 | setErrorAndEmit(QAbstractSocket::SslInternalError, | - | ||||||||||||||||||
1028 | QSslSocket::tr("Error while reading: %1").arg(getErrorsFromOpenSsl())); | - | ||||||||||||||||||
1029 | return; never executed: return; | 0 | ||||||||||||||||||
1030 | default: never executed: default: | 0 | ||||||||||||||||||
1031 | // SSL_ERROR_WANT_CONNECT, SSL_ERROR_WANT_ACCEPT: can only happen with a | - | ||||||||||||||||||
1032 | // BIO_s_connect() or BIO_s_accept(), which we do not call. | - | ||||||||||||||||||
1033 | // SSL_ERROR_WANT_X509_LOOKUP: can only happen with a | - | ||||||||||||||||||
1034 | // SSL_CTX_set_client_cert_cb(), which we do not call. | - | ||||||||||||||||||
1035 | // So this default case should never be triggered. | - | ||||||||||||||||||
1036 | setErrorAndEmit(QAbstractSocket::SslInternalError, | - | ||||||||||||||||||
1037 | QSslSocket::tr("Error while reading: %1").arg(getErrorsFromOpenSsl())); | - | ||||||||||||||||||
1038 | break; never executed: break; | 0 | ||||||||||||||||||
1039 | } | - | ||||||||||||||||||
1040 | } while (ssl && readBytes > 0);
| 12-21534 | ||||||||||||||||||
1041 | } while (ssl && transmitting); executed 15482 times by 7 tests: end of block Executed by:
| 12-15482 | ||||||||||||||||||
1042 | } executed 7589 times by 7 tests: end of block Executed by:
| 7589 | ||||||||||||||||||
1043 | - | |||||||||||||||||||
1044 | static QSslError _q_OpenSSL_to_QSslError(int errorCode, const QSslCertificate &cert) | - | ||||||||||||||||||
1045 | { | - | ||||||||||||||||||
1046 | QSslError error; | - | ||||||||||||||||||
1047 | switch (errorCode) { | - | ||||||||||||||||||
1048 | case X509_V_OK: never executed: case 0: | 0 | ||||||||||||||||||
1049 | // X509_V_OK is also reported if the peer had no certificate. | - | ||||||||||||||||||
1050 | break; never executed: break; | 0 | ||||||||||||||||||
1051 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT: never executed: case 2: | 0 | ||||||||||||||||||
1052 | error = QSslError(QSslError::UnableToGetIssuerCertificate, cert); break; never executed: break; | 0 | ||||||||||||||||||
1053 | case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE: never executed: case 4: | 0 | ||||||||||||||||||
1054 | error = QSslError(QSslError::UnableToDecryptCertificateSignature, cert); break; never executed: break; | 0 | ||||||||||||||||||
1055 | case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY: never executed: case 6: | 0 | ||||||||||||||||||
1056 | error = QSslError(QSslError::UnableToDecodeIssuerPublicKey, cert); break; never executed: break; | 0 | ||||||||||||||||||
1057 | case X509_V_ERR_CERT_SIGNATURE_FAILURE: never executed: case 7: | 0 | ||||||||||||||||||
1058 | error = QSslError(QSslError::CertificateSignatureFailed, cert); break; never executed: break; | 0 | ||||||||||||||||||
1059 | case X509_V_ERR_CERT_NOT_YET_VALID: never executed: case 9: | 0 | ||||||||||||||||||
1060 | error = QSslError(QSslError::CertificateNotYetValid, cert); break; never executed: break; | 0 | ||||||||||||||||||
1061 | case X509_V_ERR_CERT_HAS_EXPIRED: executed 4 times by 2 tests: case 10: Executed by:
| 4 | ||||||||||||||||||
1062 | error = QSslError(QSslError::CertificateExpired, cert); break; executed 4 times by 2 tests: break; Executed by:
| 4 | ||||||||||||||||||
1063 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: never executed: case 13: | 0 | ||||||||||||||||||
1064 | error = QSslError(QSslError::InvalidNotBeforeField, cert); break; never executed: break; | 0 | ||||||||||||||||||
1065 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: never executed: case 14: | 0 | ||||||||||||||||||
1066 | error = QSslError(QSslError::InvalidNotAfterField, cert); break; never executed: break; | 0 | ||||||||||||||||||
1067 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: executed 353 times by 6 tests: case 18: Executed by:
| 353 | ||||||||||||||||||
1068 | error = QSslError(QSslError::SelfSignedCertificate, cert); break; executed 353 times by 6 tests: break; Executed by:
| 353 | ||||||||||||||||||
1069 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: never executed: case 19: | 0 | ||||||||||||||||||
1070 | error = QSslError(QSslError::SelfSignedCertificateInChain, cert); break; never executed: break; | 0 | ||||||||||||||||||
1071 | case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY: executed 133 times by 4 tests: case 20: Executed by:
| 133 | ||||||||||||||||||
1072 | error = QSslError(QSslError::UnableToGetLocalIssuerCertificate, cert); break; executed 133 times by 4 tests: break; Executed by:
| 133 | ||||||||||||||||||
1073 | case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE: executed 107 times by 2 tests: case 21: Executed by:
| 107 | ||||||||||||||||||
1074 | error = QSslError(QSslError::UnableToVerifyFirstCertificate, cert); break; executed 107 times by 2 tests: break; Executed by:
| 107 | ||||||||||||||||||
1075 | case X509_V_ERR_CERT_REVOKED: never executed: case 23: | 0 | ||||||||||||||||||
1076 | error = QSslError(QSslError::CertificateRevoked, cert); break; never executed: break; | 0 | ||||||||||||||||||
1077 | case X509_V_ERR_INVALID_CA: executed 1 time by 1 test: case 24: Executed by:
| 1 | ||||||||||||||||||
1078 | error = QSslError(QSslError::InvalidCaCertificate, cert); break; executed 1 time by 1 test: break; Executed by:
| 1 | ||||||||||||||||||
1079 | case X509_V_ERR_PATH_LENGTH_EXCEEDED: never executed: case 25: | 0 | ||||||||||||||||||
1080 | error = QSslError(QSslError::PathLengthExceeded, cert); break; never executed: break; | 0 | ||||||||||||||||||
1081 | case X509_V_ERR_INVALID_PURPOSE: executed 6 times by 1 test: case 26: Executed by:
| 6 | ||||||||||||||||||
1082 | error = QSslError(QSslError::InvalidPurpose, cert); break; executed 6 times by 1 test: break; Executed by:
| 6 | ||||||||||||||||||
1083 | case X509_V_ERR_CERT_UNTRUSTED: executed 133 times by 4 tests: case 27: Executed by:
| 133 | ||||||||||||||||||
1084 | error = QSslError(QSslError::CertificateUntrusted, cert); break; executed 133 times by 4 tests: break; Executed by:
| 133 | ||||||||||||||||||
1085 | case X509_V_ERR_CERT_REJECTED: never executed: case 28: | 0 | ||||||||||||||||||
1086 | error = QSslError(QSslError::CertificateRejected, cert); break; never executed: break; | 0 | ||||||||||||||||||
1087 | default: never executed: default: | 0 | ||||||||||||||||||
1088 | error = QSslError(QSslError::UnspecifiedError, cert); break; never executed: break; | 0 | ||||||||||||||||||
1089 | } | - | ||||||||||||||||||
1090 | return error; executed 737 times by 8 tests: return error; Executed by:
| 737 | ||||||||||||||||||
1091 | } | - | ||||||||||||||||||
1092 | - | |||||||||||||||||||
1093 | bool QSslSocketBackendPrivate::startHandshake() | - | ||||||||||||||||||
1094 | { | - | ||||||||||||||||||
1095 | Q_Q(QSslSocket); | - | ||||||||||||||||||
1096 | - | |||||||||||||||||||
1097 | // Check if the connection has been established. Get all errors from the | - | ||||||||||||||||||
1098 | // verification stage. | - | ||||||||||||||||||
1099 | _q_sslErrorList()->mutex.lock(); | - | ||||||||||||||||||
1100 | _q_sslErrorList()->errors.clear(); | - | ||||||||||||||||||
1101 | int result = (mode == QSslSocket::SslClientMode) ? q_SSL_connect(ssl) : q_SSL_accept(ssl);
| 356-2745 | ||||||||||||||||||
1102 | - | |||||||||||||||||||
1103 | const QList<QPair<int, int> > &lastErrors = _q_sslErrorList()->errors; | - | ||||||||||||||||||
1104 | if (!lastErrors.isEmpty())
| 247-2854 | ||||||||||||||||||
1105 | storePeerCertificates(); executed 247 times by 7 tests: storePeerCertificates(); Executed by:
| 247 | ||||||||||||||||||
1106 | for (int i = 0; i < lastErrors.size(); ++i) {
| 371-3101 | ||||||||||||||||||
1107 | const QPair<int, int> ¤tError = lastErrors.at(i); | - | ||||||||||||||||||
1108 | emit q->peerVerifyError(_q_OpenSSL_to_QSslError(currentError.first, | - | ||||||||||||||||||
1109 | configuration.peerCertificateChain.value(currentError.second))); | - | ||||||||||||||||||
1110 | if (q->state() != QAbstractSocket::ConnectedState)
| 0-371 | ||||||||||||||||||
1111 | break; never executed: break; | 0 | ||||||||||||||||||
1112 | } executed 371 times by 7 tests: end of block Executed by:
| 371 | ||||||||||||||||||
1113 | - | |||||||||||||||||||
1114 | errorList << lastErrors; | - | ||||||||||||||||||
1115 | _q_sslErrorList()->mutex.unlock(); | - | ||||||||||||||||||
1116 | - | |||||||||||||||||||
1117 | // Connection aborted during handshake phase. | - | ||||||||||||||||||
1118 | if (q->state() != QAbstractSocket::ConnectedState)
| 0-3101 | ||||||||||||||||||
1119 | return false; never executed: return false; | 0 | ||||||||||||||||||
1120 | - | |||||||||||||||||||
1121 | // Check if we're encrypted or not. | - | ||||||||||||||||||
1122 | if (result <= 0) {
| 435-2666 | ||||||||||||||||||
1123 | switch (q_SSL_get_error(ssl, result)) { | - | ||||||||||||||||||
1124 | case SSL_ERROR_WANT_READ: executed 2657 times by 7 tests: case 2: Executed by:
| 2657 | ||||||||||||||||||
1125 | case SSL_ERROR_WANT_WRITE: never executed: case 3: | 0 | ||||||||||||||||||
1126 | // The handshake is not yet complete. | - | ||||||||||||||||||
1127 | break; executed 2657 times by 7 tests: break; Executed by:
| 2657 | ||||||||||||||||||
1128 | default: executed 9 times by 2 tests: default: Executed by:
| 9 | ||||||||||||||||||
1129 | QString errorString | - | ||||||||||||||||||
1130 | = QSslSocket::tr("Error during SSL handshake: %1").arg(getErrorsFromOpenSsl()); | - | ||||||||||||||||||
1131 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
1132 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::startHandshake: error!" << errorString; | - | ||||||||||||||||||
1133 | #endif | - | ||||||||||||||||||
1134 | setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError, errorString); | - | ||||||||||||||||||
1135 | q->abort(); | - | ||||||||||||||||||
1136 | } executed 9 times by 2 tests: end of block Executed by:
| 9 | ||||||||||||||||||
1137 | return false; executed 2666 times by 7 tests: return false; Executed by:
| 2666 | ||||||||||||||||||
1138 | } | - | ||||||||||||||||||
1139 | - | |||||||||||||||||||
1140 | // store peer certificate chain | - | ||||||||||||||||||
1141 | storePeerCertificates(); | - | ||||||||||||||||||
1142 | - | |||||||||||||||||||
1143 | // Start translating errors. | - | ||||||||||||||||||
1144 | QList<QSslError> errors; | - | ||||||||||||||||||
1145 | - | |||||||||||||||||||
1146 | // check the whole chain for blacklisting (including root, as we check for subjectInfo and issuer) | - | ||||||||||||||||||
1147 | foreach (const QSslCertificate &cert, configuration.peerCertificateChain) { | - | ||||||||||||||||||
1148 | if (QSslCertificatePrivate::isBlacklisted(cert)) {
| 1-416 | ||||||||||||||||||
1149 | QSslError error(QSslError::CertificateBlacklisted, cert); | - | ||||||||||||||||||
1150 | errors << error; | - | ||||||||||||||||||
1151 | emit q->peerVerifyError(error); | - | ||||||||||||||||||
1152 | if (q->state() != QAbstractSocket::ConnectedState)
| 0-1 | ||||||||||||||||||
1153 | return false; never executed: return false; | 0 | ||||||||||||||||||
1154 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
1155 | } executed 417 times by 7 tests: end of block Executed by:
| 417 | ||||||||||||||||||
1156 | - | |||||||||||||||||||
1157 | bool doVerifyPeer = configuration.peerVerifyMode == QSslSocket::VerifyPeer
| 6-429 | ||||||||||||||||||
1158 | || (configuration.peerVerifyMode == QSslSocket::AutoVerifyPeer
| 16-413 | ||||||||||||||||||
1159 | && mode == QSslSocket::SslClientMode);
| 37-376 | ||||||||||||||||||
1160 | - | |||||||||||||||||||
1161 | // Check the peer certificate itself. First try the subject's common name | - | ||||||||||||||||||
1162 | // (CN) as a wildcard, then try all alternate subject name DNS entries the | - | ||||||||||||||||||
1163 | // same way. | - | ||||||||||||||||||
1164 | if (!configuration.peerCertificate.isNull()) {
| 42-393 | ||||||||||||||||||
1165 | // but only if we're a client connecting to a server | - | ||||||||||||||||||
1166 | // if we're the server, don't check CN | - | ||||||||||||||||||
1167 | if (mode == QSslSocket::SslClientMode) {
| 12-381 | ||||||||||||||||||
1168 | QString peerName = (verificationPeerName.isEmpty () ? q->peerName() : verificationPeerName);
| 5-376 | ||||||||||||||||||
1169 | - | |||||||||||||||||||
1170 | if (!isMatchingHostname(configuration.peerCertificate, peerName)) {
| 60-321 | ||||||||||||||||||
1171 | // No matches in common names or alternate names. | - | ||||||||||||||||||
1172 | QSslError error(QSslError::HostNameMismatch, configuration.peerCertificate); | - | ||||||||||||||||||
1173 | errors << error; | - | ||||||||||||||||||
1174 | emit q->peerVerifyError(error); | - | ||||||||||||||||||
1175 | if (q->state() != QAbstractSocket::ConnectedState)
| 0-60 | ||||||||||||||||||
1176 | return false; never executed: return false; | 0 | ||||||||||||||||||
1177 | } executed 60 times by 2 tests: end of block Executed by:
| 60 | ||||||||||||||||||
1178 | } executed 381 times by 7 tests: end of block Executed by:
| 381 | ||||||||||||||||||
1179 | } else { executed 393 times by 7 tests: end of block Executed by:
| 393 | ||||||||||||||||||
1180 | // No peer certificate presented. Report as error if the socket | - | ||||||||||||||||||
1181 | // expected one. | - | ||||||||||||||||||
1182 | if (doVerifyPeer) {
| 1-41 | ||||||||||||||||||
1183 | QSslError error(QSslError::NoPeerCertificate); | - | ||||||||||||||||||
1184 | errors << error; | - | ||||||||||||||||||
1185 | emit q->peerVerifyError(error); | - | ||||||||||||||||||
1186 | if (q->state() != QAbstractSocket::ConnectedState)
| 0-1 | ||||||||||||||||||
1187 | return false; never executed: return false; | 0 | ||||||||||||||||||
1188 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
1189 | } executed 42 times by 2 tests: end of block Executed by:
| 42 | ||||||||||||||||||
1190 | - | |||||||||||||||||||
1191 | // Translate errors from the error list into QSslErrors. | - | ||||||||||||||||||
1192 | const int numErrors = errorList.size(); | - | ||||||||||||||||||
1193 | errors.reserve(errors.size() + numErrors); | - | ||||||||||||||||||
1194 | for (int i = 0; i < numErrors; ++i) {
| 362-435 | ||||||||||||||||||
1195 | const QPair<int, int> &errorAndDepth = errorList.at(i); | - | ||||||||||||||||||
1196 | int err = errorAndDepth.first; | - | ||||||||||||||||||
1197 | int depth = errorAndDepth.second; | - | ||||||||||||||||||
1198 | errors << _q_OpenSSL_to_QSslError(err, configuration.peerCertificateChain.value(depth)); | - | ||||||||||||||||||
1199 | } executed 362 times by 7 tests: end of block Executed by:
| 362 | ||||||||||||||||||
1200 | - | |||||||||||||||||||
1201 | if (!errors.isEmpty()) {
| 190-245 | ||||||||||||||||||
1202 | sslErrors = errors; | - | ||||||||||||||||||
1203 | - | |||||||||||||||||||
1204 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
1205 | //Skip this if not using system CAs, or if the SSL errors are configured in advance to be ignorable | - | ||||||||||||||||||
1206 | if (doVerifyPeer | - | ||||||||||||||||||
1207 | && s_loadRootCertsOnDemand | - | ||||||||||||||||||
1208 | && allowRootCertOnDemandLoading | - | ||||||||||||||||||
1209 | && !verifyErrorsHaveBeenIgnored()) { | - | ||||||||||||||||||
1210 | //Windows desktop versions starting from vista ship with minimal set of roots | - | ||||||||||||||||||
1211 | //and download on demand from the windows update server CA roots that are | - | ||||||||||||||||||
1212 | //trusted by MS. | - | ||||||||||||||||||
1213 | //However, this is only transparent if using WinINET - we have to trigger it | - | ||||||||||||||||||
1214 | //ourselves. | - | ||||||||||||||||||
1215 | QSslCertificate certToFetch; | - | ||||||||||||||||||
1216 | bool fetchCertificate = true; | - | ||||||||||||||||||
1217 | for (int i=0; i< sslErrors.count(); i++) { | - | ||||||||||||||||||
1218 | switch (sslErrors.at(i).error()) { | - | ||||||||||||||||||
1219 | case QSslError::UnableToGetLocalIssuerCertificate: // site presented intermediate cert, but root is unknown | - | ||||||||||||||||||
1220 | case QSslError::SelfSignedCertificateInChain: // site presented a complete chain, but root is unknown | - | ||||||||||||||||||
1221 | certToFetch = sslErrors.at(i).certificate(); | - | ||||||||||||||||||
1222 | break; | - | ||||||||||||||||||
1223 | case QSslError::SelfSignedCertificate: | - | ||||||||||||||||||
1224 | case QSslError::CertificateBlacklisted: | - | ||||||||||||||||||
1225 | //With these errors, we know it will be untrusted so save time by not asking windows | - | ||||||||||||||||||
1226 | fetchCertificate = false; | - | ||||||||||||||||||
1227 | break; | - | ||||||||||||||||||
1228 | default: | - | ||||||||||||||||||
1229 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
1230 | qCDebug(lcSsl) << sslErrors.at(i).errorString(); | - | ||||||||||||||||||
1231 | #endif | - | ||||||||||||||||||
1232 | break; | - | ||||||||||||||||||
1233 | } | - | ||||||||||||||||||
1234 | } | - | ||||||||||||||||||
1235 | if (fetchCertificate && !certToFetch.isNull()) { | - | ||||||||||||||||||
1236 | fetchCaRootForCert(certToFetch); | - | ||||||||||||||||||
1237 | return false; | - | ||||||||||||||||||
1238 | } | - | ||||||||||||||||||
1239 | } | - | ||||||||||||||||||
1240 | #endif | - | ||||||||||||||||||
1241 | if (!checkSslErrors())
| 94-151 | ||||||||||||||||||
1242 | return false; executed 94 times by 6 tests: return false; Executed by:
| 94 | ||||||||||||||||||
1243 | // A slot, attached to sslErrors signal can call | - | ||||||||||||||||||
1244 | // abort/close/disconnetFromHost/etc; no need to | - | ||||||||||||||||||
1245 | // continue handshake then. | - | ||||||||||||||||||
1246 | if (q->state() != QAbstractSocket::ConnectedState)
| 1-150 | ||||||||||||||||||
1247 | return false; executed 1 time by 1 test: return false; Executed by:
| 1 | ||||||||||||||||||
1248 | } else { executed 150 times by 5 tests: end of block Executed by:
| 150 | ||||||||||||||||||
1249 | sslErrors.clear(); | - | ||||||||||||||||||
1250 | } executed 190 times by 4 tests: end of block Executed by:
| 190 | ||||||||||||||||||
1251 | - | |||||||||||||||||||
1252 | continueHandshake(); | - | ||||||||||||||||||
1253 | return true; executed 340 times by 7 tests: return true; Executed by:
| 340 | ||||||||||||||||||
1254 | } | - | ||||||||||||||||||
1255 | - | |||||||||||||||||||
1256 | void QSslSocketBackendPrivate::storePeerCertificates() | - | ||||||||||||||||||
1257 | { | - | ||||||||||||||||||
1258 | // Store the peer certificate and chain. For clients, the peer certificate | - | ||||||||||||||||||
1259 | // chain includes the peer certificate; for servers, it doesn't. Both the | - | ||||||||||||||||||
1260 | // peer certificate and the chain may be empty if the peer didn't present | - | ||||||||||||||||||
1261 | // any certificate. | - | ||||||||||||||||||
1262 | X509 *x509 = q_SSL_get_peer_certificate(ssl); | - | ||||||||||||||||||
1263 | configuration.peerCertificate = QSslCertificatePrivate::QSslCertificate_from_X509(x509); | - | ||||||||||||||||||
1264 | q_X509_free(x509); | - | ||||||||||||||||||
1265 | if (configuration.peerCertificateChain.isEmpty()) {
| 244-438 | ||||||||||||||||||
1266 | configuration.peerCertificateChain = STACKOFX509_to_QSslCertificates(q_SSL_get_peer_cert_chain(ssl)); | - | ||||||||||||||||||
1267 | if (!configuration.peerCertificate.isNull() && mode == QSslSocket::SslServerMode)
| 12-396 | ||||||||||||||||||
1268 | configuration.peerCertificateChain.prepend(configuration.peerCertificate); executed 12 times by 1 test: configuration.peerCertificateChain.prepend(configuration.peerCertificate); Executed by:
| 12 | ||||||||||||||||||
1269 | } executed 438 times by 7 tests: end of block Executed by:
| 438 | ||||||||||||||||||
1270 | } executed 682 times by 7 tests: end of block Executed by:
| 682 | ||||||||||||||||||
1271 | - | |||||||||||||||||||
1272 | bool QSslSocketBackendPrivate::checkSslErrors() | - | ||||||||||||||||||
1273 | { | - | ||||||||||||||||||
1274 | Q_Q(QSslSocket); | - | ||||||||||||||||||
1275 | if (sslErrors.isEmpty())
| 0-245 | ||||||||||||||||||
1276 | return true; never executed: return true; | 0 | ||||||||||||||||||
1277 | - | |||||||||||||||||||
1278 | emit q->sslErrors(sslErrors); | - | ||||||||||||||||||
1279 | - | |||||||||||||||||||
1280 | bool doVerifyPeer = configuration.peerVerifyMode == QSslSocket::VerifyPeer
| 4-241 | ||||||||||||||||||
1281 | || (configuration.peerVerifyMode == QSslSocket::AutoVerifyPeer
| 2-239 | ||||||||||||||||||
1282 | && mode == QSslSocket::SslClientMode);
| 2-237 | ||||||||||||||||||
1283 | bool doEmitSslError = !verifyErrorsHaveBeenIgnored(); | - | ||||||||||||||||||
1284 | // check whether we need to emit an SSL handshake error | - | ||||||||||||||||||
1285 | if (doVerifyPeer && doEmitSslError) {
| 4-241 | ||||||||||||||||||
1286 | if (q->pauseMode() & QAbstractSocket::PauseOnSslErrors) {
| 20-74 | ||||||||||||||||||
1287 | pauseSocketNotifiers(q); | - | ||||||||||||||||||
1288 | paused = true; | - | ||||||||||||||||||
1289 | } else { executed 20 times by 1 test: end of block Executed by:
| 20 | ||||||||||||||||||
1290 | setErrorAndEmit(QAbstractSocket::SslHandshakeFailedError, sslErrors.first().errorString()); | - | ||||||||||||||||||
1291 | plainSocket->disconnectFromHost(); | - | ||||||||||||||||||
1292 | } executed 74 times by 6 tests: end of block Executed by:
| 74 | ||||||||||||||||||
1293 | return false; executed 94 times by 6 tests: return false; Executed by:
| 94 | ||||||||||||||||||
1294 | } | - | ||||||||||||||||||
1295 | return true; executed 151 times by 5 tests: return true; Executed by:
| 151 | ||||||||||||||||||
1296 | } | - | ||||||||||||||||||
1297 | - | |||||||||||||||||||
1298 | unsigned int QSslSocketBackendPrivate::tlsPskClientCallback(const char *hint, | - | ||||||||||||||||||
1299 | char *identity, unsigned int max_identity_len, | - | ||||||||||||||||||
1300 | unsigned char *psk, unsigned int max_psk_len) | - | ||||||||||||||||||
1301 | { | - | ||||||||||||||||||
1302 | QSslPreSharedKeyAuthenticator authenticator; | - | ||||||||||||||||||
1303 | - | |||||||||||||||||||
1304 | // Fill in some read-only fields (for the user) | - | ||||||||||||||||||
1305 | if (hint)
| 0 | ||||||||||||||||||
1306 | authenticator.d->identityHint = QByteArray::fromRawData(hint, int(::strlen(hint))); // it's NUL terminated, but do not include the NUL never executed: authenticator.d->identityHint = QByteArray::fromRawData(hint, int(::strlen(hint))); | 0 | ||||||||||||||||||
1307 | - | |||||||||||||||||||
1308 | authenticator.d->maximumIdentityLength = int(max_identity_len) - 1; // needs to be NUL terminated | - | ||||||||||||||||||
1309 | authenticator.d->maximumPreSharedKeyLength = int(max_psk_len); | - | ||||||||||||||||||
1310 | - | |||||||||||||||||||
1311 | // Let the client provide the remaining bits... | - | ||||||||||||||||||
1312 | Q_Q(QSslSocket); | - | ||||||||||||||||||
1313 | emit q->preSharedKeyAuthenticationRequired(&authenticator); | - | ||||||||||||||||||
1314 | - | |||||||||||||||||||
1315 | // No PSK set? Return now to make the handshake fail | - | ||||||||||||||||||
1316 | if (authenticator.preSharedKey().isEmpty())
| 0 | ||||||||||||||||||
1317 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
1318 | - | |||||||||||||||||||
1319 | // Copy data back into OpenSSL | - | ||||||||||||||||||
1320 | const int identityLength = qMin(authenticator.identity().length(), authenticator.maximumIdentityLength()); | - | ||||||||||||||||||
1321 | ::memcpy(identity, authenticator.identity().constData(), identityLength); | - | ||||||||||||||||||
1322 | identity[identityLength] = 0; | - | ||||||||||||||||||
1323 | - | |||||||||||||||||||
1324 | const int pskLength = qMin(authenticator.preSharedKey().length(), authenticator.maximumPreSharedKeyLength()); | - | ||||||||||||||||||
1325 | ::memcpy(psk, authenticator.preSharedKey().constData(), pskLength); | - | ||||||||||||||||||
1326 | return pskLength; never executed: return pskLength; | 0 | ||||||||||||||||||
1327 | } | - | ||||||||||||||||||
1328 | - | |||||||||||||||||||
1329 | #ifdef Q_OS_WIN | - | ||||||||||||||||||
1330 | - | |||||||||||||||||||
1331 | void QSslSocketBackendPrivate::fetchCaRootForCert(const QSslCertificate &cert) | - | ||||||||||||||||||
1332 | { | - | ||||||||||||||||||
1333 | Q_Q(QSslSocket); | - | ||||||||||||||||||
1334 | //The root certificate is downloaded from windows update, which blocks for 15 seconds in the worst case | - | ||||||||||||||||||
1335 | //so the request is done in a worker thread. | - | ||||||||||||||||||
1336 | QWindowsCaRootFetcher *fetcher = new QWindowsCaRootFetcher(cert, mode); | - | ||||||||||||||||||
1337 | QObject::connect(fetcher, SIGNAL(finished(QSslCertificate,QSslCertificate)), q, SLOT(_q_caRootLoaded(QSslCertificate,QSslCertificate)), Qt::QueuedConnection); | - | ||||||||||||||||||
1338 | QMetaObject::invokeMethod(fetcher, "start", Qt::QueuedConnection); | - | ||||||||||||||||||
1339 | pauseSocketNotifiers(q); | - | ||||||||||||||||||
1340 | paused = true; | - | ||||||||||||||||||
1341 | } | - | ||||||||||||||||||
1342 | - | |||||||||||||||||||
1343 | //This is the callback from QWindowsCaRootFetcher, trustedRoot will be invalid (default constructed) if it failed. | - | ||||||||||||||||||
1344 | void QSslSocketBackendPrivate::_q_caRootLoaded(QSslCertificate cert, QSslCertificate trustedRoot) | - | ||||||||||||||||||
1345 | { | - | ||||||||||||||||||
1346 | Q_Q(QSslSocket); | - | ||||||||||||||||||
1347 | if (!trustedRoot.isNull() && !trustedRoot.isBlacklisted()) { | - | ||||||||||||||||||
1348 | if (s_loadRootCertsOnDemand) { | - | ||||||||||||||||||
1349 | //Add the new root cert to default cert list for use by future sockets | - | ||||||||||||||||||
1350 | QSslSocket::addDefaultCaCertificate(trustedRoot); | - | ||||||||||||||||||
1351 | } | - | ||||||||||||||||||
1352 | //Add the new root cert to this socket for future connections | - | ||||||||||||||||||
1353 | q->addCaCertificate(trustedRoot); | - | ||||||||||||||||||
1354 | //Remove the broken chain ssl errors (as chain is verified by windows) | - | ||||||||||||||||||
1355 | for (int i=sslErrors.count() - 1; i >= 0; --i) { | - | ||||||||||||||||||
1356 | if (sslErrors.at(i).certificate() == cert) { | - | ||||||||||||||||||
1357 | switch (sslErrors.at(i).error()) { | - | ||||||||||||||||||
1358 | case QSslError::UnableToGetLocalIssuerCertificate: | - | ||||||||||||||||||
1359 | case QSslError::CertificateUntrusted: | - | ||||||||||||||||||
1360 | case QSslError::UnableToVerifyFirstCertificate: | - | ||||||||||||||||||
1361 | case QSslError::SelfSignedCertificateInChain: | - | ||||||||||||||||||
1362 | // error can be ignored if OS says the chain is trusted | - | ||||||||||||||||||
1363 | sslErrors.removeAt(i); | - | ||||||||||||||||||
1364 | break; | - | ||||||||||||||||||
1365 | default: | - | ||||||||||||||||||
1366 | // error cannot be ignored | - | ||||||||||||||||||
1367 | break; | - | ||||||||||||||||||
1368 | } | - | ||||||||||||||||||
1369 | } | - | ||||||||||||||||||
1370 | } | - | ||||||||||||||||||
1371 | } | - | ||||||||||||||||||
1372 | // Continue with remaining errors | - | ||||||||||||||||||
1373 | if (plainSocket) | - | ||||||||||||||||||
1374 | plainSocket->resume(); | - | ||||||||||||||||||
1375 | paused = false; | - | ||||||||||||||||||
1376 | if (checkSslErrors() && ssl) { | - | ||||||||||||||||||
1377 | bool willClose = (autoStartHandshake && pendingClose); | - | ||||||||||||||||||
1378 | continueHandshake(); | - | ||||||||||||||||||
1379 | if (!willClose) | - | ||||||||||||||||||
1380 | transmit(); | - | ||||||||||||||||||
1381 | } | - | ||||||||||||||||||
1382 | } | - | ||||||||||||||||||
1383 | - | |||||||||||||||||||
1384 | class QWindowsCaRootFetcherThread : public QThread | - | ||||||||||||||||||
1385 | { | - | ||||||||||||||||||
1386 | public: | - | ||||||||||||||||||
1387 | QWindowsCaRootFetcherThread() | - | ||||||||||||||||||
1388 | { | - | ||||||||||||||||||
1389 | qRegisterMetaType<QSslCertificate>(); | - | ||||||||||||||||||
1390 | setObjectName(QStringLiteral("QWindowsCaRootFetcher")); | - | ||||||||||||||||||
1391 | start(); | - | ||||||||||||||||||
1392 | } | - | ||||||||||||||||||
1393 | ~QWindowsCaRootFetcherThread() | - | ||||||||||||||||||
1394 | { | - | ||||||||||||||||||
1395 | quit(); | - | ||||||||||||||||||
1396 | wait(15500); // worst case, a running request can block for 15 seconds | - | ||||||||||||||||||
1397 | } | - | ||||||||||||||||||
1398 | }; | - | ||||||||||||||||||
1399 | - | |||||||||||||||||||
1400 | Q_GLOBAL_STATIC(QWindowsCaRootFetcherThread, windowsCaRootFetcherThread); | - | ||||||||||||||||||
1401 | - | |||||||||||||||||||
1402 | QWindowsCaRootFetcher::QWindowsCaRootFetcher(const QSslCertificate &certificate, QSslSocket::SslMode sslMode) | - | ||||||||||||||||||
1403 | : cert(certificate), mode(sslMode) | - | ||||||||||||||||||
1404 | { | - | ||||||||||||||||||
1405 | moveToThread(windowsCaRootFetcherThread()); | - | ||||||||||||||||||
1406 | } | - | ||||||||||||||||||
1407 | - | |||||||||||||||||||
1408 | QWindowsCaRootFetcher::~QWindowsCaRootFetcher() | - | ||||||||||||||||||
1409 | { | - | ||||||||||||||||||
1410 | } | - | ||||||||||||||||||
1411 | - | |||||||||||||||||||
1412 | void QWindowsCaRootFetcher::start() | - | ||||||||||||||||||
1413 | { | - | ||||||||||||||||||
1414 | QByteArray der = cert.toDer(); | - | ||||||||||||||||||
1415 | PCCERT_CONTEXT wincert = CertCreateCertificateContext(X509_ASN_ENCODING, (const BYTE *)der.constData(), der.length()); | - | ||||||||||||||||||
1416 | if (!wincert) { | - | ||||||||||||||||||
1417 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
1418 | qCDebug(lcSsl, "QWindowsCaRootFetcher failed to convert certificate to windows form"); | - | ||||||||||||||||||
1419 | #endif | - | ||||||||||||||||||
1420 | emit finished(cert, QSslCertificate()); | - | ||||||||||||||||||
1421 | deleteLater(); | - | ||||||||||||||||||
1422 | return; | - | ||||||||||||||||||
1423 | } | - | ||||||||||||||||||
1424 | - | |||||||||||||||||||
1425 | CERT_CHAIN_PARA parameters; | - | ||||||||||||||||||
1426 | memset(¶meters, 0, sizeof(parameters)); | - | ||||||||||||||||||
1427 | parameters.cbSize = sizeof(parameters); | - | ||||||||||||||||||
1428 | // set key usage constraint | - | ||||||||||||||||||
1429 | parameters.RequestedUsage.dwType = USAGE_MATCH_TYPE_AND; | - | ||||||||||||||||||
1430 | parameters.RequestedUsage.Usage.cUsageIdentifier = 1; | - | ||||||||||||||||||
1431 | LPSTR oid = (LPSTR)(mode == QSslSocket::SslClientMode ? szOID_PKIX_KP_SERVER_AUTH : szOID_PKIX_KP_CLIENT_AUTH); | - | ||||||||||||||||||
1432 | parameters.RequestedUsage.Usage.rgpszUsageIdentifier = &oid; | - | ||||||||||||||||||
1433 | - | |||||||||||||||||||
1434 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
1435 | QElapsedTimer stopwatch; | - | ||||||||||||||||||
1436 | stopwatch.start(); | - | ||||||||||||||||||
1437 | #endif | - | ||||||||||||||||||
1438 | PCCERT_CHAIN_CONTEXT chain; | - | ||||||||||||||||||
1439 | BOOL result = CertGetCertificateChain( | - | ||||||||||||||||||
1440 | 0, //default engine | - | ||||||||||||||||||
1441 | wincert, | - | ||||||||||||||||||
1442 | 0, //current date/time | - | ||||||||||||||||||
1443 | 0, //default store | - | ||||||||||||||||||
1444 | ¶meters, | - | ||||||||||||||||||
1445 | 0, //default dwFlags | - | ||||||||||||||||||
1446 | 0, //reserved | - | ||||||||||||||||||
1447 | &chain); | - | ||||||||||||||||||
1448 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
1449 | qCDebug(lcSsl) << "QWindowsCaRootFetcher" << stopwatch.elapsed() << "ms to get chain"; | - | ||||||||||||||||||
1450 | #endif | - | ||||||||||||||||||
1451 | - | |||||||||||||||||||
1452 | QSslCertificate trustedRoot; | - | ||||||||||||||||||
1453 | if (result) { | - | ||||||||||||||||||
1454 | #ifdef QSSLSOCKET_DEBUG | - | ||||||||||||||||||
1455 | qCDebug(lcSsl) << "QWindowsCaRootFetcher - examining windows chains"; | - | ||||||||||||||||||
1456 | if (chain->TrustStatus.dwErrorStatus == CERT_TRUST_NO_ERROR) | - | ||||||||||||||||||
1457 | qCDebug(lcSsl) << " - TRUSTED"; | - | ||||||||||||||||||
1458 | else | - | ||||||||||||||||||
1459 | qCDebug(lcSsl) << " - NOT TRUSTED" << chain->TrustStatus.dwErrorStatus; | - | ||||||||||||||||||
1460 | if (chain->TrustStatus.dwInfoStatus & CERT_TRUST_IS_SELF_SIGNED) | - | ||||||||||||||||||
1461 | qCDebug(lcSsl) << " - SELF SIGNED"; | - | ||||||||||||||||||
1462 | qCDebug(lcSsl) << "QSslSocketBackendPrivate::fetchCaRootForCert - dumping simple chains"; | - | ||||||||||||||||||
1463 | for (unsigned int i = 0; i < chain->cChain; i++) { | - | ||||||||||||||||||
1464 | if (chain->rgpChain[i]->TrustStatus.dwErrorStatus == CERT_TRUST_NO_ERROR) | - | ||||||||||||||||||
1465 | qCDebug(lcSsl) << " - TRUSTED SIMPLE CHAIN" << i; | - | ||||||||||||||||||
1466 | else | - | ||||||||||||||||||
1467 | qCDebug(lcSsl) << " - UNTRUSTED SIMPLE CHAIN" << i << "reason:" << chain->rgpChain[i]->TrustStatus.dwErrorStatus; | - | ||||||||||||||||||
1468 | for (unsigned int j = 0; j < chain->rgpChain[i]->cElement; j++) { | - | ||||||||||||||||||
1469 | QSslCertificate foundCert(QByteArray((const char *)chain->rgpChain[i]->rgpElement[j]->pCertContext->pbCertEncoded | - | ||||||||||||||||||
1470 | , chain->rgpChain[i]->rgpElement[j]->pCertContext->cbCertEncoded), QSsl::Der); | - | ||||||||||||||||||
1471 | qCDebug(lcSsl) << " - " << foundCert; | - | ||||||||||||||||||
1472 | } | - | ||||||||||||||||||
1473 | } | - | ||||||||||||||||||
1474 | qCDebug(lcSsl) << " - and" << chain->cLowerQualityChainContext << "low quality chains"; //expect 0, we haven't asked for them | - | ||||||||||||||||||
1475 | #endif | - | ||||||||||||||||||
1476 | - | |||||||||||||||||||
1477 | //based on http://msdn.microsoft.com/en-us/library/windows/desktop/aa377182%28v=vs.85%29.aspx | - | ||||||||||||||||||
1478 | //about the final chain rgpChain[cChain-1] which must begin with a trusted root to be valid | - | ||||||||||||||||||
1479 | if (chain->TrustStatus.dwErrorStatus == CERT_TRUST_NO_ERROR | - | ||||||||||||||||||
1480 | && chain->cChain > 0) { | - | ||||||||||||||||||
1481 | const PCERT_SIMPLE_CHAIN finalChain = chain->rgpChain[chain->cChain - 1]; | - | ||||||||||||||||||
1482 | // http://msdn.microsoft.com/en-us/library/windows/desktop/aa377544%28v=vs.85%29.aspx | - | ||||||||||||||||||
1483 | // rgpElement[0] is the end certificate chain element. rgpElement[cElement-1] is the self-signed "root" certificate element. | - | ||||||||||||||||||
1484 | if (finalChain->TrustStatus.dwErrorStatus == CERT_TRUST_NO_ERROR | - | ||||||||||||||||||
1485 | && finalChain->cElement > 0) { | - | ||||||||||||||||||
1486 | trustedRoot = QSslCertificate(QByteArray((const char *)finalChain->rgpElement[finalChain->cElement - 1]->pCertContext->pbCertEncoded | - | ||||||||||||||||||
1487 | , finalChain->rgpElement[finalChain->cElement - 1]->pCertContext->cbCertEncoded), QSsl::Der); | - | ||||||||||||||||||
1488 | } | - | ||||||||||||||||||
1489 | } | - | ||||||||||||||||||
1490 | CertFreeCertificateChain(chain); | - | ||||||||||||||||||
1491 | } | - | ||||||||||||||||||
1492 | CertFreeCertificateContext(wincert); | - | ||||||||||||||||||
1493 | - | |||||||||||||||||||
1494 | emit finished(cert, trustedRoot); | - | ||||||||||||||||||
1495 | deleteLater(); | - | ||||||||||||||||||
1496 | } | - | ||||||||||||||||||
1497 | #endif | - | ||||||||||||||||||
1498 | - | |||||||||||||||||||
1499 | void QSslSocketBackendPrivate::disconnectFromHost() | - | ||||||||||||||||||
1500 | { | - | ||||||||||||||||||
1501 | if (ssl) {
| 0-100 | ||||||||||||||||||
1502 | if (!shutdown) {
| 47-53 | ||||||||||||||||||
1503 | q_SSL_shutdown(ssl); | - | ||||||||||||||||||
1504 | shutdown = true; | - | ||||||||||||||||||
1505 | transmit(); | - | ||||||||||||||||||
1506 | } executed 53 times by 1 test: end of block Executed by:
| 53 | ||||||||||||||||||
1507 | } executed 100 times by 1 test: end of block Executed by:
| 100 | ||||||||||||||||||
1508 | plainSocket->disconnectFromHost(); | - | ||||||||||||||||||
1509 | } executed 100 times by 1 test: end of block Executed by:
| 100 | ||||||||||||||||||
1510 | - | |||||||||||||||||||
1511 | void QSslSocketBackendPrivate::disconnected() | - | ||||||||||||||||||
1512 | { | - | ||||||||||||||||||
1513 | if (plainSocket->bytesAvailable() <= 0)
| 21-679 | ||||||||||||||||||
1514 | destroySslContext(); executed 679 times by 9 tests: destroySslContext(); Executed by:
| 679 | ||||||||||||||||||
1515 | else { | - | ||||||||||||||||||
1516 | // Move all bytes into the plain buffer | - | ||||||||||||||||||
1517 | qint64 tmpReadBufferMaxSize = readBufferMaxSize; | - | ||||||||||||||||||
1518 | readBufferMaxSize = 0; // reset temporarily so the plain socket buffer is completely drained | - | ||||||||||||||||||
1519 | transmit(); | - | ||||||||||||||||||
1520 | readBufferMaxSize = tmpReadBufferMaxSize; | - | ||||||||||||||||||
1521 | } executed 21 times by 3 tests: end of block Executed by:
| 21 | ||||||||||||||||||
1522 | //if there is still buffered data in the plain socket, don't destroy the ssl context yet. | - | ||||||||||||||||||
1523 | //it will be destroyed when the socket is deleted. | - | ||||||||||||||||||
1524 | } | - | ||||||||||||||||||
1525 | - | |||||||||||||||||||
1526 | QSslCipher QSslSocketBackendPrivate::sessionCipher() const | - | ||||||||||||||||||
1527 | { | - | ||||||||||||||||||
1528 | if (!ssl)
| 29-624 | ||||||||||||||||||
1529 | return QSslCipher(); executed 29 times by 3 tests: return QSslCipher(); Executed by:
| 29 | ||||||||||||||||||
1530 | #if OPENSSL_VERSION_NUMBER >= 0x10000000L | - | ||||||||||||||||||
1531 | // FIXME This is fairly evil, but needed to keep source level compatibility | - | ||||||||||||||||||
1532 | // with the OpenSSL 0.9.x implementation at maximum -- some other functions | - | ||||||||||||||||||
1533 | // don't take a const SSL_CIPHER* when they should | - | ||||||||||||||||||
1534 | SSL_CIPHER *sessionCipher = const_cast<SSL_CIPHER *>(q_SSL_get_current_cipher(ssl)); | - | ||||||||||||||||||
1535 | #else | - | ||||||||||||||||||
1536 | SSL_CIPHER *sessionCipher = q_SSL_get_current_cipher(ssl); | - | ||||||||||||||||||
1537 | #endif | - | ||||||||||||||||||
1538 | return sessionCipher ? QSslCipher_from_SSL_CIPHER(sessionCipher) : QSslCipher(); executed 624 times by 4 tests: return sessionCipher ? QSslCipher_from_SSL_CIPHER(sessionCipher) : QSslCipher(); Executed by:
| 2-624 | ||||||||||||||||||
1539 | } | - | ||||||||||||||||||
1540 | - | |||||||||||||||||||
1541 | QSsl::SslProtocol QSslSocketBackendPrivate::sessionProtocol() const | - | ||||||||||||||||||
1542 | { | - | ||||||||||||||||||
1543 | if (!ssl)
| 19-607 | ||||||||||||||||||
1544 | return QSsl::UnknownProtocol; executed 19 times by 3 tests: return QSsl::UnknownProtocol; Executed by:
| 19 | ||||||||||||||||||
1545 | int ver = q_SSL_version(ssl); | - | ||||||||||||||||||
1546 | - | |||||||||||||||||||
1547 | switch (ver) { | - | ||||||||||||||||||
1548 | case 0x2: never executed: case 0x2: | 0 | ||||||||||||||||||
1549 | return QSsl::SslV2; never executed: return QSsl::SslV2; | 0 | ||||||||||||||||||
1550 | case 0x300: never executed: case 0x300: | 0 | ||||||||||||||||||
1551 | return QSsl::SslV3; never executed: return QSsl::SslV3; | 0 | ||||||||||||||||||
1552 | case 0x301: never executed: case 0x301: | 0 | ||||||||||||||||||
1553 | return QSsl::TlsV1_0; never executed: return QSsl::TlsV1_0; | 0 | ||||||||||||||||||
1554 | case 0x302: never executed: case 0x302: | 0 | ||||||||||||||||||
1555 | return QSsl::TlsV1_1; never executed: return QSsl::TlsV1_1; | 0 | ||||||||||||||||||
1556 | case 0x303: executed 607 times by 3 tests: case 0x303: Executed by:
| 607 | ||||||||||||||||||
1557 | return QSsl::TlsV1_2; executed 607 times by 3 tests: return QSsl::TlsV1_2; Executed by:
| 607 | ||||||||||||||||||
1558 | } | - | ||||||||||||||||||
1559 | - | |||||||||||||||||||
1560 | return QSsl::UnknownProtocol; never executed: return QSsl::UnknownProtocol; | 0 | ||||||||||||||||||
1561 | } | - | ||||||||||||||||||
1562 | - | |||||||||||||||||||
1563 | void QSslSocketBackendPrivate::continueHandshake() | - | ||||||||||||||||||
1564 | { | - | ||||||||||||||||||
1565 | Q_Q(QSslSocket); | - | ||||||||||||||||||
1566 | // if we have a max read buffer size, reset the plain socket's to match | - | ||||||||||||||||||
1567 | if (readBufferMaxSize)
| 100-250 | ||||||||||||||||||
1568 | plainSocket->setReadBufferSize(readBufferMaxSize); executed 100 times by 3 tests: plainSocket->setReadBufferSize(readBufferMaxSize); Executed by:
| 100 | ||||||||||||||||||
1569 | - | |||||||||||||||||||
1570 | if (q_SSL_ctrl((ssl), SSL_CTRL_GET_SESSION_REUSED, 0, NULL))
| 16-334 | ||||||||||||||||||
1571 | configuration.peerSessionShared = true; executed 16 times by 1 test: configuration.peerSessionShared = true; Executed by:
| 16 | ||||||||||||||||||
1572 | - | |||||||||||||||||||
1573 | #ifdef QT_DECRYPT_SSL_TRAFFIC | - | ||||||||||||||||||
1574 | if (ssl->session && ssl->s3) { | - | ||||||||||||||||||
1575 | const char *mk = reinterpret_cast<const char *>(ssl->session->master_key); | - | ||||||||||||||||||
1576 | QByteArray masterKey(mk, ssl->session->master_key_length); | - | ||||||||||||||||||
1577 | const char *random = reinterpret_cast<const char *>(ssl->s3->client_random); | - | ||||||||||||||||||
1578 | QByteArray clientRandom(random, SSL3_RANDOM_SIZE); | - | ||||||||||||||||||
1579 | - | |||||||||||||||||||
1580 | // different format, needed for e.g. older Wireshark versions: | - | ||||||||||||||||||
1581 | // const char *sid = reinterpret_cast<const char *>(ssl->session->session_id); | - | ||||||||||||||||||
1582 | // QByteArray sessionID(sid, ssl->session->session_id_length); | - | ||||||||||||||||||
1583 | // QByteArray debugLineRSA("RSA Session-ID:"); | - | ||||||||||||||||||
1584 | // debugLineRSA.append(sessionID.toHex().toUpper()); | - | ||||||||||||||||||
1585 | // debugLineRSA.append(" Master-Key:"); | - | ||||||||||||||||||
1586 | // debugLineRSA.append(masterKey.toHex().toUpper()); | - | ||||||||||||||||||
1587 | // debugLineRSA.append("\n"); | - | ||||||||||||||||||
1588 | - | |||||||||||||||||||
1589 | QByteArray debugLineClientRandom("CLIENT_RANDOM "); | - | ||||||||||||||||||
1590 | debugLineClientRandom.append(clientRandom.toHex().toUpper()); | - | ||||||||||||||||||
1591 | debugLineClientRandom.append(" "); | - | ||||||||||||||||||
1592 | debugLineClientRandom.append(masterKey.toHex().toUpper()); | - | ||||||||||||||||||
1593 | debugLineClientRandom.append("\n"); | - | ||||||||||||||||||
1594 | - | |||||||||||||||||||
1595 | QString sslKeyFile = QDir::tempPath() + QLatin1String("/qt-ssl-keys"); | - | ||||||||||||||||||
1596 | QFile file(sslKeyFile); | - | ||||||||||||||||||
1597 | if (!file.open(QIODevice::Append)) | - | ||||||||||||||||||
1598 | qCWarning(lcSsl) << "could not open file" << sslKeyFile << "for appending"; | - | ||||||||||||||||||
1599 | if (!file.write(debugLineClientRandom)) | - | ||||||||||||||||||
1600 | qCWarning(lcSsl) << "could not write to file" << sslKeyFile; | - | ||||||||||||||||||
1601 | file.close(); | - | ||||||||||||||||||
1602 | } else { | - | ||||||||||||||||||
1603 | qCWarning(lcSsl, "could not decrypt SSL traffic"); | - | ||||||||||||||||||
1604 | } | - | ||||||||||||||||||
1605 | #endif | - | ||||||||||||||||||
1606 | - | |||||||||||||||||||
1607 | // Cache this SSL session inside the QSslContext | - | ||||||||||||||||||
1608 | if (!(configuration.sslOptions & QSsl::SslOptionDisableSessionSharing)) {
| 5-345 | ||||||||||||||||||
1609 | if (!sslContextPointer->cacheSession(ssl)) {
| 0-345 | ||||||||||||||||||
1610 | sslContextPointer.clear(); // we could not cache the session | - | ||||||||||||||||||
1611 | } else { never executed: end of block | 0 | ||||||||||||||||||
1612 | // Cache the session for permanent usage as well | - | ||||||||||||||||||
1613 | if (!(configuration.sslOptions & QSsl::SslOptionDisableSessionPersistence)) {
| 2-343 | ||||||||||||||||||
1614 | if (!sslContextPointer->sessionASN1().isEmpty())
| 0-2 | ||||||||||||||||||
1615 | configuration.sslSession = sslContextPointer->sessionASN1(); executed 2 times by 1 test: configuration.sslSession = sslContextPointer->sessionASN1(); Executed by:
| 2 | ||||||||||||||||||
1616 | configuration.sslSessionTicketLifeTimeHint = sslContextPointer->sessionTicketLifeTimeHint(); | - | ||||||||||||||||||
1617 | } executed 2 times by 1 test: end of block Executed by:
| 2 | ||||||||||||||||||
1618 | } executed 345 times by 7 tests: end of block Executed by:
| 345 | ||||||||||||||||||
1619 | } | - | ||||||||||||||||||
1620 | - | |||||||||||||||||||
1621 | #if OPENSSL_VERSION_NUMBER >= 0x1000100fL && !defined(OPENSSL_NO_NEXTPROTONEG) | - | ||||||||||||||||||
1622 | - | |||||||||||||||||||
1623 | configuration.nextProtocolNegotiationStatus = sslContextPointer->npnContext().status; | - | ||||||||||||||||||
1624 | if (sslContextPointer->npnContext().status == QSslConfiguration::NextProtocolNegotiationUnsupported) {
| 0-350 | ||||||||||||||||||
1625 | // we could not agree -> be conservative and use HTTP/1.1 | - | ||||||||||||||||||
1626 | configuration.nextNegotiatedProtocol = QByteArrayLiteral("http/1.1"); never executed: return ba; | 0 | ||||||||||||||||||
1627 | } else { never executed: end of block | 0 | ||||||||||||||||||
1628 | const unsigned char *proto = 0; | - | ||||||||||||||||||
1629 | unsigned int proto_len = 0; | - | ||||||||||||||||||
1630 | q_SSL_get0_next_proto_negotiated(ssl, &proto, &proto_len); | - | ||||||||||||||||||
1631 | if (proto_len)
| 6-344 | ||||||||||||||||||
1632 | configuration.nextNegotiatedProtocol = QByteArray(reinterpret_cast<const char *>(proto), proto_len); executed 6 times by 1 test: configuration.nextNegotiatedProtocol = QByteArray(reinterpret_cast<const char *>(proto), proto_len); Executed by:
| 6 | ||||||||||||||||||
1633 | else | - | ||||||||||||||||||
1634 | configuration.nextNegotiatedProtocol.clear(); executed 344 times by 7 tests: configuration.nextNegotiatedProtocol.clear(); Executed by:
| 344 | ||||||||||||||||||
1635 | } | - | ||||||||||||||||||
1636 | #endif // OPENSSL_VERSION_NUMBER >= 0x1000100fL ... | - | ||||||||||||||||||
1637 | - | |||||||||||||||||||
1638 | connectionEncrypted = true; | - | ||||||||||||||||||
1639 | emit q->encrypted(); | - | ||||||||||||||||||
1640 | if (autoStartHandshake && pendingClose) {
| 9-263 | ||||||||||||||||||
1641 | pendingClose = false; | - | ||||||||||||||||||
1642 | q->disconnectFromHost(); | - | ||||||||||||||||||
1643 | } executed 9 times by 1 test: end of block Executed by:
| 9 | ||||||||||||||||||
1644 | } executed 350 times by 7 tests: end of block Executed by:
| 350 | ||||||||||||||||||
1645 | - | |||||||||||||||||||
1646 | QList<QSslCertificate> QSslSocketBackendPrivate::STACKOFX509_to_QSslCertificates(STACK_OF(X509) *x509) | - | ||||||||||||||||||
1647 | { | - | ||||||||||||||||||
1648 | ensureInitialized(); | - | ||||||||||||||||||
1649 | QList<QSslCertificate> certificates; | - | ||||||||||||||||||
1650 | for (int i = 0; i < q_sk_X509_num(x509); ++i) {
| 409-439 | ||||||||||||||||||
1651 | if (X509 *entry = q_sk_X509_value(x509, i))
| 0-409 | ||||||||||||||||||
1652 | certificates << QSslCertificatePrivate::QSslCertificate_from_X509(entry); executed 409 times by 8 tests: certificates << QSslCertificatePrivate::QSslCertificate_from_X509(entry); Executed by:
| 409 | ||||||||||||||||||
1653 | } executed 409 times by 8 tests: end of block Executed by:
| 409 | ||||||||||||||||||
1654 | return certificates; executed 439 times by 8 tests: return certificates; Executed by:
| 439 | ||||||||||||||||||
1655 | } | - | ||||||||||||||||||
1656 | - | |||||||||||||||||||
1657 | QList<QSslError> QSslSocketBackendPrivate::verify(const QList<QSslCertificate> &certificateChain, const QString &hostName) | - | ||||||||||||||||||
1658 | { | - | ||||||||||||||||||
1659 | QList<QSslError> errors; | - | ||||||||||||||||||
1660 | if (certificateChain.count() <= 0) {
| 1-7 | ||||||||||||||||||
1661 | errors << QSslError(QSslError::UnspecifiedError); | - | ||||||||||||||||||
1662 | return errors; executed 1 time by 1 test: return errors; Executed by:
| 1 | ||||||||||||||||||
1663 | } | - | ||||||||||||||||||
1664 | - | |||||||||||||||||||
1665 | // Setup the store with the default CA certificates | - | ||||||||||||||||||
1666 | X509_STORE *certStore = q_X509_STORE_new(); | - | ||||||||||||||||||
1667 | if (!certStore) {
| 0-7 | ||||||||||||||||||
1668 | qCWarning(lcSsl) << "Unable to create certificate store"; never executed: QMessageLogger(__FILE__, 1668, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning() << "Unable to create certificate store";
| 0 | ||||||||||||||||||
1669 | errors << QSslError(QSslError::UnspecifiedError); | - | ||||||||||||||||||
1670 | return errors; never executed: return errors; | 0 | ||||||||||||||||||
1671 | } | - | ||||||||||||||||||
1672 | - | |||||||||||||||||||
1673 | if (s_loadRootCertsOnDemand) {
| 1-6 | ||||||||||||||||||
1674 | setDefaultCaCertificates(defaultCaCertificates() + systemCaCertificates()); | - | ||||||||||||||||||
1675 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
1676 | - | |||||||||||||||||||
1677 | const QDateTime now = QDateTime::currentDateTimeUtc(); | - | ||||||||||||||||||
1678 | foreach (const QSslCertificate &caCertificate, QSslConfiguration::defaultConfiguration().caCertificates()) { | - | ||||||||||||||||||
1679 | // From https://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html: | - | ||||||||||||||||||
1680 | // | - | ||||||||||||||||||
1681 | // If several CA certificates matching the name, key identifier, and | - | ||||||||||||||||||
1682 | // serial number condition are available, only the first one will be | - | ||||||||||||||||||
1683 | // examined. This may lead to unexpected results if the same CA | - | ||||||||||||||||||
1684 | // certificate is available with different expiration dates. If a | - | ||||||||||||||||||
1685 | // ``certificate expired'' verification error occurs, no other | - | ||||||||||||||||||
1686 | // certificate will be searched. Make sure to not have expired | - | ||||||||||||||||||
1687 | // certificates mixed with valid ones. | - | ||||||||||||||||||
1688 | // | - | ||||||||||||||||||
1689 | // See also: QSslContext::fromConfiguration() | - | ||||||||||||||||||
1690 | if (caCertificate.expiryDate() >= now) {
| 154-2296 | ||||||||||||||||||
1691 | q_X509_STORE_add_cert(certStore, reinterpret_cast<X509 *>(caCertificate.handle())); | - | ||||||||||||||||||
1692 | } executed 2296 times by 1 test: end of block Executed by:
| 2296 | ||||||||||||||||||
1693 | } executed 2450 times by 1 test: end of block Executed by:
| 2450 | ||||||||||||||||||
1694 | - | |||||||||||||||||||
1695 | QMutexLocker sslErrorListMutexLocker(&_q_sslErrorList()->mutex); | - | ||||||||||||||||||
1696 | - | |||||||||||||||||||
1697 | // Register a custom callback to get all verification errors. | - | ||||||||||||||||||
1698 | X509_STORE_set_verify_cb_func(certStore, q_X509Callback); | - | ||||||||||||||||||
1699 | - | |||||||||||||||||||
1700 | // Build the chain of intermediate certificates | - | ||||||||||||||||||
1701 | STACK_OF(X509) *intermediates = 0; | - | ||||||||||||||||||
1702 | if (certificateChain.length() > 1) {
| 3-4 | ||||||||||||||||||
1703 | intermediates = (STACK_OF(X509) *) q_sk_new_null(); | - | ||||||||||||||||||
1704 | - | |||||||||||||||||||
1705 | if (!intermediates) {
| 0-4 | ||||||||||||||||||
1706 | q_X509_STORE_free(certStore); | - | ||||||||||||||||||
1707 | errors << QSslError(QSslError::UnspecifiedError); | - | ||||||||||||||||||
1708 | return errors; never executed: return errors; | 0 | ||||||||||||||||||
1709 | } | - | ||||||||||||||||||
1710 | - | |||||||||||||||||||
1711 | bool first = true; | - | ||||||||||||||||||
1712 | foreach (const QSslCertificate &cert, certificateChain) { | - | ||||||||||||||||||
1713 | if (first) {
| 4 | ||||||||||||||||||
1714 | first = false; | - | ||||||||||||||||||
1715 | continue; executed 4 times by 1 test: continue; Executed by:
| 4 | ||||||||||||||||||
1716 | } | - | ||||||||||||||||||
1717 | #if OPENSSL_VERSION_NUMBER >= 0x10000000L | - | ||||||||||||||||||
1718 | q_sk_push( (_STACK *)intermediates, reinterpret_cast<X509 *>(cert.handle())); | - | ||||||||||||||||||
1719 | #else | - | ||||||||||||||||||
1720 | q_sk_push( (STACK *)intermediates, reinterpret_cast<char *>(cert.handle())); | - | ||||||||||||||||||
1721 | #endif | - | ||||||||||||||||||
1722 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
1723 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
1724 | - | |||||||||||||||||||
1725 | X509_STORE_CTX *storeContext = q_X509_STORE_CTX_new(); | - | ||||||||||||||||||
1726 | if (!storeContext) {
| 0-7 | ||||||||||||||||||
1727 | q_X509_STORE_free(certStore); | - | ||||||||||||||||||
1728 | errors << QSslError(QSslError::UnspecifiedError); | - | ||||||||||||||||||
1729 | return errors; never executed: return errors; | 0 | ||||||||||||||||||
1730 | } | - | ||||||||||||||||||
1731 | - | |||||||||||||||||||
1732 | if (!q_X509_STORE_CTX_init(storeContext, certStore, reinterpret_cast<X509 *>(certificateChain[0].handle()), intermediates)) {
| 0-7 | ||||||||||||||||||
1733 | q_X509_STORE_CTX_free(storeContext); | - | ||||||||||||||||||
1734 | q_X509_STORE_free(certStore); | - | ||||||||||||||||||
1735 | errors << QSslError(QSslError::UnspecifiedError); | - | ||||||||||||||||||
1736 | return errors; never executed: return errors; | 0 | ||||||||||||||||||
1737 | } | - | ||||||||||||||||||
1738 | - | |||||||||||||||||||
1739 | // Now we can actually perform the verification of the chain we have built. | - | ||||||||||||||||||
1740 | // We ignore the result of this function since we process errors via the | - | ||||||||||||||||||
1741 | // callback. | - | ||||||||||||||||||
1742 | (void) q_X509_verify_cert(storeContext); | - | ||||||||||||||||||
1743 | - | |||||||||||||||||||
1744 | q_X509_STORE_CTX_free(storeContext); | - | ||||||||||||||||||
1745 | #if OPENSSL_VERSION_NUMBER >= 0x10000000L | - | ||||||||||||||||||
1746 | q_sk_free( (_STACK *) intermediates); | - | ||||||||||||||||||
1747 | #else | - | ||||||||||||||||||
1748 | q_sk_free( (STACK *) intermediates); | - | ||||||||||||||||||
1749 | #endif | - | ||||||||||||||||||
1750 | - | |||||||||||||||||||
1751 | // Now process the errors | - | ||||||||||||||||||
1752 | const QList<QPair<int, int> > errorList = _q_sslErrorList()->errors; | - | ||||||||||||||||||
1753 | _q_sslErrorList()->errors.clear(); | - | ||||||||||||||||||
1754 | - | |||||||||||||||||||
1755 | sslErrorListMutexLocker.unlock(); | - | ||||||||||||||||||
1756 | - | |||||||||||||||||||
1757 | // Translate the errors | - | ||||||||||||||||||
1758 | if (QSslCertificatePrivate::isBlacklisted(certificateChain[0])) {
| 1-6 | ||||||||||||||||||
1759 | QSslError error(QSslError::CertificateBlacklisted, certificateChain[0]); | - | ||||||||||||||||||
1760 | errors << error; | - | ||||||||||||||||||
1761 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
1762 | - | |||||||||||||||||||
1763 | // Check the certificate name against the hostname if one was specified | - | ||||||||||||||||||
1764 | if ((!hostName.isEmpty()) && (!isMatchingHostname(certificateChain[0], hostName))) {
| 1-5 | ||||||||||||||||||
1765 | // No matches in common names or alternate names. | - | ||||||||||||||||||
1766 | QSslError error(QSslError::HostNameMismatch, certificateChain[0]); | - | ||||||||||||||||||
1767 | errors << error; | - | ||||||||||||||||||
1768 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||||||||
1769 | - | |||||||||||||||||||
1770 | // Translate errors from the error list into QSslErrors. | - | ||||||||||||||||||
1771 | const int numErrors = errorList.size(); | - | ||||||||||||||||||
1772 | errors.reserve(errors.size() + numErrors); | - | ||||||||||||||||||
1773 | for (int i = 0; i < numErrors; ++i) {
| 4-7 | ||||||||||||||||||
1774 | const QPair<int, int> &errorAndDepth = errorList.at(i); | - | ||||||||||||||||||
1775 | int err = errorAndDepth.first; | - | ||||||||||||||||||
1776 | int depth = errorAndDepth.second; | - | ||||||||||||||||||
1777 | errors << _q_OpenSSL_to_QSslError(err, certificateChain.value(depth)); | - | ||||||||||||||||||
1778 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||
1779 | - | |||||||||||||||||||
1780 | q_X509_STORE_free(certStore); | - | ||||||||||||||||||
1781 | - | |||||||||||||||||||
1782 | return errors; executed 7 times by 1 test: return errors; Executed by:
| 7 | ||||||||||||||||||
1783 | } | - | ||||||||||||||||||
1784 | - | |||||||||||||||||||
1785 | bool QSslSocketBackendPrivate::importPkcs12(QIODevice *device, | - | ||||||||||||||||||
1786 | QSslKey *key, QSslCertificate *cert, | - | ||||||||||||||||||
1787 | QList<QSslCertificate> *caCertificates, | - | ||||||||||||||||||
1788 | const QByteArray &passPhrase) | - | ||||||||||||||||||
1789 | { | - | ||||||||||||||||||
1790 | if (!supportsSsl())
| 0-1 | ||||||||||||||||||
1791 | return false; never executed: return false; | 0 | ||||||||||||||||||
1792 | - | |||||||||||||||||||
1793 | // These are required | - | ||||||||||||||||||
1794 | Q_ASSERT(device); | - | ||||||||||||||||||
1795 | Q_ASSERT(key); | - | ||||||||||||||||||
1796 | Q_ASSERT(cert); | - | ||||||||||||||||||
1797 | - | |||||||||||||||||||
1798 | // Read the file into a BIO | - | ||||||||||||||||||
1799 | QByteArray pkcs12data = device->readAll(); | - | ||||||||||||||||||
1800 | if (pkcs12data.size() == 0)
| 0-1 | ||||||||||||||||||
1801 | return false; never executed: return false; | 0 | ||||||||||||||||||
1802 | - | |||||||||||||||||||
1803 | BIO *bio = q_BIO_new_mem_buf(const_cast<char *>(pkcs12data.constData()), pkcs12data.size()); | - | ||||||||||||||||||
1804 | - | |||||||||||||||||||
1805 | // Create the PKCS#12 object | - | ||||||||||||||||||
1806 | PKCS12 *p12 = q_d2i_PKCS12_bio(bio, 0); | - | ||||||||||||||||||
1807 | if (!p12) {
| 0-1 | ||||||||||||||||||
1808 | qCWarning(lcSsl, "Unable to read PKCS#12 structure, %s", q_ERR_error_string(q_ERR_get_error(), 0)); never executed: QMessageLogger(__FILE__, 1808, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("Unable to read PKCS#12 structure, %s", q_ERR_error_string(q_ERR_get_error(), 0));
| 0 | ||||||||||||||||||
1809 | q_BIO_free(bio); | - | ||||||||||||||||||
1810 | return false; never executed: return false; | 0 | ||||||||||||||||||
1811 | } | - | ||||||||||||||||||
1812 | - | |||||||||||||||||||
1813 | // Extract the data | - | ||||||||||||||||||
1814 | EVP_PKEY *pkey; | - | ||||||||||||||||||
1815 | X509 *x509; | - | ||||||||||||||||||
1816 | STACK_OF(X509) *ca = 0; | - | ||||||||||||||||||
1817 | - | |||||||||||||||||||
1818 | if (!q_PKCS12_parse(p12, passPhrase.constData(), &pkey, &x509, &ca)) {
| 0-1 | ||||||||||||||||||
1819 | qCWarning(lcSsl, "Unable to parse PKCS#12 structure, %s", q_ERR_error_string(q_ERR_get_error(), 0)); never executed: QMessageLogger(__FILE__, 1819, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("Unable to parse PKCS#12 structure, %s", q_ERR_error_string(q_ERR_get_error(), 0));
| 0 | ||||||||||||||||||
1820 | q_PKCS12_free(p12); | - | ||||||||||||||||||
1821 | q_BIO_free(bio); | - | ||||||||||||||||||
1822 | return false; never executed: return false; | 0 | ||||||||||||||||||
1823 | } | - | ||||||||||||||||||
1824 | - | |||||||||||||||||||
1825 | // Convert to Qt types | - | ||||||||||||||||||
1826 | if (!key->d->fromEVP_PKEY(pkey)) {
| 0-1 | ||||||||||||||||||
1827 | qCWarning(lcSsl, "Unable to convert private key"); never executed: QMessageLogger(__FILE__, 1827, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("Unable to convert private key");
| 0 | ||||||||||||||||||
1828 | q_sk_pop_free(reinterpret_cast<STACK *>(ca), reinterpret_cast<void(*)(void*)>(q_sk_free)); | - | ||||||||||||||||||
1829 | q_X509_free(x509); | - | ||||||||||||||||||
1830 | q_EVP_PKEY_free(pkey); | - | ||||||||||||||||||
1831 | q_PKCS12_free(p12); | - | ||||||||||||||||||
1832 | q_BIO_free(bio); | - | ||||||||||||||||||
1833 | - | |||||||||||||||||||
1834 | return false; never executed: return false; | 0 | ||||||||||||||||||
1835 | } | - | ||||||||||||||||||
1836 | - | |||||||||||||||||||
1837 | *cert = QSslCertificatePrivate::QSslCertificate_from_X509(x509); | - | ||||||||||||||||||
1838 | - | |||||||||||||||||||
1839 | if (caCertificates)
| 0-1 | ||||||||||||||||||
1840 | *caCertificates = QSslSocketBackendPrivate::STACKOFX509_to_QSslCertificates(ca); executed 1 time by 1 test: *caCertificates = QSslSocketBackendPrivate::STACKOFX509_to_QSslCertificates(ca); Executed by:
| 1 | ||||||||||||||||||
1841 | - | |||||||||||||||||||
1842 | // Clean up | - | ||||||||||||||||||
1843 | q_sk_pop_free(reinterpret_cast<STACK *>(ca), reinterpret_cast<void(*)(void*)>(q_sk_free)); | - | ||||||||||||||||||
1844 | q_X509_free(x509); | - | ||||||||||||||||||
1845 | q_EVP_PKEY_free(pkey); | - | ||||||||||||||||||
1846 | q_PKCS12_free(p12); | - | ||||||||||||||||||
1847 | q_BIO_free(bio); | - | ||||||||||||||||||
1848 | - | |||||||||||||||||||
1849 | return true; executed 1 time by 1 test: return true; Executed by:
| 1 | ||||||||||||||||||
1850 | } | - | ||||||||||||||||||
1851 | - | |||||||||||||||||||
1852 | - | |||||||||||||||||||
1853 | QT_END_NAMESPACE | - | ||||||||||||||||||
Source code | Switch to Preprocessed file |