485void QSslSocket::connectToHostEncrypted(
const QString &hostName, quint16 port, OpenMode mode, NetworkLayerProtocol protocol)
488 if (d->state == ConnectedState || d->state == ConnectingState) {
490 "QSslSocket::connectToHostEncrypted() called when already connecting/connected");
494 if (!supportsSsl()) {
495 qCWarning(lcSsl,
"QSslSocket::connectToHostEncrypted: TLS initialization failed");
496 d->setErrorAndEmit(QAbstractSocket::SslInternalError, tr(
"TLS initialization failed"));
500 if (!d->verifyProtocolSupported(
"QSslSocket::connectToHostEncrypted:"))
504 d->autoStartHandshake =
true;
505 d->initialized =
true;
509 connectToHost(hostName, port, mode, protocol);
523void QSslSocket::connectToHostEncrypted(
const QString &hostName, quint16 port,
524 const QString &sslPeerName, OpenMode mode,
525 NetworkLayerProtocol protocol)
528 if (d->state == ConnectedState || d->state == ConnectingState) {
530 "QSslSocket::connectToHostEncrypted() called when already connecting/connected");
534 if (!supportsSsl()) {
535 qCWarning(lcSsl,
"QSslSocket::connectToHostEncrypted: TLS initialization failed");
536 d->setErrorAndEmit(QAbstractSocket::SslInternalError, tr(
"TLS initialization failed"));
541 d->autoStartHandshake =
true;
542 d->initialized =
true;
543 d->verificationPeerName = sslPeerName;
547 connectToHost(hostName, port, mode, protocol);
562bool QSslSocket::setSocketDescriptor(qintptr socketDescriptor, SocketState state, OpenMode openMode)
565#ifdef QSSLSOCKET_DEBUG
566 qCDebug(lcSsl) <<
"QSslSocket::setSocketDescriptor(" << socketDescriptor <<
','
567 << state <<
',' << openMode <<
')';
570 d->createPlainSocket(openMode);
571 bool retVal = d->plainSocket->setSocketDescriptor(socketDescriptor, state, openMode);
572 d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();
573 d->setError(d->plainSocket->error(), d->plainSocket->errorString());
574 setSocketState(state);
575 setOpenMode(openMode);
576 setLocalPort(d->plainSocket->localPort());
577 setLocalAddress(d->plainSocket->localAddress());
578 setPeerPort(d->plainSocket->peerPort());
579 setPeerAddress(d->plainSocket->peerAddress());
580 setPeerName(d->plainSocket->peerName());
581 d->readChannelCount = d->plainSocket->readChannelCount();
582 d->writeChannelCount = d->plainSocket->writeChannelCount();
952void QSslSocket::setSslConfiguration(
const QSslConfiguration &configuration)
955 d->configuration.localCertificateChain = configuration.localCertificateChain();
956 d->configuration.privateKey = configuration.privateKey();
957 d->configuration.ciphers = configuration.ciphers();
958 d->configuration.ellipticCurves = configuration.ellipticCurves();
959 d->configuration.preSharedKeyIdentityHint = configuration.preSharedKeyIdentityHint();
960 d->configuration.dhParams = configuration.diffieHellmanParameters();
961 d->configuration.caCertificates = configuration.caCertificates();
962 d->configuration.peerVerifyDepth = configuration.peerVerifyDepth();
963 d->configuration.peerVerifyMode = configuration.peerVerifyMode();
964 d->configuration.protocol = configuration.protocol();
965 d->configuration.backendConfig = configuration.backendConfiguration();
966 d->configuration.sslOptions = configuration.d->sslOptions;
967 d->configuration.sslSession = configuration.sessionTicket();
968 d->configuration.sslSessionTicketLifeTimeHint = configuration.sessionTicketLifeTimeHint();
969 d->configuration.nextAllowedProtocols = configuration.allowedNextProtocols();
970 d->configuration.nextNegotiatedProtocol = configuration.nextNegotiatedProtocol();
971 d->configuration.nextProtocolNegotiationStatus = configuration.nextProtocolNegotiationStatus();
972 d->configuration.keyingMaterial = configuration.keyingMaterial();
974 d->configuration.ocspStaplingEnabled = configuration.ocspStaplingEnabled();
976#if QT_CONFIG(openssl)
977 d->configuration.reportFromCallback = configuration.handshakeMustInterruptOnError();
978 d->configuration.missingCertIsFatal = configuration.missingCertificateIsFatal();
983 if (!configuration.d->allowRootCertOnDemandLoading) {
984 d->allowRootCertOnDemandLoading =
false;
985 d->configuration.allowRootCertOnDemandLoading =
false;
1223void QSslSocket::setPrivateKey(
const QString &fileName, QSsl::KeyAlgorithm algorithm,
1224 QSsl::EncodingFormat format,
const QByteArray &passPhrase)
1226 QFile file(fileName);
1227 if (!file.open(QIODevice::ReadOnly)) {
1228 qCWarning(lcSsl,
"QSslSocket::setPrivateKey: Couldn't open file for reading");
1232 QSslKey key(file.readAll(), algorithm, format, QSsl::PrivateKey, passPhrase);
1234 qCWarning(lcSsl,
"QSslSocket::setPrivateKey: "
1235 "The specified file does not contain a valid key");
1240 d->configuration.privateKey = key;
1290bool QSslSocket::waitForEncrypted(
int msecs)
1293 if (!d->plainSocket || d->connectionEncrypted)
1295 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1297 if (!d->verifyProtocolSupported(
"QSslSocket::waitForEncrypted:"))
1300 QElapsedTimer stopWatch;
1303 if (d->plainSocket->state() != QAbstractSocket::ConnectedState) {
1305 if (!d->plainSocket->waitForConnected(msecs))
1309 while (!d->connectionEncrypted) {
1311 if (d->mode == UnencryptedMode)
1312 startClientEncryption();
1315 if (!d->plainSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed())))
1318 return d->connectionEncrypted;
1324bool QSslSocket::waitForReadyRead(
int msecs)
1327 if (!d->plainSocket)
1329 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1330 return d->plainSocket->waitForReadyRead(msecs);
1336 bool readyReadEmitted =
false;
1337 bool *previousReadyReadEmittedPointer = d->readyReadEmittedPointer;
1338 d->readyReadEmittedPointer = &readyReadEmitted;
1340 QElapsedTimer stopWatch;
1343 if (!d->connectionEncrypted) {
1345 if (!waitForEncrypted(msecs)) {
1346 d->readyReadEmittedPointer = previousReadyReadEmittedPointer;
1351 if (!d->writeBuffer.isEmpty()) {
1358 while (!readyReadEmitted &&
1359 d->plainSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
1362 d->readyReadEmittedPointer = previousReadyReadEmittedPointer;
1363 return readyReadEmitted;
1369bool QSslSocket::waitForBytesWritten(
int msecs)
1372 if (!d->plainSocket)
1374 if (d->mode == UnencryptedMode)
1375 return d->plainSocket->waitForBytesWritten(msecs);
1377 QElapsedTimer stopWatch;
1380 if (!d->connectionEncrypted) {
1382 if (!waitForEncrypted(msecs))
1385 if (!d->writeBuffer.isEmpty()) {
1390 return d->plainSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed()));
1400bool QSslSocket::waitForDisconnected(
int msecs)
1405 if (state() == UnconnectedState) {
1406 qCWarning(lcSsl,
"QSslSocket::waitForDisconnected() is not allowed in UnconnectedState");
1410 if (!d->plainSocket)
1413 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1414 return d->plainSocket->waitForDisconnected(msecs);
1416 QElapsedTimer stopWatch;
1419 if (!d->connectionEncrypted) {
1421 if (!waitForEncrypted(msecs))
1426 if (!d->writeBuffer.isEmpty())
1432 if (state() == UnconnectedState)
1435 bool retVal = d->plainSocket->waitForDisconnected(qt_subtract_from_timeout(msecs, stopWatch.elapsed()));
1437 setSocketState(d->plainSocket->state());
1438 d->setError(d->plainSocket->error(), d->plainSocket->errorString());
1697void QSslSocket::startClientEncryption()
1700 if (d->mode != UnencryptedMode) {
1702 "QSslSocket::startClientEncryption: cannot start handshake on non-plain connection");
1705 if (state() != ConnectedState) {
1707 "QSslSocket::startClientEncryption: cannot start handshake when not connected");
1711 if (!supportsSsl()) {
1712 qCWarning(lcSsl,
"QSslSocket::startClientEncryption: TLS initialization failed");
1713 d->setErrorAndEmit(QAbstractSocket::SslInternalError, tr(
"TLS initialization failed"));
1717 if (!d->verifyProtocolSupported(
"QSslSocket::startClientEncryption:"))
1720#ifdef QSSLSOCKET_DEBUG
1721 qCDebug(lcSsl) <<
"QSslSocket::startClientEncryption()";
1723 d->mode = SslClientMode;
1724 emit modeChanged(d->mode);
1725 d->startClientEncryption();
1850void QSslSocket::connectToHost(
const QString &hostName, quint16 port, OpenMode openMode, NetworkLayerProtocol protocol)
1853 d->preferredNetworkLayerProtocol = protocol;
1854 if (!d->initialized)
1856 d->initialized =
false;
1858#ifdef QSSLSOCKET_DEBUG
1859 qCDebug(lcSsl) <<
"QSslSocket::connectToHost("
1860 << hostName <<
',' << port <<
',' << openMode <<
')';
1862 if (!d->plainSocket) {
1863#ifdef QSSLSOCKET_DEBUG
1864 qCDebug(lcSsl) <<
"\tcreating internal plain socket";
1866 d->createPlainSocket(openMode);
1868#ifndef QT_NO_NETWORKPROXY
1869 d->plainSocket->setProtocolTag(d->protocolTag);
1870 d->plainSocket->setProxy(proxy());
1872 QIODevice::open(openMode);
1873 d->readChannelCount = d->writeChannelCount = 0;
1874 d->plainSocket->connectToHost(hostName, port, openMode, d->preferredNetworkLayerProtocol);
1875 d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();
1881void QSslSocket::disconnectFromHost()
1884#ifdef QSSLSOCKET_DEBUG
1885 qCDebug(lcSsl) <<
"QSslSocket::disconnectFromHost()";
1887 if (!d->plainSocket)
1889 if (d->state == UnconnectedState)
1891 if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
1892 d->plainSocket->disconnectFromHost();
1895 if (d->state <= ConnectingState) {
1896 d->pendingClose =
true;
1901 if (
auto *backend = d->backend.get())
1902 backend->cancelCAFetch();
1905 if (d->state != ClosingState) {
1906 d->state = ClosingState;
1907 emit stateChanged(d->state);
1910 if (!d->writeBuffer.isEmpty()) {
1911 d->pendingClose =
true;
1915 if (d->mode == UnencryptedMode) {
1916 d->plainSocket->disconnectFromHost();
1918 d->disconnectFromHost();
1925qint64 QSslSocket::readData(
char *data, qint64 maxlen)
1928 qint64 readBytes = 0;
1930 if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
1931 readBytes = d->plainSocket->read(data, maxlen);
1932#ifdef QSSLSOCKET_DEBUG
1933 qCDebug(lcSsl) <<
"QSslSocket::readData(" << (
void *)data <<
',' << maxlen <<
") =="
1938 if (d->plainSocket->bytesAvailable() || d->hasUndecryptedData())
1939 QMetaObject::invokeMethod(
this,
"_q_flushReadBuffer", Qt::QueuedConnection);
1940 else if (d->state != QAbstractSocket::ConnectedState)
1941 return maxlen ? qint64(-1) : qint64(0);
1975QSslSocketPrivate::QSslSocketPrivate()
1976 : initialized(
false)
1977 , mode(QSslSocket::UnencryptedMode)
1978 , autoStartHandshake(
false)
1979 , connectionEncrypted(
false)
1980 , ignoreAllSslErrors(
false)
1981 , readyReadEmittedPointer(
nullptr)
1982 , allowRootCertOnDemandLoading(
true)
1983 , plainSocket(
nullptr)
1985 , flushTriggered(
false)
1987 QSslConfigurationPrivate::deepCopyDefaultConfiguration(&configuration);
1990 if (!configuration.allowRootCertOnDemandLoading)
1991 allowRootCertOnDemandLoading =
false;
1993 const auto *tlsBackend = tlsBackendInUse();
1995 qCWarning(lcSsl,
"No TLS backend is available");
1998 backend.reset(tlsBackend->createTlsCryptograph());
1999 if (!backend.get()) {
2000 qCWarning(lcSsl) <<
"The backend named" << tlsBackend->backendName()
2001 <<
"does not support TLS";
2215void QSslSocketPrivate::setDefaultCaCertificates(
const QList<QSslCertificate> &certs)
2217 QSslSocketPrivate::ensureInitialized();
2218 QMutexLocker locker(&globalData()->mutex);
2219 globalData()->config.detach();
2220 globalData()->config->caCertificates = certs;
2221 globalData()->dtlsConfig.detach();
2222 globalData()->dtlsConfig->caCertificates = certs;
2225 s_loadRootCertsOnDemand =
false;
2231void QSslSocketPrivate::addDefaultCaCertificate(
const QSslCertificate &cert)
2233 QSslSocketPrivate::ensureInitialized();
2234 QMutexLocker locker(&globalData()->mutex);
2235 if (globalData()->config->caCertificates.contains(cert))
2237 globalData()->config.detach();
2238 globalData()->config->caCertificates += cert;
2239 globalData()->dtlsConfig.detach();
2240 globalData()->dtlsConfig->caCertificates += cert;
2246void QSslSocketPrivate::addDefaultCaCertificates(
const QList<QSslCertificate> &certs)
2248 QSslSocketPrivate::ensureInitialized();
2249 QMutexLocker locker(&globalData()->mutex);
2250 globalData()->config.detach();
2251 globalData()->config->caCertificates += certs;
2252 globalData()->dtlsConfig.detach();
2253 globalData()->dtlsConfig->caCertificates += certs;
2282void QSslConfigurationPrivate::deepCopyDefaultConfiguration(QSslConfigurationPrivate *ptr)
2284 QSslSocketPrivate::ensureInitialized();
2285 QMutexLocker locker(&globalData()->mutex);
2286 const QSslConfigurationPrivate *global = globalData()->config.constData();
2291 ptr->ref.storeRelaxed(1);
2292 ptr->peerCertificate = global->peerCertificate;
2293 ptr->peerCertificateChain = global->peerCertificateChain;
2294 ptr->localCertificateChain = global->localCertificateChain;
2295 ptr->privateKey = global->privateKey;
2296 ptr->sessionCipher = global->sessionCipher;
2297 ptr->sessionProtocol = global->sessionProtocol;
2298 ptr->ciphers = global->ciphers;
2299 ptr->caCertificates = global->caCertificates;
2300 ptr->allowRootCertOnDemandLoading = global->allowRootCertOnDemandLoading;
2301 ptr->protocol = global->protocol;
2302 ptr->peerVerifyMode = global->peerVerifyMode;
2303 ptr->peerVerifyDepth = global->peerVerifyDepth;
2304 ptr->sslOptions = global->sslOptions;
2305 ptr->ellipticCurves = global->ellipticCurves;
2306 ptr->backendConfig = global->backendConfig;
2308 ptr->dtlsCookieEnabled = global->dtlsCookieEnabled;
2311 ptr->ocspStaplingEnabled = global->ocspStaplingEnabled;
2313#if QT_CONFIG(openssl)
2314 ptr->reportFromCallback = global->reportFromCallback;
2315 ptr->missingCertIsFatal = global->missingCertIsFatal;
2333void QSslConfigurationPrivate::setDefaultDtlsConfiguration(
const QSslConfiguration &configuration)
2335 QSslSocketPrivate::ensureInitialized();
2336 QMutexLocker locker(&globalData()->mutex);
2337 if (globalData()->dtlsConfig == configuration.d)
2340 globalData()->dtlsConfig =
const_cast<QSslConfigurationPrivate*>(configuration.d.constData());
2346void QSslSocketPrivate::createPlainSocket(QIODevice::OpenMode openMode)
2349 q->setOpenMode(openMode);
2350 q->setSocketState(QAbstractSocket::UnconnectedState);
2351 q->setSocketError(QAbstractSocket::UnknownSocketError);
2353 q->setLocalAddress(QHostAddress());
2355 q->setPeerAddress(QHostAddress());
2356 q->setPeerName(QString());
2358 plainSocket =
new QTcpSocket(q);
2359 q->connect(plainSocket, SIGNAL(connected()),
2360 q, SLOT(_q_connectedSlot()),
2361 Qt::DirectConnection);
2362 q->connect(plainSocket, SIGNAL(hostFound()),
2363 q, SLOT(_q_hostFoundSlot()),
2364 Qt::DirectConnection);
2365 q->connect(plainSocket, SIGNAL(disconnected()),
2366 q, SLOT(_q_disconnectedSlot()),
2367 Qt::DirectConnection);
2368 q->connect(plainSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
2369 q, SLOT(_q_stateChangedSlot(QAbstractSocket::SocketState)),
2370 Qt::DirectConnection);
2371 q->connect(plainSocket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)),
2372 q, SLOT(_q_errorSlot(QAbstractSocket::SocketError)),
2373 Qt::DirectConnection);
2374 q->connect(plainSocket, SIGNAL(readyRead()),
2375 q, SLOT(_q_readyReadSlot()),
2376 Qt::DirectConnection);
2377 q->connect(plainSocket, SIGNAL(channelReadyRead(
int)),
2378 q, SLOT(_q_channelReadyReadSlot(
int)),
2379 Qt::DirectConnection);
2380 q->connect(plainSocket, SIGNAL(bytesWritten(qint64)),
2381 q, SLOT(_q_bytesWrittenSlot(qint64)),
2382 Qt::DirectConnection);
2383 q->connect(plainSocket, SIGNAL(channelBytesWritten(
int,qint64)),
2384 q, SLOT(_q_channelBytesWrittenSlot(
int,qint64)),
2385 Qt::DirectConnection);
2386 q->connect(plainSocket, SIGNAL(readChannelFinished()),
2387 q, SLOT(_q_readChannelFinishedSlot()),
2388 Qt::DirectConnection);
2389#ifndef QT_NO_NETWORKPROXY
2390 q->connect(plainSocket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
2391 q, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
2395 writeBuffer.clear();
2396 connectionEncrypted =
false;
2397 configuration.peerCertificate.clear();
2398 configuration.peerCertificateChain.clear();
2399 mode = QSslSocket::UnencryptedMode;
2400 q->setReadBufferSize(readBufferMaxSize);
2427bool QSslSocketPrivate::bind(
const QHostAddress &address, quint16 port, QAbstractSocket::BindMode mode,
2428 const QNetworkInterface *iface)
2434 initialized =
false;
2436#ifdef QSSLSOCKET_DEBUG
2437 qCDebug(lcSsl) <<
"QSslSocket::bind(" << address <<
',' << port <<
',' << mode <<
')';
2440#ifdef QSSLSOCKET_DEBUG
2441 qCDebug(lcSsl) <<
"\tcreating internal plain socket";
2443 createPlainSocket(QIODevice::ReadWrite);
2445 bool ret = plainSocket->bind(address, port, mode);
2446 localPort = plainSocket->localPort();
2447 localAddress = plainSocket->localAddress();
2448 cachedSocketDescriptor = plainSocket->socketDescriptor();
2449 readChannelCount = writeChannelCount = 0;
2456void QSslSocketPrivate::_q_connectedSlot()
2459 q->setLocalPort(plainSocket->localPort());
2460 q->setLocalAddress(plainSocket->localAddress());
2461 q->setPeerPort(plainSocket->peerPort());
2462 q->setPeerAddress(plainSocket->peerAddress());
2463 q->setPeerName(plainSocket->peerName());
2464 cachedSocketDescriptor = plainSocket->socketDescriptor();
2465 readChannelCount = plainSocket->readChannelCount();
2466 writeChannelCount = plainSocket->writeChannelCount();
2468#ifdef QSSLSOCKET_DEBUG
2469 qCDebug(lcSsl) <<
"QSslSocket::_q_connectedSlot()";
2470 qCDebug(lcSsl) <<
"\tstate =" << q->state();
2471 qCDebug(lcSsl) <<
"\tpeer =" << q->peerName() << q->peerAddress() << q->peerPort();
2472 qCDebug(lcSsl) <<
"\tlocal =" << QHostInfo::fromName(q->localAddress().toString()).hostName()
2473 << q->localAddress() << q->localPort();
2476 if (autoStartHandshake)
2477 q->startClientEncryption();
2479 emit q->connected();
2481 if (pendingClose && !autoStartHandshake) {
2482 pendingClose =
false;
2483 q->disconnectFromHost();
3013bool QSslSocketPrivate::isMatchingHostname(
const QSslCertificate &cert,
const QString &peerName)
3015 QHostAddress hostAddress(peerName);
3016 if (!hostAddress.isNull()) {
3017 const auto subjectAlternativeNames = cert.subjectAlternativeNames();
3018 const auto ipAddresses = subjectAlternativeNames.equal_range(QSsl::AlternativeNameEntryType::IpAddressEntry);
3020 for (
auto it = ipAddresses.first; it != ipAddresses.second; it++) {
3021 if (QHostAddress(*it).isEqual(hostAddress, QHostAddress::StrictConversion))
3026 const QString lowerPeerName = QString::fromLatin1(QUrl::toAce(peerName));
3027 const QStringList commonNames = cert.subjectInfo(QSslCertificate::CommonName);
3029 for (
const QString &commonName : commonNames) {
3030 if (isMatchingHostname(commonName, lowerPeerName))
3034 const auto subjectAlternativeNames = cert.subjectAlternativeNames();
3035 const auto altNames = subjectAlternativeNames.equal_range(QSsl::DnsEntry);
3036 for (
auto it = altNames.first; it != altNames.second; ++it) {
3037 if (isMatchingHostname(*it, lowerPeerName))
3048bool QSslSocketPrivate::isMatchingHostname(
const QString &cn,
const QString &hostname)
3050 qsizetype wildcard = cn.indexOf(u'*');
3054 return QLatin1StringView(QUrl::toAce(cn)) == hostname;
3056 qsizetype firstCnDot = cn.indexOf(u'.');
3057 qsizetype secondCnDot = cn.indexOf(u'.', firstCnDot+1);
3060 if ((-1 == secondCnDot) || (secondCnDot+1 >= cn.size()))
3064 if (wildcard+1 != firstCnDot)
3068 if (cn.lastIndexOf(u'*') != wildcard)
3073 if (cn.startsWith(
"xn--"_L1, Qt::CaseInsensitive))
3077 if (wildcard && QStringView{hostname}.left(wildcard).compare(QStringView{cn}.left(wildcard), Qt::CaseInsensitive) != 0)
3081 qsizetype hnDot = hostname.indexOf(u'.');
3082 if (QStringView{hostname}.mid(hnDot + 1) != QStringView{cn}.mid(firstCnDot + 1)
3083 && QStringView{hostname}.mid(hnDot + 1) != QLatin1StringView(QUrl::toAce(cn.mid(firstCnDot + 1)))) {
3088 QHostAddress addr(hostname);
3099QTlsBackend *QSslSocketPrivate::tlsBackendInUse()
3101 const QMutexLocker locker(&backendMutex);
3105 if (!activeBackendName.size())
3106 activeBackendName = QTlsBackend::defaultBackendName();
3108 if (!activeBackendName.size()) {
3109 qCWarning(lcSsl,
"No functional TLS backend was found");
3113 tlsBackend = QTlsBackend::findBackend(activeBackendName);
3115 QObject::connect(tlsBackend, &QObject::destroyed, tlsBackend, [] {
3116 const QMutexLocker locker(&backendMutex);
3117 tlsBackend =
nullptr;
3119 Qt::DirectConnection);