Coverage Report

Created: 2022-01-14 08:07

/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
38.1k
         m_typename(type), m_buf(buf_in), m_offset(0) {}
29
30
      void assert_done() const
31
15.2k
         {
32
15.2k
         if(has_remaining())
33
581
            throw decode_error("Extra bytes at end of message");
34
15.2k
         }
35
36
0
      size_t read_so_far() const { return m_offset; }
37
38
232k
      size_t remaining_bytes() const { return m_buf.size() - m_offset; }
39
40
211k
      bool has_remaining() const { return (remaining_bytes() > 0); }
41
42
      std::vector<uint8_t> get_remaining()
43
1.13k
         {
44
1.13k
         return std::vector<uint8_t>(m_buf.begin() + m_offset, m_buf.end());
45
1.13k
         }
46
47
      std::vector<uint8_t> get_data_read_so_far()
48
1.15k
         {
49
1.15k
         return std::vector<uint8_t>(m_buf.begin(), m_buf.begin() + m_offset);
50
1.15k
         }
51
52
      void discard_next(size_t bytes)
53
3.96k
         {
54
3.96k
         assert_at_least(bytes);
55
3.96k
         m_offset += bytes;
56
3.96k
         }
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
481k
         {
69
481k
         assert_at_least(2);
70
481k
         uint16_t result = make_uint16(m_buf[m_offset], m_buf[m_offset+1]);
71
481k
         m_offset += 2;
72
481k
         return result;
73
481k
         }
74
75
      uint8_t get_byte()
76
128k
         {
77
128k
         assert_at_least(1);
78
128k
         uint8_t result = m_buf[m_offset];
79
128k
         m_offset += 1;
80
128k
         return result;
81
128k
         }
82
83
      template<typename T, typename Container>
84
      Container get_elem(size_t num_elems)
85
254k
         {
86
254k
         assert_at_least(num_elems * sizeof(T));
87
88
254k
         Container result(num_elems);
89
90
2.21M
         for(size_t i = 0; i != num_elems; ++i)
91
1.96M
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
254k
         m_offset += num_elems * sizeof(T);
94
95
254k
         return result;
96
254k
         }
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
232k
         {
86
232k
         assert_at_least(num_elems * sizeof(T));
87
88
232k
         Container result(num_elems);
89
90
2.09M
         for(size_t i = 0; i != num_elems; ++i)
91
1.86M
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
232k
         m_offset += num_elems * sizeof(T);
94
95
232k
         return result;
96
232k
         }
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
21.5k
         {
86
21.5k
         assert_at_least(num_elems * sizeof(T));
87
88
21.5k
         Container result(num_elems);
89
90
120k
         for(size_t i = 0; i != num_elems; ++i)
91
99.2k
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
21.5k
         m_offset += num_elems * sizeof(T);
94
95
21.5k
         return result;
96
21.5k
         }
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
39.6k
         {
103
39.6k
         const size_t num_elems =
104
39.6k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
39.6k
         return get_elem<T, std::vector<T>>(num_elems);
107
39.6k
         }
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
39.3k
         {
103
39.3k
         const size_t num_elems =
104
39.3k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
39.3k
         return get_elem<T, std::vector<T>>(num_elems);
107
39.3k
         }
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
275
         {
103
275
         const size_t num_elems =
104
275
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
275
         return get_elem<T, std::vector<T>>(num_elems);
107
275
         }
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
71.3k
         {
114
71.3k
         const size_t num_elems =
115
71.3k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
71.3k
         return get_elem<T, std::vector<T>>(num_elems);
118
71.3k
         }
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
49.9k
         {
114
49.9k
         const size_t num_elems =
115
49.9k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
49.9k
         return get_elem<T, std::vector<T>>(num_elems);
118
49.9k
         }
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
21.4k
         {
114
21.4k
         const size_t num_elems =
115
21.4k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
21.4k
         return get_elem<T, std::vector<T>>(num_elems);
118
21.4k
         }
119
120
      std::string get_string(size_t len_bytes,
121
                             size_t min_bytes,
122
                             size_t max_bytes)
123
28.7k
         {
124
28.7k
         std::vector<uint8_t> v =
125
28.7k
            get_range_vector<uint8_t>(len_bytes, min_bytes, max_bytes);
126
127
28.7k
         return std::string(cast_uint8_ptr_to_char(v.data()), v.size());
128
28.7k
         }
129
130
      template<typename T>
131
      std::vector<T> get_fixed(size_t size)
