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

randombit / botan / 20579846577

29 Dec 2025 06:24PM UTC coverage: 90.415% (+0.2%) from 90.243%
20579846577

push

github

web-flow
Merge pull request #5167 from randombit/jack/src-size-reductions

Changes to reduce unnecessary inclusions

101523 of 112285 relevant lines covered (90.42%)

12817276.56 hits per line

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

91.67
/src/lib/tls/msg_client_hello.cpp
1
/*
2
* TLS Hello Request and Client Hello Messages
3
* (C) 2004-2011,2015,2016 Jack Lloyd
4
*     2016 Matthias Gierlings
5
*     2017 Harry Reimann, Rohde & Schwarz Cybersecurity
6
*     2021 Elektrobit Automotive GmbH
7
*     2022 René Meusel, Hannes Rantzsch - neXenio GmbH
8
*
9
* Botan is released under the Simplified BSD License (see license.txt)
10
*/
11

12
#include <botan/tls_messages.h>
13

14
#include <botan/hash.h>
15
#include <botan/rng.h>
16
#include <botan/tls_callbacks.h>
17
#include <botan/tls_exceptn.h>
18
#include <botan/tls_policy.h>
19
#include <botan/tls_version.h>
20
#include <botan/internal/parsing.h>
21
#include <botan/internal/stl_util.h>
22
#include <botan/internal/tls_handshake_hash.h>
23
#include <botan/internal/tls_handshake_io.h>
24
#include <botan/internal/tls_reader.h>
25

26
#ifdef BOTAN_HAS_TLS_13
27
   #include <botan/internal/tls_handshake_layer_13.h>
28
   #include <botan/internal/tls_transcript_hash_13.h>
29
#endif
30

31
#include <chrono>
32
#include <iterator>
33

