10#include <botan/internal/tls_record.h>
12#include <botan/aead.h>
13#include <botan/block_cipher.h>
16#include <botan/tls_ciphersuite.h>
17#include <botan/tls_exceptn.h>
18#include <botan/internal/fmt.h>
19#include <botan/internal/loadstor.h>
20#include <botan/internal/tls_seq_numbers.h>
21#include <botan/internal/tls_session_key.h>
23#if defined(BOTAN_HAS_TLS_CBC)
24 #include <botan/internal/tls_cbc.h>
27#if defined(BOTAN_HAS_TLS_NULL)
28 #include <botan/internal/tls_null.h>
40 bool uses_encrypt_then_mac) {
47 m_nonce = keys.
nonce(side);
53#if defined(BOTAN_HAS_TLS_CBC)
59 m_aead = std::make_unique<TLS_CBC_HMAC_AEAD_Encryption>(std::move(cipher),
64 uses_encrypt_then_mac);
66 m_aead = std::make_unique<TLS_CBC_HMAC_AEAD_Decryption>(std::move(cipher),
71 uses_encrypt_then_mac);
76 throw Internal_Error(
"Negotiated disabled TLS CBC+HMAC ciphersuite");
79#if defined(BOTAN_HAS_TLS_NULL)
83 m_aead = std::make_unique<TLS_NULL_HMAC_AEAD_Encryption>(std::move(mac), suite.
mac_keylen());
85 m_aead = std::make_unique<TLS_NULL_HMAC_AEAD_Decryption>(std::move(mac), suite.
mac_keylen());
95 m_aead->set_key(aead_key);
99 switch(m_nonce_format) {
128 switch(m_nonce_format) {
150 throw Decoding_Error(
"Invalid AEAD packet too short to be valid");
165 uint16_t msg_length) {
166 std::array<uint8_t, 13> ad{};
169 ad[8] =
static_cast<uint8_t
>(msg_type);
181 const uint16_t len16 =
static_cast<uint16_t
>(len_field);
189 Protocol_Version version,
190 uint64_t record_sequence) {
193 output.push_back(
static_cast<uint8_t
>(record_type));
194 output.push_back(version.major_version());
195 output.push_back(version.minor_version());
197 if(version.is_datagram_protocol()) {
198 for(
size_t i = 0; i != 8; ++i) {
209 uint64_t record_sequence,
210 const uint8_t* message,
211 size_t message_len) {
213 throw Internal_Error(
"Writing an unencrypted TLS application data record");
215 write_record_header(output, record_type, version, record_sequence);
216 append_u16_len(output, message_len);
217 output.insert(output.end(), message, message + message_len);
223 uint64_t record_sequence,
224 const uint8_t* message,
228 write_record_header(output, record_type, version, record_sequence);
231 const auto aad = cs.
format_ad(record_sequence, record_type, version,
static_cast<uint16_t
>(message_len));
239 const auto nonce = cs.
aead_nonce(record_sequence, rng);
241 append_u16_len(output, rec_size);
245 output += nonce.get();
251 const size_t header_size = output.size();
252 output += std::make_pair(message, message_len);
254 aead.
start(nonce.get());
255 aead.
finish(output, header_size);
262size_t fill_buffer_to(
263 secure_vector<uint8_t>& readbuf,
const uint8_t*& input,
size_t& input_size,
size_t& input_consumed,
size_t desired) {
264 if(readbuf.size() >= desired) {
268 const size_t taken = std::min(input_size, desired - readbuf.size());
270 readbuf.insert(readbuf.end(), input, input + taken);
271 input_consumed += taken;
275 return (desired - readbuf.size());
279 uint8_t record_contents[],
281 uint64_t record_sequence,
285 AEAD_Mode& aead = cs.aead();
287 const size_t nonce_from_record = cs.nonce_bytes_from_record();
288 if(record_len <= nonce_from_record) {
289 throw TLS_Exception(Alert::BadRecordMac,
"AEAD packet too short to be valid");
291 const auto nonce = cs.aead_nonce(record_contents, record_len, record_sequence);
292 const uint8_t* msg = &record_contents[nonce_from_record];
293 const size_t msg_length = record_len - nonce_from_record;
302 if(msg_length < aead.minimum_final_size()) {
303 throw TLS_Exception(Alert::BadRecordMac,
"AEAD packet is shorter than the tag");
306 const size_t ptext_size = aead.output_length(msg_length);
308 aead.set_associated_data(
309 cs.format_ad(record_sequence, record_type, record_version,
static_cast<uint16_t
>(ptext_size)));
311 aead.start(nonce.get());
313 output.assign(msg, msg + msg_length);
314 aead.finish(output, 0);
318 const uint8_t input[],
326 if(
const size_t needed = fill_buffer_to(readbuf, input, input_len, consumed,
TLS_HEADER_SIZE)) {
344 const bool bad_record_type = readbuf[0] < 20 || readbuf[0] > 23;
345 const bool bad_record_version = readbuf[1] != 3;
347 if(bad_record_type || bad_record_version) {
349 const std::string first5 = std::string(
reinterpret_cast<const char*
>(readbuf.data()), 5);
351 if(first5 ==
"GET /" || first5 ==
"PUT /" || first5 ==
"POST " || first5 ==
"HEAD ") {
352 throw TLS_Exception(Alert::ProtocolVersion,
"Client sent plaintext HTTP request instead of TLS handshake");
355 if(first5 ==
"CONNE") {
357 "Client sent plaintext HTTP proxy CONNECT request instead of TLS handshake");
360 if(bad_record_type) {
364 throw TLS_Exception(Alert::UnexpectedMessage,
"TLS record type had unexpected value");
366 throw TLS_Exception(Alert::ProtocolVersion,
"TLS record version had unexpected value");
371 if(version.is_datagram_protocol()) {
372 throw TLS_Exception(Alert::ProtocolVersion,
"Expected TLS but got a record with DTLS version");
378 throw TLS_Exception(Alert::RecordOverflow,
"Received a record that exceeds maximum size");
381 if(record_size == 0) {
382 throw TLS_Exception(Alert::DecodeError,
"Received a completely empty record");
385 if(
const size_t needed = fill_buffer_to(readbuf, input, input_len, consumed,
TLS_HEADER_SIZE + record_size)) {
395 uint64_t sequence = 0;
396 if(sequence_numbers !=
nullptr) {
397 sequence = sequence_numbers->next_read_sequence();
398 epoch = sequence_numbers->current_read_epoch();
412 const auto cs = get_cipherstate(epoch);
416 decrypt_record(recbuf, &readbuf[
TLS_HEADER_SIZE], record_size, sequence, version, type, *cs);
418 if(sequence_numbers !=
nullptr) {
419 sequence_numbers->read_accept(sequence);
427 const uint8_t input[],
433 bool allow_epoch0_restart) {
436 if(fill_buffer_to(readbuf, input, input_len, consumed,
DTLS_HEADER_SIZE) != 0) {
446 if(version.is_datagram_protocol() ==
false) {
459 if(fill_buffer_to(readbuf, input, input_len, consumed,
DTLS_HEADER_SIZE + record_size) != 0) {
480 const uint16_t epoch = (sequence >> 48);
482 const bool already_seen = sequence_numbers !=
nullptr && sequence_numbers->already_seen(sequence);
484 if(already_seen && !(epoch == 0 && allow_epoch0_restart)) {
501 const auto cs = get_cipherstate(epoch);
505 decrypt_record(recbuf, readbuf.data() +
DTLS_HEADER_SIZE, record_size, sequence, version, type, *cs);
506 }
catch(std::exception&) {
511 if(sequence_numbers !=
nullptr) {
512 sequence_numbers->read_accept(sequence);
523 const uint8_t input[],
529 bool allow_epoch0_restart) {
531 return read_dtls_record(
532 readbuf, input, input_len, consumed, recbuf, sequence_numbers, get_cipherstate, allow_epoch0_restart);
534 return read_tls_record(readbuf, input, input_len, consumed, recbuf, sequence_numbers, get_cipherstate);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
#define BOTAN_ASSERT(expr, assertion_made)
void set_associated_data(std::span< const uint8_t > ad)
static std::unique_ptr< AEAD_Mode > create_or_throw(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
static std::unique_ptr< BlockCipher > create_or_throw(std::string_view algo_spec, std::string_view provider="")
void start(std::span< const uint8_t > nonce)
void finish(secure_vector< uint8_t > &final_block, size_t offset=0)
virtual size_t output_length(size_t input_length) const =0
static std::unique_ptr< MessageAuthenticationCode > create_or_throw(std::string_view algo_spec, std::string_view provider="")
void randomize(std::span< uint8_t > output)
size_t nonce_bytes_from_record(Protocol_Version version) const
Nonce_Format nonce_format() const
size_t mac_keylen() const
size_t cipher_keylen() const
size_t nonce_bytes_from_handshake() const
std::string mac_algo() const
std::string cipher_algo() const
Record_Nonce aead_nonce(uint64_t seq, RandomNumberGenerator &rng) const
size_t nonce_bytes_from_record() const
Nonce_Format nonce_format() const
size_t nonce_bytes_from_handshake() const
~Connection_Cipher_State()
std::array< uint8_t, 13 > format_ad(uint64_t seq, Record_Type type, Protocol_Version version, uint16_t ptext_length)
Connection_Cipher_State(Protocol_Version version, Connection_Side which_side, bool is_our_side, const Ciphersuite &suite, const Session_Keys &keys, bool uses_encrypt_then_mac)
uint8_t major_version() const
uint8_t minor_version() const
std::span< uint8_t > get()
const std::vector< uint8_t > & nonce(Connection_Side side) const
const secure_vector< uint8_t > & aead_key(Connection_Side side) const
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)
std::function< std::shared_ptr< Connection_Cipher_State >(uint16_t)> get_cipherstate_fn
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)
Record_Header read_record(TLS_Flavor flavor, 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)
constexpr uint8_t get_byte(T input)
std::string fmt(std::string_view format, const T &... args)
constexpr void copy_mem(T *out, const T *in, size_t n)
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
std::vector< T, secure_allocator< T > > secure_vector
constexpr uint8_t get_byte_var(size_t byte_num, T input)
constexpr auto store_be(ParamTs &&... params)
constexpr auto load_be(ParamTs &&... params)
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)