diff --git a/crates/tor-cell/src/relaycell/msg.rs b/crates/tor-cell/src/relaycell/msg.rs index 3897eab9e..e88ad4675 100644 --- a/crates/tor-cell/src/relaycell/msg.rs +++ b/crates/tor-cell/src/relaycell/msg.rs @@ -75,6 +75,9 @@ pub enum RelayMsg { /// Introduce1 #[cfg(feature = "onion-service")] Introduce1(onion_service::Introduce1), + /// Introduce2 + #[cfg(feature = "onion-service")] + Introduce2(onion_service::Introduce2), /// An unrecognized command. Unrecognized(Unrecognized), @@ -128,6 +131,8 @@ impl RelayMsg { EstablishRendezvous(_) => RelayCmd::ESTABLISH_RENDEZVOUS, #[cfg(feature = "onion-service")] Introduce1(_) => RelayCmd::INTRODUCE1, + #[cfg(feature = "onion-service")] + Introduce2(_) => RelayCmd::INTRODUCE2, Unrecognized(u) => u.cmd(), } } @@ -203,6 +208,8 @@ impl RelayMsg { EstablishRendezvous(b) => b.encode_onto(w), #[cfg(feature = "onion-service")] Introduce1(b) => b.encode_onto(w), + #[cfg(feature = "onion-service")] + Introduce2(b) => b.encode_onto(w), Unrecognized(b) => b.encode_onto(w), } } diff --git a/crates/tor-cell/src/relaycell/onion_service.rs b/crates/tor-cell/src/relaycell/onion_service.rs index 796bdde6a..962aced71 100644 --- a/crates/tor-cell/src/relaycell/onion_service.rs +++ b/crates/tor-cell/src/relaycell/onion_service.rs @@ -253,9 +253,55 @@ impl msg::Body for EstablishRendezvous { } } -/// A message sent from client to introduction point. #[derive(Debug, Clone)] -pub struct Introduce1 { +/// A message sent from client to introduction point. +pub struct Introduce1(Introduce); + +impl msg::Body for Introduce1 { + fn into_message(self) -> msg::RelayMsg { + msg::RelayMsg::Introduce1(self) + } + fn decode_from_reader(r: &mut Reader<'_>) -> Result { + Ok(Self(Introduce::decode_from_reader(r)?)) + } + fn encode_onto(self, w: &mut Vec) -> EncodeResult<()> { + self.0.encode_onto(w) + } +} + +impl Introduce1 { + /// All arguments constructor + pub fn new(auth_key_type: AuthKeyType, auth_key: Vec, encrypted: Vec) -> Self { + Self(Introduce::new(auth_key_type, auth_key, encrypted)) + } +} + +#[derive(Debug, Clone)] +/// A message sent from introduction point to hidden service host. +pub struct Introduce2(Introduce); + +impl msg::Body for Introduce2 { + fn into_message(self) -> msg::RelayMsg { + msg::RelayMsg::Introduce2(self) + } + fn decode_from_reader(r: &mut Reader<'_>) -> Result { + Ok(Self(Introduce::decode_from_reader(r)?)) + } + fn encode_onto(self, w: &mut Vec) -> EncodeResult<()> { + self.0.encode_onto(w) + } +} + +impl Introduce2 { + /// All arguments constructor + pub fn new(auth_key_type: AuthKeyType, auth_key: Vec, encrypted: Vec) -> Self { + Self(Introduce::new(auth_key_type, auth_key, encrypted)) + } +} + +#[derive(Debug, Clone)] +/// A message body shared by Introduce1 and Introduce2 +struct Introduce { /// Introduction point auth key type and the type of /// the MAC used in `handshake_auth`. auth_key_type: AuthKeyType, @@ -265,10 +311,16 @@ pub struct Introduce1 { encrypted: Vec, } -impl msg::Body for Introduce1 { - fn into_message(self) -> msg::RelayMsg { - msg::RelayMsg::Introduce1(self) +impl Introduce { + /// All arguments constructor + fn new(auth_key_type: AuthKeyType, auth_key: Vec, encrypted: Vec) -> Self { + Self { + auth_key_type, + auth_key, + encrypted, + } } + /// Decode an Introduce message body from the given reader fn decode_from_reader(r: &mut Reader<'_>) -> Result { let legacy_key_id: RsaIdentity = r.extract()?; if !legacy_key_id.is_zero() { @@ -292,6 +344,7 @@ impl msg::Body for Introduce1 { encrypted, }) } + /// Encode an Introduce message body onto the given writer fn encode_onto(self, w: &mut Vec) -> EncodeResult<()> { w.write_all(&[0_u8; 20]); w.write_u8(self.auth_key_type.get()); @@ -303,14 +356,3 @@ impl msg::Body for Introduce1 { Ok(()) } } - -impl Introduce1 { - /// All arguments constructor - pub fn new(auth_key_type: AuthKeyType, auth_key: Vec, encrypted: Vec) -> Self { - Self { - auth_key_type, - auth_key, - encrypted, - } - } -} diff --git a/crates/tor-cell/tests/testvec_relaymsg.rs b/crates/tor-cell/tests/testvec_relaymsg.rs index 0dbdf796c..a846ff5ed 100644 --- a/crates/tor-cell/tests/testvec_relaymsg.rs +++ b/crates/tor-cell/tests/testvec_relaymsg.rs @@ -722,12 +722,14 @@ fn test_establish_intro() { #[cfg(feature = "onion-service")] #[test] -fn test_introduce1() { +fn test_introduce() { use tor_cell::relaycell::{ msg::RelayMsg, onion_service::{AuthKeyType, Introduce1}, }; + // Testing with Introduce1 only should be sufficient as long as + // Introduce1 and Introduce2 share the same inner body let cmd = RelayCmd::INTRODUCE1; let auth_key_type = AuthKeyType::ED25519_SHA3_256; let auth_key = vec![0, 1, 2, 3];