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();
973 d->configuration.ocspStaplingEnabled = configuration.ocspStaplingEnabled();
975#if QT_CONFIG(openssl)
976 d->configuration.reportFromCallback = configuration.handshakeMustInterruptOnError();
977 d->configuration.missingCertIsFatal = configuration.missingCertificateIsFatal();
982 if (!configuration.d->allowRootCertOnDemandLoading) {
983 d->allowRootCertOnDemandLoading =
false;
984 d->configuration.allowRootCertOnDemandLoading =
false;
1222void QSslSocket::setPrivateKey(
const QString &fileName, QSsl::KeyAlgorithm algorithm,
1223 QSsl::EncodingFormat format,
const QByteArray &passPhrase)
1225 QFile file(fileName);
1226 if (!file.open(QIODevice::ReadOnly)) {
1227 qCWarning(lcSsl,
"QSslSocket::setPrivateKey: Couldn't open file for reading");
1231 QSslKey key(file.readAll(), algorithm, format, QSsl::PrivateKey, passPhrase);
1233 qCWarning(lcSsl,
"QSslSocket::setPrivateKey: "
1234 "The specified file does not contain a valid key");
1239 d->configuration.privateKey = key;
1289bool QSslSocket::waitForEncrypted(
int msecs)
1292 if (!d->plainSocket || d->connectionEncrypted)
1294 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1296 if (!d->verifyProtocolSupported(
"QSslSocket::waitForEncrypted:"))
1299 QElapsedTimer stopWatch;
1302 if (d->plainSocket->state() != QAbstractSocket::ConnectedState) {
1304 if (!d->plainSocket->waitForConnected(msecs))
1308 while (!d->connectionEncrypted) {
1310 if (d->mode == UnencryptedMode)
1311 startClientEncryption();
1314 if (!d->plainSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed())))
1317 return d->connectionEncrypted;
1323bool QSslSocket::waitForReadyRead(
int msecs)
1326 if (!d->plainSocket)
1328 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1329 return d->plainSocket->waitForReadyRead(msecs);
1335 bool readyReadEmitted =
false;
1336 bool *previousReadyReadEmittedPointer = d->readyReadEmittedPointer;
1337 d->readyReadEmittedPointer = &readyReadEmitted;
1339 QElapsedTimer stopWatch;
1342 if (!d->connectionEncrypted) {
1344 if (!waitForEncrypted(msecs)) {
1345 d->readyReadEmittedPointer = previousReadyReadEmittedPointer;
1350 if (!d->writeBuffer.isEmpty()) {
1357 while (!readyReadEmitted &&
1358 d->plainSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
1361 d->readyReadEmittedPointer = previousReadyReadEmittedPointer;
1362 return readyReadEmitted;
1368bool QSslSocket::waitForBytesWritten(
int msecs)
1371 if (!d->plainSocket)
1373 if (d->mode == UnencryptedMode)
1374 return d->plainSocket->waitForBytesWritten(msecs);
1376 QElapsedTimer stopWatch;
1379 if (!d->connectionEncrypted) {
1381 if (!waitForEncrypted(msecs))
1384 if (!d->writeBuffer.isEmpty()) {
1389 return d->plainSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed()));
1399bool QSslSocket::waitForDisconnected(
int msecs)
1404 if (state() == UnconnectedState) {
1405 qCWarning(lcSsl,
"QSslSocket::waitForDisconnected() is not allowed in UnconnectedState");
1409 if (!d->plainSocket)
1412 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1413 return d->plainSocket->waitForDisconnected(msecs);
1415 QElapsedTimer stopWatch;
1418 if (!d->connectionEncrypted) {
1420 if (!waitForEncrypted(msecs))
1425 if (!d->writeBuffer.isEmpty())
1431 if (state() == UnconnectedState)
1434 bool retVal = d->plainSocket->waitForDisconnected(qt_subtract_from_timeout(msecs, stopWatch.elapsed()));
1436 setSocketState(d->plainSocket->state());
1437 d->setError(d->plainSocket->error(), d->plainSocket->errorString());
1696void QSslSocket::startClientEncryption()
1699 if (d->mode != UnencryptedMode) {
1701 "QSslSocket::startClientEncryption: cannot start handshake on non-plain connection");
1704 if (state() != ConnectedState) {
1706 "QSslSocket::startClientEncryption: cannot start handshake when not connected");
1710 if (!supportsSsl()) {
1711 qCWarning(lcSsl,
"QSslSocket::startClientEncryption: TLS initialization failed");
1712 d->setErrorAndEmit(QAbstractSocket::SslInternalError, tr(
"TLS initialization failed"));
1716 if (!d->verifyProtocolSupported(
"QSslSocket::startClientEncryption:"))
1719#ifdef QSSLSOCKET_DEBUG
1720 qCDebug(lcSsl) <<
"QSslSocket::startClientEncryption()";
1722 d->mode = SslClientMode;
1723 emit modeChanged(d->mode);
1724 d->startClientEncryption();
1849void QSslSocket::connectToHost(
const QString &hostName, quint16 port, OpenMode openMode, NetworkLayerProtocol protocol)
1852 d->preferredNetworkLayerProtocol = protocol;
1853 if (!d->initialized)
1855 d->initialized =
false;
1857#ifdef QSSLSOCKET_DEBUG
1858 qCDebug(lcSsl) <<
"QSslSocket::connectToHost("
1859 << hostName <<
',' << port <<
',' << openMode <<
')';
1861 if (!d->plainSocket) {
1862#ifdef QSSLSOCKET_DEBUG
1863 qCDebug(lcSsl) <<
"\tcreating internal plain socket";
1865 d->createPlainSocket(openMode);
1867#ifndef QT_NO_NETWORKPROXY
1868 d->plainSocket->setProtocolTag(d->protocolTag);
1869 d->plainSocket->setProxy(proxy());
1871 QIODevice::open(openMode);
1872 d->readChannelCount = d->writeChannelCount = 0;
1873 d->plainSocket->connectToHost(hostName, port, openMode, d->preferredNetworkLayerProtocol);
1874 d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();
1880void QSslSocket::disconnectFromHost()
1883#ifdef QSSLSOCKET_DEBUG
1884 qCDebug(lcSsl) <<
"QSslSocket::disconnectFromHost()";
1886 if (!d->plainSocket)
1888 if (d->state == UnconnectedState)
1890 if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
1891 d->plainSocket->disconnectFromHost();
1894 if (d->state <= ConnectingState) {
1895 d->pendingClose =
true;
1900 if (
auto *backend = d->backend.get())
1901 backend->cancelCAFetch();
1904 if (d->state != ClosingState) {
1905 d->state = ClosingState;
1906 emit stateChanged(d->state);
1909 if (!d->writeBuffer.isEmpty()) {
1910 d->pendingClose =
true;
1914 if (d->mode == UnencryptedMode) {
1915 d->plainSocket->disconnectFromHost();
1917 d->disconnectFromHost();
1924qint64 QSslSocket::readData(
char *data, qint64 maxlen)
1927 qint64 readBytes = 0;
1929 if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
1930 readBytes = d->plainSocket->read(data, maxlen);
1931#ifdef QSSLSOCKET_DEBUG
1932 qCDebug(lcSsl) <<
"QSslSocket::readData(" << (
void *)data <<
',' << maxlen <<
") =="
1937 if (d->plainSocket->bytesAvailable() || d->hasUndecryptedData())
1938 QMetaObject::invokeMethod(
this,
"_q_flushReadBuffer", Qt::QueuedConnection);
1939 else if (d->state != QAbstractSocket::ConnectedState)
1940 return maxlen ? qint64(-1) : qint64(0);
1974QSslSocketPrivate::QSslSocketPrivate()
1975 : initialized(
false)
1976 , mode(QSslSocket::UnencryptedMode)
1977 , autoStartHandshake(
false)
1978 , connectionEncrypted(
false)
1979 , ignoreAllSslErrors(
false)
1980 , readyReadEmittedPointer(
nullptr)
1981 , allowRootCertOnDemandLoading(
true)
1982 , plainSocket(
nullptr)
1984 , flushTriggered(
false)
1986 QSslConfigurationPrivate::deepCopyDefaultConfiguration(&configuration);
1989 if (!configuration.allowRootCertOnDemandLoading)
1990 allowRootCertOnDemandLoading =
false;
1992 const auto *tlsBackend = tlsBackendInUse();
1994 qCWarning(lcSsl,
"No TLS backend is available");
1997 backend.reset(tlsBackend->createTlsCryptograph());
1998 if (!backend.get()) {
1999 qCWarning(lcSsl) <<
"The backend named" << tlsBackend->backendName()
2000 <<
"does not support TLS";
2214void QSslSocketPrivate::setDefaultCaCertificates(
const QList<QSslCertificate> &certs)
2216 QSslSocketPrivate::ensureInitialized();
2217 QMutexLocker locker(&globalData()->mutex);
2218 globalData()->config.detach();
2219 globalData()->config->caCertificates = certs;
2220 globalData()->dtlsConfig.detach();
2221 globalData()->dtlsConfig->caCertificates = certs;
2224 s_loadRootCertsOnDemand =
false;
2230void QSslSocketPrivate::addDefaultCaCertificate(
const QSslCertificate &cert)
2232 QSslSocketPrivate::ensureInitialized();
2233 QMutexLocker locker(&globalData()->mutex);
2234 if (globalData()->config->caCertificates.contains(cert))
2236 globalData()->config.detach();
2237 globalData()->config->caCertificates += cert;
2238 globalData()->dtlsConfig.detach();
2239 globalData()->dtlsConfig->caCertificates += cert;
2245void QSslSocketPrivate::addDefaultCaCertificates(
const QList<QSslCertificate> &certs)
2247 QSslSocketPrivate::ensureInitialized();
2248 QMutexLocker locker(&globalData()->mutex);
2249 globalData()->config.detach();
2250 globalData()->config->caCertificates += certs;
2251 globalData()->dtlsConfig.detach();
2252 globalData()->dtlsConfig->caCertificates += certs;
2281void QSslConfigurationPrivate::deepCopyDefaultConfiguration(QSslConfigurationPrivate *ptr)
2283 QSslSocketPrivate::ensureInitialized();
2284 QMutexLocker locker(&globalData()->mutex);
2285 const QSslConfigurationPrivate *global = globalData()->config.constData();
2290 ptr->ref.storeRelaxed(1);
2291 ptr->peerCertificate = global->peerCertificate;
2292 ptr->peerCertificateChain = global->peerCertificateChain;
2293 ptr->localCertificateChain = global->localCertificateChain;
2294 ptr->privateKey = global->privateKey;
2295 ptr->sessionCipher = global->sessionCipher;
2296 ptr->sessionProtocol = global->sessionProtocol;
2297 ptr->ciphers = global->ciphers;
2298 ptr->caCertificates = global->caCertificates;
2299 ptr->allowRootCertOnDemandLoading = global->allowRootCertOnDemandLoading;
2300 ptr->protocol = global->protocol;
2301 ptr->peerVerifyMode = global->peerVerifyMode;
2302 ptr->peerVerifyDepth = global->peerVerifyDepth;
2303 ptr->sslOptions = global->sslOptions;
2304 ptr->ellipticCurves = global->ellipticCurves;
2305 ptr->backendConfig = global->backendConfig;
2307 ptr->dtlsCookieEnabled = global->dtlsCookieEnabled;
2310 ptr->ocspStaplingEnabled = global->ocspStaplingEnabled;
2312#if QT_CONFIG(openssl)
2313 ptr->reportFromCallback = global->reportFromCallback;
2314 ptr->missingCertIsFatal = global->missingCertIsFatal;
2332void QSslConfigurationPrivate::setDefaultDtlsConfiguration(
const QSslConfiguration &configuration)
2334 QSslSocketPrivate::ensureInitialized();
2335 QMutexLocker locker(&globalData()->mutex);
2336 if (globalData()->dtlsConfig == configuration.d)
2339 globalData()->dtlsConfig =
const_cast<QSslConfigurationPrivate*>(configuration.d.constData());
2345void QSslSocketPrivate::createPlainSocket(QIODevice::OpenMode openMode)
2348 q->setOpenMode(openMode);
2349 q->setSocketState(QAbstractSocket::UnconnectedState);
2350 q->setSocketError(QAbstractSocket::UnknownSocketError);
2352 q->setLocalAddress(QHostAddress());
2354 q->setPeerAddress(QHostAddress());
2355 q->setPeerName(QString());
2357 plainSocket =
new QTcpSocket(q);
2358 q->connect(plainSocket, SIGNAL(connected()),
2359 q, SLOT(_q_connectedSlot()),
2360 Qt::DirectConnection);
2361 q->connect(plainSocket, SIGNAL(hostFound()),
2362 q, SLOT(_q_hostFoundSlot()),
2363 Qt::DirectConnection);
2364 q->connect(plainSocket, SIGNAL(disconnected()),
2365 q, SLOT(_q_disconnectedSlot()),
2366 Qt::DirectConnection);
2367 q->connect(plainSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
2368 q, SLOT(_q_stateChangedSlot(QAbstractSocket::SocketState)),
2369 Qt::DirectConnection);
2370 q->connect(plainSocket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)),
2371 q, SLOT(_q_errorSlot(QAbstractSocket::SocketError)),
2372 Qt::DirectConnection);
2373 q->connect(plainSocket, SIGNAL(readyRead()),
2374 q, SLOT(_q_readyReadSlot()),
2375 Qt::DirectConnection);
2376 q->connect(plainSocket, SIGNAL(channelReadyRead(
int)),
2377 q, SLOT(_q_channelReadyReadSlot(
int)),
2378 Qt::DirectConnection);
2379 q->connect(plainSocket, SIGNAL(bytesWritten(qint64)),
2380 q, SLOT(_q_bytesWrittenSlot(qint64)),
2381 Qt::DirectConnection);
2382 q->connect(plainSocket, SIGNAL(channelBytesWritten(
int,qint64)),
2383 q, SLOT(_q_channelBytesWrittenSlot(
int,qint64)),
2384 Qt::DirectConnection);
2385 q->connect(plainSocket, SIGNAL(readChannelFinished()),
2386 q, SLOT(_q_readChannelFinishedSlot()),
2387 Qt::DirectConnection);
2388#ifndef QT_NO_NETWORKPROXY
2389 q->connect(plainSocket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
2390 q, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
2394 writeBuffer.clear();
2395 connectionEncrypted =
false;
2396 configuration.peerCertificate.clear();
2397 configuration.peerCertificateChain.clear();
2398 mode = QSslSocket::UnencryptedMode;
2399 q->setReadBufferSize(readBufferMaxSize);
2426bool QSslSocketPrivate::bind(
const QHostAddress &address, quint16 port, QAbstractSocket::BindMode mode,
2427 const QNetworkInterface *iface)
2433 initialized =
false;
2435#ifdef QSSLSOCKET_DEBUG
2436 qCDebug(lcSsl) <<
"QSslSocket::bind(" << address <<
',' << port <<
',' << mode <<
')';
2439#ifdef QSSLSOCKET_DEBUG
2440 qCDebug(lcSsl) <<
"\tcreating internal plain socket";
2442 createPlainSocket(QIODevice::ReadWrite);
2444 bool ret = plainSocket->bind(address, port, mode);
2445 localPort = plainSocket->localPort();
2446 localAddress = plainSocket->localAddress();
2447 cachedSocketDescriptor = plainSocket->socketDescriptor();
2448 readChannelCount = writeChannelCount = 0;
2455void QSslSocketPrivate::_q_connectedSlot()
2458 q->setLocalPort(plainSocket->localPort());
2459 q->setLocalAddress(plainSocket->localAddress());
2460 q->setPeerPort(plainSocket->peerPort());
2461 q->setPeerAddress(plainSocket->peerAddress());
2462 q->setPeerName(plainSocket->peerName());
2463 cachedSocketDescriptor = plainSocket->socketDescriptor();
2464 readChannelCount = plainSocket->readChannelCount();
2465 writeChannelCount = plainSocket->writeChannelCount();
2467#ifdef QSSLSOCKET_DEBUG
2468 qCDebug(lcSsl) <<
"QSslSocket::_q_connectedSlot()";
2469 qCDebug(lcSsl) <<
"\tstate =" << q->state();
2470 qCDebug(lcSsl) <<
"\tpeer =" << q->peerName() << q->peerAddress() << q->peerPort();
2471 qCDebug(lcSsl) <<
"\tlocal =" << QHostInfo::fromName(q->localAddress().toString()).hostName()
2472 << q->localAddress() << q->localPort();
2475 if (autoStartHandshake)
2476 q->startClientEncryption();
2478 emit q->connected();
2480 if (pendingClose && !autoStartHandshake) {
2481 pendingClose =
false;
2482 q->disconnectFromHost();
3012bool QSslSocketPrivate::isMatchingHostname(
const QSslCertificate &cert,
const QString &peerName)
3014 QHostAddress hostAddress(peerName);
3015 if (!hostAddress.isNull()) {
3016 const auto subjectAlternativeNames = cert.subjectAlternativeNames();
3017 const auto ipAddresses = subjectAlternativeNames.equal_range(QSsl::AlternativeNameEntryType::IpAddressEntry);
3019 for (
auto it = ipAddresses.first; it != ipAddresses.second; it++) {
3020 if (QHostAddress(*it).isEqual(hostAddress, QHostAddress::StrictConversion))
3025 const QString lowerPeerName = QString::fromLatin1(QUrl::toAce(peerName));
3026 const QStringList commonNames = cert.subjectInfo(QSslCertificate::CommonName);
3028 for (
const QString &commonName : commonNames) {
3029 if (isMatchingHostname(commonName, lowerPeerName))
3033 const auto subjectAlternativeNames = cert.subjectAlternativeNames();
3034 const auto altNames = subjectAlternativeNames.equal_range(QSsl::DnsEntry);
3035 for (
auto it = altNames.first; it != altNames.second; ++it) {
3036 if (isMatchingHostname(*it, lowerPeerName))
3047bool QSslSocketPrivate::isMatchingHostname(
const QString &cn,
const QString &hostname)
3049 qsizetype wildcard = cn.indexOf(u'*');
3053 return QLatin1StringView(QUrl::toAce(cn)) == hostname;
3055 qsizetype firstCnDot = cn.indexOf(u'.');
3056 qsizetype secondCnDot = cn.indexOf(u'.', firstCnDot+1);
3059 if ((-1 == secondCnDot) || (secondCnDot+1 >= cn.size()))
3063 if (wildcard+1 != firstCnDot)
3067 if (cn.lastIndexOf(u'*') != wildcard)
3072 if (cn.startsWith(
"xn--"_L1, Qt::CaseInsensitive))
3076 if (wildcard && QStringView{hostname}.left(wildcard).compare(QStringView{cn}.left(wildcard), Qt::CaseInsensitive) != 0)
3080 qsizetype hnDot = hostname.indexOf(u'.');
3081 if (QStringView{hostname}.mid(hnDot + 1) != QStringView{cn}.mid(firstCnDot + 1)
3082 && QStringView{hostname}.mid(hnDot + 1) != QLatin1StringView(QUrl::toAce(cn.mid(firstCnDot + 1)))) {
3087 QHostAddress addr(hostname);
3098QTlsBackend *QSslSocketPrivate::tlsBackendInUse()
3100 const QMutexLocker locker(&backendMutex);
3104 if (!activeBackendName.size())
3105 activeBackendName = QTlsBackend::defaultBackendName();
3107 if (!activeBackendName.size()) {
3108 qCWarning(lcSsl,
"No functional TLS backend was found");
3112 tlsBackend = QTlsBackend::findBackend(activeBackendName);
3114 QObject::connect(tlsBackend, &QObject::destroyed, tlsBackend, [] {
3115 const QMutexLocker locker(&backendMutex);
3116 tlsBackend =
nullptr;
3118 Qt::DirectConnection);