• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 4892192195

05 May 2023 10:32AM UTC coverage: 91.732% (+0.008%) from 91.724%
4892192195

push

github

77631 of 84628 relevant lines covered (91.73%)

11913034.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.38
/src/lib/tls/tls_extensions.cpp
1
/*
2
* TLS Extensions
3
* (C) 2011,2012,2015,2016 Jack Lloyd
4
*     2016 Juraj Somorovsky
5
*     2021 Elektrobit Automotive GmbH
6
*     2022 René Meusel, Hannes Rantzsch - neXenio GmbH
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10

11
#include <botan/tls_extensions.h>
12

13
#include <botan/internal/tls_reader.h>
14
#include <botan/internal/stl_util.h>
15
#include <botan/tls_exceptn.h>
16
#include <botan/tls_policy.h>
17
#include <botan/ber_dec.h>
18

19
#include <iterator>
20

21
namespace Botan::TLS {
22

23
namespace {
24

25
std::unique_ptr<Extension> make_extension(TLS_Data_Reader& reader,
38,925✔
26
                                          Extension_Code code,
27
                                          const Connection_Side from,
28
                                          const Handshake_Type message_type)
29
   {
30
   // This cast is safe because we read exactly a 16 bit length field for
31
   // the extension in Extensions::deserialize
32
   const uint16_t size = static_cast<uint16_t>(reader.remaining_bytes());
38,925✔
33
   switch(code)
38,925✔
34
      {
35
      case Extension_Code::ServerNameIndication:
2,649✔
36
         return std::make_unique<Server_Name_Indicator>(reader, size);
2,649✔
37

38
      case Extension_Code::SupportedGroups:
2,712✔
39
         return std::make_unique<Supported_Groups>(reader, size);
2,712✔
40

41
      case Extension_Code::CertificateStatusRequest:
2,494✔
42
         return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
2,494✔
43

44
      case Extension_Code::EcPointFormats:
3,038✔
45
         return std::make_unique<Supported_Point_Formats>(reader, size);
3,038✔
46

47
      case Extension_Code::SafeRenegotiation:
5,409✔
48
         return std::make_unique<Renegotiation_Extension>(reader, size);
5,409✔
49

50
      case Extension_Code::SignatureAlgorithms:
2,735✔
51
         return std::make_unique<Signature_Algorithms>(reader, size);
2,735✔
52

53
      case Extension_Code::CertSignatureAlgorithms:
×
54
         return std::make_unique<Signature_Algorithms_Cert>(reader, size);
×
55

56
      case Extension_Code::UseSrtp:
10✔
57
         return std::make_unique<SRTP_Protection_Profiles>(reader, size);
10✔
58

59
      case Extension_Code::ApplicationLayerProtocolNegotiation:
395✔
60
         return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
395✔
61

62
      case Extension_Code::ExtendedMasterSecret:
5,233✔
63
         return std::make_unique<Extended_Master_Secret>(reader, size);
5,233✔
64

65
      case Extension_Code::RecordSizeLimit:
40✔
66
         return std::make_unique<Record_Size_Limit>(reader, size, from);
40✔
67

68
      case Extension_Code::EncryptThenMac:
760✔
69
         return std::make_unique<Encrypt_then_MAC>(reader, size);
760✔
70

71
      case Extension_Code::SessionTicket:
4,703✔
72
         return std::make_unique<Session_Ticket_Extension>(reader, size);
4,703✔
73

74
      case Extension_Code::SupportedVersions:
1,640✔
75
         return std::make_unique<Supported_Versions>(reader, size, from);
1,640✔
76

77
#if defined(BOTAN_HAS_TLS_13)
78
      case Extension_Code::PresharedKey:
206✔
79
         return std::make_unique<PSK>(reader, size, message_type);
206✔
80

81
      case Extension_Code::EarlyData:
5✔
82
         return std::make_unique<EarlyDataIndication>(reader, size, message_type);
5✔
83

84
      case Extension_Code::Cookie:
10✔
85
         return std::make_unique<Cookie>(reader, size);
10✔
86

87
      case Extension_Code::PskKeyExchangeModes:
930✔
88
         return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
930✔
89

90
      case Extension_Code::CertificateAuthorities:
3✔
91
         return std::make_unique<Certificate_Authorities>(reader, size);
3✔
92

93
      case Extension_Code::KeyShare:
1,437✔
94
         return std::make_unique<Key_Share>(reader, size, message_type);
1,437✔
95
#endif
96
      }
97

98
   return std::make_unique<Unknown_Extension>(static_cast<Extension_Code>(code),
4,516✔
99
                                              reader, size);
4,516✔
100
   }
101

102
}
103

104
void Extensions::add(std::unique_ptr<Extension> extn)
80,921✔
105
   {
106
   if (has(extn->type()))
80,921✔
107
      {
108
      throw Invalid_Argument("cannot add the same extension twice: " +
×
109
                             std::to_string(static_cast<uint16_t>(extn->type())));
×
110
      }
111

112
   m_extensions.emplace_back(extn.release());
80,921✔
113
   }
80,921✔
114

115
void Extensions::deserialize(TLS_Data_Reader& reader,
7,601✔
116
                             const Connection_Side from,
117
                             const Handshake_Type message_type)
118
   {
119
   if(reader.has_remaining())
7,601✔
120
      {
121
      const uint16_t all_extn_size = reader.get_uint16_t();
7,596✔
122

123
      if(reader.remaining_bytes() != all_extn_size)
7,596✔
124
         throw Decoding_Error("Bad extension size");
118✔
125

126
      while(reader.has_remaining())
46,370✔
127
         {
128
         const uint16_t extension_code = reader.get_uint16_t();
38,947✔
129
         const uint16_t extension_size = reader.get_uint16_t();
38,946✔
130

131
         const auto type = static_cast<Extension_Code>(extension_code);
38,946✔
132

133
         if(this->has(type))
38,946✔
134
            {
135
            throw TLS_Exception(TLS::Alert::DecodeError,
13✔
136
                                "Peer sent duplicated extensions");
26✔
137
            }
138

139
         // TODO offer a function on reader that returns a byte range as a reference
140
         // to avoid this copy of the extension data
141
         const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
38,933✔
142
         TLS_Data_Reader extn_reader("Extension", extn_data);
38,925✔
143
         this->add(make_extension(extn_reader, type, from, message_type));
38,925✔
144
         extn_reader.assert_done();
38,892✔
145
         }
38,892✔
146
      }
147
   }
7,428✔
148

149
bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
3,326✔
150
                                     const bool allow_unknown_extensions) const
151
   {
152
   const auto found = extension_types();
3,326✔
153

154
   std::vector<Extension_Code> diff;
3,326✔
155
   std::set_difference(found.cbegin(), found.end(),
3,326✔
156
                       allowed_extensions.cbegin(), allowed_extensions.cend(),
157
                       std::back_inserter(diff));
158

159
   if(allow_unknown_extensions)
3,326✔
160
      {
161
      // Go through the found unexpected extensions whether any of those
162
      // is known to this TLS implementation.
163
      const auto itr = std::find_if(diff.cbegin(), diff.cend(),
1,486✔
164
                                    [this](const auto ext_type)
10✔
165
         {
166
         const auto ext = get(ext_type);
10✔
167
         return ext && ext->is_implemented();
10✔
168
         });
169

170
      // ... if yes, `contains_other_than` is true
171
      return itr != diff.cend();
1,486✔
172
      }
173

174
   return !diff.empty();
1,840✔
175
   }
3,326✔
176

177
std::unique_ptr<Extension> Extensions::take(Extension_Code type)
462✔
178
   {
179
   const auto i = std::find_if(m_extensions.begin(), m_extensions.end(),
462✔
180
                               [type](const auto &ext) {
2,451✔
181
                                  return ext->type() == type;
2,451✔
182
                               });
183

184
   std::unique_ptr<Extension> result;
462✔
185
   if (i != m_extensions.end())
462✔
186
      {
187
      std::swap(result, *i);
212✔
188
      m_extensions.erase(i);
212✔
189
      }
190

191
   return result;
462✔
192
   }
193

194
std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const
6,212✔
195
   {
196
   std::vector<uint8_t> buf(2); // 2 bytes for length field
6,212✔
197

198
   for(const auto& extn : m_extensions)
53,839✔
199
      {
200
      if(extn->empty())
47,627✔
201
         continue;
692✔
202

203
      const uint16_t extn_code = static_cast<uint16_t>(extn->type());
46,935✔
204

205
      const std::vector<uint8_t> extn_val = extn->serialize(whoami);
46,935✔
206

207
      buf.push_back(get_byte<0>(extn_code));
46,935✔
208
      buf.push_back(get_byte<1>(extn_code));
46,935✔
209

210
      buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
46,935✔
211
      buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
46,935✔
212

213
      buf += extn_val;
46,935✔
214
      }
46,935✔
215

216
   const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
6,212✔
217

218
   buf[0] = get_byte<0>(extn_size);
6,212✔
219
   buf[1] = get_byte<1>(extn_size);
6,212✔
220

221
   // avoid sending a completely empty extensions block
222
   if(buf.size() == 2)
6,212✔
223
      return std::vector<uint8_t>();
282✔
224

225
   return buf;
6,212✔
226
   }
6,212✔
227

228
std::set<Extension_Code> Extensions::extension_types() const
7,691✔
229
   {
230
   std::set<Extension_Code> offers;
7,691✔
231
   std::transform(m_extensions.cbegin(), m_extensions.cend(),
7,691✔
232
                  std::inserter(offers, offers.begin()), [] (const auto &ext) {
39,305✔
233
                     return ext->type();
39,305✔
234
                  });
235
   return offers;
7,691✔
236
   }
×
237

238
Unknown_Extension::Unknown_Extension(Extension_Code type,
4,516✔
239
                                     TLS_Data_Reader& reader,
240
                                     uint16_t extension_size) :
4,516✔
241
   m_type(type),
4,516✔
242
   m_value(reader.get_fixed<uint8_t>(extension_size))
4,516✔
243
   {
244
   }
4,516✔
245

246
std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const
2✔
247
   {
248
   return m_value;
2✔
249
   }
250

251
Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader,
2,649✔
252
                                             uint16_t extension_size)
2,649✔
253
   {
254
   /*
255
   * This is used by the server to confirm that it knew the name
256
   */