34
namespace Botan::TLS {
35

36
std::vector<uint8_t> make_hello_random(RandomNumberGenerator& rng, Callbacks& cb, const Policy& policy) {
6,962✔
37
   auto buf = rng.random_vec<std::vector<uint8_t>>(32);
6,962✔
38

39
   if(policy.hash_hello_random()) {
6,962✔
40
      auto sha256 = HashFunction::create_or_throw("SHA-256");
6,947✔
41
      sha256->update(buf);
6,947✔
42
      sha256->final(buf);
6,947✔
43
   }
6,947✔
44

45
   // TLS 1.3 does not require the insertion of a timestamp in the client hello
46
   // random. When offering both TLS 1.2 and 1.3 we nevertheless comply with the
47
   // legacy specification.
48
   if(policy.include_time_in_hello_random() && (policy.allow_tls12() || policy.allow_dtls12())) {
6,962✔
49
      const uint32_t time32 = static_cast<uint32_t>(std::chrono::system_clock::to_time_t(cb.tls_current_timestamp()));
6,939✔
50

51
      store_be(time32, buf.data());
6,939✔
52
   }
53

54
   return buf;
6,962✔
55
}
×
56

57
/**
58
 * Version-agnostic internal client hello data container that allows
59
 * parsing Client_Hello messages without prior knowledge of the contained
60
 * protocol version.
61
 */
62
class Client_Hello_Internal {
63
   public:
64
      Client_Hello_Internal() : m_comp_methods({0}) {}
3,684✔
65

66
      explicit Client_Hello_Internal(const std::vector<uint8_t>& buf) {
3,900✔
67
         if(buf.size() < 41) {
3,900✔
68
            throw Decoding_Error("Client_Hello: Packet corrupted");
45✔
69
         }
70

71
         TLS_Data_Reader reader("ClientHello", buf);
3,855✔
72

73
         const uint8_t major_version = reader.get_byte();
3,855✔
74
         const uint8_t minor_version = reader.get_byte();
3,855✔
75

76
         m_legacy_version = Protocol_Version(major_version, minor_version);
3,855✔
77
         m_random = reader.get_fixed<uint8_t>(32);
3,855✔
78
         m_session_id = Session_ID(reader.get_range<uint8_t>(1, 0, 32));
3,855✔
79

80
         if(m_legacy_version.is_datagram_protocol()) {
3,039✔
81
            auto sha256 = HashFunction::create_or_throw("SHA-256");
798✔
82
            sha256->update(reader.get_data_read_so_far());
798✔
83

84
            m_hello_cookie = reader.get_range<uint8_t>(1, 0, 255);
798✔
85

86
            sha256->update(reader.get_remaining());
798✔
87
            m_cookie_input_bits = sha256->final_stdvec();
798✔
88
         }
798✔
89

90
         m_suites = reader.get_range_vector<uint16_t>(2, 1, 32767);
3,039✔
91
         m_comp_methods = reader.get_range_vector<uint8_t>(1, 1, 255);
2,891✔
92

93
         m_extensions.deserialize(reader, Connection_Side::Client, Handshake_Type::ClientHello);
2,891✔
94
      }
6,002✔
95

96
      /**
97
       * This distinguishes between a TLS 1.3 compliant Client Hello (containing
98
       * the "supported_version" extension) and legacy Client Hello messages.
99
       *
100
       * @return TLS 1.3 if the Client Hello contains "supported_versions", or
101
       *         the content of the "legacy_version" version field if it
102
       *         indicates (D)TLS 1.2 or older, or
103
       *         (D)TLS 1.2 if the "legacy_version" was some other odd value.
104
       */
105
      Protocol_Version version() const {
978✔
106
         // RFC 8446 4.2.1
107
         //    If [the "supported_versions"] extension is not present, servers
108
         //    which are compliant with this specification and which also support
109
         //    TLS 1.2 MUST negotiate TLS 1.2 or prior as specified in [RFC5246],
110
         //    even if ClientHello.legacy_version is 0x0304 or later.
111
         //
112
         // RFC 8446 4.2.1
113
         //    Servers MUST be prepared to receive ClientHellos that include
114
         //    [the supported_versions] extension but do not include 0x0304 in
115
         //    the list of versions.
116
         //
117
         // RFC 8446 4.1.2
118
         //    TLS 1.3 ClientHellos are identified as having a legacy_version of
119
         //    0x0303 and a supported_versions extension present with 0x0304 as
120
         //    the highest version indicated therein.
121
         if(!extensions().has<Supported_Versions>() ||
1,461✔
122
            !extensions().get<Supported_Versions>()->supports(Protocol_Version::TLS_V13)) {
483✔
123
            // The exact legacy_version is ignored we just inspect it to
124
            // distinguish TLS and DTLS.
125
            return (m_legacy_version.is_datagram_protocol()) ? Protocol_Version::DTLS_V12 : Protocol_Version::TLS_V12;
1,026✔
126
         }
127

128
         // Note: The Client_Hello_13 class will make sure that legacy_version
129
         //       is exactly 0x0303 (aka ossified TLS 1.2)
130
         return Protocol_Version::TLS_V13;
465✔
131
      }
132

133
      Protocol_Version legacy_version() const { return m_legacy_version; }
17,290✔
134

135
      const Session_ID& session_id() const { return m_session_id; }
44✔
136

137
      const std::vector<uint8_t>& random() const { return m_random; }
4,391✔
138

139
      const std::vector<uint16_t>& ciphersuites() const { return m_suites; }
4,559✔
140

141
      const std::vector<uint8_t>& comp_methods() const { return m_comp_methods; }
4,811✔
142

143
      const std::vector<uint8_t>& hello_cookie() const { return m_hello_cookie; }
951✔
144

145
      const std::vector<uint8_t>& hello_cookie_input_bits() const { return m_cookie_input_bits; }
790✔
146

147
      const Extensions& extensions() const { return m_extensions; }
978✔
148

149
      Extensions& extensions() { return m_extensions; }
84,410✔
150

151
   public:
152
      Protocol_Version m_legacy_version;    // NOLINT(*-non-private-member-variable*)
153
      Session_ID m_session_id;              // NOLINT(*-non-private-member-variable*)
154
      std::vector<uint8_t> m_random;        // NOLINT(*-non-private-member-variable*)
155
      std::vector<uint16_t> m_suites;       // NOLINT(*-non-private-member-variable*)
156
      std::vector<uint8_t> m_comp_methods;  // NOLINT(*-non-private-member-variable*)
157
      Extensions m_extensions;              // NOLINT(*-non-private-member-variable*)
158

159
      // These fields are only for DTLS:
160
      std::vector<uint8_t> m_hello_cookie;       // NOLINT(*-non-private-member-variable*)
161
      std::vector<uint8_t> m_cookie_input_bits;  // NOLINT(*-non-private-member-variable*)
162
};
163

164
Client_Hello::Client_Hello(Client_Hello&&) noexcept = default;
10,492✔
165
Client_Hello& Client_Hello::operator=(Client_Hello&&) noexcept = default;
43✔
166

167
Client_Hello::~Client_Hello() = default;
17,020✔
168

169
Client_Hello::Client_Hello() : m_data(std::make_unique<Client_Hello_Internal>()) {}
3,684✔
170

171
/*
172
* Read a counterparty client hello
173
*/
174
Client_Hello::Client_Hello(std::unique_ptr<Client_Hello_Internal> data) : m_data(std::move(data)) {
2,861✔
175
   BOTAN_ASSERT_NONNULL(m_data);
2,861✔
176
}
2,861✔
177

178
Handshake_Type Client_Hello::type() const {
16,308✔
179
   return Handshake_Type::ClientHello;
16,308✔
180
}
181

182
Protocol_Version Client_Hello::legacy_version() const {
8,243✔
183
   return m_data->legacy_version();
8,243✔
184
}
185

186
const std::vector<uint8_t>& Client_Hello::random() const {
3,842✔
187
   return m_data->random();
3,842✔
188
}
189

190
const Session_ID& Client_Hello::session_id() const {
3,322✔
191
   return m_data->session_id();
3,322✔
192
}
193

194
const std::vector<uint8_t>& Client_Hello::compression_methods() const {
1,383✔
195
   return m_data->comp_methods();
1,383✔
196
}
197

198
const std::vector<uint16_t>& Client_Hello::ciphersuites() const {
1,430✔
199
   return m_data->ciphersuites();
1,430✔
200
}
201

202
std::set<Extension_Code> Client_Hello::extension_types() const {
3,256✔
203
   return m_data->extensions().extension_types();
3,256✔
204
}
205

206
const Extensions& Client_Hello::extensions() const {
9,821✔
207
   return m_data->extensions();
9,821✔
208
}
209

210
void Client_Hello_12::update_hello_cookie(const Hello_Verify_Request& hello_verify) {
467✔
211
   BOTAN_STATE_CHECK(m_data->legacy_version().is_datagram_protocol());
467✔
212

213
   m_data->m_hello_cookie = hello_verify.cookie();
467✔
214
}
467✔
215

216
/*
217
* Serialize a Client Hello message
218
*/
219
std::vector<uint8_t> Client_Hello::serialize() const {
4,347✔
220
   std::vector<uint8_t> buf;
4,347✔
221
   buf.reserve(1024);  // working around GCC warning
4,347✔
222

223
   buf.push_back(m_data->legacy_version().major_version());
4,347✔
224
   buf.push_back(m_data->legacy_version().minor_version());
4,347✔
225
   buf += m_data->random();
4,347✔
226

227
   append_tls_length_value(buf, m_data->session_id().get(), 1);
4,347✔
228

229
   if(m_data->legacy_version().is_datagram_protocol()) {
4,347✔
230
      append_tls_length_value(buf, m_data->hello_cookie(), 1);
951✔
231
   }
232

233
   append_tls_length_value(buf, m_data->ciphersuites(), 2);
4,347✔
234
   append_tls_length_value(buf, m_data->comp_methods(), 1);
4,347✔
235

236
   /*
237
   * May not want to send extensions at all in some cases. If so,
238
   * should include SCSV value (if reneg info is empty, if not we are
239
   * renegotiating with a modern server)
240
   */
241

242
   buf += m_data->extensions().serialize(Connection_Side::Client);
4,347✔
243

244
   return buf;
4,347✔
245
}
×
246

247
std::vector<uint8_t> Client_Hello::cookie_input_data() const {
790✔
248
   BOTAN_STATE_CHECK(!m_data->hello_cookie_input_bits().empty());
790✔
249

250
   return m_data->hello_cookie_input_bits();
790✔
251
}
252

253
/*
254
* Check if we offered this ciphersuite
255
*/
256
bool Client_Hello::offered_suite(uint16_t ciphersuite) const {
5,283✔
257
   return std::find(m_data->ciphersuites().cbegin(), m_data->ciphersuites().cend(), ciphersuite) !=
5,283✔
258
          m_data->ciphersuites().cend();
5,283✔
259
}
260

261
std::vector<Signature_Scheme> Client_Hello::signature_schemes() const {
4,118✔
262
   if(const Signature_Algorithms* sigs = m_data->extensions().get<Signature_Algorithms>()) {
4,118✔
263
      return sigs->supported_schemes();
4,116✔
264
   }
265
   return {};
2✔
266
}
267

268
std::vector<Signature_Scheme> Client_Hello::certificate_signature_schemes() const {
1,021✔
269
   // RFC 8446 4.2.3
270
   //   If no "signature_algorithms_cert" extension is present, then the
271
   //   "signature_algorithms" extension also applies to signatures appearing
272
   //   in certificates.
273
   if(const Signature_Algorithms_Cert* sigs = m_data->extensions().get<Signature_Algorithms_Cert>()) {
1,021✔
274
      return sigs->supported_schemes();
×
275
   } else {
276
      return signature_schemes();
1,021✔
277
   }
278
}
279

280
std::vector<Group_Params> Client_Hello::supported_ecc_curves() const {
1,369✔
281
   if(const Supported_Groups* groups = m_data->extensions().get<Supported_Groups>()) {
1,369✔
282
      return groups->ec_groups();
1,366✔
283
   }
284
   return {};
3✔
285
}
286

287
std::vector<Group_Params> Client_Hello::supported_dh_groups() const {
1,516✔
288
   if(const Supported_Groups* groups = m_data->extensions().get<Supported_Groups>()) {
1,516✔
289
      return groups->dh_groups();
1,510✔
290
   }
291
   return std::vector<Group_Params>();
6✔
292
}
293

294
bool Client_Hello_12::prefers_compressed_ec_points() const {
28✔
295
   if(const Supported_Point_Formats* ecc_formats = m_data->extensions().get<Supported_Point_Formats>()) {
28✔
296
      return ecc_formats->prefers_compressed();
28✔
297
   }
298
   return false;
299
}
300

301
std::string Client_Hello::sni_hostname() const {
4,227✔
302
   if(const Server_Name_Indicator* sni = m_data->extensions().get<Server_Name_Indicator>()) {
4,227✔
303
      return sni->host_name();
4,097✔
304
   }
305
   return "";
130✔
306
}
307

308
bool Client_Hello_12::secure_renegotiation() const {
5,171✔
309
   return m_data->extensions().has<Renegotiation_Extension>();
5,171✔
310
}
311

312
std::vector<uint8_t> Client_Hello_12::renegotiation_info() const {
4,167✔
313
   if(const Renegotiation_Extension* reneg = m_data->extensions().get<Renegotiation_Extension>()) {
4,167✔
314
      return reneg->renegotiation_info();
4,167✔
315
   }
316
   return {};
×
317
}
318

319
std::vector<Protocol_Version> Client_Hello::supported_versions() const {
1,385✔
320
   if(const Supported_Versions* versions = m_data->extensions().get<Supported_Versions>()) {
1,385✔
321
      return versions->versions();
302✔
322
   }
323
   return {};
1,083✔
324
}
325

326
bool Client_Hello_12::supports_session_ticket() const {
1,197✔
327
   return m_data->extensions().has<Session_Ticket_Extension>();
1,197✔
328
}
329

330
Session_Ticket Client_Hello_12::session_ticket() const {
1,910✔
331
   if(auto* ticket = m_data->extensions().get<Session_Ticket_Extension>()) {
1,910✔
332
      return ticket->contents();
1,778✔
333
   }
334
   return {};
132✔
335
}
336

337
std::optional<Session_Handle> Client_Hello_12::session_handle() const {
987✔
338
   // RFC 5077 3.4
339
   //    If a ticket is presented by the client, the server MUST NOT attempt
340
   //    to use the Session ID in the ClientHello for stateful session
341
   //    resumption.
342
   if(auto ticket = session_ticket(); !ticket.empty()) {
987✔
343
      return Session_Handle(ticket);
808✔
344
   } else if(const auto& id = session_id(); !id.empty()) {
785✔
345
      return Session_Handle(id);
376✔
346
   } else {
347
      return std::nullopt;
691✔
348
   }
987✔
349
}
350

351
bool Client_Hello::supports_alpn() const {
1,127✔
352
   return m_data->extensions().has<Application_Layer_Protocol_Notification>();
1,127✔
353
}
354

355
bool Client_Hello_12::supports_extended_master_secret() const {
1,199✔
356
   return m_data->extensions().has<Extended_Master_Secret>();
1,199✔
357
}
358

359
bool Client_Hello_12::supports_cert_status_message() const {
1,430✔
360
   return m_data->extensions().has<Certificate_Status_Request>();
1,430✔
361
}
362

363
bool Client_Hello_12::supports_encrypt_then_mac() const {
539✔
364
   return m_data->extensions().has<Encrypt_then_MAC>();
539✔
365
}
366

367
bool Client_Hello::sent_signature_algorithms() const {
×
368
   return m_data->extensions().has<Signature_Algorithms>();
×
369
}
370

371
std::vector<std::string> Client_Hello::next_protocols() const {
149✔
372
   if(auto* alpn = m_data->extensions().get<Application_Layer_Protocol_Notification>()) {
149✔
373
      return alpn->protocols();
149✔
374
   }
375
   return {};
×
376
}
377

378
std::vector<uint16_t> Client_Hello::srtp_profiles() const {
295✔
379
   if(const SRTP_Protection_Profiles* srtp = m_data->extensions().get<SRTP_Protection_Profiles>()) {
295✔
380
      return srtp->profiles();
4✔
381
   }
382
   return {};
291✔
383
}
384

385
const std::vector<uint8_t>& Client_Hello::cookie() const {
790✔
386
   return m_data->hello_cookie();
790✔
387
}
388

389
/*
390
* Create a new Hello Request message
391
*/
392
Hello_Request::Hello_Request(Handshake_IO& io) {
×
393
   io.send(*this);
×
394
}
×
395

396
/*
397
* Deserialize a Hello Request message
398
*/
399
Hello_Request::Hello_Request(const std::vector<uint8_t>& buf) {
42✔
400
   if(!buf.empty()) {
42✔
401
      throw Decoding_Error("Bad Hello_Request, has non-zero size");
2✔
402
   }
403
}
40✔
404

405
/*
406
* Serialize a Hello Request message
407
*/
408
std::vector<uint8_t> Hello_Request::serialize() const {
×
409
   return std::vector<uint8_t>();
×
410
}
411

412
void Client_Hello_12::add_tls12_supported_groups_extensions(const Policy& policy) {
2,696✔
413
   // RFC 7919 3.
414
   //    A client that offers a group MUST be able and willing to perform a DH
415
   //    key exchange using that group.
416
   //
417
   // We don't support hybrid key exchange in TLS 1.2
418

419
   std::vector<Group_Params> compatible_kex_groups;
2,696✔
420
   for(const auto& group : policy.key_exchange_groups()) {
37,319✔
421
      if(!group.is_post_quantum()) {
34,623✔
422
         compatible_kex_groups.push_back(group);
26,642✔
423
      }
424
   }
×
425

426
   auto supported_groups = std::make_unique<Supported_Groups>(std::move(compatible_kex_groups));
2,696✔
427

428
   if(!supported_groups->ec_groups().empty()) {
5,384✔
429
      // NOLINTNEXTLINE(*-owning-memory)
430
      m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
2,688✔
431
   }
432

433
   m_data->extensions().add(std::move(supported_groups));
2,696✔
434
}
5,392✔
435

436
Client_Hello_12::Client_Hello_12(std::unique_ptr<Client_Hello_Internal> data) : Client_Hello(std::move(data)) {
2,396✔
437
   const uint16_t TLS_EMPTY_RENEGOTIATION_INFO_SCSV = 0x00FF;
2,396✔
438

439
   if(offered_suite(static_cast<uint16_t>(TLS_EMPTY_RENEGOTIATION_INFO_SCSV))) {
2,396✔
440
      if(const Renegotiation_Extension* reneg = m_data->extensions().get<Renegotiation_Extension>()) {
12✔
441
         if(!reneg->renegotiation_info().empty()) {
×
442
            throw TLS_Exception(Alert::HandshakeFailure, "Client sent renegotiation SCSV and non-empty extension");
×
443
         }
444
      } else {
445
         // add fake extension
446
         m_data->extensions().add(new Renegotiation_Extension());  // NOLINT(*-owning-memory)
12✔
447
      }
448
   }
449
}
2,396✔
450

451
namespace {
452

453
// Avoid sending an IPv4/IPv6 address in SNI as this is prohibited
454
bool hostname_acceptable_for_sni(std::string_view hostname) {
3,684✔
455
   if(hostname.empty()) {
3,684✔
456
      return false;
457
   }
458

459
   if(string_to_ipv4(hostname).has_value()) {
3,637✔
460
      return false;
461
   }
462

463
   // IPv6? Anyway ':' is not valid in DNS
464
   if(hostname.find(':') != std::string_view::npos) {
3,637✔
465
      return false;
466
   }
467

468
   return true;
469
}
470

471
}  // namespace
472

473
// Note: This delegates to the Client_Hello_12 constructor to take advantage
474
//       of the sanity checks there.
475
Client_Hello_12::Client_Hello_12(const std::vector<uint8_t>& buf) :
2,896✔
476
      Client_Hello_12(std::make_unique<Client_Hello_Internal>(buf)) {}
2,896✔
477

478
/*
479
* Create a new Client Hello message
480
*/
481
Client_Hello_12::Client_Hello_12(Handshake_IO& io,
2,484✔
482
                                 Handshake_Hash& hash,
483
                                 const Policy& policy,
484
                                 Callbacks& cb,
485
                                 RandomNumberGenerator& rng,
486
                                 const std::vector<uint8_t>& reneg_info,
487
                                 const Client_Hello_12::Settings& client_settings,
488
                                 const std::vector<std::string>& next_protocols) {
2,484✔
489
   m_data->m_legacy_version = client_settings.protocol_version();
2,484✔
490
   m_data->m_random = make_hello_random(rng, cb, policy);
2,484✔
491
   m_data->m_suites = policy.ciphersuite_list(client_settings.protocol_version());
2,484✔
492

493
   if(!policy.acceptable_protocol_version(m_data->legacy_version())) {
2,484✔
494
      throw Internal_Error("Offering " + m_data->legacy_version().to_string() +
×
495
                           " but our own policy does not accept it");
×
496
   }
497

498
   /*
499
   * Place all empty extensions in front to avoid a bug in some systems
500
   * which reject hellos when the last extension in the list is empty.
501
   */
502

503
   // NOLINTBEGIN(*-owning-memory)
504

505
   // EMS must always be used with TLS 1.2, regardless of the policy used.
506

507
   m_data->extensions().add(new Extended_Master_Secret);
2,484✔
508

509
   if(policy.negotiate_encrypt_then_mac()) {
2,484✔
510
      m_data->extensions().add(new Encrypt_then_MAC);
2,471✔
511
   }
512

513
   m_data->extensions().add(new Session_Ticket_Extension());
2,484✔
514

515
   m_data->extensions().add(new Renegotiation_Extension(reneg_info));
2,484✔
516

517
   m_data->extensions().add(new Supported_Versions(m_data->legacy_version(), policy));
2,484✔
518

519
   if(hostname_acceptable_for_sni(client_settings.hostname())) {
2,484✔
520
      m_data->extensions().add(new Server_Name_Indicator(client_settings.hostname()));
2,461✔
521
   }
522

523
   if(policy.support_cert_status_message()) {
2,484✔
524
      m_data->extensions().add(new Certificate_Status_Request({}, {}));
4,330✔
525
   }
526

527
   add_tls12_supported_groups_extensions(policy);
2,484✔
528

529
   m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
2,484✔
530
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
2,484✔
531
      // RFC 8446 4.2.3
532
      //    TLS 1.2 implementations SHOULD also process this extension.
533
      //    Implementations which have the same policy in both cases MAY omit
534
      //    the "signature_algorithms_cert" extension.
535
      m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
×
536
   }
×
537

538
   if(reneg_info.empty() && !next_protocols.empty()) {
2,484✔
539
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
125✔
540
   }
541

542
   if(m_data->legacy_version().is_datagram_protocol()) {
2,484✔
543
      m_data->extensions().add(new SRTP_Protection_Profiles(policy.srtp_profiles()));
1,203✔
544
   }
545

546
   // NOLINTEND(*-owning-memory)
547

548
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Client, type());
2,484✔
549

