Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/ssl/ssl_client_session_cache.h" | 5 #include "net/ssl/ssl_client_session_cache.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/memory_coordinator_client_registry.h" | 9 #include "base/memory/memory_coordinator_client_registry.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/time/clock.h" | 11 #include "base/time/clock.h" |
| 12 #include "base/time/default_clock.h" | 12 #include "base/time/default_clock.h" |
| 13 #include "base/trace_event/process_memory_dump.h" | 13 #include "base/trace_event/process_memory_dump.h" |
| 14 #include "net/cert/x509_util_openssl.h" | 14 #include "net/cert/x509_util_openssl.h" |
| 15 #include "third_party/boringssl/src/include/openssl/ssl.h" | 15 #include "third_party/boringssl/src/include/openssl/ssl.h" |
| 16 #include "third_party/boringssl/src/include/openssl/x509.h" | 16 #include "third_party/boringssl/src/include/openssl/x509.h" |
| 17 | 17 |
| 18 namespace net { | 18 namespace net { |
| 19 | 19 |
| 20 SSLClientSessionCache::Entry::Entry() : lookups(0) {} | |
| 21 SSLClientSessionCache::Entry::Entry(Entry&&) = default; | |
| 22 SSLClientSessionCache::Entry::~Entry() = default; | |
|
davidben
2017/01/19 20:29:34
Nit: Order these matching the header.
nharper
2017/01/19 21:50:35
They're both in the same order (c'tor, move c'tor,
| |
| 23 | |
| 20 SSLClientSessionCache::SSLClientSessionCache(const Config& config) | 24 SSLClientSessionCache::SSLClientSessionCache(const Config& config) |
| 21 : clock_(new base::DefaultClock), | 25 : clock_(new base::DefaultClock), |
| 22 config_(config), | 26 config_(config), |
| 23 cache_(config.max_entries), | 27 cache_(config.max_entries), |
| 24 lookups_since_flush_(0) { | 28 lookups_since_flush_(0) { |
| 25 memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( | 29 memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( |
| 26 &SSLClientSessionCache::OnMemoryPressure, base::Unretained(this)))); | 30 &SSLClientSessionCache::OnMemoryPressure, base::Unretained(this)))); |
| 27 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); | 31 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
| 28 } | 32 } |
| 29 | 33 |
| 30 SSLClientSessionCache::~SSLClientSessionCache() { | 34 SSLClientSessionCache::~SSLClientSessionCache() { |
| 31 Flush(); | 35 Flush(); |
| 32 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); | 36 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); |
| 33 } | 37 } |
| 34 | 38 |
| 35 size_t SSLClientSessionCache::size() const { | 39 size_t SSLClientSessionCache::size() const { |
| 36 return cache_.size(); | 40 return cache_.size(); |
| 37 } | 41 } |
| 38 | 42 |
| 39 bssl::UniquePtr<SSL_SESSION> SSLClientSessionCache::Lookup( | 43 bssl::UniquePtr<SSL_SESSION> SSLClientSessionCache::Lookup( |
| 40 const std::string& cache_key) { | 44 const std::string& cache_key, |
| 45 size_t* count) { | |
| 41 base::AutoLock lock(lock_); | 46 base::AutoLock lock(lock_); |
| 42 | 47 |
| 43 // Expire stale sessions. | 48 // Expire stale sessions. |
| 44 lookups_since_flush_++; | 49 lookups_since_flush_++; |
| 45 if (lookups_since_flush_ >= config_.expiration_check_count) { | 50 if (lookups_since_flush_ >= config_.expiration_check_count) { |
| 46 lookups_since_flush_ = 0; | 51 lookups_since_flush_ = 0; |
| 47 FlushExpiredSessions(); | 52 FlushExpiredSessions(); |
| 48 } | 53 } |
| 49 | 54 |
| 55 // Set count to 0 if there's no session in the cache. | |
| 56 if (count != nullptr) | |
| 57 *count = 0; | |
| 58 | |
| 50 auto iter = cache_.Get(cache_key); | 59 auto iter = cache_.Get(cache_key); |
| 51 if (iter == cache_.end()) | 60 if (iter == cache_.end()) |
| 52 return nullptr; | 61 return nullptr; |
| 53 | 62 |
| 54 SSL_SESSION* session = iter->second.get(); | 63 SSL_SESSION* session = iter->second.session.get(); |
| 55 if (IsExpired(session, clock_->Now().ToTimeT())) { | 64 if (IsExpired(session, clock_->Now().ToTimeT())) { |
| 56 cache_.Erase(iter); | 65 cache_.Erase(iter); |
| 57 return nullptr; | 66 return nullptr; |
| 58 } | 67 } |
| 59 | 68 |
| 69 iter->second.lookups++; | |
| 70 if (count != nullptr) { | |
| 71 *count = iter->second.lookups; | |
| 72 } | |
| 73 | |
| 60 SSL_SESSION_up_ref(session); | 74 SSL_SESSION_up_ref(session); |
| 61 return bssl::UniquePtr<SSL_SESSION>(session); | 75 return bssl::UniquePtr<SSL_SESSION>(session); |
| 62 } | 76 } |
| 63 | 77 |
| 78 void SSLClientSessionCache::ResetLookupCount(const std::string& cache_key, | |
| 79 SSL_SESSION* session) { | |
| 80 base::AutoLock lock(lock_); | |
| 81 | |
| 82 // It's possible that the cached session for this key was deleted after the | |
| 83 // Lookup, and also possible that it has been replaced by a new Entry. If | |
| 84 // that's the case, don't do anything. | |
| 85 auto iter = cache_.Get(cache_key); | |
| 86 if (iter == cache_.end() || iter->second.session.get() != session) | |
| 87 return; | |
| 88 | |
| 89 iter->second.lookups = 0; | |
| 90 } | |
| 91 | |
| 64 void SSLClientSessionCache::Insert(const std::string& cache_key, | 92 void SSLClientSessionCache::Insert(const std::string& cache_key, |
| 65 SSL_SESSION* session) { | 93 SSL_SESSION* session) { |
| 66 base::AutoLock lock(lock_); | 94 base::AutoLock lock(lock_); |
| 67 | 95 |
| 68 SSL_SESSION_up_ref(session); | 96 SSL_SESSION_up_ref(session); |
| 69 cache_.Put(cache_key, bssl::UniquePtr<SSL_SESSION>(session)); | 97 Entry entry; |
| 98 entry.session = bssl::UniquePtr<SSL_SESSION>(session); | |
| 99 cache_.Put(cache_key, std::move(entry)); | |
| 70 } | 100 } |
| 71 | 101 |
| 72 void SSLClientSessionCache::Flush() { | 102 void SSLClientSessionCache::Flush() { |
| 73 base::AutoLock lock(lock_); | 103 base::AutoLock lock(lock_); |
| 74 | 104 |
| 75 cache_.Clear(); | 105 cache_.Clear(); |
| 76 } | 106 } |
| 77 | 107 |
| 78 void SSLClientSessionCache::SetClockForTesting( | 108 void SSLClientSessionCache::SetClockForTesting( |
| 79 std::unique_ptr<base::Clock> clock) { | 109 std::unique_ptr<base::Clock> clock) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 94 // This method can be reached from different URLRequestContexts. Since this is | 124 // This method can be reached from different URLRequestContexts. Since this is |
| 95 // a singleton, only log memory stats once. | 125 // a singleton, only log memory stats once. |
| 96 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. | 126 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. |
| 97 if (cache_dump) | 127 if (cache_dump) |
| 98 return; | 128 return; |
| 99 cache_dump = pmd->CreateAllocatorDump(absolute_name); | 129 cache_dump = pmd->CreateAllocatorDump(absolute_name); |
| 100 base::AutoLock lock(lock_); | 130 base::AutoLock lock(lock_); |
| 101 int total_serialized_cert_size = 0; | 131 int total_serialized_cert_size = 0; |
| 102 int total_cert_count = 0; | 132 int total_cert_count = 0; |
| 103 for (const auto& pair : cache_) { | 133 for (const auto& pair : cache_) { |
| 104 auto entry = pair.second.get(); | 134 auto entry = pair.second.session.get(); |
| 105 auto cert_chain = entry->x509_chain; | 135 auto cert_chain = entry->x509_chain; |
| 106 size_t cert_count = sk_X509_num(cert_chain); | 136 size_t cert_count = sk_X509_num(cert_chain); |
| 107 total_cert_count += cert_count; | 137 total_cert_count += cert_count; |
| 108 for (size_t i = 0; i < cert_count; ++i) { | 138 for (size_t i = 0; i < cert_count; ++i) { |
| 109 X509* cert = sk_X509_value(cert_chain, i); | 139 X509* cert = sk_X509_value(cert_chain, i); |
| 110 total_serialized_cert_size += i2d_X509(cert, nullptr); | 140 total_serialized_cert_size += i2d_X509(cert, nullptr); |
| 111 } | 141 } |
| 112 } | 142 } |
| 113 // This measures the lower bound of the serialized certificate. It doesn't | 143 // This measures the lower bound of the serialized certificate. It doesn't |
| 114 // measure the actual memory used, which is 4x this amount (see | 144 // measure the actual memory used, which is 4x this amount (see |
| 115 // crbug.com/671420 for more details). | 145 // crbug.com/671420 for more details). |
| 116 cache_dump->AddScalar("serialized_cert_size", | 146 cache_dump->AddScalar("serialized_cert_size", |
| 117 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 147 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 118 total_serialized_cert_size); | 148 total_serialized_cert_size); |
| 119 cache_dump->AddScalar("cert_count", | 149 cache_dump->AddScalar("cert_count", |
| 120 base::trace_event::MemoryAllocatorDump::kUnitsObjects, | 150 base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 121 total_cert_count); | 151 total_cert_count); |
| 122 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 152 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 123 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 153 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 124 total_serialized_cert_size); | 154 total_serialized_cert_size); |
| 125 } | 155 } |
| 126 | 156 |
| 127 void SSLClientSessionCache::FlushExpiredSessions() { | 157 void SSLClientSessionCache::FlushExpiredSessions() { |
| 128 time_t now = clock_->Now().ToTimeT(); | 158 time_t now = clock_->Now().ToTimeT(); |
| 129 auto iter = cache_.begin(); | 159 auto iter = cache_.begin(); |
| 130 while (iter != cache_.end()) { | 160 while (iter != cache_.end()) { |
| 131 if (IsExpired(iter->second.get(), now)) { | 161 if (IsExpired(iter->second.session.get(), now)) { |
| 132 iter = cache_.Erase(iter); | 162 iter = cache_.Erase(iter); |
| 133 } else { | 163 } else { |
| 134 ++iter; | 164 ++iter; |
| 135 } | 165 } |
| 136 } | 166 } |
| 137 } | 167 } |
| 138 | 168 |
| 139 void SSLClientSessionCache::OnMemoryPressure( | 169 void SSLClientSessionCache::OnMemoryPressure( |
| 140 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 170 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 141 switch (memory_pressure_level) { | 171 switch (memory_pressure_level) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 162 break; | 192 break; |
| 163 case base::MemoryState::SUSPENDED: | 193 case base::MemoryState::SUSPENDED: |
| 164 // Note: Not supported at present. Fall through. | 194 // Note: Not supported at present. Fall through. |
| 165 case base::MemoryState::UNKNOWN: | 195 case base::MemoryState::UNKNOWN: |
| 166 NOTREACHED(); | 196 NOTREACHED(); |
| 167 break; | 197 break; |
| 168 } | 198 } |
| 169 } | 199 } |
| 170 | 200 |
| 171 } // namespace net | 201 } // namespace net |
| OLD | NEW |