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

randombit / botan / 16100769786

06 Jul 2025 02:16PM UTC coverage: 90.577% (+0.004%) from 90.573%
16100769786

push

github

web-flow
Merge pull request #4961 from randombit/jack/fix-clang-tidy-readability-implicit-bool-conversion

Fix some clang-tidy readability-implicit-bool-conversion warnings

99055 of 109360 relevant lines covered (90.58%)

12330806.47 hits per line

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

94.44
/src/lib/tls/msg_server_hello.cpp
1
/*
2
* TLS Server Hello and Server Hello Done
3
* (C) 2004-2011,2015,2016,2019 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/mem_ops.h>
15
#include <botan/tls_callbacks.h>
16
#include <botan/tls_exceptn.h>
17
#include <botan/tls_extensions.h>
18
#include <botan/tls_session_manager.h>
19
#include <botan/internal/ct_utils.h>
20
#include <botan/internal/stl_util.h>
21
#include <botan/internal/tls_handshake_hash.h>
22
#include <botan/internal/tls_handshake_io.h>
23
#include <botan/internal/tls_reader.h>
24
#include <botan/internal/tls_session_key.h>
25

26
namespace Botan::TLS {
27

28
namespace {
29

30
const uint64_t DOWNGRADE_TLS11 = 0x444F574E47524400;
31
const uint64_t DOWNGRADE_TLS12 = 0x444F574E47524401;
32

33
// SHA-256("HelloRetryRequest")
34
const std::vector<uint8_t> HELLO_RETRY_REQUEST_MARKER = {
35
   0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
36
   0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C};
37

38
bool random_signals_hello_retry_request(const std::vector<uint8_t>& random) {
3,560✔
39
   return CT::is_equal(random.data(), HELLO_RETRY_REQUEST_MARKER.data(), HELLO_RETRY_REQUEST_MARKER.size()).as_bool();
3,560✔
40
}
41

42
std::vector<uint8_t> make_server_hello_random(RandomNumberGenerator& rng,
1,116✔
43
                                              Protocol_Version offered_version,
44
                                              Callbacks& cb,
45
                                              const Policy& policy) {
46
   BOTAN_UNUSED(offered_version);
1,116✔
47
   auto random = make_hello_random(rng, cb, policy);
1,116✔
48

49
   // RFC 8446 4.1.3
50
   //    TLS 1.3 has a downgrade protection mechanism embedded in the server's
51
   //    random value. TLS 1.3 servers which negotiate TLS 1.2 or below in
52
   //    response to a ClientHello MUST set the last 8 bytes of their Random
53
   //    value specially in their ServerHello.
54
   //
55
   //    If negotiating TLS 1.2, TLS 1.3 servers MUST set the last 8 bytes of
56
   //    their Random value to the bytes: [DOWNGRADE_TLS12]
57
   if(offered_version.is_pre_tls_13() && policy.allow_tls13()) {
1,116✔
58
      constexpr size_t downgrade_signal_length = sizeof(DOWNGRADE_TLS12);
477✔
59
      BOTAN_ASSERT_NOMSG(random.size() >= downgrade_signal_length);
477✔
60
      auto lastbytes = random.data() + random.size() - downgrade_signal_length;
477✔
61
      store_be(DOWNGRADE_TLS12, lastbytes);
477✔
62
   }
63

64
   return random;
1,116✔
65
}
×
66

67
}  // namespace
68

69
/**
70
* Version-agnostic internal server hello data container that allows
71
* parsing Server_Hello messages without prior knowledge of the contained
72
* protocol version.
73
*/
74
class Server_Hello_Internal {
75
   public:
76
      /**
77
       * Deserialize a Server Hello message
78
       */
79
      explicit Server_Hello_Internal(const std::vector<uint8_t>& buf) {
3,566✔
80
         if(buf.size() < 38) {
3,566✔
81
            throw Decoding_Error("Server_Hello: Packet corrupted");
6✔
82
         }
83

84
         TLS_Data_Reader reader("ServerHello", buf);
3,560✔
85

86
         const uint8_t major_version = reader.get_byte();
3,560✔
87
         const uint8_t minor_version = reader.get_byte();
3,560✔
88

89
         m_legacy_version = Protocol_Version(major_version, minor_version);
3,560✔
90

91
         // RFC 8446 4.1.3
92
         //    Upon receiving a message with type server_hello, implementations MUST
93
         //    first examine the Random value and, if it matches this value, process
94
         //    it as described in Section 4.1.4 [Hello Retry Request]).
95
         m_random = reader.get_fixed<uint8_t>(32);
3,560✔
96
         m_is_hello_retry_request = random_signals_hello_retry_request(m_random);
3,560✔
97

98
         m_session_id = Session_ID(reader.get_range<uint8_t>(1, 0, 32));
3,560✔
99
         m_ciphersuite = reader.get_uint16_t();
3,551✔
100
         m_comp_method = reader.get_byte();
3,549✔
101

102
         // Note that this code path might parse a TLS 1.2 (or older) server hello message that
103
         // is nevertheless marked as being a 'hello retry request' (potentially maliciously).
104
         // Extension parsing will however not be affected by the associated flag.
105
         // Only after parsing the extensions will the upstream code be able to decide
106
         // whether we're dealing with TLS 1.3 or older.
107
         m_extensions.deserialize(
3,548✔
108
            reader,
109
            Connection_Side::Server,
110
            m_is_hello_retry_request ? Handshake_Type::HelloRetryRequest : Handshake_Type::ServerHello);
3,548✔
111
      }
3,870✔
112

113
      Server_Hello_Internal(Protocol_Version lv,
1,397✔
114
                            Session_ID sid,
115
                            std::vector<uint8_t> r,
116
                            const uint16_t cs,
117
                            const uint8_t cm,
118
                            bool is_hrr = false) :
1,397✔
119
            m_legacy_version(lv),
1,397✔
120
            m_session_id(std::move(sid)),
1,397✔
121
            m_random(std::move(r)),
1,397✔
122
            m_is_hello_retry_request(is_hrr),
1,397✔
123
            m_ciphersuite(cs),
1,397✔
124
            m_comp_method(cm) {}
1,397✔
125

126
      Protocol_Version version() const {
4,446✔
127
         // RFC 8446 4.2.1
128
         //    A server which negotiates a version of TLS prior to TLS 1.3 MUST set
129
         //    ServerHello.version and MUST NOT send the "supported_versions"
130
         //    extension.  A server which negotiates TLS 1.3 MUST respond by sending
131
         //    a "supported_versions" extension containing the selected version
132
         //    value (0x0304).
133
         //
134
         // Note: Here we just take a message parsing decision, further validation of
135
         //       the extension's contents is done later.
136
         return (extensions().has<Supported_Versions>()) ? Protocol_Version::TLS_V13 : m_legacy_version;
4,446✔
137
      }
138

139
      Protocol_Version legacy_version() const { return m_legacy_version; }
2,127✔
140

141
      const Session_ID& session_id() const { return m_session_id; }
6,896✔
142

143
      const std::vector<uint8_t>& random() const { return m_random; }
2,007✔
144

145
      uint16_t ciphersuite() const { return m_ciphersuite; }
2,224✔
146

147
      uint8_t comp_method() const { return m_comp_method; }
1,394✔
148

149
      bool is_hello_retry_request() const { return m_is_hello_retry_request; }
1,076✔
150

151
      const Extensions& extensions() const { return m_extensions; }
4,446✔
152

153
      Extensions& extensions() { return m_extensions; }
39,456✔
154

155
   private:
156
      Protocol_Version m_legacy_version;
157
      Session_ID m_session_id;
158
      std::vector<uint8_t> m_random;
159
      bool m_is_hello_retry_request;
160
      uint16_t m_ciphersuite;
161
      uint8_t m_comp_method;
162

163
      Extensions m_extensions;
164
};
165

