create Mutual Witness Protocol specs and implementation. #1

Merged
mdt merged 26 commits from mwp into main 2026-02-11 17:38:08 +00:00
67 changed files with 11160 additions and 240 deletions
Showing only changes of commit 49ef3f6640 - Show all commits

View File

@@ -112,14 +112,14 @@ pub const HolePunchEngine = struct {
public: [32]u8, public: [32]u8,
secret: [64]u8, secret: [64]u8,
pub fn sign(self: KeyPair, message: [32]u8) [64]u8 { pub fn sign(self: KeyPair, message: [32]u8) error{InvalidKeyData}![64]u8 {
const secret_key = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch unreachable; 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 unreachable; const public_key = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch return error.InvalidKeyData;
const key_pair = std.crypto.sign.Ed25519.KeyPair{ const key_pair = std.crypto.sign.Ed25519.KeyPair{
.secret_key = secret_key, .secret_key = secret_key,
.public_key = public_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(); return sig.toBytes();
} }
}; };
@@ -243,7 +243,9 @@ pub const HolePunchEngine = struct {
} }
// Create and send hole punch packets // 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(); const packet_bytes = packet.serialize();
for (contexts.items) |*ctx| { for (contexts.items) |*ctx| {
@@ -301,7 +303,7 @@ pub const HolePunchEngine = struct {
} }
/// Create a hole punch packet /// Create a hole punch packet
fn createHolePunchPacket(self: *HolePunchEngine, params: RendezvousParams) HolePunchPacket { fn createHolePunchPacket(self: *HolePunchEngine, params: RendezvousParams) error{InvalidKeyData}!HolePunchPacket {
var packet = HolePunchPacket{ var packet = HolePunchPacket{
.connection_id = params.connection_id, .connection_id = params.connection_id,
.sender_id = self.our_keypair.public, .sender_id = self.our_keypair.public,
@@ -310,7 +312,7 @@ pub const HolePunchEngine = struct {
}; };
const hash = packet.computeHash(); const hash = packet.computeHash();
packet.signature = self.our_keypair.sign(hash); packet.signature = try self.our_keypair.sign(hash);
return packet; return packet;
} }
@@ -451,7 +453,7 @@ pub const HolePunchListener = struct {
}; };
const hash = packet.computeHash(); const hash = packet.computeHash();
packet.signature = self.our_keypair.sign(hash); packet.signature = try self.our_keypair.sign(hash);
const bytes = packet.serialize(); const bytes = packet.serialize();
try self.socket.sendTo(to, &bytes); try self.socket.sendTo(to, &bytes);

View File

@@ -56,14 +56,14 @@ pub const MeetingPoint = struct {
secret: [64]u8, secret: [64]u8,
/// Sign a message /// Sign a message
pub fn sign(self: KeyPair, message: [32]u8) [64]u8 { pub fn sign(self: KeyPair, message: [32]u8) error{InvalidKeyData}![64]u8 {
const secret_key = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch unreachable; 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 unreachable; const public_key = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch return error.InvalidKeyData;
const key_pair = std.crypto.sign.Ed25519.KeyPair{ const key_pair = std.crypto.sign.Ed25519.KeyPair{
.secret_key = secret_key, .secret_key = secret_key,
.public_key = public_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(); return sig.toBytes();
} }
}; };
@@ -106,7 +106,7 @@ pub const MeetingPoint = struct {
} }
/// Create a new publication /// 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{ var pub_data = MeetingPublication{
.peer_id = self.our_keypair.public, .peer_id = self.our_keypair.public,
.addresses = self.our_addresses, .addresses = self.our_addresses,
@@ -122,7 +122,7 @@ pub const MeetingPoint = struct {
// Sign // Sign
const hash = pub_data.computeHash(); 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; self.next_sequence += 1;
@@ -132,7 +132,7 @@ pub const MeetingPoint = struct {
/// Publish to a meeting point /// Publish to a meeting point
pub fn publish(self: *MeetingPoint, their_pubkey: [32]u8, ttl: u32) !MeetingPublication { pub fn publish(self: *MeetingPoint, their_pubkey: [32]u8, ttl: u32) !MeetingPublication {
const meeting_key = computeMeetingKey(self.our_keypair.public, their_pubkey); 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); const serialized = try publication.serialize(self.allocator);
defer self.allocator.free(serialized); defer self.allocator.free(serialized);

View File

@@ -91,9 +91,9 @@ pub const KeyPair = struct {
} }
/// Create from existing secret key bytes /// Create from existing secret key bytes
pub fn fromSecretKey(secret: [64]u8) KeyPair { pub fn fromSecretKey(secret: [64]u8) error{InvalidKeyData}!KeyPair {
const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(secret) catch unreachable; const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(secret) catch return error.InvalidKeyData;
const kp = std.crypto.sign.Ed25519.KeyPair.fromSecretKey(sk) catch unreachable; const kp = std.crypto.sign.Ed25519.KeyPair.fromSecretKey(sk) catch return error.InvalidKeyData;
return .{ return .{
.public = kp.public_key.toBytes(), .public = kp.public_key.toBytes(),
.secret = secret, .secret = secret,
@@ -101,14 +101,14 @@ pub const KeyPair = struct {
} }
/// Sign a 32-byte hash /// Sign a 32-byte hash
pub fn sign(self: KeyPair, hash: [32]u8) [64]u8 { pub fn sign(self: KeyPair, hash: [32]u8) error{InvalidKeyData}![64]u8 {
const sk = std.crypto.sign.Ed25519.SecretKey.fromBytes(self.secret) catch unreachable; 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 unreachable; const pk = std.crypto.sign.Ed25519.PublicKey.fromBytes(self.public) catch return error.InvalidKeyData;
const kp = std.crypto.sign.Ed25519.KeyPair{ const kp = std.crypto.sign.Ed25519.KeyPair{
.secret_key = sk, .secret_key = sk,
.public_key = pk, .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(); return sig.toBytes();
} }
}; };

View File

@@ -252,6 +252,7 @@ pub const MeetingPublication = struct {
size += 64; // signature size += 64; // signature
const buf = try allocator.alloc(u8, size); const buf = try allocator.alloc(u8, size);
errdefer allocator.free(buf);
var idx: usize = 0; var idx: usize = 0;
// Version // Version
@@ -315,6 +316,7 @@ pub const MeetingPublication = struct {
idx += 1; idx += 1;
const addresses = try allocator.alloc(Address, addr_count); const addresses = try allocator.alloc(Address, addr_count);
errdefer allocator.free(addresses);
for (0..addr_count) |i| { for (0..addr_count) |i| {
const result = try Address.deserialize(buf[idx..]); const result = try Address.deserialize(buf[idx..]);
addresses[i] = result.addr; addresses[i] = result.addr;

View File

@@ -54,6 +54,11 @@ pub const Defaults = types.Defaults;
// Re-export KeyPair from mutual_witness // Re-export KeyPair from mutual_witness
pub const KeyPair = mwp_lib.KeyPair; 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 /// Main MWP integration with zig-p2p
pub const MWP = struct { pub const MWP = struct {
allocator: Allocator, allocator: Allocator,
@@ -140,7 +145,7 @@ pub const MWP = struct {
} }
/// Create a publication for a target peer /// 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{ var pub_data = MeetingPublication{
.peer_id = self.keypair.public, .peer_id = self.keypair.public,
.addresses = self.our_addresses, .addresses = self.our_addresses,
@@ -153,7 +158,7 @@ pub const MWP = struct {
std.crypto.random.bytes(&pub_data.nonce); std.crypto.random.bytes(&pub_data.nonce);
const hash = pub_data.computeHash(); const hash = pub_data.computeHash();
pub_data.signature = self.keypair.sign(hash); pub_data.signature = try self.keypair.sign(hash);
self.next_sequence += 1; self.next_sequence += 1;
return pub_data; return pub_data;
@@ -164,7 +169,7 @@ pub const MWP = struct {
const meeting_key = computeMeetingKey(self.keypair.public, their_pubkey); const meeting_key = computeMeetingKey(self.keypair.public, their_pubkey);
const pub_key = computePublicationKey(meeting_key, self.keypair.public); 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); const serialized = try publication.serialize(self.allocator);
defer self.allocator.free(serialized); defer self.allocator.free(serialized);
@@ -204,6 +209,17 @@ pub const MWP = struct {
return null; 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; return publication;
} }
@@ -270,6 +286,15 @@ pub const MWP = struct {
peer_publication: MeetingPublication, peer_publication: MeetingPublication,
options: ConnectOptions, options: ConnectOptions,
) ConnectionResult { ) 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 // Compute rendezvous params from both publications
const params = computeRendezvousParamsFromPubs(our_publication, peer_publication, options.rendezvous_buffer_ms); const params = computeRendezvousParamsFromPubs(our_publication, peer_publication, options.rendezvous_buffer_ms);
@@ -363,7 +388,9 @@ pub const MWP = struct {
.signature = undefined, .signature = undefined,
}; };
const hash = pkt.computeHash(); 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(); const packet = pkt.serialize();
// Send-and-receive loop: resend periodically to handle timing jitter. // 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" { test "KeyPair generation and signing" {
const kp = KeyPair.generate(); const kp = KeyPair.generate();
const hash = [_]u8{0xAB} ** 32; const hash = [_]u8{0xAB} ** 32;
const sig = kp.sign(hash); const sig = try kp.sign(hash);
// Verify signature // 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); const signature = std.crypto.sign.Ed25519.Signature.fromBytes(sig);
try signature.verify(&hash, pk); try signature.verify(&hash, pk);
} }