550
   hash.update(io.send(*this));
4,968✔
551
}
2,484✔
552

553
/*
554
* Create a new Client Hello message (session resumption case)
555
*/
556
Client_Hello_12::Client_Hello_12(Handshake_IO& io,
212✔
557
                                 Handshake_Hash& hash,
558
                                 const Policy& policy,
559
                                 Callbacks& cb,
560
                                 RandomNumberGenerator& rng,
561
                                 const std::vector<uint8_t>& reneg_info,
562
                                 const Session_with_Handle& session,
563
                                 const std::vector<std::string>& next_protocols) {
212✔
564
   m_data->m_legacy_version = session.session.version();
212✔
565
   m_data->m_random = make_hello_random(rng, cb, policy);
212✔
566

567
   // RFC 5077 3.4
568
   //    When presenting a ticket, the client MAY generate and include a
569
   //    Session ID in the TLS ClientHello. [...] If a ticket is presented by
570
   //    the client, the server MUST NOT attempt to use the Session ID in the
571
   //    ClientHello for stateful session resumption.
572
   m_data->m_session_id = session.handle.id().value_or(Session_ID(make_hello_random(rng, cb, policy)));
456✔
573
   m_data->m_suites = policy.ciphersuite_list(m_data->legacy_version());
212✔
574

575
   if(!policy.acceptable_protocol_version(session.session.version())) {
212✔
576
      throw Internal_Error("Offering " + m_data->legacy_version().to_string() +
×
577
                           " but our own policy does not accept it");
×
578
   }
579

580
   if(!value_exists(m_data->ciphersuites(), session.session.ciphersuite_code())) {
424✔
581
      m_data->m_suites.push_back(session.session.ciphersuite_code());
4✔
582
   }
583

584
   /*
585
   * As EMS must always be used with TLS 1.2, add it even if it wasn't used
586
   * in the original session. If the server understands it and follows the
587
   * RFC it should reject our resume attempt and upgrade us to a new session
588
   * with the EMS protection.
589
   */
590
   // NOLINTBEGIN(*-owning-memory)
591
   m_data->extensions().add(new Extended_Master_Secret);
212✔
592

593
   if(session.session.supports_encrypt_then_mac()) {
212✔
594
      m_data->extensions().add(new Encrypt_then_MAC);
1✔
595
   }
596

597
   if(session.handle.is_ticket()) {
212✔
598
      m_data->extensions().add(new Session_Ticket_Extension(session.handle.ticket().value()));
360✔
599
   }
600

601
   m_data->extensions().add(new Renegotiation_Extension(reneg_info));
212✔
602

603
   const std::string hostname = session.session.server_info().hostname();
212✔
604

605
   if(hostname_acceptable_for_sni(hostname)) {
212✔
606
      m_data->extensions().add(new Server_Name_Indicator(hostname));
212✔
607
   }
608

609
   if(policy.support_cert_status_message()) {
212✔
610
      m_data->extensions().add(new Certificate_Status_Request({}, {}));
62✔
611
   }
612

613
   add_tls12_supported_groups_extensions(policy);
212✔
614

615
   m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
212✔
616
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
212✔
617
      // RFC 8446 4.2.3
618
      //    TLS 1.2 implementations SHOULD also process this extension.
619
      //    Implementations which have the same policy in both cases MAY omit
620
      //    the "signature_algorithms_cert" extension.
621
      m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
×
622
   }
