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

randombit / botan / 5575778031

17 Jul 2023 12:44PM UTC coverage: 91.643% (-0.1%) from 91.744%
5575778031

Pull #3609

github

web-flow
Merge 3f7c9c42d into e430d3e4d
Pull Request #3609: [TLS 1.3] Hybrid PQ/T key establishment

78672 of 85846 relevant lines covered (91.64%)

12332050.04 hits per line

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

91.54
/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/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
#include <botan/internal/tls_session_key.h>
26

27
#ifdef BOTAN_HAS_TLS_13
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
enum {
38
   TLS_EMPTY_RENEGOTIATION_INFO_SCSV = 0x00FF,
39
};
40

41
std::vector<uint8_t> make_hello_random(RandomNumberGenerator& rng, Callbacks& cb, const Policy& policy) {
6,700✔
42
   auto buf = rng.random_vec<std::vector<uint8_t>>(32);
6,700✔
43

44
   if(policy.hash_hello_random()) {
6,700✔
45
      auto sha256 = HashFunction::create_or_throw("SHA-256");
6,689✔
46
      sha256->update(buf);
6,689✔
47
      sha256->final(buf);
6,689✔
48
   }
6,689✔
49

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

56
      store_be(time32, buf.data());
6,681✔
57
   }
58

59
   return buf;
6,700✔
60
}
×
61

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

71
      Client_Hello_Internal(const std::vector<uint8_t>& buf) {
3,728✔
72
         if(buf.size() < 41) {
3,728✔
73
            throw Decoding_Error("Client_Hello: Packet corrupted");
45✔
74
         }
75

76
         TLS_Data_Reader reader("ClientHello", buf);
3,683✔
77

78
         const uint8_t major_version = reader.get_byte();
3,683✔
79
         const uint8_t minor_version = reader.get_byte();
3,683✔
80

81
         m_legacy_version = Protocol_Version(major_version, minor_version);
3,683✔
82
         m_random = reader.get_fixed<uint8_t>(32);
3,683✔
83
         m_session_id = Session_ID(reader.get_range<uint8_t>(1, 0, 32));
3,683✔
84

85
         if(m_legacy_version.is_datagram_protocol()) {
2,829✔
86
            auto sha256 = HashFunction::create_or_throw("SHA-256");
745✔
87
            sha256->update(reader.get_data_read_so_far());
745✔
88

89
            m_hello_cookie = reader.get_range<uint8_t>(1, 0, 255);
745✔
90

91
            sha256->update(reader.get_remaining());
745✔
92
            m_cookie_input_bits = sha256->final_stdvec();
745✔
93
         }
745✔
94

95
         m_suites = reader.get_range_vector<uint16_t>(2, 1, 32767);
2,829✔
96
         m_comp_methods = reader.get_range_vector<uint8_t>(1, 1, 255);
2,718✔
97

98
         m_extensions.deserialize(reader, Connection_Side::Client, Handshake_Type::ClientHello);
2,718✔
99
      }
4,787✔
100

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

133
         // Note: The Client_Hello_13 class will make sure that legacy_version
134
         //       is exactly 0x0303 (aka ossified TLS 1.2)
135
         return Protocol_Version::TLS_V13;
445✔
136
      }
137

138
      Protocol_Version legacy_version() const { return m_legacy_version; }
14,264✔
139

140
      const Session_ID& session_id() const { return m_session_id; }
30✔
141

142
      const std::vector<uint8_t>& random() const { return m_random; }
4,192✔
143

144
      const std::vector<uint16_t>& ciphersuites() const { return m_suites; }
4,379✔
145

146
      const std::vector<uint8_t>& comp_methods() const { return m_comp_methods; }
4,606✔
147

148
      const std::vector<uint8_t>& hello_cookie() const { return m_hello_cookie; }
869✔
149

150
      const std::vector<uint8_t>& hello_cookie_input_bits() const { return m_cookie_input_bits; }
738✔
151

152
      const Extensions& extensions() const { return m_extensions; }
926✔
153

154
      Extensions& extensions() { return m_extensions; }
73,740✔
155

156
   public:
157
      Protocol_Version m_legacy_version;    // NOLINT(*-non-private-member-variables-in-classes)
158
      Session_ID m_session_id;              // NOLINT(*-non-private-member-variables-in-classes)
159
      std::vector<uint8_t> m_random;        // NOLINT(*-non-private-member-variables-in-classes)
160
      std::vector<uint16_t> m_suites;       // NOLINT(*-non-private-member-variables-in-classes)
161
      std::vector<uint8_t> m_comp_methods;  // NOLINT(*-non-private-member-variables-in-classes)
162
      Extensions m_extensions;              // NOLINT(*-non-private-member-variables-in-classes)
163

164
      // These fields are only for DTLS:
165
      std::vector<uint8_t> m_hello_cookie;       // NOLINT(*-non-private-member-variables-in-classes)
166
      std::vector<uint8_t> m_cookie_input_bits;  // NOLINT(*-non-private-member-variables-in-classes)
167
};
168

169
Client_Hello::Client_Hello(Client_Hello&&) noexcept = default;
9,988✔
170
Client_Hello& Client_Hello::operator=(Client_Hello&&) noexcept = default;
29✔
171

172
Client_Hello::~Client_Hello() = default;
16,258✔
173

