feat: add ratchet, streaming, SU3 reseed, and router floodfill
- Add ratchet/ with ECIES-X25519-AEAD-Ratchet (Proposal 144), Garlic cloves, and ECIES DatabaseLookup replies - Add streaming/ with Ed25519-signed I2P Streaming (SYN/ACK/NACK/CLOSE/RESET, retransmit, window) and Repliable/Raw Datagrams - Add common/su3.mbt to parse and verify reseed .su3 bundles - Add Elligator2 encoding in crypto/ and OfflineSignature support in LeaseSet2 (flags bit 0) - Router: NTCP2 peer pool, floodfill NetDB crawl, transit tunnel relay, and Garlic/Streaming bridge - cmd/router: add --peer-dir, --su3, --reseed, --floodfill, --no-clock-skew - cmd/client: add --dest for persistent destination keys - Mark Phase 6 complete in README and document the transport scope (NTCP2 only, no SSU2)
This commit is contained in:
1 parent
8eafa11b22
commit
ae72b00323
54 files changed
+9607
-704
No files matched your search
@@ -0,0 +1,480 @@
|
||||
///|
|
||||
/// Live Proposal 157 ShortTunnelBuild engine and AES-256-CBC `TunnelData`/`TunnelGateway` relay.
|
||||
|
||||
///|
|
||||
priv struct PendingTunnelBuild {
|
||||
tunnel_id : UInt32
|
||||
direction : @tunnel.TunnelDirection
|
||||
hops : Array[@tunnel.TunnelHop]
|
||||
hop_slots_and_keys : Array[(Int32, @tunnel.HopKeys)]
|
||||
build_msg_id : UInt32
|
||||
created_sec : UInt32
|
||||
expires_sec : UInt32
|
||||
started_ms : UInt64
|
||||
}
|
||||
|
||||
///|
|
||||
/// Returns `true` if `tunnel_id` has completed building and is active in `self.tunnels`.
|
||||
pub fn Router::has_tunnel(self : Router, tunnel_id : UInt32) -> Bool {
|
||||
for t in self.tunnels.inbound {
|
||||
if t.id == tunnel_id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
for t in self.tunnels.outbound {
|
||||
if t.id == tunnel_id {
|
||||
return true
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
///|
|
||||
/// Initiates a Proposal 157 `ShortTunnelBuild` along `hop_hashes` (1..8 routers).
|
||||
/// Returns the allocated local tunnel ID; drive completion with `Router::step()`
|
||||
/// and check readiness via `Router::has_tunnel(tid)`.
|
||||
pub fn Router::build_tunnel(
|
||||
self : Router,
|
||||
hop_hashes : Array[@common.Hash],
|
||||
direction : @tunnel.TunnelDirection,
|
||||
expiration_sec~ : UInt32 = 600U,
|
||||
) -> UInt32 raise RouterError {
|
||||
let hop_count = hop_hashes.length()
|
||||
if hop_count < 1 || hop_count > 8 {
|
||||
raise Protocol("tunnel hop count must be 1..8, got \{hop_count}")
|
||||
}
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let now_sec = (now_ms / 1000UL).to_uint()
|
||||
let req_time_min = now_sec / 60U
|
||||
let local_tid = self.alloc_tunnel_id()
|
||||
let build_msg_id = self.alloc_msg_id()
|
||||
|
||||
let hop_recv_tids : Array[UInt32] = Array(capacity=hop_count)
|
||||
let hop_static_pubs : Array[Bytes] = Array(capacity=hop_count)
|
||||
for h in hop_hashes {
|
||||
let ri = match self.netdb.get_router_info(h) {
|
||||
Some(r) => r
|
||||
None => raise Protocol("missing RouterInfo for tunnel hop")
|
||||
}
|
||||
let x_pub = ri.identity.inner.crypto_public_key()
|
||||
if x_pub.length() != 32 {
|
||||
raise Protocol("tunnel hop lacks X25519 identity key")
|
||||
}
|
||||
hop_static_pubs.push(x_pub)
|
||||
hop_recv_tids.push(self.alloc_tunnel_id())
|
||||
}
|
||||
|
||||
let total_slots = if hop_count <= 4 { 4 } else { 8 }
|
||||
let specs : Array[@tunnel.ShortHopBuildSpec] = Array(capacity=hop_count)
|
||||
let next_tids : Array[UInt32] = Array(capacity=hop_count)
|
||||
for i = 0; i < hop_count; i = i + 1 {
|
||||
let next_tid = if i + 1 < hop_count {
|
||||
hop_recv_tids[i + 1]
|
||||
} else if direction == Inbound {
|
||||
local_tid
|
||||
} else {
|
||||
0U
|
||||
}
|
||||
next_tids.push(next_tid)
|
||||
let next_ident = if i + 1 < hop_count {
|
||||
hop_hashes[i + 1]
|
||||
} else {
|
||||
self.router_hash()
|
||||
}
|
||||
let flag : Byte = if direction == Inbound && i == 0 {
|
||||
b'\x80'
|
||||
} else if direction == Outbound && i == hop_count - 1 {
|
||||
b'\x40'
|
||||
} else {
|
||||
b'\x00'
|
||||
}
|
||||
let epriv = @ntcp2_net.random_bytes(32) catch { e => raise Net(e) }
|
||||
let ephem = @noise.KeyPair::from_private(epriv) catch {
|
||||
e => raise Noise(e)
|
||||
}
|
||||
let pad = @ntcp2_net.random_bytes(8) catch { _ => b"" }
|
||||
specs.push({
|
||||
slot_idx: i,
|
||||
hop_ident: hop_hashes[i],
|
||||
hop_static_pub: hop_static_pubs[i],
|
||||
ephemeral: ephem,
|
||||
record: {
|
||||
receive_tunnel_id: hop_recv_tids[i],
|
||||
next_tunnel_id: next_tid,
|
||||
next_ident,
|
||||
flag,
|
||||
layer_enc_type: b'\x00',
|
||||
request_time_min: req_time_min,
|
||||
request_expiration_sec: expiration_sec,
|
||||
send_message_id: build_msg_id,
|
||||
options: @common.Mapping::new(),
|
||||
},
|
||||
padding: pad,
|
||||
})
|
||||
}
|
||||
|
||||
let dummies : Array[Bytes] = []
|
||||
for _d = hop_count; _d < total_slots; _d = _d + 1 {
|
||||
let dummy = @ntcp2_net.random_bytes(218) catch {
|
||||
_ => Bytes::make(218, b'\x5a')
|
||||
}
|
||||
dummies.push(dummy)
|
||||
}
|
||||
let (records, hop_slots_and_keys) = @tunnel.build_short_tunnel_request(
|
||||
total_slots, specs, dummies,
|
||||
) catch {
|
||||
e => raise Tunnel(e)
|
||||
}
|
||||
|
||||
let tunnel_hops : Array[@tunnel.TunnelHop] = Array(capacity=hop_count)
|
||||
for i = 0; i < hop_count; i = i + 1 {
|
||||
let keys = hop_slots_and_keys[i].1
|
||||
tunnel_hops.push({
|
||||
router_hash: hop_hashes[i],
|
||||
receive_tunnel_id: hop_recv_tids[i],
|
||||
next_tunnel_id: next_tids[i],
|
||||
layer_key: keys.layer_key,
|
||||
iv_key: keys.iv_key,
|
||||
})
|
||||
}
|
||||
self.pending_builds.push({
|
||||
tunnel_id: local_tid,
|
||||
direction,
|
||||
hops: tunnel_hops,
|
||||
hop_slots_and_keys,
|
||||
build_msg_id,
|
||||
created_sec: now_sec,
|
||||
expires_sec: now_sec + expiration_sec,
|
||||
started_ms: now_ms,
|
||||
})
|
||||
let _ = self.send_i2np_to_peer(
|
||||
hop_hashes[0],
|
||||
ShortTunnelBuild(records~),
|
||||
msg_id_override=Some(build_msg_id),
|
||||
)
|
||||
local_tid
|
||||
}
|
||||
|
||||
///|
|
||||
/// Sends an I2NP message encrypted through a built active `Outbound` tunnel.
|
||||
pub fn Router::send_through_tunnel(
|
||||
self : Router,
|
||||
outbound_tunnel_id : UInt32,
|
||||
target : @tunnel.DeliveryTarget,
|
||||
payload : @i2np.I2npPayload,
|
||||
) -> Unit raise RouterError {
|
||||
let mut out_tun : @tunnel.Tunnel? = None
|
||||
for t in self.tunnels.outbound {
|
||||
if t.id == outbound_tunnel_id {
|
||||
out_tun = Some(t)
|
||||
break
|
||||
}
|
||||
}
|
||||
let tun = match out_tun {
|
||||
Some(t) => t
|
||||
None => raise Protocol("unknown outbound tunnel \{outbound_tunnel_id}")
|
||||
}
|
||||
if tun.hops.length() == 0 {
|
||||
raise Protocol("outbound tunnel has zero hops")
|
||||
}
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let inner_mid = self.alloc_msg_id()
|
||||
let std_bytes = @i2np.encode_standard_message(
|
||||
inner_mid,
|
||||
now_ms + 60000UL,
|
||||
payload,
|
||||
) catch {
|
||||
e => raise I2np(e)
|
||||
}
|
||||
let base_iv = @ntcp2_net.random_bytes(16) catch { e => raise Net(e) }
|
||||
let pad = non_zero_random_bytes(1024)
|
||||
let cells = @tunnel.fragment_message_to_cells(
|
||||
target,
|
||||
self.alloc_msg_id(),
|
||||
std_bytes,
|
||||
base_iv,
|
||||
padding=pad,
|
||||
) catch {
|
||||
e => raise Tunnel(e)
|
||||
}
|
||||
let first_hop = tun.hops[0]
|
||||
for cell in cells {
|
||||
let enc_cell = tun.prepare_outbound_cell(cell) catch {
|
||||
e => raise Tunnel(e)
|
||||
}
|
||||
let _ = self.send_i2np_to_peer(
|
||||
first_hop.router_hash,
|
||||
TunnelData(tunnel_id=first_hop.receive_tunnel_id, data=enc_cell),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::try_complete_pending_tunnel_build(
|
||||
self : Router,
|
||||
build_msg_id : UInt32,
|
||||
records : Array[Bytes],
|
||||
) -> Bool {
|
||||
let mut idx = -1
|
||||
for i = 0; i < self.pending_builds.length(); i = i + 1 {
|
||||
if self.pending_builds[i].build_msg_id == build_msg_id {
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
if idx < 0 {
|
||||
return false
|
||||
}
|
||||
let pb = self.pending_builds.remove(idx)
|
||||
let codes = @tunnel.decrypt_short_tunnel_replies(
|
||||
records,
|
||||
pb.hop_slots_and_keys,
|
||||
) catch {
|
||||
_ => return true
|
||||
}
|
||||
for c in codes {
|
||||
if c != b'\x00' {
|
||||
return true
|
||||
}
|
||||
}
|
||||
let dir = pb.direction
|
||||
self.tunnels.add_tunnel({
|
||||
id: pb.tunnel_id,
|
||||
direction: dir,
|
||||
hops: pb.hops,
|
||||
created_sec: pb.created_sec,
|
||||
expires_sec: pb.expires_sec,
|
||||
})
|
||||
if dir == Inbound {
|
||||
self.republish_all_sessions()
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::handle_short_tunnel_build(
|
||||
self : Router,
|
||||
msg_id : UInt32,
|
||||
records : Array[Bytes],
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
if self.try_complete_pending_tunnel_build(msg_id, records) {
|
||||
return
|
||||
}
|
||||
let reply_pad = @ntcp2_net.random_bytes(8) catch { _ => b"" }
|
||||
let (rec, keys, _slot, out_records) = @tunnel.process_short_tunnel_build_hop(
|
||||
records,
|
||||
self.router_hash(),
|
||||
self.static_key,
|
||||
b'\x00',
|
||||
reply_padding=reply_pad,
|
||||
) catch {
|
||||
_ => return
|
||||
}
|
||||
let is_obep = (rec.flag.to_int() & 0x40) != 0
|
||||
let exp_sec = now_sec +
|
||||
(if rec.request_expiration_sec > 0U {
|
||||
rec.request_expiration_sec
|
||||
} else {
|
||||
600U
|
||||
})
|
||||
self.tunnels.add_transit_hop({
|
||||
receive_tunnel_id: rec.receive_tunnel_id,
|
||||
next_tunnel_id: rec.next_tunnel_id,
|
||||
next_router: rec.next_ident,
|
||||
layer_key: keys.layer_key,
|
||||
iv_key: keys.iv_key,
|
||||
is_obep,
|
||||
expires_sec: exp_sec,
|
||||
})
|
||||
if is_obep {
|
||||
let reply_payload : @i2np.I2npPayload = ShortTunnelBuildReply(
|
||||
records=out_records,
|
||||
)
|
||||
if rec.next_tunnel_id != 0U {
|
||||
let std = @i2np.encode_standard_message(
|
||||
rec.send_message_id,
|
||||
@ntcp2_net.unix_time_ms() + 60000UL,
|
||||
reply_payload,
|
||||
) catch {
|
||||
_ => return
|
||||
}
|
||||
let _ = self.send_i2np_to_peer(
|
||||
rec.next_ident,
|
||||
TunnelGateway(tunnel_id=rec.next_tunnel_id, data=std),
|
||||
)
|
||||
} else {
|
||||
let _ = self.send_i2np_to_peer(
|
||||
rec.next_ident,
|
||||
reply_payload,
|
||||
msg_id_override=Some(rec.send_message_id),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
let _ = self.send_i2np_to_peer(
|
||||
rec.next_ident,
|
||||
ShortTunnelBuild(records=out_records),
|
||||
msg_id_override=Some(rec.send_message_id),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::handle_short_tunnel_build_reply(
|
||||
self : Router,
|
||||
msg_id : UInt32,
|
||||
records : Array[Bytes],
|
||||
) -> Unit {
|
||||
let _ = self.try_complete_pending_tunnel_build(msg_id, records)
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::deliver_reassembled_payload(
|
||||
self : Router,
|
||||
target : @tunnel.DeliveryTarget,
|
||||
full_payload : Bytes,
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
let (hdr, inner_payload) = @i2np.decode_standard_message(
|
||||
@encoding.Reader::new(full_payload),
|
||||
) catch {
|
||||
_ => return
|
||||
}
|
||||
match target {
|
||||
Local =>
|
||||
self.handle_i2np_message(
|
||||
None,
|
||||
{
|
||||
msg_type: hdr.msg_type,
|
||||
msg_id: hdr.msg_id,
|
||||
expiration_sec: now_sec + 60U,
|
||||
},
|
||||
inner_payload,
|
||||
)
|
||||
Router(router_hash~) => {
|
||||
let _ = self.send_i2np_to_peer(
|
||||
router_hash,
|
||||
inner_payload,
|
||||
msg_id_override=Some(hdr.msg_id),
|
||||
)
|
||||
}
|
||||
Tunnel(tunnel_id~, gateway~) => {
|
||||
let _ = self.send_i2np_to_peer(
|
||||
gateway,
|
||||
TunnelGateway(tunnel_id~, data=full_payload),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::consume_cleartext_cell(
|
||||
self : Router,
|
||||
cleartext_cell : Bytes,
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
let frags = @tunnel.unpack_tunnel_cell(cleartext_cell) catch { _ => return }
|
||||
for frag in frags {
|
||||
match self.reassembler.push(frag) {
|
||||
Some((target, full_payload)) =>
|
||||
self.deliver_reassembled_payload(target, full_payload, now_sec)
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::handle_tunnel_data(
|
||||
self : Router,
|
||||
tunnel_id : UInt32,
|
||||
data : Bytes,
|
||||
) -> Unit {
|
||||
let now_sec = @ntcp2_net.unix_time_sec()
|
||||
// 1. Check if we are the inbound tunnel endpoint (IBEP)
|
||||
for in_t in self.tunnels.inbound {
|
||||
if in_t.id == tunnel_id {
|
||||
let clear = in_t.decrypt_inbound_cell(data) catch { _ => return }
|
||||
self.consume_cleartext_cell(clear, now_sec)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 2. Otherwise check transit hops (IBGW / intermediate / OBEP)
|
||||
let relay_res = self.tunnels.relay_cell(tunnel_id, data) catch { _ => None }
|
||||
match relay_res {
|
||||
Some((next_router, next_tid, next_cell, is_obep)) =>
|
||||
if is_obep {
|
||||
self.consume_cleartext_cell(next_cell, now_sec)
|
||||
} else {
|
||||
let _ = self.send_i2np_to_peer(
|
||||
next_router,
|
||||
TunnelData(tunnel_id=next_tid, data=next_cell),
|
||||
)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::handle_tunnel_gateway(
|
||||
self : Router,
|
||||
tunnel_id : UInt32,
|
||||
data : Bytes,
|
||||
now_sec : UInt32,
|
||||
) -> Unit {
|
||||
// Check if tunnel_id corresponds to a transit hop where we act as IBGW
|
||||
let mut is_transit = false
|
||||
for hop in self.tunnels.transit {
|
||||
if hop.receive_tunnel_id == tunnel_id {
|
||||
is_transit = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if is_transit {
|
||||
let base_iv = @ntcp2_net.random_bytes(16) catch { _ => return }
|
||||
let pad = non_zero_random_bytes(1024)
|
||||
let cells = @tunnel.fragment_message_to_cells(
|
||||
Local,
|
||||
self.alloc_msg_id(),
|
||||
data,
|
||||
base_iv,
|
||||
padding=pad,
|
||||
) catch {
|
||||
_ => return
|
||||
}
|
||||
for cell in cells {
|
||||
let relay_res = self.tunnels.relay_cell(tunnel_id, cell) catch {
|
||||
_ => None
|
||||
}
|
||||
match relay_res {
|
||||
Some((next_router, next_tid, next_cell, is_obep)) =>
|
||||
if is_obep {
|
||||
self.consume_cleartext_cell(next_cell, now_sec)
|
||||
} else {
|
||||
let _ = self.send_i2np_to_peer(
|
||||
next_router,
|
||||
TunnelData(tunnel_id=next_tid, data=next_cell),
|
||||
)
|
||||
}
|
||||
None => ()
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
// Direct 0-hop session delivery or forwarded standard I2NP message
|
||||
self.deliver_reassembled_payload(Local, data, now_sec)
|
||||
}
|
||||
|
||||
///|
|
||||
fn Router::poll_tunnel_builds(self : Router) -> Unit {
|
||||
let now_ms = @ntcp2_net.unix_time_ms()
|
||||
let now_sec = (now_ms / 1000UL).to_uint()
|
||||
let _ = self.tunnels.prune_expired(now_sec)
|
||||
let mut i = 0
|
||||
while i < self.pending_builds.length() {
|
||||
if now_ms > self.pending_builds[i].started_ms + 10000UL {
|
||||
let _ = self.pending_builds.remove(i)
|
||||
} else {
|
||||
i += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user