×
623

624
   if(reneg_info.empty() && !next_protocols.empty()) {
212✔
625
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
16✔
626
   }
627
   // NOLINTEND(*-owning-memory)
628

629
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Client, type());
212✔
630

631
   hash.update(io.send(*this));
636✔
632
}
212✔
633

634
#if defined(BOTAN_HAS_TLS_13)
635

636
Client_Hello_13::Client_Hello_13(std::unique_ptr<Client_Hello_Internal> data) : Client_Hello(std::move(data)) {
465✔
637
   const auto& exts = m_data->extensions();
465✔
638

639
   // RFC 8446 4.1.2
640
   //    TLS 1.3 ClientHellos are identified as having a legacy_version of
641
   //    0x0303 and a "supported_versions" extension present with 0x0304 as the
642
   //    highest version indicated therein.
643
   //
644
   // Note that we already checked for "supported_versions" before entering this
645
   // c'tor in `Client_Hello_13::parse()`. This is just to be doubly sure.
646
   BOTAN_ASSERT_NOMSG(exts.has<Supported_Versions>());
465✔
647

648
   // RFC 8446 4.2.1
649
   //    Servers MAY abort the handshake upon receiving a ClientHello with
650
   //    legacy_version 0x0304 or later.
651
   if(m_data->legacy_version().is_tls_13_or_later()) {
465✔
652
      throw TLS_Exception(Alert::DecodeError, "TLS 1.3 Client Hello has invalid legacy_version");
1✔
653
   }
654

655
   // RFC 8446 4.1.2
656
   //    For every TLS 1.3 ClientHello, [the compression method] MUST contain
657
   //    exactly one byte, set to zero, [...].  If a TLS 1.3 ClientHello is
658
   //    received with any other value in this field, the server MUST abort the
659
   //    handshake with an "illegal_parameter" alert.
660
   if(m_data->comp_methods().size() != 1 || m_data->comp_methods().front() != 0) {
464✔
661
      throw TLS_Exception(Alert::IllegalParameter, "Client did not offer NULL compression");
4✔
662
   }
663

664
   // RFC 8446 4.2.9
665
   //    A client MUST provide a "psk_key_exchange_modes" extension if it
666
   //    offers a "pre_shared_key" extension. If clients offer "pre_shared_key"
667
   //    without a "psk_key_exchange_modes" extension, servers MUST abort
668
   //    the handshake.
669
   if(exts.has<PSK>()) {
460✔
670
      if(!exts.has<PSK_Key_Exchange_Modes>()) {
116✔
671
         throw TLS_Exception(Alert::MissingExtension,
1✔
672
                             "Client Hello offered a PSK without a psk_key_exchange_modes extension");
1✔
673
      }
674

675
      // RFC 8446 4.2.11
676
      //     The "pre_shared_key" extension MUST be the last extension in the
677
      //     ClientHello [...]. Servers MUST check that it is the last extension
678
      //     and otherwise fail the handshake with an "illegal_parameter" alert.
679
      if(exts.all().back()->type() != Extension_Code::PresharedKey) {
115✔
680
         throw TLS_Exception(Alert::IllegalParameter, "PSK extension was not at the very end of the Client Hello");
2✔
681
      }
682
   }
683

684
   // RFC 8446 9.2
685
   //    [A TLS 1.3 ClientHello] message MUST meet the following requirements:
686
   //
687
   //     -  If not containing a "pre_shared_key" extension, it MUST contain
688
   //        both a "signature_algorithms" extension and a "supported_groups"
689
   //        extension.
690
   //
691
   //     -  If containing a "supported_groups" extension, it MUST also contain
692
   //        a "key_share" extension, and vice versa.  An empty
693
   //        KeyShare.client_shares vector is permitted.
694
   //
695
   //    Servers receiving a ClientHello which does not conform to these
696
   //    requirements MUST abort the handshake with a "missing_extension"
697
   //    alert.
698
   if(!exts.has<PSK>()) {
457✔
699
      if(!exts.has<Supported_Groups>() || !exts.has<Signature_Algorithms>()) {
687✔
700
         throw TLS_Exception(
2✔
701
            Alert::MissingExtension,
702
            "Non-PSK Client Hello did not contain supported_groups and signature_algorithms extensions");
2✔
703
      }
704
   }
705
   if(exts.has<Supported_Groups>() != exts.has<Key_Share>()) {
455✔
706
      throw TLS_Exception(Alert::MissingExtension,
2✔
707
                          "Client Hello must either contain both key_share and supported_groups extensions or neither");
2✔
708
   }
709

710
   if(exts.has<Key_Share>()) {
453✔
711
      auto* const supported_ext = exts.get<Supported_Groups>();
453✔
712
      BOTAN_ASSERT_NONNULL(supported_ext);
453✔
713
      const auto supports = supported_ext->groups();
453✔
714
      const auto offers = exts.get<Key_Share>()->offered_groups();
453✔
715

716
      // RFC 8446 4.2.8
717
      //    Each KeyShareEntry value MUST correspond to a group offered in the
718
      //    "supported_groups" extension and MUST appear in the same order.
719
      //    [...]
720
      //    Clients MUST NOT offer any KeyShareEntry values for groups not
721
      //    listed in the client's "supported_groups" extension.
722
      //
723
      // Note: We can assume that both `offers` and `supports` are unique lists
724
      //       as this is ensured in the parsing code of the extensions.
725
      auto found_in_supported_groups = [&supports, support_offset = -1](auto group) mutable {
2,363✔
726
         const auto i = std::find(supports.begin(), supports.end(), group);
1,910✔
727
         if(i == supports.end()) {
1,910✔
728
            return false;
729
         }
730

731
         const auto found_at = std::distance(supports.begin(), i);
1,910✔
732
         if(found_at <= support_offset) {
1,910✔
733
            return false;  // The order that groups appear in "key_share" and
734
                           // "supported_groups" must be the same
735
         }
736

737
         support_offset = static_cast<decltype(support_offset)>(found_at);
1,910✔
738
         return true;
1,910✔
739
      };
453✔
740

741
      for(const auto offered : offers) {
2,363✔
742
         // RFC 8446 4.2.8
743
         //    Servers MAY check for violations of these rules and abort the
744
         //    handshake with an "illegal_parameter" alert if one is violated.
745
         if(!found_in_supported_groups(offered)) {
1,910✔
746
            throw TLS_Exception(Alert::IllegalParameter,
×
747
                                "Offered key exchange groups do not align with claimed supported groups");
×
748
         }
749
      }
750
   }
906✔
751

752
   // TODO: Reject oid_filters extension if found (which is the only known extension that
753
   //       must not occur in the TLS 1.3 client hello.
754
   // RFC 8446 4.2.5
755
   //    [The oid_filters extension] MUST only be sent in the CertificateRequest message.
756
}
465✔
757

