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_SQL Class Reference

`#include <tls_session_manager_sql.h>`

Inheritance diagram for Botan::TLS::Session_Manager_SQL:

## 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
Session_Manager_SQL & | operator= (const Session_Manager_SQL &)=delete
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_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))
| Session_Manager_SQL (const Session_Manager_SQL &)=delete

## Detailed Description

An implementation of Session_Manager that saves values in a SQL 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_sql.h.

## Constructor & Destructor Documentation

Botan::TLS::Session_Manager_SQL::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)`
| ) |  |

Parameters

  db | A connection to the database to use The table names botan_tls_sessions and botan_tls_sessions_metadata will be used
  passphrase | used to encrypt the session data
  rng | a random number generator
  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 19 of file tls_session_manager_sql.cpp.

References Botan::get_pbkdf(), Botan::make_uint16(), Botan::RandomNumberGenerator::random_vec(), and Botan::unlock().

23 :
24 m_db(db),
25 m_rng(rng),
26 m_max_sessions(max_sessions),
27 m_session_lifetime(session_lifetime)
28 {
29 m_db->create_table(
30 "create table if not exists tls_sessions "
31 "("
32 "session_id TEXT PRIMARY KEY, "
33 "session_start INTEGER, "
34 "hostname TEXT, "
35 "hostport INTEGER, "
36 "session BLOB"
37 ")");
38
39 m_db->create_table(
40 "create table if not exists tls_sessions_metadata "
41 "("
42 "passphrase_salt BLOB, "
43 "passphrase_iterations INTEGER, "
44 "passphrase_check INTEGER "
45 ")");
46
47 const size_t salts = m_db->row_count("tls_sessions_metadata");
48
49 std::unique_ptr<PBKDF> pbkdf(get_pbkdf("PBKDF2(SHA-512)"));
50
51 if(salts == 1)
52 {
53 // existing db
54 auto stmt = m_db->new_statement("select * from tls_sessions_metadata");
55
56 if(stmt->step())
57 {
58 std::pair<const uint8_t*, size_t> salt = stmt->get_blob(0);
59 const size_t iterations = stmt->get_size_t(1);
60 const size_t check_val_db = stmt->get_size_t(2);
61
62 secure_vector<uint8_t> x = pbkdf->pbkdf_iterations(32 + 2,
63 passphrase,
64 salt.first, salt.second,
65 iterations);
66
67 const size_t check_val_created = make_uint16(x[0], x[1]);
68 m_session_key.assign(x.begin() + 2, x.end());
69
70 if(check_val_created != check_val_db)
71 throw Exception("Session database password not valid");
72 }
73 }
74 else
75 {
76 // maybe just zap the salts + sessions tables in this case?
77 if(salts != 0)
78 throw Exception("Seemingly corrupted database, multiple salts found");
79
80 // new database case
81
82 std::vector<uint8_t> salt = unlock(rng.random_vec(16));
83 size_t iterations = 0;
84
85 secure_vector<uint8_t> x = pbkdf->pbkdf_timed(32 + 2,
86 passphrase,
87 salt.data(), salt.size(),
88 std::chrono::milliseconds(100),
89 iterations);
90
91 size_t check_val = make_uint16(x[0], x[1]);
92 m_session_key.assign(x.begin() + 2, x.end());
93
94 auto stmt = m_db->new_statement("insert into tls_sessions_metadata values(?1, ?2, ?3)");
95
96 stmt->bind(1, salt);
97 stmt->bind(2, iterations);
98 stmt->bind(3, check_val);
99
100 stmt->spin();
101 }
102 }
Botan::TLS::Session_Manager_SQL::session_lifetime
std::chrono::seconds session_lifetime() const override
Definition: tls_session_manager_sql.h:63
Botan::get_pbkdf
PBKDF * get_pbkdf(const std::string &algo_spec, const std::string &provider="")
Definition: pbkdf.h:216
m_rng
RandomNumberGenerator & m_rng
Definition: ecdh.cpp:52
Botan::unlock
std::vector< T > unlock(const secure_vector< T > &in)
Definition: secmem.h:125
Botan::make_uint16
uint16_t make_uint16(uint8_t i0, uint8_t i1)
Definition: loadstor.h:60

Botan::TLS::Session_Manager_SQL::Session_Manager_SQL | ( | const Session_Manager_SQL & |  | ) |
| delete

## Member Function Documentation

bool Botan::TLS::Session_Manager_SQL::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 128 of file tls_session_manager_sql.cpp.

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

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

bool Botan::TLS::Session_Manager_SQL::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 104 of file tls_session_manager_sql.cpp.

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

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

Session_Manager_SQL& Botan::TLS::Session_Manager_SQL::operator= | ( | const Session_Manager_SQL & |  | ) |
| delete

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

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

Implements Botan::TLS::Session_Manager.

Definition at line 164 of file tls_session_manager_sql.cpp.

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

void Botan::TLS::Session_Manager_SQL::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 155 of file tls_session_manager_sql.cpp.

References Botan::hex_encode().

156 {
157 auto stmt = m_db->new_statement("delete from tls_sessions where session_id = ?1");
158
159 stmt->bind(1, hex_encode(session_id));
160
161 stmt->spin();
162 }
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_SQL::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 170 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().

171 {
172 auto stmt = m_db->new_statement("insert or replace into tls_sessions"
173 " values(?1, ?2, ?3, ?4, ?5)");
174
175 stmt->bind(1, hex_encode(session.session_id()));
176 stmt->bind(2, session.start_time());
177 stmt->bind(3, session.server_info().hostname());
178 stmt->bind(4, session.server_info().port());
179 stmt->bind(5, session.encrypt(m_session_key, m_rng));
180
181 stmt->spin();
182
183 prune_session_cache();
184 }
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_SQL::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 63 of file tls_session_manager_sql.h.

64 { return m_session_lifetime; }

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

- src/lib/tls/sessions_sql/tls_session_manager_sql.h
- src/lib/tls/sessions_sql/tls_session_manager_sql.cpp