174
Client_Hello::Client_Hello() : m_data(std::make_unique<Client_Hello_Internal>()) {}
3,598✔
175

176
/*
177
* Read a counterparty client hello
178
*/
179
Client_Hello::Client_Hello(std::unique_ptr<Client_Hello_Internal> data) : m_data(std::move(data)) {
2,689✔
180
   BOTAN_ASSERT_NONNULL(m_data);
2,689✔
181
}
2,689✔
182

183
Handshake_Type Client_Hello::type() const {
15,543✔
184
   return Handshake_Type::ClientHello;
15,543✔
185
}
186

187
Protocol_Version Client_Hello::legacy_version() const {
4,802✔
188
   return m_data->legacy_version();
4,802✔
189
}
190

191
const std::vector<uint8_t>& Client_Hello::random() const {
3,497✔
192
   return m_data->random();
3,497✔
193
}
194

195
const Session_ID& Client_Hello::session_id() const {
3,072✔
196
   return m_data->session_id();
3,072✔
197
}
198

199
const std::vector<uint8_t>& Client_Hello::compression_methods() const {
1,295✔
200
   return m_data->comp_methods();
1,295✔
201
}
202

203
const std::vector<uint16_t>& Client_Hello::ciphersuites() const {
1,350✔
204
   return m_data->ciphersuites();
1,350✔
205
}
206

207
std::set<Extension_Code> Client_Hello::extension_types() const {
1,962✔
208
   return m_data->extensions().extension_types();
1,962✔
209
}
210

211
const Extensions& Client_Hello::extensions() const {
9,281✔
212
   return m_data->extensions();
9,281✔
213
}
214

215
void Client_Hello_12::update_hello_cookie(const Hello_Verify_Request& hello_verify) {
426✔
216
   BOTAN_STATE_CHECK(m_data->legacy_version().is_datagram_protocol());
426✔
217

218
   m_data->m_hello_cookie = hello_verify.cookie();
426✔
219
}
426✔
220

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

228
   buf.push_back(m_data->legacy_version().major_version());
4,162✔
229
   buf.push_back(m_data->legacy_version().minor_version());
4,162✔
230
   buf += m_data->random();
4,162✔
231

232
   append_tls_length_value(buf, m_data->session_id().get(), 1);
4,162✔
233

234
   if(m_data->legacy_version().is_datagram_protocol()) {
4,162✔
235
      append_tls_length_value(buf, m_data->hello_cookie(), 1);
869✔
236
   }
237

238
   append_tls_length_value(buf, m_data->ciphersuites(), 2);
4,162✔
239
   append_tls_length_value(buf, m_data->comp_methods(), 1);
4,162✔
240

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

247
   buf += m_data->extensions().serialize(Connection_Side::Client);
4,162✔
248

249
   return buf;
4,162✔
250
}
×
251

252
std::vector<uint8_t> Client_Hello::cookie_input_data() const {
738✔
253
   BOTAN_STATE_CHECK(!m_data->hello_cookie_input_bits().empty());
738✔
254

255
   return m_data->hello_cookie_input_bits();
738✔
256
}
257

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

266
std::vector<Signature_Scheme> Client_Hello::signature_schemes() const {
3,677✔
267
   if(Signature_Algorithms* sigs = m_data->extensions().get<Signature_Algorithms>()) {
3,677✔
268
      return sigs->supported_schemes();
3,675✔
269
   }
270
   return {};
2✔
271
}
272

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

285
std::vector<Group_Params> Client_Hello::supported_ecc_curves() const {
1,250✔
286
   if(Supported_Groups* groups = m_data->extensions().get<Supported_Groups>()) {
1,250✔
287
      return groups->ec_groups();
1,243✔
288
   }
289
   return {};
7✔
290
}
291

292
std::vector<Group_Params> Client_Hello::supported_dh_groups() const {
4✔
293
   if(Supported_Groups* groups = m_data->extensions().get<Supported_Groups>()) {
4✔
294
      return groups->dh_groups();
4✔
295
   }
296
   return std::vector<Group_Params>();
×
297
}
298

299
bool Client_Hello_12::prefers_compressed_ec_points() const {
15✔
300
   if(Supported_Point_Formats* ecc_formats = m_data->extensions().get<Supported_Point_Formats>()) {
15✔
301
      return ecc_formats->prefers_compressed();
15✔
302
   }
303
   return false;
304
}
305

306
std::string Client_Hello::sni_hostname() const {
3,909✔
307
   if(Server_Name_Indicator* sni = m_data->extensions().get<Server_Name_Indicator>()) {
3,909✔
308
      return sni->host_name();
3,779✔
309
   }
310
   return "";
130✔
311
}
312

313
bool Client_Hello_12::secure_renegotiation() const {
4,967✔
314
   return m_data->extensions().has<Renegotiation_Extension>();
4,967✔
315
}
316

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

324
std::vector<Protocol_Version> Client_Hello::supported_versions() const {
1,297✔
325
   if(Supported_Versions* versions = m_data->extensions().get<Supported_Versions>()) {
1,297✔
326
      return versions->versions();
200✔
327
   }
328
   return {};
1,097✔
329
}
330

331
bool Client_Hello_12::supports_session_ticket() const {
1,143✔
332
   return m_data->extensions().has<Session_Ticket_Extension>();
1,143✔
333
}
334

