authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-27 23:49:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-02 16:57:16-07:00
log21ab99174eabc9ae8efa2b19890d9cab51773b35
treedfd789f72b3552a9a83e1f660a74b418141b12a9
parent477864dca560b03bb3c3e8e7e1e50362e7ed681f

std.crypto.tls.Client: use enums more


2 files changed, 23 insertions(+), 21 deletions(-)

lib/std/crypto/tls.zig+3
......@@ -74,6 +74,7 @@ pub const HandshakeType = enum(u8) {
7474 finished = 20,
7575 key_update = 24,
7676 message_hash = 254,
77 _,
7778};
7879
7980pub const ExtensionType = enum(u16) {
......@@ -121,6 +122,8 @@ pub const ExtensionType = enum(u16) {
121122 signature_algorithms_cert = 50,
122123 /// RFC 8446
123124 key_share = 51,
125
126 _,
124127};
125128
126129pub const AlertLevel = enum(u8) {
lib/std/crypto/tls/Client.zig+20-21
......@@ -9,7 +9,6 @@ const assert = std.debug.assert;
99const ApplicationCipher = tls.ApplicationCipher;
1010const CipherSuite = tls.CipherSuite;
1111const ContentType = tls.ContentType;
12const HandshakeType = tls.HandshakeType;
1312const HandshakeCipher = tls.HandshakeCipher;
1413const max_ciphertext_len = tls.max_ciphertext_len;
1514const hkdfExpandLabel = tls.hkdfExpandLabel;
......@@ -91,7 +90,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
9190 extensions_header;
9291
9392 const out_handshake =
94 [_]u8{@enumToInt(HandshakeType.client_hello)} ++
93 [_]u8{@enumToInt(tls.HandshakeType.client_hello)} ++
9594 int3(@intCast(u24, client_hello.len + host_len)) ++
9695 client_hello;
9796
......@@ -142,7 +141,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
142141 return error.TlsAlert;
143142 },
144143 .handshake => {
145 if (frag[0] != @enumToInt(HandshakeType.server_hello)) {
144 if (frag[0] != @enumToInt(tls.HandshakeType.server_hello)) {
146145 return error.TlsUnexpectedMessage;
147146 }
148147 const length = mem.readIntBig(u24, frag[1..4]);
......@@ -175,27 +174,27 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
175174 var shared_key: [32]u8 = undefined;
176175 var have_shared_key = false;
177176 while (i < frag.len) {
178 const et = mem.readIntBig(u16, frag[i..][0..2]);
177 const et = @intToEnum(tls.ExtensionType, mem.readIntBig(u16, frag[i..][0..2]));
179178 i += 2;
180179 const ext_size = mem.readIntBig(u16, frag[i..][0..2]);
181180 i += 2;
182181 const next_i = i + ext_size;
183182 if (next_i > frag.len) return error.TlsBadLength;
184183 switch (et) {
185 @enumToInt(tls.ExtensionType.supported_versions) => {
184 .supported_versions => {
186185 if (supported_version != 0) return error.TlsIllegalParameter;
187186 supported_version = mem.readIntBig(u16, frag[i..][0..2]);
188187 },
189 @enumToInt(tls.ExtensionType.key_share) => {
188 .key_share => {
190189 if (have_shared_key) return error.TlsIllegalParameter;
191190 have_shared_key = true;
192 const named_group = mem.readIntBig(u16, frag[i..][0..2]);
191 const named_group = @intToEnum(tls.NamedGroup, mem.readIntBig(u16, frag[i..][0..2]));
193192 i += 2;
194193 const key_size = mem.readIntBig(u16, frag[i..][0..2]);
195194 i += 2;
196195
197196 switch (named_group) {
198 @enumToInt(tls.NamedGroup.x25519) => {
197 .x25519 => {
199198 if (key_size != 32) return error.TlsBadLength;
200199 const server_pub_key = frag[i..][0..32];
201200
......@@ -204,7 +203,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
204203 server_pub_key.*,
205204 ) catch return error.TlsDecryptFailure;
206205 },
207 @enumToInt(tls.NamedGroup.secp256r1) => {
206 .secp256r1 => {
208207 const server_pub_key = frag[i..][0..key_size];
209208
210209 const PublicKey = crypto.sign.ecdsa.EcdsaP256Sha256.PublicKey;
......@@ -217,7 +216,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
217216 shared_key = mul.affineCoordinates().x.toBytes(.Big);
218217 },
219218 else => {
220 std.debug.print("named group: {x}\n", .{named_group});
219 //std.debug.print("named group: {x}\n", .{named_group});
221220 return error.TlsIllegalParameter;
222221 },
223222 }
......@@ -380,7 +379,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
380379 .handshake => {
381380 var ct_i: usize = 0;
382381 while (true) {
383 const handshake_type = cleartext[ct_i];
382 const handshake_type = @intToEnum(tls.HandshakeType, cleartext[ct_i]);
384383 ct_i += 1;
385384 const handshake_len = mem.readIntBig(u24, cleartext[ct_i..][0..3]);
386385 ct_i += 3;
......@@ -390,7 +389,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
390389 const wrapped_handshake = cleartext[ct_i - 4 .. next_handshake_i];
391390 const handshake = cleartext[ct_i..next_handshake_i];
392391 switch (handshake_type) {
393 @enumToInt(HandshakeType.encrypted_extensions) => {
392 .encrypted_extensions => {
394393 if (handshake_state != .encrypted_extensions) return error.TlsUnexpectedMessage;
395394 handshake_state = .certificate;
396395 switch (handshake_cipher) {
......@@ -400,13 +399,13 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
400399 var hs_i: usize = 2;
401400 const end_ext_i = 2 + total_ext_size;
402401 while (hs_i < end_ext_i) {
403 const et = mem.readIntBig(u16, handshake[hs_i..][0..2]);
402 const et = @intToEnum(tls.ExtensionType, mem.readIntBig(u16, handshake[hs_i..][0..2]));
404403 hs_i += 2;
405404 const ext_size = mem.readIntBig(u16, handshake[hs_i..][0..2]);
406405 hs_i += 2;
407406 const next_ext_i = hs_i + ext_size;
408407 switch (et) {
409 @enumToInt(tls.ExtensionType.server_name) => {},
408 .server_name => {},
410409 else => {
411410 std.debug.print("encrypted extension: {any}\n", .{
412411 et,
......@@ -416,7 +415,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
416415 hs_i = next_ext_i;
417416 }
418417 },
419 @enumToInt(HandshakeType.certificate) => cert: {
418 .certificate => cert: {
420419 switch (handshake_cipher) {
421420 inline else => |*p| p.transcript_hash.update(wrapped_handshake),
422421 }
......@@ -488,7 +487,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
488487 hs_i += total_ext_size;
489488 }
490489 },
491 @enumToInt(HandshakeType.certificate_verify) => {
490 .certificate_verify => {
492491 switch (handshake_state) {
493492 .trust_chain_established => handshake_state = .finished,
494493 .certificate => return error.TlsCertificateNotVerified,
......@@ -535,7 +534,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
535534 },
536535 }
537536 },
538 @enumToInt(HandshakeType.finished) => {
537 .finished => {
539538 if (handshake_state != .finished) return error.TlsUnexpectedMessage;
540539 // This message is to trick buggy proxies into behaving correctly.
541540 const client_change_cipher_spec_msg = [_]u8{
......@@ -555,7 +554,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8)
555554 const handshake_hash = p.transcript_hash.finalResult();
556555 const verify_data = tls.hmac(P.Hmac, &handshake_hash, p.client_finished_key);
557556 const out_cleartext = [_]u8{
558 @enumToInt(HandshakeType.finished),
557 @enumToInt(tls.HandshakeType.finished),
559558 0, 0, verify_data.len, // length
560559 } ++ verify_data ++ [1]u8{@enumToInt(ContentType.handshake)};
561560
......@@ -810,7 +809,7 @@ pub fn read(c: *Client, stream: net.Stream, buffer: []u8) !usize {
810809 .handshake => {
811810 var ct_i: usize = 0;
812811 while (true) {
813 const handshake_type = cleartext[ct_i];
812 const handshake_type = @intToEnum(tls.HandshakeType, cleartext[ct_i]);
814813 ct_i += 1;
815814 const handshake_len = mem.readIntBig(u24, cleartext[ct_i..][0..3]);
816815 ct_i += 3;
......@@ -819,10 +818,10 @@ pub fn read(c: *Client, stream: net.Stream, buffer: []u8) !usize {
819818 return error.TlsBadLength;
820819 const handshake = cleartext[ct_i..next_handshake_i];
821820 switch (handshake_type) {
822 @enumToInt(HandshakeType.new_session_ticket) => {
821 .new_session_ticket => {
823822 std.debug.print("server sent a new session ticket\n", .{});
824823 },
825 @enumToInt(HandshakeType.key_update) => {
824 .key_update => {
826825 switch (c.application_cipher) {
827826 inline else => |*p| {
828827 const P = @TypeOf(p.*);