Coverage Report

Created: 2020-06-30 13:58

/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
207k
   {
22
207k
   switch(code)
23
207k
      {
24
7.16k
      case TLSEXT_SERVER_NAME_INDICATION:
25
7.16k
         return new Server_Name_Indicator(reader, size);
26
0
27
0
#if defined(BOTAN_HAS_SRP6)
28
76
      case TLSEXT_SRP_IDENTIFIER:
29
76
         return new SRP_Identifier(reader, size);
30
0
#endif
31
0
32
20.7k
      case TLSEXT_SUPPORTED_GROUPS:
33
20.7k
         return new Supported_Groups(reader, size);
34
0
35
1.35k
      case TLSEXT_CERT_STATUS_REQUEST:
36
1.35k
         return new Certificate_Status_Request(reader, size, from);
37
0
38
2.06k
      case TLSEXT_EC_POINT_FORMATS:
39
2.06k
         return new Supported_Point_Formats(reader, size);
40
0
41
4.64k
      case TLSEXT_SAFE_RENEGOTIATION:
42
4.64k
         return new Renegotiation_Extension(reader, size);
43
0
44
2.72k
      case TLSEXT_SIGNATURE_ALGORITHMS:
45
2.72k
         return new Signature_Algorithms(reader, size);
46
0
47
127
      case TLSEXT_USE_SRTP:
48
127
          return new SRTP_Protection_Profiles(reader, size);
49
0
50
9.98k
      case TLSEXT_ALPN:
51
9.98k
         return new Application_Layer_Protocol_Notification(reader, size);
52
0
53
7.70k
      case TLSEXT_EXTENDED_MASTER_SECRET:
54
7.70k
         return new Extended_Master_Secret(reader, size);
55
0
56
3.14k
      case TLSEXT_ENCRYPT_THEN_MAC:
57
3.14k
         return new Encrypt_then_MAC(reader, size);
58
0
59
3.03k
      case TLSEXT_SESSION_TICKET:
60
3.03k
         return new Session_Ticket(reader, size);
61
0
62
142
      case TLSEXT_SUPPORTED_VERSIONS:
63
142
         return new Supported_Versions(reader, size, from);
64
144k
      }
65
144k
66
144k
   return new Unknown_Extension(static_cast<Handshake_Extension_Type>(code),
67
144k
                                reader, size);
68
144k
   }
69
70
}
71
72
void Extensions::deserialize(TLS_Data_Reader& reader, Connection_Side from)
73
38.1k
   {
74
38.1k
   if(reader.has_remaining())
75
36.7k
      {
76
36.7k
      const uint16_t all_extn_size = reader.get_uint16_t();
77
36.7k
78
36.7k
      if(reader.remaining_bytes() != all_extn_size)
79
144
         throw Decoding_Error("Bad extension size");
80
36.6k
81
244k
      while(reader.has_remaining())
82
207k
         {
83
207k
         const uint16_t extension_code = reader.get_uint16_t();
84
207k
         const uint16_t extension_size = reader.get_uint16_t();
85
207k
86
207k
         const auto type = static_cast<Handshake_Extension_Type>(extension_code);
87
207k
88
207k
         if(m_extensions.find(type) != m_extensions.end())
89
24
            throw TLS_Exception(TLS::Alert::DECODE_ERROR,
90
24
                                "Peer sent duplicated extensions");
91
207k
92
207k
         Extension* extn = make_extension(
93
207k
            reader, extension_code, extension_size, from);
94
207k
95
207k
         this->add(extn);
96
207k
         }
97
36.6k
      }
98
38.1k
   }
