10#ifndef BOTAN_TLS_CIPHER_STATE_H_
11#define BOTAN_TLS_CIPHER_STATE_H_
13#include <botan/assert.h>
14#include <botan/cipher_mode.h>
15#include <botan/secmem.h>
16#include <botan/tls_ciphersuite.h>
17#include <botan/tls_magic.h>
19#include <botan/internal/tls_record_13.h>
20#include <botan/internal/tls_transcript_hash_13.h>
21#include <botan/internal/tls_types_13.h>
117 std::string_view prf_algo);
218 return (m_state == State::EarlyTraffic || m_state == State::ServerApplicationTraffic ||
219 m_state == State::Completed) &&
220 !m_exporter_master_secret.empty();
232 bool must_expect_unprotected_alert_traffic()
const;
237 bool can_encrypt_application_traffic()
const;
242 bool can_decrypt_application_traffic()
const;
247 std::string hash_algorithm()
const;
256 bool is_compatible_with(
const Ciphersuite& cipher)
const;
265 void update_read_keys();
274 void update_write_keys();
279 uint64_t records_encrypted_with_current_key()
const;
284 uint64_t records_decrypted_with_current_key()
const;
304 void advance_without_psk();
315 std::string_view label,
318 void maybe_log_secret(std::string_view label, std::span<const uint8_t> secret)
const {
319 if(m_secret_logger) {
320 m_secret_logger(label, secret);
324 std::vector<uint8_t> empty_hash()
const;
332 Cipher_State(Connection_Side whoami, std::string_view hash_function, ExpansionLabelPrefix expand_prefix);
334 static size_t protected_record_length(Epoch& epoch,
size_t payload_length,
size_t padding_bytes);
341 static void marshall_and_protect(Epoch& epoch,
342 std::span<const uint8_t> header,
343 std::span<const uint8_t> payload,
345 size_t padding_bytes,
346 secure_vector<uint8_t>& out);
347 void deprotect_and_hydrate_content_type(Epoch& epoch,
348 std::span<const uint8_t> header,
349 Record_Content& protected_record,
350 size_t incoming_record_size_limit)
const;
355 secure_vector<uint8_t> hkdf_expand_label(
const secure_vector<uint8_t>& secret,
356 std::string_view label,
357 const std::vector<uint8_t>& context,
358 size_t length)
const;
360 Cipher_State::Epoch create_epoch(Cipher_Dir direction,
361 const secure_vector<uint8_t>& traffic_secret,
362 bool handshake_epoch)
const;
364 const Ciphersuite& ciphersuite()
const;
377 enum class State : uint8_t {
382 ServerApplicationTraffic,
389 std::optional<Ciphersuite> m_ciphersuite;
392 std::unique_ptr<HKDF_Extract> m_extract;
393 std::unique_ptr<HKDF_Expand> m_expand;
394 std::unique_ptr<HashFunction> m_hash;
396 ExpansionLabelPrefix m_expansion_label_prefix;
400 uint32_t m_write_key_update_count = 0;
401 uint32_t m_read_key_update_count = 0;
403 uint16_t m_ticket_nonce;
404 bool m_ticket_nonce_exhausted =
false;
444 std::span<const uint8_t> payload,
445 size_t padding_bytes,
467 void advance_write_epoch(
const secure_vector<uint8_t>& traffic_secret,
bool handshake_epoch =
false)
override;
468 void advance_read_epoch(
const secure_vector<uint8_t>& traffic_secret,
bool handshake_epoch =
false)
override;
470 bool has_write_epoch()
const override {
return m_write_epoch.has_value(); }
472 bool has_read_epoch()
const override {
return m_read_epoch.has_value(); }
474 Epoch& latest_write_epoch()
override {
476 return *m_write_epoch;
479 const Epoch& latest_write_epoch()
const override {
481 return *m_write_epoch;
484 Epoch& latest_read_epoch()
override {
486 return *m_read_epoch;
489 const Epoch& latest_read_epoch()
const override {
491 return *m_read_epoch;
495 std::optional<Epoch> m_write_epoch;
496 std::optional<Epoch> m_read_epoch;
502 tls_cs ==
nullptr, cs ==
nullptr,
"If the cipher state is not a TLS_Cipher_State, it must be null");
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_IMPLICATION(expr1, expr2, msg)
virtual const Epoch & latest_write_epoch() const =0
void set_secret_logger(SecretLoggerFn secret_logger)
static std::unique_ptr< Cipher_State > init_with_psk(Connection_Side side, TLS_Flavor flavor, PSK_Type type, secure_vector< uint8_t > &&psk, std::string_view prf_algo)
bool can_export_keys() const
virtual Epoch & latest_write_epoch()=0
Cipher_State & operator=(Cipher_State &&other)=delete
void advance_with_client_hello(const Transcript_Hash &transcript_hash)
size_t minimum_decryption_input_length() const
virtual bool has_read_epoch() const =0
void advance_with_server_finished(const Transcript_Hash &transcript_hash)
virtual void advance_read_epoch(const secure_vector< uint8_t > &traffic_secret, bool handshake_epoch=false)=0
void advance_with_client_finished(const Transcript_Hash &transcript_hash)
bool verify_peer_finished_mac(const Transcript_Hash &transcript_hash, const std::vector< uint8_t > &peer_mac) const
virtual void clear_read_keys()=0
Cipher_State(const Cipher_State &other)=delete
secure_vector< uint8_t > psk(const Ticket_Nonce &nonce) const
void advance_with_server_hello(const Ciphersuite &cipher, secure_vector< uint8_t > &&shared_secret, const Transcript_Hash &transcript_hash)
secure_vector< uint8_t > export_key(std::string_view label, std::string_view context, size_t length) const
virtual const Epoch & latest_read_epoch() const =0
virtual void advance_write_epoch(const secure_vector< uint8_t > &traffic_secret, bool handshake_epoch=false)=0
Cipher_State(Cipher_State &&other)=delete
size_t encrypt_output_length(size_t input_length) const
static std::unique_ptr< Cipher_State > init_with_server_hello(Connection_Side side, TLS_Flavor flavor, secure_vector< uint8_t > &&shared_secret, const Ciphersuite &cipher, const Transcript_Hash &transcript_hash, SecretLoggerFn secret_logger)
std::vector< uint8_t > psk_binder_mac(const Transcript_Hash &transcript_hash_with_truncated_client_hello) const
size_t decrypt_output_length(size_t input_length) const
Cipher_State & operator=(const Cipher_State &other)=delete
std::array< uint8_t, 6 > ExpansionLabelPrefix
Ticket_Nonce next_ticket_nonce()
virtual void clear_write_keys()=0
virtual bool has_write_epoch() const =0
std::vector< uint8_t > finished_mac(const Transcript_Hash &transcript_hash) const
bool is_handshake_complete() const
virtual Epoch & latest_read_epoch()=0
Record deprotect_record(Record_TLS record, size_t incoming_record_size_limit)
TLS_Cipher_State(TLS_Cipher_State &&)=delete
~TLS_Cipher_State() override
TLS_Cipher_State(const TLS_Cipher_State &)=delete
TLS_Cipher_State(Connection_Side side, std::string_view prf_algo)
void clear_read_keys() override
void clear_write_keys() override
TLS_Cipher_State & operator=(const TLS_Cipher_State &)=delete
void protect_record(Record_Type type, std::span< const uint8_t > payload, size_t padding_bytes, secure_vector< uint8_t > &out)
TLS_Cipher_State & operator=(TLS_Cipher_State &&)=delete
TLS_Cipher_State * as_tls_cipher_state(Cipher_State *cs)
std::variant< ChangeCipherSpec_Record, Alert_Record, Handshake_Record, ApplicationData_Record > Record
std::vector< uint8_t > Transcript_Hash
Strong< std::vector< uint8_t >, struct Ticket_Nonce_ > Ticket_Nonce
Used to derive the ticket's PSK from the resumption_master_secret.
std::function< void(std::string_view label, std::span< const uint8_t > secret)> SecretLoggerFn
std::vector< T, secure_allocator< T > > secure_vector
std::unique_ptr< AEAD_Mode > cipher
std::optional< secure_vector< uint8_t > > finished_key
secure_vector< uint8_t > traffic_secret
secure_vector< uint8_t > iv