257
   if(extension_size == 0)
2,649✔
258
      return;
259

260
   uint16_t name_bytes = reader.get_uint16_t();
2,621✔
261

262
   if(name_bytes + 2 != extension_size)
2,621✔
263
      throw Decoding_Error("Bad encoding of SNI extension");
×
264

265
   while(name_bytes)
5,242✔
266
      {
267
      uint8_t name_type = reader.get_byte();
2,621✔
268
      name_bytes--;
2,621✔
269

270
      if(name_type == 0) // DNS
2,621✔
271
         {
272
         m_sni_host_name = reader.get_string(2, 1, 65535);
2,621✔
273
         name_bytes -= static_cast<uint16_t>(2 + m_sni_host_name.size());
2,621✔
274
         }
275
      else // some other unknown name type
276
         {
277
         reader.discard_next(name_bytes);
×
278
         name_bytes = 0;
×
279
         }
280
      }
281
   }
×
282

283
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const
4,456✔
284
   {
285
   // RFC 6066
286
   //    [...] the server SHALL include an extension of type "server_name" in
287
   //    the (extended) server hello. The "extension_data" field of this
288
   //    extension SHALL be empty.
289
   if(whoami == Connection_Side::Server)
4,456✔
290
      {
291
      return {};
350✔
292
      }
293

294
   std::vector<uint8_t> buf;
4,106✔
295

296
   size_t name_len = m_sni_host_name.size();
4,106✔
297

298
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len+3)));
4,106✔
299
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len+3)));
4,106✔
300
   buf.push_back(0); // DNS
