Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with creating ssl server using QSslSocket
Forum Updated to NodeBB v4.3 + New Features

Problem with creating ssl server using QSslSocket

Scheduled Pinned Locked Moved General and Desktop
qsslsocketnetwork socketnetworkserverqtcpserverqtcpsocket
11 Posts 3 Posters 5.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • cybercatalystC Offline
    cybercatalystC Offline
    cybercatalyst
    wrote on last edited by cybercatalyst
    #2

    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

    1 Reply Last reply
    0
    • E Offline
      E Offline
      Eehsanmgh
      wrote on last edited by Eehsanmgh
      #3

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

      1 Reply Last reply
      0
      • cybercatalystC Offline
        cybercatalystC Offline
        cybercatalyst
        wrote on last edited by cybercatalyst
        #4

        Yes, I am using QSslSocket in QtWebServer.

        E 1 Reply Last reply
        0
        • cybercatalystC cybercatalyst

          Yes, I am using QSslSocket in QtWebServer.

          E Offline
          E Offline
          Eehsanmgh
          wrote on last edited by
          #5

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

          1 Reply Last reply
          0
          • E Eehsanmgh

            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";
            }
            
            T Offline
            T Offline
            t3685
            wrote on last edited by
            #6

            @Eehsanmgh

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

            E 1 Reply Last reply
            0
            • T t3685

              @Eehsanmgh

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

              E Offline
              E Offline
              Eehsanmgh
              wrote on last edited by Eehsanmgh
              #7

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

              T 1 Reply Last reply
              0
              • E Eehsanmgh

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

                T Offline
                T Offline
                t3685
                wrote on last edited by
                #8

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

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  Eehsanmgh
                  wrote on last edited by Eehsanmgh
                  #9

                  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.

                  T 1 Reply Last reply
                  0
                  • E Eehsanmgh

                    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.

                    T Offline
                    T Offline
                    t3685
                    wrote on last edited by t3685
                    #10

                    @Eehsanmgh

                    Are you sure there are no SSL errors?

                    E 1 Reply Last reply
                    0
                    • T t3685

                      @Eehsanmgh

                      Are you sure there are no SSL errors?

                      E Offline
                      E Offline
                      Eehsanmgh
                      wrote on last edited by
                      #11

                      @t3685 Yes I'm sure.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved