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

randombit / botan / 22032365202

15 Feb 2026 08:16AM UTC coverage: 89.995% (-0.007%) from 90.002%
22032365202

push

github

web-flow
Merge pull request #5293 from Rohde-Schwarz/refactor/tls12_13_client_hello

Refactor: Disentangle Client_Hello implementations

102265 of 113634 relevant lines covered (90.0%)

11422362.32 hits per line

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

89.85
/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/ber_dec.h>
16
#include <botan/der_enc.h>
17
#include <botan/pkix_types.h>
18
#include <botan/tls_exceptn.h>
19
#include <botan/tls_policy.h>
20
#include <botan/internal/fmt.h>
21
#include <botan/internal/mem_utils.h>
22
#include <botan/internal/parsing.h>
23
#include <botan/internal/stl_util.h>
24
#include <botan/internal/tls_reader.h>
25

26
#include <algorithm>
27
#include <iterator>
28

29
namespace Botan::TLS {
30

31
namespace {
32

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

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

47
      case Extension_Code::CertificateStatusRequest:
2,672✔
48
         return std::make_unique<Certificate_Status_Request>(reader, size, message_type, from);
2,672✔
49

50
      case Extension_Code::EcPointFormats:
3,198✔
51
         return std::make_unique<Supported_Point_Formats>(reader, size);
3,198✔
52

53
      case Extension_Code::SafeRenegotiation:
5,691✔
54
         return std::make_unique<Renegotiation_Extension>(reader, size);
5,691✔
55

56
      case Extension_Code::SignatureAlgorithms:
2,934✔
57
         return std::make_unique<Signature_Algorithms>(reader, size);
2,934✔
58

59
      case Extension_Code::CertSignatureAlgorithms:
×
60
         return std::make_unique<Signature_Algorithms_Cert>(reader, size);
×
61

62
      case Extension_Code::UseSrtp:
10✔
63
         return std::make_unique<SRTP_Protection_Profiles>(reader, size);
10✔
64

65
      case Extension_Code::ApplicationLayerProtocolNegotiation:
407✔
66
         return std::make_unique<Application_Layer_Protocol_Notification>(reader, size, from);
407✔
67

68
      case Extension_Code::ClientCertificateType:
2✔
69
         return std::make_unique<Client_Certificate_Type>(reader, size, from);
2✔
70

71
      case Extension_Code::ServerCertificateType:
2✔
72
         return std::make_unique<Server_Certificate_Type>(reader, size, from);
2✔
73

74
      case Extension_Code::ExtendedMasterSecret:
5,515✔
75
         return std::make_unique<Extended_Master_Secret>(reader, size);
5,515✔
76

77
      case Extension_Code::RecordSizeLimit:
45✔
78
         return std::make_unique<Record_Size_Limit>(reader, size, from);
45✔
79

80
      case Extension_Code::EncryptThenMac:
812✔
81
         return std::make_unique<Encrypt_then_MAC>(reader, size);
812✔
82

83
      case Extension_Code::SessionTicket:
4,978✔
84
         return std::make_unique<Session_Ticket_Extension>(reader, size);
4,978✔
85

86
      case Extension_Code::SupportedVersions:
1,823✔
87
         return std::make_unique<Supported_Versions>(reader, size, from);
1,823✔
88

89
#if defined(BOTAN_HAS_TLS_13)
90
      case Extension_Code::PresharedKey:
232✔
91
         return std::make_unique<PSK>(reader, size, message_type);
232✔
92

93
      case Extension_Code::EarlyData:
5✔
94
         return std::make_unique<EarlyDataIndication>(reader, size, message_type);
5✔
95

96
      case Extension_Code::Cookie:
10✔
97
         return std::make_unique<Cookie>(reader, size);
10✔
98

99
      case Extension_Code::PskKeyExchangeModes:
1,072✔
100
         return std::make_unique<PSK_Key_Exchange_Modes>(reader, size);
1,072✔
101

102
      case Extension_Code::CertificateAuthorities:
3✔
103
         return std::make_unique<Certificate_Authorities>(reader, size);
3✔
104

105
      case Extension_Code::KeyShare:
1,609✔
106
         return std::make_unique<Key_Share>(reader, size, message_type);
1,609✔
107
#endif
108
   }
109

110
   return std::make_unique<Unknown_Extension>(code, reader, size);
2,708✔
111
}
112

113
}  // namespace
114

115
Extensions::~Extensions() = default;
22,766✔
116

117
Extension* Extensions::get(Extension_Code type) const {
221,126✔
118
   const auto i =
221,126✔
119
      std::find_if(m_extensions.cbegin(), m_extensions.cend(), [type](const auto& ext) { return ext->type() == type; });
1,131,742✔
120

121
   return (i != m_extensions.end()) ? i->get() : nullptr;
221,126✔
122
}
123

124
void Extensions::add(std::unique_ptr<Extension> extn) {
84,919✔
125
   if(has(extn->type())) {
84,919✔
126
      throw Invalid_Argument("cannot add the same extension twice: " +
×
127
                             std::to_string(static_cast<uint16_t>(extn->type())));
×
128
   }
129

130
   m_extensions.emplace_back(extn.release());
84,919✔
131
}
84,919✔
132

