-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
13 changed files
with
668 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
/* Copyright (c) 2024 Chilledheart */ | ||
|
||
#ifdef UNSAFE_BUFFERS_BUILD | ||
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors. | ||
#pragma allow_unsafe_buffers | ||
#endif | ||
|
||
#include "net/ssl_client_session_cache.hpp" | ||
|
||
#include <tuple> | ||
#include <utility> | ||
|
||
#include <absl/time/clock.h> | ||
#include "third_party/boringssl/src/include/openssl/ssl.h" | ||
|
||
namespace net { | ||
|
||
namespace { | ||
|
||
// Returns a tuple of references to fields of |key|, for comparison purposes. | ||
auto TieKeyFields(const SSLClientSessionCache::Key& key) { | ||
return std::tie(key.server, key.dest_ip_addr); | ||
} | ||
|
||
} // namespace | ||
|
||
SSLClientSessionCache::Key::Key() = default; | ||
SSLClientSessionCache::Key::Key(const Key& other) = default; | ||
SSLClientSessionCache::Key::Key(Key&& other) = default; | ||
SSLClientSessionCache::Key::~Key() = default; | ||
SSLClientSessionCache::Key& SSLClientSessionCache::Key::operator=(const Key& other) = default; | ||
SSLClientSessionCache::Key& SSLClientSessionCache::Key::operator=(Key&& other) = default; | ||
|
||
bool SSLClientSessionCache::Key::operator==(const Key& other) const { | ||
return TieKeyFields(*this) == TieKeyFields(other); | ||
} | ||
|
||
bool SSLClientSessionCache::Key::operator<(const Key& other) const { | ||
return TieKeyFields(*this) < TieKeyFields(other); | ||
} | ||
|
||
SSLClientSessionCache::SSLClientSessionCache(const Config& config) : config_(config), cache_(config.max_entries) {} | ||
|
||
SSLClientSessionCache::~SSLClientSessionCache() { | ||
Flush(); | ||
} | ||
|
||
size_t SSLClientSessionCache::size() const { | ||
return cache_.size(); | ||
} | ||
|
||
bssl::UniquePtr<SSL_SESSION> SSLClientSessionCache::Lookup(const Key& cache_key) { | ||
// Expire stale sessions. | ||
lookups_since_flush_++; | ||
if (lookups_since_flush_ >= config_.expiration_check_count) { | ||
lookups_since_flush_ = 0; | ||
FlushExpiredSessions(); | ||
} | ||
|
||
auto iter = cache_.Get(cache_key); | ||
if (iter == cache_.end()) | ||
return nullptr; | ||
|
||
time_t now = absl::ToTimeT(absl::Now()); | ||
bssl::UniquePtr<SSL_SESSION> session = iter->second.Pop(); | ||
if (iter->second.ExpireSessions(now)) | ||
cache_.Erase(iter); | ||
|
||
if (IsExpired(session.get(), now)) | ||
session = nullptr; | ||
|
||
return session; | ||
} | ||
|
||
void SSLClientSessionCache::Insert(const Key& cache_key, bssl::UniquePtr<SSL_SESSION> session) { | ||
auto iter = cache_.Get(cache_key); | ||
if (iter == cache_.end()) | ||
iter = cache_.Put(cache_key, Entry()); | ||
iter->second.Push(std::move(session)); | ||
} | ||
|
||
void SSLClientSessionCache::ClearEarlyData(const Key& cache_key) { | ||
auto iter = cache_.Get(cache_key); | ||
if (iter != cache_.end()) { | ||
for (auto& session : iter->second.sessions) { | ||
if (session) { | ||
session.reset(SSL_SESSION_copy_without_early_data(session.get())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void SSLClientSessionCache::Flush() { | ||
cache_.Clear(); | ||
} | ||
|
||
bool SSLClientSessionCache::IsExpired(SSL_SESSION* session, time_t now) { | ||
if (now < 0) | ||
return true; | ||
uint64_t now_u64 = static_cast<uint64_t>(now); | ||
|
||
// now_u64 may be slightly behind because of differences in how | ||
// time is calculated at this layer versus BoringSSL. | ||
// Add a second of wiggle room to account for this. | ||
return now_u64 < SSL_SESSION_get_time(session) - 1 || | ||
now_u64 >= SSL_SESSION_get_time(session) + SSL_SESSION_get_timeout(session); | ||
} | ||
|
||
SSLClientSessionCache::Entry::Entry() = default; | ||
SSLClientSessionCache::Entry::Entry(Entry&&) = default; | ||
SSLClientSessionCache::Entry::~Entry() = default; | ||
|
||
void SSLClientSessionCache::Entry::Push(bssl::UniquePtr<SSL_SESSION> session) { | ||
if (sessions[0] != nullptr && SSL_SESSION_should_be_single_use(sessions[0].get())) { | ||
sessions[1] = std::move(sessions[0]); | ||
} | ||
sessions[0] = std::move(session); | ||
} | ||
|
||
bssl::UniquePtr<SSL_SESSION> SSLClientSessionCache::Entry::Pop() { | ||
if (sessions[0] == nullptr) | ||
return nullptr; | ||
bssl::UniquePtr<SSL_SESSION> session = bssl::UpRef(sessions[0]); | ||
if (SSL_SESSION_should_be_single_use(session.get())) { | ||
sessions[0] = std::move(sessions[1]); | ||
sessions[1] = nullptr; | ||
} | ||
return session; | ||
} | ||
|
||
bool SSLClientSessionCache::Entry::ExpireSessions(time_t now) { | ||
if (sessions[0] == nullptr) | ||
return true; | ||
|
||
if (SSLClientSessionCache::IsExpired(sessions[0].get(), now)) { | ||
return true; | ||
} | ||
|
||
if (sessions[1] != nullptr && SSLClientSessionCache::IsExpired(sessions[1].get(), now)) { | ||
sessions[1] = nullptr; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
void SSLClientSessionCache::FlushExpiredSessions() { | ||
time_t now = absl::ToTimeT(absl::Now()); | ||
auto iter = cache_.begin(); | ||
while (iter != cache_.end()) { | ||
if (iter->second.ExpireSessions(now)) { | ||
iter = cache_.Erase(iter); | ||
} else { | ||
++iter; | ||
} | ||
} | ||
} | ||
|
||
#if 0 | ||
void SSLClientSessionCache::OnMemoryPressure( | ||
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | ||
switch (memory_pressure_level) { | ||
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE: | ||
break; | ||
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE: | ||
FlushExpiredSessions(); | ||
break; | ||
case base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL: | ||
Flush(); | ||
break; | ||
} | ||
} | ||
#endif | ||
|
||
} // namespace net |
Oops, something went wrong.