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

randombit / botan / 16248620128

13 Jul 2025 11:27AM UTC coverage: 90.565% (-0.01%) from 90.575%
16248620128

push

github

web-flow
Merge pull request #4983 from randombit/jack/clang-tidy-cppcoreguidelines-owning-memory

Enable and fix clang-tidy warning cppcoreguidelines-owning-memory

99026 of 109342 relevant lines covered (90.57%)

12444574.04 hits per line

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

91.89
/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/credentials_manager.h>
15
#include <botan/hash.h>
16
#include <botan/rng.h>
17
#include <botan/tls_callbacks.h>
18
#include <botan/tls_exceptn.h>
19
#include <botan/tls_version.h>
20

21
#include <botan/internal/parsing.h>
22
#include <botan/internal/stl_util.h>
23
#include <botan/internal/tls_handshake_hash.h>
24
#include <botan/internal/tls_handshake_io.h>
25
#include <botan/internal/tls_reader.h>
26
#include <botan/internal/tls_session_key.h>
27

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

33
#include <chrono>
34
#include <iterator>
35

36
namespace Botan::TLS {
37

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

41
   if(policy.hash_hello_random()) {
6,956✔
42
      auto sha256 = HashFunction::create_or_throw("SHA-256");
6,941✔
43
      sha256->update(buf);
6,941✔
44
      sha256->final(buf);
6,941✔
45
   }
6,941✔
46

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

53
      store_be(time32, buf.data());
6,933✔
54
   }
55

56
   return buf;
6,956✔
57
}
×
58

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

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

73
         TLS_Data_Reader reader("ClientHello", buf);
3,852✔
74

75
         const uint8_t major_version = reader.get_byte();
3,852✔
76
         const uint8_t minor_version = reader.get_byte();
3,852✔
77

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

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

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

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

92
         m_suites = reader.get_range_vector<uint16_t>(2, 1, 32767);
3,016✔
93
         m_comp_methods = reader.get_range_vector<uint8_t>(1, 1, 255);
2,888✔
94

95
         m_extensions.deserialize(reader, Connection_Side::Client, Handshake_Type::ClientHello);
2,887✔
96
      }
5,998✔
97

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

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

135
      Protocol_Version legacy_version() const { return m_legacy_version; }
17,265✔
136

137
      const Session_ID& session_id() const { return m_session_id; }
44✔
138

139
      const std::vector<uint8_t>& random() const { return m_random; }
4,388✔
140

141
      const std::vector<uint16_t>& ciphersuites() const { return m_suites; }
4,561✔
142

143
      const std::vector<uint8_t>& comp_methods() const { return m_comp_methods; }
4,808✔
144

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

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

149
      const Extensions& extensions() const { return m_extensions; }
978✔
150

151
      Extensions& extensions() { return m_extensions; }
84,251✔
152

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

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

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

169
Client_Hello::~Client_Hello() = default;
17,014✔
170

171
Client_Hello::Client_Hello() : m_data(std::make_unique<Client_Hello_Internal>()) {}
3,681✔
172

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

180
Handshake_Type Client_Hello::type() const {
16,299✔
181
   return Handshake_Type::ClientHello;
16,299✔
182
}
183

184
Protocol_Version Client_Hello::legacy_version() const {
8,231✔
185
   return m_data->legacy_version();
8,231✔
186
}
187

188
const std::vector<uint8_t>& Client_Hello::random() const {
3,824✔
189
   return m_data->random();
3,824✔
190
}
191

192
const Session_ID& Client_Hello::session_id() const {
3,318✔
193
   return m_data->session_id();
3,318✔
194
}
195

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

200
const std::vector<uint16_t>& Client_Hello::ciphersuites() const {
1,427✔
201
   return m_data->ciphersuites();
1,427✔
202
}
203

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

208
const Extensions& Client_Hello::extensions() const {
9,815✔
209
   return m_data->extensions();
9,815✔
210
}
211

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

215
   m_data->m_hello_cookie = hello_verify.cookie();
467✔
216
}
467✔
217

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

225
   buf.push_back(m_data->legacy_version().major_version());
4,344✔
226
   buf.push_back(m_data->legacy_version().minor_version());
4,344✔
227
   buf += m_data->random();
4,344✔
228

229
   append_tls_length_value(buf, m_data->session_id().get(), 1);
4,344✔
230

231
   if(m_data->legacy_version().is_datagram_protocol()) {
4,344✔
232
      append_tls_length_value(buf, m_data->hello_cookie(), 1);
951✔
233
   }
234

235
   append_tls_length_value(buf, m_data->ciphersuites(), 2);
