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