335
Session_Ticket Client_Hello_12::session_ticket() const {
1,846✔
336
   if(auto* ticket = m_data->extensions().get<Session_Ticket_Extension>()) {
1,846✔
337
      return ticket->contents();
1,698✔
338
   }
339
   return {};
148✔
340
}
341

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

356
bool Client_Hello::supports_alpn() const {
1,057✔
357
   return m_data->extensions().has<Application_Layer_Protocol_Notification>();
1,057✔
358
}
359

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

364
bool Client_Hello_12::supports_cert_status_message() const {
1,304✔
365
   return m_data->extensions().has<Certificate_Status_Request>();
1,304✔
366
}
367

368
bool Client_Hello_12::supports_encrypt_then_mac() const {
547✔
369
   return m_data->extensions().has<Encrypt_then_MAC>();
547✔
370
}
371

372
bool Client_Hello::sent_signature_algorithms() const {
×
373
   return m_data->extensions().has<Signature_Algorithms>();
×
374
}
375

376
std::vector<std::string> Client_Hello::next_protocols() const {
142✔
377
   if(auto alpn = m_data->extensions().get<Application_Layer_Protocol_Notification>()) {
142✔
378
      return alpn->protocols();
142✔
379
   }
380
   return {};
×
381
}
382

383
std::vector<uint16_t> Client_Hello::srtp_profiles() const {
264✔
384
   if(SRTP_Protection_Profiles* srtp = m_data->extensions().get<SRTP_Protection_Profiles>()) {
264✔
385
      return srtp->profiles();
4✔
386
   }
387
   return {};
260✔
388
}
389

390
const std::vector<uint8_t>& Client_Hello::cookie() const {
738✔
391
   return m_data->hello_cookie();
738✔
392
}
393

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

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

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

417
Client_Hello_12::Client_Hello_12(std::unique_ptr<Client_Hello_Internal> data) : Client_Hello(std::move(data)) {
2,244✔
418
   if(offered_suite(static_cast<uint16_t>(TLS_EMPTY_RENEGOTIATION_INFO_SCSV))) {
2,244✔
419
      if(Renegotiation_Extension* reneg = m_data->extensions().get<Renegotiation_Extension>()) {
12✔
420
         if(!reneg->renegotiation_info().empty()) {
×
421
            throw TLS_Exception(Alert::HandshakeFailure, "Client sent renegotiation SCSV and non-empty extension");
×
422
         }
423
      } else {
424
         // add fake extension
425
         m_data->extensions().add(new Renegotiation_Extension());
12✔
426
      }
427
   }
428
}
2,244✔
429

430
// Note: This delegates to the Client_Hello_12 constructor to take advantage
431
//       of the sanity checks there.
432
Client_Hello_12::Client_Hello_12(const std::vector<uint8_t>& buf) :
2,776✔
433
      Client_Hello_12(std::make_unique<Client_Hello_Internal>(buf)) {}
2,776✔
434

435
/*
436
* Create a new Client Hello message
437
*/
438
Client_Hello_12::Client_Hello_12(Handshake_IO& io,
2,426✔
439
                                 Handshake_Hash& hash,
440
                                 const Policy& policy,
441
                                 Callbacks& cb,
442
                                 RandomNumberGenerator& rng,
443
                                 const std::vector<uint8_t>& reneg_info,
444
                                 const Client_Hello_12::Settings& client_settings,
445
                                 const std::vector<std::string>& next_protocols) {
2,426✔
446
   m_data->m_legacy_version = client_settings.protocol_version();
2,426✔
447
   m_data->m_random = make_hello_random(rng, cb, policy);
2,426✔
448
   m_data->m_suites = policy.ciphersuite_list(client_settings.protocol_version());
2,426✔
449

450
   if(!policy.acceptable_protocol_version(m_data->legacy_version())) {
2,426✔
451
      throw Internal_Error("Offering " + m_data->legacy_version().to_string() +
×
452
                           " but our own policy does not accept it");
×
453
   }
454

455
   /*
456
   * Place all empty extensions in front to avoid a bug in some systems
457
   * which reject hellos when the last extension in the list is empty.
458
   */
459

460
   // EMS must always be used with TLS 1.2, regardless of the policy used.
461
   m_data->extensions().add(new Extended_Master_Secret);
2,426✔
462

463
   if(policy.negotiate_encrypt_then_mac()) {
2,426✔
464
      m_data->extensions().add(new Encrypt_then_MAC);
2,415✔
465
   }
466

467
   m_data->extensions().add(new Session_Ticket_Extension());
2,426✔
468

469
   m_data->extensions().add(new Renegotiation_Extension(reneg_info));
2,426✔
470

471
   m_data->extensions().add(new Supported_Versions(m_data->legacy_version(), policy));
2,426✔
472

473
   if(!client_settings.hostname().empty()) {
4,961✔
474
      m_data->extensions().add(new Server_Name_Indicator(client_settings.hostname()));
7,330✔
475
   }
476

477
   if(policy.support_cert_status_message()) {
2,426✔
478
      m_data->extensions().add(new Certificate_Status_Request({}, {}));
2,126✔
479
   }
480

481
   // RFC 7919 3.
482
   //    A client that offers a group MUST be able and willing to perform a DH
483
   //    key exchange using that group.
484
   //
485
   // We don't support hybrid key exchange in TLS 1.2
486
   const std::vector<Group_Params> kex_groups = policy.key_exchange_groups();
2,426✔
487
   std::vector<Group_Params> compatible_kex_groups;
2,426✔
488
   std::copy_if(kex_groups.begin(), kex_groups.end(), std::back_inserter(compatible_kex_groups), [](const auto group) {
31,318✔
489
      return is_ecdh(group) || is_dh(group) || is_x25519(group);
28,892✔
490
   });
491

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

494
   if(!supported_groups->ec_groups().empty()) {
4,846✔
495
      m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
2,420✔
496
   }
497
   m_data->extensions().add(supported_groups.release());
2,426✔
498

499
   m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
2,426✔
500
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
2,426✔
501
      // RFC 8446 4.2.3
502
      //    TLS 1.2 implementations SHOULD also process this extension.
503
      //    Implementations which have the same policy in both cases MAY omit
504
      //    the "signature_algorithms_cert" extension.
505
      m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
×
506
   }