133
void Extensions::deserialize(TLS_Data_Reader& reader, const Connection_Side from, const Handshake_Type message_type) {
7,973✔
134
   if(reader.has_remaining()) {
7,973✔
135
      const uint16_t all_extn_size = reader.get_uint16_t();
7,968✔
136

137
      if(reader.remaining_bytes() != all_extn_size) {
7,968✔
138
         throw Decoding_Error("Bad extension size");
118✔
139
      }
140

141
      while(reader.has_remaining()) {
47,283✔
142
         const uint16_t extension_code = reader.get_uint16_t();
39,488✔
143
         const uint16_t extension_size = reader.get_uint16_t();
39,487✔
144

145
         const auto type = static_cast<Extension_Code>(extension_code);
39,487✔
146

147
         if(this->has(type)) {
39,487✔
148
            throw TLS_Exception(TLS::Alert::DecodeError, "Peer sent duplicated extensions");
13✔
149
         }
150

151
         // TODO offer a function on reader that returns a byte range as a reference
152
         // to avoid this copy of the extension data
153
         const std::vector<uint8_t> extn_data = reader.get_fixed<uint8_t>(extension_size);
39,474✔
154
         TLS_Data_Reader extn_reader("Extension", extn_data);
39,466✔
155
         this->add(make_extension(extn_reader, type, from, message_type));
39,466✔
156
         extn_reader.assert_done();
39,433✔
157
      }
39,433✔
158
   }
159
}
7,800✔
160

161
bool Extensions::contains_other_than(const std::set<Extension_Code>& allowed_extensions,
3,438✔
162
                                     const bool allow_unknown_extensions) const {
163
   const auto found = extension_types();
3,438✔
164

165
   std::vector<Extension_Code> diff;
3,438✔
166
   std::set_difference(
3,438✔
167
      found.cbegin(), found.end(), allowed_extensions.cbegin(), allowed_extensions.cend(), std::back_inserter(diff));
168

169
   if(allow_unknown_extensions) {
3,438✔
170
      // Go through the found unexpected extensions whether any of those
171
      // is known to this TLS implementation.
172
      const auto itr = std::find_if(diff.cbegin(), diff.cend(), [this](const auto ext_type) {
1,523✔
173
         const auto ext = get(ext_type);
10✔
174
         return ext && ext->is_implemented();
10✔
175
      });
176

177
      // ... if yes, `contains_other_than` is true
178
      return itr != diff.cend();
1,523✔
179
   }
180

181
   return !diff.empty();
1,915✔
182
}
3,438✔
183

184
std::unique_ptr<Extension> Extensions::take(Extension_Code type) {
520✔
185
   const auto i =
520✔
186
      std::find_if(m_extensions.begin(), m_extensions.end(), [type](const auto& ext) { return ext->type() == type; });
3,185✔
187

188
   std::unique_ptr<Extension> result;
520✔
189
   if(i != m_extensions.end()) {
520✔
190
      std::swap(result, *i);
251✔
191
      m_extensions.erase(i);
251✔
192
   }
193

194
   return result;
520✔
195
}
196

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

200
   for(const auto& extn : m_extensions) {
59,024✔
201
      if(extn->empty()) {
52,484✔
202
         continue;
3,118✔
203
      }
204

205
      const uint16_t extn_code = static_cast<uint16_t>(extn->type());
49,366✔
206

207
      const std::vector<uint8_t> extn_val = extn->serialize(whoami);
49,366✔
208

209
      buf.push_back(get_byte<0>(extn_code));
49,366✔
210
      buf.push_back(get_byte<1>(extn_code));
49,366✔
211

212
      buf.push_back(get_byte<0>(static_cast<uint16_t>(extn_val.size())));
49,366✔
213
      buf.push_back(get_byte<1>(static_cast<uint16_t>(extn_val.size())));
49,366✔
214

215
      buf += extn_val;
49,366✔
216
   }
49,366✔
217

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

220
   buf[0] = get_byte<0>(extn_size);
6,540✔
221
   buf[1] = get_byte<1>(extn_size);
6,540✔
222

223
   // avoid sending a completely empty extensions block
224
   if(buf.size() == 2) {
6,540✔
225
      return std::vector<uint8_t>();
304✔
226
   }
227

228
   return buf;
6,236✔
229
}
6,540✔
230

231
std::set<Extension_Code> Extensions::extension_types() const {
10,357✔
232
   std::set<Extension_Code> offers;
10,357✔
233
   std::transform(
10,357✔
234
      m_extensions.cbegin(), m_extensions.cend(), std::inserter(offers, offers.begin()), [](const auto& ext) {
58,621✔
235
         return ext->type();
58,621✔
236
      });
237
   return offers;
10,357✔
238
}
×
239

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

243
std::vector<uint8_t> Unknown_Extension::serialize(Connection_Side /*whoami*/) const {
2✔
244
   return m_value;
2✔
245
}
246

247
Server_Name_Indicator::Server_Name_Indicator(TLS_Data_Reader& reader, uint16_t extension_size) {
2,829✔
248
   /*
249
   * This is used by the server to confirm that it knew the name
250
   */
251
   if(extension_size == 0) {
2,829✔
252
      return;
253
   }
254

255
   uint16_t name_bytes = reader.get_uint16_t();
2,794✔
256

257
   if(name_bytes + 2 != extension_size) {
2,794✔
258
      throw Decoding_Error("Bad encoding of SNI extension");
×
259
   }
260

261
   while(name_bytes > 0) {
5,588✔
262
      const uint8_t name_type = reader.get_byte();
2,794✔
263
      name_bytes--;
2,794✔
264

265
      if(name_type == 0) {
2,794✔
266
         // DNS
267
         m_sni_host_name = reader.get_string(2, 1, 65535);
2,794✔
268
         name_bytes -= static_cast<uint16_t>(2 + m_sni_host_name.size());
2,794✔
269
      } else {
270
         // some other unknown name type, which we will ignore
271
         reader.discard_next(name_bytes);
×
272
         name_bytes = 0;
×
273
      }
274
   }
275
}
×
276

277
std::vector<uint8_t> Server_Name_Indicator::serialize(Connection_Side whoami) const {
4,656✔
278
   // RFC 6066
279
   //    [...] the server SHALL include an extension of type "server_name" in
280
   //    the (extended) server hello. The "extension_data" field of this
281
   //    extension SHALL be empty.
282
   if(whoami == Connection_Side::Server) {
4,656✔
283
      return {};
366✔
284
   }
285

286
   std::vector<uint8_t> buf;
4,290✔
287

288
   const size_t name_len = m_sni_host_name.size();
4,290✔
289

290
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len + 3)));
4,290✔
291
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len + 3)));
4,290✔
292
   buf.push_back(0);  // DNS
4,290✔
293

294
   buf.push_back(get_byte<0>(static_cast<uint16_t>(name_len)));
4,290✔
295
   buf.push_back(get_byte<1>(static_cast<uint16_t>(name_len)));
4,290✔
296

297
   buf += as_span_of_bytes(m_sni_host_name);
4,290✔
298

299
   return buf;
4,290✔
300
}
4,290✔
301

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

305
   if(hostname.empty()) {
3,684✔
306
      return false;
307
   }
308

309
   if(string_to_ipv4(hostname).has_value()) {
3,637✔
310
      return false;
311
   }
312

313
   // IPv6? Anyway ':' is not valid in DNS
314
   if(hostname.find(':') != std::string_view::npos) {
3,637✔
315
      return false;
316
   }
317

318
   return true;
319
}
320

321
Renegotiation_Extension::Renegotiation_Extension(TLS_Data_Reader& reader, uint16_t extension_size) :
5,691✔
322
      m_reneg_data(reader.get_range<uint8_t>(1, 0, 255)) {
5,691✔
323
   if(m_reneg_data.size() + 1 != extension_size) {
5,689✔
324
      throw Decoding_Error("Bad encoding for secure renegotiation extn");
1✔
325
   }
326
}
5,689✔
327

328
std::vector<uint8_t> Renegotiation_Extension::serialize(Connection_Side /*whoami*/) const {
5,268✔
329
   std::vector<uint8_t> buf;
5,268✔
330
   append_tls_length_value(buf, m_reneg_data, 1);
5,268✔
331
   return buf;
5,268✔
332
}
×
333

