9#include <botan/internal/tls_channel_io.h>
11#include <botan/tls_callbacks.h>
12#include <botan/tls_exceptn.h>
13#include <botan/tls_policy.h>
14#include <botan/internal/bit_ops.h>
15#include <botan/internal/buffer_slicer.h>
16#include <botan/internal/stl_util.h>
17#include <botan/internal/tls_cipher_state.h>
18#include <botan/internal/tls_handshake_layer_13.h>
19#include <botan/internal/tls_record_layer_13.h>
20#include <botan/internal/tls_transcript_hash_13.h>
25 std::optional<Transcript_Hash_State>& transcript_hash,
bool handshake_complete) {
29 if(!handshake_complete) {
31 auto handshake_msg = hs_layer.next_message(
policy(), *transcript_hash);
32 if(!handshake_msg.has_value()) {
58 hs_layer.has_pending_data()) {
59 throw Unexpected_Message(
"Unexpected additional handshake message data found in record");
65 if(!m_first_message_delivered) {
66 rec_layer.disable_receiving_compat_mode();
67 m_first_message_delivered =
true;
70 return std::move(handshake_msg).value();
72 auto post_handshake_msg = hs_layer.next_post_handshake_message(
policy());
73 if(!post_handshake_msg.has_value()) {
79 if(std::holds_alternative<Key_Update>(post_handshake_msg.value()) && hs_layer.has_pending_data()) {
80 throw Unexpected_Message(
"Unexpected additional post-handshake message data found in record");
83 return std::move(post_handshake_msg).value();
91 if(m_closed_for_reading) {
99 bool handshake_complete) {
107 return std::move(event).value();
116 auto res = std::visit(
118 [](
BytesNeeded bytes) -> std::optional<Channel_IO::ReceiveEvent> {
return bytes; },
119 [&](
const Handshake_Record& record) -> std::optional<Channel_IO::ReceiveEvent> {
123 [&](
auto anything_else) -> std::optional<Channel_IO::ReceiveEvent> {
129 return anything_else;
134 if(res.has_value()) {
135 return std::move(res).value();
153 if(m_key_update_reciprocation_pending) {
155 m_key_update_reciprocation_pending =
false;
156 }
else if(needs_traffic_based_key_update()) {
182bool Channel_IO::needs_traffic_based_key_update()
const {
198 if(limit == 0 || !cs->is_handshake_complete()) {
205 if(cs->can_encrypt_application_traffic() && cs->records_encrypted_with_current_key() >= limit) {
218 const uint64_t read_limit = limit + limit / 2;
219 return !m_key_update_requested && cs->can_decrypt_application_traffic() &&
220 cs->records_decrypted_with_current_key() >= read_limit;
233 if(
const uint64_t min_interval =
policy().minimum_key_update_interval_ms();
234 min_interval > 0 && !solicited_reciprocation) {
237 if(m_last_peer_key_update_ms != 0 && (now - m_last_peer_key_update_ms) < min_interval) {
238 throw TLS_Exception(Alert::UnexpectedMessage,
"Peer is requesting KeyUpdates too frequently");
241 m_last_peer_key_update_ms = now;
244 cs->update_read_keys();
256 m_key_update_reciprocation_pending =
true;
261 m_key_update_requested =
false;
273 if(request_peer_update) {
274 m_key_update_requested =
true;
276 cs->update_write_keys();
309 constexpr auto ccs = std::array<uint8_t, 1>{0x01};
311 m_dummy_ccs_emitted =
true;
316 m_closed_for_reading =
true;
332 return m_cipher_state.lock();
336 return m_cipher_state.lock();
346class TLS_Channel_IO final :
public Channel_IO {
348 TLS_Channel_IO(
Connection_Side side, std::shared_ptr<const Policy> policy, std::shared_ptr<Callbacks> callbacks) :
349 Channel_IO(side, std::move(policy), std::move(callbacks)), m_record_layer(side), m_handshake_layer(side) {}
351 void send_data(
Record_Type record_type, std::span<const uint8_t> payload, Cipher_State* cipher_state)
override {
352 const size_t max_plaintext_payload_size = record_layer().record_payload_size_limit(policy(), cipher_state);
363 "Only Handshake and Application Data records may be fragmented");
365 auto& output = m_write_buffer;
367 if(output.capacity() > max_retained_write_buffer_size) {
368 output.shrink_to_fit();
371 const auto records =
ceil_division(payload.size(), max_plaintext_payload_size);
373 if(cipher_state ==
nullptr) {
375 }
else if(records > 1) {
376 const auto max_record_size =
377 cipher_state !=
nullptr
378 ? cipher_state->encrypt_output_length(record_layer().outgoing_record_size_limit())
379 : max_plaintext_payload_size;
383 BufferSlicer bs(payload);
392 const size_t pt_size = std::min(bs.remaining(), max_plaintext_payload_size);
393 const auto pt_fragment = bs.take(pt_size);
395 m_record_layer.prepare_record(policy(), record_type, pt_fragment, cipher_state, output);
396 }
while(!bs.empty());
398 callbacks().tls_emit_data(output);
401 void send_flight(std::vector<Flight::Message> flight)
override {
402 auto cs = cipher_state();
412 for(
const auto& msg_info : flight) {
415 [&](
const Flight::Dummy_ChangeCipherSpec&) {
420 if(!msgs.get().empty()) {
424 send_dummy_change_cipher_spec();
426 [&](
const Flight::Message_Info& info) {
428 msgs.get().insert(msgs.get().end(), info.header.begin(), info.header.end());
429 msgs.get().insert(msgs.get().end(), info.serialized.begin(), info.serialized.end());
435 if(!msgs.get().empty()) {
445 m_record_layer.disable_sending_compat_mode();
449 void process(
const Handshake_Record& record)
override { m_handshake_layer.copy_data(policy(), record); }
451 Record_Layer& record_layer()
override {
return m_record_layer; }
453 const Record_Layer& record_layer()
const override {
return m_record_layer; }
455 Handshake_Layer& handshake_layer()
override {
return m_handshake_layer; }
457 const Handshake_Layer& handshake_layer()
const override {
return m_handshake_layer; }
460 TLS_Record_Layer m_record_layer;
461 TLS_Handshake_Layer m_handshake_layer;
470 std::shared_ptr<const Policy>
policy,
475 return std::make_shared<TLS_Channel_IO>(side, std::move(
policy), std::move(
callbacks));
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
std::vector< uint8_t > serialize() const
virtual uint64_t tls_current_monotonic_clock_ms()
void copy_data(std::span< const uint8_t > data)
std::shared_ptr< const Cipher_State > cipher_state() const
virtual void process(const Handshake_Record &record)=0
const Callbacks & callbacks() const
void send(std::vector< Flight::Message > flight)
Sends a flight of handshake messages to the peer.
std::variant< BytesNeeded, Handshake_Message_13, Post_Handshake_Message_13, ChangeCipherSpec_Record, Alert_Record, ApplicationData_Record > ReceiveEvent
void send_dummy_change_cipher_spec()
Sends a dummy ChangeCipherSpec record to the peer.
void handle_key_update(const Key_Update &key_update)
const Policy & policy() const
void set_selected_certificate_type(Certificate_Type cert_type)
void notify_closed_for_reading()
virtual void send_data(Record_Type record_type, std::span< const uint8_t > payload, Cipher_State *cipher_state)=0
void set_record_size_limits(uint16_t out, uint16_t in)
ReceiveEvent next_pending_event(std::optional< Transcript_Hash_State > &transcript_hash, bool handshake_complete)
std::optional< ReceiveEvent > next_pending_handshake_message(std::optional< Transcript_Hash_State > &transcript_hash, bool handshake_complete)
virtual void send_flight(std::vector< Flight::Message > flight)=0
void update_traffic_keys(bool request_peer_update)
static std::shared_ptr< Channel_IO > create(Connection_Side side, TLS_Flavor flavor, std::shared_ptr< const Policy > policy, std::shared_ptr< Callbacks > callbacks)
virtual Record_Layer & record_layer()=0
virtual Handshake_Layer & handshake_layer()=0
void set_selected_certificate_type(Certificate_Type cert_type)
bool expects_reciprocation() const
virtual uint64_t records_per_traffic_key() const
virtual void copy_data(std::span< const uint8_t > data_from_peer)=0
void set_record_size_limits(uint16_t outgoing_limit, uint16_t incoming_limit)
virtual ReadResult next_record(Cipher_State *cipher_state=nullptr)=0
virtual void clear_read_buffer()=0
Strong< std::vector< uint8_t >, struct MarshalledHandshakeMessageFlight_ > MarshalledHandshakeMessageFlight
@ MAX_CIPHERTEXT_SIZE_TLS13
BOTAN_FORCE_INLINE constexpr T ceil_division(T a, T b)
constexpr bool holds_any_of(const std::variant< Ts... > &v) noexcept
std::vector< T, secure_allocator< T > > secure_vector
overloaded(Ts...) -> overloaded< Ts... >