×
507

508
   if(reneg_info.empty() && !next_protocols.empty()) {
2,426✔
509
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
113✔
510
   }
511

512
   if(m_data->legacy_version().is_datagram_protocol()) {
2,426✔
513
      m_data->extensions().add(new SRTP_Protection_Profiles(policy.srtp_profiles()));
702✔
514
   }
515

516
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Client, type());
2,426✔
517

518
   hash.update(io.send(*this));
4,852✔
519
}
7,274✔
520

521
/*
522
* Create a new Client Hello message (session resumption case)
523
*/
524
Client_Hello_12::Client_Hello_12(Handshake_IO& io,
217✔
525
                                 Handshake_Hash& hash,
526
                                 const Policy& policy,
527
                                 Callbacks& cb,
528
                                 RandomNumberGenerator& rng,
529
                                 const std::vector<uint8_t>& reneg_info,
530
                                 const Session_with_Handle& session,
531
                                 const std::vector<std::string>& next_protocols) {
217✔
532
   m_data->m_legacy_version = session.session.version();
217✔
533
   m_data->m_random = make_hello_random(rng, cb, policy);
217✔
534

535
   // RFC 5077 3.4
536
   //    When presenting a ticket, the client MAY generate and include a
537
   //    Session ID in the TLS ClientHello. [...] If a ticket is presented by
538
   //    the client, the server MUST NOT attempt to use the Session ID in the
539
   //    ClientHello for stateful session resumption.
540
   m_data->m_session_id = session.handle.id().value_or(Session_ID(make_hello_random(rng, cb, policy)));
471✔
541
   m_data->m_suites = policy.ciphersuite_list(m_data->legacy_version());
217✔
542

543
   if(!policy.acceptable_protocol_version(session.session.version())) {
217✔
544
      throw Internal_Error("Offering " + m_data->legacy_version().to_string() +
×
545
                           " but our own policy does not accept it");
×
546
   }
547

548
   if(!value_exists(m_data->ciphersuites(), session.session.ciphersuite_code())) {
434✔
549
      m_data->m_suites.push_back(session.session.ciphersuite_code());
6✔
550
   }
551

552
   /*
553
   * As EMS must always be used with TLS 1.2, add it even if it wasn't used
554
   * in the original session. If the server understands it and follows the
555
   * RFC it should reject our resume attempt and upgrade us to a new session
556
   * with the EMS protection.
557
   */
558
   m_data->extensions().add(new Extended_Master_Secret);
217✔
559

560
   if(session.session.supports_encrypt_then_mac()) {
217✔
561
      m_data->extensions().add(new Encrypt_then_MAC);
2✔
562
   }
563

564
   if(session.handle.is_ticket()) {
217✔
565
      m_data->extensions().add(new Session_Ticket_Extension(session.handle.ticket().value()));
540✔
566
   }
567

568
   m_data->extensions().add(new Renegotiation_Extension(reneg_info));
217✔
569

570
   m_data->extensions().add(new Server_Name_Indicator(session.session.server_info().hostname()));
651✔
571

572
   if(policy.support_cert_status_message()) {
217✔
573
      m_data->extensions().add(new Certificate_Status_Request({}, {}));
34✔
574
   }
575

576
   // RFC 7919 3.
577
   //    A client that offers a group MUST be able and willing to perform a DH
578
   //    key exchange using that group.
579
   //
580
   // We don't support hybrid key exchange in TLS 1.2
581
   const std::vector<Group_Params> kex_groups = policy.key_exchange_groups();
217✔
582
   std::vector<Group_Params> compatible_kex_groups;
217✔
583
   std::copy_if(kex_groups.begin(), kex_groups.end(), std::back_inserter(compatible_kex_groups), [](const auto group) {
2,717✔
584
      return is_ecdh(group) || is_dh(group) || is_x25519(group);
2,500✔
585
   });
586

587
   auto supported_groups = std::make_unique<Supported_Groups>(std::move(compatible_kex_groups));
217✔
588

589
   if(!supported_groups->ec_groups().empty()) {
432✔
590
      m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
215✔
591
   }
592

593
   m_data->extensions().add(supported_groups.release());
217✔
594

595
   m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
217✔
596
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
217✔
597
      // RFC 8446 4.2.3
598
      //    TLS 1.2 implementations SHOULD also process this extension.
599
      //    Implementations which have the same policy in both cases MAY omit
600
      //    the "signature_algorithms_cert" extension.
601
      m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
×
602
   }
×
603

