Problem with creating ssl server using QSslSocket



  • 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:

    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()));
        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->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";
    }
    


  • Hey there, you might take a look into QtWebServer. This is a plug and play ssl server library that you can use for your own apps. It already has SSL support available and is very easy to use:

    https://github.com/cybercatalyst/qtwebserver

    It has only Qt as the only dependency, so no other libs required. Here are a bunch of examples:

    https://github.com/cybercatalyst/qtwebserver-examples

    This example shows you how to set up an SSL server for delivering websites over HTTPS:
    https://github.com/cybercatalyst/qtwebserver-examples/tree/master/https



  • Thank you my friend. But using QSslSocket is critical for the project I have involved. Actually we must use QSslSocket for this project.



  • Yes, I am using QSslSocket in QtWebServer.



  • @cybercatalyst Actually I intended to implement an encryption based secure socket library for using by our Qt users such as QSslSocket. The protocol has implemented and now I want to know how QSslSocket works. After that I want to mimic QSslSocket for using our encryption protocol.



  • @Eehsanmgh

    Stupid question, but do you have an event loop running?



  • @t3685 How I should do that? I guess this is for multi-threading



  • @Eehsanmgh

    The event loop is not needed for multi-threading, but is needed for signal and slots. You should check the documentation regarding this, but usually it involves creating a Q(Core)Application object in the main function and call the run function.



  • 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.



  • @Eehsanmgh

    Are you sure there are no SSL errors?



  • @t3685 Yes I'm sure.



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.