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 a QTcpSocket
QtWS25 Last Chance

Problem with a QTcpSocket

Scheduled Pinned Locked Moved Unsolved General and Desktop
socketssl
14 Posts 3 Posters 6.2k Views
  • 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.
  • M Offline
    M Offline
    moffa13
    wrote on 7 Jul 2016, 21:44 last edited by moffa13 7 Jul 2016, 21:46
    #3

    Thanks for your reply.

    The error thrown is QAbstractSocket::SslHandshakeFailedError (13) handled with the signal (error) and not with the signal (sslErrors) so I can't handle it with ignoreSslErrors and event if I could what should I do ?

    When you say "write to the unencrypted socket", I don't know how to remove the encryption to write as plaintext because there is the startServerEncryption method but I thing there is no reverse function.

    K 1 Reply Last reply 7 Jul 2016, 21:48
    0
    • M moffa13
      7 Jul 2016, 21:44

      Thanks for your reply.

      The error thrown is QAbstractSocket::SslHandshakeFailedError (13) handled with the signal (error) and not with the signal (sslErrors) so I can't handle it with ignoreSslErrors and event if I could what should I do ?

      When you say "write to the unencrypted socket", I don't know how to remove the encryption to write as plaintext because there is the startServerEncryption method but I thing there is no reverse function.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 7 Jul 2016, 21:48 last edited by
      #4

      @moffa13
      That's why linked the documentation page. See what it says there:

      If an error occurs, QSslSocket emits the sslErrors() signal. In this case, if no action is taken to ignore the error(s), the connection is dropped. To continue, despite the occurrence of an error, you can call ignoreSslErrors(), either from within this slot after the error occurs, or any time after construction of the QSslSocket and before the connection is attempted. This will allow QSslSocket to ignore the errors it encounters when establishing the identity of the peer. Ignoring errors during an SSL handshake should be used with caution, since a fundamental characteristic of secure connections is that they should be established with a successful handshake.

      So as I see it, you connect the sslErrors signal to your slot, call ignoreSslErrors and after that use the socket as if it weren't an SSL socket, but rather plain ol' QTcpSocket.

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        moffa13
        wrote on 7 Jul 2016, 22:00 last edited by moffa13 7 Jul 2016, 22:39
        #5

        I already did that; the problem is with my with slot because it's not called.

        So with this code

        void Connection::sslErrors(const QList<QSslError> &errors){
            std::cout << "SSL error !";
        }
        

        The error message is not written. Here's my connect :

          connect(m_socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
        

        As I said only de error signal is called

        What did I do wrong ?

        K 1 Reply Last reply 8 Jul 2016, 16:40
        0
        • M moffa13
          7 Jul 2016, 22:00

          I already did that; the problem is with my with slot because it's not called.

          So with this code

          void Connection::sslErrors(const QList<QSslError> &errors){
              std::cout << "SSL error !";
          }
          

          The error message is not written. Here's my connect :

            connect(m_socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
          

          As I said only de error signal is called

          What did I do wrong ?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 8 Jul 2016, 16:40 last edited by
          #6

          @moffa13
          Yeah, I'm talking nonsense. sslErrors() will not be emitted before the handshake has completed, which in your case it doesn't. See instead QSslSocket::startClientEncryption and QSslSocket::startServerEncryption, which are specifically tailored for delayed handshakes.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            moffa13
            wrote on 8 Jul 2016, 17:34 last edited by
            #7

            I don't really see how to do this

            K 1 Reply Last reply 8 Jul 2016, 20:00
            0
            • M moffa13
              8 Jul 2016, 17:34

              I don't really see how to do this

              K Offline
              K Offline
              kshegunov
              Moderators
              wrote on 8 Jul 2016, 20:00 last edited by
              #8

              @moffa13
              As far as understand it (I haven't done this) you create your socket as usual. But instead of calling QSslSocket::connectToHostEncrypted , you call the regular connectToHost. And at one point, when you want to upgrade to an encrypted connection you call the QSslSocket::startClientEncryption. There are a few notes in the docs of how to do it for the server side too. However, I'm not that convinced that is what you want to do ... am I misunderstand you?

              Kind regards.

              Read and abide by the Qt Code of Conduct

              1 Reply Last reply
              0
              • M Offline
                M Offline
                moffa13
                wrote on 8 Jul 2016, 20:25 last edited by moffa13 7 Aug 2016, 20:36
                #9

                Actually I am the server so I'm calling QSslSocket::startServerEncryption then the error is thrown. See the code:

                void SSLServer::incomingConnection(qintptr socketDescriptor)
                {
                    QScopedPointer<QSslSocket> socket(new QSslSocket(this));
                    if (socket->setSocketDescriptor(socketDescriptor)) {
                
                        socket->addCaCertificate(certificate);
                        socket->setLocalCertificate(certificate);
                        socket->setPrivateKey(key);
                        
                        socket->startServerEncryption();
                        queue.enqueue(socket.take());
                    }
                }
                

                Maybe I can do something with the slot(error) because at this point I can see the ssl error (handshake failed) and if I sleep the program the connection is closed yet. So, with this:

                void Connection::error(QAbstractSocket::SocketError e){
                    std::cout << "SSL error ! code : " << e;
                }
                

                This is written "SSL error ! code : 13" and if I add _sleep(1000) the connection is not dropped yet so maybe I can do something with it.

                K 1 Reply Last reply 9 Jul 2016, 18:13
                0
                • M moffa13
                  8 Jul 2016, 20:25

                  Actually I am the server so I'm calling QSslSocket::startServerEncryption then the error is thrown. See the code:

                  void SSLServer::incomingConnection(qintptr socketDescriptor)
                  {
                      QScopedPointer<QSslSocket> socket(new QSslSocket(this));
                      if (socket->setSocketDescriptor(socketDescriptor)) {
                  
                          socket->addCaCertificate(certificate);
                          socket->setLocalCertificate(certificate);
                          socket->setPrivateKey(key);
                          
                          socket->startServerEncryption();
                          queue.enqueue(socket.take());
                      }
                  }
                  

                  Maybe I can do something with the slot(error) because at this point I can see the ssl error (handshake failed) and if I sleep the program the connection is closed yet. So, with this:

                  void Connection::error(QAbstractSocket::SocketError e){
                      std::cout << "SSL error ! code : " << e;
                  }
                  

                  This is written "SSL error ! code : 13" and if I add _sleep(1000) the connection is not dropped yet so maybe I can do something with it.

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 9 Jul 2016, 18:13 last edited by
                  #10

                  @moffa13
                  If you get "SSL error ! code : 13" then you should also get the sslErrors() signal. You could try calling ignoreSslErrors() inside the error handler and this will hopefully prevent the socket from closing the connection.

                  Kind regards.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    moffa13
                    wrote on 9 Jul 2016, 18:25 last edited by
                    #11

                    I wrote this :

                    
                    connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
                    connect(m_socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
                    
                    ...
                    
                    void Connection::error(QAbstractSocket::SocketError e){
                        std::cout << "SSL error ! code : " << e;
                        m_socket->ignoreSslErrors();
                        write("Please use SSL");
                    }
                    
                    void Connection::sslErrors(const QList<QSslError> &errors){
                        std::cout << "Fuck it ";
                        std::cout << "SSL error " << errors.length();
                        m_socket->ignoreSslErrors();
                        write("Please use SSL");
                    }
                    

                    Only the first works and the ignoreSslErrors() doesn't change anything.

                    K 1 Reply Last reply 9 Jul 2016, 19:39
                    0
                    • M moffa13
                      9 Jul 2016, 18:25

                      I wrote this :

                      
                      connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)));
                      connect(m_socket, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
                      
                      ...
                      
                      void Connection::error(QAbstractSocket::SocketError e){
                          std::cout << "SSL error ! code : " << e;
                          m_socket->ignoreSslErrors();
                          write("Please use SSL");
                      }
                      
                      void Connection::sslErrors(const QList<QSslError> &errors){
                          std::cout << "Fuck it ";
                          std::cout << "SSL error " << errors.length();
                          m_socket->ignoreSslErrors();
                          write("Please use SSL");
                      }
                      

                      Only the first works and the ignoreSslErrors() doesn't change anything.

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 9 Jul 2016, 19:39 last edited by
                      #12

                      @moffa13
                      That is strange. I'm sorry I don't know, I can't see anything wrong with the snippets you provided. As far as I understand it, the signal should be raised (and you should get your slot executed).

                      Kind regards.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        moffa13
                        wrote on 13 Jul 2016, 17:12 last edited by moffa13
                        #13

                        Hello,

                        I'm sending to you a short version my code, maybe you can try something ? If you want me to write it here, I'll do it.

                        https://mega.nz/#!0AQxzYjZ!PDXxC-QgsWlBJYnwtQEk16jNdQiXy-d-S4AfkRWeoPc

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          marcbf
                          wrote on 2 Sept 2016, 06:28 last edited by
                          #14

                          I currently have a very similar problem. I'm implementing a send mail client which should gracefully fall back to an unencrypted connection if encryption fails (and the user has decided to go ahead anyway). For that purpose I've connected to the QAbstractSocket::error() signal.

                          While I do get the signal it's ultimately moot since the code in question (QSslSocketBackendPrivate::startHandshake() in qsslsocket_openssl.cpp) immediately closes the socket by calling QSslSocket::abort() after emitting the signal. Because of that calling QSslSocket::ignoreSslErrors() is never an option.

                          I currently have no workaround in place. Maybe the only solution is to connect to the disconnected() signal and then check whether the connection was closed because of a handshake error. If it was, then open a new unencrypted socket/connection. I'm open to suggestions, though... :-)

                          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