fix MWP: incremental Blake2b hashing, address count limit in
deserialization
This commit is contained in:
@@ -11,6 +11,9 @@ pub const PROTOCOL_VERSION: u8 = 0x01;
|
||||
/// Magic bytes for hole punch packets
|
||||
pub const HOLE_PUNCH_MAGIC = [4]u8{ 'M', 'W', 'P', '1' };
|
||||
|
||||
/// Maximum addresses per meeting publication (prevents malicious inflation)
|
||||
const MAX_ADDRESSES: u8 = 32;
|
||||
|
||||
/// Default parameters
|
||||
pub const Defaults = struct {
|
||||
/// Time to live for publications (seconds)
|
||||
@@ -187,44 +190,46 @@ pub const MeetingPublication = struct {
|
||||
|
||||
/// Compute hash of publication (for signing)
|
||||
pub fn computeHash(self: MeetingPublication) [32]u8 {
|
||||
var buf: [1024]u8 = undefined;
|
||||
var idx: usize = 0;
|
||||
const Blake2b256 = std.crypto.hash.blake2.Blake2b256;
|
||||
var hasher = Blake2b256.init(.{});
|
||||
|
||||
// Version
|
||||
buf[idx] = PROTOCOL_VERSION;
|
||||
idx += 1;
|
||||
hasher.update(&[_]u8{PROTOCOL_VERSION});
|
||||
|
||||
// Peer ID
|
||||
@memcpy(buf[idx..][0..32], &self.peer_id);
|
||||
idx += 32;
|
||||
hasher.update(&self.peer_id);
|
||||
|
||||
// Address count
|
||||
buf[idx] = @intCast(self.addresses.len);
|
||||
idx += 1;
|
||||
hasher.update(&[_]u8{@intCast(self.addresses.len)});
|
||||
|
||||
// Addresses (simplified - just count for hash)
|
||||
// Addresses
|
||||
for (self.addresses) |addr| {
|
||||
const size = addr.serialize(buf[idx..]) catch 0;
|
||||
idx += size;
|
||||
var addr_buf: [19]u8 = undefined; // max: 1 type + 16 ipv6 + 2 port
|
||||
const size = addr.serialize(&addr_buf) catch 0;
|
||||
hasher.update(addr_buf[0..size]);
|
||||
}
|
||||
|
||||
// Nonce
|
||||
@memcpy(buf[idx..][0..32], &self.nonce);
|
||||
idx += 32;
|
||||
hasher.update(&self.nonce);
|
||||
|
||||
// Timestamp
|
||||
std.mem.writeInt(i64, buf[idx..][0..8], self.timestamp, .big);
|
||||
idx += 8;
|
||||
var ts_buf: [8]u8 = undefined;
|
||||
std.mem.writeInt(i64, &ts_buf, self.timestamp, .big);
|
||||
hasher.update(&ts_buf);
|
||||
|
||||
// TTL
|
||||
std.mem.writeInt(u32, buf[idx..][0..4], self.ttl, .big);
|
||||
idx += 4;
|
||||
var ttl_buf: [4]u8 = undefined;
|
||||
std.mem.writeInt(u32, &ttl_buf, self.ttl, .big);
|
||||
hasher.update(&ttl_buf);
|
||||
|
||||
// Sequence
|
||||
std.mem.writeInt(u64, buf[idx..][0..8], self.sequence, .big);
|
||||
idx += 8;
|
||||
var seq_buf: [8]u8 = undefined;
|
||||
std.mem.writeInt(u64, &seq_buf, self.sequence, .big);
|
||||
hasher.update(&seq_buf);
|
||||
|
||||
return blake2b256(buf[0..idx]);
|
||||
var out: [32]u8 = undefined;
|
||||
hasher.final(&out);
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Verify signature
|
||||
@@ -314,6 +319,7 @@ pub const MeetingPublication = struct {
|
||||
// Addresses
|
||||
const addr_count = buf[idx];
|
||||
idx += 1;
|
||||
if (addr_count > MAX_ADDRESSES) return error.InvalidData;
|
||||
|
||||
const addresses = try allocator.alloc(Address, addr_count);
|
||||
errdefer allocator.free(addresses);
|
||||
|
||||
@@ -57,7 +57,7 @@ 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
|
||||
const MAX_TIMESTAMP_SKEW_MS: i64 = 30_000; // 30 seconds
|
||||
|
||||
/// Main MWP integration with zig-p2p
|
||||
pub const MWP = struct {
|
||||
|
||||
Reference in New Issue
Block a user