ssl_session_cache_openssl.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// found in the LICENSE file.
4a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
5a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "net/socket/ssl_session_cache_openssl.h"
6a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
7a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <list>
8a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <map>
9a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
10a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <openssl/rand.h>
11a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <openssl/ssl.h>
12a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
13a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/containers/hash_tables.h"
14a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/lazy_instance.h"
15a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/logging.h"
16a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/synchronization/lock.h"
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace net {
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace {
21a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// A helper class to lazily create a new EX_DATA index to map SSL_CTX handles
23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// to their corresponding SSLSessionCacheOpenSSLImpl object.
24a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class SSLContextExIndex {
25a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)public:
26a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SSLContextExIndex() {
27a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    index_ = SSL_CTX_get_ex_new_index(0, 0, 0, 0, 0);
28a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_NE(-1, index_);
29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
30a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
31a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  int index() const { return index_; }
32a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
33a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) private:
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  int index_;
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)};
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// static
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::LazyInstance<SSLContextExIndex>::Leaky s_ssl_context_ex_instance =
39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    LAZY_INSTANCE_INITIALIZER;
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Retrieve the global EX_DATA index, created lazily on first call, to
42a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// be used with SSL_CTX_set_ex_data() and SSL_CTX_get_ex_data().
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)static int GetSSLContextExIndex() {
44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return s_ssl_context_ex_instance.Get().index();
45a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
46a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
47a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Helper struct used to store session IDs in a SessionIdIndex container
48a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// (see definition below). To save memory each entry only holds a pointer
49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// to the session ID buffer, which must outlive the entry itself. On the
50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// other hand, a hash is included to minimize the number of hashing
51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// computations during cache operations.
52a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)struct SessionId {
53a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SessionId(const unsigned char* a_id, unsigned a_id_len)
54a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      : id(a_id), id_len(a_id_len), hash(ComputeHash(a_id, a_id_len)) {}
55a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
56a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  explicit SessionId(const SessionId& other)
57a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      : id(other.id), id_len(other.id_len), hash(other.hash) {}
58a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
59a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  explicit SessionId(SSL_SESSION* session)
60a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      : id(session->session_id),
61a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        id_len(session->session_id_length),
62a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        hash(ComputeHash(session->session_id, session->session_id_length)) {}
63a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool operator==(const SessionId& other) const {
65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return hash == other.hash && id_len == other.id_len &&
66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)           !memcmp(id, other.id, id_len);
67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
68a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
69a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const unsigned char* id;
70a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  unsigned id_len;
71a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  size_t hash;
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) private:
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Session ID are random strings of bytes. This happens to compute the same
75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // value as std::hash<std::string> without the extra string copy. See
76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // base/containers/hash_tables.h. Other hashing computations are possible,
77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // this one is just simple enough to do the job.
78a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  size_t ComputeHash(const unsigned char* id, unsigned id_len) {
79a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    size_t result = 0;
80a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    for (unsigned n = 0; n < id_len; ++n)
81a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      result += 131 * id[n];
82a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return result;
83a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)};
85a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
86a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace
87a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
88a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace net
89a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
90a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace BASE_HASH_NAMESPACE {
91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
92a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)template <>
93a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)struct hash<net::SessionId> {
94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  std::size_t operator()(const net::SessionId& entry) const {
95a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return entry.hash;
96a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
97a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)};
98a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
99a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace BASE_HASH_NAMESPACE
100a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
101a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)namespace net {
102a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
103a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Implementation of the real SSLSessionCache.
104a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
105a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// The implementation is inspired by base::MRUCache, except that the deletor
106a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// also needs to remove the entry from other containers. In a nutshell, this
107a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// uses several basic containers:
108a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
109a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   |ordering_| is a doubly-linked list of SSL_SESSION handles, ordered in
110a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   MRU order.
111a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
112a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   |key_index_| is a hash table mapping unique cache keys (e.g. host/port
113a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   values) to a single iterator of |ordering_|. It is used to efficiently
114a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   find the cached session associated with a given key.
115a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
116a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   |id_index_| is a hash table mapping SessionId values to iterators
117a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   of |key_index_|. If is used to efficiently remove sessions from the cache,
118a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   as well as check for the existence of a session ID value in the cache.
119a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
120a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   SSL_SESSION objects are reference-counted, and owned by the cache. This
121a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   means that their reference count is incremented when they are added, and
122a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//   decremented when they are removed.
123a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
124a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Assuming an average key size of 100 characters, each node requires the
125a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// following memory usage on 32-bit Android, when linked against STLport:
126a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
127a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//      12   (ordering_ node, including SSL_SESSION handle)
128a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//     100   (key characters)
129a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//    + 24   (std::string header/minimum size)
130a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//    +  8   (key_index_ node, excluding the 2 lines above for the key).
131a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//    + 20   (id_index_ node)
132a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//  --------
133a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//     164   bytes/node
134a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
135a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Hence, 41 KiB for a full cache with a maximum of 1024 entries, excluding
136a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// the size of SSL_SESSION objects and heap fragmentation.
137a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
138a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
139a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class SSLSessionCacheOpenSSLImpl {
140a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) public:
141a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Construct new instance. This registers various hooks into the SSL_CTX
142a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // context |ctx|. OpenSSL will call back during SSL connection
143a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // operations. |key_func| is used to map a SSL handle to a unique cache
144a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // string, according to the client's preferences.
145a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SSLSessionCacheOpenSSLImpl(SSL_CTX* ctx,
146a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                             const SSLSessionCacheOpenSSL::Config& config)
147a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      : ctx_(ctx), config_(config), expiration_check_(0) {
148a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK(ctx);
149a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
150a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // NO_INTERNAL_STORE disables OpenSSL's builtin cache, and
151a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // NO_AUTO_CLEAR disables the call to SSL_CTX_flush_sessions
152a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // every 256 connections (this number is hard-coded in the library
153a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // and can't be changed).
154a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_set_session_cache_mode(ctx_,
155a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                   SSL_SESS_CACHE_CLIENT |
156a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                       SSL_SESS_CACHE_NO_INTERNAL_STORE |
157a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                       SSL_SESS_CACHE_NO_AUTO_CLEAR);
158a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
159a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_sess_set_new_cb(ctx_, NewSessionCallbackStatic);
160a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_sess_set_remove_cb(ctx_, RemoveSessionCallbackStatic);
161a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_set_generate_session_id(ctx_, GenerateSessionIdStatic);
162a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_set_timeout(ctx_, config_.timeout_seconds);
163a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
164a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_set_ex_data(ctx_, GetSSLContextExIndex(), this);
165a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
166a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
167a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Destroy this instance. Must happen before |ctx_| is destroyed.
168a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ~SSLSessionCacheOpenSSLImpl() {
169a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    Flush();
170a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_set_ex_data(ctx_, GetSSLContextExIndex(), NULL);
171a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_sess_set_new_cb(ctx_, NULL);
172a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_sess_set_remove_cb(ctx_, NULL);
173a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_CTX_set_generate_session_id(ctx_, NULL);
174a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
175a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
176a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Return the number of items in this cache.
177a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  size_t size() const { return key_index_.size(); }
178a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
179a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Retrieve the cache key from |ssl| and look for a corresponding
180a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // cached session ID. If one is found, call SSL_set_session() to associate
181a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // it with the |ssl| connection.
182a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  //
183a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Will also check for expired sessions every |expiration_check_count|
184a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // calls.
185a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  //
186a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Return true if a cached session ID was found, false otherwise.
187a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool SetSSLSession(SSL* ssl) {
188a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    std::string cache_key = config_.key_func(ssl);
189a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (cache_key.empty())
190a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return false;
191a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
192a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return SetSSLSessionWithKey(ssl, cache_key);
193a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
194a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
195a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Variant of SetSSLSession to be used when the client already has computed
196a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // the cache key. Avoid a call to the configuration's |key_func| function.
197a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool SetSSLSessionWithKey(SSL* ssl, const std::string& cache_key) {
198a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::AutoLock locked(lock_);
199a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
200a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_EQ(config_.key_func(ssl), cache_key);
201a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
202a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (++expiration_check_ >= config_.expiration_check_count) {
203a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      expiration_check_ = 0;
204a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      FlushExpiredSessionsLocked();
205a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
206a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
207a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    KeyIndex::iterator it = key_index_.find(cache_key);
208a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (it == key_index_.end())
209a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return false;
210a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
211a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_SESSION* session = *it->second;
212a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK(session);
213a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
214a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DVLOG(2) << "Lookup session: " << session << " for " << cache_key;
215a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
216a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // Move to front of MRU list.
217a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ordering_.push_front(session);
218a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ordering_.erase(it->second);
219a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    it->second = ordering_.begin();
220a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
221a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return SSL_set_session(ssl, session) == 1;
222a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
223a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
224a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Flush all entries from the cache.
225a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void Flush() {
226a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::AutoLock lock(lock_);
227a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    id_index_.clear();
228a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    key_index_.clear();
229a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    while (!ordering_.empty()) {
230a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      SSL_SESSION* session = ordering_.front();
231a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ordering_.pop_front();
232a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      SSL_SESSION_free(session);
233a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
234a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
235a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
236a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) private:
237a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Type for list of SSL_SESSION handles, ordered in MRU order.
238a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  typedef std::list<SSL_SESSION*> MRUSessionList;
239a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Type for a dictionary from unique cache keys to session list nodes.
240a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  typedef base::hash_map<std::string, MRUSessionList::iterator> KeyIndex;
241a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Type for a dictionary from SessionId values to key index nodes.
242a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  typedef base::hash_map<SessionId, KeyIndex::iterator> SessionIdIndex;
243a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
244a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Return the key associated with a given session, or the empty string if
245a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // none exist. This shall only be used for debugging.
246a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  std::string SessionKey(SSL_SESSION* session) {
247a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (!session)
248a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return std::string("<null-session>");
249a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
250a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (session->session_id_length == 0)
251a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return std::string("<empty-session-id>");
252a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
253a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SessionIdIndex::iterator it = id_index_.find(SessionId(session));
254a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (it == id_index_.end())
255a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return std::string("<unknown-session>");
256a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
257a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return it->second->first;
258a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
259a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
260a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Remove a given |session| from the cache. Lock must be held.
261a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void RemoveSessionLocked(SSL_SESSION* session) {
262a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    lock_.AssertAcquired();
263a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK(session);
264a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_GT(session->session_id_length, 0U);
265a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SessionId session_id(session);
266a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SessionIdIndex::iterator id_it = id_index_.find(session_id);
267a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (id_it == id_index_.end()) {
268a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      LOG(ERROR) << "Trying to remove unknown session from cache: " << session;
269a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return;
270a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
271a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    KeyIndex::iterator key_it = id_it->second;
272a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK(key_it != key_index_.end());
273a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_EQ(session, *key_it->second);
274a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
275a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    id_index_.erase(session_id);
276a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    ordering_.erase(key_it->second);
277a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    key_index_.erase(key_it);
278a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
279a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL_SESSION_free(session);
280a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
281a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_EQ(key_index_.size(), id_index_.size());
282a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
283a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
284a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Used internally to flush expired sessions. Lock must be held.
285a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void FlushExpiredSessionsLocked() {
286a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    lock_.AssertAcquired();
287a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
288a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // Unfortunately, OpenSSL initializes |session->time| with a time()
289a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // timestamps, which makes mocking / unit testing difficult.
290a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    long timeout_secs = static_cast<long>(::time(NULL));
291a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    MRUSessionList::iterator it = ordering_.begin();
292a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    while (it != ordering_.end()) {
293a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      SSL_SESSION* session = *it++;
294a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
295a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // Important, use <= instead of < here to allow unit testing to
296a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // work properly. That's because unit tests that check the expiration
297a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // behaviour will use a session timeout of 0 seconds.
298a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (session->time + session->timeout <= timeout_secs) {
299a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        DVLOG(2) << "Expiring session " << session << " for "
300a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                 << SessionKey(session);
301a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        RemoveSessionLocked(session);
302a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      }
303a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
304a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
305a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
306a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Retrieve the cache associated with a given SSL context |ctx|.
307a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  static SSLSessionCacheOpenSSLImpl* GetCache(SSL_CTX* ctx) {
308a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK(ctx);
309a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    void* result = SSL_CTX_get_ex_data(ctx, GetSSLContextExIndex());
310a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK(result);
311a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return reinterpret_cast<SSLSessionCacheOpenSSLImpl*>(result);
312a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
313a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
314a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Called by OpenSSL when a new |session| was created and added to a given
315a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |ssl| connection. Note that the session's reference count was already
316a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // incremented before the function is entered. The function must return 1
317a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // to indicate that it took ownership of the session, i.e. that the caller
318a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // should not decrement its reference count after completion.
319a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  static int NewSessionCallbackStatic(SSL* ssl, SSL_SESSION* session) {
320a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    GetCache(ssl->ctx)->OnSessionAdded(ssl, session);
321a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return 1;
322a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
323a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
324a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Called by OpenSSL to indicate that a session must be removed from the
325a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // cache. This happens when SSL_CTX is destroyed.
326a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  static void RemoveSessionCallbackStatic(SSL_CTX* ctx, SSL_SESSION* session) {
327a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    GetCache(ctx)->OnSessionRemoved(session);
328a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
329a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
330a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Called by OpenSSL to generate a new session ID. This happens during a
331a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // SSL connection operation, when the SSL object doesn't have a session yet.
332a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  //
333a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // A session ID is a random string of bytes used to uniquely identify the
334a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // session between a client and a server.
335a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  //
336a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |ssl| is a SSL connection handle. Ignored here.
337a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |id| is the target buffer where the ID must be generated.
338a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // |*id_len| is, on input, the size of the desired ID. It will be 16 for
339a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // SSLv2, and 32 for anything else. OpenSSL allows an implementation
340a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // to change it on output, but this will not happen here.
341a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  //
342a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // The function must ensure the generated ID is really unique, i.e. that
343a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // another session in the cache doesn't already use the same value. It must
344a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // return 1 to indicate success, or 0 for failure.
345a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  static int GenerateSessionIdStatic(const SSL* ssl,
346a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                     unsigned char* id,
347a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                     unsigned* id_len) {
348a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (!GetCache(ssl->ctx)->OnGenerateSessionId(id, *id_len))
349a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      return 0;
350a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
351a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return 1;
352a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
353a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
354a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Add |session| to the cache in association with |cache_key|. If a session
355a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // already exists, it is replaced with the new one. This assumes that the
356a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // caller already incremented the session's reference count.
357a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnSessionAdded(SSL* ssl, SSL_SESSION* session) {
358a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::AutoLock locked(lock_);
359a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK(ssl);
360a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_GT(session->session_id_length, 0U);
361a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    std::string cache_key = config_.key_func(ssl);
362a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    KeyIndex::iterator it = key_index_.find(cache_key);
363a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (it == key_index_.end()) {
364a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DVLOG(2) << "Add session " << session << " for " << cache_key;
365a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // This is a new session. Add it to the cache.
366a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ordering_.push_front(session);
367a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      std::pair<KeyIndex::iterator, bool> ret =
368a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          key_index_.insert(std::make_pair(cache_key, ordering_.begin()));
369a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DCHECK(ret.second);
370a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      it = ret.first;
371a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DCHECK(it != key_index_.end());
372a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    } else {
373a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      // An existing session exists for this key, so replace it if needed.
374a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DVLOG(2) << "Replace session " << *it->second << " with " << session
375a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)               << " for " << cache_key;
376a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      SSL_SESSION* old_session = *it->second;
377a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (old_session != session) {
378a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        id_index_.erase(SessionId(old_session));
379a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        SSL_SESSION_free(old_session);
380a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      }
381a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ordering_.erase(it->second);
382a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ordering_.push_front(session);
383a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      it->second = ordering_.begin();
384a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
385a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
386a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    id_index_[SessionId(session)] = it;
387a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
388a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    if (key_index_.size() > config_.max_entries)
389a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ShrinkCacheLocked();
390a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
391a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_EQ(key_index_.size(), id_index_.size());
392a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_LE(key_index_.size(), config_.max_entries);
393a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
394a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
395a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Shrink the cache to ensure no more than config_.max_entries entries,
396a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // starting with older entries first. Lock must be acquired.
397a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void ShrinkCacheLocked() {
398a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    lock_.AssertAcquired();
399a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_EQ(key_index_.size(), ordering_.size());
400a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DCHECK_EQ(key_index_.size(), id_index_.size());
401a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
402a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    while (key_index_.size() > config_.max_entries) {
403a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      MRUSessionList::reverse_iterator it = ordering_.rbegin();
404a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DCHECK(it != ordering_.rend());
405a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
406a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      SSL_SESSION* session = *it;
407a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DCHECK(session);
408a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      DVLOG(2) << "Evicting session " << session << " for "
409a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)               << SessionKey(session);
410a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      RemoveSessionLocked(session);
411a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
412a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
413a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
414a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Remove |session| from the cache.
415a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnSessionRemoved(SSL_SESSION* session) {
416a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::AutoLock locked(lock_);
417a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DVLOG(2) << "Remove session " << session << " for " << SessionKey(session);
418a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    RemoveSessionLocked(session);
419a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
420a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
421a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // See GenerateSessionIdStatic for a description of what this function does.
422a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  bool OnGenerateSessionId(unsigned char* id, unsigned id_len) {
423a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    base::AutoLock locked(lock_);
424a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // This mimics def_generate_session_id() in openssl/ssl/ssl_sess.cc,
425a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // I.e. try to generate a pseudo-random bit string, and check that no
426a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    // other entry in the cache has the same value.
427a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const size_t kMaxTries = 10;
428a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    for (size_t tries = 0; tries < kMaxTries; ++tries) {
429a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (RAND_pseudo_bytes(id, id_len) <= 0) {
430a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        DLOG(ERROR) << "Couldn't generate " << id_len
431a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                    << " pseudo random bytes?";
432a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        return false;
433a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      }
434a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      if (id_index_.find(SessionId(id, id_len)) == id_index_.end())
435a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        return true;
436a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    }
437a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    DLOG(ERROR) << "Couldn't generate unique session ID of " << id_len
438a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                << "bytes after " << kMaxTries << " tries.";
439a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return false;
440a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
441a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
442a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SSL_CTX* ctx_;
443a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SSLSessionCacheOpenSSL::Config config_;
444a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
445a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // method to get the index which can later be used with SSL_CTX_get_ex_data()
446a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // or SSL_CTX_set_ex_data().
447a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::Lock lock_;  // Protects access to containers below.
448a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
449a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  MRUSessionList ordering_;
450a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  KeyIndex key_index_;
451a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  SessionIdIndex id_index_;
452a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
453a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  size_t expiration_check_;
454a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)};
455a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
456a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)SSLSessionCacheOpenSSL::~SSLSessionCacheOpenSSL() { delete impl_; }
457a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
458a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)size_t SSLSessionCacheOpenSSL::size() const { return impl_->size(); }
459a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
460a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void SSLSessionCacheOpenSSL::Reset(SSL_CTX* ctx, const Config& config) {
461a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (impl_)
462a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    delete impl_;
463a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
464a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  impl_ = new SSLSessionCacheOpenSSLImpl(ctx, config);
465a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
466a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
467a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool SSLSessionCacheOpenSSL::SetSSLSession(SSL* ssl) {
468a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return impl_->SetSSLSession(ssl);
469a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
470a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
471a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool SSLSessionCacheOpenSSL::SetSSLSessionWithKey(
472a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    SSL* ssl,
473a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    const std::string& cache_key) {
474a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return impl_->SetSSLSessionWithKey(ssl, cache_key);
475a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
476a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
477a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)void SSLSessionCacheOpenSSL::Flush() { impl_->Flush(); }
478a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
479a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}  // namespace net
480