Coverage Report

Created: 2021-10-13 08:49

/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.6k
         m_typename(type), m_buf(buf_in), m_offset(0) {}
29
30
      void assert_done() const
31
11.8k
         {
32
11.8k
         if(has_remaining())
33
338
            throw decode_error("Extra bytes at end of message");
34
11.8k
         }
35
36
0
      size_t read_so_far() const { return m_offset; }
37
38
248k
      size_t remaining_bytes() const { return m_buf.size() - m_offset; }
39
40
224k
      bool has_remaining() const { return (remaining_bytes() > 0); }
41
42
      std::vector<uint8_t> get_remaining()
43
3.98k
         {
44
3.98k
         return std::vector<uint8_t>(m_buf.begin() + m_offset, m_buf.end());
45
3.98k
         }
46
47
      std::vector<uint8_t> get_data_read_so_far()
48
4.00k
         {
49
4.00k
         return std::vector<uint8_t>(m_buf.begin(), m_buf.begin() + m_offset);
50
4.00k
         }
51
52
      void discard_next(size_t bytes)
53
2.73k
         {
54
2.73k
         assert_at_least(bytes);
55
2.73k
         m_offset += bytes;
56
2.73k
         }
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
      uint16_t get_uint16_t()
68
507k
         {
69
507k
         assert_at_least(2);
70
507k
         uint16_t result = make_uint16(m_buf[m_offset], m_buf[m_offset+1]);
71
507k
         m_offset += 2;
72
507k
         return result;
73
507k
         }
74
75
      uint8_t get_byte()
76
146k
         {
77
146k
         assert_at_least(1);
78
146k
         uint8_t result = m_buf[m_offset];
79
146k
         m_offset += 1;
80
146k
         return result;
81
146k
         }
82
83
      template<typename T, typename Container>
84
      Container get_elem(size_t num_elems)
85
270k
         {
86
270k
         assert_at_least(num_elems * sizeof(T));
87
88
270k
         Container result(num_elems);
89
90
2.15M
         for(size_t i = 0; i != num_elems; ++i)
91
1.88M
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
270k
         m_offset += num_elems * sizeof(T);
94
95
270k
         return result;
96
270k
         }
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
85
245k
         {
86
245k
         assert_at_least(num_elems * sizeof(T));
87
88
245k
         Container result(num_elems);
89
90
2.05M
         for(size_t i = 0; i != num_elems; ++i)
91
1.81M
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
245k
         m_offset += num_elems * sizeof(T);
94
95
245k
         return result;
96
245k
         }
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
85
24.8k
         {
86
24.8k
         assert_at_least(num_elems * sizeof(T));
87
88
24.8k
         Container result(num_elems);
89
90
96.4k
         for(size_t i = 0; i != num_elems; ++i)
91
71.5k
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
24.8k
         m_offset += num_elems * sizeof(T);
94
95
24.8k
         return result;
96
24.8k
         }
97
98
      template<typename T>
99
      std::vector<T> get_range(size_t len_bytes,
100
                               size_t min_elems,
101
                               size_t max_elems)
102
42.7k
         {
103
42.7k
         const size_t num_elems =
104
42.7k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
42.7k
         return get_elem<T, std::vector<T>>(num_elems);
107
42.7k
         }
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
102
42.2k
         {
103
42.2k
         const size_t num_elems =
104
42.2k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
42.2k
         return get_elem<T, std::vector<T>>(num_elems);
107
42.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
102
492
         {
103
492
         const size_t num_elems =
104
492
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
492
         return get_elem<T, std::vector<T>>(num_elems);
107
492
         }
108
109
      template<typename T>
110
      std::vector<T> get_range_vector(size_t len_bytes,
111
                                      size_t min_elems,
112
                                      size_t max_elems)
113
79.5k
         {
114
79.5k
         const size_t num_elems =
115
79.5k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
79.5k
         return get_elem<T, std::vector<T>>(num_elems);
118
79.5k
         }
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
113
55.0k
         {
114
55.0k
         const size_t num_elems =
115
55.0k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
55.0k
         return get_elem<T, std::vector<T>>(num_elems);
118
55.0k
         }
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
113
24.4k
         {
114
24.4k
         const size_t num_elems =
115
24.4k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
24.4k
         return get_elem<T, std::vector<T>>(num_elems);
118
24.4k
         }
119
120
      std::string get_string(size_t len_bytes,
121
                             size_t min_bytes,
122
                             size_t max_bytes)
123
30.7k
         {
124
30.7k
         std::vector<uint8_t> v =
125
30.7k
            get_range_vector<uint8_t>(len_bytes, min_bytes, max_bytes);
126
127
30.7k
         return std::string(cast_uint8_ptr_to_char(v.data()), v.size());
128
30.7k
         }
129
130
      template<typename T>
131
      std::vector<T> get_fixed(size_t size)
132
145k
         {
133
145k
         return get_elem<T, std::vector<T>>(size);
134
145k
         }
135
136
   private:
137
      size_t get_length_field(size_t len_bytes)
138
122k
         {
139
122k
         assert_at_least(len_bytes);
140
141
122k
         if(len_bytes == 1)
142
84.7k
            return get_byte();
143
37.4k
         else if(len_bytes == 2)
144
37.3k
            return get_uint16_t();
145
146
173
         throw decode_error("Bad length size");
147
122k
         }
148
149
      size_t get_num_elems(size_t len_bytes,
150
                           size_t T_size,
151
                           size_t min_elems,
152
                           size_t max_elems)
153
122k
         {
154
122k
         const size_t byte_length = get_length_field(len_bytes);
155
156
122k
         if(byte_length % T_size != 0)
157
37
            throw decode_error("Size isn't multiple of T");
158
159
122k
         const size_t num_elems = byte_length / T_size;
160
161
122k
         if(num_elems < min_elems || num_elems > max_elems)
162
108
            throw decode_error("Length field outside parameters");
163
164
122k
         return num_elems;
165
122k
         }
166
167
      void assert_at_least(size_t n) const
168
1.04M
         {
169
1.04M
         if(m_buf.size() - m_offset < n)
170
1.08k
            throw decode_error("Expected " + std::to_string(n) +
171
1.08k
                               " bytes remaining, only " +
172
1.08k
                               std::to_string(m_buf.size()-m_offset) +
173
1.08k
                               " left");
174
1.04M
         }
175
176
      Decoding_Error decode_error(const std::string& why) const
177
1.57k
         {
178
1.57k
         return Decoding_Error("Invalid " + std::string(m_typename) + ": " + why);
179
1.57k
         }
180
181
      const char* m_typename;
182
      const std::vector<uint8_t>& m_buf;
183
      size_t m_offset;
184
   };
