【问题标题】:Server crash when trying to access QSslSocket functions尝试访问 QSslSocket 函数时服务器崩溃
【发布时间】:2013-12-05 13:12:49
【问题描述】:

所以我正在 QTCreator 中开发一个非常基本的服务器/客户端。当客户端尝试连接时,我应该让服务器将证书发送给客户端。我在服务器中有以下代码:

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);导致崩溃。 tcpSocket->setPrivateKey(key);不会导致崩溃,尽管我认为这是不正确的...... 同样处理 tcpSocket->setLocalCertificate();

有什么想法吗?顺便说一句,在这个位置 tcpSocket->flush() 也会崩溃...... 谢谢提前

【问题讨论】:

    标签: ssl-certificate qtcpserver qsslsocket


    【解决方案1】:

    您不能简单地将 QTcpSocket 转换为 QSslSocket 并期望一切正常。有关如何使用 Qt 编写 SSL 服务器的示例,请参阅 https://gitorious.org/qt-examples/qt-examples/source/7fdab8bcca3b1083cd07670131c75874300ad2d3:sslserver

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-09
      • 2023-02-08
      • 2018-02-09
      相关资源
      最近更新 更多