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 21:56:12
Oh, I meant order relative to the other methods. I
nharper
2017/01/19 22:09:07
I moved them so the ordering in the .cc is closer
| |
| 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 base::AutoLock lock(lock_); | |
| 80 | |
| 81 // It's possible that the cached session for this key was deleted after the | |
| 82 // Lookup. If that's the case, don't do anything. | |
| 83 auto iter = cache_.Get(cache_key); | |
| 84 if (iter == cache_.end()) | |
| 85 return; | |
| 86 | |
| 87 iter->second.lookups = 0; | |
| 88 } | |
| 89 | |
| 64 void SSLClientSessionCache::Insert(const std::string& cache_key, | 90 void SSLClientSessionCache::Insert(const std::string& cache_key, |
| 65 SSL_SESSION* session) { | 91 SSL_SESSION* session) { |
| 66 base::AutoLock lock(lock_); | 92 base::AutoLock lock(lock_); |
| 67 | 93 |
| 68 SSL_SESSION_up_ref(session); | 94 SSL_SESSION_up_ref(session); |
| 69 cache_.Put(cache_key, bssl::UniquePtr<SSL_SESSION>(session)); | 95 Entry entry; |
| 96 entry.session = bssl::UniquePtr<SSL_SESSION>(session); | |
| 97 cache_.Put(cache_key, std::move(entry)); | |
| 70 } | 98 } |
| 71 | 99 |
| 72 void SSLClientSessionCache::Flush() { | 100 void SSLClientSessionCache::Flush() { |
| 73 base::AutoLock lock(lock_); | 101 base::AutoLock lock(lock_); |
| 74 | 102 |
| 75 cache_.Clear(); | 103 cache_.Clear(); |
| 76 } | 104 } |
| 77 | 105 |
| 78 void SSLClientSessionCache::SetClockForTesting( | 106 void SSLClientSessionCache::SetClockForTesting( |
| 79 std::unique_ptr<base::Clock> clock) { | 107 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 | 122 // This method can be reached from different URLRequestContexts. Since this is |
| 95 // a singleton, only log memory stats once. | 123 // a singleton, only log memory stats once. |
| 96 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. | 124 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. |
| 97 if (cache_dump) | 125 if (cache_dump) |
| 98 return; | 126 return; |
| 99 cache_dump = pmd->CreateAllocatorDump(absolute_name); | 127 cache_dump = pmd->CreateAllocatorDump(absolute_name); |
| 100 base::AutoLock lock(lock_); | 128 base::AutoLock lock(lock_); |
| 101 size_t total_serialized_cert_size = 0; | 129 size_t total_serialized_cert_size = 0; |
| 102 size_t total_cert_count = 0; | 130 size_t total_cert_count = 0; |
| 103 for (const auto& pair : cache_) { | 131 for (const auto& pair : cache_) { |
| 104 const SSL_SESSION* session = pair.second.get(); | 132 const SSL_SESSION* session = pair.second.session.get(); |
| 105 size_t cert_count = sk_CRYPTO_BUFFER_num(session->certs); | 133 size_t cert_count = sk_CRYPTO_BUFFER_num(session->certs); |
| 106 total_cert_count += cert_count; | 134 total_cert_count += cert_count; |
| 107 for (size_t i = 0; i < cert_count; ++i) { | 135 for (size_t i = 0; i < cert_count; ++i) { |
| 108 const CRYPTO_BUFFER* cert = sk_CRYPTO_BUFFER_value(session->certs, i); | 136 const CRYPTO_BUFFER* cert = sk_CRYPTO_BUFFER_value(session->certs, i); |
| 109 total_serialized_cert_size += CRYPTO_BUFFER_len(cert); | 137 total_serialized_cert_size += CRYPTO_BUFFER_len(cert); |
| 110 } | 138 } |
| 111 } | 139 } |
| 112 // This measures the lower bound of the serialized certificate. It doesn't | 140 // This measures the lower bound of the serialized certificate. It doesn't |
| 113 // measure the actual memory used, which is 4x this amount (see | 141 // measure the actual memory used, which is 4x this amount (see |
| 114 // crbug.com/671420 for more details). | 142 // crbug.com/671420 for more details). |
| 115 cache_dump->AddScalar("serialized_cert_size", | 143 cache_dump->AddScalar("serialized_cert_size", |
| 116 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 144 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 117 total_serialized_cert_size); | 145 total_serialized_cert_size); |
| 118 cache_dump->AddScalar("cert_count", | 146 cache_dump->AddScalar("cert_count", |
| 119 base::trace_event::MemoryAllocatorDump::kUnitsObjects, | 147 base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 120 total_cert_count); | 148 total_cert_count); |
| 121 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 149 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 122 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 150 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 123 total_serialized_cert_size); | 151 total_serialized_cert_size); |
| 124 } | 152 } |
| 125 | 153 |
| 126 void SSLClientSessionCache::FlushExpiredSessions() { | 154 void SSLClientSessionCache::FlushExpiredSessions() { |
| 127 time_t now = clock_->Now().ToTimeT(); | 155 time_t now = clock_->Now().ToTimeT(); |
| 128 auto iter = cache_.begin(); | 156 auto iter = cache_.begin(); |
| 129 while (iter != cache_.end()) { | 157 while (iter != cache_.end()) { |
| 130 if (IsExpired(iter->second.get(), now)) { | 158 if (IsExpired(iter->second.session.get(), now)) { |
| 131 iter = cache_.Erase(iter); | 159 iter = cache_.Erase(iter); |
| 132 } else { | 160 } else { |
| 133 ++iter; | 161 ++iter; |
| 134 } | 162 } |
| 135 } | 163 } |
| 136 } | 164 } |
| 137 | 165 |
| 138 void SSLClientSessionCache::OnMemoryPressure( | 166 void SSLClientSessionCache::OnMemoryPressure( |
| 139 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 167 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 140 switch (memory_pressure_level) { | 168 switch (memory_pressure_level) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 161 break; | 189 break; |
| 162 case base::MemoryState::SUSPENDED: | 190 case base::MemoryState::SUSPENDED: |
| 163 // Note: Not supported at present. Fall through. | 191 // Note: Not supported at present. Fall through. |
| 164 case base::MemoryState::UNKNOWN: | 192 case base::MemoryState::UNKNOWN: |
| 165 NOTREACHED(); | 193 NOTREACHED(); |
| 166 break; | 194 break; |
| 167 } | 195 } |
| 168 } | 196 } |
| 169 | 197 |
| 170 } // namespace net | 198 } // namespace net |
| OLD | NEW |