Coverage Report

Created: 2022-06-23 06:44

/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/internal/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
37.7k
         m_typename(type), m_buf(buf_in), m_offset(0) {}
29
30
      void assert_done() const
31
13.5k
         {
32
13.5k
         if(has_remaining())
33
445
            throw decode_error("Extra bytes at end of message");
34
13.5k
         }
35
36
0
      size_t read_so_far() const { return m_offset; }
37
38
244k
      size_t remaining_bytes() const { return m_buf.size() - m_offset; }
39
40
222k
      bool has_remaining() const { return (remaining_bytes() > 0); }
41
42
      std::vector<uint8_t> get_remaining()
43
1.34k
         {
44
1.34k
         return std::vector<uint8_t>(m_buf.begin() + m_offset, m_buf.end());
45
1.34k
         }
46
47
      std::vector<uint8_t> get_data_read_so_far()
48
1.36k
         {
49
1.36k
         return std::vector<uint8_t>(m_buf.begin(), m_buf.begin() + m_offset);
50
1.36k
         }
51
52
      void discard_next(size_t bytes)
53
4.42k
         {
54
4.42k
         assert_at_least(bytes);
55
4.42k
         m_offset += bytes;
56
4.42k
         }
57
58
      uint32_t get_uint32_t()
59
0
         {
60
0
         assert_at_least(4);
61
0
         uint32_t result = make_uint32(m_buf[m_offset  ], m_buf[m_offset+1],
62
0
                                     m_buf[m_offset+2], m_buf[m_offset+3]);
63
0
         m_offset += 4;
64
0
         return result;
65
0
         }
66
67
      uint32_t get_uint24_t()
68
0
         {
69
0
         assert_at_least(3);
70
0
         uint32_t result = make_uint32(0,                 m_buf[m_offset],
71
0
                                       m_buf[m_offset+1], m_buf[m_offset+2]);
72
0
         m_offset += 3;
73
0
         return result;
74
0
         }
75
76
      uint16_t get_uint16_t()
77
510k
         {
78
510k
         assert_at_least(2);
79
510k
         uint16_t result = make_uint16(m_buf[m_offset], m_buf[m_offset+1]);
80
510k
         m_offset += 2;
81
510k
         return result;
82
510k
         }
83
84
      uint16_t peek_uint16_t() const
85
0
         {
86
0
         assert_at_least(2);
87
0
         return make_uint16(m_buf[m_offset], m_buf[m_offset+1]);
88
0
         }
89
90
      uint8_t get_byte()
91
128k
         {
92
128k
         assert_at_least(1);
93
128k
         uint8_t result = m_buf[m_offset];
94
128k
         m_offset += 1;
95
128k
         return result;
96
128k
         }
97
98
      template<typename T, typename Container>
99
      Container get_elem(size_t num_elems)
100
260k
         {
101
260k
         assert_at_least(num_elems * sizeof(T));
102
103
260k
         Container result(num_elems);
104
105
2.17M
         for(size_t i = 0; i != num_elems; ++i)
106
1.91M
            result[i] = load_be<T>(&m_buf[m_offset], i);
107
108
260k
         m_offset += num_elems * sizeof(T);
109
110
260k
         return result;
111
260k
         }
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
100
237k
         {
101
237k
         assert_at_least(num_elems * sizeof(T));
102
103
237k
         Container result(num_elems);
104
105
2.06M
         for(size_t i = 0; i != num_elems; ++i)
106
1.82M
            result[i] = load_be<T>(&m_buf[m_offset], i);
107
108
237k
         m_offset += num_elems * sizeof(T);
109
110
237k
         return result;
111
237k
         }
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
100
22.8k
         {
101
22.8k
         assert_at_least(num_elems * sizeof(T));
102
103
22.8k
         Container result(num_elems);
104
105
106k
         for(size_t i = 0; i != num_elems; ++i)
106
83.7k
            result[i] = load_be<T>(&m_buf[m_offset], i);
107
108
22.8k
         m_offset += num_elems * sizeof(T);
109
110
22.8k
         return result;
111
22.8k
         }
112
113
      std::vector<uint8_t> get_tls_length_value(size_t len_bytes)
114
0
         {
115
0
         return get_fixed<uint8_t>(get_length_field(len_bytes));
116
0
         }
117
118
      template<typename T>
119
      std::vector<T> get_range(size_t len_bytes,
120
                               size_t min_elems,
121
                               size_t max_elems)
