Ehsan Maiqani Farahani
Ehsan Maiqani Farahani

Reputation: 1844

Ssl Server using QSslSocket in Qt

I have implemented a ssl server using QSslSocket and run it correctly. But I have some problem with it that I couldn't solve them immediately. I thought that just connecting readyRead() signal to a slot for reading buffer is sufficient to do that but I have recognized that the readyRead() does not emit at all in this situation and I must also use waitForReadyRead() function in my code. But the problem is using this function cause blocking read the buffer. Actually I want to know how I can read buffer when data has arrived without blocking?

Bellow is my implemented ssl server:

#include "sslserver.h"
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>
#include <QFile>
#include <QtNetwork/QSslKey>
#include <QtNetwork/QSslConfiguration>
#include <QtNetwork/QSslError>

SslServer::SslServer(QObject *parent) : QTcpServer(parent)
{
    server = new QTcpServer(this);

    if(!server->listen(QHostAddress::Any, 9996))
    {
        qDebug() << "Server could not start";
    }
    else
    {
        qDebug() << "Server started!";
    }

    connect(server, SIGNAL(newConnection()), this, SLOT(newConnectionRecognized()));

}

void SslServer::showErrors()
{
    this-> err = socket->sslErrors();
    for(int i=0;i<err.size();i++)
        qDebug() << err[i];
}

SslServer::~SslServer()
{

}

void SslServer::newConnectionRecognized()
{

    incomingConnection(server->nextPendingConnection()->socketDescriptor());

}

void SslServer::incomingConnection(qintptr socketDescriptor)
{
    socket = new QSslSocket(this);
    socket->setProtocol(QSsl::SslV3);

    connect(socket, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(showErrors()));
    connect(socket, SIGNAL(encrypted()), this, SLOT(ready()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(readChannel()));

    // Read Key from file

    QByteArray key;
    QFile KeyFile("server.key");
    if(KeyFile.open(QIODevice::ReadOnly))
    {
        key = KeyFile.readAll();
        KeyFile.close();
    }
    else
    {
        qDebug() << KeyFile.errorString();
    }

    QSslKey sslKey(key, QSsl::Rsa);
    socket->setPrivateKey(sslKey);


    // Load server ssl certificate from file
    QByteArray cert;
    QFile CertFile("server.csr");
    if(CertFile.open(QIODevice::ReadOnly))
    {
        cert = CertFile.readAll();
        CertFile.close();
    }
    else
    {
        qDebug() << CertFile.errorString();
    }

    QSslCertificate sslCert(cert);
    socket->setLocalCertificate(sslCert);

    QSslConfiguration cfg = socket->sslConfiguration();
    cfg.caCertificates();

    if (!socket->setSocketDescriptor(socketDescriptor))ee
    {
        qDebug() << ("! Couldn't set socket descriptor");
        delete socket;
        return;
    }

    socket->startServerEncryption();

    if (socket->isEncrypted())
        emit socket->encrypted();

    if(!socket->waitForEncrypted(3000)) {
        qDebug("Wait for encrypted!!!!");
        return;
    }

    while (true) {
        socket->waitForReadyRead();
    }
}


void SslServer::readChannel()
{
    QByteArray qstrbytes = socket->readLine();
    qDebug() << qstrbytes;

}

void SslServer::ready()
{
    qDebug() << "Encrypted";
}

Upvotes: 1

Views: 4070

Answers (1)

Ehsan Maiqani Farahani
Ehsan Maiqani Farahani

Reputation: 1844

I have found the problem when I implement another client/server but this time with QTcpSocket. I dont know exactly why but I guess the problem is because of using socketDescriptor for creating a QSslSocket. When I created client and server with QTcpSocket they works perfectly without any event loop and only by connecting readyRead() signal to an slot. After that in order to testing some situation I have create QTcpSocket using socketDescriptor. Then I found the problem is from creating socket using socketDescriptor because this time the readyRead() signal doesn't work as before.

Upvotes: 0

Related Questions