blob: 823f4c123e1fffb088a40a3b6c8a5724fb1653eb (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/*
* TLS Session Management
* (C) 2011 Jack Lloyd
*
* Released under the terms of the Botan license
*/
#include <botan/tls_session_manager.h>
#include <botan/hex.h>
#include <chrono>
namespace Botan {
namespace TLS {
bool Session_Manager_In_Memory::load_from_session_str(
const std::string& session_str, Session& session)
{
// assert(lock is held)
auto i = m_sessions.find(session_str);
if(i == m_sessions.end())
return false;
// if session has expired, remove it
const auto now = std::chrono::system_clock::now();
if(i->second.start_time() + session_lifetime() < now)
{
m_sessions.erase(i);
return false;
}
session = i->second;
return true;
}
bool Session_Manager_In_Memory::load_from_session_id(
const std::vector<byte>& session_id, Session& session)
{
std::lock_guard<std::mutex> lock(m_mutex);
return load_from_session_str(hex_encode(session_id), session);
}
bool Session_Manager_In_Memory::load_from_host_info(
const std::string& hostname, u16bit port, Session& session)
{
std::lock_guard<std::mutex> lock(m_mutex);
auto i = m_host_sessions.find(hostname + ":" + std::to_string(port));
if(i == m_host_sessions.end())
{
if(port > 0)
i = m_host_sessions.find(hostname + ":" + std::to_string(0));
if(i == m_host_sessions.end())
return false;
}
if(load_from_session_str(i->second, session))
return true;
// was removed from sessions map, remove m_host_sessions entry
m_host_sessions.erase(i);
return false;
}
void Session_Manager_In_Memory::remove_entry(
const std::vector<byte>& session_id)
{
std::lock_guard<std::mutex> lock(m_mutex);
auto i = m_sessions.find(hex_encode(session_id));
if(i != m_sessions.end())
m_sessions.erase(i);
}
void Session_Manager_In_Memory::save(const Session& session, u16bit port)
{
std::lock_guard<std::mutex> lock(m_mutex);
if(m_max_sessions != 0)
{
/*
We generate new session IDs with the first 4 bytes being a
timestamp, so this actually removes the oldest sessions first.
*/
while(m_sessions.size() >= m_max_sessions)
m_sessions.erase(m_sessions.begin());
}
const std::string session_id_str = hex_encode(session.session_id());
m_sessions[session_id_str] = session;
const std::string hostname = session.sni_hostname();
if(session.side() == CLIENT && hostname != "")
m_host_sessions[hostname + ":" + std::to_string(port)] = session_id_str;
}
}
}
|