166
Server_Hello::Server_Hello(std::unique_ptr<Server_Hello_Internal> data) : m_data(std::move(data)) {}
4,808✔
167

168
Server_Hello::Server_Hello(Server_Hello&&) noexcept = default;
11,596✔
169
Server_Hello& Server_Hello::operator=(Server_Hello&&) noexcept = default;
1✔
170

171
Server_Hello::~Server_Hello() = default;
16,391✔
172

173
/*
174
* Serialize a Server Hello message
175
*/
176
std::vector<uint8_t> Server_Hello::serialize() const {
1,394✔
177
   std::vector<uint8_t> buf;
1,394✔
178
   buf.reserve(1024);  // working around GCC warning
1,394✔
179

180
   buf.push_back(m_data->legacy_version().major_version());
1,394✔
181
   buf.push_back(m_data->legacy_version().minor_version());
1,394✔
182
   buf += m_data->random();
1,394✔
183

184
   append_tls_length_value(buf, m_data->session_id().get(), 1);
1,394✔
185

186
   buf.push_back(get_byte<0>(m_data->ciphersuite()));
1,394✔
187
   buf.push_back(get_byte<1>(m_data->ciphersuite()));
1,394✔
188

189
   buf.push_back(m_data->comp_method());
1,394✔
190

191
   buf += m_data->extensions().serialize(Connection_Side::Server);
1,394✔
192

193
   return buf;
1,394✔
194
}
×
195

196
Handshake_Type Server_Hello::type() const {
7,373✔
197
   return Handshake_Type::ServerHello;
7,373✔
198
}
199

200
Protocol_Version Server_Hello::legacy_version() const {
13,944✔
201
   return m_data->legacy_version();
13,944✔
202
}
203

204
const std::vector<uint8_t>& Server_Hello::random() const {
3,814✔
205
   return m_data->random();
3,814✔
206
}
207

208
uint8_t Server_Hello::compression_method() const {
6,497✔
209
   return m_data->comp_method();
6,497✔
210
}
211

212
const Session_ID& Server_Hello::session_id() const {
6,896✔
213
   return m_data->session_id();
6,896✔
214
}
215

