此类为 UDP (用户数据报协议) 套接字提供加密。 更多 …
New in version 5.12.
def
abortHandshake
(socket)
def
decryptDatagram
(socket, dgram)
def
doHandshake
(socket[, dgram={}])
def
dtlsConfiguration
()
def
dtlsError
()
def
dtlsErrorString
()
def
handleTimeout
(socket)
def
handshakeState
()
def
ignoreVerificationErrors
(errorsToIgnore)
def
isConnectionEncrypted
()
def
mtuHint
()
def
peerAddress
()
def
peerPort
()
def
peerVerificationErrors
()
def
peerVerificationName
()
def
resumeHandshake
(socket)
def
sessionCipher
()
def
sessionProtocol
()
def
setDtlsConfiguration
(configuration)
def
setMtuHint
(mtuHint)
def
setPeer
(address, port[, verificationName={}])
def
setPeerVerificationName
(name)
def
shutdown
(socket)
def
sslMode
()
def
writeDatagramEncrypted
(socket, dgram)
def
handshakeTimeout
()
def
pskRequired
(authenticator)
QDtlsclass can be used to establish a secure connection with a network peer using User Datagram Protocol (UDP). DTLS connection over essentially connectionless UDP means that two peers first have to successfully complete a TLS handshake by callingdoHandshake(). After the handshake has completed, encrypted datagrams can be sent to the peer usingwriteDatagramEncrypted(). Encrypted datagrams coming from the peer can be decrypted bydecryptDatagram().
QDtlsis designed to work withQUdpSocket。由于QUdpSocketcan receive datagrams coming from different peers, an application must implement demultiplexing, forwarding datagrams coming from different peers to their corresponding instances ofQDtls. An association between a network peer and itsQDtlsobject can be established using the peer’s address and port number. Before starting a handshake, the application must set the peer’s address and port number usingsetPeer().
QDtlsdoes not read datagrams fromQUdpSocket, this is expected to be done by the application, for example, in a slot attached to thereadyRead()signal. Then, these datagrams must be processed byQDtls.注意
QDtlsdoes not 拥有所有权对于QUdpSocket对象。Normally, several datagrams are to be received and sent by both peers during the handshake phase. Upon reading datagrams, server and client must pass these datagrams to
doHandshake()until some error is found orhandshakeState()返回HandshakeComplete:// A client initiates a handshake: QUdpSocket clientSocket; QDtls clientDtls; clientDtls.setPeer(address, port, peerName); clientDtls.doHandshake(&clientSocket); // A server accepting an incoming connection; address, port, clientHello are // read by QUdpSocket::readDatagram(): QByteArray clientHello(serverSocket.pendingDatagramSize(), Qt::Uninitialized); QHostAddress address; quin16 port = {}; serverSocket.readDatagram(clientHello.data(), clientHello.size(), &address, &port); QDtls serverDtls; serverDtls.setPeer(address, port); serverDtls.doHandshake(&serverSocket, clientHello); // Handshake completion, both for server and client: void DtlsConnection::continueHandshake(const QByteArray &datagram) { if (dtls.doHandshake(&udpSocket, datagram)) { // Check handshake status: if (dtls.handshakeStatus() == QDlts::HandshakeComplete) { // Secure DTLS connection is now established. } } else { // Error handling. } }For a server, the first call to
doHandshake()requires a non-empty datagram containing a ClientHello message. If the server also deploysQDtlsClientVerifier, the first ClientHello message is expected to be the one verified byQDtlsClientVerifier.In case the peer’s identity cannot be validated during the handshake, the application must inspect errors returned by
peerVerificationErrors()and then either ignore errors by callingignoreVerificationErrors()or abort the handshake by callingabortHandshake(). If errors were ignored, the handshake can be resumed by callingresumeHandshake().After the handshake has been completed, datagrams can be sent to and received from the network peer securely:
// Sending an encrypted datagram: dtlsConnection.writeDatagramEncrypted(&clientSocket, "Hello DTLS server!"); // Decryption: QByteArray encryptedMessage(dgramSize); socket.readDatagram(encryptedMessage.data(), dgramSize); const QByteArray plainText = dtlsConnection.decryptDatagram(&socket, encryptedMessage);A DTLS connection may be closed using
shutdown().DtlsClient::~DtlsClient() { clientDtls.shutdown(&clientSocket); }警告
It’s recommended to call
shutdown()before destroying the client’sQDtlsobject if you are planning to re-use the same port number to connect to the server later. Otherwise, the server may drop incoming ClientHello messages, see RFC 6347, section 4.2.8 for more details and implementation hints.If the server does not use
QDtlsClientVerifier, it must configure itsQDtlsobjects to disable the cookie verification procedure:auto config = QSslConfiguration::defaultDtlsConfiguration(); config.setDtlsCookieVerificationEnabled(false); // Some other customization ... dtlsConnection.setDtlsConfiguration(config);A server that uses cookie verification with non-default generator parameters must set the same parameters for its
QDtlsobject before starting the handshake.注意
The DTLS protocol leaves Path Maximum Transmission Unit (PMTU) discovery to the application. The application may provide
QDtlswith the MTU usingsetMtuHint(). This hint affects only the handshake phase, since only handshake messages can be fragmented and reassembled by the DTLS. All other messages sent by the application must fit into a single datagram.注意
DTLS-specific headers add some overhead to application data further reducing the possible message size.
警告
A server configured to reply with HelloVerifyRequest will drop all fragmented ClientHello messages, never starting a handshake.
DTLS 服务器 and DTLS 客户端 examples illustrate how to use
QDtlsin applications.另请参阅
QUdpSocketQDtlsClientVerifierHandshakeStateQDtlsErrorQSslConfiguration
QDtls
(
mode
[
,
parent=None
]
)
¶
- param parent
QObject- param mode
SslMode
创建
QDtls
对象,
parent
会被传递给
QObject
构造函数。
mode
is
SslServerMode
for a server-side DTLS connection or
SslClientMode
for a client.
另请参阅
sslMode()
SslMode
PySide2.QtNetwork.QDtls.
HandshakeState
¶
描述 DTLS 握手的当前状态。
This enum describes the current state of DTLS handshake for a
QDtls
连接。
|
常量 |
描述 |
|---|---|
|
QDtls.HandshakeNotStarted |
什么都没做。 |
|
QDtls.HandshakeInProgress |
握手被初始,且到目前为止没有发现错误。 |
|
QDtls.PeerVerificationFailed |
The identity of the peer can’t be established. |
|
QDtls.HandshakeComplete |
握手成功完成并建立加密连接。 |
PySide2.QtNetwork.QDtls.
abortHandshake
(
socket
)
¶
socket
–
QUdpSocket
bool
Aborts the ongoing handshake. Returns true if one was on-going on
socket
; otherwise, sets a suitable error and returns false.
PySide2.QtNetwork.QDtls.
decryptDatagram
(
socket
,
dgram
)
¶
socket
–
QUdpSocket
dgram
–
QByteArray
QByteArray
Decrypts
dgram
and returns its contents as plain text. The handshake must be completed before datagrams can be decrypted. Depending on the type of the TLS message the connection may write into
socket
, which must be a valid pointer.
PySide2.QtNetwork.QDtls.
doHandshake
(
socket
[
,
dgram={}
]
)
¶
socket
–
QUdpSocket
dgram
–
QByteArray
bool
Starts or continues a DTLS handshake.
socket
must be a valid pointer. When starting a server-side DTLS handshake,
dgram
must contain the initial ClientHello message read from
QUdpSocket
. This function returns
true
if no error was found. Handshake state can be tested using
handshakeState()
.
false
return means some error occurred, use
dtlsError()
for more detailed information.
注意
If the identity of the peer can’t be established, the error is set to QDtlsError::PeerVerificationError. If you want to ignore verification errors and continue connecting, you must call
ignoreVerificationErrors()
and then
resumeHandshake()
. If the errors cannot be ignored, you must call
abortHandshake()
.
if (!dtls.doHandshake(&socket, dgram)) {
if (dtls.dtlsError() == QDtlsError::PeerVerificationError)
dtls.abortAfterError(&socket);
}
PySide2.QtNetwork.QDtls.
dtlsConfiguration
(
)
¶
Returns either the default DTLS configuration or the configuration set by an earlier call to
setDtlsConfiguration()
.
PySide2.QtNetwork.QDtls.
dtlsError
(
)
¶
QDtlsError
Returns the last error encountered by the connection or QDtlsError::NoError.
另请参阅
dtlsErrorString()
QDtlsError
PySide2.QtNetwork.QDtls.
dtlsErrorString
(
)
¶
unicode
Returns a textual description for the last error encountered by the connection or empty string.
另请参阅
PySide2.QtNetwork.QDtls.
handleTimeout
(
socket
)
¶
socket
–
QUdpSocket
bool
If a timeout occures during the handshake, the
handshakeTimeout()
signal is emitted. The application must call to retransmit handshake messages; returns
true
if a timeout has occurred, false otherwise.
socket
must be a valid pointer.
另请参阅
PySide2.QtNetwork.QDtls.
handshakeState
(
)
¶
返回当前握手状态为此
QDtls
.
另请参阅
doHandshake()
HandshakeState
PySide2.QtNetwork.QDtls.
handshakeTimeout
(
)
¶
PySide2.QtNetwork.QDtls.
ignoreVerificationErrors
(
errorsToIgnore
)
¶
errorsToIgnore –
此方法告诉
QDtls
to ignore only the errors given in
errorsToIgnore
.
If, for instance, you want to connect to a server that uses a self-signed certificate, consider the following snippet:
QList<QSslCertificate> cert = QSslCertificate::fromPath(QLatin1String("server-certificate.pem"));
QSslError error(QSslError::SelfSignedCertificate, cert.at(0));
QList<QSslError> expectedSslErrors;
expectedSslErrors.append(error);
QDtls dtls;
dtls.ignoreVerificationErrors(expectedSslErrors);
dtls.doHandshake(udpSocket);
You can also call this function after
doHandshake()
encountered the QDtlsError::PeerVerificationError error, and then resume the handshake by calling
resumeHandshake()
.
Later calls to this function will replace the list of errors that were passed in previous calls. You can clear the list of errors you want to ignore by calling this function with an empty list.
PySide2.QtNetwork.QDtls.
isConnectionEncrypted
(
)
¶
bool
返回
true
if DTLS handshake completed successfully.
PySide2.QtNetwork.QDtls.
mtuHint
(
)
¶
quint16
Returns the value previously set by
setMtuHint()
. The default value is 0.
另请参阅
PySide2.QtNetwork.QDtls.
peerAddress
(
)
¶
Returns the peer’s address, set by
setPeer()
,或
Null
.
另请参阅
PySide2.QtNetwork.QDtls.
peerPort
(
)
¶
quint16
Returns the peer’s port number, set by
setPeer()
, or 0.
另请参阅
PySide2.QtNetwork.QDtls.
peerVerificationErrors
(
)
¶
Returns errors found while establishing the identity of the peer.
If you want to continue connecting despite the errors that have occurred, you must call
ignoreVerificationErrors()
.
PySide2.QtNetwork.QDtls.
peerVerificationName
(
)
¶
unicode
Returns the host name set by
setPeer()
or
setPeerVerificationName()
. The default value is an empty string.
PySide2.QtNetwork.QDtls.
pskRequired
(
authenticator
)
¶
authenticator
–
QSslPreSharedKeyAuthenticator
PySide2.QtNetwork.QDtls.
resumeHandshake
(
socket
)
¶
socket
–
QUdpSocket
bool
If peer verification errors were ignored during the handshake, resumes and completes the handshake and returns
true
.
socket
must be a valid pointer. Returns
false
if the handshake could not be resumed.
PySide2.QtNetwork.QDtls.
sessionCipher
(
)
¶
Returns the cryptographic
cipher
used by this connection, or a null cipher if the connection isn’t encrypted. The cipher for the session is selected during the handshake phase. The cipher is used to encrypt and decrypt data.
QSslConfiguration
provides functions for setting the ordered list of ciphers from which the handshake phase will eventually select the session cipher. This ordered list must be in place before the handshake phase begins.
PySide2.QtNetwork.QDtls.
sessionProtocol
(
)
¶
SslProtocol
Returns the DTLS protocol version used by this connection, or UnknownProtocol if the connection isn’t encrypted yet. The protocol for the connection is selected during the handshake phase.
setDtlsConfiguration()
can set the preferred version before the handshake starts.
PySide2.QtNetwork.QDtls.
setDtlsConfiguration
(
configuration
)
¶
configuration
–
QSslConfiguration
bool
Sets the connection’s TLS configuration from
configuration
并返回
true
若成功。
注意
This function must be called before the handshake starts.
PySide2.QtNetwork.QDtls.
setMtuHint
(
mtuHint
)
¶
mtuHint
–
quint16
mtuHint
is the maximum transmission unit (MTU), either discovered or guessed by the application. The application is not required to set this value.
另请参阅
mtuHint()
PathMtuSocketOption
PySide2.QtNetwork.QDtls.
setPeer
(
address
,
port
[
,
verificationName={}
]
)
¶
address
–
QHostAddress
port
–
quint16
verificationName – unicode
bool
Sets the peer’s address,
port
, and host name and returns
true
若成功。
address
must not be null, multicast, or broadcast.
verificationName
is the host name used for the certificate validation.
PySide2.QtNetwork.QDtls.
setPeerVerificationName
(
name
)
¶
name – unicode
bool
Sets the host
name
that will be used for the certificate validation and returns
true
若成功。
注意
This function must be called before the handshake starts.
PySide2.QtNetwork.QDtls.
shutdown
(
socket
)
¶
socket
–
QUdpSocket
bool
Sends an encrypted shutdown alert message and closes the DTLS connection. Handshake state changes to
HandshakeNotStarted
.
socket
must be a valid pointer. This function returns
true
当成功时。
另请参阅
PySide2.QtNetwork.QDtls.
sslMode
(
)
¶
SslMode
返回
SslServerMode
for a server-side connection and
SslClientMode
for a client.
另请参阅
QDtls()
SslMode
PySide2.QtNetwork.QDtls.
writeDatagramEncrypted
(
socket
,
dgram
)
¶
socket
–
QUdpSocket
dgram
–
QByteArray
qint64
Encrypts
dgram
and writes the encrypted data into
socket
. Returns the number of bytes written, or -1 in case of error. The handshake must be completed before writing encrypted data.
socket
must be a valid pointer.