SslClient.cpp Example File
securesocketclient/src/SslClient.cpp
#include "SslClient.hpp"
#include <QtCore/QStringList>
#include <QtNetwork/QSslCipher>
SslClient::SslClient(QObject *parent)
: QObject(parent)
, m_socket(0)
, m_sslErrorControl(new SslErrorControl(this))
, m_hostName("www.blackberry.com")
, m_port(443)
, m_sessionActive(false)
, m_cipher("<none>")
{
connect(m_sslErrorControl, SIGNAL(ignoreSslErrors()),
this, SLOT(ignoreSslErrors()));
connect(m_sslErrorControl, SIGNAL(viewCertificateChainRequested()),
this, SIGNAL(viewCertificateChainRequested()));
}
void SslClient::setHostName(const QString &hostName)
{
if (m_hostName == hostName)
return;
m_hostName = hostName;
emit hostNameChanged();
updateEnabledState();
}
QString SslClient::hostName() const
{
return m_hostName;
}
void SslClient::setPort(const QString &portString)
{
bool ok = false;
const int port = portString.toInt(&ok);
if (!ok || (m_port == port))
return;
m_port = port;
emit portChanged();
}
QString SslClient::port() const
{
return QString::number(m_port);
}
bool SslClient::sessionActive() const
{
return m_sessionActive;
}
QString SslClient::cipher() const
{
return m_cipher;
}
QString SslClient::response() const
{
return m_response;
}
SslErrorControl* SslClient::sslErrorControl() const
{
return m_sslErrorControl;
}
void SslClient::updateEnabledState()
{
const bool sessionActive = (m_socket && (m_socket->state() == QAbstractSocket::ConnectedState));
if (m_sessionActive == sessionActive)
return;
m_sessionActive = sessionActive;
emit sessionActiveChanged();
}
void SslClient::secureConnect()
{
if (!m_socket) {
m_socket = new QSslSocket(this);
connect(m_socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
connect(m_socket, SIGNAL(encrypted()),
this, SLOT(socketEncrypted()));
connect(m_socket, SIGNAL(sslErrors(QList<QSslError>)),
this, SLOT(sslErrors(QList<QSslError>)));
connect(m_socket, SIGNAL(readyRead()),
this, SLOT(socketReadyRead()));
}
m_socket->connectToHostEncrypted(m_hostName, m_port);
updateEnabledState();
}
void SslClient::socketStateChanged(QAbstractSocket::SocketState state)
{
if (m_sslErrorControl->visible())
return;
updateEnabledState();
if (state == QAbstractSocket::UnconnectedState) {
m_cipher = "<none>";
emit cipherChanged();
m_socket->deleteLater();
m_socket = 0;
}
}
void SslClient::socketEncrypted()
{
if (!m_socket)
return;
m_response.clear();
emit responseChanged();
const QSslCipher cipher = m_socket->sessionCipher();
m_cipher = QString("%1, %2 (%3/%4)").arg(cipher.authenticationMethod())
.arg(cipher.name())
.arg(cipher.usedBits())
.arg(cipher.supportedBits());
emit cipherChanged();
emit certificateChainChanged(m_socket->peerCertificateChain());
}
void SslClient::socketReadyRead()
{
appendString(QString::fromUtf8(m_socket->readAll()));
}
void SslClient::sendData(const QString &input)
{
if (input.isEmpty())
return;
appendString(input + '\n');
m_socket->write(input.toUtf8() + "\r\n");
}
void SslClient::sslErrors(const QList<QSslError> &errors)
{
QStringList messages;
foreach (const QSslError &error, errors)
messages << error.errorString();
emit certificateChainChanged(m_socket->peerCertificateChain());
m_sslErrorControl->exec(messages.join("\n"));
if (m_socket && (m_socket->state() != QAbstractSocket::ConnectedState))
socketStateChanged(m_socket->state());
}
void SslClient::ignoreSslErrors()
{
if (m_socket)
m_socket->ignoreSslErrors();
}
void SslClient::appendString(const QString &line)
{
m_response += line;
emit responseChanged();
}