99
100
std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const
101
30.1k
   {
102
30.1k
   std::vector<uint8_t> buf(2); // 2 bytes for length field
103
30.1k
104
30.1k
   for(auto& extn : m_extensions)
105
82.5k
      {
106
82.5k
      if(extn.second->empty())
107
5.71k
         continue;
108
76.8k
109
76.8k
      const uint16_t extn_code = static_cast<uint16_t>(extn.second->type());
110
76.8k
111
76.8k
      const std::vector<uint8_t> extn_val = extn.second->serialize(whoami);
112
76.8k
113
76.8k
      buf.push_back(get_byte(0, extn_code));
114
76.8k
      buf.push_back(get_byte(1, extn_code));
115
76.8k
116
76.8k
      buf.push_back(get_byte(0, static_cast<uint16_t>(extn_val.size())));
117
76.8k
      buf.push_back(get_byte(1, static_cast<uint16_t>(extn_val.size())));
118
76.8k
119
76.8k
      buf += extn_val;
120
76.8k
      }
121
30.1k
122
30.1k
   const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
123
30.1k
124
30.1k
   buf[0] = get_byte(0, extn_size);
125
30.1k
   buf[1] = get_byte(1, extn_size);
126
30.1k
127
30.1k
   // avoid sending a completely empty extensions block
128
30.1k
   if(buf.size() == 2)
129
11.3k
      return std::vector<uint8_t>();
130
18.8k
131
18.8k
   return buf;
132
18.8k
   }
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.0k
   {
145
27.0k
   std::set<Handshake_Extension_Type> offers;
146
230k
   for(auto i = m_extensions.begin(); i != m_extensions.end(); ++i)
147
203k
      offers.insert(i->first);
148
27.0k
   return offers;
149
27.0k
   }
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
144k
   {
157
144k
   }
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.16k
   {
167
7.16k
   /*
168
7.16k
   * This is used by the server to confirm that it knew the name
169
7.16k
   */
170
7.16k
   if(extension_size == 0)
171
6.88k
      return;
172
282
173
282
   uint16_t name_bytes = reader.get_uint16_t();
174
282
175
282
   if(name_bytes + 2 != extension_size)
176
76
      throw Decoding_Error("Bad encoding of SNI extension");
177
206
178
1.06k
   while(name_bytes)
179
858
      {
180
858
      uint8_t name_type = reader.get_byte();
181
858
      name_bytes--;
182
858
183
858
      if(name_type == 0) // DNS
184
706
         {
185
706
         m_sni_host_name = reader.get_string(2, 1, 65535);
186
706
         name_bytes -= static_cast<uint16_t>(2 + m_sni_host_name.size());
187
706
         }
188
152
      else // some other unknown name type
189
152
         {
190
152
         reader.discard_next(name_bytes);
191
152
         name_bytes = 0;
192
152
         }
193
858
      }
194
206
   }
195
196
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side /*whoami*/) const
197
5.71k
   {
198
5.71k
   std::vector<uint8_t> buf;
199
5.71k
200
5.71k
   size_t name_len = m_sni_host_name.size();
201
5.71k
202
5.71k
   buf.push_back(get_byte(0, static_cast<uint16_t>(name_len+3)));
203
5.71k
   buf.push_back(get_byte(1, static_cast<uint16_t>(name_len+3)));
204
5.71k
   buf.push_back(0); // DNS
205
5.71k
206
5.71k
   buf.push_back(get_byte(0, static_cast<uint16_t>(name_len)));
207
5.71k
   buf.push_back(get_byte(1, static_cast<uint16_t>(name_len)));
208
5.71k
209
5.71k
   buf += std::make_pair(
210
5.71k
      cast_char_ptr_to_uint8(m_sni_host_name.data()),
211
5.71k
      m_sni_host_name.size());
212
5.71k
213
5.71k
   return buf;
214
5.71k
   }
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
76
   {
221
76
   if(m_srp_identifier.size() + 1 != extension_size)
222
38
      throw Decoding_Error("Bad encoding for SRP identifier extension");
223
76
   }
224
225
std::vector<uint8_t> SRP_Identifier::serialize(Connection_Side /*whoami*/) const
226
0
   {
227
0
   std::vector<uint8_t> buf;
228
0
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
0
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.64k
   {
240
4.64k
   if(m_reneg_data.size() + 1 != extension_size)
241
61
      throw Decoding_Error("Bad encoding for secure renegotiation extn");
242
4.64k
   }
243
244
std::vector<uint8_t> Renegotiation_Extension::serialize(Connection_Side /*whoami*/) const
245
11.3k
   {
246
11.3k
   std::vector<uint8_t> buf;
247
11.3k
   append_tls_length_value(buf, m_reneg_data, 1);
248
11.3k
   return buf;
249
11.3k
   }
250
251
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
252
                                                                                 uint16_t extension_size)