758
/*
759
* Create a new Client Hello message
760
*/
761
Client_Hello_13::Client_Hello_13(const Policy& policy,
988✔
762
                                 Callbacks& cb,
763
                                 RandomNumberGenerator& rng,
764
                                 std::string_view hostname,
765
                                 const std::vector<std::string>& next_protocols,
766
                                 std::optional<Session_with_Handle>& session,
767
                                 std::vector<ExternalPSK> psks) {
988✔
768
   // RFC 8446 4.1.2
769
   //    In TLS 1.3, the client indicates its version preferences in the
770
   //    "supported_versions" extension (Section 4.2.1) and the
771
   //    legacy_version field MUST be set to 0x0303, which is the version
772
   //    number for TLS 1.2.
773
   m_data->m_legacy_version = Protocol_Version::TLS_V12;
988✔
774
   m_data->m_random = make_hello_random(rng, cb, policy);
988✔
775
   m_data->m_suites = policy.ciphersuite_list(Protocol_Version::TLS_V13);
988✔
776

777
   if(policy.allow_tls12()) {
988✔
778
      // Note: DTLS 1.3 is NYI, hence dtls_12 is not checked
779
      const auto legacy_suites = policy.ciphersuite_list(Protocol_Version::TLS_V12);
959✔
780
      m_data->m_suites.insert(m_data->m_suites.end(), legacy_suites.cbegin(), legacy_suites.cend());
959✔
781
   }
959✔
782

783
   if(policy.tls_13_middlebox_compatibility_mode()) {
988✔
784
      // RFC 8446 4.1.2
785
      //    In compatibility mode (see Appendix D.4), this field MUST be non-empty,
786
      //    so a client not offering a pre-TLS 1.3 session MUST generate a new
787
      //    32-byte value.
788
      //
789
      // Note: we won't ever offer a TLS 1.2 session. In such a case we would
790
      //       have instantiated a TLS 1.2 client in the first place.
791
      m_data->m_session_id = Session_ID(make_hello_random(rng, cb, policy));
975✔
792
   }
793

794
   // NOLINTBEGIN(*-owning-memory)
795
   if(hostname_acceptable_for_sni(hostname)) {
988✔
796
      m_data->extensions().add(new Server_Name_Indicator(hostname));
964✔
797
   }
798

799
   m_data->extensions().add(new Supported_Groups(policy.key_exchange_groups()));
988✔
800

801
   m_data->extensions().add(new Key_Share(policy, cb, rng));
988✔
802

803
   m_data->extensions().add(new Supported_Versions(Protocol_Version::TLS_V13, policy));
988✔
804

805
   m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
988✔
806
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
988✔
807
      // RFC 8446 4.2.3
808
      //    Implementations which have the same policy in both cases MAY omit
809
      //    the "signature_algorithms_cert" extension.
810
      m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
×
811
   }
