Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/network/ssl/qsslcontext_openssl.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||
2 | ** | - | ||||||||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||
4 | ** Copyright (C) 2014 BlackBerry Limited. All rights reserved. | - | ||||||||||||
5 | ** Copyright (C) 2014 Governikus GmbH & Co. KG. | - | ||||||||||||
6 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
7 | ** | - | ||||||||||||
8 | ** This file is part of the QtNetwork module of the Qt Toolkit. | - | ||||||||||||
9 | ** | - | ||||||||||||
10 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
11 | ** Commercial License Usage | - | ||||||||||||
12 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
13 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
14 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
15 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
16 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
17 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
18 | ** | - | ||||||||||||
19 | ** GNU Lesser General Public License Usage | - | ||||||||||||
20 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
21 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
22 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
23 | ** packaging of this file. Please review the following information to | - | ||||||||||||
24 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
25 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
26 | ** | - | ||||||||||||
27 | ** GNU General Public License Usage | - | ||||||||||||
28 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
29 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
30 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
31 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
32 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
33 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
34 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
35 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
36 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
37 | ** | - | ||||||||||||
38 | ** $QT_END_LICENSE$ | - | ||||||||||||
39 | ** | - | ||||||||||||
40 | ****************************************************************************/ | - | ||||||||||||
41 | - | |||||||||||||
42 | - | |||||||||||||
43 | #include <QtNetwork/qsslsocket.h> | - | ||||||||||||
44 | #include <QtCore/qmutex.h> | - | ||||||||||||
45 | - | |||||||||||||
46 | #include "private/qssl_p.h" | - | ||||||||||||
47 | #include "private/qsslcontext_openssl_p.h" | - | ||||||||||||
48 | #include "private/qsslsocket_p.h" | - | ||||||||||||
49 | #include "private/qsslsocket_openssl_p.h" | - | ||||||||||||
50 | #include "private/qsslsocket_openssl_symbols_p.h" | - | ||||||||||||
51 | - | |||||||||||||
52 | QT_BEGIN_NAMESPACE | - | ||||||||||||
53 | - | |||||||||||||
54 | // defined in qsslsocket_openssl.cpp: | - | ||||||||||||
55 | extern int q_X509Callback(int ok, X509_STORE_CTX *ctx); | - | ||||||||||||
56 | extern QString getErrorsFromOpenSsl(); | - | ||||||||||||
57 | - | |||||||||||||
58 | static DH *get_dh1024() | - | ||||||||||||
59 | { | - | ||||||||||||
60 | // Default DH params | - | ||||||||||||
61 | // 1024-bit MODP Group | - | ||||||||||||
62 | // From RFC 2409 | - | ||||||||||||
63 | QByteArray params = QByteArray::fromBase64( | - | ||||||||||||
64 | QByteArrayLiteral("MIGHAoGBAP//////////yQ/aoiFowjTExmKLgNwc0SkCTgiKZ8x0Agu+pjsTmyJR" \ executed 450 times by 7 tests: return ba; Executed by:
| 450 | ||||||||||||
65 | "Sgh5jjQE3e+VGbPNOkMbMCsKbfJfFDdP4TVtbVHCReSFtXZiXn7G9ExC6aY37WsL" \ | - | ||||||||||||
66 | "/1y29Aa37e44a/taiZ+lrp8kEXxLH+ZJKGZR7OZTgf//////////AgEC")); | - | ||||||||||||
67 | - | |||||||||||||
68 | const char *ptr = params.constData(); | - | ||||||||||||
69 | DH *dh = q_d2i_DHparams(NULL, reinterpret_cast<const unsigned char **>(&ptr), params.length()); | - | ||||||||||||
70 | - | |||||||||||||
71 | return dh; executed 450 times by 7 tests: return dh; Executed by:
| 450 | ||||||||||||
72 | } | - | ||||||||||||
73 | - | |||||||||||||
74 | QSslContext::QSslContext() | - | ||||||||||||
75 | : ctx(0), | - | ||||||||||||
76 | pkey(0), | - | ||||||||||||
77 | session(0), | - | ||||||||||||
78 | m_sessionTicketLifeTimeHint(-1) | - | ||||||||||||
79 | { | - | ||||||||||||
80 | } executed 458 times by 7 tests: end of block Executed by:
| 458 | ||||||||||||
81 | - | |||||||||||||
82 | QSslContext::~QSslContext() | - | ||||||||||||
83 | { | - | ||||||||||||
84 | if (ctx)
| 0-457 | ||||||||||||
85 | // This will decrement the reference count by 1 and free the context eventually when possible | - | ||||||||||||
86 | q_SSL_CTX_free(ctx); executed 457 times by 8 tests: q_SSL_CTX_free(ctx); Executed by:
| 457 | ||||||||||||
87 | - | |||||||||||||
88 | if (pkey)
| 83-374 | ||||||||||||
89 | q_EVP_PKEY_free(pkey); executed 83 times by 2 tests: q_EVP_PKEY_free(pkey); Executed by:
| 83 | ||||||||||||
90 | - | |||||||||||||
91 | if (session)
| 124-333 | ||||||||||||
92 | q_SSL_SESSION_free(session); executed 333 times by 8 tests: q_SSL_SESSION_free(session); Executed by:
| 333 | ||||||||||||
93 | } executed 457 times by 8 tests: end of block Executed by:
| 457 | ||||||||||||
94 | - | |||||||||||||
95 | static inline QString msgErrorSettingEllipticCurves(const QString &why) | - | ||||||||||||
96 | { | - | ||||||||||||
97 | return QSslSocket::tr("Error when setting the elliptic curves (%1)").arg(why); never executed: return QSslSocket::tr("Error when setting the elliptic curves (%1)").arg(why); | 0 | ||||||||||||
98 | } | - | ||||||||||||
99 | - | |||||||||||||
100 | // static | - | ||||||||||||
101 | void QSslContext::initSslContext(QSslContext *sslContext, QSslSocket::SslMode mode, const QSslConfiguration &configuration, bool allowRootCertOnDemandLoading) | - | ||||||||||||
102 | { | - | ||||||||||||
103 | sslContext->sslConfiguration = configuration; | - | ||||||||||||
104 | sslContext->errorCode = QSslError::NoError; | - | ||||||||||||
105 | - | |||||||||||||
106 | bool client = (mode == QSslSocket::SslClientMode); | - | ||||||||||||
107 | - | |||||||||||||
108 | bool reinitialized = false; | - | ||||||||||||
109 | bool unsupportedProtocol = false; | - | ||||||||||||
110 | init_context: code before this statement executed 458 times by 7 tests: init_context: Executed by:
| 458 | ||||||||||||
111 | switch (sslContext->sslConfiguration.protocol()) { | - | ||||||||||||
112 | case QSsl::SslV2: never executed: case QSsl::SslV2: | 0 | ||||||||||||
113 | #ifndef OPENSSL_NO_SSL2 | - | ||||||||||||
114 | sslContext->ctx = q_SSL_CTX_new(client ? q_SSLv2_client_method() : q_SSLv2_server_method()); | - | ||||||||||||
115 | #else | - | ||||||||||||
116 | // SSL 2 not supported by the system, but chosen deliberately -> error | - | ||||||||||||
117 | sslContext->ctx = 0; | - | ||||||||||||
118 | unsupportedProtocol = true; | - | ||||||||||||
119 | #endif | - | ||||||||||||
120 | break; never executed: break; | 0 | ||||||||||||
121 | case QSsl::SslV3: executed 19 times by 1 test: case QSsl::SslV3: Executed by:
| 19 | ||||||||||||
122 | #ifndef OPENSSL_NO_SSL3_METHOD | - | ||||||||||||
123 | sslContext->ctx = q_SSL_CTX_new(client ? q_SSLv3_client_method() : q_SSLv3_server_method()); | - | ||||||||||||
124 | #else | - | ||||||||||||
125 | // SSL 3 not supported by the system, but chosen deliberately -> error | - | ||||||||||||
126 | sslContext->ctx = 0; | - | ||||||||||||
127 | unsupportedProtocol = true; | - | ||||||||||||
128 | #endif | - | ||||||||||||
129 | break; executed 19 times by 1 test: break; Executed by:
| 19 | ||||||||||||
130 | case QSsl::SecureProtocols: executed 328 times by 7 tests: case QSsl::SecureProtocols: Executed by:
| 328 | ||||||||||||
131 | // SSLv2 and SSLv3 will be disabled by SSL options | - | ||||||||||||
132 | // But we need q_SSLv23_server_method() otherwise AnyProtocol will be unable to connect on Win32. | - | ||||||||||||
133 | case QSsl::TlsV1SslV3: executed 18 times by 1 test: case QSsl::TlsV1SslV3: Executed by:
| 18 | ||||||||||||
134 | // SSLv2 will will be disabled by SSL options | - | ||||||||||||
135 | case QSsl::AnyProtocol: executed 24 times by 2 tests: case QSsl::AnyProtocol: Executed by:
| 24 | ||||||||||||
136 | default: never executed: default: | 0 | ||||||||||||
137 | sslContext->ctx = q_SSL_CTX_new(client ? q_SSLv23_client_method() : q_SSLv23_server_method()); | - | ||||||||||||
138 | break; executed 370 times by 7 tests: break; Executed by:
| 370 | ||||||||||||
139 | case QSsl::TlsV1_0: executed 51 times by 1 test: case QSsl::TlsV1_0: Executed by:
| 51 | ||||||||||||
140 | sslContext->ctx = q_SSL_CTX_new(client ? q_TLSv1_client_method() : q_TLSv1_server_method()); | - | ||||||||||||
141 | break; executed 51 times by 1 test: break; Executed by:
| 51 | ||||||||||||
142 | case QSsl::TlsV1_1: executed 9 times by 1 test: case QSsl::TlsV1_1: Executed by:
| 9 | ||||||||||||
143 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L | - | ||||||||||||
144 | sslContext->ctx = q_SSL_CTX_new(client ? q_TLSv1_1_client_method() : q_TLSv1_1_server_method()); | - | ||||||||||||
145 | #else | - | ||||||||||||
146 | // TLS 1.1 not supported by the system, but chosen deliberately -> error | - | ||||||||||||
147 | sslContext->ctx = 0; | - | ||||||||||||
148 | unsupportedProtocol = true; | - | ||||||||||||
149 | #endif | - | ||||||||||||
150 | break; executed 9 times by 1 test: break; Executed by:
| 9 | ||||||||||||
151 | case QSsl::TlsV1_2: executed 9 times by 1 test: case QSsl::TlsV1_2: Executed by:
| 9 | ||||||||||||
152 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L | - | ||||||||||||
153 | sslContext->ctx = q_SSL_CTX_new(client ? q_TLSv1_2_client_method() : q_TLSv1_2_server_method()); | - | ||||||||||||
154 | #else | - | ||||||||||||
155 | // TLS 1.2 not supported by the system, but chosen deliberately -> error | - | ||||||||||||
156 | sslContext->ctx = 0; | - | ||||||||||||
157 | unsupportedProtocol = true; | - | ||||||||||||
158 | #endif | - | ||||||||||||
159 | break; executed 9 times by 1 test: break; Executed by:
| 9 | ||||||||||||
160 | case QSsl::TlsV1_0OrLater: never executed: case QSsl::TlsV1_0OrLater: | 0 | ||||||||||||
161 | // Specific protocols will be specified via SSL options. | - | ||||||||||||
162 | sslContext->ctx = q_SSL_CTX_new(client ? q_SSLv23_client_method() : q_SSLv23_server_method()); | - | ||||||||||||
163 | break; never executed: break; | 0 | ||||||||||||
164 | case QSsl::TlsV1_1OrLater: never executed: case QSsl::TlsV1_1OrLater: | 0 | ||||||||||||
165 | case QSsl::TlsV1_2OrLater: never executed: case QSsl::TlsV1_2OrLater: | 0 | ||||||||||||
166 | #if OPENSSL_VERSION_NUMBER >= 0x10001000L | - | ||||||||||||
167 | // Specific protocols will be specified via SSL options. | - | ||||||||||||
168 | sslContext->ctx = q_SSL_CTX_new(client ? q_SSLv23_client_method() : q_SSLv23_server_method()); | - | ||||||||||||
169 | #else | - | ||||||||||||
170 | // TLS 1.1/1.2 not supported by the system, but chosen deliberately -> error | - | ||||||||||||
171 | sslContext->ctx = 0; | - | ||||||||||||
172 | unsupportedProtocol = true; | - | ||||||||||||
173 | #endif | - | ||||||||||||
174 | break; never executed: break; | 0 | ||||||||||||
175 | } | - | ||||||||||||
176 | - | |||||||||||||
177 | if (!sslContext->ctx) {
| 0-458 | ||||||||||||
178 | // After stopping Flash 10 the SSL library looses its ciphers. Try re-adding them | - | ||||||||||||
179 | // by re-initializing the library. | - | ||||||||||||
180 | if (!reinitialized) {
| 0 | ||||||||||||
181 | reinitialized = true; | - | ||||||||||||
182 | if (q_SSL_library_init() == 1)
| 0 | ||||||||||||
183 | goto init_context; never executed: goto init_context; | 0 | ||||||||||||
184 | } never executed: end of block | 0 | ||||||||||||
185 | - | |||||||||||||
186 | sslContext->errorStr = QSslSocket::tr("Error creating SSL context (%1)").arg( | - | ||||||||||||
187 | unsupportedProtocol ? QSslSocket::tr("unsupported protocol") : QSslSocketBackendPrivate::getErrorsFromOpenSsl() | - | ||||||||||||
188 | ); | - | ||||||||||||
189 | sslContext->errorCode = QSslError::UnspecifiedError; | - | ||||||||||||
190 | return; never executed: return; | 0 | ||||||||||||
191 | } | - | ||||||||||||
192 | - | |||||||||||||
193 | // Enable bug workarounds. | - | ||||||||||||
194 | long options = QSslSocketBackendPrivate::setupOpenSslOptions(configuration.protocol(), configuration.d->sslOptions); | - | ||||||||||||
195 | q_SSL_CTX_set_options(sslContext->ctx, options); | - | ||||||||||||
196 | - | |||||||||||||
197 | #if OPENSSL_VERSION_NUMBER >= 0x10000000L | - | ||||||||||||
198 | // Tell OpenSSL to release memory early | - | ||||||||||||
199 | // http://www.openssl.org/docs/ssl/SSL_CTX_set_mode.html | - | ||||||||||||
200 | if (q_SSLeay() >= 0x10000000L)
| 0-458 | ||||||||||||
201 | q_SSL_CTX_set_mode(sslContext->ctx, SSL_MODE_RELEASE_BUFFERS); executed 458 times by 7 tests: q_SSL_CTX_ctrl((sslContext->ctx),33,(0x00000010L),__null); Executed by:
| 458 | ||||||||||||
202 | #endif | - | ||||||||||||
203 | - | |||||||||||||
204 | // Initialize ciphers | - | ||||||||||||
205 | QByteArray cipherString; | - | ||||||||||||
206 | bool first = true; | - | ||||||||||||
207 | QList<QSslCipher> ciphers = sslContext->sslConfiguration.ciphers(); | - | ||||||||||||
208 | if (ciphers.isEmpty())
| 54-404 | ||||||||||||
209 | ciphers = QSslSocketPrivate::defaultCiphers(); executed 54 times by 4 tests: ciphers = QSslSocketPrivate::defaultCiphers(); Executed by:
| 54 | ||||||||||||
210 | for (const QSslCipher &cipher : qAsConst(ciphers)) { | - | ||||||||||||
211 | if (first)
| 451-28800 | ||||||||||||
212 | first = false; executed 451 times by 7 tests: first = false; Executed by:
| 451 | ||||||||||||
213 | else | - | ||||||||||||
214 | cipherString.append(':'); executed 28800 times by 7 tests: cipherString.append(':'); Executed by:
| 28800 | ||||||||||||
215 | cipherString.append(cipher.name().toLatin1()); | - | ||||||||||||
216 | } executed 29251 times by 7 tests: end of block Executed by:
| 29251 | ||||||||||||
217 | - | |||||||||||||
218 | if (!q_SSL_CTX_set_cipher_list(sslContext->ctx, cipherString.data())) {
| 7-451 | ||||||||||||
219 | sslContext->errorStr = QSslSocket::tr("Invalid or empty cipher list (%1)").arg(QSslSocketBackendPrivate::getErrorsFromOpenSsl()); | - | ||||||||||||
220 | sslContext->errorCode = QSslError::UnspecifiedError; | - | ||||||||||||
221 | return; executed 7 times by 2 tests: return; Executed by:
| 7 | ||||||||||||
222 | } | - | ||||||||||||
223 | - | |||||||||||||
224 | const QDateTime now = QDateTime::currentDateTimeUtc(); | - | ||||||||||||
225 | - | |||||||||||||
226 | // Add all our CAs to this store. | - | ||||||||||||
227 | const auto caCertificates = sslContext->sslConfiguration.caCertificates(); | - | ||||||||||||
228 | for (const QSslCertificate &caCertificate : caCertificates) { | - | ||||||||||||
229 | // From https://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html: | - | ||||||||||||
230 | // | - | ||||||||||||
231 | // If several CA certificates matching the name, key identifier, and | - | ||||||||||||
232 | // serial number condition are available, only the first one will be | - | ||||||||||||
233 | // examined. This may lead to unexpected results if the same CA | - | ||||||||||||
234 | // certificate is available with different expiration dates. If a | - | ||||||||||||
235 | // ``certificate expired'' verification error occurs, no other | - | ||||||||||||
236 | // certificate will be searched. Make sure to not have expired | - | ||||||||||||
237 | // certificates mixed with valid ones. | - | ||||||||||||
238 | // | - | ||||||||||||
239 | // See also: QSslSocketBackendPrivate::verify() | - | ||||||||||||
240 | if (caCertificate.expiryDate() >= now) {
| 3454-51484 | ||||||||||||
241 | q_X509_STORE_add_cert(q_SSL_CTX_get_cert_store(sslContext->ctx), (X509 *)caCertificate.handle()); | - | ||||||||||||
242 | } executed 51484 times by 3 tests: end of block Executed by:
| 51484 | ||||||||||||
243 | } executed 54938 times by 3 tests: end of block Executed by:
| 54938 | ||||||||||||
244 | - | |||||||||||||
245 | if (QSslSocketPrivate::s_loadRootCertsOnDemand && allowRootCertOnDemandLoading) {
| 48-340 | ||||||||||||
246 | // tell OpenSSL the directories where to look up the root certs on demand | - | ||||||||||||
247 | const QList<QByteArray> unixDirs = QSslSocketPrivate::unixRootCertDirectories(); | - | ||||||||||||
248 | for (const QByteArray &unixDir : unixDirs) | - | ||||||||||||
249 | q_SSL_CTX_load_verify_locations(sslContext->ctx, 0, unixDir.constData()); executed 567 times by 5 tests: q_SSL_CTX_load_verify_locations(sslContext->ctx, 0, unixDir.constData()); Executed by:
| 567 | ||||||||||||
250 | } executed 63 times by 5 tests: end of block Executed by:
| 63 | ||||||||||||
251 | - | |||||||||||||
252 | if (!sslContext->sslConfiguration.localCertificate().isNull()) {
| 89-362 | ||||||||||||
253 | // Require a private key as well. | - | ||||||||||||
254 | if (sslContext->sslConfiguration.privateKey().isNull()) {
| 1-88 | ||||||||||||
255 | sslContext->errorStr = QSslSocket::tr("Cannot provide a certificate with no key, %1").arg(QSslSocketBackendPrivate::getErrorsFromOpenSsl()); | - | ||||||||||||
256 | sslContext->errorCode = QSslError::UnspecifiedError; | - | ||||||||||||
257 | return; executed 1 time by 1 test: return; Executed by:
| 1 | ||||||||||||
258 | } | - | ||||||||||||
259 | - | |||||||||||||
260 | // Load certificate | - | ||||||||||||
261 | if (!q_SSL_CTX_use_certificate(sslContext->ctx, (X509 *)sslContext->sslConfiguration.localCertificate().handle())) {
| 0-88 | ||||||||||||
262 | sslContext->errorStr = QSslSocket::tr("Error loading local certificate, %1").arg(QSslSocketBackendPrivate::getErrorsFromOpenSsl()); | - | ||||||||||||
263 | sslContext->errorCode = QSslError::UnspecifiedError; | - | ||||||||||||
264 | return; never executed: return; | 0 | ||||||||||||
265 | } | - | ||||||||||||
266 | - | |||||||||||||
267 | if (configuration.d->privateKey.algorithm() == QSsl::Opaque) {
| 4-84 | ||||||||||||
268 | sslContext->pkey = reinterpret_cast<EVP_PKEY *>(configuration.d->privateKey.handle()); | - | ||||||||||||
269 | } else { executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||
270 | // Load private key | - | ||||||||||||
271 | sslContext->pkey = q_EVP_PKEY_new(); | - | ||||||||||||
272 | // before we were using EVP_PKEY_assign_R* functions and did not use EVP_PKEY_free. | - | ||||||||||||
273 | // this lead to a memory leak. Now we use the *_set1_* functions which do not | - | ||||||||||||
274 | // take ownership of the RSA/DSA key instance because the QSslKey already has ownership. | - | ||||||||||||
275 | if (configuration.d->privateKey.algorithm() == QSsl::Rsa)
| 0-84 | ||||||||||||
276 | q_EVP_PKEY_set1_RSA(sslContext->pkey, reinterpret_cast<RSA *>(configuration.d->privateKey.handle())); executed 84 times by 2 tests: q_EVP_PKEY_set1_RSA(sslContext->pkey, reinterpret_cast<RSA *>(configuration.d->privateKey.handle())); Executed by:
| 84 | ||||||||||||
277 | else if (configuration.d->privateKey.algorithm() == QSsl::Dsa)
| 0 | ||||||||||||
278 | q_EVP_PKEY_set1_DSA(sslContext->pkey, reinterpret_cast<DSA *>(configuration.d->privateKey.handle())); never executed: q_EVP_PKEY_set1_DSA(sslContext->pkey, reinterpret_cast<DSA *>(configuration.d->privateKey.handle())); | 0 | ||||||||||||
279 | #ifndef OPENSSL_NO_EC | - | ||||||||||||
280 | else if (configuration.d->privateKey.algorithm() == QSsl::Ec)
| 0 | ||||||||||||
281 | q_EVP_PKEY_set1_EC_KEY(sslContext->pkey, reinterpret_cast<EC_KEY *>(configuration.d->privateKey.handle())); never executed: q_EVP_PKEY_set1_EC_KEY(sslContext->pkey, reinterpret_cast<EC_KEY *>(configuration.d->privateKey.handle())); | 0 | ||||||||||||
282 | #endif | - | ||||||||||||
283 | } executed 84 times by 2 tests: end of block Executed by:
| 84 | ||||||||||||
284 | - | |||||||||||||
285 | if (!q_SSL_CTX_use_PrivateKey(sslContext->ctx, sslContext->pkey)) {
| 0-88 | ||||||||||||
286 | sslContext->errorStr = QSslSocket::tr("Error loading private key, %1").arg(QSslSocketBackendPrivate::getErrorsFromOpenSsl()); | - | ||||||||||||
287 | sslContext->errorCode = QSslError::UnspecifiedError; | - | ||||||||||||
288 | return; never executed: return; | 0 | ||||||||||||
289 | } | - | ||||||||||||
290 | if (configuration.d->privateKey.algorithm() == QSsl::Opaque)
| 4-84 | ||||||||||||
291 | sslContext->pkey = 0; // Don't free the private key, it belongs to QSslKey executed 4 times by 1 test: sslContext->pkey = 0; Executed by:
| 4 | ||||||||||||
292 | - | |||||||||||||
293 | // Check if the certificate matches the private key. | - | ||||||||||||
294 | if (!q_SSL_CTX_check_private_key(sslContext->ctx)) {
| 0-88 | ||||||||||||
295 | sslContext->errorStr = QSslSocket::tr("Private key does not certify public key, %1").arg(QSslSocketBackendPrivate::getErrorsFromOpenSsl()); | - | ||||||||||||
296 | sslContext->errorCode = QSslError::UnspecifiedError; | - | ||||||||||||
297 | return; never executed: return; | 0 | ||||||||||||
298 | } | - | ||||||||||||
299 | - | |||||||||||||
300 | // If we have any intermediate certificates then we need to add them to our chain | - | ||||||||||||
301 | bool first = true; | - | ||||||||||||
302 | for (const QSslCertificate &cert : qAsConst(configuration.d->localCertificateChain)) { | - | ||||||||||||
303 | if (first) {
| 5-88 | ||||||||||||
304 | first = false; | - | ||||||||||||
305 | continue; executed 88 times by 2 tests: continue; Executed by:
| 88 | ||||||||||||
306 | } | - | ||||||||||||
307 | q_SSL_CTX_ctrl(sslContext->ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, | - | ||||||||||||
308 | q_X509_dup(reinterpret_cast<X509 *>(cert.handle()))); | - | ||||||||||||
309 | } executed 5 times by 1 test: end of block Executed by:
| 5 | ||||||||||||
310 | } executed 88 times by 2 tests: end of block Executed by:
| 88 | ||||||||||||
311 | - | |||||||||||||
312 | // Initialize peer verification. | - | ||||||||||||
313 | if (sslContext->sslConfiguration.peerVerifyMode() == QSslSocket::VerifyNone) {
| 7-443 | ||||||||||||
314 | q_SSL_CTX_set_verify(sslContext->ctx, SSL_VERIFY_NONE, 0); | - | ||||||||||||
315 | } else { executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
316 | q_SSL_CTX_set_verify(sslContext->ctx, SSL_VERIFY_PEER, q_X509Callback); | - | ||||||||||||
317 | } executed 443 times by 7 tests: end of block Executed by:
| 443 | ||||||||||||
318 | - | |||||||||||||
319 | // Set verification depth. | - | ||||||||||||
320 | if (sslContext->sslConfiguration.peerVerifyDepth() != 0)
| 0-450 | ||||||||||||
321 | q_SSL_CTX_set_verify_depth(sslContext->ctx, sslContext->sslConfiguration.peerVerifyDepth()); never executed: q_SSL_CTX_set_verify_depth(sslContext->ctx, sslContext->sslConfiguration.peerVerifyDepth()); | 0 | ||||||||||||
322 | - | |||||||||||||
323 | // set persisted session if the user set it | - | ||||||||||||
324 | if (!configuration.sessionTicket().isEmpty())
| 1-449 | ||||||||||||
325 | sslContext->setSessionASN1(configuration.sessionTicket()); executed 1 time by 1 test: sslContext->setSessionASN1(configuration.sessionTicket()); Executed by:
| 1 | ||||||||||||
326 | - | |||||||||||||
327 | // Set temp DH params | - | ||||||||||||
328 | DH *dh = 0; | - | ||||||||||||
329 | dh = get_dh1024(); | - | ||||||||||||
330 | q_SSL_CTX_set_tmp_dh(sslContext->ctx, dh); | - | ||||||||||||
331 | q_DH_free(dh); | - | ||||||||||||
332 | - | |||||||||||||
333 | #ifndef OPENSSL_NO_EC | - | ||||||||||||
334 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L | - | ||||||||||||
335 | if (q_SSLeay() >= 0x10002000L) { | - | ||||||||||||
336 | q_SSL_CTX_ctrl(sslContext->ctx, SSL_CTRL_SET_ECDH_AUTO, 1, NULL); | - | ||||||||||||
337 | } else | - | ||||||||||||
338 | #endif | - | ||||||||||||
339 | { | - | ||||||||||||
340 | // Set temp ECDH params | - | ||||||||||||
341 | EC_KEY *ecdh = 0; | - | ||||||||||||
342 | ecdh = q_EC_KEY_new_by_curve_name(NID_X9_62_prime256v1); | - | ||||||||||||
343 | q_SSL_CTX_set_tmp_ecdh(sslContext->ctx, ecdh); | - | ||||||||||||
344 | q_EC_KEY_free(ecdh); | - | ||||||||||||
345 | } | - | ||||||||||||
346 | #endif // OPENSSL_NO_EC | - | ||||||||||||
347 | - | |||||||||||||
348 | const QVector<QSslEllipticCurve> qcurves = sslContext->sslConfiguration.ellipticCurves(); | - | ||||||||||||
349 | if (!qcurves.isEmpty()) {
| 0-450 | ||||||||||||
350 | #if OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(OPENSSL_NO_EC) | - | ||||||||||||
351 | // Set the curves to be used | - | ||||||||||||
352 | if (q_SSLeay() >= 0x10002000L) { | - | ||||||||||||
353 | // SSL_CTX_ctrl wants a non-const pointer as last argument, | - | ||||||||||||
354 | // but let's avoid a copy into a temporary array | - | ||||||||||||
355 | if (!q_SSL_CTX_ctrl(sslContext->ctx, | - | ||||||||||||
356 | SSL_CTRL_SET_CURVES, | - | ||||||||||||
357 | qcurves.size(), | - | ||||||||||||
358 | const_cast<int *>(reinterpret_cast<const int *>(qcurves.data())))) { | - | ||||||||||||
359 | sslContext->errorStr = msgErrorSettingEllipticCurves(QSslSocketBackendPrivate::getErrorsFromOpenSsl()); | - | ||||||||||||
360 | sslContext->errorCode = QSslError::UnspecifiedError; | - | ||||||||||||
361 | } | - | ||||||||||||
362 | } else | - | ||||||||||||
363 | #endif // OPENSSL_VERSION_NUMBER >= 0x10002000L && !defined(OPENSSL_NO_EC) | - | ||||||||||||
364 | { | - | ||||||||||||
365 | // specific curves requested, but not possible to set -> error | - | ||||||||||||
366 | sslContext->errorStr = msgErrorSettingEllipticCurves(QSslSocket::tr("OpenSSL version too old, need at least v1.0.2")); | - | ||||||||||||
367 | sslContext->errorCode = QSslError::UnspecifiedError; | - | ||||||||||||
368 | } | - | ||||||||||||
369 | } never executed: end of block | 0 | ||||||||||||
370 | } executed 450 times by 7 tests: end of block Executed by:
| 450 | ||||||||||||
371 | - | |||||||||||||
372 | QSslContext* QSslContext::fromConfiguration(QSslSocket::SslMode mode, const QSslConfiguration &configuration, bool allowRootCertOnDemandLoading) | - | ||||||||||||
373 | { | - | ||||||||||||
374 | QSslContext *sslContext = new QSslContext(); | - | ||||||||||||
375 | initSslContext(sslContext, mode, configuration, allowRootCertOnDemandLoading); | - | ||||||||||||
376 | return sslContext; never executed: return sslContext; | 0 | ||||||||||||
377 | } | - | ||||||||||||
378 | - | |||||||||||||
379 | QSharedPointer<QSslContext> QSslContext::sharedFromConfiguration(QSslSocket::SslMode mode, const QSslConfiguration &configuration, bool allowRootCertOnDemandLoading) | - | ||||||||||||
380 | { | - | ||||||||||||
381 | QSharedPointer<QSslContext> sslContext = QSharedPointer<QSslContext>::create(); | - | ||||||||||||
382 | initSslContext(sslContext.data(), mode, configuration, allowRootCertOnDemandLoading); | - | ||||||||||||
383 | return sslContext; executed 458 times by 7 tests: return sslContext; Executed by:
| 458 | ||||||||||||
384 | } | - | ||||||||||||
385 | - | |||||||||||||
386 | #if OPENSSL_VERSION_NUMBER >= 0x1000100fL && !defined(OPENSSL_NO_NEXTPROTONEG) | - | ||||||||||||
387 | - | |||||||||||||
388 | static int next_proto_cb(SSL *, unsigned char **out, unsigned char *outlen, | - | ||||||||||||
389 | const unsigned char *in, unsigned int inlen, void *arg) | - | ||||||||||||
390 | { | - | ||||||||||||
391 | QSslContext::NPNContext *ctx = reinterpret_cast<QSslContext::NPNContext *>(arg); | - | ||||||||||||
392 | - | |||||||||||||
393 | // comment out to debug: | - | ||||||||||||
394 | // QList<QByteArray> supportedVersions; | - | ||||||||||||
395 | // for (unsigned int i = 0; i < inlen; ) { | - | ||||||||||||
396 | // QByteArray version(reinterpret_cast<const char *>(&in[i+1]), in[i]); | - | ||||||||||||
397 | // supportedVersions << version; | - | ||||||||||||
398 | // i += in[i] + 1; | - | ||||||||||||
399 | // } | - | ||||||||||||
400 | - | |||||||||||||
401 | int proto = q_SSL_select_next_proto(out, outlen, in, inlen, ctx->data, ctx->len); | - | ||||||||||||
402 | switch (proto) { | - | ||||||||||||
403 | case OPENSSL_NPN_UNSUPPORTED: never executed: case 0: | 0 | ||||||||||||
404 | ctx->status = QSslConfiguration::NextProtocolNegotiationNone; | - | ||||||||||||
405 | break; never executed: break; | 0 | ||||||||||||
406 | case OPENSSL_NPN_NEGOTIATED: executed 7 times by 1 test: case 1: Executed by:
| 7 | ||||||||||||
407 | ctx->status = QSslConfiguration::NextProtocolNegotiationNegotiated; | - | ||||||||||||
408 | break; executed 7 times by 1 test: break; Executed by:
| 7 | ||||||||||||
409 | case OPENSSL_NPN_NO_OVERLAP: never executed: case 2: | 0 | ||||||||||||
410 | ctx->status = QSslConfiguration::NextProtocolNegotiationUnsupported; | - | ||||||||||||
411 | break; never executed: break; | 0 | ||||||||||||
412 | default: never executed: default: | 0 | ||||||||||||
413 | qCWarning(lcSsl, "OpenSSL sent unknown NPN status"); never executed: QMessageLogger(__FILE__, 413, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("OpenSSL sent unknown NPN status");
| 0 | ||||||||||||
414 | } never executed: end of block | 0 | ||||||||||||
415 | - | |||||||||||||
416 | return SSL_TLSEXT_ERR_OK; executed 7 times by 1 test: return 0; Executed by:
| 7 | ||||||||||||
417 | } | - | ||||||||||||
418 | - | |||||||||||||
419 | QSslContext::NPNContext QSslContext::npnContext() const | - | ||||||||||||
420 | { | - | ||||||||||||
421 | return m_npnContext; executed 710 times by 7 tests: return m_npnContext; Executed by:
| 710 | ||||||||||||
422 | } | - | ||||||||||||
423 | #endif // OPENSSL_VERSION_NUMBER >= 0x1000100fL ... | - | ||||||||||||
424 | - | |||||||||||||
425 | // Needs to be deleted by caller | - | ||||||||||||
426 | SSL* QSslContext::createSsl() | - | ||||||||||||
427 | { | - | ||||||||||||
428 | SSL* ssl = q_SSL_new(ctx); | - | ||||||||||||
429 | q_SSL_clear(ssl); | - | ||||||||||||
430 | - | |||||||||||||
431 | if (!session && !sessionASN1().isEmpty()
| 1-461 | ||||||||||||
432 | && !sslConfiguration.testSslOption(QSsl::SslOptionDisableSessionPersistence)) {
| 0-1 | ||||||||||||
433 | const unsigned char *data = reinterpret_cast<const unsigned char *>(m_sessionASN1.constData()); | - | ||||||||||||
434 | session = q_d2i_SSL_SESSION(0, &data, m_sessionASN1.size()); // refcount is 1 already, set by function above | - | ||||||||||||
435 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
436 | - | |||||||||||||
437 | if (session) {
| 16-460 | ||||||||||||
438 | // Try to resume the last session we cached | - | ||||||||||||
439 | if (!q_SSL_set_session(ssl, session)) {
| 0-16 | ||||||||||||
440 | qCWarning(lcSsl, "could not set SSL session"); never executed: QMessageLogger(__FILE__, 440, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("could not set SSL session");
| 0 | ||||||||||||
441 | q_SSL_SESSION_free(session); | - | ||||||||||||
442 | session = 0; | - | ||||||||||||
443 | } never executed: end of block | 0 | ||||||||||||
444 | } executed 16 times by 1 test: end of block Executed by:
| 16 | ||||||||||||
445 | - | |||||||||||||
446 | #if OPENSSL_VERSION_NUMBER >= 0x1000100fL && !defined(OPENSSL_NO_NEXTPROTONEG) | - | ||||||||||||
447 | QList<QByteArray> protocols = sslConfiguration.d->nextAllowedProtocols; | - | ||||||||||||
448 | if (!protocols.isEmpty()) {
| 7-469 | ||||||||||||
449 | m_supportedNPNVersions.clear(); | - | ||||||||||||
450 | for (int a = 0; a < protocols.count(); ++a) {
| 7-14 | ||||||||||||
451 | if (protocols.at(a).size() > 255) {
| 0-14 | ||||||||||||
452 | qCWarning(lcSsl) << "TLS NPN extension" << protocols.at(a) never executed: QMessageLogger(__FILE__, 452, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning() << "TLS NPN extension" << protocols.at(a) << "is too long and will be truncated to 255 characters.";
| 0 | ||||||||||||
453 | << "is too long and will be truncated to 255 characters."; never executed: QMessageLogger(__FILE__, 452, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning() << "TLS NPN extension" << protocols.at(a) << "is too long and will be truncated to 255 characters."; | 0 | ||||||||||||
454 | protocols[a] = protocols.at(a).left(255); | - | ||||||||||||
455 | } never executed: end of block | 0 | ||||||||||||
456 | m_supportedNPNVersions.append(protocols.at(a).size()).append(protocols.at(a)); | - | ||||||||||||
457 | } executed 14 times by 1 test: end of block Executed by:
| 14 | ||||||||||||
458 | m_npnContext.data = reinterpret_cast<unsigned char *>(m_supportedNPNVersions.data()); | - | ||||||||||||
459 | m_npnContext.len = m_supportedNPNVersions.count(); | - | ||||||||||||
460 | m_npnContext.status = QSslConfiguration::NextProtocolNegotiationNone; | - | ||||||||||||
461 | q_SSL_CTX_set_next_proto_select_cb(ctx, next_proto_cb, &m_npnContext); | - | ||||||||||||
462 | } executed 7 times by 1 test: end of block Executed by:
| 7 | ||||||||||||
463 | #endif // OPENSSL_VERSION_NUMBER >= 0x1000100fL ... | - | ||||||||||||
464 | - | |||||||||||||
465 | return ssl; executed 476 times by 7 tests: return ssl; Executed by:
| 476 | ||||||||||||
466 | } | - | ||||||||||||
467 | - | |||||||||||||
468 | // We cache exactly one session here | - | ||||||||||||
469 | bool QSslContext::cacheSession(SSL* ssl) | - | ||||||||||||
470 | { | - | ||||||||||||
471 | // don't cache the same session again | - | ||||||||||||
472 | if (session && session == q_SSL_get_session(ssl))
| 0-333 | ||||||||||||
473 | return true; executed 16 times by 1 test: return true; Executed by:
| 16 | ||||||||||||
474 | - | |||||||||||||
475 | // decrease refcount of currently stored session | - | ||||||||||||
476 | // (this might happen if there are several concurrent handshakes in flight) | - | ||||||||||||
477 | if (session)
| 0-333 | ||||||||||||
478 | q_SSL_SESSION_free(session); never executed: q_SSL_SESSION_free(session); | 0 | ||||||||||||
479 | - | |||||||||||||
480 | // cache the session the caller gave us and increase reference count | - | ||||||||||||
481 | session = q_SSL_get1_session(ssl); | - | ||||||||||||
482 | - | |||||||||||||
483 | if (session && !sslConfiguration.testSslOption(QSsl::SslOptionDisableSessionPersistence)) {
| 0-333 | ||||||||||||
484 | int sessionSize = q_i2d_SSL_SESSION(session, 0); | - | ||||||||||||
485 | if (sessionSize > 0) {
| 0-1 | ||||||||||||
486 | m_sessionASN1.resize(sessionSize); | - | ||||||||||||
487 | unsigned char *data = reinterpret_cast<unsigned char *>(m_sessionASN1.data()); | - | ||||||||||||
488 | if (!q_i2d_SSL_SESSION(session, &data))
| 0-1 | ||||||||||||
489 | qCWarning(lcSsl, "could not store persistent version of SSL session"); never executed: QMessageLogger(__FILE__, 489, __PRETTY_FUNCTION__, lcSsl().categoryName()).warning("could not store persistent version of SSL session");
| 0 | ||||||||||||
490 | m_sessionTicketLifeTimeHint = session->tlsext_tick_lifetime_hint; | - | ||||||||||||
491 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
492 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
493 | - | |||||||||||||
494 | return (session != 0); executed 333 times by 7 tests: return (session != 0); Executed by:
| 333 | ||||||||||||
495 | } | - | ||||||||||||
496 | - | |||||||||||||
497 | QByteArray QSslContext::sessionASN1() const | - | ||||||||||||
498 | { | - | ||||||||||||
499 | return m_sessionASN1; executed 465 times by 7 tests: return m_sessionASN1; Executed by:
| 465 | ||||||||||||
500 | } | - | ||||||||||||
501 | - | |||||||||||||
502 | void QSslContext::setSessionASN1(const QByteArray &session) | - | ||||||||||||
503 | { | - | ||||||||||||
504 | m_sessionASN1 = session; | - | ||||||||||||
505 | } executed 1 time by 1 test: end of block Executed by:
| 1 | ||||||||||||
506 | - | |||||||||||||
507 | int QSslContext::sessionTicketLifeTimeHint() const | - | ||||||||||||
508 | { | - | ||||||||||||
509 | return m_sessionTicketLifeTimeHint; executed 2 times by 1 test: return m_sessionTicketLifeTimeHint; Executed by:
| 2 | ||||||||||||
510 | } | - | ||||||||||||
511 | - | |||||||||||||
512 | QSslError::SslError QSslContext::error() const | - | ||||||||||||
513 | { | - | ||||||||||||
514 | return errorCode; executed 484 times by 7 tests: return errorCode; Executed by:
| 484 | ||||||||||||
515 | } | - | ||||||||||||
516 | - | |||||||||||||
517 | QString QSslContext::errorString() const | - | ||||||||||||
518 | { | - | ||||||||||||
519 | return errorStr; executed 8 times by 2 tests: return errorStr; Executed by:
| 8 | ||||||||||||
520 | } | - | ||||||||||||
521 | - | |||||||||||||
522 | QT_END_NAMESPACE | - | ||||||||||||
Source code | Switch to Preprocessed file |