Qt SSL Unable to init SSL Context error
-
I am trying to connect an SSL client to an SSL server using the Qt SSL classes, but I am getting the error "Unable to init SSL Context". I made my code as easy as possible to figure out what the problem could be. I created the private key and certificate with the following command on my MacOS terminal:
openssl req -x509 -newkey rsa:4096 -keyout privatekey.pem -out certificate.pem -days 365. So it looks like openssl is installed? QSslSocket::supportSsl() also returns true. What do I need to do to make the server and client work so that they can communicate with eachother? My code and output is below. Thank you in advance!Client:
#include <QSslSocket> #include <QFile> int main() { QSslSocket* socket = new QSslSocket; qDebug() << socket->supportsSsl(); socket->setProtocol(QSsl::TlsV1_2); socket->connectToHostEncrypted("192.168.2.33", 5555, "192.168.2.33"); if (!socket->waitForEncrypted()) { qDebug() << socket->errorString(); return 0; } qDebug() << "Encryption succesfull"; socket->write("Client here"); qDebug() << "Written to socket"; while (socket->waitForReadyRead()) { qDebug() << "Reading"; qDebug() << socket->readAll().data(); } qDebug() << "Ending program"; delete socket; return 0; }
Server:
#include <QTcpServer> #include <QSslSocket> int main() { QTcpServer* server = new QTcpServer; QSslSocket* socket = new QSslSocket; qDebug() << socket->supportsSsl(); socket->setLocalCertificate("/mylocalfolders/certificate.pem"); socket->setPrivateKey("/mylocalfolders/privatekey.pem",QSsl::Rsa,QSsl::Pem,"secret"); socket->setProtocol(QSsl::TlsV1_2); server->listen(QHostAddress("192.168.2.33"), 5555); if(server->waitForNewConnection(-1)) { qDebug() << "New connection"; socket->startServerEncryption(); if (!socket->waitForEncrypted()) { qDebug() << socket->errorString(); return 0; } qDebug() << "Encryption succesfull"; socket->write("Server here"); qDebug() << "Written to socket"; while (socket->waitForReadyRead()) { qDebug() << "Reading"; qDebug() << socket->readAll().data(); } } qDebug() << "Ending program"; delete server; delete socket; return 0; }
Output client:
true "The remote host closed the connection"
Output server:
true New connection "Unable to init SSL Context"
-
Hi and welcome to devnet,
What version of Qt are you using ?
On what version of macOS ? -
I wonder if it's related to QTBUG-59068.
Otherwise, you maybe using a key with the "modern" header which I remember has caused some trouble to another member. Can you show the header of your key ?
-
The header of my privatekey.pem is BEGIN ENCRYPTED PRIVATE KEY.
Today I tried to run the code again. Strangely the output is different!!! I am absolutely sure I didn't change something! The output of my server program is now:
true Ending program
And my client:
true "Unknown error"
The "Unknown error" appears exactly 30 seconds after I start the client. True and the server output showed up immediately.
-
Update of Qt version ?
Update of OpenSSL ?