The code below shows how to create a socket that can operate over an Transport Layer Security (TLS, also known as SSL) connection.
The code below shows how to create a socket that can operate over an Transport Layer Security (TLS, also known as SSL) connection.
#include "tlssocket.h"
#include <QCoreApplication>
int main(int argc, char **argv)
{
QCoreApplication qapp(argc, argv);
TLSSocket socket;
socket.connectToHostEncrypted(QStringLiteral("www.paypal.com"), 443);
socket.write("GET / HTTP/1.0\r\n\r\n");
while (socket.waitForReadyRead())
printf("%s", socket.readAll().constData());
return 0;
}
Convenience method for initialising and cleaning up QCA.
Definition qca_core.h:660
#include "tlssocket.h"
#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif
class TLSSocket::Private :
public QObject
{
Q_OBJECT
public:
TLSSocket *q;
QTcpSocket *sock;
QString host;
bool encrypted;
bool error, done;
QByteArray readbuf, writebuf;
bool waiting;
Private(TLSSocket *_q)
, q(_q)
, sync(_q)
{
sock = new QTcpSocket(this);
connect(sock, &QTcpSocket::connected, this, &TLSSocket::Private::sock_connected);
connect(sock, &QTcpSocket::readyRead, this, &TLSSocket::Private::sock_readyRead);
connect(sock, &QTcpSocket::bytesWritten, this, &TLSSocket::Private::sock_bytesWritten);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
connect(sock, &QTcpSocket::errorOccurred, this, &TLSSocket::Private::sock_error);
#else
connect(sock,
QOverload<QAbstractSocket::SocketError>::of(&QTcpSocket::error),
this,
&TLSSocket::Private::sock_error);
#endif
encrypted = false;
error = false;
waiting = false;
done = false;
}
bool waitForReadyRead(int msecs)
{
waiting = true;
waiting = false;
if (error || done)
return false;
return ok;
}
private Q_SLOTS:
void sock_connected()
{
}
void sock_readyRead()
{
QByteArray buf = sock->readAll();
}
void sock_bytesWritten(qint64 x)
{
Q_UNUSED(x);
}
void sock_error(QAbstractSocket::SocketError x)
{
Q_UNUSED(x);
done = true;
if (waiting)
}
void tls_handshaken()
{
printf("not valid\n");
sock->abort();
error = true;
} else {
encrypted = true;
if (!writebuf.isEmpty()) {
writebuf.clear();
}
}
if (waiting)
}
void tls_readyRead()
{
if (waiting)
}
void tls_readyReadOutgoing()
{
sock->write(buf);
}
void tls_closed()
{
}
void tls_error()
{
}
};
TLSSocket::TLSSocket(
QObject *parent)
: QTcpSocket(parent)
{
d = new Private(this);
}
TLSSocket::~TLSSocket()
{
delete d;
}
void TLSSocket::connectToHostEncrypted(const QString &host, quint16 port)
{
d->host = host;
setOpenMode(QIODevice::ReadWrite);
d->sock->connectToHost(host, port);
}
{
return d->tls;
}
bool TLSSocket::waitForReadyRead(int msecs)
{
return d->waitForReadyRead(msecs);
}
qint64 TLSSocket::readData(char *data, qint64 maxlen)
{
if (!d->error)
d->readbuf += d->tls->
read();
unsigned char *p = (unsigned char *)d->readbuf.data();
int size = d->readbuf.size();
int readsize = qMin(size, (int)maxlen);
int newsize = size - readsize;
memcpy(data, p, readsize);
memmove(p, p + readsize, newsize);
d->readbuf.resize(newsize);
return readsize;
}
qint64 TLSSocket::writeData(const char *data, qint64 len)
{
QByteArray buf(data, len);
if (d->encrypted)
d->tls->write(buf);
else
d->writebuf += buf;
return len;
}
#include "tlssocket.moc"
void error()
This signal is emitted when an error is detected.
void readyReadOutgoing()
This signal is emitted when SecureLayer has encrypted (network side) data ready to be read.
void closed()
This signal is emitted when the SecureLayer connection is closed.
void readyRead()
This signal is emitted when SecureLayer has decrypted (application side) data ready to be read.
Enable synchronization between two threads.
Definition qca_support.h:362
bool waitForCondition(int msecs=-1)
Call to pause execution in this thread.
void conditionMet()
Call to continue execution in the paused thread.
void write(const QByteArray &a) override
This method writes unencrypted (plain) data to the SecureLayer implementation.
void startClient(const QString &host=QString())
Start the TLS/SSL connection as a client.
void writeIncoming(const QByteArray &a) override
This method accepts encoded (typically encrypted) data for processing.
void setTrustedCertificates(const CertificateCollection &trusted)
Set up the set of trusted certificates that will be used to verify that the certificate provided is v...
void reset()
Reset the connection.
IdentityResult peerIdentityResult() const
After the SSL/TLS handshake is complete, this method allows you to determine if the other end of the ...
QByteArray readOutgoing(int *plainBytes=nullptr) override
This method provides encoded (typically encrypted) data.
@ Valid
identity is verified
Definition qca_securelayer.h:331
QByteArray read() override
This method reads decrypted (plain) data from the SecureLayer implementation.
void handshaken()
Emitted when the protocol handshake is complete.
QCA_EXPORT CertificateCollection systemStore()
Get system-wide root Certificate Authority (CA) certificates.