Botan 3.14.1
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* 2026 René Meusel - Rohde & Schwarz Cybersecurity GmbH
9*
10* Botan is released under the Simplified BSD License (see license.txt)
11*/
12
13#include <botan/tls_messages.h>
14#include <botan/internal/tls_messages_internal.h>
15
16#include <botan/tls_alert.h>
17#include <botan/tls_exceptn.h>
18#include <botan/tls_policy.h>
19#include <botan/internal/ct_utils.h>
20#include <botan/internal/tls_reader.h>
21
22namespace Botan::TLS {
23
25 Protocol_Version offered_version,
26 Callbacks& cb,
27 const Policy& policy,
28 TLS_Flavor flavor) {
29 auto random = make_hello_random(rng, cb, policy);
30
31 // RFC 8446 4.1.3
32 // TLS 1.3 has a downgrade protection mechanism embedded in the server's
33 // random value. TLS 1.3 servers which negotiate TLS 1.2 or below in
34 // response to a ClientHello MUST set the last 8 bytes of their Random
35 // value specially in their ServerHello.
36 //
37 // If negotiating TLS 1.2, TLS 1.3 servers MUST set the last 8 bytes of
38 // their Random value to the bytes: [DOWNGRADE_TLS12]
39 //
40 // DTLS 1.3 is not implemented, so a DTLS server never sets these.
41 if(flavor == TLS_Flavor::TLS && offered_version.is_pre_tls_13() && policy.allow_tls13()) {
42 constexpr size_t downgrade_signal_length = sizeof(DOWNGRADE_TLS12);
43 BOTAN_ASSERT_NOMSG(random.size() >= downgrade_signal_length);
44 const auto lastbytes = std::span{random}.last(downgrade_signal_length);
45 store_be(DOWNGRADE_TLS12, lastbytes);
46 }
47
48 return random;
49}
50
51Server_Hello_Internal::Server_Hello_Internal(std::span<const uint8_t> buf) {
52 if(buf.size() < 38) {
53 throw Decoding_Error("Server_Hello: Packet corrupted");
54 }
55
56 TLS_Data_Reader reader("ServerHello", buf);
57
58 const uint8_t major_version = reader.get_byte();
59 const uint8_t minor_version = reader.get_byte();
60
61 m_legacy_version = Protocol_Version(major_version, minor_version);
62
63 // RFC 8446 4.1.3
64 // Upon receiving a message with type server_hello, implementations MUST
65 // first examine the Random value and, if it matches this value, process
66 // it as described in Section 4.1.4 [Hello Retry Request]).
67 m_random = reader.get_fixed<uint8_t>(32);
68 m_is_hello_retry_request = CT::is_equal<uint8_t>(m_random, HELLO_RETRY_REQUEST_MARKER).as_bool();
69
70 m_session_id = Session_ID(reader.get_range<uint8_t>(1, 0, 32));
71 m_ciphersuite = reader.get_uint16_t();
72 m_comp_method = reader.get_byte();
73
74 // Note that this code path might parse a TLS 1.2 (or older) server hello message that
75 // is nevertheless marked as being a 'hello retry request' (potentially maliciously).
76 // Extension parsing will however not be affected by the associated flag.
77 // Only after parsing the extensions will the upstream code be able to decide
78 // whether we're dealing with TLS 1.3 or older.
79 m_extensions.deserialize(reader,
82}
83
85 // RFC 8446 4.2.1
86 // A server which negotiates a version of TLS prior to TLS 1.3 MUST set
87 // ServerHello.version and MUST NOT send the "supported_versions"
88 // extension. A server which negotiates TLS 1.3 MUST respond by sending
89 // a "supported_versions" extension containing the selected version
90 // value (0x0304).
91 //
92 // Note: Here we just take a message parsing decision, further validation of
93 // the extension's contents is done later.
94 return (extensions().has<Supported_Versions>()) ? Protocol_Version::TLS_V13 : m_legacy_version;
95}
96
97Server_Hello::Server_Hello(std::unique_ptr<Server_Hello_Internal> data) : m_data(std::move(data)) {}
98
99Server_Hello::Server_Hello(Server_Hello&&) noexcept = default;
100Server_Hello& Server_Hello::operator=(Server_Hello&&) noexcept = default;
101
102Server_Hello::~Server_Hello() = default;
103
104/*
105* Serialize a Server Hello message
106*/
107std::vector<uint8_t> Server_Hello::serialize() const {
108 std::vector<uint8_t> buf;
109 buf.reserve(1024); // working around GCC warning
110
111 buf.push_back(m_data->legacy_version().major_version());
112 buf.push_back(m_data->legacy_version().minor_version());
113 buf += m_data->random();
114
115 append_tls_length_value(buf, m_data->session_id().get(), 1);
116
117 buf.push_back(get_byte<0>(m_data->ciphersuite()));
118 buf.push_back(get_byte<1>(m_data->ciphersuite()));
119
120 buf.push_back(m_data->comp_method());
121
122 buf += m_data->extensions().serialize(Connection_Side::Server);
123
124 return buf;
125}
126
130
132 return m_data->legacy_version();
133}
134
135const std::vector<uint8_t>& Server_Hello::random() const {
136 return m_data->random();
137}
138
140 return m_data->comp_method();
141}
142
144 return m_data->session_id();
145}
146
148 return m_data->ciphersuite();
149}
150
151std::set<Extension_Code> Server_Hello::extension_types() const {
152 return m_data->extensions().extension_types();
153}
154
156 return m_data->extensions();
157}
158
159Server_Hello_12_Shim::Server_Hello_12_Shim(std::span<const uint8_t> buf) :
160 Server_Hello_12_Shim(std::make_unique<Server_Hello_Internal>(buf)) {}
161
162Server_Hello_12_Shim::Server_Hello_12_Shim(std::unique_ptr<Server_Hello_Internal> data) :
163 Server_Hello(std::move(data)) {
164 if(!m_data->version().is_pre_tls_13()) {
165 throw TLS_Exception(Alert::ProtocolVersion, "Expected server hello of (D)TLS 1.2 or lower");
166 }
167}
168
172
173std::optional<Protocol_Version> Server_Hello_12_Shim::random_signals_downgrade() const {
174 const uint64_t last8 = load_be<uint64_t>(m_data->random().data(), 3);
175 if(last8 == DOWNGRADE_TLS11) {
176 return Protocol_Version::TLS_V11;
177 }
178 if(last8 == DOWNGRADE_TLS12) {
179 return Protocol_Version::TLS_V12;
180 }
181
182 return std::nullopt;
183}
184
185} // namespace Botan::TLS
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
virtual bool allow_tls13() const
Server_Hello_12_Shim(std::span< const uint8_t > buf)
Protocol_Version selected_version() const final
std::optional< Protocol_Version > random_signals_downgrade() const
Server_Hello_Internal(std::span< const uint8_t > buf)
const Extensions & extensions() const
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
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:115
std::vector< T > get_fixed(size_t size)
Definition tls_reader.h:134
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:803
std::vector< uint8_t > make_server_hello_random(RandomNumberGenerator &rng, Protocol_Version offered_version, Callbacks &cb, const Policy &policy, TLS_Flavor flavor)
constexpr uint64_t DOWNGRADE_TLS12
Definition tls_magic.h:130
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:182
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 uint64_t DOWNGRADE_TLS11
Definition tls_magic.h:119
constexpr std::array< uint8_t, 32 > HELLO_RETRY_REQUEST_MARKER
Definition tls_magic.h:138
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504