authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-16 13:57:56-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-02 16:57:15-07:00
log02c33d02e05f3dd067bc5492d2617b7805ef897d
treeb1353fd682d2ced9b2ea8172d7b68c235f3007dc
parent462b3ed69c20ea5dcae1660761012b3d5fa91367

std.crypto.Tls: parse encrypted extensions


1 files changed, 21 insertions(+), 13 deletions(-)

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