| ... | ... | @@ -9,7 +9,6 @@ const assert = std.debug.assert; |
| 9 | 9 | const ApplicationCipher = tls.ApplicationCipher; |
| 10 | 10 | const CipherSuite = tls.CipherSuite; |
| 11 | 11 | const ContentType = tls.ContentType; |
| 12 | | const HandshakeType = tls.HandshakeType; |
| 13 | 12 | const HandshakeCipher = tls.HandshakeCipher; |
| 14 | 13 | const max_ciphertext_len = tls.max_ciphertext_len; |
| 15 | 14 | const hkdfExpandLabel = tls.hkdfExpandLabel; |
| ... | ... | @@ -91,7 +90,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 91 | 90 | extensions_header; |
| 92 | 91 | |
| 93 | 92 | const out_handshake = |
| 94 | | [_]u8{@enumToInt(HandshakeType.client_hello)} ++ |
| 93 | [_]u8{@enumToInt(tls.HandshakeType.client_hello)} ++ |
| 95 | 94 | int3(@intCast(u24, client_hello.len + host_len)) ++ |
| 96 | 95 | client_hello; |
| 97 | 96 | |
| ... | ... | @@ -142,7 +141,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 142 | 141 | return error.TlsAlert; |
| 143 | 142 | }, |
| 144 | 143 | .handshake => { |
| 145 | | if (frag[0] != @enumToInt(HandshakeType.server_hello)) { |
| 144 | if (frag[0] != @enumToInt(tls.HandshakeType.server_hello)) { |
| 146 | 145 | return error.TlsUnexpectedMessage; |
| 147 | 146 | } |
| 148 | 147 | 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) |
| 175 | 174 | var shared_key: [32]u8 = undefined; |
| 176 | 175 | var have_shared_key = false; |
| 177 | 176 | 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])); |
| 179 | 178 | i += 2; |
| 180 | 179 | const ext_size = mem.readIntBig(u16, frag[i..][0..2]); |
| 181 | 180 | i += 2; |
| 182 | 181 | const next_i = i + ext_size; |
| 183 | 182 | if (next_i > frag.len) return error.TlsBadLength; |
| 184 | 183 | switch (et) { |
| 185 | | @enumToInt(tls.ExtensionType.supported_versions) => { |
| 184 | .supported_versions => { |
| 186 | 185 | if (supported_version != 0) return error.TlsIllegalParameter; |
| 187 | 186 | supported_version = mem.readIntBig(u16, frag[i..][0..2]); |
| 188 | 187 | }, |
| 189 | | @enumToInt(tls.ExtensionType.key_share) => { |
| 188 | .key_share => { |
| 190 | 189 | if (have_shared_key) return error.TlsIllegalParameter; |
| 191 | 190 | 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])); |
| 193 | 192 | i += 2; |
| 194 | 193 | const key_size = mem.readIntBig(u16, frag[i..][0..2]); |
| 195 | 194 | i += 2; |
| 196 | 195 | |
| 197 | 196 | switch (named_group) { |
| 198 | | @enumToInt(tls.NamedGroup.x25519) => { |
| 197 | .x25519 => { |
| 199 | 198 | if (key_size != 32) return error.TlsBadLength; |
| 200 | 199 | const server_pub_key = frag[i..][0..32]; |
| 201 | 200 | |
| ... | ... | @@ -204,7 +203,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 204 | 203 | server_pub_key.*, |
| 205 | 204 | ) catch return error.TlsDecryptFailure; |
| 206 | 205 | }, |
| 207 | | @enumToInt(tls.NamedGroup.secp256r1) => { |
| 206 | .secp256r1 => { |
| 208 | 207 | const server_pub_key = frag[i..][0..key_size]; |
| 209 | 208 | |
| 210 | 209 | const PublicKey = crypto.sign.ecdsa.EcdsaP256Sha256.PublicKey; |
| ... | ... | @@ -217,7 +216,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 217 | 216 | shared_key = mul.affineCoordinates().x.toBytes(.Big); |
| 218 | 217 | }, |
| 219 | 218 | else => { |
| 220 | | std.debug.print("named group: {x}\n", .{named_group}); |
| 219 | //std.debug.print("named group: {x}\n", .{named_group}); |
| 221 | 220 | return error.TlsIllegalParameter; |
| 222 | 221 | }, |
| 223 | 222 | } |
| ... | ... | @@ -380,7 +379,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 380 | 379 | .handshake => { |
| 381 | 380 | var ct_i: usize = 0; |
| 382 | 381 | while (true) { |
| 383 | | const handshake_type = cleartext[ct_i]; |
| 382 | const handshake_type = @intToEnum(tls.HandshakeType, cleartext[ct_i]); |
| 384 | 383 | ct_i += 1; |
| 385 | 384 | const handshake_len = mem.readIntBig(u24, cleartext[ct_i..][0..3]); |
| 386 | 385 | ct_i += 3; |
| ... | ... | @@ -390,7 +389,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 390 | 389 | const wrapped_handshake = cleartext[ct_i - 4 .. next_handshake_i]; |
| 391 | 390 | const handshake = cleartext[ct_i..next_handshake_i]; |
| 392 | 391 | switch (handshake_type) { |
| 393 | | @enumToInt(HandshakeType.encrypted_extensions) => { |
| 392 | .encrypted_extensions => { |
| 394 | 393 | if (handshake_state != .encrypted_extensions) return error.TlsUnexpectedMessage; |
| 395 | 394 | handshake_state = .certificate; |
| 396 | 395 | switch (handshake_cipher) { |
| ... | ... | @@ -400,13 +399,13 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 400 | 399 | var hs_i: usize = 2; |
| 401 | 400 | const end_ext_i = 2 + total_ext_size; |
| 402 | 401 | 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])); |
| 404 | 403 | hs_i += 2; |
| 405 | 404 | const ext_size = mem.readIntBig(u16, handshake[hs_i..][0..2]); |
| 406 | 405 | hs_i += 2; |
| 407 | 406 | const next_ext_i = hs_i + ext_size; |
| 408 | 407 | switch (et) { |
| 409 | | @enumToInt(tls.ExtensionType.server_name) => {}, |
| 408 | .server_name => {}, |
| 410 | 409 | else => { |
| 411 | 410 | std.debug.print("encrypted extension: {any}\n", .{ |
| 412 | 411 | et, |
| ... | ... | @@ -416,7 +415,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 416 | 415 | hs_i = next_ext_i; |
| 417 | 416 | } |
| 418 | 417 | }, |
| 419 | | @enumToInt(HandshakeType.certificate) => cert: { |
| 418 | .certificate => cert: { |
| 420 | 419 | switch (handshake_cipher) { |
| 421 | 420 | inline else => |*p| p.transcript_hash.update(wrapped_handshake), |
| 422 | 421 | } |
| ... | ... | @@ -488,7 +487,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 488 | 487 | hs_i += total_ext_size; |
| 489 | 488 | } |
| 490 | 489 | }, |
| 491 | | @enumToInt(HandshakeType.certificate_verify) => { |
| 490 | .certificate_verify => { |
| 492 | 491 | switch (handshake_state) { |
| 493 | 492 | .trust_chain_established => handshake_state = .finished, |
| 494 | 493 | .certificate => return error.TlsCertificateNotVerified, |
| ... | ... | @@ -535,7 +534,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 535 | 534 | }, |
| 536 | 535 | } |
| 537 | 536 | }, |
| 538 | | @enumToInt(HandshakeType.finished) => { |
| 537 | .finished => { |
| 539 | 538 | if (handshake_state != .finished) return error.TlsUnexpectedMessage; |
| 540 | 539 | // This message is to trick buggy proxies into behaving correctly. |
| 541 | 540 | const client_change_cipher_spec_msg = [_]u8{ |
| ... | ... | @@ -555,7 +554,7 @@ pub fn init(stream: net.Stream, ca_bundle: Certificate.Bundle, host: []const u8) |
| 555 | 554 | const handshake_hash = p.transcript_hash.finalResult(); |
| 556 | 555 | const verify_data = tls.hmac(P.Hmac, &handshake_hash, p.client_finished_key); |
| 557 | 556 | const out_cleartext = [_]u8{ |
| 558 | | @enumToInt(HandshakeType.finished), |
| 557 | @enumToInt(tls.HandshakeType.finished), |
| 559 | 558 | 0, 0, verify_data.len, // length |
| 560 | 559 | } ++ verify_data ++ [1]u8{@enumToInt(ContentType.handshake)}; |
| 561 | 560 | |
| ... | ... | @@ -810,7 +809,7 @@ pub fn read(c: *Client, stream: net.Stream, buffer: []u8) !usize { |
| 810 | 809 | .handshake => { |
| 811 | 810 | var ct_i: usize = 0; |
| 812 | 811 | while (true) { |
| 813 | | const handshake_type = cleartext[ct_i]; |
| 812 | const handshake_type = @intToEnum(tls.HandshakeType, cleartext[ct_i]); |
| 814 | 813 | ct_i += 1; |
| 815 | 814 | const handshake_len = mem.readIntBig(u24, cleartext[ct_i..][0..3]); |
| 816 | 815 | ct_i += 3; |
| ... | ... | @@ -819,10 +818,10 @@ pub fn read(c: *Client, stream: net.Stream, buffer: []u8) !usize { |
| 819 | 818 | return error.TlsBadLength; |
| 820 | 819 | const handshake = cleartext[ct_i..next_handshake_i]; |
| 821 | 820 | switch (handshake_type) { |
| 822 | | @enumToInt(HandshakeType.new_session_ticket) => { |
| 821 | .new_session_ticket => { |
| 823 | 822 | std.debug.print("server sent a new session ticket\n", .{}); |
| 824 | 823 | }, |
| 825 | | @enumToInt(HandshakeType.key_update) => { |
| 824 | .key_update => { |
| 826 | 825 | switch (c.application_cipher) { |
| 827 | 826 | inline else => |*p| { |
| 828 | 827 | const P = @TypeOf(p.*); |