Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/tls_record.cc

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/ssl.h>
6
7 #include <assert.h>
8 #include <string.h>
9
10 #include <openssl/bytestring.h>
11 #include <openssl/err.h>
12 #include <openssl/mem.h>
13
14 #include "internal.h"
15 #include "../crypto/internal.h"
16
17
18 BSSL_NAMESPACE_BEGIN
19
20 // kMaxEmptyRecords is the number of consecutive, empty records that will be
21 // processed. Without this limit an attacker could send empty records at a
22 // faster rate than we can process and cause record processing to loop
23 // forever.
24 static const uint8_t kMaxEmptyRecords = 32;
25
26 // kMaxEarlyDataSkipped is the maximum number of rejected early data bytes that
27 // will be skipped. Without this limit an attacker could send records at a
28 // faster rate than we can process and cause trial decryption to loop forever.
29 // This value should be slightly above kMaxEarlyDataAccepted, which is measured
30 // in plaintext.
31 static const size_t kMaxEarlyDataSkipped = 16384;
32
33 // kMaxWarningAlerts is the number of consecutive warning alerts that will be
34 // processed.
35 static const uint8_t kMaxWarningAlerts = 4;
36
37 // ssl_needs_record_splitting returns one if |ssl|'s current outgoing cipher
38 // state needs record-splitting and zero otherwise.
39 static bool ssl_needs_record_splitting(const SSL *ssl) {
40 #if !defined(BORINGSSL_UNSAFE_FUZZER_MODE)
41 return !ssl->s3->aead_write_ctx->is_null_cipher() &&
42 ssl->s3->aead_write_ctx->ProtocolVersion() < TLS1_1_VERSION &&
43 (ssl->mode & SSL_MODE_CBC_RECORD_SPLITTING) != 0 &&
44 SSL_CIPHER_is_block_cipher(ssl->s3->aead_write_ctx->cipher());
45 #else
46 return false;
47 #endif
48 }
49
50 bool ssl_record_sequence_update(uint8_t *seq, size_t seq_len) {
51 for (size_t i = seq_len - 1; i < seq_len; i--) {
52 ++seq[i];
53 if (seq[i] != 0) {
54 return true;
55 }
56 }
57 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
58 return false;
59 }
60
61 size_t ssl_record_prefix_len(const SSL *ssl) {
62 size_t header_len;
63 if (SSL_is_dtls(ssl)) {
64 header_len = DTLS1_RT_HEADER_LENGTH;
65 } else {
66 header_len = SSL3_RT_HEADER_LENGTH;
67 }
68
69 return header_len + ssl->s3->aead_read_ctx->ExplicitNonceLen();
70 }
71
72 size_t ssl_seal_align_prefix_len(const SSL *ssl) {
73 if (SSL_is_dtls(ssl)) {
74 return DTLS1_RT_HEADER_LENGTH + ssl->s3->aead_write_ctx->ExplicitNonceLen();
75 }
76
77 size_t ret =
78 SSL3_RT_HEADER_LENGTH + ssl->s3->aead_write_ctx->ExplicitNonceLen();
79 if (ssl_needs_record_splitting(ssl)) {
80 ret += SSL3_RT_HEADER_LENGTH;
81 ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher());
82 }
83 return ret;
84 }
85
86 static ssl_open_record_t skip_early_data(SSL *ssl, uint8_t *out_alert,
87 size_t consumed) {
88 ssl->s3->early_data_skipped += consumed;
89 if (ssl->s3->early_data_skipped < consumed) {
90 ssl->s3->early_data_skipped = kMaxEarlyDataSkipped + 1;
91 }
92
93 if (ssl->s3->early_data_skipped > kMaxEarlyDataSkipped) {
94 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MUCH_SKIPPED_EARLY_DATA);
95 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
96 return ssl_open_record_error;
97 }
98
99 return ssl_open_record_discard;
100 }
101
102 ssl_open_record_t tls_open_record(SSL *ssl, uint8_t *out_type,
103 Span<uint8_t> *out, size_t *out_consumed,
104 uint8_t *out_alert, Span<uint8_t> in) {
105 *out_consumed = 0;
106 if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
107 return ssl_open_record_close_notify;
108 }
109
110 // If there is an unprocessed handshake message or we are already buffering
111 // too much, stop before decrypting another handshake record.
112 if (!tls_can_accept_handshake_data(ssl, out_alert)) {
113 return ssl_open_record_error;
114 }
115
116 CBS cbs = CBS(in);
117
118 // Decode the record header.
119 uint8_t type;
120 uint16_t version, ciphertext_len;
121 if (!CBS_get_u8(&cbs, &type) ||
122 !CBS_get_u16(&cbs, &version) ||
123 !CBS_get_u16(&cbs, &ciphertext_len)) {
124 *out_consumed = SSL3_RT_HEADER_LENGTH;
125 return ssl_open_record_partial;
126 }
127
128 bool version_ok;
129 if (ssl->s3->aead_read_ctx->is_null_cipher()) {
130 // Only check the first byte. Enforcing beyond that can prevent decoding
131 // version negotiation failure alerts.
132 version_ok = (version >> 8) == SSL3_VERSION_MAJOR;
133 } else {
134 version_ok = version == ssl->s3->aead_read_ctx->RecordVersion();
135 }
136
137 if (!version_ok) {
138 OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER);
139 *out_alert = SSL_AD_PROTOCOL_VERSION;
140 return ssl_open_record_error;
141 }
142
143 // Check the ciphertext length.
144 if (ciphertext_len > SSL3_RT_MAX_ENCRYPTED_LENGTH) {
145 OPENSSL_PUT_ERROR(SSL, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);
146 *out_alert = SSL_AD_RECORD_OVERFLOW;
147 return ssl_open_record_error;
148 }
149
150 // Extract the body.
151 CBS body;
152 if (!CBS_get_bytes(&cbs, &body, ciphertext_len)) {
153 *out_consumed = SSL3_RT_HEADER_LENGTH + (size_t)ciphertext_len;
154 return ssl_open_record_partial;
155 }
156
157 Span<const uint8_t> header = in.subspan(0, SSL3_RT_HEADER_LENGTH);
158 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_HEADER, header);
159
160 *out_consumed = in.size() - CBS_len(&cbs);
161
162 if (ssl->s3->have_version &&
163 ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
164 SSL_in_init(ssl) &&
165 type == SSL3_RT_CHANGE_CIPHER_SPEC &&
166 ciphertext_len == 1 &&
167 CBS_data(&body)[0] == 1) {
168 ssl->s3->empty_record_count++;
169 if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
170 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
171 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
172 return ssl_open_record_error;
173 }
174 return ssl_open_record_discard;
175 }
176
177 // Skip early data received when expecting a second ClientHello if we rejected
178 // 0RTT.
179 if (ssl->s3->skip_early_data &&
180 ssl->s3->aead_read_ctx->is_null_cipher() &&
181 type == SSL3_RT_APPLICATION_DATA) {
182 return skip_early_data(ssl, out_alert, *out_consumed);
183 }
184
185 // Decrypt the body in-place.
186 if (!ssl->s3->aead_read_ctx->Open(
187 out, type, version, ssl->s3->read_sequence, header,
188 MakeSpan(const_cast<uint8_t *>(CBS_data(&body)), CBS_len(&body)))) {
189 if (ssl->s3->skip_early_data && !ssl->s3->aead_read_ctx->is_null_cipher()) {
190 ERR_clear_error();
191 return skip_early_data(ssl, out_alert, *out_consumed);
192 }
193
194 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
195 *out_alert = SSL_AD_BAD_RECORD_MAC;
196 return ssl_open_record_error;
197 }
198
199 ssl->s3->skip_early_data = false;
200
201 if (!ssl_record_sequence_update(ssl->s3->read_sequence, 8)) {
202 *out_alert = SSL_AD_INTERNAL_ERROR;
203 return ssl_open_record_error;
204 }
205
206 // TLS 1.3 hides the record type inside the encrypted data.
207 bool has_padding =
208 !ssl->s3->aead_read_ctx->is_null_cipher() &&
209 ssl->s3->aead_read_ctx->ProtocolVersion() >= TLS1_3_VERSION;
210
211 // If there is padding, the plaintext limit includes the padding, but includes
212 // extra room for the inner content type.
213 size_t plaintext_limit =
214 has_padding ? SSL3_RT_MAX_PLAIN_LENGTH + 1 : SSL3_RT_MAX_PLAIN_LENGTH;
215 if (out->size() > plaintext_limit) {
216 OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
217 *out_alert = SSL_AD_RECORD_OVERFLOW;
218 return ssl_open_record_error;
219 }
220
221 if (has_padding) {
222 // The outer record type is always application_data.
223 if (type != SSL3_RT_APPLICATION_DATA) {
224 OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_OUTER_RECORD_TYPE);
225 *out_alert = SSL_AD_DECODE_ERROR;
226 return ssl_open_record_error;
227 }
228
229 do {
230 if (out->empty()) {
231 OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);
232 *out_alert = SSL_AD_DECRYPT_ERROR;
233 return ssl_open_record_error;
234 }
235 type = out->back();
236 *out = out->subspan(0, out->size() - 1);
237 } while (type == 0);
238 }
239
240 // Limit the number of consecutive empty records.
241 if (out->empty()) {
242 ssl->s3->empty_record_count++;
243 if (ssl->s3->empty_record_count > kMaxEmptyRecords) {
244 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_EMPTY_FRAGMENTS);
245 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
246 return ssl_open_record_error;
247 }
248 // Apart from the limit, empty records are returned up to the caller. This
249 // allows the caller to reject records of the wrong type.
250 } else {
251 ssl->s3->empty_record_count = 0;
252 }
253
254 if (type == SSL3_RT_ALERT) {
255 return ssl_process_alert(ssl, out_alert, *out);
256 }
257
258 // Handshake messages may not interleave with any other record type.
259 if (type != SSL3_RT_HANDSHAKE &&
260 tls_has_unprocessed_handshake_data(ssl)) {
261 OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_RECORD);
262 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
263 return ssl_open_record_error;
264 }
265
266 ssl->s3->warning_alert_count = 0;
267
268 *out_type = type;
269 return ssl_open_record_success;
270 }
271
272 static bool do_seal_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
273 uint8_t *out_suffix, uint8_t type, const uint8_t *in,
274 const size_t in_len) {
275 SSLAEADContext *aead = ssl->s3->aead_write_ctx.get();
276 uint8_t *extra_in = NULL;
277 size_t extra_in_len = 0;
278 if (!aead->is_null_cipher() &&
279 aead->ProtocolVersion() >= TLS1_3_VERSION) {
280 // TLS 1.3 hides the actual record type inside the encrypted data.
281 extra_in = &type;
282 extra_in_len = 1;
283 }
284
285 size_t suffix_len, ciphertext_len;
286 if (!aead->SuffixLen(&suffix_len, in_len, extra_in_len) ||
287 !aead->CiphertextLen(&ciphertext_len, in_len, extra_in_len)) {
288 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
289 return false;
290 }
291
292 assert(in == out || !buffers_alias(in, in_len, out, in_len));
293 assert(!buffers_alias(in, in_len, out_prefix, ssl_record_prefix_len(ssl)));
294 assert(!buffers_alias(in, in_len, out_suffix, suffix_len));
295
296 if (extra_in_len) {
297 out_prefix[0] = SSL3_RT_APPLICATION_DATA;
298 } else {
299 out_prefix[0] = type;
300 }
301
302 uint16_t record_version = aead->RecordVersion();
303
304 out_prefix[1] = record_version >> 8;
305 out_prefix[2] = record_version & 0xff;
306 out_prefix[3] = ciphertext_len >> 8;
307 out_prefix[4] = ciphertext_len & 0xff;
308 Span<const uint8_t> header = MakeSpan(out_prefix, SSL3_RT_HEADER_LENGTH);
309
310 if (!aead->SealScatter(out_prefix + SSL3_RT_HEADER_LENGTH, out, out_suffix,
311 out_prefix[0], record_version, ssl->s3->write_sequence,
312 header, in, in_len, extra_in, extra_in_len) ||
313 !ssl_record_sequence_update(ssl->s3->write_sequence, 8)) {
314 return false;
315 }
316
317 ssl_do_msg_callback(ssl, 1 /* write */, SSL3_RT_HEADER, header);
318 return true;
319 }
320
321 static size_t tls_seal_scatter_prefix_len(const SSL *ssl, uint8_t type,
322 size_t in_len) {
323 size_t ret = SSL3_RT_HEADER_LENGTH;
324 if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
325 ssl_needs_record_splitting(ssl)) {
326 // In the case of record splitting, the 1-byte record (of the 1/n-1 split)
327 // will be placed in the prefix, as will four of the five bytes of the
328 // record header for the main record. The final byte will replace the first
329 // byte of the plaintext that was used in the small record.
330 ret += ssl_cipher_get_record_split_len(ssl->s3->aead_write_ctx->cipher());
331 ret += SSL3_RT_HEADER_LENGTH - 1;
332 } else {
333 ret += ssl->s3->aead_write_ctx->ExplicitNonceLen();
334 }
335 return ret;
336 }
337
338 static bool tls_seal_scatter_suffix_len(const SSL *ssl, size_t *out_suffix_len,
339 uint8_t type, size_t in_len) {
340 size_t extra_in_len = 0;
341 if (!ssl->s3->aead_write_ctx->is_null_cipher() &&
342 ssl->s3->aead_write_ctx->ProtocolVersion() >= TLS1_3_VERSION) {
343 // TLS 1.3 adds an extra byte for encrypted record type.
344 extra_in_len = 1;
345 }
346 // clang-format off
347 if (type == SSL3_RT_APPLICATION_DATA &&
348 in_len > 1 &&
349 ssl_needs_record_splitting(ssl)) {
350 // With record splitting enabled, the first byte gets sealed into a separate
351 // record which is written into the prefix.
352 in_len -= 1;
353 }
354 // clang-format on
355 return ssl->s3->aead_write_ctx->SuffixLen(out_suffix_len, in_len, extra_in_len);
356 }
357
358 // tls_seal_scatter_record seals a new record of type |type| and body |in| and
359 // splits it between |out_prefix|, |out|, and |out_suffix|. Exactly
360 // |tls_seal_scatter_prefix_len| bytes are written to |out_prefix|, |in_len|
361 // bytes to |out|, and |tls_seal_scatter_suffix_len| bytes to |out_suffix|. It
362 // returns one on success and zero on error. If enabled,
363 // |tls_seal_scatter_record| implements TLS 1.0 CBC 1/n-1 record splitting and
364 // may write two records concatenated.
365 static bool tls_seal_scatter_record(SSL *ssl, uint8_t *out_prefix, uint8_t *out,
366 uint8_t *out_suffix, uint8_t type,
367 const uint8_t *in, size_t in_len) {
368 if (type == SSL3_RT_APPLICATION_DATA && in_len > 1 &&
369 ssl_needs_record_splitting(ssl)) {
370 assert(ssl->s3->aead_write_ctx->ExplicitNonceLen() == 0);
371 const size_t prefix_len = SSL3_RT_HEADER_LENGTH;
372
373 // Write the 1-byte fragment into |out_prefix|.
374 uint8_t *split_body = out_prefix + prefix_len;
375 uint8_t *split_suffix = split_body + 1;
376
377 if (!do_seal_record(ssl, out_prefix, split_body, split_suffix, type, in,
378 1)) {
379 return false;
380 }
381
382 size_t split_record_suffix_len;
383 if (!ssl->s3->aead_write_ctx->SuffixLen(&split_record_suffix_len, 1, 0)) {
384 assert(false);
385 return false;
386 }
387 const size_t split_record_len = prefix_len + 1 + split_record_suffix_len;
388 assert(SSL3_RT_HEADER_LENGTH + ssl_cipher_get_record_split_len(
389 ssl->s3->aead_write_ctx->cipher()) ==
390 split_record_len);
391
392 // Write the n-1-byte fragment. The header gets split between |out_prefix|
393 // (header[:-1]) and |out| (header[-1:]).
394 uint8_t tmp_prefix[SSL3_RT_HEADER_LENGTH];
395 if (!do_seal_record(ssl, tmp_prefix, out + 1, out_suffix, type, in + 1,
396 in_len - 1)) {
397 return false;
398 }
399 assert(tls_seal_scatter_prefix_len(ssl, type, in_len) ==
400 split_record_len + SSL3_RT_HEADER_LENGTH - 1);
401 OPENSSL_memcpy(out_prefix + split_record_len, tmp_prefix,
402 SSL3_RT_HEADER_LENGTH - 1);
403 OPENSSL_memcpy(out, tmp_prefix + SSL3_RT_HEADER_LENGTH - 1, 1);
404 return true;
405 }
406
407 return do_seal_record(ssl, out_prefix, out, out_suffix, type, in, in_len);
408 }
409
410 bool tls_seal_record(SSL *ssl, uint8_t *out, size_t *out_len,
411 size_t max_out_len, uint8_t type, const uint8_t *in,
412 size_t in_len) {
413 if (buffers_alias(in, in_len, out, max_out_len)) {
414 OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT);
415 return false;
416 }
417
418 const size_t prefix_len = tls_seal_scatter_prefix_len(ssl, type, in_len);
419 size_t suffix_len;
420 if (!tls_seal_scatter_suffix_len(ssl, &suffix_len, type, in_len)) {
421 return false;
422 }
423 if (in_len + prefix_len < in_len ||
424 prefix_len + in_len + suffix_len < prefix_len + in_len) {
425 OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE);
426 return false;
427 }
428 if (max_out_len < in_len + prefix_len + suffix_len) {
429 OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL);
430 return false;
431 }
432
433 uint8_t *prefix = out;
434 uint8_t *body = out + prefix_len;
435 uint8_t *suffix = body + in_len;
436 if (!tls_seal_scatter_record(ssl, prefix, body, suffix, type, in, in_len)) {
437 return false;
438 }
439
440 *out_len = prefix_len + in_len + suffix_len;
441 return true;
442 }
443
444 enum ssl_open_record_t ssl_process_alert(SSL *ssl, uint8_t *out_alert,
445 Span<const uint8_t> in) {
446 // Alerts records may not contain fragmented or multiple alerts.
447 if (in.size() != 2) {
448 *out_alert = SSL_AD_DECODE_ERROR;
449 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
450 return ssl_open_record_error;
451 }
452
453 ssl_do_msg_callback(ssl, 0 /* read */, SSL3_RT_ALERT, in);
454
455 const uint8_t alert_level = in[0];
456 const uint8_t alert_descr = in[1];
457
458 uint16_t alert = (alert_level << 8) | alert_descr;
459 ssl_do_info_callback(ssl, SSL_CB_READ_ALERT, alert);
460
461 if (alert_level == SSL3_AL_WARNING) {
462 if (alert_descr == SSL_AD_CLOSE_NOTIFY) {
463 ssl->s3->read_shutdown = ssl_shutdown_close_notify;
464 return ssl_open_record_close_notify;
465 }
466
467 // Warning alerts do not exist in TLS 1.3, but RFC 8446 section 6.1
468 // continues to define user_canceled as a signal to cancel the handshake,
469 // without specifying how to handle it. JDK11 misuses it to signal
470 // full-duplex connection close after the handshake. As a workaround, skip
471 // user_canceled as in TLS 1.2. This matches NSS and OpenSSL.
472 if (ssl->s3->have_version &&
473 ssl_protocol_version(ssl) >= TLS1_3_VERSION &&
474 alert_descr != SSL_AD_USER_CANCELLED) {
475 *out_alert = SSL_AD_DECODE_ERROR;
476 OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ALERT);
477 return ssl_open_record_error;
478 }
479
480 ssl->s3->warning_alert_count++;
481 if (ssl->s3->warning_alert_count > kMaxWarningAlerts) {
482 *out_alert = SSL_AD_UNEXPECTED_MESSAGE;
483 OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_WARNING_ALERTS);
484 return ssl_open_record_error;
485 }
486 return ssl_open_record_discard;
487 }
488
489 if (alert_level == SSL3_AL_FATAL) {
490 OPENSSL_PUT_ERROR(SSL, SSL_AD_REASON_OFFSET + alert_descr);
491 ERR_add_error_dataf("SSL alert number %d", alert_descr);
492 *out_alert = 0; // No alert to send back to the peer.
493 return ssl_open_record_error;
494 }
495
496 *out_alert = SSL_AD_ILLEGAL_PARAMETER;
497 OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_ALERT_TYPE);
498 return ssl_open_record_error;
499 }
500
501 BSSL_NAMESPACE_END
502
503 using namespace bssl;
504
505 size_t SSL_max_seal_overhead(const SSL *ssl) {
506 if (SSL_is_dtls(ssl)) {
507 return dtls_max_seal_overhead(ssl, dtls1_use_current_epoch);
508 }
509
510 size_t ret = SSL3_RT_HEADER_LENGTH;
511 ret += ssl->s3->aead_write_ctx->MaxOverhead();
512 // TLS 1.3 needs an extra byte for the encrypted record type.
513 if (!ssl->s3->aead_write_ctx->is_null_cipher() &&
514 ssl->s3->aead_write_ctx->ProtocolVersion() >= TLS1_3_VERSION) {
515 ret += 1;
516 }
517 if (ssl_needs_record_splitting(ssl)) {
518 ret *= 2;
519 }
520 return ret;
521 }