Botan: src/ssl/tls_reader.h Source File

Botan 1.10.17

tls_reader.h
Go to the documentation of this file.
1 /*
2 * TLS Data Reader
3 * (C) 2010 Jack Lloyd
4 *
5 * Released under the terms of the Botan license
6 */
7
8 #ifndef BOTAN_TLS_READER_H__
9 #define BOTAN_TLS_READER_H__
10
11 #include <botan/secmem.h>
12 #include <botan/loadstor.h>
13
14 namespace Botan {
15
16 /**
17 * Helper class for decoding TLS protocol messages
18 */
19 class TLS_Data_Reader
20 {
21 public:
22 TLS_Data_Reader(const MemoryRegion<byte>& buf_in) :
23 buf(buf_in), offset(0) {}
24
25 size_t remaining_bytes() const
26 {
27 return buf.size() - offset;
28 }
29
30 bool has_remaining() const
31 {
32 return (remaining_bytes() > 0);
33 }
34
35 void discard_next(size_t bytes)
36 {
37 assert_at_least(bytes);
38 offset += bytes;
39 }
40
41 u16bit get_u16bit()
42 {
43 assert_at_least(2);
44 u16bit result = make_u16bit(buf[offset], buf[offset+1]);
45 offset += 2;
46 return result;
47 }
48
49 byte get_byte()
50 {
51 assert_at_least(1);
52 byte result = buf[offset];
53 offset += 1;
54 return result;
55 }
56
57 template<typename T, typename Container>
58 Container get_elem(size_t num_elems)
59 {
60 assert_at_least(num_elems * sizeof(T));
61
62 Container result(num_elems);
63
64 for(size_t i = 0; i != num_elems; ++i)
65 result[i] = load_be<T>(&buf[offset], i);
66
67 offset += num_elems * sizeof(T);
68
69 return result;
70 }
71
72 template<typename T>
73 SecureVector<T> get_range(size_t len_bytes,
74 size_t min_elems,
75 size_t max_elems)
76 {
77 const size_t num_elems =
78 get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
79
80 return get_elem<T, SecureVector<T> >(num_elems);
81 }
82
83 template<typename T>
84 std::vector<T> get_range_vector(size_t len_bytes,
85 size_t min_elems,
86 size_t max_elems)
87 {
88 const size_t num_elems =
89 get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
90
91 return get_elem<T, std::vector<T> >(num_elems);
92 }
93
94 template<typename T>
95 SecureVector<T> get_fixed(size_t size)
96 {
97 return get_elem<T, SecureVector<T> >(size);
98 }
99
100 private:
101 size_t get_length_field(size_t len_bytes)
102 {
103 assert_at_least(len_bytes);
104
105 if(len_bytes == 1)
106 return get_byte();
107 else if(len_bytes == 2)
108 return get_u16bit();
109
110 throw Decoding_Error("TLS_Data_Reader: Bad length size");
111 }
112
113 size_t get_num_elems(size_t len_bytes,
114 size_t T_size,
115 size_t min_elems,
116 size_t max_elems)
117 {
118 const size_t byte_length = get_length_field(len_bytes);
119
120 if(byte_length % T_size != 0)
121 throw Decoding_Error("TLS_Data_Reader: Size isn't multiple of T");
122
123 const size_t num_elems = byte_length / T_size;
124
125 if(num_elems < min_elems || num_elems > max_elems)
126 throw Decoding_Error("TLS_Data_Reader: Range outside paramaters");
127
128 return num_elems;
129 }
130
131 void assert_at_least(size_t n) const
132 {
133 if(buf.size() - offset < n)
134 throw Decoding_Error("TLS_Data_Reader: Corrupt packet");
135 }
136
137 const MemoryRegion<byte>& buf;
138 size_t offset;
139 };
140
141 /**
142 * Helper function for encoding length-tagged vectors
143 */
144 template<typename T>
145 void append_tls_length_value(MemoryRegion<byte>& buf,
146 const T* vals,
147 size_t vals_size,
148 size_t tag_size)
149 {
150 const size_t T_size = sizeof(T);
151 const size_t val_bytes = T_size * vals_size;
152
153 if(tag_size != 1 && tag_size != 2)
154 throw std::invalid_argument("append_tls_length_value: invalid tag size");
155
156 if((tag_size == 1 && val_bytes > 255) ||
157 (tag_size == 2 && val_bytes > 65535))
158 throw std::invalid_argument("append_tls_length_value: value too large");
159
160 for(size_t i = 0; i != tag_size; ++i)
161 buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes));
162
163 for(size_t i = 0; i != vals_size; ++i)
164 for(size_t j = 0; j != T_size; ++j)
165 buf.push_back(get_byte(j, vals[i]));
166 }
167
168 template<typename T>
169 void append_tls_length_value(MemoryRegion<byte>& buf,
170 const MemoryRegion<T>& vals,
171 size_t tag_size)
172 {
173 append_tls_length_value(buf, &vals[0], vals.size(), tag_size);
174 }
175
176 template<typename T>
177 void append_tls_length_value(MemoryRegion<byte>& buf,
178 const std::vector<T>& vals,
179 size_t tag_size)
180 {
181 append_tls_length_value(buf, &vals[0], vals.size(), tag_size);
182 }
183
184 }
185
186 #endif
Botan::append_tls_length_value
void append_tls_length_value(MemoryRegion< byte > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition: tls_reader.h:145
Botan::TLS_Data_Reader::has_remaining
bool has_remaining() const
Definition: tls_reader.h:30
Botan::MemoryRegion::push_back
void push_back(T x)
Definition: secmem.h:149
Botan::TLS_Data_Reader::discard_next
void discard_next(size_t bytes)
Definition: tls_reader.h:35
Botan::get_byte
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::TLS_Data_Reader::get_range_vector
std::vector< T > get_range_vector(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition: tls_reader.h:84
Botan::TLS_Data_Reader::remaining_bytes
size_t remaining_bytes() const
Definition: tls_reader.h:25
Botan::u16bit
unsigned short u16bit
Definition: types.h:27
Botan::TLS_Data_Reader
Definition: tls_reader.h:19
Botan::Decoding_Error
Definition: exceptn.h:138
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::TLS_Data_Reader::TLS_Data_Reader
TLS_Data_Reader(const MemoryRegion< byte > &buf_in)
Definition: tls_reader.h:22
Botan::TLS_Data_Reader::get_fixed
SecureVector< T > get_fixed(size_t size)
Definition: tls_reader.h:95
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::TLS_Data_Reader::get_byte
byte get_byte()
Definition: tls_reader.h:49
Botan::make_u16bit
u16bit make_u16bit(byte i0, byte i1)
Definition: loadstor.h:47
Botan::SecureVector
Definition: secmem.h:328
Botan::TLS_Data_Reader::get_elem
Container get_elem(size_t num_elems)
Definition: tls_reader.h:58
Botan::TLS_Data_Reader::get_range
SecureVector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition: tls_reader.h:73
Botan::TLS_Data_Reader::get_u16bit
u16bit get_u16bit()
Definition: tls_reader.h:41