/src/botan/build/include/botan/internal/tls_reader.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * TLS Data Reader |
3 | | * (C) 2010-2011,2014 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_TLS_READER_H_ |
9 | | #define BOTAN_TLS_READER_H_ |
10 | | |
11 | | #include <botan/exceptn.h> |
12 | | #include <botan/secmem.h> |
13 | | #include <botan/loadstor.h> |
14 | | #include <string> |
15 | | #include <vector> |
16 | | |
17 | | namespace Botan { |
18 | | |
19 | | namespace TLS { |
20 | | |
21 | | /** |
22 | | * Helper class for decoding TLS protocol messages |
23 | | */ |
24 | | class TLS_Data_Reader final |
25 | | { |
26 | | public: |
27 | | TLS_Data_Reader(const char* type, const std::vector<uint8_t>& buf_in) : |
28 | 55.0k | m_typename(type), m_buf(buf_in), m_offset(0) {} |
29 | | |
30 | | void assert_done() const |
31 | 14.8k | { |
32 | 14.8k | if(has_remaining()) |
33 | 148 | throw decode_error("Extra bytes at end of message"); |
34 | 14.8k | } |
35 | | |
36 | 1.11k | size_t read_so_far() const { return m_offset; } |
37 | | |
38 | 333k | size_t remaining_bytes() const { return m_buf.size() - m_offset; } |
39 | | |
40 | 296k | bool has_remaining() const { return (remaining_bytes() > 0); } |
41 | | |
42 | | std::vector<uint8_t> get_remaining() |
43 | 0 | { |
44 | 0 | return std::vector<uint8_t>(m_buf.begin() + m_offset, m_buf.end()); |
45 | 0 | } |
46 | | |
47 | | void discard_next(size_t bytes) |
48 | 1.30k | { |
49 | 1.30k | assert_at_least(bytes); |
50 | 1.30k | m_offset += bytes; |
51 | 1.30k | } |
52 | | |
53 | | uint32_t get_uint32_t() |
54 | 5 | { |
55 | 5 | assert_at_least(4); |
56 | 5 | uint32_t result = make_uint32(m_buf[m_offset ], m_buf[m_offset+1], |
57 | 5 | m_buf[m_offset+2], m_buf[m_offset+3]); |
58 | 5 | m_offset += 4; |
59 | 5 | return result; |
60 | 5 | } |
61 | | |
62 | | uint16_t get_uint16_t() |
63 | 681k | { |
64 | 681k | assert_at_least(2); |
65 | 681k | uint16_t result = make_uint16(m_buf[m_offset], m_buf[m_offset+1]); |
66 | 681k | m_offset += 2; |
67 | 681k | return result; |
68 | 681k | } |
69 | | |
70 | | uint8_t get_byte() |
71 | 218k | { |
72 | 218k | assert_at_least(1); |
73 | 218k | uint8_t result = m_buf[m_offset]; |
74 | 218k | m_offset += 1; |
75 | 218k | return result; |
76 | 218k | } |
77 | | |
78 | | template<typename T, typename Container> |
79 | | Container get_elem(size_t num_elems) |
80 | 365k | { |
81 | 365k | assert_at_least(num_elems * sizeof(T)); |
82 | 365k | |
83 | 365k | Container result(num_elems); |
84 | 365k | |
85 | 3.17M | for(size_t i = 0; i != num_elems; ++i) |
86 | 2.80M | result[i] = load_be<T>(&m_buf[m_offset], i); |
87 | 365k | |
88 | 365k | m_offset += num_elems * sizeof(T); |
89 | 365k | |
90 | 365k | return result; |
91 | 365k | } std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > Botan::TLS::TLS_Data_Reader::get_elem<unsigned char, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > >(unsigned long) Line | Count | Source | 80 | 332k | { | 81 | 332k | assert_at_least(num_elems * sizeof(T)); | 82 | 332k | | 83 | 332k | Container result(num_elems); | 84 | 332k | | 85 | 3.05M | for(size_t i = 0; i != num_elems; ++i) | 86 | 2.72M | result[i] = load_be<T>(&m_buf[m_offset], i); | 87 | 332k | | 88 | 332k | m_offset += num_elems * sizeof(T); | 89 | 332k | | 90 | 332k | return result; | 91 | 332k | } |
std::__1::vector<unsigned short, std::__1::allocator<unsigned short> > Botan::TLS::TLS_Data_Reader::get_elem<unsigned short, std::__1::vector<unsigned short, std::__1::allocator<unsigned short> > >(unsigned long) Line | Count | Source | 80 | 33.7k | { | 81 | 33.7k | assert_at_least(num_elems * sizeof(T)); | 82 | 33.7k | | 83 | 33.7k | Container result(num_elems); | 84 | 33.7k | | 85 | 117k | for(size_t i = 0; i != num_elems; ++i) | 86 | 83.8k | result[i] = load_be<T>(&m_buf[m_offset], i); | 87 | 33.7k | | 88 | 33.7k | m_offset += num_elems * sizeof(T); | 89 | 33.7k | | 90 | 33.7k | return result; | 91 | 33.7k | } |
|
92 | | |
93 | | template<typename T> |
94 | | std::vector<T> get_range(size_t len_bytes, |
95 | | size_t min_elems, |
96 | | size_t max_elems) |
97 | 67.4k | { |
98 | 67.4k | const size_t num_elems = |
99 | 67.4k | get_num_elems(len_bytes, sizeof(T), min_elems, max_elems); |
100 | 67.4k | |
101 | 67.4k | return get_elem<T, std::vector<T>>(num_elems); |
102 | 67.4k | } std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > Botan::TLS::TLS_Data_Reader::get_range<unsigned char>(unsigned long, unsigned long, unsigned long) Line | Count | Source | 97 | 67.2k | { | 98 | 67.2k | const size_t num_elems = | 99 | 67.2k | get_num_elems(len_bytes, sizeof(T), min_elems, max_elems); | 100 | 67.2k | | 101 | 67.2k | return get_elem<T, std::vector<T>>(num_elems); | 102 | 67.2k | } |
std::__1::vector<unsigned short, std::__1::allocator<unsigned short> > Botan::TLS::TLS_Data_Reader::get_range<unsigned short>(unsigned long, unsigned long, unsigned long) Line | Count | Source | 97 | 233 | { | 98 | 233 | const size_t num_elems = | 99 | 233 | get_num_elems(len_bytes, sizeof(T), min_elems, max_elems); | 100 | 233 | | 101 | 233 | return get_elem<T, std::vector<T>>(num_elems); | 102 | 233 | } |
|
103 | | |
104 | | template<typename T> |
105 | | std::vector<T> get_range_vector(size_t len_bytes, |
106 | | size_t min_elems, |
107 | | size_t max_elems) |
108 | 113k | { |
109 | 113k | const size_t num_elems = |
110 | 113k | get_num_elems(len_bytes, sizeof(T), min_elems, max_elems); |
111 | 113k | |
112 | 113k | return get_elem<T, std::vector<T>>(num_elems); |
113 | 113k | } std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > Botan::TLS::TLS_Data_Reader::get_range_vector<unsigned char>(unsigned long, unsigned long, unsigned long) Line | Count | Source | 108 | 79.4k | { | 109 | 79.4k | const size_t num_elems = | 110 | 79.4k | get_num_elems(len_bytes, sizeof(T), min_elems, max_elems); | 111 | 79.4k | | 112 | 79.4k | return get_elem<T, std::vector<T>>(num_elems); | 113 | 79.4k | } |
std::__1::vector<unsigned short, std::__1::allocator<unsigned short> > Botan::TLS::TLS_Data_Reader::get_range_vector<unsigned short>(unsigned long, unsigned long, unsigned long) Line | Count | Source | 108 | 33.5k | { | 109 | 33.5k | const size_t num_elems = | 110 | 33.5k | get_num_elems(len_bytes, sizeof(T), min_elems, max_elems); | 111 | 33.5k | | 112 | 33.5k | return get_elem<T, std::vector<T>>(num_elems); | 113 | 33.5k | } |
|
114 | | |
115 | | std::string get_string(size_t len_bytes, |
116 | | size_t min_bytes, |
117 | | size_t max_bytes) |
118 | 45.1k | { |
119 | 45.1k | std::vector<uint8_t> v = |
120 | 45.1k | get_range_vector<uint8_t>(len_bytes, min_bytes, max_bytes); |
121 | 45.1k | |
122 | 45.1k | return std::string(cast_uint8_ptr_to_char(v.data()), v.size()); |
123 | 45.1k | } |
124 | | |
125 | | template<typename T> |
126 | | std::vector<T> get_fixed(size_t size) |
127 | 182k | { |
128 | 182k | return get_elem<T, std::vector<T>>(size); |
129 | 182k | } |
130 | | |
131 | | private: |
132 | | size_t get_length_field(size_t len_bytes) |
133 | 180k | { |
134 | 180k | assert_at_least(len_bytes); |
135 | 180k | |
136 | 180k | if(len_bytes == 1) |
137 | 127k | return get_byte(); |
138 | 53.2k | else if(len_bytes == 2) |
139 | 53.0k | return get_uint16_t(); |
140 | 262 | |
141 | 262 | throw decode_error("Bad length size"); |
142 | 262 | } |
143 | | |
144 | | size_t get_num_elems(size_t len_bytes, |
145 | | size_t T_size, |
146 | | size_t min_elems, |
147 | | size_t max_elems) |
148 | 180k | { |
149 | 180k | const size_t byte_length = get_length_field(len_bytes); |
150 | 180k | |
151 | 180k | if(byte_length % T_size != 0) |
152 | 18 | throw decode_error("Size isn't multiple of T"); |
153 | 180k | |
154 | 180k | const size_t num_elems = byte_length / T_size; |
155 | 180k | |
156 | 180k | if(num_elems < min_elems || num_elems > max_elems) |
157 | 121 | throw decode_error("Length field outside parameters"); |
158 | 180k | |
159 | 180k | return num_elems; |
160 | 180k | } |
161 | | |
162 | | void assert_at_least(size_t n) const |
163 | 1.44M | { |
164 | 1.44M | if(m_buf.size() - m_offset < n) |
165 | 1.22k | throw decode_error("Expected " + std::to_string(n) + |
166 | 1.22k | " bytes remaining, only " + |
167 | 1.22k | std::to_string(m_buf.size()-m_offset) + |
168 | 1.22k | " left"); |
169 | 1.44M | } |
170 | | |
171 | | Decoding_Error decode_error(const std::string& why) const |
172 | 1.51k | { |
173 | 1.51k | return Decoding_Error("Invalid " + std::string(m_typename) + ": " + why); |
174 | 1.51k | } |
175 | | |
176 | | const char* m_typename; |
177 | | const std::vector<uint8_t>& m_buf; |
178 | | size_t m_offset; |
179 | | }; |
180 | | |
181 | | /** |
182 | | * Helper function for encoding length-tagged vectors |
183 | | */ |
184 | | template<typename T, typename Alloc> |
185 | | void append_tls_length_value(std::vector<uint8_t, Alloc>& buf, |
186 | | const T* vals, |
187 | | size_t vals_size, |
188 | | size_t tag_size) |
189 | 165k | { |
190 | 165k | const size_t T_size = sizeof(T); |
191 | 165k | const size_t val_bytes = T_size * vals_size; |
192 | 165k | |
193 | 165k | if(tag_size != 1 && tag_size != 2) |
194 | 0 | throw Invalid_Argument("append_tls_length_value: invalid tag size"); |
195 | 165k | |
196 | 165k | if((tag_size == 1 && val_bytes > 255) || |
197 | 165k | (tag_size == 2 && val_bytes > 65535)) |
198 | 0 | throw Invalid_Argument("append_tls_length_value: value too large"); |
199 | 165k | |
200 | 409k | for(size_t i = 0; i != tag_size; ++i) |
201 | 243k | buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes)); |
202 | 165k | |
203 | 9.92M | for(size_t i = 0; i != vals_size; ++i) |
204 | 20.5M | for(size_t j = 0; j != T_size; ++j) |
205 | 10.8M | buf.push_back(get_byte(j, vals[i])); |
206 | 165k | } void Botan::TLS::append_tls_length_value<unsigned char, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, unsigned char const*, unsigned long, unsigned long) Line | Count | Source | 189 | 127k | { | 190 | 127k | const size_t T_size = sizeof(T); | 191 | 127k | const size_t val_bytes = T_size * vals_size; | 192 | 127k | | 193 | 127k | if(tag_size != 1 && tag_size != 2) | 194 | 0 | throw Invalid_Argument("append_tls_length_value: invalid tag size"); | 195 | 127k | | 196 | 127k | if((tag_size == 1 && val_bytes > 255) || | 197 | 127k | (tag_size == 2 && val_bytes > 65535)) | 198 | 0 | throw Invalid_Argument("append_tls_length_value: value too large"); | 199 | 127k | | 200 | 294k | for(size_t i = 0; i != tag_size; ++i) | 201 | 167k | buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes)); | 202 | 127k | | 203 | 7.50M | for(size_t i = 0; i != vals_size; ++i) | 204 | 14.7M | for(size_t j = 0; j != T_size; ++j) | 205 | 7.38M | buf.push_back(get_byte(j, vals[i])); | 206 | 127k | } |
void Botan::TLS::append_tls_length_value<unsigned short, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, unsigned short const*, unsigned long, unsigned long) Line | Count | Source | 189 | 12.7k | { | 190 | 12.7k | const size_t T_size = sizeof(T); | 191 | 12.7k | const size_t val_bytes = T_size * vals_size; | 192 | 12.7k | | 193 | 12.7k | if(tag_size != 1 && tag_size != 2) | 194 | 0 | throw Invalid_Argument("append_tls_length_value: invalid tag size"); | 195 | 12.7k | | 196 | 12.7k | if((tag_size == 1 && val_bytes > 255) || | 197 | 12.7k | (tag_size == 2 && val_bytes > 65535)) | 198 | 0 | throw Invalid_Argument("append_tls_length_value: value too large"); | 199 | 12.7k | | 200 | 38.3k | for(size_t i = 0; i != tag_size; ++i) | 201 | 25.5k | buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes)); | 202 | 12.7k | | 203 | 1.05M | for(size_t i = 0; i != vals_size; ++i) | 204 | 3.12M | for(size_t j = 0; j != T_size; ++j) | 205 | 2.08M | buf.push_back(get_byte(j, vals[i])); | 206 | 12.7k | } |
void Botan::TLS::append_tls_length_value<unsigned char, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned char const*, unsigned long, unsigned long) Line | Count | Source | 189 | 25.6k | { | 190 | 25.6k | const size_t T_size = sizeof(T); | 191 | 25.6k | const size_t val_bytes = T_size * vals_size; | 192 | 25.6k | | 193 | 25.6k | if(tag_size != 1 && tag_size != 2) | 194 | 0 | throw Invalid_Argument("append_tls_length_value: invalid tag size"); | 195 | 25.6k | | 196 | 25.6k | if((tag_size == 1 && val_bytes > 255) || | 197 | 25.6k | (tag_size == 2 && val_bytes > 65535)) | 198 | 0 | throw Invalid_Argument("append_tls_length_value: value too large"); | 199 | 25.6k | | 200 | 76.9k | for(size_t i = 0; i != tag_size; ++i) | 201 | 51.2k | buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes)); | 202 | 25.6k | | 203 | 1.36M | for(size_t i = 0; i != vals_size; ++i) | 204 | 2.67M | for(size_t j = 0; j != T_size; ++j) | 205 | 1.33M | buf.push_back(get_byte(j, vals[i])); | 206 | 25.6k | } |
|
207 | | |
208 | | template<typename T, typename Alloc, typename Alloc2> |
209 | | void append_tls_length_value(std::vector<uint8_t, Alloc>& buf, |
210 | | const std::vector<T, Alloc2>& vals, |
211 | | size_t tag_size) |
212 | 137k | { |
213 | 137k | append_tls_length_value(buf, vals.data(), vals.size(), tag_size); |
214 | 137k | } void Botan::TLS::append_tls_length_value<unsigned char, std::__1::allocator<unsigned char>, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, unsigned long) Line | Count | Source | 212 | 98.8k | { | 213 | 98.8k | append_tls_length_value(buf, vals.data(), vals.size(), tag_size); | 214 | 98.8k | } |
Unexecuted instantiation: void Botan::TLS::append_tls_length_value<unsigned char, std::__1::allocator<unsigned char>, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&, unsigned long) void Botan::TLS::append_tls_length_value<unsigned short, std::__1::allocator<unsigned char>, std::__1::allocator<unsigned short> >(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> >&, std::__1::vector<unsigned short, std::__1::allocator<unsigned short> > const&, unsigned long) Line | Count | Source | 212 | 12.7k | { | 213 | 12.7k | append_tls_length_value(buf, vals.data(), vals.size(), tag_size); | 214 | 12.7k | } |
void Botan::TLS::append_tls_length_value<unsigned char, Botan::secure_allocator<unsigned char>, std::__1::allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&, unsigned long) Line | Count | Source | 212 | 1.10k | { | 213 | 1.10k | append_tls_length_value(buf, vals.data(), vals.size(), tag_size); | 214 | 1.10k | } |
void Botan::TLS::append_tls_length_value<unsigned char, Botan::secure_allocator<unsigned char>, Botan::secure_allocator<unsigned char> >(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > const&, unsigned long) Line | Count | Source | 212 | 24.5k | { | 213 | 24.5k | append_tls_length_value(buf, vals.data(), vals.size(), tag_size); | 214 | 24.5k | } |
|
215 | | |
216 | | template<typename Alloc> |
217 | | void append_tls_length_value(std::vector<uint8_t, Alloc>& buf, |
218 | | const std::string& str, |
219 | | size_t tag_size) |
220 | 22.2k | { |
221 | 22.2k | append_tls_length_value(buf, |
222 | 22.2k | cast_char_ptr_to_uint8(str.data()), |
223 | 22.2k | str.size(), |
224 | 22.2k | tag_size); |
225 | 22.2k | } |
226 | | |
227 | | } |
228 | | |
229 | | } |
230 | | |
231 | | #endif |