Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qdtls_openssl_p.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#ifndef QDTLS_OPENSSL_P_H
5#define QDTLS_OPENSSL_P_H
6
7#include <QtNetwork/private/qtnetworkglobal_p.h>
8
11#include "qtls_openssl_p.h"
12#include "qopenssl_p.h"
13
15
16#include <QtNetwork/private/qdtls_p.h>
17
18#include <QtNetwork/qsslpresharedkeyauthenticator.h>
19#include <QtNetwork/qhostaddress.h>
20
21#include <QtCore/qsharedpointer.h>
22#include <QtCore/qbytearray.h>
23#include <QtCore/qglobal.h>
24#include <QtCore/qlist.h>
25
26#include <openssl/ossl_typ.h>
27
28//
29// W A R N I N G
30// -------------
31//
32// This file is not part of the Qt API. It exists purely as an
33// implementation detail. This header file may change from version to
34// version without notice, or even be removed.
35//
36// We mean it.
37//
38
41
43
46class QUdpSocket;
47
48namespace dtlsopenssl
49{
50
52{
53public:
54 // Note, bioMethod _must_ outlive BIOs it was used to create. Thus
55 // the order of declarations here matters.
56 using BioMethod = QSharedPointer<BIO_METHOD>;
58
59 using TlsContext = std::shared_ptr<QSslContext>;
61
62 using TlsConnection = QSharedPointer<SSL>;
64
66
69
70 QList<QSslErrorEntry> x509Errors;
71
72 long peeking = false;
74 bool writeSuppressed = false;
75
76 bool init(QDtlsBasePrivate *dtlsBase, QUdpSocket *socket,
77 const QHostAddress &remote, quint16 port,
78 const QByteArray &receivedMessage);
79
80 void reset();
81
84
85#ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1
87#else
89#endif
90
91private:
92
93 bool initTls(QDtlsBasePrivate *dtlsBase);
94 bool initCtxAndConnection(QDtlsBasePrivate *dtlsBase);
95 bool initBIO(QDtlsBasePrivate *dtlsBase);
96 void setLinkMtu(QDtlsBasePrivate *dtlsBase);
97};
98
99} // namespace dtlsopenssl
100
101// The trick with 'right' ancestor in the tree overriding (only once) some shared
102// virtual functions is intentional. Too bad MSVC warns me about ... exactly the
103// feature of C++ that I want to use.
104
107
108class QDtlsClientVerifierOpenSSL : public QTlsPrivate::DtlsCookieVerifier, public QDtlsBasePrivate
109{
110public:
112
113 bool verifyClient(QUdpSocket *socket, const QByteArray &dgram,
114 const QHostAddress &address, quint16 port) override;
115 QByteArray verifiedHello() const override;
116
117private:
119 QByteArray verifiedClientHello;
120};
121
122class QDtlsPrivateOpenSSL : public QTlsPrivate::DtlsCryptograph, public QDtlsBasePrivate
123{
124public:
125
127
128private:
129
130 QSslSocket::SslMode cryptographMode() const override;
131 void setPeer(const QHostAddress &addr, quint16 port, const QString &name) override;
132 QHostAddress peerAddress() const override;
133 quint16 peerPort() const override;
134 void setPeerVerificationName(const QString &name) override;
135 QString peerVerificationName() const override;
136
137 virtual void setDtlsMtuHint(quint16 mtu) override;
138 virtual quint16 dtlsMtuHint() const override;
139
140 virtual QDtls::HandshakeState state() const override;
141 virtual bool isConnectionEncrypted() const override;
142
143 bool startHandshake(QUdpSocket *socket, const QByteArray &datagram) override;
144 bool continueHandshake(QUdpSocket *socket, const QByteArray &datagram) override;
145 bool resumeHandshake(QUdpSocket *socket) override;
146 void abortHandshake(QUdpSocket *socket) override;
147 bool handleTimeout(QUdpSocket *socket) override;
148 void sendShutdownAlert(QUdpSocket *socket) override;
149
150 QList<QSslError> peerVerificationErrors() const override;
151 void ignoreVerificationErrors(const QList<QSslError> &errorsToIgnore) override;
152
153 QSslCipher dtlsSessionCipher() const override;
154 QSsl::SslProtocol dtlsSessionProtocol() const override;
155
156 qint64 writeDatagramEncrypted(QUdpSocket *socket, const QByteArray &datagram) override;
157 QByteArray decryptDatagram(QUdpSocket *socket, const QByteArray &tlsdgram) override;
158
159public:
160 unsigned pskClientCallback(const char *hint, char *identity, unsigned max_identity_len,
161 unsigned char *psk, unsigned max_psk_len);
162 unsigned pskServerCallback(const char *identity, unsigned char *psk,
163 unsigned max_psk_len);
164
165private:
166
167 bool verifyPeer();
168 void storePeerCertificates();
169 bool tlsErrorsWereIgnored() const;
170 void fetchNegotiatedParameters();
171 void reportTimeout();
172 void resetDtls();
173
174 QList<QSslErrorEntry> opensslErrors;
176
177 // We have to externally handle timeouts since we have non-blocking
178 // sockets and OpenSSL(DTLS) with non-blocking UDP sockets does not
179 // know if a timeout has occurred.
180 struct TimeoutHandler : QObject
181 {
182 TimeoutHandler() = default;
183
184 void start(int hintMs = 0);
185 void doubleTimeout();
186 void resetTimeout() {timeoutMs = 1000;}
187 void stop();
188 void timerEvent(QTimerEvent *event) override;
189
190 int timerId = -1;
191 int timeoutMs = 1000;
192
193 QDtlsPrivateOpenSSL *dtlsConnection = nullptr;
194 };
195
196 QDtls *q = nullptr;
198
199 QList<QSslError> tlsErrors;
200 QList<QSslError> tlsErrorsToIgnore;
201 bool connectionEncrypted = false;
202 // We will initialize it 'lazily', just in case somebody wants to move
203 // QDtls to another thread.
204 QScopedPointer<TimeoutHandler> timeoutHandler;
205 bool connectionWasShutdown = false;
206 QSslPreSharedKeyAuthenticator pskAuthenticator;
207 QByteArray identityHint;
208};
209
210QT_WARNING_POP // C4250
211
213
214#endif // QDTLS_OPENSSL_P_H
\inmodule QtCore
Definition qbytearray.h:57
void abortHandshake(QUdpSocket *socket) override
virtual QDtls::HandshakeState state() const override
QHostAddress peerAddress() const override
QSslSocket::SslMode cryptographMode() const override
void ignoreVerificationErrors(const QList< QSslError > &errorsToIgnore) override
QSslCipher dtlsSessionCipher() const override
bool startHandshake(QUdpSocket *socket, const QByteArray &datagram) override
QDtlsPrivateOpenSSL(QDtls *qObject, QSslSocket::SslMode mode)
bool resumeHandshake(QUdpSocket *socket) override
unsigned pskServerCallback(const char *identity, unsigned char *psk, unsigned max_psk_len)
bool handleTimeout(QUdpSocket *socket) override
virtual void setDtlsMtuHint(quint16 mtu) override
quint16 peerPort() const override
qint64 writeDatagramEncrypted(QUdpSocket *socket, const QByteArray &datagram) override
void sendShutdownAlert(QUdpSocket *socket) override
unsigned pskClientCallback(const char *hint, char *identity, unsigned max_identity_len, unsigned char *psk, unsigned max_psk_len)
void setPeer(const QHostAddress &addr, quint16 port, const QString &name) override
QByteArray decryptDatagram(QUdpSocket *socket, const QByteArray &tlsdgram) override
QSsl::SslProtocol dtlsSessionProtocol() const override
virtual bool isConnectionEncrypted() const override
bool continueHandshake(QUdpSocket *socket, const QByteArray &datagram) override
QList< QSslError > peerVerificationErrors() const override
void setPeerVerificationName(const QString &name) override
virtual quint16 dtlsMtuHint() const override
QString peerVerificationName() const override
This class provides encryption for UDP sockets.
Definition qdtls.h:83
HandshakeState
Describes the current state of DTLS handshake.
Definition qdtls.h:89
@ HandshakeNotStarted
Definition qdtls.h:90
The QHostAddress class provides an IP address.
\inmodule QtCore
Definition qobject.h:103
The QSslCipher class represents an SSL cryptographic cipher.
Definition qsslcipher.h:22
The QSslPreSharedKeyAuthenticator class provides authentication data for pre shared keys (PSK) cipher...
SslMode
Describes the connection modes available for QSslSocket.
Definition qsslsocket.h:33
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\inmodule QtCore
Definition qcoreevent.h:366
\reentrant
Definition qudpsocket.h:21
bool init(QDtlsBasePrivate *dtlsBase, QUdpSocket *socket, const QHostAddress &remote, quint16 port, const QByteArray &receivedMessage)
QCryptographicHash::Algorithm hashAlgorithm
QDtlsPrivateOpenSSL * dtlsPrivate
TlsConnection tlsConnection
std::shared_ptr< QSslContext > TlsContext
QList< QSslErrorEntry > x509Errors
SslProtocol
Describes the protocol of the cipher.
Definition qssl.h:50
Combined button and popup list for selecting options.
Namespace containing onternal types that TLS backends implement.
#define QT_WARNING_POP
#define QT_WARNING_DISABLE_MSVC(number)
#define QT_WARNING_PUSH
EGLOutputPortEXT port
GLenum mode
GLuint start
GLuint name
struct _cl_event * event
GLenum const void * addr
GLuint GLuint64EXT address
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define QT_REQUIRE_CONFIG(feature)
unsigned short quint16
Definition qtypes.h:48
long long qint64
Definition qtypes.h:60
QTcpSocket * socket
[1]