| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 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 The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | |
| 41 | #ifndef QSSLSOCKET_H |
| 42 | #define QSSLSOCKET_H |
| 43 | |
| 44 | #include <QtNetwork/qtnetworkglobal.h> |
| 45 | #include <QtCore/qlist.h> |
| 46 | #ifndef QT_NO_SSL |
| 47 | # include <QtNetwork/qtcpsocket.h> |
| 48 | # include <QtNetwork/qsslerror.h> |
| 49 | #endif |
| 50 | |
| 51 | QT_BEGIN_NAMESPACE |
| 52 | |
| 53 | |
| 54 | #ifndef QT_NO_SSL |
| 55 | |
| 56 | class QDir; |
| 57 | class QSslCipher; |
| 58 | class QSslCertificate; |
| 59 | class QSslConfiguration; |
| 60 | class QSslPreSharedKeyAuthenticator; |
| 61 | class QOcspResponse; |
| 62 | |
| 63 | class QSslSocketPrivate; |
| 64 | class Q_NETWORK_EXPORT QSslSocket : public QTcpSocket |
| 65 | { |
| 66 | Q_OBJECT |
| 67 | Q_MOC_INCLUDE(<QtNetwork/qsslpresharedkeyauthenticator.h>) |
| 68 | public: |
| 69 | enum SslMode { |
| 70 | UnencryptedMode, |
| 71 | SslClientMode, |
| 72 | SslServerMode |
| 73 | }; |
| 74 | |
| 75 | enum PeerVerifyMode { |
| 76 | VerifyNone, |
| 77 | QueryPeer, |
| 78 | VerifyPeer, |
| 79 | AutoVerifyPeer |
| 80 | }; |
| 81 | |
| 82 | explicit QSslSocket(QObject *parent = nullptr); |
| 83 | ~QSslSocket(); |
| 84 | void resume() override; // to continue after proxy authentication required, SSL errors etc. |
| 85 | |
| 86 | // Autostarting the SSL client handshake. |
| 87 | void connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol); |
| 88 | void connectToHostEncrypted(const QString &hostName, quint16 port, const QString &sslPeerName, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol); |
| 89 | bool setSocketDescriptor(qintptr socketDescriptor, SocketState state = ConnectedState, |
| 90 | OpenMode openMode = ReadWrite) override; |
| 91 | |
| 92 | using QAbstractSocket::connectToHost; |
| 93 | void connectToHost(const QString &hostName, quint16 port, OpenMode openMode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol) override; |
| 94 | void disconnectFromHost() override; |
| 95 | |
| 96 | virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value) override; |
| 97 | virtual QVariant socketOption(QAbstractSocket::SocketOption option) override; |
| 98 | |
| 99 | SslMode mode() const; |
| 100 | bool isEncrypted() const; |
| 101 | |
| 102 | QSsl::SslProtocol protocol() const; |
| 103 | void setProtocol(QSsl::SslProtocol protocol); |
| 104 | |
| 105 | QSslSocket::PeerVerifyMode peerVerifyMode() const; |
| 106 | void setPeerVerifyMode(QSslSocket::PeerVerifyMode mode); |
| 107 | |
| 108 | int peerVerifyDepth() const; |
| 109 | void setPeerVerifyDepth(int depth); |
| 110 | |
| 111 | QString peerVerifyName() const; |
| 112 | void setPeerVerifyName(const QString &hostName); |
| 113 | |
| 114 | // From QIODevice |
| 115 | qint64 bytesAvailable() const override; |
| 116 | qint64 bytesToWrite() const override; |
| 117 | bool canReadLine() const override; |
| 118 | void close() override; |
| 119 | bool atEnd() const override; |
| 120 | |
| 121 | // From QAbstractSocket: |
| 122 | void setReadBufferSize(qint64 size) override; |
| 123 | |
| 124 | // Similar to QIODevice's: |
| 125 | qint64 encryptedBytesAvailable() const; |
| 126 | qint64 encryptedBytesToWrite() const; |
| 127 | |
| 128 | // SSL configuration |
| 129 | QSslConfiguration sslConfiguration() const; |
| 130 | void setSslConfiguration(const QSslConfiguration &config); |
| 131 | |
| 132 | // Certificate & cipher accessors. |
| 133 | void setLocalCertificateChain(const QList<QSslCertificate> &localChain); |
| 134 | QList<QSslCertificate> localCertificateChain() const; |
| 135 | |
| 136 | void setLocalCertificate(const QSslCertificate &certificate); |
| 137 | void setLocalCertificate(const QString &fileName, QSsl::EncodingFormat format = QSsl::Pem); |
| 138 | QSslCertificate localCertificate() const; |
| 139 | QSslCertificate peerCertificate() const; |
| 140 | QList<QSslCertificate> peerCertificateChain() const; |
| 141 | QSslCipher sessionCipher() const; |
| 142 | QSsl::SslProtocol sessionProtocol() const; |
| 143 | QList<QOcspResponse> ocspResponses() const; |
| 144 | |
| 145 | // Private keys, for server sockets. |
| 146 | void setPrivateKey(const QSslKey &key); |
| 147 | void setPrivateKey(const QString &fileName, QSsl::KeyAlgorithm algorithm = QSsl::Rsa, |
| 148 | QSsl::EncodingFormat format = QSsl::Pem, |
| 149 | const QByteArray &passPhrase = QByteArray()); |
| 150 | QSslKey privateKey() const; |
| 151 | |
| 152 | bool waitForConnected(int msecs = 30000) override; |
| 153 | bool waitForEncrypted(int msecs = 30000); |
| 154 | bool waitForReadyRead(int msecs = 30000) override; |
| 155 | bool waitForBytesWritten(int msecs = 30000) override; |
| 156 | bool waitForDisconnected(int msecs = 30000) override; |
| 157 | |
| 158 | QList<QSslError> sslHandshakeErrors() const; |
| 159 | |
| 160 | static bool supportsSsl(); |
| 161 | static long sslLibraryVersionNumber(); |
| 162 | static QString sslLibraryVersionString(); |
| 163 | static long sslLibraryBuildVersionNumber(); |
| 164 | static QString sslLibraryBuildVersionString(); |
| 165 | |
| 166 | void ignoreSslErrors(const QList<QSslError> &errors); |
| 167 | void continueInterruptedHandshake(); |
| 168 | |
| 169 | public Q_SLOTS: |
| 170 | void startClientEncryption(); |
| 171 | void startServerEncryption(); |
| 172 | void ignoreSslErrors(); |
| 173 | |
| 174 | Q_SIGNALS: |
| 175 | void encrypted(); |
| 176 | void peerVerifyError(const QSslError &error); |
| 177 | void sslErrors(const QList<QSslError> &errors); |
| 178 | void modeChanged(QSslSocket::SslMode newMode); |
| 179 | void encryptedBytesWritten(qint64 totalBytes); |
| 180 | void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator); |
| 181 | void newSessionTicketReceived(); |
| 182 | void alertSent(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description); |
| 183 | void alertReceived(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description); |
| 184 | void handshakeInterruptedOnError(const QSslError &error); |
| 185 | |
| 186 | protected: |
| 187 | qint64 readData(char *data, qint64 maxlen) override; |
| 188 | qint64 skipData(qint64 maxSize) override; |
| 189 | qint64 writeData(const char *data, qint64 len) override; |
| 190 | |
| 191 | private: |
| 192 | Q_DECLARE_PRIVATE(QSslSocket) |
| 193 | Q_DISABLE_COPY_MOVE(QSslSocket) |
| 194 | Q_PRIVATE_SLOT(d_func(), void _q_connectedSlot()) |
| 195 | Q_PRIVATE_SLOT(d_func(), void _q_hostFoundSlot()) |
| 196 | Q_PRIVATE_SLOT(d_func(), void _q_disconnectedSlot()) |
| 197 | Q_PRIVATE_SLOT(d_func(), void _q_stateChangedSlot(QAbstractSocket::SocketState)) |
| 198 | Q_PRIVATE_SLOT(d_func(), void _q_errorSlot(QAbstractSocket::SocketError)) |
| 199 | Q_PRIVATE_SLOT(d_func(), void _q_readyReadSlot()) |
| 200 | Q_PRIVATE_SLOT(d_func(), void _q_channelReadyReadSlot(int)) |
| 201 | Q_PRIVATE_SLOT(d_func(), void _q_bytesWrittenSlot(qint64)) |
| 202 | Q_PRIVATE_SLOT(d_func(), void _q_channelBytesWrittenSlot(int, qint64)) |
| 203 | Q_PRIVATE_SLOT(d_func(), void _q_readChannelFinishedSlot()) |
| 204 | Q_PRIVATE_SLOT(d_func(), void _q_flushWriteBuffer()) |
| 205 | Q_PRIVATE_SLOT(d_func(), void _q_flushReadBuffer()) |
| 206 | Q_PRIVATE_SLOT(d_func(), void _q_resumeImplementation()) |
| 207 | #if defined(Q_OS_WIN) && !QT_CONFIG(schannel) |
| 208 | Q_PRIVATE_SLOT(d_func(), void _q_caRootLoaded(QSslCertificate,QSslCertificate)) |
| 209 | #endif |
| 210 | friend class QSslSocketBackendPrivate; |
| 211 | }; |
| 212 | |
| 213 | #endif // QT_NO_SSL |
| 214 | |
| 215 | QT_END_NAMESPACE |
| 216 | |
| 217 | #endif |
| 218 | |