The QSslSocket 类为客户端和服务器两者提供 SSL (安全套接字层) 加密套接字。 更多...
| 头: | #include <QSslSocket> |
| Since: | Qt 4.3 |
| 继承: | QTcpSocket |
注意: 此类的所有函数 可重入 .
| enum | PeerVerifyMode { VerifyNone, QueryPeer, VerifyPeer, AutoVerifyPeer } |
| enum | SslMode { UnencryptedMode, SslClientMode, SslServerMode } |
| QSslSocket (QObject * parent = 0) | |
| ~QSslSocket () | |
| void | abort () |
| void | addCaCertificate (const QSslCertificate & certificate ) |
| bool | addCaCertificates (const QString & path , QSsl::EncodingFormat format = QSsl::Pem, QRegExp::PatternSyntax syntax = QRegExp::FixedString) |
| void | addCaCertificates (const QList<QSslCertificate> & certificates ) |
| QList<QSslCertificate> | caCertificates () const |
| QList<QSslCipher> | ciphers () const |
| void | connectToHostEncrypted (const QString & hostName , quint16 port , OpenMode mode = ReadWrite) |
| void | connectToHostEncrypted (const QString & hostName , quint16 port , const QString & sslPeerName , OpenMode mode = ReadWrite) |
| qint64 | encryptedBytesAvailable () const |
| qint64 | encryptedBytesToWrite () const |
| bool | flush () |
| void | ignoreSslErrors (const QList<QSslError> & errors ) |
| bool | isEncrypted () const |
| QSslCertificate | localCertificate () const |
| SslMode | mode () const |
| QSslCertificate | peerCertificate () const |
| QList<QSslCertificate> | peerCertificateChain () const |
| int | peerVerifyDepth () const |
| QSslSocket::PeerVerifyMode | peerVerifyMode () const |
| QString | peerVerifyName () const |
| QSslKey | privateKey () const |
| QSsl::SslProtocol | protocol () const |
| QSslCipher | sessionCipher () const |
| void | setCaCertificates (const QList<QSslCertificate> & certificates ) |
| void | setCiphers (const QList<QSslCipher> & ciphers ) |
| void | setCiphers (const QString & ciphers ) |
| void | setLocalCertificate (const QSslCertificate & certificate ) |
| void | setLocalCertificate (const QString & path , QSsl::EncodingFormat format = QSsl::Pem) |
| void | setPeerVerifyDepth (int depth ) |
| void | setPeerVerifyMode (QSslSocket::PeerVerifyMode mode ) |
| void | setPeerVerifyName (const QString & hostName ) |
| void | setPrivateKey (const QSslKey & key ) |
| void | setPrivateKey (const QString & fileName , QSsl::KeyAlgorithm algorithm = QSsl::Rsa, QSsl::EncodingFormat format = QSsl::Pem, const QByteArray & passPhrase = QByteArray()) |
| void | setProtocol (QSsl::SslProtocol protocol ) |
| void | setReadBufferSize (qint64 size ) |
| bool | setSocketDescriptor (int socketDescriptor , SocketState state = ConnectedState, OpenMode openMode = ReadWrite) |
| void | setSocketOption (QAbstractSocket::SocketOption option , const QVariant & value ) |
| void | setSslConfiguration (const QSslConfiguration & configuration ) |
| QVariant | socketOption (QAbstractSocket::SocketOption option ) |
| QSslConfiguration | sslConfiguration () const |
| QList<QSslError> | sslErrors () const |
| bool | waitForConnected (int msecs = 30000) |
| bool | waitForDisconnected (int msecs = 30000) |
| bool | waitForEncrypted (int msecs = 30000) |
| virtual bool | atEnd () const |
| virtual qint64 | bytesAvailable () const |
| virtual qint64 | bytesToWrite () const |
| virtual bool | canReadLine () const |
| virtual void | close () |
| virtual bool | waitForBytesWritten (int msecs = 30000) |
| virtual bool | waitForReadyRead (int msecs = 30000) |
| void | ignoreSslErrors () |
| void | startClientEncryption () |
| void | startServerEncryption () |
| void | encrypted () |
| void | encryptedBytesWritten (qint64 written ) |
| void | modeChanged (QSslSocket::SslMode mode ) |
| void | peerVerifyError (const QSslError & error ) |
| void | sslErrors (const QList<QSslError> & errors ) |
| void | addDefaultCaCertificate (const QSslCertificate & certificate ) |
| bool | addDefaultCaCertificates (const QString & path , QSsl::EncodingFormat encoding = QSsl::Pem, QRegExp::PatternSyntax syntax = QRegExp::FixedString) |
| void | addDefaultCaCertificates (const QList<QSslCertificate> & certificates ) |
| QList<QSslCertificate> | defaultCaCertificates () |
| QList<QSslCipher> | defaultCiphers () |
| void | setDefaultCaCertificates (const QList<QSslCertificate> & certificates ) |
| void | setDefaultCiphers (const QList<QSslCipher> & ciphers ) |
| QList<QSslCipher> | supportedCiphers () |
| bool | supportsSsl () |
| QList<QSslCertificate> | systemCaCertificates () |
| virtual qint64 | readData (char * data , qint64 maxlen ) |
| virtual qint64 | writeData (const char * data , qint64 len ) |
The QSslSocket 类为客户端和服务器两者提供 SSL (安全套接字层) 加密套接字。
QSslSocket establishes a secure, encrypted TCP connection you can use for transmitting encrypted data. It can operate in both client and server mode, and it supports modern SSL protocols, including SSLv3 and TLSv1. By default, QSslSocket uses TLSv1, but you can change the SSL protocol by calling setProtocol () 只要在握手开始之前这样做。
SSL 加密运转于现有 TCP 流之上,后于套接字进入 ConnectedState 。有 2 种简单方式建立安全连接使用 QSslSocket :立即 SSL 握手,或以非加密模式建立连接后延迟发生 SSL 握手。
最常见方式是使用 QSslSocket 构造对象并启动安全连接通过调用 connectToHostEncrypted ()。此方法立即启动 SSL 握手,一旦连接已建立。
QSslSocket *socket = new QSslSocket(this); connect(socket, SIGNAL(encrypted()), this, SLOT(ready())); socket->connectToHostEncrypted("imap.example.com", 993);
就像纯 QTcpSocket , QSslSocket 进入 HostLookupState , ConnectingState ,及最终 ConnectedState ,若连接成功。然后握手自动开始,且若它成功, encrypted () 信号被发射以指示套接字已进入加密状态且准备使用。
注意,之后可以立即把数据写入套接字,当返回从 connectToHostEncrypted () (即:之前的 encrypted () 信号被发射)。数据被队列在 QSslSocket until after the encrypted () 信号发射。
An example of using the delayed SSL handshake to secure an existing connection is the case where an SSL server secures an incoming connection. Suppose you create an SSL server class as a subclass of QTcpServer . You would override QTcpServer::incomingConnection () with something like the example below, which first constructs an instance of QSslSocket and then calls setSocketDescriptor () to set the new socket's descriptor to the existing one passed in. It then initiates the SSL handshake by calling startServerEncryption ().
void SslServer::incomingConnection(int socketDescriptor) { QSslSocket *serverSocket = new QSslSocket; if (serverSocket->setSocketDescriptor(socketDescriptor)) { connect(serverSocket, SIGNAL(encrypted()), this, SLOT(ready())); serverSocket->startServerEncryption(); } else { delete serverSocket; } }
若出现错误, QSslSocket 发射 sslErrors () signal. In this case, if no action is taken to ignore the error(s), the connection is dropped. To continue, despite the occurrence of an error, you can call ignoreSslErrors (), either from within this slot after the error occurs, or any time after construction of the QSslSocket and before the connection is attempted. This will allow QSslSocket to ignore the errors it encounters when establishing the identity of the peer. Ignoring errors during an SSL handshake should be used with caution, since a fundamental characteristic of secure connections is that they should be established with a successful handshake.
Once encrypted, you use QSslSocket as a regular QTcpSocket 。当 readyRead () 被发射,可以调用 read (), canReadLine () 和 readLine (),或 getChar () to read decrypted data from QSslSocket 's internal buffer, and you can call write () 或 putChar () to write data back to the peer. QSslSocket will automatically encrypt the written data for you, and emit encryptedBytesWritten () once the data has been written to the peer.
As a convenience, QSslSocket supports QTcpSocket 's blocking functions waitForConnected (), waitForReadyRead (), waitForBytesWritten (),和 waitForDisconnected ()。它还提供 waitForEncrypted (), which will block the calling thread until an encrypted connection has been established.
QSslSocket socket; socket.connectToHostEncrypted("http.example.com", 443); if (!socket.waitForEncrypted()) { qDebug() << socket.errorString(); return false; } socket.write("GET / HTTP/1.0\r\n\r\n"); while (socket.waitForReadyRead()) qDebug() << socket.readAll().data();
QSslSocket provides an extensive, easy-to-use API for handling cryptographic ciphers, private keys, and local, peer, and Certification Authority (CA) certificates. It also provides an API for handling errors that occur during the handshake phase.
还可以定制下列特征:
注意: If available, root certificates on Unix (excluding Mac OS X) will be loaded on demand from the standard certificate directories. If you do not want to load root certificates on demand, you need to call either the static function setDefaultCaCertificates () before the first SSL handshake is made in your application, (e.g. via " QSslSocket::setDefaultCaCertificates ( QSslSocket::systemCaCertificates ());"), or call setCaCertificates () on your QSslSocket instance prior to the SSL handshake.
For more information about ciphers and certificates, refer to QSslCipher and QSslCertificate .
This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit ( http://www.openssl.org/ ).
注意: Be aware of the difference between the bytesWritten () signal and the encryptedBytesWritten () signal. For a QTcpSocket , bytesWritten () will get emitted as soon as data has been written to the TCP socket. For a QSslSocket , bytesWritten () will get emitted when the data is being encrypted and encryptedBytesWritten () will get emitted as soon as data has been written to the TCP socket.
On Symbian, processes which use this class must have the
NetworkServices
platform security capability. If the client process lacks this capability, operations will fail.
Platform security capabilities are added via the TARGET.CAPABILITY qmake variable.
另请参阅 QSslCertificate , QSslCipher ,和 QSslError .
描述对等验证模式为 QSslSocket . The default mode is AutoVerifyPeer, which selects an appropriate mode depending on the socket's QSocket::SslMode.
| 常量 | 值 | 描述 |
|---|---|---|
QSslSocket::VerifyNone
|
0
|
QSslSocket will not request a certificate from the peer. You can set this mode if you are not interested in the identity of the other side of the connection. The connection will still be encrypted, and your socket will still send its local certificate to the peer if it's requested. |
QSslSocket::QueryPeer
|
1
|
QSslSocket will request a certificate from the peer, but does not require this certificate to be valid. This is useful when you want to display peer certificate details to the user without affecting the actual SSL handshake. This mode is the default for servers. |
QSslSocket::VerifyPeer
|
2
|
QSslSocket will request a certificate from the peer during the SSL handshake phase, and requires that this certificate is valid. On failure, QSslSocket 将发射 QSslSocket::sslErrors () signal. This mode is the default for clients. |
QSslSocket::AutoVerifyPeer
|
3
|
QSslSocket will automatically use QueryPeer for server sockets and VerifyPeer for client sockets. |
该枚举在 Qt 4.4 引入或被修改。
另请参阅 QSslSocket::peerVerifyMode ().
描述可用连接模式为 QSslSocket .
| 常量 | 值 | 描述 |
|---|---|---|
QSslSocket::UnencryptedMode
|
0
|
The socket is unencrypted. Its behavior is identical to QTcpSocket . |
QSslSocket::SslClientMode
|
1
|
The socket is a client-side SSL socket. It is either alreayd encrypted, or it is in the SSL handshake phase (see QSslSocket::isEncrypted ()). |
QSslSocket::SslServerMode
|
2
|
The socket is a server-side SSL socket. It is either already encrypted, or it is in the SSL handshake phase (see QSslSocket::isEncrypted ()). |
构造 QSslSocket 对象。 parent 会被传递给 QObject 的构造函数。新套接字的 cipher suite is set to the one returned by the static method defaultCiphers ().
销毁 QSslSocket .
中止当前连接并重置套接字。不像 disconnectFromHost (),此函数立即关闭套接字,清零任何写入缓冲待决数据。
另请参阅 disconnectFromHost () 和 close ().
添加 certificate to this socket's CA certificate database. The CA certificate database is used by the socket during the handshake phase to validate the peer's certificate.
To add multiple certificates, use addCaCertificates ().
另请参阅 caCertificates () 和 setCaCertificates ().
Searches all files in the path for certificates encoded in the specified format and adds them to this socket's CA certificate database. path can be explicit, or it can contain wildcards in the format specified by syntax . Returns true if one or more certificates are added to the socket's CA certificate database; otherwise returns false.
The CA certificate database is used by the socket during the handshake phase to validate the peer's certificate.
对于更准确控制,使用 addCaCertificate ().
另请参阅 addCaCertificate () 和 QSslCertificate::fromPath ().
添加 certificates to this socket's CA certificate database. The CA certificate database is used by the socket during the handshake phase to validate the peer's certificate.
对于更准确控制,使用 addCaCertificate ().
另请参阅 caCertificates () 和 addDefaultCaCertificate ().
[static]
void
QSslSocket::
addDefaultCaCertificate
(const
QSslCertificate
&
certificate
)
添加 certificate to the default CA certificate database. Each SSL socket's CA certificate database is initialized to the default CA certificate database.
另请参阅 defaultCaCertificates () 和 addCaCertificates ().
[static]
bool
QSslSocket::
addDefaultCaCertificates
(const
QString
&
path
,
QSsl::EncodingFormat
encoding
= QSsl::Pem,
QRegExp::PatternSyntax
syntax
= QRegExp::FixedString)
Searches all files in the path for certificates with the specified encoding and adds them to the default CA certificate database. path can be an explicit file, or it can contain wildcards in the format specified by syntax . Returns true if any CA certificates are added to the default database.
Each SSL socket's CA certificate database is initialized to the default CA certificate database.
另请参阅 defaultCaCertificates (), addCaCertificates (),和 addDefaultCaCertificate ().
[static]
void
QSslSocket::
addDefaultCaCertificates
(const
QList
<
QSslCertificate
> &
certificates
)
添加 certificates to the default CA certificate database. Each SSL socket's CA certificate database is initialized to the default CA certificate database.
另请参阅 defaultCaCertificates () 和 addCaCertificates ().
[虚拟]
bool
QSslSocket::
atEnd
() const
重实现自 QIODevice::atEnd ().
[虚拟]
qint64
QSslSocket::
bytesAvailable
() const
重实现自 QIODevice::bytesAvailable ().
Returns the number of decrypted bytes that are immediately available for reading.
[虚拟]
qint64
QSslSocket::
bytesToWrite
() const
重实现自 QIODevice::bytesToWrite ().
Returns the number of unencrypted bytes that are waiting to be encrypted and written to the network.
Returns this socket's CA certificate database. The CA certificate database is used by the socket during the handshake phase to validate the peer's certificate. It can be moodified prior to the handshake with addCaCertificate (), addCaCertificates (),和 setCaCertificates ().
注意: On Unix, this method may return an empty list if the root certificates are loaded on demand.
另请参阅 addCaCertificate (), addCaCertificates (),和 setCaCertificates ().
[虚拟]
bool
QSslSocket::
canReadLine
() const
重实现自 QIODevice::canReadLine ().
Returns true if you can read one while line (terminated by a single ASCII '\n' character) of decrypted characters; otherwise, false is returned.
Returns this socket's current cryptographic cipher suite. This list is used during the socket's handshake phase for choosing a session cipher. The returned list of ciphers is ordered by descending preference. (i.e., the first cipher in the list is the most preferred cipher). The session cipher will be the first one in the list that is also supported by the peer.
By default, the handshake phase can choose any of the ciphers supported by this system's SSL libraries, which may vary from system to system. The list of ciphers supported by this system's SSL libraries is returned by supportedCiphers (). You can restrict the list of ciphers used for choosing the session cipher for this socket by calling setCiphers () with a subset of the supported ciphers. You can revert to using the entire set by calling setCiphers () with the list returned by supportedCiphers ().
You can restrict the list of ciphers used for choosing the session cipher for all sockets by calling setDefaultCiphers () with a subset of the supported ciphers. You can revert to using the entire set by calling setCiphers () with the list returned by supportedCiphers ().
另请参阅 setCiphers (), defaultCiphers (), setDefaultCiphers (),和 supportedCiphers ().
[虚拟]
void
QSslSocket::
close
()
重实现自 QIODevice::close ().
启动加密连接到设备 hostName on port ,使用 mode 作为 OpenMode 。这相当于调用 connectToHost () 以建立连接,紧随其后调用 startClientEncryption ().
QSslSocket 首先进入 HostLookupState 。然后,在进入事件循环或某一 waitFo … () 函数后,进入 ConnectingState ,发射 connected (),然后初启 SSL 客户端握手。每次状态改变时, QSslSocket 发射信号 stateChanged ().
在初启 SSL (安全套接字层) 客户端握手后,若无法建立对等方身份,信号 sslErrors () is emitted. If you want to ignore the errors and continue connecting, you must call ignoreSslErrors (), either from inside a slot function connected to the sslErrors () signal, or prior to entering encrypted mode. If ignoreSslErrors () is not called, the connection is dropped, signal disconnected () is emitted, and QSslSocket returns to the UnconnectedState .
若 SSL (安全套接字层) 握手成功, QSslSocket 发射 encrypted ().
QSslSocket socket; connect(&socket, SIGNAL(encrypted()), receiver, SLOT(socketEncrypted())); socket.connectToHostEncrypted("imap", 993); socket->write("1 CAPABILITY\r\n");
注意: The example above shows that text can be written to the socket immediately after requesting the encrypted connection, before the encrypted () signal has been emitted. In such cases, the text is queued in the object and written to the socket after the connection is established and the encrypted () 信号已发射。
默认为 mode is ReadWrite .
若想要创建 QSslSocket on the server side of a connection, you should instead call startServerEncryption () upon receiving the incoming connection through QTcpServer .
另请参阅 connectToHost (), startClientEncryption (), waitForConnected (),和 waitForEncrypted ().
这是重载函数。
In addition to the original behaviour of connectToHostEncrypted , this overloaded method enables the usage of a different hostname ( sslPeerName ) for the certificate validation instead of the one used for the TCP connection ( hostName ).
该函数在 Qt 4.6 引入。
另请参阅 connectToHostEncrypted ().
[static]
QList
<
QSslCertificate
> QSslSocket::
defaultCaCertificates
()
Returns the current default CA certificate database. This database is originally set to your system's default CA certificate database. If no system default database is found, an empty database will be returned. You can override the default CA certificate database with your own CA certificate database using setDefaultCaCertificates ().
Each SSL socket's CA certificate database is initialized to the default CA certificate database.
注意: On Unix, this method may return an empty list if the root certificates are loaded on demand.
另请参阅 setDefaultCaCertificates () 和 caCertificates ().
[static]
QList
<
QSslCipher
> QSslSocket::
defaultCiphers
()
Returns the default cryptographic cipher suite for all sockets in this application. This list is used during the socket's handshake phase when negotiating with the peer to choose a session cipher. The list is ordered by preference (i.e., the first cipher in the list is the most preferred cipher).
By default, the handshake phase can choose any of the ciphers supported by this system's SSL libraries, which may vary from system to system. The list of ciphers supported by this system's SSL libraries is returned by supportedCiphers ().
另请参阅 setDefaultCiphers () 和 supportedCiphers ().
[signal]
void
QSslSocket::
encrypted
()
此信号发射当 QSslSocket 进入加密模式。在发射此信号后, QSslSocket::isEncrypted () 将返回 true,且套接字的所有进一步传输都会被加密。
另请参阅 QSslSocket::connectToHostEncrypted () 和 QSslSocket::isEncrypted ().
Returns the number of encrypted bytes that are awaiting decryption. Normally, this function will return 0 because QSslSocket decrypts its incoming data as soon as it can.
该函数在 Qt 4.4 引入。
Returns the number of encrypted bytes that are waiting to be written to the network.
该函数在 Qt 4.4 引入。
[signal]
void
QSslSocket::
encryptedBytesWritten
(
qint64
written
)
此信号发射当 QSslSocket writes its encrypted data to the network. The written parameter contains the number of bytes that were successfully written.
该函数在 Qt 4.4 引入。
另请参阅 QIODevice::bytesWritten ().
This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. If any data was written, this function returns true; otherwise false is returned.
调用此函数若需要 QSslSocket 立即开始发送缓冲数据。成功写入的字节数从属操作系统。在大多数情况下,不需调用此函数,因为 QAbstractSocket 将开始自动发送数据,一旦控制回到事件循环。若缺乏事件循环,调用 waitForBytesWritten () 代替。
另请参阅 write () 和 waitForBytesWritten ().
[slot]
void
QSslSocket::
ignoreSslErrors
()
此槽告诉 QSslSocket 去忽略错误在 QSslSocket 的握手阶段并继续连接。若想要继续连接即使在握手阶段发生错误,必须调用此槽,或从连接槽到 sslErrors (),或在握手阶段之前。若在响应出错或握手之前不调用此槽,连接将被丢弃当 sslErrors () 信号已发射。
If there are no errors during the SSL handshake phase (i.e., the identity of the peer is established with no problems), QSslSocket will not emit the sslErrors () signal, and it is unnecessary to call this function.
警告: 确保始终让用户审查报告的错误通过 sslErrors () 信号,且才调用此方法 (当用户确认后继续进行是 OK 的)。若存在意外错误,连接应该被中止。不审查实际错误就调用此方法,很可能会给应用程序带来安全风险。小心使用!
另请参阅 sslErrors ().
这是重载函数。
此方法告诉 QSslSocket 仅忽略的错误给出于 errors .
注意: Because most SSL errors are associated with a certificate, for most of them you must set the expected certificate this SSL error is related to. 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); QSslSocket socket; socket.ignoreSslErrors(expectedSslErrors); socket.connectToHostEncrypted("server.tld", 443);
多次调用此函数将替换先前调用传入错误列表。可以清零想要忽略的错误列表通过采用空列表调用此函数。
该函数在 Qt 4.6 引入。
另请参阅 sslErrors ().
Returns true if the socket is encrypted; otherwise, false is returned.
加密套接字加密的所有数据的写入是通过调用 write () 或 putChar () 在将数据写入网络之前,并以数据形式解密从网络收到的所有传入数据,先于调用 read (), readLine () 或 getChar ().
QSslSocket 发射 encrypted () 当它进入加密模式时。
可以调用 sessionCipher () 以查找用于加密和解密数据的加密密码。
另请参阅 mode ().
返回套接字的本地 certificate ,或空证书若没有赋值本地证书。
另请参阅 setLocalCertificate () 和 privateKey ().
返回套接字的当前模式; UnencryptedMode ,其中 QSslSocket behaves identially to QTcpSocket , or one of SslClientMode or SslServerMode , where the client is either negotiating or in encrypted mode.
当模式改变时, QSslSocket 发射 modeChanged ()
另请参阅 SslMode .
[signal]
void
QSslSocket::
modeChanged
(
QSslSocket::SslMode
mode
)
此信号发射当 QSslSocket 改变从 QSslSocket::UnencryptedMode 到 QSslSocket::SslClientMode or QSslSocket::SslServerMode . mode 是新模式。
另请参阅 QSslSocket::mode ().
Returns the peer's digital certificate (i.e., the immediate certificate of the host you are connected to), or a null certificate, if the peer has not assigned a certificate.
The peer certificate is checked automatically during the handshake phase, so this function is normally used to fetch the certificate for display or for connection diagnostic purposes. It contains information about the peer, including its host name, the certificate issuer, and the peer's public key.
Because the peer certificate is set during the handshake phase, it is safe to access the peer certificate from a slot connected to the sslErrors () 信号或 encrypted () 信号。
If a null certificate is returned, it can mean the SSL handshake failed, or it can mean the host you are connected to doesn't have a certificate, or it can mean there is no connection.
If you want to check the peer's complete chain of certificates, use peerCertificateChain () to get them all at once.
另请参阅 peerCertificateChain ().
Returns the peer's chain of digital certificates, or an empty list of certificates.
Peer certificates are checked automatically during the handshake phase. This function is normally used to fetch certificates for display, or for performing connection diagnostics. Certificates contain information about the peer and the certificate issuers, including host name, issuer names, and issuer public keys.
The peer certificates are set in QSslSocket during the handshake phase, so it is safe to call this function from a slot connected to the sslErrors () 信号或 encrypted () 信号。
If an empty list is returned, it can mean the SSL handshake failed, or it can mean the host you are connected to doesn't have a certificate, or it can mean there is no connection.
If you want to get only the peer's immediate certificate, use peerCertificate ().
另请参阅 peerCertificate ().
Returns the maximum number of certificates in the peer's certificate chain to be checked during the SSL handshake phase, or 0 (the default) if no maximum depth has been set, indicating that the whole certificate chain should be checked.
The certificates are checked in issuing order, starting with the peer's own certificate, then its issuer's certificate, and so on.
该函数在 Qt 4.4 引入。
另请参阅 setPeerVerifyDepth () 和 peerVerifyMode ().
[signal]
void
QSslSocket::
peerVerifyError
(const
QSslError
&
error
)
QSslSocket 可以在 SSL 握手期间多次发射此信号,在建立加密之前,以指示当建立对等方的标识时有发生错误。 error 通常指示 QSslSocket 无法安全标识对等方。
This signal provides you with an early indication when something's wrong. By connecting to this signal, you can manually choose to tear down the connection from inside the connected slot before the handshake has completed. If no action is taken, QSslSocket 将继续进行以发射 QSslSocket::sslErrors ().
该函数在 Qt 4.4 引入。
另请参阅 sslErrors ().
Returns the socket's verify mode. This mode mode decides whether QSslSocket should request a certificate from the peer (i.e., the client requests a certificate from the server, or a server requesting a certificate from the client), and whether it should require that this certificate is valid.
默认模式为 AutoVerifyPeer ,其告诉 QSslSocket 要使用 VerifyPeer 对于客户端和 QueryPeer 对于服务器。
该函数在 Qt 4.4 引入。
另请参阅 setPeerVerifyMode (), peerVerifyDepth (),和 mode ().
Returns the different hostname for the certificate validation, as set by setPeerVerifyName 或通过 connectToHostEncrypted .
该函数在 Qt 4.8 引入。
另请参阅 setPeerVerifyName () 和 connectToHostEncrypted ().
返回此套接字的私钥。
另请参阅 setPrivateKey () 和 localCertificate ().
返回套接字的 SSL 协议。默认情况下, QSsl::SecureProtocols 被使用。
另请参阅 setProtocol ().
[virtual protected]
qint64
QSslSocket::
readData
(
char
*
data
,
qint64
maxlen
)
重实现自 QIODevice::readData ().
返回套接字的加密 cipher , or a null cipher if the connection isn't encrypted. The socket's cipher for the session is set during the handshake phase. The cipher is used to encrypt and decrypt data transmitted through the socket.
QSslSocket also 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.
另请参阅 ciphers (), setCiphers (), setDefaultCiphers (), defaultCiphers (),和 supportedCiphers ().
Sets this socket's CA certificate database to be certificates . The certificate database must be set prior to the SSL handshake. The CA certificate database is used by the socket during the handshake phase to validate the peer's certificate.
The CA certificate database can be reset to the current default CA certificate database by calling this function with the list of CA certificates returned by defaultCaCertificates ().
另请参阅 caCertificates () 和 defaultCaCertificates ().
Sets the cryptographic cipher suite for this socket to ciphers , which must contain a subset of the ciphers in the list returned by supportedCiphers ().
Restricting the cipher suite must be done before the handshake phase, where the session cipher is chosen.
另请参阅 ciphers (), setDefaultCiphers (),和 supportedCiphers ().
Sets the cryptographic cipher suite for this socket to ciphers , which is a colon-separated list of cipher suite names. The ciphers are listed in order of preference, starting with the most preferred cipher. For example:
QSslSocket socket; socket.setCiphers("DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:AES256-SHA");
Each cipher name in ciphers must be the name of a cipher in the list returned by supportedCiphers (). Restricting the cipher suite must be done before the handshake phase, where the session cipher is chosen.
另请参阅 ciphers (), setDefaultCiphers (),和 supportedCiphers ().
[static]
void
QSslSocket::
setDefaultCaCertificates
(const
QList
<
QSslCertificate
> &
certificates
)
Sets the default CA certificate database to certificates . The default CA certificate database is originally set to your system's default CA certificate database. You can override the default CA certificate database with your own CA certificate database using this function.
Each SSL socket's CA certificate database is initialized to the default CA certificate database.
另请参阅 defaultCaCertificates () 和 addDefaultCaCertificate ().
[static]
void
QSslSocket::
setDefaultCiphers
(const
QList
<
QSslCipher
> &
ciphers
)
Sets the default cryptographic cipher suite for all sockets in this application to ciphers , which must contain a subset of the ciphers in the list returned by supportedCiphers ().
Restricting the default cipher suite only affects SSL sockets that perform their handshake phase after the default cipher suite has been changed.
另请参阅 setCiphers (), defaultCiphers (),和 supportedCiphers ().
Sets the socket's local certificate to certificate . The local certificate is necessary if you need to confirm your identity to the peer. It is used together with the private key; if you set the local certificate, you must also set the private key.
The local certificate and private key are always necessary for server sockets, but are also rarely used by client sockets if the server requires the client to authenticate.
另请参阅 localCertificate () 和 setPrivateKey ().
这是重载函数。
Sets the socket's local certificate to the first one found in file path , which is parsed according to the specified format .
Sets the maximum number of certificates in the peer's certificate chain to be checked during the SSL handshake phase, to depth . Setting a depth of 0 means that no maximum depth is set, indicating that the whole certificate chain should be checked.
The certificates are checked in issuing order, starting with the peer's own certificate, then its issuer's certificate, and so on.
该函数在 Qt 4.4 引入。
另请参阅 peerVerifyDepth () 和 setPeerVerifyMode ().
Sets the socket's verify mode to mode . This mode decides whether QSslSocket should request a certificate from the peer (i.e., the client requests a certificate from the server, or a server requesting a certificate from the client), and whether it should require that this certificate is valid.
默认模式为 AutoVerifyPeer ,其告诉 QSslSocket 要使用 VerifyPeer 对于客户端和 QueryPeer 对于服务器。
Setting this mode after encryption has started has no effect on the current connection.
该函数在 Qt 4.4 引入。
另请参阅 peerVerifyMode (), setPeerVerifyDepth (),和 mode ().
Sets a different host name, given by hostName , for the certificate validation instead of the one used for the TCP connection.
该函数在 Qt 4.8 引入。
另请参阅 peerVerifyName () 和 connectToHostEncrypted ().
设置套接字的私有 key to key . The private key and the local certificate are used by clients and servers that must prove their identity to SSL peers.
Both the key and the local certificate are required if you are creating an SSL server socket. If you are creating an SSL client socket, the key and local certificate are required if your client must identify itself to an SSL server.
另请参阅 privateKey () 和 setLocalCertificate ().
这是重载函数。
读取字符串在文件 fileName 并解码它使用指定 algorithm 和编码 format 以构造 SSL key . If the encoded key is encrypted, passPhrase is used to decrypt it.
The socket's private key is set to the constructed key. The private key and the local certificate are used by clients and servers that must prove their identity to SSL peers.
Both the key and the local certificate are required if you are creating an SSL server socket. If you are creating an SSL client socket, the key and local certificate are required if your client must identify itself to an SSL server.
另请参阅 privateKey () 和 setLocalCertificate ().
将套接字的 SSL (安全套接字层) 协议设为 protocol 。这将影响下次初启握手;在已加密套接字调用此函数,不会影响套接字协议。
另请参阅 protocol ().
设置尺寸为 QSslSocket 的内部读取缓冲到 size 字节。
该函数在 Qt 4.4 引入。
初始化 QSslSocket 采用本机套接字描述符 socketDescriptor . Returns true if socketDescriptor is accepted as a valid socket descriptor; otherwise returns false. The socket is opened in the mode specified by openMode ,并进入指定套接字状态通过 state .
注意: 采用相同本机套接字描述符初始化 2 套接字是不可能的。
另请参阅 socketDescriptor ().
设置给定 option 到描述值 value .
该函数在 Qt 4.6 引入。
另请参阅 socketOption ().
把套接字的 SSL 配置内容设为 configuration . This function sets the local certificate, the ciphers, the private key and the CA certificates to those stored in configuration .
设置 SSL (安全套接字层) 状态相关字段是不可能的。
该函数在 Qt 4.4 引入。
另请参阅 sslConfiguration (), setLocalCertificate (), setPrivateKey (), setCaCertificates (),和 setCiphers ().
返回值为 option 选项。
该函数在 Qt 4.6 引入。
另请参阅 setSocketOption ().
Returns the socket's SSL configuration state. The default SSL configuration of a socket is to use the default ciphers, default CA certificates, no local private key or certificate.
SSL 配置还包含没有通知可以随时改变的字段。
该函数在 Qt 4.4 引入。
另请参阅 setSslConfiguration (), localCertificate (), peerCertificate (), peerCertificateChain (), sessionCipher (), privateKey (), ciphers (),和 caCertificates ().
Returns a list of the last SSL errors that occurred. This is the same list as QSslSocket passes via the sslErrors() signal. If the connection has been encrypted with no errors, this function will return an empty list.
另请参阅 connectToHostEncrypted ().
[signal]
void
QSslSocket::
sslErrors
(const
QList
<
QSslError
> &
errors
)
QSslSocket 发射此信号在 SSL 握手之后以指示当建立对等方标识时有发生一个或多个错误。错误通常指示 QSslSocket 无法安全地识别对等方。除非采取任何行动,连接将被丢弃在此信号被发射之后。
If you want to continue connecting despite the errors that have occurred, you must call QSslSocket::ignoreSslErrors () from inside a slot connected to this signal. If you need to access the error list at a later point, you can call sslErrors () (without arguments).
errors 包含一个或多个错误阻止 QSslSocket 验证对等方身份。
Note: You cannot use Qt::QueuedConnection 当连接到此信号时,或调用 QSslSocket::ignoreSslErrors () 没有起作用。
注意: 信号 sslErrors 在此类中是重载。要使用函数指针句法连接到此信号,必须在静态铸造中指定信号类型,如此范例所示:
connect(sslSocket, static_cast<void(QSslSocket::*)(const QList<QSslError> &)>(&QSslSocket::sslErrors), [=](const QList<QSslError> &errors){ /* ... */ });
另请参阅 peerVerifyError ().
[slot]
void
QSslSocket::
startClientEncryption
()
启动延迟 SSL (安全套接字层) 握手为客户端连接。可以调用此函数当套接字在 ConnectedState 但仍处于 UnencryptedMode 。若尚未连接 (或已加密),此函数不起作用。
有实现 STARTTLS 功能的客户端经常使用延迟 SSL 握手。大多数其它客户端可以避免直接调用此函数通过使用 connectToHostEncrypted () 代替,自动履行握手。
另请参阅 connectToHostEncrypted () 和 startServerEncryption ().
[slot]
void
QSslSocket::
startServerEncryption
()
Starts a delayed SSL handshake for a server connection. This function can be called when the socket is in the ConnectedState 但仍处于 UnencryptedMode . If it is not connected or it is already encrypted, the function has no effect.
For server sockets, calling this function is the only way to initiate the SSL handshake. Most servers will call this function immediately upon receiving a connection, or as a result of having received a protocol-specific command to enter SSL mode (e.g, the server may respond to receiving the string "STARTTLS\r\n" by calling this function).
实现 SSL 服务器的最常见方式是创建子类化的 QTcpServer 并重实现 QTcpServer::incomingConnection (). The returned socket descriptor is then passed to QSslSocket::setSocketDescriptor ().
另请参阅 connectToHostEncrypted () 和 startClientEncryption ().
[static]
QList
<
QSslCipher
> QSslSocket::
supportedCiphers
()
Returns the list of cryptographic ciphers supported by this system. This list is set by the system's SSL libraries and may vary from system to system.
另请参阅 defaultCiphers (), ciphers (),和 setCiphers ().
[static]
bool
QSslSocket::
supportsSsl
()
Returns true if this platform supports SSL; otherwise, returns false. If the platform doesn't support SSL, the socket will fail in the connection phase.
[static]
QList
<
QSslCertificate
> QSslSocket::
systemCaCertificates
()
This function provides the CA certificate database provided by the operating system. The CA certificate database returned by this function is used to initialize the database returned by defaultCaCertificates (). You can replace that database with your own with setDefaultCaCertificates ().
另请参阅 caCertificates (), defaultCaCertificates (),和 setDefaultCaCertificates ().
[虚拟]
bool
QSslSocket::
waitForBytesWritten
(
int
msecs
= 30000)
重实现自 QIODevice::waitForBytesWritten ().
等待直到套接字被连接或 msecs milliseconds, whichever happens first. If the connection has been established, this function returns true; otherwise it returns false.
另请参阅 QAbstractSocket::waitForConnected ().
等待直到套接字已断开连接或 msecs milliseconds, whichever comes first. If the connection has been disconnected, this function returns true; otherwise it returns false.
另请参阅 QAbstractSocket::waitForDisconnected ().
等待直到套接字完成 SSL 握手且有发射 encrypted (),或 msecs 毫秒,以先到的为准。若 encrypted () 已发射,此函数返回 true;否则 (如:套接字断开连接,或 SSL 握手失败) 返回 false。
以下范例为加密套接字最多等待 1 秒:
socket->connectToHostEncrypted("imap", 993); if (socket->waitForEncrypted(1000)) qDebug("Encrypted!");
若 msecs 为 -1,此函数不会超时。
另请参阅 startClientEncryption (), startServerEncryption (), encrypted (),和 isEncrypted ().
[虚拟]
bool
QSslSocket::
waitForReadyRead
(
int
msecs
= 30000)
重实现自 QIODevice::waitForReadyRead ().
[virtual protected]
qint64
QSslSocket::
writeData
(const
char
*
data
,
qint64
len
)
重实现自 QIODevice::writeData ().