Botan 3.10.0
Crypto and TLS for C&
Botan::TLS::Server_Hello_13 Class Reference

#include <tls_messages.h>

Inheritance diagram for Botan::TLS::Server_Hello_13:
Botan::TLS::Server_Hello Botan::TLS::Handshake_Message Botan::TLS::Hello_Retry_Request

Classes

struct  Hello_Retry_Request_Creation_Tag
struct  Hello_Retry_Request_Tag
struct  Server_Hello_Tag

Public Member Functions

uint16_t ciphersuite () const
const Extensionsextensions () const
std::optional< Protocol_Versionrandom_signals_downgrade () const
Protocol_Version selected_version () const final
std::vector< uint8_t > serialize () const override
const Session_IDsession_id () const
Handshake_Type type () const override
std::string type_string () const
virtual Handshake_Type wire_type () const

Static Public Member Functions

static std::variant< Hello_Retry_Request, Server_Hello_13create (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)
static std::variant< Hello_Retry_Request, Server_Hello_13, Server_Hello_12parse (const std::vector< uint8_t > &buf)

Protected Member Functions

void basic_validation () const
uint8_t compression_method () const
std::set< Extension_Codeextension_types () const
Protocol_Version legacy_version () const
const std::vector< uint8_t > & random () const
 Server_Hello_13 (const Client_Hello_13 &ch, std::optional< Named_Group > key_exchange_group, Session_Manager &session_mgr, Credentials_Manager &credentials_mgr, RandomNumberGenerator &rng, Callbacks &cb, const Policy &policy)
 Server_Hello_13 (std::unique_ptr< Server_Hello_Internal > data, Hello_Retry_Request_Creation_Tag tag)
 Server_Hello_13 (std::unique_ptr< Server_Hello_Internal > data, Hello_Retry_Request_Tag tag)
 Server_Hello_13 (std::unique_ptr< Server_Hello_Internal > data, Server_Hello_Tag tag=as_server_hello)

Protected Attributes

std::unique_ptr< Server_Hello_Internal > m_data

Static Protected Attributes

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
static const struct Botan::TLS::Server_Hello_13::Server_Hello_Tag as_server_hello

Detailed Description

Definition at line 400 of file tls_messages.h.

Constructor & Destructor Documentation

◆ Server_Hello_13() [1/4]

Botan::TLS::Server_Hello_13::Server_Hello_13 ( std::unique_ptr< Server_Hello_Internal > data,
Server_Hello_13::Server_Hello_Tag tag = as_server_hello )
explicitprotected

Definition at line 586 of file msg_server_hello.cpp.

587 :
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}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
Server_Hello(const Server_Hello &)=delete
std::unique_ptr< Server_Hello_Internal > m_data
const Extensions & extensions() const

References basic_validation(), BOTAN_ASSERT_NOMSG, Botan::TLS::Server_Hello::extensions(), Botan::TLS::KeyShare, Botan::TLS::Server_Hello::m_data, Botan::TLS::PresharedKey, Botan::TLS::Server_Hello::Server_Hello(), and Botan::TLS::SupportedVersions.

Referenced by create(), and parse().

◆ Server_Hello_13() [2/4]

Botan::TLS::Server_Hello_13::Server_Hello_13 ( std::unique_ptr< Server_Hello_Internal > data,
Server_Hello_13::Hello_Retry_Request_Tag tag )
explicitprotected

Definition at line 624 of file msg_server_hello.cpp.

625 :
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}

References basic_validation(), BOTAN_ASSERT_NOMSG, Botan::TLS::Cookie, Botan::TLS::Server_Hello::extensions(), Botan::TLS::KeyShare, Botan::TLS::Server_Hello::m_data, Botan::TLS::Server_Hello::Server_Hello(), and Botan::TLS::SupportedVersions.

◆ Server_Hello_13() [3/4]

Botan::TLS::Server_Hello_13::Server_Hello_13 ( const Client_Hello_13 & ch,
std::optional< Named_Group > key_exchange_group,
Session_Manager & session_mgr,
Credentials_Manager & credentials_mgr,
RandomNumberGenerator & rng,
Callbacks & cb,
const Policy & policy )
protected