604
   if(reneg_info.empty() && !next_protocols.empty()) {
217✔
605
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
21✔
606
   }
607

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

610
   hash.update(io.send(*this));
434✔
611
}
651✔
612

613
#if defined(BOTAN_HAS_TLS_13)
614

615
Client_Hello_13::Client_Hello_13(std::unique_ptr<Client_Hello_Internal> data) : Client_Hello(std::move(data)) {
445✔
616
   const auto& exts = m_data->extensions();
445✔
617

618
   // RFC 8446 4.1.2
619
   //    TLS 1.3 ClientHellos are identified as having a legacy_version of
620
   //    0x0303 and a "supported_versions" extension present with 0x0304 as the
621
   //    highest version indicated therein.
622
   //
623
   // Note that we already checked for "supported_versions" before entering this
624
   // c'tor in `Client_Hello_13::parse()`. This is just to be doubly sure.
625
   BOTAN_ASSERT_NOMSG(exts.has<Supported_Versions>());
445✔
626

627
   // RFC 8446 4.2.1
628
   //    Servers MAY abort the handshake upon receiving a ClientHello with
629
   //    legacy_version 0x0304 or later.
630
   if(m_data->legacy_version().is_tls_13_or_later()) {
445✔
631
      throw TLS_Exception(Alert::DecodeError, "TLS 1.3 Client Hello has invalid legacy_version");
1✔
632
   }
633

634
   // RFC 8446 4.1.2
635
   //    For every TLS 1.3 ClientHello, [the compression method] MUST contain
636
   //    exactly one byte, set to zero, [...].  If a TLS 1.3 ClientHello is
637
   //    received with any other value in this field, the server MUST abort the
638
   //    handshake with an "illegal_parameter" alert.
639
   if(m_data->comp_methods().size() != 1 || m_data->comp_methods().front() != 0) {
444✔
640
      throw TLS_Exception(Alert::IllegalParameter, "Client did not offer NULL compression");
4✔
641
   }
642

643
   // RFC 8446 4.2.9
644
   //    A client MUST provide a "psk_key_exchange_modes" extension if it
645
   //    offers a "pre_shared_key" extension. If clients offer "pre_shared_key"
646
   //    without a "psk_key_exchange_modes" extension, servers MUST abort
647
   //    the handshake.
648
   if(exts.has<PSK>()) {
440✔
649
      if(!exts.has<PSK_Key_Exchange_Modes>()) {
116✔
650
         throw TLS_Exception(Alert::MissingExtension,
1✔
651
                             "Client Hello offered a PSK without a psk_key_exchange_modes extension");
2✔
652
      }
653

654
      // RFC 8446 4.2.11
655
      //     The "pre_shared_key" extension MUST be the last extension in the
656
      //     ClientHello [...]. Servers MUST check that it is the last extension
657
      //     and otherwise fail the handshake with an "illegal_parameter" alert.
658
      if(exts.all().back()->type() != Extension_Code::PresharedKey) {
115✔
659
         throw TLS_Exception(Alert::IllegalParameter, "PSK extension was not at the very end of the Client Hello");
2✔
660
      }
661
   }
662

663
   // RFC 8446 9.2
664
   //    [A TLS 1.3 ClientHello] message MUST meet the following requirements:
665
   //
666
   //     -  If not containing a "pre_shared_key" extension, it MUST contain
667
   //        both a "signature_algorithms" extension and a "supported_groups"
668
   //        extension.
669
   //
670
   //     -  If containing a "supported_groups" extension, it MUST also contain
671
   //        a "key_share" extension, and vice versa.  An empty
672
   //        KeyShare.client_shares vector is permitted.
673
   //
674
   //    Servers receiving a ClientHello which does not conform to these
675
   //    requirements MUST abort the handshake with a "missing_extension"
676
   //    alert.
677
   if(!exts.has<PSK>()) {
437✔
678
      if(!exts.has<Supported_Groups>() || !exts.has<Signature_Algorithms>()) {
647✔
679
         throw TLS_Exception(
2✔
680
            Alert::MissingExtension,
681
            "Non-PSK Client Hello did not contain supported_groups and signature_algorithms extensions");
4✔
682
      }
683
   }
684
   if(exts.has<Supported_Groups>() != exts.has<Key_Share>()) {
435✔
685
      throw TLS_Exception(Alert::MissingExtension,
2✔
686
                          "Client Hello must either contain both key_share and supported_groups extensions or neither");
4✔
687
   }
688

689
   if(exts.has<Key_Share>()) {
433✔
690
      const auto supported_ext = exts.get<Supported_Groups>();
433✔
691
      BOTAN_ASSERT_NONNULL(supported_ext);
433✔
692
      const auto supports = supported_ext->groups();
433✔
693
      const auto offers = exts.get<Key_Share>()->offered_groups();
433✔
694

695
      // RFC 8446 4.2.8
696
      //    Each KeyShareEntry value MUST correspond to a group offered in the
697
      //    "supported_groups" extension and MUST appear in the same order.
698
      //    [...]
699
      //    Clients MUST NOT offer any KeyShareEntry values for groups not
700
      //    listed in the client's "supported_groups" extension.
701
      //
702
      // Note: We can assume that both `offers` and `supports` are unique lists
703
      //       as this is ensured in the parsing code of the extensions.
704
      auto found_in_supported_groups = [&supports, support_offset = -1](auto group) mutable {
1,679✔
705
         const auto i = std::find(supports.begin(), supports.end(), group);
1,679✔
706
         if(i == supports.end()) {
1,679✔
707
            return false;
708
         }
709

710
         const auto found_at = std::distance(supports.begin(), i);
1,679✔
711
         if(found_at <= support_offset) {
1,679✔
712
            return false;  // The order that groups appear in "key_share" and
713
                           // "supported_groups" must be the same
714
         }
715

716
         support_offset = static_cast<decltype(support_offset)>(found_at);
1,679✔
717
         return true;
1,679✔
718
      };
433✔
719

720
      for(const auto offered : offers) {
2,112✔
721
         // RFC 8446 4.2.8
722
         //    Servers MAY check for violations of these rules and abort the
723
         //    handshake with an "illegal_parameter" alert if one is violated.
724
         if(!found_in_supported_groups(offered)) {
1,679✔
725
            throw TLS_Exception(Alert::IllegalParameter,
×
726
                                "Offered key exchange groups do not align with claimed supported groups");
×
727
         }
728
      }
729
   }
866✔
730

731
   // TODO: Reject oid_filters extension if found (which is the only known extension that
732
   //       must not occur in the TLS 1.3 client hello.
733
   // RFC 8446 4.2.5
734
   //    [The oid_filters extension] MUST only be sent in the CertificateRequest message.
735
}
445✔
736

