Botan 3.14.1
Crypto and TLS for C&
tls_seq_numbers.h
Go to the documentation of this file.
1/*
2* TLS Sequence Number Handling
3* (C) 2012 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_TLS_SEQ_NUMBERS_H_
9#define BOTAN_TLS_SEQ_NUMBERS_H_
10
11#include <botan/assert.h>
12#include <botan/exceptn.h>
13#include <limits>
14#include <map>
15
16namespace Botan::TLS {
17
18class Connection_Sequence_Numbers /* NOLINT(*-special-member-functions) */ {
19 public:
20 virtual ~Connection_Sequence_Numbers() = default;
21
22 virtual void new_read_cipher_state() = 0;
23 virtual void new_write_cipher_state() = 0;
24
25 virtual uint16_t current_read_epoch() const = 0;
26 virtual uint16_t current_write_epoch() const = 0;
27
28 virtual uint64_t next_write_sequence(uint16_t) = 0;
29 virtual uint64_t next_read_sequence() = 0;
30
31 virtual bool already_seen(uint64_t seq) const = 0;
32 virtual void read_accept(uint64_t seq) = 0;
33
34 virtual void reset() = 0;
35};
36
38 public:
39 Stream_Sequence_Numbers() : m_write_seq_no(0), m_read_seq_no(0), m_read_epoch(0), m_write_epoch(0) {}
40
41 void reset() override {
42 m_write_seq_no = 0;
43 m_read_seq_no = 0;
44 m_read_epoch = 0;
45 m_write_epoch = 0;
46 }
47
48 void new_read_cipher_state() override {
49 m_read_epoch = next_epoch(m_read_epoch);
50 m_read_seq_no = 0;
51 }
52
53 void new_write_cipher_state() override {
54 m_write_epoch = next_epoch(m_write_epoch);
55 m_write_seq_no = 0;
56 }
57
58 uint16_t current_read_epoch() const override { return m_read_epoch; }
59
60 uint16_t current_write_epoch() const override { return m_write_epoch; }
61
62 uint64_t next_write_sequence(uint16_t /*epoch*/) override {
63 if(m_write_seq_no == std::numeric_limits<uint64_t>::max()) {
64 throw Invalid_State("TLS 1.2 write sequence number overflow");
65 }
66 return m_write_seq_no++;
67 }
68
69 uint64_t next_read_sequence() override { return m_read_seq_no; }
70
71 bool already_seen(uint64_t /*seq*/) const override { return false; }
72
73 void read_accept(uint64_t /*seq*/) override {
74 if(m_read_seq_no == std::numeric_limits<uint64_t>::max()) {
75 throw Invalid_State("TLS 1.2 read sequence number overflow");
76 }
77 m_read_seq_no++;
78 }
79
80 private:
81 // Epoch 0 denotes the initial, unprotected state
82 static uint16_t next_epoch(uint16_t epoch) {
83 if(epoch == std::numeric_limits<uint16_t>::max()) {
84 throw Invalid_State("TLS epoch counter exhausted");
85 }
86 return static_cast<uint16_t>(epoch + 1);
87 }
88
89 uint64_t m_write_seq_no;
90 uint64_t m_read_seq_no;
91 uint16_t m_read_epoch;
92 uint16_t m_write_epoch;
93};
94
96 public:
97 explicit Datagram_Sequence_Numbers(uint64_t initial_read_sequence_number = 0,
98 uint64_t initial_write_sequence_number = 0) {
100
101 const auto is_epoch0 = ((initial_read_sequence_number >> 48) + (initial_write_sequence_number >> 48)) == 0;
102 BOTAN_ARG_CHECK(is_epoch0, "Initial sequence numbers must be in epoch 0");
103
104 m_write_seqs[0] = initial_write_sequence_number;
105 }
106
107 void reset() override {
108 m_write_seqs.clear();
109 m_write_seqs[0] = 0;
110 m_write_epoch = 0;
111 m_read_epoch = 0;
112 m_read_windows.clear();
113 m_read_windows[0] = Replay_Window{};
114 }
115
116 void new_read_cipher_state() override {
117 m_read_epoch = next_epoch(m_read_epoch);
118 m_read_windows.try_emplace(m_read_epoch);
119 prune_epochs(m_read_windows, m_read_epoch);
120 }
121
122 void new_write_cipher_state() override {
123 m_write_epoch = next_epoch(m_write_epoch);
124 m_write_seqs[m_write_epoch] = 0;
125 prune_epochs(m_write_seqs, m_write_epoch);
126 }
127
128 uint16_t current_read_epoch() const override { return m_read_epoch; }
129
130 uint16_t current_write_epoch() const override { return m_write_epoch; }
131
132 uint64_t next_write_sequence(uint16_t epoch) override {
133 const auto i = m_write_seqs.find(epoch);
134 if(i == m_write_seqs.end()) {
135 throw Invalid_State("DTLS epoch not found");
136 }
137 if(i->second > 0x0000FFFFFFFFFFFF) {
138 throw Invalid_State("DTLS write sequence number overflow");
139 }
140 return (static_cast<uint64_t>(epoch) << 48) | i->second++;
141 }
142
143 uint64_t next_read_sequence() override { throw Invalid_State("DTLS uses explicit sequence numbers"); }
144
145 bool already_seen(uint64_t sequence) const override {
146 const uint16_t epoch = static_cast<uint16_t>(sequence >> 48);
147 const uint64_t record_sequence = sequence & 0x0000FFFFFFFFFFFF;
148 const auto window = m_read_windows.find(epoch);
149
150 if(window == m_read_windows.end()) {
151 return false;
152 }
153
154 const size_t window_size = sizeof(window->second.bits) * 8;
155
156 if(record_sequence > window->second.highest) {
157 return false;
158 }
159
160 const uint64_t offset = window->second.highest - record_sequence;
161
162 if(offset >= window_size) {
163 return true; // really old?
164 }
165
166 return (((window->second.bits >> offset) & 1) == 1);
167 }
168
169 void read_accept(uint64_t sequence) override {
170 const uint16_t epoch = static_cast<uint16_t>(sequence >> 48);
171 const uint64_t record_sequence = sequence & 0x0000FFFFFFFFFFFF;
172 auto& window = m_read_windows[epoch];
173 const size_t window_size = sizeof(window.bits) * 8;
174
175 if(record_sequence > window.highest) {
176 // We've received a later sequence which advances our window
177 const uint64_t offset = record_sequence - window.highest;
178 window.highest += offset;
179
180 if(offset >= window_size) {
181 window.bits = 0;
182 } else {
183 window.bits <<= offset;
184 }
185
186 window.bits |= 0x01;
187 } else {
188 const uint64_t offset = window.highest - record_sequence;
189
190 if(offset < window_size) {
191 // We've received an old sequence but still within our window
192 window.bits |= (static_cast<uint64_t>(1) << offset);
193 } else {
194 // DTLS reconnection: recenter the window on this sequence. Bit 0
195 // marks the sequence itself as seen so an immediate replay is
196 // detected; the other branches above set this implicitly.
197 window.highest = record_sequence;
198 window.bits = 1;
199 }
200 }
201 }
202
203 private:
204 struct Replay_Window final {
205 uint64_t highest = 0;
206 uint64_t bits = 0;
207 };
208
209 /*
210 * RFC 6347 4.1: "Similarly, implementations MUST NOT allow the epoch to
211 * wrap, but instead MUST establish a new association, terminating the old
212 * association as described in Section 4.2.8."
213 *
214 * Throwing leaves the counters untouched; the channel turns it into a
215 * fatal alert, which is the termination the requirement asks for.
216 */
217 static uint16_t next_epoch(uint16_t epoch) {
218 if(epoch == std::numeric_limits<uint16_t>::max()) {
219 throw Invalid_State("DTLS epoch counter exhausted");
220 }
221 return static_cast<uint16_t>(epoch + 1);
222 }
223
224 /*
225 * Only the current epoch and the one it replaced can still carry traffic;
226 *
227 * The behavior of this function must match the value of TLS_RETAINED_CIPHERSTATES
228 * in tls_channel_impl_12.cpp
229 *
230 * Epoch 0 is exempt: a HelloVerifyRequest is written under it
231 * however many times the association has rekeyed.
232 */
233 template <typename T>
234 static void prune_epochs(std::map<uint16_t, T>& epochs, uint16_t current) {
235 for(auto i = epochs.begin(); i != epochs.end();) {
236 if(i->first == 0 || i->first + 1 >= current) {
237 ++i;
238 } else {
239 i = epochs.erase(i);
240 }
241 }
242 }
243
244 std::map<uint16_t, uint64_t> m_write_seqs;
245 std::map<uint16_t, Replay_Window> m_read_windows;
246 uint16_t m_write_epoch = 0;
247 uint16_t m_read_epoch = 0;
248};
249
250} // namespace Botan::TLS
251
252#endif
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
virtual uint16_t current_read_epoch() const =0
virtual ~Connection_Sequence_Numbers()=default
virtual void read_accept(uint64_t seq)=0
virtual uint16_t current_write_epoch() const =0
virtual bool already_seen(uint64_t seq) const =0
virtual uint64_t next_write_sequence(uint16_t)=0
Datagram_Sequence_Numbers(uint64_t initial_read_sequence_number=0, uint64_t initial_write_sequence_number=0)
void read_accept(uint64_t sequence) override
uint16_t current_read_epoch() const override
uint16_t current_write_epoch() const override
uint64_t next_write_sequence(uint16_t epoch) override
bool already_seen(uint64_t sequence) const override
uint64_t next_write_sequence(uint16_t) override
void read_accept(uint64_t) override
uint16_t current_read_epoch() const override
bool already_seen(uint64_t) const override
uint16_t current_write_epoch() const override