9#include <botan/internal/tls_transcript_hash_13.h>
11#include <botan/hash.h>
12#include <botan/tls_exceptn.h>
13#include <botan/tls_extensions.h>
14#include <botan/internal/stl_util.h>
15#include <botan/internal/tls_reader.h>
34 m_flavor(other.m_flavor),
35 m_hash((other.m_hash != nullptr) ? other.m_hash->copy_state() : nullptr),
36 m_unprocessed_transcript(other.m_unprocessed_transcript),
37 m_current(other.m_current),
38 m_previous(other.m_previous),
39 m_truncated(other.m_truncated) {}
46 BOTAN_STATE_CHECK(prev_transcript_hash_state.m_unprocessed_transcript.size() == 2);
50 const auto& client_hello_1 = prev_transcript_hash_state.m_unprocessed_transcript.front();
51 const auto& hello_retry_request = prev_transcript_hash_state.m_unprocessed_transcript.back();
53 const auto hash_length = transcript_hash.m_hash->output_length();
60 const auto message_hash_header =
63 transcript_hash.m_hash->update(client_hello_1.first);
64 transcript_hash.m_hash->update(client_hello_1.second);
67 transcript_hash.
update(message_hash_header, message_hash_msg);
68 transcript_hash.
update(hello_retry_request.first, hello_retry_request.second);
70 return transcript_hash;
87std::optional<size_t> find_client_hello_truncation_mark(std::span<const uint8_t> client_hello,
TLS_Flavor flavor) {
93 reader.discard_next(2);
96 reader.discard_next(32);
99 const auto session_id_length = reader.get_byte();
100 reader.discard_next(session_id_length);
103 const auto ciphersuites_length = reader.get_uint16_t();
104 reader.discard_next(ciphersuites_length);
107 const auto compression_methods_length = reader.get_byte();
108 reader.discard_next(compression_methods_length);
111 const auto extensions_length = reader.get_uint16_t();
112 const auto extensions_offset = reader.read_so_far();
113 while(reader.has_remaining() && reader.read_so_far() - extensions_offset < extensions_length) {
114 const auto ext_type =
static_cast<Extension_Code>(reader.get_uint16_t());
115 const auto ext_length = reader.get_uint16_t();
119 reader.discard_next(ext_length);
124 const auto identities_length = reader.get_uint16_t();
125 reader.discard_next(identities_length);
128 const auto binders_length = reader.peek_uint16_t();
129 if(binders_length != reader.remaining_bytes() - 2 ) {
130 throw TLS_Exception(Alert::IllegalParameter,
131 "Failed to truncate Client Hello that doesn't end on the PSK binders list");
135 return reader.read_so_far();
142std::vector<uint8_t> read_hash_state(std::unique_ptr<HashFunction>& hash) {
146 return hash->copy_state()->final_stdvec();
153 if(m_hash !=
nullptr) {
154 m_hash->update(tls_message_header);
158 const auto message_type =
static_cast<Handshake_Type>(tls_message_header[0]);
160 const auto truncation_mark = find_client_hello_truncation_mark(serialized_message_s, m_flavor);
161 if(truncation_mark.has_value()) {
162 m_hash->update(serialized_message_s.
get().first(*truncation_mark));
163 m_truncated = read_hash_state(m_hash);
164 m_hash->update(serialized_message_s.
get().subspan(*truncation_mark));
167 m_hash->update(serialized_message_s);
171 m_hash->update(serialized_message_s);
174 m_previous = std::exchange(m_current, read_hash_state(m_hash));
176 m_unprocessed_transcript.emplace_back(tls_message_header, serialized_message_s);
197 if(m_hash !=
nullptr) {
202 for(
const auto& [header, msg] : m_unprocessed_transcript) {
205 m_unprocessed_transcript.clear();
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_STATE_CHECK(expr)
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
underlying_span get() const
const Transcript_Hash & current() const
void update(HandshakeProtocolHeader tls_message_header, StrongSpan< const SerializedHandshakeMessage > serialized_message)
Transcript_Hash_State clone() const
static Transcript_Hash_State recreate_after_hello_retry_request(std::string_view algo_spec, const Transcript_Hash_State &prev_transcript_hash_state)
void set_algorithm(std::string_view algo_spec)
const Transcript_Hash & previous() const
const Transcript_Hash & truncated() const
Transcript_Hash_State(TLS_Flavor flavor)
Transcript_Hash_State & operator=(const Transcript_Hash_State &)=delete
std::vector< uint8_t > Transcript_Hash
Strong< std::array< uint8_t, 4 >, struct HandshakeProtocolHeader_ > HandshakeProtocolHeader
Holds the serialization of a TLS 1.3 handshake protocol header.
Strong< std::vector< uint8_t >, struct SerializedHandshakeMessage_ > SerializedHandshakeMessage
auto to_underlying(T e) noexcept