216
uint16_t Server_Hello::ciphersuite() const {
14,923✔
217
   return m_data->ciphersuite();
14,923✔
218
}
219

220
std::set<Extension_Code> Server_Hello::extension_types() const {
2,316✔
221
   return m_data->extensions().extension_types();
2,316✔
222
}
223

224
const Extensions& Server_Hello::extensions() const {
8,174✔
225
   return m_data->extensions();
8,174✔
226
}
227

228
// New session case
229
Server_Hello_12::Server_Hello_12(Handshake_IO& io,
733✔
230
                                 Handshake_Hash& hash,
231
                                 const Policy& policy,
232
                                 Callbacks& cb,
233
                                 RandomNumberGenerator& rng,
234
                                 const std::vector<uint8_t>& reneg_info,
235
                                 const Client_Hello_12& client_hello,
236
                                 const Server_Hello_12::Settings& server_settings,
237
                                 std::string_view next_protocol) :
733✔
238
      Server_Hello(std::make_unique<Server_Hello_Internal>(
733✔
239
         server_settings.protocol_version(),
1,466✔
240
         server_settings.session_id(),
733✔
241
         make_server_hello_random(rng, server_settings.protocol_version(), cb, policy),
733✔
242
         server_settings.ciphersuite(),
733✔
243
         uint8_t(0))) {
1,466✔
244
   if(client_hello.supports_extended_master_secret()) {
733✔
245
      m_data->extensions().add(new Extended_Master_Secret);
728✔
246
   }
247

248
   // Sending the extension back does not commit us to sending a stapled response
249
   if(client_hello.supports_cert_status_message() && policy.support_cert_status_message()) {
733✔
250
      m_data->extensions().add(new Certificate_Status_Request);
199✔
251
   }
252

253
   if(!next_protocol.empty() && client_hello.supports_alpn()) {
733✔
254
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocol));
119✔
255
   }
256

257
   const auto c = Ciphersuite::by_id(m_data->ciphersuite());
733✔
258

259
   if(c && c->cbc_ciphersuite() && client_hello.supports_encrypt_then_mac() && policy.negotiate_encrypt_then_mac()) {
733✔
260
      m_data->extensions().add(new Encrypt_then_MAC);
14✔
261
   }
262

263
   if(c && c->ecc_ciphersuite() && client_hello.extension_types().contains(Extension_Code::EcPointFormats)) {
2,099✔
264
      m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
632✔
265
   }
266

267
   if(client_hello.secure_renegotiation()) {
733✔
268
      m_data->extensions().add(new Renegotiation_Extension(reneg_info));
730✔
269
   }
270

271
   if(client_hello.supports_session_ticket() && server_settings.offer_session_ticket()) {
733✔
272
      m_data->extensions().add(new Session_Ticket_Extension());
543✔
273
   }
274

275
   if(m_data->legacy_version().is_datagram_protocol()) {
733✔
276
      const std::vector<uint16_t> server_srtp = policy.srtp_profiles();
288✔
277
      const std::vector<uint16_t> client_srtp = client_hello.srtp_profiles();
288✔
278

279
      if(!server_srtp.empty() && !client_srtp.empty()) {
288✔
280
         uint16_t shared = 0;
281
         // always using server preferences for now
282
         for(auto s_srtp : server_srtp) {
6✔
283
            for(auto c_srtp : client_srtp) {
16✔
284
               if(shared == 0 && s_srtp == c_srtp) {
12✔
285
                  shared = s_srtp;
1✔
286
               }
287
            }
288
         }
289

290
         if(shared != 0) {
2✔
291
            m_data->extensions().add(new SRTP_Protection_Profiles(shared));
1✔
292
         }
293
      }
294
   }
290✔
295

296
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Server, type());
733✔
297

298
   hash.update(io.send(*this));
1,466✔
299
}
733✔
300

301
// Resuming
302
Server_Hello_12::Server_Hello_12(Handshake_IO& io,
232✔
303
                                 Handshake_Hash& hash,
304
                                 const Policy& policy,
305
                                 Callbacks& cb,
306
                                 RandomNumberGenerator& rng,
307
                                 const std::vector<uint8_t>& reneg_info,
308
                                 const Client_Hello_12& client_hello,
309
                                 const Session& resumed_session,
310
                                 bool offer_session_ticket,
311
                                 std::string_view next_protocol) :
232✔
312
      Server_Hello(std::make_unique<Server_Hello_Internal>(resumed_session.version(),
464✔
313
                                                           client_hello.session_id(),
232✔
314
                                                           make_hello_random(rng, cb, policy),
232✔
315
                                                           resumed_session.ciphersuite_code(),
464✔
316
                                                           uint8_t(0))) {
464✔
317
   if(client_hello.supports_extended_master_secret()) {
232✔
318
      m_data->extensions().add(new Extended_Master_Secret);
231✔
319
   }
320

321
   if(!next_protocol.empty() && client_hello.supports_alpn()) {
232✔
322
      m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocol));
17✔
323
   }
324

