Botan 3.14.1
Crypto and TLS for C&
tls_session_manager_sql.h
Go to the documentation of this file.
1/*
2* TLS Session Manager storing to encrypted SQL db table
3* (C) 2012,2014 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_TLS_SQL_SESSION_MANAGER_H_
9#define BOTAN_TLS_SQL_SESSION_MANAGER_H_
10
11#include <botan/database.h>
12#include <botan/symkey.h>
13#include <botan/tls_session_manager.h>
14
15namespace Botan {
16
18
19namespace TLS {
20
21/**
22* An implementation of Session_Manager that saves values in a SQL
23* database file, with the session data encrypted using a passphrase.
24*
25* @warning For clients, the hostnames associated with the saved
26* sessions are stored in the database in plaintext. This may be a
27* serious privacy risk in some situations.
28*/
30 public:
31 /**
32 * @param db A connection to the database to use
33 * The tables tls_sessions and tls_sessions_metadata (and the
34 * index tls_tickets) are owned by the session manager. They
35 * are dropped and recreated if their schema is missing or not
36 * recognized.
37 * @param passphrase used to encrypt the session data
38 * @param rng a random number generator
39 * @param max_sessions the maximum number of sessions kept in the
40 * database; whenever a session is stored, the oldest sessions
41 * beyond this limit are permanently deleted. (If zero, don't cap)
42 */
43 Session_Manager_SQL(std::shared_ptr<SQL_Database> db,
44 std::string_view passphrase,
45 const std::shared_ptr<RandomNumberGenerator>& rng,
46 size_t max_sessions = 1000);
47
52 ~Session_Manager_SQL() override = default;
53
54 void store(const Session& session, const Session_Handle& handle) override;
55 size_t remove(const Session_Handle& handle) override;
56 size_t remove_all() override;
57
58 bool emits_session_tickets() override { return false; }
59
60 protected:
61 std::optional<Session> retrieve_one(const Session_Handle& handle) override;
62 std::vector<Session_with_Handle> find_some(const Server_Information& info, size_t max_sessions_hint) override;
63
64 /**
65 * Decides whether the underlying database is considered threadsafe in the
66 * context the Session_Manager is used. If this returns `false`, accesses
67 * to the database are serialized with the base class' recursive mutex.
68 */
69 virtual bool database_is_threadsafe() const { return m_db->is_threadsafe(); }
70
71 private:
72 // Database Schema Revision history
73 //
74 // 0 - empty database (needs creation with latest schema)
75 // 1 - corrupted database detected (re-create it with latest schema)
76 // 20120609 - older (Botan 2.0) database scheme
77 // 20230113 - adapt to Botan 3.0 Session_Manager API
78 // (Session objects don't contain Session_ID, Session_Ticket)
79 enum Schema_Revision /* NOLINT(*-use-enum-class) */ {
80 EMPTY = 0,
81 CORRUPTED = 1,
82 PRE_BOTAN_3_0 = 20120609,
83 BOTAN_3_0 = 20230112,
84 };
85
86 void create_or_migrate_and_open(std::string_view passphrase);
87 Schema_Revision detect_schema_revision();
88 void create_with_latest_schema(std::string_view passphrase, Schema_Revision rev);
89 void initialize_existing_database(std::string_view passphrase);
90
91 void prune_session_cache();
92
93 private:
94 std::shared_ptr<SQL_Database> m_db;
95 SymmetricKey m_session_key;
96 size_t m_max_sessions;
97};
98
99} // namespace TLS
100
101} // namespace Botan
102
103#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
Helper class to embody a session handle in all protocol versions.
Session_Manager_SQL(std::shared_ptr< SQL_Database > db, std::string_view passphrase, const std::shared_ptr< RandomNumberGenerator > &rng, size_t max_sessions=1000)
void store(const Session &session, const Session_Handle &handle) override
Save a Session under a Session_Handle (TLS Client).
size_t remove(const Session_Handle &handle) override
~Session_Manager_SQL() override=default
Session_Manager_SQL & operator=(const Session_Manager_SQL &)=delete
Session_Manager_SQL(const Session_Manager_SQL &)=delete
Session_Manager_SQL(Session_Manager_SQL &&)=delete
Session_Manager_SQL & operator=(Session_Manager_SQL &&)=delete
BOTAN_FUTURE_EXPLICIT Session_Manager(const std::shared_ptr< RandomNumberGenerator > &rng)
OctetString SymmetricKey
Definition symkey.h:153