Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does boost::asio use ssl session cache?

Tags:

For example, I develope email client. I know that some servers, for example, imap.gmail.com, cache SSL sessions. So I want reuse SSL sessions (from cache on my side) to reduce server load.

I use boost::asio as network engine. Questions are:

  1. if boost::asio::ssl::stream doesn't use the ssl-session-cache, how can I enable it?
  2. if boost::asio::ssl::stream use the ssl-session-cache, how can I turn it off? :)
like image 871
Andrew Moylan Avatar asked Mar 25 '16 19:03

Andrew Moylan


People also ask

Does SSL use session caching?

SSL can cache session information based on the Session ID (SID). SSL connections can request that a previous session be resumed. When session information is found in the cache, connections can use the SSL short handshake, which requires less processing.

What is SSL session cache?

File-based cache of established SSL sessions. When re-establishing a connection to the same server, using an SSL session cache can save some time, power, and bandwidth by skipping directly to an encrypted stream. This is a persistent cache which can span executions of the application.


1 Answers

boost::asio does not support ssl-session caching mechanism directly. But, as boost::asio::ssl::stream keeps SSL_SESSION object (from the openssl library) inside, it is easy to do manually.

An implementation could be as follows:

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
}

It is important that this approach supports both caching mechanism:

  • ssl-tickets (RFC 5077)
  • session identifiers (RFC 5246)
like image 71
Andrew Moylan Avatar answered Oct 11 '22 14:10

Andrew Moylan