4,106✔
301

302
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
4,106✔
303
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
4,106✔
304

305
   buf += std::make_pair(
4,106✔
306
      cast_char_ptr_to_uint8(m_sni_host_name.data()),
4,106✔
307
      m_sni_host_name.size());
4,106✔
308

309
   return buf;
4,106✔
310
   }
4,456✔
311

312
Renegotiation_Extension::Renegotiation_Extension(TLS_Data_Reader& reader,
5,409✔
313
                                                 uint16_t extension_size) : m_reneg_data(reader.get_range<uint8_t>(1, 0, 255))
5,409✔
314
   {
315
   if(m_reneg_data.size() + 1 != extension_size)
5,407✔
316
      throw Decoding_Error("Bad encoding for secure renegotiation extn");
1✔
317
   }
5,407✔
318

319
std::vector<uint8_t> Renegotiation_Extension::serialize(Connection_Side /*whoami*/) const
5,036✔
320
   {
321
   std::vector<uint8_t> buf;
5,036✔
322
   append_tls_length_value(buf, m_reneg_data, 1);
5,036✔
323
   return buf;
5,036✔
324
   }
×
325

326
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
395✔
327
                                                                                 uint16_t extension_size,
328
                                                                                 Connection_Side from)
395✔
329
   {
330
   if(extension_size == 0)
395✔
331
      return; // empty extension
332

333
   const uint16_t name_bytes = reader.get_uint16_t();
395✔
334

335
   size_t bytes_remaining = extension_size - 2;
395✔
336

337
   if(name_bytes != bytes_remaining)
395✔
338
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
×
339

340
   while(bytes_remaining)
1,052✔
341
      {
342
      const std::string p = reader.get_string(1, 0, 255);
664✔
343

344
      if(bytes_remaining < p.size() + 1)
664✔
345
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
×
346

347
      if(p.empty())
664✔
348
         throw Decoding_Error("Empty ALPN protocol not allowed");
7✔
349

350
      bytes_remaining -= (p.size() + 1);
657✔
351

352
      m_protocols.push_back(p);
657✔
353
      }
664✔
354

355
   // RFC 7301 3.1
356
   //    The "extension_data" field of the [...] extension is structured the
357
   //    same as described above for the client "extension_data", except that
358
   //    the "ProtocolNameList" MUST contain exactly one "ProtocolName".
359
   if(from == Connection_Side::Server && m_protocols.size() != 1)
388✔
360
      {
361
      throw TLS_Exception(Alert::DecodeError,
×
362
                          "Server sent " + std::to_string(m_protocols.size()) +
×
363
                          " protocols in ALPN extension response");
×
364
      }
365
   }
