Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: net/socket/ssl_session_cache_openssl_unittest.cc

Issue 89623002: net: Implement new SSL session cache for OpenSSL sockets. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Optimize insertion into KeyIndex for new sessions. Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/socket/ssl_session_cache_openssl.h"
6
7 #include <openssl/ssl.h>
8
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "crypto/openssl_util.h"
13
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 // This is an internal OpenSSL function that can be used to create a new
17 // session for an existing SSL object. This shall force a call to the
18 // 'generate_session_id' callback from the SSL's session context.
19 // |s| is the target SSL connection handle.
20 // |session| is non-0 to ask for the creation of a new session. If 0,
21 // this will set an empty session with no ID instead.
22 extern "C" int ssl_get_new_session(SSL* s, int session);
23
24 // This is an internal OpenSSL function which is used internally to add
25 // a new session to the cache. It is normally triggered by a succesful
26 // connection. However, this unit test does not use the network at all.
27 extern "C" void ssl_update_cache(SSL* s, int mode);
28
29 namespace net {
30
31 namespace {
32
33 typedef crypto::ScopedOpenSSL<SSL, SSL_free> ScopedSSL;
34
35 // Helper class used to associate liberal std::string keys with SSL objects.
agl 2013/11/27 21:58:33 liberal?
digit1 2013/11/28 10:37:20 I've changed it to 'arbitrary' which is the meanin
36 class SSLKeyHelper {
37 public:
38 // Return the string associated with a given SSL handle |ssl|, or the
39 // empty string if none exists.
40 static std::string Get(const SSL* ssl) {
41 return GetInstance()->GetValue(ssl);
42 }
43
44 // Associate a string with a given SSL handle |ssl|.
45 static void Set(SSL* ssl, const std::string& value) {
46 GetInstance()->SetValue(ssl, value);
47 }
48
49 static SSLKeyHelper* GetInstance() {
50 static base::LazyInstance<SSLKeyHelper>::Leaky s_instance =
agl 2013/11/27 21:58:33 I'll take your word that this compiles into nothin
digit1 2013/11/28 10:37:20 I confirm it does (I disassembled the result to ma
51 LAZY_INSTANCE_INITIALIZER;
52 return s_instance.Pointer();
53 }
54
55 SSLKeyHelper() {
56 ex_index_ = SSL_get_ex_new_index(0, NULL, NULL, KeyDup, KeyFree);
57 CHECK_NE(-1, ex_index_);
58 }
59
60 std::string GetValue(const SSL* ssl) {
61 std::string* value =
62 reinterpret_cast<std::string*>(SSL_get_ex_data(ssl, ex_index_));
63 if (!value)
64 return std::string();
65 return *value;
66 }
67
68 void SetValue(SSL* ssl, const std::string& value) {
69 int ret = SSL_set_ex_data(ssl, ex_index_, new std::string(value));
70 CHECK_EQ(1, ret);
71 }
72
73 // Called when a SSL object is copied through SSL_dup(). This needs to copy
agl 2013/11/27 21:58:33 s/a/an/ I think.
digit1 2013/11/28 10:37:20 Done.
74 // the value as well.
75 static int KeyDup(CRYPTO_EX_DATA* to,
76 CRYPTO_EX_DATA* from,
77 void* from_fd,
78 int idx,
79 long argl,
80 void* argp) {
81 // |from_fd| is really the address of a temporary pointer. On input, it
82 // points to the value from the original SSL object. The function must
83 // update it to the address of a copy.
84 std::string** ptr = reinterpret_cast<std::string**>(from_fd);
85 std::string* old_string = *ptr;
86 std::string* new_string = new std::string(*old_string);
87 *ptr = new_string;
88 return 0; // Ignored by the implementation.
89 }
90
91 // Called to destroy the value associated with a SSL object.
agl 2013/11/27 21:58:33 s/a/an/
digit1 2013/11/28 10:37:20 Done.
92 static void KeyFree(void* parent,
93 void* ptr,
94 CRYPTO_EX_DATA* ad,
95 int index,
96 long argl,
97 void* argp) {
98 std::string* value = reinterpret_cast<std::string*>(ptr);
99 delete value;
100 }
101
102 int ex_index_;
103 };
104
105 } // namespace
106
107 class SSLSessionCacheOpenSSLTest : public testing::Test {
108 public:
109 SSLSessionCacheOpenSSLTest() {
110 crypto::EnsureOpenSSLInit();
111 ctx_.reset(SSL_CTX_new(SSLv23_client_method()));
112 cache_.Reset(ctx_.get(), kDefaultConfig);
113 }
114
115 // Reset cache configuration.
116 void ResetConfig(const SSLSessionCacheOpenSSL::Config& config) {
117 cache_.Reset(ctx_.get(), config);
118 }
119
120 // Helper function to create a new SSL connection object associated with
121 // a given unique |cache_key|. This does _not_ add the session to the cache.
122 // Caller must free the object with SSL_free().
123 SSL* NewSSL(const std::string& cache_key) {
124 SSL* ssl = SSL_new(ctx_.get());
125 if (!ssl)
126 return NULL;
127
128 SSLKeyHelper::Set(ssl, cache_key); // associate cache key.
129 ResetSessionID(ssl); // create new unique session ID.
130 return ssl;
131 }
132
133 // Reset the session ID of a given SSL object. This creates a new session
134 // with a new unique random ID. Does not add it to the cache.
135 static void ResetSessionID(SSL* ssl) { ssl_get_new_session(ssl, 1); }
136
137 // Add a given SSL object and its session to the cache.
138 void AddToCache(SSL* ssl) {
139 ssl_update_cache(ssl, ctx_.get()->session_cache_mode);
140 }
141
142 static const SSLSessionCacheOpenSSL::Config kDefaultConfig;
143
144 protected:
145 crypto::ScopedOpenSSL<SSL_CTX, SSL_CTX_free> ctx_;
146 // |cache_| must be destroyed before |ctx_| and thus appears after it.
147 SSLSessionCacheOpenSSL cache_;
148 };
149
150 // static
151 const SSLSessionCacheOpenSSL::Config
152 SSLSessionCacheOpenSSLTest::kDefaultConfig = {
153 &SSLKeyHelper::Get, // key_func
154 1024, // max_entries
155 256, // expiration_check_count
156 60 * 60, // timeout_seconds
157 };
158
159 TEST_F(SSLSessionCacheOpenSSLTest, EmptyCacheCreation) {
160 EXPECT_EQ(0U, cache_.size());
161 }
162
163 TEST_F(SSLSessionCacheOpenSSLTest, CacheOneSession) {
164 ScopedSSL ssl(NewSSL("hello"));
165
166 EXPECT_EQ(0U, cache_.size());
167 AddToCache(ssl.get());
168 EXPECT_EQ(1U, cache_.size());
169 ssl.reset(NULL);
170 EXPECT_EQ(1U, cache_.size());
171 }
172
173 TEST_F(SSLSessionCacheOpenSSLTest, CacheMultipleSessions) {
174 const size_t kNumItems = 100;
175 int local_id = 1;
176
177 // Add kNumItems to the cache.
178 for (size_t n = 0; n < kNumItems; ++n) {
179 std::string local_id_string = base::StringPrintf("%d", local_id++);
180 ScopedSSL ssl(NewSSL(local_id_string));
181 AddToCache(ssl.get());
182 EXPECT_EQ(n + 1, cache_.size());
183 }
184 }
185
186 TEST_F(SSLSessionCacheOpenSSLTest, Flush) {
187 const size_t kNumItems = 100;
188 int local_id = 1;
189
190 // Add kNumItems to the cache.
191 for (size_t n = 0; n < kNumItems; ++n) {
192 std::string local_id_string = base::StringPrintf("%d", local_id++);
193 ScopedSSL ssl(NewSSL(local_id_string));
194 AddToCache(ssl.get());
195 }
196 EXPECT_EQ(kNumItems, cache_.size());
197
198 cache_.Flush();
199 EXPECT_EQ(0U, cache_.size());
200 }
201
202 TEST_F(SSLSessionCacheOpenSSLTest, SetSSLSession) {
203 const std::string key("hello");
204 ScopedSSL ssl(NewSSL(key));
205
206 // First call should fail because the session is not in the cache.
207 EXPECT_FALSE(cache_.SetSSLSession(ssl.get()));
208 SSL_SESSION* session = ssl.get()->session;
209 EXPECT_TRUE(session);
210 EXPECT_EQ(1, session->references);
211
212 AddToCache(ssl.get());
213 EXPECT_EQ(2, session->references);
214
215 ssl.reset(NULL);
216 EXPECT_EQ(1, session->references);
217
218 // Second call should find the session ID and associate it with |ssl2|.
219 ScopedSSL ssl2(NewSSL(key));
220 EXPECT_TRUE(cache_.SetSSLSession(ssl2.get()));
221
222 EXPECT_EQ(session, ssl2.get()->session);
223 EXPECT_EQ(2, session->references);
224 }
225
226 TEST_F(SSLSessionCacheOpenSSLTest, SetSSLSessionWithKey) {
227 const std::string key("hello");
228 ScopedSSL ssl(NewSSL(key));
229 AddToCache(ssl.get());
230 ssl.reset(NULL);
231
232 ScopedSSL ssl2(NewSSL(key));
233 EXPECT_TRUE(cache_.SetSSLSessionWithKey(ssl2.get(), key));
234 }
235
236 TEST_F(SSLSessionCacheOpenSSLTest, CheckSessionReplacement) {
237 // Check that if two SSL connections have the same key, only one
238 // corresponding session can be stored in the cache.
239 const std::string common_key("common-key");
240 ScopedSSL ssl1(NewSSL(common_key));
241 ScopedSSL ssl2(NewSSL(common_key));
242
243 AddToCache(ssl1.get());
244 EXPECT_EQ(1U, cache_.size());
245 EXPECT_EQ(2, ssl1.get()->session->references);
246
247 // This ends up calling OnSessionAdded which will discover that there is
248 // already one session ID associated with the key, and will replace it.
249 AddToCache(ssl2.get());
250 EXPECT_EQ(1U, cache_.size());
251 EXPECT_EQ(1, ssl1.get()->session->references);
252 EXPECT_EQ(2, ssl2.get()->session->references);
253 }
254
255 TEST_F(SSLSessionCacheOpenSSLTest, CheckEviction) {
256 const size_t kMaxItems = 20;
257 int local_id = 1;
258
259 SSLSessionCacheOpenSSL::Config config = kDefaultConfig;
260 config.max_entries = kMaxItems;
261 ResetConfig(config);
262
263 // Add kMaxItems to the cache.
264 for (size_t n = 0; n < kMaxItems; ++n) {
265 std::string local_id_string = base::StringPrintf("%d", local_id++);
266 ScopedSSL ssl(NewSSL(local_id_string));
267
268 AddToCache(ssl.get());
269 EXPECT_EQ(n + 1, cache_.size());
270 }
271
272 // Continue adding new items to the cache, check that old ones are
273 // evicted.
274 for (size_t n = 0; n < kMaxItems; ++n) {
275 std::string local_id_string = base::StringPrintf("%d", local_id++);
276 ScopedSSL ssl(NewSSL(local_id_string));
277
278 AddToCache(ssl.get());
279 EXPECT_EQ(kMaxItems, cache_.size());
280 }
281 }
282
283 // Check that session expiration works properly.
284 TEST_F(SSLSessionCacheOpenSSLTest, CheckExpiration) {
285 const size_t kMaxCheckCount = 10;
286 const size_t kNumEntries = 20;
287
288 SSLSessionCacheOpenSSL::Config config = kDefaultConfig;
289 config.expiration_check_count = kMaxCheckCount;
290 config.timeout_seconds = 1000;
291 ResetConfig(config);
292
293 // Add |kNumItems - 1| session entries with crafted time values.
294 for (size_t n = 0; n < kNumEntries - 1U; ++n) {
295 std::string key = base::StringPrintf("%d", static_cast<int>(n));
296 ScopedSSL ssl(NewSSL(key));
297 // Cheat a little: Force the session |time| value, this guarantees that they
298 // are expired, given that ::time() will always return a value that is
299 // past the first 100 seconds after the Unix epoch.
300 ssl.get()->session->time = static_cast<long>(n);
301 AddToCache(ssl.get());
302 }
303 EXPECT_EQ(kNumEntries - 1U, cache_.size());
304
305 // Add nother session which will get the current time, and thus not be
306 // expirable until 1000 seconds have passed.
307 ScopedSSL good_ssl(NewSSL("good-key"));
308 AddToCache(good_ssl.get());
309 good_ssl.reset(NULL);
310 EXPECT_EQ(kNumEntries, cache_.size());
311
312 // Call SetSSLSession() |kMaxCheckCount - 1| times, this shall not expire
313 // any session
314 for (size_t n = 0; n < kMaxCheckCount - 1U; ++n) {
315 ScopedSSL ssl(NewSSL("unknown-key"));
316 cache_.SetSSLSession(ssl.get());
317 EXPECT_EQ(kNumEntries, cache_.size());
318 }
319
320 // Call SetSSLSession another time, this shall expire all sessions except
321 // the last one.
322 ScopedSSL bad_ssl(NewSSL("unknown-key"));
323 cache_.SetSSLSession(bad_ssl.get());
324 bad_ssl.reset(NULL);
325 EXPECT_EQ(1U, cache_.size());
326 }
327
328 } // namespace net
OLDNEW
« net/socket/ssl_session_cache_openssl.cc ('K') | « net/socket/ssl_session_cache_openssl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698