9#include <botan/tls_session_manager.h>
12#include <botan/tls_callbacks.h>
13#include <botan/tls_policy.h>
22 const std::optional<Session_ID>&
id,
23 bool tls12_no_ticket) {
32 store(session, handle);
44 if(!session.has_value()) {
49 const std::chrono::seconds policy_lifetime =
70 const auto ticket_age =
71 std::chrono::duration_cast<std::chrono::seconds>(callbacks.
tls_current_timestamp() - session->start_time());
72 const bool expired = ticket_age > policy_lifetime;
82std::vector<Session_with_Handle> Session_Manager::find_and_filter(
const Server_Information& info,
87 const std::chrono::seconds policy_lifetime =
95 constexpr unsigned int max_attempts = 10;
96 std::vector<Session_with_Handle> sessions_and_handles;
100 for(
unsigned int attempt = 0; attempt < max_attempts && sessions_and_handles.empty(); ++attempt) {
101 sessions_and_handles =
find_some(info, max_sessions_hint);
104 if(sessions_and_handles.empty()) {
109 sessions_and_handles.erase(
110 std::remove_if(sessions_and_handles.begin(),
111 sessions_and_handles.end(),
112 [&](
const auto& session) {
114 std::chrono::duration_cast<std::chrono::seconds>(now - session.session.start_time());
146 const auto session_lifetime_hint = session.session.lifetime_hint();
147 const bool expired = age > std::min(policy_lifetime, session_lifetime_hint);
150 remove(session.handle);
155 sessions_and_handles.end());
158 return sessions_and_handles;
170 std::optional<lock_guard_type<recursive_mutex_type>> lk;
171 if(!allow_reusing_tickets) {
175 auto sessions_and_handles = find_and_filter(info, callbacks, policy);
180 while(session_limit > 0 && sessions_and_handles.size() > session_limit) {
181 sessions_and_handles.pop_back();
190 if(!allow_reusing_tickets) {
194 for(
const auto& [session, handle] : sessions_and_handles) {
195 if(!session.version().is_pre_tls_13() || !handle.is_id()) {
201 return sessions_and_handles;
204#if defined(BOTAN_HAS_TLS_13)
206std::optional<std::pair<Session, uint16_t>> Session_Manager::choose_from_offered_tickets(
207 const std::vector<PskIdentity>& tickets,
208 std::string_view hash_function,
214 for(uint16_t i = 0;
const auto& ticket : tickets) {
216 if(session.has_value() && session->ciphersuite().prf_algo() == hash_function &&
217 session->version().is_tls_13_or_later()) {
218 return std::pair{std::move(session.value()), i};
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ASSERT(expr, assertion_made)
virtual std::chrono::system_clock::time_point tls_current_timestamp()
virtual bool reuse_session_tickets() const
virtual size_t maximum_session_tickets_per_client_hello() const
virtual std::chrono::seconds session_ticket_lifetime() const
Connection_Side side() const
Helper class to embody a session handle in all protocol versions.
virtual size_t remove(const Session_Handle &handle)=0
virtual std::optional< Session > retrieve_one(const Session_Handle &handle)=0
Internal retrieval function for a single session.
virtual void store(const Session &session, const Session_Handle &handle)=0
Save a Session under a Session_Handle (TLS Client)
Session_Manager(const std::shared_ptr< RandomNumberGenerator > &rng)
virtual std::vector< Session_with_Handle > find_some(const Server_Information &info, size_t max_sessions_hint)=0
Internal retrieval function to find sessions to resume.
virtual std::optional< Session_Handle > establish(const Session &session, const std::optional< Session_ID > &id=std::nullopt, bool tls12_no_ticket=false)
Save a new Session and assign a Session_Handle (TLS Server)
std::shared_ptr< RandomNumberGenerator > m_rng
virtual std::optional< Session > retrieve(const Session_Handle &handle, Callbacks &callbacks, const Policy &policy)
Retrieves a specific session given a handle.