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) {
33 auto self = std::make_shared<Client_Impl_13>(
36#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
37 if(
policy->allow_tls12()) {
38 self->expect_downgrade(self->m_info, next_protocols);
42 if(
auto session = self->find_session_for_resumption()) {
43 if(session->session.version().is_tls_13_or_later()) {
44 self->m_handshake->resumed_session = std::move(session);
46#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
47 else if(self->expects_downgrade()) {
50 self->request_downgrade_for_resumption(std::move(session.value()));
58 auto flight =
Flight(*self->m_transcript_hash, self->callbacks())
59 .
add(self->m_handshake->state.sending(
63 self->m_info.hostname(),
65 self->m_handshake->resumed_session,
76 self->send_flight(flight.commit());
88 m_handshake->transitions.confirm_transition_to(std::visit([](
const auto& msg) {
return msg.type(); }, message));
98 m_handshake->state.received(std::move(message)));
106 throw TLS_Exception(Alert::UnexpectedMessage,
"received an unexpected post-handshake message");
122 if(!m_handshake || !m_handshake->state.has_client_hello() || m_handshake->state.has_server_finished()) {
123 throw TLS_Exception(Alert::UnexpectedMessage,
"Received an unexpected dummy Change Cipher Spec");
139std::optional<Session_with_Handle> Client_Impl_13::find_session_for_resumption() {
156 if(sessions.empty()) {
163 auto& session_to_resume = sessions.front();
165 return std::move(session_to_resume);
168void Client_Impl_13::handle(
const Server_Hello_12_Shim& server_hello_msg) {
171 if(m_handshake->state.has_hello_retry_request()) {
172 throw TLS_Exception(Alert::UnexpectedMessage,
"Version downgrade received after Hello Retry");
175#if !defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
177 throw TLS_Exception(Alert::ProtocolVersion,
"Received an unsupported Server Hello");
185 throw TLS_Exception(Alert::ProtocolVersion,
"Received an unexpected legacy Server Hello");
198 if(server_hello_msg.random_signals_downgrade().has_value()) {
199 throw TLS_Exception(Alert::IllegalParameter,
"Downgrade attack detected");
208 if(server_hello_msg.extensions().has<Supported_Versions>()) {
209 throw TLS_Exception(Alert::IllegalParameter,
"Unexpected extension received");
213 const auto& sh_exts = server_hello_msg.extensions();
220 if(sh_exts.has(tls13_only)) {
221 throw TLS_Exception(Alert::IllegalParameter,
"TLS 1.2 Server Hello contained a TLS 1.3 extension");
229 const auto& client_hello_exts = m_handshake->state.client_hello().extensions();
231 if(!client_hello_exts.get<Supported_Versions>()->supports(server_hello_msg.selected_version())) {
232 throw TLS_Exception(Alert::ProtocolVersion,
"Protocol version was not offered");
235 if(
policy().tls_13_middlebox_compatibility_mode() &&
236 m_handshake->state.client_hello().session_id() == server_hello_msg.session_id()) {
241 throw TLS_Exception(Alert::IllegalParameter,
"Unexpected session ID during downgrade");
244 preserve_client_hello(m_handshake->state.take_client_hello());
249 m_handshake->transitions.set_expected_next({});
259 if(ch.session_id() != sh.session_id()) {
260 throw TLS_Exception(Alert::IllegalParameter,
"echoed session id did not match");
266 if(!ch.offered_suite(sh.ciphersuite())) {
267 throw TLS_Exception(Alert::IllegalParameter,
"Server replied with ciphersuite we didn't send");
278 throw TLS_Exception(Alert::IllegalParameter,
"Protocol version was not offered");
289 const auto& ch = m_handshake->state.client_hello();
291 validate_server_hello_ish(ch, sh);
299 if(sh.random_signals_downgrade().has_value()) {
300 throw TLS_Exception(Alert::IllegalParameter,
"Downgrade attack detected");
308 if(sh.extensions().contains_other_than(ch.extensions().extension_types())) {
309 throw TLS_Exception(Alert::UnsupportedExtension,
"Unsupported extension found in Server Hello");
312 if(m_handshake->state.has_hello_retry_request()) {
313 const auto& hrr = m_handshake->state.hello_retry_request();
319 if(hrr.ciphersuite() != sh.ciphersuite()) {
320 throw TLS_Exception(Alert::IllegalParameter,
"server changed its chosen ciphersuite");
327 if(hrr.selected_version() != sh.selected_version()) {
328 throw TLS_Exception(Alert::IllegalParameter,
"server changed its chosen protocol version");
339 if(!cipher->usable_in_version(Protocol_Version::TLS_V13)) {
340 throw TLS_Exception(Alert::IllegalParameter,
341 "Server replied using a ciphersuite not allowed in version it offered");
354 if(!sh.extensions().has<Key_Share>()) {
355 throw TLS_Exception(Alert::IllegalParameter,
"Server Hello did not contain a key share extension");
358 auto* my_keyshare = ch.extensions().get<Key_Share>();
359 auto shared_secret = my_keyshare->decapsulate(*sh.extensions().get<Key_Share>(),
policy(),
callbacks(),
rng());
364 if(sh.extensions().has<
PSK>()) {
365 std::unique_ptr<Cipher_State> new_cipher_state;
366 std::tie(m_handshake->psk_identity, new_cipher_state) =
367 ch.extensions().get<
PSK>()->take_selected_psk_info(*sh.extensions().get<
PSK>(), cipher.value());
373 if(m_handshake->psk_identity.has_value() && m_handshake->resumed_session.has_value()) {
374 m_handshake->resumed_session.reset();
381 new_cipher_state->advance_with_server_hello(
384 return new_cipher_state;
386 m_handshake->resumed_session.reset();
389 std::move(shared_secret),
408 auto& ch = m_handshake->state.client_hello();
410 validate_server_hello_ish(ch, hrr);
416 auto allowed_exts = ch.extensions().extension_types();
418 if(hrr.extensions().contains_other_than(allowed_exts)) {
419 throw TLS_Exception(Alert::UnsupportedExtension,
"Unsupported extension found in Hello Retry Request");
428 if(!cipher->usable_in_version(Protocol_Version::TLS_V13)) {
429 throw TLS_Exception(Alert::IllegalParameter,
"HelloRetryRequest selected a cipher suite not usable in TLS 1.3");
450 flight.add_dummy_change_cipher_spec();
465 const auto& exts = encrypted_extensions_msg.extensions();
472 const auto& requested_exts = m_handshake->state.client_hello().extensions().extension_types();
473 if(exts.contains_other_than(requested_exts)) {
474 throw TLS_Exception(Alert::UnsupportedExtension,
475 "Encrypted Extensions contained an extension that was not offered");
481 if(exts.has<Application_Layer_Protocol_Notification>()) {
486 const auto* server_alpn = exts.get<Application_Layer_Protocol_Notification>();
487 const auto selected = server_alpn->single_protocol();
488 const auto* client_alpn =
489 m_handshake->state.client_hello().extensions().get<Application_Layer_Protocol_Notification>();
491 const auto& offered = client_alpn->protocols();
493 throw TLS_Exception(Alert::IllegalParameter,
"Server selected an ALPN protocol not offered by the client");
497 if(exts.has<Record_Size_Limit>() && m_handshake->state.client_hello().extensions().has<Record_Size_Limit>()) {
505 const auto*
const outgoing_limit = exts.get<Record_Size_Limit>();
506 const auto*
const incoming_limit = m_handshake->state.client_hello().extensions().get<Record_Size_Limit>();
510 if(exts.has<Server_Certificate_Type>()) {
512 BOTAN_ASSERT_NOMSG(m_handshake->state.client_hello().extensions().has<Server_Certificate_Type>());
513 const auto* server_cert_type = exts.get<Server_Certificate_Type>();
514 const auto* our_server_cert_types = m_handshake->state.client_hello().extensions().get<Server_Certificate_Type>();
515 our_server_cert_types->validate_selection(*server_cert_type);
527 if(
const auto* client_cert_type = exts.get<Client_Certificate_Type>()) {
532 m_handshake->state.client_hello().extensions().get<Client_Certificate_Type>()->validate_selection(
544 if(m_handshake->state.server_hello().extensions().has<
PSK>()) {
545 throw TLS_Exception(Alert::IllegalParameter,
546 "Server selected a client certificate type without requesting a certificate");
552 if(m_handshake->state.server_hello().extensions().has<
PSK>()) {
569 throw TLS_Exception(Alert::DecodeError,
"Certificate_Request context must be empty in the main handshake");
577void Client_Impl_13::handle(
const Certificate_13& certificate_msg) {
583 if(!certificate_msg.request_context().empty()) {
584 throw TLS_Exception(Alert::DecodeError,
"Received a server certificate message with non-empty request context");
590 certificate_msg.validate_extensions(m_handshake->state.client_hello().extensions().extension_types(),
callbacks());
595 m_handshake->state.client_hello().extensions().has<Certificate_Status_Request>());
612 const auto offered = m_handshake->state.client_hello().signature_schemes();
613 if(!
value_exists(offered, certificate_verify_msg.signature_scheme())) {
614 throw TLS_Exception(Alert::IllegalParameter,
615 "We did not offer the usage of " + certificate_verify_msg.signature_scheme().to_string() +
616 " as a signature scheme");
619 const bool sig_valid = certificate_verify_msg.verify(
623 throw TLS_Exception(Alert::DecryptError,
"Server certificate verification failed");
629void Client_Impl_13::create_client_authentication_flight(
Flight& flight) {
633 const auto& cert_request = m_handshake->state.certificate_request();
635 const auto cert_type = [&] {
636 const auto& exts = m_handshake->state.encrypted_extensions().extensions();
637 const auto& chexts = m_handshake->state.client_hello().extensions();
638 if(exts.has<Client_Certificate_Type>()) {
641 const auto* client_cert_type = exts.get<Client_Certificate_Type>();
642 chexts.get<Client_Certificate_Type>()->validate_selection(*client_cert_type);
651 return client_cert_type->selected_certificate_type();
665 flight.add(m_handshake->state.sending(
674 if(!m_handshake->state.client_certificate().empty()) {
675 flight.add(m_handshake->state.sending(Certificate_Verify_13(m_handshake->state.client_certificate(),
676 cert_request.signature_schemes(),
687void Client_Impl_13::handle(
const Finished_13& finished_msg) {
696 throw TLS_Exception(Alert::DecryptError,
"Finished message didn't verify");
699 m_handshake->state.confirm_peer_finished_verified();
701 if(m_handshake->state.encrypted_extensions().extensions().has<Client_Certificate_Type>() &&
702 !m_handshake->state.has_certificate_request()) {
703 throw TLS_Exception(Alert::IllegalParameter,
704 "Server selected a client certificate type without requesting a certificate");
714 m_handshake->resumed_session.has_value(),
734 flight.add_dummy_change_cipher_spec();
740 if(m_handshake->state.has_certificate_request()) {
741 create_client_authentication_flight(flight);
757 m_handshake->transitions.set_expected_next({});
763 const auto extract_certs = [&]() -> std::vector<X509_Certificate> {
764 if(m_handshake->state.has_server_certificate_msg() &&
765 m_handshake->state.server_certificate().has_certificate_chain()) {
766 return m_handshake->state.server_certificate().cert_chain();
768 if(m_handshake->resumed_session.has_value()) {
769 return m_handshake->resumed_session->session.peer_certs();
774 const auto extract_raw_pk = [&]() -> std::shared_ptr<const Public_Key> {
775 if(m_handshake->state.has_server_certificate_msg() &&
776 m_handshake->state.server_certificate().is_raw_public_key()) {
777 return m_handshake->state.server_certificate().public_key();
779 if(m_handshake->resumed_session.has_value()) {
780 return m_handshake->resumed_session->session.peer_raw_public_key();
788 m_handshake->psk_identity,
801 if(
const size_t max_tickets = policy().maximum_session_tickets_per_connection();
802 max_tickets > 0 && m_session_tickets_received >= max_tickets) {
807 ++m_session_tickets_received;
809 callbacks().tls_examine_extensions(
812 const Session session(m_cipher_state->psk(new_session_ticket.nonce()),
813 new_session_ticket.early_data_byte_limit(),
814 new_session_ticket.ticket_age_add(),
815 new_session_ticket.lifetime_hint(),
816 m_active_state->version(),
817 m_active_state->ciphersuite_code(),
820 peer_raw_public_key(),
822 callbacks().tls_current_timestamp());
824 if(callbacks().tls_should_persist_resumption_information(session)) {
825 session_manager().store(session,
Session_Handle(new_session_ticket.handle()));
836 if(m_handshake->state.has_server_certificate_msg() &&
837 m_handshake->state.server_certificate().has_certificate_chain()) {
838 return m_handshake->state.server_certificate().cert_chain();
841 if(m_handshake->resumed_session.has_value()) {
842 return m_handshake->resumed_session->session.peer_certs();
856 if(m_handshake->state.has_server_certificate_msg() &&
857 m_handshake->state.server_certificate().is_raw_public_key()) {
858 return m_handshake->state.server_certificate().public_key();
861 if(m_handshake->resumed_session.has_value()) {
862 return m_handshake->resumed_session->session.peer_raw_public_key();
874 return m_handshake->psk_identity;
883 if(!
policy().tls_13_middlebox_compatibility_mode()) {
890void Client_Impl_13::maybe_log_secret(std::string_view label, std::span<const uint8_t> secret)
const {
891 if(
policy().allow_ssl_key_log_file()) {
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
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)
std::shared_ptr< Cipher_State > m_cipher_state
Cipher_State & setup_cipher_state(std::unique_ptr< Cipher_State > cipher_state)
const Policy & policy() const
void send_flight(std::vector< Flight::Message > flight)
Credentials_Manager & credentials_manager()
RandomNumberGenerator & rng()
std::optional< Transcript_Hash_State > m_transcript_hash
virtual bool compat_mode_ccs_requested() const =0
std::optional< Active_Connection_State_13 > m_active_state
SecretLoggerFn secret_logger() const
virtual void process_dummy_change_cipher_spec()=0
Session_Manager & session_manager()
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)
bool expects_downgrade() const
static std::unique_ptr< Cipher_State > init_with_server_hello(Connection_Side side, TLS_Flavor flavor, secure_vector< uint8_t > &&shared_secret, const Ciphersuite &cipher, const Transcript_Hash &transcript_hash, SecretLoggerFn secret_logger)
static std::optional< Ciphersuite > by_id(uint16_t suite)
std::optional< std::string > external_psk_identity() const override
static std::shared_ptr< Client_Impl_13 > create(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::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
Flight & add(Handshake_Message_13_Ref msg) &
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
constexpr std::optional< SpecificVariantT > specialize_to(GeneralVariantT &&v)
Converts a given variant into another variant whose type states are a subset of the given variant.
bool value_exists(const std::vector< T > &vec, const V &val)