authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-28 19:54:17-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-02 16:57:16-07:00
log1d20ada3665102ad09fec0ff486b22f4f0a56141
treeaf97be70775fed5ecc7d51d0635eb92d186d29ed
parent16af6286c8055a870128aa1f7c785273b23cad55

std.crypto.tls.Client: refactor to reduce namespace bloat


1 files changed, 18 insertions(+), 22 deletions(-)

lib/std/crypto/tls/Client.zig+18-22
......@@ -5,18 +5,14 @@ const net = std.net;
55const mem = std.mem;
66const crypto = std.crypto;
77const assert = std.debug.assert;
8const Certificate = std.crypto.Certificate;
89
9const ApplicationCipher = tls.ApplicationCipher;
10const CipherSuite = tls.CipherSuite;
11const ContentType = tls.ContentType;
12const HandshakeCipher = tls.HandshakeCipher;
1310const max_ciphertext_len = tls.max_ciphertext_len;
1411const hkdfExpandLabel = tls.hkdfExpandLabel;
1512const int2 = tls.int2;
1613const int3 = tls.int3;
1714const array = tls.array;
1815const enum_array = tls.enum_array;
19const Certificate = crypto.Certificate;
2016
2117read_seq: u64,
2218write_seq: u64,
......@@ -27,7 +23,7 @@ partially_read_len: u15,
2723/// re-decrypt bytes from `partially_read_buffer` when the buffer supplied by
2824/// the read() API user is not large enough.
2925partial_cleartext_index: u15,
30application_cipher: ApplicationCipher,
26application_cipher: tls.ApplicationCipher,
3127eof: bool,
3228/// The size is enough to contain exactly one TLSCiphertext record.
3329/// Contains encrypted bytes.
......@@ -101,7 +97,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
10197 client_hello;
10298
10399 const plaintext_header = [_]u8{
104 @enumToInt(ContentType.handshake),
100 @enumToInt(tls.ContentType.handshake),
105101 0x03, 0x01, // legacy_record_version
106102 } ++ int2(@intCast(u16, out_handshake.len + host_len)) ++ out_handshake;
107103
......@@ -121,7 +117,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
121117
122118 const client_hello_bytes1 = plaintext_header[5..];
123119
124 var handshake_cipher: HandshakeCipher = undefined;
120 var handshake_cipher: tls.HandshakeCipher = undefined;
125121
126122 var handshake_buf: [8000]u8 = undefined;
127123 var len: usize = 0;
......@@ -129,7 +125,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
129125 const plaintext = handshake_buf[0..5];
130126 len = try stream.readAtLeast(&handshake_buf, plaintext.len);
131127 if (len < plaintext.len) return error.EndOfStream;
132 const ct = @intToEnum(ContentType, plaintext[0]);
128 const ct = @intToEnum(tls.ContentType, plaintext[0]);
133129 const frag_len = mem.readIntBig(u16, plaintext[3..][0..2]);
134130 const end = plaintext.len + frag_len;
135131 if (end > handshake_buf.len) return error.TlsRecordOverflow;
......@@ -169,7 +165,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
169165 i += 32;
170166 const cipher_suite_int = mem.readIntBig(u16, frag[i..][0..2]);
171167 i += 2;
172 const cipher_suite_tag = @intToEnum(CipherSuite, cipher_suite_int);
168 const cipher_suite_tag = @intToEnum(tls.CipherSuite, cipher_suite_int);
173169 const legacy_compression_method = frag[i];
174170 i += 1;
175171 _ = legacy_compression_method;
......@@ -247,8 +243,8 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
247243 .AEGIS_256_SHA384,
248244 .AEGIS_128L_SHA256,
249245 => |tag| {
250 const P = std.meta.TagPayloadByName(HandshakeCipher, @tagName(tag));
251 handshake_cipher = @unionInit(HandshakeCipher, @tagName(tag), .{
246 const P = std.meta.TagPayloadByName(tls.HandshakeCipher, @tagName(tag));
247 handshake_cipher = @unionInit(tls.HandshakeCipher, @tagName(tag), .{
252248 .handshake_secret = undefined,
253249 .master_secret = undefined,
254250 .client_handshake_key = undefined,
......@@ -338,7 +334,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
338334 len += try stream.readAtLeast(handshake_buf[len..], end_hdr - len);
339335 if (end_hdr > len) return error.EndOfStream;
340336 }
341 const ct = @intToEnum(ContentType, handshake_buf[i]);
337 const ct = @intToEnum(tls.ContentType, handshake_buf[i]);
342338 i += 1;
343339 const legacy_version = mem.readIntBig(u16, handshake_buf[i..][0..2]);
344340 i += 2;
......@@ -380,7 +376,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
380376 },
381377 };
382378
383 const inner_ct = @intToEnum(ContentType, cleartext[cleartext.len - 1]);
379 const inner_ct = @intToEnum(tls.ContentType, cleartext[cleartext.len - 1]);
384380 switch (inner_ct) {
385381 .handshake => {
386382 var ct_i: usize = 0;
......@@ -546,7 +542,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
546542 if (handshake_state != .finished) return error.TlsUnexpectedMessage;
547543 // This message is to trick buggy proxies into behaving correctly.
548544 const client_change_cipher_spec_msg = [_]u8{
549 @enumToInt(ContentType.change_cipher_spec),
545 @enumToInt(tls.ContentType.change_cipher_spec),
550546 0x03, 0x03, // legacy protocol version
551547 0x00, 0x01, // length
552548 0x01,
......@@ -564,12 +560,12 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
564560 const out_cleartext = [_]u8{
565561 @enumToInt(tls.HandshakeType.finished),
566562 0, 0, verify_data.len, // length
567 } ++ verify_data ++ [1]u8{@enumToInt(ContentType.handshake)};
563 } ++ verify_data ++ [1]u8{@enumToInt(tls.ContentType.handshake)};
568564
569565 const wrapped_len = out_cleartext.len + P.AEAD.tag_length;
570566
571567 var finished_msg = [_]u8{
572 @enumToInt(ContentType.application_data),
568 @enumToInt(tls.ContentType.application_data),
573569 0x03, 0x03, // legacy protocol version
574570 0, wrapped_len, // byte length of encrypted record
575571 } ++ @as([wrapped_len]u8, undefined);
......@@ -590,7 +586,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
590586 // std.fmt.fmtSliceHexLower(&client_secret),
591587 // std.fmt.fmtSliceHexLower(&server_secret),
592588 //});
593 break :c @unionInit(ApplicationCipher, @tagName(tag), .{
589 break :c @unionInit(tls.ApplicationCipher, @tagName(tag), .{
594590 .client_secret = client_secret,
595591 .server_secret = server_secret,
596592 .client_key = hkdfExpandLabel(P.Hkdf, client_secret, "key", "", P.AEAD.key_length),
......@@ -661,7 +657,7 @@ pub fn write(c: *Client, stream: net.Stream, bytes: []const u8) !usize {
661657 if (encrypted_content_len == 0) break :l overhead_len;
662658
663659 mem.copy(u8, &cleartext_buf, bytes[bytes_i..][0..encrypted_content_len]);
664 cleartext_buf[encrypted_content_len] = @enumToInt(ContentType.application_data);
660 cleartext_buf[encrypted_content_len] = @enumToInt(tls.ContentType.application_data);
665661 bytes_i += encrypted_content_len;
666662 const ciphertext_len = encrypted_content_len + 1;
667663 const cleartext = cleartext_buf[0..ciphertext_len];
......@@ -669,7 +665,7 @@ pub fn write(c: *Client, stream: net.Stream, bytes: []const u8) !usize {
669665 const record_start = ciphertext_end;
670666 const ad = ciphertext_buf[ciphertext_end..][0..5];
671667 ad.* =
672 [_]u8{@enumToInt(ContentType.application_data)} ++
668 [_]u8{@enumToInt(tls.ContentType.application_data)} ++
673669 int2(@enumToInt(tls.ProtocolVersion.tls_1_2)) ++
674670 int2(ciphertext_len + P.AEAD.tag_length);
675671 ciphertext_end += ad.len;
......@@ -818,7 +814,7 @@ pub fn readAdvanced(c: *Client, stream: net.Stream, buffer: []u8) !usize {
818814 return finishRead(c, frag, in, out);
819815 }
820816 const record_start = in;
821 const ct = @intToEnum(ContentType, frag[in]);
817 const ct = @intToEnum(tls.ContentType, frag[in]);
822818 in += 1;
823819 const legacy_version = mem.readIntBig(u16, frag[in..][0..2]);
824820 in += 2;
......@@ -861,7 +857,7 @@ pub fn readAdvanced(c: *Client, stream: net.Stream, buffer: []u8) !usize {
861857 },
862858 };
863859
864 const inner_ct = @intToEnum(ContentType, cleartext[cleartext.len - 1]);
860 const inner_ct = @intToEnum(tls.ContentType, cleartext[cleartext.len - 1]);
865861 switch (inner_ct) {
866862 .alert => {
867863 c.read_seq += 1;