9#include <botan/tls_session_manager_memory.h>
12#include <botan/internal/stl_util.h>
20 size_t max_sessions) :
22 if(max_sessions > 0) {
35 auto id = handle.
id().has_value() ? handle.
id().value() :
m_rng->random_vec<
Session_ID>(32);
42 const auto is_live = [&](
const std::pair<Session_ID, uint64_t>& entry) {
43 const auto stored = m_sessions.find(entry.first);
44 return stored != m_sessions.end() && stored->second.sequence_number == entry.second;
47 if(m_fifo.has_value()) {
48 while(m_sessions.size() >=
capacity()) {
50 if(is_live(m_fifo->front())) {
51 m_sessions.erase(m_fifo->front().first);
57 const uint64_t sequence_number = m_next_sequence_number++;
58 m_sessions.emplace(
id, Stored_Session{
Session_with_Handle{session, handle}, sequence_number});
60 if(m_fifo.has_value()) {
61 m_fifo->emplace_back(std::move(
id), sequence_number);
64 if(m_fifo->size() > 2 *
capacity()) {
65 std::erase_if(*m_fifo, [&](
const auto& entry) {
return !is_live(entry); });
73 if(
auto id = handle.
id()) {
74 const auto stored = m_sessions.find(
id.value());
75 if(stored != m_sessions.end()) {
76 const auto& session = stored->second.session_and_handle.session;
88 const size_t max_sessions_hint) {
93 std::vector<std::reference_wrapper<const Stored_Session>> matches;
94 for(
const auto& [_, stored] : m_sessions) {
95 const auto& session = stored.session_and_handle.session;
98 matches.emplace_back(stored);
105 std::ranges::sort(matches, [](
const Stored_Session& a,
const Stored_Session& b) {
108 return std::tie(a_start, a.sequence_number) > std::tie(b_start, b.sequence_number);
111 std::vector<Session_with_Handle> found_sessions;
112 found_sessions.reserve(matches.size());
113 for(
const Stored_Session& stored : matches) {
114 found_sessions.emplace_back(stored.session_and_handle);
117 return found_sessions;
122 return remove_internal(handle);
125size_t Session_Manager_In_Memory::remove_internal(
const Session_Handle& handle) {
134 return std::erase_if(m_sessions, [&](
const auto& item) {
135 const auto& [_unused1, stored] = item;
136 const auto& [_unused2, this_handle] = stored.session_and_handle;
138 const auto this_ticket = this_handle.ticket();
139 return this_ticket.has_value() && this_ticket.value() == ticket;
142 [&](
const Session_ID& id) ->
size_t {
return m_sessions.erase(
id); },
144 if(
auto id = handle.
id()) {
145 auto removed = remove_internal(Session_Handle(
id.value()));
151 if(
auto ticket = handle.
ticket()) {
152 return remove_internal(Session_Handle(ticket.value()));
164 const auto sessions = m_sessions.size();
166 if(m_fifo.has_value()) {
#define BOTAN_ASSERT_NOMSG(expr)
std::chrono::system_clock::time_point start_time() const
Helper class to embody a session handle in all protocol versions.
std::optional< Session_Ticket > ticket() const
decltype(auto) get() const
std::optional< Session_ID > id() const
void store(const Session &session, const Session_Handle &handle) override
Save a Session under a Session_Handle (TLS Client).
size_t remove_all() override
BOTAN_FUTURE_EXPLICIT Session_Manager_In_Memory(const std::shared_ptr< RandomNumberGenerator > &rng, size_t max_sessions=1000)
std::vector< Session_with_Handle > find_some(const Server_Information &info, size_t max_sessions_hint) override
Internal retrieval function to find sessions to resume.
size_t remove(const Session_Handle &handle) override
std::optional< Session > retrieve_one(const Session_Handle &handle) override
Internal retrieval function for a single session.
recursive_mutex_type & mutex()
BOTAN_FUTURE_EXPLICIT Session_Manager(const std::shared_ptr< RandomNumberGenerator > &rng)
std::shared_ptr< RandomNumberGenerator > m_rng
Strong< std::vector< uint8_t >, struct Session_ID_ > Session_ID
holds a TLS 1.2 session ID for stateful resumption
Strong< std::vector< uint8_t >, struct Session_Ticket_ > Session_Ticket
holds a TLS 1.2 session ticket for stateless resumption
Strong< std::vector< uint8_t >, struct Opaque_Session_Handle_ > Opaque_Session_Handle
holds an opaque session handle as used in TLS 1.3 that could be either a ticket for stateless resumpt...
lock_guard< T > lock_guard_type