harden MWP error handling. return errors from signing instead of

unreachable, fix memory leak on deserialization failure, validate peer
publication timestamps against max skew
This commit is contained in:
2026-02-10 20:11:30 -03:00
parent bc4c49f724
commit 49ef3f6640
5 changed files with 59 additions and 28 deletions

View File

@@ -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);