| ... | @@ -31,16 +31,22 @@ eof: bool, | ... | @@ -31,16 +31,22 @@ eof: bool, |
| 31 | pub fn init(stream: net.Stream, host: []const u8) !Client { | 31 | pub fn init(stream: net.Stream, host: []const u8) !Client { |
| 32 | const host_len = @intCast(u16, host.len); | 32 | const host_len = @intCast(u16, host.len); |
| 33 | | 33 | |
| 34 | const kp = crypto.dh.X25519.KeyPair.create(null) catch |err| switch (err) { | 34 | var random_buffer: [128]u8 = undefined; |
| 35 | // Only possible to happen if the private key is all zeroes. | | |
| 36 | error.IdentityElement => return error.InsufficientEntropy, | | |
| 37 | }; | | |
| 38 | | | |
| 39 | // This is used both for the random bytes and for the legacy session id. | | |
| 40 | var random_buffer: [64]u8 = undefined; | | |
| 41 | crypto.random.bytes(&random_buffer); | 35 | crypto.random.bytes(&random_buffer); |
| 42 | const hello_rand = random_buffer[0..32].*; | 36 | const hello_rand = random_buffer[0..32].*; |
| 43 | const legacy_session_id = random_buffer[32..64].*; | 37 | const legacy_session_id = random_buffer[32..64].*; |
| | 38 | const x25519_kp_seed = random_buffer[64..96].*; |
| | 39 | const secp256r1_kp_seed = random_buffer[96..128].*; |
| | 40 | |
| | 41 | const x25519_kp = crypto.dh.X25519.KeyPair.create(x25519_kp_seed) catch |err| switch (err) { |
| | 42 | // Only possible to happen if the private key is all zeroes. |
| | 43 | error.IdentityElement => return error.InsufficientEntropy, |
| | 44 | }; |
| | 45 | const secp256r1_kp = crypto.sign.ecdsa.EcdsaP256Sha256.KeyPair.create(secp256r1_kp_seed) catch |err| switch (err) { |
| | 46 | // Only possible to happen if the private key is all zeroes. |
| | 47 | error.IdentityElement => return error.InsufficientEntropy, |
| | 48 | }; |
| | 49 | _ = secp256r1_kp; |
| 44 | | 50 | |
| 45 | const extensions_payload = | 51 | const extensions_payload = |
| 46 | tls.extension(.supported_versions, [_]u8{ | 52 | tls.extension(.supported_versions, [_]u8{ |
| ... | @@ -66,14 +72,10 @@ pub fn init(stream: net.Stream, host: []const u8) !Client { | ... | @@ -66,14 +72,10 @@ pub fn init(stream: net.Stream, host: []const u8) !Client { |
| 66 | })) ++ tls.extension(.supported_groups, enum_array(tls.NamedGroup, &.{ | 72 | })) ++ tls.extension(.supported_groups, enum_array(tls.NamedGroup, &.{ |
| 67 | //.secp256r1, | 73 | //.secp256r1, |
| 68 | .x25519, | 74 | .x25519, |
| 69 | })) ++ [_]u8{ | 75 | })) ++ tls.extension( |
| 70 | // Extension: key_share | 76 | .key_share, |
| 71 | 0, 51, // ExtensionType.key_share | 77 | array(1, int2(@enumToInt(tls.NamedGroup.x25519)) ++ array(1, x25519_kp.public_key)), |
| 72 | 0, 38, // byte length of this extension payload | 78 | ) ++ |
| 73 | 0, 36, // byte length of client_shares | | |
| 74 | 0x00, 0x1D, // NamedGroup.x25519 | | |
| 75 | 0, 32, // byte length of key_exchange | | |
| 76 | } ++ kp.public_key ++ | | |
| 77 | int2(@enumToInt(tls.ExtensionType.server_name)) ++ | 79 | int2(@enumToInt(tls.ExtensionType.server_name)) ++ |
| 78 | int2(host_len + 5) ++ // byte length of this extension payload | 80 | int2(host_len + 5) ++ // byte length of this extension payload |
| 79 | int2(host_len + 3) ++ // server_name_list byte count | 81 | int2(host_len + 3) ++ // server_name_list byte count |
| ... | @@ -230,7 +232,7 @@ pub fn init(stream: net.Stream, host: []const u8) !Client { | ... | @@ -230,7 +232,7 @@ pub fn init(stream: net.Stream, host: []const u8) !Client { |
| 230 | } | 232 | } |
| 231 | | 233 | |
| 232 | const shared_key = crypto.dh.X25519.scalarmult( | 234 | const shared_key = crypto.dh.X25519.scalarmult( |
| 233 | kp.secret_key, | 235 | x25519_kp.secret_key, |
| 234 | x25519_server_pub_key.*, | 236 | x25519_server_pub_key.*, |
| 235 | ) catch return error.TlsDecryptFailure; | 237 | ) catch return error.TlsDecryptFailure; |
| 236 | | 238 | |