1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/ssl/ssl_client_session_cache.h"
6
7 #include <tuple>
8 #include <utility>
9
10 #include "base/containers/flat_set.h"
11 #include "base/time/clock.h"
12 #include "base/time/default_clock.h"
13 #include "third_party/boringssl/src/include/openssl/ssl.h"
14
15 namespace net {
16
17 namespace {
18
19 // Returns a tuple of references to fields of |key|, for comparison purposes.
TieKeyFields(const SSLClientSessionCache::Key & key)20 auto TieKeyFields(const SSLClientSessionCache::Key& key) {
21 return std::tie(key.server, key.dest_ip_addr, key.network_anonymization_key,
22 key.privacy_mode, key.disable_legacy_crypto);
23 }
24
25 } // namespace
26
27 SSLClientSessionCache::Key::Key() = default;
28 SSLClientSessionCache::Key::Key(const Key& other) = default;
29 SSLClientSessionCache::Key::Key(Key&& other) = default;
30 SSLClientSessionCache::Key::~Key() = default;
31 SSLClientSessionCache::Key& SSLClientSessionCache::Key::operator=(
32 const Key& other) = default;
33 SSLClientSessionCache::Key& SSLClientSessionCache::Key::operator=(Key&& other) =
34 default;
35
operator ==(const Key & other) const36 bool SSLClientSessionCache::Key::operator==(const Key& other) const {
37 return TieKeyFields(*this) == TieKeyFields(other);
38 }
39
operator <(const Key & other) const40 bool SSLClientSessionCache::Key::operator<(const Key& other) const {
41 return TieKeyFields(*this) < TieKeyFields(other);
42 }
43
SSLClientSessionCache(const Config & config)44 SSLClientSessionCache::SSLClientSessionCache(const Config& config)
45 : clock_(base::DefaultClock::GetInstance()),
46 config_(config),
47 cache_(config.max_entries) {
48 memory_pressure_listener_ = std::make_unique<base::MemoryPressureListener>(
49 FROM_HERE, base::BindRepeating(&SSLClientSessionCache::OnMemoryPressure,
50 base::Unretained(this)));
51 }
52
~SSLClientSessionCache()53 SSLClientSessionCache::~SSLClientSessionCache() {
54 Flush();
55 }
56
size() const57 size_t SSLClientSessionCache::size() const {
58 return cache_.size();
59 }
60
Lookup(const Key & cache_key)61 bssl::UniquePtr<SSL_SESSION> SSLClientSessionCache::Lookup(
62 const Key& cache_key) {
63 // Expire stale sessions.
64 lookups_since_flush_++;
65 if (lookups_since_flush_ >= config_.expiration_check_count) {
66 lookups_since_flush_ = 0;
67 FlushExpiredSessions();
68 }
69
70 auto iter = cache_.Get(cache_key);
71 if (iter == cache_.end())
72 return nullptr;
73
74 time_t now = clock_->Now().ToTimeT();
75 bssl::UniquePtr<SSL_SESSION> session = iter->second.Pop();
76 if (iter->second.ExpireSessions(now))
77 cache_.Erase(iter);
78
79 if (IsExpired(session.get(), now))
80 session = nullptr;
81
82 return session;
83 }
84
Insert(const Key & cache_key,bssl::UniquePtr<SSL_SESSION> session)85 void SSLClientSessionCache::Insert(const Key& cache_key,
86 bssl::UniquePtr<SSL_SESSION> session) {
87 auto iter = cache_.Get(cache_key);
88 if (iter == cache_.end())
89 iter = cache_.Put(cache_key, Entry());
90 iter->second.Push(std::move(session));
91 }
92
ClearEarlyData(const Key & cache_key)93 void SSLClientSessionCache::ClearEarlyData(const Key& cache_key) {
94 auto iter = cache_.Get(cache_key);
95 if (iter != cache_.end()) {
96 for (auto& session : iter->second.sessions) {
97 if (session) {
98 session.reset(SSL_SESSION_copy_without_early_data(session.get()));
99 }
100 }
101 }
102 }
103
FlushForServer(const HostPortPair & server)104 void SSLClientSessionCache::FlushForServer(const HostPortPair& server) {
105 auto iter = cache_.begin();
106 while (iter != cache_.end()) {
107 if (iter->first.server == server) {
108 iter = cache_.Erase(iter);
109 } else {
110 ++iter;
111 }
112 }
113 }
114
Flush()115 void SSLClientSessionCache::Flush() {
116 cache_.Clear();
117 }
118
SetClockForTesting(base::Clock * clock)119 void SSLClientSessionCache::SetClockForTesting(base::Clock* clock) {
120 clock_ = clock;
121 }
122
IsExpired(SSL_SESSION * session,time_t now)123 bool SSLClientSessionCache::IsExpired(SSL_SESSION* session, time_t now) {
124 if (now < 0)
125 return true;
126 uint64_t now_u64 = static_cast<uint64_t>(now);
127
128 // now_u64 may be slightly behind because of differences in how
129 // time is calculated at this layer versus BoringSSL.
130 // Add a second of wiggle room to account for this.
131 return now_u64 < SSL_SESSION_get_time(session) - 1 ||
132 now_u64 >=
133 SSL_SESSION_get_time(session) + SSL_SESSION_get_timeout(session);
134 }
135
136 SSLClientSessionCache::Entry::Entry() = default;
137 SSLClientSessionCache::Entry::Entry(Entry&&) = default;
138 SSLClientSessionCache::Entry::~Entry() = default;
139
Push(bssl::UniquePtr<SSL_SESSION> session)140 void SSLClientSessionCache::Entry::Push(bssl::UniquePtr<SSL_SESSION> session) {
141 if (sessions[0] != nullptr &&
142 SSL_SESSION_should_be_single_use(sessions[0].get())) {
143 sessions[1] = std::move(sessions[0]);
144 }
145 sessions[0] = std::move(session);
146 }
147
Pop()148 bssl::UniquePtr<SSL_SESSION> SSLClientSessionCache::Entry::Pop() {
149 if (sessions[0] == nullptr)
150 return nullptr;
151 bssl::UniquePtr<SSL_SESSION> session = bssl::UpRef(sessions[0]);
152 if (SSL_SESSION_should_be_single_use(session.get())) {
153 sessions[0] = std::move(sessions[1]);
154 sessions[1] = nullptr;
155 }
156 return session;
157 }
158
ExpireSessions(time_t now)159 bool SSLClientSessionCache::Entry::ExpireSessions(time_t now) {
160 if (sessions[0] == nullptr)
161 return true;
162
163 if (SSLClientSessionCache::IsExpired(sessions[0].get(), now)) {
164 return true;
165 }
166
167 if (sessions[1] != nullptr &&
168 SSLClientSessionCache::IsExpired(sessions[1].get(), now)) {
169 sessions[1] = nullptr;
170 }
171
172 return false;
173 }
174
FlushExpiredSessions()175 void SSLClientSessionCache::FlushExpiredSessions() {
176 time_t now = clock_->Now().ToTimeT();
177 auto iter = cache_.begin();
178 while (iter != cache_.end()) {
179 if (iter->second.ExpireSessions(now)) {
180 iter = cache_.Erase(iter);
181 } else {
182 ++iter;
183 }
184 }
185 }
186
OnMemoryPressure(base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level)187 void SSLClientSessionCache::OnMemoryPressure(
188 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
189 switch (memory_pressure_level) {
190 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE:
191 break;
192 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE:
193 FlushExpiredSessions();
194 break;
195 case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL:
196 Flush();
197 break;
198 }
199 }
200
201 } // namespace net
202