- Add `--public`, `--hops`, `--hosts`, and `--debug` router flags; `--public` auto-reseeds from the live network and keeps a status loop running. - Route NetDB lookups through tunnels with Noise_N garlic wrapping, fan out to two floodfills, and fetch unknown RouterInfos in the background. - Back off failed NTCP2 peers and fall back to IPv6 addresses when no IPv4 NTCP2 address is available. - Size session LeaseSet2 expiry from lease end dates and use the destination's X25519 key when present. - Add canonical Ed25519 Destination KeyCertificate constructors with round-trip tests. - Fix DatabaseStore gzip handling to honor the length prefix, and send `Wget/1.11.4` as the reseed User-Agent.
375 lines
11 KiB
MoonBit
375 lines
11 KiB
MoonBit
///|
|
|
/// Canonical I2P Streaming Protocol Packet codec (protocol 6 inside I2CP Gzip envelope).
|
|
|
|
///|
|
|
#derive(Eq, Debug)
|
|
pub suberror StreamingError {
|
|
Encoding(@encoding.EncodingError)
|
|
Common(@common.CommonError)
|
|
Crypto(@crypto.CryptoError)
|
|
InvalidFormat(String)
|
|
InvalidLength(String)
|
|
InvalidSignature
|
|
ProtocolError(String)
|
|
}
|
|
|
|
///|
|
|
pub let flag_synchronize : UInt32 = 0x0001U
|
|
|
|
///|
|
|
pub let flag_close : UInt32 = 0x0002U
|
|
|
|
///|
|
|
pub let flag_reset : UInt32 = 0x0004U
|
|
|
|
///|
|
|
pub let flag_signature_included : UInt32 = 0x0008U
|
|
|
|
///|
|
|
pub let flag_signature_requested : UInt32 = 0x0010U
|
|
|
|
///|
|
|
pub let flag_from_included : UInt32 = 0x0020U
|
|
|
|
///|
|
|
pub let flag_delay_requested : UInt32 = 0x0040U
|
|
|
|
///|
|
|
pub let flag_max_packet_size_included : UInt32 = 0x0080U
|
|
|
|
///|
|
|
pub let flag_profile_interactive : UInt32 = 0x0100U
|
|
|
|
///|
|
|
pub let flag_echo : UInt32 = 0x0200U
|
|
|
|
///|
|
|
pub let flag_no_ack : UInt32 = 0x0400U
|
|
|
|
///|
|
|
pub let flag_offline_signature : UInt32 = 0x0800U
|
|
|
|
///|
|
|
/// Canonical I2P Streaming Packet.
|
|
#derive(Eq, Debug)
|
|
pub(all) struct StreamingPacket {
|
|
send_stream_id : UInt32
|
|
recv_stream_id : UInt32
|
|
sequence_num : UInt32
|
|
ack_through : UInt32
|
|
nacks : Array[UInt32]
|
|
resend_delay : Byte
|
|
flags : UInt32
|
|
delay_requested : UInt32? // Option Order 1 (0x0040)
|
|
from_dest : @common.Destination? // Option Order 2 (0x0020)
|
|
max_packet_size : UInt32? // Option Order 3 (0x0080)
|
|
offline_sig : @common.OfflineSignature? // Option Order 4 (0x0800)
|
|
signature : Bytes? // Option Order 5 (0x0008)
|
|
payload : Bytes
|
|
}
|
|
|
|
///|
|
|
fn serialize_options(
|
|
pkt : StreamingPacket,
|
|
zero_sig_len~ : Int32 = -1,
|
|
) -> Bytes raise StreamingError {
|
|
let ow = @encoding.Writer::new(capacity=512)
|
|
if (pkt.flags & flag_delay_requested) != 0U {
|
|
let d = match pkt.delay_requested {
|
|
Some(v) => v
|
|
None =>
|
|
raise InvalidFormat("DELAY_REQUESTED flag set without delay_requested")
|
|
}
|
|
ow.write_u16_be(d)
|
|
}
|
|
if (pkt.flags & flag_from_included) != 0U {
|
|
let dest = match pkt.from_dest {
|
|
Some(d) => d
|
|
None => raise InvalidFormat("FROM_INCLUDED flag set without from_dest")
|
|
}
|
|
dest.encode(ow)
|
|
}
|
|
if (pkt.flags & flag_max_packet_size_included) != 0U {
|
|
let m = match pkt.max_packet_size {
|
|
Some(v) => v
|
|
None =>
|
|
raise InvalidFormat(
|
|
"MAX_PACKET_SIZE_INCLUDED flag set without max_packet_size",
|
|
)
|
|
}
|
|
ow.write_u16_be(m)
|
|
}
|
|
if (pkt.flags & flag_offline_signature) != 0U {
|
|
let os = match pkt.offline_sig {
|
|
Some(o) => o
|
|
None =>
|
|
raise InvalidFormat("OFFLINE_SIGNATURE flag set without offline_sig")
|
|
}
|
|
os.encode(ow)
|
|
}
|
|
if (pkt.flags & flag_signature_included) != 0U {
|
|
if zero_sig_len > 0 {
|
|
ow.write_bytes(Bytes::make(zero_sig_len, b'\x00'))
|
|
} else {
|
|
let sig = match pkt.signature {
|
|
Some(s) => s
|
|
None =>
|
|
raise InvalidFormat("SIGNATURE_INCLUDED flag set without signature")
|
|
}
|
|
ow.write_bytes(sig)
|
|
}
|
|
}
|
|
ow.to_bytes()
|
|
}
|
|
|
|
///|
|
|
fn encode_with_opt_zero_sig(
|
|
pkt : StreamingPacket,
|
|
zero_sig_len~ : Int32 = -1,
|
|
) -> Bytes raise StreamingError {
|
|
let opts = serialize_options(pkt, zero_sig_len~)
|
|
if pkt.nacks.length() > 255 {
|
|
raise InvalidLength("StreamingPacket nacks exceed 255")
|
|
}
|
|
let w = @encoding.Writer::new(
|
|
capacity=22 + pkt.nacks.length() * 4 + opts.length() + pkt.payload.length(),
|
|
)
|
|
w.write_u32_be(pkt.send_stream_id)
|
|
w.write_u32_be(pkt.recv_stream_id)
|
|
w.write_u32_be(pkt.sequence_num)
|
|
w.write_u32_be(pkt.ack_through)
|
|
w.write_u8(pkt.nacks.length().to_byte())
|
|
for n in pkt.nacks {
|
|
w.write_u32_be(n)
|
|
}
|
|
w.write_u8(pkt.resend_delay)
|
|
w.write_u16_be(pkt.flags)
|
|
w.write_u16_be(opts.length().reinterpret_as_uint())
|
|
w.write_bytes(opts)
|
|
w.write_bytes(pkt.payload)
|
|
w.to_bytes()
|
|
}
|
|
|
|
///|
|
|
pub fn StreamingPacket::encode(
|
|
self : StreamingPacket,
|
|
) -> Bytes raise StreamingError {
|
|
encode_with_opt_zero_sig(self)
|
|
}
|
|
|
|
///|
|
|
/// Signs the `StreamingPacket` using an Ed25519 32-byte `seed` (over the full packet
|
|
/// with the 64-byte signature slot zeroed out).
|
|
pub fn StreamingPacket::sign_ed25519(
|
|
self : StreamingPacket,
|
|
seed : Bytes,
|
|
) -> StreamingPacket raise StreamingError {
|
|
let with_flag : StreamingPacket = {
|
|
send_stream_id: self.send_stream_id,
|
|
recv_stream_id: self.recv_stream_id,
|
|
sequence_num: self.sequence_num,
|
|
ack_through: self.ack_through,
|
|
nacks: self.nacks,
|
|
resend_delay: self.resend_delay,
|
|
flags: self.flags | flag_signature_included,
|
|
delay_requested: self.delay_requested,
|
|
from_dest: self.from_dest,
|
|
max_packet_size: self.max_packet_size,
|
|
offline_sig: self.offline_sig,
|
|
signature: Some(Bytes::make(64, b'\x00')),
|
|
payload: self.payload,
|
|
}
|
|
let to_sign = encode_with_opt_zero_sig(with_flag, zero_sig_len=64)
|
|
let sig = @crypto.ed25519_sign(seed, to_sign) catch { e => raise Crypto(e) }
|
|
{
|
|
send_stream_id: with_flag.send_stream_id,
|
|
recv_stream_id: with_flag.recv_stream_id,
|
|
sequence_num: with_flag.sequence_num,
|
|
ack_through: with_flag.ack_through,
|
|
nacks: with_flag.nacks,
|
|
resend_delay: with_flag.resend_delay,
|
|
flags: with_flag.flags,
|
|
delay_requested: with_flag.delay_requested,
|
|
from_dest: with_flag.from_dest,
|
|
max_packet_size: with_flag.max_packet_size,
|
|
offline_sig: with_flag.offline_sig,
|
|
signature: Some(sig),
|
|
payload: with_flag.payload,
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Verifies the Ed25519 signature of a `StreamingPacket` directly against a 32-byte
|
|
/// Ed25519 public key (used for follow-on packets signed by a cached offline transient key).
|
|
pub fn StreamingPacket::verify_signature_with_key(
|
|
self : StreamingPacket,
|
|
signing_pub : Bytes,
|
|
) -> Bool {
|
|
if (self.flags & flag_signature_included) == 0U || signing_pub.length() != 32 {
|
|
return false
|
|
}
|
|
let sig = match self.signature {
|
|
Some(s) => s
|
|
None => return false
|
|
}
|
|
let to_verify = encode_with_opt_zero_sig(self, zero_sig_len=sig.length()) catch {
|
|
_ => return false
|
|
}
|
|
@crypto.ed25519_verify(signing_pub, to_verify, sig)
|
|
}
|
|
|
|
///|
|
|
/// Verifies the Ed25519 signature of a `StreamingPacket` against `sender_dest`
|
|
/// (or the embedded `offline_sig.transient_pub_key` when `FLAG_OFFLINE_SIGNATURE` is set).
|
|
pub fn StreamingPacket::verify_signature(
|
|
self : StreamingPacket,
|
|
sender_dest : @common.Destination,
|
|
) -> Bool {
|
|
if (self.flags & flag_signature_included) == 0U {
|
|
return false
|
|
}
|
|
let sig = match self.signature {
|
|
Some(s) => s
|
|
None => return false
|
|
}
|
|
let verify_key = if (self.flags & flag_offline_signature) != 0U {
|
|
match self.offline_sig {
|
|
Some(os) => {
|
|
if os.transient_sig_type != EdDsaSha512Ed25519 ||
|
|
sender_dest.inner.signing_type() != EdDsaSha512Ed25519 {
|
|
return false
|
|
}
|
|
if !os.verify_ed25519(sender_dest.inner.signing_public_key()) {
|
|
return false
|
|
}
|
|
os.transient_pub_key
|
|
}
|
|
None => return false
|
|
}
|
|
} else {
|
|
if sender_dest.inner.signing_type() != EdDsaSha512Ed25519 {
|
|
return false
|
|
}
|
|
sender_dest.inner.signing_public_key()
|
|
}
|
|
let to_verify = encode_with_opt_zero_sig(self, zero_sig_len=sig.length()) catch {
|
|
_ => return false
|
|
}
|
|
@crypto.ed25519_verify(verify_key, to_verify, sig)
|
|
}
|
|
|
|
///|
|
|
/// Decodes a canonical I2P Streaming Packet.
|
|
/// Pass `peer_sig_type` if `FROM_INCLUDED` is not set in the packet so the signature length is known.
|
|
pub fn StreamingPacket::decode(
|
|
data : Bytes,
|
|
peer_sig_type~ : @common.SigningPublicKeyType = EdDsaSha512Ed25519,
|
|
) -> StreamingPacket raise StreamingError {
|
|
if data.length() < 22 {
|
|
raise InvalidLength(
|
|
"StreamingPacket shorter than 22 bytes: \{data.length()}",
|
|
)
|
|
}
|
|
let r = @encoding.Reader::new(data)
|
|
let send_stream_id = r.read_u32_be() catch { e => raise Encoding(e) }
|
|
let recv_stream_id = r.read_u32_be() catch { e => raise Encoding(e) }
|
|
let sequence_num = r.read_u32_be() catch { e => raise Encoding(e) }
|
|
let ack_through = r.read_u32_be() catch { e => raise Encoding(e) }
|
|
let nack_count = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
|
|
let nacks : Array[UInt32] = Array(capacity=nack_count)
|
|
for _i = 0; _i < nack_count; _i = _i + 1 {
|
|
nacks.push(r.read_u32_be() catch { e => raise Encoding(e) })
|
|
}
|
|
let resend_delay = r.read_u8() catch { e => raise Encoding(e) }
|
|
let flags = r.read_u16_be() catch { e => raise Encoding(e) }
|
|
let opt_size = (r.read_u16_be() catch { e => raise Encoding(e) }).reinterpret_as_int()
|
|
let opt_bytes = r.read_bytes(opt_size) catch { e => raise Encoding(e) }
|
|
let payload = r.read_remaining()
|
|
|
|
let or = @encoding.Reader::new(opt_bytes)
|
|
let delay_requested = if (flags & flag_delay_requested) != 0U {
|
|
Some(or.read_u16_be() catch { e => raise Encoding(e) })
|
|
} else {
|
|
None
|
|
}
|
|
let from_dest = if (flags & flag_from_included) != 0U {
|
|
Some(@common.Destination::decode(or) catch { e => raise Common(e) })
|
|
} else {
|
|
None
|
|
}
|
|
let max_packet_size = if (flags & flag_max_packet_size_included) != 0U {
|
|
Some(or.read_u16_be() catch { e => raise Encoding(e) })
|
|
} else {
|
|
None
|
|
}
|
|
let effective_sig_type = match from_dest {
|
|
Some(d) => d.inner.signing_type()
|
|
None => peer_sig_type
|
|
}
|
|
let offline_sig = if (flags & flag_offline_signature) != 0U {
|
|
Some(
|
|
@common.OfflineSignature::decode(or, effective_sig_type) catch {
|
|
e => raise Common(e)
|
|
},
|
|
)
|
|
} else {
|
|
None
|
|
}
|
|
let signature = if (flags & flag_signature_included) != 0U {
|
|
let sig_len = match offline_sig {
|
|
Some(os) => os.transient_sig_type.signature_length()
|
|
None => effective_sig_type.signature_length()
|
|
}
|
|
if sig_len <= 0 {
|
|
Some(or.read_remaining())
|
|
} else {
|
|
Some(or.read_bytes(sig_len) catch { e => raise Encoding(e) })
|
|
}
|
|
} else {
|
|
None
|
|
}
|
|
{
|
|
send_stream_id,
|
|
recv_stream_id,
|
|
sequence_num,
|
|
ack_through,
|
|
nacks,
|
|
resend_delay,
|
|
flags,
|
|
delay_requested,
|
|
from_dest,
|
|
max_packet_size,
|
|
offline_sig,
|
|
signature,
|
|
payload,
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Wraps a `StreamingPacket` inside an I2CP Gzip envelope (`protocol = 6`).
|
|
pub fn encode_i2cp_streaming(
|
|
pkt : StreamingPacket,
|
|
from_port~ : UInt32 = 0U,
|
|
to_port~ : UInt32 = 0U,
|
|
) -> Bytes raise StreamingError {
|
|
let raw = pkt.encode()
|
|
@encoding.i2cp_gzip_wrap(raw, from_port~, to_port~, protocol=b'\x06')
|
|
}
|
|
|
|
///|
|
|
/// Unwraps an I2CP Gzip envelope and decodes a `StreamingPacket` (verifying `protocol == 6`).
|
|
pub fn decode_i2cp_streaming(
|
|
i2cp_bytes : Bytes,
|
|
peer_sig_type~ : @common.SigningPublicKeyType = EdDsaSha512Ed25519,
|
|
) -> (@encoding.I2cpGzipHeader, StreamingPacket) raise StreamingError {
|
|
let (hdr, raw) = @encoding.i2cp_gzip_unwrap(i2cp_bytes) catch {
|
|
e => raise Encoding(e)
|
|
}
|
|
if hdr.protocol != b'\x06' {
|
|
raise InvalidFormat(
|
|
"expected I2CP protocol 6 (Streaming), got \{hdr.protocol.to_int()}",
|
|
)
|
|
}
|
|
(hdr, StreamingPacket::decode(raw, peer_sig_type~))
|
|
}
|