7✔
366

367
std::string Application_Layer_Protocol_Notification::single_protocol() const
145✔
368
   {
369
   BOTAN_STATE_CHECK(m_protocols.size() == 1);
145✔
370
   return m_protocols.front();
145✔
371
   }
372

373
std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize(Connection_Side /*whoami*/) const
353✔
374
   {
375
   std::vector<uint8_t> buf(2);
353✔
376

377
   for(auto&& p: m_protocols)
925✔
378
      {
379
      if(p.length() >= 256)
572✔
380
         throw TLS_Exception(Alert::InternalError, "ALPN name too long");
×
381
      if(!p.empty())
572✔
382
         append_tls_length_value(buf,
572✔
383
                                 cast_char_ptr_to_uint8(p.data()),
384
                                 p.size(),
385
                                 1);
386
      }
387

388
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size()-2));
353✔
389
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size()-2));
353✔
390

391
   return buf;
353✔
392
   }
×
393

394
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups)
3,937✔
395
   {
396
   }
3,937✔
397

398
const std::vector<Group_Params>& Supported_Groups::groups() const
874✔
399
   {
400
   return m_groups;
874✔
401
   }
402

403
std::vector<Group_Params> Supported_Groups::ec_groups() const
4,822✔
404
   {
405
   std::vector<Group_Params> ec;
4,822✔
406
   for(auto g : m_groups)
53,841✔
407
      {
408
      if(group_param_is_dh(g) == false)
49,019✔
409
         ec.push_back(g);
30,893✔
410
      }
411
   return ec;
4,822✔
412
   }
×
413

414
std::vector<Group_Params> Supported_Groups::dh_groups() const
6✔
415
   {
416
   std::vector<Group_Params> dh;
6✔
417
   for(auto g : m_groups)
42✔
418
      {
419
      if(group_param_is_dh(g) == true)
36✔
420
         dh.push_back(g);
17✔
421
      }
422
   return dh;
6✔
423
   }
×
424

425
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const
4,507✔
426
   {
427
   std::vector<uint8_t> buf(2);
4,507✔
428

429
   for(auto g : m_groups)
56,274✔
430
      {
431
      const uint16_t id = static_cast<uint16_t>(g);
51,767✔
432

433
      if(id > 0)
51,767✔
434
         {
435
         buf.push_back(get_byte<0>(id));
51,767✔
436
         buf.push_back(get_byte<1>(id));
51,767✔
437
         }
438
      }
439

440
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size()-2));
4,507✔
441
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size()-2));
4,507✔
442

443
   return buf;
4,507✔
444
   }
×
445

446
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader,
2,714✔
447
                                   uint16_t extension_size)
2,714✔
448
   {
449
   const uint16_t len = reader.get_uint16_t();
2,714✔
450

451
   if(len + 2 != extension_size)
2,714✔
452
      throw Decoding_Error("Inconsistent length field in supported groups list");
4✔
453

454
   if(len % 2 == 1)
2,710✔
455
      throw Decoding_Error("Supported groups list of strange size");
×
456

457
   const size_t elems = len / 2;
2,710✔
458

459
   for(size_t i = 0; i != elems; ++i)
20,491✔
460
      {
461
      const auto group = static_cast<Group_Params>(reader.get_uint16_t());
17,781✔
462
      // Note: RFC 8446 does not explicitly enforce that groups must be unique.
463
      if(!value_exists(m_groups, group))
35,562✔
464
         m_groups.push_back(group);
17,781✔
465
      }
466
   }
2,714✔
467

