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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* TLS Data Reader
* (C) 2010-2011,2014 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#ifndef BOTAN_TLS_READER_H__
#define BOTAN_TLS_READER_H__
#include <botan/exceptn.h>
#include <botan/secmem.h>
#include <botan/loadstor.h>
#include <string>
#include <vector>
#include <stdexcept>
namespace Botan {
namespace TLS {
/**
* Helper class for decoding TLS protocol messages
*/
class TLS_Data_Reader
{
public:
TLS_Data_Reader(const char* type, const std::vector<byte>& buf_in) :
m_typename(type), m_buf(buf_in), m_offset(0) {}
void assert_done() const
{
if(has_remaining())
throw decode_error("Extra bytes at end of message");
}
size_t read_so_far() const { return m_offset; }
size_t remaining_bytes() const { return m_buf.size() - m_offset; }
bool has_remaining() const { return (remaining_bytes() > 0); }
std::vector<byte> get_remaining()
{
return std::vector<byte>(m_buf.begin() + m_offset, m_buf.end());
}
void discard_next(size_t bytes)
{
assert_at_least(bytes);
m_offset += bytes;
}
u16bit get_u32bit()
{
assert_at_least(4);
u16bit result = make_u32bit(m_buf[m_offset ], m_buf[m_offset+1],
m_buf[m_offset+2], m_buf[m_offset+3]);
m_offset += 4;
return result;
}
u16bit get_u16bit()
{
assert_at_least(2);
u16bit result = make_u16bit(m_buf[m_offset], m_buf[m_offset+1]);
m_offset += 2;
return result;
}
byte get_byte()
{
assert_at_least(1);
byte result = m_buf[m_offset];
m_offset += 1;
return result;
}
template<typename T, typename Container>
Container get_elem(size_t num_elems)
{
assert_at_least(num_elems * sizeof(T));
Container result(num_elems);
for(size_t i = 0; i != num_elems; ++i)
result[i] = load_be<T>(&m_buf[m_offset], i);
m_offset += num_elems * sizeof(T);
return result;
}
template<typename T>
std::vector<T> get_range(size_t len_bytes,
size_t min_elems,
size_t max_elems)
{
const size_t num_elems =
get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
return get_elem<T, std::vector<T> >(num_elems);
}
template<typename T>
std::vector<T> get_range_vector(size_t len_bytes,
size_t min_elems,
size_t max_elems)
{
const size_t num_elems =
get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
return get_elem<T, std::vector<T> >(num_elems);
}
std::string get_string(size_t len_bytes,
size_t min_bytes,
size_t max_bytes)
{
std::vector<byte> v =
get_range_vector<byte>(len_bytes, min_bytes, max_bytes);
return std::string(reinterpret_cast<char*>(v.data()), v.size());
}
template<typename T>
std::vector<T> get_fixed(size_t size)
{
return get_elem<T, std::vector<T> >(size);
}
private:
size_t get_length_field(size_t len_bytes)
{
assert_at_least(len_bytes);
if(len_bytes == 1)
return get_byte();
else if(len_bytes == 2)
return get_u16bit();
throw decode_error("Bad length size");
}
size_t get_num_elems(size_t len_bytes,
size_t T_size,
size_t min_elems,
size_t max_elems)
{
const size_t byte_length = get_length_field(len_bytes);
if(byte_length % T_size != 0)
throw decode_error("Size isn't multiple of T");
const size_t num_elems = byte_length / T_size;
if(num_elems < min_elems || num_elems > max_elems)
throw decode_error("Length field outside parameters");
return num_elems;
}
void assert_at_least(size_t n) const
{
if(m_buf.size() - m_offset < n)
throw decode_error("Expected " + std::to_string(n) +
" bytes remaining, only " +
std::to_string(m_buf.size()-m_offset) +
" left");
}
Decoding_Error decode_error(const std::string& why) const
{
return Decoding_Error("Invalid " + std::string(m_typename) + ": " + why);
}
const char* m_typename;
const std::vector<byte>& m_buf;
size_t m_offset;
};
/**
* Helper function for encoding length-tagged vectors
*/
template<typename T, typename Alloc>
void append_tls_length_value(std::vector<byte, Alloc>& buf,
const T* vals,
size_t vals_size,
size_t tag_size)
{
const size_t T_size = sizeof(T);
const size_t val_bytes = T_size * vals_size;
if(tag_size != 1 && tag_size != 2)
throw Invalid_Argument("append_tls_length_value: invalid tag size");
if((tag_size == 1 && val_bytes > 255) ||
(tag_size == 2 && val_bytes > 65535))
throw Invalid_Argument("append_tls_length_value: value too large");
for(size_t i = 0; i != tag_size; ++i)
buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes));
for(size_t i = 0; i != vals_size; ++i)
for(size_t j = 0; j != T_size; ++j)
buf.push_back(get_byte(j, vals[i]));
}
template<typename T, typename Alloc, typename Alloc2>
void append_tls_length_value(std::vector<byte, Alloc>& buf,
const std::vector<T, Alloc2>& vals,
size_t tag_size)
{
append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
}
template<typename Alloc>
void append_tls_length_value(std::vector<byte, Alloc>& buf,
const std::string& str,
size_t tag_size)
{
append_tls_length_value(buf,
reinterpret_cast<const byte*>(str.data()),
str.size(),
tag_size);
}
}
}
#endif
|