diff --git a/src/mwp/hole_punch.zig b/src/mwp/hole_punch.zig index ce042cb..77b1d42 100644 --- a/src/mwp/hole_punch.zig +++ b/src/mwp/hole_punch.zig @@ -112,14 +112,14 @@ pub const HolePunchEngine = struct { public: [32]u8, secret: [64]u8, - pub fn sign(self: KeyPair, message: [32]u8) [64]u8 { - const secret_key = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch unreachable; - const public_key = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch unreachable; + pub fn sign(self: KeyPair, message: [32]u8) error{InvalidKeyData}![64]u8 { + const secret_key = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch return error.InvalidKeyData; + const public_key = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch return error.InvalidKeyData; const key_pair = std.crypto.sign.Ed25519.KeyPair{ .secret_key = secret_key, .public_key = public_key, }; - const sig = std.crypto.sign.Ed25519.KeyPair.sign(key_pair, &message, null) catch unreachable; + const sig = std.crypto.sign.Ed25519.KeyPair.sign(key_pair, &message, null) catch return error.InvalidKeyData; return sig.toBytes(); } }; @@ -243,7 +243,9 @@ pub const HolePunchEngine = struct { } // Create and send hole punch packets - const packet = self.createHolePunchPacket(params); + const packet = self.createHolePunchPacket(params) catch |err| { + return .{ .other_error = err }; + }; const packet_bytes = packet.serialize(); for (contexts.items) |*ctx| { @@ -301,7 +303,7 @@ pub const HolePunchEngine = struct { } /// Create a hole punch packet - fn createHolePunchPacket(self: *HolePunchEngine, params: RendezvousParams) HolePunchPacket { + fn createHolePunchPacket(self: *HolePunchEngine, params: RendezvousParams) error{InvalidKeyData}!HolePunchPacket { var packet = HolePunchPacket{ .connection_id = params.connection_id, .sender_id = self.our_keypair.public, @@ -310,7 +312,7 @@ pub const HolePunchEngine = struct { }; const hash = packet.computeHash(); - packet.signature = self.our_keypair.sign(hash); + packet.signature = try self.our_keypair.sign(hash); return packet; } @@ -451,7 +453,7 @@ pub const HolePunchListener = struct { }; const hash = packet.computeHash(); - packet.signature = self.our_keypair.sign(hash); + packet.signature = try self.our_keypair.sign(hash); const bytes = packet.serialize(); try self.socket.sendTo(to, &bytes); diff --git a/src/mwp/meeting_point.zig b/src/mwp/meeting_point.zig index 0e4dc5c..1263125 100644 --- a/src/mwp/meeting_point.zig +++ b/src/mwp/meeting_point.zig @@ -56,14 +56,14 @@ pub const MeetingPoint = struct { secret: [64]u8, /// Sign a message - pub fn sign(self: KeyPair, message: [32]u8) [64]u8 { - const secret_key = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch unreachable; - const public_key = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch unreachable; + pub fn sign(self: KeyPair, message: [32]u8) error{InvalidKeyData}![64]u8 { + const secret_key = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch return error.InvalidKeyData; + const public_key = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch return error.InvalidKeyData; const key_pair = std.crypto.sign.Ed25519.KeyPair{ .secret_key = secret_key, .public_key = public_key, }; - const sig = std.crypto.sign.Ed25519.KeyPair.sign(key_pair, &message, null) catch unreachable; + const sig = std.crypto.sign.Ed25519.KeyPair.sign(key_pair, &message, null) catch return error.InvalidKeyData; return sig.toBytes(); } }; @@ -106,7 +106,7 @@ pub const MeetingPoint = struct { } /// Create a new publication - pub fn createPublication(self: *MeetingPoint, ttl: u32) MeetingPublication { + pub fn createPublication(self: *MeetingPoint, ttl: u32) error{InvalidKeyData}!MeetingPublication { var pub_data = MeetingPublication{ .peer_id = self.our_keypair.public, .addresses = self.our_addresses, @@ -122,7 +122,7 @@ pub const MeetingPoint = struct { // Sign const hash = pub_data.computeHash(); - pub_data.signature = self.our_keypair.sign(hash); + pub_data.signature = try self.our_keypair.sign(hash); self.next_sequence += 1; @@ -132,7 +132,7 @@ pub const MeetingPoint = struct { /// Publish to a meeting point pub fn publish(self: *MeetingPoint, their_pubkey: [32]u8, ttl: u32) !MeetingPublication { const meeting_key = computeMeetingKey(self.our_keypair.public, their_pubkey); - const publication = self.createPublication(ttl); + const publication = try self.createPublication(ttl); const serialized = try publication.serialize(self.allocator); defer self.allocator.free(serialized); diff --git a/src/mwp/mutual_witness.zig b/src/mwp/mutual_witness.zig index 2ba6597..1800628 100644 --- a/src/mwp/mutual_witness.zig +++ b/src/mwp/mutual_witness.zig @@ -91,9 +91,9 @@ pub const KeyPair = struct { } /// Create from existing secret key bytes - pub fn fromSecretKey(secret: [64]u8) KeyPair { - const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(secret) catch unreachable; - const kp = std.crypto.sign.Ed25519.KeyPair.fromSecretKey(sk) catch unreachable; + pub fn fromSecretKey(secret: [64]u8) error{InvalidKeyData}!KeyPair { + const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(secret) catch return error.InvalidKeyData; + const kp = std.crypto.sign.Ed25519.KeyPair.fromSecretKey(sk) catch return error.InvalidKeyData; return .{ .public = kp.public_key.toBytes(), .secret = secret, @@ -101,14 +101,14 @@ pub const KeyPair = struct { } /// Sign a 32-byte hash - pub fn sign(self: KeyPair, hash: [32]u8) [64]u8 { - const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch unreachable; - const pk = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch unreachable; + pub fn sign(self: KeyPair, hash: [32]u8) error{InvalidKeyData}![64]u8 { + const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch return error.InvalidKeyData; + const pk = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch return error.InvalidKeyData; const kp = std.crypto.sign.Ed25519.KeyPair{ .secret_key = sk, .public_key = pk, }; - const sig = std.crypto.sign.Ed25519.KeyPair.sign(kp, &hash, null) catch unreachable; + const sig = std.crypto.sign.Ed25519.KeyPair.sign(kp, &hash, null) catch return error.InvalidKeyData; return sig.toBytes(); } }; diff --git a/src/mwp/types.zig b/src/mwp/types.zig index c1223b1..2c462ef 100644 --- a/src/mwp/types.zig +++ b/src/mwp/types.zig @@ -252,6 +252,7 @@ pub const MeetingPublication = struct { size += 64; // signature const buf = try allocator.alloc(u8, size); + errdefer allocator.free(buf); var idx: usize = 0; // Version @@ -315,6 +316,7 @@ pub const MeetingPublication = struct { idx += 1; const addresses = try allocator.alloc(Address, addr_count); + errdefer allocator.free(addresses); for (0..addr_count) |i| { const result = try Address.deserialize(buf[idx..]); addresses[i] = result.addr; diff --git a/src/mwp_integration.zig b/src/mwp_integration.zig index 5f8ba76..3f306dd 100644 --- a/src/mwp_integration.zig +++ b/src/mwp_integration.zig @@ -54,6 +54,11 @@ pub const Defaults = types.Defaults; // Re-export KeyPair from mutual_witness pub const KeyPair = mwp_lib.KeyPair; +/// Maximum allowed clock skew for peer publications (milliseconds). +/// Publications with timestamps more than this far in the future are rejected +/// to prevent a malicious peer from delaying rendezvous arbitrarily. +const MAX_TIMESTAMP_SKEW_MS: i64 = 60_000; // 60 seconds + /// Main MWP integration with zig-p2p pub const MWP = struct { allocator: Allocator, @@ -140,7 +145,7 @@ pub const MWP = struct { } /// Create a publication for a target peer - fn createPublication(self: *MWP, ttl: u32) MeetingPublication { + fn createPublication(self: *MWP, ttl: u32) error{InvalidKeyData}!MeetingPublication { var pub_data = MeetingPublication{ .peer_id = self.keypair.public, .addresses = self.our_addresses, @@ -153,7 +158,7 @@ pub const MWP = struct { std.crypto.random.bytes(&pub_data.nonce); const hash = pub_data.computeHash(); - pub_data.signature = self.keypair.sign(hash); + pub_data.signature = try self.keypair.sign(hash); self.next_sequence += 1; return pub_data; @@ -164,7 +169,7 @@ pub const MWP = struct { const meeting_key = computeMeetingKey(self.keypair.public, their_pubkey); const pub_key = computePublicationKey(meeting_key, self.keypair.public); - const publication = self.createPublication(ttl); + const publication = try self.createPublication(ttl); const serialized = try publication.serialize(self.allocator); defer self.allocator.free(serialized); @@ -204,6 +209,17 @@ pub const MWP = struct { return null; } + // SECURITY: Reject publications with far-future timestamps + // A malicious peer could use this to delay rendezvous arbitrarily + const now_ms = std.time.milliTimestamp(); + if (publication.timestamp > now_ms + MAX_TIMESTAMP_SKEW_MS) { + std.log.warn("MWP: Rejecting publication with far-future timestamp (skew: {}ms)", .{ + publication.timestamp - now_ms, + }); + publication.deinit(self.allocator); + return null; + } + return publication; } @@ -270,6 +286,15 @@ pub const MWP = struct { peer_publication: MeetingPublication, options: ConnectOptions, ) ConnectionResult { + // SECURITY: Reject peer publications with far-future timestamps + const now_ms = std.time.milliTimestamp(); + if (peer_publication.timestamp > now_ms + MAX_TIMESTAMP_SKEW_MS) { + std.log.warn("MWP: Peer publication has far-future timestamp (skew: {}ms), rejecting", .{ + peer_publication.timestamp - now_ms, + }); + return .{ .hole_punch_failed = .all_addresses_failed }; + } + // Compute rendezvous params from both publications const params = computeRendezvousParamsFromPubs(our_publication, peer_publication, options.rendezvous_buffer_ms); @@ -363,7 +388,9 @@ pub const MWP = struct { .signature = undefined, }; const hash = pkt.computeHash(); - pkt.signature = self.keypair.sign(hash); + pkt.signature = self.keypair.sign(hash) catch |err| { + return .{ .other_error = err }; + }; const packet = pkt.serialize(); // Send-and-receive loop: resend periodically to handle timing jitter. @@ -472,10 +499,10 @@ test "MWP meeting key is symmetric" { test "KeyPair generation and signing" { const kp = KeyPair.generate(); const hash = [_]u8{0xAB} ** 32; - const sig = kp.sign(hash); + const sig = try kp.sign(hash); // Verify signature - const pk = std.crypto.sign.Ed25519.PublicKey.fromBytes(kp.public) catch unreachable; + const pk = std.crypto.sign.Ed25519.PublicKey.fromBytes(kp.public) catch return error.InvalidKeyData; const signature = std.crypto.sign.Ed25519.Signature.fromBytes(sig); try signature.verify(&hash, pk); }