Botan 3.10.0
Crypto and TLS for C&
msg_server_hello.cpp
Go to the documentation of this file.
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/tls_callbacks.h>
15#include <botan/tls_exceptn.h>
16#include <botan/tls_extensions.h>
17#include <botan/tls_session_manager.h>
18#include <botan/internal/ct_utils.h>
19#include <botan/internal/stl_util.h>
20#include <botan/internal/tls_handshake_hash.h>
21#include <botan/internal/tls_handshake_io.h>
22#include <botan/internal/tls_reader.h>
23#include <array>
24
25namespace Botan::TLS {
26
27namespace {
28
29const uint64_t DOWNGRADE_TLS11 = 0x444F574E47524400;
30const uint64_t DOWNGRADE_TLS12 = 0x444F574E47524401;
31
32// SHA-256("HelloRetryRequest")
33const std::array<uint8_t, 32> HELLO_RETRY_REQUEST_MARKER = {
34 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
35 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C};
36
37bool random_signals_hello_retry_request(const std::vector<uint8_t>& random) {
38 return CT::is_equal(random.data(), HELLO_RETRY_REQUEST_MARKER.data(), HELLO_RETRY_REQUEST_MARKER.size()).as_bool();
39}
40
41std::vector<uint8_t> make_server_hello_random(RandomNumberGenerator& rng,
42 Protocol_Version offered_version,
43 Callbacks& cb,
44 const Policy& policy) {
45 BOTAN_UNUSED(offered_version);
46 auto random = make_hello_random(rng, cb, policy);
47
48 // RFC 8446 4.1.3
49 // TLS 1.3 has a downgrade protection mechanism embedded in the server's
50 // random value. TLS 1.3 servers which negotiate TLS 1.2 or below in
51 // response to a ClientHello MUST set the last 8 bytes of their Random
52 // value specially in their ServerHello.
53 //
54 // If negotiating TLS 1.2, TLS 1.3 servers MUST set the last 8 bytes of
55 // their Random value to the bytes: [DOWNGRADE_TLS12]
56 if(offered_version.is_pre_tls_13() && policy.allow_tls13()) {
57 constexpr size_t downgrade_signal_length = sizeof(DOWNGRADE_TLS12);
58 BOTAN_ASSERT_NOMSG(random.size() >= downgrade_signal_length);
59 auto* lastbytes = random.data() + random.size() - downgrade_signal_length;
60 store_be(DOWNGRADE_TLS12, lastbytes);
61 }
62
63 return random;
64}
65
66} // namespace
67
68/**
69* Version-agnostic internal server hello data container that allows
70* parsing Server_Hello messages without prior knowledge of the contained
71* protocol version.
72*/
73class Server_Hello_Internal {
74 public:
75 /**
76 * Deserialize a Server Hello message
77 */
78 explicit Server_Hello_Internal(const std::vector<uint8_t>& buf) {
79 if(buf.size() < 38) {
80 throw Decoding_Error("Server_Hello: Packet corrupted");
81 }
82
83 TLS_Data_Reader reader("ServerHello", buf);
84
85 const uint8_t major_version = reader.get_byte();
86 const uint8_t minor_version = reader.get_byte();
87
88 m_legacy_version = Protocol_Version(major_version, minor_version);
89
90 // RFC 8446 4.1.3
91 // Upon receiving a message with type server_hello, implementations MUST
92 // first examine the Random value and, if it matches this value, process
93 // it as described in Section 4.1.4 [Hello Retry Request]).
94 m_random = reader.get_fixed<uint8_t>(32);
95 m_is_hello_retry_request = random_signals_hello_retry_request(m_random);
96
97 m_session_id = Session_ID(reader.get_range<uint8_t>(1, 0, 32));
98 m_ciphersuite = reader.get_uint16_t();
99 m_comp_method = reader.get_byte();
100
101 // Note that this code path might parse a TLS 1.2 (or older) server hello message that
102 // is nevertheless marked as being a 'hello retry request' (potentially maliciously).
103 // Extension parsing will however not be affected by the associated flag.
104 // Only after parsing the extensions will the upstream code be able to decide
105 // whether we're dealing with TLS 1.3 or older.
106 m_extensions.deserialize(
107 reader,
110 }
111
112 Server_Hello_Internal(Protocol_Version lv,
113 Session_ID sid,
114 std::vector<uint8_t> r,
115 const uint16_t cs,
116 const uint8_t cm,
117 bool is_hrr = false) :
118 m_legacy_version(lv),
119 m_session_id(std::move(sid)),
120 m_random(std::move(r)),
121 m_is_hello_retry_request(is_hrr),
122 m_ciphersuite(cs),
123 m_comp_method(cm) {}
124
125 Protocol_Version version() const {
126 // RFC 8446 4.2.1
127 // A server which negotiates a version of TLS prior to TLS 1.3 MUST set
128 // ServerHello.version and MUST NOT send the "supported_versions"
129 // extension. A server which negotiates TLS 1.3 MUST respond by sending
130 // a "supported_versions" extension containing the selected version
131 // value (0x0304).
132 //
133 // Note: Here we just take a message parsing decision, further validation of
134 // the extension's contents is done later.
135 return (extensions().has<Supported_Versions>()) ? Protocol_Version::TLS_V13 : m_legacy_version;
136 }
137
138 Protocol_Version legacy_version() const { return m_legacy_version; }
139
140 const Session_ID& session_id() const { return m_session_id; }
141
142 const std::vector<uint8_t>& random() const { return m_random; }
143
144 uint16_t ciphersuite() const { return m_ciphersuite; }
145
146 uint8_t comp_method() const { return m_comp_method; }
147
148 bool is_hello_retry_request() const { return m_is_hello_retry_request; }
149
150 const Extensions& extensions() const { return m_extensions; }
151
152 Extensions& extensions() { return m_extensions; }
153
154 private:
155 Protocol_Version m_legacy_version;
156 Session_ID m_session_id;
157 std::vector<uint8_t> m_random;
158 bool m_is_hello_retry_request;
159 uint16_t m_ciphersuite;
160 uint8_t m_comp_method;
161
162 Extensions m_extensions;
163};
164
165Server_Hello::Server_Hello(std::unique_ptr<Server_Hello_Internal> data) : m_data(std::move(data)) {}
166
167Server_Hello::Server_Hello(Server_Hello&&) noexcept = default;
168Server_Hello& Server_Hello::operator=(Server_Hello&&) noexcept = default;
169
170Server_Hello::~Server_Hello() = default;
171
172/*
173* Serialize a Server Hello message
174*/
175std::vector<uint8_t> Server_Hello::serialize() const {
176 std::vector<uint8_t> buf;
177 buf.reserve(1024); // working around GCC warning
178
179 buf.push_back(m_data->legacy_version().major_version());
180 buf.push_back(m_data->legacy_version().minor_version());
181 buf += m_data->random();
182
183 append_tls_length_value(buf, m_data->session_id().get(), 1);
184
185 buf.push_back(get_byte<0>(m_data->ciphersuite()));
186 buf.push_back(get_byte<1>(m_data->ciphersuite()));
187
188 buf.push_back(m_data->comp_method());
189
190 buf += m_data->extensions().serialize(Connection_Side::Server);
191
192 return buf;
193}
194
198
200 return m_data->legacy_version();
201}
202
203const std::vector<uint8_t>& Server_Hello::random() const {
204 return m_data->random();
205}
206
208 return m_data->comp_method();
209}
210
212 return m_data->session_id();
213}
214
216 return m_data->ciphersuite();
217}
218
219std::set<Extension_Code> Server_Hello::extension_types() const {
220 return m_data->extensions().extension_types();
221}
222
224 return m_data->extensions();
225}
226
227// New session case
229 Handshake_Hash& hash,
230 const Policy& policy,
231 Callbacks& cb,
233 const std::vector<uint8_t>& reneg_info,
234 const Client_Hello_12& client_hello,
235 const Server_Hello_12::Settings& server_settings,
236 std::string_view next_protocol) :
237 Server_Hello(std::make_unique<Server_Hello_Internal>(
238 server_settings.protocol_version(),
239 server_settings.session_id(),
240 make_server_hello_random(rng, server_settings.protocol_version(), cb, policy),
241 server_settings.ciphersuite(),
242 uint8_t(0))) {
243 // NOLINTBEGIN(*-owning-memory)
244 if(client_hello.supports_extended_master_secret()) {
245 m_data->extensions().add(new Extended_Master_Secret);
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()) {
250 m_data->extensions().add(new Certificate_Status_Request);
251 }
252
253 if(!next_protocol.empty() && client_hello.supports_alpn()) {
254 m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocol));
255 }
256
257 const auto c = Ciphersuite::by_id(m_data->ciphersuite());
258
259 if(c && c->cbc_ciphersuite() && client_hello.supports_encrypt_then_mac() && policy.negotiate_encrypt_then_mac()) {
260 m_data->extensions().add(new Encrypt_then_MAC);
261 }
262
263 if(c && c->ecc_ciphersuite() && client_hello.extension_types().contains(Extension_Code::EcPointFormats)) {
264 m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
265 }
266
267 if(client_hello.secure_renegotiation()) {
268 m_data->extensions().add(new Renegotiation_Extension(reneg_info));
269 }
270
271 if(client_hello.supports_session_ticket() && server_settings.offer_session_ticket()) {
272 m_data->extensions().add(new Session_Ticket_Extension());
273 }
274
275 if(m_data->legacy_version().is_datagram_protocol()) {
276 const std::vector<uint16_t> server_srtp = policy.srtp_profiles();
277 const std::vector<uint16_t> client_srtp = client_hello.srtp_profiles();
278
279 if(!server_srtp.empty() && !client_srtp.empty()) {
280 uint16_t shared = 0;
281 // always using server preferences for now
282 for(auto s_srtp : server_srtp) {
283 for(auto c_srtp : client_srtp) {
284 if(shared == 0 && s_srtp == c_srtp) {
285 shared = s_srtp;
286 }
287 }
288 }
289
290 if(shared != 0) {
291 m_data->extensions().add(new SRTP_Protection_Profiles(shared));
292 }
293 }
294 }
295 // NOLINTEND(*-owning-memory)
296
297 cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Server, type());
299 hash.update(io.send(*this));
302// Resuming
304 Handshake_Hash& hash,
305 const Policy& policy,
306 Callbacks& cb,
308 const std::vector<uint8_t>& reneg_info,
309 const Client_Hello_12& client_hello,
310 const Session& resumed_session,
311 bool offer_session_ticket,
312 std::string_view next_protocol) :
313 Server_Hello(std::make_unique<Server_Hello_Internal>(resumed_session.version(),
314 client_hello.session_id(),
315 make_hello_random(rng, cb, policy),
316 resumed_session.ciphersuite_code(),
317 uint8_t(0))) {
318 // NOLINTBEGIN(*-owning-memory)
319 if(client_hello.supports_extended_master_secret()) {
320 m_data->extensions().add(new Extended_Master_Secret);
321 }
322
323 if(!next_protocol.empty() && client_hello.supports_alpn()) {
324 m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocol));
325 }
326
327 if(client_hello.supports_encrypt_then_mac() && policy.negotiate_encrypt_then_mac()) {
328 Ciphersuite c = resumed_session.ciphersuite();
329 if(c.cbc_ciphersuite()) {
330 m_data->extensions().add(new Encrypt_then_MAC);
331 }
332 }
333
334 if(resumed_session.ciphersuite().ecc_ciphersuite() &&
335 client_hello.extension_types().contains(Extension_Code::EcPointFormats)) {
336 m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
337 }
338
339 if(client_hello.secure_renegotiation()) {
340 m_data->extensions().add(new Renegotiation_Extension(reneg_info));
341 }
342
343 if(client_hello.supports_session_ticket() && offer_session_ticket) {
344 m_data->extensions().add(new Session_Ticket_Extension());
345 }
346 // NOLINTEND(*-owning-memory)
347
348 cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Server, type());
349
350 hash.update(io.send(*this));
351}
352
353Server_Hello_12::Server_Hello_12(const std::vector<uint8_t>& buf) :
354 Server_Hello_12(std::make_unique<Server_Hello_Internal>(buf)) {}
355
356Server_Hello_12::Server_Hello_12(std::unique_ptr<Server_Hello_Internal> data) : Server_Hello(std::move(data)) {
357 if(!m_data->version().is_pre_tls_13()) {
358 throw TLS_Exception(Alert::ProtocolVersion, "Expected server hello of (D)TLS 1.2 or lower");
359 }
360}
361
365
367 return m_data->extensions().has<Renegotiation_Extension>();
368}
369
370std::vector<uint8_t> Server_Hello_12::renegotiation_info() const {
371 if(Renegotiation_Extension* reneg = m_data->extensions().get<Renegotiation_Extension>()) {
372 return reneg->renegotiation_info();
373 }
374 return std::vector<uint8_t>();
375}
376
378 return m_data->extensions().has<Extended_Master_Secret>();
379}
380
382 return m_data->extensions().has<Encrypt_then_MAC>();
383}
384
388
390 return m_data->extensions().has<Session_Ticket_Extension>();
391}
392
394 if(auto* srtp = m_data->extensions().get<SRTP_Protection_Profiles>()) {
395 auto prof = srtp->profiles();
396 if(prof.size() != 1 || prof[0] == 0) {
397 throw Decoding_Error("Server sent malformed DTLS-SRTP extension");
398 }
399 return prof[0];
400 }
401
402 return 0;
403}
404
406 if(auto* alpn = m_data->extensions().get<Application_Layer_Protocol_Notification>()) {
407 return alpn->single_protocol();
408 }
409 return "";
410}
411
413 if(auto* ecc_formats = m_data->extensions().get<Supported_Point_Formats>()) {
414 return ecc_formats->prefers_compressed();
415 }
416 return false;
417}
418
419std::optional<Protocol_Version> Server_Hello_12::random_signals_downgrade() const {
420 const uint64_t last8 = load_be<uint64_t>(m_data->random().data(), 3);
421 if(last8 == DOWNGRADE_TLS11) {
422 return Protocol_Version::TLS_V11;
423 }
424 if(last8 == DOWNGRADE_TLS12) {
425 return Protocol_Version::TLS_V12;
426 }
427
428 return std::nullopt;
429}
430
431/*
432* Create a new Server Hello Done message
433*/
437
438/*
439* Deserialize a Server Hello Done message
440*/
441Server_Hello_Done::Server_Hello_Done(const std::vector<uint8_t>& buf) {
442 if(!buf.empty()) {
443 throw Decoding_Error("Server_Hello_Done: Must be empty, and is not");
444 }
445}
446
447/*
448* Serialize a Server Hello Done message
449*/
450std::vector<uint8_t> Server_Hello_Done::serialize() const {
451 return std::vector<uint8_t>();
452}
453
454#if defined(BOTAN_HAS_TLS_13)
455
456const Server_Hello_13::Server_Hello_Tag Server_Hello_13::as_server_hello;
457const Server_Hello_13::Hello_Retry_Request_Tag Server_Hello_13::as_hello_retry_request;
458const Server_Hello_13::Hello_Retry_Request_Creation_Tag Server_Hello_13::as_new_hello_retry_request;
459
460std::variant<Hello_Retry_Request, Server_Hello_13> Server_Hello_13::create(const Client_Hello_13& ch,
461 bool hello_retry_request_allowed,
462 Session_Manager& session_mgr,
463 Credentials_Manager& credentials_mgr,
465 const Policy& policy,
466 Callbacks& cb) {
467 const auto& exts = ch.extensions();
468
469 // RFC 8446 4.2.9
470 // [With PSK with (EC)DHE key establishment], the client and server MUST
471 // supply "key_share" values [...].
472 //
473 // Note: We currently do not support PSK without (EC)DHE, hence, we can
474 // assume that those extensions are available.
475 BOTAN_ASSERT_NOMSG(exts.has<Supported_Groups>() && exts.has<Key_Share>());
476 const auto& supported_by_client = exts.get<Supported_Groups>()->groups();
477 const auto& offered_by_client = exts.get<Key_Share>()->offered_groups();
478 const auto selected_group = policy.choose_key_exchange_group(supported_by_client, offered_by_client);
479
480 // RFC 8446 4.1.1
481 // If there is no overlap between the received "supported_groups" and the
482 // groups supported by the server, then the server MUST abort the
483 // handshake with a "handshake_failure" or an "insufficient_security" alert.
484 if(selected_group == Named_Group::NONE) {
485 throw TLS_Exception(Alert::HandshakeFailure, "Client did not offer any acceptable group");
486 }
487
488 // RFC 8446 4.2.8:
489 // Servers MUST NOT send a KeyShareEntry for any group not indicated in the
490 // client's "supported_groups" extension [...]
491 if(!value_exists(supported_by_client, selected_group)) {
492 throw TLS_Exception(Alert::InternalError, "Application selected a group that is not supported by the client");
493 }
494
495 // RFC 8446 4.1.4
496 // The server will send this message in response to a ClientHello
497 // message if it is able to find an acceptable set of parameters but the
498 // ClientHello does not contain sufficient information to proceed with
499 // the handshake.
500 //
501 // In this case, the Client Hello did not contain a key share offer for
502 // the group selected by the application.
503 if(!value_exists(offered_by_client, selected_group)) {
504 // RFC 8446 4.1.4
505 // If a client receives a second HelloRetryRequest in the same
506 // connection (i.e., where the ClientHello was itself in response to a
507 // HelloRetryRequest), it MUST abort the handshake with an
508 // "unexpected_message" alert.
509 BOTAN_STATE_CHECK(hello_retry_request_allowed);
510 return Hello_Retry_Request(ch, selected_group, policy, cb);
511 } else {
512 return Server_Hello_13(ch, selected_group, session_mgr, credentials_mgr, rng, cb, policy);
513 }
514}
515
516std::variant<Hello_Retry_Request, Server_Hello_13, Server_Hello_12> Server_Hello_13::parse(
517 const std::vector<uint8_t>& buf) {
518 auto data = std::make_unique<Server_Hello_Internal>(buf);
519 const auto version = data->version();
520
521 // server hello that appears to be pre-TLS 1.3, takes precedence over...
522 if(version.is_pre_tls_13()) {
523 return Server_Hello_12(std::move(data));
524 }
525
526 // ... the TLS 1.3 "special case" aka. Hello_Retry_Request
527 if(version == Protocol_Version::TLS_V13) {
528 if(data->is_hello_retry_request()) {
529 return Hello_Retry_Request(std::move(data));
530 }
531
532 return Server_Hello_13(std::move(data));
533 }
534
535 throw TLS_Exception(Alert::ProtocolVersion, "unexpected server hello version: " + version.to_string());
536}
537
538/**
539 * Validation that applies to both Server Hello and Hello Retry Request
540 */
542 BOTAN_ASSERT_NOMSG(m_data->version() == Protocol_Version::TLS_V13);
543
544 // Note: checks that cannot be performed without contextual information
545 // are done in the specific TLS client implementation.
546 // Note: The Supported_Version extension makes sure internally that
547 // exactly one entry is provided.
548
549 // Note: Hello Retry Request basic validation is equivalent with the
550 // basic validations required for Server Hello
551 //
552 // RFC 8446 4.1.4
553 // Upon receipt of a HelloRetryRequest, the client MUST check the
554 // legacy_version, [...], and legacy_compression_method as specified in
555 // Section 4.1.3 and then process the extensions, starting with determining
556 // the version using "supported_versions".
557
558 // RFC 8446 4.1.3
559 // In TLS 1.3, [...] the legacy_version field MUST be set to 0x0303
560 if(legacy_version() != Protocol_Version::TLS_V12) {
561 throw TLS_Exception(Alert::ProtocolVersion,
562 "legacy_version '" + legacy_version().to_string() + "' is not allowed");
563 }
564
565 // RFC 8446 4.1.3
566 // legacy_compression_method: A single byte which MUST have the value 0.
567 if(compression_method() != 0x00) {
568 throw TLS_Exception(Alert::DecodeError, "compression is not supported in TLS 1.3");
569 }
570
571 // RFC 8446 4.1.3
572 // All TLS 1.3 ServerHello messages MUST contain the "supported_versions" extension.
573 if(!extensions().has<Supported_Versions>()) {
574 throw TLS_Exception(Alert::MissingExtension, "server hello did not contain 'supported version' extension");
575 }
576
577 // RFC 8446 4.2.1
578 // A server which negotiates TLS 1.3 MUST respond by sending
579 // a "supported_versions" extension containing the selected version
580 // value (0x0304).
581 if(selected_version() != Protocol_Version::TLS_V13) {
582 throw TLS_Exception(Alert::IllegalParameter, "TLS 1.3 Server Hello selected a different version");
583 }
584}
585
586Server_Hello_13::Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data,
587 Server_Hello_13::Server_Hello_Tag /*tag*/) :
588 Server_Hello(std::move(data)) {
589 BOTAN_ASSERT_NOMSG(!m_data->is_hello_retry_request());
591
592 const auto& exts = extensions();
593
594 // RFC 8446 4.1.3
595 // The ServerHello MUST only include extensions which are required to
596 // establish the cryptographic context and negotiate the protocol version.
597 // [...]
598 // Other extensions (see Section 4.2) are sent separately in the
599 // EncryptedExtensions message.
600 //
601 // Note that further validation dependent on the client hello is done in the
602 // TLS client implementation.
603 const std::set<Extension_Code> allowed = {
607 };
608
609 // As the ServerHello shall only contain essential extensions, we don't give
610 // any slack for extensions not implemented by Botan here.
611 if(exts.contains_other_than(allowed)) {
612 throw TLS_Exception(Alert::UnsupportedExtension, "Server Hello contained an extension that is not allowed");
613 }
614
615 // RFC 8446 4.1.3
616 // Current ServerHello messages additionally contain
617 // either the "pre_shared_key" extension or the "key_share"
618 // extension, or both [...].
619 if(!exts.has<Key_Share>() && !exts.has<PSK_Key_Exchange_Modes>()) {
620 throw TLS_Exception(Alert::MissingExtension, "server hello must contain key exchange information");
621 }
622}
623
624Server_Hello_13::Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data,
625 Server_Hello_13::Hello_Retry_Request_Tag /*tag*/) :
626 Server_Hello(std::move(data)) {
627 BOTAN_ASSERT_NOMSG(m_data->is_hello_retry_request());
629
630 const auto& exts = extensions();
631
632 // RFC 8446 4.1.4
633 // The HelloRetryRequest extensions defined in this specification are:
634 // - supported_versions (see Section 4.2.1)
635 // - cookie (see Section 4.2.2)
636 // - key_share (see Section 4.2.8)
637 const std::set<Extension_Code> allowed = {
641 };
642
643 // As the Hello Retry Request shall only contain essential extensions, we
644 // don't give any slack for extensions not implemented by Botan here.
645 if(exts.contains_other_than(allowed)) {
646 throw TLS_Exception(Alert::UnsupportedExtension,
647 "Hello Retry Request contained an extension that is not allowed");
648 }
649
650 // RFC 8446 4.1.4
651 // Clients MUST abort the handshake with an "illegal_parameter" alert if
652 // the HelloRetryRequest would not result in any change in the ClientHello.
653 if(!exts.has<Key_Share>() && !exts.has<Cookie>()) {
654 throw TLS_Exception(Alert::IllegalParameter, "Hello Retry Request does not request any changes to Client Hello");
655 }
656}
657
658Server_Hello_13::Server_Hello_13(std::unique_ptr<Server_Hello_Internal> data,
659 Hello_Retry_Request_Creation_Tag /*tag*/) :
660 Server_Hello(std::move(data)) {}
661
662namespace {
663
664uint16_t choose_ciphersuite(const Client_Hello_13& ch, const Policy& policy) {
665 auto pref_list = ch.ciphersuites();
666 // TODO: DTLS might need to make this version dynamic
667 auto other_list = policy.ciphersuite_list(Protocol_Version::TLS_V13);
668
670 std::swap(pref_list, other_list);
671 }
672
673 for(auto suite_id : pref_list) {
674 // TODO: take potentially available PSKs into account to select a
675 // compatible ciphersuite.
676 //
677 // Assuming the client sent one or more PSKs, we would first need to find
678 // the hash functions they are associated to. For session tickets, that
679 // would mean decrypting the ticket and comparing the cipher suite used in
680 // those tickets. For (currently not yet supported) pre-assigned PSKs, the
681 // hash function needs to be specified along with them.
682 //
683 // Then we could refine the ciphersuite selection using the required hash
684 // function for the PSK(s) we are wishing to use down the road.
685 //
686 // For now, we just negotiate the cipher suite blindly and hope for the
687 // best. As long as PSKs are used for session resumption only, this has a
688 // high chance of success. Previous handshakes with this client have very
689 // likely selected the same ciphersuite anyway.
690 //
691 // See also RFC 8446 4.2.11
692 // When session resumption is the primary use case of PSKs, the most
693 // straightforward way to implement the PSK/cipher suite matching
694 // requirements is to negotiate the cipher suite first [...].
695 if(value_exists(other_list, suite_id)) {
696 return suite_id;
697 }
698 }
699
700 // RFC 8446 4.1.1
701 // If the server is unable to negotiate a supported set of parameters
702 // [...], it MUST abort the handshake with either a "handshake_failure"
703 // or "insufficient_security" fatal alert [...].
704 throw TLS_Exception(Alert::HandshakeFailure, "Can't agree on a ciphersuite with client");
705}
706} // namespace
707
709 std::optional<Named_Group> key_exchange_group,
710 Session_Manager& session_mgr,
711 Credentials_Manager& credentials_mgr,
713 Callbacks& cb,
714 const Policy& policy) :
715 Server_Hello(std::make_unique<Server_Hello_Internal>(
717 ch.session_id(),
718 make_server_hello_random(rng, Protocol_Version::TLS_V13, cb, policy),
719 choose_ciphersuite(ch, policy),
720 uint8_t(0) /* compression method */
721 )) {
722 // RFC 8446 4.2.1
723 // A server which negotiates TLS 1.3 MUST respond by sending a
724 // "supported_versions" extension containing the selected version
725 // value (0x0304). It MUST set the ServerHello.legacy_version field to
726 // 0x0303 (TLS 1.2).
727 //
728 // Note that the legacy version (TLS 1.2) is set in this constructor's
729 // initializer list, accordingly.
730 m_data->extensions().add(new Supported_Versions(Protocol_Version::TLS_V13)); // NOLINT(*-owning-memory)
731
732 if(key_exchange_group.has_value()) {
733 BOTAN_ASSERT_NOMSG(ch.extensions().has<Key_Share>());
734 m_data->extensions().add(Key_Share::create_as_encapsulation(
735 key_exchange_group.value(), *ch.extensions().get<Key_Share>(), policy, cb, rng));
736 }
737
738 const auto& ch_exts = ch.extensions();
739
740 if(ch_exts.has<PSK>()) {
741 const auto cs = Ciphersuite::by_id(m_data->ciphersuite());
742 BOTAN_ASSERT_NOMSG(cs);
743
744 // RFC 8446 4.2.9
745 // A client MUST provide a "psk_key_exchange_modes" extension if it
746 // offers a "pre_shared_key" extension.
747 //
748 // Note: Client_Hello_13 constructor already performed a graceful check.
749 auto* const psk_modes = ch_exts.get<PSK_Key_Exchange_Modes>();
750 BOTAN_ASSERT_NONNULL(psk_modes);
751
752 // TODO: also support PSK_Key_Exchange_Mode::PSK_KE
753 // (PSK-based handshake without an additional ephemeral key exchange)
754 if(value_exists(psk_modes->modes(), PSK_Key_Exchange_Mode::PSK_DHE_KE)) {
755 if(auto server_psk = ch_exts.get<PSK>()->select_offered_psk(
756 ch.sni_hostname(), cs.value(), session_mgr, credentials_mgr, cb, policy)) {
757 // RFC 8446 4.2.11
758 // In order to accept PSK key establishment, the server sends a
759 // "pre_shared_key" extension indicating the selected identity.
760 m_data->extensions().add(std::move(server_psk));
761 }
762 }
763 }
764
765 cb.tls_modify_extensions(m_data->extensions(), Connection_Side::Server, type());
766}
767
768std::optional<Protocol_Version> Server_Hello_13::random_signals_downgrade() const {
769 const uint64_t last8 = load_be<uint64_t>(m_data->random().data(), 3);
770 if(last8 == DOWNGRADE_TLS11) {
771 return Protocol_Version::TLS_V11;
772 }
773 if(last8 == DOWNGRADE_TLS12) {
774 return Protocol_Version::TLS_V12;
775 }
776
777 return std::nullopt;
778}
779
781 auto* const versions_ext = m_data->extensions().get<Supported_Versions>();
782 BOTAN_ASSERT_NOMSG(versions_ext);
783 const auto& versions = versions_ext->versions();
784 BOTAN_ASSERT_NOMSG(versions.size() == 1);
785 return versions.front();
786}
787
788Hello_Retry_Request::Hello_Retry_Request(std::unique_ptr<Server_Hello_Internal> data) :
790
792 Named_Group selected_group,
793 const Policy& policy,
794 Callbacks& cb) :
795 Server_Hello_13(std::make_unique<Server_Hello_Internal>(
796 Protocol_Version::TLS_V12 /* legacy_version */,
797 ch.session_id(),
798 std::vector<uint8_t>(HELLO_RETRY_REQUEST_MARKER.begin(), HELLO_RETRY_REQUEST_MARKER.end()),
799 choose_ciphersuite(ch, policy),
800 uint8_t(0) /* compression method */,
801 true /* is Hello Retry Request */
802 ),
804 // RFC 8446 4.1.4
805 // As with the ServerHello, a HelloRetryRequest MUST NOT contain any
806 // extensions that were not first offered by the client in its
807 // ClientHello, with the exception of optionally the "cookie" [...]
808 // extension.
811
813
814 // RFC 8446 4.1.4
815 // The server's extensions MUST contain "supported_versions".
816 //
817 // RFC 8446 4.2.1
818 // A server which negotiates TLS 1.3 MUST respond by sending a
819 // "supported_versions" extension containing the selected version
820 // value (0x0304). It MUST set the ServerHello.legacy_version field to
821 // 0x0303 (TLS 1.2).
822 //
823 // Note that the legacy version (TLS 1.2) is set in this constructor's
824 // initializer list, accordingly.
825 // NOLINTBEGIN(*-owning-memory)
826 m_data->extensions().add(new Supported_Versions(Protocol_Version::TLS_V13));
827
828 m_data->extensions().add(new Key_Share(selected_group));
829 // NOLINTEND(*-owning-memory)
830
832}
833
834#endif // BOTAN_HAS_TLS_13
835
836} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:144
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
virtual void tls_modify_extensions(Extensions &extn, Connection_Side which_side, Handshake_Type which_message)
static std::optional< Ciphersuite > by_id(uint16_t suite)
const Extensions & extensions() const
const std::vector< uint16_t > & ciphersuites() const
std::set< Extension_Code > extension_types() const
void update(const uint8_t in[], size_t length)
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=0
virtual std::vector< uint8_t > serialize() const =0
Hello_Retry_Request(std::unique_ptr< Server_Hello_Internal > data)
Handshake_Type type() const override
std::vector< Named_Group > offered_groups() const
virtual std::vector< uint16_t > ciphersuite_list(Protocol_Version version) const
virtual bool negotiate_encrypt_then_mac() const
virtual bool server_uses_own_ciphersuite_preferences() const
virtual bool support_cert_status_message() const
virtual Group_Params choose_key_exchange_group(const std::vector< Group_Params > &supported_by_peer, const std::vector< Group_Params > &offered_by_peer) const
std::optional< Protocol_Version > random_signals_downgrade() const
Protocol_Version selected_version() const override
bool supports_certificate_status_message() const
std::string next_protocol() const
std::vector< uint8_t > renegotiation_info() const
Server_Hello_12(Handshake_IO &io, Handshake_Hash &hash, const Policy &policy, Callbacks &cb, RandomNumberGenerator &rng, const std::vector< uint8_t > &secure_reneg_info, const Client_Hello_12 &client_hello, const Settings &settings, std::string_view next_protocol)
Protocol_Version legacy_version() const
static const struct Botan::TLS::Server_Hello_13::Hello_Retry_Request_Tag as_hello_retry_request
static const struct Botan::TLS::Server_Hello_13::Hello_Retry_Request_Creation_Tag as_new_hello_retry_request
Server_Hello_13(std::unique_ptr< Server_Hello_Internal > data, Server_Hello_Tag tag=as_server_hello)
std::optional< Protocol_Version > random_signals_downgrade() const
static std::variant< Hello_Retry_Request, Server_Hello_13 > create(const Client_Hello_13 &ch, bool hello_retry_request_allowed, Session_Manager &session_mgr, Credentials_Manager &credentials_mgr, RandomNumberGenerator &rng, const Policy &policy, Callbacks &cb)
Protocol_Version selected_version() const final
static std::variant< Hello_Retry_Request, Server_Hello_13, Server_Hello_12 > parse(const std::vector< uint8_t > &buf)
static const struct Botan::TLS::Server_Hello_13::Server_Hello_Tag as_server_hello
Server_Hello_Done(Handshake_IO &io, Handshake_Hash &hash)
Server_Hello(const Server_Hello &)=delete
std::vector< uint8_t > serialize() const override
std::set< Extension_Code > extension_types() const
Handshake_Type type() const override
const Session_ID & session_id() const
const std::vector< uint8_t > & random() const
std::unique_ptr< Server_Hello_Internal > m_data
const Extensions & extensions() const
Protocol_Version legacy_version() const
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:826
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
Definition tls_reader.h:184
std::vector< uint8_t > make_hello_random(RandomNumberGenerator &rng, Callbacks &cb, const Policy &policy)
Group_Params Named_Group
Strong< std::vector< uint8_t >, struct Session_ID_ > Session_ID
holds a TLS 1.2 session ID for stateful resumption
Definition tls_session.h:31
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:51
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504