| 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/metrics/histogram_macros.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
| 12 #include "base/time/default_clock.h" | 13 #include "base/time/default_clock.h" |
| 13 #include "base/trace_event/process_memory_dump.h" | 14 #include "base/trace_event/process_memory_dump.h" |
| 14 #include "net/cert/x509_util_openssl.h" | 15 #include "net/cert/x509_util_openssl.h" |
| 15 #include "third_party/boringssl/src/include/openssl/ssl.h" | 16 #include "third_party/boringssl/src/include/openssl/ssl.h" |
| 16 #include "third_party/boringssl/src/include/openssl/x509.h" | 17 #include "third_party/boringssl/src/include/openssl/x509.h" |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 | 20 |
| 21 SSLClientSessionCache::Entry::Entry() |
| 22 : active_lookups(0), max_lookups(0), should_log(false) {} |
| 23 SSLClientSessionCache::Entry::Entry(Entry&&) = default; |
| 24 SSLClientSessionCache::Entry::~Entry() = default; |
| 25 |
| 20 SSLClientSessionCache::SSLClientSessionCache(const Config& config) | 26 SSLClientSessionCache::SSLClientSessionCache(const Config& config) |
| 21 : clock_(new base::DefaultClock), | 27 : clock_(new base::DefaultClock), |
| 22 config_(config), | 28 config_(config), |
| 23 cache_(config.max_entries), | 29 cache_(config.max_entries), |
| 24 lookups_since_flush_(0) { | 30 lookups_since_flush_(0) { |
| 25 memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( | 31 memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( |
| 26 &SSLClientSessionCache::OnMemoryPressure, base::Unretained(this)))); | 32 &SSLClientSessionCache::OnMemoryPressure, base::Unretained(this)))); |
| 27 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); | 33 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
| 28 } | 34 } |
| 29 | 35 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 lookups_since_flush_++; | 50 lookups_since_flush_++; |
| 45 if (lookups_since_flush_ >= config_.expiration_check_count) { | 51 if (lookups_since_flush_ >= config_.expiration_check_count) { |
| 46 lookups_since_flush_ = 0; | 52 lookups_since_flush_ = 0; |
| 47 FlushExpiredSessions(); | 53 FlushExpiredSessions(); |
| 48 } | 54 } |
| 49 | 55 |
| 50 auto iter = cache_.Get(cache_key); | 56 auto iter = cache_.Get(cache_key); |
| 51 if (iter == cache_.end()) | 57 if (iter == cache_.end()) |
| 52 return nullptr; | 58 return nullptr; |
| 53 | 59 |
| 54 SSL_SESSION* session = iter->second.get(); | 60 SSL_SESSION* session = iter->second.session.get(); |
| 55 if (IsExpired(session, clock_->Now().ToTimeT())) { | 61 if (IsExpired(session, clock_->Now().ToTimeT())) { |
| 56 cache_.Erase(iter); | 62 cache_.Erase(iter); |
| 57 return nullptr; | 63 return nullptr; |
| 58 } | 64 } |
| 59 | 65 |
| 66 iter->second.active_lookups++; |
| 67 if (iter->second.active_lookups > iter->second.max_lookups) |
| 68 iter->second.max_lookups = iter->second.active_lookups; |
| 69 |
| 60 SSL_SESSION_up_ref(session); | 70 SSL_SESSION_up_ref(session); |
| 61 return bssl::UniquePtr<SSL_SESSION>(session); | 71 return bssl::UniquePtr<SSL_SESSION>(session); |
| 62 } | 72 } |
| 63 | 73 |
| 74 void SSLClientSessionCache::DecrementLookupCount(const std::string& cache_key, |
| 75 bool should_log) { |
| 76 base::AutoLock lock(lock_); |
| 77 |
| 78 // It's possible that the cached session for this key was deleted after the |
| 79 // Lookup, and also possible that it has been replaced by a new Entry. If |
| 80 // that's the case, don't do anything. |
| 81 auto iter = cache_.Get(cache_key); |
| 82 if (iter == cache_.end() || iter->second.active_lookups == 0) |
| 83 return; |
| 84 |
| 85 iter->second.active_lookups--; |
| 86 if (should_log) |
| 87 iter->second.should_log = true; |
| 88 |
| 89 if (iter->second.active_lookups == 0) { |
| 90 if (iter->second.should_log) { |
| 91 UMA_HISTOGRAM_EXACT_LINEAR("Net.SSLSessionConcurrentLookupCount", |
| 92 iter->second.max_lookups, 20); |
| 93 } |
| 94 iter->second.max_lookups = 0; |
| 95 iter->second.should_log = false; |
| 96 } |
| 97 } |
| 98 |
| 64 void SSLClientSessionCache::Insert(const std::string& cache_key, | 99 void SSLClientSessionCache::Insert(const std::string& cache_key, |
| 65 SSL_SESSION* session) { | 100 SSL_SESSION* session) { |
| 66 base::AutoLock lock(lock_); | 101 base::AutoLock lock(lock_); |
| 67 | 102 |
| 68 SSL_SESSION_up_ref(session); | 103 SSL_SESSION_up_ref(session); |
| 69 cache_.Put(cache_key, bssl::UniquePtr<SSL_SESSION>(session)); | 104 Entry entry; |
| 105 entry.session = bssl::UniquePtr<SSL_SESSION>(session); |
| 106 cache_.Put(cache_key, std::move(entry)); |
| 70 } | 107 } |
| 71 | 108 |
| 72 void SSLClientSessionCache::Flush() { | 109 void SSLClientSessionCache::Flush() { |
| 73 base::AutoLock lock(lock_); | 110 base::AutoLock lock(lock_); |
| 74 | 111 |
| 75 cache_.Clear(); | 112 cache_.Clear(); |
| 76 } | 113 } |
| 77 | 114 |
| 78 void SSLClientSessionCache::SetClockForTesting( | 115 void SSLClientSessionCache::SetClockForTesting( |
| 79 std::unique_ptr<base::Clock> clock) { | 116 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 | 131 // This method can be reached from different URLRequestContexts. Since this is |
| 95 // a singleton, only log memory stats once. | 132 // a singleton, only log memory stats once. |
| 96 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. | 133 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. |
| 97 if (cache_dump) | 134 if (cache_dump) |
| 98 return; | 135 return; |
| 99 cache_dump = pmd->CreateAllocatorDump(absolute_name); | 136 cache_dump = pmd->CreateAllocatorDump(absolute_name); |
| 100 base::AutoLock lock(lock_); | 137 base::AutoLock lock(lock_); |
| 101 int total_serialized_cert_size = 0; | 138 int total_serialized_cert_size = 0; |
| 102 int total_cert_count = 0; | 139 int total_cert_count = 0; |
| 103 for (const auto& pair : cache_) { | 140 for (const auto& pair : cache_) { |
| 104 auto entry = pair.second.get(); | 141 auto entry = pair.second.session.get(); |
| 105 auto cert_chain = entry->x509_chain; | 142 auto cert_chain = entry->x509_chain; |
| 106 size_t cert_count = sk_X509_num(cert_chain); | 143 size_t cert_count = sk_X509_num(cert_chain); |
| 107 total_cert_count += cert_count; | 144 total_cert_count += cert_count; |
| 108 for (size_t i = 0; i < cert_count; ++i) { | 145 for (size_t i = 0; i < cert_count; ++i) { |
| 109 X509* cert = sk_X509_value(cert_chain, i); | 146 X509* cert = sk_X509_value(cert_chain, i); |
| 110 total_serialized_cert_size += i2d_X509(cert, nullptr); | 147 total_serialized_cert_size += i2d_X509(cert, nullptr); |
| 111 } | 148 } |
| 112 } | 149 } |
| 113 // This measures the lower bound of the serialized certificate. It doesn't | 150 // This measures the lower bound of the serialized certificate. It doesn't |
| 114 // measure the actual memory used, which is 4x this amount (see | 151 // measure the actual memory used, which is 4x this amount (see |
| 115 // crbug.com/671420 for more details). | 152 // crbug.com/671420 for more details). |
| 116 cache_dump->AddScalar("serialized_cert_size", | 153 cache_dump->AddScalar("serialized_cert_size", |
| 117 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 154 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 118 total_serialized_cert_size); | 155 total_serialized_cert_size); |
| 119 cache_dump->AddScalar("cert_count", | 156 cache_dump->AddScalar("cert_count", |
| 120 base::trace_event::MemoryAllocatorDump::kUnitsObjects, | 157 base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 121 total_cert_count); | 158 total_cert_count); |
| 122 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 159 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 123 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 160 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 124 total_serialized_cert_size); | 161 total_serialized_cert_size); |
| 125 } | 162 } |
| 126 | 163 |
| 127 void SSLClientSessionCache::FlushExpiredSessions() { | 164 void SSLClientSessionCache::FlushExpiredSessions() { |
| 128 time_t now = clock_->Now().ToTimeT(); | 165 time_t now = clock_->Now().ToTimeT(); |
| 129 auto iter = cache_.begin(); | 166 auto iter = cache_.begin(); |
| 130 while (iter != cache_.end()) { | 167 while (iter != cache_.end()) { |
| 131 if (IsExpired(iter->second.get(), now)) { | 168 if (IsExpired(iter->second.session.get(), now)) { |
| 132 iter = cache_.Erase(iter); | 169 iter = cache_.Erase(iter); |
| 133 } else { | 170 } else { |
| 134 ++iter; | 171 ++iter; |
| 135 } | 172 } |
| 136 } | 173 } |
| 137 } | 174 } |
| 138 | 175 |
| 139 void SSLClientSessionCache::OnMemoryPressure( | 176 void SSLClientSessionCache::OnMemoryPressure( |
| 140 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 177 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 141 switch (memory_pressure_level) { | 178 switch (memory_pressure_level) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 162 break; | 199 break; |
| 163 case base::MemoryState::SUSPENDED: | 200 case base::MemoryState::SUSPENDED: |
| 164 // Note: Not supported at present. Fall through. | 201 // Note: Not supported at present. Fall through. |
| 165 case base::MemoryState::UNKNOWN: | 202 case base::MemoryState::UNKNOWN: |
| 166 NOTREACHED(); | 203 NOTREACHED(); |
| 167 break; | 204 break; |
| 168 } | 205 } |
| 169 } | 206 } |
| 170 | 207 |
| 171 } // namespace net | 208 } // namespace net |
| OLD | NEW |