Botan 3.11.0
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 table names botan_tls_sessions and
34 botan_tls_sessions_metadata will be used
35 * @param passphrase used to encrypt the session data
36 * @param rng a random number generator
37 * @param max_sessions a hint on the maximum number of sessions
38 * to keep in memory at any one time. (If zero, don't cap)
39 */
40 Session_Manager_SQL(std::shared_ptr<SQL_Database> db,
41 std::string_view passphrase,
42 const std::shared_ptr<RandomNumberGenerator>& rng,
43 size_t max_sessions = 1000);
44
49 ~Session_Manager_SQL() override = default;
50
51 void store(const Session& session, const Session_Handle& handle) override;
52 size_t remove(const Session_Handle& handle) override;
53 size_t remove_all() override;
54
55 bool emits_session_tickets() override { return false; }
56
57 protected:
58 std::optional<Session> retrieve_one(const Session_Handle& handle) override;
59 std::vector<Session_with_Handle> find_some(const Server_Information& info, size_t max_sessions_hint) override;
60
61 /**
62 * Decides whether the underlying database is considered threadsafe in the
63 * context the Session_Manager is used. If this returns `false`, accesses
64 * to the database are serialized with the base class' recursive mutex.
65 */
66 virtual bool database_is_threadsafe() const { return m_db->is_threadsafe(); }
67
68 private:
69 // Database Schema Revision history
70 //
71 // 0 - empty database (needs creation with latest schema)
72 // 1 - corrupted database detected (re-create it with latest schema)
73 // 20120609 - older (Botan 2.0) database scheme
74 // 20230113 - adapt to Botan 3.0 Session_Manager API
75 // (Session objects don't contain Session_ID, Session_Ticket)
76 enum Schema_Revision /* NOLINT(*-use-enum-class) */ {
77 EMPTY = 0,
78 CORRUPTED = 1,
79 PRE_BOTAN_3_0 = 20120609,
80 BOTAN_3_0 = 20230112,
81 };
82
83 void create_or_migrate_and_open(std::string_view passphrase);
84 Schema_Revision detect_schema_revision();
85 void create_with_latest_schema(std::string_view passphrase, Schema_Revision rev);
86 void initialize_existing_database(std::string_view passphrase);
87
88 void prune_session_cache();
89
90 private:
91 std::shared_ptr<SQL_Database> m_db;
92 SymmetricKey m_session_key;
93 size_t m_max_sessions;
94};
95
96} // namespace TLS
97
98} // namespace Botan
99
100#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:140