468
std::vector<uint8_t> Supported_Point_Formats::serialize(Connection_Side /*whoami*/) const
4,892✔
469
   {
470
   // if this extension is sent, it MUST include uncompressed (RFC 4492, section 5.1)
471
   if(m_prefers_compressed)
4,892✔
472
      {
473
      return std::vector<uint8_t>{2, ANSIX962_COMPRESSED_PRIME, UNCOMPRESSED};
10✔
474
      }
475
   else
476
      {
477
      return std::vector<uint8_t>{1, UNCOMPRESSED};
4,882✔
478
      }
479
   }
480

481
Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader,
3,038✔
482
                                                 uint16_t extension_size)
3,038✔
483
   {
484
   uint8_t len = reader.get_byte();
3,038✔
485

486
   if(len + 1 != extension_size)
3,038✔
487
      throw Decoding_Error("Inconsistent length field in supported point formats list");
2✔
488

489
   bool includes_uncompressed = false;
3,041✔
490
   for(size_t i = 0; i != len; ++i)
3,041✔
491
      {
492
      uint8_t format = reader.get_byte();
3,040✔
493

494
      if(static_cast<ECPointFormat>(format) == UNCOMPRESSED)
3,040✔
495
         {
496
         m_prefers_compressed = false;
3,025✔
497
         reader.discard_next(len-i-1);
3,025✔
498
         return;
3,025✔
499
         }
500
      else if(static_cast<ECPointFormat>(format) == ANSIX962_COMPRESSED_PRIME)
15✔
501
         {
502
         m_prefers_compressed = true;
10✔
503
         std::vector<uint8_t> remaining_formats = reader.get_fixed<uint8_t>(len-i-1);
10✔
504
         includes_uncompressed = std::any_of(std::begin(remaining_formats),
10✔
505
                                             std::end(remaining_formats), [](uint8_t remaining_format)
506
            {
507
            return static_cast<ECPointFormat>(remaining_format) == UNCOMPRESSED;
508
            });
509
         break;
10✔
510
         }
10✔
511

512
      // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves
513
      }
514

515
   // RFC 4492 5.1.:
516
   //   If the Supported Point Formats Extension is indeed sent, it MUST contain the value 0 (uncompressed)
517
   //   as one of the items in the list of point formats.
518
   // Note:
519
   //   RFC 8422 5.1.2. explicitly requires this check,
520
   //   but only if the Supported Groups extension was sent.
521
   if(!includes_uncompressed)
11✔
522
      {
523
      throw TLS_Exception(Alert::IllegalParameter,
1✔
524
                          "Supported Point Formats Extension must contain the uncompressed point format");
2✔
525
      }
526
   }
527

528
namespace {
529

530
std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes)
4,488✔
531
   {
532
   BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
4,488✔
533

534
   std::vector<uint8_t> buf;
4,488✔
535

536
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
4,488✔
537

538
   buf.push_back(get_byte<0>(len));
4,488✔
539
   buf.push_back(get_byte<1>(len));
4,488✔
540

541
   for(Signature_Scheme scheme : schemes)
43,970✔
542
      {
543
      buf.push_back(get_byte<0>(scheme.wire_code()));
39,482✔
544
      buf.push_back(get_byte<1>(scheme.wire_code()));
39,482✔
545
      }
546

547
   return buf;
4,488✔
548
   }
×
549

550
std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader,
2,737✔
551
                                                         uint16_t extension_size)
552
   {
553
   uint16_t len = reader.get_uint16_t();
2,737✔
554

555
   if(len + 2 != extension_size || len % 2 == 1 || len == 0)
2,736✔
556
      {
557
      throw Decoding_Error("Bad encoding on signature algorithms extension");
1✔
558
      }
559

560
   std::vector<Signature_Scheme> schemes;
2,735✔
561
   schemes.reserve(len / 2);
2,735✔
562
   while(len)
21,361✔
563
      {
564
      schemes.emplace_back(reader.get_uint16_t());
18,626✔
565
      len -= 2;
18,626✔
566
      }
567

568
   return schemes;
2,735✔
569
   }
×
570

571
}
572

573
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const
4,486✔
574
   {
575
   return serialize_signature_algorithms(m_schemes);
4,486✔
576
   }
577

578
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader,
2,735✔
579
                                           uint16_t extension_size)
2,735✔
580
   : m_schemes(parse_signature_algorithms(reader, extension_size))
2,735✔
581
   {}
2,733✔
582

583
std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const
2✔
584
   {
585
   return serialize_signature_algorithms(m_schemes);
2✔
586
   }
587

588
Signature_Algorithms_Cert::Signature_Algorithms_Cert(TLS_Data_Reader& reader,
2✔
589
                                                     uint16_t extension_size)
2✔
590
   : m_schemes(parse_signature_algorithms(reader, extension_size))
2✔
591
   {}
