10#include <botan/internal/tls_client_impl_13.h>
12#include <botan/credentials_manager.h>
13#include <botan/tls_callbacks.h>
14#include <botan/tls_extensions_13.h>
15#include <botan/tls_messages_13.h>
16#include <botan/tls_policy.h>
17#include <botan/x509cert.h>
18#include <botan/internal/stl_util.h>
19#include <botan/internal/tls_channel_impl_13.h>
20#include <botan/internal/tls_cipher_state.h>
28 const std::shared_ptr<Credentials_Manager>& creds,
29 const std::shared_ptr<const Policy>&
policy,
30 const std::shared_ptr<RandomNumberGenerator>&
rng,
32 const std::vector<std::string>& next_protocols) :
34 m_info(std::move(info)),
35 m_should_send_ccs(false) {
36#if defined(BOTAN_HAS_TLS_12)
37 if(
policy->allow_tls12()) {
38 expect_downgrade(m_info, next_protocols);
42 if(
auto session = find_session_for_resumption()) {
43 if(!session->session.version().is_pre_tls_13()) {
44 m_resumed_session = std::move(session);
72 if(
policy->tls_13_middlebox_compatibility_mode()) {
73 m_should_send_ccs =
true;
91 m_handshake_state.
received(std::move(message)));
97 std::visit([&](
auto msg) { handle(msg); }, m_handshake_state.received(std::move(message)));
105 if(!m_handshake_state.has_client_hello() || m_handshake_state.has_server_finished()) {
106 throw TLS_Exception(Alert::UnexpectedMessage,
"Received an unexpected dummy Change Cipher Spec");
119 return m_handshake_state.handshake_finished();
122std::optional<Session_with_Handle> Client_Impl_13::find_session_for_resumption() {
139 if(sessions.empty()) {
146 auto& session_to_resume = sessions.front();
148 return std::move(session_to_resume);
151void Client_Impl_13::handle(
const Server_Hello_12_Shim& server_hello_msg) {
153 throw TLS_Exception(Alert::UnexpectedMessage,
"Version downgrade received after Hello Retry");
161 throw TLS_Exception(Alert::ProtocolVersion,
"Received an unexpected legacy Server Hello");
174 if(server_hello_msg.random_signals_downgrade().has_value()) {
175 throw TLS_Exception(Alert::IllegalParameter,
"Downgrade attack detected");
184 if(server_hello_msg.extensions().has<Supported_Versions>()) {
185 throw TLS_Exception(Alert::IllegalParameter,
"Unexpected extension received");
192 const auto& client_hello_exts = m_handshake_state.client_hello().extensions();
194 if(!client_hello_exts.get<Supported_Versions>()->supports(server_hello_msg.selected_version())) {
195 throw TLS_Exception(Alert::ProtocolVersion,
"Protocol version was not offered");
198 if(
policy().tls_13_middlebox_compatibility_mode() &&
199 m_handshake_state.client_hello().session_id() == server_hello_msg.session_id()) {
204 throw TLS_Exception(Alert::IllegalParameter,
"Unexpected session ID during downgrade");
211 m_transitions.set_expected_next({});
220 if(ch.session_id() != sh.session_id()) {
221 throw TLS_Exception(Alert::IllegalParameter,
"echoed session id did not match");
227 if(!ch.offered_suite(sh.ciphersuite())) {
228 throw TLS_Exception(Alert::IllegalParameter,
"Server replied with ciphersuite we didn't send");
239 throw TLS_Exception(Alert::IllegalParameter,
"Protocol version was not offered");
248 const auto& ch = m_handshake_state.client_hello();
250 validate_server_hello_ish(ch, sh);
257 if(sh.extensions().contains_other_than(ch.extensions().extension_types())) {
258 throw TLS_Exception(Alert::UnsupportedExtension,
"Unsupported extension found in Server Hello");
261 if(m_handshake_state.has_hello_retry_request()) {
262 const auto& hrr = m_handshake_state.hello_retry_request();
268 if(hrr.ciphersuite() != sh.ciphersuite()) {
269 throw TLS_Exception(Alert::IllegalParameter,
"server changed its chosen ciphersuite");
276 if(hrr.selected_version() != sh.selected_version()) {
277 throw TLS_Exception(Alert::IllegalParameter,
"server changed its chosen protocol version");
288 if(!cipher->usable_in_version(Protocol_Version::TLS_V13)) {
289 throw TLS_Exception(Alert::IllegalParameter,
290 "Server replied using a ciphersuite not allowed in version it offered");
303 if(!sh.extensions().has<Key_Share>()) {
304 throw TLS_Exception(Alert::IllegalParameter,
"Server Hello did not contain a key share extension");
307 auto* my_keyshare = ch.extensions().get<Key_Share>();
308 auto shared_secret = my_keyshare->decapsulate(*sh.extensions().get<Key_Share>(),
policy(),
callbacks(),
rng());
312 if(sh.extensions().has<
PSK>()) {
314 ch.extensions().get<
PSK>()->take_selected_psk_info(*sh.extensions().get<
PSK>(), cipher.value());
319 if(m_psk_identity.has_value() && m_resumed_session.has_value()) {
320 m_resumed_session.reset();
330 m_resumed_session.reset();
345 auto& ch = m_handshake_state.client_hello();
347 validate_server_hello_ish(ch, hrr);
353 auto allowed_exts = ch.extensions().extension_types();
355 if(hrr.extensions().contains_other_than(allowed_exts)) {
356 throw TLS_Exception(Alert::UnsupportedExtension,
"Unsupported extension found in Hello Retry Request");
378 const auto& exts = encrypted_extensions_msg.extensions();
385 const auto& requested_exts = m_handshake_state.client_hello().extensions().extension_types();
386 if(exts.contains_other_than(requested_exts)) {
387 throw TLS_Exception(Alert::UnsupportedExtension,
388 "Encrypted Extensions contained an extension that was not offered");
394 if(exts.has<Record_Size_Limit>() && m_handshake_state.client_hello().extensions().has<Record_Size_Limit>()) {
402 auto*
const outgoing_limit = exts.get<Record_Size_Limit>();
403 auto*
const incoming_limit = m_handshake_state.client_hello().extensions().get<Record_Size_Limit>();
407 if(exts.has<Server_Certificate_Type>() &&
408 m_handshake_state.client_hello().extensions().has<Server_Certificate_Type>()) {
409 const auto* server_cert_type = exts.get<Server_Certificate_Type>();
410 const auto* our_server_cert_types = m_handshake_state.client_hello().extensions().get<Server_Certificate_Type>();
411 our_server_cert_types->validate_selection(*server_cert_type);
425 if(m_handshake_state.server_hello().extensions().has<
PSK>()) {
440 throw TLS_Exception(Alert::DecodeError,
"Certificate_Request context must be empty in the main handshake");
448void Client_Impl_13::handle(
const Certificate_13& certificate_msg) {
452 if(!certificate_msg.request_context().empty()) {
453 throw TLS_Exception(Alert::DecodeError,
"Received a server certificate message with non-empty request context");
459 certificate_msg.validate_extensions(m_handshake_state.client_hello().extensions().extension_types(),
callbacks());
464 m_handshake_state.client_hello().extensions().has<Certificate_Status_Request>());
478 const auto offered = m_handshake_state.client_hello().signature_schemes();
479 if(!
value_exists(offered, certificate_verify_msg.signature_scheme())) {
480 throw TLS_Exception(Alert::IllegalParameter,
481 "We did not offer the usage of " + certificate_verify_msg.signature_scheme().to_string() +
482 " as a signature scheme");
485 const bool sig_valid = certificate_verify_msg.verify(
489 throw TLS_Exception(Alert::DecryptError,
"Server certificate verification failed");
497 const auto& cert_request = m_handshake_state.certificate_request();
499 const auto cert_type = [&] {
500 const auto& exts = m_handshake_state.encrypted_extensions().extensions();
501 const auto& chexts = m_handshake_state.client_hello().extensions();
502 if(exts.has<Client_Certificate_Type>() && chexts.has<Client_Certificate_Type>()) {
503 const auto* client_cert_type = exts.get<Client_Certificate_Type>();
504 chexts.get<Client_Certificate_Type>()->validate_selection(*client_cert_type);
513 return client_cert_type->selected_certificate_type();
527 flight.add(m_handshake_state.sending(
536 if(!m_handshake_state.client_certificate().empty()) {
537 flight.add(m_handshake_state.sending(Certificate_Verify_13(m_handshake_state.client_certificate(),
538 cert_request.signature_schemes(),
549void Client_Impl_13::handle(
const Finished_13& finished_msg) {
555 throw TLS_Exception(Alert::DecryptError,
"Finished message didn't verify");
565 m_resumed_session.has_value(),
578 if(m_handshake_state.has_certificate_request()) {
579 send_client_authentication(flight);
595 m_transitions.set_expected_next({});
601 callbacks().tls_examine_extensions(
604 const Session session(m_cipher_state->psk(new_session_ticket.nonce()),
605 new_session_ticket.early_data_byte_limit(),
606 new_session_ticket.ticket_age_add(),
607 new_session_ticket.lifetime_hint(),
608 m_handshake_state.server_hello().selected_version(),
609 m_handshake_state.server_hello().ciphersuite(),
612 peer_raw_public_key(),
614 callbacks().tls_current_timestamp());
616 if(callbacks().tls_should_persist_resumption_information(session)) {
617 session_manager().store(session,
Session_Handle(new_session_ticket.handle()));
622 if(m_handshake_state.has_server_certificate_msg() &&
623 m_handshake_state.server_certificate().has_certificate_chain()) {
624 return m_handshake_state.server_certificate().cert_chain();
627 if(m_resumed_session.has_value()) {
628 return m_resumed_session->session.peer_certs();
635 if(m_handshake_state.has_server_certificate_msg() && m_handshake_state.server_certificate().is_raw_public_key()) {
636 return m_handshake_state.server_certificate().public_key();
639 if(m_resumed_session.has_value()) {
640 return m_resumed_session->session.peer_raw_public_key();
647 return m_psk_identity;
651 return std::exchange(m_should_send_ccs,
false);
654void Client_Impl_13::maybe_log_secret(std::string_view label, std::span<const uint8_t> secret)
const {
655 if(
policy().allow_ssl_key_log_file()) {
662 const auto& eee = m_handshake_state.encrypted_extensions().extensions();
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
virtual void tls_session_activated()
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 void tls_ssl_key_log_data(std::string_view label, std::span< const uint8_t > client_random, std::span< const uint8_t > secret) const
virtual void tls_inspect_handshake_msg(const Handshake_Message &message)
const Policy & policy() const
AggregatedHandshakeMessages aggregate_handshake_messages()
Credentials_Manager & credentials_manager()
RandomNumberGenerator & rng()
std::vector< uint8_t > send_handshake_message(const std::variant< MsgTs... > &message)
virtual bool prepend_ccs()
Transcript_Hash_State m_transcript_hash
Channel_Impl_13(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< Credentials_Manager > &credentials_manager, const std::shared_ptr< RandomNumberGenerator > &rng, const std::shared_ptr< const Policy > &policy, bool is_server)
virtual void process_dummy_change_cipher_spec()=0
Session_Manager & session_manager()
std::unique_ptr< Cipher_State > m_cipher_state
const Connection_Side m_side
Callbacks & callbacks() const
void set_selected_certificate_type(Certificate_Type cert_type)
void set_record_size_limits(uint16_t outgoing_limit, uint16_t incoming_limit)
void request_downgrade_for_resumption(Session_with_Handle session)
void preserve_client_hello(std::span< const uint8_t > msg)
bool expects_downgrade() const
static std::unique_ptr< Cipher_State > init_with_server_hello(Connection_Side side, secure_vector< uint8_t > &&shared_secret, const Ciphersuite &cipher, const Transcript_Hash &transcript_hash, const Secret_Logger &channel)
static std::optional< Ciphersuite > by_id(uint16_t suite)
const std::vector< uint8_t > & random() const
Client_Impl_13(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(), const std::vector< std::string > &next_protocols={})
std::optional< std::string > external_psk_identity() const override
std::string application_protocol() const override
std::shared_ptr< const Public_Key > peer_raw_public_key() const override
bool is_handshake_complete() const override
std::vector< X509_Certificate > peer_cert_chain() const override
decltype(auto) received(Handshake_Message_13 message)
void confirm_transition_to(Handshake_Type msg_type)
bool has_hello_retry_request() const
Client_Hello_13 & client_hello()
Helper class to embody a session handle in all protocol versions.
virtual std::vector< Session_with_Handle > find(const Server_Information &info, Callbacks &callbacks, const Policy &policy)
Find all sessions that match a given server info.
bool supports(Protocol_Version version) const
static Transcript_Hash_State recreate_after_hello_retry_request(std::string_view algo_spec, const Transcript_Hash_State &prev_transcript_hash_state)
std::variant< Client_Hello_13, Client_Hello_12_Shim, Server_Hello_13, Server_Hello_12_Shim, Hello_Retry_Request, Encrypted_Extensions, Certificate_13, Certificate_Request_13, Certificate_Verify_13, Finished_13 > Handshake_Message_13
std::variant< New_Session_Ticket_13, Key_Update > Post_Handshake_Message_13
bool value_exists(const std::vector< T > &vec, const V &val)