| ... | @@ -338,18 +338,13 @@ pub fn init(input: *Reader, output: *Writer, options: Options) InitError!Client | ... | @@ -338,18 +338,13 @@ pub fn init(input: *Reader, output: *Writer, options: Options) InitError!Client |
| 338 | var cleartext_fragment_end: usize = 0; | 338 | var cleartext_fragment_end: usize = 0; |
| 339 | var cleartext_bufs: [2][tls.max_ciphertext_inner_record_len]u8 = undefined; | 339 | var cleartext_bufs: [2][tls.max_ciphertext_inner_record_len]u8 = undefined; |
| 340 | fragment: while (true) { | 340 | fragment: while (true) { |
| 341 | // Ensure the input buffer pointer is stable in this scope. | 341 | const record_header = (input.takeArray(tls.record_header_len) catch |err| switch (err) { |
| 342 | input.rebase(tls.max_ciphertext_record_len) catch |err| switch (err) { | | |
| 343 | error.EndOfStream => {}, // We have assurance the remainder of stream can be buffered. | | |
| 344 | error.ReadFailed => |e| return e, | | |
| 345 | }; | | |
| 346 | const record_header = input.peek(tls.record_header_len) catch |err| switch (err) { | | |
| 347 | error.EndOfStream => return error.TlsConnectionTruncated, | 342 | error.EndOfStream => return error.TlsConnectionTruncated, |
| 348 | error.ReadFailed => |e| return e, | 343 | error.ReadFailed => |e| return e, |
| 349 | }; | 344 | }).*; |
| 350 | const record_ct = input.takeEnumNonexhaustive(tls.ContentType, .big) catch unreachable; // already peeked | 345 | const record_ct: tls.ContentType = @fromBackingInt(record_header[0]); |
| 351 | input.toss(2); // legacy_version | 346 | // record_header[1..3] is legacy_version |
| 352 | const record_len = input.takeInt(u16, .big) catch unreachable; // already peeked | 347 | const record_len = mem.readInt(u16, record_header[3..5], .big); |
| 353 | if (record_len > tls.max_ciphertext_len) return error.TlsRecordOverflow; | 348 | if (record_len > tls.max_ciphertext_len) return error.TlsRecordOverflow; |
| 354 | const record_buffer = input.take(record_len) catch |err| switch (err) { | 349 | const record_buffer = input.take(record_len) catch |err| switch (err) { |
| 355 | error.EndOfStream => return error.TlsConnectionTruncated, | 350 | error.EndOfStream => return error.TlsConnectionTruncated, |
| ... | @@ -379,7 +374,7 @@ pub fn init(input: *Reader, output: *Writer, options: Options) InitError!Client | ... | @@ -379,7 +374,7 @@ pub fn init(input: *Reader, output: *Writer, options: Options) InitError!Client |
| 379 | const operand: V = pad ++ @as([8]u8, @bitCast(@byteSwap(read_seq))); | 374 | const operand: V = pad ++ @as([8]u8, @bitCast(@byteSwap(read_seq))); |
| 380 | break :nonce @as(V, pv.server_handshake_iv) ^ operand; | 375 | break :nonce @as(V, pv.server_handshake_iv) ^ operand; |
| 381 | }; | 376 | }; |
| 382 | P.AEAD.decrypt(cleartext, ciphertext, auth_tag, record_header, nonce, pv.server_handshake_key) catch | 377 | P.AEAD.decrypt(cleartext, ciphertext, auth_tag, &record_header, nonce, pv.server_handshake_key) catch |
| 383 | return error.TlsBadRecordMac; | 378 | return error.TlsBadRecordMac; |
| 384 | // TODO use scalar, non-slice version | 379 | // TODO use scalar, non-slice version |
| 385 | const trimmed_len = mem.trimEnd(u8, cleartext, "\x00").len; | 380 | const trimmed_len = mem.trimEnd(u8, cleartext, "\x00").len; |