Qt 4.8
qsslsocket.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtNetwork module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 
43 //#define QSSLSOCKET_DEBUG
44 
307 #include "qsslcipher.h"
308 #include "qsslsocket.h"
309 #include "qsslsocket_openssl_p.h"
310 #include "qsslconfiguration_p.h"
311 
312 #include <QtCore/qdebug.h>
313 #include <QtCore/qdir.h>
314 #include <QtCore/qmutex.h>
315 #include <QtCore/qelapsedtimer.h>
316 #include <QtNetwork/qhostaddress.h>
317 #include <QtNetwork/qhostinfo.h>
318 
320 
321 /*
322  Returns the difference between msecs and elapsed. If msecs is -1,
323  however, -1 is returned.
324 */
325 static int qt_timeout_value(int msecs, int elapsed)
326 {
327  if (msecs == -1)
328  return -1;
329 
330  int timeout = msecs - elapsed;
331  return timeout < 0 ? 0 : timeout;
332 }
333 
335 {
336 public:
338 
342 };
344 
345 
351  : QTcpSocket(*new QSslSocketBackendPrivate, parent)
352 {
353  Q_D(QSslSocket);
354 #ifdef QSSLSOCKET_DEBUG
355  qDebug() << "QSslSocket::QSslSocket(" << parent << "), this =" << (void *)this;
356 #endif
357  d->q_ptr = this;
358  d->init();
359 }
360 
365 {
366  Q_D(QSslSocket);
367 #ifdef QSSLSOCKET_DEBUG
368  qDebug() << "QSslSocket::~QSslSocket(), this =" << (void *)this;
369 #endif
370  delete d->plainSocket;
371  d->plainSocket = 0;
372 }
373 
414 void QSslSocket::connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode mode)
415 {
416  Q_D(QSslSocket);
417  if (d->state == ConnectedState || d->state == ConnectingState) {
418  qWarning("QSslSocket::connectToHostEncrypted() called when already connecting/connected");
419  return;
420  }
421 
422  d->init();
423  d->autoStartHandshake = true;
424  d->initialized = true;
425 
426  // Note: When connecting to localhost, some platforms (e.g., HP-UX and some BSDs)
427  // establish the connection immediately (i.e., first attempt).
428  connectToHost(hostName, port, mode);
429 }
430 
446  const QString &sslPeerName, OpenMode mode)
447 {
448  Q_D(QSslSocket);
449  if (d->state == ConnectedState || d->state == ConnectingState) {
450  qWarning("QSslSocket::connectToHostEncrypted() called when already connecting/connected");
451  return;
452  }
453 
454  d->init();
455  d->autoStartHandshake = true;
456  d->initialized = true;
457  d->verificationPeerName = sslPeerName;
458 
459  // Note: When connecting to localhost, some platforms (e.g., HP-UX and some BSDs)
460  // establish the connection immediately (i.e., first attempt).
461  connectToHost(hostName, port, mode);
462 }
463 
476 bool QSslSocket::setSocketDescriptor(int socketDescriptor, SocketState state, OpenMode openMode)
477 {
478  Q_D(QSslSocket);
479 #ifdef QSSLSOCKET_DEBUG
480  qDebug() << "QSslSocket::setSocketDescriptor(" << socketDescriptor << ','
481  << state << ',' << openMode << ')';
482 #endif
483  if (!d->plainSocket)
484  d->createPlainSocket(openMode);
485  bool retVal = d->plainSocket->setSocketDescriptor(socketDescriptor, state, openMode);
486  d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();
487  setSocketError(d->plainSocket->error());
488  setSocketState(state);
489  setOpenMode(openMode);
490  setLocalPort(d->plainSocket->localPort());
491  setLocalAddress(d->plainSocket->localAddress());
492  setPeerPort(d->plainSocket->peerPort());
493  setPeerAddress(d->plainSocket->peerAddress());
494  setPeerName(d->plainSocket->peerName());
495  return retVal;
496 }
497 
508 {
509  Q_D(QSslSocket);
510  if (d->plainSocket)
511  d->plainSocket->setSocketOption(option, value);
512 }
513 
524 {
525  Q_D(QSslSocket);
526  if (d->plainSocket)
527  return d->plainSocket->socketOption(option);
528  else
529  return QVariant();
530 }
531 
543 {
544  Q_D(const QSslSocket);
545  return d->mode;
546 }
547 
564 {
565  Q_D(const QSslSocket);
566  return d->connectionEncrypted;
567 }
568 
575 {
576  Q_D(const QSslSocket);
577  return d->configuration.protocol;
578 }
579 
586 {
587  Q_D(QSslSocket);
588  d->configuration.protocol = protocol;
589 }
590 
609 {
610  Q_D(const QSslSocket);
611  return d->configuration.peerVerifyMode;
612 }
613 
635 {
636  Q_D(QSslSocket);
637  d->configuration.peerVerifyMode = mode;
638 }
639 
657 {
658  Q_D(const QSslSocket);
659  return d->configuration.peerVerifyDepth;
660 }
661 
679 {
680  Q_D(QSslSocket);
681  if (depth < 0) {
682  qWarning("QSslSocket::setPeerVerifyDepth: cannot set negative depth of %d", depth);
683  return;
684  }
685  d->configuration.peerVerifyDepth = depth;
686 }
687 
700 {
701  Q_D(const QSslSocket);
702  return d->verificationPeerName;
703 }
704 
717 {
718  Q_D(QSslSocket);
719  d->verificationPeerName = hostName;
720 }
721 
732 {
733  Q_D(const QSslSocket);
734  if (d->mode == UnencryptedMode)
735  return QIODevice::bytesAvailable() + (d->plainSocket ? d->plainSocket->bytesAvailable() : 0);
736  return QIODevice::bytesAvailable() + d->readBuffer.size();
737 }
738 
749 {
750  Q_D(const QSslSocket);
751  if (d->mode == UnencryptedMode)
752  return d->plainSocket ? d->plainSocket->bytesToWrite() : 0;
753  return d->writeBuffer.size();
754 }
755 
767 {
768  Q_D(const QSslSocket);
769  if (d->mode == UnencryptedMode)
770  return 0;
771  return d->plainSocket->bytesAvailable();
772 }
773 
784 {
785  Q_D(const QSslSocket);
786  if (d->mode == UnencryptedMode)
787  return 0;
788  return d->plainSocket->bytesToWrite();
789 }
790 
801 {
802  Q_D(const QSslSocket);
803  if (d->mode == UnencryptedMode)
804  return QIODevice::canReadLine() || (d->plainSocket && d->plainSocket->canReadLine());
805  return QIODevice::canReadLine() || (!d->readBuffer.isEmpty() && d->readBuffer.canReadLine());
806 }
807 
812 {
813 #ifdef QSSLSOCKET_DEBUG
814  qDebug() << "QSslSocket::close()";
815 #endif
816  Q_D(QSslSocket);
817  if (d->plainSocket)
818  d->plainSocket->close();
820 
821  // must be cleared, reading/writing not possible on closed socket:
822  d->readBuffer.clear();
823  d->writeBuffer.clear();
824  // for QTcpSocket this is already done because it uses the readBuffer/writeBuffer
825  // if the QIODevice it is based on
826  // ### FIXME QSslSocket should probably do similar instead of having
827  // its own readBuffer/writeBuffer
828 }
829 
833 bool QSslSocket::atEnd() const
834 {
835  Q_D(const QSslSocket);
836  if (d->mode == UnencryptedMode)
837  return QIODevice::atEnd() && (!d->plainSocket || d->plainSocket->atEnd());
838  return QIODevice::atEnd() && d->readBuffer.isEmpty();
839 }
840 
855 // Note! docs copied from QAbstractSocket::flush()
857 {
858  Q_D(QSslSocket);
859 #ifdef QSSLSOCKET_DEBUG
860  qDebug() << "QSslSocket::flush()";
861 #endif
862  if (d->mode != UnencryptedMode)
863  // encrypt any unencrypted bytes in our buffer
864  d->transmit();
865 
866  return d->plainSocket ? d->plainSocket->flush() : false;
867 }
868 
878 {
879  Q_D(QSslSocket);
880  d->readBufferMaxSize = size;
881 
882  if (d->plainSocket)
883  d->plainSocket->setReadBufferSize(size);
884 }
885 
894 {
895  Q_D(QSslSocket);
896 #ifdef QSSLSOCKET_DEBUG
897  qDebug() << "QSslSocket::abort()";
898 #endif
899  if (d->plainSocket)
900  d->plainSocket->abort();
901  close();
902 }
903 
921 {
922  Q_D(const QSslSocket);
923 
924  // create a deep copy of our configuration
925  QSslConfigurationPrivate *copy = new QSslConfigurationPrivate(d->configuration);
926  copy->ref = 0; // the QSslConfiguration constructor refs up
927  copy->sessionCipher = d->sessionCipher();
928 
929  return QSslConfiguration(copy);
930 }
931 
947 {
948  Q_D(QSslSocket);
949  d->configuration.localCertificate = configuration.localCertificate();
950  d->configuration.privateKey = configuration.privateKey();
951  d->configuration.ciphers = configuration.ciphers();
952  d->configuration.caCertificates = configuration.caCertificates();
953  d->configuration.peerVerifyDepth = configuration.peerVerifyDepth();
954  d->configuration.peerVerifyMode = configuration.peerVerifyMode();
955  d->configuration.protocol = configuration.protocol();
956  d->configuration.sslOptions = configuration.d->sslOptions;
957 
958  // if the CA certificates were set explicitly (either via
959  // QSslConfiguration::setCaCertificates() or QSslSocket::setCaCertificates(),
960  // we cannot load the certificates on demand
961  if (!configuration.d->allowRootCertOnDemandLoading)
962  d->allowRootCertOnDemandLoading = false;
963 }
964 
978 {
979  Q_D(QSslSocket);
980  d->configuration.localCertificate = certificate;
981 }
982 
995 {
996  Q_D(QSslSocket);
997  QFile file(path);
999  d->configuration.localCertificate = QSslCertificate(file.readAll(), format);
1000 }
1001 
1009 {
1010  Q_D(const QSslSocket);
1011  return d->configuration.localCertificate;
1012 }
1013 
1040 {
1041  Q_D(const QSslSocket);
1042  return d->configuration.peerCertificate;
1043 }
1044 
1069 {
1070  Q_D(const QSslSocket);
1071  return d->configuration.peerCertificateChain;
1072 }
1073 
1089 {
1090  Q_D(const QSslSocket);
1091  return d->sessionCipher();
1092 }
1093 
1108 {
1109  Q_D(QSslSocket);
1110  d->configuration.privateKey = key;
1111 }
1112 
1137  QSsl::EncodingFormat format, const QByteArray &passPhrase)
1138 {
1139  Q_D(QSslSocket);
1140  QFile file(fileName);
1141  if (file.open(QIODevice::ReadOnly)) {
1142  d->configuration.privateKey = QSslKey(file.readAll(), algorithm,
1143  format, QSsl::PrivateKey, passPhrase);
1144  }
1145 }
1146 
1153 {
1154  Q_D(const QSslSocket);
1155  return d->configuration.privateKey;
1156 }
1157 
1184 {
1185  Q_D(const QSslSocket);
1186  return d->configuration.ciphers;
1187 }
1188 
1200 {
1201  Q_D(QSslSocket);
1202  d->configuration.ciphers = ciphers;
1203 }
1204 
1219 void QSslSocket::setCiphers(const QString &ciphers)
1220 {
1221  Q_D(QSslSocket);
1222  d->configuration.ciphers.clear();
1223  foreach (const QString &cipherName, ciphers.split(QLatin1String(":"),QString::SkipEmptyParts)) {
1224  for (int i = 0; i < 3; ++i) {
1225  // ### Crude
1226  QSslCipher cipher(cipherName, QSsl::SslProtocol(i));
1227  if (!cipher.isNull())
1228  d->configuration.ciphers << cipher;
1229  }
1230  }
1231 }
1232 
1245 {
1247 }
1248 
1264 {
1266 }
1267 
1276 {
1278 }
1279 
1296  QRegExp::PatternSyntax syntax)
1297 {
1298  Q_D(QSslSocket);
1299  QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax);
1300  if (certs.isEmpty())
1301  return false;
1302 
1303  d->configuration.caCertificates += certs;
1304  return true;
1305 }
1306 
1317 {
1318  Q_D(QSslSocket);
1319  d->configuration.caCertificates += certificate;
1320 }
1321 
1332 {
1333  Q_D(QSslSocket);
1334  d->configuration.caCertificates += certificates;
1335 }
1336 
1350 {
1351  Q_D(QSslSocket);
1352  d->configuration.caCertificates = certificates;
1353  d->allowRootCertOnDemandLoading = false;
1354 }
1355 
1369 {
1370  Q_D(const QSslSocket);
1371  return d->configuration.caCertificates;
1372 }
1373 
1387  QRegExp::PatternSyntax syntax)
1388 {
1389  return QSslSocketPrivate::addDefaultCaCertificates(path, encoding, syntax);
1390 }
1391 
1400 {
1402 }
1403 
1412 {
1414 }
1415 
1429 {
1431 }
1432 
1449 {
1451 }
1452 
1463 {
1464  // we are calling ensureInitialized() in the method below
1466 }
1467 
1476 {
1477  Q_D(QSslSocket);
1478  if (!d->plainSocket)
1479  return false;
1480  bool retVal = d->plainSocket->waitForConnected(msecs);
1481  if (!retVal) {
1482  setSocketState(d->plainSocket->state());
1483  setSocketError(d->plainSocket->error());
1484  setErrorString(d->plainSocket->errorString());
1485  }
1486  return retVal;
1487 }
1488 
1506 {
1507  Q_D(QSslSocket);
1508  if (!d->plainSocket || d->connectionEncrypted)
1509  return false;
1510  if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1511  return false;
1512 
1513  QElapsedTimer stopWatch;
1514  stopWatch.start();
1515 
1516  if (d->plainSocket->state() != QAbstractSocket::ConnectedState) {
1517  // Wait until we've entered connected state.
1518  if (!d->plainSocket->waitForConnected(msecs))
1519  return false;
1520  }
1521 
1522  while (!d->connectionEncrypted) {
1523  // Start the handshake, if this hasn't been started yet.
1524  if (d->mode == UnencryptedMode)
1525  startClientEncryption();
1526  // Loop, waiting until the connection has been encrypted or an error
1527  // occurs.
1528  if (!d->plainSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed())))
1529  return false;
1530  }
1531  return d->connectionEncrypted;
1532 }
1533 
1538 {
1539  Q_D(QSslSocket);
1540  if (!d->plainSocket)
1541  return false;
1542  if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1543  return d->plainSocket->waitForReadyRead(msecs);
1544 
1545  // This function must return true if and only if readyRead() *was* emitted.
1546  // So we initialize "readyReadEmitted" to false and check if it was set to true.
1547  // waitForReadyRead() could be called recursively, so we can't use the same variable
1548  // (the inner waitForReadyRead() may fail, but the outer one still succeeded)
1549  bool readyReadEmitted = false;
1550  bool *previousReadyReadEmittedPointer = d->readyReadEmittedPointer;
1551  d->readyReadEmittedPointer = &readyReadEmitted;
1552 
1553  QElapsedTimer stopWatch;
1554  stopWatch.start();
1555 
1556  if (!d->connectionEncrypted) {
1557  // Wait until we've entered encrypted mode, or until a failure occurs.
1558  if (!waitForEncrypted(msecs)) {
1559  d->readyReadEmittedPointer = previousReadyReadEmittedPointer;
1560  return false;
1561  }
1562  }
1563 
1564  if (!d->writeBuffer.isEmpty()) {
1565  // empty our cleartext write buffer first
1566  d->transmit();
1567  }
1568 
1569  // test readyReadEmitted first because either operation above
1570  // (waitForEncrypted or transmit) may have set it
1571  while (!readyReadEmitted &&
1572  d->plainSocket->waitForReadyRead(qt_timeout_value(msecs, stopWatch.elapsed()))) {
1573  }
1574 
1575  d->readyReadEmittedPointer = previousReadyReadEmittedPointer;
1576  return readyReadEmitted;
1577 }
1578 
1583 {
1584  Q_D(QSslSocket);
1585  if (!d->plainSocket)
1586  return false;
1587  if (d->mode == UnencryptedMode)
1588  return d->plainSocket->waitForBytesWritten(msecs);
1589 
1590  QElapsedTimer stopWatch;
1591  stopWatch.start();
1592 
1593  if (!d->connectionEncrypted) {
1594  // Wait until we've entered encrypted mode, or until a failure occurs.
1595  if (!waitForEncrypted(msecs))
1596  return false;
1597  }
1598  if (!d->writeBuffer.isEmpty()) {
1599  // empty our cleartext write buffer first
1600  d->transmit();
1601  }
1602 
1603  return d->plainSocket->waitForBytesWritten(qt_timeout_value(msecs, stopWatch.elapsed()));
1604 }
1605 
1614 {
1615  Q_D(QSslSocket);
1616 
1617  // require calling connectToHost() before waitForDisconnected()
1618  if (state() == UnconnectedState) {
1619  qWarning("QSslSocket::waitForDisconnected() is not allowed in UnconnectedState");
1620  return false;
1621  }
1622 
1623  if (!d->plainSocket)
1624  return false;
1625  if (d->mode == UnencryptedMode)
1626  return d->plainSocket->waitForDisconnected(msecs);
1627 
1628  QElapsedTimer stopWatch;
1629  stopWatch.start();
1630 
1631  if (!d->connectionEncrypted) {
1632  // Wait until we've entered encrypted mode, or until a failure occurs.
1633  if (!waitForEncrypted(msecs))
1634  return false;
1635  }
1636  bool retVal = d->plainSocket->waitForDisconnected(qt_timeout_value(msecs, stopWatch.elapsed()));
1637  if (!retVal) {
1638  setSocketState(d->plainSocket->state());
1639  setSocketError(d->plainSocket->error());
1640  setErrorString(d->plainSocket->errorString());
1641  }
1642  return retVal;
1643 }
1644 
1654 {
1655  Q_D(const QSslSocket);
1656  return d->sslErrors;
1657 }
1658 
1665 {
1667 }
1668 
1683 {
1684  Q_D(QSslSocket);
1685  if (d->mode != UnencryptedMode) {
1686  qWarning("QSslSocket::startClientEncryption: cannot start handshake on non-plain connection");
1687  return;
1688  }
1689 #ifdef QSSLSOCKET_DEBUG
1690  qDebug() << "QSslSocket::startClientEncryption()";
1691 #endif
1692  d->mode = SslClientMode;
1693  emit modeChanged(d->mode);
1694  d->startClientEncryption();
1695 }
1696 
1718 {
1719  Q_D(QSslSocket);
1720  if (d->mode != UnencryptedMode) {
1721  qWarning("QSslSocket::startServerEncryption: cannot start handshake on non-plain connection");
1722  return;
1723  }
1724 #ifdef QSSLSOCKET_DEBUG
1725  qDebug() << "QSslSocket::startServerEncryption()";
1726 #endif
1727  d->mode = SslServerMode;
1728  emit modeChanged(d->mode);
1729  d->startServerEncryption();
1730 }
1731 
1758 {
1759  Q_D(QSslSocket);
1760  d->ignoreAllSslErrors = true;
1761 }
1762 
1787 {
1788  Q_D(QSslSocket);
1789  d->ignoreErrorsList = errors;
1790 }
1791 
1796  OpenMode openMode)
1797 {
1798  Q_D(QSslSocket);
1799  if (!d->initialized)
1800  d->init();
1801  d->initialized = false;
1802 
1803 #ifdef QSSLSOCKET_DEBUG
1804  qDebug() << "QSslSocket::connectToHostImplementation("
1805  << hostName << ',' << port << ',' << openMode << ')';
1806 #endif
1807  if (!d->plainSocket) {
1808 #ifdef QSSLSOCKET_DEBUG
1809  qDebug() << "\tcreating internal plain socket";
1810 #endif
1811  d->createPlainSocket(openMode);
1812  }
1813 #ifndef QT_NO_NETWORKPROXY
1814  d->plainSocket->setProxy(proxy());
1815  //copy user agent down to the plain socket (if it has been set)
1816  d->plainSocket->setProperty("_q_user-agent", property("_q_user-agent"));
1817 #endif
1818  QIODevice::open(openMode);
1819  d->plainSocket->connectToHost(hostName, port, openMode);
1820  d->cachedSocketDescriptor = d->plainSocket->socketDescriptor();
1821 }
1822 
1827 {
1828  Q_D(QSslSocket);
1829 #ifdef QSSLSOCKET_DEBUG
1830  qDebug() << "QSslSocket::disconnectFromHostImplementation()";
1831 #endif
1832  if (!d->plainSocket)
1833  return;
1834  if (d->state == UnconnectedState)
1835  return;
1836  if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
1837  d->plainSocket->disconnectFromHost();
1838  return;
1839  }
1840  if (d->state <= ConnectingState) {
1841  d->pendingClose = true;
1842  return;
1843  }
1844 
1845  // Perhaps emit closing()
1846  if (d->state != ClosingState) {
1847  d->state = ClosingState;
1848  emit stateChanged(d->state);
1849  }
1850 
1851  if (!d->writeBuffer.isEmpty())
1852  return;
1853 
1854  if (d->mode == UnencryptedMode) {
1855  d->plainSocket->disconnectFromHost();
1856  } else {
1857  d->disconnectFromHost();
1858  }
1859 }
1860 
1865 {
1866  Q_D(QSslSocket);
1867  qint64 readBytes = 0;
1868 
1869  if (d->mode == UnencryptedMode && !d->autoStartHandshake) {
1870  readBytes = d->plainSocket->read(data, maxlen);
1871  } else {
1872  do {
1873  const char *readPtr = d->readBuffer.readPointer();
1874  int bytesToRead = qMin<int>(maxlen - readBytes, d->readBuffer.nextDataBlockSize());
1875  ::memcpy(data + readBytes, readPtr, bytesToRead);
1876  readBytes += bytesToRead;
1877  d->readBuffer.free(bytesToRead);
1878  } while (!d->readBuffer.isEmpty() && readBytes < maxlen);
1879  }
1880 #ifdef QSSLSOCKET_DEBUG
1881  qDebug() << "QSslSocket::readData(" << (void *)data << ',' << maxlen << ") ==" << readBytes;
1882 #endif
1883 
1884  // possibly trigger another transmit() to decrypt more data from the socket
1885  if (d->readBuffer.isEmpty() && d->plainSocket->bytesAvailable())
1886  QMetaObject::invokeMethod(this, "_q_flushReadBuffer", Qt::QueuedConnection);
1887 
1888  return readBytes;
1889 }
1890 
1895 {
1896  Q_D(QSslSocket);
1897 #ifdef QSSLSOCKET_DEBUG
1898  qDebug() << "QSslSocket::writeData(" << (void *)data << ',' << len << ')';
1899 #endif
1900  if (d->mode == UnencryptedMode && !d->autoStartHandshake)
1901  return d->plainSocket->write(data, len);
1902 
1903  char *writePtr = d->writeBuffer.reserve(len);
1904  ::memcpy(writePtr, data, len);
1905 
1906  // make sure we flush to the plain socket's buffer
1907  QMetaObject::invokeMethod(this, "_q_flushWriteBuffer", Qt::QueuedConnection);
1908 
1909  return len;
1910 }
1911 
1916  : initialized(false)
1917  , mode(QSslSocket::UnencryptedMode)
1918  , autoStartHandshake(false)
1919  , connectionEncrypted(false)
1920  , shutdown(false)
1921  , ignoreAllSslErrors(false)
1922  , readyReadEmittedPointer(0)
1923  , allowRootCertOnDemandLoading(true)
1924  , plainSocket(0)
1925 {
1927 }
1928 
1933 {
1934 }
1935 
1940 {
1942  autoStartHandshake = false;
1943  connectionEncrypted = false;
1944  ignoreAllSslErrors = false;
1945  shutdown = false;
1946 
1947  // we don't want to clear the ignoreErrorsList, so
1948  // that it is possible setting it before connecting
1949 // ignoreErrorsList.clear();
1950 
1951  readBuffer.clear();
1952  writeBuffer.clear();
1955 }
1956 
1961 {
1962  QMutexLocker locker(&globalData()->mutex);
1963  return globalData()->config->ciphers;
1964 }
1965 
1970 {
1972  QMutexLocker locker(&globalData()->mutex);
1973  return globalData()->supportedCiphers;
1974 }
1975 
1980 {
1981  QMutexLocker locker(&globalData()->mutex);
1982  globalData()->config.detach();
1983  globalData()->config->ciphers = ciphers;
1984 }
1985 
1990 {
1991  QMutexLocker locker(&globalData()->mutex);
1992  globalData()->config.detach();
1993  globalData()->supportedCiphers = ciphers;
1994 }
1995 
2000 {
2001  // ### Qt5: rename everything containing "caCertificates" to "rootCertificates" or similar
2003  QMutexLocker locker(&globalData()->mutex);
2004  return globalData()->config->caCertificates;
2005 }
2006 
2011 {
2013  QMutexLocker locker(&globalData()->mutex);
2014  globalData()->config.detach();
2015  globalData()->config->caCertificates = certs;
2016  // when the certificates are set explicitly, we do not want to
2017  // load the system certificates on demand
2018  s_loadRootCertsOnDemand = false;
2019 }
2020 
2025  QRegExp::PatternSyntax syntax)
2026 {
2028  QList<QSslCertificate> certs = QSslCertificate::fromPath(path, format, syntax);
2029  if (certs.isEmpty())
2030  return false;
2031 
2032  QMutexLocker locker(&globalData()->mutex);
2033  globalData()->config.detach();
2034  globalData()->config->caCertificates += certs;
2035  return true;
2036 }
2037 
2042 {
2044  QMutexLocker locker(&globalData()->mutex);
2045  globalData()->config.detach();
2046  globalData()->config->caCertificates += cert;
2047 }
2048 
2053 {
2055  QMutexLocker locker(&globalData()->mutex);
2056  globalData()->config.detach();
2057  globalData()->config->caCertificates += certs;
2058 }
2059 
2064 {
2066  QMutexLocker locker(&globalData()->mutex);
2067  return QSslConfiguration(globalData()->config.data());
2068 }
2069 
2074 {
2076  QMutexLocker locker(&globalData()->mutex);
2077  if (globalData()->config == configuration.d)
2078  return; // nothing to do
2079 
2080  globalData()->config = const_cast<QSslConfigurationPrivate*>(configuration.d.constData());
2081 }
2082 
2087 {
2089  QMutexLocker locker(&globalData()->mutex);
2090  const QSslConfigurationPrivate *global = globalData()->config.constData();
2091 
2092  if (!global) {
2093  ptr = 0;
2094  return;
2095  }
2096 
2097  ptr->ref = 1;
2098  ptr->peerCertificate = global->peerCertificate;
2100  ptr->localCertificate = global->localCertificate;
2101  ptr->privateKey = global->privateKey;
2102  ptr->sessionCipher = global->sessionCipher;
2103  ptr->ciphers = global->ciphers;
2104  ptr->caCertificates = global->caCertificates;
2105  ptr->protocol = global->protocol;
2106  ptr->peerVerifyMode = global->peerVerifyMode;
2107  ptr->peerVerifyDepth = global->peerVerifyDepth;
2108  ptr->sslOptions = global->sslOptions;
2109 }
2110 
2115 {
2116  Q_Q(QSslSocket);
2117  q->setOpenMode(openMode); // <- from QIODevice
2118  q->setSocketState(QAbstractSocket::UnconnectedState);
2119  q->setSocketError(QAbstractSocket::UnknownSocketError);
2120  q->setLocalPort(0);
2121  q->setLocalAddress(QHostAddress());
2122  q->setPeerPort(0);
2123  q->setPeerAddress(QHostAddress());
2124  q->setPeerName(QString());
2125 
2126  plainSocket = new QTcpSocket(q);
2127 #ifndef QT_NO_BEARERMANAGEMENT
2128  //copy network session down to the plain socket (if it has been set)
2129  plainSocket->setProperty("_q_networksession", q->property("_q_networksession"));
2130 #endif
2131  q->connect(plainSocket, SIGNAL(connected()),
2132  q, SLOT(_q_connectedSlot()),
2134  q->connect(plainSocket, SIGNAL(hostFound()),
2135  q, SLOT(_q_hostFoundSlot()),
2137  q->connect(plainSocket, SIGNAL(disconnected()),
2138  q, SLOT(_q_disconnectedSlot()),
2140  q->connect(plainSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
2146  q->connect(plainSocket, SIGNAL(readyRead()),
2147  q, SLOT(_q_readyReadSlot()),
2149  q->connect(plainSocket, SIGNAL(bytesWritten(qint64)),
2152 #ifndef QT_NO_NETWORKPROXY
2155 #endif
2156 
2157  readBuffer.clear();
2158  writeBuffer.clear();
2159  connectionEncrypted = false;
2163  q->setReadBufferSize(readBufferMaxSize);
2164 }
2165 
2167 {
2168  if (!socket->d_func()->plainSocket)
2169  return;
2170  QAbstractSocketPrivate::pauseSocketNotifiers(socket->d_func()->plainSocket);
2171 }
2172 
2174 {
2175  if (!socket->d_func()->plainSocket)
2176  return;
2177  QAbstractSocketPrivate::resumeSocketNotifiers(socket->d_func()->plainSocket);
2178 }
2179 
2184 {
2185  Q_Q(QSslSocket);
2186  q->setLocalPort(plainSocket->localPort());
2187  q->setLocalAddress(plainSocket->localAddress());
2188  q->setPeerPort(plainSocket->peerPort());
2189  q->setPeerAddress(plainSocket->peerAddress());
2190  q->setPeerName(plainSocket->peerName());
2192 
2193 #ifdef QSSLSOCKET_DEBUG
2194  qDebug() << "QSslSocket::_q_connectedSlot()";
2195  qDebug() << "\tstate =" << q->state();
2196  qDebug() << "\tpeer =" << q->peerName() << q->peerAddress() << q->peerPort();
2197  qDebug() << "\tlocal =" << QHostInfo::fromName(q->localAddress().toString()).hostName()
2198  << q->localAddress() << q->localPort();
2199 #endif
2200  emit q->connected();
2201 
2202  if (autoStartHandshake) {
2203  q->startClientEncryption();
2204  } else if (pendingClose) {
2205  pendingClose = false;
2206  q->disconnectFromHost();
2207  }
2208 }
2209 
2214 {
2215  Q_Q(QSslSocket);
2216 #ifdef QSSLSOCKET_DEBUG
2217  qDebug() << "QSslSocket::_q_hostFoundSlot()";
2218  qDebug() << "\tstate =" << q->state();
2219 #endif
2220  emit q->hostFound();
2221 }
2222 
2227 {
2228  Q_Q(QSslSocket);
2229 #ifdef QSSLSOCKET_DEBUG
2230  qDebug() << "QSslSocket::_q_disconnectedSlot()";
2231  qDebug() << "\tstate =" << q->state();
2232 #endif
2233  disconnected();
2234  emit q->disconnected();
2235 }
2236 
2241 {
2242  Q_Q(QSslSocket);
2243 #ifdef QSSLSOCKET_DEBUG
2244  qDebug() << "QSslSocket::_q_stateChangedSlot(" << state << ')';
2245 #endif
2246  q->setSocketState(state);
2247  emit q->stateChanged(state);
2248 }
2249 
2254 {
2255  Q_Q(QSslSocket);
2256 #ifdef QSSLSOCKET_DEBUG
2257  qDebug() << "QSslSocket::_q_errorSlot(" << error << ')';
2258  qDebug() << "\tstate =" << q->state();
2259  qDebug() << "\terrorString =" << q->errorString();
2260 #endif
2261  q->setSocketError(plainSocket->error());
2262  q->setErrorString(plainSocket->errorString());
2263  emit q->error(error);
2264 }
2265 
2270 {
2271  Q_Q(QSslSocket);
2272 #ifdef QSSLSOCKET_DEBUG
2273  qDebug() << "QSslSocket::_q_readyReadSlot() -" << plainSocket->bytesAvailable() << "bytes available";
2274 #endif
2277  *readyReadEmittedPointer = true;
2278  emit q->readyRead();
2279  return;
2280  }
2281 
2282  transmit();
2283 }
2284 
2289 {
2290  Q_Q(QSslSocket);
2291 #ifdef QSSLSOCKET_DEBUG
2292  qDebug() << "QSslSocket::_q_bytesWrittenSlot(" << written << ')';
2293 #endif
2294 
2296  emit q->bytesWritten(written);
2297  else
2298  emit q->encryptedBytesWritten(written);
2300  q->disconnectFromHost();
2301 }
2302 
2307 {
2308  Q_Q(QSslSocket);
2309  if (!writeBuffer.isEmpty())
2310  q->flush();
2311 }
2312 
2317 {
2318  // trigger a read from the plainSocket into SSL
2320  transmit();
2321 }
2322 
2327 {
2329  //unencrypted mode - do not use QIODevice::peek, as it reads ahead data from the plain socket
2330  //peek at data already in the QIODevice buffer (from a previous read)
2331  qint64 r = buffer.peek(data, maxSize);
2332  if (r == maxSize)
2333  return r;
2334  data += r;
2335  //peek at data in the plain socket
2336  if (plainSocket) {
2337  qint64 r2 = plainSocket->peek(data, maxSize - r);
2338  if (r2 < 0)
2339  return (r > 0 ? r : r2);
2340  return r + r2;
2341  } else {
2342  return -1;
2343  }
2344  } else {
2345  //encrypted mode - the socket engine will read and decrypt data into the QIODevice buffer
2346  return QTcpSocketPrivate::peek(data, maxSize);
2347  }
2348 }
2349 
2354 {
2356  //unencrypted mode - do not use QIODevice::peek, as it reads ahead data from the plain socket
2357  //peek at data already in the QIODevice buffer (from a previous read)
2358  QByteArray ret;
2359  ret.reserve(maxSize);
2360  ret.resize(buffer.peek(ret.data(), maxSize));
2361  if (ret.length() == maxSize)
2362  return ret;
2363  //peek at data in the plain socket
2364  if (plainSocket)
2365  return ret + plainSocket->peek(maxSize - ret.length());
2366  else
2367  return QByteArray();
2368  } else {
2369  //encrypted mode - the socket engine will read and decrypt data into the QIODevice buffer
2370  return QTcpSocketPrivate::peek(maxSize);
2371  }
2372 }
2373 
2378 {
2379  return s_loadRootCertsOnDemand;
2380 }
2381 
2386 {
2387  return QList<QByteArray>() << "/etc/ssl/certs/" // (K)ubuntu, OpenSUSE, Mandriva, MeeGo ...
2388  << "/usr/lib/ssl/certs/" // Gentoo, Mandrake
2389  << "/usr/share/ssl/" // Centos, Redhat, SuSE
2390  << "/usr/local/ssl/" // Normal OpenSSL Tarball
2391  << "/var/ssl/certs/" // AIX
2392  << "/usr/local/ssl/certs/" // Solaris
2393  << "/etc/openssl/certs/" // BlackBerry
2394  << "/opt/openssl/certs/"; // HP-UX
2395 }
2396 
2398 
2399 // For private slots
2400 #define d d_ptr
2401 #include "moc_qsslsocket.cpp"
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QAtomicInt ref
Definition: qshareddata.h:59
static bool addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat format, QRegExp::PatternSyntax syntax)
QString peerName() const
Returns the name of the peer as specified by connectToHost(), or an empty QString if connectToHost() ...
int socketDescriptor() const
Returns the native socket descriptor of the QAbstractSocket object if this is available; otherwise re...
The QSslKey class provides an interface for private and public keys.
Definition: qsslkey.h:64
QSharedDataPointer< QSslConfigurationPrivate > d
static double elapsed(qint64 after, qint64 before)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QList< QSslError > sslErrors() const
Returns a list of the last SSL errors that occurred.
The QMutex class provides access serialization between threads.
Definition: qmutex.h:60
void startServerEncryption()
Starts a delayed SSL handshake for a server connection.
QList< QSslCipher > ciphers() const
Returns this connection&#39;s current cryptographic cipher suite.
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QIODevice::OpenMode openMode
Definition: qiodevice_p.h:212
void startClientEncryption()
Starts a delayed SSL handshake for a client connection.
QHostAddress localAddress() const
Returns the host address of the local socket if available; otherwise returns QHostAddress::Null.
static void resumeSocketNotifiers(QSslSocket *)
static void setDefaultSupportedCiphers(const QList< QSslCipher > &ciphers)
qint64 bytesAvailable() const
Returns the number of incoming bytes that are waiting to be read.
void proxyAuthenticationRequired(const QNetworkProxy &proxy, QAuthenticator *authenticator)
SslMode mode() const
Returns the current mode for the socket; either UnencryptedMode, where QSslSocket behaves identially ...
Definition: qsslsocket.cpp:542
bool setSocketDescriptor(int socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite)
Initializes QSslSocket with the native socket descriptor socketDescriptor.
Definition: qsslsocket.cpp:476
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
static QList< QByteArray > unixRootCertDirectories()
void createPlainSocket(QIODevice::OpenMode openMode)
QSslConfiguration sslConfiguration() const
Returns the socket&#39;s SSL configuration state.
Definition: qsslsocket.cpp:920
void ignoreSslErrors()
This slot tells QSslSocket to ignore errors during QSslSocket&#39;s handshake phase and continue connecti...
virtual qint64 peek(char *data, qint64 maxSize)
Definition: qiodevice.cpp:1502
~QSslSocket()
Destroys the QSslSocket.
Definition: qsslsocket.cpp:364
static bool addDefaultCaCertificates(const QString &path, QSsl::EncodingFormat format=QSsl::Pem, QRegExp::PatternSyntax syntax=QRegExp::FixedString)
Searches all files in the path for certificates with the specified encoding and adds them to the defa...
qint64 encryptedBytesToWrite() const
Returns the number of encrypted bytes that are waiting to be written to the network.
Definition: qsslsocket.cpp:783
#define error(msg)
QList< QSslCipher > ciphers
QSslCertificate localCertificate() const
Returns the socket&#39;s local QSslCertificate {certificate}, or an empty certificate if no local certifi...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
#define SLOT(a)
Definition: qobjectdefs.h:226
SocketOption
This enum represents the options that can be set on a socket.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
Definition: qiodevice.cpp:1671
QIODevicePrivateLinearBuffer buffer
Definition: qiodevice_p.h:215
The QSslSocket class provides an SSL encrypted socket for both clients and servers.
Definition: qsslsocket.h:67
static void addDefaultCaCertificate(const QSslCertificate &certificate)
Adds certificate to the default CA certificate database.
static void resumeSocketNotifiers(QAbstractSocket *)
bool setProperty(const char *name, const QVariant &value)
Sets the value of the object&#39;s name property to value.
Definition: qobject.cpp:3755
QSsl::SslProtocol protocol() const
Returns the socket&#39;s SSL protocol.
Definition: qsslsocket.cpp:574
void setSslConfiguration(const QSslConfiguration &config)
Sets the socket&#39;s SSL configuration to be the contents of configuration.
Definition: qsslsocket.cpp:946
static void setDefaultCaCertificates(const QList< QSslCertificate > &certificates)
Sets the default CA certificate database to certificates.
quint16 peerPort() const
Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0...
void abort()
Aborts the current connection and resets the socket.
Definition: qsslsocket.cpp:893
static void addDefaultCaCertificate(const QSslCertificate &cert)
static QList< QSslCertificate > defaultCaCertificates()
Returns the current default CA certificate database.
void _q_stateChangedSlot(QAbstractSocket::SocketState)
bool isEmpty() const
The QString class provides a Unicode character string.
Definition: qstring.h:83
static QList< QSslCertificate > defaultCaCertificates()
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
QSslSocket::SslMode mode
Definition: qsslsocket_p.h:108
The QElapsedTimer class provides a fast way to calculate elapsed times.
Definition: qelapsedtimer.h:53
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i.e.
Definition: qiodevice.cpp:711
QVariant socketOption(QAbstractSocket::SocketOption option)
Returns the value of the option option.
Definition: qsslsocket.cpp:523
static bool s_loadRootCertsOnDemand
Definition: qsslsocket_p.h:187
void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value)
Sets the given option to the value described by value.
Definition: qsslsocket.cpp:507
void _q_bytesWrittenSlot(qint64)
The QSslCipher class represents an SSL cryptographic cipher.
Definition: qsslcipher.h:59
qint64 elapsed() const
Returns the number of milliseconds since this QElapsedTimer was last started.
bool waitForBytesWritten(int msecs=30000)
Reimplemented Function
static void deepCopyDefaultConfiguration(QSslConfigurationPrivate *config)
qint64 encryptedBytesAvailable() const
Returns the number of encrypted bytes that are awaiting decryption.
Definition: qsslsocket.cpp:766
static bool supportsSsl()
Returns true if this platform supports SSL; otherwise, returns false.
bool waitForDisconnected(int msecs=30000)
Waits until the socket has disconnected or msecs milliseconds, whichever comes first.
qint64 readData(char *data, qint64 maxlen)
Reimplemented Function
bool waitForConnected(int msecs=30000)
Waits until the socket is connected, or msecs milliseconds, whichever happens first.
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
void disconnectFromHostImplementation()
#define Q_Q(Class)
Definition: qglobal.h:2483
qint64 writeData(const char *data, qint64 len)
Reimplemented Function
QSsl::SslProtocol protocol() const
Returns the protocol setting for this SSL configuration.
void _q_disconnectedSlot()
SocketState
This enum describes the different states in which a socket can be.
void setProtocol(QSsl::SslProtocol protocol)
Sets the socket&#39;s SSL protocol to protocol.
Definition: qsslsocket.cpp:585
Q_CORE_EXPORT void qDebug(const char *,...)
void addCaCertificate(const QSslCertificate &certificate)
Adds the certificate to this socket&#39;s CA certificate database.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void connectToHostImplementation(const QString &hostName, quint16 port, OpenMode openMode)
static void setDefaultCiphers(const QList< QSslCipher > &ciphers)
QAbstractSocket::SocketState state
The QNetworkProxy class provides a network layer proxy.
QList< QSslCertificate > caCertificates
void setPeerVerifyName(const QString &hostName)
Sets a different host name, given by hostName, for the certificate validation instead of the one used...
Definition: qsslsocket.cpp:716
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static QIntfbScreen * connected
virtual void transmit()=0
QList< QSslCipher > ciphers() const
Returns this socket&#39;s current cryptographic cipher suite.
SslProtocol
Describes the protocol of the cipher.
Definition: qssl.h:76
SslMode
Describes the connection modes available for QSslSocket.
Definition: qsslsocket.h:71
int peerVerifyDepth() const
Returns the maximum number of certificates in the peer&#39;s certificate chain to be checked during the S...
Definition: qsslsocket.cpp:656
static QSslConfiguration defaultConfiguration()
QList< QSslCertificate > peerCertificateChain() const
Returns the peer&#39;s chain of digital certificates, or an empty list of certificates.
void setPeerVerifyDepth(int depth)
Sets the maximum number of certificates in the peer&#39;s certificate chain to be checked during the SSL ...
Definition: qsslsocket.cpp:678
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
qint64 bytesAvailable() const
Returns the number of decrypted bytes that are immediately available for reading. ...
Definition: qsslsocket.cpp:731
#define emit
Definition: qobjectdefs.h:76
SocketError
This enum describes the socket errors that can occur.
void connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode mode=ReadWrite)
Starts an encrypted connection to the device hostName on port, using mode as the OpenMode ...
Definition: qsslsocket.cpp:414
static void setDefaultCaCertificates(const QList< QSslCertificate > &certs)
#define d
qint64 peek(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, without side effects (i.
Definition: qiodevice.cpp:1563
QSslSocket::PeerVerifyMode peerVerifyMode
unsigned short quint16
Definition: qglobal.h:936
QString peerVerifyName() const
Returns the different hostname for the certificate validation, as set by setPeerVerifyName or by conn...
Definition: qsslsocket.cpp:699
Q_CORE_EXPORT void qWarning(const char *,...)
const T * constData() const
Returns a const pointer to the shared data object.
Definition: qshareddata.h:84
static const char * data(const QByteArray &arr)
QSslCertificate peerCertificate() const
Returns the peer&#39;s digital certificate (i.e., the immediate certificate of the host you are connected...
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static int qt_timeout_value(int msecs, int elapsed)
Definition: qsslsocket.cpp:325
The QTcpSocket class provides a TCP socket.
Definition: qtcpsocket.h:56
static QList< QSslCipher > supportedCiphers()
Returns the list of cryptographic ciphers supported by this system.
int peek(char *target, int size)
Definition: qiodevice_p.h:113
const T * ptr(const T &t)
void clear()
Removes all items from the list.
Definition: qlist.h:764
__int64 qint64
Definition: qglobal.h:942
static void setDefaultConfiguration(const QSslConfiguration &configuration)
QList< QSslCertificate > caCertificates() const
Returns this socket&#39;s CA certificate database.
static QList< QSslCipher > defaultCiphers()
Returns the default cryptographic cipher suite for all sockets in this application.
void close()
Reimplemented Function
Definition: qsslsocket.cpp:811
static bool supportsSsl()
Does the minimum amount of initialization to determine whether SSL is supported or not...
void setLocalCertificate(const QSslCertificate &certificate)
Sets the socket&#39;s local certificate to certificate.
Definition: qsslsocket.cpp:977
void setPrivateKey(const QSslKey &key)
Sets the socket&#39;s private QSslKey {key} to key.
bool isEncrypted() const
Returns true if the socket is encrypted; otherwise, false is returned.
Definition: qsslsocket.cpp:563
void close()
Closes the I/O device for the socket, disconnects the socket&#39;s connection with the host...
The QAuthenticator class provides an authentication object.
QSslKey privateKey() const
Returns the QSslKey {SSL key} assigned to this connection or a null key if none has been assigned yet...
static QList< QSslCertificate > systemCaCertificates()
This function provides the CA certificate database provided by the operating system.
QSslConfigurationPrivate configuration
Definition: qsslsocket_p.h:116
int length() const
Same as size().
Definition: qbytearray.h:356
QSslCertificate localCertificate
QSslCipher sessionCipher() const
Returns the socket&#39;s cryptographic QSslCipher {cipher}, or a null cipher if the connection isn&#39;t encr...
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QHostInfo fromName(const QString &name)
Looks up the IP address(es) for the given host name.
Definition: qhostinfo.cpp:273
PatternSyntax
The syntax used to interpret the meaning of the pattern.
Definition: qregexp.h:64
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
EncodingFormat
Describes supported encoding formats for certificates and keys.
Definition: qssl.h:61
void setCaCertificates(const QList< QSslCertificate > &certificates)
Sets this socket&#39;s CA certificate database to be certificates.
bool addCaCertificates(const QString &path, QSsl::EncodingFormat format=QSsl::Pem, QRegExp::PatternSyntax syntax=QRegExp::FixedString)
Searches all files in the path for certificates encoded in the specified format and adds them to this...
quint16 localPort() const
Returns the host port number (in native byte order) of the local socket if available; otherwise retur...
bool flush()
This function writes as much as possible from the internal write buffer to the underlying network soc...
Definition: qsslsocket.cpp:856
virtual void disconnected()=0
QExplicitlySharedDataPointer< QSslConfigurationPrivate > config
Definition: qsslsocket.cpp:341
static QList< QSslCertificate > systemCaCertificates()
int peerVerifyDepth() const
Returns the maximum number of certificates in the peer&#39;s certificate chain to be checked during the S...
bool isNull() const
Returns true if this is a null cipher; otherwise returns false.
Definition: qsslcipher.cpp:151
QTcpSocket * plainSocket
Definition: qsslsocket_p.h:153
int key
QSslCertificate localCertificate() const
Returns the certificate to be presented to the peer during the SSL handshake process.
QSslKey privateKey() const
Returns this socket&#39;s private key.
void resize(int size)
Sets the size of the byte array to size bytes.
void _q_flushWriteBuffer()
virtual qint64 bytesAvailable() const
Returns the number of bytes that are available for reading.
Definition: qiodevice.cpp:752
bool waitForReadyRead(int msecs=30000)
Reimplemented Function
const char * property
Definition: qwizard.cpp:138
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
bool canReadLine() const
Returns true if you can read one while line (terminated by a single ASCII &#39; &#39; character) of decrypted...
Definition: qsslsocket.cpp:800
static QList< QSslCertificate > fromPath(const QString &path, QSsl::EncodingFormat format=QSsl::Pem, QRegExp::PatternSyntax syntax=QRegExp::FixedString)
Searches all files in the path for certificates encoded in the specified format and returns them in a...
QHostAddress peerAddress() const
Returns the address of the connected peer if the socket is in ConnectedState; otherwise returns QHost...
void _q_errorSlot(QAbstractSocket::SocketError)
bool * readyReadEmittedPointer
Definition: qsslsocket_p.h:114
virtual bool open(OpenMode mode)
Opens the device and sets its OpenMode to mode.
Definition: qiodevice.cpp:570
virtual ~QSslSocketPrivate()
static void ensureInitialized()
Declared static in QSslSocketPrivate, makes sure the SSL libraries have been initialized.
static bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType, QGenericReturnArgument ret, QGenericArgument val0=QGenericArgument(0), QGenericArgument val1=QGenericArgument(), QGenericArgument val2=QGenericArgument(), QGenericArgument val3=QGenericArgument(), QGenericArgument val4=QGenericArgument(), QGenericArgument val5=QGenericArgument(), QGenericArgument val6=QGenericArgument(), QGenericArgument val7=QGenericArgument(), QGenericArgument val8=QGenericArgument(), QGenericArgument val9=QGenericArgument())
Invokes the member (a signal or a slot name) on the object obj.
bool waitForEncrypted(int msecs=30000)
Waits until the socket has completed the SSL handshake and has emitted encrypted(), or msecs milliseconds, whichever comes first.
KeyAlgorithm
Describes the different key algorithms supported by QSslKey.
Definition: qssl.h:66
The QSslCertificate class provides a convenient API for an X509 certificate.
bool atEnd() const
Reimplemented Function
Definition: qsslsocket.cpp:833
The QSslConfiguration class holds the configuration and state of an SSL connection.
static QList< QSslCipher > supportedCiphers()
QStringList split(const QString &sep, SplitBehavior behavior=KeepEmptyParts, Qt::CaseSensitivity cs=Qt::CaseSensitive) const Q_REQUIRED_RESULT
Splits the string into substrings wherever sep occurs, and returns the list of those strings...
Definition: qstring.cpp:6526
QSslSocket::PeerVerifyMode peerVerifyMode() const
Returns the verify mode.
QList< QSslCertificate > caCertificates() const
Returns this connection&#39;s CA certificate database.
void clear()
Clears the contents of this certificate, making it a null certificate.
static QList< QSslCipher > defaultCiphers()
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false...
Definition: qiodevice.cpp:1330
The QHostAddress class provides an IP address.
Definition: qhostaddress.h:70
QList< QSslCertificate > peerCertificateChain
static void pauseSocketNotifiers(QAbstractSocket *)
void setPeerVerifyMode(QSslSocket::PeerVerifyMode mode)
Sets the socket&#39;s verify mode to mode.
Definition: qsslsocket.cpp:634
qint64 bytesToWrite() const
Returns the number of unencrypted bytes that are waiting to be encrypted and written to the network...
Definition: qsslsocket.cpp:748
SocketError error() const
Returns the type of error that last occurred.
void reserve(int size)
Attempts to allocate memory for at least size bytes.
Definition: qbytearray.h:449
void setReadBufferSize(qint64 size)
Sets the size of QSslSocket&#39;s internal read buffer to be size bytes.
Definition: qsslsocket.cpp:877
static void setDefaultCiphers(const QList< QSslCipher > &ciphers)
Sets the default cryptographic cipher suite for all sockets in this application to ciphers...
static QString fileName(const QString &fileUrl)
void start()
Starts this timer.
static void pauseSocketNotifiers(QSslSocket *)
QList< QSslCipher > supportedCiphers
Definition: qsslsocket.cpp:340
void setCiphers(const QList< QSslCipher > &ciphers)
Sets the cryptographic cipher suite for this socket to ciphers, which must contain a subset of the ci...
QSslSocket::PeerVerifyMode peerVerifyMode() const
Returns the socket&#39;s verify mode.
Definition: qsslsocket.cpp:608
virtual qint64 peek(char *data, qint64 maxSize)
static Q_AUTOTEST_EXPORT bool rootCertOnDemandLoadingSupported()
PeerVerifyMode
Describes the peer verification modes for QSslSocket.
Definition: qsslsocket.h:77