9#include <botan/internal/tls_channel_impl_12.h>
12#include <botan/tls_callbacks.h>
13#include <botan/tls_messages_12.h>
14#include <botan/tls_policy.h>
15#include <botan/x509cert.h>
16#include <botan/internal/concat_util.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/loadstor.h>
19#include <botan/internal/mem_utils.h>
20#include <botan/internal/stl_util.h>
21#include <botan/internal/tls_handshake_state.h>
22#include <botan/internal/tls_record.h>
23#include <botan/internal/tls_seq_numbers.h>
29 const std::shared_ptr<RandomNumberGenerator>&
rng,
30 const std::shared_ptr<const Policy>&
policy,
33 size_t reserved_io_buffer_size) :
34 m_is_server(is_server),
35 m_is_datagram(is_datagram),
40 m_has_been_closed(false) {
47 m_write_cipher_states[0] =
nullptr;
48 m_read_cipher_states[0] =
nullptr;
50 m_writebuf.reserve(reserved_io_buffer_size);
51 m_readbuf.reserve(reserved_io_buffer_size);
54void Channel_Impl_12::reset_state() {
55 m_active_state.reset();
56 m_pending_state.reset();
58 m_write_cipher_states.clear();
59 m_read_cipher_states.clear();
65 m_active_state.reset();
66 m_read_cipher_states.clear();
67 m_write_cipher_states.clear();
69 m_write_cipher_states[0] =
nullptr;
70 m_read_cipher_states[0] =
nullptr;
72 if(m_sequence_numbers) {
73 m_sequence_numbers->reset();
80 BOTAN_ASSERT(m_sequence_numbers,
"Have a sequence numbers object");
81 return *m_sequence_numbers;
84std::shared_ptr<Connection_Cipher_State> Channel_Impl_12::read_cipher_state_epoch(uint16_t epoch)
const {
85 auto i = m_read_cipher_states.find(epoch);
86 if(i == m_read_cipher_states.end()) {
87 throw Internal_Error(
"TLS::Channel_Impl_12 No read cipherstate for epoch " + std::to_string(epoch));
92std::shared_ptr<Connection_Cipher_State> Channel_Impl_12::write_cipher_state_epoch(uint16_t epoch)
const {
93 auto i = m_write_cipher_states.find(epoch);
94 if(i == m_write_cipher_states.end()) {
95 throw Internal_Error(
"TLS::Channel_Impl_12 No write cipherstate for epoch " + std::to_string(epoch));
101 if(m_active_state.has_value()) {
102 return m_active_state->peer_certs();
104 return std::vector<X509_Certificate>();
108 if(m_active_state.has_value()) {
109 return m_active_state->psk_identity();
111 if(
const auto* state = pending_state()) {
112 return state->psk_identity();
118 if(pending_state() !=
nullptr) {
119 throw Internal_Error(
"create_handshake_state called during handshake");
122 if(m_active_state.has_value()) {
127 "Active state using version " + active_version.
to_string() +
" cannot change to " +
132 if(!m_sequence_numbers) {
134 m_sequence_numbers = std::make_unique<Datagram_Sequence_Numbers>();
136 m_sequence_numbers = std::make_unique<Stream_Sequence_Numbers>();
140 using namespace std::placeholders;
142 std::unique_ptr<Handshake_IO> io;
148 auto send_record_f = [
this](uint16_t epoch,
Record_Type record_type,
const std::vector<uint8_t>& record) {
149 send_record_under_epoch(epoch, record_type, record);
151 io = std::make_unique<Datagram_Handshake_IO>(send_record_f,
156 policy().maximum_handshake_message_size());
158 auto send_record_f = [
this](
Record_Type rec_type,
const std::vector<uint8_t>& record) {
159 send_record(rec_type, record);
161 io = std::make_unique<Stream_Handshake_IO>(send_record_f);
166 if(m_active_state.has_value()) {
167 m_pending_state->set_version(m_active_state->version());
170 return *m_pending_state;
174 if(m_pending_state) {
175 return m_pending_state->handshake_io().timeout_check();
183 if(pending_state() !=
nullptr) {
187 if(m_active_state.has_value()) {
188 if(!force_full_renegotiation) {
194 throw Invalid_State(
"Cannot renegotiate on inactive connection");
203 const auto* pending = pending_state();
205 BOTAN_ASSERT(pending && pending->server_hello(),
"Have received server hello");
207 if(pending->server_hello()->compression_method() != 0) {
211 sequence_numbers().new_read_cipher_state();
213 const uint16_t epoch = sequence_numbers().current_read_epoch();
215 BOTAN_ASSERT(!m_read_cipher_states.contains(epoch),
"No read cipher state currently set for next epoch");
218 auto read_state = std::make_shared<Connection_Cipher_State>(
222 pending->ciphersuite(),
223 pending->session_keys(),
224 pending->server_hello()->supports_encrypt_then_mac());
226 m_read_cipher_states[epoch] = read_state;
230 const auto* pending = pending_state();
232 BOTAN_ASSERT(pending && pending->server_hello(),
"Have received server hello");
234 if(pending->server_hello()->compression_method() != 0) {
238 sequence_numbers().new_write_cipher_state();
240 const uint16_t epoch = sequence_numbers().current_write_epoch();
242 BOTAN_ASSERT(!m_write_cipher_states.contains(epoch),
"No write cipher state currently set for next epoch");
244 auto write_state = std::make_shared<Connection_Cipher_State>(pending->version(),
247 pending->ciphersuite(),
248 pending->session_keys(),
249 pending->server_hello()->supports_encrypt_then_mac());
251 m_write_cipher_states[epoch] = write_state;
255 return m_active_state.has_value();
263 return m_has_been_closed;
269 const auto& state = *m_pending_state;
271 if(!state.version().is_datagram_protocol()) {
273 const uint16_t current_epoch = sequence_numbers().current_write_epoch();
275 const auto not_current_epoch = [current_epoch](uint16_t epoch) {
return (epoch != current_epoch); };
288 m_pending_state.reset();
296 const auto* input = data.data();
297 auto input_size = data.size();
300 while(input_size > 0) {
303 auto get_epoch = [
this](uint16_t epoch) {
return read_cipher_state_epoch(epoch); };
311 m_sequence_numbers.get(),
313 allow_epoch0_restart);
315 const size_t needed = record.
needed();
319 BOTAN_ASSERT(consumed <= input_size,
"Record reader consumed sane amount");
322 input_size -= consumed;
324 BOTAN_ASSERT(input_size == 0 || needed == 0,
"Got a full record or consumed all input");
326 if(input_size == 0 && needed != 0) {
336 throw TLS_Exception(Alert::RecordOverflow,
"TLS plaintext record is larger than allowed maximum");
339 const bool epoch0_restart = m_is_datagram && record.
epoch() == 0 && m_active_state.has_value();
342 const bool initial_record = epoch0_restart || (pending_state() ==
nullptr && !m_active_state.has_value());
343 bool initial_handshake_message =
false;
353 throw TLS_Exception(Alert::ProtocolVersion,
"Received unexpected record version in initial record");
355 }
else if(
const auto* pending = pending_state()) {
356 if(pending->server_hello() !=
nullptr && !initial_handshake_message &&
357 record.
version() != pending->version()) {
358 throw TLS_Exception(Alert::ProtocolVersion,
"Received unexpected record version");
360 }
else if(m_active_state.has_value()) {
361 if(record.
version() != m_active_state->version() && !initial_handshake_message) {
362 throw TLS_Exception(Alert::ProtocolVersion,
"Received unexpected record version");
368 if(m_has_been_closed) {
369 throw TLS_Exception(Alert::UnexpectedMessage,
"Received handshake data after connection closure");
371 process_handshake_ccs(m_record_buf, record.
sequence(), record.
type(), record.
version(), epoch0_restart);
373 if(m_has_been_closed) {
374 throw TLS_Exception(Alert::UnexpectedMessage,
"Received application data after connection closure");
376 if(pending_state() !=
nullptr) {
377 throw TLS_Exception(Alert::UnexpectedMessage,
"Can't interleave application and handshake data");
379 process_application_data(record.
sequence(), m_record_buf);
381 process_alert(m_record_buf);
384 " from counterparty");
405 uint64_t record_sequence,
408 bool epoch0_restart) {
409 if(!m_pending_state) {
412 if(m_sequence_numbers) {
419 const uint16_t epoch = record_sequence >> 48;
422 if(epoch == current_epoch) {
424 }
else if(current_epoch > 0 && epoch == current_epoch - 1) {
425 BOTAN_ASSERT(m_active_state.has_value() && m_active_state->dtls_handshake_io(),
426 "Have DTLS handshake IO for retransmission");
427 m_active_state->dtls_handshake_io()->add_record(
428 record.data(), record.size(), record_type, record_sequence);
439 if(m_pending_state) {
440 m_pending_state->handshake_io().add_record(record.data(), record.size(), record_type, record_sequence);
442 while(
auto* pending = m_pending_state.get()) {
443 auto msg = pending->get_next_handshake_msg(
policy().maximum_handshake_message_size());
451 if(!m_pending_state) {
459 if(!m_active_state.has_value()) {
460 throw Unexpected_Message(
"Application data before handshake done");
464 const uint16_t read_epoch =
465 m_is_datagram ?
static_cast<uint16_t
>(seq_no >> 48) : sequence_numbers().current_read_epoch();
466 if(read_epoch == 0) {
467 throw Unexpected_Message(
"Application data received in unexpected read epoch");
474 const Alert alert_msg(record);
480 if(alert_msg.type() == Alert::NoRenegotiation && m_active_state.has_value()) {
481 m_pending_state.reset();
487 if(alert_msg.is_fatal() && m_active_state.has_value()) {
488 const auto& sid = m_active_state->session_id();
494 if(alert_msg.type() == Alert::CloseNotify) {
501 if(alert_msg.type() == Alert::CloseNotify || alert_msg.is_fatal()) {
502 m_has_been_closed =
true;
509 const uint8_t input[],
511 BOTAN_ASSERT(m_pending_state || m_active_state.has_value(),
"Some connection state exists");
513 const Protocol_Version record_version = (m_pending_state) ? (m_pending_state->version()) : m_active_state->version();
515 const uint64_t next_seq = sequence_numbers().next_write_sequence(epoch);
517 if(cipher_state ==
nullptr) {
520 TLS::write_record(m_writebuf, record_type, record_version, next_seq, input, length, *cipher_state,
rng());
526void Channel_Impl_12::send_record_array(uint16_t epoch,
Record_Type type,
const uint8_t input[],
size_t length) {
531 auto cipher_state = write_cipher_state_epoch(epoch);
535 write_record(cipher_state.get(), epoch, type, input, sending);
542void Channel_Impl_12::send_record(
Record_Type record_type,
const std::vector<uint8_t>& record) {
543 send_record_array(sequence_numbers().current_write_epoch(), record_type, record.data(), record.size());
546void Channel_Impl_12::send_record_under_epoch(uint16_t epoch,
548 const std::vector<uint8_t>& record) {
549 send_record_array(epoch, record_type, record.data(), record.size());
554 throw Invalid_State(
"Data cannot be sent on inactive TLS connection");
561 const bool ready_to_send_anything = !
is_closed() && m_sequence_numbers;
562 if(alert.
is_valid() && ready_to_send_anything) {
569 if(alert.
type() == Alert::NoRenegotiation && m_active_state.has_value()) {
570 m_pending_state.reset();
574 if(m_active_state.has_value()) {
575 const auto& sid = m_active_state->session_id();
583 if(alert.
type() == Alert::CloseNotify || alert.
is_fatal()) {
584 m_has_been_closed =
true;
592 if(m_active_state && m_active_state->client_supports_secure_renegotiation() != secure_renegotiation) {
593 throw TLS_Exception(Alert::HandshakeFailure,
"Client changed its mind about secure renegotiation");
596 if(secure_renegotiation) {
601 throw TLS_Exception(Alert::HandshakeFailure,
"Client sent bad values for secure renegotiation");
610 if(m_active_state && m_active_state->server_supports_secure_renegotiation() != secure_renegotiation) {
611 throw TLS_Exception(Alert::HandshakeFailure,
"Server changed its mind about secure renegotiation");
614 if(secure_renegotiation) {
619 throw TLS_Exception(Alert::HandshakeFailure,
"Server sent bad values for secure renegotiation");
625 if(m_active_state.has_value()) {
626 return m_active_state->client_finished_verify_data();
628 return std::vector<uint8_t>();
632 if(m_active_state.has_value()) {
633 return concat(m_active_state->client_finished_verify_data(), m_active_state->server_finished_verify_data());
640 if(m_active_state.has_value()) {
641 return m_active_state->server_supports_secure_renegotiation();
644 if(
const auto* pending = pending_state()) {
645 if(
const auto* hello = pending->server_hello()) {
646 return hello->secure_renegotiation();
654 std::string_view context,
655 size_t length)
const {
656 if(!m_active_state.has_value()) {
657 throw Invalid_State(
"Channel_Impl_12::key_material_export connection not active");
660 if(pending_state() !=
nullptr) {
661 throw Invalid_State(
"Channel_Impl_12::key_material_export cannot export during renegotiation");
666 const auto salt = [&] {
667 if(context.empty()) {
668 return concat(m_active_state->client_random(), m_active_state->server_random());
670 return concat(m_active_state->client_random(),
671 m_active_state->server_random(),
672 store_be(
static_cast<uint16_t
>(context.size())),
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
#define BOTAN_ASSERT(expr, assertion_made)
std::vector< uint8_t > serialize() const
virtual void tls_session_activated()
virtual std::unique_ptr< KDF > tls12_protocol_specific_kdf(std::string_view prf_algo) const
virtual void tls_record_received(uint64_t seq_no, std::span< const uint8_t > data)=0
virtual void tls_alert(Alert alert)=0
virtual bool tls_peer_closed_connection()
virtual void tls_emit_data(std::span< const uint8_t > data)=0
RandomNumberGenerator & rng()
bool is_closed() const override
void change_cipher_spec_reader(Connection_Side side)
void update_traffic_keys(bool request_peer_update=false) override
Handshake_State & create_handshake_state(Protocol_Version version)
std::vector< uint8_t > secure_renegotiation_data_for_server_hello() const
~Channel_Impl_12() override
bool is_handshake_complete() const override
Callbacks & callbacks() const
size_t from_peer(std::span< const uint8_t > data) override
void secure_renegotiation_check(const Client_Hello_12 *client_hello)
bool timeout_check() override
bool is_active() const override
Session_Manager & session_manager()
const Policy & policy() const
void send_alert(const Alert &alert) override
virtual void initiate_handshake(Handshake_State &state, bool force_full_renegotiation)=0
std::vector< X509_Certificate > peer_cert_chain() const override
void to_peer(std::span< const uint8_t > data) override
void change_cipher_spec_writer(Connection_Side side)
std::vector< uint8_t > secure_renegotiation_data_for_client_hello() const
virtual std::unique_ptr< Handshake_State > new_handshake_state(std::unique_ptr< class Handshake_IO > io)=0
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)
SymmetricKey key_material_export(std::string_view label, std::string_view context, size_t length) const override
std::optional< std::string > external_psk_identity() const override
void reset_active_association_state()
virtual void process_handshake_msg(Handshake_State &pending_state, Handshake_Type type, const std::vector< uint8_t > &contents, bool epoch0_restart)=0
bool secure_renegotiation_supported() const override
void renegotiate(bool force_full_renegotiation=false) override
virtual std::string application_protocol() const =0
void send_warning_alert(Alert::Type type)
void send_fatal_alert(Alert::Type type)
std::vector< uint8_t > renegotiation_info() const
bool secure_renegotiation() const
virtual uint16_t current_read_epoch() const =0
virtual void read_accept(uint64_t seq)=0
virtual size_t dtls_maximum_timeout() const
virtual size_t dtls_default_mtu() const
virtual bool allow_dtls_epoch0_restart() const
virtual size_t dtls_initial_timeout() const
virtual bool allow_resumption_for_renegotiation() const
std::string to_string() const
uint8_t major_version() const
bool is_datagram_protocol() const
std::vector< uint8_t > renegotiation_info() const
bool secure_renegotiation() const
Helper class to embody a session handle in all protocol versions.
virtual size_t remove(const Session_Handle &handle)=0
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Record_Header read_record(bool is_datagram, secure_vector< uint8_t > &readbuf, const uint8_t input[], size_t input_len, size_t &consumed, secure_vector< uint8_t > &recbuf, Connection_Sequence_Numbers *sequence_numbers, const get_cipherstate_fn &get_cipherstate, bool allow_epoch0_restart)
void write_unencrypted_record(secure_vector< uint8_t > &output, Record_Type record_type, Protocol_Version version, uint64_t record_sequence, const uint8_t *message, size_t message_len)
void write_record(secure_vector< uint8_t > &output, Record_Type record_type, Protocol_Version version, uint64_t record_sequence, const uint8_t *message, size_t message_len, Connection_Cipher_State &cs, RandomNumberGenerator &rng)
void map_remove_if(Pred pred, T &assoc)
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
constexpr auto concat(Rs &&... ranges)
std::vector< T, secure_allocator< T > > secure_vector
constexpr auto store_be(ParamTs &&... params)