Botan 3.11.0
Crypto and TLS for C&
msg_client_hello_13.cpp
Go to the documentation of this file.
1/*
2* TLS Client Hello Messages
3* (C) 2004-2011,2015,2016 Jack Lloyd
4* 2021 Elektrobit Automotive GmbH
5* 2022 René Meusel, Hannes Rantzsch - neXenio GmbH
6* 2026 René Meusel - Rohde & Schwarz Cybersecurity GmbH
7*
8* Botan is released under the Simplified BSD License (see license.txt)
9*/
10
11#include <botan/tls_messages_13.h>
12
13#include <botan/assert.h>
14#include <botan/tls_alert.h>
15#include <botan/tls_callbacks.h>
16#include <botan/tls_exceptn.h>
17#include <botan/tls_extensions_13.h>
18#include <botan/tls_policy.h>
19#include <botan/internal/tls_handshake_layer_13.h>
20#include <botan/internal/tls_messages_internal.h>
21#include <botan/internal/tls_transcript_hash_13.h>
22#include <algorithm>
23
24#if defined(BOTAN_HAS_TLS_12)
25 #include <botan/tls_extensions_12.h>
26#endif
27
28namespace Botan::TLS {
29
30Client_Hello_13::Client_Hello_13(std::unique_ptr<Client_Hello_Internal> data) : Client_Hello(std::move(data)) {
31 const auto& exts = m_data->extensions();
32
33 // RFC 8446 4.1.2
34 // TLS 1.3 ClientHellos are identified as having a legacy_version of
35 // 0x0303 and a "supported_versions" extension present with 0x0304 as the
36 // highest version indicated therein.
37 //
38 // Note that we already checked for "supported_versions" before entering this
39 // c'tor in `Client_Hello_13::parse()`. This is just to be doubly sure.
40 BOTAN_ASSERT_NOMSG(exts.has<Supported_Versions>());
41
42 // RFC 8446 4.2.1
43 // Servers MAY abort the handshake upon receiving a ClientHello with
44 // legacy_version 0x0304 or later.
45 if(m_data->legacy_version().is_tls_13_or_later()) {
46 throw TLS_Exception(Alert::DecodeError, "TLS 1.3 Client Hello has invalid legacy_version");
47 }
48
49 // RFC 8446 4.1.2
50 // For every TLS 1.3 ClientHello, [the compression method] MUST contain
51 // exactly one byte, set to zero, [...]. If a TLS 1.3 ClientHello is
52 // received with any other value in this field, the server MUST abort the
53 // handshake with an "illegal_parameter" alert.
54 if(m_data->comp_methods().size() != 1 || m_data->comp_methods().front() != 0) {
55 throw TLS_Exception(Alert::IllegalParameter, "Client did not offer NULL compression");
56 }
57
58 // RFC 8446 4.2.9
59 // A client MUST provide a "psk_key_exchange_modes" extension if it
60 // offers a "pre_shared_key" extension. If clients offer "pre_shared_key"
61 // without a "psk_key_exchange_modes" extension, servers MUST abort
62 // the handshake.
63 if(exts.has<PSK>()) {
64 if(!exts.has<PSK_Key_Exchange_Modes>()) {
65 throw TLS_Exception(Alert::MissingExtension,
66 "Client Hello offered a PSK without a psk_key_exchange_modes extension");
67 }
68
69 // RFC 8446 4.2.11
70 // The "pre_shared_key" extension MUST be the last extension in the
71 // ClientHello [...]. Servers MUST check that it is the last extension
72 // and otherwise fail the handshake with an "illegal_parameter" alert.
73 if(exts.all().back()->type() != Extension_Code::PresharedKey) {
74 throw TLS_Exception(Alert::IllegalParameter, "PSK extension was not at the very end of the Client Hello");
75 }
76 }
77
78 // RFC 8446 9.2
79 // [A TLS 1.3 ClientHello] message MUST meet the following requirements:
80 //
81 // - If not containing a "pre_shared_key" extension, it MUST contain
82 // both a "signature_algorithms" extension and a "supported_groups"
83 // extension.
84 //
85 // - If containing a "supported_groups" extension, it MUST also contain
86 // a "key_share" extension, and vice versa. An empty
87 // KeyShare.client_shares vector is permitted.
88 //
89 // Servers receiving a ClientHello which does not conform to these
90 // requirements MUST abort the handshake with a "missing_extension"
91 // alert.
92 if(!exts.has<PSK>()) {
93 if(!exts.has<Supported_Groups>() || !exts.has<Signature_Algorithms>()) {
94 throw TLS_Exception(
95 Alert::MissingExtension,
96 "Non-PSK Client Hello did not contain supported_groups and signature_algorithms extensions");
97 }
98 }
99 if(exts.has<Supported_Groups>() != exts.has<Key_Share>()) {
100 throw TLS_Exception(Alert::MissingExtension,
101 "Client Hello must either contain both key_share and supported_groups extensions or neither");
102 }
103
104 if(exts.has<Key_Share>()) {
105 auto* const supported_ext = exts.get<Supported_Groups>();
106 BOTAN_ASSERT_NONNULL(supported_ext);
107 const auto supports = supported_ext->groups();
108 const auto offers = exts.get<Key_Share>()->offered_groups();
109
110 // RFC 8446 4.2.8
111 // Each KeyShareEntry value MUST correspond to a group offered in the
112 // "supported_groups" extension and MUST appear in the same order.
113 // [...]
114 // Clients MUST NOT offer any KeyShareEntry values for groups not
115 // listed in the client's "supported_groups" extension.
116 //
117 // Note: We can assume that both `offers` and `supports` are unique lists
118 // as this is ensured in the parsing code of the extensions.
119 auto found_in_supported_groups = [&supports, support_offset = -1](auto group) mutable {
120 const auto i = std::find(supports.begin(), supports.end(), group);
121 if(i == supports.end()) {
122 return false;
123 }
124
125 const auto found_at = std::distance(supports.begin(), i);
126 if(found_at <= support_offset) {
127 return false; // The order that groups appear in "key_share" and
128 // "supported_groups" must be the same
129 }
130
131 support_offset = static_cast<decltype(support_offset)>(found_at);
132 return true;
133 };
134
135 for(const auto offered : offers) {
136 // RFC 8446 4.2.8
137 // Servers MAY check for violations of these rules and abort the
138 // handshake with an "illegal_parameter" alert if one is violated.
139 if(!found_in_supported_groups(offered)) {
140 throw TLS_Exception(Alert::IllegalParameter,
141 "Offered key exchange groups do not align with claimed supported groups");
142 }
143 }
144 }
145
146 // TODO: Reject oid_filters extension if found (which is the only known extension that
147 // must not occur in the TLS 1.3 client hello.
148 // RFC 8446 4.2.5
149 // [The oid_filters extension] MUST only be sent in the CertificateRequest message.
150}
151
152/*
153 * Create a new Client Hello message
154 */
156 Callbacks& cb,
158 std::string_view hostname,
159 const std::vector<std::string>& next_protocols,
160 std::optional<Session_with_Handle>& session,
161 std::vector<ExternalPSK> psks) {
162 // RFC 8446 4.1.2
163 // In TLS 1.3, the client indicates its version preferences in the
164 // "supported_versions" extension (Section 4.2.1) and the
165 // legacy_version field MUST be set to 0x0303, which is the version
166 // number for TLS 1.2.
167 m_data->m_legacy_version = Protocol_Version::TLS_V12;
168 m_data->m_random = make_hello_random(rng, cb, policy);
169 m_data->m_suites = policy.ciphersuite_list(Protocol_Version::TLS_V13);
170
171 if(policy.allow_tls12()) {
172 // Note: DTLS 1.3 is NYI, hence dtls_12 is not checked
173 const auto legacy_suites = policy.ciphersuite_list(Protocol_Version::TLS_V12);
174 m_data->m_suites.insert(m_data->m_suites.end(), legacy_suites.cbegin(), legacy_suites.cend());
175 }
176
178 // RFC 8446 4.1.2
179 // In compatibility mode (see Appendix D.4), this field MUST be non-empty,
180 // so a client not offering a pre-TLS 1.3 session MUST generate a new
181 // 32-byte value.
182 //
183 // Note: we won't ever offer a TLS 1.2 session. In such a case we would
184 // have instantiated a TLS 1.2 client in the first place.
185 m_data->m_session_id = Session_ID(make_hello_random(rng, cb, policy));
186 }
187
188 // NOLINTBEGIN(*-owning-memory)
190 m_data->extensions().add(new Server_Name_Indicator(hostname));
191 }
192
193 m_data->extensions().add(new Supported_Groups(policy.key_exchange_groups()));
194
195 m_data->extensions().add(new Key_Share(policy, cb, rng));
196
197 m_data->extensions().add(new Supported_Versions(Protocol_Version::TLS_V13, policy));
198
199 m_data->extensions().add(new Signature_Algorithms(policy.acceptable_signature_schemes()));
200 if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
201 // RFC 8446 4.2.3
202 // Implementations which have the same policy in both cases MAY omit
203 // the "signature_algorithms_cert" extension.
204 m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
205 }
206
207 // TODO: Support for PSK-only mode without a key exchange.
208 // This should be configurable in TLS::Policy and should allow no PSK
209 // support at all (e.g. to disable support for session resumption).
211
212 if(policy.support_cert_status_message()) {
213 m_data->extensions().add(new Certificate_Status_Request({}, {}));
214 }
215
216 // We currently support "record_size_limit" for TLS 1.3 exclusively. Hence,
217 // when TLS 1.2 is advertised as a supported protocol, we must not offer this
218 // extension.
219 if(policy.record_size_limit().has_value() && !policy.allow_tls12()) {
220 m_data->extensions().add(new Record_Size_Limit(policy.record_size_limit().value()));
221 }
222
223 if(!next_protocols.empty()) {
225 }
226
227 // RFC 7250 4.1
228 // In order to indicate the support of raw public keys, clients include
229 // the client_certificate_type and/or the server_certificate_type
230 // extensions in an extended client hello message.
233
234#if defined(BOTAN_HAS_TLS_12)
235 if(policy.allow_tls12()) {
236 m_data->extensions().add(new Renegotiation_Extension());
237 m_data->extensions().add(new Session_Ticket_Extension());
238
239 // EMS must always be used with TLS 1.2, regardless of the policy
240 m_data->extensions().add(new Extended_Master_Secret);
241
242 if(policy.negotiate_encrypt_then_mac()) {
243 m_data->extensions().add(new Encrypt_then_MAC);
244 }
245
246 if(m_data->extensions().has<Supported_Groups>() &&
247 !m_data->extensions().get<Supported_Groups>()->ec_groups().empty()) {
248 m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
249 }
250 }
251#endif
252
253 if(session.has_value() || !psks.empty()) {
254 m_data->extensions().add(new PSK(session, std::move(psks), cb));
255 }
256 // NOLINTEND(*-owning-memory)
257
259
260 if(m_data->extensions().has<PSK>()) {
261 // RFC 8446 4.2.11
262 // The "pre_shared_key" extension MUST be the last extension in the
263 // ClientHello (this facilitates implementation [...]).
264 if(m_data->extensions().all().back()->type() != Extension_Code::PresharedKey) {
265 throw TLS_Exception(Alert::InternalError,
266 "Application modified extensions of Client Hello, PSK is not last anymore");
267 }
268 calculate_psk_binders({});
269 }
270}
271
272std::variant<Client_Hello_13, Client_Hello_12_Shim> Client_Hello_13::parse(const std::vector<uint8_t>& buf) {
273 auto data = std::make_unique<Client_Hello_Internal>(buf);
274 const auto version = data->version();
275
276 if(version.is_pre_tls_13()) {
277 return Client_Hello_12_Shim(std::move(data));
278 } else {
279 return Client_Hello_13(std::move(data));
280 }
281}
282
284 const Transcript_Hash_State& transcript_hash_state,
285 Callbacks& cb,
287 BOTAN_STATE_CHECK(m_data->extensions().has<Supported_Groups>());
288 BOTAN_STATE_CHECK(m_data->extensions().has<Key_Share>());
289
290 auto* hrr_ks = hrr.extensions().get<Key_Share>();
291 const auto& supported_groups = m_data->extensions().get<Supported_Groups>()->groups();
292
293 if(hrr.extensions().has<Key_Share>()) {
294 m_data->extensions().get<Key_Share>()->retry_offer(*hrr_ks, supported_groups, cb, rng);
295 }
296
297 // RFC 8446 4.2.2
298 // When sending the new ClientHello, the client MUST copy
299 // the contents of the extension received in the HelloRetryRequest into
300 // a "cookie" extension in the new ClientHello.
301 //
302 // RFC 8446 4.2.2
303 // Clients MUST NOT use cookies in their initial ClientHello in subsequent
304 // connections.
305 if(hrr.extensions().has<Cookie>()) {
306 BOTAN_STATE_CHECK(!m_data->extensions().has<Cookie>());
307 m_data->extensions().add(new Cookie(hrr.extensions().get<Cookie>()->get_cookie())); // NOLINT(*-owning-memory)
308 }
309
310 // Note: the consumer of the TLS implementation won't be able to distinguish
311 // invocations to this callback due to the first Client_Hello or the
312 // retried Client_Hello after receiving a Hello_Retry_Request. We assume
313 // that the user keeps and detects this state themselves.
315
316 auto* psk = m_data->extensions().get<PSK>();
317 if(psk != nullptr) {
318 // Cipher suite should always be a known suite as this is checked upstream
319 const auto cipher = Ciphersuite::by_id(hrr.ciphersuite());
320 BOTAN_ASSERT_NOMSG(cipher.has_value());
321
322 // RFC 8446 4.1.4
323 // In [...] its updated ClientHello, the client SHOULD NOT offer
324 // any pre-shared keys associated with a hash other than that of the
325 // selected cipher suite.
326 psk->filter(cipher.value());
327
328 // RFC 8446 4.2.11.2
329 // If the server responds with a HelloRetryRequest and the client
330 // then sends ClientHello2, its binder will be computed over: [...].
331 calculate_psk_binders(transcript_hash_state.clone());
332 }
333}
334
336 // RFC 8446 4.1.2
337 // The client will also send a ClientHello when the server has responded
338 // to its ClientHello with a HelloRetryRequest. In that case, the client
339 // MUST send the same ClientHello without modification, except as follows:
340
341 if(m_data->session_id() != new_ch.m_data->session_id() || m_data->random() != new_ch.m_data->random() ||
342 m_data->ciphersuites() != new_ch.m_data->ciphersuites() ||
343 m_data->comp_methods() != new_ch.m_data->comp_methods()) {
344 throw TLS_Exception(Alert::IllegalParameter, "Client Hello core values changed after Hello Retry Request");
345 }
346
347 const auto oldexts = extension_types();
348 const auto newexts = new_ch.extension_types();
349
350 // Check that extension omissions are justified
351 for(const auto oldext : oldexts) {
352 if(!newexts.contains(oldext)) {
353 auto* const ext = extensions().get(oldext);
354
355 // We don't make any assumptions about unimplemented extensions.
356 if(!ext->is_implemented()) {
357 continue;
358 }
359
360 // RFC 8446 4.1.2
361 // Removing the "early_data" extension (Section 4.2.10) if one was
362 // present. Early data is not permitted after a HelloRetryRequest.
363 if(oldext == EarlyDataIndication::static_type()) {
364 continue;
365 }
366
367 // RFC 8446 4.1.2
368 // Optionally adding, removing, or changing the length of the
369 // "padding" extension.
370 //
371 // TODO: implement the Padding extension
372 // if(oldext == Padding::static_type())
373 // continue;
374
375 throw TLS_Exception(Alert::IllegalParameter, "Extension removed in updated Client Hello");
376 }
377 }
378
379 // Check that extension additions are justified
380 for(const auto newext : newexts) {
381 if(!oldexts.contains(newext)) {
382 auto* const ext = new_ch.extensions().get(newext);
383
384 // We don't make any assumptions about unimplemented extensions.
385 if(!ext->is_implemented()) {
386 continue;
387 }
388
389 // RFC 8446 4.1.2
390 // Including a "cookie" extension if one was provided in the
391 // HelloRetryRequest.
392 if(newext == Cookie::static_type()) {
393 continue;
394 }
395
396 // RFC 8446 4.1.2
397 // Optionally adding, removing, or changing the length of the
398 // "padding" extension.
399 //
400 // TODO: implement the Padding extension
401 // if(newext == Padding::static_type())
402 // continue;
403
404 throw TLS_Exception(Alert::UnsupportedExtension, "Added an extension in updated Client Hello");
405 }
406 }
407
408 // RFC 8446 4.1.2
409 // Removing the "early_data" extension (Section 4.2.10) if one was
410 // present. Early data is not permitted after a HelloRetryRequest.
411 if(new_ch.extensions().has<EarlyDataIndication>()) {
412 throw TLS_Exception(Alert::IllegalParameter, "Updated Client Hello indicates early data");
413 }
414
415 // TODO: Contents of extensions are not checked for update compatibility, see:
416 //
417 // RFC 8446 4.1.2
418 // If a "key_share" extension was supplied in the HelloRetryRequest,
419 // replacing the list of shares with a list containing a single
420 // KeyShareEntry from the indicated group.
421 //
422 // Updating the "pre_shared_key" extension if present by recomputing
423 // the "obfuscated_ticket_age" and binder values and (optionally)
424 // removing any PSKs which are incompatible with the server's
425 // indicated cipher suite.
426 //
427 // Optionally adding, removing, or changing the length of the
428 // "padding" extension.
429}
430
431void Client_Hello_13::calculate_psk_binders(Transcript_Hash_State transcript_hash) {
432 auto* psk = m_data->extensions().get<PSK>();
433 if(psk == nullptr || psk->empty()) {
434 return;
435 }
436
437 // RFC 8446 4.2.11.2
438 // Each entry in the binders list is computed as an HMAC over a
439 // transcript hash (see Section 4.4.1) containing a partial ClientHello
440 // [...].
441 //
442 // Therefore we marshal the entire message prematurely to obtain the
443 // (truncated) transcript hash, calculate the PSK binders with it, update
444 // the Client Hello thus finalizing the message. Down the road, it will be
445 // re-marshalled with the correct binders and sent over the wire.
446 Handshake_Layer::prepare_message(*this, transcript_hash);
447 psk->calculate_binders(transcript_hash);
448}
449
450std::optional<Protocol_Version> Client_Hello_13::highest_supported_version(const Policy& policy) const {
451 // RFC 8446 4.2.1
452 // The "supported_versions" extension is used by the client to indicate
453 // which versions of TLS it supports and by the server to indicate which
454 // version it is using. The extension contains a list of supported
455 // versions in preference order, with the most preferred version first.
456 auto* const supvers = m_data->extensions().get<Supported_Versions>();
457 BOTAN_ASSERT_NONNULL(supvers);
458
459 std::optional<Protocol_Version> result;
460
461 for(const auto& v : supvers->versions()) {
462 // RFC 8446 4.2.1
463 // Servers MUST only select a version of TLS present in that extension
464 // and MUST ignore any unknown versions that are present in that
465 // extension.
466 if(!v.known_version() || !policy.acceptable_protocol_version(v)) {
467 continue;
468 }
469
470 result = (result.has_value()) ? std::optional(std::max(result.value(), v)) : std::optional(v);
471 }
472
473 return result;
474}
475
476} // namespace Botan::TLS
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
virtual void tls_modify_extensions(Extensions &extn, Connection_Side which_side, Handshake_Type which_message)
static std::optional< Ciphersuite > by_id(uint16_t suite)
void validate_updates(const Client_Hello_13 &new_ch)
static std::variant< Client_Hello_13, Client_Hello_12_Shim > parse(const std::vector< uint8_t > &buf)
std::optional< Protocol_Version > highest_supported_version(const Policy &policy) const
Client_Hello_13(const Policy &policy, Callbacks &cb, RandomNumberGenerator &rng, std::string_view hostname, const std::vector< std::string > &next_protocols, std::optional< Session_with_Handle > &session, std::vector< ExternalPSK > psks)
void retry(const Hello_Retry_Request &hrr, const Transcript_Hash_State &transcript_hash_state, Callbacks &cb, RandomNumberGenerator &rng)
const Extensions & extensions() const
std::unique_ptr< Client_Hello_Internal > m_data
std::set< Extension_Code > extension_types() const
std::vector< std::string > next_protocols() const
Handshake_Type type() const override
const std::vector< uint8_t > & get_cookie() const
static Extension_Code static_type()
static Extension_Code static_type()
virtual bool allow_tls12() const
virtual std::vector< uint16_t > ciphersuite_list(Protocol_Version version) const
virtual std::vector< Certificate_Type > accepted_server_certificate_types() const
virtual std::vector< Certificate_Type > accepted_client_certificate_types() const
virtual std::vector< Group_Params > key_exchange_groups() const
virtual bool tls_13_middlebox_compatibility_mode() const
virtual bool negotiate_encrypt_then_mac() const
virtual bool acceptable_protocol_version(Protocol_Version version) const
virtual bool support_cert_status_message() const
virtual std::optional< std::vector< Signature_Scheme > > acceptable_certificate_signature_schemes() const
virtual std::vector< Signature_Scheme > acceptable_signature_schemes() const
virtual bool use_ecc_point_compression() const
virtual std::optional< uint16_t > record_size_limit() const
const Extensions & extensions() const
static bool hostname_acceptable_for_sni(std::string_view hostname)
std::vector< Group_Params > ec_groups() const
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