Botan 3.12.0
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_seq_no = 0;
50 m_read_epoch++;
51 }
52
53 void new_write_cipher_state() override {
54 m_write_seq_no = 0;
55 m_write_epoch++;
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 uint64_t m_write_seq_no;
82 uint64_t m_read_seq_no;
83 uint16_t m_read_epoch;
84 uint16_t m_write_epoch;
85};
86
88 public:
90
91 void reset() override {
92 m_write_seqs.clear();
93 m_write_seqs[0] = 0;
94 m_write_epoch = 0;
95 m_read_epoch = 0;
96 m_window_highest = 0;
97 m_window_bits = 0;
98 }
99
100 void new_read_cipher_state() override { m_read_epoch++; }
101
102 void new_write_cipher_state() override {
103 m_write_epoch++;
104 m_write_seqs[m_write_epoch] = 0;
105 }
106
107 uint16_t current_read_epoch() const override { return m_read_epoch; }
108
109 uint16_t current_write_epoch() const override { return m_write_epoch; }
110
111 uint64_t next_write_sequence(uint16_t epoch) override {
112 auto i = m_write_seqs.find(epoch);
113 BOTAN_ASSERT(i != m_write_seqs.end(), "Found epoch");
114 if(i->second > 0x0000FFFFFFFFFFFF) {
115 throw Invalid_State("DTLS write sequence number overflow");
116 }
117 return (static_cast<uint64_t>(epoch) << 48) | i->second++;
118 }
119
120 uint64_t next_read_sequence() override { throw Invalid_State("DTLS uses explicit sequence numbers"); }
121
122 bool already_seen(uint64_t sequence) const override {
123 const size_t window_size = sizeof(m_window_bits) * 8;
124
125 if(sequence > m_window_highest) {
126 return false;
127 }
128
129 const uint64_t offset = m_window_highest - sequence;
130
131 if(offset >= window_size) {
132 return true; // really old?
133 }
134
135 return (((m_window_bits >> offset) & 1) == 1);
136 }
137
138 void read_accept(uint64_t sequence) override {
139 const size_t window_size = sizeof(m_window_bits) * 8;
140
141 if(sequence > m_window_highest) {
142 // We've received a later sequence which advances our window
143 const uint64_t offset = sequence - m_window_highest;
144 m_window_highest += offset;
145
146 if(offset >= window_size) {
147 m_window_bits = 0;
148 } else {
149 m_window_bits <<= offset;
150 }
151
152 m_window_bits |= 0x01;
153 } else {
154 const uint64_t offset = m_window_highest - sequence;
155
156 if(offset < window_size) {
157 // We've received an old sequence but still within our window
158 m_window_bits |= (static_cast<uint64_t>(1) << offset);
159 } else {
160 // This occurs only if we have reset state (DTLS reconnection case)
161 m_window_highest = sequence;
162 m_window_bits = 0;
163 }
164 }
165 }
166
167 private:
168 std::map<uint16_t, uint64_t> m_write_seqs;
169 uint16_t m_write_epoch = 0;
170 uint16_t m_read_epoch = 0;
171 uint64_t m_window_highest = 0;
172 uint64_t m_window_bits = 0;
173};
174
175} // namespace Botan::TLS
176
177#endif
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
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
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