Coverage Report

Created: 2021-02-21 07:20

/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
50.1k
         m_typename(type), m_buf(buf_in), m_offset(0) {}
29
30
      void assert_done() const
31
11.2k
         {
32
11.2k
         if(has_remaining())
33
588
            throw decode_error("Extra bytes at end of message");
34
11.2k
         }
35
36
1.38k
      size_t read_so_far() const { return m_offset; }
37
38
323k
      size_t remaining_bytes() const { return m_buf.size() - m_offset; }
39
40
287k
      bool has_remaining() const { return (remaining_bytes() > 0); }
41
42
      std::vector<uint8_t> get_remaining()
43
8.76k
         {
44
8.76k
         return std::vector<uint8_t>(m_buf.begin() + m_offset, m_buf.end());
45
8.76k
         }
46
47
      std::vector<uint8_t> get_data_read_so_far()
48
8.78k
         {
49
8.78k
         return std::vector<uint8_t>(m_buf.begin(), m_buf.begin() + m_offset);
50
8.78k
         }
51
52
      void discard_next(size_t bytes)
53
10.2k
         {
54
10.2k
         assert_at_least(bytes);
55
10.2k
         m_offset += bytes;
56
10.2k
         }
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
651k
         {
69
651k
         assert_at_least(2);
70
651k
         uint16_t result = make_uint16(m_buf[m_offset], m_buf[m_offset+1]);
71
651k
         m_offset += 2;
72
651k
         return result;
73
651k
         }
74
75
      uint8_t get_byte()
76
242k
         {
77
242k
         assert_at_least(1);
78
242k
         uint8_t result = m_buf[m_offset];
79
242k
         m_offset += 1;
80
242k
         return result;
81
242k
         }
82
83
      template<typename T, typename Container>
84
      Container get_elem(size_t num_elems)
85
358k
         {
86
358k
         assert_at_least(num_elems * sizeof(T));
87
88
358k
         Container result(num_elems);
89
90
2.95M
         for(size_t i = 0; i != num_elems; ++i)
91
2.59M
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
358k
         m_offset += num_elems * sizeof(T);
94
95
358k
         return result;
96
358k
         }
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
324k
         {
86
324k
         assert_at_least(num_elems * sizeof(T));
87
88
324k
         Container result(num_elems);
89
90
2.83M
         for(size_t i = 0; i != num_elems; ++i)
91
2.50M
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
324k
         m_offset += num_elems * sizeof(T);
94
95
324k
         return result;
96
324k
         }
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
33.7k
         {
86
33.7k
         assert_at_least(num_elems * sizeof(T));
87
88
33.7k
         Container result(num_elems);
89
90
127k
         for(size_t i = 0; i != num_elems; ++i)
91
93.9k
            result[i] = load_be<T>(&m_buf[m_offset], i);
92
93
33.7k
         m_offset += num_elems * sizeof(T);
94
95
33.7k
         return result;
96
33.7k
         }
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
63.3k
         {
103
63.3k
         const size_t num_elems =
104
63.3k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
63.3k
         return get_elem<T, std::vector<T>>(num_elems);
107
63.3k
         }
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
61.6k
         {
103
61.6k
         const size_t num_elems =
104
61.6k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
61.6k
         return get_elem<T, std::vector<T>>(num_elems);
107
61.6k
         }
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
1.72k
         {
103
1.72k
         const size_t num_elems =
104
1.72k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
105
106
1.72k
         return get_elem<T, std::vector<T>>(num_elems);
107
1.72k
         }
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
124k
         {
114
124k
         const size_t num_elems =
115
124k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
124k
         return get_elem<T, std::vector<T>>(num_elems);
118
124k
         }
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
92.3k
         {
114
92.3k
         const size_t num_elems =
115
92.3k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
92.3k
         return get_elem<T, std::vector<T>>(num_elems);
118
92.3k
         }
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
32.1k
         {
114
32.1k
         const size_t num_elems =
115
32.1k
            get_num_elems(len_bytes, sizeof(T), min_elems, max_elems);
116
117
32.1k
         return get_elem<T, std::vector<T>>(num_elems);
118
32.1k
         }
119
120
      std::string get_string(size_t len_bytes,
121
                             size_t min_bytes,
122
                             size_t max_bytes)
123
52.1k
         {
124
52.1k
         std::vector<uint8_t> v =
125
52.1k
            get_range_vector<uint8_t>(len_bytes, min_bytes, max_bytes);
126
127
52.1k
         return std::string(cast_uint8_ptr_to_char(v.data()), v.size());
128
52.1k
         }
129
130
      template<typename T>
131
      std::vector<T> get_fixed(size_t size)
132
166k
         {
133
166k
         return get_elem<T, std::vector<T>>(size);
134
166k
         }
135
136
   private:
137
      size_t get_length_field(size_t len_bytes)