×
812

813
   // TODO: Support for PSK-only mode without a key exchange.
814
   //       This should be configurable in TLS::Policy and should allow no PSK
815
   //       support at all (e.g. to disable support for session resumption).
816
   m_data->extensions().add(new PSK_Key_Exchange_Modes({PSK_Key_Exchange_Mode::PSK_DHE_KE}));
988✔
817

818
   if(policy.support_cert_status_message()) {
988✔
819
      m_data->extensions().add(new Certificate_Status_Request({}, {}));
212✔
820
   }
821

822
   // We currently support "record_size_limit" for TLS 1.3 exclusively. Hence,
823
   // when TLS 1.2 is advertised as a supported protocol, we must not offer this
824
   // extension.
825
   if(policy.record_size_limit().has_value() && !policy.allow_tls12()) {
988✔
826
      m_data->extensions().add(new Record_Size_Limit(policy.record_size_limit().value()));
16✔
827
   }
828

829
   if(!next_protocols.empty()) {
988✔
830
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
11✔
831
   }
832

833
   // RFC 7250 4.1
834
   //    In order to indicate the support of raw public keys, clients include
835
   //    the client_certificate_type and/or the server_certificate_type
836
   //    extensions in an extended client hello message.
837
   m_data->extensions().add(new Client_Certificate_Type(policy.accepted_client_certificate_types()));