737
/*
738
* Create a new Client Hello message
739
*/
740
Client_Hello_13::Client_Hello_13(const Policy& policy,
955✔
741
                                 Callbacks& cb,
742
                                 RandomNumberGenerator& rng,
743
                                 std::string_view hostname,
744
                                 const std::vector<std::string>& next_protocols,
745
                                 std::optional<Session_with_Handle>& session) {
955✔
746
   // RFC 8446 4.1.2
747
   //    In TLS 1.3, the client indicates its version preferences in the
748
   //    "supported_versions" extension (Section 4.2.1) and the
749
   //    legacy_version field MUST be set to 0x0303, which is the version
750
   //    number for TLS 1.2.
751
   m_data->m_legacy_version = Protocol_Version::TLS_V12;
955✔
752
   m_data->m_random = make_hello_random(rng, cb, policy);
955✔
753
   m_data->m_suites = policy.ciphersuite_list(Protocol_Version::TLS_V13);
955✔
754

755
   if(policy.allow_tls12())  // Note: DTLS 1.3 is NYI, hence dtls_12 is not checked
955✔
756
   {
757
      const auto legacy_suites = policy.ciphersuite_list(Protocol_Version::TLS_V12);
933✔
758
      m_data->m_suites.insert(m_data->m_suites.end(), legacy_suites.cbegin(), legacy_suites.cend());
933✔
759
   }
933✔
760

761
   if(policy.tls_13_middlebox_compatibility_mode()) {
955✔
762
      // RFC 8446 4.1.2
763
      //    In compatibility mode (see Appendix D.4), this field MUST be non-empty,
764
      //    so a client not offering a pre-TLS 1.3 session MUST generate a new
765
      //    32-byte value.
766
      //
767
      // Note: we won't ever offer a TLS 1.2 session. In such a case we would
768
      //       have instantiated a TLS 1.2 client in the first place.
769
      m_data->m_session_id = Session_ID(make_hello_random(rng, cb, policy));
944✔
770
   }
771

772
   if(!hostname.empty()) {
955✔
773
      m_data->extensions().add(new Server_Name_Indicator(hostname));
1,870✔
774
   }
775

776
   m_data->extensions().add(new Supported_Groups(policy.key_exchange_groups()));
955✔
777

778
   m_data->extensions().add(new Key_Share(policy, cb, rng));
955✔
779

780
   m_data->extensions().add(new Supported_Versions(Protocol_Version::TLS_V13, policy));
955✔
781

782
   m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
955✔
783
   if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
955✔
784
      // RFC 8446 4.2.3
785
      //    Implementations which have the same policy in both cases MAY omit
786
      //    the "signature_algorithms_cert" extension.
787
      m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
×
788
   }
×
789

790
   // TODO: Support for PSK-only mode without a key exchange.
791
   //       This should be configurable in TLS::Policy and should allow no PSK
792
   //       support at all (e.g. to disable support for session resumption).
793
   m_data->extensions().add(new PSK_Key_Exchange_Modes({PSK_Key_Exchange_Mode::PSK_DHE_KE}));
955✔
794

795
   if(policy.support_cert_status_message()) {
955✔
796
      m_data->extensions().add(new Certificate_Status_Request({}, {}));
99✔
797
   }
798

799
   // We currently support "record_size_limit" for TLS 1.3 exclusively. Hence,
800
   // when TLS 1.2 is advertised as a supported protocol, we must not offer this
801
   // extension.
802
   if(policy.record_size_limit().has_value() && !policy.allow_tls12()) {
955✔
803
      m_data->extensions().add(new Record_Size_Limit(policy.record_size_limit().value()));
12✔
804
   }
805

806
   if(!next_protocols.empty()) {
955✔
807
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocols));
11✔
808
   }
809

