10#include <botan/internal/tls_record_layer_13.h>
12#include <botan/tls_alert.h>
13#include <botan/tls_exceptn.h>
14#include <botan/tls_policy.h>
15#include <botan/tls_version.h>
16#include <botan/internal/concat_util.h>
17#include <botan/internal/int_utils.h>
18#include <botan/internal/loadstor.h>
19#include <botan/internal/tls_cipher_state.h>
28constexpr uint16_t content_type_tag_length = 1;
30bool verify_change_cipher_spec(std::span<const uint8_t> data) {
37 constexpr size_t expected_fragment_length = 1;
38 constexpr uint8_t expected_fragment_byte = 0x01;
39 return (data.size() == expected_fragment_length && data.front() == expected_fragment_byte);
66 m_receiving_compat_mode(true) {}
72 BOTAN_ARG_CHECK(outgoing_limit >= 64,
"Invalid outgoing record size limit");
74 "Invalid incoming record size limit");
80 m_outgoing_record_size_limit =
81 std::min(outgoing_limit,
static_cast<uint16_t
>(
MAX_PLAINTEXT_SIZE + content_type_tag_length));
82 m_incoming_record_size_limit = incoming_limit;
96 while(!data.empty()) {
98 if(m_incoming_records.empty() || m_incoming_records.back().complete()) {
99 return m_incoming_records.emplace_back();
101 return m_incoming_records.back();
106 const size_t consumed = record.append(data);
108 data = data.subspan(consumed);
114 std::span<const uint8_t> payload,
120 "data size exceeds record payload size limit");
139 "Application Data records MUST NOT be written to the wire unprotected");
142 "TLS 1.3 deprecated CHANGE_CIPHER_SPEC");
152 "Zero-length fragments are only allowed for Application Data");
155 const size_t pt_size_with_type_tag = payload.size() + content_type_tag_length;
162 const size_t padding_length =
163 (pt_size_with_type_tag < max_record_size)
164 ? std::min(policy.
record_padding_bytes(pt_size_with_type_tag), max_record_size - pt_size_with_type_tag)
168 cs->protect_record(type, payload, padding_length, out);
174 const auto legacy_record_version = (m_sending_compat_mode) ? Protocol_Version::TLS_V10
175 : Protocol_Version::TLS_V12;
184 legacy_record_version,
220 if(m_incoming_records.empty()) {
224 if(!m_incoming_records.front().complete()) {
225 return m_incoming_records.front().missing_bytes_hint();
228 auto record = std::move(m_incoming_records.front());
229 m_incoming_records.pop_front();
244 throw TLS_Exception(Alert::IllegalParameter,
"Received unexpected record version");
258 (!cs->must_expect_unprotected_alert_traffic() || record.type() !=
Record_Type::Alert)) {
259 throw TLS_Exception(Alert::UnexpectedMessage,
"unprotected record received where protected traffic was expected");
263 throw TLS_Exception(Alert::UnexpectedMessage,
"malformed change cipher spec record received");
268 .type = record.type(),
269 .sequence_number = std::nullopt,
270 .payload = record.take_payload(),
277 throw TLS_Exception(Alert::UnexpectedMessage,
"premature Application Data received");
285 m_incoming_records.clear();
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
#define BOTAN_ARG_CHECK(expr, msg)
virtual size_t record_padding_bytes(size_t plaintext_bytes) const
uint16_t outgoing_record_size_limit() const noexcept
void set_record_size_limits(uint16_t outgoing_limit, uint16_t incoming_limit)
variant_append_t< Record, BytesNeeded > ReadResult
uint16_t incoming_record_size_limit() const noexcept
bool receiving_compat_mode() const noexcept
static std::array< uint8_t, TLS_HEADER_SIZE > serialize_header(Record_Type type, Protocol_Version legacy_version, uint16_t payload_length)
TLS_Record_Layer(Connection_Side side)
void prepare_record(const Policy &policy, Record_Type type, std::span< const uint8_t > payload, Cipher_State *cipher_state, secure_vector< uint8_t > &out) override
void clear_read_buffer() override
void copy_data(std::span< const uint8_t > data) override
uint16_t record_payload_size_limit(const Policy &policy, Cipher_State *cipher_state=nullptr) const override
ReadResult next_record(Cipher_State *cipher_state=nullptr) override
TLS_Cipher_State * as_tls_cipher_state(Cipher_State *cs)
Record annotate_record_type(Record_Content &&record)
constexpr RT checked_cast_to(AT i)
void append(std::vector< T, Alloc > &out, std::type_identity_t< std::span< const T > > in)
std::vector< T, secure_allocator< T > > secure_vector
constexpr GeneralVariantT generalize_to(SpecialT &&specific)
Converts a given variant into another variant-ish whose type states are a super set of the given vari...