authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-18 18:27:53-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-02 16:57:15-07:00
logf460c2170504ce94471b16631c43638f50735241
tree0cacbd2fbdafacb08b4d0fb13bf5cbeac2f2c340
parent7a2377838414157fb65850aa045c2112a0bbd006

std.crypto.tls.Client: avoid hard-coded bytes in key_share


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

lib/std/crypto/tls/Client.zig+18-16
......@@ -31,16 +31,22 @@ eof: bool,
3131pub fn init(stream: net.Stream, host: []const u8) !Client {
3232 const host_len = @intCast(u16, host.len);
3333
34 const kp = crypto.dh.X25519.KeyPair.create(null) catch |err| switch (err) {
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;
34 var random_buffer: [128]u8 = undefined;
4135 crypto.random.bytes(&random_buffer);
4236 const hello_rand = random_buffer[0..32].*;
4337 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;
4450
4551 const extensions_payload =
4652 tls.extension(.supported_versions, [_]u8{
......@@ -66,14 +72,10 @@ pub fn init(stream: net.Stream, host: []const u8) !Client {
6672 })) ++ tls.extension(.supported_groups, enum_array(tls.NamedGroup, &.{
6773 //.secp256r1,
6874 .x25519,
69 })) ++ [_]u8{
70 // Extension: key_share
71 0, 51, // ExtensionType.key_share
72 0, 38, // byte length of this extension payload
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 ++
75 })) ++ tls.extension(
76 .key_share,
77 array(1, int2(@enumToInt(tls.NamedGroup.x25519)) ++ array(1, x25519_kp.public_key)),
78 ) ++
7779 int2(@enumToInt(tls.ExtensionType.server_name)) ++
7880 int2(host_len + 5) ++ // byte length of this extension payload
7981 int2(host_len + 3) ++ // server_name_list byte count
......@@ -230,7 +232,7 @@ pub fn init(stream: net.Stream, host: []const u8) !Client {
230232 }
231233
232234 const shared_key = crypto.dh.X25519.scalarmult(
233 kp.secret_key,
235 x25519_kp.secret_key,
234236 x25519_server_pub_key.*,
235237 ) catch return error.TlsDecryptFailure;
236238