Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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;
5//! [0]
6// A client initiates a handshake:
9clientDtls.setPeer(address, port, peerName);
10clientDtls.doHandshake(&clientSocket);
11
12// A server accepting an incoming connection; address, port, clientHello are
13// read by QUdpSocket::readDatagram():
14QByteArray clientHello(serverSocket.pendingDatagramSize(), Qt::Uninitialized);
17serverSocket.readDatagram(clientHello.data(), clientHello.size(), &address, &port);
18
20serverDtls.setPeer(address, port);
21serverDtls.doHandshake(&serverSocket, clientHello);
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
36//! [0]
37
38//! [1]
39DtlsClient::DtlsClient()
40{
41 // Some initialization code here ...
42 connect(&clientDtls, &QDtls::handshakeTimeout, this, &DtlsClient::handleTimeout);
43}
44
45void DtlsClient::handleTimeout()
46{
47 clientDtls.handleTimeout(&clientSocket);
48}
49//! [1]
50
51//! [2]
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);
59//! [2]
60
61//! [3]
62DtlsClient::~DtlsClient()
63{
64 clientDtls.shutdown(&clientSocket);
65}
66//! [3]
67
68//! [4]
69auto config = QSslConfiguration::defaultDtlsConfiguration();
70config.setDtlsCookieVerificationEnabled(false);
71// Some other customization ...
72dtlsConnection.setDtlsConfiguration(config);
73//! [4]
74
75//! [5]
76if (!dtls.doHandshake(&socket, dgram)) {
77 if (dtls.dtlsError() == QDtlsError::PeerVerificationError)
78 dtls.abortAfterError(&socket);
79}
80//! [5]
81
82//! [6]
83QList<QSslCertificate> cert = QSslCertificate::fromPath("server-certificate.pem"_L1);
84QSslError error(QSslError::SelfSignedCertificate, cert.at(0));
86expectedSslErrors.append(error);
87
89dtls.ignoreVerificationErrors(expectedSslErrors);
90dtls.doHandshake(udpSocket);
91//! [6]
QJSValue error
QList< QSslCertificate > cert
[0]
QList< QSslError > expectedSslErrors
QHostAddress address
QDtls serverDtls
quin16 port
const QByteArray plainText
auto config
[3]
QDtls clientDtls
QByteArray clientHello(serverSocket.pendingDatagramSize(), Qt::Uninitialized)
QUdpSocket clientSocket
[0]
QByteArray encryptedMessage(dgramSize)