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();
72 uint16_t resumed_session_ciphersuite_code()
const {
74 return m_resumed_session->ciphersuite_code();
77 std::vector<X509_Certificate> peer_cert_chain()
const override {
78 if(is_a_resumption()) {
79 return resume_peer_certs();
81 if(server_certs() !=
nullptr) {
82 return server_certs()->cert_chain();
88 std::unique_ptr<Public_Key> m_server_public_key;
91 std::optional<Session> m_resumed_session;
92 bool m_is_reneg =
false;
102 const std::shared_ptr<Credentials_Manager>& creds,
103 const std::shared_ptr<const Policy>&
policy,
104 const std::shared_ptr<RandomNumberGenerator>&
rng,
107 const std::vector<std::string>& next_protocols,
111 m_info(std::move(info)) {
113 const auto version = datagram ? Protocol_Version::DTLS_V12 : Protocol_Version::TLS_V12;
115 send_client_hello(state,
false, version, std::nullopt , next_protocols);
125 downgrade_info.io_buffer_size),
126 m_creds(downgrade_info.creds),
127 m_info(downgrade_info.server_info) {
133 const std::vector<uint8_t> client_hello_msg(
137 state.
client_hello(std::make_unique<Client_Hello_12>(client_hello_msg));
146 downgrade_info.
tls12_session->session.version().is_pre_tls_13());
147 send_client_hello(state,
155std::unique_ptr<Handshake_State> Client_Impl_12::new_handshake_state(std::unique_ptr<Handshake_IO> io) {
156 return std::make_unique<Client_Handshake_State_12>(std::move(io),
callbacks());
162void Client_Impl_12::initiate_handshake(Handshake_State& state,
bool force_full_renegotiation) {
164 const auto version = state.version().is_datagram_protocol() ? Protocol_Version::DTLS_V12 : Protocol_Version::TLS_V12;
165 send_client_hello(state, force_full_renegotiation, version);
168void Client_Impl_12::send_client_hello(Handshake_State& state_base,
169 bool force_full_renegotiation,
170 Protocol_Version version,
171 std::optional<Session_with_Handle> session_and_handle,
172 const std::vector<std::string>& next_protocols) {
173 Client_Handshake_State_12& state =
dynamic_cast<Client_Handshake_State_12&
>(state_base);
175 if(state.version().is_datagram_protocol()) {
180 if(!force_full_renegotiation) {
182 if(!session_and_handle.has_value() && !m_info.empty()) {
184 session_and_handle = std::move(sessions.front());
188 if(session_and_handle.has_value()) {
193 auto& session_info = session_and_handle->session;
194 const bool exact_version = session_info.version() == version;
195 const bool ok_version = (session_info.version().is_datagram_protocol() == version.is_datagram_protocol()) &&
200 if(
policy().acceptable_ciphersuite(session_info.ciphersuite()) && session_version_ok) {
201 state.client_hello(std::make_unique<Client_Hello_12>(state.handshake_io(),
207 session_and_handle.value(),
210 state.record_resumption_info(std::move(session_info));
215 if(state.client_hello() ==
nullptr) {
217 const Client_Hello_12::Settings client_settings(version, m_info.hostname());
218 state.client_hello(std::make_unique<Client_Hello_12>(state.handshake_io(),
233bool key_usage_matches_ciphersuite(Key_Constraints usage,
const Ciphersuite& suite) {
252 const std::vector<uint8_t>& contents,
253 bool epoch0_restart) {
256 Client_Handshake_State_12& state =
dynamic_cast<Client_Handshake_State_12&
>(state_base);
259 const Hello_Request hello_request(contents);
261 if(state.client_hello() !=
nullptr) {
262 throw TLS_Exception(Alert::HandshakeFailure,
"Cannot renegotiate during a handshake");
265 if(
policy().allow_server_initiated_renegotiation()) {
267 state.mark_as_renegotiation();
268 initiate_handshake(state,
true );
270 throw TLS_Exception(Alert::HandshakeFailure,
"Client policy prohibits insecure renegotiation");
273 if(
policy().abort_connection_on_undesired_renegotiation()) {
274 throw TLS_Exception(Alert::NoRenegotiation,
"Client policy prohibits renegotiation");
284 state.confirm_transition_to(type);
288 state.hash().update(state.handshake_io().format(contents, type));
295 const Hello_Verify_Request hello_verify_request(contents);
296 state.hello_verify_request(hello_verify_request);
298 state.server_hello(std::make_unique<Server_Hello_12>(contents));
300 if(!state.server_hello()->legacy_version().valid()) {
301 throw TLS_Exception(Alert::ProtocolVersion,
"Server replied with an invalid version");
304 if(!state.client_hello()->offered_suite(state.server_hello()->ciphersuite())) {
305 throw TLS_Exception(Alert::HandshakeFailure,
"Server replied with ciphersuite we didn't send");
309 if(!suite || !suite->usable_in_version(state.server_hello()->legacy_version())) {
310 throw TLS_Exception(Alert::HandshakeFailure,
311 "Server replied using a ciphersuite not allowed in version it offered");
319 if(suite->aead_ciphersuite() && state.server_hello()->supports_encrypt_then_mac()) {
320 throw TLS_Exception(Alert::IllegalParameter,
321 "Server replied using an AEAD ciphersuite and an encrypt-then-MAC response extension");
325 throw TLS_Exception(Alert::HandshakeFailure,
"Server replied with a signaling ciphersuite");
328 if(state.server_hello()->compression_method() != 0) {
329 throw TLS_Exception(Alert::IllegalParameter,
"Server replied with non-null compression method");
332 if(state.client_hello()->legacy_version() > state.server_hello()->legacy_version()) {
342 if(
auto requested = state.server_hello()->random_signals_downgrade();
343 requested.has_value() && requested.value() <= Protocol_Version::TLS_V11) {
344 throw TLS_Exception(Alert::IllegalParameter,
"Downgrade attack detected");
348 auto client_extn = state.client_hello()->extension_types();
349 auto server_extn = state.server_hello()->extension_types();
351 std::vector<Extension_Code> diff;
354 server_extn.begin(), server_extn.end(), client_extn.begin(), client_extn.end(), std::back_inserter(diff));
359 std::ostringstream msg;
360 msg <<
"Server replied with unsupported extensions:";
361 for(
auto&& d : diff) {
362 msg <<
" " <<
static_cast<int>(d);
364 throw TLS_Exception(Alert::UnsupportedExtension, msg.str());
367 if(
const uint16_t srtp = state.server_hello()->srtp_profile()) {
368 if(!
value_exists(state.client_hello()->srtp_profiles(), srtp)) {
369 throw TLS_Exception(Alert::HandshakeFailure,
"Server replied with DTLS-SRTP alg we did not send");
376 state.set_version(state.server_hello()->legacy_version());
378 if(state.server_hello()->extensions().has<Application_Layer_Protocol_Notification>()) {
379 const auto* server_alpn = state.server_hello()->extensions().get<Application_Layer_Protocol_Notification>();
380 const auto selected = server_alpn->single_protocol();
381 const auto* client_alpn = state.client_hello()->extensions().get<Application_Layer_Protocol_Notification>();
383 const auto& offered = client_alpn->protocols();
385 throw TLS_Exception(Alert::IllegalParameter,
"Server selected an ALPN protocol not offered by the client");
388 m_application_protocol = state.server_hello()->next_protocol();
393 if(
policy().require_extended_master_secret() && !state.server_hello()->supports_extended_master_secret()) {
394 throw TLS_Exception(Alert::HandshakeFailure,
395 "Policy requires the Extended Master Secret extension but the server did not send it");
398 const bool server_returned_same_session_id =
399 !state.server_hello()->session_id().empty() &&
400 (state.server_hello()->session_id() == state.client_hello()->session_id());
402 if(server_returned_same_session_id) {
410 if(state.server_hello()->legacy_version() != state.client_hello()->legacy_version()) {
411 throw TLS_Exception(Alert::HandshakeFailure,
"Server resumed session but with wrong version");
416 if(state.server_hello()->ciphersuite() != state.resumed_session_ciphersuite_code()) {
417 throw TLS_Exception(Alert::HandshakeFailure,
"Server resumed session with a different ciphersuite");
420 if(state.server_hello()->supports_extended_master_secret() &&
421 !state.resumed_session_supports_extended_master_secret()) {
422 throw TLS_Exception(Alert::HandshakeFailure,
"Server resumed session but added extended master secret");
425 if(!state.server_hello()->supports_extended_master_secret() &&
426 state.resumed_session_supports_extended_master_secret()) {
427 throw TLS_Exception(Alert::HandshakeFailure,
"Server resumed session and removed extended master secret");
430 state.compute_session_keys(state.resume_master_secret());
431 if(
policy().allow_ssl_key_log_file()) {
437 "CLIENT_RANDOM", state.client_hello()->random(), state.session_keys().master_secret());
440 if(state.server_hello()->supports_session_ticket()) {
453 if(
active_state()->version() != state.server_hello()->legacy_version()) {
454 throw TLS_Exception(Alert::ProtocolVersion,
"Server changed version after renegotiation");
457 if(state.server_hello()->supports_extended_master_secret() !=
459 throw TLS_Exception(Alert::HandshakeFailure,
"Server changed its mind about extended master secret");
463 state.discard_resumption_state();
465 if(state.client_hello()->legacy_version().is_datagram_protocol() !=
466 state.server_hello()->legacy_version().is_datagram_protocol()) {
467 throw TLS_Exception(Alert::ProtocolVersion,
"Server replied with different protocol type than we offered");
470 if(state.version() > state.client_hello()->legacy_version()) {
471 throw TLS_Exception(Alert::HandshakeFailure,
"Server replied with later version than client offered");
474 if(state.version().major_version() == 3 && state.version().minor_version() == 0) {
475 throw TLS_Exception(Alert::ProtocolVersion,
"Server attempting to negotiate SSLv3 which is not supported");
478 if(!
policy().acceptable_protocol_version(state.version())) {
479 throw TLS_Exception(Alert::ProtocolVersion,
480 "Server version " + state.version().to_string() +
" is unacceptable by policy");
483 if(state.ciphersuite().is_certificate_required()) {
485 }
else if(state.ciphersuite().kex_method() ==
Kex_Algo::PSK) {
506 state.server_certs(std::make_unique<Certificate_12>(contents,
policy()));
508 const std::vector<X509_Certificate>& server_certs = state.server_certs()->cert_chain();
510 if(server_certs.empty()) {
511 throw TLS_Exception(Alert::HandshakeFailure,
"Client: No certificates sent by server");
520 const X509_Certificate server_cert = server_certs[0];
523 const X509_Certificate& current_cert =
active_state()->peer_certs().at(0);
525 if(current_cert != server_cert) {
526 throw TLS_Exception(Alert::BadCertificate,
"Server certificate changed during renegotiation");
530 auto peer_key = server_cert.subject_public_key();
532 const std::string expected_key_type =
533 state.ciphersuite().signature_used() ? state.ciphersuite().sig_algo() :
"RSA";
535 if(peer_key->algo_name() != expected_key_type) {
536 throw TLS_Exception(Alert::IllegalParameter,
"Certificate key type did not match ciphersuite");
539 if(!key_usage_matches_ciphersuite(server_cert.constraints(), state.ciphersuite())) {
540 throw TLS_Exception(Alert::BadCertificate,
"Certificate usage constraints do not allow this ciphersuite");
543 state.record_server_public_key(std::move(peer_key));
552 if(state.server_hello()->supports_certificate_status_message()) {
556 auto trusted_CAs = m_creds->trusted_certificate_authorities(
"tls-client", m_info.hostname());
560 }
catch(TLS_Exception&) {
562 }
catch(std::exception& e) {
563 throw TLS_Exception(Alert::InternalError, e.what());
576 if(!state.ciphersuite().psk_ciphersuite()) {
581 state.server_kex(std::make_unique<Server_Key_Exchange>(
582 contents, state.ciphersuite().kex_method(), state.ciphersuite().auth_method(), state.version()));
584 if(state.ciphersuite().signature_used()) {
585 const Public_Key& server_key = state.server_public_key();
587 if(!state.server_kex()->verify(server_key, state,
policy())) {
588 throw TLS_Exception(Alert::DecryptError,
"Bad signature on server key exchange");
593 state.cert_req(std::make_unique<Certificate_Request_12>(contents));
595 state.server_hello_done(std::make_unique<Server_Hello_Done>(contents));
597 if(state.handshake_io().have_more_data()) {
598 throw TLS_Exception(Alert::UnexpectedMessage,
"Have data remaining in buffer after ServerHelloDone");
601 if(state.server_certs() !=
nullptr && state.server_hello()->supports_certificate_status_message()) {
603 auto trusted_CAs = m_creds->trusted_certificate_authorities(
"tls-client", m_info.hostname());
605 std::vector<std::optional<OCSP::Response>> ocsp;
606 if(state.server_cert_status() !=
nullptr) {
607 ocsp.emplace_back(
callbacks().tls_parse_ocsp_response(state.server_cert_status()->response()));
616 }
catch(TLS_Exception&) {
618 }
catch(std::exception& e) {
619 throw TLS_Exception(Alert::InternalError, e.what());
624 const auto& types = state.cert_req()->acceptable_cert_types();
626 const std::vector<X509_Certificate> client_certs =
627 m_creds->find_cert_chain(types, {}, state.cert_req()->acceptable_CAs(),
"tls-client", m_info.hostname());
629 state.client_certs(std::make_unique<Certificate_12>(state.handshake_io(), state.hash(), client_certs));
632 state.client_kex(std::make_unique<Client_Key_Exchange>(
633 state.handshake_io(), state,
policy(), *m_creds, state.maybe_server_public_key(), m_info.hostname(),
rng()));
635 state.compute_session_keys();
636 if(
policy().allow_ssl_key_log_file()) {
642 "CLIENT_RANDOM", state.client_hello()->random(), state.session_keys().master_secret());
647 m_creds->private_key_for(state.client_certs()->cert_chain()[0],
"tls-client", m_info.hostname());
650 throw TLS_Exception(Alert::InternalError,
"Failed to get private key for signing");
654 std::make_unique<Certificate_Verify_12>(state.handshake_io(), state,
policy(),
rng(), private_key.get()));
657 state.handshake_io().send(Change_Cipher_Spec());
663 if(state.server_hello()->supports_session_ticket()) {
669 state.new_session_ticket(std::make_unique<New_Session_Ticket_12>(contents));
677 if(state.handshake_io().have_more_data()) {
678 throw TLS_Exception(Alert::UnexpectedMessage,
"Have data remaining in buffer after Finished");
681 state.server_finished(std::make_unique<Finished_12>(contents));
684 throw TLS_Exception(Alert::DecryptError,
"Finished message didn't verify");
687 state.hash().update(state.handshake_io().format(contents, type));
689 if(state.client_finished() ==
nullptr) {
691 state.handshake_io().send(Change_Cipher_Spec());
698 const std::chrono::seconds session_lifetime_hint = [&] {
699 if(state.new_session_ticket() !=
nullptr) {
700 return std::chrono::seconds(state.new_session_ticket()->ticket_lifetime_hint());
702 return std::chrono::seconds::max();
706 Session session_info(state.session_keys().master_secret(),
707 state.server_hello()->legacy_version(),
708 state.server_hello()->ciphersuite(),
710 state.server_hello()->supports_extended_master_secret(),
711 state.server_hello()->supports_encrypt_then_mac(),
712 state.peer_cert_chain(),
714 state.server_hello()->srtp_profile(),
716 session_lifetime_hint);
721 const auto handle = [&]() -> std::optional<Session_Handle> {
729 if(
const auto* nst = state.new_session_ticket(); nst !=
nullptr && !nst->ticket().empty()) {
730 return Session_Handle(nst->ticket());
732 if(state.is_a_resumption() && !state.client_hello()->session_ticket().empty()) {
733 return Session_Handle(state.client_hello()->session_ticket());
735 if(
const auto& session_id = state.server_hello()->session_id(); !session_id.empty()) {
736 return Session_Handle(session_id);
745 summary.set_session_id(state.server_hello()->session_id());
746 if(
const auto* nst = state.new_session_ticket()) {
747 summary.set_session_ticket(nst->ticket());
752 if(handle.has_value()) {
762 if(state.is_a_resumption() && !state.client_hello()->session_ticket().empty() && handle->is_ticket() &&
770 if(!state.is_a_resumption()) {
781 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)
const std::optional< Active_Connection_State_12 > & active_state() const
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