Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsslsocket.h
Go to the documentation of this file.
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
16
17
18#ifndef QT_NO_SSL
19
20class QDir;
21class QSslCipher;
22class QSslCertificate;
25class QOcspResponse;
26
28class Q_NETWORK_EXPORT QSslSocket : public QTcpSocket
29{
31 Q_MOC_INCLUDE(<QtNetwork/qsslpresharedkeyauthenticator.h>)
32public:
33 enum SslMode {
36 SslServerMode
37 };
38
43 AutoVerifyPeer
44 };
45
46 explicit QSslSocket(QObject *parent = nullptr);
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
57 void connectToHost(const QString &hostName, quint16 port, OpenMode openMode = ReadWrite, NetworkLayerProtocol protocol = AnyIPProtocol) override;
58 void disconnectFromHost() override;
59
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;
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,
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
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
149 void encrypted();
151 void sslErrors(const QList<QSslError> &errors);
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
187
188#endif
The QAbstractSocket class provides the base functionality common to all socket types.
SocketState
This enum describes the different states in which a socket can be.
virtual void setReadBufferSize(qint64 size)
Sets the size of QAbstractSocket's internal read buffer to be size bytes.
virtual void setSocketOption(QAbstractSocket::SocketOption option, const QVariant &value)
virtual QVariant socketOption(QAbstractSocket::SocketOption option)
bool waitForReadyRead(int msecs=30000) override
This function blocks until new data is available for reading and the \l{QIODevice::}{readyRead()} sig...
qint64 bytesToWrite() const override
Returns the number of bytes that are waiting to be written.
bool waitForBytesWritten(int msecs=30000) override
\reimp
virtual bool waitForDisconnected(int msecs=30000)
Waits until the socket has disconnected, up to msecs milliseconds.
qint64 bytesAvailable() const override
Returns the number of incoming bytes that are waiting to be read.
qint64 skipData(qint64 maxSize) override
\reimp
virtual bool waitForConnected(int msecs=30000)
Waits until the socket is connected, up to msecs milliseconds.
virtual void disconnectFromHost()
Attempts to close the socket.
qint64 writeData(const char *data, qint64 len) override
\reimp
void close() override
Closes the I/O device for the socket and calls disconnectFromHost() to close the socket's connection.
SocketError
This enum describes the socket errors that can occur.
virtual void connectToHost(const QString &hostName, quint16 port, OpenMode mode=ReadWrite, NetworkLayerProtocol protocol=AnyIPProtocol)
Attempts to make a connection to hostName on the given port.
virtual void resume()
virtual bool setSocketDescriptor(qintptr socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite)
Initializes QAbstractSocket with the native socket descriptor socketDescriptor.
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qdir.h:19
virtual bool atEnd() const
Returns true if the current read and write position is at the end of the device (i....
virtual bool canReadLine() const
Returns true if a complete line of data can be read from the device; otherwise returns false.
Definition qlist.h:74
\inmodule QtCore
Definition qobject.h:90
This class represents Online Certificate Status Protocol response.
The QSslCertificate class provides a convenient API for an X509 certificate.
The QSslCipher class represents an SSL cryptographic cipher.
Definition qsslcipher.h:22
The QSslConfiguration class holds the configuration and state of an SSL connection.
The QSslError class provides an SSL error.
Definition qsslerror.h:21
The QSslKey class provides an interface for private and public keys.
Definition qsslkey.h:23
The QSslPreSharedKeyAuthenticator class provides authentication data for pre shared keys (PSK) cipher...
The QSslSocket class provides an SSL encrypted socket for both clients and servers.
Definition qsslsocket.h:29
void sslErrors(const QList< QSslError > &errors)
QSslSocket emits this signal after the SSL handshake to indicate that one or more errors have occurre...
void encryptedBytesWritten(qint64 totalBytes)
void alertSent(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description)
QSslSocket emits this signal if an alert message was sent to a peer.
void peerVerifyError(const QSslError &error)
SslMode
Describes the connection modes available for QSslSocket.
Definition qsslsocket.h:33
@ UnencryptedMode
Definition qsslsocket.h:34
void newSessionTicketReceived()
void preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator)
void encrypted()
This signal is emitted when QSslSocket enters encrypted mode.
void alertReceived(QSsl::AlertLevel level, QSsl::AlertType type, const QString &description)
QSslSocket emits this signal if an alert message was received from a peer.
void handshakeInterruptedOnError(const QSslError &error)
QSslSocket emits this signal if a certificate verification error was found and if early error reporti...
void modeChanged(QSslSocket::SslMode newMode)
This signal is emitted when QSslSocket changes from \l QSslSocket::UnencryptedMode to either \l QSslS...
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
The QTcpSocket class provides a TCP socket.
Definition qtcpsocket.h:18
\inmodule QtCore
Definition qvariant.h:64
else opt state
[0]
SupportedFeature
Definition qssl.h:132
ImplementedClass
Definition qssl.h:121
AlertLevel
Definition qssl.h:77
KeyAlgorithm
Describes the different key algorithms supported by QSslKey.
Definition qssl.h:29
@ Rsa
Definition qssl.h:31
EncodingFormat
Describes supported encoding formats for certificates and keys.
Definition qssl.h:24
@ Pem
Definition qssl.h:25
AlertType
Definition qssl.h:83
SslProtocol
Describes the protocol of the cipher.
Definition qssl.h:43
Combined button and popup list for selecting options.
DBusConnection const char DBusError * error
EGLConfig config
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
EGLOutputPortEXT port
static QString backendName
static bool isEncrypted(const my_mach_header *header)
GLint GLenum GLsizei GLsizei GLsizei depth
GLenum mode
GLenum GLuint GLint level
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum type
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint GLsizei GLsizei GLenum format
GLenum GLsizei len
GLuint GLenum option
static QStringList availableBackends()
#define Q_OBJECT
#define Q_SLOTS
#define Q_MOC_INCLUDE(...)
#define Q_PRIVATE_SLOT(d, signature)
#define Q_SIGNALS
unsigned short quint16
Definition qtypes.h:43
long long qint64
Definition qtypes.h:55
ptrdiff_t qintptr
Definition qtypes.h:71
QByteArray readData()
reply ignoreSslErrors(expectedSslErrors)
config setProtocol(QSsl::TlsV1_2)
sslSocket setSslConfiguration(config)
socket connectToHostEncrypted("imap.example.com", 993)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent