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

randombit / botan / 22077325440

16 Feb 2026 08:58PM UTC coverage: 90.047% (+0.003%) from 90.044%
22077325440

push

github

randombit
Add missing <algorithm> includes

102337 of 113649 relevant lines covered (90.05%)

11376088.49 hits per line

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

89.62
/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
*     2023 Mateusz Berezecki
8
*     2023 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
9
*
10
* Botan is released under the Simplified BSD License (see license.txt)
11
*/
12

13
#include <botan/tls_extensions.h>
14

15
#include <botan/tls_exceptn.h>
16
#include <botan/tls_policy.h>
17
#include <botan/internal/fmt.h>
18
#include <botan/internal/parsing.h>
19
#include <botan/internal/stl_util.h>
20
#include <botan/internal/tls_reader.h>
21
#include <algorithm>
22

23
#if defined(BOTAN_HAS_TLS_13)
24
   #include <botan/tls_extensions_13.h>
25
#endif
26

27
#if defined(BOTAN_HAS_TLS_12)
28
   #include <botan/tls_extensions_12.h>
29
#endif
30

31
namespace Botan::TLS {
32

33
namespace {
34

35
std::unique_ptr<Extension> make_extension(TLS_Data_Reader& reader,
39,461✔
36
                                          Extension_Code code,
37
                                          const Connection_Side from,
38
                                          const Handshake_Type message_type) {
39
   // This cast is safe because we read exactly a 16 bit length field for
40
   // the extension in Extensions::deserialize
41
   const uint16_t size = static_cast<uint16_t>(reader.remaining_bytes());
39,461✔
42
   switch(code) {
39,461✔
43
      case Extension_Code::ServerNameIndication:
2,829✔
44
         return std::make_unique<Server_Name_Indicator>(reader, size);
2,829✔
45

46
      case Extension_Code::SupportedGroups:
2,909✔
47
         return std::make_unique<Supported_Groups>(reader, size);
2,909✔
48

49
      case Extension_Code::CertificateStatusRequest:
2,670✔
50
         return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
2,670✔
51

52
      case Extension_Code::SignatureAlgorithms:
2,934✔
53
         return std::make_unique<Signature_Algorithms>(reader, size);
2,934✔
54

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

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

61
      case Extension_Code::ApplicationLayerProtocolNegotiation:
407✔
62
         return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
407✔
63

64
      case Extension_Code::ClientCertificateType:
2✔
65
         return std::make_unique<Client_Certificate_Type>(reader, size, from);
2✔
66

67
      case Extension_Code::ServerCertificateType:
2✔
68
         return std::make_unique<Server_Certificate_Type>(reader, size, from);
2✔
69

70
      case Extension_Code::RecordSizeLimit:
45✔
71
         return std::make_unique<Record_Size_Limit>(reader, size, from);
45✔
72

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

76
#if defined(BOTAN_HAS_TLS_12)
77
      case Extension_Code::EcPointFormats:
3,198✔
78
         return std::make_unique<Supported_Point_Formats>(reader, size);
3,198✔
79

80
      case Extension_Code::SafeRenegotiation:
5,691✔
81
         return std::make_unique<Renegotiation_Extension>(reader, size);
5,691✔
82

83
      case Extension_Code::ExtendedMasterSecret:
5,515✔
84
         return std::make_unique<Extended_Master_Secret>(reader, size);
5,515✔
85

86
      case Extension_Code::EncryptThenMac:
811✔
87
         return std::make_unique<Encrypt_then_MAC>(reader, size);
811✔
88

89
      case Extension_Code::SessionTicket:
4,977✔
90
         return std::make_unique<Session_Ticket_Extension>(reader, size);
4,977✔
91
#else
92
      case Extension_Code::EcPointFormats:
93
      case Extension_Code::SafeRenegotiation:
94
      case Extension_Code::ExtendedMasterSecret:
95
      case Extension_Code::EncryptThenMac:
96
      case Extension_Code::SessionTicket:
97
         break;  // considered as 'unknown extension'
98
#endif
99

100
#if defined(BOTAN_HAS_TLS_13)
101
      case Extension_Code::PresharedKey:
232✔
102
         return std::make_unique<PSK>(reader, size, message_type);
232✔
103

104
      case Extension_Code::EarlyData:
5✔
105
         return std::make_unique<EarlyDataIndication>(reader, size, message_type);
5✔
106

107
      case Extension_Code::Cookie:
10✔
108
         return std::make_unique<Cookie>(reader, size);
10✔
109

110
      case Extension_Code::PskKeyExchangeModes:
1,072✔
111
         return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
1,072✔
112

113
      case Extension_Code::CertificateAuthorities:
3✔
114
         return std::make_unique<Certificate_Authorities>(reader, size);
3✔
115

116
      case Extension_Code::KeyShare:
1,609✔
117
         return std::make_unique<Key_Share>(reader, size, message_type);
1,609✔
118
#else
119
      case Extension_Code::PresharedKey:
120
      case Extension_Code::EarlyData:
121
      case Extension_Code::Cookie:
122
      case Extension_Code::PskKeyExchangeModes:
123
      case Extension_Code::CertificateAuthorities:
124
      case Extension_Code::KeyShare:
125
         break;  // considered as 'unknown extension'
126
#endif
127
   }
128

129
   return std::make_unique<Unknown_Extension>(code, reader, size);
2,708✔
130
}
131

132
}  // namespace
133

134
Extensions::~Extensions() = default;
22,766✔
135

136
Extension* Extensions::get(Extension_Code type) const {
221,091✔
137
   const auto i =
221,091✔
138
      std::find_if(m_extensions.cbegin(), m_extensions.cend(), [type](const auto& ext) { return ext->type() == type; });
1,131,363✔
139

140
   return (i != m_extensions.end()) ? i->get() : nullptr;
221,091✔
141
}
142

143
void Extensions::add(std::unique_ptr<Extension> extn) {
84,907✔
144
   if(has(extn->type())) {
84,907✔
145
      throw Invalid_Argument("cannot add the same extension twice: " +
×
146
                             std::to_string(static_cast<uint16_t>(extn->type())));
×
147
   }
148

149
   m_extensions.emplace_back(extn.release());
84,907✔
150
}
84,907✔
151

152
void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
7,973✔
153
   if(reader.has_remaining()) {
7,973✔
154
      const uint16_t all_extn_size = reader.get_uint16_t();
7,968✔
155

156
      if(reader.remaining_bytes() != all_extn_size) {
7,968✔
157
         throw Decoding_Error("Bad extension size");
118✔
158
      }
159

160
      while(reader.has_remaining()) {
47,278✔
161
         const uint16_t extension_code = reader.get_uint16_t();
39,483✔
162
         const uint16_t extension_size = reader.get_uint16_t();
39,482✔
163

164
         const auto type = static_cast<Extension_Code>(extension_code);
39,482✔
165

166
         if(this->has(type)) {
39,482✔
167
            throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
13✔
168
         }
169

170
         // TODO offer a function on reader that returns a byte range as a reference
171
         // to avoid this copy of the extension data
172
         const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
39,469✔
173
         TLS_Data_Reader extn_reader("Extension", extn_data);
39,461✔
174
         this->add(make_extension(extn_reader, type, from, message_type));
39,461✔
175
         extn_reader.assert_done();
39,428✔
176
      }
39,428✔
177
   }
178
}
7,800✔
179

180
bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
3,438✔
181
                                     const bool allow_unknown_extensions) const {
182
   const auto found = extension_types();
3,438✔
183

184
   std::vector<Extension_Code> diff;
3,438✔
185
   std::set_difference(
3,438✔
186
      found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
187

188
   if(allow_unknown_extensions) {
3,438✔
189
      // Go through the found unexpected extensions whether any of those
190
      // is known to this TLS implementation.
191
      const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
1,523✔
192
         const auto ext = get(ext_type);
10✔
193
         return ext && ext->is_implemented();
10✔
194
      });
195

196
      // ... if yes, `contains_other_than` is true
197
      return itr != diff.cend();
1,523✔
198
   }
199

200
   return !diff.empty();
1,915✔
201
}
3,438✔
202

203
std::unique_ptr<Extension> Extensions::take(Extension_Code type) {
520✔
204
   const auto i =
520✔
205
      std::find_if(m_extensions.begin(), m_extensions.end(), [type](const auto& ext) { return ext->type() == type; });
3,177✔
206

207
   std::unique_ptr<Extension> result;
520✔
208
   if(i != m_extensions.end()) {
520✔
209
      std::swap(result, *i);
251✔
210
      m_extensions.erase(i);
251✔
211
   }
212

213
   return result;
520✔
214
}
215

216
std::vector<uint8_t> Extensions::serialize(Connection_Side whoami) const {
6,540✔
217
   std::vector<uint8_t> buf(2);  // 2 bytes for length field
6,540✔
218

219
   for(const auto& extn : m_extensions) {
59,021✔
220
      if(extn->empty()) {
52,481✔
221
         continue;
3,120✔
222
      }
223

224
      const uint16_t extn_code = static_cast<uint16_t>(extn->type());
49,361✔
225

226
      const std::vector<uint8_t> extn_val = extn->serialize(whoami);
49,361✔
227

228
      buf.push_back(get_byte<0>(extn_code));
49,361✔
229
      buf.push_back(get_byte<1>(extn_code));
49,361✔
230

231
      buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
49,361✔
232
      buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
49,361✔
233

234
      buf += extn_val;
49,361✔
235
   }
49,361✔
236

237
   const uint16_t extn_size = static_cast<uint16_t>(buf.size() - 2);
6,540✔
238

239
   buf[0] = get_byte<0>(extn_size);
6,540✔
240
   buf[1] = get_byte<1>(extn_size);
6,540✔
241

242
   // avoid sending a completely empty extensions block
243
   if(buf.size() == 2) {
6,540✔
244
      return std::vector<uint8_t>();
304✔
245
   }
246

247
   return buf;
6,236✔
248
}
6,540✔
249

250
std::set<Extension_Code> Extensions::extension_types() const {
10,357✔
251
   std::set<Extension_Code> offers;
10,357✔
252
   std::transform(
10,357✔
253
      m_extensions.cbegin(), m_extensions.cend(), std::inserter(offers, offers.begin()), [](const auto& ext) {
58,611✔
254
         return ext->type();
58,611✔
255
      });
256
   return offers;
10,357✔
257
}
×
258

259
Unknown_Extension::Unknown_Extension(Extension_Code type, TLS_Data_Reader& reader, uint16_t extension_size) :
2,708✔
260
      m_type(type), m_value(reader.get_fixed<uint8_t>(extension_size)) {}
2,708✔
261

262
std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const {
2✔
263
   return m_value;
2✔
264
}
265

266
Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader, uint16_t extension_size) {
2,829✔
267
   /*
268
   * This is used by the server to confirm that it knew the name
269
   */
270
   if(extension_size == 0) {
2,829✔
271
      return;
272
   }
273

274
   uint16_t name_bytes = reader.get_uint16_t();
2,794✔
275

276
   if(name_bytes + 2 != extension_size) {
2,794✔
277
      throw Decoding_Error("Bad encoding of SNI extension");
×
278
   }
279

280
   while(name_bytes > 0) {
5,588✔
281
      const uint8_t name_type = reader.get_byte();
2,794✔
282
      name_bytes--;
2,794✔
283

284
      if(name_type == 0) {
2,794✔
285
         // DNS
286
         m_sni_host_name = reader.get_string(2, 1, 65535);
2,794✔
287
         name_bytes -= static_cast<uint16_t>(2 + m_sni_host_name.size());
2,794✔
288
      } else {
289
         // some other unknown name type, which we will ignore
290
         reader.discard_next(name_bytes);
×
291
         name_bytes = 0;
×
292
      }
293
   }
294
}
×
295

296
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
4,656✔
297
   // RFC 6066
298
   //    [...] the server SHALL include an extension of type "server_name" in
299
   //    the (extended) server hello. The "extension_data" field of this
300
   //    extension SHALL be empty.
301
   if(whoami == Connection_Side::Server) {
4,656✔
302
      return {};
366✔
303
   }
304

305
   std::vector<uint8_t> buf;
4,290✔
306

307
   const size_t name_len = m_sni_host_name.size();
4,290✔
308

309
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
4,290✔
310
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
4,290✔
311
   buf.push_back(0);  // DNS
4,290✔
312

313
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
4,290✔
314
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
4,290✔
315

316
   buf += as_span_of_bytes(m_sni_host_name);
4,290✔
317

318
   return buf;
4,290✔
319
}
4,290✔
320

321
bool Server_Name_Indicator::hostname_acceptable_for_sni(std::string_view hostname) {
3,684✔
322
   // Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
323

324
   if(hostname.empty()) {
3,684✔
325
      return false;
326
   }
327

328
   if(string_to_ipv4(hostname).has_value()) {
3,637✔
329
      return false;
330
   }
331

332
   // IPv6? Anyway ':' is not valid in DNS
333
   if(hostname.find(':') != std::string_view::npos) {
3,637✔
334
      return false;
335
   }
336

337
   return true;
338
}
339

340
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
407✔
341
                                                                                 uint16_t extension_size,
342
                                                                                 Connection_Side from) {
407✔
343
   if(extension_size == 0) {
407✔
344
      return;  // empty extension
345
   }
346

347
   const uint16_t name_bytes = reader.get_uint16_t();
407✔
348

349
   size_t bytes_remaining = extension_size - 2;
407✔
350

351
   if(name_bytes != bytes_remaining) {
407✔
352
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
×
353
   }
354

355
   while(bytes_remaining > 0) {
1,085✔
356
      const std::string p = reader.get_string(1, 0, 255);
685✔
357

358
      if(bytes_remaining < p.size() + 1) {
685✔
359
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
×
360
      }
361

362
      if(p.empty()) {
685✔
363
         throw Decoding_Error("Empty ALPN protocol not allowed");
7✔
364
      }
365

366
      bytes_remaining -= (p.size() + 1);
678✔
367

368
      m_protocols.push_back(p);
678✔
369
   }
685✔
370

371
   // RFC 7301 3.1
372
   //    The "extension_data" field of the [...] extension is structured the
373
   //    same as described above for the client "extension_data", except that
374
   //    the "ProtocolNameList" MUST contain exactly one "ProtocolName".
375
   if(from == Connection_Side::Server && m_protocols.size() != 1) {
400✔
376
      throw TLS_Exception(
×
377
         Alert::DecodeError,
378
         "Server sent " + std::to_string(m_protocols.size()) + " protocols in ALPN extension response");
×
379
   }
380
}
7✔
381

382
std::string Application_Layer_Protocol_Notification::single_protocol() const {
148✔
383
   BOTAN_STATE_CHECK(m_protocols.size() == 1);
148✔
384
   return m_protocols.front();
148✔
385
}
386

387
std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize(Connection_Side /*whoami*/) const {
365✔
388
   std::vector<uint8_t> buf(2);
365✔
389

390
   for(auto&& proto : m_protocols) {
958✔
391
      if(proto.length() >= 256) {
593✔
392
         throw TLS_Exception(Alert::InternalError, "ALPN name too long");
×
393
      }
394
      if(!proto.empty()) {
593✔
395
         append_tls_length_value(buf, proto, 1);
1,186✔
396
      }
397
   }
398

399
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
365✔
400
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
365✔
401

402
   return buf;
365✔
403
}
×
404

405
Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
1,976✔
406
      m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
1,976✔
407
   BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
1,976✔
408
}
1,976✔
409

410
Client_Certificate_Type::Client_Certificate_Type(const Client_Certificate_Type& cct, const Policy& policy) :
1✔
411
      Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
1✔
412

413
Server_Certificate_Type::Server_Certificate_Type(const Server_Certificate_Type& sct, const Policy& policy) :
1✔
414
      Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
1✔
415

416
Certificate_Type_Base::Certificate_Type_Base(const Certificate_Type_Base& certificate_type_from_client,
2✔
417
                                             const std::vector<Certificate_Type>& server_preference) :
2✔
418
      m_from(Connection_Side::Server) {
2✔
419
   // RFC 7250 4.2
420
   //    The server_certificate_type extension in the client hello indicates the
421
   //    types of certificates the client is able to process when provided by
422
   //    the server in a subsequent certificate payload. [...] With the
423
   //    server_certificate_type extension in the server hello, the TLS server
424
   //    indicates the certificate type carried in the Certificate payload.
425
   for(const auto server_supported_cert_type : server_preference) {
2✔
426
      if(value_exists(certificate_type_from_client.m_certificate_types, server_supported_cert_type)) {
4✔
427
         m_certificate_types.push_back(server_supported_cert_type);
2✔
428
         return;
2✔
429
      }
430
   }
431

432
   // RFC 7250 4.2 (2.)
433
   //    The server supports the extension defined in this document, but
434
   //    it does not have any certificate type in common with the client.
435
   //    Then, the server terminates the session with a fatal alert of
436
   //    type "unsupported_certificate".
437
   throw TLS_Exception(Alert::UnsupportedCertificate, "Failed to agree on certificate_type");
×
438
}
×
439

440
Certificate_Type_Base::Certificate_Type_Base(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) :
4✔
441
      m_from(from) {
4✔
442
   if(extension_size == 0) {
4✔
443
      throw Decoding_Error("Certificate type extension cannot be empty");
×
444
   }
445

446
   if(from == Connection_Side::Client) {
4✔
447
      const auto type_bytes = reader.get_tls_length_value(1);
2✔
448
      if(static_cast<size_t>(extension_size) != type_bytes.size() + 1) {
2✔
449
         throw Decoding_Error("certificate type extension had inconsistent length");
×
450
      }
451
      std::transform(
2✔
452
         type_bytes.begin(), type_bytes.end(), std::back_inserter(m_certificate_types), [](const auto type_byte) {
2✔
453
            return static_cast<Certificate_Type>(type_byte);
454
         });
455
   } else {
2✔
456
      // RFC 7250 4.2
457
      //    Note that only a single value is permitted in the
458
      //    server_certificate_type extension when carried in the server hello.
459
      if(extension_size != 1) {
2✔
460
         throw Decoding_Error("Server's certificate type extension must be of length 1");
×
461
      }
462
      const auto type_byte = reader.get_byte();
2✔
463
      m_certificate_types.push_back(static_cast<Certificate_Type>(type_byte));
2✔
464
   }
465
}
4✔
466

467
std::vector<uint8_t> Certificate_Type_Base::serialize(Connection_Side whoami) const {
12✔
468
   std::vector<uint8_t> result;
12✔
469
   if(whoami == Connection_Side::Client) {
12✔
470
      std::vector<uint8_t> type_bytes;
6✔
471
      std::transform(
6✔
472
         m_certificate_types.begin(), m_certificate_types.end(), std::back_inserter(type_bytes), [](const auto type) {
473
            return static_cast<uint8_t>(type);
474
         });
475
      append_tls_length_value(result, type_bytes, 1);
12✔
476
   } else {
6✔
477
      BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
478
      result.push_back(static_cast<uint8_t>(m_certificate_types.front()));
6✔
479
   }
480
   return result;
12✔
481
}
×
482

483
void Certificate_Type_Base::validate_selection(const Certificate_Type_Base& from_server) const {
2✔
484
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Client);
2✔
485
   BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
2✔
486

487
   // RFC 7250 4.2
488
   //    The value conveyed in the [client_]certificate_type extension MUST be
489
   //    selected from one of the values provided in the [client_]certificate_type
490
   //    extension sent in the client hello.
491
   if(!value_exists(m_certificate_types, from_server.selected_certificate_type())) {
4✔
492
      throw TLS_Exception(Alert::IllegalParameter,
×
493
                          Botan::fmt("Selected certificate type was not offered: {}",
×
494
                                     certificate_type_to_string(from_server.selected_certificate_type())));
×
495
   }
496
}
2✔
497

498
Certificate_Type Certificate_Type_Base::selected_certificate_type() const {
6✔
499
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Server);
6✔
500
   BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
501
   return m_certificate_types.front();
6✔
502
}
503

504
Supported_Groups::Supported_Groups(const std::vector<Group_Params>& groups) : m_groups(groups) {}
4,047✔
505

506
const std::vector<Group_Params>& Supported_Groups::groups() const {
948✔
507
   return m_groups;
948✔
508
}
509

510
std::vector<Group_Params> Supported_Groups::ec_groups() const {
5,019✔
511
   std::vector<Group_Params> ec;
5,019✔
512
   for(auto g : m_groups) {
52,045✔
513
      if(g.is_pure_ecc_group()) {
94,052✔
514
         ec.push_back(g);
34,649✔
515
      }
516
   }
517
   return ec;
5,019✔
518
}
×
519

520
std::vector<Group_Params> Supported_Groups::dh_groups() const {
1,505✔
521
   std::vector<Group_Params> dh;
1,505✔
522
   for(auto g : m_groups) {
11,050✔
523
      if(g.is_in_ffdhe_range()) {
12,375✔
524
         dh.push_back(g);
538✔
525
      }
526
   }
527
   return dh;
1,505✔
528
}
×
529

530
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
4,724✔
531
   std::vector<uint8_t> buf(2);
4,724✔
532

533
   for(auto g : m_groups) {
54,923✔
534
      const uint16_t id = g.wire_code();
50,199✔
535

536
      if(id > 0) {
50,199✔
537
         buf.push_back(get_byte<0>(id));
50,199✔
538
         buf.push_back(get_byte<1>(id));
50,199✔
539
      }
540
   }
541

542
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
4,724✔
543
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
4,724✔
544

545
   return buf;
4,724✔
546
}
×
547

548
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
2,911✔
549
   const uint16_t len = reader.get_uint16_t();
2,911✔
550

551
   if(len + 2 != extension_size) {
2,911✔
552
      throw Decoding_Error("Inconsistent length field in supported groups list");
4✔
553
   }
554

555
   if(len % 2 == 1) {
2,907✔
556
      throw Decoding_Error("Supported groups list of strange size");
×
557
   }
558

559
   const size_t elems = len / 2;
2,907✔
560

561
   for(size_t i = 0; i != elems; ++i) {
23,913✔
562
      const auto group = static_cast<Group_Params>(reader.get_uint16_t());
21,006✔
563
      // Note: RFC 8446 does not explicitly enforce that groups must be unique.
564
      if(!value_exists(m_groups, group)) {
42,012✔
565
         m_groups.push_back(group);
21,006✔
566
      }
567
   }
568
}
2,911✔
569

570
namespace {
571

572
std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
4,728✔
573
   BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
4,728✔
574

575
   std::vector<uint8_t> buf;
4,728✔
576

577
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
4,728✔
578

579
   buf.push_back(get_byte<0>(len));
4,728✔
580
   buf.push_back(get_byte<1>(len));
4,728✔
581

582
   for(const Signature_Scheme scheme : schemes) {
46,320✔
583
      buf.push_back(get_byte<0>(scheme.wire_code()));
41,592✔
584
      buf.push_back(get_byte<1>(scheme.wire_code()));
41,592✔
585
   }
586

587
   return buf;
4,728✔
588
}
×
589

590
std::vector<Signature_Scheme> parse_signature_algorithms(TLS_Data_Reader& reader, uint16_t extension_size) {
2,936✔
591
   uint16_t len = reader.get_uint16_t();
2,936✔
592

593
   if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
2,935✔
594
      throw Decoding_Error("Bad encoding on signature algorithms extension");
1✔
595
   }
596

597
   std::vector<Signature_Scheme> schemes;
2,934✔
598
   schemes.reserve(len / 2);
2,934✔
599
   while(len > 0) {
30,781✔
600
      schemes.emplace_back(reader.get_uint16_t());
27,847✔
601
      len -= 2;
27,847✔
602
   }
603

604
   return schemes;
2,934✔
605
}
×
606

607
}  // namespace
608

609
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
4,726✔
610
   return serialize_signature_algorithms(m_schemes);
4,726✔
611
}
612

613
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size) :
2,934✔
614
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2,934✔
615

616
std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
2✔
617
   return serialize_signature_algorithms(m_schemes);
2✔
618
}
619

620
Signature_Algorithms_Cert::Signature_Algorithms_Cert(TLS_Data_Reader& reader, uint16_t extension_size) :
2✔
621
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2✔
622

623
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size) :
10✔
624
      m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
10✔
625
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
9✔
626

627
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
9✔
628
      throw Decoding_Error("Bad encoding for SRTP protection extension");
×
629
   }
630

631
   if(!mki.empty()) {
9✔
632
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
×
633
   }
634
}
9✔
635

636
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
5✔
637
   std::vector<uint8_t> buf;
5✔
638

639
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
5✔
640
   buf.push_back(get_byte<0>(pp_len));
5✔
641
   buf.push_back(get_byte<1>(pp_len));
5✔
642

643
   for(const uint16_t pp : m_pp) {
12✔
644
      buf.push_back(get_byte<0>(pp));
7✔
645
      buf.push_back(get_byte<1>(pp));
7✔
646
   }
647

648
   buf.push_back(0);  // srtp_mki, always empty here
5✔
649

650
   return buf;
5✔
651
}
×
652

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

