23 auto now = std::chrono::system_clock::now();
29 auto now = std::chrono::system_clock::now();
30 auto age = std::chrono::duration_cast<std::chrono::milliseconds>(
34 uint32_t age_ms =
static_cast<uint32_t
>(age.count());
46 std::lock_guard<std::mutex> lock(mutex_);
47 auto key = make_key(server, port);
48 tickets_[key] = ticket;
52 unsigned short port)
const
53 -> std::optional<session_ticket_info>
55 std::lock_guard<std::mutex> lock(mutex_);
56 auto key = make_key(server, port);
58 auto it = tickets_.find(key);
59 if (it == tickets_.end())
65 if (!it->second.is_valid())
74 unsigned short port) ->
bool
76 std::lock_guard<std::mutex> lock(mutex_);
77 auto key = make_key(server, port);
78 return tickets_.erase(key) > 0;
83 std::lock_guard<std::mutex> lock(mutex_);
86 auto now = std::chrono::system_clock::now();
88 for (
auto it = tickets_.begin(); it != tickets_.end();)
90 if (it->second.expiry <= now)
92 it = tickets_.erase(it);
106 std::lock_guard<std::mutex> lock(mutex_);
112 std::lock_guard<std::mutex> lock(
mutex_);
117 unsigned short port)
const ->
bool
119 std::lock_guard<std::mutex> lock(mutex_);
120 auto key = make_key(server, port);
122 auto it = tickets_.find(key);
123 if (it == tickets_.end())
128 return it->second.is_valid();
132 unsigned short port) -> std::string
134 return server +
":" + std::to_string(port);
152 std::span<const uint8_t> nonce,
153 std::chrono::system_clock::time_point timestamp) ->
bool
155 std::lock_guard<std::mutex> lock(mutex_);
158 auto window_start = timestamp - config_.window_size;
161 std::remove_if(entries_.begin(), entries_.end(),
163 return entry.timestamp < window_start;
168 std::vector<uint8_t> nonce_vec(nonce.begin(), nonce.end());
169 for (
const auto& entry : entries_)
171 if (entry.
nonce == nonce_vec)
179 if (entries_.size() >= config_.max_entries)
182 std::sort(entries_.begin(), entries_.end(),
184 return a.timestamp < b.timestamp;
187 size_t to_remove = entries_.size() / 4;
188 entries_.erase(entries_.begin(),
189 entries_.begin() +
static_cast<ptrdiff_t
>(to_remove));
193 entries_.push_back({std::move(nonce_vec), timestamp});
200 std::lock_guard<std::mutex> lock(mutex_);
202 auto window_start = now - config_.window_size;
203 size_t original_size = entries_.size();
206 std::remove_if(entries_.begin(), entries_.end(),
208 return entry.timestamp < window_start;
212 return original_size - entries_.size();
217 std::lock_guard<std::mutex> lock(mutex_);
223 std::lock_guard<std::mutex> lock(
mutex_);
auto check_and_record(std::span< const uint8_t > nonce, std::chrono::system_clock::time_point timestamp=std::chrono::system_clock::now()) -> bool
Check if data should be accepted (not a replay)
auto cleanup(std::chrono::system_clock::time_point now=std::chrono::system_clock::now()) -> size_t
Remove old entries outside the window.
replay_filter()
Construct a replay filter with default configuration.
auto size() const -> size_t
Get the number of tracked nonces.
std::vector< nonce_entry > entries_
auto clear() -> void
Clear all recorded nonces.
auto clear() -> void
Clear all stored tickets.
auto cleanup_expired() -> size_t
Remove all expired tickets from the store.
auto remove(const std::string &server, unsigned short port) -> bool
Remove a session ticket for a server.
auto size() const -> size_t
Get the number of stored tickets.
auto has_ticket(const std::string &server, unsigned short port) const -> bool
Check if a valid ticket exists for a server.
static auto make_key(const std::string &server, unsigned short port) -> std::string
Generate a key for the ticket map.
std::mutex mutex_
Thread-safety mutex.
std::unordered_map< std::string, session_ticket_info > tickets_
Ticket storage (key: "server:port")
auto retrieve(const std::string &server, unsigned short port) const -> std::optional< session_ticket_info >
Retrieve a session ticket for a server.
auto store(const std::string &server, unsigned short port, const session_ticket_info &ticket) -> void
Store a session ticket for a server.
Configuration for the replay filter.
std::vector< uint8_t > nonce
Contains session ticket data for 0-RTT resumption.
std::chrono::system_clock::time_point received_time
Time when the ticket was received.
std::chrono::system_clock::time_point expiry
Ticket expiration time.
auto is_valid() const noexcept -> bool
Check if the ticket is still valid (not expired)
auto get_obfuscated_age() const noexcept -> uint32_t
Get obfuscated ticket age (RFC 8446 Section 4.2.11.1)
uint32_t ticket_age_add
Ticket age add value for obfuscation (RFC 8446)
std::vector< uint8_t > ticket_data
Raw session ticket data from TLS 1.3 NewSessionTicket.