325
   if(client_hello.supports_encrypt_then_mac() && policy.negotiate_encrypt_then_mac()) {
232✔
326
      Ciphersuite c = resumed_session.ciphersuite();
2✔
327
      if(c.cbc_ciphersuite()) {
2✔
328
         m_data->extensions().add(new Encrypt_then_MAC);
2✔
329
      }
330
   }
331

332
   if(resumed_session.ciphersuite().ecc_ciphersuite() &&
232✔
333
      client_hello.extension_types().contains(Extension_Code::EcPointFormats)) {
650✔
334
      m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
209✔
335
   }
336

337
   if(client_hello.secure_renegotiation()) {
232✔
338
      m_data->extensions().add(new Renegotiation_Extension(reneg_info));
232✔
339
   }
340

341
   if(client_hello.supports_session_ticket() && offer_session_ticket) {
232✔
342
      m_data->extensions().add(new Session_Ticket_Extension());
1✔
343
   }
344

345
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Server, type());
232✔
346

347
   hash.update(io.send(*this));
464✔
348
}
232✔
349

350
Server_Hello_12::Server_Hello_12(const std::vector<uint8_t>& buf) :
2,519✔
351
      Server_Hello_12(std::make_unique<Server_Hello_Internal>(buf)) {}
2,519✔
352

353
Server_Hello_12::Server_Hello_12(std::unique_ptr<Server_Hello_Internal> data) : Server_Hello(std::move(data)) {
2,873✔
354
   if(!m_data->version().is_pre_tls_13()) {
5,746✔
355
      throw TLS_Exception(Alert::ProtocolVersion, "Expected server hello of (D)TLS 1.2 or lower");
×
356
   }
357
}
2,873✔
358

359
Protocol_Version Server_Hello_12::selected_version() const {
490✔
360
   return legacy_version();
490✔
361
}
362

363
bool Server_Hello_12::secure_renegotiation() const {
3,200✔
364
   return m_data->extensions().has<Renegotiation_Extension>();
3,200✔
365
}
366

367
std::vector<uint8_t> Server_Hello_12::renegotiation_info() const {
3,107✔
368
   if(Renegotiation_Extension* reneg = m_data->extensions().get<Renegotiation_Extension>()) {
3,107✔
369
      return reneg->renegotiation_info();
3,107✔
370
   }
371
   return std::vector<uint8_t>();
×
372
}
373

374
bool Server_Hello_12::supports_extended_master_secret() const {
3,944✔
375
   return m_data->extensions().has<Extended_Master_Secret>();
3,944✔
376
}
377

378
bool Server_Hello_12::supports_encrypt_then_mac() const {
6,866✔
379
   return m_data->extensions().has<Encrypt_then_MAC>();
6,866✔
380
}
381

382
bool Server_Hello_12::supports_certificate_status_message() const {
1,923✔
383
   return m_data->extensions().has<Certificate_Status_Request>();
1,923✔
384
}
385

386
bool Server_Hello_12::supports_session_ticket() const {
2,607✔
387
   return m_data->extensions().has<Session_Ticket_Extension>();
2,607✔
388
}
389

390
uint16_t Server_Hello_12::srtp_profile() const {
3,708✔
391
   if(auto srtp = m_data->extensions().get<SRTP_Protection_Profiles>()) {
3,708✔
392
      auto prof = srtp->profiles();
4✔
393
      if(prof.size() != 1 || prof[0] == 0) {
4✔
394
         throw Decoding_Error("Server sent malformed DTLS-SRTP extension");
×
395
      }
396
      return prof[0];
4✔
397
   }
4✔
398

399
   return 0;
400
}
401

402
std::string Server_Hello_12::next_protocol() const {
2,160✔
403
   if(auto alpn = m_data->extensions().get<Application_Layer_Protocol_Notification>()) {
2,160✔
404
      return alpn->single_protocol();
137✔
405
   }
406
   return "";
2,023✔
407
}
408

409
bool Server_Hello_12::prefers_compressed_ec_points() const {
13✔
410
   if(auto ecc_formats = m_data->extensions().get<Supported_Point_Formats>()) {
13✔
411
      return ecc_formats->prefers_compressed();
10✔
412
   }
413
   return false;
414
}
415

416
std::optional<Protocol_Version> Server_Hello_12::random_signals_downgrade() const {
613✔
417
   const uint64_t last8 = load_be<uint64_t>(m_data->random().data(), 3);
613✔
418
   if(last8 == DOWNGRADE_TLS11) {
613✔
419
      return Protocol_Version::TLS_V11;
×
420
   }
421
   if(last8 == DOWNGRADE_TLS12) {
613✔
422
      return Protocol_Version::TLS_V12;
1✔
423
   }
424

425
   return std::nullopt;
612✔
426
}
427

428
/*
429
* Create a new Server Hello Done message
430
*/
431
Server_Hello_Done::Server_Hello_Done(Handshake_IO& io, Handshake_Hash& hash) {
708✔
432
   hash.update(io.send(*this));
1,416✔
433
}
708✔
434

