Coverage Report

Created: 2020-11-21 08:34

/src/botan/src/lib/tls/tls_extensions.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* TLS Extensions
3
* (C) 2011,2012,2015,2016 Jack Lloyd
4
*     2016 Juraj Somorovsky
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#include <botan/tls_extensions.h>
10
#include <botan/internal/tls_reader.h>
11
#include <botan/tls_exceptn.h>
12
#include <botan/tls_policy.h>
13
14
namespace Botan {
15
16
namespace TLS {
17
18
namespace {
19
20
Extension* make_extension(TLS_Data_Reader& reader, uint16_t code, uint16_t size, Connection_Side from)
21
213k
   {
22
213k
   switch(code)
23
213k
      {
24
7.51k
      case TLSEXT_SERVER_NAME_INDICATION:
25
7.51k
         return new Server_Name_Indicator(reader, size);
26
27
0
#if defined(BOTAN_HAS_SRP6)
28
138
      case TLSEXT_SRP_IDENTIFIER:
29
138
         return new SRP_Identifier(reader, size);
30
0
#endif
31
32
21.3k
      case TLSEXT_SUPPORTED_GROUPS:
33
21.3k
         return new Supported_Groups(reader, size);
34
35
721
      case TLSEXT_CERT_STATUS_REQUEST:
36
721
         return new Certificate_Status_Request(reader, size, from);
37
38
2.22k
      case TLSEXT_EC_POINT_FORMATS:
39
2.22k
         return new Supported_Point_Formats(reader, size);
40
41
4.00k
      case TLSEXT_SAFE_RENEGOTIATION:
42
4.00k
         return new Renegotiation_Extension(reader, size);
43
44
2.89k
      case TLSEXT_SIGNATURE_ALGORITHMS:
45
2.89k
         return new Signature_Algorithms(reader, size);
46
47
114
      case TLSEXT_USE_SRTP:
48
114
          return new SRTP_Protection_Profiles(reader, size);
49
50
11.4k
      case TLSEXT_ALPN:
51
11.4k
         return new Application_Layer_Protocol_Notification(reader, size);
52
53
9.18k
      case TLSEXT_EXTENDED_MASTER_SECRET:
54
9.18k
         return new Extended_Master_Secret(reader, size);
55
56
3.32k
      case TLSEXT_ENCRYPT_THEN_MAC:
57
3.32k
         return new Encrypt_then_MAC(reader, size);
58
59
3.58k
      case TLSEXT_SESSION_TICKET:
60
3.58k
         return new Session_Ticket(reader, size);
61
62
155
      case TLSEXT_SUPPORTED_VERSIONS:
63
155
         return new Supported_Versions(reader, size, from);
64
147k
      }
65
66
147k
   return new Unknown_Extension(static_cast<Handshake_Extension_Type>(code),
67
147k
                                reader, size);
68
147k
   }
69
70
}
71
72
void Extensions::deserialize(TLS_Data_Reader& reader, Connection_Side from)
73
39.6k
   {
74
39.6k
   if(reader.has_remaining())
75
37.5k
      {
76
37.5k
      const uint16_t all_extn_size = reader.get_uint16_t();
77
78
37.5k
      if(reader.remaining_bytes() != all_extn_size)
79
142
         throw Decoding_Error("Bad extension size");
80
81
251k
      while(reader.has_remaining())
82
214k
         {
83
214k
         const uint16_t extension_code = reader.get_uint16_t();
84
214k
         const uint16_t extension_size = reader.get_uint16_t();
85
86
214k
         const auto type = static_cast<Handshake_Extension_Type>(extension_code);
87
88
214k
         if(m_extensions.find(type) != m_extensions.end())
89
19
            throw TLS_Exception(TLS::Alert::DECODE_ERROR,
90
19
                                "Peer sent duplicated extensions");
91
92
214k
         Extension* extn = make_extension(
93
214k
            reader, extension_code, extension_size, from);
94
95
214k
         this->add(extn);
96
214k
         }
97
37.4k
      }
98
39.6k
   }
99
100
std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const
101
31.6k
   {
102
31.6k
   std::vector<uint8_t> buf(2); // 2 bytes for length field
103
104
31.6k
   for(auto& extn : m_extensions)
105
91.0k
      {
106
91.0k
      if(extn.second->empty())
107
6.01k
         continue;
108
109
85.0k
      const uint16_t extn_code = static_cast<uint16_t>(extn.second->type());
110
111
85.0k
      const std::vector<uint8_t> extn_val = extn.second->serialize(whoami);
112
113
85.0k
      buf.push_back(get_byte(0, extn_code));
114
85.0k
      buf.push_back(get_byte(1, extn_code));
115
116
85.0k
      buf.push_back(get_byte(0, static_cast<uint16_t>(extn_val.size())));
117
85.0k
      buf.push_back(get_byte(1, static_cast<uint16_t>(extn_val.size())));
118
119
85.0k
      buf += extn_val;
120
85.0k
      }
121
122
31.6k
   const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
123
124
31.6k
   buf[0] = get_byte(0, extn_size);
125
31.6k
   buf[1] = get_byte(1, extn_size);
126
127
   // avoid sending a completely empty extensions block
128
31.6k
   if(buf.size() == 2)
129
9.19k
      return std::vector<uint8_t>();
130
131
22.4k
   return buf;
132
22.4k
   }
133
134
bool Extensions::remove_extension(Handshake_Extension_Type typ)
135
0
   {
136
0
   auto i = m_extensions.find(typ);
137
0
   if(i == m_extensions.end())
138
0
      return false;
139
0
   m_extensions.erase(i);
140
0
   return true;
141
0
   }
142
143
std::set<Handshake_Extension_Type> Extensions::extension_types() const
144
27.8k
   {
145
27.8k
   std::set<Handshake_Extension_Type> offers;
146
233k
   for(auto i = m_extensions.begin(); i != m_extensions.end(); ++i)
147
206k
      offers.insert(i->first);
148
27.8k
   return offers;
149
27.8k
   }
150
151
Unknown_Extension::Unknown_Extension(Handshake_Extension_Type type,
152
                                     TLS_Data_Reader& reader,
153
                                     uint16_t extension_size) :
154
   m_type(type),
155
   m_value(reader.get_fixed<uint8_t>(extension_size))
156
147k
   {
157
147k
   }
158
159
std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const
160
0
   {
161
0
   throw Invalid_State("Cannot encode an unknown TLS extension");
162
0
   }
163
164
Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader,
165
                                             uint16_t extension_size)
166
7.51k
   {
167
   /*
168
   * This is used by the server to confirm that it knew the name
169
   */
170
7.51k
   if(extension_size == 0)
171
7.16k
      return;
172
173
354
   uint16_t name_bytes = reader.get_uint16_t();
174
175
354
   if(name_bytes + 2 != extension_size)
176
83
      throw Decoding_Error("Bad encoding of SNI extension");
177
178
1.49k
   while(name_bytes)
179
1.21k
      {
180
1.21k
      uint8_t name_type = reader.get_byte();
181
1.21k
      name_bytes--;
182
183
1.21k
      if(name_type == 0) // DNS
184
1.00k
         {
185
1.00k
         m_sni_host_name = reader.get_string(2, 1, 65535);
186
1.00k
         name_bytes -= static_cast<uint16_t>(2 + m_sni_host_name.size());
187
1.00k
         }
188
217
      else // some other unknown name type
189
217
         {
190
217
         reader.discard_next(name_bytes);
191
217
         name_bytes = 0;
192
217
         }
193
1.21k
      }
194
271
   }
195
196
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side /*whoami*/) const
197
6.01k
   {
198
6.01k
   std::vector<uint8_t> buf;
199
200
6.01k
   size_t name_len = m_sni_host_name.size();
201
202
6.01k
   buf.push_back(get_byte(0, static_cast<uint16_t>(name_len+3)));
203
6.01k
   buf.push_back(get_byte(1, static_cast<uint16_t>(name_len+3)));
204
6.01k
   buf.push_back(0); // DNS
205
206
6.01k
   buf.push_back(get_byte(0, static_cast<uint16_t>(name_len)));
207
6.01k
   buf.push_back(get_byte(1, static_cast<uint16_t>(name_len)));
208
209
6.01k
   buf += std::make_pair(
210
6.01k
      cast_char_ptr_to_uint8(m_sni_host_name.data()),
211
6.01k
      m_sni_host_name.size());
212
213
6.01k
   return buf;
214
6.01k
   }
