authorgravatar for 44501064+melonedo@users.noreply.github.commelonedo <44501064+melonedo@users.noreply.github.com> 2024-01-15 22:31:15+08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-16 14:58:56-08:00
log9b0da5ccef50bf5018142dd38deaeaf91fa16429
tree804974a6283f6742af0d1caa4110e830188b563f
parentc4a1b54ebefb77122d2055b09bc0bf01bbc1d8b6

Fix TLS record overflow by limiting inner record length to 2^14

Per last paragraph of RFC 8446, Section 5.2, the length of the inner content of an encrypted record must not exceed 2^14 + 1, while that of the whole encrypted record must not exceed 2^14 + 256.

2 files changed, 5 insertions(+), 4 deletions(-)

lib/std/crypto/tls.zig+2-1
......@@ -40,7 +40,8 @@ const assert = std.debug.assert;
4040pub const Client = @import("tls/Client.zig");
4141
4242pub const record_header_len = 5;
43pub const max_ciphertext_len = (1 << 14) + 256;
43pub const max_cipertext_inner_record_len = 1 << 14;
44pub const max_ciphertext_len = max_cipertext_inner_record_len + 256;
4445pub const max_ciphertext_record_len = max_ciphertext_len + record_header_len;
4546pub const hello_retry_request_sequence = [32]u8{
4647 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11, 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
lib/std/crypto/tls/Client.zig+3-3
......@@ -805,9 +805,9 @@ fn prepareCiphertextRecord(
805805 const close_notify_alert_reserved = tls.close_notify_alert.len + overhead_len;
806806 while (true) {
807807 const encrypted_content_len: u16 = @intCast(@min(
808 @min(bytes.len - bytes_i, max_ciphertext_len - 1),
809 ciphertext_buf.len - close_notify_alert_reserved -
810 overhead_len - ciphertext_end,
808 @min(bytes.len - bytes_i, tls.max_cipertext_inner_record_len),
809 ciphertext_buf.len -|
810 (close_notify_alert_reserved + overhead_len + ciphertext_end),
811811 ));
812812 if (encrypted_content_len == 0) return .{
813813 .iovec_end = iovec_end,