435
/*
436
* Deserialize a Server Hello Done message
437
*/
438
Server_Hello_Done::Server_Hello_Done(const std::vector<uint8_t>& buf) {
854✔
439
   if(!buf.empty()) {
854✔
440
      throw Decoding_Error("Server_Hello_Done: Must be empty, and is not");
2✔
441
   }
442
}
852✔
443

444
/*
445
* Serialize a Server Hello Done message
446
*/
447
std::vector<uint8_t> Server_Hello_Done::serialize() const {
708✔
448
   return std::vector<uint8_t>();
708✔
449
}
450

451
#if defined(BOTAN_HAS_TLS_13)
452

453
const Server_Hello_13::Server_Hello_Tag Server_Hello_13::as_server_hello;
454
const Server_Hello_13::Hello_Retry_Request_Tag Server_Hello_13::as_hello_retry_request;
455
const Server_Hello_13::Hello_Retry_Request_Creation_Tag Server_Hello_13::as_new_hello_retry_request;
456

457
std::variant<Hello_Retry_Request, Server_Hello_13> Server_Hello_13::create(const Client_Hello_13& ch,
432✔
458
                                                                           bool hello_retry_request_allowed,
459
                                                                           Session_Manager& session_mgr,
460
                                                                           Credentials_Manager& credentials_mgr,
461
                                                                           RandomNumberGenerator& rng,
462
                                                                           const Policy& policy,
463
                                                                           Callbacks& cb) {
464
   const auto& exts = ch.extensions();
432✔
465

466
   // RFC 8446 4.2.9
467
   //    [With PSK with (EC)DHE key establishment], the client and server MUST
468
   //    supply "key_share" values [...].
469
   //
470
   // Note: We currently do not support PSK without (EC)DHE, hence, we can
471
   //       assume that those extensions are available.
472
   BOTAN_ASSERT_NOMSG(exts.has<Supported_Groups>() && exts.has<Key_Share>());
864✔
473
   const auto& supported_by_client = exts.get<Supported_Groups>()->groups();
432✔
474
   const auto& offered_by_client = exts.get<Key_Share>()->offered_groups();
432✔
475
   const auto selected_group = policy.choose_key_exchange_group(supported_by_client, offered_by_client);
432✔
476

477
   // RFC 8446 4.1.1
478
   //    If there is no overlap between the received "supported_groups" and the
479
   //    groups supported by the server, then the server MUST abort the
480
   //    handshake with a "handshake_failure" or an "insufficient_security" alert.
481
   if(selected_group == Named_Group::NONE) {
432✔
482
      throw TLS_Exception(Alert::HandshakeFailure, "Client did not offer any acceptable group");
×
483
   }
484

485
   // RFC 8446 4.2.8:
486
   //    Servers MUST NOT send a KeyShareEntry for any group not indicated in the
487
   //    client's "supported_groups" extension [...]
488
   if(!value_exists(supported_by_client, selected_group)) {
432✔
489
      throw TLS_Exception(Alert::InternalError, "Application selected a group that is not supported by the client");
×
490
   }
491

492
   // RFC 8446 4.1.4
493
   //    The server will send this message in response to a ClientHello
494
   //    message if it is able to find an acceptable set of parameters but the
495
   //    ClientHello does not contain sufficient information to proceed with
496
   //    the handshake.
497
   //
498
   // In this case, the Client Hello did not contain a key share offer for
499
   // the group selected by the application.
500
   if(!value_exists(offered_by_client, selected_group)) {
432✔
501
      // RFC 8446 4.1.4
502
      //    If a client receives a second HelloRetryRequest in the same
503
      //    connection (i.e., where the ClientHello was itself in response to a
504
      //    HelloRetryRequest), it MUST abort the handshake with an
505
      //    "unexpected_message" alert.
506
      BOTAN_STATE_CHECK(hello_retry_request_allowed);
49✔
507
      return Hello_Retry_Request(ch, selected_group, policy, cb);
98✔
508
   } else {
509
      return Server_Hello_13(ch, selected_group, session_mgr, credentials_mgr, rng, cb, policy);
750✔
510
   }
511
}
416✔
512

513
std::variant<Hello_Retry_Request, Server_Hello_13, Server_Hello_12> Server_Hello_13::parse(
1,047✔
514
   const std::vector<uint8_t>& buf) {
515
   auto data = std::make_unique<Server_Hello_Internal>(buf);
1,047✔
516
   const auto version = data->version();
1,035✔
517

518
   // server hello that appears to be pre-TLS 1.3, takes precedence over...
519
   if(version.is_pre_tls_13()) {
1,035✔
520
      return Server_Hello_12(std::move(data));
994✔
521
   }
522

523
   // ... the TLS 1.3 "special case" aka. Hello_Retry_Request
524
   if(version == Protocol_Version::TLS_V13) {
538✔
525
      if(data->is_hello_retry_request()) {
538✔
526
         return Hello_Retry_Request(std::move(data));
135✔
527
      }
528

529
      return Server_Hello_13(std::move(data));
930✔
530
   }
531

532
   throw TLS_Exception(Alert::ProtocolVersion, "unexpected server hello version: " + version.to_string());
×
533
}
1,035✔
534

