Botan 3.6.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/tls_session_manager.h>
13
14namespace Botan {
15
16class RandomNumberGenerator;
17
18namespace TLS {
19
20/**
21* An implementation of Session_Manager that saves values in a SQL
22* database file, with the session data encrypted using a passphrase.
23*
24* @warning For clients, the hostnames associated with the saved
25* sessions are stored in the database in plaintext. This may be a
26* serious privacy risk in some situations.
27*/
29 public:
30 /**
31 * @param db A connection to the database to use
32 The table names botan_tls_sessions and
33 botan_tls_sessions_metadata will be used
34 * @param passphrase used to encrypt the session data
35 * @param rng a random number generator
36 * @param max_sessions a hint on the maximum number of sessions
37 * to keep in memory at any one time. (If zero, don't cap)
38 */
39 Session_Manager_SQL(std::shared_ptr<SQL_Database> db,
40 std::string_view passphrase,
41 const std::shared_ptr<RandomNumberGenerator>& rng,
42 size_t max_sessions = 1000);
43
46
47 void store(const Session& session, const Session_Handle& handle) override;
48 size_t remove(const Session_Handle& handle) override;
49 size_t remove_all() override;
50
51 bool emits_session_tickets() override { return false; }
52
53 protected:
54 std::optional<Session> retrieve_one(const Session_Handle& handle) override;
55 std::vector<Session_with_Handle> find_some(const Server_Information& info, size_t max_sessions_hint) override;
56
57 /**
58 * Decides whether the underlying database is considered threadsafe in the
59 * context the Session_Manager is used. If this returns `false`, accesses
60 * to the database are serialized with the base class' recursive mutex.
61 */
62 virtual bool database_is_threadsafe() const { return m_db->is_threadsafe(); }
63
64 private:
65 // Database Schema Revision history
66 //
67 // 0 - empty database (needs creation with latest schema)
68 // 1 - corrupted database detected (re-create it with latest schema)
69 // 20120609 - older (Botan 2.0) database scheme
70 // 20230113 - adapt to Botan 3.0 Session_Manager API
71 // (Session objects don't contain Session_ID, Session_Ticket)
72 enum Schema_Revision {
73 EMPTY = 0,
74 CORRUPTED = 1,
75 PRE_BOTAN_3_0 = 20120609,
76 BOTAN_3_0 = 20230112,
77 };
78
79 void create_or_migrate_and_open(std::string_view passphrase);
80 Schema_Revision detect_schema_revision();
81 void create_with_latest_schema(std::string_view passphrase, Schema_Revision rev);
82 void initialize_existing_database(std::string_view passphrase);
83
84 void prune_session_cache();
85
86 private:
87 std::shared_ptr<SQL_Database> m_db;
88 SymmetricKey m_session_key;
89 size_t m_max_sessions;
90};
91
92} // namespace TLS
93
94} // namespace Botan
95
96#endif
Helper class to embody a session handle in all protocol versions.
Definition tls_session.h:64
Session_Manager_SQL & operator=(const Session_Manager_SQL &)=delete
Session_Manager_SQL(const Session_Manager_SQL &)=delete
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31