253
9.98k
   {
254
9.98k
   if(extension_size == 0)
255
5.96k
      return; // empty extension
256
4.01k
257
4.01k
   const uint16_t name_bytes = reader.get_uint16_t();
258
4.01k
259
4.01k
   size_t bytes_remaining = extension_size - 2;
260
4.01k
261
4.01k
   if(name_bytes != bytes_remaining)
262
123
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
263
3.89k
264
33.2k
   while(bytes_remaining)
265
29.4k
      {
266
29.4k
      const std::string p = reader.get_string(1, 0, 255);
267
29.4k
268
29.4k
      if(bytes_remaining < p.size() + 1)
269
3
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
270
29.4k
271
29.4k
      if(p.empty())
272
83
         throw Decoding_Error("Empty ALPN protocol not allowed");
273
29.3k
274
29.3k
      bytes_remaining -= (p.size() + 1);
275
29.3k
276
29.3k
      m_protocols.push_back(p);
277
29.3k
      }
278
3.89k
   }
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
5.88k
   {
291
5.88k
   std::vector<uint8_t> buf(2);
292
5.88k
293
5.88k
   for(auto&& p: m_protocols)
294
5.88k
      {
295
5.88k
      if(p.length() >= 256)
296
0
         throw TLS_Exception(Alert::INTERNAL_ERROR, "ALPN name too long");
297
5.88k
      if(p != "")
298
5.88k
         append_tls_length_value(buf,
299
5.88k
                                 cast_char_ptr_to_uint8(p.data()),
300
5.88k
                                 p.size(),
301
5.88k
                                 1);
302
5.88k
      }
303
5.88k
304
5.88k
   buf[0] = get_byte(0, static_cast<uint16_t>(buf.size()-2));
305
5.88k
   buf[1] = get_byte(1, static_cast<uint16_t>(buf.size()-2));
306
5.88k
307
5.88k
   return buf;
308
5.88k
   }
309
310
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups)
311
5.71k
   {
312
5.71k
   }
313
314
std::vector<Group_Params> Supported_Groups::ec_groups() const
315
44.2k
   {
316
44.2k
   std::vector<Group_Params> ec;
317
44.2k
   for(auto g : m_groups)
318
225k
      {
319
225k
      if(group_param_is_dh(g) == false)
320
171k
         ec.push_back(g);
321
225k
      }
322
44.2k
   return ec;
323
44.2k
   }
324
325
std::vector<Group_Params> Supported_Groups::dh_groups() const
326
1.83k
   {
327
1.83k
   std::vector<Group_Params> dh;
328
1.83k
   for(auto g : m_groups)
329
8.14k
      {
330
8.14k
      if(group_param_is_dh(g) == true)
331
2.81k
         dh.push_back(g);
332
8.14k
      }
333
1.83k
   return dh;
334
1.83k
   }
335
336
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const
337
5.71k
   {
338
5.71k
   std::vector<uint8_t> buf(2);
339
5.71k
340
5.71k
   for(auto g : m_groups)
341
68.6k
      {
342
68.6k
      const uint16_t id = static_cast<uint16_t>(g);
343
68.6k
344
68.6k
      if(id > 0)
345
68.6k
         {
346
68.6k
         buf.push_back(get_byte(0, id));
347
68.6k
         buf.push_back(get_byte(1, id));
348
68.6k
         }
349
68.6k
      }
350
5.71k
351
5.71k
   buf[0] = get_byte(0, static_cast<uint16_t>(buf.size()-2));
352
5.71k
   buf[1] = get_byte(1, static_cast<uint16_t>(buf.size()-2));
353
5.71k
354
5.71k
   return buf;
355
5.71k
   }
356
357
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader,
358
                                   uint16_t extension_size)
359
20.7k
   {
360
20.7k
   const uint16_t len = reader.get_uint16_t();
361
20.7k
362
20.7k
   if(len + 2 != extension_size)
363
65
      throw Decoding_Error("Inconsistent length field in supported groups list");
364
20.6k
365
20.6k
   if(len % 2 == 1)
366
3
      throw Decoding_Error("Supported groups list of strange size");
367
20.6k
368
20.6k
   const size_t elems = len / 2;
369
20.6k
370
121k
   for(size_t i = 0; i != elems; ++i)
371
100k
      {
372
100k
      const uint16_t id = reader.get_uint16_t();
373
100k
      m_groups.push_back(static_cast<Group_Params>(id));
374
100k
      }
375
20.6k
   }
376
377
std::vector<uint8_t> Supported_Point_Formats::serialize(Connection_Side /*whoami*/) const
378
6.47k
   {
379
6.47k
   // if this extension is sent, it MUST include uncompressed (RFC 4492, section 5.1)
380
6.47k
   if(m_prefers_compressed)
381
0
      {
382
0
      return std::vector<uint8_t>{2, ANSIX962_COMPRESSED_PRIME, UNCOMPRESSED};
383
0
      }
384
6.47k
   else
385
6.47k
      {
386
6.47k
      return std::vector<uint8_t>{1, UNCOMPRESSED};
387
6.47k
      }
388
6.47k
   }
