Botan 2.19.1
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::TLS::Session_Manager_In_Memory Class Referencefinal

#include <tls_session_manager.h>

Inheritance diagram for Botan::TLS::Session_Manager_In_Memory:
Botan::TLS::Session_Manager

Public Member Functions

bool load_from_server_info (const Server_Information &info, Session &session) override
 
bool load_from_session_id (const std::vector< uint8_t > &session_id, Session &session) override
 
size_t remove_all () override
 
void remove_entry (const std::vector< uint8_t > &session_id) override
 
void save (const Session &session_data) override
 
std::chrono::seconds session_lifetime () const override
 
 Session_Manager_In_Memory (RandomNumberGenerator &rng, size_t max_sessions=1000, std::chrono::seconds session_lifetime=std::chrono::seconds(7200))
 

Detailed Description

An implementation of Session_Manager that saves values in memory.

Definition at line 108 of file tls_session_manager.h.

Constructor & Destructor Documentation

◆ Session_Manager_In_Memory()

Botan::TLS::Session_Manager_In_Memory::Session_Manager_In_Memory ( RandomNumberGenerator rng,
size_t  max_sessions = 1000,
std::chrono::seconds  session_lifetime = std::chrono::seconds(7200) 
)
Parameters
rnga RNG used for generating session key and for session encryption
max_sessionsa hint on the maximum number of sessions to keep in memory at any one time. (If zero, don't cap)
session_lifetimesessions are expired after this many seconds have elapsed from initial handshake.

Definition at line 17 of file tls_session_manager_memory.cpp.

20 :
21 m_max_sessions(max_sessions),
22 m_session_lifetime(session_lifetime),
23 m_rng(rng),
24 m_session_key(m_rng.random_vec(32))
25 {}
secure_vector< uint8_t > random_vec(size_t bytes)
Definition: rng.h:143
std::chrono::seconds session_lifetime() const override

Member Function Documentation

◆ load_from_server_info()

bool Botan::TLS::Session_Manager_In_Memory::load_from_server_info ( const Server_Information info,
Session session 
)
overridevirtual

Try to load a saved session (using info about server)

Parameters
infothe information about the server
sessionwill be set to the saved session data (if found), or not modified if not found
Returns
true if session was modified

Implements Botan::TLS::Session_Manager.

Definition at line 66 of file tls_session_manager_memory.cpp.

68 {
69 lock_guard_type<mutex_type> lock(m_mutex);
70
71 auto i = m_info_sessions.find(info);
72
73 if(i == m_info_sessions.end())
74 return false;
75
76 if(load_from_session_str(i->second, session))
77 return true;
78
79 /*
80 * It existed at one point but was removed from the sessions map,
81 * remove m_info_sessions entry as well
82 */
83 m_info_sessions.erase(i);
84
85 return false;
86 }

◆ load_from_session_id()

bool Botan::TLS::Session_Manager_In_Memory::load_from_session_id ( const std::vector< uint8_t > &  session_id,
Session session 
)
overridevirtual

Try to load a saved session (using session ID)

Parameters
session_idthe session identifier we are trying to resume
sessionwill be set to the saved session data (if found), or not modified if not found
Returns
true if session was modified

Implements Botan::TLS::Session_Manager.

Definition at line 58 of file tls_session_manager_memory.cpp.

60 {
61 lock_guard_type<mutex_type> lock(m_mutex);
62
63 return load_from_session_str(hex_encode(session_id), session);
64 }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:31

References Botan::hex_encode().

◆ remove_all()

size_t Botan::TLS::Session_Manager_In_Memory::remove_all ( )
overridevirtual

Remove all sessions from the cache, return number of sessions deleted

Implements Botan::TLS::Session_Manager.

Definition at line 99 of file tls_session_manager_memory.cpp.

100 {
101 const size_t removed = m_sessions.size();
102 m_info_sessions.clear();
103 m_sessions.clear();
104 m_rng.random_vec(m_session_key, 32);
105 return removed;
106 }

References Botan::RandomNumberGenerator::random_vec().

◆ remove_entry()

void Botan::TLS::Session_Manager_In_Memory::remove_entry ( const std::vector< uint8_t > &  session_id)
overridevirtual

Remove this session id from the cache, if it exists

Implements Botan::TLS::Session_Manager.

Definition at line 88 of file tls_session_manager_memory.cpp.

90 {
91 lock_guard_type<mutex_type> lock(m_mutex);
92
93 auto i = m_sessions.find(hex_encode(session_id));
94
95 if(i != m_sessions.end())
96 m_sessions.erase(i);
97 }

References Botan::hex_encode().

◆ save()

void Botan::TLS::Session_Manager_In_Memory::save ( const Session session)
overridevirtual

Save a session on a best effort basis; the manager may not in fact be able to save the session for whatever reason; this is not an error. Caller cannot assume that calling save followed immediately by load_from_* will result in a successful lookup.

Parameters
sessionto save

Implements Botan::TLS::Session_Manager.

Definition at line 108 of file tls_session_manager_memory.cpp.

109 {
110 lock_guard_type<mutex_type> lock(m_mutex);
111
112 if(m_max_sessions != 0)
113 {
114 /*
115 We generate new session IDs with the first 4 bytes being a
116 timestamp, so this actually removes the oldest sessions first.
117 */
118 while(m_sessions.size() >= m_max_sessions)
119 m_sessions.erase(m_sessions.begin());
120 }
121
122 const std::string session_id_str = hex_encode(session.session_id());
123
124 m_sessions[session_id_str] = session.encrypt(m_session_key, m_rng);
125
126 if(session.side() == CLIENT && !session.server_info().empty())
127 m_info_sessions[session.server_info()] = session_id_str;
128 }

References Botan::TLS::CLIENT, Botan::TLS::Server_Information::empty(), Botan::TLS::Session::encrypt(), Botan::hex_encode(), Botan::TLS::Session::server_info(), Botan::TLS::Session::session_id(), and Botan::TLS::Session::side().

◆ session_lifetime()

std::chrono::seconds Botan::TLS::Session_Manager_In_Memory::session_lifetime ( ) const
inlineoverridevirtual

Return the allowed lifetime of a session; beyond this time, sessions are not resumed. Returns 0 if unknown/no explicit expiration policy.

Implements Botan::TLS::Session_Manager.

Definition at line 136 of file tls_session_manager.h.

137 { return m_session_lifetime; }

The documentation for this class was generated from the following files: