Results 1 to 2 of 2

Thread: QSslSocket based server never finishes handshake (Solved)

  1. #1
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy QSslSocket based server never finishes handshake (Solved)

    I'm trying to write a simple server using QSslSocket.

    I followed the theory in the docs and made a QTcpServer that creates a new thread for each connection, creates a new QSslSocket, then takes the socketDescriptor and applies that to it.

    The connection seems to start OK, but the encrypt() singal is never called, neither is sslErrors'

    The main relevant code from inside my QThread subclass

    Qt Code:
    1. IMThread::IMThread(int socketDescriptor, QObject *parent)
    2. : QThread(parent), socketDescriptor(socketDescriptor)
    3. {
    4. qRegisterMetaType<QAbstractSocket::SocketState>();
    5. qRegisterMetaType<QSslSocket::SslMode>();
    6. }
    7.  
    8. IMThread::~IMThread()
    9. {
    10.  
    11. }
    12.  
    13. void IMThread::run()
    14. {
    15. // Convert the socket to an SSL one
    16.  
    17. serverSocket = new QSslSocket();
    18. serverSocket->ignoreSslErrors();
    19.  
    20. connect( serverSocket, SIGNAL(readyRead()), this, SLOT(readyRead()) );
    21. connect( serverSocket, SIGNAL(encrypted()), this, SLOT(encrypted()) );
    22. connect( serverSocket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(sslModeChanged(QSslSocket::SslMode)) );
    23. connect( serverSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)) );
    24. connect( serverSocket, SIGNAL(disconnected()), this, SLOT(disconnected()) );
    25. connect( serverSocket, SIGNAL(connected()), this, SLOT(connected()) );
    26. connect( serverSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)) );
    27. connect( serverSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(stateChanged(QAbstractSocket::SocketState)) );
    28.  
    29. QSslCertificate myCert = QSslCertificate( "mycert.pem" );
    30. qDebug("Null Cert: %i Valid: %i", myCert.isNull(), myCert.isValid() );
    31.  
    32. QSslKey myKey = QSslKey( "mykey.pem", QSsl::Rsa );
    33. qDebug("Null Key: %i", myKey.isNull() );
    34.  
    35. serverSocket->setLocalCertificate( myCert );
    36. serverSocket->setPrivateKey( myKey );
    37.  
    38. if (serverSocket->setSocketDescriptor(socketDescriptor)) {
    39. serverSocket->startServerEncryption();
    40. }
    41. else {
    42. delete serverSocket;
    43. }
    44.  
    45. qDebug("State: %i Mode: %i Crypt: %i", serverSocket->state(), serverSocket->mode(), serverSocket->isEncrypted() );
    46.  
    47. }
    To copy to clipboard, switch view to plain text mode 

    When I start the server, and then try and connect using openssl, I only get:

    Qt Code:
    1. openssl s_client -bugs -connect 127.0.0.1:6162 -state
    2. CONNECTED(00000003)
    3. SSL_connect:before/connect initialization
    4. SSL_connect:SSLv2/v3 write client hello A
    To copy to clipboard, switch view to plain text mode 

    And the server shows:
    Qt Code:
    1. Null Cert: 0 Valid: 1
    2. Null Key: 0
    3. IMThread::stateChanged( 3 )
    4. IMThread::sslModeChanged( 2 )
    5. State: 3 Mode: 2 Crypt: 0
    To copy to clipboard, switch view to plain text mode 


    .. and there is just sits. No errors from the QSslSocket to explain why everything has stopped. Any idea where I might start looking for problems?

    Thanks
    Last edited by December; 19th September 2009 at 15:16. Reason: Solved

  2. #2
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSslSocket based server never finishes handshake

    Strange, adding:

    Qt Code:
    1. serverSocket->setProtocol( QSsl::AnyProtocol );
    To copy to clipboard, switch view to plain text mode 

    .. makes it work. Odd, as it never emits any errors, but obviously there are some with one of the protocols.

Similar Threads

  1. QSslSocket server
    By tpf80 in forum Qt Programming
    Replies: 3
    Last Post: 7th May 2009, 05:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.