Botan 3.14.1
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
11#include <botan/kdf.h>
12#include <botan/pkix_types.h>
13#include <botan/tls_callbacks.h>
14#include <botan/tls_messages_12.h>
15#include <botan/tls_policy.h>
16#include <botan/tls_signature_scheme.h>
17#include <botan/internal/stl_util.h>
18#include <botan/internal/tls_record.h>
19
20namespace Botan::TLS {
21
22std::string Handshake_Message::type_string() const {
24}
25
26/*
27* Initialize the SSL/TLS Handshake State
28*/
30
31Handshake_State::Handshake_State(std::unique_ptr<Handshake_IO> io, Callbacks& cb) :
32 m_callbacks(cb), m_handshake_io(std::move(io)), m_version(m_handshake_io->initial_record_version()) {}
33
35 m_callbacks.tls_inspect_handshake_msg(msg);
36}
37
39 note_message(hello_verify);
40
41 BOTAN_ASSERT_NONNULL(m_client_hello);
42 m_client_hello->update_hello_cookie(hello_verify);
43 hash().reset();
44 hash().update(handshake_io().send(*m_client_hello));
45 note_message(*m_client_hello);
46}
47
48void Handshake_State::client_hello(std::unique_ptr<Client_Hello_12> client_hello) {
49 // Legacy behavior (exception to the rule): Allow client_hello to be nullptr to reset state.
50 if(client_hello == nullptr) {
51 m_client_hello.reset();
52 hash().reset();
53 } else {
54 m_client_hello = std::move(client_hello);
55 note_message(*m_client_hello);
56 }
57}
58
59void Handshake_State::server_hello(std::unique_ptr<Server_Hello_12> server_hello) {
61 m_server_hello = std::move(server_hello);
62 m_ciphersuite = Ciphersuite::by_id(m_server_hello->ciphersuite());
63 note_message(*m_server_hello);
64}
65
66void Handshake_State::server_certs(std::unique_ptr<Certificate_12> server_certs) {
68 m_server_certs = std::move(server_certs);
69 note_message(*m_server_certs);
70}
71
72void Handshake_State::server_cert_status(std::unique_ptr<Certificate_Status> server_cert_status) {
74 m_server_cert_status = std::move(server_cert_status);
75 note_message(*m_server_cert_status);
76}
77
78void Handshake_State::server_kex(std::unique_ptr<Server_Key_Exchange> server_kex) {
80 m_server_kex = std::move(server_kex);
81 note_message(*m_server_kex);
82}
83
84void Handshake_State::cert_req(std::unique_ptr<Certificate_Request_12> cert_req) {
86 m_cert_req = std::move(cert_req);
87 note_message(*m_cert_req);
88}
89
90void Handshake_State::server_hello_done(std::unique_ptr<Server_Hello_Done> server_hello_done) {
92 m_server_hello_done = std::move(server_hello_done);
93 note_message(*m_server_hello_done);
94}
95
96void Handshake_State::client_certs(std::unique_ptr<Certificate_12> client_certs) {
98 m_client_certs = std::move(client_certs);
99 note_message(*m_client_certs);
100}
101
102void Handshake_State::client_kex(std::unique_ptr<Client_Key_Exchange> client_kex) {
104 m_client_kex = std::move(client_kex);
105 note_message(*m_client_kex);
106}
107
108void Handshake_State::client_verify(std::unique_ptr<Certificate_Verify_12> client_verify) {
110 m_client_verify = std::move(client_verify);
111 note_message(*m_client_verify);
112}
113
114void Handshake_State::server_verify(std::unique_ptr<Certificate_Verify_12> server_verify) {
116 m_server_verify = std::move(server_verify);
117 note_message(*m_server_verify);
118}
119
120void Handshake_State::new_session_ticket(std::unique_ptr<New_Session_Ticket_12> new_session_ticket) {
122 m_new_session_ticket = std::move(new_session_ticket);
123 note_message(*m_new_session_ticket);
124}
125
126void Handshake_State::server_finished(std::unique_ptr<Finished_12> server_finished) {
128 m_server_finished = std::move(server_finished);
129 note_message(*m_server_finished);
130}
131
132void Handshake_State::client_finished(std::unique_ptr<Finished_12> client_finished) {
134 m_client_finished = std::move(client_finished);
135 note_message(*m_client_finished);
136}
137
139 if(!m_ciphersuite.has_value()) {
140 throw Invalid_State("Cipher suite is not set");
141 }
142 return m_ciphersuite.value();
143}
144
145std::optional<std::string> Handshake_State::psk_identity() const {
146 if(!m_client_kex) {
147 return std::nullopt;
148 }
149 return m_client_kex->psk_identity();
150}
151
155
157 m_session_keys = Session_Keys(this, client_kex().pre_master_secret(), false);
158}
159
161 m_session_keys = Session_Keys(this, resume_master_secret, true);
162}
163
165 m_transitions.confirm_transition_to(handshake_msg);
166}
167
169 m_transitions.set_expected_next(handshake_msg);
170}
171
173 return m_transitions.received_handshake_msg(handshake_msg);
174}
175
176std::pair<Handshake_Type, std::vector<uint8_t>> Handshake_State::get_next_handshake_msg(size_t max_handshake_msg_size) {
177 return m_handshake_io->get_next_record(m_transitions.change_cipher_spec_expected(), max_handshake_msg_size);
178}
179
181 if(const auto* nst = maybe_new_session_ticket()) {
182 const auto& ticket = nst->ticket();
183 if(!ticket.empty()) {
184 return ticket;
185 }
186 }
187
188 return client_hello().session_ticket();
189}
190
191std::unique_ptr<KDF> Handshake_State::protocol_specific_prf() const {
192 return m_callbacks.tls12_protocol_specific_kdf(ciphersuite().prf_algo());
193}
194
195std::pair<std::string, Signature_Format> Handshake_State::choose_sig_format(const Private_Key& key,
196 Signature_Scheme& chosen_scheme,
197 bool for_client_auth,
198 const Policy& policy) const {
199 const std::string sig_algo = key.algo_name();
200
201 const std::vector<Signature_Scheme> allowed = policy.allowed_signature_schemes();
202
203 std::vector<Signature_Scheme> requested =
204 (for_client_auth) ? cert_req().signature_schemes() : client_hello().signature_schemes();
205
206 for(const Signature_Scheme scheme : allowed) {
207 if(!scheme.is_available() || !scheme.is_compatible_with(version())) {
208 continue;
209 }
210
211 if(scheme.algorithm_name() == sig_algo) {
212 if(std::find(requested.begin(), requested.end(), scheme) != requested.end()) {
213 chosen_scheme = scheme;
214 break;
215 }
216 }
217 }
218
219 if(!chosen_scheme.is_set()) {
220 throw TLS_Exception(Alert::HandshakeFailure,
221 "Could not agree on a signature scheme with peer for " + sig_algo + " key");
222 }
223
224 const std::string hash = chosen_scheme.hash_function_name();
225
226 if(!policy.allowed_signature_hash(hash)) {
227 throw TLS_Exception(Alert::HandshakeFailure, "Policy refuses to accept signing with any hash supported by peer");
228 }
229
230 if(!chosen_scheme.format().has_value()) {
231 throw Invalid_Argument(sig_algo + " is invalid/unknown for TLS signatures");
232 }
233
234 return std::make_pair(chosen_scheme.padding_string(), chosen_scheme.format().value());
235}
236
237namespace {
238
239bool supported_algos_include(const std::vector<Signature_Scheme>& schemes, Signature_Scheme received_scheme) {
240 for(const Signature_Scheme scheme : schemes) {
241 if(scheme == received_scheme && scheme.is_available()) {
242 return true;
243 }
244 }
245
246 return false;
247}
248
249} // namespace
250
251std::pair<std::string, Signature_Format> Handshake_State::parse_sig_format(
252 const Public_Key& key,
253 Signature_Scheme scheme,
254 const std::vector<Signature_Scheme>& offered_schemes,
255 bool for_client_auth,
256 const Policy& policy) const {
257 const std::string key_type = key.algo_name();
258
259 if(!policy.allowed_signature_method(key_type)) {
260 throw TLS_Exception(Alert::HandshakeFailure, "Rejecting " + key_type + " signature");
261 }
262
263 if(!scheme.is_available()) {
264 throw TLS_Exception(Alert::IllegalParameter, "Peer sent unknown signature scheme");
265 }
266
267 if(key_type != scheme.algorithm_name()) {
268 throw Decoding_Error("Counterparty sent inconsistent key and sig types");
269 }
270
271 if(for_client_auth && !has_cert_req()) {
272 throw TLS_Exception(Alert::HandshakeFailure, "No CertificateVerify message received");
273 }
274
275 /*
276 Confirm the signature type we just received against the
277 supported_algos list that we sent; it better be there.
278 */
279
280 const std::vector<Signature_Scheme> supported_algos =
281 for_client_auth ? cert_req().signature_schemes() : offered_schemes;
282
283 if(!scheme.is_compatible_with(Protocol_Version::TLS_V12)) {
284 throw TLS_Exception(Alert::IllegalParameter, "Peer sent unacceptable signature scheme");
285 }
286
287 if(!supported_algos_include(supported_algos, scheme)) {
288 throw TLS_Exception(Alert::IllegalParameter,
289 "TLS signature extension did not allow for " + scheme.to_string() + " signature");
290 }
291
292 // The offered schemes might have been modified by the application
293 if(!value_exists(policy.acceptable_signature_schemes(), scheme)) {
294 throw TLS_Exception(Alert::IllegalParameter, "Peer sent " + scheme.to_string() + " signature rejected by policy");
295 }
296
297 if(!scheme.format().has_value()) {
298 throw Invalid_Argument(key_type + " is invalid/unknown for TLS signatures");
299 }
300
301 return std::make_pair(scheme.padding_string(), scheme.format().value());
302}
303
304} // namespace Botan::TLS
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
virtual std::string algo_name() const =0
const std::vector< Signature_Scheme > & signature_schemes() const
static std::optional< Ciphersuite > by_id(uint16_t suite)
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
void server_finished(std::unique_ptr< Finished_12 > server_finished)
void client_hello(std::unique_ptr< Client_Hello_12 > client_hello)
const Finished_12 & server_finished() 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)
std::pair< Handshake_Type, std::vector< uint8_t > > get_next_handshake_msg(size_t max_handshake_msg_size)
const Certificate_Verify_12 & client_verify() const
const Certificate_Verify_12 & server_verify() const
Handshake_State(std::unique_ptr< Handshake_IO > io, Callbacks &callbacks)
void server_verify(std::unique_ptr< Certificate_Verify_12 > server_verify)
void new_session_ticket(std::unique_ptr< New_Session_Ticket_12 > new_session_ticket)
void client_certs(std::unique_ptr< Certificate_12 > client_certs)
void client_kex(std::unique_ptr< Client_Key_Exchange > client_kex)
const Server_Key_Exchange & server_kex() const
void server_hello_done(std::unique_ptr< Server_Hello_Done > server_hello_done)
const New_Session_Ticket_12 & new_session_ticket() const
void cert_req(std::unique_ptr< Certificate_Request_12 > cert_req)
const Server_Hello_12 & server_hello() const
const Certificate_Status & server_cert_status() const
const Finished_12 & client_finished() const
void server_cert_status(std::unique_ptr< Certificate_Status > server_cert_status)
const Client_Hello_12 & client_hello() const
void server_hello(std::unique_ptr< Server_Hello_12 > server_hello)
void confirm_transition_to(Handshake_Type msg_type)
const Certificate_12 & server_certs() const
void set_version(const Protocol_Version &version)
void client_finished(std::unique_ptr< Finished_12 > client_finished)
std::optional< std::string > psk_identity() const
const Server_Hello_Done & server_hello_done() const
const Client_Key_Exchange & client_kex() const
void client_verify(std::unique_ptr< Certificate_Verify_12 > client_verify)
const Ciphersuite & ciphersuite() const
Session_Ticket session_ticket() const
const Certificate_Request_12 & cert_req() const
void server_kex(std::unique_ptr< Server_Key_Exchange > server_kex)
const Certificate_12 & client_certs() const
bool received_handshake_msg(Handshake_Type msg_type) const
void server_certs(std::unique_ptr< Certificate_12 > server_certs)
const New_Session_Ticket_12 * maybe_new_session_ticket() const
std::unique_ptr< KDF > protocol_specific_prf() 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
virtual std::vector< Signature_Scheme > allowed_signature_schemes() const
bool allowed_signature_method(std::string_view sig_method) const
bool allowed_signature_hash(std::string_view hash) const
virtual std::vector< Signature_Scheme > acceptable_signature_schemes() const
bool is_compatible_with(const Protocol_Version &protocol_version) const noexcept
std::optional< Signature_Format > format() const noexcept
const char * handshake_type_to_string(Handshake_Type type)
Definition tls_magic.cpp:15
Strong< std::vector< uint8_t >, struct Session_Ticket_ > Session_Ticket
holds a TLS 1.2 session ticket for stateless resumption
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:44
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128