Botan 3.0.0
Crypto and TLS for C&
tls_handshake_state.cpp
Go to the documentation of this file.
1/*
2* TLS Handshaking
3* (C) 2004-2006,2011,2012,2015,2016 Jack Lloyd
4* 2017 Harry Reimann, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/tls_handshake_state.h>
10#include <botan/internal/tls_record.h>
11#include <botan/tls_messages.h>
12#include <botan/tls_signature_scheme.h>
13#include <botan/kdf.h>
14#include <sstream>
15
16namespace Botan::TLS {
17
19 {
21 }
22
24 {
25 switch(type)
26 {
28 return "hello_verify_request";
29
31 return "hello_request";
32
34 return "client_hello";
35
37 return "server_hello";
38
40 return "hello_retry_request";
41
43 return "certificate";
44
46 return "certificate_url";
47
49 return "certificate_status";
50
52 return "server_key_exchange";
53
55 return "certificate_request";
56
58 return "server_hello_done";
59
61 return "certificate_verify";
62
64 return "client_key_exchange";
65
67 return "new_session_ticket";
68
70 return "change_cipher_spec";
71
73 return "finished";
74
76 return "end_of_early_data";
77
79 return "encrypted_extensions";
80
82 return "key_update";
83
85 return "invalid";
86 }
87
88 throw TLS_Exception(Alert::UnexpectedMessage,
89 "Unknown TLS handshake message type " +
90 std::to_string(static_cast<size_t>(type)));
91 }
92
93
94/*
95* Initialize the SSL/TLS Handshake State
96*/
98
99Handshake_State::Handshake_State(std::unique_ptr<Handshake_IO> io, Callbacks& cb) :
100 m_callbacks(cb),
101 m_handshake_io(std::move(io)),
102 m_version(m_handshake_io->initial_record_version())
103 {
104 }
105
107 {
108 m_callbacks.tls_inspect_handshake_msg(msg);
109 }
110
112 {
113 note_message(hello_verify);
114
115 m_client_hello->update_hello_cookie(hello_verify);
116 hash().reset();
117 hash().update(handshake_io().send(*m_client_hello));
118 note_message(*m_client_hello);
119 }
120
122 {
123 if(client_hello == nullptr)
124 {
125 m_client_hello.reset();
126 hash().reset();
127 }
128 else
129 {
130 m_client_hello.reset(client_hello);
131 note_message(*m_client_hello);
132 }
133 }
134
136 {
137 m_server_hello.reset(server_hello);
138 m_ciphersuite = Ciphersuite::by_id(m_server_hello->ciphersuite());
139 note_message(*m_server_hello);
140 }
141
143 {
144 m_server_certs.reset(server_certs);
145 note_message(*m_server_certs);
146 }
147
149 {
150 m_server_cert_status.reset(server_cert_status);
151 note_message(*m_server_cert_status);
152 }
153
155 {
156 m_server_kex.reset(server_kex);
157 note_message(*m_server_kex);
158 }
159
161 {
162 m_cert_req.reset(cert_req);
163 note_message(*m_cert_req);
164 }
165
167 {
168 m_server_hello_done.reset(server_hello_done);
169 note_message(*m_server_hello_done);
170 }
171
173 {
174 m_client_certs.reset(client_certs);
175 note_message(*m_client_certs);
176 }
177
179 {
180 m_client_kex.reset(client_kex);
181 note_message(*m_client_kex);
182 }
183
185 {
186 m_client_verify.reset(client_verify);
187 note_message(*m_client_verify);
188 }
189
191 {
192 m_server_verify.reset(server_verify);
193 note_message(*m_server_verify);
194 }
195
197 {
198 m_new_session_ticket.reset(new_session_ticket);
199 note_message(*m_new_session_ticket);
200 }
201
203 {
204 m_server_finished.reset(server_finished);
205 note_message(*m_server_finished);
206 }
207
209 {
210 m_client_finished.reset(client_finished);
211 note_message(*m_client_finished);
212 }
213
215 {
216 if(!m_ciphersuite.has_value())
217 {
218 throw Invalid_State("Cipher suite is not set");
219 }
220 return m_ciphersuite.value();
221 }
222
224 {
225 m_version = version;
226 }
227
229 {
230 m_session_keys = Session_Keys(this, client_kex()->pre_master_secret(), false);
231 }
232
234 {
235 m_session_keys = Session_Keys(this, resume_master_secret, true);
236 }
237
239 {
240 m_transitions.confirm_transition_to(handshake_msg);
241 }
242
244 {
245 m_transitions.set_expected_next(handshake_msg);
246 }
247
249 {
250 return m_transitions.received_handshake_msg(handshake_msg);
251 }
252
253std::pair<Handshake_Type, std::vector<uint8_t>>
255 {
256 return m_handshake_io->get_next_record(m_transitions.change_cipher_spec_expected());
257 }
258
260 {
261 if(new_session_ticket() && !new_session_ticket()->ticket().empty())
262 { return new_session_ticket()->ticket(); }
263
264 return client_hello()->session_ticket();
265 }
266
267std::unique_ptr<KDF> Handshake_State::protocol_specific_prf() const
268 {
269 const std::string prf_algo = ciphersuite().prf_algo();
270
271 if(prf_algo == "MD5" || prf_algo == "SHA-1")
272 { return KDF::create_or_throw("TLS-12-PRF(SHA-256)"); }
273
274 return KDF::create_or_throw("TLS-12-PRF(" + prf_algo + ")");
275 }
276
277std::pair<std::string, Signature_Format>
279 Signature_Scheme& chosen_scheme,
280 bool for_client_auth,
281 const Policy& policy) const
282 {
283 const std::string sig_algo = key.algo_name();
284
285 const std::vector<Signature_Scheme> allowed = policy.allowed_signature_schemes();
286
287 std::vector<Signature_Scheme> requested =
288 (for_client_auth) ? cert_req()->signature_schemes() : client_hello()->signature_schemes();
289
290 for(Signature_Scheme scheme : allowed)
291 {
292 if(!scheme.is_available())
293 {
294 continue;
295 }
296
297 if(scheme.algorithm_name() == sig_algo)
298 {
299 if(std::find(requested.begin(), requested.end(), scheme) != requested.end())
300 {
301 chosen_scheme = scheme;
302 break;
303 }
304 }
305 }
306
307 const std::string hash = chosen_scheme.hash_function_name();
308
309 if(!policy.allowed_signature_hash(hash))
310 {
311 throw TLS_Exception(Alert::HandshakeFailure,
312 "Policy refuses to accept signing with any hash supported by peer");
313 }
314
315 if(!chosen_scheme.format().has_value())
316 { throw Invalid_Argument(sig_algo + " is invalid/unknown for TLS signatures"); }
317
318 return std::make_pair(chosen_scheme.padding_string(), chosen_scheme.format().value());
319 }
320
321namespace {
322
323bool supported_algos_include(
324 const std::vector<Signature_Scheme>& schemes,
325 std::string_view key_type,
326 std::string_view hash_type)
327 {
328 for(Signature_Scheme scheme : schemes)
329 {
330 if(scheme.is_available() &&
331 hash_type == scheme.hash_function_name() &&
332 key_type == scheme.algorithm_name())
333 {
334 return true;
335 }
336 }
337
338 return false;
339 }
340
341}
342
343std::pair<std::string, Signature_Format>
345 Signature_Scheme scheme,
346 const std::vector<Signature_Scheme>& offered_schemes,
347 bool for_client_auth,
348 const Policy& policy) const
349 {
350 const std::string key_type = key.algo_name();
351
352 if(!policy.allowed_signature_method(key_type))
353 {
354 throw TLS_Exception(Alert::HandshakeFailure,
355 "Rejecting " + key_type + " signature");
356 }
357
358 if(!scheme.is_available())
359 {
360 throw TLS_Exception(Alert::HandshakeFailure,
361 "Peer sent unknown signature scheme");
362 }
363
364 if(key_type != scheme.algorithm_name())
365 { throw Decoding_Error("Counterparty sent inconsistent key and sig types"); }
366
367 if(for_client_auth && !cert_req())
368 {
369 throw TLS_Exception(Alert::HandshakeFailure,
370 "No certificate verify set");
371 }
372
373 /*
374 Confirm the signature type we just received against the
375 supported_algos list that we sent; it better be there.
376 */
377
378 const std::vector<Signature_Scheme> supported_algos =
379 for_client_auth ? cert_req()->signature_schemes() :
380 offered_schemes;
381
382 const std::string hash_algo = scheme.hash_function_name();
383
384 if(!scheme.is_compatible_with(Protocol_Version::TLS_V12))
385 { throw TLS_Exception(Alert::IllegalParameter, "Peer sent unexceptable signature scheme"); }
386
387 if(!supported_algos_include(supported_algos, key_type, hash_algo))
388 {
389 throw TLS_Exception(Alert::IllegalParameter,
390 "TLS signature extension did not allow for " +
391 key_type + "/" + hash_algo + " signature");
392 }
393
394 if(!scheme.format().has_value())
395 { throw Invalid_Argument(key_type + " is invalid/unknown for TLS signatures"); }
396
397 return std::make_pair(scheme.padding_string(), scheme.format().value());
398 }
399
400}
virtual std::string algo_name() const =0
static std::unique_ptr< KDF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition: kdf.cpp:213
virtual void tls_inspect_handshake_msg(const Handshake_Message &message)
const std::vector< Signature_Scheme > & signature_schemes() const
static std::optional< Ciphersuite > by_id(uint16_t suite)
std::string prf_algo() const
Session_Ticket session_ticket() const
std::vector< Signature_Scheme > signature_schemes() const
void update(const uint8_t in[], size_t length)
virtual Handshake_Type type() const =0
std::pair< std::string, Signature_Format > parse_sig_format(const Public_Key &key, Signature_Scheme scheme, const std::vector< Signature_Scheme > &offered_schemes, bool for_client_auth, const Policy &policy) const
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_handshake_msg()
const Server_Hello_Done * server_hello_done() const
void hello_verify_request(const Hello_Verify_Request &hello_verify)
void set_expected_next(Handshake_Type msg_type)
void note_message(const Handshake_Message &msg)
const Server_Key_Exchange * server_kex() const
const Certificate_Status * server_cert_status() const
const Certificate_Verify_12 * server_verify() const
Handshake_State(std::unique_ptr< Handshake_IO > io, Callbacks &callbacks)
const Certificate_Verify_12 * client_verify() const
const Finished_12 * server_finished() const
const Client_Hello_12 * client_hello() const
const Client_Key_Exchange * client_kex() const
void confirm_transition_to(Handshake_Type msg_type)
void set_version(const Protocol_Version &version)
const Certificate_12 * server_certs() const
const Certificate_12 * client_certs() const
const Finished_12 * client_finished() const
const Certificate_Request_12 * cert_req() const
const Ciphersuite & ciphersuite() const
Session_Ticket session_ticket() const
const Server_Hello_12 * server_hello() const
bool received_handshake_msg(Handshake_Type msg_type) const
std::unique_ptr< KDF > protocol_specific_prf() const
const New_Session_Ticket_12 * new_session_ticket() const
std::pair< std::string, Signature_Format > choose_sig_format(const Private_Key &key, Signature_Scheme &scheme, bool for_client_auth, const Policy &policy) const
Protocol_Version version() const
void confirm_transition_to(Handshake_Type msg_type)
bool received_handshake_msg(Handshake_Type msg_type) const
void set_expected_next(Handshake_Type msg_type)
const Session_Ticket & ticket() const
Definition: tls_messages.h:923
virtual std::vector< Signature_Scheme > allowed_signature_schemes() const
Definition: tls_policy.cpp:22
bool allowed_signature_method(std::string_view sig_method) const
Definition: tls_policy.cpp:116
bool allowed_signature_hash(std::string_view hash) const
Definition: tls_policy.cpp:121
std::string hash_function_name() const noexcept
bool is_compatible_with(const Protocol_Version &protocol_version) const noexcept
std::optional< Signature_Format > format() const noexcept
std::string padding_string() const noexcept
bool is_available() const noexcept
std::string algorithm_name() const noexcept
const char * handshake_type_to_string(Handshake_Type type)
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64
Definition: bigint.h:1092