Botan 3.12.0
Crypto and TLS for C&
tls_record_layer_13.cpp
Go to the documentation of this file.
1/*
2* TLS record layer 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_record_layer_13.h>
10
11#include <botan/tls_alert.h>
12#include <botan/tls_exceptn.h>
13#include <botan/tls_version.h>
14#include <botan/internal/ct_utils.h>
15#include <botan/internal/loadstor.h>
16#include <botan/internal/tls_cipher_state.h>
17#include <algorithm>
18
19namespace Botan::TLS {
20
21namespace {
22
23template <typename IteratorT>
24bool verify_change_cipher_spec(const IteratorT data, const size_t size) {
25 // RFC 8446 5.
26 // An implementation may receive an unencrypted record of type
27 // change_cipher_spec consisting of the single byte value 0x01
28 // at any time [...]. An implementation which receives any other
29 // change_cipher_spec value or which receives a protected
30 // change_cipher_spec record MUST abort the handshake [...].
31 const size_t expected_fragment_length = 1;
32 const uint8_t expected_fragment_byte = 0x01;
33 return (size == expected_fragment_length && *data == expected_fragment_byte);
34}
35
36Record_Type read_record_type(const uint8_t type_byte) {
37 // RFC 8446 5.
38 // If a TLS implementation receives an unexpected record type,
39 // it MUST terminate the connection with an "unexpected_message" alert.
40 if(type_byte != static_cast<uint8_t>(Record_Type::ApplicationData) &&
41 type_byte != static_cast<uint8_t>(Record_Type::Handshake) &&
42 type_byte != static_cast<uint8_t>(Record_Type::Alert) &&
43 type_byte != static_cast<uint8_t>(Record_Type::ChangeCipherSpec)) {
44 throw TLS_Exception(Alert::UnexpectedMessage, "TLS record type had unexpected value");
45 }
46
47 return static_cast<Record_Type>(type_byte);
48}
49
50/**
51 * RFC 8446 5.1 `TLSPlaintext` without the `fragment` payload data
52 */
53class TLSPlaintext_Header final {
54 public:
55 TLSPlaintext_Header(std::vector<uint8_t> hdr, const bool check_tls13_version) {
56 // NOLINTBEGIN(*-prefer-member-initializer)
57 m_type = read_record_type(hdr[0]);
58 m_legacy_version = Protocol_Version(make_uint16(hdr[1], hdr[2]));
59 m_fragment_length = make_uint16(hdr[3], hdr[4]);
60 m_serialized = std::move(hdr);
61 // NOLINTEND(*-prefer-member-initializer)
62
63 // If no full version check is requested, we just verify the practically
64 // ossified major version number.
65 if(m_legacy_version.major_version() != 0x03) {
66 throw TLS_Exception(Alert::IllegalParameter, "Received unexpected record version");
67 }
68
69 // RFC 8446 5.1
70 // legacy_record_version: MUST be set to 0x0303 for all records
71 // generated by a TLS 1.3 implementation
72 if(check_tls13_version && m_legacy_version.version_code() != 0x0303) {
73 throw TLS_Exception(Alert::IllegalParameter, "Received unexpected record version");
74 }
75
76 // RFC 8446 5.1
77 // Implementations MUST NOT send zero-length fragments of Handshake
78 // types, even if those fragments contain padding.
79 //
80 // Zero-length fragments of Application Data MAY be sent, as they are
81 // potentially useful as a traffic analysis countermeasure.
82 if(m_fragment_length == 0 && type() != Record_Type::ApplicationData) {
83 throw TLS_Exception(Alert::DecodeError, "empty record received");
84 }
85
86 if(m_type == Record_Type::ApplicationData) {
87 // RFC 8446 5.2
88 // The length [...] is the sum of the lengths of the content and the
89 // padding, plus one for the inner content type, plus any expansion
90 // added by the AEAD algorithm. The length MUST NOT exceed 2^14 + 256 bytes.
91 //
92 // Note: Limits imposed by a "record_size_limit" extension do not come
93 // into play here, as those limits are on the plaintext _not_ the
94 // encrypted data. Constricted devices must be able to deal with
95 // data overhead inflicted by the AEAD.
96 if(m_fragment_length > MAX_CIPHERTEXT_SIZE_TLS13) {
97 throw TLS_Exception(Alert::RecordOverflow, "Received an encrypted record that exceeds maximum size");
98 }
99 } else {
100 // RFC 8446 5.1
101 // The length MUST NOT exceed 2^14 bytes. An endpoint that receives a record that
102 // exceeds this length MUST terminate the connection with a "record_overflow" alert.
103 //
104 // RFC 8449 4.
105 // When the "record_size_limit" extension is negotiated, an endpoint
106 // MUST NOT generate a protected record with plaintext that is larger
107 // than the RecordSizeLimit value it receives from its peer.
108 // -> Unprotected messages are not subject to this limit. <-
109 if(m_fragment_length > MAX_PLAINTEXT_SIZE) {
110 throw TLS_Exception(Alert::RecordOverflow, "Received a record that exceeds maximum size");
111 }
112 }
113 }
114
115 TLSPlaintext_Header(const Record_Type record_type,
116 const size_t frgmnt_length,
117 const bool use_compatibility_version) :
118 m_type(record_type),
119 m_legacy_version(use_compatibility_version ? 0x0301 : 0x0303) // RFC 8446 5.1
120 ,
121 m_fragment_length(static_cast<uint16_t>(frgmnt_length)),
122 m_serialized({
123 static_cast<uint8_t>(m_type),
124 m_legacy_version.major_version(),
125 m_legacy_version.minor_version(),
126 get_byte<0>(m_fragment_length),
127 get_byte<1>(m_fragment_length),
128 }) {}
129
130 Record_Type type() const { return m_type; }
131
132 uint16_t fragment_length() const { return m_fragment_length; }
133
134 Protocol_Version legacy_version() const { return m_legacy_version; }
135
136 const std::vector<uint8_t>& serialized() const { return m_serialized; }
137
138 private:
139 Record_Type m_type;
140 Protocol_Version m_legacy_version;
141 uint16_t m_fragment_length;
142 std::vector<uint8_t> m_serialized;
143};
144
145} // namespace
146
148 m_side(side),
149 m_outgoing_record_size_limit(MAX_PLAINTEXT_SIZE + 1 /* content type byte */),
150 m_incoming_record_size_limit(MAX_PLAINTEXT_SIZE + 1 /* content type byte */)
151
152 // RFC 8446 5.1
153 // legacy_record_version: MUST be set to 0x0303 for all records
154 // generated by a TLS 1.3 implementation other than an initial
155 // ClientHello [...], where it MAY also be 0x0301 for compatibility
156 // purposes.
157 //
158 // Additionally, older peers might send other values while requesting a
159 // protocol downgrade. I.e. we need to be able to tolerate/emit legacy
160 // values until we negotiated a TLS 1.3 compliant connection.
161 //
162 // As a client: we may initially emit the compatibility version and
163 // accept a wider range of incoming legacy record versions.
164 // As a server: we start with emitting the specified legacy version of 0x0303
165 // but must also allow a wider range of incoming legacy values.
166 //
167 // Once TLS 1.3 is negotiateed, the implementations will disable these
168 // compatibility modes accordingly or a protocol downgrade will transfer
169 // the marshalling responsibility to our TLS 1.2 implementation.
170 ,
171 m_sending_compat_mode(m_side == Connection_Side::Client),
172 m_receiving_compat_mode(true) {}
173
174void Record_Layer::copy_data(std::span<const uint8_t> data) {
175 // Compact consumed data before appending new data
176 BOTAN_ASSERT_NOMSG(m_read_offset <= m_read_buffer.size());
177 if(m_read_offset > 0) {
178 m_read_buffer.erase(m_read_buffer.begin(), m_read_buffer.begin() + m_read_offset);
179 m_read_offset = 0;
180 }
181
182 m_read_buffer.insert(m_read_buffer.end(), data.begin(), data.end());
183}
184
185std::vector<uint8_t> Record_Layer::prepare_records(const Record_Type type,
186 std::span<const uint8_t> data,
187 Cipher_State* cipher_state) const {
188 // RFC 8446 5.
189 // Note that [change_cipher_spec records] may appear at a point at the
190 // handshake where the implementation is expecting protected records.
191 //
192 // RFC 8446 5.
193 // An implementation which receives [...] a protected change_cipher_spec
194 // record MUST abort the handshake [...].
195 //
196 // ... hence, CHANGE_CIPHER_SPEC is never protected, even if a usable cipher
197 // state was passed to this method.
198 const bool protect = cipher_state != nullptr && type != Record_Type::ChangeCipherSpec;
199
200 // RFC 8446 5.1
202 "Application Data records MUST NOT be written to the wire unprotected");
203
204 // RFC 8446 5.1
205 // "MUST NOT sent zero-length fragments of Handshake types"
206 // "a record with an Alert type MUST contain exactly one message" [of non-zero length]
207 // "Zero-length fragments of Application Data MAY be sent"
208 BOTAN_ASSERT(!data.empty() || type == Record_Type::ApplicationData,
209 "zero-length fragments of types other than application data are not allowed");
210
211 if(type == Record_Type::ChangeCipherSpec && !verify_change_cipher_spec(data.begin(), data.size())) {
212 throw Invalid_Argument("TLS 1.3 deprecated CHANGE_CIPHER_SPEC");
213 }
214
215 std::vector<uint8_t> output;
216
217 // RFC 8446 5.2
218 // type: The TLSPlaintext.type value containing the content type of the record.
219 constexpr size_t content_type_tag_length = 1;
220
221 // RFC 8449 4.
222 // When the "record_size_limit" extension is negotiated, an endpoint
223 // MUST NOT generate a protected record with plaintext that is larger
224 // than the RecordSizeLimit value it receives from its peer.
225 // Unprotected messages are not subject to this limit.
226 const size_t max_plaintext_size =
227 (protect) ? m_outgoing_record_size_limit - content_type_tag_length : static_cast<uint16_t>(MAX_PLAINTEXT_SIZE);
228
229 const auto records = std::max((data.size() + max_plaintext_size - 1) / max_plaintext_size, size_t(1));
230 auto output_length = records * TLS_HEADER_SIZE;
231 if(protect) {
232 // n-1 full records of size max_plaintext_size
233 output_length +=
234 (records - 1) * cipher_state->encrypt_output_length(max_plaintext_size + content_type_tag_length);
235 // last record with size of remaining data
236 output_length += cipher_state->encrypt_output_length(data.size() - ((records - 1) * max_plaintext_size) +
237 content_type_tag_length);
238 } else {
239 output_length += data.size();
240 }
241 output.reserve(output_length);
242
243 size_t pt_offset = 0;
244 size_t to_process = data.size();
245
246 // For protected records we need to write at least one encrypted fragment,
247 // even if the plaintext size is zero. This happens only for Application
248 // Data types.
249 BOTAN_ASSERT_NOMSG(to_process != 0 || protect);
250 // NOLINTNEXTLINE(*-avoid-do-while)
251 do {
252 const size_t pt_size = std::min<size_t>(to_process, max_plaintext_size);
253 const size_t ct_size =
254 (!protect) ? pt_size : cipher_state->encrypt_output_length(pt_size + content_type_tag_length);
255 const auto pt_type = (!protect) ? type : Record_Type::ApplicationData;
256
257 // RFC 8446 5.1
258 // MUST be set to 0x0303 for all records generated by a TLS 1.3
259 // implementation other than an initial ClientHello [...], where
260 // it MAY also be 0x0301 for compatibility purposes.
261 const auto record_header = TLSPlaintext_Header(pt_type, ct_size, m_sending_compat_mode).serialized();
262
263 output.insert(output.end(), record_header.cbegin(), record_header.cend());
264
265 auto pt_fragment = data.subspan(pt_offset, pt_size);
266 if(protect) {
267 secure_vector<uint8_t> fragment;
268 fragment.reserve(ct_size);
269
270 // assemble TLSInnerPlaintext structure
271 fragment.insert(fragment.end(), pt_fragment.begin(), pt_fragment.end());
272 fragment.push_back(static_cast<uint8_t>(type));
273 // TODO: zero padding could go here, see RFC 8446 5.4
274
275 cipher_state->encrypt_record_fragment(record_header, fragment);
276 BOTAN_ASSERT_NOMSG(fragment.size() == ct_size);
277
278 output.insert(output.end(), fragment.cbegin(), fragment.cend());
279 } else {
280 output.insert(output.end(), pt_fragment.begin(), pt_fragment.end());
281 }
282
283 pt_offset += pt_size;
284 to_process -= pt_size;
285 } while(to_process > 0);
286
287 BOTAN_ASSERT_NOMSG(output.size() == output_length);
288 return output;
289}
290
292 const auto remaining = m_read_buffer.size() - m_read_offset;
293
294 if(remaining < TLS_HEADER_SIZE) {
295 return TLS_HEADER_SIZE - remaining;
296 }
297
298 const auto header_begin = m_read_buffer.cbegin() + m_read_offset;
299 const auto header_end = header_begin + TLS_HEADER_SIZE;
300
301 // The first received record(s) are likely a client or server hello. To be able to
302 // perform protocol downgrades we must be less vigorous with the record's
303 // legacy version. Hence, `check_tls13_version` is `false` for the first record(s).
304 const TLSPlaintext_Header plaintext_header({header_begin, header_end}, !m_receiving_compat_mode);
305
306 // After the key exchange phase of the handshake is completed and record protection is engaged,
307 // cipher_state is set. At this point, only protected traffic (and CCS) is allowed.
308 //
309 // RFC 8446 2.
310 // - Key Exchange: Establish shared keying material and select the
311 // cryptographic parameters. Everything after this phase is
312 // encrypted.
313 // RFC 8446 5.
314 // An implementation may receive an unencrypted [CCS] at any time
315 if(cipher_state != nullptr && plaintext_header.type() != Record_Type::ApplicationData &&
316 plaintext_header.type() != Record_Type::ChangeCipherSpec &&
317 (!cipher_state->must_expect_unprotected_alert_traffic() || plaintext_header.type() != Record_Type::Alert)) {
318 throw TLS_Exception(Alert::UnexpectedMessage, "unprotected record received where protected traffic was expected");
319 }
320
321 if(remaining < TLS_HEADER_SIZE + plaintext_header.fragment_length()) {
322 return TLS_HEADER_SIZE + plaintext_header.fragment_length() - remaining;
323 }
324
325 const auto fragment_begin = header_end;
326 const auto fragment_end = fragment_begin + plaintext_header.fragment_length();
327
328 if(plaintext_header.type() == Record_Type::ChangeCipherSpec &&
329 !verify_change_cipher_spec(fragment_begin, plaintext_header.fragment_length())) {
330 throw TLS_Exception(Alert::UnexpectedMessage, "malformed change cipher spec record received");
331 }
332
333 Record record(plaintext_header.type(), secure_vector<uint8_t>(fragment_begin, fragment_end));
334 m_read_offset += TLS_HEADER_SIZE + plaintext_header.fragment_length();
335
336 // If all buffered data has been consumed, release the buffer memory
337 // to avoid retaining peak allocation on idle connections.
338 if(m_read_offset == m_read_buffer.size()) {
339 zap(m_read_buffer);
340 m_read_offset = 0;
341 }
342
343 if(record.type == Record_Type::ApplicationData) {
344 if(cipher_state == nullptr) {
345 // This could also mean a misuse of the interface, i.e. failing to provide a valid
346 // cipher_state to parse_records when receiving valid (encrypted) Application Data.
347 throw TLS_Exception(Alert::UnexpectedMessage, "premature Application Data received");
348 }
349
350 if(record.fragment.size() < cipher_state->minimum_decryption_input_length()) {
351 throw TLS_Exception(Alert::BadRecordMac, "incomplete record mac received");
352 }
353
354 if(cipher_state->decrypt_output_length(record.fragment.size()) > m_incoming_record_size_limit) {
355 throw TLS_Exception(Alert::RecordOverflow, "Received an encrypted record that exceeds maximum plaintext size");
356 }
357
358 record.seq_no = cipher_state->decrypt_record_fragment(plaintext_header.serialized(), record.fragment);
359
360 // Remove record padding (RFC 8446 5.4). The TLSInnerPlaintext layout is
361 // content || content_type || zero_padding
362 auto seen_nonzero = CT::Mask<uint8_t>::cleared();
363 uint8_t content_type_byte = 0;
364 size_t content_index = 0;
365 for(size_t i = record.fragment.size(); i-- > 0;) {
366 const uint8_t b = record.fragment[i];
367 const auto byte_is_nonzero = CT::Mask<uint8_t>::expand(b);
368 // Set on the first non-zero byte we encounter scanning right-to-left.
369 const auto first_nonzero = byte_is_nonzero & ~seen_nonzero;
370 content_type_byte = first_nonzero.select(b, content_type_byte);
371 content_index = CT::Mask<size_t>::expand(first_nonzero.value()).select(i, content_index);
372 seen_nonzero |= byte_is_nonzero;
373 }
374
375 if(!seen_nonzero.as_bool()) {
376 // RFC 8446 5.4
377 // If a receiving implementation does not
378 // find a non-zero octet in the cleartext, it MUST terminate the
379 // connection with an "unexpected_message" alert.
380 throw TLS_Exception(Alert::UnexpectedMessage, "No content type found in encrypted record");
381 }
382
383 // hydrate the actual content type from TLSInnerPlaintext
384 record.type = read_record_type(content_type_byte);
385
386 if(record.type == Record_Type::ChangeCipherSpec) {
387 // RFC 8446 5
388 // An implementation [...] which receives a protected change_cipher_spec record MUST
389 // abort the handshake with an "unexpected_message" alert.
390 throw TLS_Exception(Alert::UnexpectedMessage, "protected change cipher spec received");
391 }
392
393 // Truncate to drop the content_type byte and padding. resize() on a
394 // vector of trivially-destructible elements is bookkeeping-only and
395 // does not allocate or iterate over the dropped suffix.
396 record.fragment.resize(content_index);
397
398 // RFC 8446 5.4
399 // Implementations MUST NOT send Handshake and Alert records that have
400 // a zero-length TLSInnerPlaintext.content; if such a message is
401 // received, the receiving implementation MUST terminate the connection
402 // with an "unexpected_message" alert.
403 if(record.fragment.empty() && record.type != Record_Type::ApplicationData) {
404 throw TLS_Exception(Alert::UnexpectedMessage,
405 "Received a protected record with empty TLSInnerPlaintext content");
406 }
407 }
408
409 return record;
410}
411
412void Record_Layer::set_record_size_limits(const uint16_t outgoing_limit, const uint16_t incoming_limit) {
413 BOTAN_ARG_CHECK(outgoing_limit >= 64, "Invalid outgoing record size limit");
414 BOTAN_ARG_CHECK(incoming_limit >= 64 && incoming_limit <= MAX_PLAINTEXT_SIZE + 1,
415 "Invalid incoming record size limit");
416
417 // RFC 8449 4.
418 // Even if a larger record size limit is provided by a peer, an endpoint
419 // MUST NOT send records larger than the protocol-defined limit, unless
420 // explicitly allowed by a future TLS version or extension.
421 m_outgoing_record_size_limit = std::min(outgoing_limit, static_cast<uint16_t>(MAX_PLAINTEXT_SIZE + 1));
422 m_incoming_record_size_limit = incoming_limit;
423}
424
425} // namespace Botan::TLS
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
static constexpr Mask< T > expand(T v)
Definition ct_utils.h:392
static constexpr Mask< T > cleared()
Definition ct_utils.h:387
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)
@ MAX_PLAINTEXT_SIZE
Definition tls_magic.h:31
@ MAX_CIPHERTEXT_SIZE_TLS13
Definition tls_magic.h:41
@ TLS_HEADER_SIZE
Definition tls_magic.h:26
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:157
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)
Definition loadstor.h:92
std::optional< uint64_t > seq_no
secure_vector< uint8_t > fragment