Botan 3.12.0
Crypto and TLS for C&
msg_client_hello.cpp
Go to the documentation of this file.
1/*
2* TLS Client Hello Messages
3* (C) 2004-2011,2015,2016 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/hash.h>
17#include <botan/rng.h>
18#include <botan/tls_callbacks.h>
19#include <botan/tls_policy.h>
20#include <botan/internal/tls_reader.h>
21#include <algorithm>
22
23namespace Botan::TLS {
24
25std::vector<uint8_t> make_hello_random(RandomNumberGenerator& rng, Callbacks& cb, const Policy& policy) {
26 auto buf = rng.random_vec<std::vector<uint8_t>>(32);
27
28 if(policy.hash_hello_random()) {
29 auto sha256 = HashFunction::create_or_throw("SHA-256");
30 sha256->update(buf);
31 sha256->final(buf);
32 }
33
34 // TLS 1.3 does not require the insertion of a timestamp in the client hello
35 // random. When offering both TLS 1.2 and 1.3 we nevertheless comply with the
36 // legacy specification.
37 if(policy.include_time_in_hello_random() && (policy.allow_tls12() || policy.allow_dtls12())) {
38 const uint32_t time32 = static_cast<uint32_t>(std::chrono::system_clock::to_time_t(cb.tls_current_timestamp()));
39
40 store_be(time32, buf.data());
41 }
42
43 return buf;
44}
45
46Client_Hello_Internal::Client_Hello_Internal(const std::vector<uint8_t>& buf) {
47 /*
48 Minimum possible client hello
49
50 version: 2 bytes
51 random: 32 bytes
52 session_id len: 1 byte
53 ciphersuite_len: 2
54 ciphersuite (single): 2
55 compression_len: 1
56 compression (single): 1
57 */
58
59 constexpr size_t MinimumClientHelloBytes = 2 + 32 + 1 + 2 + 2 + 1 + 1;
60 if(buf.size() < MinimumClientHelloBytes) {
61 throw Decoding_Error("Client_Hello: Packet corrupted");
62 }
63
64 TLS_Data_Reader reader("ClientHello", buf);
65
66 const uint8_t major_version = reader.get_byte();
67 const uint8_t minor_version = reader.get_byte();
68
69 m_legacy_version = Protocol_Version(major_version, minor_version);
70
71 // DTLS has an additional 1 byte cookie length field
72 if(m_legacy_version.is_datagram_protocol() && buf.size() < MinimumClientHelloBytes + 1) {
73 throw Decoding_Error("Client_Hello: DTLS packet corrupted");
74 }
75
76 m_random = reader.get_fixed<uint8_t>(32);
77 m_session_id = Session_ID(reader.get_range<uint8_t>(1, 0, 32));
78
79 if(m_legacy_version.is_datagram_protocol()) {
80 auto sha256 = HashFunction::create_or_throw("SHA-256");
81 sha256->update(reader.get_data_read_so_far());
82
83 m_hello_cookie = reader.get_range<uint8_t>(1, 0, 255);
84
85 sha256->update(reader.get_remaining());
86 m_cookie_input_bits = sha256->final_stdvec();
87 }
88
89 m_suites = reader.get_range_vector<uint16_t>(2, 1, 32767);
90 m_comp_methods = reader.get_range_vector<uint8_t>(1, 1, 255);
91
93}
94
96 // RFC 8446 4.2.1
97 // If [the "supported_versions"] extension is not present, servers
98 // which are compliant with this specification and which also support
99 // TLS 1.2 MUST negotiate TLS 1.2 or prior as specified in [RFC5246],
100 // even if ClientHello.legacy_version is 0x0304 or later.
101 //
102 // RFC 8446 4.2.1
103 // Servers MUST be prepared to receive ClientHellos that include
104 // [the supported_versions] extension but do not include 0x0304 in
105 // the list of versions.
106 //
107 // RFC 8446 4.1.2
108 // TLS 1.3 ClientHellos are identified as having a legacy_version of
109 // 0x0303 and a supported_versions extension present with 0x0304 as
110 // the highest version indicated therein.
111 if(!extensions().has<Supported_Versions>() ||
112 !extensions().get<Supported_Versions>()->supports(Protocol_Version::TLS_V13)) {
113 // The exact legacy_version is ignored we just inspect it to
114 // distinguish TLS and DTLS.
115 return (m_legacy_version.is_datagram_protocol()) ? Protocol_Version::DTLS_V12 : Protocol_Version::TLS_V12;
116 }
117
118 // Note: The Client_Hello_13 class will make sure that legacy_version
119 // is exactly 0x0303 (aka ossified TLS 1.2)
120 return Protocol_Version::TLS_V13;
121}
122
123Client_Hello::Client_Hello(Client_Hello&&) noexcept = default;
124Client_Hello& Client_Hello::operator=(Client_Hello&&) noexcept = default;
125
126Client_Hello::~Client_Hello() = default;
127
129
130/*
131* Read a counterparty client hello
132*/
133Client_Hello::Client_Hello(std::unique_ptr<Client_Hello_Internal> data) : m_data(std::move(data)) {
135}
136
140
142 return m_data->legacy_version();
144
145const std::vector<uint8_t>& Client_Hello::random() const {
146 return m_data->random();
147}
148
150 return m_data->session_id();
151}
152
153const std::vector<uint8_t>& Client_Hello::compression_methods() const {
154 return m_data->comp_methods();
155}
156
157const std::vector<uint16_t>& Client_Hello::ciphersuites() const {
158 return m_data->ciphersuites();
159}
160
161std::set<Extension_Code> Client_Hello::extension_types() const {
162 return m_data->extensions().extension_types();
163}
164
166 return m_data->extensions();
167}
168
169/*
170* Serialize a Client Hello message
171*/
172std::vector<uint8_t> Client_Hello::serialize() const {
173 std::vector<uint8_t> buf;
174 buf.reserve(1024); // working around GCC warning
175
176 buf.push_back(m_data->legacy_version().major_version());
177 buf.push_back(m_data->legacy_version().minor_version());
178 buf += m_data->random();
179
180 append_tls_length_value(buf, m_data->session_id().get(), 1);
181
182 if(m_data->legacy_version().is_datagram_protocol()) {
183 append_tls_length_value(buf, m_data->hello_cookie(), 1);
184 }
185
186 append_tls_length_value(buf, m_data->ciphersuites(), 2);
187 append_tls_length_value(buf, m_data->comp_methods(), 1);
188
189 /*
190 * May not want to send extensions at all in some cases. If so,
191 * should include SCSV value (if reneg info is empty, if not we are
192 * renegotiating with a modern server)
193 */
194
195 buf += m_data->extensions().serialize(Connection_Side::Client);
196
197 return buf;
198}
199
200std::vector<uint8_t> Client_Hello::cookie_input_data() const {
201 BOTAN_STATE_CHECK(!m_data->hello_cookie_input_bits().empty());
202
203 return m_data->hello_cookie_input_bits();
204}
205
206/*
207* Check if we offered this ciphersuite
208*/
209bool Client_Hello::offered_suite(uint16_t ciphersuite) const {
210 return std::find(m_data->ciphersuites().cbegin(), m_data->ciphersuites().cend(), ciphersuite) !=
211 m_data->ciphersuites().cend();
212}
213
214std::vector<Signature_Scheme> Client_Hello::signature_schemes() const {
215 if(const Signature_Algorithms* sigs = m_data->extensions().get<Signature_Algorithms>()) {
216 return sigs->supported_schemes();
217 }
218 return {};
219}
220
221std::vector<Signature_Scheme> Client_Hello::certificate_signature_schemes() const {
222 // RFC 8446 4.2.3
223 // If no "signature_algorithms_cert" extension is present, then the
224 // "signature_algorithms" extension also applies to signatures appearing
225 // in certificates.
226 if(const Signature_Algorithms_Cert* sigs = m_data->extensions().get<Signature_Algorithms_Cert>()) {
227 return sigs->supported_schemes();
228 } else {
229 return signature_schemes();
230 }
231}
232
233std::vector<Group_Params> Client_Hello::supported_ecc_curves() const {
234 if(const Supported_Groups* groups = m_data->extensions().get<Supported_Groups>()) {
235 return groups->ec_groups();
236 }
237 return {};
238}
239
240std::vector<Group_Params> Client_Hello::supported_dh_groups() const {
241 if(const Supported_Groups* groups = m_data->extensions().get<Supported_Groups>()) {
242 return groups->dh_groups();
243 }
244 return std::vector<Group_Params>();
245}
246
247std::string Client_Hello::sni_hostname() const {
248 if(const Server_Name_Indicator* sni = m_data->extensions().get<Server_Name_Indicator>()) {
249 return sni->host_name();
250 }
251 return "";
252}
253
254std::vector<Protocol_Version> Client_Hello::supported_versions() const {
255 if(const Supported_Versions* versions = m_data->extensions().get<Supported_Versions>()) {
256 return versions->versions();
257 }
258 return {};
259}
260
262 return m_data->extensions().has<Application_Layer_Protocol_Notification>();
263}
264
266 return m_data->extensions().has<Signature_Algorithms>();
267}
268
269std::vector<std::string> Client_Hello::next_protocols() const {
270 if(auto* alpn = m_data->extensions().get<Application_Layer_Protocol_Notification>()) {
271 return alpn->protocols();
272 }
273 return {};
274}
275
276std::vector<uint16_t> Client_Hello::srtp_profiles() const {
277 if(const SRTP_Protection_Profiles* srtp = m_data->extensions().get<SRTP_Protection_Profiles>()) {
278 return srtp->profiles();
279 }
280 return {};
281}
282
283const std::vector<uint8_t>& Client_Hello::cookie() const {
284 return m_data->hello_cookie();
285}
286
287Client_Hello_12_Shim::Client_Hello_12_Shim(std::unique_ptr<Client_Hello_Internal> data) :
288 Client_Hello(std::move(data)) {}
289
290Client_Hello_12_Shim::Client_Hello_12_Shim(const std::vector<uint8_t>& buf) :
291 Client_Hello_12_Shim(std::make_unique<Client_Hello_Internal>(buf)) {}
292
293} // namespace Botan::TLS
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
void random_vec(std::span< uint8_t > v)
Definition rng.h:204
virtual std::chrono::system_clock::time_point tls_current_timestamp()
Client_Hello_12_Shim(const std::vector< uint8_t > &buf)
Client_Hello(const Client_Hello &)=delete
const Extensions & extensions() const
const std::vector< uint8_t > & cookie() const
std::string sni_hostname() const
std::vector< uint8_t > serialize() const override
const std::vector< uint8_t > & random() const
std::vector< Signature_Scheme > signature_schemes() const
const Extensions & extensions() const
bool offered_suite(uint16_t ciphersuite) const
std::unique_ptr< Client_Hello_Internal > m_data
std::vector< Group_Params > supported_ecc_curves() const
std::vector< Signature_Scheme > certificate_signature_schemes() const
const std::vector< uint16_t > & ciphersuites() const
std::vector< uint8_t > cookie_input_data() const
std::set< Extension_Code > extension_types() const
std::vector< Group_Params > supported_dh_groups() const
std::vector< std::string > next_protocols() const
const Session_ID & session_id() const
Protocol_Version legacy_version() const
const std::vector< uint8_t > & compression_methods() const
std::vector< uint16_t > srtp_profiles() const
Handshake_Type type() const override
std::vector< Protocol_Version > supported_versions() const
Client_Hello(const Client_Hello &)=delete
virtual bool include_time_in_hello_random() const
virtual bool allow_tls12() const
virtual bool hash_hello_random() const
virtual bool allow_dtls12() const
std::vector< uint8_t > get_remaining()
Definition tls_reader.h:41
std::vector< T > get_range(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:110
std::vector< T > get_fixed(size_t size)
Definition tls_reader.h:129
std::vector< uint8_t > get_data_read_so_far()
Definition tls_reader.h:46
std::vector< T > get_range_vector(size_t len_bytes, size_t min_elems, size_t max_elems)
Definition tls_reader.h:117
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:177
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 auto store_be(ParamTs &&... params)
Definition loadstor.h:745