810
   if(policy.allow_tls12()) {
955✔
811
      m_data->extensions().add(new Renegotiation_Extension());
933✔
812
      m_data->extensions().add(new Session_Ticket_Extension());
933✔
813

814
      // EMS must always be used with TLS 1.2, regardless of the policy
815
      m_data->extensions().add(new Extended_Master_Secret);
933✔
816

817
      if(policy.negotiate_encrypt_then_mac()) {
933✔
818
         m_data->extensions().add(new Encrypt_then_MAC);
933✔
819
      }
820

821
      if(m_data->extensions().has<Supported_Groups>() &&
1,866✔
822
         !m_data->extensions().get<Supported_Groups>()->ec_groups().empty()) {
1,866✔
823
         m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
933✔
824
      }
825
   }
826

827
   if(session.has_value()) {
955✔
828
      m_data->extensions().add(new PSK(session.value(), cb));
82✔
829
   }
830

831
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Client, type());
955✔
832

833
   if(m_data->extensions().has<PSK>()) {
955✔
834
      // RFC 8446 4.2.11
835
      //    The "pre_shared_key" extension MUST be the last extension in the
836
      //    ClientHello (this facilitates implementation [...]).
837
      if(m_data->extensions().all().back()->type() != Extension_Code::PresharedKey) {
82✔
838
         throw TLS_Exception(Alert::InternalError,
×
839
                             "Application modified extensions of Client Hello, PSK is not last anymore");
×
840
      }
841
      calculate_psk_binders({});
82✔
842
   }
843
}
955✔
844

845
std::variant<Client_Hello_13, Client_Hello_12> Client_Hello_13::parse(const std::vector<uint8_t>& buf) {
952✔
846
   auto data = std::make_unique<Client_Hello_Internal>(buf);
952✔
847
   const auto version = data->version();
926✔
848

849
   if(version.is_pre_tls_13()) {
926✔
850
      return Client_Hello_12(std::move(data));
962✔
851
   } else {
852
      return Client_Hello_13(std::move(data));
878✔
853
   }
854
}
926✔
855

856
void Client_Hello_13::retry(const Hello_Retry_Request& hrr,
34✔
857
                            const Transcript_Hash_State& transcript_hash_state,
858
                            Callbacks& cb,
859
                            RandomNumberGenerator& rng) {
860
   BOTAN_STATE_CHECK(m_data->extensions().has<Supported_Groups>());
34✔
861
   BOTAN_STATE_CHECK(m_data->extensions().has<Key_Share>());
34✔
862

863
   auto hrr_ks = hrr.extensions().get<Key_Share>();
34✔
864
   const auto& supported_groups = m_data->extensions().get<Supported_Groups>()->groups();
34✔
865

866
   if(hrr.extensions().has<Key_Share>()) {
34✔
867
      m_data->extensions().get<Key_Share>()->retry_offer(*hrr_ks, supported_groups, cb, rng);
33✔
868
   }
869

870
   // RFC 8446 4.2.2
871
   //    When sending the new ClientHello, the client MUST copy
872
   //    the contents of the extension received in the HelloRetryRequest into
873
   //    a "cookie" extension in the new ClientHello.
874
   //
875
   // RFC 8446 4.2.2
876
   //    Clients MUST NOT use cookies in their initial ClientHello in subsequent
877
   //    connections.
878
   if(hrr.extensions().has<Cookie>()) {
31✔
879
      BOTAN_STATE_CHECK(!m_data->extensions().has<Cookie>());
3✔
880
      m_data->extensions().add(new Cookie(hrr.extensions().get<Cookie>()->get_cookie()));
3✔
881
   }
882

883
   // Note: the consumer of the TLS implementation won't be able to distinguish
884
   //       invocations to this callback due to the first Client_Hello or the
885
   //       retried Client_Hello after receiving a Hello_Retry_Request. We assume
886
   //       that the user keeps and detects this state themselves.
887
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Client, type());
31✔
888

889
   auto psk = m_data->extensions().get<PSK>();
31✔
890
   if(psk) {
31✔
891
      // Cipher suite should always be a known suite as this is checked upstream
892
      const auto cipher = Ciphersuite::by_id(hrr.ciphersuite());
10✔
893
      BOTAN_ASSERT_NOMSG(cipher.has_value());
10✔
894

895
      // RFC 8446 4.1.4
896
      //    In [...] its updated ClientHello, the client SHOULD NOT offer
897
      //    any pre-shared keys associated with a hash other than that of the
898
      //    selected cipher suite.
899
      psk->filter(cipher.value());
10✔
900

901
      // RFC 8446 4.2.11.2
902
      //    If the server responds with a HelloRetryRequest and the client
903
      //    then sends ClientHello2, its binder will be computed over: [...].
904
      calculate_psk_binders(transcript_hash_state.clone());
10✔
905
   }
906
}
31✔
907

