Botan 3.14.1
Crypto and TLS for C&
tls_handshake_layer_13.cpp
Go to the documentation of this file.
1/*
2* TLS handshake state (machine) implementation for TLS 1.3
3* (C) 2022 Jack Lloyd
4* 2022 Hannes Rantzsch, René Meusel - neXenio GmbH
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/tls_handshake_layer_13.h>
10
11#include <botan/tls_alert.h>
12#include <botan/tls_exceptn.h>
13#include <botan/tls_policy.h>
14#include <botan/internal/buffer_slicer.h>
15#include <botan/internal/concat_util.h>
16#include <botan/internal/fmt.h>
17#include <botan/internal/loadstor.h>
18#include <botan/internal/stl_util.h>
19#include <botan/internal/tls_reader.h>
20#include <botan/internal/tls_transcript_hash_13.h>
21
22namespace Botan::TLS {
23
24namespace {
25
26constexpr size_t HEADER_LENGTH = 4;
27
28} // namespace
29
31 std::span<const uint8_t> msg,
32 const Policy& policy) const {
33 switch(type) {
34 // Client Hello and Server Hello messages are ambiguous. Both may come
35 // from non-TLS 1.3 peers. Hence, their parsing is somewhat different.
37 // ... might be TLS 1.2 Client Hello or TLS 1.3 Client Hello
40 // ... might be TLS 1.2 Server Hello or TLS 1.3 Server Hello or
41 // a TLS 1.3 Hello Retry Request disguising as a Server Hello
43 // case Handshake_Type::EndOfEarlyData:
44 // return End_Of_Early_Data(msg);
46 return Encrypted_Extensions(msg);
48 return Certificate_13(msg, policy, peer(), certificate_type());
50 return Certificate_Request_13(msg, peer());
52 return Certificate_Verify_13(msg, peer());
54 return Finished_13({msg.begin(), msg.end()});
55 default:
56 throw TLS_Exception(AlertType::UnexpectedMessage, "Unexpected handshake message received");
57 }
58}
59
61 std::span<const uint8_t> msg) const {
62 switch(type) {
64 return New_Session_Ticket_13(msg, peer());
66 return Key_Update(msg);
67 default:
68 throw TLS_Exception(AlertType::UnexpectedMessage, "Unexpected post-handshake message received");
69 }
70}
71
73 Transcript_Hash_State& transcript_hash) {
74 const auto msg_bytes = serialize(message);
75 transcript_hash.update(TLS_Handshake_Layer::prepare_header(message.wire_type(), msg_bytes.size()), msg_bytes);
76}
77
79 const auto type = static_cast<Handshake_Type>(value);
80
81 switch(type) {
82 // Handshake message types
90 if(phase == Handshake_Phase::Handshake) {
91 return type;
92 }
93 break;
94
95 // Post-handshake message types
99 return type;
100 }
101 break;
102
103 // TODO(0-RTT): implement handling for EndOfEarlyData
105
106 // TLS 1.2 specific handshake messages
114
115 // Pseudo handshake messages that aren't actually expected on the wire
120 break;
121 };
122
123 if(phase == Handshake_Phase::Handshake) {
124 throw TLS_Exception(AlertType::UnexpectedMessage, "Unknown handshake message received");
125 } else {
126 throw TLS_Exception(AlertType::UnexpectedMessage, "Unknown post-handshake message received");
127 }
128}
129
131 BOTAN_ASSERT_NOMSG(payload_length <= 0xFFFFFF);
132 auto header = HandshakeProtocolHeader(store_be(static_cast<uint32_t>(payload_length)));
133 header[0] = static_cast<uint8_t>(type);
134 return header;
135}
136
137void TLS_Handshake_Layer::copy_data(const Policy& policy, const Handshake_Record& data_from_peer) {
138 BOTAN_UNUSED(policy);
139
140 // Compact consumed data before appending new data
141 BOTAN_ASSERT_NOMSG(m_read_offset <= m_read_buffer.size());
142 if(m_read_offset > 0) {
143 m_read_buffer.erase(m_read_buffer.begin(), m_read_buffer.begin() + m_read_offset);
144 m_read_offset = 0;
145 }
146
147 m_read_buffer.insert(m_read_buffer.end(), data_from_peer.payload.begin(), data_from_peer.payload.end());
148}
149
150std::optional<TLS_Handshake_Layer::IncomingMessage> TLS_Handshake_Layer::next_incoming_message(const Policy& policy,
151 Handshake_Phase phase) {
152 BOTAN_ASSERT_NOMSG(m_read_offset <= m_read_buffer.size());
153
154 // prepare reading from the current read offset
155 auto bs = BufferSlicer(std::span{m_read_buffer}.subspan(m_read_offset));
156
157 // read the message header
158 if(bs.remaining() < HEADER_LENGTH) {
159 return std::nullopt;
160 }
161
163 bs.copy_into(header);
164 const auto type = read_handshake_message_type(header[0], phase);
165 const auto msg_length = make_uint32(0, header[1], header[2], header[3]);
166
167 const auto max_msg_length = policy.maximum_handshake_message_size();
168 if(max_msg_length > 0 && msg_length > max_msg_length) {
169 throw TLS_Exception(
170 Alert::HandshakeFailure,
171 Botan::fmt("Handshake message is {} bytes, policy maximum is {}", msg_length, max_msg_length));
172 }
173
174 // read the message body
175 if(bs.remaining() < msg_length) {
176 return std::nullopt;
177 }
178
179 auto incoming_msg = IncomingMessage{
180 .type = type,
181 .header = header,
182 .message = bs.copy<SerializedHandshakeMessage>(msg_length),
183 };
184
185 // update the read buffer and offset after consuming the message
186 if(bs.empty()) {
187 m_read_buffer.clear();
188 m_read_offset = 0;
189 } else {
190 m_read_offset = m_read_buffer.size() - bs.remaining();
191 BOTAN_ASSERT_NOMSG(m_read_offset <= m_read_buffer.size());
192 }
193
194 return incoming_msg;
195}
196
197std::optional<Handshake_Message_13> TLS_Handshake_Layer::next_message(const Policy& policy,
198 Transcript_Hash_State& transcript_hash) {
199 if(auto msg = next_incoming_message(policy, Handshake_Phase::Handshake)) {
200 auto parsed = parse_handshake_message(msg->type, msg->message, policy);
201 transcript_hash.update(msg->header, msg->message);
202 return parsed;
203 } else {
204 return std::nullopt;
205 }
206}
207
208std::optional<Post_Handshake_Message_13> TLS_Handshake_Layer::next_post_handshake_message(const Policy& policy) {
209 if(auto msg = next_incoming_message(policy, Handshake_Phase::PostHandshake)) {
210 return parse_post_handshake_message(msg->type, msg->message);
211 } else {
212 return std::nullopt;
213 }
214}
215
216} // namespace Botan::TLS
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
static std::variant< Client_Hello_13, Client_Hello_12_Shim > parse(std::span< const uint8_t > buf)
static void update_transcript_for_psk_binder_calc(const Client_Hello_13 &message, Transcript_Hash_State &transcript_hash)
static Handshake_Type read_handshake_message_type(uint8_t value, Handshake_Phase phase)
Post_Handshake_Message_13 parse_post_handshake_message(Handshake_Type type, std::span< const uint8_t > msg) const
virtual Handshake_Message_13 parse_handshake_message(Handshake_Type type, std::span< const uint8_t > msg, const Policy &policy) const
Certificate_Type certificate_type() const
static SerializedHandshakeMessage serialize(const Handshake_Message &message)
virtual Handshake_Type wire_type() const
virtual size_t maximum_handshake_message_size() const
static std::variant< Hello_Retry_Request, Server_Hello_13, Server_Hello_12_Shim > parse(std::span< const uint8_t > buf)
std::optional< Handshake_Message_13 > next_message(const Policy &policy, Transcript_Hash_State &transcript_hash) override
void copy_data(const Policy &policy, const Handshake_Record &data_from_peer) override
static HandshakeProtocolHeader prepare_header(Handshake_Type type, size_t payload_length)
std::optional< Post_Handshake_Message_13 > next_post_handshake_message(const Policy &policy) override
void update(HandshakeProtocolHeader tls_message_header, StrongSpan< const SerializedHandshakeMessage > serialized_message)
Strong< std::array< uint8_t, 4 >, struct HandshakeProtocolHeader_ > HandshakeProtocolHeader
Holds the serialization of a TLS 1.3 handshake protocol header.
std::variant< Client_Hello_13, Client_Hello_12_Shim, Server_Hello_13, Server_Hello_12_Shim, Hello_Retry_Request, Encrypted_Extensions, Certificate_13, Certificate_Request_13, Certificate_Verify_13, Finished_13 > Handshake_Message_13
Strong< std::vector< uint8_t >, struct SerializedHandshakeMessage_ > SerializedHandshakeMessage
std::variant< New_Session_Ticket_13, Key_Update > Post_Handshake_Message_13
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3)
Definition loadstor.h:104
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
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...
Definition stl_util.h:88
secure_vector< uint8_t > payload