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.1k 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.
  • E Offline
    E Offline
    Eehsanmgh
    wrote on 8 Jun 2015, 08:13 last edited by Eehsanmgh 6 Aug 2015, 08:15
    #1

    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 1 Reply Last reply 9 Jun 2015, 05:47
    0
    • C Offline
      C Offline
      cybercatalyst
      wrote on 8 Jun 2015, 11:25 last edited by cybercatalyst 6 Aug 2015, 11:26
      #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 8 Jun 2015, 12:12 last edited by Eehsanmgh 6 Aug 2015, 12:14
        #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
        • C Offline
          C Offline
          cybercatalyst
          wrote on 8 Jun 2015, 13:42 last edited by cybercatalyst 6 Aug 2015, 13:42
          #4

          Yes, I am using QSslSocket in QtWebServer.

          E 1 Reply Last reply 9 Jun 2015, 04:11
          0
          • C cybercatalyst
            8 Jun 2015, 13:42

            Yes, I am using QSslSocket in QtWebServer.

            E Offline
            E Offline
            Eehsanmgh
            wrote on 9 Jun 2015, 04:11 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
              8 Jun 2015, 08:13

              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 9 Jun 2015, 05:47 last edited by
              #6

              @Eehsanmgh

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

              E 1 Reply Last reply 9 Jun 2015, 10:32
              0
              • T t3685
                9 Jun 2015, 05:47

                @Eehsanmgh

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

                E Offline
                E Offline
                Eehsanmgh
                wrote on 9 Jun 2015, 10:32 last edited by Eehsanmgh 6 Sept 2015, 12:06
                #7

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

                T 1 Reply Last reply 10 Jun 2015, 08:00
                0
                • E Eehsanmgh
                  9 Jun 2015, 10:32

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

                  T Offline
                  T Offline
                  t3685
                  wrote on 10 Jun 2015, 08:00 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 11 Jun 2015, 14:12 last edited by Eehsanmgh 6 Nov 2015, 14:13
                    #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 11 Jun 2015, 14:39
                    0
                    • E Eehsanmgh
                      11 Jun 2015, 14:12

                      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 11 Jun 2015, 14:39 last edited by t3685 6 Nov 2015, 14:48
                      #10

                      @Eehsanmgh

                      Are you sure there are no SSL errors?

                      E 1 Reply Last reply 13 Jun 2015, 13:12
                      0
                      • T t3685
                        11 Jun 2015, 14:39

                        @Eehsanmgh

                        Are you sure there are no SSL errors?

                        E Offline
                        E Offline
                        Eehsanmgh
                        wrote on 13 Jun 2015, 13:12 last edited by
                        #11

                        @t3685 Yes I'm sure.

                        1 Reply Last reply
                        0

                        3/11

                        8 Jun 2015, 12:12

                        8 unread
                        • Login

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