122
39.2k
         {
123
39.2k
         const size_t num_elems =
124
39.2k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
125
126
39.2k
         return get_elem<T, std::vector<T>>(num_elems);
127
39.2k
         }
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
122
39.0k
         {
123
39.0k
         const size_t num_elems =
124
39.0k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
125
126
39.0k
         return get_elem<T, std::vector<T>>(num_elems);
127
39.0k
         }
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
122
246
         {
123
246
         const size_t num_elems =
124
246
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
125
126
246
         return get_elem<T, std::vector<T>>(num_elems);
127
246
         }
128
129
      template<typename T>
130
      std::vector<T> get_range_vector(size_t len_bytes,
131
                                      size_t min_elems,
132
                                      size_t max_elems)
133
69.2k
         {
134
69.2k
         const size_t num_elems =
135
69.2k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
136
137
69.2k
         return get_elem<T, std::vector<T>>(num_elems);
138
69.2k
         }
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
133
46.5k
         {
134
46.5k
         const size_t num_elems =
135
46.5k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
136
137
46.5k
         return get_elem<T, std::vector<T>>(num_elems);
138
46.5k
         }
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
133
22.7k
         {
134
22.7k
         const size_t num_elems =
135
22.7k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
136
137
22.7k
         return get_elem<T, std::vector<T>>(num_elems);
138
22.7k
         }
139
140
      std::string get_string(size_t len_bytes,
141
                             size_t min_bytes,
142
                             size_t max_bytes)
143
23.9k
         {
144
23.9k
         std::vector<uint8_t> v =
145
23.9k
            get_range_vector<uint8_t>(len_bytes, min_bytes, max_bytes);
146
147
23.9k
         return std::string(cast_uint8_ptr_to_char(v.data()), v.size());
148
23.9k
         }
149
150
      template<typename T>
151
      std::vector<T> get_fixed(size_t size)
152
145k
         {
153
145k
         return get_elem<T, std::vector<T>>(size);
154
145k
         }
155
156
   private:
157
      size_t get_length_field(size_t len_bytes)
158
108k
         {
159
108k
         assert_at_least(len_bytes);
160
161
108k
         if(len_bytes == 1)
162
70.3k
            return get_byte();
163
38.1k
         else if(len_bytes == 2)
164
37.9k
            return get_uint16_t();
165
185
         else if(len_bytes == 3)
166
0
            return get_uint24_t();
167
168
185
         throw decode_error("Bad length size");
169
108k
         }
170
171
      size_t get_num_elems(size_t len_bytes,
172
                           size_t T_size,
173
                           size_t min_elems,
174
                           size_t max_elems)
175
108k
         {
176
108k
         const size_t byte_length = get_length_field(len_bytes);
177
178
108k
         if(byte_length % T_size != 0)
179
26
            throw decode_error("Size isn't multiple of T");
180
181
108k
         const size_t num_elems = byte_length / T_size;
182
183
108k
         if(num_elems < min_elems || num_elems > max_elems)
184
107
            throw decode_error("Length field outside parameters");
185
186
108k
         return num_elems;
187
108k
         }
188
189
      void assert_at_least(size_t n) const
190
1.01M
         {
191
1.01M
         if(m_buf.size() - m_offset < n)
192
959
            throw decode_error("Expected " + std::to_string(n) +
193
959
                               " bytes remaining, only " +
194
959
                               std::to_string(m_buf.size()-m_offset) +
195
959
                               " left");
196
1.01M
         }
197
198
      Decoding_Error decode_error(const std::string& why) const
199
1.53k
         {
200
1.53k
         return Decoding_Error("Invalid " + std::string(m_typename) + ": " + why);
201
1.53k
         }
202
203
      const char* m_typename;
204
      const std::vector<uint8_t>& m_buf;
205
      size_t m_offset;
206
   };
207
208
/**
209
* Helper function for encoding length-tagged vectors
210
*/
211
template<typename T, typename Alloc>
212
void append_tls_length_value(std::vector<uint8_t, Alloc>& buf,
213
                             const T* vals,
214
                             size_t vals_size,
215
                             size_t tag_size)