1,976✔
838
   m_data->extensions().add(new Server_Certificate_Type(policy.accepted_server_certificate_types()));
1,976✔
839

840
   if(policy.allow_tls12()) {
988✔
841
      m_data->extensions().add(new Renegotiation_Extension());
959✔
842
      m_data->extensions().add(new Session_Ticket_Extension());
959✔
843

844
      // EMS must always be used with TLS 1.2, regardless of the policy
845
      m_data->extensions().add(new Extended_Master_Secret);
959✔
846

847
      if(policy.negotiate_encrypt_then_mac()) {
959✔
848
         m_data->extensions().add(new Encrypt_then_MAC);
959✔
849
      }
850

851
      if(m_data->extensions().has<Supported_Groups>() &&
1,918✔
852
         !m_data->extensions().get<Supported_Groups>()->ec_groups().empty()) {
1,918✔
853
         m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
957✔
854
      }
855
   }
856

857
   if(session.has_value() || !psks.empty()) {
988✔
858
      m_data->extensions().add(new PSK(session, std::move(psks), cb));
108✔
859
   }
860
   // NOLINTEND(*-owning-memory)
861

862
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Client, type());
988✔
863

864
   if(m_data->extensions().has<PSK>()) {
988✔
865
      // RFC 8446 4.2.11
866
      //    The "pre_shared_key" extension MUST be the last extension in the
867
      //    ClientHello (this facilitates implementation [...]).
868
      if(m_data->extensions().all().back()->type() != Extension_Code::PresharedKey) {
108✔
869
         throw TLS_Exception(Alert::InternalError,
×
870
                             "Application modified extensions of Client Hello, PSK is not last anymore");
×
871
      }
872
      calculate_psk_binders({});
108✔
873
   }
874
}
988✔
875

876
std::variant<Client_Hello_13, Client_Hello_12> Client_Hello_13::parse(const std::vector<uint8_t>& buf) {
1,004✔
877
   auto data = std::make_unique<Client_Hello_Internal>(buf);
1,004✔
878
   const auto version = data->version();
978✔
879

880
   if(version.is_pre_tls_13()) {
978✔
881
      return Client_Hello_12(std::move(data));
1,026✔
882
   } else {
883
      return Client_Hello_13(std::move(data));
918✔
884
   }
885
}
978✔
886

887
void Client_Hello_13::retry(const Hello_Retry_Request& hrr,
61✔
888
                            const Transcript_Hash_State& transcript_hash_state,
889
                            Callbacks& cb,
890
                            RandomNumberGenerator& rng) {
891
   BOTAN_STATE_CHECK(m_data->extensions().has<Supported_Groups>());
61✔
892
   BOTAN_STATE_CHECK(m_data->extensions().has<Key_Share>());
61✔
893

894
   auto* hrr_ks = hrr.extensions().get<Key_Share>();
61✔
895
   const auto& supported_groups = m_data->extensions().get<Supported_Groups>()->groups();
61✔
896

897
   if(hrr.extensions().has<Key_Share>()) {
61✔
898
      m_data->extensions().get<Key_Share>()->retry_offer(*hrr_ks, supported_groups, cb, rng);
60✔
899
   }
900

901
   // RFC 8446 4.2.2
902
   //    When sending the new ClientHello, the client MUST copy
903
   //    the contents of the extension received in the HelloRetryRequest into
904
   //    a "cookie" extension in the new ClientHello.
905
   //
906
   // RFC 8446 4.2.2
907
   //    Clients MUST NOT use cookies in their initial ClientHello in subsequent
908
   //    connections.
909
   if(hrr.extensions().has<Cookie>()) {
58✔
910
      BOTAN_STATE_CHECK(!m_data->extensions().has<Cookie>());
3✔
911
      m_data->extensions().add(new Cookie(hrr.extensions().get<Cookie>()->get_cookie()));  // NOLINT(*-owning-memory)
3✔
912
   }
913

914
   // Note: the consumer of the TLS implementation won't be able to distinguish
915
   //       invocations to this callback due to the first Client_Hello or the
916
   //       retried Client_Hello after receiving a Hello_Retry_Request. We assume
917
   //       that the user keeps and detects this state themselves.
918
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Client, type());
58✔
919

920
   auto* psk = m_data->extensions().get<PSK>();
58✔
921
   if(psk != nullptr) {
58✔
922
      // Cipher suite should always be a known suite as this is checked upstream
923
      const auto cipher = Ciphersuite::by_id(hrr.ciphersuite());
11✔
924
      BOTAN_ASSERT_NOMSG(cipher.has_value());
11✔
925

926
      // RFC 8446 4.1.4
927
      //    In [...] its updated ClientHello, the client SHOULD NOT offer
928
      //    any pre-shared keys associated with a hash other than that of the
929
      //    selected cipher suite.
930
      psk->filter(cipher.value());
11✔
931

932
      // RFC 8446 4.2.11.2
933
      //    If the server responds with a HelloRetryRequest and the client
934
      //    then sends ClientHello2, its binder will be computed over: [...].
935
      calculate_psk_binders(transcript_hash_state.clone());
11✔
936
   }
937
}
58✔
938