535
/**
536
 * Validation that applies to both Server Hello and Hello Retry Request
537
 */
538
void Server_Hello_13::basic_validation() const {
538✔
539
   BOTAN_ASSERT_NOMSG(m_data->version() == Protocol_Version::TLS_V13);
1,076✔
540

541
   // Note: checks that cannot be performed without contextual information
542
   //       are done in the specific TLS client implementation.
543
   // Note: The Supported_Version extension makes sure internally that
544
   //       exactly one entry is provided.
545

546
   // Note: Hello Retry Request basic validation is equivalent with the
547
   //       basic validations required for Server Hello
548
   //
549
   // RFC 8446 4.1.4
550
   //    Upon receipt of a HelloRetryRequest, the client MUST check the
551
   //    legacy_version, [...], and legacy_compression_method as specified in
552
   //    Section 4.1.3 and then process the extensions, starting with determining
553
   //    the version using "supported_versions".
554

555
   // RFC 8446 4.1.3
556
   //    In TLS 1.3, [...] the legacy_version field MUST be set to 0x0303
557
   if(legacy_version() != Protocol_Version::TLS_V12) {
538✔
558
      throw TLS_Exception(Alert::ProtocolVersion,
2✔
559
                          "legacy_version '" + legacy_version().to_string() + "' is not allowed");
6✔
560
   }
561

562
   // RFC 8446 4.1.3
563
   //    legacy_compression_method:  A single byte which MUST have the value 0.
564
   if(compression_method() != 0x00) {
536✔
565
      throw TLS_Exception(Alert::DecodeError, "compression is not supported in TLS 1.3");
2✔
566
   }
567

568
   // RFC 8446 4.1.3
569
   //    All TLS 1.3 ServerHello messages MUST contain the "supported_versions" extension.
570
   if(!extensions().has<Supported_Versions>()) {
534✔
571
      throw TLS_Exception(Alert::MissingExtension, "server hello did not contain 'supported version' extension");
×
572
   }
573

574
   // RFC 8446 4.2.1
575
   //    A server which negotiates TLS 1.3 MUST respond by sending
576
   //    a "supported_versions" extension containing the selected version
577
   //    value (0x0304).
578
   if(selected_version() != Protocol_Version::TLS_V13) {
534✔
579
      throw TLS_Exception(Alert::IllegalParameter, "TLS 1.3 Server Hello selected a different version");
1✔
580
   }
581
}
533✔
582

583
Server_Hello_13::Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Server_Hello_13::Server_Hello_Tag) :
469✔
584
      Server_Hello(std::move(data)) {
469✔
585
   BOTAN_ASSERT_NOMSG(!m_data->is_hello_retry_request());
469✔
586
   basic_validation();
469✔
587

588
   const auto& exts = extensions();
465✔
589

590
   // RFC 8446 4.1.3
591
   //    The ServerHello MUST only include extensions which are required to
592
   //    establish the cryptographic context and negotiate the protocol version.
593
   //    [...]
594
   //    Other extensions (see Section 4.2) are sent separately in the
595
   //    EncryptedExtensions message.
596
   //
597
   // Note that further validation dependent on the client hello is done in the
598
   // TLS client implementation.
599
   const std::set<Extension_Code> allowed = {
465✔
600
      Extension_Code::KeyShare,
601
      Extension_Code::SupportedVersions,
602
      Extension_Code::PresharedKey,
603
   };
465✔
604

605
   // As the ServerHello shall only contain essential extensions, we don't give
606
   // any slack for extensions not implemented by Botan here.
607
   if(exts.contains_other_than(allowed)) {
465✔
608
      throw TLS_Exception(Alert::UnsupportedExtension, "Server Hello contained an extension that is not allowed");
2✔
609
   }
610

611
   // RFC 8446 4.1.3
612
   //    Current ServerHello messages additionally contain
613
   //    either the "pre_shared_key" extension or the "key_share"
614
   //    extension, or both [...].
615
   if(!exts.has<Key_Share>() && !exts.has<PSK_Key_Exchange_Modes>()) {
465✔
616
      throw TLS_Exception(Alert::MissingExtension, "server hello must contain key exchange information");
2✔
617
   }
618
}
469✔
619

620
Server_Hello_13::Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data,
69✔
621
                                 Server_Hello_13::Hello_Retry_Request_Tag) :
