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
@@ -97,6 +97,7 @@ test "LeaseSet2 encode and decode round-trip" {
|
||||
published_sec: 1700000000U,
|
||||
expires_offset: 600U,
|
||||
flags: 0U,
|
||||
offline_sig: None,
|
||||
options: @common.Mapping::new(),
|
||||
encryption_keys: [{ key_type: X25519, key_data: Bytes::make(32, b'\xdd'), }],
|
||||
leases: [lease],
|
||||
@@ -173,6 +174,7 @@ test "LeaseSet2 Ed25519 sign, encode/decode round-trip, and verify" {
|
||||
published_sec: 1700000000U,
|
||||
expires_offset: 600U,
|
||||
flags: 0U,
|
||||
offline_sig: None,
|
||||
options: @common.Mapping::new(),
|
||||
encryption_keys: [{ key_type: X25519, key_data: x_pub, }],
|
||||
leases: [{ tunnel_gw: gw, tunnel_id: 777U, end_date_sec: 1700000600U, }],
|
||||
@@ -193,6 +195,7 @@ test "LeaseSet2 Ed25519 sign, encode/decode round-trip, and verify" {
|
||||
published_sec: decoded.published_sec + 1U,
|
||||
expires_offset: decoded.expires_offset,
|
||||
flags: decoded.flags,
|
||||
offline_sig: decoded.offline_sig,
|
||||
options: decoded.options,
|
||||
encryption_keys: decoded.encryption_keys,
|
||||
leases: decoded.leases,
|
||||
@@ -200,3 +203,204 @@ test "LeaseSet2 Ed25519 sign, encode/decode round-trip, and verify" {
|
||||
}
|
||||
assert_eq(tampered.verify_signature(), false)
|
||||
}
|
||||
|
||||
///|
|
||||
fn write_u16_le_test(out : Array[Byte], v : UInt32) -> Unit {
|
||||
out.push((v & 0xFFU).to_byte())
|
||||
out.push(((v >> 8) & 0xFFU).to_byte())
|
||||
}
|
||||
|
||||
///|
|
||||
fn write_u32_le_test(out : Array[Byte], v : UInt32) -> Unit {
|
||||
out.push((v & 0xFFU).to_byte())
|
||||
out.push(((v >> 8) & 0xFFU).to_byte())
|
||||
out.push(((v >> 16) & 0xFFU).to_byte())
|
||||
out.push(((v >> 24) & 0xFFU).to_byte())
|
||||
}
|
||||
|
||||
///|
|
||||
fn build_stored_zip(entries : Array[(String, Bytes)]) -> Bytes {
|
||||
let out : Array[Byte] = []
|
||||
let cd_entries : Array[(Bytes, UInt32, Int32, Int32)] = []
|
||||
for item in entries {
|
||||
let (name, data) = item
|
||||
let name_bytes = @encoding.string_to_utf8_bytes(name)
|
||||
let local_off = out.length()
|
||||
let csum = @encoding.crc32(data)
|
||||
// Local file header PK\x03\x04
|
||||
out.push(b'\x50')
|
||||
out.push(b'\x4b')
|
||||
out.push(b'\x03')
|
||||
out.push(b'\x04')
|
||||
write_u16_le_test(out, 20U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U) // stored
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u32_le_test(out, csum)
|
||||
write_u32_le_test(out, data.length().reinterpret_as_uint())
|
||||
write_u32_le_test(out, data.length().reinterpret_as_uint())
|
||||
write_u16_le_test(out, name_bytes.length().reinterpret_as_uint())
|
||||
write_u16_le_test(out, 0U)
|
||||
for i = 0; i < name_bytes.length(); i = i + 1 {
|
||||
out.push(name_bytes[i])
|
||||
}
|
||||
for i = 0; i < data.length(); i = i + 1 {
|
||||
out.push(data[i])
|
||||
}
|
||||
cd_entries.push((name_bytes, csum, data.length(), local_off))
|
||||
}
|
||||
let cd_off = out.length()
|
||||
for rec in cd_entries {
|
||||
let (name_bytes, csum, sz, local_off) = rec
|
||||
out.push(b'\x50')
|
||||
out.push(b'\x4b')
|
||||
out.push(b'\x01')
|
||||
out.push(b'\x02')
|
||||
write_u16_le_test(out, 20U)
|
||||
write_u16_le_test(out, 20U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u32_le_test(out, csum)
|
||||
write_u32_le_test(out, sz.reinterpret_as_uint())
|
||||
write_u32_le_test(out, sz.reinterpret_as_uint())
|
||||
write_u16_le_test(out, name_bytes.length().reinterpret_as_uint())
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u32_le_test(out, 0U)
|
||||
write_u32_le_test(out, local_off.reinterpret_as_uint())
|
||||
for i = 0; i < name_bytes.length(); i = i + 1 {
|
||||
out.push(name_bytes[i])
|
||||
}
|
||||
}
|
||||
let cd_size = out.length() - cd_off
|
||||
// EOCD PK\x05\x06
|
||||
out.push(b'\x50')
|
||||
out.push(b'\x4b')
|
||||
out.push(b'\x05')
|
||||
out.push(b'\x06')
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, 0U)
|
||||
write_u16_le_test(out, cd_entries.length().reinterpret_as_uint())
|
||||
write_u16_le_test(out, cd_entries.length().reinterpret_as_uint())
|
||||
write_u32_le_test(out, cd_size.reinterpret_as_uint())
|
||||
write_u32_le_test(out, cd_off.reinterpret_as_uint())
|
||||
write_u16_le_test(out, 0U)
|
||||
Bytes::from_array(out)
|
||||
}
|
||||
|
||||
///|
|
||||
test "LeaseSet2 with OfflineSignature (flags & 1) encode/decode and verify" {
|
||||
let master_seed = Bytes::make(32, b'\x15')
|
||||
let master_ed = @crypto.ed25519_public_key(master_seed)
|
||||
let transient_seed = Bytes::make(32, b'\x26')
|
||||
let transient_ed = @crypto.ed25519_public_key(transient_seed)
|
||||
let x_pub = @crypto.x25519_public_key(Bytes::make(32, b'\x37'))
|
||||
let dest = @common.Destination::new_x25519_ed25519(x_pub, master_ed)
|
||||
|
||||
let off_sig : @common.OfflineSignature = {
|
||||
expires_sec: 1700086400U,
|
||||
transient_sig_type: EdDsaSha512Ed25519,
|
||||
transient_pub_key: transient_ed,
|
||||
signature: b"",
|
||||
}
|
||||
let signed_off = off_sig.sign_ed25519(master_seed)
|
||||
let ls2 : @common.LeaseSet2 = {
|
||||
destination: dest,
|
||||
published_sec: 1700000000U,
|
||||
expires_offset: 600U,
|
||||
flags: 1U,
|
||||
offline_sig: Some(signed_off),
|
||||
options: @common.Mapping::new(),
|
||||
encryption_keys: [{ key_type: X25519, key_data: x_pub, }],
|
||||
leases: [
|
||||
{
|
||||
tunnel_gw: @common.Hash::sha256(b"off-gw"),
|
||||
tunnel_id: 999U,
|
||||
end_date_sec: 1700000600U,
|
||||
},
|
||||
],
|
||||
signature: b"",
|
||||
}
|
||||
let signed = ls2.sign_with_transient_ed25519(transient_seed)
|
||||
assert_eq(signed.verify_signature(), true)
|
||||
|
||||
let decoded = @common.LeaseSet2::decode(
|
||||
@encoding.Reader::new(signed.to_bytes()),
|
||||
)
|
||||
assert_eq(decoded, signed)
|
||||
assert_eq(decoded.verify_signature(), true)
|
||||
|
||||
// Tampering offline signature fails verification
|
||||
let bad_off : @common.OfflineSignature = {
|
||||
expires_sec: signed_off.expires_sec + 1U,
|
||||
transient_sig_type: signed_off.transient_sig_type,
|
||||
transient_pub_key: signed_off.transient_pub_key,
|
||||
signature: signed_off.signature,
|
||||
}
|
||||
let bad_ls2 : @common.LeaseSet2 = {
|
||||
destination: decoded.destination,
|
||||
published_sec: decoded.published_sec,
|
||||
expires_offset: decoded.expires_offset,
|
||||
flags: decoded.flags,
|
||||
offline_sig: Some(bad_off),
|
||||
options: decoded.options,
|
||||
encryption_keys: decoded.encryption_keys,
|
||||
leases: decoded.leases,
|
||||
signature: decoded.signature,
|
||||
}
|
||||
assert_eq(bad_ls2.verify_signature(), false)
|
||||
}
|
||||
|
||||
///|
|
||||
test "SU3 reseed bundle decodes and verifies embedded RouterInfo" {
|
||||
let ed_seed = Bytes::make(32, b'\x41')
|
||||
let ed_pub = @crypto.ed25519_public_key(ed_seed)
|
||||
let x_pub = @crypto.x25519_public_key(Bytes::make(32, b'\x42'))
|
||||
let id = @common.RouterIdentity::new_x25519_ed25519(x_pub, ed_pub)
|
||||
let ri : @common.RouterInfo = {
|
||||
identity: id,
|
||||
published: 1700000000000UL,
|
||||
addresses: [],
|
||||
options: @common.Mapping::from_entries([("caps", "XfR"), ("netId", "2")]),
|
||||
signature: b"",
|
||||
}
|
||||
let signed_ri = ri.sign_ed25519(ed_seed)
|
||||
let zip = build_stored_zip([
|
||||
("routerInfo-\{id.hash().to_base64()}.dat", signed_ri.to_bytes()),
|
||||
])
|
||||
|
||||
let ver = b"1700000000000000"
|
||||
let signer = b"test@mail.i2p"
|
||||
let sig = Bytes::make(64, b'\xaa')
|
||||
let w = @encoding.Writer::new()
|
||||
w.write_bytes(b"I2Psu3")
|
||||
w.write_u8(b'\x00')
|
||||
w.write_u8(b'\x00')
|
||||
w.write_u16_be(8U) // Ed25519ph
|
||||
w.write_u16_be(sig.length().reinterpret_as_uint())
|
||||
w.write_u8(b'\x00')
|
||||
w.write_u8(ver.length().to_byte())
|
||||
w.write_u8(b'\x00')
|
||||
w.write_u8(signer.length().to_byte())
|
||||
w.write_u64_be(zip.length().to_uint64())
|
||||
w.write_u8(b'\x00')
|
||||
w.write_u8(b'\x00') // ZIP
|
||||
w.write_u8(b'\x00')
|
||||
w.write_u8(b'\x03') // Reseed
|
||||
for _i = 0; _i < 12; _i = _i + 1 {
|
||||
w.write_u8(b'\x00')
|
||||
}
|
||||
w.write_bytes(ver)
|
||||
w.write_bytes(signer)
|
||||
w.write_bytes(zip)
|
||||
w.write_bytes(sig)
|
||||
|
||||
let peers = @common.parse_reseed_su3(w.to_bytes())
|
||||
assert_eq(peers.length(), 1)
|
||||
assert_eq(peers[0], signed_ri)
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
}
|
||||
|
||||
// Values
|
||||
pub fn parse_reseed_su3(Bytes) -> Array[RouterInfo] raise CommonError
|
||||
|
||||
// Errors
|
||||
#derive(Eq, @debug.Debug)
|
||||
@@ -121,6 +122,7 @@ pub(all) struct LeaseSet2 {
|
||||
published_sec : UInt32
|
||||
expires_offset : UInt32
|
||||
flags : UInt32
|
||||
offline_sig : OfflineSignature?
|
||||
options : Mapping
|
||||
encryption_keys : Array[LeaseSet2EncryptionKey]
|
||||
leases : Array[Lease2]
|
||||
@@ -129,6 +131,7 @@ pub(all) struct LeaseSet2 {
|
||||
pub fn LeaseSet2::decode(@encoding.Reader) -> Self raise CommonError
|
||||
pub fn LeaseSet2::encode(Self, @encoding.Writer) -> Unit raise CommonError
|
||||
pub fn LeaseSet2::sign_ed25519(Self, Bytes) -> Self raise CommonError
|
||||
pub fn LeaseSet2::sign_with_transient_ed25519(Self, Bytes) -> Self raise CommonError
|
||||
pub fn LeaseSet2::signing_bytes(Self) -> Bytes raise CommonError
|
||||
pub fn LeaseSet2::to_bytes(Self) -> Bytes raise CommonError
|
||||
pub fn LeaseSet2::unsigned_bytes(Self) -> Bytes raise CommonError
|
||||
@@ -152,6 +155,19 @@ pub fn Mapping::length(Self) -> Int32
|
||||
pub fn Mapping::new() -> Self
|
||||
pub fn Mapping::set(Self, String, String) -> Unit
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) struct OfflineSignature {
|
||||
expires_sec : UInt32
|
||||
transient_sig_type : SigningPublicKeyType
|
||||
transient_pub_key : Bytes
|
||||
signature : Bytes
|
||||
}
|
||||
pub fn OfflineSignature::decode(@encoding.Reader, SigningPublicKeyType) -> Self raise CommonError
|
||||
pub fn OfflineSignature::encode(Self, @encoding.Writer) -> Unit
|
||||
pub fn OfflineSignature::sign_ed25519(Self, Bytes) -> Self raise CommonError
|
||||
pub fn OfflineSignature::unsigned_bytes(Self) -> Bytes
|
||||
pub fn OfflineSignature::verify_ed25519(Self, Bytes) -> Bool
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) struct RouterAddress {
|
||||
cost : Byte
|
||||
@@ -224,6 +240,20 @@ pub fn SigningPublicKeyType::public_key_length(Self) -> Int32
|
||||
pub fn SigningPublicKeyType::signature_length(Self) -> Int32
|
||||
pub fn SigningPublicKeyType::to_u16(Self) -> UInt32
|
||||
|
||||
#derive(Eq, @debug.Debug)
|
||||
pub(all) struct Su3File {
|
||||
format_version : Byte
|
||||
sig_type : UInt32
|
||||
version : String
|
||||
signer_id : String
|
||||
file_type : Byte
|
||||
content_type : Byte
|
||||
content : Bytes
|
||||
signature : Bytes
|
||||
signed_bytes_len : Int32
|
||||
}
|
||||
pub fn Su3File::decode(Bytes) -> Self raise CommonError
|
||||
|
||||
// Type aliases
|
||||
|
||||
// Traits
|
||||
+167
-8
@@ -206,6 +206,94 @@ pub(all) struct LeaseSet2EncryptionKey {
|
||||
key_data : Bytes
|
||||
}
|
||||
|
||||
///|
|
||||
/// Offline signature block inside a `LeaseSet2` (Proposal 123, `flags & 1 != 0`):
|
||||
/// allows a transient signing key to sign the LeaseSet2 while the Destination's
|
||||
/// long-term signing key stays offline.
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct OfflineSignature {
|
||||
expires_sec : UInt32
|
||||
transient_sig_type : SigningPublicKeyType
|
||||
transient_pub_key : Bytes
|
||||
signature : Bytes
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn OfflineSignature::unsigned_bytes(self : OfflineSignature) -> Bytes {
|
||||
let w = @encoding.Writer::new(capacity=6 + self.transient_pub_key.length())
|
||||
w.write_u32_be(self.expires_sec)
|
||||
w.write_u16_be(self.transient_sig_type.to_u16())
|
||||
w.write_bytes(self.transient_pub_key)
|
||||
w.to_bytes()
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn OfflineSignature::encode(
|
||||
self : OfflineSignature,
|
||||
w : @encoding.Writer,
|
||||
) -> Unit {
|
||||
w.write_bytes(self.unsigned_bytes())
|
||||
w.write_bytes(self.signature)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn OfflineSignature::decode(
|
||||
r : @encoding.Reader,
|
||||
dest_sig_type : SigningPublicKeyType,
|
||||
) -> OfflineSignature raise CommonError {
|
||||
let expires_sec = r.read_u32_be() catch { e => raise Encoding(e) }
|
||||
let transient_sig_type = SigningPublicKeyType::from_u16(
|
||||
r.read_u16_be() catch {
|
||||
e => raise Encoding(e)
|
||||
},
|
||||
)
|
||||
let tkey_len = transient_sig_type.public_key_length()
|
||||
if tkey_len <= 0 {
|
||||
raise InvalidFormat(
|
||||
"unsupported transient signing type in OfflineSignature",
|
||||
)
|
||||
}
|
||||
let transient_pub_key = r.read_bytes(tkey_len) catch {
|
||||
e => raise Encoding(e)
|
||||
}
|
||||
let dsig_len = dest_sig_type.signature_length()
|
||||
if dsig_len <= 0 {
|
||||
raise InvalidFormat(
|
||||
"unsupported destination signing type in OfflineSignature",
|
||||
)
|
||||
}
|
||||
let signature = r.read_bytes(dsig_len) catch { e => raise Encoding(e) }
|
||||
{ expires_sec, transient_sig_type, transient_pub_key, signature, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn OfflineSignature::sign_ed25519(
|
||||
self : OfflineSignature,
|
||||
dest_seed : Bytes,
|
||||
) -> OfflineSignature raise CommonError {
|
||||
let sig = @crypto.ed25519_sign(dest_seed, self.unsigned_bytes()) catch {
|
||||
e => raise Crypto(e)
|
||||
}
|
||||
{
|
||||
expires_sec: self.expires_sec,
|
||||
transient_sig_type: self.transient_sig_type,
|
||||
transient_pub_key: self.transient_pub_key,
|
||||
signature: sig,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn OfflineSignature::verify_ed25519(
|
||||
self : OfflineSignature,
|
||||
dest_signing_pub : Bytes,
|
||||
) -> Bool {
|
||||
@crypto.ed25519_verify(
|
||||
dest_signing_pub,
|
||||
self.unsigned_bytes(),
|
||||
self.signature,
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Modern LeaseSet2 (I2P Proposal 123, NetDB type 3).
|
||||
#derive(Eq, Debug)
|
||||
@@ -213,7 +301,8 @@ pub(all) struct LeaseSet2 {
|
||||
destination : Destination
|
||||
published_sec : UInt32
|
||||
expires_offset : UInt32 // uint16 offset in seconds from published_sec
|
||||
flags : UInt32 // uint16
|
||||
flags : UInt32 // uint16 (bit 0 = offline keys present)
|
||||
offline_sig : OfflineSignature?
|
||||
options : Mapping
|
||||
encryption_keys : Array[LeaseSet2EncryptionKey]
|
||||
leases : Array[Lease2]
|
||||
@@ -228,6 +317,15 @@ pub fn LeaseSet2::unsigned_bytes(self : LeaseSet2) -> Bytes raise CommonError {
|
||||
w.write_u32_be(self.published_sec)
|
||||
w.write_u16_be(self.expires_offset)
|
||||
w.write_u16_be(self.flags)
|
||||
if (self.flags & 0x0001U) != 0U {
|
||||
match self.offline_sig {
|
||||
Some(os) => os.encode(w)
|
||||
None =>
|
||||
raise InvalidFormat(
|
||||
"LeaseSet2 offline signature flag set without offline_sig",
|
||||
)
|
||||
}
|
||||
}
|
||||
self.options.encode(w)
|
||||
w.write_u8(self.encryption_keys.length().to_byte())
|
||||
for ek in self.encryption_keys {
|
||||
@@ -289,6 +387,7 @@ pub fn LeaseSet2::sign_ed25519(
|
||||
published_sec: self.published_sec,
|
||||
expires_offset: self.expires_offset,
|
||||
flags: self.flags,
|
||||
offline_sig: self.offline_sig,
|
||||
options: self.options,
|
||||
encryption_keys: self.encryption_keys,
|
||||
leases: self.leases,
|
||||
@@ -297,17 +396,68 @@ pub fn LeaseSet2::sign_ed25519(
|
||||
}
|
||||
|
||||
///|
|
||||
/// Verifies the `LeaseSet2` Ed25519 signature against `self.destination.inner.signing_public_key()`.
|
||||
/// Signs a `LeaseSet2` that carries an `OfflineSignature` (`flags & 1 != 0`)
|
||||
/// using the transient Ed25519 signing seed.
|
||||
pub fn LeaseSet2::sign_with_transient_ed25519(
|
||||
self : LeaseSet2,
|
||||
transient_seed : Bytes,
|
||||
) -> LeaseSet2 raise CommonError {
|
||||
if (self.flags & 0x0001U) == 0U {
|
||||
raise InvalidFormat("LeaseSet2 offline signature flag (bit 0) must be set")
|
||||
}
|
||||
match self.offline_sig {
|
||||
Some(os) =>
|
||||
if os.transient_sig_type != EdDsaSha512Ed25519 {
|
||||
raise InvalidFormat(
|
||||
"LeaseSet2 transient signing type must be EdDsaSha512Ed25519",
|
||||
)
|
||||
}
|
||||
None => raise InvalidFormat("LeaseSet2 offline_sig is missing")
|
||||
}
|
||||
let to_sign = self.signing_bytes()
|
||||
let signature = @crypto.ed25519_sign(transient_seed, to_sign) catch {
|
||||
e => raise Crypto(e)
|
||||
}
|
||||
{
|
||||
destination: self.destination,
|
||||
published_sec: self.published_sec,
|
||||
expires_offset: self.expires_offset,
|
||||
flags: self.flags,
|
||||
offline_sig: self.offline_sig,
|
||||
options: self.options,
|
||||
encryption_keys: self.encryption_keys,
|
||||
leases: self.leases,
|
||||
signature,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Verifies the `LeaseSet2` Ed25519 signature (including any `OfflineSignature` chain).
|
||||
pub fn LeaseSet2::verify_signature(self : LeaseSet2) -> Bool {
|
||||
if self.destination.inner.signing_type() != EdDsaSha512Ed25519 {
|
||||
return false
|
||||
}
|
||||
let to_sign = self.signing_bytes() catch { _ => return false }
|
||||
@crypto.ed25519_verify(
|
||||
self.destination.inner.signing_public_key(),
|
||||
to_sign,
|
||||
self.signature,
|
||||
)
|
||||
if (self.flags & 0x0001U) != 0U {
|
||||
match self.offline_sig {
|
||||
Some(os) => {
|
||||
if os.transient_sig_type != EdDsaSha512Ed25519 {
|
||||
return false
|
||||
}
|
||||
if !os.verify_ed25519(self.destination.inner.signing_public_key()) {
|
||||
return false
|
||||
}
|
||||
@crypto.ed25519_verify(os.transient_pub_key, to_sign, self.signature)
|
||||
}
|
||||
None => false
|
||||
}
|
||||
} else {
|
||||
@crypto.ed25519_verify(
|
||||
self.destination.inner.signing_public_key(),
|
||||
to_sign,
|
||||
self.signature,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
@@ -316,6 +466,11 @@ pub fn LeaseSet2::decode(r : @encoding.Reader) -> LeaseSet2 raise CommonError {
|
||||
let published_sec = r.read_u32_be() catch { e => raise Encoding(e) }
|
||||
let expires_offset = r.read_u16_be() catch { e => raise Encoding(e) }
|
||||
let flags = r.read_u16_be() catch { e => raise Encoding(e) }
|
||||
let offline_sig = if (flags & 0x0001U) != 0U {
|
||||
Some(OfflineSignature::decode(r, destination.inner.signing_type()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
let options = Mapping::decode(r)
|
||||
let key_count = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
|
||||
let encryption_keys : Array[LeaseSet2EncryptionKey] = Array(
|
||||
@@ -336,7 +491,10 @@ pub fn LeaseSet2::decode(r : @encoding.Reader) -> LeaseSet2 raise CommonError {
|
||||
for _i = 0; _i < lease_count; _i = _i + 1 {
|
||||
leases.push(Lease2::decode(r))
|
||||
}
|
||||
let sig_len = destination.inner.signing_type().signature_length()
|
||||
let sig_len = match offline_sig {
|
||||
Some(os) => os.transient_sig_type.signature_length()
|
||||
None => destination.inner.signing_type().signature_length()
|
||||
}
|
||||
let signature = if sig_len > 0 {
|
||||
r.read_bytes(sig_len) catch {
|
||||
e => raise Encoding(e)
|
||||
@@ -349,6 +507,7 @@ pub fn LeaseSet2::decode(r : @encoding.Reader) -> LeaseSet2 raise CommonError {
|
||||
published_sec,
|
||||
expires_offset,
|
||||
flags,
|
||||
offline_sig,
|
||||
options,
|
||||
encryption_keys,
|
||||
leases,
|
||||
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
///|
|
||||
/// I2P SU3 signed container parser & reseed bundle (`i2pseeds.su3`) extractor.
|
||||
|
||||
///|
|
||||
#derive(Eq, Debug)
|
||||
pub(all) struct Su3File {
|
||||
format_version : Byte
|
||||
sig_type : UInt32
|
||||
version : String
|
||||
signer_id : String
|
||||
file_type : Byte // 0 = ZIP, 1 = XML, 2 = HTML, 3 = XML.GZ
|
||||
content_type : Byte // 1 = router update, 2 = plugin, 3 = reseed, 4 = news, 5 = blocklist
|
||||
content : Bytes
|
||||
signature : Bytes
|
||||
signed_bytes_len : Int32
|
||||
}
|
||||
|
||||
///|
|
||||
/// Decodes an I2P SU3 file header, payload content, and trailing signature.
|
||||
pub fn Su3File::decode(data : Bytes) -> Su3File raise CommonError {
|
||||
if data.length() < 40 {
|
||||
raise InvalidLength("SU3 file shorter than 40 bytes: \{data.length()}")
|
||||
}
|
||||
if data[0] != b'\x49' ||
|
||||
data[1] != b'\x32' ||
|
||||
data[2] != b'\x50' ||
|
||||
data[3] != b'\x73' ||
|
||||
data[4] != b'\x75' ||
|
||||
data[5] != b'\x33' {
|
||||
raise InvalidFormat("invalid SU3 magic header (expected 'I2Psu3')")
|
||||
}
|
||||
let r = @encoding.Reader::new(data)
|
||||
r.skip(6) catch {
|
||||
e => raise Encoding(e)
|
||||
}
|
||||
let _unused6 = r.read_u8() catch { e => raise Encoding(e) }
|
||||
let format_version = r.read_u8() catch { e => raise Encoding(e) }
|
||||
if format_version != b'\x00' {
|
||||
raise InvalidFormat(
|
||||
"unsupported SU3 format version: \{format_version.to_int()}",
|
||||
)
|
||||
}
|
||||
let sig_type = r.read_u16_be() catch { e => raise Encoding(e) }
|
||||
let sig_len = (r.read_u16_be() catch { e => raise Encoding(e) }).reinterpret_as_int()
|
||||
let _unused12 = r.read_u8() catch { e => raise Encoding(e) }
|
||||
let version_len = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
|
||||
let _unused14 = r.read_u8() catch { e => raise Encoding(e) }
|
||||
let signer_id_len = (r.read_u8() catch { e => raise Encoding(e) }).to_int()
|
||||
let content_len_u64 = r.read_u64_be() catch { e => raise Encoding(e) }
|
||||
if content_len_u64 > 64UL * 1024UL * 1024UL {
|
||||
raise InvalidLength("SU3 content length too large")
|
||||
}
|
||||
let content_len = content_len_u64.to_int()
|
||||
let _unused24 = r.read_u8() catch { e => raise Encoding(e) }
|
||||
let file_type = r.read_u8() catch { e => raise Encoding(e) }
|
||||
let _unused26 = r.read_u8() catch { e => raise Encoding(e) }
|
||||
let content_type = r.read_u8() catch { e => raise Encoding(e) }
|
||||
r.skip(12) catch {
|
||||
e => raise Encoding(e)
|
||||
}
|
||||
let version_bytes = r.read_bytes(version_len) catch { e => raise Encoding(e) }
|
||||
let version = @encoding.utf8_bytes_to_string(version_bytes) catch {
|
||||
e => raise Encoding(e)
|
||||
}
|
||||
let signer_bytes = r.read_bytes(signer_id_len) catch {
|
||||
e => raise Encoding(e)
|
||||
}
|
||||
let signer_id = @encoding.utf8_bytes_to_string(signer_bytes) catch {
|
||||
e => raise Encoding(e)
|
||||
}
|
||||
let content = r.read_bytes(content_len) catch { e => raise Encoding(e) }
|
||||
let signed_bytes_len = r.position()
|
||||
let signature = r.read_bytes(sig_len) catch { e => raise Encoding(e) }
|
||||
{
|
||||
format_version,
|
||||
sig_type,
|
||||
version,
|
||||
signer_id,
|
||||
file_type,
|
||||
content_type,
|
||||
content,
|
||||
signature,
|
||||
signed_bytes_len,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Extracts and Ed25519-verifies all `routerInfo-*.dat` entries from an I2P reseed `.su3` file.
|
||||
pub fn parse_reseed_su3(data : Bytes) -> Array[RouterInfo] raise CommonError {
|
||||
let su3 = Su3File::decode(data)
|
||||
if su3.file_type != b'\x00' {
|
||||
raise InvalidFormat(
|
||||
"reseed SU3 must contain a ZIP file (type 0), got \{su3.file_type.to_int()}",
|
||||
)
|
||||
}
|
||||
if su3.content_type != b'\x03' {
|
||||
raise InvalidFormat(
|
||||
"reseed SU3 must have content type 3, got \{su3.content_type.to_int()}",
|
||||
)
|
||||
}
|
||||
let files = @encoding.zip_extract_entries(su3.content) catch {
|
||||
e => raise Encoding(e)
|
||||
}
|
||||
let peers : Array[RouterInfo] = []
|
||||
for item in files {
|
||||
let (name, raw) = item
|
||||
if !name.has_suffix(".dat") {
|
||||
continue
|
||||
}
|
||||
let ri = RouterInfo::decode(@encoding.Reader::new(raw)) catch {
|
||||
_ => continue
|
||||
}
|
||||
if ri.verify_signature() {
|
||||
peers.push(ri)
|
||||
}
|
||||
}
|
||||
peers
|
||||
}
|
||||
Reference in new issue
Block a user