4,344✔
236
   append_tls_length_value(buf, m_data->comp_methods(), 1);
4,344✔
237

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

244
   buf += m_data->extensions().serialize(Connection_Side::Client);
4,344✔
245

246
   return buf;
4,344✔
247
}
×
248

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

252
   return m_data->hello_cookie_input_bits();
790✔
253
}
254

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

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

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

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

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

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

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

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

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

321
std::vector<Protocol_Version> Client_Hello::supported_versions() const {
1,382✔
322
   if(Supported_Versions* versions = m_data->extensions().get<Supported_Versions>()) {
1,382✔
323
      return versions->versions();
285✔
324
   }
325
   return {};
1,097✔
326
}
327

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

332
Session_Ticket Client_Hello_12::session_ticket() const {
1,906✔
333
   if(auto* ticket = m_data->extensions().get<Session_Ticket_Extension>()) {
1,906✔
334
      return ticket->contents();
1,762✔
335
   }
336
   return {};
144✔
337
}
338

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

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

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

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

365
bool Client_Hello_12::supports_encrypt_then_mac() const {
542✔
366
   return m_data->extensions().has<Encrypt_then_MAC>();
542✔
367
}
368

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

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

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

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

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

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

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

414
void Client_Hello_12::add_tls12_supported_groups_extensions(const Policy& policy) {
2,693✔
415
   // RFC 7919 3.
416
   //    A client that offers a group MUST be able and willing to perform a DH
417
   //    key exchange using that group.
418
   //
419
   // We don't support hybrid key exchange in TLS 1.2
420
   const std::vector<Group_Params> kex_groups = policy.key_exchange_groups();
2,693✔
421
   std::vector<Group_Params> compatible_kex_groups;
2,693✔
422
   std::copy_if(kex_groups.begin(), kex_groups.end(), std::back_inserter(compatible_kex_groups), [](const auto group) {
2,693✔
423
      return !group.is_post_quantum();
34,620✔
424
   });
425

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

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

433
   m_data->extensions().add(std::move(supported_groups));
2,693✔
434
}
8,079✔
435

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

439
   if(offered_suite(static_cast<uint16_t>(TLS_EMPTY_RENEGOTIATION_INFO_SCSV))) {
2,393✔
440
      if(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,393✔
450

451
namespace {
452

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

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

463
   // IPv6? Anyway ':' is not valid in DNS
464
   if(hostname.find(':') != std::string_view::npos) {
3,634✔
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,893✔
476
      Client_Hello_12(std::make_unique<Client_Hello_Internal>(buf)) {}
2,893✔
477

478
/*
479
* Create a new Client Hello message
480
*/
481
Client_Hello_12::Client_Hello_12(Handshake_IO& io,
2,476✔
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,476✔
489
   m_data->m_legacy_version = client_settings.protocol_version();
2,476✔
490
   m_data->m_random = make_hello_random(rng, cb, policy);
2,476✔
491
   m_data->m_suites = policy.ciphersuite_list(client_settings.protocol_version());
2,476✔
492

493
   if(!policy.acceptable_protocol_version(m_data->legacy_version())) {
2,476✔
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,476✔
508

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

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

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

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

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

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

527
   add_tls12_supported_groups_extensions(policy);
2,476✔
528

529
   m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
2,476✔
530
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
2,476✔
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,476✔
539
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
117✔
540
   }
541

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

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

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

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

553
/*
554
* Create a new Client Hello message (session resumption case)
555
*/
556
Client_Hello_12::Client_Hello_12(Handshake_IO& io,
217✔
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) {
217✔
564
   m_data->m_legacy_version = session.session.version();
217✔
565
   m_data->m_random = make_hello_random(rng, cb, policy);
217✔
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)));
471✔
573
   m_data->m_suites = policy.ciphersuite_list(m_data->legacy_version());
217✔
574

575
   if(!policy.acceptable_protocol_version(session.session.version())) {
217✔
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())) {
434✔
581
      m_data->m_suites.push_back(session.session.ciphersuite_code());
6✔
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);
217✔
592

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

597
   if(session.handle.is_ticket()) {
217✔
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));
217✔
602

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

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

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

613
   add_tls12_supported_groups_extensions(policy);
217✔
614

615
   m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
217✔
616
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
217✔
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()) {
217✔
625
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
21✔
626
   }
627
   // NOLINTEND(*-owning-memory)
628

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

631
   hash.update(io.send(*this));
651✔
632
}
217✔
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) {
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 ths) {
119✔
1036
   auto* psk = m_data->extensions().get<PSK>();
119✔
1037
   if(!psk || 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, ths);
118✔
1051
   psk->calculate_binders(ths);
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