9#include <botan/internal/tls_server_impl_12.h>
11#include <botan/certstor.h>
12#include <botan/ocsp.h>
13#include <botan/tls_callbacks.h>
14#include <botan/tls_magic.h>
15#include <botan/tls_messages_12.h>
16#include <botan/tls_policy.h>
17#include <botan/tls_version.h>
18#include <botan/internal/stl_util.h>
19#include <botan/internal/tls_handshake_state.h>
20#include <botan/internal/tls_messages_internal.h>
26 Server_Handshake_State(std::unique_ptr<Handshake_IO> io, Callbacks& cb) :
Handshake_State(std::move(io), cb) {}
28 Private_Key* server_rsa_kex_key() {
return m_server_rsa_kex_key.get(); }
30 void set_server_rsa_kex_key(std::shared_ptr<Private_Key> key) { m_server_rsa_kex_key = std::move(key); }
32 bool allow_session_resumption()
const {
return m_allow_session_resumption; }
34 void set_allow_session_resumption(
bool allow_session_resumption) {
35 m_allow_session_resumption = allow_session_resumption;
38 const std::vector<X509_Certificate>& resume_peer_certs()
const {
return m_resume_peer_certs; }
40 void set_resume_certs(
const std::vector<X509_Certificate>& certs) { m_resume_peer_certs = certs; }
42 void mark_as_resumption() { m_is_a_resumption =
true; }
44 bool is_a_resumption()
const {
return m_is_a_resumption; }
48 std::shared_ptr<Private_Key> m_server_rsa_kex_key;
54 bool m_allow_session_resumption =
true;
56 bool m_is_a_resumption =
false;
58 std::vector<X509_Certificate> m_resume_peer_certs;
63std::optional<Session> check_for_resume(
const Session_Handle& handle_to_resume,
68 auto session = session_manager.retrieve(handle_to_resume, cb, policy);
69 if(!session.has_value()) {
74 if(client_hello->legacy_version() != session->version()) {
79 if(!
value_exists(client_hello->ciphersuites(), session->ciphersuite_code())) {
84 if(!client_hello->sni_hostname().empty() && client_hello->sni_hostname() != session->server_info().hostname()) {
89 if(client_hello->supports_extended_master_secret() != session->supports_extended_master_secret()) {
90 if(!session->supports_extended_master_secret()) {
97 throw TLS_Exception(Alert::HandshakeFailure,
"Client resumed extended ms session without sending extension");
102 if(!client_hello->supports_encrypt_then_mac() && session->supports_encrypt_then_mac()) {
107 throw TLS_Exception(Alert::HandshakeFailure,
"Client resumed Encrypt-then-MAC session without sending extension");
116uint16_t choose_ciphersuite(
const Policy& policy,
118 const std::map<std::string, std::vector<X509_Certificate>>& cert_chains,
120 const bool our_choice = policy.server_uses_own_ciphersuite_preferences();
121 const std::vector<uint16_t>& client_suites = client_hello.ciphersuites();
122 const std::vector<uint16_t> server_suites = policy.ciphersuite_list(version);
124 if(server_suites.empty()) {
125 throw TLS_Exception(Alert::HandshakeFailure,
"Policy forbids us from negotiating any ciphersuite");
128 const bool have_shared_ecc_curve =
129 (policy.choose_key_exchange_group(client_hello.supported_ecc_curves(), {}) != Group_Params::NONE);
131 const bool client_supports_ffdhe_groups = !client_hello.supported_dh_groups().empty();
133 const bool have_shared_dh_group =
134 (policy.choose_key_exchange_group(client_hello.supported_dh_groups(), {}) != Group_Params::NONE);
139 std::vector<uint16_t> pref_list = server_suites;
140 std::vector<uint16_t> other_list = client_suites;
143 std::swap(pref_list, other_list);
146 for(
auto suite_id : pref_list) {
153 if(!suite.has_value() || !suite->valid()) {
157 if(have_shared_ecc_curve ==
false && suite->ecc_ciphersuite()) {
161 if(suite->kex_method() ==
Kex_Algo::DH && client_supports_ffdhe_groups && !have_shared_dh_group) {
166 if(suite->signature_used()) {
167 const std::string sig_algo = suite->sig_algo();
170 if(!cert_chains.contains(sig_algo)) {
174 const std::vector<Signature_Scheme> allowed = policy.allowed_signature_schemes();
176 const std::vector<Signature_Scheme> client_sig_methods = client_hello.signature_schemes();
183 bool we_support_some_hash_by_client =
false;
186 if(!scheme.is_available()) {
190 if(scheme.algorithm_name() == suite->sig_algo() &&
191 policy.allowed_signature_hash(scheme.hash_function_name())) {
192 we_support_some_hash_by_client =
true;
196 if(we_support_some_hash_by_client ==
false) {
198 "Policy does not accept any hash function supported by client");
211 if(client_supports_ffdhe_groups && !have_shared_dh_group) {
212 throw TLS_Exception(Alert::InsufficientSecurity,
"Can't agree on a sufficiently strong ciphersuite with client");
215 throw TLS_Exception(Alert::HandshakeFailure,
"Can't agree on a ciphersuite with client");
218std::map<std::string, std::vector<X509_Certificate>> get_server_certs(
219 std::string_view hostname,
const std::vector<Signature_Scheme>& cert_sig_schemes, Credentials_Manager& creds) {
220 const std::vector<std::string> cert_types = {
"RSA",
"ECDSA"};
222 std::map<std::string, std::vector<X509_Certificate>> cert_chains;
224 for(
const auto& cert_type : cert_types) {
225 const std::vector<X509_Certificate> certs = creds.cert_chain_single_type(
229 cert_chains[cert_type] = certs;
240 const std::shared_ptr<Credentials_Manager>& creds,
241 const std::shared_ptr<const Policy>&
policy,
242 const std::shared_ptr<RandomNumberGenerator>&
rng,
256 downgrade_info.io_buffer_size),
257 m_creds(downgrade_info.creds) {}
259std::unique_ptr<Handshake_State> Server_Impl_12::new_handshake_state(std::unique_ptr<Handshake_IO> io) {
260 auto state = std::make_unique<Server_Handshake_State>(std::move(io),
callbacks());
265std::vector<X509_Certificate> Server_Impl_12::get_peer_cert_chain(
const Handshake_State& state_base)
const {
266 const Server_Handshake_State& state =
dynamic_cast<const Server_Handshake_State&
>(state_base);
267 if(!state.resume_peer_certs().empty()) {
268 return state.resume_peer_certs();
271 if(state.client_certs() !=
nullptr) {
272 return state.client_certs()->cert_chain();
274 return std::vector<X509_Certificate>();
280void Server_Impl_12::initiate_handshake(
Handshake_State& state,
bool force_full_renegotiation) {
281 dynamic_cast<Server_Handshake_State&
>(state).set_allow_session_resumption(!force_full_renegotiation);
283 const Hello_Request hello_req(state.handshake_io());
291 const std::vector<Protocol_Version>& supported_versions) {
293 const bool initial_handshake = (active_version.valid() ==
false);
295 if(!supported_versions.empty()) {
298 return Protocol_Version::DTLS_V12;
300 throw TLS_Exception(Alert::ProtocolVersion,
"No shared DTLS version");
303 return Protocol_Version::TLS_V12;
305 throw TLS_Exception(Alert::ProtocolVersion,
"No shared TLS version");
309 if(!initial_handshake) {
317 if(active_version > client_offer) {
319 Alert::ProtocolVersion,
320 "Client negotiated " + active_version.to_string() +
" then renegotiated with " + client_offer.to_string());
322 return active_version;
327 if(policy.allow_dtls12() && client_offer >= Protocol_Version::DTLS_V12) {
328 return Protocol_Version::DTLS_V12;
331 if(policy.allow_tls12() && client_offer >= Protocol_Version::TLS_V12) {
332 return Protocol_Version::TLS_V12;
337 "Client version " + client_offer.to_string() +
" is unacceptable by policy");
344void Server_Impl_12::process_client_hello_msg(
const Handshake_State* active_state,
345 Server_Handshake_State& pending_state,
346 const std::vector<uint8_t>& contents,
347 bool epoch0_restart) {
350 const bool initial_handshake = epoch0_restart || active_state ==
nullptr;
352 if(initial_handshake ==
false &&
policy().allow_client_initiated_renegotiation() ==
false) {
353 if(
policy().abort_connection_on_undesired_renegotiation()) {
354 throw TLS_Exception(Alert::NoRenegotiation,
"Server policy prohibits renegotiation");
366 if(pending_state.handshake_io().have_more_data()) {
367 throw TLS_Exception(Alert::UnexpectedMessage,
"Have data remaining in buffer after ClientHello");
370 pending_state.client_hello(std::make_unique<Client_Hello_12>(contents));
371 const Protocol_Version client_offer = pending_state.client_hello()->legacy_version();
372 const bool datagram = client_offer.is_datagram_protocol();
375 if(client_offer.major_version() == 0xFF) {
376 throw TLS_Exception(Alert::ProtocolVersion,
"Client offered DTLS version with major version 0xFF");
379 if(client_offer.major_version() < 3) {
380 throw TLS_Exception(Alert::ProtocolVersion,
"Client offered TLS version with major version under 3");
382 if(client_offer.major_version() == 3 && client_offer.minor_version() == 0) {
383 throw TLS_Exception(Alert::ProtocolVersion,
"Client offered SSLv3 which is not supported");
397 const Protocol_Version negotiated_version =
400 active_state !=
nullptr ? active_state->version() : Protocol_Version(),
401 pending_state.client_hello()->supported_versions());
403 pending_state.set_version(negotiated_version);
405 const auto compression_methods = pending_state.client_hello()->compression_methods();
407 throw TLS_Exception(Alert::IllegalParameter,
"Client did not offer NULL compression");
410 if(initial_handshake && datagram) {
414 cookie_secret = m_creds->psk(
"tls-server",
"dtls-cookie-secret",
"");
417 if(!cookie_secret.empty()) {
419 const Hello_Verify_Request verify(
420 pending_state.client_hello()->cookie_input_data(), client_identity, cookie_secret);
422 if(pending_state.client_hello()->cookie() != verify.cookie()) {
424 pending_state.handshake_io().send_under_epoch(verify, 0);
426 pending_state.handshake_io().send(verify);
429 pending_state.client_hello(
nullptr);
433 }
else if(epoch0_restart) {
434 throw TLS_Exception(Alert::HandshakeFailure,
"Reuse of DTLS association requires DTLS cookie secret be set");
448 const auto session_handle = pending_state.client_hello()->session_handle();
450 std::optional<Session> session_info;
451 if(pending_state.allow_session_resumption() && session_handle.has_value()) {
452 session_info = check_for_resume(
456 m_next_protocol =
"";
457 if(pending_state.client_hello()->supports_alpn()) {
461 if(session_info.has_value()) {
462 this->session_resume(pending_state, {session_info.value(), session_handle.value()});
465 this->session_create(pending_state);
469void Server_Impl_12::process_certificate_msg(Server_Handshake_State& pending_state,
470 const std::vector<uint8_t>& contents) {
471 pending_state.client_certs(std::make_unique<Certificate_12>(contents,
policy()));
474 if(pending_state.client_certs()->empty() &&
policy().require_client_certificate_authentication()) {
475 throw TLS_Exception(Alert::HandshakeFailure,
"Policy requires client send a certificate, but it did not");
481void Server_Impl_12::process_client_key_exchange_msg(Server_Handshake_State& pending_state,
482 const std::vector<uint8_t>& contents) {
489 pending_state.client_kex(std::make_unique<Client_Key_Exchange>(
490 contents, pending_state, pending_state.server_rsa_kex_key(), *m_creds,
policy(),
rng()));
492 pending_state.compute_session_keys();
493 if(
policy().allow_ssl_key_log_file()) {
499 "CLIENT_RANDOM", pending_state.client_hello()->random(), pending_state.session_keys().master_secret());
503void Server_Impl_12::process_change_cipher_spec_msg(Server_Handshake_State& pending_state) {
508void Server_Impl_12::process_certificate_verify_msg(Server_Handshake_State& pending_state,
510 const std::vector<uint8_t>& contents) {
511 pending_state.client_verify(std::make_unique<Certificate_Verify_12>(contents));
513 const std::vector<X509_Certificate>& client_certs = pending_state.client_certs()->cert_chain();
515 if(client_certs.empty()) {
516 throw TLS_Exception(Alert::DecodeError,
"No client certificate sent");
520 throw TLS_Exception(Alert::BadCertificate,
"Client certificate does not support signing");
523 const bool sig_valid = pending_state.client_verify()->verify(client_certs[0], pending_state,
policy());
525 pending_state.hash().update(pending_state.handshake_io().format(contents, type));
533 throw TLS_Exception(Alert::DecryptError,
"Client cert verify failed");
537 const std::string sni_hostname = pending_state.client_hello()->sni_hostname();
538 auto trusted_CAs = m_creds->trusted_certificate_authorities(
"tls-server", sni_hostname);
546 }
catch(std::exception& e) {
547 throw TLS_Exception(Alert::BadCertificate, e.what());
553void Server_Impl_12::process_finished_msg(Server_Handshake_State& pending_state,
555 const std::vector<uint8_t>& contents) {
558 if(pending_state.handshake_io().have_more_data()) {
559 throw TLS_Exception(Alert::UnexpectedMessage,
"Have data remaining in buffer after Finished");
562 pending_state.client_finished(std::make_unique<Finished_12>(contents));
565 throw TLS_Exception(Alert::DecryptError,
"Finished message didn't verify");
568 if(pending_state.server_finished() ==
nullptr) {
571 pending_state.hash().update(pending_state.handshake_io().format(contents, type));
573 Session session_info(pending_state.session_keys().master_secret(),
574 pending_state.server_hello()->legacy_version(),
575 pending_state.server_hello()->ciphersuite(),
577 pending_state.server_hello()->supports_extended_master_secret(),
578 pending_state.server_hello()->supports_encrypt_then_mac(),
579 get_peer_cert_chain(pending_state),
580 Server_Information(pending_state.client_hello()->sni_hostname()),
581 pending_state.server_hello()->srtp_profile(),
588 summary.set_session_id(pending_state.server_hello()->session_id());
592 if(
callbacks().tls_should_persist_resumption_information(session_info)) {
594 pending_state.server_hello()->session_id(),
595 !pending_state.server_hello()->supports_session_ticket());
597 if(pending_state.server_hello()->supports_session_ticket() && handle.has_value() && handle->is_ticket()) {
598 pending_state.new_session_ticket(std::make_unique<New_Session_Ticket_12>(
599 pending_state.handshake_io(),
600 pending_state.hash(),
601 handle->ticket().value(),
602 static_cast<uint32_t
>(
policy().session_ticket_lifetime().count())));
606 if(pending_state.new_session_ticket() ==
nullptr && pending_state.server_hello()->supports_session_ticket()) {
607 pending_state.new_session_ticket(
608 std::make_unique<New_Session_Ticket_12>(pending_state.handshake_io(), pending_state.hash()));
611 pending_state.handshake_io().send(Change_Cipher_Spec());
615 pending_state.server_finished(
625void Server_Impl_12::process_handshake_msg(
const Handshake_State* active_state,
628 const std::vector<uint8_t>& contents,
629 bool epoch0_restart) {
630 Server_Handshake_State& state =
dynamic_cast<Server_Handshake_State&
>(state_base);
631 state.confirm_transition_to(type);
642 state.hash().update(state.handshake_io().format(contents, type));
647 return this->process_client_hello_msg(active_state, state, contents, epoch0_restart);
650 return this->process_certificate_msg(state, contents);
653 return this->process_client_key_exchange_msg(state, contents);
656 return this->process_certificate_verify_msg(state, type, contents);
659 return this->process_change_cipher_spec_msg(state);
662 return this->process_finished_msg(state, type, contents);
665 throw Unexpected_Message(
"Unknown handshake message received");
669void Server_Impl_12::session_resume(Server_Handshake_State& pending_state,
const Session_with_Handle& session) {
673 const bool offer_new_session_ticket = pending_state.client_hello()->supports_session_ticket() &&
674 pending_state.client_hello()->session_ticket().empty() &&
677 pending_state.server_hello(std::make_unique<Server_Hello_12>(pending_state.handshake_io(),
678 pending_state.hash(),
683 *pending_state.client_hello(),
685 offer_new_session_ticket,
690 pending_state.mark_as_resumption();
691 pending_state.compute_session_keys(session.session.master_secret());
692 if(
policy().allow_ssl_key_log_file()) {
698 "CLIENT_RANDOM", pending_state.client_hello()->random(), pending_state.session_keys().master_secret());
700 pending_state.set_resume_certs(session.session.peer_certs());
706 summary.set_session_id(pending_state.server_hello()->session_id());
707 if(
auto ticket = session.handle.ticket()) {
708 summary.set_session_ticket(std::move(ticket.value()));
713 auto new_handle = [&,
this]() -> std::optional<Session_Handle> {
714 if(!
callbacks().tls_should_persist_resumption_information(session.session)) {
722 if(pending_state.server_hello()->supports_session_ticket()) {
723 if(new_handle.has_value() && new_handle->is_ticket()) {
725 pending_state.new_session_ticket(std::make_unique<New_Session_Ticket_12>(
726 pending_state.handshake_io(), pending_state.hash(), new_handle->ticket().value(), lifetime));
728 pending_state.new_session_ticket(
729 std::make_unique<New_Session_Ticket_12>(pending_state.handshake_io(), pending_state.hash()));
733 pending_state.handshake_io().send(Change_Cipher_Spec());
737 pending_state.server_finished(
742void Server_Impl_12::session_create(Server_Handshake_State& pending_state) {
743 std::map<std::string, std::vector<X509_Certificate>> cert_chains;
745 const std::string sni_hostname = pending_state.client_hello()->sni_hostname();
753 const auto cert_signature_schemes = pending_state.client_hello()->certificate_signature_schemes();
754 cert_chains = get_server_certs(sni_hostname, cert_signature_schemes, *m_creds);
756 if(!sni_hostname.empty() && cert_chains.empty()) {
757 cert_chains = get_server_certs(
"", cert_signature_schemes, *m_creds);
766 if(!cert_chains.empty()) {
771 const uint16_t ciphersuite =
772 choose_ciphersuite(
policy(), pending_state.version(), cert_chains, *pending_state.client_hello());
775 pending_state.version(),
779 pending_state.server_hello(std::make_unique<Server_Hello_12>(pending_state.handshake_io(),
780 pending_state.hash(),
785 *pending_state.client_hello(),
791 const Ciphersuite& pending_suite = pending_state.ciphersuite();
793 std::shared_ptr<Private_Key> private_key;
796 const std::string algo_used = pending_suite.signature_used() ? pending_suite.sig_algo() :
"RSA";
798 BOTAN_ASSERT(!cert_chains[algo_used].empty(),
"Attempting to send empty certificate chain");
800 pending_state.server_certs(
801 std::make_unique<Certificate_12>(pending_state.handshake_io(), pending_state.hash(), cert_chains[algo_used]));
803 if(pending_state.client_hello()->supports_cert_status_message() && pending_state.is_a_resumption() ==
false) {
804 auto* csr = pending_state.client_hello()->extensions().get<Certificate_Status_Request>();
808 if(!resp_bytes.empty()) {
809 pending_state.server_cert_status(
810 std::make_unique<Certificate_Status_12>(pending_state.handshake_io(), pending_state.hash(), resp_bytes));
814 private_key = m_creds->private_key_for(pending_state.server_certs()->cert_chain()[0],
"tls-server", sni_hostname);
817 throw Internal_Error(
"No private key located for associated server cert");
822 pending_state.set_server_rsa_kex_key(private_key);
824 pending_state.server_kex(std::make_unique<Server_Key_Exchange>(
825 pending_state.handshake_io(), pending_state,
policy(), *m_creds,
rng(), private_key.get()));
828 auto trusted_CAs = m_creds->trusted_certificate_authorities(
"tls-server", sni_hostname);
830 std::vector<X509_DN> client_auth_CAs;
832 for(
auto* store : trusted_CAs) {
833 auto subjects = store->all_subjects();
834 client_auth_CAs.insert(client_auth_CAs.end(), subjects.begin(), subjects.end());
839 if(request_cert && pending_state.ciphersuite().signature_used()) {
840 pending_state.cert_req(std::make_unique<Certificate_Request_12>(
841 pending_state.handshake_io(), pending_state.hash(),
policy(), client_auth_CAs));
853 pending_state.server_hello_done(
854 std::make_unique<Server_Hello_Done>(pending_state.handshake_io(), pending_state.hash()));
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
#define BOTAN_ASSERT(expr, assertion_made)
virtual std::vector< uint8_t > tls_provide_cert_status(const std::vector< X509_Certificate > &chain, const Certificate_Status_Request &csr)
virtual std::string tls_peer_network_identity()
virtual std::string tls_server_choose_app_protocol(const std::vector< std::string > &client_protos)
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_verify_cert_chain(const std::vector< X509_Certificate > &cert_chain, const std::vector< std::optional< OCSP::Response > > &ocsp_responses, const std::vector< Certificate_Store * > &trusted_roots, Usage_Type usage, std::string_view hostname, const TLS::Policy &policy)
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
RandomNumberGenerator & rng()
void change_cipher_spec_reader(Connection_Side side)
std::vector< uint8_t > secure_renegotiation_data_for_server_hello() const
Callbacks & callbacks() const
void secure_renegotiation_check(const Client_Hello_12 *client_hello)
Session_Manager & session_manager()
const Policy & policy() const
void change_cipher_spec_writer(Connection_Side side)
Channel_Impl_12(const std::shared_ptr< Callbacks > &callbacks, const std::shared_ptr< Session_Manager > &session_manager, const std::shared_ptr< RandomNumberGenerator > &rng, const std::shared_ptr< const Policy > &policy, bool is_server, bool is_datagram, size_t io_buf_sz=TLS::Channel::IO_BUF_DEFAULT_SIZE)
std::optional< std::string > external_psk_identity() const override
void reset_active_association_state()
bool secure_renegotiation_supported() const override
void send_warning_alert(Alert::Type type)
static std::optional< Ciphersuite > by_id(uint16_t suite)
Handshake_State(std::unique_ptr< Handshake_IO > io, Callbacks &callbacks)
virtual bool request_client_certificate_authentication() const
virtual std::chrono::seconds session_ticket_lifetime() const
bool is_datagram_protocol() const
Server_Impl_12(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, bool is_datagram=false, size_t reserved_io_buffer_size=TLS::Channel::IO_BUF_DEFAULT_SIZE)
Helper class to embody a session handle in all protocol versions.
virtual size_t remove(const Session_Handle &handle)=0
virtual std::optional< Session_Handle > establish(const Session &session, const std::optional< Session_ID > &id=std::nullopt, bool tls12_no_ticket=false)
Save a new Session and assign a Session_Handle (TLS Server).
virtual bool emits_session_tickets()
std::vector< AlgorithmIdentifier > to_algorithm_identifiers(const std::vector< Signature_Scheme > &schemes)
std::vector< uint8_t > make_hello_random(RandomNumberGenerator &rng, Callbacks &cb, const Policy &policy)
Strong< std::vector< uint8_t >, struct Session_ID_ > Session_ID
holds a TLS 1.2 session ID for stateful resumption
bool value_exists(const std::vector< T > &vec, const V &val)