Definition at line 708 of file msg_server_hello.cpp.

714 :
715 Server_Hello(std::make_unique<Server_Hello_Internal>(
716 Protocol_Version::TLS_V12,
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>());
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());
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}
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
static std::optional< Ciphersuite > by_id(uint16_t suite)
virtual Handshake_Type type() const =0
static std::unique_ptr< Key_Share > create_as_encapsulation(Group_Params selected_group, const Key_Share &client_keyshare, const Policy &policy, Callbacks &cb, RandomNumberGenerator &rng)
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:51

References Botan::TLS::Client_Hello::extensions(), Botan::TLS::Server_Hello::m_data, Botan::TLS::Server_Hello::Server_Hello(), and Botan::TLS::Server_Hello::session_id().

◆ Server_Hello_13() [4/4]

Botan::TLS::Server_Hello_13::Server_Hello_13 ( std::unique_ptr< Server_Hello_Internal > data,
Hello_Retry_Request_Creation_Tag tag )
explicitprotected

Definition at line 658 of file msg_server_hello.cpp.

659 :
660 Server_Hello(std::move(data)) {}

References Botan::TLS::Server_Hello::Server_Hello().

Member Function Documentation

◆ basic_validation()

void Botan::TLS::Server_Hello_13::basic_validation ( ) const
protected

Validation that applies to both Server Hello and Hello Retry Request

Definition at line 541 of file msg_server_hello.cpp.

541 {
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}
Protocol_Version selected_version() const final
Protocol_Version legacy_version() const
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13

References BOTAN_ASSERT_NOMSG, Botan::TLS::Server_Hello::compression_method(), Botan::TLS::Server_Hello::extensions(), Botan::TLS::Server_Hello::legacy_version(), Botan::TLS::Server_Hello::m_data, selected_version(), and Botan::to_string().

Referenced by Server_Hello_13(), and Server_Hello_13().

◆ ciphersuite()

uint16_t Botan::TLS::Server_Hello::ciphersuite ( ) const
inherited

Definition at line 215 of file msg_server_hello.cpp.

215 {
216 return m_data->ciphersuite();
217}

References m_data.

Referenced by Botan::TLS::Client_Hello_13::retry(), Botan::TLS::Server_Hello_12::Server_Hello_12(), and ~Server_Hello().

◆ compression_method()

uint8_t Botan::TLS::Server_Hello::compression_method ( ) const
protectedinherited

Definition at line 207 of file msg_server_hello.cpp.

207 {
208 return m_data->comp_method();
209}

References m_data.

Referenced by Botan::TLS::Server_Hello_13::basic_validation(), selected_version(), and Botan::TLS::Server_Hello_12::Server_Hello_13.

◆ create()

std::variant< Hello_Retry_Request, Server_Hello_13 > Botan::TLS::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 )
static

Definition at line 460 of file msg_server_hello.cpp.

466 {
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}
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
Server_Hello_13(std::unique_ptr< Server_Hello_Internal > data, Server_Hello_Tag tag=as_server_hello)

References BOTAN_ASSERT_NOMSG, BOTAN_STATE_CHECK, Botan::TLS::Policy::choose_key_exchange_group(), Botan::TLS::Client_Hello::extensions(), Server_Hello_13(), and Botan::value_exists().

◆ extension_types()

std::set< Extension_Code > Botan::TLS::Server_Hello::extension_types ( ) const
protectedinherited

Definition at line 219 of file msg_server_hello.cpp.

219 {
220 return m_data->extensions().extension_types();
221}

References m_data.

Referenced by selected_version(), and Botan::TLS::Server_Hello_12::Server_Hello_13.

◆ extensions()

const Extensions & Botan::TLS::Server_Hello::extensions ( ) const
inherited

◆ legacy_version()

Protocol_Version Botan::TLS::Server_Hello::legacy_version ( ) const
protectedinherited

Definition at line 199 of file msg_server_hello.cpp.

199 {
200 return m_data->legacy_version();
201}

