1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
5#ifndef QSSLSOCKET_H
6#define QSSLSOCKET_H
7
8#include <QtNetwork/qtnetworkglobal.h>
9#include <QtCore/qlist.h>
10#ifndef QT_NO_SSL
11# include <QtNetwork/qtcpsocket.h>
12# include <QtNetwork/qsslerror.h>
13#endif
14
15QT_BEGIN_NAMESPACE
16
17
18#ifndef QT_NO_SSL
19
20class QDir;
21class QSslCipher;
22class QSslCertificate;
23class QSslConfiguration;
24class QSslPreSharedKeyAuthenticator;
25class QOcspResponse;
26
27class QSslSocketPrivate;
28class Q_NETWORK_EXPORT QSslSocket : public QTcpSocket
29{
30 Q_OBJECT
31 Q_MOC_INCLUDE(<QtNetwork/qsslpresharedkeyauthenticator.h>)
32public:
33 enum SslMode {
34 UnencryptedMode,
35 SslClientMode,
36 SslServerMode
37 };
38
39 enum PeerVerifyMode {
40 VerifyNone,
41 QueryPeer,
42 VerifyPeer,
43 AutoVerifyPeer
44 };
45
46 explicit QSslSocket(QObject *parent = nullptr);
47 ~QSslSocket();
48 void resume() override; // to continue after proxy authentication required, SSL errors etc.
49
50 // Autostarting the SSL client handshake.
51 void connectToHostEncrypted(const QString &hostName, quint16 port, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol);
52 void connectToHostEncrypted(const QString &hostName, quint16 port, const QString &sslPeerName, OpenMode mode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol);
53 bool setSocketDescriptor(qintptr socketDescriptor, SocketState state = ConnectedState,
54 OpenMode openMode = ReadWrite) override;
55
56 using QAbstractSocket::connectToHost;
57 void connectToHost(const QString &hostName, quint16 port, OpenMode openMode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol) override;
58 void disconnectFromHost() override;
59
60 virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value) override;
61 virtual QVariant socketOption(QAbstractSocket::SocketOption option) override;
62
63 SslMode mode() const;
64 bool isEncrypted() const;
65
66 QSsl::SslProtocol protocol() const;
67 void setProtocol(QSsl::SslProtocol protocol);
68
69 QSslSocket::PeerVerifyMode peerVerifyMode() const;
70 void setPeerVerifyMode(QSslSocket::PeerVerifyMode mode);
71
72 int peerVerifyDepth() const;
73 void setPeerVerifyDepth(int depth);
74
75 QString peerVerifyName() const;
76 void setPeerVerifyName(const QString &hostName);
77
78 // From QIODevice
79 qint64 bytesAvailable() const override;
80 qint64 bytesToWrite() const override;
81 bool canReadLine() const override;
82 void close() override;
83 bool atEnd() const override;
84
85 // From QAbstractSocket:
86 void setReadBufferSize(qint64 size) override;
87
88 // Similar to QIODevice's:
89 qint64 encryptedBytesAvailable() const;
90 qint64 encryptedBytesToWrite() const;
91
92 // SSL configuration
93 QSslConfiguration sslConfiguration() const;
94 void setSslConfiguration(const QSslConfiguration &config);
95
96 // Certificate & cipher accessors.
97 void setLocalCertificateChain(const QList<QSslCertificate> &localChain);
98 QList<QSslCertificate> localCertificateChain() const;
99
100 void setLocalCertificate(const QSslCertificate &certificate);
101 void setLocalCertificate(const QString &fileName, QSsl::EncodingFormat format = QSsl::Pem);
102 QSslCertificate localCertificate() const;
103 QSslCertificate peerCertificate() const;
104 QList<QSslCertificate> peerCertificateChain() const;
105 QSslCipher sessionCipher() const;
106 QSsl::SslProtocol sessionProtocol() const;
107 QList<QOcspResponse> ocspResponses() const;
108
109 // Private keys, for server sockets.
110 void setPrivateKey(const QSslKey &key);
111 void setPrivateKey(const QString &fileName, QSsl::KeyAlgorithm algorithm = QSsl::Rsa,
112 QSsl::EncodingFormat format = QSsl::Pem,
113 const QByteArray &passPhrase = QByteArray());
114 QSslKey privateKey() const;
115
116 bool waitForConnected(int msecs = 30000) override;
117 bool waitForEncrypted(int msecs = 30000);
118 bool waitForReadyRead(int msecs = 30000) override;
119 bool waitForBytesWritten(int msecs = 30000) override;
120 bool waitForDisconnected(int msecs = 30000) override;
121
122 QList<QSslError> sslHandshakeErrors() const;
123
124 static bool supportsSsl();
125 static long sslLibraryVersionNumber();
126 static QString sslLibraryVersionString();
127 static long sslLibraryBuildVersionNumber();
128 static QString sslLibraryBuildVersionString();
129
130 static QList<QString> availableBackends();
131 static QString activeBackend();
132 static bool setActiveBackend(const QString &backendName);
133 static QList<QSsl::SslProtocol> supportedProtocols(const QString &backendName = {});
134 static bool isProtocolSupported(QSsl::SslProtocol protocol, const QString &backendName = {});
135 static QList<QSsl::ImplementedClass> implementedClasses(const QString &backendName = {});
136 static bool isClassImplemented(QSsl::ImplementedClass cl, const QString &backendName = {});
137 static QList<QSsl::SupportedFeature> supportedFeatures(const QString &backendName = {});
138 static bool isFeatureSupported(QSsl::SupportedFeature feat, const QString &backendName = {});
139
140 void ignoreSslErrors(const QList<QSslError> &errors);
141 void continueInterruptedHandshake();
142
143public Q_SLOTS:
144 void startClientEncryption();
145 void startServerEncryption();
146 void ignoreSslErrors();
147
148Q_SIGNALS:
149 void encrypted();
150 void peerVerifyError(const QSslError &error);
151 void sslErrors(const QList<QSslError> &errors);
152 void modeChanged(QSslSocket::SslMode newMode);
153 void encryptedBytesWritten(qint64 totalBytes);
154 void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator);
155 void newSessionTicketReceived();
156 void alertSent(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description);
157 void alertReceived(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description);
158 void handshakeInterruptedOnError(const QSslError &error);
159
160protected:
161 qint64 readData(char *data, qint64 maxlen) override;
162 qint64 skipData(qint64 maxSize) override;
163 qint64 writeData(const char *data, qint64 len) override;
164
165private:
166 Q_DECLARE_PRIVATE(QSslSocket)
167 Q_DISABLE_COPY_MOVE(QSslSocket)
168
169 Q_PRIVATE_SLOT(d_func(), void _q_connectedSlot())
170 Q_PRIVATE_SLOT(d_func(), void _q_hostFoundSlot())
171 Q_PRIVATE_SLOT(d_func(), void _q_disconnectedSlot())
172 Q_PRIVATE_SLOT(d_func(), void _q_stateChangedSlot(QAbstractSocket::SocketState))
173 Q_PRIVATE_SLOT(d_func(), void _q_errorSlot(QAbstractSocket::SocketError))
174 Q_PRIVATE_SLOT(d_func(), void _q_readyReadSlot())
175 Q_PRIVATE_SLOT(d_func(), void _q_channelReadyReadSlot(int))
176 Q_PRIVATE_SLOT(d_func(), void _q_bytesWrittenSlot(qint64))
177 Q_PRIVATE_SLOT(d_func(), void _q_channelBytesWrittenSlot(int, qint64))
178 Q_PRIVATE_SLOT(d_func(), void _q_readChannelFinishedSlot())
179 Q_PRIVATE_SLOT(d_func(), void _q_flushWriteBuffer())
180 Q_PRIVATE_SLOT(d_func(), void _q_flushReadBuffer())
181 Q_PRIVATE_SLOT(d_func(), void _q_resumeImplementation())
182};
183
184#endif // QT_NO_SSL
185
186QT_END_NAMESPACE
187
188#endif
189

source code of qtbase/src/network/ssl/qsslsocket.h