12#include <botan/tls_messages.h>
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>
29const uint64_t DOWNGRADE_TLS11 = 0x444F574E47524400;
30const uint64_t DOWNGRADE_TLS12 = 0x444F574E47524401;
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};
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();
41std::vector<uint8_t> make_server_hello_random(RandomNumberGenerator& rng,
56 if(offered_version.is_pre_tls_13() && policy.allow_tls13()) {
57 constexpr size_t downgrade_signal_length =
sizeof(DOWNGRADE_TLS12);
59 auto* lastbytes = random.data() + random.size() - downgrade_signal_length;
60 store_be(DOWNGRADE_TLS12, lastbytes);
73class Server_Hello_Internal {
78 explicit Server_Hello_Internal(
const std::vector<uint8_t>& buf) {
80 throw Decoding_Error(
"Server_Hello: Packet corrupted");
83 TLS_Data_Reader reader(
"ServerHello", buf);
85 const uint8_t major_version = reader.get_byte();
86 const uint8_t minor_version = reader.get_byte();
88 m_legacy_version = Protocol_Version(major_version, minor_version);
94 m_random = reader.get_fixed<uint8_t>(32);
95 m_is_hello_retry_request = random_signals_hello_retry_request(m_random);
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();
106 m_extensions.deserialize(
112 Server_Hello_Internal(Protocol_Version lv,
114 std::vector<uint8_t> r,
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),
125 Protocol_Version version()
const {
135 return (extensions().has<Supported_Versions>()) ? Protocol_Version::TLS_V13 : m_legacy_version;
138 Protocol_Version legacy_version()
const {
return m_legacy_version; }
140 const Session_ID& session_id()
const {
return m_session_id; }
142 const std::vector<uint8_t>& random()
const {
return m_random; }
144 uint16_t ciphersuite()
const {
return m_ciphersuite; }
146 uint8_t comp_method()
const {
return m_comp_method; }
148 bool is_hello_retry_request()
const {
return m_is_hello_retry_request; }
150 const Extensions& extensions()
const {
return m_extensions; }
152 Extensions& extensions() {
return m_extensions; }
155 Protocol_Version m_legacy_version;
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;
162 Extensions m_extensions;
176 std::vector<uint8_t> buf;
179 buf.push_back(
m_data->legacy_version().major_version());
180 buf.push_back(
m_data->legacy_version().minor_version());
188 buf.push_back(
m_data->comp_method());
200 return m_data->legacy_version();
208 return m_data->comp_method();
212 return m_data->session_id();
216 return m_data->ciphersuite();
220 return m_data->extensions().extension_types();
224 return m_data->extensions();
233 const std::vector<uint8_t>& reneg_info,
238 server_settings.protocol_version(),
240 make_server_hello_random(rng, server_settings.protocol_version(), cb, policy),
245 m_data->extensions().add(new Extended_Master_Secret);
250 m_data->extensions().add(new Certificate_Status_Request);
254 m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocol));
260 m_data->extensions().add(new Encrypt_then_MAC);
264 m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
268 m_data->extensions().add(new Renegotiation_Extension(reneg_info));
272 m_data->extensions().add(new Session_Ticket_Extension());
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();
279 if(!server_srtp.empty() && !client_srtp.empty()) {
282 for(auto s_srtp : server_srtp) {
283 for(auto c_srtp : client_srtp) {
284 if(shared == 0 && s_srtp == c_srtp) {
291 m_data->extensions().add(new SRTP_Protection_Profiles(shared));
299 hash.update(io.send(*
this));
308 const std::vector<uint8_t>& reneg_info,
310 const Session& resumed_session,
311 bool offer_session_ticket,
313 Server_Hello(std::make_unique<Server_Hello_Internal>(resumed_session.version(),
316 resumed_session.ciphersuite_code(),
320 m_data->extensions().add(new Extended_Master_Secret);
324 m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocol));
328 Ciphersuite c = resumed_session.ciphersuite();
329 if(c.cbc_ciphersuite()) {
330 m_data->extensions().add(new Encrypt_then_MAC);
334 if(resumed_session.ciphersuite().ecc_ciphersuite() &&
336 m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
339 if(client_hello.secure_renegotiation()) {
340 m_data->extensions().add(new Renegotiation_Extension(reneg_info));
343 if(client_hello.supports_session_ticket() && offer_session_ticket) {
344 m_data->extensions().add(new Session_Ticket_Extension());
350 hash.update(io.send(*
this));
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");
372 return reneg->renegotiation_info();
374 return std::vector<uint8_t>();
395 auto prof = srtp->profiles();
396 if(prof.size() != 1 || prof[0] == 0) {
397 throw Decoding_Error(
"Server sent malformed DTLS-SRTP extension");
407 return alpn->single_protocol();
414 return ecc_formats->prefers_compressed();
421 if(last8 == DOWNGRADE_TLS11) {
422 return Protocol_Version::TLS_V11;
424 if(last8 == DOWNGRADE_TLS12) {
425 return Protocol_Version::TLS_V12;
443 throw Decoding_Error(
"Server_Hello_Done: Must be empty, and is not");
451 return std::vector<uint8_t>();
454#if defined(BOTAN_HAS_TLS_13)
461 bool hello_retry_request_allowed,
477 const auto& offered_by_client = exts.get<
Key_Share>()->offered_groups();
484 if(selected_group == Named_Group::NONE) {
485 throw TLS_Exception(Alert::HandshakeFailure,
"Client did not offer any acceptable group");
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");
512 return Server_Hello_13(ch, selected_group, session_mgr, credentials_mgr, rng, cb, policy);
517 const std::vector<uint8_t>& buf) {
518 auto data = std::make_unique<Server_Hello_Internal>(buf);
519 const auto version = data->version();
522 if(version.is_pre_tls_13()) {
527 if(version == Protocol_Version::TLS_V13) {
528 if(data->is_hello_retry_request()) {
535 throw TLS_Exception(Alert::ProtocolVersion,
"unexpected server hello version: " + version.to_string());
568 throw TLS_Exception(Alert::DecodeError,
"compression is not supported in TLS 1.3");
574 throw TLS_Exception(Alert::MissingExtension,
"server hello did not contain 'supported version' extension");
582 throw TLS_Exception(Alert::IllegalParameter,
"TLS 1.3 Server Hello selected a different version");
587 Server_Hello_13::Server_Hello_Tag ) :
603 const std::set<Extension_Code> allowed = {
611 if(exts.contains_other_than(allowed)) {
612 throw TLS_Exception(Alert::UnsupportedExtension,
"Server Hello contained an extension that is not allowed");
620 throw TLS_Exception(Alert::MissingExtension,
"server hello must contain key exchange information");
625 Server_Hello_13::Hello_Retry_Request_Tag ) :
637 const std::set<Extension_Code> allowed = {
645 if(exts.contains_other_than(allowed)) {
647 "Hello Retry Request contained an extension that is not allowed");
654 throw TLS_Exception(Alert::IllegalParameter,
"Hello Retry Request does not request any changes to Client Hello");
659 Hello_Retry_Request_Creation_Tag ) :
670 std::swap(pref_list, other_list);
673 for(
auto suite_id : pref_list) {
704 throw TLS_Exception(Alert::HandshakeFailure,
"Can't agree on a ciphersuite with client");
709 std::optional<Named_Group> key_exchange_group,
719 choose_ciphersuite(ch, policy),
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));
740 if(ch_exts.has<
PSK>()) {
741 const auto cs = Ciphersuite::by_id(m_data->ciphersuite());
742 BOTAN_ASSERT_NOMSG(cs);
749 auto* const psk_modes = ch_exts.get<PSK_Key_Exchange_Modes>();
750 BOTAN_ASSERT_NONNULL(psk_modes);
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)) {
760 m_data->extensions().add(std::move(server_psk));
770 if(last8 == DOWNGRADE_TLS11) {
771 return Protocol_Version::TLS_V11;
773 if(last8 == DOWNGRADE_TLS12) {
774 return Protocol_Version::TLS_V12;
783 const auto& versions = versions_ext->versions();
785 return versions.front();
798 std::vector<uint8_t>(HELLO_RETRY_REQUEST_MARKER.begin(), HELLO_RETRY_REQUEST_MARKER.end()),
799 choose_ciphersuite(ch, policy),
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
virtual void tls_modify_extensions(Extensions &extn, Connection_Side which_side, Handshake_Type which_message)
static std::optional< Ciphersuite > by_id(uint16_t suite)
bool supports_encrypt_then_mac() const
bool supports_cert_status_message() const
bool secure_renegotiation() const
bool supports_extended_master_secret() const
bool supports_session_ticket() const
const Extensions & extensions() const
bool supports_alpn() 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
friend class Server_Hello_13
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
bool offer_session_ticket() const
std::optional< Protocol_Version > random_signals_downgrade() const
bool supports_session_ticket() const
bool supports_extended_master_secret() const
bool prefers_compressed_ec_points() const
Protocol_Version selected_version() const override
uint16_t srtp_profile() const
bool supports_encrypt_then_mac() const
bool supports_certificate_status_message() const
std::string next_protocol() const
std::vector< uint8_t > renegotiation_info() const
bool secure_renegotiation() 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
void basic_validation() 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
uint16_t ciphersuite() const
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
uint8_t compression_method() 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)
void append_tls_length_value(std::vector< uint8_t, Alloc > &buf, const T *vals, size_t vals_size, size_t tag_size)
std::vector< uint8_t > make_hello_random(RandomNumberGenerator &rng, Callbacks &cb, const Policy &policy)
Strong< std::vector< uint8_t >, struct Session_ID_ > Session_ID
holds a TLS 1.2 session ID for stateful resumption
constexpr uint8_t get_byte(T input)
bool value_exists(const std::vector< T > &vec, const V &val)
std::string to_string(ErrorType type)
Convert an ErrorType to string.
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)