在 Qt C++ 中使用 OpenSSL

2021-12-09 00:00:00 qt ssl openssl c++

我得到了一个示例项目,它使用 QSslSocketQSslCertificate 从 SSL 证书(Qt 5.7、Windows 10)获取信息.QSslSocket::supportsSsl() 函数返回 false,我在运行时收到这些错误:

I got a sample projct that uses QSslSocket and QSslCertificate to get information from a SSL certificate (Qt 5.7, Windows 10). QSslSocket::supportsSsl() function returns false and I get these errors at runtime:

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

所以我编译了 OpenSSL_1.1.0 并将其链接到我的项目:

So I compiled OpenSSL_1.1.0 and linked it to my project:

INCLUDEPATH += . "C:/OpenSSL-Win32/include/" LIBS +=
-L"C:/OpenSSL-Win32/lib/" -llibcrypto -llibssl

但还是一样的错误.我下载了 OpenSSL 安装程序,但还是一样.

But still same errors. I downloaded OpenSSL installer and still the same.

奇怪的是 QSslSocket::sslLibraryBuildVersionString() 返回 OpenSSL 1.0.2g 1 Mar 2016,即使我编译并安装了 OpenSSL_1.1.0.

Weird is QSslSocket::sslLibraryBuildVersionString() returns OpenSSL 1.0.2g 1 Mar 2016, even though I compiled and installed OpenSSL_1.1.0.

我知道我遗漏了一个简单的细节.请告诉我是什么.谢谢.

I know I'm missing a simple detail. Please tell me what is it. Thanks.

推荐答案

来自文档:

QSslSocket::sslLibraryBuildVersionString() 返回 SSL 库的版本字符串在编译时使用.(强调我的)

QSslSocket::sslLibraryBuildVersionString() Returns the version string of the SSL library in use at compile time. (emphasis mine)

你从它得到结果并不意味着 Qt 可以访问 OpenSSL,只是它是针对某个版本编译的.这让您知道如果动态链接(默认情况下),您应该在运行时提供哪个二进制兼容版本.

That you get a result from it doesn't mean that Qt has access to OpenSSL, only that it was compiled against a certain version. That lets you know what binary-compatible version you should provide at runtime if it's dynamically linked (as it is by default).

您想检查 QSslSocket::sslLibraryVersionNumber() - [...] 库的版本在运行时使用(重点是我的).

You want to check QSslSocket::sslLibraryVersionNumber() - [...] the version of the library in use at run-time (emphasis mine).

Qt 可能会尝试在 PATH 中找到 OpenSSL dll 的副本,如果没有,则在 QCoreApplication::libraryPaths 给出的路径中.在尝试使用 OpenSSL 之前,您应该将 OpenSSL dll 的位置添加到其中之一.

Qt will presumably attempt to find a copy of OpenSSL dlls in PATH, and if not, in the paths given by QCoreApplication::libraryPaths. You should add the location of OpenSSL dlls to either one before attempting to use OpenSSL.

相关文章