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/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() : lookup_count(0) {} | |
| 22 SSLClientSessionCache::Entry::~Entry() = default; | |
| 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 |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 44 lookups_since_flush_++; | 48 lookups_since_flush_++; |
| 45 if (lookups_since_flush_ >= config_.expiration_check_count) { | 49 if (lookups_since_flush_ >= config_.expiration_check_count) { |
| 46 lookups_since_flush_ = 0; | 50 lookups_since_flush_ = 0; |
| 47 FlushExpiredSessions(); | 51 FlushExpiredSessions(); |
| 48 } | 52 } |
| 49 | 53 |
| 50 auto iter = cache_.Get(cache_key); | 54 auto iter = cache_.Get(cache_key); |
| 51 if (iter == cache_.end()) | 55 if (iter == cache_.end()) |
| 52 return nullptr; | 56 return nullptr; |
| 53 | 57 |
| 54 SSL_SESSION* session = iter->second.get(); | 58 SSL_SESSION* session = iter->second->session.get(); |
| 55 if (IsExpired(session, clock_->Now().ToTimeT())) { | 59 if (IsExpired(session, clock_->Now().ToTimeT())) { |
| 56 cache_.Erase(iter); | 60 cache_.Erase(iter); |
| 57 return nullptr; | 61 return nullptr; |
| 58 } | 62 } |
| 59 | 63 |
| 64 iter->second->lookup_count++; | |
|
davidben
2017/01/11 16:47:49
To simulate TLS 1.3 renewal, I think we want this
nharper
2017/01/11 22:31:18
You're right that I need to consider when the hand
| |
| 65 UMA_HISTOGRAM_EXACT_LINEAR("Net.SSLSessionLookupCount", | |
| 66 iter->second->lookup_count, 20); | |
|
davidben
2017/01/11 16:47:50
This will record for both HTTP/1.1 and HTTP/2 wher
nharper
2017/01/11 22:31:18
It sounds like returning an Entry and having SSLCl
| |
| 67 | |
| 60 SSL_SESSION_up_ref(session); | 68 SSL_SESSION_up_ref(session); |
| 61 return bssl::UniquePtr<SSL_SESSION>(session); | 69 return bssl::UniquePtr<SSL_SESSION>(session); |
| 62 } | 70 } |
| 63 | 71 |
| 64 void SSLClientSessionCache::Insert(const std::string& cache_key, | 72 void SSLClientSessionCache::Insert(const std::string& cache_key, |
| 65 SSL_SESSION* session) { | 73 SSL_SESSION* session) { |
| 66 base::AutoLock lock(lock_); | 74 base::AutoLock lock(lock_); |
| 67 | 75 |
| 68 SSL_SESSION_up_ref(session); | 76 SSL_SESSION_up_ref(session); |
| 69 cache_.Put(cache_key, bssl::UniquePtr<SSL_SESSION>(session)); | 77 std::unique_ptr<Entry> entry(new Entry()); |
| 78 entry->session = bssl::UniquePtr<SSL_SESSION>(session); | |
| 79 cache_.Put(cache_key, std::move(entry)); | |
| 80 // Record 0 lookups for the new session. | |
| 81 UMA_HISTOGRAM_EXACT_LINEAR("Net.SSLSessionLookupCount", 0, 20); | |
| 70 } | 82 } |
| 71 | 83 |
| 72 void SSLClientSessionCache::Flush() { | 84 void SSLClientSessionCache::Flush() { |
| 73 base::AutoLock lock(lock_); | 85 base::AutoLock lock(lock_); |
| 74 | 86 |
| 75 cache_.Clear(); | 87 cache_.Clear(); |
| 76 } | 88 } |
| 77 | 89 |
| 78 void SSLClientSessionCache::SetClockForTesting( | 90 void SSLClientSessionCache::SetClockForTesting( |
| 79 std::unique_ptr<base::Clock> clock) { | 91 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 | 106 // This method can be reached from different URLRequestContexts. Since this is |
| 95 // a singleton, only log memory stats once. | 107 // a singleton, only log memory stats once. |
| 96 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. | 108 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. |
| 97 if (cache_dump) | 109 if (cache_dump) |
| 98 return; | 110 return; |
| 99 cache_dump = pmd->CreateAllocatorDump(absolute_name); | 111 cache_dump = pmd->CreateAllocatorDump(absolute_name); |
| 100 base::AutoLock lock(lock_); | 112 base::AutoLock lock(lock_); |
| 101 int total_serialized_cert_size = 0; | 113 int total_serialized_cert_size = 0; |
| 102 int total_cert_count = 0; | 114 int total_cert_count = 0; |
| 103 for (const auto& pair : cache_) { | 115 for (const auto& pair : cache_) { |
| 104 auto entry = pair.second.get(); | 116 auto entry = pair.second->session.get(); |
| 105 auto cert_chain = entry->x509_chain; | 117 auto cert_chain = entry->x509_chain; |
| 106 size_t cert_count = sk_X509_num(cert_chain); | 118 size_t cert_count = sk_X509_num(cert_chain); |
| 107 total_cert_count += cert_count; | 119 total_cert_count += cert_count; |
| 108 for (size_t i = 0; i < cert_count; ++i) { | 120 for (size_t i = 0; i < cert_count; ++i) { |
| 109 X509* cert = sk_X509_value(cert_chain, i); | 121 X509* cert = sk_X509_value(cert_chain, i); |
| 110 total_serialized_cert_size += i2d_X509(cert, nullptr); | 122 total_serialized_cert_size += i2d_X509(cert, nullptr); |
| 111 } | 123 } |
| 112 } | 124 } |
| 113 // This measures the lower bound of the serialized certificate. It doesn't | 125 // This measures the lower bound of the serialized certificate. It doesn't |
| 114 // measure the actual memory used, which is 4x this amount (see | 126 // measure the actual memory used, which is 4x this amount (see |
| 115 // crbug.com/671420 for more details). | 127 // crbug.com/671420 for more details). |
| 116 cache_dump->AddScalar("serialized_cert_size", | 128 cache_dump->AddScalar("serialized_cert_size", |
| 117 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 129 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 118 total_serialized_cert_size); | 130 total_serialized_cert_size); |
| 119 cache_dump->AddScalar("cert_count", | 131 cache_dump->AddScalar("cert_count", |
| 120 base::trace_event::MemoryAllocatorDump::kUnitsObjects, | 132 base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 121 total_cert_count); | 133 total_cert_count); |
| 122 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 134 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 123 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 135 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 124 total_serialized_cert_size); | 136 total_serialized_cert_size); |
| 125 } | 137 } |
| 126 | 138 |
| 127 void SSLClientSessionCache::FlushExpiredSessions() { | 139 void SSLClientSessionCache::FlushExpiredSessions() { |
| 128 time_t now = clock_->Now().ToTimeT(); | 140 time_t now = clock_->Now().ToTimeT(); |
| 129 auto iter = cache_.begin(); | 141 auto iter = cache_.begin(); |
| 130 while (iter != cache_.end()) { | 142 while (iter != cache_.end()) { |
| 131 if (IsExpired(iter->second.get(), now)) { | 143 if (IsExpired(iter->second->session.get(), now)) { |
| 132 iter = cache_.Erase(iter); | 144 iter = cache_.Erase(iter); |
| 133 } else { | 145 } else { |
| 134 ++iter; | 146 ++iter; |
| 135 } | 147 } |
| 136 } | 148 } |
| 137 } | 149 } |
| 138 | 150 |
| 139 void SSLClientSessionCache::OnMemoryPressure( | 151 void SSLClientSessionCache::OnMemoryPressure( |
| 140 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 152 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 141 switch (memory_pressure_level) { | 153 switch (memory_pressure_level) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 162 break; | 174 break; |
| 163 case base::MemoryState::SUSPENDED: | 175 case base::MemoryState::SUSPENDED: |
| 164 // Note: Not supported at present. Fall through. | 176 // Note: Not supported at present. Fall through. |
| 165 case base::MemoryState::UNKNOWN: | 177 case base::MemoryState::UNKNOWN: |
| 166 NOTREACHED(); | 178 NOTREACHED(); |
| 167 break; | 179 break; |
| 168 } | 180 } |
| 169 } | 181 } |
| 170 | 182 |
| 171 } // namespace net | 183 } // namespace net |
| OLD | NEW |