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:
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user