Aymeric
Aymeric

Reputation: 51

Qt error message "qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed"

I get the following error when I try to run my Qt application:

qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed
qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed
Error: "TLS initialization failed"

My application is really basic here is what it is look like:

#include "widget.h"
#include "ui_widget.h"
#include <QNetworkReply>

#include <QDebug>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    netManager  = new QNetworkAccessManager(this);
    netReply = nullptr;
    mDataBuffer = new QByteArray();

    //Define network request
    QNetworkRequest request;
    request.setUrl(QUrl("https://www.qt.io"));

    netReply = netManager->get(request);
    connect(netReply,&QIODevice::readyRead,this,&Widget::dataReadyToRead);
    connect(netReply,&QNetworkReply::finished,this,&Widget::dataReadFinished);

}

Widget::~Widget()
{
    delete ui;
}

void Widget::dataReadyToRead()
{
    qDebug() << "Datas available to read";
    mDataBuffer->append(netReply->readAll());
}

void Widget::dataReadFinished()
{
    if(netReply->error())
    {
        qDebug() << "Error: " << netReply->errorString();
    }
    else
    {
        ui->textEdit->setPlainText(QString(*mDataBuffer));
    }
}

So I searched on stack overflow and find some answers (it seems that the error occurs because the openSSl librairies are not found) but it still doesn't work.
So here is what I've done so far:

Did I miss something?

Edit:

Thanks for the reply:

Here is what is get from the qDebugs:

So here is I've grab and install OpenSSL in 1.0.2 version, (unistall the other one), add to the PATH variable the install folder (which is simply C:\OpenSSL-Win64, also tried C:\OpenSSL-Win64\bin both of those file cotains dll)

Unfortunately I still have the same problem.

Installation folder content:

Now I've tried to do somethings after I've read Qt documentation: https://doc.qt.io/qt-5/windows-requirements.html

Upvotes: 4

Views: 11761

Answers (4)

Manuel Vazquez
Manuel Vazquez

Reputation: 16

Go to: Qt5.12.2\Tools\mingw730_64\opt\bin directory ssleay_x64.dll libeay_x64.dll Copy those libs to the program directory.

this worked for me. I copied the specified files _x64 version and finally working. Copying other files didn't work for me.

Upvotes: 0

J. Gwinner
J. Gwinner

Reputation: 1565

The answers definitely work, but you have to find a nearly exact match of the OpenSSL library that your Qt app requires.

This may not be easy. For various reasons, the guy at https://slproweb.com/products/Win32OpenSSL.html seems to be particularly adamant about not supporting older versions of OpenSSL.

I get the point; it really does make sense, but in our case, it was a server side component that did a "Get" to a well known public https location, one single URL only. (An API). There's no real vulnerability with older versions, IMHO, but I realize that is certainly not be true for every app.

What my fix was, was to open up the app in the Visual Studio debugger. (Any debugger that shows demand load DLL's will work). I then hit "Run".

I found that our app, recently compiled by the way, loaded OpenSSL from "Folding at Home" despite the fact I had loaded every version of OpenSSL (and verified the path location) on our test box. Wouldn't run.

On my Dev box, I had NEVER loaded OpenSSL, yet the code ran fine.

I added the line of code that @Fareanor suggested (Upvote his answer also!):

qDebug() << QSslSocket::sslLibraryVersionString();

Our code said: OpenSSL Version needed: "OpenSSL 1.0.2p 14 Aug 2018"

Our code on my Dev box was demand loading:

C:\Program Files (x86)\FAHClient\SSLEAY32.dll

Yours may vary.

The FAH client was version 1.0.2o, or DLL module version: 1.0.2.15. The oldest version from the web site was: 1.0.2.21 and would NOT load even if placed directly in the run location (which is always checked before PATH locations).

Once I copied the OpenSSL files from Folding at Home, to the Prod box from my Dev box, suddenly OpenSSL started working in our code.

We also needed these files: libgcc_s_dw2-1.dll libgcc_s_dw2-1.dll libwinpthread-1.dll zlib1.dll

and those are not documented

Our code is based on an open source project that bundled the Qt library build in a separate "SDK" directory. I found the cmake scripts wouldn't run with VC 2019, or even a patched version of VC 2017, so we've been very reticent to upgrading the QT in it.

Hopefully, if you "do the right thing" and download Qt and build from their sources, you'll be able to bundle a newer OpenSSL with your app. I just wish that were slightly better documented in the Qt side. To be fair, if you do some digging they do mention you'll need OpenSSL, but it's not obvious.

In the event you're trying to get an app running and don't have, or can't upgrade to newer source, this method will at least tell you where the app is getting OpenSSL from.

    == John ==

Upvotes: 2

Aymeric
Aymeric

Reputation: 51

Here is the solution (that worked for me):

  • Check which version of OpenSSL was used for the Qt build with qDebug (see Fareanor answer).
  • Download a close version of OpenSSl for Windows 64 bits as it is my OS.
  • Execute the downloaded installer and check 'Copy OpenSSL DLLs to: The Windows system directory' checkbox.

Upvotes: 1

Fareanor
Fareanor

Reputation: 6805

Based on the information provided on the question, it looks like you have installed a too recent OpenSSL version. In fact I already had the same issue.

As you've found out, Qt does not embed OpenSSL. What you have to do is to install it by yourself. But you should ensure that you have the same (or the closest) version of OpenSSL than the one used for the Qt build.

  • You can check what version of OpenSSL was used for the Qt build with:
qDebug() << QSslSocket::sslLibraryBuildVersionString();
  • You can check if SSL is supported with:
qDebug() << QSslSocket::supportsSsl();
  • You can check what version you have with:
qDebug() << QSslSocket::sslLibraryVersionString();

Note: Don't forget to include the install directory to the PATH environment variable to make the dlls accessible (or copy the dlls).


Let me know if it does not solve your issue so that I can remove this answer to avoid unnecessary noise.

Upvotes: 4

Related Questions