132
137k
         {
133
137k
         return get_elem<T, std::vector<T>>(size);
134
137k
         }
135
136
   private:
137
      size_t get_length_field(size_t len_bytes)
138
111k
         {
139
111k
         assert_at_least(len_bytes);
140
141
111k
         if(len_bytes == 1)
142
72.9k
            return get_byte();
143
38.1k
         else if(len_bytes == 2)
144
37.9k
            return get_uint16_t();
145
146
158
         throw decode_error("Bad length size");
147
111k
         }
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
111k
         {
154
111k
         const size_t byte_length = get_length_field(len_bytes);
155
156
111k
         if(byte_length % T_size != 0)
157
36
            throw decode_error("Size isn't multiple of T");
158
159
111k
         const size_t num_elems = byte_length / T_size;
160
161
111k
         if(num_elems < min_elems || num_elems > max_elems)
162
113
            throw decode_error("Length field outside parameters");
163
164
110k
         return num_elems;
165
111k
         }
166
167
      void assert_at_least(size_t n) const
168
979k
         {
169
979k
         if(m_buf.size() - m_offset < n)
170
1.05k
            throw decode_error("Expected " + std::to_string(n) +
171
1.05k
                               " bytes remaining, only " +
172
1.05k
                               std::to_string(m_buf.size()-m_offset) +
173
1.05k
                               " left");
174
979k
         }
175
176
      Decoding_Error decode_error(const std::string& why) const
177
1.78k
         {
178
1.78k
         return Decoding_Error("Invalid " + std::string(m_typename) + ": " + why);
179
1.78k
         }
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
93.3k
   {
195
93.3k
   const size_t T_size = sizeof(T);
196
93.3k
   const size_t val_bytes = T_size * vals_size;
197
198
93.3k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
93.3k
   if((tag_size == 1 && val_bytes > 255) ||
202
93.3k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
226k
   for(size_t i = 0; i != tag_size; ++i)
206
133k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
3.25M
   for(size_t i = 0; i != vals_size; ++i)
209
6.33M
      for(size_t j = 0; j != T_size; ++j)
210
3.16M
         buf.push_back(get_byte_var(j, vals[i]));
211
93.3k
   }
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
72.2k
   {
195
72.2k
   const size_t T_size = sizeof(T);
196
72.2k
   const size_t val_bytes = T_size * vals_size;
197
198
72.2k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
72.2k
   if((tag_size == 1 && val_bytes > 255) ||
202
72.2k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
163k
   for(size_t i = 0; i != tag_size; ++i)
206
90.9k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
2.56M
   for(size_t i = 0; i != vals_size; ++i)
209
4.99M
      for(size_t j = 0; j != T_size; ++j)
210
2.49M
         buf.push_back(get_byte_var(j, vals[i]));
211
72.2k
   }
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.53k
   {
195
1.53k
   const size_t T_size = sizeof(T);
196
1.53k
   const size_t val_bytes = T_size * vals_size;
197
198
1.53k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
1.53k
   if((tag_size == 1 && val_bytes > 255) ||
202
1.53k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
4.61k
   for(size_t i = 0; i != tag_size; ++i)
206
3.07k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
1.53k
   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.53k
   }
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
19.5k
   {
195
19.5k
   const size_t T_size = sizeof(T);
196
19.5k
   const size_t val_bytes = T_size * vals_size;
197
198
19.5k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
19.5k
   if((tag_size == 1 && val_bytes > 255) ||
202
19.5k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
58.6k
   for(size_t i = 0; i != tag_size; ++i)
206
39.1k
      buf.push_back(get_byte_var(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
688k
   for(size_t i = 0; i != vals_size; ++i)
209
1.33M
      for(size_t j = 0; j != T_size; ++j)
210
669k
         buf.push_back(get_byte_var(j, vals[i]));
211
19.5k
   }
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
71.0k
   {
218
71.0k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
71.0k
   }
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
49.9k
   {
218
49.9k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
49.9k
   }
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
217
1.53k
   {
218
1.53k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
1.53k
   }
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
366
   {
218
366
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
366
   }
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
19.1k
   {
218
19.1k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
19.1k
   }
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.4k
   {
226
18.4k
   append_tls_length_value(buf,
227
18.4k
                           cast_char_ptr_to_uint8(str.data()),
228
18.4k
                           str.size(),
229
18.4k
                           tag_size);
230
18.4k
   }
231
232
}
233
234
}
235
236
#endif