10#include <botan/internal/tls_client_impl_12.h>
12#include <botan/ocsp.h>
13#include <botan/tls_callbacks.h>
14#include <botan/tls_messages_12.h>
15#include <botan/tls_policy.h>
16#include <botan/internal/stl_util.h>
17#include <botan/internal/tls_handshake_state.h>
29 Client_Handshake_State_12(std::unique_ptr<Handshake_IO> io, Callbacks& cb) :
30 Handshake_State(std::move(io), cb), m_is_reneg(false) {}
32 const Public_Key& server_public_key()
const {
33 BOTAN_ASSERT(m_server_public_key,
"Server sent us a certificate");
34 return *m_server_public_key;
37 const Public_Key* maybe_server_public_key()
const {
return m_server_public_key.get(); }
39 void record_server_public_key(std::unique_ptr<Public_Key> spk) {
41 m_server_public_key = std::move(spk);
44 bool is_a_resumption()
const {
return m_resumed_session.has_value(); }
46 void discard_resumption_state() { m_resumed_session.reset(); }
48 void record_resumption_info(std::optional<Session> session_info) {
50 m_resumed_session = std::move(session_info);
53 bool is_a_renegotiation()
const {
return m_is_reneg; }
55 void mark_as_renegotiation() { m_is_reneg =
true; }
59 return m_resumed_session->master_secret();
62 const std::vector<X509_Certificate>& resume_peer_certs()
const {
64 return m_resumed_session->peer_certs();
67 bool resumed_session_supports_extended_master_secret()
const {
69 return m_resumed_session->supports_extended_master_secret();
73 std::unique_ptr<Public_Key> m_server_public_key;
76 std::optional<Session> m_resumed_session;
77 bool m_is_reneg =
false;
87 const std::shared_ptr<Credentials_Manager>& creds,
88 const std::shared_ptr<const Policy>&
policy,
89 const std::shared_ptr<RandomNumberGenerator>&
rng,
92 const std::vector<std::string>& next_protocols,
96 m_info(std::move(info)) {
98 const auto version = datagram ? Protocol_Version::DTLS_V12 : Protocol_Version::TLS_V12;
100 send_client_hello(state,
false, version, std::nullopt , next_protocols);
110 downgrade_info.io_buffer_size),
111 m_creds(downgrade_info.creds),
112 m_info(downgrade_info.server_info) {
118 const std::vector<uint8_t> client_hello_msg(
122 state.
client_hello(std::make_unique<Client_Hello_12>(client_hello_msg));
131 downgrade_info.
tls12_session->session.version().is_pre_tls_13());
132 send_client_hello(state,
140std::unique_ptr<Handshake_State> Client_Impl_12::new_handshake_state(std::unique_ptr<Handshake_IO> io) {
141 return std::make_unique<Client_Handshake_State_12>(std::move(io),
callbacks());
144std::vector<X509_Certificate> Client_Impl_12::get_peer_cert_chain(
const Handshake_State& state)
const {
145 const Client_Handshake_State_12& cstate =
dynamic_cast<const Client_Handshake_State_12&
>(state);
147 if(cstate.is_a_resumption()) {
148 return cstate.resume_peer_certs();
151 if(state.server_certs() !=
nullptr) {
152 return state.server_certs()->cert_chain();
154 return std::vector<X509_Certificate>();
160void Client_Impl_12::initiate_handshake(
Handshake_State& state,
bool force_full_renegotiation) {
162 const auto version = state.version().is_datagram_protocol() ? Protocol_Version::DTLS_V12 : Protocol_Version::TLS_V12;
163 send_client_hello(state, force_full_renegotiation, version);
167 bool force_full_renegotiation,
169 std::optional<Session_with_Handle> session_and_handle,
170 const std::vector<std::string>& next_protocols) {
171 Client_Handshake_State_12& state =
dynamic_cast<Client_Handshake_State_12&
>(state_base);
173 if(state.version().is_datagram_protocol()) {
178 if(!force_full_renegotiation) {
180 if(!session_and_handle.has_value() && !m_info.empty()) {
182 session_and_handle = std::move(sessions.front());
186 if(session_and_handle.has_value()) {
191 auto& session_info = session_and_handle->session;
192 const bool exact_version = session_info.version() == version;
193 const bool ok_version = (session_info.version().is_datagram_protocol() == version.is_datagram_protocol()) &&
198 if(
policy().acceptable_ciphersuite(session_info.ciphersuite()) && session_version_ok) {
199 state.client_hello(std::make_unique<Client_Hello_12>(state.handshake_io(),
205 session_and_handle.value(),
208 state.record_resumption_info(std::move(session_info));
213 if(state.client_hello() ==
nullptr) {
215 const Client_Hello_12::Settings client_settings(version, m_info.hostname());
216 state.client_hello(std::make_unique<Client_Hello_12>(state.handshake_io(),
231bool key_usage_matches_ciphersuite(Key_Constraints usage,
const Ciphersuite& suite) {
248void Client_Impl_12::process_handshake_msg(
const Handshake_State* active_state,
251 const std::vector<uint8_t>& contents,
252 bool epoch0_restart) {
255 Client_Handshake_State_12& state =
dynamic_cast<Client_Handshake_State_12&
>(state_base);
258 const Hello_Request hello_request(contents);
260 if(state.client_hello() !=
nullptr) {
261 throw TLS_Exception(Alert::HandshakeFailure,
"Cannot renegotiate during a handshake");
264 if(
policy().allow_server_initiated_renegotiation()) {
266 state.mark_as_renegotiation();
267 initiate_handshake(state,
true );
269 throw TLS_Exception(Alert::HandshakeFailure,
"Client policy prohibits insecure renegotiation");
272 if(
policy().abort_connection_on_undesired_renegotiation()) {
273 throw TLS_Exception(Alert::NoRenegotiation,
"Client policy prohibits renegotiation");
283 state.confirm_transition_to(type);
287 state.hash().update(state.handshake_io().format(contents, type));
294 const Hello_Verify_Request hello_verify_request(contents);
295 state.hello_verify_request(hello_verify_request);
297 state.server_hello(std::make_unique<Server_Hello_12>(contents));
299 if(!state.server_hello()->legacy_version().valid()) {
300 throw TLS_Exception(Alert::ProtocolVersion,
"Server replied with an invalid version");
303 if(!state.client_hello()->offered_suite(state.server_hello()->ciphersuite())) {
304 throw TLS_Exception(Alert::HandshakeFailure,
"Server replied with ciphersuite we didn't send");
308 if(!suite || !suite->usable_in_version(state.server_hello()->legacy_version())) {
309 throw TLS_Exception(Alert::HandshakeFailure,
310 "Server replied using a ciphersuite not allowed in version it offered");
318 if(suite->aead_ciphersuite() && state.server_hello()->supports_encrypt_then_mac()) {
319 throw TLS_Exception(Alert::IllegalParameter,
320 "Server replied using an AEAD ciphersuite and an encrypt-then-MAC response extension");
324 throw TLS_Exception(Alert::HandshakeFailure,
"Server replied with a signaling ciphersuite");
327 if(state.server_hello()->compression_method() != 0) {
328 throw TLS_Exception(Alert::IllegalParameter,
"Server replied with non-null compression method");
331 if(state.client_hello()->legacy_version() > state.server_hello()->legacy_version()) {
341 if(
auto requested = state.server_hello()->random_signals_downgrade();
342 requested.has_value() && requested.value() <= Protocol_Version::TLS_V11) {
343 throw TLS_Exception(Alert::IllegalParameter,
"Downgrade attack detected");
347 auto client_extn = state.client_hello()->extension_types();
348 auto server_extn = state.server_hello()->extension_types();
350 std::vector<Extension_Code> diff;
353 server_extn.begin(), server_extn.end(), client_extn.begin(), client_extn.end(), std::back_inserter(diff));
358 std::ostringstream msg;
359 msg <<
"Server replied with unsupported extensions:";
360 for(
auto&& d : diff) {
361 msg <<
" " <<
static_cast<int>(d);
363 throw TLS_Exception(Alert::UnsupportedExtension, msg.str());
366 if(
const uint16_t srtp = state.server_hello()->srtp_profile()) {
367 if(!
value_exists(state.client_hello()->srtp_profiles(), srtp)) {
368 throw TLS_Exception(Alert::HandshakeFailure,
"Server replied with DTLS-SRTP alg we did not send");
375 state.set_version(state.server_hello()->legacy_version());
376 m_application_protocol = state.server_hello()->next_protocol();
380 const bool server_returned_same_session_id =
381 !state.server_hello()->session_id().empty() &&
382 (state.server_hello()->session_id() == state.client_hello()->session_id());
384 if(server_returned_same_session_id) {
392 if(state.server_hello()->legacy_version() != state.client_hello()->legacy_version()) {
393 throw TLS_Exception(Alert::HandshakeFailure,
"Server resumed session but with wrong version");
396 if(state.server_hello()->supports_extended_master_secret() &&
397 !state.resumed_session_supports_extended_master_secret()) {
398 throw TLS_Exception(Alert::HandshakeFailure,
"Server resumed session but added extended master secret");
401 if(!state.server_hello()->supports_extended_master_secret() &&
402 state.resumed_session_supports_extended_master_secret()) {
403 throw TLS_Exception(Alert::HandshakeFailure,
"Server resumed session and removed extended master secret");
406 state.compute_session_keys(state.resume_master_secret());
407 if(
policy().allow_ssl_key_log_file()) {
413 "CLIENT_RANDOM", state.client_hello()->random(), state.session_keys().master_secret());
416 if(state.server_hello()->supports_session_ticket()) {
424 if(active_state !=
nullptr) {
429 if(active_state->version() != state.server_hello()->legacy_version()) {
430 throw TLS_Exception(Alert::ProtocolVersion,
"Server changed version after renegotiation");
433 if(state.server_hello()->supports_extended_master_secret() !=
434 active_state->server_hello()->supports_extended_master_secret()) {
435 throw TLS_Exception(Alert::HandshakeFailure,
"Server changed its mind about extended master secret");
439 state.discard_resumption_state();
441 if(state.client_hello()->legacy_version().is_datagram_protocol() !=
442 state.server_hello()->legacy_version().is_datagram_protocol()) {
443 throw TLS_Exception(Alert::ProtocolVersion,
"Server replied with different protocol type than we offered");
446 if(state.version() > state.client_hello()->legacy_version()) {
447 throw TLS_Exception(Alert::HandshakeFailure,
"Server replied with later version than client offered");
450 if(state.version().major_version() == 3 && state.version().minor_version() == 0) {
451 throw TLS_Exception(Alert::ProtocolVersion,
"Server attempting to negotiate SSLv3 which is not supported");
454 if(!
policy().acceptable_protocol_version(state.version())) {
455 throw TLS_Exception(Alert::ProtocolVersion,
456 "Server version " + state.version().to_string() +
" is unacceptable by policy");
459 if(state.ciphersuite().signature_used() || state.ciphersuite().kex_method() ==
Kex_Algo::STATIC_RSA) {
461 }
else if(state.ciphersuite().kex_method() ==
Kex_Algo::PSK) {
480 state.server_certs(std::make_unique<Certificate_12>(contents,
policy()));
482 const std::vector<X509_Certificate>& server_certs = state.server_certs()->cert_chain();
484 if(server_certs.empty()) {
485 throw TLS_Exception(Alert::HandshakeFailure,
"Client: No certificates sent by server");
494 const X509_Certificate server_cert = server_certs[0];
496 if(active_state !=
nullptr && active_state->server_certs() !=
nullptr) {
497 const X509_Certificate current_cert = active_state->server_certs()->cert_chain().at(0);
499 if(current_cert != server_cert) {
500 throw TLS_Exception(Alert::BadCertificate,
"Server certificate changed during renegotiation");
504 auto peer_key = server_cert.subject_public_key();
506 const std::string expected_key_type =
507 state.ciphersuite().signature_used() ? state.ciphersuite().sig_algo() :
"RSA";
509 if(peer_key->algo_name() != expected_key_type) {
510 throw TLS_Exception(Alert::IllegalParameter,
"Certificate key type did not match ciphersuite");
513 if(!key_usage_matches_ciphersuite(server_cert.constraints(), state.ciphersuite())) {
514 throw TLS_Exception(Alert::BadCertificate,
"Certificate usage constraints do not allow this ciphersuite");
517 state.record_server_public_key(std::move(peer_key));
526 if(state.server_hello()->supports_certificate_status_message()) {
530 auto trusted_CAs = m_creds->trusted_certificate_authorities(
"tls-client", m_info.hostname());
534 }
catch(TLS_Exception&) {
536 }
catch(std::exception& e) {
537 throw TLS_Exception(Alert::InternalError, e.what());
550 if(!state.ciphersuite().psk_ciphersuite()) {
555 state.server_kex(std::make_unique<Server_Key_Exchange>(
556 contents, state.ciphersuite().kex_method(), state.ciphersuite().auth_method(), state.version()));
558 if(state.ciphersuite().signature_used()) {
559 const Public_Key& server_key = state.server_public_key();
561 if(!state.server_kex()->verify(server_key, state,
policy())) {
562 throw TLS_Exception(Alert::DecryptError,
"Bad signature on server key exchange");
567 state.cert_req(std::make_unique<Certificate_Request_12>(contents));
569 state.server_hello_done(std::make_unique<Server_Hello_Done>(contents));
571 if(state.handshake_io().have_more_data()) {
572 throw TLS_Exception(Alert::UnexpectedMessage,
"Have data remaining in buffer after ServerHelloDone");
575 if(state.server_certs() !=
nullptr && state.server_hello()->supports_certificate_status_message()) {
577 auto trusted_CAs = m_creds->trusted_certificate_authorities(
"tls-client", m_info.hostname());
579 std::vector<std::optional<OCSP::Response>> ocsp;
580 if(state.server_cert_status() !=
nullptr) {
581 ocsp.emplace_back(
callbacks().tls_parse_ocsp_response(state.server_cert_status()->response()));
590 }
catch(TLS_Exception&) {
592 }
catch(std::exception& e) {
593 throw TLS_Exception(Alert::InternalError, e.what());
598 const auto& types = state.cert_req()->acceptable_cert_types();
600 const std::vector<X509_Certificate> client_certs =
601 m_creds->find_cert_chain(types, {}, state.cert_req()->acceptable_CAs(),
"tls-client", m_info.hostname());
603 state.client_certs(std::make_unique<Certificate_12>(state.handshake_io(), state.hash(), client_certs));
606 state.client_kex(std::make_unique<Client_Key_Exchange>(
607 state.handshake_io(), state,
policy(), *m_creds, state.maybe_server_public_key(), m_info.hostname(),
rng()));
609 state.compute_session_keys();
610 if(
policy().allow_ssl_key_log_file()) {
616 "CLIENT_RANDOM", state.client_hello()->random(), state.session_keys().master_secret());
621 m_creds->private_key_for(state.client_certs()->cert_chain()[0],
"tls-client", m_info.hostname());
624 throw TLS_Exception(Alert::InternalError,
"Failed to get private key for signing");
628 std::make_unique<Certificate_Verify_12>(state.handshake_io(), state,
policy(),
rng(), private_key.get()));
631 state.handshake_io().send(Change_Cipher_Spec());
637 if(state.server_hello()->supports_session_ticket()) {
643 state.new_session_ticket(std::make_unique<New_Session_Ticket_12>(contents));
651 if(state.handshake_io().have_more_data()) {
652 throw TLS_Exception(Alert::UnexpectedMessage,
"Have data remaining in buffer after Finished");
655 state.server_finished(std::make_unique<Finished_12>(contents));
658 throw TLS_Exception(Alert::DecryptError,
"Finished message didn't verify");
661 state.hash().update(state.handshake_io().format(contents, type));
663 if(state.client_finished() ==
nullptr) {
665 state.handshake_io().send(Change_Cipher_Spec());
672 const std::chrono::seconds session_lifetime_hint = [&] {
673 if(state.new_session_ticket() !=
nullptr) {
674 return std::chrono::seconds(state.new_session_ticket()->ticket_lifetime_hint());
676 return std::chrono::seconds::max();
680 Session session_info(state.session_keys().master_secret(),
681 state.server_hello()->legacy_version(),
682 state.server_hello()->ciphersuite(),
684 state.server_hello()->supports_extended_master_secret(),
685 state.server_hello()->supports_encrypt_then_mac(),
686 get_peer_cert_chain(state),
688 state.server_hello()->srtp_profile(),
690 session_lifetime_hint);
695 const auto handle = [&]() -> std::optional<Session_Handle> {
696 if(
const auto& session_ticket = state.session_ticket(); !session_ticket.empty()) {
697 return Session_Handle(session_ticket);
698 }
else if(
const auto& session_id = state.server_hello()->session_id(); !session_id.empty()) {
699 return Session_Handle(session_id);
709 summary.set_session_id(state.server_hello()->session_id());
710 if(
const auto* nst = state.new_session_ticket()) {
711 summary.set_session_ticket(nst->ticket());
716 if(handle.has_value()) {
726 if(state.is_a_resumption() && !state.client_hello()->session_ticket().empty() && handle->is_ticket() &&
734 if(!state.is_a_resumption()) {
745 throw Unexpected_Message(
"Unknown handshake message received");
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ASSERT(expr, assertion_made)
virtual void tls_examine_extensions(const Extensions &extn, Connection_Side which_side, Handshake_Type which_message)
virtual void tls_session_established(const Session_Summary &session)
virtual bool tls_should_persist_resumption_information(const Session &session)
virtual void tls_verify_cert_chain(const std::vector< X509_Certificate > &cert_chain, const std::vector< std::optional< OCSP::Response > > &ocsp_responses, const std::vector< Certificate_Store * > &trusted_roots, Usage_Type usage, std::string_view hostname, const TLS::Policy &policy)
virtual void tls_ssl_key_log_data(std::string_view label, std::span< const uint8_t > client_random, std::span< const uint8_t > secret) const
RandomNumberGenerator & rng()
void change_cipher_spec_reader(Connection_Side side)
Handshake_State & create_handshake_state(Protocol_Version version)
Callbacks & callbacks() const
void secure_renegotiation_check(const Client_Hello_12 *client_hello)
Session_Manager & session_manager()
const Policy & policy() const
void change_cipher_spec_writer(Connection_Side side)
std::vector< uint8_t > secure_renegotiation_data_for_client_hello() const
Channel_Impl_12(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< RandomNumberGenerator > &rng, const std::shared_ptr< const Policy > &policy, bool is_server, bool is_datagram, size_t io_buf_sz=TLS::Channel::IO_BUF_DEFAULT_SIZE)
std::optional< std::string > external_psk_identity() const override
bool secure_renegotiation_supported() const override
void send_warning_alert(Alert::Type type)
static bool is_scsv(uint16_t suite)
static std::optional< Ciphersuite > by_id(uint16_t suite)
Client_Impl_12(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< Credentials_Manager > &creds, const std::shared_ptr< const Policy > &policy, const std::shared_ptr< RandomNumberGenerator > &rng, Server_Information server_info=Server_Information(), bool datagram=false, const std::vector< std::string > &next_protocols={}, size_t reserved_io_buffer_size=TLS::Channel::IO_BUF_DEFAULT_SIZE)
void update(const uint8_t in[], size_t length)
void client_hello(std::unique_ptr< Client_Hello_12 > client_hello)
void set_expected_next(Handshake_Type msg_type)
virtual bool only_resume_with_exact_version() const
virtual bool acceptable_protocol_version(Protocol_Version version) const
virtual size_t remove(const Session_Handle &handle)=0
virtual void store(const Session &session, const Session_Handle &handle)=0
Save a Session under a Session_Handle (TLS Client).
bool value_exists(const std::vector< T > &vec, const V &val)
std::vector< T, secure_allocator< T > > secure_vector