c++qtvisual-studio-2015opensslqsslsocket

How to install OpenSSL 1.1.0 for Qt on Windows?


My goal is to get the source code (or images) from a webpage over a SSL connection using QNetworkRequest with the QNetworkAccessManager.

OpenSSL 1.1.0 doesn't work with Qt 5.8! Look in Edit 2 for the install solution!

First Try: Get the ssleay32.lib and the libeay32.lib and copy them into the debug and the release folder. Dont work.

Second Try: (deleted because it's nonsense) But it doesnt work again.

Code (works fine for normal http):

QUrl url = QUrl(name);
data.clear();

QNetworkRequest *request = new QNetworkRequest(url);
request->setRawHeader("User-Agent", userAgent);

QSslConfiguration sslConfiguration(QSslConfiguration::defaultConfiguration());
request->setSslConfiguration(sslConfiguration);

reply = webCtrl->get(*request);

connect(webCtrl, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
connect(reply, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(reply, SIGNAL(finished()), this, SLOT(onReplyFinished()));

Error Messages:

qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
qt.network.ssl: QSslSocket: cannot call unresolved function d2i_DHparams
qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
qt.network.ssl: QSslSocket: cannot call unresolved function SSLv23_client_method
qt.network.ssl: QSslSocket: cannot call unresolved function SSL_CTX_new
qt.network.ssl: QSslSocket: cannot call unresolved function SSL_library_init
qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error
qt.network.ssl: QSslSocket: cannot call unresolved function ERR_get_error
Error creating SSL context ()

Edit 1: I found this Artikel and OpenSSL 1.1.0 will be working with Qt 5.10 but not in Qt 5.8 what I am using. And in this Artikel I found the QSslSocket::sslLibraryBuildVersionString() command and I Need OpenSSL Version: OpenSSL 1.0.2h 3 May 2016.

I will checkout the other OpenSSL Version later and edit this post if it is working or not.

Edit 2: Download OpenSSL 1.0.2h 3 May 2016 and Install 7zip and Perl. Extract the openSSL folder and follow INSTALL.W64. Go to the bin folder and copy libeay32-xxx.dll and ssleay32-xxx.dll to the application folder and rename the files to libeay32.dll and ssleay32.dll. Thanks @[Max Go].

Now the SSL connection finaly works, but I get an error message when I Close the application and the web connection was used at runtime.

Exception at 0x000000007772D1CB (ntdll.dll) in onlineTest.exe: 0xC0000005: Access Violation reading position 0x0000000000000074.

VS2015 Debug Window "mem.c" line 443:

  1. free_debug_func 0x0000000000000000 void(*)(void *, int)
  2. free_func 0x000007fee778b1ba {libeay32.dll!free} void(*)(void *)
  3. str 0x0000000002500cf0 void *

Edit 3: I forgot to compile the -d debug .dll's. Simply change the compiler mode to Debug, generate the Debug .dll's, rename if needed without the -d and then copy the debug .dll's into the debug folder and the normal .dll's into the release folder.


Solution

  • Here's the script I use to build OpenSSL automatically from a Visual Studio prompt. It will build either a 32 or 64 bit library, depending on what compiler is enabled.

    Prerequisites (must be in PATH):

    :: Configurable Options
    :: If you have jom available, set it to jom - it speeds up the build.
    @set JOM=nmake
    :: Choose a static or dynamic build
    @set LIBTYPE=dynamic
    :: OpenSSL version in the 1.0.2 series
    @set SRC=openssl-1.0.2k
    
    @if "%VSCMD_ARG_TGT_ARCH%"=="x86" (
        @set BITS=32
        @set DST=OpenSSL-Win32
        @set CONFIG=VC-WIN32
        @set SETUP=ms\do_nasm
    ) else if "%VSCMD_ARG_TGT_ARCH%"=="x64" (
        @set BITS=64
        @set DST=OpenSSL-Win64
        @set CONFIG=VC-WIN64A
        @set SETUP=ms\do_win64a
    ) else goto no_vscmd
    
    @if "%LIBTYPE%"=="static" (
        @set LIBTYPE=nt
    ) else if "%LIBTYPE%"=="dynamic" (
        @set LIBTYPE=ntdll
    ) else goto no_libtype
    
    @echo Building %SRC% for %BITS% bits.
    
    @echo - Downloading
    @perl ^
        -e "use LWP::Simple;" ^
        -e "mirror('https://www.openssl.org/source/%SRC%.tar.gz', '%SRC%.tar.gz');"
    
    @echo - Decompressing
    @if not exist %SRC%.tar.gz goto no_archive
    @rmdir /S /Q %SRC% %DST% 2>NUL
    @7z x -bsp2 -y %SRC%.tar.gz >NUL && ^
    7z x -bsp2 -y %SRC%.tar     >NUL && ^
    del %SRC%.tar
    @if errorlevel 1 goto unpack_failed
    @if not exist %SRC% goto no_source
    
    @echo - Building
    @pushd %SRC%
    @perl Configure %CONFIG% --prefix=%~dp0..\%DST% && ^
    call %SETUP% && ^
    nmake -f ms\%LIBTYPE%.mak init && ^
    %JOM% -f ms\%LIBTYPE%.mak "CC=cl /FS" && ^
    %JOM% -f ms\%LIBTYPE%.mak test && ^
    nmake -f ms\%LIBTYPE%.mak install || goto build_failed
    @popd
    @rmdir /S /Q %SRC%
    
    @echo Build has succeeded.
    @goto :eof
    
    :no_libtype
    @echo Error: LIBTYPE must be either "static" or "dynamic">&2
    @exit /b 1    
    
    :no_archive
    @echo Error: can't find %SRC%.tar.gz - the download has failed :(>&2
    @exit /b 1
    
    :unpack_failed
    @echo Error: unpacking has failed.>&2
    @exit /b %errorlevel%
    
    :no_source
    @echo Error: can't find %SRC%\>&2
    @exit /b 1
    
    :build_failed
    @echo The build had failed.>&2
    @popd
    @exit /b 2
    
    :no_vscmd
    @echo Use vcvarsall x86 or vcvarsall x64 to set up the Visual Studio>&2
    @echo build environment first.>&2
    @exit /b 100