2✔
592

593
Session_Ticket_Extension::Session_Ticket_Extension(TLS_Data_Reader& reader,
4,703✔
594
                                                   uint16_t extension_size)
4,703✔
595
   : m_ticket(Session_Ticket(reader.get_elem<uint8_t, std::vector<uint8_t>>(extension_size)))
4,703✔
596
   {}
4,703✔
597

598
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader,
10✔
599
                                                   uint16_t extension_size) : m_pp(reader.get_range<uint16_t>(2, 0, 65535))
10✔
600
   {
601
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
9✔
602

603
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size)
9✔
604
      throw Decoding_Error("Bad encoding for SRTP protection extension");
×
605

606
   if(!mki.empty())
9✔
607
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
×
608
   }
9✔
609

610
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const
5✔
611
   {
612
   std::vector<uint8_t> buf;
5✔
613

614
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
5✔
615
   buf.push_back(get_byte<0>(pp_len));
5✔
616
   buf.push_back(get_byte<1>(pp_len));
5✔
617

618
   for(uint16_t pp : m_pp)
12✔
619
      {
620
      buf.push_back(get_byte<0>(pp));
7✔
621
      buf.push_back(get_byte<1>(pp));
7✔
622
      }
623

624
   buf.push_back(0); // srtp_mki, always empty here
5✔
625

626
   return buf;
5✔
627
   }
×
628

629
Extended_Master_Secret::Extended_Master_Secret(TLS_Data_Reader& /*unused*/,
5,233✔
630
                                               uint16_t extension_size)
5,233✔
631
   {
632
   if(extension_size != 0)
5,233✔
633
      throw Decoding_Error("Invalid extended_master_secret extension");
2✔
634
   }
5,231✔
635

636
std::vector<uint8_t> Extended_Master_Secret::serialize(Connection_Side /*whoami*/) const
5,010✔
637
   {
638
   return std::vector<uint8_t>();
5,010✔
639
   }
640

641
Encrypt_then_MAC::Encrypt_then_MAC(TLS_Data_Reader& /*unused*/,
760✔
642
                                   uint16_t extension_size)
760✔
643
   {
644
   if(extension_size != 0)
760✔
645
      throw Decoding_Error("Invalid encrypt_then_mac extension");
×
646
   }
760✔
647

648
std::vector<uint8_t> Encrypt_then_MAC::serialize(Connection_Side /*whoami*/) const
3,813✔
649
   {
650
   return std::vector<uint8_t>();
3,813✔
651
   }
652

653
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const
4,253✔
654
   {
655
   std::vector<uint8_t> buf;
4,253✔
656

657
   if(whoami == Connection_Side::Server)
4,253✔
658
      {
659
      BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
400✔
660
      buf.push_back(m_versions[0].major_version());
400✔
661
      buf.push_back(m_versions[0].minor_version());
400✔
662
      }
663
   else
664
      {
665
      BOTAN_ASSERT_NOMSG(!m_versions.empty());
3,853✔
666
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
3,853✔
667

668
      buf.push_back(len);
3,853✔
669

670
      for(Protocol_Version version : m_versions)
8,757✔
671
         {
672
         buf.push_back(version.major_version());
4,904✔
673
         buf.push_back(version.minor_version());
4,904✔
674
         }
675
      }
676

677
   return buf;
4,253✔
678
   }
×
679

680

681
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy)
3,374✔
682
   {
683
   if(offer.is_datagram_protocol())
3,374✔
684
      {
685
#if defined(BOTAN_HAS_TLS_12)
686
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12())
356✔
687
         m_versions.push_back(Protocol_Version::DTLS_V12);
356✔
688
#endif
689
      }
690
   else
691
      {
692
#if defined(BOTAN_HAS_TLS_13)
693
      if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13())
5,087✔
694
         m_versions.push_back(Protocol_Version::TLS_V13);
949✔
695
#endif
696
#if defined(BOTAN_HAS_TLS_12)
697
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12())
3,967✔
698
         m_versions.push_back(Protocol_Version::TLS_V12);
2,999✔
699
#endif
700
      }
701
   }
3,374✔
702

703
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader,
1,642✔
704
                                       uint16_t extension_size,
705
                                       Connection_Side from)
1,642✔
706
   {
707
   if(from == Connection_Side::Server)
1,642✔
708
      {
709
      if(extension_size != 2)
511✔
710
         throw Decoding_Error("Server sent invalid supported_versions extension");
×
711
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
511✔
712
      }
713
   else
714
      {
715
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
1,131✔
716

717
      for(auto v : versions)
3,964✔
718
         m_versions.push_back(Protocol_Version(v));
2,833✔
719

720
      if(extension_size != 1+2*versions.size())
1,131✔
721
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
722
      }
1,131✔
723
   }
