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

randombit / botan / 21746428601

06 Feb 2026 07:47AM UTC coverage: 90.074% (+0.002%) from 90.072%
21746428601

push

github

web-flow
Merge pull request #5288 from Rohde-Schwarz/feature/disentangle_tls12_from_tls13

Refactor: Organize most TLS handshake messages into TLS 1.2 and TLS 1.3 modules

102235 of 113501 relevant lines covered (90.07%)

11561717.77 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_12.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/tls_messages_13.h>
28
   #include <botan/internal/tls_handshake_layer_13.h>
29
   #include <botan/internal/tls_transcript_hash_13.h>
30
#endif
31

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

35
namespace Botan::TLS {
36

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

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

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

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

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

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

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

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

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

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

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

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

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

91
         m_suites = reader.get_range_vector<uint16_t>(2, 1, 32767);
3,025✔
92
         m_comp_methods = reader.get_range_vector<uint8_t>(1, 1, 255);
2,890✔
93

94
         m_extensions.deserialize(reader, Connection_Side::Client, Handshake_Type::ClientHello);
2,890✔
95
      }
6,000✔
96

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

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

134
      Protocol_Version legacy_version() const { return m_legacy_version; }
17,294✔
135

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

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

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

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

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

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

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

150
      Extensions& extensions() { return m_extensions; }
84,432✔
151

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

452
namespace {
453

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

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

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

469
   return true;
470
}
471

472
}  // namespace
473

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

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

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

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

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

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

508
   m_data->extensions().add(new Extended_Master_Secret);
2,486✔
509

510
   if(policy.negotiate_encrypt_then_mac()) {
2,486✔
511
      m_data->extensions().add(new Encrypt_then_MAC);
2,473✔
512
   }
513

514
   m_data->extensions().add(new Session_Ticket_Extension());
2,486✔
515

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

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

520
   if(hostname_acceptable_for_sni(client_settings.hostname())) {
2,486✔
521
      m_data->extensions().add(new Server_Name_Indicator(client_settings.hostname()));
2,463✔
522
   }
523

524
   if(policy.support_cert_status_message()) {
2,486✔
525
      m_data->extensions().add(new Certificate_Status_Request({}, {}));
4,334✔
526
   }
527

528
   add_tls12_supported_groups_extensions(policy);
2,486✔
529

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

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

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

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

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

551
   hash.update(io.send(*this));
4,972✔
552
}
2,486✔
553

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

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

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

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

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

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

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

602
   m_data->extensions().add(new Renegotiation_Extension(reneg_info));
210✔
603

604
   const std::string hostname = session.session.server_info().hostname();
210✔
605

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

610
   if(policy.support_cert_status_message()) {
210✔
611
      m_data->extensions().add(new Certificate_Status_Request({}, {}));
58✔
612
   }
613

614
   add_tls12_supported_groups_extensions(policy);
210✔
615

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

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

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

632
   hash.update(io.send(*this));
630✔
633
}
210✔
634

635
#if defined(BOTAN_HAS_TLS_13)
636

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1078
   return result;
390✔
1079
}
1080

1081
#endif  // BOTAN_HAS_TLS_13
1082

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