References m_data.

Referenced by Botan::TLS::Server_Hello_13::basic_validation(), selected_version(), and Botan::TLS::Server_Hello_12::Server_Hello_13.

◆ parse()

std::variant< Hello_Retry_Request, Server_Hello_13, Server_Hello_12 > Botan::TLS::Server_Hello_13::parse ( const std::vector< uint8_t > & buf)
static

Definition at line 516 of file msg_server_hello.cpp.

517 {
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}

References Server_Hello_13().

◆ random()

const std::vector< uint8_t > & Botan::TLS::Server_Hello::random ( ) const
protectedinherited

Definition at line 203 of file msg_server_hello.cpp.

203 {
204 return m_data->random();
205}

References m_data.

Referenced by selected_version(), and Botan::TLS::Server_Hello_12::Server_Hello_13.

◆ random_signals_downgrade()

std::optional< Protocol_Version > Botan::TLS::Server_Hello_13::random_signals_downgrade ( ) const

Return desired downgrade version indicated by hello random, if any.

Definition at line 768 of file msg_server_hello.cpp.

768 {
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}
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::load_be(), and Botan::TLS::Server_Hello::m_data.

◆ selected_version()

Protocol_Version Botan::TLS::Server_Hello_13::selected_version ( ) const
finalvirtual
Returns
the selected version as indicated by the supported_versions extension

Implements Botan::TLS::Server_Hello.

Definition at line 780 of file msg_server_hello.cpp.

780 {
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}

References BOTAN_ASSERT_NOMSG, and Botan::TLS::Server_Hello::m_data.

Referenced by basic_validation().

◆ serialize()

std::vector< uint8_t > Botan::TLS::Server_Hello::serialize ( ) const
overridevirtualinherited
Returns
DER representation of this message

Implements Botan::TLS::Handshake_Message.

Definition at line 175 of file msg_server_hello.cpp.

175 {
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}
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
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79

References Botan::TLS::append_tls_length_value(), Botan::get_byte(), m_data, serialize(), Botan::TLS::Server, and Server_Hello().

Referenced by serialize(), and ~Server_Hello().

◆ session_id()

const Session_ID & Botan::TLS::Server_Hello::session_id ( ) const
inherited

◆ type()

Handshake_Type Botan::TLS::Server_Hello::type ( ) const
overridevirtualinherited
Returns
the message type

Implements Botan::TLS::Handshake_Message.

Definition at line 195 of file msg_server_hello.cpp.

References Botan::TLS::ServerHello.

Referenced by ~Server_Hello().

◆ type_string()

std::string Botan::TLS::Handshake_Message::type_string ( ) const
inherited
Returns
string representation of this message type

Definition at line 18 of file tls_handshake_state.cpp.

18 {
20}
const char * handshake_type_to_string(Handshake_Type type)

References Botan::TLS::handshake_type_to_string(), and type().

◆ wire_type()

virtual Handshake_Type Botan::TLS::Handshake_Message::wire_type ( ) const
inlinevirtualinherited
Returns
the wire representation of the message's type

Reimplemented in Botan::TLS::Hello_Retry_Request.

Definition at line 39 of file tls_handshake_msg.h.

39 {
40 // Usually equal to the Handshake_Type enum value,
41 // with the exception of TLS 1.3 Hello Retry Request.
42 return type();
43 }

References type().

Referenced by Botan::TLS::Stream_Handshake_IO::send().

Member Data Documentation

◆ as_hello_retry_request

const Server_Hello_13::Hello_Retry_Request_Tag Botan::TLS::Server_Hello_13::as_hello_retry_request
staticprotected

◆ as_new_hello_retry_request

const Server_Hello_13::Hello_Retry_Request_Creation_Tag Botan::TLS::Server_Hello_13::as_new_hello_retry_request
staticprotected

◆ as_server_hello

const Server_Hello_13::Server_Hello_Tag Botan::TLS::Server_Hello_13::as_server_hello
staticprotected

Definition at line 456 of file msg_server_hello.cpp.

◆ m_data


The documentation for this class was generated from the following files: