This source file includes following definitions.
- context_index
- session_index
- GetSSLContextExIndex
- GetSSLSessionExIndex
- expiration_check_
- SetSSLSession
- SetSSLSessionWithKey
- MarkSSLSessionAsGood
- Flush
- SessionKey
- RemoveSessionLocked
- FlushExpiredSessionsLocked
- GetCache
- NewSessionCallbackStatic
- RemoveSessionCallbackStatic
- GenerateSessionIdStatic
- OnSessionAdded
- ShrinkCacheLocked
- OnSessionRemoved
- OnGenerateSessionId
- Reset
- SetSSLSession
- SetSSLSessionWithKey
- MarkSSLSessionAsGood
- Flush
#include "net/socket/ssl_session_cache_openssl.h"
#include <list>
#include <map>
#include <openssl/rand.h>
#include <openssl/ssl.h>
#include "base/containers/hash_tables.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/synchronization/lock.h"
namespace net {
namespace {
class SSLContextExIndex {
public:
SSLContextExIndex() {
context_index_ = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL);
DCHECK_NE(-1, context_index_);
session_index_ = SSL_SESSION_get_ex_new_index(0, NULL, NULL, NULL, NULL);
DCHECK_NE(-1, session_index_);
}
int context_index() const { return context_index_; }
int session_index() const { return session_index_; }
private:
int context_index_;
int session_index_;
};
base::LazyInstance<SSLContextExIndex>::Leaky s_ssl_context_ex_instance =
LAZY_INSTANCE_INITIALIZER;
static int GetSSLContextExIndex() {
return s_ssl_context_ex_instance.Get().context_index();
}
static int GetSSLSessionExIndex() {
return s_ssl_context_ex_instance.Get().session_index();
}
struct SessionId {
SessionId(const unsigned char* a_id, unsigned a_id_len)
: id(a_id), id_len(a_id_len), hash(ComputeHash(a_id, a_id_len)) {}
explicit SessionId(const SessionId& other)
: id(other.id), id_len(other.id_len), hash(other.hash) {}
explicit SessionId(SSL_SESSION* session)
: id(session->session_id),
id_len(session->session_id_length),
hash(ComputeHash(session->session_id, session->session_id_length)) {}
bool operator==(const SessionId& other) const {
return hash == other.hash && id_len == other.id_len &&
!memcmp(id, other.id, id_len);
}
const unsigned char* id;
unsigned id_len;
size_t hash;
private:
size_t ComputeHash(const unsigned char* id, unsigned id_len) {
size_t result = 0;
for (unsigned n = 0; n < id_len; ++n)
result += 131 * id[n];
return result;
}
};
}
}
namespace BASE_HASH_NAMESPACE {
template <>
struct hash<net::SessionId> {
std::size_t operator()(const net::SessionId& entry) const {
return entry.hash;
}
};
}
namespace net {
class SSLSessionCacheOpenSSLImpl {
public:
SSLSessionCacheOpenSSLImpl(SSL_CTX* ctx,
const SSLSessionCacheOpenSSL::Config& config)
: ctx_(ctx), config_(config), expiration_check_(0) {
DCHECK(ctx);
SSL_CTX_set_session_cache_mode(ctx_,
SSL_SESS_CACHE_CLIENT |
SSL_SESS_CACHE_NO_INTERNAL_STORE |
SSL_SESS_CACHE_NO_AUTO_CLEAR);
SSL_CTX_sess_set_new_cb(ctx_, NewSessionCallbackStatic);
SSL_CTX_sess_set_remove_cb(ctx_, RemoveSessionCallbackStatic);
SSL_CTX_set_generate_session_id(ctx_, GenerateSessionIdStatic);
SSL_CTX_set_timeout(ctx_, config_.timeout_seconds);
SSL_CTX_set_ex_data(ctx_, GetSSLContextExIndex(), this);
}
~SSLSessionCacheOpenSSLImpl() {
Flush();
SSL_CTX_set_ex_data(ctx_, GetSSLContextExIndex(), NULL);
SSL_CTX_sess_set_new_cb(ctx_, NULL);
SSL_CTX_sess_set_remove_cb(ctx_, NULL);
SSL_CTX_set_generate_session_id(ctx_, NULL);
}
size_t size() const { return key_index_.size(); }
bool SetSSLSession(SSL* ssl) {
std::string cache_key = config_.key_func(ssl);
if (cache_key.empty())
return false;
return SetSSLSessionWithKey(ssl, cache_key);
}
bool SetSSLSessionWithKey(SSL* ssl, const std::string& cache_key) {
base::AutoLock locked(lock_);
DCHECK_EQ(config_.key_func(ssl), cache_key);
if (++expiration_check_ >= config_.expiration_check_count) {
expiration_check_ = 0;
FlushExpiredSessionsLocked();
}
KeyIndex::iterator it = key_index_.find(cache_key);
if (it == key_index_.end())
return false;
SSL_SESSION* session = *it->second;
DCHECK(session);
DVLOG(2) << "Lookup session: " << session << " for " << cache_key;
void* session_is_good =
SSL_SESSION_get_ex_data(session, GetSSLSessionExIndex());
if (!session_is_good)
return false;
ordering_.push_front(session);
ordering_.erase(it->second);
it->second = ordering_.begin();
return SSL_set_session(ssl, session) == 1;
}
void MarkSSLSessionAsGood(SSL* ssl) {
SSL_SESSION* session = SSL_get_session(ssl);
if (!session)
return;
SSL_SESSION_set_ex_data(
session, GetSSLSessionExIndex(), reinterpret_cast<void*>(1));
}
void Flush() {
base::AutoLock lock(lock_);
id_index_.clear();
key_index_.clear();
while (!ordering_.empty()) {
SSL_SESSION* session = ordering_.front();
ordering_.pop_front();
SSL_SESSION_free(session);
}
}
private:
typedef std::list<SSL_SESSION*> MRUSessionList;
typedef base::hash_map<std::string, MRUSessionList::iterator> KeyIndex;
typedef base::hash_map<SessionId, KeyIndex::iterator> SessionIdIndex;
std::string SessionKey(SSL_SESSION* session) {
if (!session)
return std::string("<null-session>");
if (session->session_id_length == 0)
return std::string("<empty-session-id>");
SessionIdIndex::iterator it = id_index_.find(SessionId(session));
if (it == id_index_.end())
return std::string("<unknown-session>");
return it->second->first;
}
void RemoveSessionLocked(SSL_SESSION* session) {
lock_.AssertAcquired();
DCHECK(session);
DCHECK_GT(session->session_id_length, 0U);
SessionId session_id(session);
SessionIdIndex::iterator id_it = id_index_.find(session_id);
if (id_it == id_index_.end()) {
LOG(ERROR) << "Trying to remove unknown session from cache: " << session;
return;
}
KeyIndex::iterator key_it = id_it->second;
DCHECK(key_it != key_index_.end());
DCHECK_EQ(session, *key_it->second);
id_index_.erase(session_id);
ordering_.erase(key_it->second);
key_index_.erase(key_it);
SSL_SESSION_free(session);
DCHECK_EQ(key_index_.size(), id_index_.size());
}
void FlushExpiredSessionsLocked() {
lock_.AssertAcquired();
long timeout_secs = static_cast<long>(::time(NULL));
MRUSessionList::iterator it = ordering_.begin();
while (it != ordering_.end()) {
SSL_SESSION* session = *it++;
if (session->time + session->timeout <= timeout_secs) {
DVLOG(2) << "Expiring session " << session << " for "
<< SessionKey(session);
RemoveSessionLocked(session);
}
}
}
static SSLSessionCacheOpenSSLImpl* GetCache(SSL_CTX* ctx) {
DCHECK(ctx);
void* result = SSL_CTX_get_ex_data(ctx, GetSSLContextExIndex());
DCHECK(result);
return reinterpret_cast<SSLSessionCacheOpenSSLImpl*>(result);
}
static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) {
GetCache(ssl->ctx)->OnSessionAdded(ssl, session);
return 1;
}
static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) {
GetCache(ctx)->OnSessionRemoved(session);
}
static int GenerateSessionIdStatic(const SSL* ssl,
unsigned char* id,
unsigned* id_len) {
if (!GetCache(ssl->ctx)->OnGenerateSessionId(id, *id_len))
return 0;
return 1;
}
void OnSessionAdded(SSL* ssl, SSL_SESSION* session) {
base::AutoLock locked(lock_);
DCHECK(ssl);
DCHECK_GT(session->session_id_length, 0U);
std::string cache_key = config_.key_func(ssl);
KeyIndex::iterator it = key_index_.find(cache_key);
if (it == key_index_.end()) {
DVLOG(2) << "Add session " << session << " for " << cache_key;
ordering_.push_front(session);
std::pair<KeyIndex::iterator, bool> ret =
key_index_.insert(std::make_pair(cache_key, ordering_.begin()));
DCHECK(ret.second);
it = ret.first;
DCHECK(it != key_index_.end());
} else {
DVLOG(2) << "Replace session " << *it->second << " with " << session
<< " for " << cache_key;
SSL_SESSION* old_session = *it->second;
if (old_session != session) {
id_index_.erase(SessionId(old_session));
SSL_SESSION_free(old_session);
}
ordering_.erase(it->second);
ordering_.push_front(session);
it->second = ordering_.begin();
}
id_index_[SessionId(session)] = it;
if (key_index_.size() > config_.max_entries)
ShrinkCacheLocked();
DCHECK_EQ(key_index_.size(), id_index_.size());
DCHECK_LE(key_index_.size(), config_.max_entries);
}
void ShrinkCacheLocked() {
lock_.AssertAcquired();
DCHECK_EQ(key_index_.size(), ordering_.size());
DCHECK_EQ(key_index_.size(), id_index_.size());
while (key_index_.size() > config_.max_entries) {
MRUSessionList::reverse_iterator it = ordering_.rbegin();
DCHECK(it != ordering_.rend());
SSL_SESSION* session = *it;
DCHECK(session);
DVLOG(2) << "Evicting session " << session << " for "
<< SessionKey(session);
RemoveSessionLocked(session);
}
}
void OnSessionRemoved(SSL_SESSION* session) {
base::AutoLock locked(lock_);
DVLOG(2) << "Remove session " << session << " for " << SessionKey(session);
RemoveSessionLocked(session);
}
bool OnGenerateSessionId(unsigned char* id, unsigned id_len) {
base::AutoLock locked(lock_);
const size_t kMaxTries = 10;
for (size_t tries = 0; tries < kMaxTries; ++tries) {
if (RAND_pseudo_bytes(id, id_len) <= 0) {
DLOG(ERROR) << "Couldn't generate " << id_len
<< " pseudo random bytes?";
return false;
}
if (id_index_.find(SessionId(id, id_len)) == id_index_.end())
return true;
}
DLOG(ERROR) << "Couldn't generate unique session ID of " << id_len
<< "bytes after " << kMaxTries << " tries.";
return false;
}
SSL_CTX* ctx_;
SSLSessionCacheOpenSSL::Config config_;
base::Lock lock_;
MRUSessionList ordering_;
KeyIndex key_index_;
SessionIdIndex id_index_;
size_t expiration_check_;
};
SSLSessionCacheOpenSSL::~SSLSessionCacheOpenSSL() { delete impl_; }
size_t SSLSessionCacheOpenSSL::size() const { return impl_->size(); }
void SSLSessionCacheOpenSSL::Reset(SSL_CTX* ctx, const Config& config) {
if (impl_)
delete impl_;
impl_ = new SSLSessionCacheOpenSSLImpl(ctx, config);
}
bool SSLSessionCacheOpenSSL::SetSSLSession(SSL* ssl) {
return impl_->SetSSLSession(ssl);
}
bool SSLSessionCacheOpenSSL::SetSSLSessionWithKey(
SSL* ssl,
const std::string& cache_key) {
return impl_->SetSSLSessionWithKey(ssl, cache_key);
}
void SSLSessionCacheOpenSSL::MarkSSLSessionAsGood(SSL* ssl) {
return impl_->MarkSSLSessionAsGood(ssl);
}
void SSLSessionCacheOpenSSL::Flush() { impl_->Flush(); }
}