boost :: asio不直接支持ssl会话缓存机制。但是,由于boost :: asio :: ssl :: stream保留了SSL_SESSION对象(来自openssl库),所以很容易手动完成。
实现可能如下:
boost::asio::io_service io;
boost::asio::ssl::context ctx(boost::asio::ssl::context::sslv23_client);
boost::asio::ssl::stream<boost::asio::ip::tcp::socket> backend(io, ctx);
// need some object that will store the cache
std::map<std::string, SSL_SESSION*> ssl_cache;
// add session to the cache after a successful connection
SSL_SESSION *session = SSL_get1_session(backend.native_handle());
ssl_cache[host] = session;
// before a new connection to the 'host', check the cache
auto cached_session = ssl_cache.find(host);
if (cached_session != ssl_cache.end())
{
SSL_SESSION *session = cached_session->second;
SSL_set_session(backend.native_handle(), session);
}
// after a connection can check if ssl-session was reused
if (SSL_session_reused(backend.native_handle()))
{
// reused
}
重要的是,这种方法同时支持缓存机制:
- SSL票(RFC 5077)
- 会话标识符(RFC 5246)
我认为这是由于ssl_context:http://stackoverflow.com/a/35136363/85371 – sehe
@sehe,谢谢,这是非常有帮助的。我明白了,我需要使用'ssl :: context :: native_handle()'+'SSL_CTX_set_session_cache_mode()'+'SSL_set_session()'等等。我会尝试 ) – o2gy