Create a SSl Connection only with Slots and Signals ...



  • Hi,

    I am trying to create a application with a SSl Connection between a client and a server application, but the communication stop after the connection signal on the client app.

    I have the following slots on the client app:

        connect(m_sslSocket,SIGNAL(disconnected(void)), this,SLOT(disconnected(void)));
        connect(m_sslSocket,SIGNAL(hostFound(void)), this,SLOT(hostFound(void)));
        connect(m_sslSocket,SIGNAL(encrypted()), this, SLOT(SSLReady()));
        connect(m_sslSocket,SIGNAL(connected(void)), this,SLOT(connected(void)));
        connect(m_sslSocket,SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(SSLError(QAbstractSocket::SocketError)));
        connect(m_sslSocket,SIGNAL(readyRead(void)), this,SLOT(readyRead(void)));
        connect(m_sslSocket,SIGNAL(aboutToClose(void)), this,SLOT(aboutToClose(void)));
        connect(m_sslSocket,SIGNAL(bytesWritten(qint64)), this,SLOT(bytesWritten(qint64)));
    

    and the following slots on the server

        connect(&m_tcpSocket,SIGNAL(disconnected(void)), this,SLOT(disconnected(void)));
        connect(&m_tcpSocket,SIGNAL(hostFound(void)), this,SLOT(hostFound(void)));
        connect(&m_tcpSocket,SIGNAL(connected(void)), this,SLOT(connected(void)));
        connect(&m_tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)), this,SLOT(SSLError(QAbstractSocket::SocketError)));
        connect(&m_tcpSocket,SIGNAL(readyRead(void)), this,SLOT(readyRead(void)));
        connect(&m_tcpSocket,SIGNAL(aboutToClose(void)), this,SLOT(aboutToClose(void)));
        connect(&m_tcpSocket,SIGNAL(bytesWritten(qint64)), this,SLOT(bytesWritten(qint64)));
    

    The program is coded as a threaded server application.
    I get a incoming Connection on the server and the client application gets a "connected" signal.

    But after this, the communication stops.
    I coded the start of the client communication in the SSLReady() Slot, but it never created.

    How can I get closer to the problem ?

    I placed in all the functions a break point, but no one is reached.

    Edit: I have a sample used with the following code, which is working fine, but not threaded and also not event driven.

    Which event will happen after a connection such a connection

        sslSocket.connectToHostEncrypted(hostName, port);
    

    The sample code will wait on a connection with this line.

        if (sslSocket.waitForEncrypted(-1))    // Wait until encrypted connection is established, -1 means no timeout
    

    I will only get a event:

    void    DataThread::hostFound(void)
    {
    //    m_StateMachineState=HostConnected;
    }
    

    But I thought will come to start the communiction to the server:

    void    DataThread::SSLReady(void)
    {
        m_StateMachineState=SocketConnected;
        SensorQueryBuffer->setLocked();
        sendRecordData();                                      // Send out the data
    }
    

    Thanks for any kind of help.
    R.


  • Lifetime Qt Champion

    Hi,

    Which version of Qt are you using ?
    On which platform ?
    With which compiler ?

    Note that you are missing the sslErrors signal which might give you more information about what is happening.



  • @Ritchie said in Create a SSl Connection only with Slots and Signals ...:

    m_sslSocket

    just to make sure, m_sslSocket is a member variable/object and does not go out of scope at the end of the function?



  • @SGaist
    Which version of Qt are you using ? QT 5.9 (Free version)
    On which platform ? Kubuntu 17.04
    With which compiler ? gcc (64 bit)

    @J-Hilk
    Yes, this is a Class Member variable

    I have already the error handling in place, but the program did not run into this function up to now.

    After executing this code on the client, nothing happen.

    m_StateMachineState=TryToConnect;
    m_sslSocket->connectToHostEncrypted(m_ServerTCPIPAddress, m_ServerTCPIPPort);
    

    and the event "HostFound" is executed.

    On the server, the code of "incomingConnection" is executed and the Connection Thread is created and the socket handle is also
    handled over.
    This code is also executed on the server.

        m_tcpSocket.setPrivateKey(sPrivatSSLKey);
        m_tcpSocket.setLocalCertificate(sLocalSSLCertificate);
        m_tcpSocket.startServerEncryption();
    

    I changed the errorhandling into

    {
        m_LastErrorMessage=m_sslSocket->errorString();
        m_sslSocket->ignoreSslErrors( );
    }
    

    I also updated the error installation for the signal to

        connect(m_sslSocket,SIGNAL(sslErrors(const QList<QSslError> &errors)), this,SLOT(SSLError(const QList<QSslError> &errors)));
    

    But i get this error during execution of the program:

    QObject::connect: No such signal QSslSocket::sslErrors(const QList<QSslError> &error)
    

    But the member m_sslSocket if type "QSslSocket".

    I thought it should run at least into the error function, but it does not.

    Best regards
    R.


  • Lifetime Qt Champion

    You should use the new connect syntax, this will give you compile time error rather than runtime error.



  • @SGaist
    Hi,
    I try to find the new way of using the "Connection" command and failed.
    I always used this documentation as a reference.
    http://doc.qt.io/qt-5/signalsandslots.html

    Is there a other, better documentation for QT ?
    Do you have an example, sorry asking for.

    Best regards
    R.


  • Moderators

    @Ritchie There is nothing wrong with this documentation.
    In the link you posted the new way to connect signals and slots is described with examples:

     QObject::connect(&a, &Counter::valueChanged,
                      &b, &Counter::setValue);
    


  • @jsulm
    But what is about this

    You should use the new connect syntax, this will give you compile time error rather than runtime error

    Where is the "new connect syntax" descripted ?
    Best regards
    R.


  • Moderators

    @Ritchie This is the new syntax:

    QObject::connect(&a, &Counter::valueChanged,
                     &b, &Counter::setValue);
    

    It does not use SIGNAL/SLOT macros, instead it uses function pointers.


  • Lifetime Qt Champion

    The signals and slots chapter might be of interest.



  • Thanks, that was a misunderstanding from my side.

    When I use the following code:

    m_sslSocket->connectToHostEncrypted(m_ServerTCPIPAddress, m_ServerTCPIPPort);
    

    to start the SSL Communication, I do not get a "encrypted(void)" event.

    But when I use the following lines

    m_sslSocket->connectToHostEncrypted(m_ServerTCPIPAddress, m_ServerTCPIPPort);
    m_sslSocket->waitForEncrypted(-1);
    

    I will get the "encrypted(void)" event. Additional line "waitForEncrypted(-1);", will start my communication. Why ?

    I am using the following signals for the communication

        connect(m_sslSocket,SIGNAL(disconnected(void)), this,SLOT(disconnected(void)));
        connect(m_sslSocket,SIGNAL(hostFound(void)), this,SLOT(hostFound(void)));
        connect(m_sslSocket,SIGNAL(encrypted(void)), this, SLOT(SSLReady(void)));
        connect(m_sslSocket,SIGNAL(connected(void)), this,SLOT(connected(void)));
        typedef void (QSslSocket::* sslErrorsSignal)(const QList<QSslError> &);
        connect(m_sslSocket, static_cast<sslErrorsSignal>(&QSslSocket::sslErrors),this, &DataThread::SSLError);
        connect(m_sslSocket,SIGNAL(readyRead(void)), this,SLOT(readyRead(void)));
        connect(m_sslSocket,SIGNAL(aboutToClose(void)), this,SLOT(aboutToClose(void)));
        connect(m_sslSocket,SIGNAL(bytesWritten(qint64)), this,SLOT(bytesWritten(qint64)));
    

    Where is my failure, because I wouldn't like to use the command "waitForEncrypted(-1);", because this is the reason
    of the SLOTS and SIGNALS.

    Best regards
    R.


  • Lifetime Qt Champion

    I don't see a good reason for that.

    Can you provide a minimal compilable example that shows the behaviour ?



  • @SGaist
    Hi,

    Try the following, maybe You can understand the problem.
    Download the sample ssl application from here
    https://github.com/GuiTeK/Qt-SslServer

    Modify the file "ClientExample.h"

    public slots:
        void run();
        void                SSLReady(void);           <-------- Add this line
    
    

    Add the following function file "ClientExample.cpp"

    void ClientExample::SSLReady(void)
    {
        qDebug() << "SSL Ready Connected";
    }
    

    Modify the void ClientExample::run()

    
        connect(&sslSocket,SIGNAL(encrypted(void)), this, SLOT(SSLReady(void)));  <---- Add
    //    QThread::msleep(2000);
    //    if (sslSocket.isEncrypted())    // Wait until encrypted connection is established, -1 means no timeout
        if (sslSocket.waitForEncrypted(-1))    // Wait until encrypted connection is established, -1 means no timeout
        {
            qDebug() << "isEncrypted  Connected";
    
    

    Remove and add the "//" depending on what you would like to see

    This configuration show the The of the SSLReady

    //    QThread::msleep(2000);
    //    if (sslSocket.isEncrypted())    // Wait until encrypted connection is established, -1 means no timeout
        if (sslSocket.waitForEncrypted(-1))    // Wait until encrypted connection is established, -1 means no timeout
    

    This does not give a connection

        QThread::msleep(2000);
        if (sslSocket.isEncrypted())    // Wait until encrypted connection is established, -1 means no timeout
    //    if (sslSocket.waitForEncrypted(-1))    // Wait until encrypted connection is established, -1 means no timeout
    

    But I am not sure, if the is a good sample, but it a base code to show the problem (hopefully).

    Best regards
    R.



  • Hi,

    still working on the problem.

    Is this the correct way to ignore a error, when you are using a self registered ssl certficate ?

       m_sslSocket=new QSslSocket(this);
        m_sslSocket->addCaCertificates(ApplicationPath + "../config/" + m_LocalSSLCertificate);
        m_sslSocket->ignoreSslErrors();
    

    Is this enough for disable the failure ?

    How can I disable only the certification error and not all ?

    Is this the correct way ?

        m_sslSocket=new QSslSocket(this);
        m_sslSocket->addCaCertificates(ApplicationPath + "../config/" + m_LocalSSLCertificate);
    
    
        QList<QSslCertificate> cert = QSslCertificate::fromPath(ApplicationPath + "../config/" + m_LocalSSLCertificate);
        QSslError error(QSslError::SelfSignedCertificate, cert.at(0));
        QList<QSslError> expectedSslErrors;
        expectedSslErrors.append(error);
    
        m_sslSocket->ignoreSslErrors(expectedSslErrors);
    

    I still did not get a event in the "sslErrors" and in the "encrypted".

    And I get the Messages in the DebuggerConsole

    "QSslSocket: cannot resolve SSLv2_client_method"
    "QSslSocket: cannot resolve SSLv2_server_method"
    

    Best regards
    R.


  • Lifetime Qt Champion

    What are you giving exactly to addCaCertificates ?


Log in to reply
 

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