215
216
#if defined(BOTAN_HAS_SRP6)
217
218
SRP_Identifier::SRP_Identifier(TLS_Data_Reader& reader,
219
                               uint16_t extension_size) : m_srp_identifier(reader.get_string(1, 1, 255))
220
138
   {
221
138
   if(m_srp_identifier.size() + 1 != extension_size)
222
59
      throw Decoding_Error("Bad encoding for SRP identifier extension");
223
138
   }
224
225
std::vector<uint8_t> SRP_Identifier::serialize(Connection_Side /*whoami*/) const
226
0
   {
227
0
   std::vector<uint8_t> buf;
228
229
0
   const uint8_t* srp_bytes = cast_char_ptr_to_uint8(m_srp_identifier.data());
230
0
   append_tls_length_value(buf, srp_bytes, m_srp_identifier.size(), 1);
231
232
0
   return buf;
233
0
   }
234
235
#endif
236
237
Renegotiation_Extension::Renegotiation_Extension(TLS_Data_Reader& reader,
238
                                                 uint16_t extension_size) : m_reneg_data(reader.get_range<uint8_t>(1, 0, 255))
239
4.00k
   {
240
4.00k
   if(m_reneg_data.size() + 1 != extension_size)
241
59
      throw Decoding_Error("Bad encoding for secure renegotiation extn");
242
4.00k
   }