334
Application_Layer_Protocol_Notification::Application_Layer_Protocol_Notification(TLS_Data_Reader& reader,
407✔
335
                                                                                 uint16_t extension_size,
336
                                                                                 Connection_Side from) {
407✔
337
   if(extension_size == 0) {
407✔
338
      return;  // empty extension
339
   }
340

341
   const uint16_t name_bytes = reader.get_uint16_t();
407✔
342

343
   size_t bytes_remaining = extension_size - 2;
407✔
344

345
   if(name_bytes != bytes_remaining) {
407✔
346
      throw Decoding_Error("Bad encoding of ALPN extension, bad length field");
×
347
   }
348

349
   while(bytes_remaining > 0) {
1,085✔
350
      const std::string p = reader.get_string(1, 0, 255);
685✔
351

352
      if(bytes_remaining < p.size() + 1) {
685✔
353
         throw Decoding_Error("Bad encoding of ALPN, length field too long");
×
354
      }
355

356
      if(p.empty()) {
685✔
357
         throw Decoding_Error("Empty ALPN protocol not allowed");
7✔
358
      }
359

360
      bytes_remaining -= (p.size() + 1);
678✔
361

362
      m_protocols.push_back(p);
678✔
363
   }
685✔
364

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

376
std::string Application_Layer_Protocol_Notification::single_protocol() const {
148✔
377
   BOTAN_STATE_CHECK(m_protocols.size() == 1);
148✔
378
   return m_protocols.front();
148✔
379
}
380

381
std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize(Connection_Side /*whoami*/) const {
365✔
382
   std::vector<uint8_t> buf(2);
365✔
383

384
   for(auto&& proto : m_protocols) {
958✔
385
      if(proto.length() >= 256) {
593✔
386
         throw TLS_Exception(Alert::InternalError, "ALPN name too long");
×
387
      }
388
      if(!proto.empty()) {
593✔
389
         append_tls_length_value(buf, proto, 1);
1,186✔
390
      }
391
   }
392

393
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
365✔
394
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
365✔
395

396
   return buf;
365✔
397
}
×
398

399
Certificate_Type_Base::Certificate_Type_Base(std::vector<Certificate_Type> supported_cert_types) :
1,976✔
400
      m_certificate_types(std::move(supported_cert_types)), m_from(Connection_Side::Client) {
1,976✔
401
   BOTAN_ARG_CHECK(!m_certificate_types.empty(), "at least one certificate type must be supported");
1,976✔
402
}
1,976✔
403

404
Client_Certificate_Type::Client_Certificate_Type(const Client_Certificate_Type& cct, const Policy& policy) :
1✔
405
      Certificate_Type_Base(cct, policy.accepted_client_certificate_types()) {}
1✔
406

407
Server_Certificate_Type::Server_Certificate_Type(const Server_Certificate_Type& sct, const Policy& policy) :
1✔
408
      Certificate_Type_Base(sct, policy.accepted_server_certificate_types()) {}
1✔
409

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

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

434
Certificate_Type_Base::Certificate_Type_Base(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) :
4✔
435
      m_from(from) {
4✔
436
   if(extension_size == 0) {
4✔
437
      throw Decoding_Error("Certificate type extension cannot be empty");
×
438
   }
439

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

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

477
void Certificate_Type_Base::validate_selection(const Certificate_Type_Base& from_server) const {
2✔
478
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Client);
2✔
479
   BOTAN_ASSERT_NOMSG(from_server.m_from == Connection_Side::Server);
2✔
480

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

492
Certificate_Type Certificate_Type_Base::selected_certificate_type() const {
6✔
493
   BOTAN_ASSERT_NOMSG(m_from == Connection_Side::Server);
6✔
494
   BOTAN_ASSERT_NOMSG(m_certificate_types.size() == 1);
6✔
495
   return m_certificate_types.front();
6✔
496
}
497

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

500
const std::vector<Group_Params>& Supported_Groups::groups() const {
948✔
501
   return m_groups;
948✔
502
}
503

504
std::vector<Group_Params> Supported_Groups::ec_groups() const {
5,022✔
505
   std::vector<Group_Params> ec;
5,022✔
506
   for(auto g : m_groups) {
52,078✔
507
      if(g.is_pure_ecc_group()) {
94,112✔
508
         ec.push_back(g);
34,673✔
509
      }
510
   }
511
   return ec;
5,022✔
512
}
×
513

514
std::vector<Group_Params> Supported_Groups::dh_groups() const {
1,509✔
515
   std::vector<Group_Params> dh;
1,509✔
516
   for(auto g : m_groups) {
11,094✔
517
      if(g.is_in_ffdhe_range()) {
12,423✔
518
         dh.push_back(g);
546✔
519
      }
520
   }
521
   return dh;
1,509✔
522
}
×
523

524
std::vector<uint8_t> Supported_Groups::serialize(Connection_Side /*whoami*/) const {
4,724✔
525
   std::vector<uint8_t> buf(2);
4,724✔
526

527
   for(auto g : m_groups) {
54,923✔
528
      const uint16_t id = g.wire_code();
50,199✔
529

530
      if(id > 0) {
50,199✔
531
         buf.push_back(get_byte<0>(id));
50,199✔
532
         buf.push_back(get_byte<1>(id));
50,199✔
533
      }
534
   }
535

536
   buf[0] = get_byte<0>(static_cast<uint16_t>(buf.size() - 2));
4,724✔
537
   buf[1] = get_byte<1>(static_cast<uint16_t>(buf.size() - 2));
4,724✔
538

539
   return buf;
4,724✔
540
}
×
541

542
Supported_Groups::Supported_Groups(TLS_Data_Reader& reader, uint16_t extension_size) {
2,911✔
543
   const uint16_t len = reader.get_uint16_t();
2,911✔
544

545
   if(len + 2 != extension_size) {
2,911✔
546
      throw Decoding_Error("Inconsistent length field in supported groups list");
4✔
547
   }
548

549
   if(len % 2 == 1) {
2,907✔
550
      throw Decoding_Error("Supported groups list of strange size");
×
551
   }
552

553
   const size_t elems = len / 2;
2,907✔
554

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

564
std::vector<uint8_t> Supported_Point_Formats::serialize(Connection_Side /*whoami*/) const {
5,110✔
565
   // if this extension is sent, it MUST include uncompressed (RFC 4492, section 5.1)
566
   if(m_prefers_compressed) {
5,110✔
567
      return std::vector<uint8_t>{2, ANSIX962_COMPRESSED_PRIME, UNCOMPRESSED};
10✔
568
   } else {
569
      return std::vector<uint8_t>{1, UNCOMPRESSED};
5,100✔
570
   }
571
}
572

573
Supported_Point_Formats::Supported_Point_Formats(TLS_Data_Reader& reader, uint16_t extension_size) {
3,198✔
574
   const uint8_t len = reader.get_byte();
3,198✔
575

576
   if(len + 1 != extension_size) {
3,198✔
577
      throw Decoding_Error("Inconsistent length field in supported point formats list");
2✔
578
   }
579

580
   bool includes_uncompressed = false;
3,201✔
581
   for(size_t i = 0; i != len; ++i) {
3,201✔
582
      const uint8_t format = reader.get_byte();
3,200✔
583

584
      if(static_cast<ECPointFormat>(format) == UNCOMPRESSED) {
3,200✔
585
         m_prefers_compressed = false;
3,185✔
586
         reader.discard_next(len - i - 1);
3,185✔
587
         return;
3,185✔
588
      } else if(static_cast<ECPointFormat>(format) == ANSIX962_COMPRESSED_PRIME) {
15✔
589
         m_prefers_compressed = true;
10✔
590
         std::vector<uint8_t> remaining_formats = reader.get_fixed<uint8_t>(len - i - 1);
10✔
591
         includes_uncompressed =
10✔
592
            std::any_of(std::begin(remaining_formats), std::end(remaining_formats), [](uint8_t remaining_format) {
10✔
593
               return static_cast<ECPointFormat>(remaining_format) == UNCOMPRESSED;
594
            });
595
         break;
10✔
596
      }
10✔
597

598
      // ignore ANSIX962_COMPRESSED_CHAR2, we don't support these curves
599
   }
600

601
   // RFC 4492 5.1.:
602
   //   If the Supported Point Formats Extension is indeed sent, it MUST contain the value 0 (uncompressed)
603
   //   as one of the items in the list of point formats.
604
   // Note:
605
   //   RFC 8422 5.1.2. explicitly requires this check,
606
   //   but only if the Supported Groups extension was sent.
607
   if(!includes_uncompressed) {
11✔
608
      throw TLS_Exception(Alert::IllegalParameter,
1✔
609
                          "Supported Point Formats Extension must contain the uncompressed point format");
1✔
610
   }
611
}
612

613
namespace {
614

615
std::vector<uint8_t> serialize_signature_algorithms(const std::vector<Signature_Scheme>& schemes) {
4,729✔
616
   BOTAN_ASSERT(schemes.size() < 256, "Too many signature schemes");
4,729✔
617

618
   std::vector<uint8_t> buf;
4,729✔
619

620
   const uint16_t len = static_cast<uint16_t>(schemes.size() * 2);
4,729✔
621

622
   buf.push_back(get_byte<0>(len));
4,729✔
623
   buf.push_back(get_byte<1>(len));
4,729✔
624

625
   for(const Signature_Scheme scheme : schemes) {
46,330✔
626
      buf.push_back(get_byte<0>(scheme.wire_code()));
41,601✔
627
      buf.push_back(get_byte<1>(scheme.wire_code()));
41,601✔
628
   }
629

630
   return buf;
4,729✔
631
}
×
632

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

636
   if(len + 2 != extension_size || len % 2 == 1 || len == 0) {
2,935✔
637
      throw Decoding_Error("Bad encoding on signature algorithms extension");
1✔
638
   }
639

640
   std::vector<Signature_Scheme> schemes;
2,934✔
641
   schemes.reserve(len / 2);
2,934✔
642
   while(len > 0) {
30,781✔
643
      schemes.emplace_back(reader.get_uint16_t());
27,847✔
644
      len -= 2;
27,847✔
645
   }
646

647
   return schemes;
2,934✔
648
}
×
649

650
}  // namespace
651

652
std::vector<uint8_t> Signature_Algorithms::serialize(Connection_Side /*whoami*/) const {
4,727✔
653
   return serialize_signature_algorithms(m_schemes);
4,727✔
654
}
655

656
Signature_Algorithms::Signature_Algorithms(TLS_Data_Reader& reader, uint16_t extension_size) :
2,934✔
657
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2,934✔
658

659
std::vector<uint8_t> Signature_Algorithms_Cert::serialize(Connection_Side /*whoami*/) const {
2✔
660
   return serialize_signature_algorithms(m_schemes);
2✔
661
}
662

663
Signature_Algorithms_Cert::Signature_Algorithms_Cert(TLS_Data_Reader& reader, uint16_t extension_size) :
2✔
664
      m_schemes(parse_signature_algorithms(reader, extension_size)) {}
2✔
665

666
Session_Ticket_Extension::Session_Ticket_Extension(TLS_Data_Reader& reader, uint16_t extension_size) :
4,978✔
667
      m_ticket(Session_Ticket(reader.get_elem<uint8_t, std::vector<uint8_t>>(extension_size))) {}
4,978✔
668

669
SRTP_Protection_Profiles::SRTP_Protection_Profiles(TLS_Data_Reader& reader, uint16_t extension_size) :
10✔
670
      m_pp(reader.get_range<uint16_t>(2, 0, 65535)) {
10✔
671
   const std::vector<uint8_t> mki = reader.get_range<uint8_t>(1, 0, 255);
9✔
672

673
   if(m_pp.size() * 2 + mki.size() + 3 != extension_size) {
9✔
674
      throw Decoding_Error("Bad encoding for SRTP protection extension");
×
675
   }
676

677
   if(!mki.empty()) {
9✔
678
      throw Decoding_Error("Unhandled non-empty MKI for SRTP protection extension");
×
679
   }
680
}
9✔
681

682
std::vector<uint8_t> SRTP_Protection_Profiles::serialize(Connection_Side /*whoami*/) const {
5✔
683
   std::vector<uint8_t> buf;
5✔
684

685
   const uint16_t pp_len = static_cast<uint16_t>(m_pp.size() * 2);
5✔
686
   buf.push_back(get_byte<0>(pp_len));
5✔
687
   buf.push_back(get_byte<1>(pp_len));
5✔
688

689
   for(const uint16_t pp : m_pp) {
12✔
690
      buf.push_back(get_byte<0>(pp));
7✔
691
      buf.push_back(get_byte<1>(pp));
7✔
692
   }
693

694
   buf.push_back(0);  // srtp_mki, always empty here
5✔
695

696
   return buf;
5✔
697
}
×
698

699
Extended_Master_Secret::Extended_Master_Secret(TLS_Data_Reader& /*unused*/, uint16_t extension_size) {
5,515✔
700
   if(extension_size != 0) {
5,515✔
701
      throw Decoding_Error("Invalid extended_master_secret extension");
2✔
702
   }
703
}
5,513✔
704

705
std::vector<uint8_t> Extended_Master_Secret::serialize(Connection_Side /*whoami*/) const {
5,242✔
706
   return std::vector<uint8_t>();
5,242✔
707
}
708

709
Encrypt_then_MAC::Encrypt_then_MAC(TLS_Data_Reader& /*unused*/, uint16_t extension_size) {
812✔
710
   if(extension_size != 0) {
812✔
711
      throw Decoding_Error("Invalid encrypt_then_mac extension");
×
712
   }
713
}
812✔
714

715
std::vector<uint8_t> Encrypt_then_MAC::serialize(Connection_Side /*whoami*/) const {
3,980✔
716
   return std::vector<uint8_t>();
3,980✔
717
}
718

719
std::vector<uint8_t> Supported_Versions::serialize(Connection_Side whoami) const {
4,480✔
720
   std::vector<uint8_t> buf;
4,480✔
721

722
   if(whoami == Connection_Side::Server) {
4,480✔
723
      BOTAN_ASSERT_NOMSG(m_versions.size() == 1);
429✔
724
      buf.push_back(m_versions[0].major_version());
429✔
725
      buf.push_back(m_versions[0].minor_version());
429✔
726
   } else {
727
      BOTAN_ASSERT_NOMSG(!m_versions.empty());
4,051✔
728
      const uint8_t len = static_cast<uint8_t>(m_versions.size() * 2);
4,051✔
729

730
      buf.push_back(len);
4,051✔
731

732
      for(const Protocol_Version version : m_versions) {
9,220✔
733
         buf.push_back(version.major_version());
5,169✔
734
         buf.push_back(version.minor_version());
5,169✔
735
      }
736
   }
737

738
   return buf;
4,480✔
739
}
×
740

741
Supported_Versions::Supported_Versions(Protocol_Version offer, const Policy& policy) {
3,471✔
742
   if(offer.is_datagram_protocol()) {
3,471✔
743
#if defined(BOTAN_HAS_TLS_12)
744
      if(offer >= Protocol_Version::DTLS_V12 && policy.allow_dtls12()) {
399✔
745
         m_versions.push_back(Protocol_Version::DTLS_V12);
399✔
746
      }
747
#endif
748
   } else {
749
#if defined(BOTAN_HAS_TLS_13)
750
      if(offer >= Protocol_Version::TLS_V13 && policy.allow_tls13()) {
5,156✔
751
         m_versions.push_back(Protocol_Version::TLS_V13);
988✔
752
      }
753
#endif
754
#if defined(BOTAN_HAS_TLS_12)
755
      if(offer >= Protocol_Version::TLS_V12 && policy.allow_tls12()) {
4,060✔
756
         m_versions.push_back(Protocol_Version::TLS_V12);
3,043✔
757
      }
758
#endif
759
   }
760
}
3,471✔
761

762
Supported_Versions::Supported_Versions(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
1,825✔
763
   if(from == Connection_Side::Server) {
1,825✔
764
      if(extension_size != 2) {
541✔
765
         throw Decoding_Error("Server sent invalid supported_versions extension");
×
766
      }
767
      m_versions.push_back(Protocol_Version(reader.get_uint16_t()));
541✔
768
   } else {
769
      auto versions = reader.get_range<uint16_t>(1, 1, 127);
1,284✔
770

771
      for(auto v : versions) {
4,557✔
772
         m_versions.push_back(Protocol_Version(v));
3,273✔
773
      }
774

775
      if(extension_size != 1 + 2 * versions.size()) {
1,284✔
776
         throw Decoding_Error("Client sent invalid supported_versions extension");
×
777
      }
778
   }
1,284✔
779
}
1,825✔
780

781
bool Supported_Versions::supports(Protocol_Version version) const {
1,486✔
782
   for(auto v : m_versions) {
1,997✔
783
      if(version == v) {
1,978✔
784
         return true;
1,486✔
785
      }
786
   }
787
   return false;
788
}
789

790
Record_Size_Limit::Record_Size_Limit(const uint16_t limit) : m_limit(limit) {
16✔
791
   BOTAN_ASSERT(limit >= 64, "RFC 8449 does not allow record size limits smaller than 64 bytes");
16✔
792
   BOTAN_ASSERT(limit <= MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */,
16✔
793
                "RFC 8449 does not allow record size limits larger than 2^14+1");
794
}
16✔
795

796
Record_Size_Limit::Record_Size_Limit(TLS_Data_Reader& reader, uint16_t extension_size, Connection_Side from) {
45✔
797
   if(extension_size != 2) {
45✔
798
      throw TLS_Exception(Alert::DecodeError, "invalid record_size_limit extension");
×
799
   }
800

801
   m_limit = reader.get_uint16_t();
45✔
802

803
   // RFC 8449 4.
804
   //    This value is the length of the plaintext of a protected record.
805
   //    The value includes the content type and padding added in TLS 1.3 (that
806
   //    is, the complete length of TLSInnerPlaintext).
807
   //
808
   //    A server MUST NOT enforce this restriction; a client might advertise
809
   //    a higher limit that is enabled by an extension or version the server
810
   //    does not understand. A client MAY abort the handshake with an
811
   //    "illegal_parameter" alert.
812
   //
813
   // Note: We are currently supporting this extension in TLS 1.3 only, hence
814
   //       we check for the TLS 1.3 limit. The TLS 1.2 limit would not include
815
   //       the "content type byte" and hence be one byte less!
816
   if(m_limit > MAX_PLAINTEXT_SIZE + 1 /* encrypted content type byte */ && from == Connection_Side::Server) {
45✔
817
      throw TLS_Exception(Alert::IllegalParameter,
×
818
                          "Server requested a record size limit larger than the protocol's maximum");
×
819
   }
820

821
   // RFC 8449 4.
822
   //    Endpoints MUST NOT send a "record_size_limit" extension with a value
823
   //    smaller than 64.  An endpoint MUST treat receipt of a smaller value
824
   //    as a fatal error and generate an "illegal_parameter" alert.
825
   if(m_limit < 64) {
45✔
826
      throw TLS_Exception(Alert::IllegalParameter, "Received a record size limit smaller than 64 bytes");
×
827
   }
828
}
45✔
829

830
std::vector<uint8_t> Record_Size_Limit::serialize(Connection_Side /*whoami*/) const {
53✔
831
   std::vector<uint8_t> buf;
53✔
832

833
   buf.push_back(get_byte<0>(m_limit));
53✔
834
   buf.push_back(get_byte<1>(m_limit));
53✔
835

836
   return buf;
53✔
837
}
×
838

839
#if defined(BOTAN_HAS_TLS_13)
840
Cookie::Cookie(const std::vector<uint8_t>& cookie) : m_cookie(cookie) {}
4✔
841

842
Cookie::Cookie(TLS_Data_Reader& reader, uint16_t extension_size) {
12✔
843
   if(extension_size == 0) {
12✔
844
      return;
845
   }
846

847
   const uint16_t len = reader.get_uint16_t();
12✔
848

849
   if(len == 0) {
12✔
850
      // Based on RFC 8446 4.2.2, len of the Cookie buffer must be at least 1
851
      throw Decoding_Error("Cookie length must be at least 1 byte");
1✔
852
   }
853

854
   if(len > reader.remaining_bytes()) {
11✔
855
      throw Decoding_Error("Not enough bytes in the buffer to decode Cookie");
×
856
   }
857

858
   for(size_t i = 0; i < len; ++i) {
719✔
859
      m_cookie.push_back(reader.get_byte());
708✔
860
   }
861
}
1✔
862

863
std::vector<uint8_t> Cookie::serialize(Connection_Side /*whoami*/) const {
10✔
864
   std::vector<uint8_t> buf;
10✔
865

866
   const uint16_t len = static_cast<uint16_t>(m_cookie.size());
10✔
867

868
   buf.push_back(get_byte<0>(len));
10✔
869
   buf.push_back(get_byte<1>(len));
10✔
870

871
   for(const auto& cookie_byte : m_cookie) {
712✔
872
      buf.push_back(cookie_byte);
702✔
873
   }
874

875
   return buf;
10✔
876
}
×
877

878
std::vector<uint8_t> PSK_Key_Exchange_Modes::serialize(Connection_Side /*whoami*/) const {
1,184✔
879
   std::vector<uint8_t> buf;
1,184✔
880

881
   BOTAN_ASSERT_NOMSG(m_modes.size() < 256);
1,184✔
882
   buf.push_back(static_cast<uint8_t>(m_modes.size()));
1,184✔
883
   for(const auto& mode : m_modes) {
2,368✔
884
      buf.push_back(static_cast<uint8_t>(mode));
1,184✔
885
   }
886

887
   return buf;
1,184✔
888
}
×
889

890
PSK_Key_Exchange_Modes::PSK_Key_Exchange_Modes(TLS_Data_Reader& reader, uint16_t extension_size) {
1,072✔
891
   if(extension_size < 2) {
1,072✔
892
      throw Decoding_Error("Empty psk_key_exchange_modes extension is illegal");
×
893
   }
894

895
   const auto mode_count = reader.get_byte();
1,072✔
896
   for(uint16_t i = 0; i < mode_count; ++i) {
2,148✔
897
      const auto mode = static_cast<PSK_Key_Exchange_Mode>(reader.get_byte());
1,076✔
898
      if(mode == PSK_Key_Exchange_Mode::PSK_KE || mode == PSK_Key_Exchange_Mode::PSK_DHE_KE) {
1,076✔
899
         m_modes.push_back(mode);
1,070✔
900
      }
901
   }
902
}
1,072✔
903

904
std::vector<uint8_t> Certificate_Authorities::serialize(Connection_Side /*whoami*/) const {
81✔
905
   std::vector<uint8_t> out;
81✔
906
   std::vector<uint8_t> dn_list;
81✔
907

908
   for(const auto& dn : m_distinguished_names) {
162✔
909
      std::vector<uint8_t> encoded_dn;
81✔
910
      auto encoder = DER_Encoder(encoded_dn);
81✔
911
      dn.encode_into(encoder);
81✔
912
      append_tls_length_value(dn_list, encoded_dn, 2);
81✔
913
   }
162✔
914

915
   append_tls_length_value(out, dn_list, 2);
81✔
916

917
   return out;
81✔
918
}
81✔
919

920
Certificate_Authorities::Certificate_Authorities(TLS_Data_Reader& reader, uint16_t extension_size) {
3✔
921
   if(extension_size < 2) {
3✔
922
      throw Decoding_Error("Empty certificate_authorities extension is illegal");
×
923
   }
924

925
   const uint16_t purported_size = reader.get_uint16_t();
3✔
926

927
   if(reader.remaining_bytes() != purported_size) {
3✔
928
      throw Decoding_Error("Inconsistent length in certificate_authorities extension");
×
929
   }
930

931
   while(reader.has_remaining()) {
9✔
932
      std::vector<uint8_t> name_bits = reader.get_tls_length_value(2);
6✔
933

934
      BER_Decoder decoder(name_bits.data(), name_bits.size());
6✔
935
      m_distinguished_names.emplace_back();
6✔
936
      decoder.decode(m_distinguished_names.back());
6✔
937
   }
12✔
938
}
3✔
939

940
Certificate_Authorities::~Certificate_Authorities() = default;
170✔
941

942
Certificate_Authorities::Certificate_Authorities(const std::vector<X509_DN>& acceptable_DNs) :
82✔
943
      m_distinguished_names(acceptable_DNs) {}
82✔
944

945
std::vector<uint8_t> EarlyDataIndication::serialize(Connection_Side /*whoami*/) const {
8✔
946
   std::vector<uint8_t> result;
8✔
947
   if(m_max_early_data_size.has_value()) {
8✔
948
      const auto max_data = m_max_early_data_size.value();
2✔
949
      result.push_back(get_byte<0>(max_data));
2✔
950
      result.push_back(get_byte<1>(max_data));
2✔
951
      result.push_back(get_byte<2>(max_data));
2✔
952
      result.push_back(get_byte<3>(max_data));
2✔
953
   }
954
   return result;
8✔
955
}
×
956

957
EarlyDataIndication::EarlyDataIndication(TLS_Data_Reader& reader,
5✔
958
                                         uint16_t extension_size,
959
                                         Handshake_Type message_type) {
5✔
960
   if(message_type == Handshake_Type::NewSessionTicket) {
5✔
961
      if(extension_size != 4) {
1✔
962
         throw TLS_Exception(Alert::DecodeError,
×
963
                             "Received an early_data extension in a NewSessionTicket message "
964
                             "without maximum early data size indication");
×
965
      }
966

967
      m_max_early_data_size = reader.get_uint32_t();
1✔
968
   } else if(extension_size != 0) {
4✔
969
      throw TLS_Exception(Alert::DecodeError,
×
970
                          "Received an early_data extension containing an unexpected data "
971
                          "size indication");
×
972
   }
973
}
5✔
974

975
bool EarlyDataIndication::empty() const {
8✔
976
   // This extension may be empty by definition but still carry information
977
   return false;
8✔
978
}
979

980
#endif
981
}  // 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