美好的一天。
我正在创建一个需要使用OpenSSL的应用程序。
问题:
问题是多次阅读documentation,我仍然不太明白如何添加SSL支持。
有许多线程可以为Windows应用程序添加SSL,但对于Linux来说却很少,因为它没有提供很多细节。
我查看了提供的project file,但此处没有进行ssl链接。
我尝试了什么:
在我的应用程序中,我运行以下代码:
if (!QSslSocket::supportsSsl()) {
QMessageBox::critical(0, "Application Notice", "We care about your security. \nPlease install OpenSSL.");
return -1;
}
检查完成后会显示消息。
为了添加SSL支持,我查看了QT提供的实施SSL的示例securesocketclient
添加OPENSSL_LIBS
环境变量
回顾文档,它提到添加一个环境变量,允许qmake找到openssl lib文件的位置。
我有一个带标题的标准openssl安装
$ which openssl
/usr/bin/openssl
$ locate openssl/aes.h
/opt/Qt5.9.1/5.9.1/Src/qtwebengine/src/3rdparty/chromium/third_party/boringssl/src/include/openssl/aes.h
/usr/include/openssl/aes.h
/usr/include/openssl-1.0/openssl/aes.h
/usr/lib/libssl.so
/usr/lib/libssl.so.1.0.0
/usr/lib/libssl.so.1.1
/usr/lib/libssl3.so
/usr/lib/openssl-1.0/libssl.so
/usr/lib/openssl-1.0/pkgconfig/libssl.pc
/usr/lib32/libssl.so
/usr/lib32/libssl.so.1.0.0
/usr/lib32/libssl.so.1.1
/usr/lib32/libssl3.so
/usr/lib32/openssl-1.0/libssl.so
/usr/lib32/openssl-1.0/pkgconfig/libssl.pc
我认为提供的env变量指向错误的位置:
OPENSSL_LIBS='-L/opt/ssl/lib -lssl -lcrypto'
我根据我的系统进行了修改(虽然我不认为这是正确的):
OPENSSL_LIBS='-L/usr/lib -lssl -lcrypto'
在文档中还指出,需要配置标志-openssl-linked
。
搜索了将此标志添加到配置文件的方法(因为我的项目目录中没有该标志)
根据this,我将标志添加到我的项目文件中:
QMAKE_CXXFLAGS += -openssl-linked
但仍然没有SSL支持。
问题:
鉴于下面的.pro文件,在标准安装OpenSSL的情况下,如何向我的Qt应用程序添加OpenSSL支持?
应用输出
Debugging starts
qt.network.ssl: QSslSocket: cannot resolve CRYPTO_num_locks
qt.network.ssl: QSslSocket: cannot resolve CRYPTO_set_id_callback
qt.network.ssl: QSslSocket: cannot resolve CRYPTO_set_locking_callback
qt.network.ssl: QSslSocket: cannot resolve ERR_free_strings
qt.network.ssl: QSslSocket: cannot resolve EVP_CIPHER_CTX_cleanup
qt.network.ssl: QSslSocket: cannot resolve EVP_CIPHER_CTX_init
qt.network.ssl: QSslSocket: cannot resolve sk_new_null
qt.network.ssl: QSslSocket: cannot resolve sk_push
qt.network.ssl: QSslSocket: cannot resolve sk_free
qt.network.ssl: QSslSocket: cannot resolve sk_num
qt.network.ssl: QSslSocket: cannot resolve sk_pop_free
qt.network.ssl: QSslSocket: cannot resolve sk_value
qt.network.ssl: QSslSocket: cannot resolve SSL_library_init
qt.network.ssl: QSslSocket: cannot resolve SSL_load_error_strings
qt.network.ssl: QSslSocket: cannot resolve SSL_get_ex_new_index
qt.network.ssl: QSslSocket: cannot resolve SSLv2_client_method
qt.network.ssl: QSslSocket: cannot resolve SSLv3_client_method
qt.network.ssl: QSslSocket: cannot resolve SSLv23_client_method
qt.network.ssl: QSslSocket: cannot resolve SSLv2_server_method
qt.network.ssl: QSslSocket: cannot resolve SSLv3_server_method
qt.network.ssl: QSslSocket: cannot resolve SSLv23_server_method
qt.network.ssl: QSslSocket: cannot resolve X509_STORE_CTX_get_chain
qt.network.ssl: QSslSocket: cannot resolve OPENSSL_add_all_algorithms_noconf
qt.network.ssl: QSslSocket: cannot resolve OPENSSL_add_all_algorithms_conf
qt.network.ssl: QSslSocket: cannot resolve SSLeay
qt.network.ssl: QSslSocket: cannot resolve SSLeay_version
qt.network.ssl: Incompatible version of OpenSSL
Debugging has finished
专业档案
#-------------------------------------------------
#
# Project created by QtCreator 2017-12-02T18:53:15
#
#-------------------------------------------------
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MySSLApp
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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
SOURCES += \
main.cpp \
mainwindow.cpp \
loginform.cpp \
networkthread.cpp \
sslclient.cpp
HEADERS += \
mainwindow.h \
loginform.h \
networkthread.h \
sslclient.h
FORMS += \
mainwindow.ui \
loginform.ui
RESOURCES += \
resources.qrc
OPENSSL_LIBS='-L/usr/lib -lssl -lcrypto'
QMAKE_CXXFLAGS += -openssl-linked