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();
959void QSslSocket::setSslConfiguration(
const QSslConfiguration &configuration)
962 d->configuration.localCertificateChain = configuration.localCertificateChain();
963 d->configuration.privateKey = configuration.privateKey();
964 d->configuration.ciphers = configuration.ciphers();
965 d->configuration.ellipticCurves = configuration.ellipticCurves();
966 d->configuration.preSharedKeyIdentityHint = configuration.preSharedKeyIdentityHint();
967 d->configuration.dhParams = configuration.diffieHellmanParameters();
968 d->configuration.caCertificates = configuration.caCertificates();
969 d->configuration.peerVerifyDepth = configuration.peerVerifyDepth();
970 d->configuration.peerVerifyMode = configuration.peerVerifyMode();
971 d->configuration.protocol = configuration.protocol();
972 d->configuration.backendConfig = configuration.backendConfiguration();
973 d->configuration.sslOptions = configuration.d->sslOptions;
974 d->configuration.sslSession = configuration.sessionTicket();
975 d->configuration.sslSessionTicketLifeTimeHint = configuration.sessionTicketLifeTimeHint();
976 d->configuration.nextAllowedProtocols = configuration.allowedNextProtocols();
977 d->configuration.nextNegotiatedProtocol = configuration.nextNegotiatedProtocol();
978 d->configuration.nextProtocolNegotiationStatus = configuration.nextProtocolNegotiationStatus();
979 d->configuration.keyingMaterial = configuration.keyingMaterial();
981 d->configuration.ocspStaplingEnabled = configuration.ocspStaplingEnabled();
983#if QT_CONFIG(openssl)
984 d->configuration.reportFromCallback = configuration.handshakeMustInterruptOnError();
985 d->configuration.missingCertIsFatal = configuration.missingCertificateIsFatal();
990 if (!configuration.d->allowRootCertOnDemandLoading) {
991 d->allowRootCertOnDemandLoading =
false;
992 d->configuration.allowRootCertOnDemandLoading =
false;
1230void QSslSocket::setPrivateKey(
const QString &fileName, QSsl::KeyAlgorithm algorithm,
1231 QSsl::EncodingFormat format,
const QByteArray &passPhrase)
1233 QFile file(fileName);
1234 if (!file.open(QIODevice::ReadOnly)) {
1235 qCWarning(lcSsl,
"QSslSocket::setPrivateKey: Couldn't open file for reading");
1239 QSslKey key(file.readAll(), algorithm, format, QSsl::PrivateKey, passPhrase);
1241 qCWarning(lcSsl,
"QSslSocket::setPrivateKey: "
1242 "The specified file does not contain a valid key");
1247 d->configuration.privateKey = key;
1297bool QSslSocket::waitForEncrypted(
int msecs)
1300 if (!d->plainSocket || d->connectionEncrypted)
1302 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1304 if (!d->verifyProtocolSupported(
"QSslSocket::waitForEncrypted:"))
1307 QElapsedTimer stopWatch;
1310 if (d->plainSocket->state() != QAbstractSocket::ConnectedState) {
1312 if (!d->plainSocket->waitForConnected(msecs))
1316 while (!d->connectionEncrypted) {
1318 if (d->mode == UnencryptedMode)
1319 startClientEncryption();
1322 if (!d->plainSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed())))
1325 return d->connectionEncrypted;
1331bool QSslSocket::waitForReadyRead(
int msecs)
1334 if (!d->plainSocket)
1336 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1337 return d->plainSocket->waitForReadyRead(msecs);
1343 bool readyReadEmitted =
false;
1344 bool *previousReadyReadEmittedPointer = d->readyReadEmittedPointer;
1345 d->readyReadEmittedPointer = &readyReadEmitted;
1347 QElapsedTimer stopWatch;
1350 if (!d->connectionEncrypted) {
1352 if (!waitForEncrypted(msecs)) {
1353 d->readyReadEmittedPointer = previousReadyReadEmittedPointer;
1358 if (!d->writeBuffer.isEmpty()) {
1365 while (!readyReadEmitted &&
1366 d->plainSocket->waitForReadyRead(qt_subtract_from_timeout(msecs, stopWatch.elapsed()))) {
1369 d->readyReadEmittedPointer = previousReadyReadEmittedPointer;
1370 return readyReadEmitted;
1376bool QSslSocket::waitForBytesWritten(
int msecs)
1379 if (!d->plainSocket)
1381 if (d->mode == UnencryptedMode)
1382 return d->plainSocket->waitForBytesWritten(msecs);
1384 QElapsedTimer stopWatch;
1387 if (!d->connectionEncrypted) {
1389 if (!waitForEncrypted(msecs))
1392 if (!d->writeBuffer.isEmpty()) {
1397 return d->plainSocket->waitForBytesWritten(qt_subtract_from_timeout(msecs, stopWatch.elapsed()));
1407bool QSslSocket::waitForDisconnected(
int msecs)
1412 if (state() == UnconnectedState) {
1413 qCWarning(lcSsl,
"QSslSocket::waitForDisconnected() is not allowed in UnconnectedState");
1417 if (!d->plainSocket)
1420 if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1421 return d->plainSocket->waitForDisconnected(msecs);
1423 QElapsedTimer stopWatch;
1426 if (!d->connectionEncrypted) {
1428 if (!waitForEncrypted(msecs))
1433 if (!d->writeBuffer.isEmpty())
1439 if (state() == UnconnectedState)
1442 bool retVal = d->plainSocket->waitForDisconnected(qt_subtract_from_timeout(msecs, stopWatch.elapsed()));
1444 setSocketState(d->plainSocket->state());
1445 d->setError(d->plainSocket->error(), d->plainSocket->errorString());
1704void QSslSocket::startClientEncryption()
1707 if (d->mode != UnencryptedMode) {
1709 "QSslSocket::startClientEncryption: cannot start handshake on non-plain connection");
1712 if (state() != ConnectedState) {
1714 "QSslSocket::startClientEncryption: cannot start handshake when not connected");
1718 if (!supportsSsl()) {
1719 qCWarning(lcSsl,
"QSslSocket::startClientEncryption: TLS initialization failed");
1720 d->setErrorAndEmit(QAbstractSocket::SslInternalError, tr(
"TLS initialization failed"));
1724 if (!d->verifyProtocolSupported(
"QSslSocket::startClientEncryption:"))
1727#ifdef QSSLSOCKET_DEBUG
1728 qCDebug(lcSsl) <<
"QSslSocket::startClientEncryption()";
1730 d->mode = SslClientMode;
1731 emit modeChanged(d->mode);
1732 d->startClientEncryption();
1857void QSslSocket::connectToHost(
const QString &hostName, quint16 port, OpenMode openMode, NetworkLayerProtocol protocol)
1860 d->preferredNetworkLayerProtocol = protocol;
1861 if (!d->initialized)
1863 d->initialized =
false;
1865#ifdef QSSLSOCKET_DEBUG
1866 qCDebug(lcSsl) <<
"QSslSocket::connectToHost("
1867 << hostName <<
',' << port <<
',' << openMode <<
')';
1869 if (!d->plainSocket) {
1870#ifdef QSSLSOCKET_DEBUG
1871 qCDebug(lcSsl) <<
"\tcreating internal plain socket";
1873 d->createPlainSocket(openMode);
1875#ifndef QT_NO_NETWORKPROXY
1876 d->plainSocket->setProtocolTag(d->protocolTag);
1877 d->plainSocket->setProxy(proxy());
1879 QIODevice::open(openMode);
1880 d->readChannelCount = d->writeChannelCount = 0;
1881 d->plainSocket->connectToHost(hostName, port, openMode, d->preferredNetworkLayerProtocol);
1882 d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();
1888void QSslSocket::disconnectFromHost()
1891#ifdef QSSLSOCKET_DEBUG
1892 qCDebug(lcSsl) <<
"QSslSocket::disconnectFromHost()";
1894 if (!d->plainSocket)
1896 if (d->state == UnconnectedState)
1898 if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
1899 d->plainSocket->disconnectFromHost();
1902 if (d->state <= ConnectingState) {
1903 d->pendingClose =
true;
1908 if (
auto *backend = d->backend.get())
1909 backend->cancelCAFetch();
1912 if (d->state != ClosingState) {
1913 d->state = ClosingState;
1914 emit stateChanged(d->state);
1917 if (!d->writeBuffer.isEmpty()) {
1918 d->pendingClose =
true;
1922 if (d->mode == UnencryptedMode) {
1923 d->plainSocket->disconnectFromHost();
1925 d->disconnectFromHost();
1932qint64 QSslSocket::readData(
char *data, qint64 maxlen)
1935 qint64 readBytes = 0;
1937 if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
1938 readBytes = d->plainSocket->read(data, maxlen);
1939#ifdef QSSLSOCKET_DEBUG
1940 qCDebug(lcSsl) <<
"QSslSocket::readData(" << (
void *)data <<
',' << maxlen <<
") =="
1945 if (d->plainSocket->bytesAvailable() || d->hasUndecryptedData())
1946 QMetaObject::invokeMethod(
this,
"_q_flushReadBuffer", Qt::QueuedConnection);
1947 else if (d->state != QAbstractSocket::ConnectedState)
1948 return maxlen ? qint64(-1) : qint64(0);
1982QSslSocketPrivate::QSslSocketPrivate()
1983 : initialized(
false)
1984 , mode(QSslSocket::UnencryptedMode)
1985 , autoStartHandshake(
false)
1986 , connectionEncrypted(
false)
1987 , ignoreAllSslErrors(
false)
1988 , readyReadEmittedPointer(
nullptr)
1989 , allowRootCertOnDemandLoading(
true)
1990 , plainSocket(
nullptr)
1992 , flushTriggered(
false)
1994 QSslConfigurationPrivate::deepCopyDefaultConfiguration(&configuration);
1997 if (!configuration.allowRootCertOnDemandLoading)
1998 allowRootCertOnDemandLoading =
false;
2000 const auto *tlsBackend = tlsBackendInUse();
2002 qCWarning(lcSsl,
"No TLS backend is available");
2005 backend.reset(tlsBackend->createTlsCryptograph());
2006 if (!backend.get()) {
2007 qCWarning(lcSsl) <<
"The backend named" << tlsBackend->backendName()
2008 <<
"does not support TLS";
2222void QSslSocketPrivate::setDefaultCaCertificates(
const QList<QSslCertificate> &certs)
2224 QSslSocketPrivate::ensureInitialized();
2225 QMutexLocker locker(&globalData()->mutex);
2226 globalData()->config.detach();
2227 globalData()->config->caCertificates = certs;
2228 globalData()->dtlsConfig.detach();
2229 globalData()->dtlsConfig->caCertificates = certs;
2232 s_loadRootCertsOnDemand =
false;
2238void QSslSocketPrivate::addDefaultCaCertificate(
const QSslCertificate &cert)
2240 QSslSocketPrivate::ensureInitialized();
2241 QMutexLocker locker(&globalData()->mutex);
2242 if (globalData()->config->caCertificates.contains(cert))
2244 globalData()->config.detach();
2245 globalData()->config->caCertificates += cert;
2246 globalData()->dtlsConfig.detach();
2247 globalData()->dtlsConfig->caCertificates += cert;
2253void QSslSocketPrivate::addDefaultCaCertificates(
const QList<QSslCertificate> &certs)
2255 QSslSocketPrivate::ensureInitialized();
2256 QMutexLocker locker(&globalData()->mutex);
2257 globalData()->config.detach();
2258 globalData()->config->caCertificates += certs;
2259 globalData()->dtlsConfig.detach();
2260 globalData()->dtlsConfig->caCertificates += certs;
2289void QSslConfigurationPrivate::deepCopyDefaultConfiguration(QSslConfigurationPrivate *ptr)
2291 QSslSocketPrivate::ensureInitialized();
2292 QMutexLocker locker(&globalData()->mutex);
2293 const QSslConfigurationPrivate *global = globalData()->config.constData();
2298 ptr->ref.storeRelaxed(1);
2299 ptr->peerCertificate = global->peerCertificate;
2300 ptr->peerCertificateChain = global->peerCertificateChain;
2301 ptr->localCertificateChain = global->localCertificateChain;
2302 ptr->privateKey = global->privateKey;
2303 ptr->sessionCipher = global->sessionCipher;
2304 ptr->sessionProtocol = global->sessionProtocol;
2305 ptr->ciphers = global->ciphers;
2306 ptr->caCertificates = global->caCertificates;
2307 ptr->allowRootCertOnDemandLoading = global->allowRootCertOnDemandLoading;
2308 ptr->protocol = global->protocol;
2309 ptr->peerVerifyMode = global->peerVerifyMode;
2310 ptr->peerVerifyDepth = global->peerVerifyDepth;
2311 ptr->sslOptions = global->sslOptions;
2312 ptr->ellipticCurves = global->ellipticCurves;
2313 ptr->backendConfig = global->backendConfig;
2315 ptr->dtlsCookieEnabled = global->dtlsCookieEnabled;
2318 ptr->ocspStaplingEnabled = global->ocspStaplingEnabled;
2320#if QT_CONFIG(openssl)
2321 ptr->reportFromCallback = global->reportFromCallback;
2322 ptr->missingCertIsFatal = global->missingCertIsFatal;
2340void QSslConfigurationPrivate::setDefaultDtlsConfiguration(
const QSslConfiguration &configuration)
2342 QSslSocketPrivate::ensureInitialized();
2343 QMutexLocker locker(&globalData()->mutex);
2344 if (globalData()->dtlsConfig == configuration.d)
2347 globalData()->dtlsConfig =
const_cast<QSslConfigurationPrivate*>(configuration.d.constData());
2353void QSslSocketPrivate::createPlainSocket(QIODevice::OpenMode openMode)
2356 q->setOpenMode(openMode);
2357 q->setSocketState(QAbstractSocket::UnconnectedState);
2358 q->setSocketError(QAbstractSocket::UnknownSocketError);
2360 q->setLocalAddress(QHostAddress());
2362 q->setPeerAddress(QHostAddress());
2363 q->setPeerName(QString());
2365 plainSocket =
new QTcpSocket(q);
2366 q->connect(plainSocket, SIGNAL(connected()),
2367 q, SLOT(_q_connectedSlot()),
2368 Qt::DirectConnection);
2369 q->connect(plainSocket, SIGNAL(hostFound()),
2370 q, SLOT(_q_hostFoundSlot()),
2371 Qt::DirectConnection);
2372 q->connect(plainSocket, SIGNAL(disconnected()),
2373 q, SLOT(_q_disconnectedSlot()),
2374 Qt::DirectConnection);
2375 q->connect(plainSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
2376 q, SLOT(_q_stateChangedSlot(QAbstractSocket::SocketState)),
2377 Qt::DirectConnection);
2378 q->connect(plainSocket, SIGNAL(errorOccurred(QAbstractSocket::SocketError)),
2379 q, SLOT(_q_errorSlot(QAbstractSocket::SocketError)),
2380 Qt::DirectConnection);
2381 q->connect(plainSocket, SIGNAL(readyRead()),
2382 q, SLOT(_q_readyReadSlot()),
2383 Qt::DirectConnection);
2384 q->connect(plainSocket, SIGNAL(channelReadyRead(
int)),
2385 q, SLOT(_q_channelReadyReadSlot(
int)),
2386 Qt::DirectConnection);
2387 q->connect(plainSocket, SIGNAL(bytesWritten(qint64)),
2388 q, SLOT(_q_bytesWrittenSlot(qint64)),
2389 Qt::DirectConnection);
2390 q->connect(plainSocket, SIGNAL(channelBytesWritten(
int,qint64)),
2391 q, SLOT(_q_channelBytesWrittenSlot(
int,qint64)),
2392 Qt::DirectConnection);
2393 q->connect(plainSocket, SIGNAL(readChannelFinished()),
2394 q, SLOT(_q_readChannelFinishedSlot()),
2395 Qt::DirectConnection);
2396#ifndef QT_NO_NETWORKPROXY
2397 q->connect(plainSocket, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)),
2398 q, SIGNAL(proxyAuthenticationRequired(QNetworkProxy,QAuthenticator*)));
2402 writeBuffer.clear();
2403 connectionEncrypted =
false;
2404 configuration.peerCertificate.clear();
2405 configuration.peerCertificateChain.clear();
2406 mode = QSslSocket::UnencryptedMode;
2407 q->setReadBufferSize(readBufferMaxSize);
2434bool QSslSocketPrivate::bind(
const QHostAddress &address, quint16 port, QAbstractSocket::BindMode mode,
2435 const QNetworkInterface *iface)
2441 initialized =
false;
2443#ifdef QSSLSOCKET_DEBUG
2444 qCDebug(lcSsl) <<
"QSslSocket::bind(" << address <<
',' << port <<
',' << mode <<
')';
2447#ifdef QSSLSOCKET_DEBUG
2448 qCDebug(lcSsl) <<
"\tcreating internal plain socket";
2450 createPlainSocket(QIODevice::ReadWrite);
2452 bool ret = plainSocket->bind(address, port, mode);
2453 localPort = plainSocket->localPort();
2454 localAddress = plainSocket->localAddress();
2455 cachedSocketDescriptor = plainSocket->socketDescriptor();
2456 readChannelCount = writeChannelCount = 0;
2463void QSslSocketPrivate::_q_connectedSlot()
2466 q->setLocalPort(plainSocket->localPort());
2467 q->setLocalAddress(plainSocket->localAddress());
2468 q->setPeerPort(plainSocket->peerPort());
2469 q->setPeerAddress(plainSocket->peerAddress());
2470 q->setPeerName(plainSocket->peerName());
2471 cachedSocketDescriptor = plainSocket->socketDescriptor();
2472 readChannelCount = plainSocket->readChannelCount();
2473 writeChannelCount = plainSocket->writeChannelCount();
2475#ifdef QSSLSOCKET_DEBUG
2476 qCDebug(lcSsl) <<
"QSslSocket::_q_connectedSlot()";
2477 qCDebug(lcSsl) <<
"\tstate =" << q->state();
2478 qCDebug(lcSsl) <<
"\tpeer =" << q->peerName() << q->peerAddress() << q->peerPort();
2479 qCDebug(lcSsl) <<
"\tlocal =" << QHostInfo::fromName(q->localAddress().toString()).hostName()
2480 << q->localAddress() << q->localPort();
2483 if (autoStartHandshake)
2484 q->startClientEncryption();
2486 emit q->connected();
2488 if (pendingClose && !autoStartHandshake) {
2489 pendingClose =
false;
2490 q->disconnectFromHost();
3021bool QSslSocketPrivate::isMatchingHostname(
const QSslCertificate &cert,
const QString &peerName)
3023 QHostAddress hostAddress(peerName);
3024 if (!hostAddress.isNull()) {
3025 const auto subjectAlternativeNames = cert.subjectAlternativeNames();
3026 const auto ipAddresses = subjectAlternativeNames.equal_range(QSsl::AlternativeNameEntryType::IpAddressEntry);
3028 for (
auto it = ipAddresses.first; it != ipAddresses.second; it++) {
3029 if (QHostAddress(*it).isEqual(hostAddress, QHostAddress::StrictConversion))
3034 const QString lowerPeerName = QString::fromLatin1(QUrl::toAce(peerName));
3035 const QStringList commonNames = cert.subjectInfo(QSslCertificate::CommonName);
3037 for (
const QString &commonName : commonNames) {
3038 if (isMatchingHostname(commonName, lowerPeerName))
3042 const auto subjectAlternativeNames = cert.subjectAlternativeNames();
3043 const auto altNames = subjectAlternativeNames.equal_range(QSsl::DnsEntry);
3044 for (
auto it = altNames.first; it != altNames.second; ++it) {
3045 if (isMatchingHostname(*it, lowerPeerName))
3056bool QSslSocketPrivate::isMatchingHostname(
const QString &cn,
const QString &hostname)
3058 qsizetype wildcard = cn.indexOf(u'*');
3062 return QLatin1StringView(QUrl::toAce(cn)) == hostname;
3064 qsizetype firstCnDot = cn.indexOf(u'.');
3065 qsizetype secondCnDot = cn.indexOf(u'.', firstCnDot+1);
3068 if ((-1 == secondCnDot) || (secondCnDot+1 >= cn.size()))
3072 if (wildcard+1 != firstCnDot)
3076 if (cn.lastIndexOf(u'*') != wildcard)
3081 if (cn.startsWith(
"xn--"_L1, Qt::CaseInsensitive))
3085 if (wildcard && QStringView{hostname}.left(wildcard).compare(QStringView{cn}.left(wildcard), Qt::CaseInsensitive) != 0)
3089 qsizetype hnDot = hostname.indexOf(u'.');
3090 if (QStringView{hostname}.mid(hnDot + 1) != QStringView{cn}.mid(firstCnDot + 1)
3091 && QStringView{hostname}.mid(hnDot + 1) != QLatin1StringView(QUrl::toAce(cn.mid(firstCnDot + 1)))) {
3096 QHostAddress addr(hostname);
3107QTlsBackend *QSslSocketPrivate::tlsBackendInUse()
3109 const QMutexLocker locker(&backendMutex);
3113 if (!activeBackendName.size())
3114 activeBackendName = QTlsBackend::defaultBackendName();
3116 if (!activeBackendName.size()) {
3117 qCWarning(lcSsl,
"No functional TLS backend was found");
3121 tlsBackend = QTlsBackend::findBackend(activeBackendName);
3123 QObject::connect(tlsBackend, &QObject::destroyed, tlsBackend, [] {
3124 const QMutexLocker locker(&backendMutex);
3125 tlsBackend =
nullptr;
3127 Qt::DirectConnection);