939
void Client_Hello_13::validate_updates(const Client_Hello_13& new_ch) {
44✔
940
   // RFC 8446 4.1.2
941
   //    The client will also send a ClientHello when the server has responded
942
   //    to its ClientHello with a HelloRetryRequest. In that case, the client
943
   //    MUST send the same ClientHello without modification, except as follows:
944

945
   if(m_data->session_id() != new_ch.m_data->session_id() || m_data->random() != new_ch.m_data->random() ||
44✔
946
      m_data->ciphersuites() != new_ch.m_data->ciphersuites() ||
88✔
947
      m_data->comp_methods() != new_ch.m_data->comp_methods()) {
44✔
948
      throw TLS_Exception(Alert::IllegalParameter, "Client Hello core values changed after Hello Retry Request");
×
949
   }
950

951
   const auto oldexts = extension_types();
44✔
952
   const auto newexts = new_ch.extension_types();
44✔
953

954
   // Check that extension omissions are justified
955
   for(const auto oldext : oldexts) {
502✔
956
      if(!newexts.contains(oldext)) {
918✔
957
         auto* const ext = extensions().get(oldext);
1✔
958

959
         // We don't make any assumptions about unimplemented extensions.
960
         if(!ext->is_implemented()) {
1✔
961
            continue;
×
962
         }
963

964
         // RFC 8446 4.1.2
965
         //    Removing the "early_data" extension (Section 4.2.10) if one was
966
         //    present.  Early data is not permitted after a HelloRetryRequest.
967
         if(oldext == EarlyDataIndication::static_type()) {
1✔
968
            continue;
×
969
         }
970

971
         // RFC 8446 4.1.2
972
         //    Optionally adding, removing, or changing the length of the
973
         //    "padding" extension.
974
         //
975
         // TODO: implement the Padding extension
976
         // if(oldext == Padding::static_type())
977
         //    continue;
978

979
         throw TLS_Exception(Alert::IllegalParameter, "Extension removed in updated Client Hello");
1✔
980
      }
981
   }
982

983
   // Check that extension additions are justified
984
   for(const auto newext : newexts) {
495✔
985
      if(!oldexts.contains(newext)) {
904✔
986
         auto* const ext = new_ch.extensions().get(newext);
2✔
987

988
         // We don't make any assumptions about unimplemented extensions.
989
         if(!ext->is_implemented()) {
2✔
990
            continue;
1✔
991
         }
992

993
         // RFC 8446 4.1.2
994
         //    Including a "cookie" extension if one was provided in the
995
         //    HelloRetryRequest.
996
         if(newext == Cookie::static_type()) {
1✔
997
            continue;
1✔
998
         }
999

1000
         // RFC 8446 4.1.2
1001
         //    Optionally adding, removing, or changing the length of the
1002
         //    "padding" extension.
1003
         //
1004
         // TODO: implement the Padding extension
1005
         // if(newext == Padding::static_type())
1006
         //    continue;
1007

1008
         throw TLS_Exception(Alert::UnsupportedExtension, "Added an extension in updated Client Hello");
×
1009
      }
1010
   }
1011

1012
   // RFC 8446 4.1.2
1013
   //    Removing the "early_data" extension (Section 4.2.10) if one was
1014
   //    present.  Early data is not permitted after a HelloRetryRequest.
1015
   if(new_ch.extensions().has<EarlyDataIndication>()) {
43✔
1016
      throw TLS_Exception(Alert::IllegalParameter, "Updated Client Hello indicates early data");
×
1017
   }
1018

1019
   // TODO: Contents of extensions are not checked for update compatibility, see:
1020
   //
1021
   // RFC 8446 4.1.2
1022
   //    If a "key_share" extension was supplied in the HelloRetryRequest,
1023
   //    replacing the list of shares with a list containing a single
1024
   //    KeyShareEntry from the indicated group.
1025
   //
1026
   //    Updating the "pre_shared_key" extension if present by recomputing
1027
   //    the "obfuscated_ticket_age" and binder values and (optionally)
1028
   //    removing any PSKs which are incompatible with the server's
1029
   //    indicated cipher suite.
1030
   //
1031
   //    Optionally adding, removing, or changing the length of the
1032
   //    "padding" extension.
1033
}
44✔
1034

1035
void Client_Hello_13::calculate_psk_binders(Transcript_Hash_State transcript_hash) {
119✔
1036
   auto* psk = m_data->extensions().get<PSK>();
119✔
1037
   if(psk == nullptr || psk->empty()) {
119✔
1038
      return;
1✔
1039
   }
1040

1041
   // RFC 8446 4.2.11.2
1042
   //    Each entry in the binders list is computed as an HMAC over a
1043
   //    transcript hash (see Section 4.4.1) containing a partial ClientHello
1044
   //    [...].
1045
   //
1046
   // Therefore we marshal the entire message prematurely to obtain the
1047
   // (truncated) transcript hash, calculate the PSK binders with it, update
1048
   // the Client Hello thus finalizing the message. Down the road, it will be
1049
   // re-marshalled with the correct binders and sent over the wire.
1050
   Handshake_Layer::prepare_message(*this, transcript_hash);
118✔
1051
   psk->calculate_binders(transcript_hash);
118✔
1052
}
1053

1054
std::optional<Protocol_Version> Client_Hello_13::highest_supported_version(const Policy& policy) const {
390✔
1055
   // RFC 8446 4.2.1
1056
   //    The "supported_versions" extension is used by the client to indicate
1057
   //    which versions of TLS it supports and by the server to indicate which
1058
   //    version it is using. The extension contains a list of supported
1059
   //    versions in preference order, with the most preferred version first.
1060
   auto* const supvers = m_data->extensions().get<Supported_Versions>();
390✔
1061
   BOTAN_ASSERT_NONNULL(supvers);
390✔
1062

1063
   std::optional<Protocol_Version> result;
390✔
1064

1065
   for(const auto& v : supvers->versions()) {
1,977✔
1066
      // RFC 8446 4.2.1
1067
      //    Servers MUST only select a version of TLS present in that extension
1068
      //    and MUST ignore any unknown versions that are present in that
1069
      //    extension.
1070
      if(!v.known_version() || !policy.acceptable_protocol_version(v)) {
1,587✔
1071
         continue;
899✔
1072
      }
1073

1074
      result = (result.has_value()) ? std::optional(std::max(result.value(), v)) : std::optional(v);
986✔
1075
   }
1076

1077
   return result;
390✔
1078
}
1079

1080
#endif  // BOTAN_HAS_TLS_13
1081

1082
}  // 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