12#include <botan/tls_messages.h>
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>
32const uint64_t DOWNGRADE_TLS11 = 0x444F574E47524400;
33const uint64_t DOWNGRADE_TLS12 = 0x444F574E47524401;
36const std::vector<uint8_t> HELLO_RETRY_REQUEST_MARKER = {
37 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
38 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E, 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33, 0x9C};
40bool random_signals_hello_retry_request(
const std::vector<uint8_t>& random) {
41 return CT::is_equal(random.data(), HELLO_RETRY_REQUEST_MARKER.data(), HELLO_RETRY_REQUEST_MARKER.size()).as_bool();
44std::vector<uint8_t> make_server_hello_random(RandomNumberGenerator& rng,
45 Protocol_Version offered_version,
47 const Policy& policy) {
59 if(offered_version.is_pre_tls_13() && policy.allow_tls13()) {
60 constexpr size_t downgrade_signal_length =
sizeof(DOWNGRADE_TLS12);
62 auto lastbytes = random.data() + random.size() - downgrade_signal_length;
63 store_be(DOWNGRADE_TLS12, lastbytes);
76class Server_Hello_Internal {
81 Server_Hello_Internal(
const std::vector<uint8_t>& buf) {
83 throw Decoding_Error(
"Server_Hello: Packet corrupted");
86 TLS_Data_Reader reader(
"ServerHello", buf);
88 const uint8_t major_version = reader.get_byte();
89 const uint8_t minor_version = reader.get_byte();
91 m_legacy_version = Protocol_Version(major_version, minor_version);
97 m_random = reader.get_fixed<uint8_t>(32);
98 m_is_hello_retry_request = random_signals_hello_retry_request(m_random);
100 m_session_id =
Session_ID(reader.get_range<uint8_t>(1, 0, 32));
101 m_ciphersuite = reader.get_uint16_t();
102 m_comp_method = reader.get_byte();
115 Server_Hello_Internal(Protocol_Version lv,
117 std::vector<uint8_t> r,
120 bool is_hrr =
false) :
121 m_legacy_version(lv),
122 m_session_id(std::move(sid)),
123 m_random(std::move(r)),
124 m_is_hello_retry_request(is_hrr),
128 Protocol_Version version()
const {
138 return (extensions().has<Supported_Versions>()) ? Protocol_Version::TLS_V13 : m_legacy_version;
141 Protocol_Version legacy_version()
const {
return m_legacy_version; }
143 const Session_ID& session_id()
const {
return m_session_id; }
145 const std::vector<uint8_t>& random()
const {
return m_random; }
147 uint16_t ciphersuite()
const {
return m_ciphersuite; }
149 uint8_t comp_method()
const {
return m_comp_method; }
151 bool is_hello_retry_request()
const {
return m_is_hello_retry_request; }
153 const Extensions& extensions()
const {
return m_extensions; }
155 Extensions& extensions() {
return m_extensions; }
158 Protocol_Version m_legacy_version;
160 std::vector<uint8_t> m_random;
161 bool m_is_hello_retry_request;
162 uint16_t m_ciphersuite;
163 uint8_t m_comp_method;
165 Extensions m_extensions;
179 std::vector<uint8_t> buf;
182 buf.push_back(m_data->legacy_version().major_version());
183 buf.push_back(m_data->legacy_version().minor_version());
191 buf.push_back(m_data->comp_method());
203 return m_data->legacy_version();
211 return m_data->comp_method();
215 return m_data->session_id();
219 return m_data->ciphersuite();
223 return m_data->extensions().extension_types();
227 return m_data->extensions();
236 const std::vector<uint8_t>& reneg_info,
239 std::string_view next_protocol) :
241 server_settings.protocol_version(),
242 server_settings.session_id(),
243 make_server_hello_random(rng, server_settings.protocol_version(), cb, policy),
244 server_settings.ciphersuite(),
247 m_data->extensions().add(new Extended_Master_Secret);
252 m_data->extensions().add(new Certificate_Status_Request);
256 m_data->extensions().add(new Application_Layer_Protocol_Notification(next_protocol));
262 m_data->extensions().add(new Encrypt_then_MAC);
266 m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
270 m_data->extensions().add(new Renegotiation_Extension(reneg_info));
274 m_data->extensions().add(new Session_Ticket_Extension());
277 if(
m_data->legacy_version().is_datagram_protocol()) {
278 const std::vector<uint16_t> server_srtp = policy.srtp_profiles();
279 const std::vector<uint16_t> client_srtp = client_hello.srtp_profiles();
281 if(!server_srtp.empty() && !client_srtp.empty()) {
284 for(auto s_srtp : server_srtp) {
285 for(auto c_srtp : client_srtp) {
286 if(shared == 0 && s_srtp == c_srtp) {
293 m_data->extensions().add(new SRTP_Protection_Profiles(shared));
300 hash.update(io.send(*
this));
309 const std::vector<uint8_t>& reneg_info,
311 const Session& resumed_session,
312 bool offer_session_ticket,
313 std::string_view next_protocol) :
314 Server_Hello(std::make_unique<Server_Hello_Internal>(resumed_session.version(),
315 client_hello.session_id(),
317 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());
349 hash.update(io.send(*
this));
356 if(!
m_data->version().is_pre_tls_13()) {
357 throw TLS_Exception(Alert::ProtocolVersion,
"Expected server hello of (D)TLS 1.2 or lower");
371 return reneg->renegotiation_info();
373 return std::vector<uint8_t>();
394 auto prof = srtp->profiles();
395 if(prof.size() != 1 || prof[0] == 0) {
396 throw Decoding_Error(
"Server sent malformed DTLS-SRTP extension");
406 return alpn->single_protocol();
413 return ecc_formats->prefers_compressed();
420 if(last8 == DOWNGRADE_TLS11) {
421 return Protocol_Version::TLS_V11;
423 if(last8 == DOWNGRADE_TLS12) {
424 return Protocol_Version::TLS_V12;
442 throw Decoding_Error(
"Server_Hello_Done: Must be empty, and is not");
449std::vector<uint8_t> Server_Hello_Done::serialize()
const {
450 return std::vector<uint8_t>();
453#if defined(BOTAN_HAS_TLS_13)
460 bool hello_retry_request_allowed,
476 const auto& offered_by_client = exts.get<
Key_Share>()->offered_groups();
483 if(selected_group == Named_Group::NONE) {
484 throw TLS_Exception(Alert::HandshakeFailure,
"Client did not offer any acceptable group");
490 if(!
value_exists(supported_by_client, selected_group)) {
491 throw TLS_Exception(Alert::InternalError,
"Application selected a group that is not supported by the client");
511 return Server_Hello_13(ch, selected_group, session_mgr, credentials_mgr, rng, cb, policy);
516 const std::vector<uint8_t>& buf) {
517 auto data = std::make_unique<Server_Hello_Internal>(buf);
518 const auto version = data->version();
521 if(version.is_pre_tls_13()) {
526 if(version == Protocol_Version::TLS_V13) {
527 if(data->is_hello_retry_request()) {
534 throw TLS_Exception(Alert::ProtocolVersion,
"unexpected server hello version: " + version.to_string());
567 throw TLS_Exception(Alert::DecodeError,
"compression is not supported in TLS 1.3");
573 throw TLS_Exception(Alert::MissingExtension,
"server hello did not contain 'supported version' extension");
581 throw TLS_Exception(Alert::IllegalParameter,
"TLS 1.3 Server Hello selected a different version");
601 const std::set<Extension_Code> allowed = {
609 if(exts.contains_other_than(allowed)) {
610 throw TLS_Exception(Alert::UnsupportedExtension,
"Server Hello contained an extension that is not allowed");
618 throw TLS_Exception(Alert::MissingExtension,
"server hello must contain key exchange information");
623 Server_Hello_13::Hello_Retry_Request_Tag) :
635 const std::set<Extension_Code> allowed = {
643 if(exts.contains_other_than(allowed)) {
645 "Hello Retry Request contained an extension that is not allowed");
652 throw TLS_Exception(Alert::IllegalParameter,
"Hello Retry Request does not request any changes to Client Hello");
667 std::swap(pref_list, other_list);
670 for(
auto suite_id : pref_list) {
701 throw TLS_Exception(Alert::HandshakeFailure,
"Can't agree on a ciphersuite with client");
706 std::optional<Named_Group> key_exchange_group,
716 choose_ciphersuite(ch, policy),
729 if(key_exchange_group.has_value()) {
730 BOTAN_ASSERT_NOMSG(ch.extensions().has<Key_Share>());
731 m_data->extensions().add(Key_Share::create_as_encapsulation(
732 key_exchange_group.value(), *ch.extensions().get<Key_Share>(), policy, cb, rng));
737 if(ch_exts.has<
PSK>()) {
738 const auto cs = Ciphersuite::by_id(m_data->ciphersuite());
739 BOTAN_ASSERT_NOMSG(cs);
746 const auto psk_modes = ch_exts.get<PSK_Key_Exchange_Modes>();
747 BOTAN_ASSERT_NONNULL(psk_modes);
751 if(value_exists(psk_modes->modes(), PSK_Key_Exchange_Mode::PSK_DHE_KE)) {
752 if(auto server_psk = ch_exts.get<PSK>()->select_offered_psk(
753 ch.sni_hostname(), cs.value(), session_mgr, credentials_mgr, cb, policy)) {
757 m_data->extensions().add(std::move(server_psk));
767 if(last8 == DOWNGRADE_TLS11) {
768 return Protocol_Version::TLS_V11;
770 if(last8 == DOWNGRADE_TLS12) {
771 return Protocol_Version::TLS_V12;
780 const auto& versions = versions_ext->versions();
782 return versions.front();
794 HELLO_RETRY_REQUEST_MARKER,
795 choose_ciphersuite(ch, policy),
799 as_new_hello_retry_request) {
#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 deserialize(TLS_Data_Reader &reader, Connection_Side from, Handshake_Type message_type)
void update(const uint8_t in[], size_t length)
virtual std::vector< uint8_t > send(const Handshake_Message &msg)=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
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::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 OT &val)
std::string to_string(ErrorType type)
Convert an ErrorType to string.
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)