Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

TLS initialization failed during API call?



  • I've used this same code for a project working with the ITunes API and it worked fine. Currently I am trying to work with a crypto api shown here (documentation also on webpage). I make the correct call but all I get it this error:

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

    Is there a reason for this and a fix? I know it either has to do with openssl or the type of api I'm calling to?

    API: https://www.cryptocompare.com/coins/guides/how-to-use-our-api/

    Current code:
    //pro file
    QT += network
    QT += websockets

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = binance
    TEMPLATE = app

    The following define makes your compiler emit warnings if you use

    any feature of Qt which has been marked as deprecated (the exact warnings

    depend on your compiler). Please consult the documentation of the

    deprecated API in order to know how to port your code away from it.

    DEFINES += QT_DEPRECATED_WARNINGS

    You can also make your code fail to compile if you use deprecated APIs.

    In order to do so, uncomment the following line.

    You can also select to disable deprecated APIs only up to a certain version of Qt.

    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

    CONFIG += c++11

    SOURCES +=
    main.cpp
    widget.cpp

    HEADERS +=
    widget.h

    FORMS +=
    widget.ui

    Default rules for deployment.

    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target


    //widget.h

    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QWidget>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QUrl>
    #include <QUrlQuery>
    #include <QtNetwork/QNetworkReply>
    #include <QJsonArray>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QJsonValue>
    #include <QDebug>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <QtWebSockets/QtWebSockets>
    #include <QSslSocket>

    using namespace std;

    namespace Ui {
    class Widget;
    }

    class Widget : public QWidget
    {
    Q_OBJECT

    public:
    string apiKey = "vmPUZE6mv9SD5VNHk4HlWFsOr6aKE2zvsw0MuIgwCIPy6utIco14y7Ju91duEh8A";
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

    private:
    Ui::Widget *ui;

    private slots:
    void managerFinished(QNetworkReply *reply);
    };

    #endif // WIDGET_H


    //widget.cpp

    #include "widget.h"
    #include "ui_widget.h"

    using namespace std;

    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    QUrl myUrl("https://min-api.cryptocompare.com/data/pricemulti?fsyms=ETH,DASH&tsyms=BTC,USD,EUR&api_key=3f8d2ada68e40eab3e20ea047d34530b28922f7ced692603334ddff18455f90b");
    QNetworkRequest myRequest(myUrl);
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(managerFinished(QNetworkReply*)));
    manager->get(myRequest);
    ui->setupUi(this);
    

    }

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

    void Widget::managerFinished(QNetworkReply *reply) {
    QByteArray *myByteArray = new QByteArray();
    myByteArray->append(reply->readAll());
    string str = myByteArray->toStdString();
    qDebug() <<"str" << str.c_str();
    QJsonDocument myJson = QJsonDocument::fromJson(*myByteArray);
    //Now use QJsonObject and QJsonArray to parse the Json
    }


  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?
    What platform are you running on ?



  • I am running Qt Cteator 4.8.0 Community on Windows 10.


  • Lifetime Qt Champion

    Again: what version of Qt are you using ?
    Do you have OpenSSL installed on your system ?



  • Apologies I thought 4.8 was the version, that's just what it shows when I open it. I installed 5.12.0.

    I recently downloaded openssl-1.0,2a-win32-mingw so I have OpenSSL downloaded.


  • Lifetime Qt Champion

    No, that's the Qt Creator version which is unrelated to the Qt version you are using to build your application.

    IIRC, Qt 5,12 is using OpenSSL 1.1. The OpenSSL 1.1 series is not backward compatible with the OpenSSL 1.0 series.



  • Ok so Qt 5.12.
    Currently I have no OpenSSL from before 1.1 attached to the project though. I have 1.0.2 in my downloads but it currently is not associated with Qt or the project. Knowing this and seeing the code, any idea how to fix the error?


  • Lifetime Qt Champion

    Download OpenSSL 1.1 and then in the Run part of the Project panel, modify the PATH environnement variable and add the folder where the OpenSSL .dll files can be found.



  • @SGaist My I ask how to actually do that? I have the file in C:/OpenSSL-Win64 but I'm not sure where to exactly add it or insert it? I don't see an explicit PATH environment variable that this could be added to, or at least if I do see it I'm unsure if it is it.


  • Lifetime Qt Champion

    @ckzaner Did you check the Run settings as @SGaist suggested?
    I'm pretty sure you have PATH variable under "Run Environment" there.
    So, click on "Projects" at the left side in QtCreator and select "Run" in the Kit you're using and then expand "Run Environment".
    You need to add the path containing OpenSSL libraries to PATH there.



  • Ok so I went from Projects--->Run (on left side underneath Build)
    There is not anything explicitly called the PATH environment or anything like that.
    There is the section under Run which shows me the Run Configuration which shows my current directory for the project.
    Would I select "add" here and then give the directions to the OpenSSL-Win64 file?

    Edit::: @jsulm Actually at the bottom there is a Run Environment section. I'll see if I do this right.
    I'm assuming select add, name it opensslv1.1, then give path to the libs folder and that's it?



  • @jsulm So I have this in the Run Environment now.
    0_1545118944140_openssl.PNG
    I assume there's more to be done?
    I still get the same error "qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed"


  • Lifetime Qt Champion

    @ckzaner You need to change PATH variable there as @SGaist suggested...



  • @jsulm I found "Path". Maybe I need to change something else?
    This is the value now:
    C:\Qt\Tools\mingw730_64\bin;C:\Qt\5.12.0\mingw73_64\bin;C:\Qt\Tools\mingw730_64\bin;C:\OpenSSL-Win64\bin;C:\Windows\system32;......(so on so forth).

    The bin has all the .dll folders. Not sure if Qt is using them though. I used the line:
    qDebug() <<"ssl:" << QSslSocket::sslLibraryBuildVersionString();
    to try and see if Qt was using it but it says OpenSSL 1.0.2p


  • Lifetime Qt Champion

    Ok, then in that case, point it to the OpenSSL 1.0 you installed on your machine.



  • This post is deleted!

Log in to reply