Botan  2.7.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::TLS::Session_Manager_SQLite Class Referencefinal

#include <tls_session_manager_sqlite.h>

Inheritance diagram for Botan::TLS::Session_Manager_SQLite:
Botan::TLS::Session_Manager_SQL 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_SQLite (const std::string &passphrase, RandomNumberGenerator &rng, const std::string &db_filename, 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 a SQLite3 database file, with the session data encrypted using a passphrase.

Warning
For clients, the hostnames associated with the saved sessions are stored in the database in plaintext. This may be a serious privacy risk in some situations.

Definition at line 27 of file tls_session_manager_sqlite.h.

Constructor & Destructor Documentation

◆ Session_Manager_SQLite()

Botan::TLS::Session_Manager_SQLite::Session_Manager_SQLite ( const std::string &  passphrase,
RandomNumberGenerator rng,
const std::string &  db_filename,
size_t  max_sessions = 1000,
std::chrono::seconds  session_lifetime = std::chrono::seconds(7200) 
)
Parameters
passphraseused to encrypt the session data
rnga random number generator
db_filenamefilename of the SQLite database file. The table names tls_sessions and tls_sessions_metadata will be used
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 15 of file tls_session_manager_sqlite.cpp.

19  :
20  Session_Manager_SQL(std::make_shared<Sqlite3_Database>(db_filename),
21  passphrase,
22  rng,
23  max_sessions,
25  {}
std::chrono::seconds session_lifetime() const override
Session_Manager_SQL(std::shared_ptr< SQL_Database > db, const std::string &passphrase, RandomNumberGenerator &rng, size_t max_sessions=1000, std::chrono::seconds session_lifetime=std::chrono::seconds(7200))

Member Function Documentation

◆ load_from_server_info()

bool Botan::TLS::Session_Manager_SQL::load_from_server_info ( const Server_Information info,
Session session 
)
overridevirtualinherited

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 129 of file tls_session_manager_sql.cpp.

References Botan::TLS::Session::decrypt(), Botan::TLS::Server_Information::hostname(), and Botan::TLS::Server_Information::port().

131  {
132  auto stmt = m_db->new_statement("select session from tls_sessions"
133  " where hostname = ?1 and hostport = ?2"
134  " order by session_start desc");
135 
136  stmt->bind(1, server.hostname());
137  stmt->bind(2, server.port());
138 
139  while(stmt->step())
140  {
141  std::pair<const uint8_t*, size_t> blob = stmt->get_blob(0);
142 
143  try
144  {
145  session = Session::decrypt(blob.first, blob.second, m_session_key);
146  return true;
147  }
148  catch(...)
149  {
150  }
151  }
152 
153  return false;
154  }
static Session decrypt(const uint8_t ctext[], size_t ctext_size, const SymmetricKey &key)

◆ load_from_session_id()

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

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 105 of file tls_session_manager_sql.cpp.

References Botan::TLS::Session::decrypt(), and Botan::hex_encode().

107  {
108  auto stmt = m_db->new_statement("select session from tls_sessions where session_id = ?1");
109 
110  stmt->bind(1, hex_encode(session_id));
111 
112  while(stmt->step())
113  {
114  std::pair<const uint8_t*, size_t> blob = stmt->get_blob(0);
115 
116  try
117  {
118  session = Session::decrypt(blob.first, blob.second, m_session_key);
119  return true;
120  }
121  catch(...)
122  {
123  }
124  }
125 
126  return false;
127  }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14
static Session decrypt(const uint8_t ctext[], size_t ctext_size, const SymmetricKey &key)

◆ remove_all()

size_t Botan::TLS::Session_Manager_SQL::remove_all ( )
overridevirtualinherited

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

Implements Botan::TLS::Session_Manager.

Definition at line 165 of file tls_session_manager_sql.cpp.

166  {
167  auto stmt = m_db->new_statement("delete from tls_sessions");
168  return stmt->spin();
169  }

◆ remove_entry()

void Botan::TLS::Session_Manager_SQL::remove_entry ( const std::vector< uint8_t > &  session_id)
overridevirtualinherited

Remove this session id from the cache, if it exists

Implements Botan::TLS::Session_Manager.

Definition at line 156 of file tls_session_manager_sql.cpp.

References Botan::hex_encode().

157  {
158  auto stmt = m_db->new_statement("delete from tls_sessions where session_id = ?1");
159 
160  stmt->bind(1, hex_encode(session_id));
161 
162  stmt->spin();
163  }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14

◆ save()

void Botan::TLS::Session_Manager_SQL::save ( const Session session)
overridevirtualinherited

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 171 of file tls_session_manager_sql.cpp.

References Botan::TLS::Session::encrypt(), Botan::hex_encode(), Botan::TLS::Server_Information::hostname(), Botan::TLS::Server_Information::port(), Botan::TLS::Session::server_info(), Botan::TLS::Session::session_id(), and Botan::TLS::Session::start_time().

172  {
173  if(session.server_info().hostname().empty())
174  return;
175 
176  auto stmt = m_db->new_statement("insert or replace into tls_sessions"
177  " values(?1, ?2, ?3, ?4, ?5)");
178 
179  stmt->bind(1, hex_encode(session.session_id()));
180  stmt->bind(2, session.start_time());
181  stmt->bind(3, session.server_info().hostname());
182  stmt->bind(4, session.server_info().port());
183  stmt->bind(5, session.encrypt(m_session_key, m_rng));
184 
185  stmt->spin();
186 
187  prune_session_cache();
188  }
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition: hex.cpp:14

◆ session_lifetime()

std::chrono::seconds Botan::TLS::Session_Manager_SQL::session_lifetime ( ) const
inlineoverridevirtualinherited

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 64 of file tls_session_manager_sql.h.

65  { return m_session_lifetime; }

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