243
244
std::vector<uint8_t> Renegotiation_Extension::serialize(Connection_Side /*whoami*/) const
245
12.7k
   {
246
12.7k
   std::vector<uint8_t> buf;
247
12.7k
   append_tls_length_value(buf, m_reneg_data, 1);
248
12.7k
   return buf;
249
12.7k
   }
250
251
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
252
                                                                                 uint16_t extension_size)
253
11.4k
   {
254
11.4k
   if(extension_size == 0)
255
7.69k
      return; // empty extension
256
257
3.78k
   const uint16_t name_bytes = reader.get_uint16_t();
258
259
3.78k
   size_t bytes_remaining = extension_size - 2;
260
261
3.78k
   if(name_bytes != bytes_remaining)
262
119
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
263
264
33.2k
   while(bytes_remaining)
265
29.6k
      {
266
29.6k
      const std::string p = reader.get_string(1, 0, 255);
267
268
29.6k
      if(bytes_remaining < p.size() + 1)
269
5
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
270
271
29.6k
      if(p.empty())
272
87
         throw Decoding_Error("Empty ALPN protocol not allowed");
273
274
29.5k
      bytes_remaining -= (p.size() + 1);
275
276
29.5k
      m_protocols.push_back(p);
277
29.5k
      }
278
3.66k
   }
279
280
const std::string& Application_Layer_Protocol_Notification::single_protocol() const
281
0
   {
282
0
   if(m_protocols.size() != 1)
283
0
      throw TLS_Exception(Alert::HANDSHAKE_FAILURE,
284
0
                          "Server sent " + std::to_string(m_protocols.size()) +
285
0
                          " protocols in ALPN extension response");
286
0
   return m_protocols[0];
287
0
   }
288
289
std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize(Connection_Side /*whoami*/) const
290
7.60k
   {
291
7.60k
   std::vector<uint8_t> buf(2);
292
293
7.60k
   for(auto&& p: m_protocols)
294
7.60k
      {
295
7.60k
      if(p.length() >= 256)
296
0
         throw TLS_Exception(Alert::INTERNAL_ERROR, "ALPN name too long");
297
7.60k
      if(p != "")
298
7.60k
         append_tls_length_value(buf,
299
7.60k
                                 cast_char_ptr_to_uint8(p.data()),
300
7.60k
                                 p.size(),
301
7.60k
                                 1);
302
7.60k
      }
303
304
7.60k
   buf[0] = get_byte(0, static_cast<uint16_t>(buf.size()-2));
305
7.60k
   buf[1] = get_byte(1, static_cast<uint16_t>(buf.size()-2));
306
307
7.60k
   return buf;
308
7.60k
   }
309
310
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups)
311
6.01k
   {
312
6.01k
   }
313
314
std::vector<Group_Params> Supported_Groups::ec_groups() const
315
45.4k
   {
316
45.4k
   std::vector<Group_Params> ec;
317
45.4k
   for(auto g : m_groups)
318
232k
      {
319
232k
      if(group_param_is_dh(g) == false)
320
178k
         ec.push_back(g);
321
232k
      }
322
45.4k
   return ec;
323
45.4k
   }
