当前位置:首页 > Web开发 > 正文

* 如果平台不支持SSL

2024-03-31 Web开发

使用Qt的https(get,post,put等)请求时报qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed错误。

开发环境

Windows10 64位

Qt 5.12.1

MSVC 2017编译器(版本:15.0)

发明问题

在带开发环境的电脑上运行没问题,而移植到其他系统则有问题,报SSL错误,导致https请求不事情。

qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed. 问题分析

按照打印的错误提示找到对应源码。

void QSslSocket::connectToHostEncrypted(const QString &hostName, quint16 port, const QString &sslPeerName, OpenMode mode, NetworkLayerProtocol protocol) { ... if (!supportsSsl()) { qCWarning(lcSsl, "QSslSocket::connectToHostEncrypted: TLS initialization failed"); d->setErrorAndEmit(QAbstractSocket::SslInternalError, tr("TLS initialization failed")); return; } d->init(); ... }

如果supportsSsl函数返回false时打印错误(警告)信息,就不再执行后续操纵了,,于是继续检察supportsSsl函数。

/* 如果此平台撑持SSL,则返回true;否则,返回false。 * 如果平台不撑持SSL,套接字将掉败在连接阶段。 */ bool QSslSocket::supportsSsl() { return QSslSocketPrivate::supportsSsl(); } bool QSslSocketPrivate::supportsSsl() { return ensureLibraryLoaded(); }

ensureLibraryLoaded实现。

bool QSslSocketPrivate::ensureLibraryLoaded() { if (!q_resolveOpenSslSymbols()) return false; ... return true; }

由于Qt君使用的是windows系统所以选用loadOpenSslWin32加载SSL库函数。

bool q_resolveOpenSslSymbols() { ... #ifdef Q_OS_WIN QPair<QSystemLibrary *, QSystemLibrary *> libs = loadOpenSslWin32(); #else QPair<QLibrary *, QLibrary *> libs = loadOpenSsl(); #endif if (!libs.first || !libs.second) // failed to load them return false; ...

loadOpenSslWin32实现。

static QPair<QSystemLibrary*, QSystemLibrary*> loadOpenSslWin32() { ... #if QT_CONFIG(opensslv11) // With OpenSSL 1.1 the names have changed to libssl-1_1(-x64) and libcrypto-1_1(-x64), for builds using // MSVC and GCC, (-x64 suffix for 64-bit builds). #ifdef Q_PROCESSOR_X86_64 #define QT_SSL_SUFFIX "-x64" #else // !Q_PROCESSOFR_X86_64 #define QT_SSL_SUFFIX #endif // !Q_PROCESSOR_x86_64 tryToLoadOpenSslWin32Library(QLatin1String("libssl-1_1" QT_SSL_SUFFIX), QLatin1String("libcrypto-1_1" QT_SSL_SUFFIX), pair); #undef QT_SSL_SUFFIX #else // QT_CONFIG(opensslv11) // When OpenSSL is built using MSVC then the libraries are named 'ssleay32.dll' and 'libeay32'dll'. // When OpenSSL is built using GCC then different library names are used (depending on the OpenSSL version) // The oldest version of a GCC-based OpenSSL which can be detected by the code below is 0.9.8g (released in 2007) if (!tryToLoadOpenSslWin32Library(QLatin1String("ssleay32"), QLatin1String("libeay32"), pair)) { if (!tryToLoadOpenSslWin32Library(QLatin1String("libssl-10"), QLatin1String("libcrypto-10"), pair)) { if (!tryToLoadOpenSslWin32Library(QLatin1String("libssl-8"), QLatin1String("libcrypto-8"), pair)) { tryToLoadOpenSslWin32Library(QLatin1String("libssl-7"), QLatin1String("libcrypto-7"), pair); } } } #endif // !QT_CONFIG(opensslv11) return pair; }

Qt版本没有配置opensslv11,所以加载以下的SSL库操纵。依次查找ssleay32和libeay32,如果没有找到就查找libssl-10和libcrypto-10依此类推直到libssl-7和libcrypto-7。

if (!tryToLoadOpenSslWin32Library(QLatin1String("ssleay32"), QLatin1String("libeay32"), pair)) { if (!tryToLoadOpenSslWin32Library(QLatin1String("libssl-10"), QLatin1String("libcrypto-10"), pair)) { if (!tryToLoadOpenSslWin32Library(QLatin1String("libssl-8"), QLatin1String("libcrypto-8"), pair)) { tryToLoadOpenSslWin32Library(QLatin1String("libssl-7"), QLatin1String("libcrypto-7"), pair); } } } 问题解决

找对应平台版本的SSL库,Qt君将ssleay32和libeay32库放到运行目录下即解决了问题。

由于开发环境存在ssleay32和libeay32路径链接,而打包措施又没有复制SSL库,导致移植到其他电脑的SSL成果不正常的问题。

一些总结

可以将SSL库与应用措施一起部署,也可以在计算机上安置OpenSSL。

按照差此外Qt版本SSL库可能有所差别。

Dependency Walker工具也找不到依赖的库,因为它是运行时加载的库。注:Dependency Walker可以扫描任何32位或64位Windows模块exe,dll,ocx,sys等,并构建所有东西的条理布局树图。

SSL库Windows版本下载地点:

https://slproweb.com/products/Win32OpenSSL.html

SSL源码地点:

https://github.com/openssl/openssl

如果是mingw版本的SSL库,可不才列路径找到(是你实际的路径):

C:\Qt\Qt5.12.1\Tools\mingw730_64\opt\bin

开发日常 | 解决SSL缺库导致https掉败的问题

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31771.html