OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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/ssl/ssl_client_session_cache_openssl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/time/clock.h" | |
11 #include "base/time/default_clock.h" | |
12 #include "net/ssl/scoped_openssl_types.h" | |
13 | |
14 namespace net { | |
15 | |
16 struct SSLClientSessionCacheOpenSSL::CacheEntry { | |
17 ScopedSSL_SESSION session; | |
18 // The time at which this entry was created. | |
19 base::Time creation_time; | |
20 }; | |
21 | |
22 SSLClientSessionCacheOpenSSL::SSLClientSessionCacheOpenSSL(const Config& config) | |
23 : clock_(new base::DefaultClock), | |
24 config_(config), | |
25 cache_(config.max_entries), | |
26 lookups_since_flush_(0) { | |
27 } | |
28 | |
29 SSLClientSessionCacheOpenSSL::~SSLClientSessionCacheOpenSSL() { | |
30 // TODO(davidben): The session cache is currently a singleton, so it is | |
31 // destroyed on a different thread than the one it's created on. When | |
32 // https://crbug.com/458365 is fixed, this will no longer be an issue. | |
33 thread_checker_.DetachFromThread(); | |
34 | |
35 Flush(); | |
36 } | |
37 | |
38 size_t SSLClientSessionCacheOpenSSL::size() const { | |
39 return cache_.size(); | |
40 } | |
41 | |
42 SSL_SESSION* SSLClientSessionCacheOpenSSL::Lookup( | |
43 const std::string& cache_key) { | |
44 DCHECK(thread_checker_.CalledOnValidThread()); | |
45 | |
46 // Expire stale sessions. | |
47 lookups_since_flush_++; | |
48 if (lookups_since_flush_ >= config_.expiration_check_count) { | |
49 lookups_since_flush_ = 0; | |
50 FlushExpiredSessions(); | |
51 } | |
52 | |
53 CacheEntryMap::iterator iter = cache_.Get(cache_key); | |
54 if (iter == cache_.end()) | |
55 return nullptr; | |
56 if (IsExpired(iter->second, clock_->Now())) { | |
57 cache_.Erase(iter); | |
58 return nullptr; | |
59 } | |
60 return iter->second->session.get(); | |
61 } | |
62 | |
63 void SSLClientSessionCacheOpenSSL::Insert(const std::string& cache_key, | |
64 SSL_SESSION* session) { | |
65 DCHECK(thread_checker_.CalledOnValidThread()); | |
66 | |
67 // Make a new entry. | |
68 CacheEntry* entry = new CacheEntry; | |
69 entry->session.reset(SSL_SESSION_up_ref(session)); | |
70 entry->creation_time = clock_->Now(); | |
71 | |
72 // Takes ownership. | |
73 cache_.Put(cache_key, entry); | |
74 } | |
75 | |
76 void SSLClientSessionCacheOpenSSL::Flush() { | |
77 DCHECK(thread_checker_.CalledOnValidThread()); | |
78 | |
79 cache_.Clear(); | |
80 } | |
81 | |
82 void SSLClientSessionCacheOpenSSL::SetClockForTesting( | |
83 scoped_ptr<base::Clock> clock) { | |
84 DCHECK(thread_checker_.CalledOnValidThread()); | |
85 | |
86 clock_ = clock.Pass(); | |
87 } | |
88 | |
89 bool SSLClientSessionCacheOpenSSL::IsExpired( | |
90 SSLClientSessionCacheOpenSSL::CacheEntry* entry, | |
91 base::Time now) { | |
Ryan Sleevi
2015/04/02 20:00:14
const-ref
davidben
2015/04/03 00:37:11
Done.
| |
92 return now < entry->creation_time || | |
93 entry->creation_time + config_.timeout < now; | |
94 } | |
95 | |
96 void SSLClientSessionCacheOpenSSL::FlushExpiredSessions() { | |
97 base::Time now = clock_->Now(); | |
98 CacheEntryMap::iterator iter = cache_.begin(); | |
99 while (iter != cache_.end()) { | |
100 if (IsExpired(iter->second, now)) { | |
101 iter = cache_.Erase(iter); | |
102 } else { | |
103 ++iter; | |
104 } | |
105 } | |
106 } | |
107 | |
108 } // namespace net | |
OLD | NEW |