From 2d3b96725bd5253fb4eefc2a70101627ffa70dd8 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 9 May 2022 18:24:07 -0400 Subject: [PATCH] Fix a few serialization bugs. --- .../default-rootset/root.zerotier.com.bin | Bin 738 -> 750 bytes .../default-rootset/root.zerotier.com.json | 8 ++++---- zerotier-network-hypervisor/src/util/buffer.rs | 2 +- zerotier-network-hypervisor/src/vl1/identity.rs | 14 ++++++++++++-- zerotier-network-hypervisor/src/vl1/rootset.rs | 4 ++-- zerotier-system-service/src/cli/rootset.rs | 4 ++++ zerotier-system-service/src/main.rs | 5 ++++- 7 files changed, 27 insertions(+), 10 deletions(-) diff --git a/zerotier-network-hypervisor/default-rootset/root.zerotier.com.bin b/zerotier-network-hypervisor/default-rootset/root.zerotier.com.bin index 88ea585a25309b5e2aff348424ee198785ad7a99..d167c3d5004ef63f419e50c0b990d2338a8539e2 100644 GIT binary patch delta 332 zcmV-S0ki(%1?~lqi~<7yk&7Nb`~2cKTNaA1Ng`a%me~0Z?(1iZR4OnUZ!mSX&PzQ2|uoqyyejFFEWKMNE7n1JPY?i->YBJ;;>#lZ(!?Fi3%0^&#HHA{cR%>mru zrR;8jNry_tI@h#L5$>N-9+aPQ_#zYS_q@3Vk?Tv7IRPC&u*n;IMX`SPB8_&J6gCyC z&p}Z*CTBM36^8)E1GPmC{EbzRAp*k!gq#?7R|kGKUQ$lOI_Q~9wPpog+aAdala>KW zU4jHu5jC492?i~U$e)V!ZM_g4-X%s0hit{B!=poLzrt<@1TorPfrVo=q0!ovO0v$wk zuMCEFm9k1WJc|?=jI8N@#;?<_Swpc$ftn%N4w#lXXM4e_6U!{k`zBiiQ{a0=u02Wb Szz9^qn Buffer { let mut a = &self.1[c..]; crate::util::varint::read(&mut a).map(|r| { *cursor = c + r.1; - debug_assert!(*cursor < self.0); + debug_assert!(*cursor <= self.0); r.0 }) } else { diff --git a/zerotier-network-hypervisor/src/vl1/identity.rs b/zerotier-network-hypervisor/src/vl1/identity.rs index 53367a779..defc1a191 100644 --- a/zerotier-network-hypervisor/src/vl1/identity.rs +++ b/zerotier-network-hypervisor/src/vl1/identity.rs @@ -440,6 +440,10 @@ impl Identity { } } + // A size of zero tells unmarshal() to stop. + buf.append_u8(0x03)?; + buf.append_u16(0)?; + Ok(()) } @@ -659,7 +663,8 @@ impl Marshalable for Identity { if algorithm.is_err() { break; } - match algorithm.unwrap() { + let algorithm = algorithm.unwrap(); + match algorithm { 0x00 | IDENTITY_ALGORITHM_X25519 => { let a = buf.read_bytes_fixed::(cursor)?; let b = buf.read_bytes_fixed::(cursor)?; @@ -677,7 +682,12 @@ impl Marshalable for Identity { // This isn't an algorithm; each algorithm is identified by just one bit. This // indicates the total size of the section after the x25519 keys for backward // compatibility. See comments in marshal(). New versions can ignore this field. - *cursor += 2; + let size = buf.read_u16(cursor)?; + if size == 0 { + break; + } else { + *cursor += size as usize; + } } IDENTITY_ALGORITHM_EC_NIST_P384 => { let size = buf.read_u16(cursor)?; diff --git a/zerotier-network-hypervisor/src/vl1/rootset.rs b/zerotier-network-hypervisor/src/vl1/rootset.rs index 49b95b6ee..d35c4a59f 100644 --- a/zerotier-network-hypervisor/src/vl1/rootset.rs +++ b/zerotier-network-hypervisor/src/vl1/rootset.rs @@ -93,7 +93,7 @@ impl RootSet { /// Get the ZeroTier default root set, which contains roots run by ZeroTier Inc. pub fn zerotier_default() -> Self { let mut cursor = 0; - let rs = Self::unmarshal(&Buffer::from(include_bytes!("../../default-rootset/root.zerotier.com.json")), &mut cursor).unwrap(); + let rs = Self::unmarshal(&Buffer::from(include_bytes!("../../default-rootset/root.zerotier.com.bin")), &mut cursor).unwrap(); assert!(rs.verify()); rs } @@ -105,7 +105,7 @@ impl RootSet { if self.url.is_some() { let url = self.url.as_ref().unwrap().as_bytes(); buf.append_varint(url.len() as u64)?; - buf.append_bytes(url); + buf.append_bytes(url)?; } else { buf.append_varint(0)?; } diff --git a/zerotier-system-service/src/cli/rootset.rs b/zerotier-system-service/src/cli/rootset.rs index 647d6865a..bc341424d 100644 --- a/zerotier-system-service/src/cli/rootset.rs +++ b/zerotier-system-service/src/cli/rootset.rs @@ -112,6 +112,10 @@ pub async fn cmd(flags: Flags, cmd_args: &ArgMatches) -> i32 { } } + Some(("default", _)) => { + let _ = std::io::stdout().write_all(crate::utils::to_json_pretty(&RootSet::zerotier_default()).as_bytes()); + } + _ => panic!(), } return exitcode::OK; diff --git a/zerotier-system-service/src/main.rs b/zerotier-system-service/src/main.rs index 79e98eb04..c2cf2c45d 100644 --- a/zerotier-system-service/src/main.rs +++ b/zerotier-system-service/src/main.rs @@ -86,6 +86,8 @@ Advanced Operations: ยท list List root sets in use sign Sign a root set with an identity verify Load and verify a root set + marshal Dump root set as binary to stdout + default Dump the default root set as JSON service Start local service (usually not invoked manually) @@ -197,7 +199,8 @@ fn main() { .subcommand(Command::new("list")) .subcommand(Command::new("sign").arg(Arg::new("path").index(1).required(true)).arg(Arg::new("secret").index(2).required(true))) .subcommand(Command::new("verify").arg(Arg::new("path").index(1).required(true))) - .subcommand(Command::new("marshal").arg(Arg::new("path").index(1).required(true))), + .subcommand(Command::new("marshal").arg(Arg::new("path").index(1).required(true))) + .subcommand(Command::new("default")), ) .override_help(help.as_str()) .override_usage("")