69✔
622
      Server_Hello(std::move(data)) {
69✔
623
   BOTAN_ASSERT_NOMSG(m_data->is_hello_retry_request());
69✔
624
   basic_validation();
69✔
625

626
   const auto& exts = extensions();
68✔
627

628
   // RFC 8446 4.1.4
629
   //     The HelloRetryRequest extensions defined in this specification are:
630
   //     -  supported_versions (see Section 4.2.1)
631
   //     -  cookie (see Section 4.2.2)
632
   //     -  key_share (see Section 4.2.8)
633
   const std::set<Extension_Code> allowed = {
68✔
634
      Extension_Code::Cookie,
635
      Extension_Code::SupportedVersions,
636
      Extension_Code::KeyShare,
637
   };
68✔
638

639
   // As the Hello Retry Request shall only contain essential extensions, we
640
   // don't give any slack for extensions not implemented by Botan here.
641
   if(exts.contains_other_than(allowed)) {
68✔
642
      throw TLS_Exception(Alert::UnsupportedExtension,
1✔
643
                          "Hello Retry Request contained an extension that is not allowed");
1✔
644
   }
645

646
   // RFC 8446 4.1.4
647
   //    Clients MUST abort the handshake with an "illegal_parameter" alert if
648
   //    the HelloRetryRequest would not result in any change in the ClientHello.
649
   if(!exts.has<Key_Share>() && !exts.has<Cookie>()) {
69✔
650
      throw TLS_Exception(Alert::IllegalParameter, "Hello Retry Request does not request any changes to Client Hello");
1✔
651
   }
652
}
69✔
653

654
Server_Hello_13::Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data, Hello_Retry_Request_Creation_Tag) :
49✔
655
      Server_Hello(std::move(data)) {}
49✔
656

657
namespace {
658

659
uint16_t choose_ciphersuite(const Client_Hello_13& ch, const Policy& policy) {
432✔
660
   auto pref_list = ch.ciphersuites();
432✔
661
   // TODO: DTLS might need to make this version dynamic
662
   auto other_list = policy.ciphersuite_list(Protocol_Version::TLS_V13);
432✔
663

664
   if(policy.server_uses_own_ciphersuite_preferences()) {
432✔
665
      std::swap(pref_list, other_list);
432✔
666
   }
667

668
   for(auto suite_id : pref_list) {
1,263✔
669
      // TODO: take potentially available PSKs into account to select a
670
      //       compatible ciphersuite.
671
      //
672
      // Assuming the client sent one or more PSKs, we would first need to find
673
      // the hash functions they are associated to. For session tickets, that
674
      // would mean decrypting the ticket and comparing the cipher suite used in
675
      // those tickets. For (currently not yet supported) pre-assigned PSKs, the
676
      // hash function needs to be specified along with them.
677
      //
678
      // Then we could refine the ciphersuite selection using the required hash
679
      // function for the PSK(s) we are wishing to use down the road.
680
      //
681
      // For now, we just negotiate the cipher suite blindly and hope for the
682
      // best. As long as PSKs are used for session resumption only, this has a
683
      // high chance of success. Previous handshakes with this client have very
684
      // likely selected the same ciphersuite anyway.
685
      //
686
      // See also RFC 8446 4.2.11
687
      //    When session resumption is the primary use case of PSKs, the most
688
      //    straightforward way to implement the PSK/cipher suite matching
689
      //    requirements is to negotiate the cipher suite first [...].
690
      if(value_exists(other_list, suite_id)) {
2,526✔
691
         return suite_id;
432✔
692
      }
693
   }
694

695
   // RFC 8446 4.1.1
696
   //     If the server is unable to negotiate a supported set of parameters
697
   //     [...], it MUST abort the handshake with either a "handshake_failure"
698
   //     or "insufficient_security" fatal alert [...].
699
   throw TLS_Exception(Alert::HandshakeFailure, "Can't agree on a ciphersuite with client");
×
700
}
864✔
701
}  // namespace
702

703
Server_Hello_13::Server_Hello_13(const Client_Hello_13& ch,
383✔
704
                                 std::optional<Named_Group> key_exchange_group,
705
                                 Session_Manager& session_mgr,
706
                                 Credentials_Manager& credentials_mgr,
707
                                 RandomNumberGenerator& rng,
708
                                 Callbacks& cb,
709
                                 const Policy& policy) :