1,642✔
724

725
bool Supported_Versions::supports(Protocol_Version version) const
1,395✔
726
   {
727
   for(auto v : m_versions)
1,870✔
728
      if(version == v)
1,855✔
729
         return true;
1,395✔
730
   return false;
731
   }
732

733

734
Record_Size_Limit::Record_Size_Limit(const uint16_t limit)
12✔
735
   : m_limit(limit)
12✔
736
   {
737
   BOTAN_ASSERT(limit >= 64,
12✔
738
                "RFC 8449 does not allow record size limits smaller than 64 bytes");
739
   BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
12✔
740
                "RFC 8449 does not allow record size limits larger than 2^14+1");
741
   }
12✔
742

743
Record_Size_Limit::Record_Size_Limit(TLS_Data_Reader& reader,
40✔
744
                                     uint16_t extension_size,
745
                                     Connection_Side from)
40✔
746
   {
747
   if(extension_size != 2)
40✔
748
      {
749
      throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
×
750
      }
751

752
   m_limit = reader.get_uint16_t();
40✔
753

754
   // RFC 8449 4.
755
   //    This value is the length of the plaintext of a protected record.
756
   //    The value includes the content type and padding added in TLS 1.3 (that
757
   //    is, the complete length of TLSInnerPlaintext).
758
   //
759
   //    A server MUST NOT enforce this restriction; a client might advertise
760
   //    a higher limit that is enabled by an extension or version the server
761
   //    does not understand. A client MAY abort the handshake with an
762
   //    "illegal_parameter" alert.
763
   //
764
   // Note: We are currently supporting this extension in TLS 1.3 only, hence
765
   //       we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
766
   //       the "content type byte" and hence be one byte less!
767
   if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server)
40✔
768
      {
769
      throw TLS_Exception(Alert::IllegalParameter,
×
770
                          "Server requested a record size limit larger than the protocol's maximum");
×
771
      }
772

773
   // RFC 8449 4.
774
   //    Endpoints MUST NOT send a "record_size_limit" extension with a value
775
   //    smaller than 64.  An endpoint MUST treat receipt of a smaller value
776
   //    as a fatal error and generate an "illegal_parameter" alert.
777
   if(m_limit < 64)
40✔
778
      {
779
      throw TLS_Exception(Alert::IllegalParameter,
×
780
                          "Received a record size limit smaller than 64 bytes");
×
781
      }
782
   }
40✔
783

784
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side) const
39✔
785
   {
786
   std::vector<uint8_t> buf;
39✔
787

788
   buf.push_back(get_byte<0>(m_limit));
39✔
789
   buf.push_back(get_byte<1>(m_limit));
39✔
790

791
   return buf;
39✔
792
   }
×
793

794

795
#if defined(BOTAN_HAS_TLS_13)
796
Cookie::Cookie(const std::vector<uint8_t>& cookie) :
4✔
797
   m_cookie(cookie)
4✔
798
   {
799
   }
4✔
800

801
Cookie::Cookie(TLS_Data_Reader& reader,
12✔
802
               uint16_t extension_size)
12✔
803
   {
804
   if (extension_size == 0)
12✔
805
      {
806
      return;
807
      }
808

809
   const uint16_t len = reader.get_uint16_t();
12✔
810

811
   if (len == 0)
12✔
812
      {
813
      // Based on RFC 8446 4.2.2, len of the Cookie buffer must be at least 1
814
      throw Decoding_Error("Cookie length must be at least 1 byte");
1✔
815
      }
816

817
   if (len > reader.remaining_bytes())
11✔
818
      {
819
      throw Decoding_Error("Not enough bytes in the buffer to decode Cookie");
×
820
      }
821

822
   for(size_t i = 0; i < len; ++i)
719✔
823
      {
824
      m_cookie.push_back(reader.get_byte());
708✔
825
      }
826
   }
1✔
827

828
std::vector<uint8_t> Cookie::serialize(Connection_Side /*whoami*/) const
10✔
829
   {
830
   std::vector<uint8_t> buf;
10✔
831

832
   const uint16_t len = static_cast<uint16_t>(m_cookie.size());
10✔
833

834
   buf.push_back(get_byte<0>(len));
10✔
835
   buf.push_back(get_byte<1>(len));
10✔
836

837
   for (const auto& cookie_byte : m_cookie)
712✔
838
      {
839
      buf.push_back(cookie_byte);
702✔
840
      }
841

842
   return buf;
10✔
843
   }
