tor-cell: Support extensions in INTRODUCE{1,2}

This commit is contained in:
Nick Mathewson 2023-02-27 14:09:27 -05:00
parent 7af2efdb82
commit ca055d4cb5
2 changed files with 26 additions and 22 deletions

View File

@ -93,6 +93,22 @@ impl Introduce2 {
}
}
caret_int! {
/// The recognized extension types for an `Introduce1` or `Introduce2 message.
#[derive(Ord,PartialOrd)]
pub struct IntroduceExtType(u8) {
}
}
decl_extension_group! {
/// An extension to an IntroEstablished message.
///
/// (Currently, no extensions of this type are recognized)
#[derive(Debug,Clone)]
enum IntroduceExt [ IntroduceExtType ] {
}
}
#[derive(Debug, Clone)]
/// A message body shared by Introduce1 and Introduce2
struct Introduce {
@ -101,6 +117,8 @@ struct Introduce {
auth_key_type: AuthKeyType,
/// The public introduction point auth key.
auth_key: Vec<u8>,
/// A list of extensions
extensions: ExtList<IntroduceExt>,
/// Up to end of relay payload.
encrypted: Vec<u8>,
}
@ -111,6 +129,7 @@ impl Introduce {
Self {
auth_key_type,
auth_key,
extensions: Default::default(),
encrypted,
}
}
@ -125,18 +144,12 @@ impl Introduce {
let auth_key_type = r.take_u8()?.into();
let auth_key_len = r.take_u16()?;
let auth_key = r.take(auth_key_len as usize)?.into();
let n_ext = r.take_u8()?;
for _ in 0..n_ext {
let _ext_type = r.take_u8()?;
r.read_nested_u8len(|r| {
r.take_rest();
Ok(())
})?;
}
let extensions = r.extract()?;
let encrypted = r.take_rest().into();
Ok(Self {
auth_key_type,
auth_key,
extensions,
encrypted,
})
}
@ -146,8 +159,7 @@ impl Introduce {
w.write_u8(self.auth_key_type.get());
w.write_u16(u16::try_from(self.auth_key.len()).map_err(|_| EncodeError::BadLengthValue)?);
w.write_all(&self.auth_key[..]);
// No Introduce1 extension for now.
w.write_u8(0_u8);
w.write(&self.extensions)?;
w.write_all(&self.encrypted[..]);
Ok(())
}

View File

@ -717,10 +717,7 @@ fn test_establish_intro() {
#[cfg(feature = "hs-service")]
#[test]
fn test_introduce() {
use tor_cell::relaycell::{
hs::{AuthKeyType, Introduce1},
msg::AnyRelayMsg,
};
use tor_cell::relaycell::hs::{AuthKeyType, Introduce1};
// Testing with Introduce1 only should be sufficient as long as
// Introduce1 and Introduce2 share the same inner body
@ -738,7 +735,7 @@ fn test_introduce() {
02 0004 00010203
00
01090804",
&intro1.clone().into(),
&intro1.into(),
);
// Introduce1 with unknown extensions
@ -747,16 +744,11 @@ fn test_introduce() {
02 01 01 00 02 01 00
01090804";
let actual_msg = decode(cmd, &unhex(body)[..]).unwrap();
let mut actual_bytes = vec![];
let mut expect_bytes = vec![];
let mut actual_bytes = Vec::new();
actual_msg
.encode_onto(&mut actual_bytes)
.expect("Encode msg onto byte vector");
let expected_msg: AnyRelayMsg = intro1.into();
expected_msg
.encode_onto(&mut expect_bytes)
.expect("Encode msg onto byte vector");
assert_eq!(actual_bytes, expect_bytes);
assert_eq!(actual_bytes, unhex(body));
// Introduce1 with legacy key id
msg_error(