908
void Client_Hello_13::validate_updates(const Client_Hello_13& new_ch) {
30✔
909
   // RFC 8446 4.1.2
910
   //    The client will also send a ClientHello when the server has responded
911
   //    to its ClientHello with a HelloRetryRequest. In that case, the client
912
   //    MUST send the same ClientHello without modification, except as follows:
913

914
   if(m_data->session_id() != new_ch.m_data->session_id() || m_data->random() != new_ch.m_data->random() ||
30✔
915
      m_data->ciphersuites() != new_ch.m_data->ciphersuites() ||
60✔
916
      m_data->comp_methods() != new_ch.m_data->comp_methods()) {
30✔
917
      throw TLS_Exception(Alert::IllegalParameter, "Client Hello core values changed after Hello Retry Request");
×
918
   }
919

920
   const auto oldexts = extension_types();
30✔
921
   const auto newexts = new_ch.extension_types();
30✔
922

923
   // Check that extension omissions are justified
924
   for(const auto oldext : oldexts) {
421✔
925
      if(!newexts.contains(oldext)) {
784✔
926
         const auto ext = extensions().get(oldext);
1✔
927

928
         // We don't make any assumptions about unimplemented extensions.
929
         if(!ext->is_implemented()) {
1✔
930
            continue;
×
931
         }
932

933
         // RFC 8446 4.1.2
934
         //    Removing the "early_data" extension (Section 4.2.10) if one was
935
         //    present.  Early data is not permitted after a HelloRetryRequest.
936
         if(oldext == EarlyDataIndication::static_type()) {
1✔
937
            continue;
×
938
         }
939

940
         // RFC 8446 4.1.2
941
         //    Optionally adding, removing, or changing the length of the
942
         //    "padding" extension.
943
         //
944
         // TODO: implement the Padding extension
945
         // if(oldext == Padding::static_type())
946
         //    continue;
947

948
         throw TLS_Exception(Alert::IllegalParameter, "Extension removed in updated Client Hello");
1✔
949
      }
950
   }
951

952
   // Check that extension additions are justified
953
   for(const auto newext : newexts) {
413✔
954
      if(!oldexts.contains(newext)) {
768✔
955
         const auto ext = new_ch.extensions().get(newext);
2✔
956

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

962
         // RFC 8446 4.1.2
963
         //    Including a "cookie" extension if one was provided in the
964
         //    HelloRetryRequest.
965
         if(newext == Cookie::static_type()) {
1✔
966
            continue;
1✔
967
         }
968

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

977
         throw TLS_Exception(Alert::UnsupportedExtension, "Added an extension in updated Client Hello");
×
978
      }
979
   }
980

981
   // RFC 8446 4.1.2
982
   //    Removing the "early_data" extension (Section 4.2.10) if one was
983
   //    present.  Early data is not permitted after a HelloRetryRequest.
984
   if(new_ch.extensions().has<EarlyDataIndication>()) {
29✔
985
      throw TLS_Exception(Alert::IllegalParameter, "Updated Client Hello indicates early data");
×
986
   }
987

988
   // TODO: Contents of extensions are not checked for update compatibility, see:
989
   //
990
   // RFC 8446 4.1.2
991
   //    If a "key_share" extension was supplied in the HelloRetryRequest,
992
   //    replacing the list of shares with a list containing a single
993
   //    KeyShareEntry from the indicated group.
994
   //
995
   //    Updating the "pre_shared_key" extension if present by recomputing
996
   //    the "obfuscated_ticket_age" and binder values and (optionally)
997
   //    removing any PSKs which are incompatible with the server's
998
   //    indicated cipher suite.
999
   //
1000
   //    Optionally adding, removing, or changing the length of the
1001
   //    "padding" extension.
1002
}
30✔
1003

1004
void Client_Hello_13::calculate_psk_binders(Transcript_Hash_State ths) {
92✔
1005
   auto psk = m_data->extensions().get<PSK>();
92✔
1006
   if(!psk || psk->empty()) {
92✔
1007
      return;
1✔
1008
   }
1009

1010
   // RFC 8446 4.2.11.2
1011
   //    Each entry in the binders list is computed as an HMAC over a
1012
   //    transcript hash (see Section 4.4.1) containing a partial ClientHello
1013
   //    [...].
1014
   //
1015
   // Therefore we marshal the entire message prematurely to obtain the
1016
   // (truncated) transcript hash, calculate the PSK binders with it, update
1017
   // the Client Hello thus finalizing the message. Down the road, it will be
1018
   // re-marshalled with the correct binders and sent over the wire.
1019
   Handshake_Layer::prepare_message(*this, ths);
91✔
1020
   psk->calculate_binders(ths);
91✔
1021
}
1022

1023
std::optional<Protocol_Version> Client_Hello_13::highest_supported_version(const Policy& policy) const {
387✔
1024
   // RFC 8446 4.2.1
1025
   //    The "supported_versions" extension is used by the client to indicate
1026
   //    which versions of TLS it supports and by the server to indicate which
1027
   //    version it is using. The extension contains a list of supported
1028
   //    versions in preference order, with the most preferred version first.
1029
   const auto supvers = m_data->extensions().get<Supported_Versions>();
387✔
1030
   BOTAN_ASSERT_NONNULL(supvers);
387✔
1031

1032
   std::optional<Protocol_Version> result;
387✔
1033

1034
   for(const auto& v : supvers->versions()) {
1,879✔
1035
      // RFC 8446 4.2.1
1036
      //    Servers MUST only select a version of TLS present in that extension
1037
      //    and MUST ignore any unknown versions that are present in that
1038
      //    extension.
1039
      if(!v.known_version() || !policy.acceptable_protocol_version(v)) {
1,492✔
1040
         continue;
830✔
1041
      }
1042

1043
      result = (result.has_value()) ? std::optional(std::max(result.value(), v)) : std::optional(v);
937✔
1044
   }
1045

1046
   return result;
387✔
1047
}
1048

1049
#endif  // BOTAN_HAS_TLS_13
1050

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

© 2025 Coveralls, Inc