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
src_network_ssl_qdtls.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4using namespace Qt::StringLiterals;
6// A client initiates a handshake:
11
12// A server accepting an incoming connection; address, port, clientHello are
13// read by QUdpSocket::readDatagram():
16quin16 port = {};
18
22
23// Handshake completion, both for server and client:
24void DtlsConnection::continueHandshake(const QByteArray &datagram)
25{
26 if (dtls.doHandshake(&udpSocket, datagram)) {
27 // Check handshake status:
28 if (dtls.handshakeStatus() == QDlts::HandshakeComplete) {
29 // Secure DTLS connection is now established.
30 }
31 } else {
32 // Error handling.
33 }
34}
35
37
39DtlsClient::DtlsClient()
40{
41 // Some initialization code here ...
42 connect(&clientDtls, &QDtls::handshakeTimeout, this, &DtlsClient::handleTimeout);
43}
44
45void DtlsClient::handleTimeout()
46{
48}
50
52// Sending an encrypted datagram:
53dtlsConnection.writeDatagramEncrypted(&clientSocket, "Hello DTLS server!");
54
55// Decryption:
57socket.readDatagram(encryptedMessage.data(), dgramSize);
58const QByteArray plainText = dtlsConnection.decryptDatagram(&socket, encryptedMessage);
60
62DtlsClient::~DtlsClient()
63{
65}
67
69auto config = QSslConfiguration::defaultDtlsConfiguration();
70config.setDtlsCookieVerificationEnabled(false);
71// Some other customization ...
72dtlsConnection.setDtlsConfiguration(config);
74
76if (!dtls.doHandshake(&socket, dgram)) {
78 dtls.abortAfterError(&socket);
79}
81
83QList<QSslCertificate> cert = QSslCertificate::fromPath("server-certificate.pem"_L1);
85QList<QSslError> expectedSslErrors;
87
90dtls.doHandshake(udpSocket);
92
\inmodule QtCore
Definition qbytearray.h:57
char * data()
\macro QT_NO_CAST_FROM_BYTEARRAY
Definition qbytearray.h:611
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
This class provides encryption for UDP sockets.
Definition qdtls.h:83
bool setPeer(const QHostAddress &address, quint16 port, const QString &verificationName={})
Sets the peer's address, port, and host name and returns true if successful.
Definition qdtls.cpp:542
void handshakeTimeout()
Packet loss can result in timeouts during the handshake phase.
bool handleTimeout(QUdpSocket *socket)
If a timeout occurs during the handshake, the handshakeTimeout() signal is emitted.
Definition qdtls.cpp:873
bool doHandshake(QUdpSocket *socket, const QByteArray &dgram={})
Starts or continues a DTLS handshake.
Definition qdtls.cpp:810
bool shutdown(QUdpSocket *socket)
Sends an encrypted shutdown alert message and closes the DTLS connection.
Definition qdtls.cpp:981
void ignoreVerificationErrors(const QList< QSslError > &errorsToIgnore)
This method tells QDtls to ignore only the errors given in errorsToIgnore.
Definition qdtls.cpp:1190
QDtlsError dtlsError() const
Returns the last error encountered by the connection or QDtlsError::NoError.
Definition qdtls.cpp:1129
The QHostAddress class provides an IP address.
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
void append(parameter_type t)
Definition qlist.h:458
static QList< QSslCertificate > fromPath(const QString &path, QSsl::EncodingFormat format=QSsl::Pem, PatternSyntax syntax=PatternSyntax::FixedString)
The QSslError class provides an SSL error.
Definition qsslerror.h:21
@ SelfSignedCertificate
Definition qsslerror.h:34
\reentrant
Definition qudpsocket.h:21
constexpr Initialization Uninitialized
DBusConnection const char DBusError * error
@ PeerVerificationError
EGLConfig config
EGLOutputPortEXT port
GLuint GLuint64EXT address
connect(quitButton, &QPushButton::clicked, &app, &QCoreApplication::quit, Qt::QueuedConnection)
QTcpSocket * socket
[1]
QList< QSslError > expectedSslErrors
QList< QSslCertificate > cert
[0]
QHostAddress address
QDtls serverDtls
const QByteArray plainText
QDtls clientDtls
QByteArray encryptedMessage(dgramSize)
QByteArray clientHello(serverSocket.pendingDatagramSize(), Qt::Uninitialized)
QUdpSocket clientSocket
[0]
serverDtls doHandshake & serverSocket