PDA

View Full Version : QSslSocket based server never finishes handshake (Solved)



December
19th September 2009, 11:57
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



IMThread::IMThread(int socketDescriptor, QObject *parent)
: QThread(parent), socketDescriptor(socketDescriptor)
{
qRegisterMetaType<QAbstractSocket::SocketState>();
qRegisterMetaType<QSslSocket::SslMode>();
}

IMThread::~IMThread()
{

}

void IMThread::run()
{
// Convert the socket to an SSL one

serverSocket = new QSslSocket();
serverSocket->ignoreSslErrors();

connect( serverSocket, SIGNAL(readyRead()), this, SLOT(readyRead()) );
connect( serverSocket, SIGNAL(encrypted()), this, SLOT(encrypted()) );
connect( serverSocket, SIGNAL(modeChanged(QSslSocket::SslMode)), this, SLOT(sslModeChanged(QSslSocket::SslMode)) );
connect( serverSocket, SIGNAL(sslErrors(const QList<QSslError>&)), this, SLOT(sslErrors(const QList<QSslError>&)) );
connect( serverSocket, SIGNAL(disconnected()), this, SLOT(disconnected()) );
connect( serverSocket, SIGNAL(connected()), this, SLOT(connected()) );
connect( serverSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(error(QAbstractSocket::SocketError)) );
connect( serverSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)) , this, SLOT(stateChanged(QAbstractSocket::SocketState)) );

QSslCertificate myCert = QSslCertificate( "mycert.pem" );
qDebug("Null Cert: %i Valid: %i", myCert.isNull(), myCert.isValid() );

QSslKey myKey = QSslKey( "mykey.pem", QSsl::Rsa );
qDebug("Null Key: %i", myKey.isNull() );

serverSocket->setLocalCertificate( myCert );
serverSocket->setPrivateKey( myKey );

if (serverSocket->setSocketDescriptor(socketDescriptor)) {
serverSocket->startServerEncryption();
}
else {
delete serverSocket;
}

qDebug("State: %i Mode: %i Crypt: %i", serverSocket->state(), serverSocket->mode(), serverSocket->isEncrypted() );

}


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



openssl s_client -bugs -connect 127.0.0.1:6162 -state
CONNECTED(00000003)
SSL_connect:before/connect initialization
SSL_connect:SSLv2/v3 write client hello A


And the server shows:


Null Cert: 0 Valid: 1
Null Key: 0
IMThread::stateChanged( 3 )
IMThread::sslModeChanged( 2 )
State: 3 Mode: 2 Crypt: 0



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

December
19th September 2009, 15:15
Strange, adding:



serverSocket->setProtocol( QSsl::AnyProtocol );


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