| 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 <openssl/ssl.h> |
| 8 #include <openssl/x509.h> |
| 7 #include <utility> | 9 #include <utility> |
| 8 | 10 |
| 9 #include "base/memory/memory_coordinator_client_registry.h" | 11 #include "base/memory/memory_coordinator_client_registry.h" |
| 10 #include "base/time/clock.h" | 12 #include "base/time/clock.h" |
| 11 #include "base/time/default_clock.h" | 13 #include "base/time/default_clock.h" |
| 12 #include "third_party/boringssl/src/include/openssl/ssl.h" | 14 #include "third_party/boringssl/src/include/openssl/ssl.h" |
| 13 | 15 |
| 16 #include "base/trace_event/memory_dump_manager.h" |
| 17 #include "net/cert/x509_util_openssl.h" |
| 18 |
| 19 #include "base/strings/stringprintf.h" |
| 14 namespace net { | 20 namespace net { |
| 15 | 21 |
| 16 SSLClientSessionCache::SSLClientSessionCache(const Config& config) | 22 SSLClientSessionCache::SSLClientSessionCache(const Config& config) |
| 17 : clock_(new base::DefaultClock), | 23 : clock_(new base::DefaultClock), |
| 18 config_(config), | 24 config_(config), |
| 19 cache_(config.max_entries), | 25 cache_(config.max_entries), |
| 20 lookups_since_flush_(0) { | 26 lookups_since_flush_(0) { |
| 21 memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( | 27 memory_pressure_listener_.reset(new base::MemoryPressureListener(base::Bind( |
| 22 &SSLClientSessionCache::OnMemoryPressure, base::Unretained(this)))); | 28 &SSLClientSessionCache::OnMemoryPressure, base::Unretained(this)))); |
| 23 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); | 29 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 std::unique_ptr<base::Clock> clock) { | 81 std::unique_ptr<base::Clock> clock) { |
| 76 clock_ = std::move(clock); | 82 clock_ = std::move(clock); |
| 77 } | 83 } |
| 78 | 84 |
| 79 bool SSLClientSessionCache::IsExpired(SSL_SESSION* session, time_t now) { | 85 bool SSLClientSessionCache::IsExpired(SSL_SESSION* session, time_t now) { |
| 80 return now < SSL_SESSION_get_time(session) || | 86 return now < SSL_SESSION_get_time(session) || |
| 81 now >= | 87 now >= |
| 82 SSL_SESSION_get_time(session) + SSL_SESSION_get_timeout(session); | 88 SSL_SESSION_get_time(session) + SSL_SESSION_get_timeout(session); |
| 83 } | 89 } |
| 84 | 90 |
| 91 void SSLClientSessionCache::PopulateAllocatorDump( |
| 92 base::trace_event::MemoryAllocatorDump* dump) const { |
| 93 std::string absolute_name = "net/ssl_session_cache"; |
| 94 base::trace_event::MemoryAllocatorDump* cache_dump = |
| 95 dump->process_memory_dump()->GetAllocatorDump(absolute_name); |
| 96 // This is a singleton, so only log it once. |
| 97 if (cache_dump) |
| 98 return; |
| 99 cache_dump = |
| 100 dump->process_memory_dump()->CreateAllocatorDump(absolute_name); |
| 101 auto iter = cache_.begin(); |
| 102 while (iter != cache_.end()) { |
| 103 auto entry = iter->second.get(); |
| 104 auto cert_chain = entry->cert_chain; |
| 105 size_t cert_count = sk_X509_num(cert_chain); |
| 106 base::trace_event::MemoryAllocatorDump* entry_dump = |
| 107 dump->process_memory_dump()->CreateAllocatorDump( |
| 108 base::StringPrintf("%s/entry_%p", absolute_name.c_str(), entry)); |
| 109 int cert_size = 0; |
| 110 for (size_t i = 0; i < cert_count; ++i) { |
| 111 X509* cert = sk_X509_value(cert_chain, i); |
| 112 cert_size += i2d_X509(cert, nullptr); |
| 113 } |
| 114 entry_dump->AddScalar("cert_size", |
| 115 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 116 cert_size); |
| 117 entry_dump->AddScalar("cert_count", |
| 118 base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 119 cert_count); |
| 120 entry_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 121 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 122 cert_size); |
| 123 |
| 124 ++iter; |
| 125 } |
| 126 } |
| 127 |
| 85 void SSLClientSessionCache::FlushExpiredSessions() { | 128 void SSLClientSessionCache::FlushExpiredSessions() { |
| 86 time_t now = clock_->Now().ToTimeT(); | 129 time_t now = clock_->Now().ToTimeT(); |
| 87 auto iter = cache_.begin(); | 130 auto iter = cache_.begin(); |
| 88 while (iter != cache_.end()) { | 131 while (iter != cache_.end()) { |
| 89 if (IsExpired(iter->second.get(), now)) { | 132 if (IsExpired(iter->second.get(), now)) { |
| 90 iter = cache_.Erase(iter); | 133 iter = cache_.Erase(iter); |
| 91 } else { | 134 } else { |
| 92 ++iter; | 135 ++iter; |
| 93 } | 136 } |
| 94 } | 137 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 120 break; | 163 break; |
| 121 case base::MemoryState::SUSPENDED: | 164 case base::MemoryState::SUSPENDED: |
| 122 // Note: Not supported at present. Fall through. | 165 // Note: Not supported at present. Fall through. |
| 123 case base::MemoryState::UNKNOWN: | 166 case base::MemoryState::UNKNOWN: |
| 124 NOTREACHED(); | 167 NOTREACHED(); |
| 125 break; | 168 break; |
| 126 } | 169 } |
| 127 } | 170 } |
| 128 | 171 |
| 129 } // namespace net | 172 } // namespace net |
| OLD | NEW |