656
   if(whoami == Connection_Side::Server) {
4,479✔
657
      BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
429✔
658
      buf.push_back(m_versions[0].major_version());
429✔
659
      buf.push_back(m_versions[0].minor_version());
429✔
660
   } else {
661
      BOTAN_ASSERT_NOMSG(!m_versions.empty());
4,050✔
662
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
4,050✔
663

664
      buf.push_back(len);
4,050✔
665

666
      for(const Protocol_Version version : m_versions) {
9,218✔
667
         buf.push_back(version.major_version());
5,168✔
668
         buf.push_back(version.minor_version());
5,168✔
669
      }
670
   }
671

672
   return buf;
4,479✔
673
}
×
674

675
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy) {
3,469✔
676
   if(offer.is_datagram_protocol()) {
3,469✔
677
#if defined(BOTAN_HAS_TLS_12)
678
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
400✔
679
         m_versions.push_back(Protocol_Version::DTLS_V12);
400✔
680
      }
681
#endif
682
   } else {
683
#if defined(BOTAN_HAS_TLS_13)
684
      if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
5,150✔
685
         m_versions.push_back(Protocol_Version::TLS_V13);
988✔
686
      }
687
#endif
688
#if defined(BOTAN_HAS_TLS_12)
689
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
4,057✔
690
         m_versions.push_back(Protocol_Version::TLS_V12);
3,040✔
691
      }
692
#endif
693
   }
694
}
3,469✔
695

696
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
1,824✔
697
   if(from == Connection_Side::Server) {
1,824✔
698
      if(extension_size != 2) {
541✔
699
         throw Decoding_Error("Server sent invalid supported_versions extension");
×
700
      }
701
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
541✔
702
   } else {
703
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
1,283✔
704

705
      for(auto v : versions) {
4,555✔
706
         m_versions.push_back(Protocol_Version(v));
3,272✔
707
      }
708

709
      if(extension_size != 1 + 2 * versions.size()) {
1,283✔
710
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
711
      }
712
   }
1,283✔
713
}
1,824✔
714

715
bool Supported_Versions::supports(Protocol_Version version) const {
1,486✔
716
   for(auto v : m_versions) {
1,997✔
717
      if(version == v) {
1,978✔
718
         return true;
1,486✔
719
      }
720
   }
721
   return false;
722
}
723

724
Record_Size_Limit::Record_Size_Limit(const uint16_t limit) : m_limit(limit) {
16✔
725
   BOTAN_ASSERT(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
16✔
726
   BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
16✔
727
                "RFC 8449 does not allow record size limits larger than 2^14+1");
728
}
16✔
729

730
Record_Size_Limit::Record_Size_Limit(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
45✔
731
   if(extension_size != 2) {
45✔
732
      throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
×
733
   }
734

735
   m_limit = reader.get_uint16_t();
45✔
736

737
   // RFC 8449 4.
738
   //    This value is the length of the plaintext of a protected record.
739
   //    The value includes the content type and padding added in TLS 1.3 (that
740
   //    is, the complete length of TLSInnerPlaintext).
741
   //
742
   //    A server MUST NOT enforce this restriction; a client might advertise
743
   //    a higher limit that is enabled by an extension or version the server
744
   //    does not understand. A client MAY abort the handshake with an
745
   //    "illegal_parameter" alert.
746
   //
747
   // Note: We are currently supporting this extension in TLS 1.3 only, hence
748
   //       we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
749
   //       the "content type byte" and hence be one byte less!
750
   if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
45✔
751
      throw TLS_Exception(Alert::IllegalParameter,
×
752
                          "Server requested a record size limit larger than the protocol's maximum");
×
753
   }
754

755
   // RFC 8449 4.
756
   //    Endpoints MUST NOT send a "record_size_limit" extension with a value
757
   //    smaller than 64.  An endpoint MUST treat receipt of a smaller value
758
   //    as a fatal error and generate an "illegal_parameter" alert.
759
   if(m_limit < 64) {
45✔
760
      throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
×
761
   }
762
}
45✔
763

764
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
53✔
765
   std::vector<uint8_t> buf;
53✔
766

767
   buf.push_back(get_byte<0>(m_limit));
53✔
768
   buf.push_back(get_byte<1>(m_limit));
53✔
769

770
   return buf;
53✔
771
}
×
772

773
}  // namespace Botan::TLS
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