如何在 Windows 上为 Qt 安装 OpenSSL 1.1.0?

How to install OpenSSL 1.1.0 for Qt on Windows?

我的目标是使用 QNetworkRequestQNetworkAccessManager 通过 SSL 连接从网页获取源代码(或图像)。

OpenSSL 1.1.0 不适用于 Qt 5.8!在编辑 2 中查找安装解决方案!

第一次尝试: 获取 ssleay32.lib 和 libeay32.lib 并将它们复制到调试和发布文件夹中。不工作。

二次尝试:(废话删) 但是它又不起作用了。

代码(适用于普通 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()));

错误信息:

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 ()

编辑 1: 我发现 this Artikel 和 OpenSSL 1.1.0 可以与 Qt 5.10 一起使用,但不能在我正在使用的 Qt 5.8 中使用。 在 this Artikel 中,我找到了 QSslSocket::sslLibraryBuildVersionString() 命令,我需要 OpenSSL 版本:OpenSSL 1.0.2h 3 May 2016

稍后我会检查其他 OpenSSL 版本并编辑此 post 是否有效。

编辑 2: 下载 OpenSSL 1.0.2h 3 May 2016 并安装 7zip 和 Perl。提取 openSSL 文件夹并按照 INSTALL.W64。转到 bin 文件夹并将 libeay32-xxx.dllssleay32-xxx.dll 复制到应用程序文件夹并将文件重命名为 libeay32.dllssleay32.dll。感谢@[Max Go]。

现在 SSL 连接终于可以工作了,但是当我关闭应用程序并且在运行时使用了 Web 连接时,我收到一条错误消息。

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

VS2015 调试 Window "mem.c" 第 443 行:

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

编辑 3: 我忘了编译 -d debug .dll。只需将编译器模式更改为“调试”,生成调试 .dll,如果需要则不使用 -d 重命名,然后将调试 .dll 复制到调试文件夹,将普通 .dll 复制到发布文件夹。

这是我用来从 Visual Studio 提示符自动构建 OpenSSL 的脚本。它将构建 32 位或 64 位库,具体取决于启用的编译器。

先决条件(必须在 PATH 中):

  • 活跃的 Perl
  • 7zip
  • NASM
  • Visual Studio 2015 年或 2017 年(我没有测试其他任何东西)
  • jom(可选)
:: 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