216
93.7k
   {
217
93.7k
   const size_t T_size = sizeof(T);
218
93.7k
   const size_t val_bytes = T_size * vals_size;
219
220
93.7k
   if(tag_size != 1 && tag_size != 2 && tag_size != 3)
221
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
222
223
93.7k
   if((tag_size == 1 && val_bytes > 255) ||
224
93.7k
      (tag_size == 2 && val_bytes > 65535) ||
225
93.7k
      (tag_size == 3 && val_bytes > 16777215))
226
0
      throw Invalid_Argument("append_tls_length_value: value too large");
227
228
223k
   for(size_t i = 0; i != tag_size; ++i)
229
129k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
230
231
3.31M
   for(size_t i = 0; i != vals_size; ++i)
232
6.43M
      for(size_t j = 0; j != T_size; ++j)
233
3.21M
         buf.push_back(get_byte_var(j, vals[i]));
234
93.7k
   }
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
216
77.5k
   {
217
77.5k
   const size_t T_size = sizeof(T);
218
77.5k
   const size_t val_bytes = T_size * vals_size;
219
220
77.5k
   if(tag_size != 1 && tag_size != 2 && tag_size != 3)
221
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
222
223
77.5k
   if((tag_size == 1 && val_bytes > 255) ||
224
77.5k
      (tag_size == 2 && val_bytes > 65535) ||
225
77.5k
      (tag_size == 3 && val_bytes > 16777215))
226
0
      throw Invalid_Argument("append_tls_length_value: value too large");
227
228
174k
   for(size_t i = 0; i != tag_size; ++i)
229
97.3k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
230
231
2.78M
   for(size_t i = 0; i != vals_size; ++i)
232
5.40M
      for(size_t j = 0; j != T_size; ++j)
233
2.70M
         buf.push_back(get_byte_var(j, vals[i]));
234
77.5k
   }
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
216
1.42k
   {
217
1.42k
   const size_t T_size = sizeof(T);
218
1.42k
   const size_t val_bytes = T_size * vals_size;
219
220
1.42k
   if(tag_size != 1 && tag_size != 2 && tag_size != 3)
221
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
222
223
1.42k
   if((tag_size == 1 && val_bytes > 255) ||
224
1.42k
      (tag_size == 2 && val_bytes > 65535) ||
225
1.42k
      (tag_size == 3 && val_bytes > 16777215))
226
0
      throw Invalid_Argument("append_tls_length_value: value too large");
227
228
4.27k
   for(size_t i = 0; i != tag_size; ++i)
229
2.85k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
230
231
1.42k
   for(size_t i = 0; i != vals_size; ++i)
232
0
      for(size_t j = 0; j != T_size; ++j)
233
0
         buf.push_back(get_byte_var(j, vals[i]));
234
1.42k
   }
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
216
14.8k
   {
217
14.8k
   const size_t T_size = sizeof(T);
218
14.8k
   const size_t val_bytes = T_size * vals_size;
219
220
14.8k
   if(tag_size != 1 && tag_size != 2 && tag_size != 3)
221
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
222
223
14.8k
   if((tag_size == 1 && val_bytes > 255) ||
224
14.8k
      (tag_size == 2 && val_bytes > 65535) ||
225
14.8k
      (tag_size == 3 && val_bytes > 16777215))
226
0
      throw Invalid_Argument("append_tls_length_value: value too large");
227
228
44.5k
   for(size_t i = 0; i != tag_size; ++i)
229
29.6k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
230
231
530k
   for(size_t i = 0; i != vals_size; ++i)
232
1.03M
      for(size_t j = 0; j != T_size; ++j)
233
515k
         buf.push_back(get_byte_var(j, vals[i]));
234
14.8k
   }
235
236
template<typename T, typename Alloc, typename Alloc2>
237
void append_tls_length_value(std::vector<uint8_t, Alloc>& buf,
238
                             const std::vector<T, Alloc2>& vals,
239
                             size_t tag_size)
240
70.6k
   {
241
70.6k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
242
70.6k
   }
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
240
54.4k
   {
241
54.4k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
242
54.4k
   }
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
240
1.42k
   {
241
1.42k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
242
1.42k
   }
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
240
460
   {
241
460
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
242
460
   }
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
240
14.3k
   {
241
14.3k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
242
14.3k
   }
243
244
template<typename Alloc>
245
void append_tls_length_value(std::vector<uint8_t, Alloc>& buf,
246
                             const std::string& str,
247
                             size_t tag_size)
248
19.6k
   {
249
19.6k
   append_tls_length_value(buf,
250
19.6k
                           cast_char_ptr_to_uint8(str.data()),
251
19.6k
                           str.size(),
252
19.6k
                           tag_size);
253
19.6k
   }
254
255
}
256
257
}
258
259
#endif