9#include <botan/internal/tls_record_layer_13.h>
11#include <botan/tls_alert.h>
12#include <botan/tls_exceptn.h>
13#include <botan/tls_version.h>
14#include <botan/internal/tls_cipher_state.h>
15#include <botan/internal/tls_reader.h>
21template <
typename IteratorT>
22bool verify_change_cipher_spec(
const IteratorT data,
const size_t size) {
29 const size_t expected_fragment_length = 1;
30 const uint8_t expected_fragment_byte = 0x01;
31 return (size == expected_fragment_length && *data == expected_fragment_byte);
34Record_Type read_record_type(
const uint8_t type_byte) {
42 throw TLS_Exception(Alert::UnexpectedMessage,
"TLS record type had unexpected value");
51class TLSPlaintext_Header
final {
53 TLSPlaintext_Header(std::vector<uint8_t> hdr,
const bool check_tls13_version) {
54 m_type = read_record_type(hdr[0]);
55 m_legacy_version = Protocol_Version(
make_uint16(hdr[1], hdr[2]));
57 m_serialized = std::move(hdr);
61 if(m_legacy_version.major_version() != 0x03) {
62 throw TLS_Exception(Alert::IllegalParameter,
"Received unexpected record version");
68 if(check_tls13_version && m_legacy_version.version_code() != 0x0303) {
69 throw TLS_Exception(Alert::IllegalParameter,
"Received unexpected record version");
79 throw TLS_Exception(Alert::DecodeError,
"empty record received");
93 throw TLS_Exception(Alert::RecordOverflow,
"Received an encrypted record that exceeds maximum size");
106 throw TLS_Exception(Alert::RecordOverflow,
"Received a record that exceeds maximum size");
112 const size_t frgmnt_length,
113 const bool use_compatibility_version) :
115 m_legacy_version(use_compatibility_version ? 0x0301 : 0x0303)
117 m_fragment_length(static_cast<uint16_t>(frgmnt_length)),
119 static_cast<uint8_t
>(m_type),
120 m_legacy_version.major_version(),
121 m_legacy_version.minor_version(),
128 uint16_t fragment_length()
const {
return m_fragment_length; }
130 Protocol_Version legacy_version()
const {
return m_legacy_version; }
132 const std::vector<uint8_t>& serialized()
const {
return m_serialized; }
136 Protocol_Version m_legacy_version;
137 uint16_t m_fragment_length;
138 std::vector<uint8_t> m_serialized;
168 m_receiving_compat_mode(true) {}
171 m_read_buffer.insert(m_read_buffer.end(), data.begin(), data.end());
175 std::span<const uint8_t> data,
191 "Application Data records MUST NOT be written to the wire unprotected");
198 "zero-length fragments of types other than application data are not allowed");
204 std::vector<uint8_t> output;
208 constexpr size_t content_type_tag_length = 1;
215 const size_t max_plaintext_size =
216 (protect) ? m_outgoing_record_size_limit - content_type_tag_length :
static_cast<uint16_t
>(
MAX_PLAINTEXT_SIZE);
218 const auto records = std::max((data.size() + max_plaintext_size - 1) / max_plaintext_size,
size_t(1));
225 output_length += cipher_state->
encrypt_output_length(data.size() - ((records - 1) * max_plaintext_size) +
226 content_type_tag_length);
228 output_length += data.size();
230 output.reserve(output_length);
232 size_t pt_offset = 0;
233 size_t to_process = data.size();
240 const size_t pt_size = std::min<size_t>(to_process, max_plaintext_size);
241 const size_t ct_size =
249 const auto record_header = TLSPlaintext_Header(pt_type, ct_size, m_sending_compat_mode).serialized();
251 output.insert(output.end(), record_header.cbegin(), record_header.cend());
253 auto pt_fragment = data.subspan(pt_offset, pt_size);
256 fragment.reserve(ct_size);
259 fragment.insert(fragment.end(), pt_fragment.begin(), pt_fragment.end());
260 fragment.push_back(
static_cast<uint8_t
>(type));
266 output.insert(output.end(), fragment.cbegin(), fragment.cend());
268 output.insert(output.end(), pt_fragment.begin(), pt_fragment.end());
271 pt_offset += pt_size;
272 to_process -= pt_size;
273 }
while(to_process > 0);
284 const auto header_begin = m_read_buffer.cbegin();
290 TLSPlaintext_Header plaintext_header({header_begin, header_end}, !m_receiving_compat_mode);
304 throw TLS_Exception(Alert::UnexpectedMessage,
"unprotected record received where protected traffic was expected");
307 if(m_read_buffer.size() <
TLS_HEADER_SIZE + plaintext_header.fragment_length()) {
308 return TLS_HEADER_SIZE + plaintext_header.fragment_length() - m_read_buffer.size();
311 const auto fragment_begin = header_end;
312 const auto fragment_end = fragment_begin + plaintext_header.fragment_length();
315 !verify_change_cipher_spec(fragment_begin, plaintext_header.fragment_length())) {
316 throw TLS_Exception(Alert::UnexpectedMessage,
"malformed change cipher spec record received");
320 m_read_buffer.erase(header_begin, fragment_end);
323 if(cipher_state ==
nullptr) {
326 throw TLS_Exception(Alert::UnexpectedMessage,
"premature Application Data received");
330 throw TLS_Exception(Alert::BadRecordMac,
"incomplete record mac received");
334 throw TLS_Exception(Alert::RecordOverflow,
"Received an encrypted record that exceeds maximum plaintext size");
340 const auto end_of_content =
341 std::find_if(record.
fragment.crbegin(), record.
fragment.crend(), [](
auto byte) { return byte != 0x00; });
343 if(end_of_content == record.
fragment.crend()) {
348 throw TLS_Exception(Alert::UnexpectedMessage,
"No content type found in encrypted record");
352 record.
type = read_record_type(*end_of_content);
358 throw TLS_Exception(Alert::UnexpectedMessage,
"protected change cipher spec received");
369 BOTAN_ARG_CHECK(outgoing_limit >= 64,
"Invalid outgoing record size limit");
371 "Invalid incoming record size limit");
377 m_outgoing_record_size_limit = std::min(outgoing_limit,
static_cast<uint16_t
>(
MAX_PLAINTEXT_SIZE + 1));
378 m_incoming_record_size_limit = incoming_limit;
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
uint64_t decrypt_record_fragment(const std::vector< uint8_t > &header, secure_vector< uint8_t > &encrypted_fragment)
size_t minimum_decryption_input_length() const
bool must_expect_unprotected_alert_traffic() const
uint64_t encrypt_record_fragment(const std::vector< uint8_t > &header, secure_vector< uint8_t > &fragment)
size_t encrypt_output_length(size_t input_length) const
size_t decrypt_output_length(size_t input_length) const
std::variant< BytesNeeded, ResT > ReadResult
Record_Layer(Connection_Side side)
void copy_data(std::span< const uint8_t > data_from_peer)
std::vector< uint8_t > prepare_records(Record_Type type, std::span< const uint8_t > data, Cipher_State *cipher_state=nullptr) const
void set_record_size_limits(uint16_t outgoing_limit, uint16_t incoming_limit)
ReadResult< Record > next_record(Cipher_State *cipher_state=nullptr)
int(* final)(unsigned char *, CTX *)
@ MAX_CIPHERTEXT_SIZE_TLS13
constexpr uint8_t get_byte(T input)
std::vector< T, secure_allocator< T > > secure_vector
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)
std::optional< uint64_t > seq_no
secure_vector< uint8_t > fragment