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/stl_util.h>
18#include <botan/internal/tls_cipher_state.h>
25 const std::shared_ptr<const Policy>&
policy,
26 const std::shared_ptr<RandomNumberGenerator>&
rng) :
28#if defined(BOTAN_HAS_TLS_12)
29 if(
policy->allow_tls12()) {
30 expect_downgrade({}, {});
39 const auto& eee = m_handshake_state.encrypted_extensions().extensions();
41 return alpn->single_protocol();
49 if(m_handshake_state.has_client_certificate_msg() &&
50 m_handshake_state.client_certificate().has_certificate_chain()) {
51 return m_handshake_state.client_certificate().cert_chain();
54 if(m_resumed_session.has_value()) {
55 return m_resumed_session->peer_certs();
62 if(m_handshake_state.has_client_certificate_msg() && m_handshake_state.client_certificate().is_raw_public_key()) {
63 return m_handshake_state.client_certificate().public_key();
66 if(m_resumed_session.has_value()) {
67 return m_resumed_session->peer_raw_public_key();
74 return m_psk_identity;
101 size_t tickets_created = 0;
103 for(
size_t i = 0; i < tickets; ++i) {
107 policy().session_ticket_lifetime(),
110 m_handshake_state.client_hello(),
111 m_handshake_state.server_hello(),
115 if(
callbacks().tls_should_persist_resumption_information(session)) {
123 if(flight.contains_messages()) {
127 return tickets_created;
134 m_transitions.confirm_transition_to(msg.get().type());
137 callbacks().tls_inspect_handshake_msg(msg.get());
142 m_handshake_state.received(std::move(message)));
145void Server_Impl_13::process_post_handshake_msg(Post_Handshake_Message_13 message) {
148 std::visit([&](
auto msg) { handle(msg); }, m_handshake_state.received(std::move(message)));
151void Server_Impl_13::process_dummy_change_cipher_spec() {
156 if(!m_handshake_state.has_client_hello() || m_handshake_state.has_client_finished()) {
157 throw TLS_Exception(Alert::UnexpectedMessage,
"Received an unexpected dummy Change Cipher Spec");
170 return m_handshake_state.handshake_finished();
173void Server_Impl_13::maybe_log_secret(std::string_view label, std::span<const uint8_t> secret)
const {
174 if(policy().allow_ssl_key_log_file()) {
175 callbacks().tls_ssl_key_log_data(label, m_handshake_state.client_hello().random(), secret);
179void Server_Impl_13::downgrade() {
186 m_transitions.set_expected_next({});
189void Server_Impl_13::maybe_handle_compatibility_mode() {
191 BOTAN_ASSERT_NOMSG(m_handshake_state.has_hello_retry_request() || m_handshake_state.has_server_hello());
213 const bool just_after_first_handshake_message =
214 m_handshake_state.has_hello_retry_request() ^ m_handshake_state.has_server_hello();
215 const bool client_requested_compatibility_mode = !m_handshake_state.client_hello().session_id().empty();
217 if(just_after_first_handshake_message &&
218 (policy().tls_13_middlebox_compatibility_mode() || client_requested_compatibility_mode)) {
219 send_dummy_change_cipher_spec();
223void Server_Impl_13::handle_reply_to_client_hello(Server_Hello_13 server_hello) {
224 const auto& client_hello = m_handshake_state.client_hello();
225 const auto& exts = client_hello.extensions();
227 const bool uses_psk = server_hello.extensions().has<
PSK>();
229 const auto cipher_opt = Ciphersuite::by_id(server_hello.ciphersuite());
231 const auto& cipher = cipher_opt.value();
232 m_transcript_hash.set_algorithm(cipher.prf_algo());
234 std::unique_ptr<Cipher_State> psk_cipher_state;
236 auto* psk_extension = server_hello.extensions().get<
PSK>();
239 std::visit(
overloaded{[&,
this](Session session) {
240 m_resumed_session = std::move(session);
241 return Cipher_State::init_with_psk(Connection_Side::Server,
242 Cipher_State::PSK_Type::Resumption,
243 m_resumed_session->extract_master_secret(),
246 [&,
this](ExternalPSK psk) {
247 m_psk_identity = psk.identity();
248 return Cipher_State::init_with_psk(Connection_Side::Server,
249 Cipher_State::PSK_Type::External,
250 psk.extract_master_secret(),
253 psk_extension->take_session_to_resume_or_psk());
266 if(!exts.get<PSK>()->validate_binder(*psk_extension,
267 psk_cipher_state->psk_binder_mac(m_transcript_hash.truncated()))) {
268 throw TLS_Exception(Alert::DecryptError,
"PSK binder does not check out");
288 send_handshake_message(m_handshake_state.sending(std::move(server_hello)));
289 maybe_handle_compatibility_mode();
292 m_cipher_state = [&] {
294 auto*
const my_keyshare = m_handshake_state.server_hello().extensions().get<Key_Share>();
299 psk_cipher_state->advance_with_client_hello(m_transcript_hash.previous(), *
this);
300 psk_cipher_state->advance_with_server_hello(
301 cipher, my_keyshare->take_shared_secret(), m_transcript_hash.current(), *
this);
303 return std::move(psk_cipher_state);
305 return Cipher_State::init_with_server_hello(
306 m_side, my_keyshare->take_shared_secret(), cipher, m_transcript_hash.current(), *
this);
310 auto flight = aggregate_handshake_messages();
311 flight.add(m_handshake_state.sending(Encrypted_Extensions(client_hello, policy(), callbacks())));
318 if(
auto certificate_request =
319 Certificate_Request_13::maybe_create(client_hello, credentials_manager(), callbacks(), policy())) {
320 flight.add(m_handshake_state.sending(std::move(certificate_request.value())));
323 const auto& enc_exts = m_handshake_state.encrypted_extensions().extensions();
332 if(
auto* client_cert_type = enc_exts.get<Client_Certificate_Type>()) {
333 set_selected_certificate_type(client_cert_type->selected_certificate_type());
341 const auto cert_type = [&] {
342 if(
auto* server_cert_type = enc_exts.get<Server_Certificate_Type>()) {
343 return server_cert_type->selected_certificate_type();
345 return Certificate_Type::X509;
349 flight.add(m_handshake_state.sending(Certificate_13(client_hello, credentials_manager(), callbacks(), cert_type)))
350 .add(m_handshake_state.sending(Certificate_Verify_13(m_handshake_state.server_certificate(),
351 client_hello.signature_schemes(),
352 client_hello.sni_hostname(),
353 m_transcript_hash.current(),
354 Connection_Side::Server,
355 credentials_manager(),
361 flight.add(m_handshake_state.sending(Finished_13(m_cipher_state.get(), m_transcript_hash.current())));
363 if(client_hello.extensions().has<Record_Size_Limit>() &&
364 m_handshake_state.encrypted_extensions().extensions().has<Record_Size_Limit>()) {
380 auto*
const outgoing_limit = client_hello.extensions().get<Record_Size_Limit>();
381 auto*
const incoming_limit = m_handshake_state.encrypted_extensions().extensions().get<Record_Size_Limit>();
382 set_record_size_limits(outgoing_limit->limit(), incoming_limit->limit());
387 m_cipher_state->advance_with_server_finished(m_transcript_hash.current(), *
this);
389 if(m_handshake_state.has_certificate_request()) {
396 m_transitions.set_expected_next(Handshake_Type::Certificate);
398 m_transitions.set_expected_next(Handshake_Type::Finished);
402void Server_Impl_13::handle_reply_to_client_hello(Hello_Retry_Request hello_retry_request) {
403 auto cipher = Ciphersuite::by_id(hello_retry_request.ciphersuite());
406 send_handshake_message(m_handshake_state.sending(std::move(hello_retry_request)));
407 maybe_handle_compatibility_mode();
409 m_transcript_hash = Transcript_Hash_State::recreate_after_hello_retry_request(cipher->prf_algo(), m_transcript_hash);
411 m_transitions.set_expected_next(Handshake_Type::ClientHello);
414void Server_Impl_13::handle(
const Client_Hello_12_Shim& ch) {
420 if(m_handshake_state.has_hello_retry_request()) {
421 throw TLS_Exception(Alert::UnexpectedMessage,
"Received a TLS 1.2 Client Hello after Hello Retry Request");
430 if(!expects_downgrade()) {
431 throw TLS_Exception(Alert::ProtocolVersion,
"Received a legacy Client Hello");
437void Server_Impl_13::handle(
const Client_Hello_13& client_hello) {
438 const auto& exts = client_hello.extensions();
440 const bool is_initial_client_hello = !m_handshake_state.has_hello_retry_request();
442 if(is_initial_client_hello) {
443 const auto preferred_version = client_hello.highest_supported_version(policy());
444 if(!preferred_version) {
445 throw TLS_Exception(Alert::ProtocolVersion,
"No shared TLS version");
451 if(exts.has<Cookie>()) {
452 throw TLS_Exception(Alert::IllegalParameter,
"Received a Cookie in the initial client hello");
458 if(!exts.has<Supported_Groups>()) {
469 if(!is_initial_client_hello) {
470 const auto& hrr_exts = m_handshake_state.hello_retry_request().extensions();
471 const auto offered_groups = exts.get<Key_Share>()->offered_groups();
472 const auto selected_group = hrr_exts.get<Key_Share>()->selected_group();
473 if(offered_groups.size() != 1 || offered_groups.at(0) != selected_group) {
474 throw TLS_Exception(Alert::IllegalParameter,
"Client did not comply with the requested key exchange group");
478 callbacks().tls_examine_extensions(exts, Connection_Side::Client, client_hello.type());
479 std::visit([
this](
auto msg) { handle_reply_to_client_hello(std::move(msg)); },
480 Server_Hello_13::create(client_hello,
481 is_initial_client_hello,
483 credentials_manager(),
489void Server_Impl_13::handle(
const Certificate_13& certificate_msg) {
493 if(!is_handshake_complete() && !certificate_msg.request_context().empty()) {
494 throw TLS_Exception(Alert::DecodeError,
"Received a client certificate message with non-empty request context");
500 certificate_msg.validate_extensions(m_handshake_state.certificate_request().extensions().extension_types(),
508 if(certificate_msg.empty()) {
509 if(policy().require_client_certificate_authentication()) {
510 throw TLS_Exception(Alert::CertificateRequired,
"Policy requires client send a certificate, but it did not");
516 m_transitions.set_expected_next(Handshake_Type::Finished);
527 certificate_msg.verify(callbacks(),
529 credentials_manager(),
530 m_handshake_state.client_hello().sni_hostname(),
531 m_handshake_state.client_hello().extensions().has<Certificate_Status_Request>());
538 m_transitions.set_expected_next(Handshake_Type::CertificateVerify);
542void Server_Impl_13::handle(
const Certificate_Verify_13& certificate_verify_msg) {
548 const auto offered = m_handshake_state.certificate_request().signature_schemes();
549 if(!
value_exists(offered, certificate_verify_msg.signature_scheme())) {
550 throw TLS_Exception(Alert::IllegalParameter,
551 "We did not offer the usage of " + certificate_verify_msg.signature_scheme().to_string() +
552 " as a signature scheme");
556 !m_handshake_state.client_certificate().empty());
557 const bool sig_valid = certificate_verify_msg.verify(
558 *m_handshake_state.client_certificate().public_key(), callbacks(), m_transcript_hash.previous());
564 throw TLS_Exception(Alert::DecryptError,
"Client certificate verification failed");
567 m_transitions.set_expected_next(Handshake_Type::Finished);
570void Server_Impl_13::handle(
const Finished_13& finished_msg) {
575 if(!finished_msg.verify(m_cipher_state.get(), m_transcript_hash.previous())) {
576 throw TLS_Exception(Alert::DecryptError,
"Finished message didn't verify");
581 callbacks().tls_session_established(
582 Session_Summary(m_handshake_state.server_hello(),
583 Connection_Side::Server,
585 peer_raw_public_key(),
587 m_resumed_session.has_value(),
588 Server_Information(m_handshake_state.client_hello().sni_hostname()),
589 callbacks().tls_current_timestamp()));
591 m_cipher_state->advance_with_client_finished(m_transcript_hash.current());
594 m_transitions.set_expected_next({});
596 callbacks().tls_session_activated();
598 if(new_session_ticket_supported()) {
599 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
AggregatedPostHandshakeMessages aggregate_post_handshake_messages()
Credentials_Manager & credentials_manager()
RandomNumberGenerator & rng()
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)
Session_Manager & session_manager()
std::unique_ptr< Cipher_State > m_cipher_state
Callbacks & callbacks() const
const std::vector< PSK_Key_Exchange_Mode > & modes() const
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
std::string application_protocol() const override
bool new_session_ticket_supported() const override
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_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
bool value_exists(const std::vector< T > &vec, const V &val)