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