324
325
std::vector<Group_Params> Supported_Groups::dh_groups() const
326
2.16k
   {
327
2.16k
   std::vector<Group_Params> dh;
328
2.16k
   for(auto g : m_groups)
329
9.18k
      {
330
9.18k
      if(group_param_is_dh(g) == true)
331
3.48k
         dh.push_back(g);
332
9.18k
      }
333
2.16k
   return dh;
334
2.16k
   }
335
336
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const
337
6.01k
   {
338
6.01k
   std::vector<uint8_t> buf(2);
339
340
6.01k
   for(auto g : m_groups)
341
72.1k
      {
342
72.1k
      const uint16_t id = static_cast<uint16_t>(g);
343
344
72.1k
      if(id > 0)
345
72.1k
         {
346
72.1k
         buf.push_back(get_byte(0, id));
347
72.1k
         buf.push_back(get_byte(1, id));
348
72.1k
         }
349
72.1k
      }
350
351
6.01k
   buf[0] = get_byte(0, static_cast<uint16_t>(buf.size()-2));
352
6.01k
   buf[1] = get_byte(1, static_cast<uint16_t>(buf.size()-2));
353
354
6.01k
   return buf;
355
6.01k
   }
356
357
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader,
358
                                   uint16_t extension_size)
359
21.3k
   {
360
21.3k
   const uint16_t len = reader.get_uint16_t();
361
362
21.3k
   if(len + 2 != extension_size)
363
74
      throw Decoding_Error("Inconsistent length field in supported groups list");
364
365
21.2k
   if(len % 2 == 1)
366
3
      throw Decoding_Error("Supported groups list of strange size");
367
368
21.2k
   const size_t elems = len / 2;
369
370
125k
   for(size_t i = 0; i != elems; ++i)
371
104k
      {
372
104k
      const uint16_t id = reader.get_uint16_t();
373
104k
      m_groups.push_back(static_cast<Group_Params>(id));
374
104k
      }
375
21.2k
   }
376
377
std::vector<uint8_t> Supported_Point_Formats::serialize(Connection_Side /*whoami*/) const
378
6.88k
   {
379
   // if this extension is sent, it MUST include uncompressed (RFC 4492, section 5.1)
380
6.88k
   if(m_prefers_compressed)
381
0
      {
382
0
      return std::vector<uint8_t>{2, ANSIX962_COMPRESSED_PRIME, UNCOMPRESSED};
383
0
      }
384
6.88k
   else
385
6.88k
      {
386
6.88k
      return std::vector<uint8_t>{1, UNCOMPRESSED};
387
6.88k
      }
388
6.88k
   }
389
390
Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader,
391
                                                 uint16_t extension_size)
392
2.22k
   {
393
2.22k
   uint8_t len = reader.get_byte();
394
395
2.22k
   if(len + 1 != extension_size)
396
55
      throw Decoding_Error("Inconsistent length field in supported point formats list");
397
398
4.92k
   for(size_t i = 0; i != len; ++i)
399
3.94k
      {
400
3.94k
      uint8_t format = reader.get_byte();
401
402
3.94k
      if(static_cast<ECPointFormat>(format) == UNCOMPRESSED)
403
771
         {
404
771
         m_prefers_compressed = false;
405
771
         reader.discard_next(len-i-1);
406
771
         return;
407
771
         }
408
3.17k
      else if(static_cast<ECPointFormat>(format) == ANSIX962_COMPRESSED_PRIME)
409
418
         {
410
418
         m_prefers_compressed = true;
411
418
         reader.discard_next(len-i-1);
412
418
         return;
413
418
         }
414
415
      // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves
416
3.94k
      }
417
2.16k
   }
418
419
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const
420
6.01k
   {
421
6.01k
   BOTAN_ASSERT(m_schemes.size() < 256, "Too many signature schemes");
422
423
6.01k
   std::vector<uint8_t> buf;
424
425
6.01k
   const uint16_t len = static_cast<uint16_t>(m_schemes.size() * 2);
426
427
6.01k
   buf.push_back(get_byte(0, len));
428
6.01k
   buf.push_back(get_byte(1, len));
429
430
6.01k
   for(Signature_Scheme scheme : m_schemes)
431
54.1k
      {
432
54.1k
      const uint16_t scheme_code = static_cast<uint16_t>(scheme);
433
434
54.1k
      buf.push_back(get_byte(0, scheme_code));
435
54.1k
      buf.push_back(get_byte(1, scheme_code));
436
54.1k
      }
437
438
6.01k
   return buf;
439
6.01k
   }
440
441
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader,
442
                                           uint16_t extension_size)
443
2.89k
   {
444
2.89k
   uint16_t len = reader.get_uint16_t();
445
446
2.89k
   if(len + 2 != extension_size || len % 2 == 1 || len == 0)
447
76
      {
448
76
      throw Decoding_Error("Bad encoding on signature algorithms extension");
449
76
      }
450
451
44.9k
   while(len)
452
42.1k
      {
453
42.1k
      const uint16_t scheme_code = reader.get_uint16_t();
454
42.1k
      m_schemes.push_back(static_cast<Signature_Scheme>(scheme_code));
455
42.1k
      len -= 2;
456
42.1k
      }
457
2.81k
   }
458
459
Session_Ticket::Session_Ticket(TLS_Data_Reader& reader,
460
                               uint16_t extension_size) : m_ticket(reader.get_elem<uint8_t, std::vector<uint8_t>>(extension_size))
461
3.58k
   {}
462
463
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader,
464
                                                   uint16_t extension_size) : m_pp(reader.get_range<uint16_t>(2, 0, 65535))
465
114
   {
466
114
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
467
468
114
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size)
469
63
      throw Decoding_Error("Bad encoding for SRTP protection extension");
470
471
51
   if(!mki.empty())
472
3
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
473
51
   }
474
475
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const
476
0
   {
477
0
   std::vector<uint8_t> buf;
478
479
0
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
480
0
   buf.push_back(get_byte(0, pp_len));
481
0
   buf.push_back(get_byte(1, pp_len));
482
483
0
   for(uint16_t pp : m_pp)
484
0
      {
485
0
      buf.push_back(get_byte(0, pp));
486
0
      buf.push_back(get_byte(1, pp));
487
0
      }
488
489
0
   buf.push_back(0); // srtp_mki, always empty here
490
491
0
   return buf;
492
0
   }
493
494
Extended_Master_Secret::Extended_Master_Secret(TLS_Data_Reader&,
495
                                               uint16_t extension_size)
496
9.18k
   {
497
9.18k
   if(extension_size != 0)
498
48
      throw Decoding_Error("Invalid extended_master_secret extension");
499
9.18k
   }
500
501
std::vector<uint8_t> Extended_Master_Secret::serialize(Connection_Side /*whoami*/) const
502
13.1k
   {
503
13.1k
   return std::vector<uint8_t>();
504
13.1k
   }
505
506
Encrypt_then_MAC::Encrypt_then_MAC(TLS_Data_Reader&,
507
                                   uint16_t extension_size)
508
3.32k
   {
509
3.32k
   if(extension_size != 0)
510
45
      throw Decoding_Error("Invalid encrypt_then_mac extension");
511
3.32k
   }
512
513
std::vector<uint8_t> Encrypt_then_MAC::serialize(Connection_Side /*whoami*/) const
514
6.56k
   {
515
6.56k
   return std::vector<uint8_t>();
516
6.56k
   }
517
518
std::vector<uint8_t> Certificate_Status_Request::serialize(Connection_Side whoami) const
519
6.33k
   {
520
6.33k
   std::vector<uint8_t> buf;
521
522
6.33k
   if(whoami == Connection_Side::SERVER)
523
322
      return buf; // server reply is empty
524
525
   /*
526
   opaque ResponderID<1..2^16-1>;
527
   opaque Extensions<0..2^16-1>;
528
529
   CertificateStatusType status_type = ocsp(1)
530
   ResponderID responder_id_list<0..2^16-1>
531
   Extensions  request_extensions;
532
   */
533
534
6.01k
   buf.push_back(1); // CertificateStatusType ocsp
535
536
6.01k
   buf.push_back(0);
537
6.01k
   buf.push_back(0);
538
6.01k
   buf.push_back(0);
539
6.01k
   buf.push_back(0);
540
541
6.01k
   return buf;
542
6.01k
   }
543
544
Certificate_Status_Request::Certificate_Status_Request(TLS_Data_Reader& reader,
545
                                                       uint16_t extension_size,
546
                                                       Connection_Side from)
