9#include <botan/internal/tls_server_impl_13.h>
11#include <botan/credentials_manager.h>
13#include <botan/internal/loadstor.h>
14#include <botan/internal/stl_util.h>
15#include <botan/internal/tls_cipher_state.h>
20 const std::shared_ptr<Session_Manager>& session_manager,
21 const std::shared_ptr<Credentials_Manager>& credentials_manager,
22 const std::shared_ptr<const Policy>& policy,
23 const std::shared_ptr<RandomNumberGenerator>& rng) :
24 Channel_Impl_13(callbacks, session_manager, credentials_manager, rng, policy, true ) {
25#if defined(BOTAN_HAS_TLS_12)
27 expect_downgrade({}, {});
34std::string Server_Impl_13::application_protocol()
const {
35 if(is_handshake_complete()) {
36 const auto& eee = m_handshake_state.encrypted_extensions().extensions();
38 return alpn->single_protocol();
45std::vector<X509_Certificate> Server_Impl_13::peer_cert_chain()
const {
46 if(m_handshake_state.has_client_certificate_msg() &&
47 m_handshake_state.client_certificate().has_certificate_chain()) {
48 return m_handshake_state.client_certificate().cert_chain();
51 if(m_resumed_session.has_value()) {
52 return m_resumed_session->peer_certs();
58std::shared_ptr<const Public_Key> Server_Impl_13::peer_raw_public_key()
const {
59 if(m_handshake_state.has_client_certificate_msg() && m_handshake_state.client_certificate().is_raw_public_key()) {
60 return m_handshake_state.client_certificate().public_key();
63 if(m_resumed_session.has_value()) {
64 return m_resumed_session->peer_raw_public_key();
70std::optional<std::string> Server_Impl_13::external_psk_identity()
const {
71 return m_psk_identity;
74bool Server_Impl_13::new_session_ticket_supported()
const {
85 return is_handshake_complete() && m_handshake_state.client_hello().extensions().has<
PSK_Key_Exchange_Modes>() &&
87 PSK_Key_Exchange_Mode::PSK_DHE_KE);
90size_t Server_Impl_13::send_new_session_tickets(
const size_t tickets) {
97 auto flight = aggregate_post_handshake_messages();
98 size_t tickets_created = 0;
100 for(
size_t i = 0; i < tickets; ++i) {
101 auto nonce = m_cipher_state->next_ticket_nonce();
102 const Session session(m_cipher_state->psk(nonce),
104 policy().session_ticket_lifetime(),
106 peer_raw_public_key(),
107 m_handshake_state.client_hello(),
108 m_handshake_state.server_hello(),
112 if(callbacks().tls_should_persist_resumption_information(session)) {
113 if(
auto handle = session_manager().establish(session)) {
120 if(flight.contains_messages()) {
124 return tickets_created;
131 m_transitions.confirm_transition_to(msg.get().type());
134 callbacks().tls_inspect_handshake_msg(msg.get());
139 m_handshake_state.received(std::move(message)));
142void Server_Impl_13::process_post_handshake_msg(Post_Handshake_Message_13 message) {
145 std::visit([&](
auto msg) { handle(msg); }, m_handshake_state.received(std::move(message)));
148void Server_Impl_13::process_dummy_change_cipher_spec() {
153 if(!m_handshake_state.has_client_hello() || m_handshake_state.has_client_finished()) {
154 throw TLS_Exception(Alert::UnexpectedMessage,
"Received an unexpected dummy Change Cipher Spec");
166bool Server_Impl_13::is_handshake_complete()
const {
167 return m_handshake_state.handshake_finished();
170void Server_Impl_13::maybe_log_secret(std::string_view label, std::span<const uint8_t> secret)
const {
171 if(policy().allow_ssl_key_log_file()) {
172 callbacks().tls_ssl_key_log_data(label, m_handshake_state.client_hello().random(), secret);
176void Server_Impl_13::downgrade() {
183 m_transitions.set_expected_next({});
186void Server_Impl_13::maybe_handle_compatibility_mode() {
188 BOTAN_ASSERT_NOMSG(m_handshake_state.has_hello_retry_request() || m_handshake_state.has_server_hello());
210 const bool just_after_first_handshake_message =
211 m_handshake_state.has_hello_retry_request() ^ m_handshake_state.has_server_hello();
212 const bool client_requested_compatibility_mode = !m_handshake_state.client_hello().session_id().empty();
214 if(just_after_first_handshake_message &&
215 (policy().tls_13_middlebox_compatibility_mode() || client_requested_compatibility_mode)) {
216 send_dummy_change_cipher_spec();
220void Server_Impl_13::handle_reply_to_client_hello(Server_Hello_13 server_hello) {
221 const auto& client_hello = m_handshake_state.client_hello();
222 const auto& exts = client_hello.extensions();
224 const bool uses_psk = server_hello.extensions().has<
PSK>();
226 const auto cipher_opt = Ciphersuite::by_id(server_hello.ciphersuite());
228 const auto& cipher = cipher_opt.value();
229 m_transcript_hash.set_algorithm(cipher.prf_algo());
231 std::unique_ptr<Cipher_State> psk_cipher_state;
233 auto psk_extension = server_hello.extensions().get<
PSK>();
236 std::visit(
overloaded{[&,
this](Session session) {
237 m_resumed_session = std::move(session);
238 return Cipher_State::init_with_psk(Connection_Side::Server,
239 Cipher_State::PSK_Type::Resumption,
240 m_resumed_session->extract_master_secret(),
243 [&,
this](ExternalPSK psk) {
244 m_psk_identity = psk.identity();
245 return Cipher_State::init_with_psk(Connection_Side::Server,
246 Cipher_State::PSK_Type::External,
247 psk.extract_master_secret(),
250 psk_extension->take_session_to_resume_or_psk());
263 if(!exts.get<PSK>()->validate_binder(*psk_extension,
264 psk_cipher_state->psk_binder_mac(m_transcript_hash.truncated()))) {
265 throw TLS_Exception(Alert::DecryptError,
"PSK binder does not check out");
285 send_handshake_message(m_handshake_state.sending(std::move(server_hello)));
286 maybe_handle_compatibility_mode();
289 m_cipher_state = [&] {
291 const auto my_keyshare = m_handshake_state.server_hello().extensions().get<Key_Share>();
296 psk_cipher_state->advance_with_client_hello(m_transcript_hash.previous(), *
this);
297 psk_cipher_state->advance_with_server_hello(
298 cipher, my_keyshare->take_shared_secret(), m_transcript_hash.current(), *
this);
300 return std::move(psk_cipher_state);
302 return Cipher_State::init_with_server_hello(
303 m_side, my_keyshare->take_shared_secret(), cipher, m_transcript_hash.current(), *
this);
307 auto flight = aggregate_handshake_messages();
308 flight.add(m_handshake_state.sending(Encrypted_Extensions(client_hello, policy(), callbacks())));
315 if(
auto certificate_request =
316 Certificate_Request_13::maybe_create(client_hello, credentials_manager(), callbacks(), policy())) {
317 flight.add(m_handshake_state.sending(std::move(certificate_request.value())));
320 const auto& enc_exts = m_handshake_state.encrypted_extensions().extensions();
329 if(
auto client_cert_type = enc_exts.get<Client_Certificate_Type>()) {
330 set_selected_certificate_type(client_cert_type->selected_certificate_type());
338 const auto cert_type = [&] {
339 if(
auto server_cert_type = enc_exts.get<Server_Certificate_Type>()) {
340 return server_cert_type->selected_certificate_type();
342 return Certificate_Type::X509;
346 flight.add(m_handshake_state.sending(Certificate_13(client_hello, credentials_manager(), callbacks(), cert_type)))
347 .add(m_handshake_state.sending(Certificate_Verify_13(m_handshake_state.server_certificate(),
348 client_hello.signature_schemes(),
349 client_hello.sni_hostname(),
350 m_transcript_hash.current(),
351 Connection_Side::Server,
352 credentials_manager(),
358 flight.add(m_handshake_state.sending(Finished_13(m_cipher_state.get(), m_transcript_hash.current())));
360 if(client_hello.extensions().has<Record_Size_Limit>() &&
361 m_handshake_state.encrypted_extensions().extensions().has<Record_Size_Limit>()) {
377 const auto outgoing_limit = client_hello.extensions().get<Record_Size_Limit>();
378 const auto incoming_limit = m_handshake_state.encrypted_extensions().extensions().get<Record_Size_Limit>();
379 set_record_size_limits(outgoing_limit->limit(), incoming_limit->limit());
384 m_cipher_state->advance_with_server_finished(m_transcript_hash.current(), *
this);
386 if(m_handshake_state.has_certificate_request()) {
393 m_transitions.set_expected_next(Handshake_Type::Certificate);
395 m_transitions.set_expected_next(Handshake_Type::Finished);
399void Server_Impl_13::handle_reply_to_client_hello(Hello_Retry_Request hello_retry_request) {
400 auto cipher = Ciphersuite::by_id(hello_retry_request.ciphersuite());
403 send_handshake_message(m_handshake_state.sending(std::move(hello_retry_request)));
404 maybe_handle_compatibility_mode();
406 m_transcript_hash = Transcript_Hash_State::recreate_after_hello_retry_request(cipher->prf_algo(), m_transcript_hash);
408 m_transitions.set_expected_next(Handshake_Type::ClientHello);
411void Server_Impl_13::handle(
const Client_Hello_12& ch) {
417 if(m_handshake_state.has_hello_retry_request()) {
418 throw TLS_Exception(Alert::UnexpectedMessage,
"Received a TLS 1.2 Client Hello after Hello Retry Request");
427 if(!expects_downgrade()) {
428 throw TLS_Exception(Alert::ProtocolVersion,
"Received a legacy Client Hello");
434void Server_Impl_13::handle(
const Client_Hello_13& client_hello) {
435 const auto& exts = client_hello.extensions();
437 const bool is_initial_client_hello = !m_handshake_state.has_hello_retry_request();
439 if(is_initial_client_hello) {
440 const auto preferred_version = client_hello.highest_supported_version(policy());
441 if(!preferred_version) {
442 throw TLS_Exception(Alert::ProtocolVersion,
"No shared TLS version");
448 if(exts.has<Cookie>()) {
449 throw TLS_Exception(Alert::IllegalParameter,
"Received a Cookie in the initial client hello");
455 if(!exts.has<Supported_Groups>()) {
456 throw Not_Implemented(
"PSK-only handshake NYI");
466 if(!is_initial_client_hello) {
467 const auto& hrr_exts = m_handshake_state.hello_retry_request().extensions();
468 const auto offered_groups = exts.get<Key_Share>()->offered_groups();
469 const auto selected_group = hrr_exts.get<Key_Share>()->selected_group();
470 if(offered_groups.size() != 1 || offered_groups.at(0) != selected_group) {
471 throw TLS_Exception(Alert::IllegalParameter,
"Client did not comply with the requested key exchange group");
475 callbacks().tls_examine_extensions(exts, Connection_Side::Client, client_hello.type());
476 std::visit([
this](
auto msg) { handle_reply_to_client_hello(std::move(msg)); },
477 Server_Hello_13::create(client_hello,
478 is_initial_client_hello,
480 credentials_manager(),
486void Server_Impl_13::handle(
const Certificate_13& certificate_msg) {
490 if(!is_handshake_complete() && !certificate_msg.request_context().empty()) {
491 throw TLS_Exception(Alert::DecodeError,
"Received a client certificate message with non-empty request context");
497 certificate_msg.validate_extensions(m_handshake_state.certificate_request().extensions().extension_types(),
505 if(certificate_msg.empty()) {
506 if(policy().require_client_certificate_authentication()) {
507 throw TLS_Exception(Alert::CertificateRequired,
"Policy requires client send a certificate, but it did not");
513 m_transitions.set_expected_next(Handshake_Type::Finished);
524 certificate_msg.verify(callbacks(),
526 credentials_manager(),
527 m_handshake_state.client_hello().sni_hostname(),
528 m_handshake_state.client_hello().extensions().has<Certificate_Status_Request>());
535 m_transitions.set_expected_next(Handshake_Type::CertificateVerify);
539void Server_Impl_13::handle(
const Certificate_Verify_13& certificate_verify_msg) {
545 const auto offered = m_handshake_state.certificate_request().signature_schemes();
546 if(!
value_exists(offered, certificate_verify_msg.signature_scheme())) {
547 throw TLS_Exception(Alert::IllegalParameter,
548 "We did not offer the usage of " + certificate_verify_msg.signature_scheme().to_string() +
549 " as a signature scheme");
553 !m_handshake_state.client_certificate().empty());
554 bool sig_valid = certificate_verify_msg.verify(
555 *m_handshake_state.client_certificate().public_key(), callbacks(), m_transcript_hash.previous());
561 throw TLS_Exception(Alert::DecryptError,
"Client certificate verification failed");
564 m_transitions.set_expected_next(Handshake_Type::Finished);
567void Server_Impl_13::handle(
const Finished_13& finished_msg) {
572 if(!finished_msg.verify(m_cipher_state.get(), m_transcript_hash.previous())) {
573 throw TLS_Exception(Alert::DecryptError,
"Finished message didn't verify");
578 callbacks().tls_session_established(
579 Session_Summary(m_handshake_state.server_hello(),
580 Connection_Side::Server,
582 peer_raw_public_key(),
584 m_resumed_session.has_value(),
585 Server_Information(m_handshake_state.client_hello().sni_hostname()),
586 callbacks().tls_current_timestamp()));
588 m_cipher_state->advance_with_client_finished(m_transcript_hash.current());
591 m_transitions.set_expected_next({});
593 callbacks().tls_session_activated();
595 if(new_session_ticket_supported()) {
596 send_new_session_tickets(policy().new_session_tickets_upon_handshake_success());
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
const Policy & policy() const
const std::vector< PSK_Key_Exchange_Mode > & modes() const
virtual bool allow_tls12() const
Server_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< const Policy > &policy, const std::shared_ptr< RandomNumberGenerator > &rng)
std::variant< Client_Hello_13, Client_Hello_12, Server_Hello_13, Server_Hello_12, Hello_Retry_Request, Encrypted_Extensions, Certificate_13, Certificate_Request_13, Certificate_Verify_13, Finished_13 > Handshake_Message_13
bool value_exists(const std::vector< T > &vec, const OT &val)
overloaded(Ts...) -> overloaded< Ts... >