QtBase  v6.3.1
src_network_ssl_qdtlscookie.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the documentation of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
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 ** BSD License Usage
18 ** Alternatively, you may use this file under the terms of the BSD license
19 ** as follows:
20 **
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
23 ** met:
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
29 ** distribution.
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
33 **
34 **
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46 **
47 ** $QT_END_LICENSE$
48 **
49 ****************************************************************************/
50 
52 class DtlsServer : public QObject
53 {
54 public:
56  // ...
57 
58 private:
59  void readyRead();
60  // ...
61 
62  QUdpSocket serverSocket;
63  QDtlsClientVerifier verifier;
64  // ...
65 };
66 
67 bool DtlsServer::listen(const QHostAddress &serverAddress, quint16 serverPort)
68 {
69  if (serverSocket.bind(serverAddress, serverPort))
70  connect(&serverSocket, &QUdpSocket::readyRead, this, &DtlsServer::readyRead);
71  return serverSocket.state() == QAbstractSocket::BoundState;
72 }
73 
74 void DtlsServer::readyRead()
75 {
76  QByteArray dgram(serverSocket.pendingDatagramSize(), Qt::Uninitialized);
78  quint16 port = {};
79  serverSocket.readDatagram(dgram.data(), dgram.size(), &address, &port);
80  if (verifiedClients.contains({address, port}) {
81  // This client was verified previously, we either continue the
82  // handshake or decrypt the incoming message.
83  } else if (verifier.verifyClient(&serverSocket, dgram, address, port)) {
84  // Apparently we have a real DTLS client who wants to send us
85  // encrypted datagrams. Remember this client as verified
86  // and proceed with a handshake.
87  } else {
88  // No matching cookie was found in the incoming datagram,
89  // verifyClient() has sent a ClientVerify message.
90  // We'll hear from the client again soon, if they're real.
91  }
92 }
94 
96 void DtlsServer::updateServerSecret()
97 {
98  const QByteArray newSecret(generateCryptoStrongSecret());
99  if (newSecret.size()) {
100  usedCookies.append(newSecret);
102  }
103 }
105 
107 if (!verifier.verifyClient(&socket, message, address, port)) {
108  switch (verifyClient.dtlsError()) {
109  case QDtlsError::NoError:
110  // Not verified yet, but no errors found and we have to wait for the next
111  // message from this client.
112  return;
114  // This error is fatal, nothing we can do about it.
115  // Probably, quit the server after reporting the error.
116  return;
118  // There is some problem in QUdpSocket, handle it (see QUdpSocket::error())
119  return;
121  default:
122  Q_UNREACHABLE();
123  }
124 }
[0]
Definition: server.h:63
bool listen(const QHostAddress &address, quint16 port)
virtual bool bind(const QHostAddress &address, quint16 port=0, BindMode mode=DefaultForPlatform)
SocketState state() const
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
This class implements server-side DTLS cookie generation and verification.
Definition: qdtls.h:80
bool verifyClient(QUdpSocket *socket, const QByteArray &dgram, const QHostAddress &address, quint16 port)
Definition: qdtls.cpp:467
bool setCookieGeneratorParameters(const GeneratorParameters &params)
Definition: qdtls.cpp:423
The QHostAddress class provides an IP address.\inmodule QtNetwork.
Definition: qhostaddress.h:74
void readyRead()
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Definition: qobject.cpp:2772
The QUdpSocket class provides a UDP socket.
Definition: qudpsocket.h:57
qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *host=nullptr, quint16 *port=nullptr)
Definition: qudpsocket.cpp:494
qint64 pendingDatagramSize() const
Definition: qudpsocket.cpp:314
constexpr Initialization Uninitialized
Definition: qnamespace.h:1613
#define Q_UNREACHABLE()
@ TlsInitializationError
@ UnderlyingSocketError
@ InvalidInputParameters
EGLOutputPortEXT port
unsigned short quint16
Definition: qglobal.h:286
GLenum GLuint GLenum GLsizei const GLchar * message
Definition: qopengl.h:270
GLuint GLuint64EXT address
Definition: qopenglext.h:11428
QTcpSocket * socket
[1]
if(!verifier.verifyClient(&socket, message, address, port))
[1]