9#include <botan/internal/tls_server_impl_13.h>
11#include <botan/credentials_manager.h>
13#include <botan/tls_callbacks.h>
14#include <botan/tls_extensions_13.h>
15#include <botan/tls_policy.h>
16#include <botan/x509cert.h>
17#include <botan/internal/loadstor.h>
18#include <botan/internal/stl_util.h>
19#include <botan/internal/tls_cipher_state.h>
26 const std::shared_ptr<const Policy>&
policy,
27 const std::shared_ptr<RandomNumberGenerator>&
rng) {
31#if defined(BOTAN_HAS_TLS_12)
32 if(
policy->allow_tls12()) {
33 self->expect_downgrade({}, {});
56 if(m_handshake->state.has_client_certificate_msg() &&
57 m_handshake->state.client_certificate().has_certificate_chain()) {
58 return m_handshake->state.client_certificate().cert_chain();
61 if(m_handshake->resumed_session.has_value()) {
62 return m_handshake->resumed_session->peer_certs();
75 if(m_handshake->state.has_client_certificate_msg() &&
76 m_handshake->state.client_certificate().is_raw_public_key()) {
77 return m_handshake->state.client_certificate().public_key();
80 if(m_handshake->resumed_session.has_value()) {
81 return m_handshake->resumed_session->peer_raw_public_key();
91 }
else if(m_handshake) {
92 return m_handshake->psk_identity;
121 size_t tickets_created = 0;
129 const std::chrono::seconds max_ticket_lifetime(604800);
131 const auto ticket_lifetime =
132 (policy_lifetime.count() <= 0 || policy_lifetime > max_ticket_lifetime) ? max_ticket_lifetime : policy_lifetime;
134 for(
size_t i = 0; i < tickets; ++i) {
136 const uint32_t ticket_age_add =
load_be(
rng().random_array<4>());
149 if(
callbacks().tls_should_persist_resumption_information(session)) {
157 if(tickets_created > 0) {
161 return tickets_created;
169 m_handshake->transitions.confirm_transition_to(std::visit([](
const auto& msg) {
return msg.type(); }, message));
179 m_handshake->state.received(std::move(message)));
187 throw TLS_Exception(Alert::UnexpectedMessage,
"Received an unexpected post-handshake message");
203 if(!m_handshake || !m_handshake->state.has_client_hello() || m_handshake->state.has_client_finished()) {
204 throw TLS_Exception(Alert::UnexpectedMessage,
"Received an unexpected dummy Change Cipher Spec");
219 (m_handshake !=
nullptr && m_handshake->state.handshake_finished() && !
is_closed());
222void Server_Impl_13::maybe_log_secret(std::string_view label, std::span<const uint8_t> secret)
const {
223 if(
policy().allow_ssl_key_log_file()) {
232#if defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
234void Server_Impl_13::downgrade() {
241 m_handshake->transitions.set_expected_next({});
253 if(m_handshake ==
nullptr || !m_handshake->state.has_client_hello()) {
269 const bool client_requested_compatibility_mode = !m_handshake->state.client_hello().session_id().empty();
274void Server_Impl_13::handle_reply_to_client_hello(
Server_Hello_13 server_hello) {
275 const auto& client_hello = m_handshake->state.client_hello();
276 const auto& exts = client_hello.extensions();
278 bool uses_psk = server_hello.extensions().has<
PSK>();
282 const auto& cipher = cipher_opt.value();
286 std::unique_ptr<Cipher_State> psk_cipher_state;
288 auto* psk_extension = server_hello.extensions().get<
PSK>();
290 psk_cipher_state = std::visit(
292 [&,
this](Session session) {
293 m_handshake->resumed_session = std::move(session);
297 m_handshake->resumed_session->extract_master_secret(),
300 [&,
this](ExternalPSK psk) {
301 m_handshake->psk_identity = psk.identity();
302 const auto psk_type =
308 psk_extension->take_session_to_resume_or_psk());
322 if(!exts.get<
PSK>()->validate_binder(*psk_extension,
324 throw TLS_Exception(Alert::DecryptError,
"PSK binder does not check out");
334 if(m_handshake->resumed_session.has_value() &&
policy().require_client_certificate_authentication()) {
335 const auto credential_type = m_handshake->resumed_session->peer_credential_type();
337 if(!credential_type.has_value() ||
338 !
value_exists(
policy().accepted_client_certificate_types(), credential_type.value())) {
339 m_handshake->resumed_session.reset();
340 psk_cipher_state.reset();
341 server_hello.decline_psk();
364 .add(m_handshake->state.sending(std::move(server_hello)));
371 server_hello_flight.add_dummy_change_cipher_spec();
379 auto*
const my_keyshare = m_handshake->state.server_hello().extensions().get<Key_Share>();
385 psk_cipher_state->advance_with_server_hello(
388 return std::move(psk_cipher_state);
392 my_keyshare->take_shared_secret(),
402 auto certificate_request =
403 uses_psk ? std::nullopt
406 const bool is_resumption = m_handshake->resumed_session.has_value();
407 const bool requesting_client_auth = certificate_request.has_value();
409 .add(m_handshake->state.sending(Encrypted_Extensions(
410 client_hello,
policy(),
callbacks(), is_resumption, requesting_client_auth)));
417 if(certificate_request.has_value()) {
418 flight.add(m_handshake->state.sending(std::move(certificate_request.value())));
421 const auto& enc_exts = m_handshake->state.encrypted_extensions().extensions();
430 if(
const auto* client_cert_type = enc_exts.get<Client_Certificate_Type>()) {
439 const auto cert_type = [&] {
440 if(
const auto* server_cert_type = enc_exts.get<Server_Certificate_Type>()) {
441 return server_cert_type->selected_certificate_type();
448 throw TLS_Exception(Alert::UnsupportedCertificate,
449 "Client does not support a certificate type that our policy allows");
454 .add(m_handshake->state.sending(Certificate_Verify_13(m_handshake->state.server_certificate(),
455 client_hello.signature_schemes(),
456 client_hello.sni_hostname(),
467 if(client_hello.extensions().has<Record_Size_Limit>() &&
468 m_handshake->state.encrypted_extensions().extensions().has<Record_Size_Limit>()) {
484 const auto*
const outgoing_limit = client_hello.extensions().get<Record_Size_Limit>();
485 const auto*
const incoming_limit =
486 m_handshake->state.encrypted_extensions().extensions().get<Record_Size_Limit>();
494 if(m_handshake->state.has_certificate_request()) {
507void Server_Impl_13::handle_reply_to_client_hello(
Hello_Retry_Request hello_retry_request) {
514 .add(m_handshake->state.sending(std::move(hello_retry_request)));
521 flight.add_dummy_change_cipher_spec();
539 if(m_handshake->state.has_hello_retry_request()) {
540 throw TLS_Exception(Alert::UnexpectedMessage,
"Received a TLS 1.2 Client Hello after Hello Retry Request");
543#if !defined(BOTAN_HAS_TLS_DOWNGRADE_SUPPORT)
544 throw TLS_Exception(Alert::ProtocolVersion,
"Received an unsupported Client Hello");
554 throw TLS_Exception(Alert::ProtocolVersion,
"Received a legacy Client Hello");
564 const auto& exts = client_hello.extensions();
566 const bool is_initial_client_hello = !m_handshake->state.has_hello_retry_request();
568 if(is_initial_client_hello) {
569 const auto preferred_version = client_hello.highest_supported_version(
policy());
570 if(!preferred_version) {
571 throw TLS_Exception(Alert::ProtocolVersion,
"No shared TLS version");
578 throw TLS_Exception(Alert::IllegalParameter,
"Received a Cookie in the initial client hello");
584 if(!exts.has<Supported_Groups>()) {
589 const auto* psk_modes = exts.get<PSK_Key_Exchange_Modes>();
591 throw TLS_Exception(Alert::MissingExtension,
"Client offered psk_dhe_ke without a key_share");
593 throw TLS_Exception(Alert::HandshakeFailure,
"PSK-only key exchange is not supported");
603 if(!is_initial_client_hello) {
604 const auto& hrr_exts = m_handshake->state.hello_retry_request().extensions();
605 const auto offered_groups = exts.get<Key_Share>()->offered_groups();
606 const auto* hrr_key_share = hrr_exts.get<Key_Share>();
608 const auto selected_group = hrr_key_share->selected_group();
609 if(offered_groups.size() != 1 || offered_groups.at(0) != selected_group) {
610 throw TLS_Exception(Alert::IllegalParameter,
"Client did not comply with the requested key exchange group");
617 const auto* hrr_cookie = hrr_exts.get<
Cookie>();
618 const auto* client_cookie = exts.get<
Cookie>();
619 if((hrr_cookie ==
nullptr) != (client_cookie ==
nullptr) ||
620 (hrr_cookie !=
nullptr && hrr_cookie->get_cookie() != client_cookie->get_cookie())) {
621 throw TLS_Exception(Alert::IllegalParameter,
"Client did not echo the cookie of the HelloRetryRequest");
626 std::visit([
this](
auto msg) { handle_reply_to_client_hello(std::move(msg)); },
628 is_initial_client_hello,
636void Server_Impl_13::handle(
const Certificate_13& certificate_msg) {
643 throw TLS_Exception(Alert::DecodeError,
"Received a client certificate message with non-empty request context");
649 certificate_msg.validate_extensions(m_handshake->state.certificate_request().extensions().extension_types(),
657 if(certificate_msg.empty()) {
658 if(
policy().require_client_certificate_authentication()) {
659 throw TLS_Exception(Alert::CertificateRequired,
"Policy requires client send a certificate, but it did not");
677 const bool use_ocsp = m_handshake->state.certificate_request().extensions().has<Certificate_Status_Request>();
678 certificate_msg.verify(
699 const auto offered = m_handshake->state.certificate_request().signature_schemes();
700 if(!
value_exists(offered, certificate_verify_msg.signature_scheme())) {
701 throw TLS_Exception(Alert::IllegalParameter,
702 "We did not offer the usage of " + certificate_verify_msg.signature_scheme().to_string() +
703 " as a signature scheme");
707 !m_handshake->state.client_certificate().empty());
708 const bool sig_valid = certificate_verify_msg.verify(
715 throw TLS_Exception(Alert::DecryptError,
"Client certificate verification failed");
721void Server_Impl_13::handle(
const Finished_13& finished_msg) {
730 throw TLS_Exception(Alert::DecryptError,
"Finished message didn't verify");
733 m_handshake->state.confirm_peer_finished_verified();
738 Session_Summary(m_handshake->state.server_hello(),
742 m_handshake->psk_identity,
743 m_handshake->resumed_session.has_value(),
744 Server_Information(m_handshake->state.client_hello().sni_hostname()),
750 m_handshake->transitions.set_expected_next({});
754 const auto extract_certs = [&]() -> std::vector<X509_Certificate> {
755 if(m_handshake->state.has_client_certificate_msg() &&
756 m_handshake->state.client_certificate().has_certificate_chain()) {
757 return m_handshake->state.client_certificate().cert_chain();
759 if(m_handshake->resumed_session.has_value()) {
760 return m_handshake->resumed_session->peer_certs();
765 const auto extract_raw_pk = [&]() -> std::shared_ptr<const Public_Key> {
766 if(m_handshake->state.has_client_certificate_msg() &&
767 m_handshake->state.client_certificate().is_raw_public_key()) {
768 return m_handshake->state.client_certificate().public_key();
770 if(m_handshake->resumed_session.has_value()) {
771 return m_handshake->resumed_session->peer_raw_public_key();
776 const bool supports_psk_dhe =
777 m_handshake->state.client_hello().extensions().has<PSK_Key_Exchange_Modes>() &&
778 value_exists(m_handshake->state.client_hello().extensions().get<PSK_Key_Exchange_Modes>()->modes(),
784 m_handshake->psk_identity,
785 m_handshake->state.client_hello().sni_hostname(),
#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)
static std::optional< Certificate_Request_13 > maybe_create(const Client_Hello_13 &sni_hostname, Credentials_Manager &cred_mgr, Callbacks &callbacks, const Policy &policy)
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()
bool is_closed() const override
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_psk(Connection_Side side, TLS_Flavor flavor, PSK_Type type, secure_vector< uint8_t > &&psk, std::string_view prf_algo)
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)
virtual bool tls_13_middlebox_compatibility_mode() const
virtual std::chrono::seconds session_ticket_lifetime() const
static std::variant< Hello_Retry_Request, Server_Hello_13 > create(const Client_Hello_13 &ch, bool hello_retry_request_allowed, Session_Manager &session_mgr, Credentials_Manager &credentials_mgr, RandomNumberGenerator &rng, const Policy &policy, Callbacks &cb)
std::vector< X509_Certificate > peer_cert_chain() const override
size_t send_new_session_tickets(size_t tickets) override
std::optional< std::string > external_psk_identity() const override
std::shared_ptr< const Public_Key > peer_raw_public_key() const override
bool is_handshake_complete() const override
static std::shared_ptr< Server_Impl_13 > create(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< const Policy > &policy, const std::shared_ptr< RandomNumberGenerator > &rng)
std::string application_protocol() const override
bool new_session_ticket_supported() const override
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)
overloaded(Ts...) -> overloaded< Ts... >
constexpr auto load_be(ParamTs &&... params)