389
390
Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader,
391
                                                 uint16_t extension_size)
392
2.06k
   {
393
2.06k
   uint8_t len = reader.get_byte();
394
2.06k
395
2.06k
   if(len + 1 != extension_size)
396
61
      throw Decoding_Error("Inconsistent length field in supported point formats list");
397
2.00k
398
4.80k
   for(size_t i = 0; i != len; ++i)
399
3.88k
      {
400
3.88k
      uint8_t format = reader.get_byte();
401
3.88k
402
3.88k
      if(static_cast<ECPointFormat>(format) == UNCOMPRESSED)
403
744
         {
404
744
         m_prefers_compressed = false;
405
744
         reader.discard_next(len-i-1);
406
744
         return;
407
744
         }
408
3.13k
      else if(static_cast<ECPointFormat>(format) == ANSIX962_COMPRESSED_PRIME)
409
333
         {
410
333
         m_prefers_compressed = true;
411
333
         reader.discard_next(len-i-1);
412
333
         return;
413
333
         }
414
3.88k
415
3.88k
      // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves
416
3.88k
      }
417
2.00k
   }
418
419
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const
420
5.71k
   {
421
5.71k
   BOTAN_ASSERT(m_schemes.size() < 256, "Too many signature schemes");
422
5.71k
423
5.71k
   std::vector<uint8_t> buf;
424
5.71k
425
5.71k
   const uint16_t len = static_cast<uint16_t>(m_schemes.size() * 2);
426
5.71k
427
5.71k
   buf.push_back(get_byte(0, len));
428
5.71k
   buf.push_back(get_byte(1, len));
429
5.71k
430
5.71k
   for(Signature_Scheme scheme : m_schemes)
431
51.4k
      {
432
51.4k
      const uint16_t scheme_code = static_cast<uint16_t>(scheme);
433
51.4k
434
51.4k
      buf.push_back(get_byte(0, scheme_code));
435
51.4k
      buf.push_back(get_byte(1, scheme_code));
436
51.4k
      }
437
5.71k
438
5.71k
   return buf;
439
5.71k
   }
440
441
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader,
442
                                           uint16_t extension_size)
443
2.72k
   {
444
2.72k
   uint16_t len = reader.get_uint16_t();
445
2.72k
446
2.72k
   if(len + 2 != extension_size || len % 2 == 1 || len == 0)
447
65
      {
448
65
      throw Decoding_Error("Bad encoding on signature algorithms extension");
449
65
      }
450
2.66k
451
46.2k
   while(len)
452
43.5k
      {
453
43.5k
      const uint16_t scheme_code = reader.get_uint16_t();
454
43.5k
      m_schemes.push_back(static_cast<Signature_Scheme>(scheme_code));
455
43.5k
      len -= 2;
456
43.5k
      }
457
2.66k
   }
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.03k
   {}
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
127
   {
466
127
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
467
127
468
127
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size)
469
76
      throw Decoding_Error("Bad encoding for SRTP protection extension");
470
51
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
0
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
0
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
0
489
0
   buf.push_back(0); // srtp_mki, always empty here
490
0
491
0
   return buf;
492
0
   }
493
494
Extended_Master_Secret::Extended_Master_Secret(TLS_Data_Reader&,
495
                                               uint16_t extension_size)
496
7.70k
   {
497
7.70k
   if(extension_size != 0)
498
41
      throw Decoding_Error("Invalid extended_master_secret extension");
499
7.70k
   }
500
501
std::vector<uint8_t> Extended_Master_Secret::serialize(Connection_Side /*whoami*/) const
502
10.8k
   {
503
10.8k
   return std::vector<uint8_t>();
504
10.8k
   }
505
506
Encrypt_then_MAC::Encrypt_then_MAC(TLS_Data_Reader&,
507
                                   uint16_t extension_size)
508
3.14k
   {
509
3.14k
   if(extension_size != 0)
510
41
      throw Decoding_Error("Invalid encrypt_then_mac extension");
511
3.14k
   }
512
513
std::vector<uint8_t> Encrypt_then_MAC::serialize(Connection_Side /*whoami*/) const
514
5.97k
   {
515
5.97k
   return std::vector<uint8_t>();
516
5.97k
   }
