Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Botan::TLS::Session_Manager_In_Memory Class Reference

`#include <tls_session_manager.h>`

Inheritance diagram for Botan::TLS::Session_Manager_In_Memory:

## 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

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

  rng | a RNG used for generating session key and for session encryption
  max_sessions | a hint on the maximum number of sessions to keep in memory at any one time. (If zero, don't cap)
  session_lifetime | sessions are expired after this many seconds have elapsed from initial handshake.

Definition at line 16 of file tls_session_manager_memory.cpp.

19 :
20 m_max_sessions(max_sessions),
21 m_session_lifetime(session_lifetime),
22 m_rng(rng),
23 m_session_key(m_rng.random_vec(32))
24 {}
m_rng
RandomNumberGenerator & m_rng
Definition: ecdh.cpp:52
Botan::TLS::Session_Manager_In_Memory::session_lifetime
std::chrono::seconds session_lifetime() const override
Definition: tls_session_manager.h:136

## Member Function Documentation

bool Botan::TLS::Session_Manager_In_Memory::load_from_server_info | ( | const Server_Information & | info,
|  | Session & | session
| ) |  |
| override virtual

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

Parameters

  info | the information about the server
  session | will 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 65 of file tls_session_manager_memory.cpp.

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

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

Try to load a saved session (using session ID)

Parameters

  session_id | the session identifier we are trying to resume
  session | will 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 57 of file tls_session_manager_memory.cpp.

References Botan::hex_encode().

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

size_t Botan::TLS::Session_Manager_In_Memory::remove_all | ( |  | ) |
| override virtual

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

Implements Botan::TLS::Session_Manager.

Definition at line 98 of file tls_session_manager_memory.cpp.

References Botan::RandomNumberGenerator::random_vec().

99 {
100 const size_t removed = m_sessions.size();
101 m_info_sessions.clear();
102 m_sessions.clear();
103 m_session_key = m_rng.random_vec(32);
104 return removed;
105 }
m_rng
RandomNumberGenerator & m_rng
Definition: ecdh.cpp:52

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

Remove this session id from the cache, if it exists

Implements Botan::TLS::Session_Manager.

Definition at line 87 of file tls_session_manager_memory.cpp.

References Botan::hex_encode().

89 {
90 lock_guard_type<mutex_type> lock(m_mutex);
91
92 auto i = m_sessions.find(hex_encode(session_id));
93
94 if(i != m_sessions.end())
95 m_sessions.erase(i);
96 }
Botan::hex_encode
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14

void Botan::TLS::Session_Manager_In_Memory::save | ( | const Session & | session | ) |
| override virtual

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

  session | to save

Implements Botan::TLS::Session_Manager.

Definition at line 107 of file tls_session_manager_memory.cpp.

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().

108 {
109 lock_guard_type<mutex_type> lock(m_mutex);
110
111 if(m_max_sessions != 0)
112 {
113 /*
114 We generate new session IDs with the first 4 bytes being a
115 timestamp, so this actually removes the oldest sessions first.
116 */
117 while(m_sessions.size() >= m_max_sessions)
118 m_sessions.erase(m_sessions.begin());
119 }
120
121 const std::string session_id_str = hex_encode(session.session_id());
122
123 m_sessions[session_id_str] = session.encrypt(m_session_key, m_rng);
124
125 if(session.side() == CLIENT && !session.server_info().empty())
126 m_info_sessions[session.server_info()] = session_id_str;
127 }
Botan::TLS::CLIENT
Definition: tls_magic.h:27
m_rng
RandomNumberGenerator & m_rng
Definition: ecdh.cpp:52
Botan::hex_encode
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14

std::chrono::seconds Botan::TLS::Session_Manager_In_Memory::session_lifetime | ( |  | ) | const
| inline override virtual

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:

- src/lib/tls/tls_session_manager.h
- src/lib/tls/tls_session_manager_memory.cpp