Botan 3.14.1
Crypto and TLS for C&
msg_client_hello_12.cpp
Go to the documentation of this file.
1/*
2* TLS Hello Request and Client Hello Messages
3* (C) 2004-2011,2015,2016 Jack Lloyd
4* 2016 Matthias Gierlings
5* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
6*
7* Botan is released under the Simplified BSD License (see license.txt)
8*/
9
10#include <botan/tls_messages_12.h>
11
12#include <botan/tls_callbacks.h>
13#include <botan/tls_exceptn.h>
14#include <botan/tls_extensions_12.h>
15#include <botan/tls_policy.h>
16#include <botan/internal/stl_util.h>
17#include <botan/internal/tls_handshake_hash.h>
18#include <botan/internal/tls_handshake_io.h>
19#include <botan/internal/tls_messages_internal.h>
20
21namespace Botan::TLS {
22
23namespace {
24
25std::vector<Signature_Scheme> tls12_signature_schemes(const Policy& policy) {
26 std::vector<Signature_Scheme> schemes;
27 for(const auto scheme : policy.acceptable_signature_schemes()) {
28 if(scheme.is_compatible_with(Protocol_Version::TLS_V12)) {
29 schemes.push_back(scheme);
30 }
31 }
32 return schemes;
33}
34
35} // namespace
36
38 BOTAN_STATE_CHECK(m_data->legacy_version().is_datagram_protocol());
39
40 m_data->m_hello_cookie = hello_verify.cookie();
41}
42
44 if(const Supported_Point_Formats* ecc_formats = m_data->extensions().get<Supported_Point_Formats>()) {
45 return ecc_formats->prefers_compressed();
46 }
47 return false;
48}
49
51 return m_data->extensions().has<Renegotiation_Extension>();
52}
53
54std::vector<uint8_t> Client_Hello_12::renegotiation_info() const {
55 if(const Renegotiation_Extension* reneg = m_data->extensions().get<Renegotiation_Extension>()) {
56 return reneg->renegotiation_info();
57 }
58 return {};
59}
60
62 return m_data->extensions().has<Session_Ticket_Extension>();
63}
64
66 if(const auto* ticket = m_data->extensions().get<Session_Ticket_Extension>()) {
67 return ticket->contents();
68 }
69 return {};
70}
71
72std::optional<Session_Handle> Client_Hello_12::session_handle() const {
73 // RFC 5077 3.4
74 // If a ticket is presented by the client, the server MUST NOT attempt
75 // to use the Session ID in the ClientHello for stateful session
76 // resumption.
77 if(const auto ticket = session_ticket(); !ticket.empty()) {
78 return Session_Handle(ticket);
79 } else if(const auto& id = session_id(); !id.empty()) {
80 return Session_Handle(id);
81 } else {
82 return std::nullopt;
83 }
84}
85
87 return m_data->extensions().has<Extended_Master_Secret>();
88}
89
91 return m_data->extensions().has<Certificate_Status_Request>();
92}
93
95 return m_data->extensions().has<Encrypt_then_MAC>();
96}
97
98void Client_Hello_12::add_tls12_supported_groups_extensions(const Policy& policy) {
99 // RFC 7919 3.
100 // A client that offers a group MUST be able and willing to perform a DH
101 // key exchange using that group.
102 //
103 // We don't support hybrid key exchange in TLS 1.2
104 auto supported_groups = std::make_unique<Supported_Groups>(
105 groups_usable_in_version(policy.key_exchange_groups(), Protocol_Version::TLS_V12));
106
107 if(!supported_groups->ec_groups().empty()) {
108 // NOLINTNEXTLINE(*-owning-memory)
109 m_data->extensions().add(new Supported_Point_Formats(policy.use_ecc_point_compression()));
110 }
111
112 m_data->extensions().add(std::move(supported_groups));
113}
114
115/*
116* Create a new Client Hello message
117*/
119 Handshake_Hash& hash,
120 const Policy& policy,
121 Callbacks& cb,
123 std::vector<uint8_t> reneg_info,
124 const Client_Hello_12::Settings& client_settings,
125 std::vector<std::string> next_protocols) {
126 m_data->m_legacy_version = client_settings.protocol_version();
127 m_data->m_random = make_hello_random(rng, cb, policy);
128 m_data->m_suites = policy.ciphersuite_list(client_settings.protocol_version());
129
130 if(!policy.acceptable_protocol_version(m_data->legacy_version())) {
131 throw Internal_Error("Offering " + m_data->legacy_version().to_string() +
132 " but our own policy does not accept it");
133 }
134
135 /*
136 * Place all empty extensions in front to avoid a bug in some systems
137 * which reject hellos when the last extension in the list is empty.
138 */
139
140 // NOLINTBEGIN(*-owning-memory)
141
142 // EMS must always be used with TLS 1.2, regardless of the policy used.
143
144 m_data->extensions().add(new Extended_Master_Secret);
145
146 if(policy.negotiate_encrypt_then_mac()) {
147 m_data->extensions().add(new Encrypt_then_MAC);
148 }
149
150 m_data->extensions().add(new Session_Ticket_Extension());
151
152 const bool has_reneg_info = !reneg_info.empty();
153 m_data->extensions().add(new Renegotiation_Extension(std::move(reneg_info)));
154
155 m_data->extensions().add(new Supported_Versions(m_data->legacy_version(), policy));
156
158 m_data->extensions().add(new Server_Name_Indicator(client_settings.hostname()));
159 }
160
161 if(policy.support_cert_status_message()) {
162 m_data->extensions().add(new Certificate_Status_Request({}, {}));
163 }
164
165 add_tls12_supported_groups_extensions(policy);
166
167 m_data->extensions().add(new Signature_Algorithms(tls12_signature_schemes(policy)));
168 if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
169 // RFC 8446 4.2.3
170 // TLS 1.2 implementations SHOULD also process this extension.
171 // Implementations which have the same policy in both cases MAY omit
172 // the "signature_algorithms_cert" extension.
173 m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
174 }
175
176 if(!has_reneg_info && !next_protocols.empty()) {
177 m_data->extensions().add(new Application_Layer_Protocol_Notification(std::move(next_protocols)));
178 }
179
180 if(m_data->legacy_version().is_datagram_protocol()) {
181 m_data->extensions().add(new SRTP_Protection_Profiles(policy.srtp_profiles()));
182 }
183
184 // NOLINTEND(*-owning-memory)
185
187
188 hash.update(io.send(*this));
189}
190
191/*
192* Create a new Client Hello message (session resumption case)
193*/
195 Handshake_Hash& hash,
196 const Policy& policy,
197 Callbacks& cb,
199 std::vector<uint8_t> reneg_info,
200 const Session_with_Handle& session,
201 std::vector<std::string> next_protocols) {
202 m_data->m_legacy_version = session.session.version();
203 m_data->m_random = make_hello_random(rng, cb, policy);
204
205 // RFC 5077 3.4
206 // When presenting a ticket, the client MAY generate and include a
207 // Session ID in the TLS ClientHello. [...] If a ticket is presented by
208 // the client, the server MUST NOT attempt to use the Session ID in the
209 // ClientHello for stateful session resumption.
210 m_data->m_session_id = session.handle.id().value_or(Session_ID(make_hello_random(rng, cb, policy)));
211 m_data->m_suites = policy.ciphersuite_list(m_data->legacy_version());
212
213 if(!policy.acceptable_protocol_version(session.session.version())) {
214 throw Internal_Error("Offering " + m_data->legacy_version().to_string() +
215 " but our own policy does not accept it");
216 }
217
218 /*
219 * As EMS must always be used with TLS 1.2, add it even if it wasn't used
220 * in the original session. If the server understands it and follows the
221 * RFC it should reject our resume attempt and upgrade us to a new session
222 * with the EMS protection.
223 */
224 // NOLINTBEGIN(*-owning-memory)
225 m_data->extensions().add(new Extended_Master_Secret);
226
227 if(session.session.supports_encrypt_then_mac()) {
228 m_data->extensions().add(new Encrypt_then_MAC);
229 }
230
231 // An opaque handle that is not usable as a session ID must be a ticket
232 if(session.handle.is_ticket() || (session.handle.is_opaque_handle() && !session.handle.id().has_value())) {
233 m_data->extensions().add(new Session_Ticket_Extension(session.handle.ticket().value()));
234 }
235
236 const bool has_reneg_info = !reneg_info.empty();
237 m_data->extensions().add(new Renegotiation_Extension(std::move(reneg_info)));
238
239 const std::string hostname = session.session.server_info().hostname();
240
242 m_data->extensions().add(new Server_Name_Indicator(hostname));
243 }
244
245 if(policy.support_cert_status_message()) {
246 m_data->extensions().add(new Certificate_Status_Request({}, {}));
247 }
248
249 add_tls12_supported_groups_extensions(policy);
250
251 m_data->extensions().add(new Signature_Algorithms(tls12_signature_schemes(policy)));
252 if(auto cert_signing_prefs = policy.acceptable_certificate_signature_schemes()) {
253 // RFC 8446 4.2.3
254 // TLS 1.2 implementations SHOULD also process this extension.
255 // Implementations which have the same policy in both cases MAY omit
256 // the "signature_algorithms_cert" extension.
257 m_data->extensions().add(new Signature_Algorithms_Cert(std::move(cert_signing_prefs.value())));
258 }
259
260 if(!has_reneg_info && !next_protocols.empty()) {
261 m_data->extensions().add(new Application_Layer_Protocol_Notification(std::move(next_protocols)));
262 }
263
264 // Re-offer the SRTP profile negotiated in the original session so the
265 // abbreviated handshake keeps DTLS-SRTP keying (RFC 5764).
266 if(m_data->legacy_version().is_datagram_protocol() && session.session.dtls_srtp_profile() != 0) {
267 m_data->extensions().add(new SRTP_Protection_Profiles(session.session.dtls_srtp_profile()));
268 }
269
270 // NOLINTEND(*-owning-memory)
271
273
274 hash.update(io.send(*this));
275}
276
277Client_Hello_12::Client_Hello_12(std::span<const uint8_t> buf) :
278 Client_Hello_12(std::make_unique<Client_Hello_Internal>(buf)) {}
279
280#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
281
283 Client_Hello_12(std::move(client_hello.m_data)) {
284 hash.update(io.start_with_client_hello_from_downgrade(*this));
285}
286
287#endif
288
289Client_Hello_12::Client_Hello_12(std::unique_ptr<Client_Hello_Internal> data) : Client_Hello_12_Shim(std::move(data)) {
290 const uint16_t TLS_EMPTY_RENEGOTIATION_INFO_SCSV = 0x00FF;
291
292 if(offered_suite(static_cast<uint16_t>(TLS_EMPTY_RENEGOTIATION_INFO_SCSV))) {
293 if(const Renegotiation_Extension* reneg = m_data->extensions().get<Renegotiation_Extension>()) {
294 if(!reneg->renegotiation_info().empty()) {
295 throw TLS_Exception(Alert::HandshakeFailure, "Client sent renegotiation SCSV and non-empty extension");
296 }
297 } else {
298 // add fake extension
299 m_data->extensions().add(new Renegotiation_Extension()); // NOLINT(*-owning-memory)
300 }
301 }
302}
303
307
308Hello_Request::Hello_Request(std::span<const uint8_t> buf) {
309 if(!buf.empty()) {
310 throw Decoding_Error("Bad Hello_Request, has non-zero size");
311 }
312}
313
314std::vector<uint8_t> Hello_Request::serialize() const {
315 return std::vector<uint8_t>();
316}
317
318} // namespace Botan::TLS
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
virtual void tls_modify_extensions(Extensions &extn, Connection_Side which_side, Handshake_Type which_message)
const std::string & hostname() const
Protocol_Version protocol_version() const
void update_hello_cookie(const Hello_Verify_Request &hello_verify)
std::vector< uint8_t > renegotiation_info() const
Client_Hello_12(Handshake_IO &io, Handshake_Hash &hash, const Policy &policy, Callbacks &cb, RandomNumberGenerator &rng, std::vector< uint8_t > reneg_info, const Settings &client_settings, std::vector< std::string > next_protocols)
Session_Ticket session_ticket() const
std::optional< Session_Handle > session_handle() const
bool offered_suite(uint16_t ciphersuite) const
std::unique_ptr< Client_Hello_Internal > m_data
std::vector< std::string > next_protocols() const
const Session_ID & session_id() const
Handshake_Type type() const override
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
const std::vector< uint8_t > & cookie() const
virtual std::vector< uint16_t > ciphersuite_list(Protocol_Version version) const
virtual std::vector< Group_Params > key_exchange_groups() const
virtual bool negotiate_encrypt_then_mac() const
virtual bool acceptable_protocol_version(Protocol_Version version) const
virtual std::vector< uint16_t > srtp_profiles() const
virtual bool support_cert_status_message() const
virtual std::optional< std::vector< Signature_Scheme > > acceptable_certificate_signature_schemes() const
virtual bool use_ecc_point_compression() const
static bool hostname_acceptable_for_sni(std::string_view hostname)
uint16_t dtls_srtp_profile() const
Definition tls_session.h:94
Protocol_Version version() const
Definition tls_session.h:74
bool supports_encrypt_then_mac() const
const Server_Information & server_info() const
Helper class to embody a session handle in all protocol versions.
std::optional< Session_Ticket > ticket() const
std::optional< Session_ID > id() const
bool empty() const noexcept(noexcept(this->get().empty()))
std::vector< uint8_t > make_hello_random(RandomNumberGenerator &rng, Callbacks &cb, const Policy &policy)
std::vector< Group_Params > groups_usable_in_version(std::span< const Group_Params > groups, Protocol_Version version)
Strong< std::vector< uint8_t >, struct Session_ID_ > Session_ID
holds a TLS 1.2 session ID for stateful resumption
Strong< std::vector< uint8_t >, struct Session_Ticket_ > Session_Ticket
holds a TLS 1.2 session ticket for stateless resumption