547
721
   {
548
721
   if(from == Connection_Side::SERVER)
549
255
      {
550
255
      if(extension_size != 0)
551
13
         throw Decoding_Error("Server sent non-empty Certificate_Status_Request extension");
552
466
      }
553
466
   else if(extension_size > 0)
554
138
      {
555
138
      const uint8_t type = reader.get_byte();
556
138
      if(type == 1)
557
12
         {
558
12
         const size_t len_resp_id_list = reader.get_uint16_t();
559
12
         m_ocsp_names = reader.get_fixed<uint8_t>(len_resp_id_list);
560
12
         const size_t len_requ_ext = reader.get_uint16_t();
561
12
         m_extension_bytes = reader.get_fixed<uint8_t>(len_requ_ext);
562
12
         }
563
126
      else
564
126
         {
565
126
         reader.discard_next(extension_size - 1);
566
126
         }
567
138
      }
568
721
   }
569
570
Certificate_Status_Request::Certificate_Status_Request(const std::vector<uint8_t>& ocsp_responder_ids,
571
                                                       const std::vector<std::vector<uint8_t>>& ocsp_key_ids) :
572
   m_ocsp_names(ocsp_responder_ids),
573
   m_ocsp_keys(ocsp_key_ids)
574
6.01k
   {
575
6.01k
   }
576
577
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const
578
6.01k
   {
579
6.01k
   std::vector<uint8_t> buf;
580
581
6.01k
   if(whoami == Connection_Side::SERVER)
582
0
      {
583
0
      BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
584
0
      buf.push_back(m_versions[0].major_version());
585
0
      buf.push_back(m_versions[0].minor_version());
586
0
      }
587
6.01k
   else
588
6.01k
      {
589
6.01k
      BOTAN_ASSERT_NOMSG(m_versions.size() >= 1);
590
6.01k
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
591
592
6.01k
      buf.push_back(len);
593
594
6.01k
      for(Protocol_Version version : m_versions)
595
6.01k
         {
596
6.01k
         buf.push_back(get_byte(0, version.major_version()));
597
6.01k
         buf.push_back(get_byte(1, version.minor_version()));
598
6.01k
         }
599
6.01k
      }
600
601
6.01k
   return buf;
602
6.01k
   }
603
604
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy)
605
6.01k
   {
606
6.01k
   if(offer.is_datagram_protocol())
607
0
      {
608
0
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12())
609
0
         m_versions.push_back(Protocol_Version::DTLS_V12);
610
0
#if defined(BOTAN_HAS_TLS_V10)
611
0
      if(offer >= Protocol_Version::DTLS_V10 && policy.allow_dtls10())
612
0
         m_versions.push_back(Protocol_Version::DTLS_V10);
613
0
#endif
614
0
      }
615
6.01k
   else
616
6.01k
      {
617
6.01k
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12())
618
6.01k
         m_versions.push_back(Protocol_Version::TLS_V12);
619
6.01k
#if defined(BOTAN_HAS_TLS_V10)
620
6.01k
      if(offer >= Protocol_Version::TLS_V11 && policy.allow_tls11())
621
0
         m_versions.push_back(Protocol_Version::TLS_V11);
622
6.01k
      if(offer >= Protocol_Version::TLS_V10 && policy.allow_tls10())
623
0
         m_versions.push_back(Protocol_Version::TLS_V10);
624
6.01k
#endif
625
6.01k
      }
626
6.01k
   }
627
628
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader,
629
                                       uint16_t extension_size,
630
                                       Connection_Side from)
631
155
   {
632
155
   if(from == Connection_Side::SERVER)
633
49
      {
634
49
      if(extension_size != 2)
635
15
         throw Decoding_Error("Server sent invalid supported_versions extension");
636
34
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
637
34
      }
638
106
   else
639
106
      {
640
106
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
641
642
106
      for(auto v : versions)
643
3.71k
         m_versions.push_back(Protocol_Version(v));
644
645
106
      if(extension_size != 1+2*versions.size())
646
77
         throw Decoding_Error("Client sent invalid supported_versions extension");
647
106
      }
648
155
   }
649
650
bool Supported_Versions::supports(Protocol_Version version) const
651
0
   {
652
0
   for(auto v : m_versions)
653
0
      if(version == v)
654
0
         return true;
655
0
   return false;
656
0
   }
657
658
}
659
660
}