383✔
710
      Server_Hello(std::make_unique<Server_Hello_Internal>(
766✔
711
         Protocol_Version::TLS_V12,
712
         ch.session_id(),
383✔
713
         make_server_hello_random(rng, Protocol_Version::TLS_V13, cb, policy),
383✔
714
         choose_ciphersuite(ch, policy),
383✔
715
         uint8_t(0) /* compression method */
766✔
716
         )) {
766✔
717
   // RFC 8446 4.2.1
718
   //    A server which negotiates TLS 1.3 MUST respond by sending a
719
   //    "supported_versions" extension containing the selected version
720
   //    value (0x0304). It MUST set the ServerHello.legacy_version field to
721
   //     0x0303 (TLS 1.2).
722
   //
723
   // Note that the legacy version (TLS 1.2) is set in this constructor's
724
   // initializer list, accordingly.
725
   m_data->extensions().add(new Supported_Versions(Protocol_Version::TLS_V13));
383✔
726

727
   if(key_exchange_group.has_value()) {
383✔
728
      BOTAN_ASSERT_NOMSG(ch.extensions().has<Key_Share>());
383✔
729
      m_data->extensions().add(Key_Share::create_as_encapsulation(
1,133✔
730
         key_exchange_group.value(), *ch.extensions().get<Key_Share>(), policy, cb, rng));
383✔
731
   }
732

733
   auto& ch_exts = ch.extensions();
367✔
734

735
   if(ch_exts.has<PSK>()) {
367✔
736
      const auto cs = Ciphersuite::by_id(m_data->ciphersuite());
97✔
737
      BOTAN_ASSERT_NOMSG(cs);
97✔
738

739
      // RFC 8446 4.2.9
740
      //    A client MUST provide a "psk_key_exchange_modes" extension if it
741
      //    offers a "pre_shared_key" extension.
742
      //
743
      // Note: Client_Hello_13 constructor already performed a graceful check.
744
      const auto psk_modes = ch_exts.get<PSK_Key_Exchange_Modes>();
97✔
745
      BOTAN_ASSERT_NONNULL(psk_modes);
97✔
746

747
      // TODO: also support PSK_Key_Exchange_Mode::PSK_KE
748
      //       (PSK-based handshake without an additional ephemeral key exchange)
749
      if(value_exists(psk_modes->modes(), PSK_Key_Exchange_Mode::PSK_DHE_KE)) {
97✔
750
         if(auto server_psk = ch_exts.get<PSK>()->select_offered_psk(
96✔
751
               ch.sni_hostname(), cs.value(), session_mgr, credentials_mgr, cb, policy)) {
192✔
752
            // RFC 8446 4.2.11
753
            //    In order to accept PSK key establishment, the server sends a
754
            //    "pre_shared_key" extension indicating the selected identity.
755
            m_data->extensions().add(std::move(server_psk));
188✔
756
         }
96✔
757
      }
758
   }
759

760
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Server, type());
367✔
761
}
383✔
762

763
std::optional<Protocol_Version> Server_Hello_13::random_signals_downgrade() const {
×
764
   const uint64_t last8 = load_be<uint64_t>(m_data->random().data(), 3);
×
765
   if(last8 == DOWNGRADE_TLS11) {
×
766
      return Protocol_Version::TLS_V11;
×
767
   }
768
   if(last8 == DOWNGRADE_TLS12) {
×
769
      return Protocol_Version::TLS_V12;
×
770
   }
771

772
   return std::nullopt;
×
773
}
774

775
Protocol_Version Server_Hello_13::selected_version() const {
2,604✔
776
   const auto versions_ext = m_data->extensions().get<Supported_Versions>();
2,604✔
777
   BOTAN_ASSERT_NOMSG(versions_ext);
2,604✔
778
   const auto& versions = versions_ext->versions();
2,604✔
779
   BOTAN_ASSERT_NOMSG(versions.size() == 1);
2,604✔
780
   return versions.front();
2,604✔
781
}
782

783
Hello_Retry_Request::Hello_Retry_Request(std::unique_ptr<Server_Hello_Internal> data) :
69✔
784
      Server_Hello_13(std::move(data), Server_Hello_13::as_hello_retry_request) {}
69✔
785

786
Hello_Retry_Request::Hello_Retry_Request(const Client_Hello_13& ch,
49✔
787
                                         Named_Group selected_group,
788
                                         const Policy& policy,
789
                                         Callbacks& cb) :
49✔
790
      Server_Hello_13(std::make_unique<Server_Hello_Internal>(Protocol_Version::TLS_V12 /* legacy_version */,
98✔
791
                                                              ch.session_id(),
49✔
792
                                                              HELLO_RETRY_REQUEST_MARKER,
793
                                                              choose_ciphersuite(ch, policy),
49✔
794
                                                              uint8_t(0) /* compression method */,
98✔
795
                                                              true /* is Hello Retry Request */
98✔
796
                                                              ),
797
                      as_new_hello_retry_request) {
98✔
798
   // RFC 8446 4.1.4
799
   //     As with the ServerHello, a HelloRetryRequest MUST NOT contain any
800
   //     extensions that were not first offered by the client in its
801
   //     ClientHello, with the exception of optionally the "cookie" [...]
802
   //     extension.
803
   BOTAN_STATE_CHECK(ch.extensions().has<Supported_Groups>());
49✔
804
   BOTAN_STATE_CHECK(ch.extensions().has<Key_Share>());
49✔
805

806
   BOTAN_STATE_CHECK(!value_exists(ch.extensions().get<Key_Share>()->offered_groups(), selected_group));
114✔
807

808
   // RFC 8446 4.1.4
809
   //    The server's extensions MUST contain "supported_versions".
810
   //
811
   // RFC 8446 4.2.1
812
   //    A server which negotiates TLS 1.3 MUST respond by sending a
813
   //    "supported_versions" extension containing the selected version
814
   //    value (0x0304). It MUST set the ServerHello.legacy_version field to
815
   //    0x0303 (TLS 1.2).
816
   //
817
   // Note that the legacy version (TLS 1.2) is set in this constructor's
818
   // initializer list, accordingly.
819
   m_data->extensions().add(new Supported_Versions(Protocol_Version::TLS_V13));
49✔
820

821
   m_data->extensions().add(new Key_Share(selected_group));
49✔
822

823
   cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Server, type());
49✔
824
}
49✔
825

826
#endif  // BOTAN_HAS_TLS_13
827

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