| ... | ... | @@ -592,9 +592,7 @@ pub fn init(stream: net.Stream, host: []const u8) !Tls { |
| 592 | 592 | const end_hdr = i + 5; |
| 593 | 593 | if (end_hdr > handshake_buf.len) return error.TlsRecordOverflow; |
| 594 | 594 | if (end_hdr > len) { |
| 595 | | std.debug.print("read len={d} atleast={d}\n", .{ len, end_hdr - len }); |
| 596 | 595 | len += try stream.readAtLeast(handshake_buf[len..], end_hdr - len); |
| 597 | | std.debug.print("new len: {d} bytes\n", .{len}); |
| 598 | 596 | if (end_hdr > len) return error.EndOfStream; |
| 599 | 597 | } |
| 600 | 598 | const ct = @intToEnum(ContentType, handshake_buf[i]); |
| ... | ... | @@ -605,12 +603,9 @@ pub fn init(stream: net.Stream, host: []const u8) !Tls { |
| 605 | 603 | const record_size = mem.readIntBig(u16, handshake_buf[i..][0..2]); |
| 606 | 604 | i += 2; |
| 607 | 605 | const end = i + record_size; |
| 608 | | std.debug.print("ct={any} record_size={d} end={d}\n", .{ ct, record_size, end }); |
| 609 | 606 | if (end > handshake_buf.len) return error.TlsRecordOverflow; |
| 610 | 607 | if (end > len) { |
| 611 | | std.debug.print("read len={d} atleast={d}\n", .{ len, end - len }); |
| 612 | 608 | len += try stream.readAtLeast(handshake_buf[len..], end - len); |
| 613 | | std.debug.print("new len: {d} bytes\n", .{len}); |
| 614 | 609 | if (end > len) return error.EndOfStream; |
| 615 | 610 | } |
| 616 | 611 | switch (ct) { |
| ... | ... | @@ -665,11 +660,25 @@ pub fn init(stream: net.Stream, host: []const u8) !Tls { |
| 665 | 660 | return error.TlsBadLength; |
| 666 | 661 | switch (handshake_type) { |
| 667 | 662 | @enumToInt(HandshakeType.encrypted_extensions) => { |
| 668 | | const ext_size = mem.readIntBig(u16, cleartext[ct_i..][0..2]); |
| 663 | const total_ext_size = mem.readIntBig(u16, cleartext[ct_i..][0..2]); |
| 669 | 664 | ct_i += 2; |
| 670 | | std.debug.print("{d} bytes of encrypted extensions\n", .{ |
| 671 | | ext_size, |
| 672 | | }); |
| 665 | const end_ext_i = ct_i + total_ext_size; |
| 666 | while (ct_i < end_ext_i) { |
| 667 | const et = mem.readIntBig(u16, cleartext[ct_i..][0..2]); |
| 668 | ct_i += 2; |
| 669 | const ext_size = mem.readIntBig(u16, cleartext[ct_i..][0..2]); |
| 670 | ct_i += 2; |
| 671 | const next_ext_i = ct_i + ext_size; |
| 672 | switch (et) { |
| 673 | @enumToInt(ExtensionType.server_name) => {}, |
| 674 | else => { |
| 675 | std.debug.print("encrypted extension: {any}\n", .{ |
| 676 | et, |
| 677 | }); |
| 678 | }, |
| 679 | } |
| 680 | ct_i = next_ext_i; |
| 681 | } |
| 673 | 682 | }, |
| 674 | 683 | @enumToInt(HandshakeType.certificate) => { |
| 675 | 684 | std.debug.print("cool certificate bro\n", .{}); |
| ... | ... | @@ -887,19 +896,18 @@ pub fn read(tls: *Tls, stream: net.Stream, buffer: []u8) !usize { |
| 887 | 896 | // Capacity of output buffer, in records, rounded up. |
| 888 | 897 | const buf_cap = (buffer.len +| (max_ciphertext_len - 1)) / max_ciphertext_len; |
| 889 | 898 | const wanted_read_len = buf_cap * (max_ciphertext_len + ciphertext_record_header_len); |
| 890 | | const actual_read_len = try stream.read(in_buf[prev_len..@min(wanted_read_len, in_buf.len)]); |
| 899 | const ask_slice = in_buf[prev_len..@min(wanted_read_len, in_buf.len)]; |
| 900 | const actual_read_len = try stream.read(ask_slice); |
| 891 | 901 | const frag = in_buf[0 .. prev_len + actual_read_len]; |
| 892 | 902 | if (frag.len == 0) { |
| 893 | 903 | tls.eof = true; |
| 894 | 904 | return 0; |
| 895 | 905 | } |
| 896 | | std.debug.print("actual_read_len={d} frag.len={d}\n", .{ actual_read_len, frag.len }); |
| 897 | 906 | var in: usize = 0; |
| 898 | 907 | var out: usize = 0; |
| 899 | 908 | |
| 900 | 909 | while (true) { |
| 901 | 910 | if (in + ciphertext_record_header_len > frag.len) { |
| 902 | | std.debug.print("in={d} frag.len={d}\n", .{ in, frag.len }); |
| 903 | 911 | return finishRead(tls, frag, in, out); |
| 904 | 912 | } |
| 905 | 913 | const ct = @intToEnum(ContentType, frag[in]); |
| ... | ... | @@ -912,7 +920,6 @@ pub fn read(tls: *Tls, stream: net.Stream, buffer: []u8) !usize { |
| 912 | 920 | const end = in + record_size; |
| 913 | 921 | if (end > frag.len) { |
| 914 | 922 | if (record_size > max_ciphertext_len) return error.TlsRecordOverflow; |
| 915 | | std.debug.print("end={d} frag.len={d}\n", .{ end, frag.len }); |
| 916 | 923 | return finishRead(tls, frag, in, out); |
| 917 | 924 | } |
| 918 | 925 | switch (ct) { |
| ... | ... | @@ -980,6 +987,7 @@ pub fn read(tls: *Tls, stream: net.Stream, buffer: []u8) !usize { |
| 980 | 987 | } |
| 981 | 988 | }, |
| 982 | 989 | else => { |
| 990 | std.debug.print("unexpected ct: {any}\n", .{ct}); |
| 983 | 991 | return error.TlsUnexpectedMessage; |
| 984 | 992 | }, |
| 985 | 993 | } |