138
187k
         {
139
187k
         assert_at_least(len_bytes);
140
141
187k
         if(len_bytes == 1)
142
134k
            return get_byte();
143
53.7k
         else if(len_bytes == 2)
144
53.5k
            return get_uint16_t();
145
146
222
         throw decode_error("Bad length size");
147
222
         }
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
187k
         {
154
187k
         const size_t byte_length = get_length_field(len_bytes);
155
156
187k
         if(byte_length % T_size != 0)
157
28
            throw decode_error("Size isn't multiple of T");
158
159
187k
         const size_t num_elems = byte_length / T_size;
160
161
187k
         if(num_elems < min_elems || num_elems > max_elems)
162
151
            throw decode_error("Length field outside parameters");
163
164
187k
         return num_elems;
165
187k
         }
166
167
      void assert_at_least(size_t n) const
168
1.44M
         {
169
1.44M
         if(m_buf.size() - m_offset < n)
170
1.20k
            throw decode_error("Expected " + std::to_string(n) +
171
1.20k
                               " bytes remaining, only " +
172
1.20k
                               std::to_string(m_buf.size()-m_offset) +
173
1.20k
                               " left");
174
1.44M
         }
175
176
      Decoding_Error decode_error(const std::string& why) const
177
1.97k
         {
178
1.97k
         return Decoding_Error("Invalid " + std::string(m_typename) + ": " + why);
179
1.97k
         }
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
131k
   {
195
131k
   const size_t T_size = sizeof(T);
196
131k
   const size_t val_bytes = T_size * vals_size;
197
198
131k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
131k
   if((tag_size == 1 && val_bytes > 255) ||
202
131k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
308k
   for(size_t i = 0; i != tag_size; ++i)
206
176k
      buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
4.20M
   for(size_t i = 0; i != vals_size; ++i)
209
8.74M
      for(size_t j = 0; j != T_size; ++j)
210
4.67M
         buf.push_back(get_byte(j, vals[i]));
211
131k
   }
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
110k
   {
195
110k
   const size_t T_size = sizeof(T);
196
110k
   const size_t val_bytes = T_size * vals_size;
197
198
110k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
110k
   if((tag_size == 1 && val_bytes > 255) ||
202
110k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
243k
   for(size_t i = 0; i != tag_size; ++i)
206
133k
      buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
3.07M
   for(size_t i = 0; i != vals_size; ++i)
209
5.93M
      for(size_t j = 0; j != T_size; ++j)
210
2.96M
         buf.push_back(get_byte(j, vals[i]));
211
110k
   }
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
6.31k
   {
195
6.31k
   const size_t T_size = sizeof(T);
196
6.31k
   const size_t val_bytes = T_size * vals_size;
197
198
6.31k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
6.31k
   if((tag_size == 1 && val_bytes > 255) ||
202
6.31k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
18.9k
   for(size_t i = 0; i != tag_size; ++i)
206
12.6k
      buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
606k
   for(size_t i = 0; i != vals_size; ++i)
209
1.79M
      for(size_t j = 0; j != T_size; ++j)
210
1.19M
         buf.push_back(get_byte(j, vals[i]));
211
6.31k
   }
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
15.3k
   {
195
15.3k
   const size_t T_size = sizeof(T);
196
15.3k
   const size_t val_bytes = T_size * vals_size;
197
198
15.3k
   if(tag_size != 1 && tag_size != 2)
199
0
      throw Invalid_Argument("append_tls_length_value: invalid tag size");
200
201
15.3k
   if((tag_size == 1 && val_bytes > 255) ||
202
15.3k
      (tag_size == 2 && val_bytes > 65535))
203
0
      throw Invalid_Argument("append_tls_length_value: value too large");
204
205
45.9k
   for(size_t i = 0; i != tag_size; ++i)
206
30.6k
      buf.push_back(get_byte(sizeof(val_bytes)-tag_size+i, val_bytes));
207
208
522k
   for(size_t i = 0; i != vals_size; ++i)
209
1.01M
      for(size_t j = 0; j != T_size; ++j)
210
507k
         buf.push_back(get_byte(j, vals[i]));
211
15.3k
   }
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
97.2k
   {
218
97.2k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
97.2k
   }
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
75.6k
   {
218
75.6k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
75.6k
   }
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
6.31k
   {
218
6.31k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
6.31k
   }
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
897
   {
218
897
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
897
   }
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.4k
   {
218
14.4k
   append_tls_length_value(buf, vals.data(), vals.size(), tag_size);
219
14.4k
   }
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
22.6k
   {
226
22.6k
   append_tls_length_value(buf,
227
22.6k
                           cast_char_ptr_to_uint8(str.data()),
228
22.6k
                           str.size(),
229
22.6k
                           tag_size);
230
22.6k
   }
231
232
}
233
234
}
235
236
#endif