similar questionBoost Asio SSL握手永不返回
我的情況是不同的。我已經使用boost::asio::ssl
編寫了一個SSL客戶端,但目前沒有服務器可以測試,因爲它正在由其他組開發。所以本服務器只接受普通的TCP(不安全)連接。當我用我的SSL客戶端對服務器,阻止handshake()
掛起/從不返回。我在網上搜索了一下,發現Mozilla也有類似的問題 - 當SSL連接到非SSL支持服務器時,它掛起,但是它們的bug已經修復。我只是把解釋,我的部分代碼,以確保沒有編碼錯誤:
在ctor
:
SecuredConnectionPolicy<ThreadPolicy>::SecuredConnectionPolicy() :
m_sslContext(boost::asio::ssl::context::sslv23),
m_socket(m_ioService, m_sslContext) //ssl::stream<tcp::socket>
{
}
然後,當我的「connect
(......)」被稱爲:
m_sslContext.set_options(boost::asio::ssl::context::default_workarounds);
m_sslContext.set_verify_mode(
boost::asio::ssl::context::verify_none,
errorCode
);
if(!errorCode)
{
/*m_sslContext.set_verify_callback(
[this](bool bIsPreverificationSuccessful, boost::asio::ssl::verify_context &context){return this->verificationHandler(bIsPreverificationSuccessful, context);},
errorCode
);*/
if(!errorCode)
{
m_sslContext.load_verify_file("newcert.pem", errorCode);
if(!errorCode)
{
m_socket.lowest_layer().connect(remoteEndpoint, errorCode);
if(!errorCode)
{ // ########### Following NEVER RETURNS #############
m_socket.handshake(boost::asio::ssl::stream_base::client, errorCode);
if(errorCode)
{
std::cerr << "Secured Connection Handshake Failed! " << errorCode.message() << std::endl;
}
}
else
{
std::cerr << "Secured Connection Failed! " << errorCode.message() << std::endl;
}
}
else
{
std::cerr << "Secured Connection loading certificate files from default paths Failed! " << errorCode.message() << std::endl;
}
}
else
{
std::cerr << "Registering Verification callback failed! " << errorCode.message() << std::endl;
}
}
else
{
std::cerr << "Secured Connection verify mode Failed! " << errorCode.message() << std::endl;
}
可能是什麼原因?我做錯了什麼?
我沒有提供任何verify_callback處理程序,因爲我假設由OpenSSL完成的預驗證(因爲無處不在,它說它調用OpenSSL等效函數)應該足夠了。是否有任何缺點或可能影響< 1>?
這是一個微不足道的問題,但只是爲了確保它不會導致問題:通常boost中的示例顯示ssl上下文對象已在設置爲
ssl::stream<tcp::socket>
的ctor之前設置。然而,我在之前(在上面的ctor中)給出它,並且在connect()
之後更改了它的屬性。這些會反映在構造ssl :: stream的行爲中(因爲它需要引用,我希望它不會創建任何副本)?
在旁註中(如果有用),我創建了一個CA rootKey,CA自簽名PEM證書,由CA證書籤名的服務器證書。 CA證書是我給load_verify_file(...)
。