Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Qt SSL Unable to init SSL Context error
Qt 6.11 is out! See what's new in the release blog

Qt SSL Unable to init SSL Context error

Scheduled Pinned Locked Moved Unsolved Installation and Deployment
6 Posts 2 Posters 3.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JackA
    wrote on last edited by
    #1

    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"
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      What version of Qt are you using ?
      On what version of macOS ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JackA
        wrote on last edited by
        #3

        Thank you!
        I am using Qt Creator 4.6.1 based on Qt 5.10.1 on macOS High Sierra 10.13.4.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • J Offline
            J Offline
            JackA
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Update of Qt version ?
              Update of OpenSSL ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved