1

So I am working on a very basic server/client thing in QTCreator. I am supposed to have the server send the certificate to the client when a client tries to connect. I have the following code in the server:

void Dialog::createSocket()

{    
tcpSocket = (QSslSocket*)tcpServer->nextPendingConnection();

QByteArray key;
QByteArray cert;
QByteArray block;

QFile fileKey("C:/Users/siu850967358.AD.002/Desktop/ECE424_Lab2/server.key");
if(fileKey.open(QIODevice ::ReadOnly))
{
      key = fileKey.readAll();
      fileKey.close();
}
else
{
      qDebug() << fileKey.errorString();
}

QFile fileCert("C:/Users/siu850967358.AD.002/Desktop/ECE424_Lab2/server.crt");
if(fileCert.open(QIODevice ::ReadOnly))
{
      cert = fileCert.readAll();
      fileCert.close();
}
else
{
      qDebug() << fileCert.errorString();
}

qDebug() << key + "\n" + cert;

QString mykey(cert);
QSslKey ssl_Key(key, QSsl::Rsa);
QSslKey* sslKey = &ssl_Key;
QSslCertificate ssl_Cert(cert);
QSslCertificate* sslCert = &ssl_Cert;


//CRASH HAPPENS HERE

qDebug() << "ok";
tcpSocket->setPrivateKey(ssl_Key);
qDebug() << "ok2";
tcpSocket->setLocalCertificate(ssl_Cert);
qDebug() << "Key and certificate set";


//CRASH HAPPENS HERE


QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_0);
out << (quint16)0;
out << mykey;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
qDebug() << "write the certificate to the socket so that client may get the pubkey";
tcpSocket->write(block);


//connect(tcpSocket, SIGNAL(encrypted()), this, SLOT(readSocket()));
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readSocket()));
tcpSocket->startServerEncryption();


}

tcpSocket->setPrivateKey(ssl_Key); causes the crash. tcpSocket->setPrivateKey(key); does not cause a crash, although I believe that is incorrect.... same deal with tcpSocket->setLocalCertificate();

any ideas?? btw in this location tcpSocket->flush() will crash too... Thanks is advance

DDauS
  • 105
  • 1
  • 2
  • 11

1 Answers1

0

You can't simple cast a QTcpSocket to a QSslSocket and expect things to work. See https://gitorious.org/qt-examples/qt-examples/source/7fdab8bcca3b1083cd07670131c75874300ad2d3:sslserver for an example how to write an SSL server with Qt.

Richard Moore
  • 441
  • 3
  • 3