×
844

845

846
std::vector<uint8_t> PSK_Key_Exchange_Modes::serialize(Connection_Side) const
1,087✔
847
   {
848
   std::vector<uint8_t> buf;
1,087✔
849

850
   BOTAN_ASSERT_NOMSG(m_modes.size() < 256);
1,087✔
851
   buf.push_back(static_cast<uint8_t>(m_modes.size()));
1,087✔
852
   for (const auto& mode : m_modes)
2,174✔
853
      {
854
      buf.push_back(static_cast<uint8_t>(mode));
1,087✔
855
      }
856

857
   return buf;
1,087✔
858
   }
×
859

860
PSK_Key_Exchange_Modes::PSK_Key_Exchange_Modes(TLS_Data_Reader& reader, uint16_t extension_size)
930✔
861
   {
862
   if (extension_size < 2)
930✔
863
      {
864
      throw Decoding_Error("Empty psk_key_exchange_modes extension is illegal");
×
865
      }
866

867
   const auto mode_count = reader.get_byte();
930✔
868
   for(uint16_t i = 0; i < mode_count; ++i)
1,864✔
869
      {
870
      const auto mode = static_cast<PSK_Key_Exchange_Mode>(reader.get_byte());
934✔
871
      if (mode == PSK_Key_Exchange_Mode::PSK_KE ||
934✔
872
          mode == PSK_Key_Exchange_Mode::PSK_DHE_KE)
873
         {
874
         m_modes.push_back(mode);
928✔
875
         }
876
      }
877
   }
930✔
878

879

880
std::vector<uint8_t> Certificate_Authorities::serialize(Connection_Side) const
×
881
   { throw Not_Implemented("serializing Certificate_Authorities is NYI"); }
×
882

883
Certificate_Authorities::Certificate_Authorities(TLS_Data_Reader& reader, uint16_t extension_size)
3✔
884
   {
885
   if (extension_size < 2)
3✔
886
      {
887
      throw Decoding_Error("Empty certificate_authorities extension is illegal");
×
888
      }
889

890
   const uint16_t purported_size = reader.get_uint16_t();
3✔
891

892
   if(reader.remaining_bytes() != purported_size)
3✔
893
      throw Decoding_Error("Inconsistent length in certificate_authorities extension");
×
894

895
   while(reader.has_remaining())
9✔
896
      {
897
      std::vector<uint8_t> name_bits = reader.get_tls_length_value(2);
6✔
898

899
      BER_Decoder decoder(name_bits.data(), name_bits.size());
6✔
900
      m_distinguished_names.emplace_back();
6✔
901
      decoder.decode(m_distinguished_names.back());
6✔
902
      }
12✔
903
   }
3✔
904

905
Certificate_Authorities::Certificate_Authorities(std::vector<X509_DN> acceptable_DNs)
×
906
   : m_distinguished_names(std::move(acceptable_DNs)) {}
×
907

908

909
std::vector<uint8_t> EarlyDataIndication::serialize(Connection_Side) const
8✔
910
   {
911
   std::vector<uint8_t> result;
8✔
912
   if(m_max_early_data_size.has_value())
8✔
913
      {
914
      const auto max_data = m_max_early_data_size.value();
2✔
915
      result.push_back(get_byte<0>(max_data));
2✔
916
      result.push_back(get_byte<1>(max_data));
2✔
917
      result.push_back(get_byte<2>(max_data));
2✔
918
      result.push_back(get_byte<3>(max_data));
2✔
919
      }
920
   return result;
8✔
921
   }
×
922

923
EarlyDataIndication::EarlyDataIndication(TLS_Data_Reader& reader,
5✔
924
                                         uint16_t extension_size,
925
                                         Handshake_Type message_type)
5✔
926
   {
927
   if(message_type == Handshake_Type::NewSessionTicket)
5✔
928
      {
929
      if(extension_size != 4)
1✔
930
         {
931
         throw TLS_Exception(Alert::DecodeError,
×
932
                             "Received an early_data extension in a NewSessionTicket message "
933
                             "without maximum early data size indication");
×
934
         }
935

936
      m_max_early_data_size = reader.get_uint32_t();
1✔
937
      }
938
   else if(extension_size != 0)
4✔
939
      {
940
      throw TLS_Exception(Alert::DecodeError,
×
941
                          "Received an early_data extension containing an unexpected data "
942
                          "size indication");
×
943
      }
944
   }
5✔
945

946
bool EarlyDataIndication::empty() const
8✔
947
   {
948
   // This extension may be empty by definition but still carry information
949
   return false;
8✔
950
   }
951

952
#endif
953
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc