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 {
592592 const end_hdr = i + 5;
593593 if (end_hdr > handshake_buf.len) return error.TlsRecordOverflow;
594594 if (end_hdr > len) {
595 std.debug.print("read len={d} atleast={d}\n", .{ len, end_hdr - len });
596595 len += try stream.readAtLeast(handshake_buf[len..], end_hdr - len);
597 std.debug.print("new len: {d} bytes\n", .{len});
598596 if (end_hdr > len) return error.EndOfStream;
599597 }
600598 const ct = @intToEnum(ContentType, handshake_buf[i]);
......@@ -605,12 +603,9 @@ pub fn init(stream: net.Stream, host: []const u8) !Tls {
605603 const record_size = mem.readIntBig(u16, handshake_buf[i..][0..2]);
606604 i += 2;
607605 const end = i + record_size;
608 std.debug.print("ct={any} record_size={d} end={d}\n", .{ ct, record_size, end });
609606 if (end > handshake_buf.len) return error.TlsRecordOverflow;
610607 if (end > len) {
611 std.debug.print("read len={d} atleast={d}\n", .{ len, end - len });
612608 len += try stream.readAtLeast(handshake_buf[len..], end - len);
613 std.debug.print("new len: {d} bytes\n", .{len});
614609 if (end > len) return error.EndOfStream;
615610 }
616611 switch (ct) {
......@@ -665,11 +660,25 @@ pub fn init(stream: net.Stream, host: []const u8) !Tls {
665660 return error.TlsBadLength;
666661 switch (handshake_type) {
667662 @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]);
669664 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 }
673682 },
674683 @enumToInt(HandshakeType.certificate) => {
675684 std.debug.print("cool certificate bro\n", .{});
......@@ -887,19 +896,18 @@ pub fn read(tls: *Tls, stream: net.Stream, buffer: []u8) !usize {
887896 // Capacity of output buffer, in records, rounded up.
888897 const buf_cap = (buffer.len +| (max_ciphertext_len - 1)) / max_ciphertext_len;
889898 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);
891901 const frag = in_buf[0 .. prev_len + actual_read_len];
892902 if (frag.len == 0) {
893903 tls.eof = true;
894904 return 0;
895905 }
896 std.debug.print("actual_read_len={d} frag.len={d}\n", .{ actual_read_len, frag.len });
897906 var in: usize = 0;
898907 var out: usize = 0;
899908
900909 while (true) {
901910 if (in + ciphertext_record_header_len > frag.len) {
902 std.debug.print("in={d} frag.len={d}\n", .{ in, frag.len });
903911 return finishRead(tls, frag, in, out);
904912 }
905913 const ct = @intToEnum(ContentType, frag[in]);
......@@ -912,7 +920,6 @@ pub fn read(tls: *Tls, stream: net.Stream, buffer: []u8) !usize {
912920 const end = in + record_size;
913921 if (end > frag.len) {
914922 if (record_size > max_ciphertext_len) return error.TlsRecordOverflow;
915 std.debug.print("end={d} frag.len={d}\n", .{ end, frag.len });
916923 return finishRead(tls, frag, in, out);
917924 }
918925 switch (ct) {
......@@ -980,6 +987,7 @@ pub fn read(tls: *Tls, stream: net.Stream, buffer: []u8) !usize {
980987 }
981988 },
982989 else => {
990 std.debug.print("unexpected ct: {any}\n", .{ct});
983991 return error.TlsUnexpectedMessage;
984992 },
985993 }