185
186
/**
187
* Helper function for encoding length-tagged vectors
188
*/
189
template<typename T, typename Alloc>
190
void append_tls_length_value(std::vector<uint8_t, Alloc>& buf,
191
                             const T* vals,
192
                             size_t vals_size,
193
                             size_t tag_size)
194
87.7k
   {
195
87.7k
   const size_t T_size = sizeof(T);
196
87.7k
   const size_t val_bytes = T_size * vals_size;
197
198
87.7k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
87.7k
   if((tag_size == 1 && val_bytes > 255) ||
202
87.7k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
210k
   for(size_t i = 0; i != tag_size; ++i)
206
122k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
3.29M
   for(size_t i = 0; i != vals_size; ++i)
209
6.40M
      for(size_t j = 0; j != T_size; ++j)
210
3.20M
         buf.push_back(get_byte_var(j, vals[i]));
211
87.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
194
71.3k
   {
195
71.3k
   const size_t T_size = sizeof(T);
196
71.3k
   const size_t val_bytes = T_size * vals_size;
197
198
71.3k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
71.3k
   if((tag_size == 1 && val_bytes > 255) ||
202
71.3k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
161k
   for(size_t i = 0; i != tag_size; ++i)
206
90.1k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
2.76M
   for(size_t i = 0; i != vals_size; ++i)
209
5.38M
      for(size_t j = 0; j != T_size; ++j)
210
2.69M
         buf.push_back(get_byte_var(j, vals[i]));
211
71.3k
   }
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
194
1.51k
   {
195
1.51k
   const size_t T_size = sizeof(T);
196
1.51k
   const size_t val_bytes = T_size * vals_size;
197
198
1.51k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
1.51k
   if((tag_size == 1 && val_bytes > 255) ||
202
1.51k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
4.53k
   for(size_t i = 0; i != tag_size; ++i)
206
3.02k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
1.51k
   for(size_t i = 0; i != vals_size; ++i)
209
0
      for(size_t j = 0; j != T_size; ++j)
210
0
         buf.push_back(get_byte_var(j, vals[i]));
211
1.51k
   }
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
194
14.8k
   {
195
14.8k
   const size_t T_size = sizeof(T);
196
14.8k
   const size_t val_bytes = T_size * vals_size;
197
198
14.8k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
14.8k
   if((tag_size == 1 && val_bytes > 255) ||
202
14.8k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
44.6k
   for(size_t i = 0; i != tag_size; ++i)
206
29.7k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
527k
   for(size_t i = 0; i != vals_size; ++i)
209
1.02M
      for(size_t j = 0; j != T_size; ++j)
210
512k
         buf.push_back(get_byte_var(j, vals[i]));
211
14.8k
   }
212
213
template<typename T, typename Alloc, typename Alloc2>
214
void append_tls_length_value(std::vector<uint8_t, Alloc>& buf,
215
                             const std::vector<T, Alloc2>& vals,
216
                             size_t tag_size)
217
63.9k
   {
218
63.9k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
63.9k
   }
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
217
47.5k
   {
218
47.5k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
47.5k
   }
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
217
1.51k
   {
218
1.51k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
1.51k
   }
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 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
217
268
   {
218
268
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
268
   }
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
217
14.6k
   {
218
14.6k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
14.6k
   }
220
221
template<typename Alloc>
222
void append_tls_length_value(std::vector<uint8_t, Alloc>& buf,
223
                             const std::string& str,
224
                             size_t tag_size)
225
18.5k
   {
226
18.5k
   append_tls_length_value(buf,
227
18.5k
                           cast_char_ptr_to_uint8(str.data()),
228
18.5k
                           str.size(),
229
18.5k
                           tag_size);
230
18.5k
   }
231
232
}
233
234
}
235
236
#endif