Botan 3.14.1
Crypto and TLS for C&
tls_session_manager_memory.cpp
Go to the documentation of this file.
1/**
2 * TLS Session Management
3 * (C) 2011,2012 Jack Lloyd
4 * 2023 René Meusel - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#include <botan/tls_session_manager_memory.h>
10
11#include <botan/rng.h>
12#include <botan/internal/stl_util.h>
13#include <algorithm>
14#include <functional>
15#include <tuple>
16
17namespace Botan::TLS {
18
19Session_Manager_In_Memory::Session_Manager_In_Memory(const std::shared_ptr<RandomNumberGenerator>& rng,
20 size_t max_sessions) :
21 Session_Manager(rng), m_max_sessions(max_sessions) {
22 if(max_sessions > 0) {
23 m_fifo.emplace();
24 }
25}
26
27void Session_Manager_In_Memory::store(const Session& session, const Session_Handle& handle) {
28 // TODO: C++20 allows CTAD for template aliases (read: lock_guard_type), so
29 // technically we should be able to omit the explicit mutex type.
30 // Unfortunately clang does not agree, yet.
32
33 // Generate a random session ID if the peer did not provide one. Note that
34 // this ID is just for internal use and won't be returned on ::find().
35 auto id = handle.id().has_value() ? handle.id().value() : m_rng->random_vec<Session_ID>(32);
36
37 // Storing a handle again (eg. after a TLS 1.2 resumption) replaces the
38 // session, which then counts as the most recently stored one
39 m_sessions.erase(id);
40
41 // A FIFO entry is stale if its session was removed or stored again since
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;
45 };
46
47 if(m_fifo.has_value()) {
48 while(m_sessions.size() >= capacity()) {
49 BOTAN_ASSERT_NOMSG(m_sessions.size() <= m_fifo->size());
50 if(is_live(m_fifo->front())) {
51 m_sessions.erase(m_fifo->front().first);
52 }
53 m_fifo->pop_front();
54 }
55 }
56
57 const uint64_t sequence_number = m_next_sequence_number++;
58 m_sessions.emplace(id, Stored_Session{Session_with_Handle{session, handle}, sequence_number});
59
60 if(m_fifo.has_value()) {
61 m_fifo->emplace_back(std::move(id), sequence_number);
62
63 // Drop stale entries before they can accumulate without bound
64 if(m_fifo->size() > 2 * capacity()) {
65 std::erase_if(*m_fifo, [&](const auto& entry) { return !is_live(entry); });
66 }
67 }
68}
69
70std::optional<Session> Session_Manager_In_Memory::retrieve_one(const Session_Handle& handle) {
72
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;
77 // Sessions stored by the client role are not subject to server-side lookup
78 if(session.side() == Connection_Side::Server) {
79 return session;
80 }
81 }
82 }
83
84 return std::nullopt;
85}
86
87std::vector<Session_with_Handle> Session_Manager_In_Memory::find_some(const Server_Information& info,
88 const size_t max_sessions_hint) {
89 BOTAN_UNUSED(max_sessions_hint);
90
92
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;
96 // Sessions established by the server role are not offered to clients
97 if(session.side() == Connection_Side::Client && session.server_info() == info) {
98 matches.emplace_back(stored);
99 }
100 }
101
102 // Prefer the most recently established session, i.e. the freshest ticket.
103 // Insertion order breaks ties, e.g. between tickets received within the
104 // same connection or from applications with a coarse-grained clock.
105 std::ranges::sort(matches, [](const Stored_Session& a, const Stored_Session& b) {
106 const auto a_start = a.session_and_handle.session.start_time();
107 const auto b_start = b.session_and_handle.session.start_time();
108 return std::tie(a_start, a.sequence_number) > std::tie(b_start, b.sequence_number);
109 });
110
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);
115 }
116
117 return found_sessions;
118}
119
122 return remove_internal(handle);
123}
124
125size_t Session_Manager_In_Memory::remove_internal(const Session_Handle& handle) {
126 return std::visit(overloaded{
127 // We deliberately leave the deleted session in m_fifo. Finding it would be
128 // another O(n) operation. Instead, purging will run in a loop and skip m_fifo
129 // entries that do not match a stored session anymore.
130 [&](const Session_Ticket& ticket) -> size_t {
131 // TODO: This is an O(n) operation. Typically, the Session_Manager will
132 // not contain a plethora of sessions and this should be fine. If
133 // it's not, we'll need to consider another index on tickets.
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;
137 // TLS 1.3 clients store their tickets as Opaque_Session_Handle
138 const auto this_ticket = this_handle.ticket();
139 return this_ticket.has_value() && this_ticket.value() == ticket;
140 });
141 },
142 [&](const Session_ID& id) -> size_t { return m_sessions.erase(id); },
143 [&](const Opaque_Session_Handle&) -> size_t {
144 if(auto id = handle.id()) {
145 auto removed = remove_internal(Session_Handle(id.value()));
146 if(removed > 0) {
147 return removed;
148 }
149 }
150
151 if(auto ticket = handle.ticket()) {
152 return remove_internal(Session_Handle(ticket.value()));
153 }
154
155 return 0;
156 },
157 },
158 handle.get());
159}
160
163
164 const auto sessions = m_sessions.size();
165 m_sessions.clear();
166 if(m_fifo.has_value()) {
167 m_fifo->clear();
168 }
169
170 return sessions;
171}
172
173} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
std::chrono::system_clock::time_point start_time() const
Definition tls_session.h:69
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).
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
Definition mutex.h:58