517
518
std::vector<uint8_t> Certificate_Status_Request::serialize(Connection_Side whoami) const
519
6.68k
   {
520
6.68k
   std::vector<uint8_t> buf;
521
6.68k
522
6.68k
   if(whoami == Connection_Side::SERVER)
523
965
      return buf; // server reply is empty
524
5.71k
525
5.71k
   /*
526
5.71k
   opaque ResponderID<1..2^16-1>;
527
5.71k
   opaque Extensions<0..2^16-1>;
528
5.71k
529
5.71k
   CertificateStatusType status_type = ocsp(1)
530
5.71k
   ResponderID responder_id_list<0..2^16-1>
531
5.71k
   Extensions  request_extensions;
532
5.71k
   */
533
5.71k
534
5.71k
   buf.push_back(1); // CertificateStatusType ocsp
535
5.71k
536
5.71k
   buf.push_back(0);
537
5.71k
   buf.push_back(0);
538
5.71k
   buf.push_back(0);
539
5.71k
   buf.push_back(0);
540
5.71k
541
5.71k
   return buf;
542
5.71k
   }
543
544
Certificate_Status_Request::Certificate_Status_Request(TLS_Data_Reader& reader,
545
                                                       uint16_t extension_size,
546
                                                       Connection_Side from)
547
1.35k
   {
548
1.35k
   if(from == Connection_Side::SERVER)
549
234
      {
550
234
      if(extension_size != 0)
551
8
         throw Decoding_Error("Server sent non-empty Certificate_Status_Request extension");
552
1.11k
      }
553
1.11k
   else if(extension_size > 0)
554
136
      {
555
136
      const uint8_t type = reader.get_byte();
556
136
      if(type == 1)
557
13
         {
558
13
         const size_t len_resp_id_list = reader.get_uint16_t();
559
13
         m_ocsp_names = reader.get_fixed<uint8_t>(len_resp_id_list);
560
13
         const size_t len_requ_ext = reader.get_uint16_t();
561
13
         m_extension_bytes = reader.get_fixed<uint8_t>(len_requ_ext);
562
13
         }
563
123
      else
564
123
         {
565
123
         reader.discard_next(extension_size - 1);
566
123
         }
567
136
      }
568
1.35k
   }
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
5.71k
   {
575
5.71k
   }
576
577
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const
578
5.71k
   {
579
5.71k
   std::vector<uint8_t> buf;
580
5.71k
581
5.71k
   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
5.71k
   else
588
5.71k
      {
589
5.71k
      BOTAN_ASSERT_NOMSG(m_versions.size() >= 1);
590
5.71k
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
591
5.71k
592
5.71k
      buf.push_back(len);
593
5.71k
594
5.71k
      for(Protocol_Version version : m_versions)
595
5.71k
         {
596
5.71k
         buf.push_back(get_byte(0, version.major_version()));
597
5.71k
         buf.push_back(get_byte(1, version.minor_version()));
598
5.71k
         }
599
5.71k
      }
600
5.71k
601
5.71k
   return buf;
602
5.71k
   }
603
604
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy)
605
5.71k
   {
606
5.71k
   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
5.71k
   else
616
5.71k
      {
617
5.71k
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12())
618
5.71k
         m_versions.push_back(Protocol_Version::TLS_V12);
619
5.71k
#if defined(BOTAN_HAS_TLS_V10)
620
5.71k
      if(offer >= Protocol_Version::TLS_V11 && policy.allow_tls11())
621
0
         m_versions.push_back(Protocol_Version::TLS_V11);
622
5.71k
      if(offer >= Protocol_Version::TLS_V10 && policy.allow_tls10())
623
0
         m_versions.push_back(Protocol_Version::TLS_V10);
624
5.71k
#endif
625
5.71k
      }
626
5.71k
   }
627
628
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader,
629
                                       uint16_t extension_size,
630
                                       Connection_Side from)
631
142
   {
632
142
   if(from == Connection_Side::SERVER)
633
36
      {
634
36
      if(extension_size != 2)
635
14
         throw Decoding_Error("Server sent invalid supported_versions extension");
636
22
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
637
22
      }
638
106
   else
639
106
      {
640
106
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
641
106
642
106
      for(auto v : versions)
643
4.23k
         m_versions.push_back(Protocol_Version(v));
644
106
645
106
      if(extension_size != 1+2*versions.size())
646
75
         throw Decoding_Error("Client sent invalid supported_versions extension");
647
106
      }
648
142
   }
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
}