From 17fab6e91390418c216caa33155214ccb3e2fa36 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 20 Jun 2023 14:00:41 -0400 Subject: [PATCH] netdoc: Use derive amplify::Getters for HsDesc accessors. Closes #909. --- Cargo.lock | 48 +++++++++++++++++++++++++ crates/tor-netdoc/Cargo.toml | 1 + crates/tor-netdoc/src/doc/hsdesc.rs | 54 +++++------------------------ 3 files changed, 57 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 67bec5b8b..de3a5b43b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,6 +49,47 @@ dependencies = [ "memchr", ] +[[package]] +name = "amplify" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26966af46e0d200e8bf2b7f16230997c1c3f2d141bc27ccc091c012ed527b58" +dependencies = [ + "amplify_derive", + "amplify_num", + "ascii", + "wasm-bindgen", +] + +[[package]] +name = "amplify_derive" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c87df0f28e6eb1f2d355f29ba6793fa9ca643967528609608d5cbd70bd68f9d1" +dependencies = [ + "amplify_syn", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "amplify_num" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddce3bc63e807ea02065e8d8b702695f3d302ae4158baddff8b0ce5c73947251" + +[[package]] +name = "amplify_syn" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7736fb8d473c0d83098b5bac44df6a561e20470375cd8bcae30516dc889fd62a" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -288,6 +329,12 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "assert-impl" version = "0.1.3" @@ -4569,6 +4616,7 @@ dependencies = [ name = "tor-netdoc" version = "0.8.0" dependencies = [ + "amplify", "base64ct", "bitflags 2.3.1", "cipher", diff --git a/crates/tor-netdoc/Cargo.toml b/crates/tor-netdoc/Cargo.toml index afc4823c9..607f39c03 100644 --- a/crates/tor-netdoc/Cargo.toml +++ b/crates/tor-netdoc/Cargo.toml @@ -78,6 +78,7 @@ dangerous-expose-struct-fields = ["visible", "visibility", "__is_experimental"] __is_experimental = [] [dependencies] +amplify = { version = "4", default-features = false, features = ["derive"] } base64ct = { version = "1.5.1", features = ["alloc"] } bitflags = "2" cipher = { version = "0.4.1", features = ["zeroize"] } diff --git a/crates/tor-netdoc/src/doc/hsdesc.rs b/crates/tor-netdoc/src/doc/hsdesc.rs index 49c9ca7d3..4a64a57b2 100644 --- a/crates/tor-netdoc/src/doc/hsdesc.rs +++ b/crates/tor-netdoc/src/doc/hsdesc.rs @@ -130,7 +130,7 @@ pub enum IntroAuthType { /// Information in an onion service descriptor about a single /// introduction point. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, amplify::Getters)] pub struct IntroPointDesc { /// The list of link specifiers needed to extend a circuit to the introduction point. /// @@ -138,6 +138,7 @@ pub struct IntroPointDesc { // // TODO hs: perhaps we should make certain link specifiers mandatory? That // would make it possible for IntroPointDesc to implement CircTarget. + #[getter(skip)] link_specifiers: Vec, /// The key to be used to extend a circuit _to the introduction point_, using the @@ -298,21 +299,11 @@ impl HsDesc { /// One or more introduction points used to contact the onion service. /// /// Accessor function. - // TODO HS derive this accessor // - // For this and others I considered the following crates: + // TODO: We'd like to derive this, but amplify::Getters would give us &Vec<>, + // not &[]. // - // * amplify_derive, derive-getters, accessors-rs: - // No way to deref the Vec to [], which I think is desirable - // (after all, eventually these might become Box<[]>. - // - // * getset: Always generates methods called `get_` which isn't what we want. - // * tia: Same problem. Also strange API. - // * field_accessor: Works with Strings. - // * accessors: Always uses `get` prefix, underdocumented. - // * structural: Seems to be something much more reflection-y. - // - // derive-adhoc would make short work of this problem. + // Perhaps someday we can use derive_adhoc, or add as_ref() support? pub fn intro_points(&self) -> &[IntroPointDesc] { &self.intro_points } @@ -324,41 +315,12 @@ impl IntroPointDesc { /// These can include public keys and network addresses. /// /// Accessor function. - // TODO HS derive this accessor (see above) + // + // TODO: It would be better to derive this too, but this accessor needs to + // return a slice; Getters can only give us a &Vec<> in this case. pub fn link_specifiers(&self) -> &[EncodedLinkSpec] { &self.link_specifiers } - - /// The key to be used to extend a circuit _to the introduction point_, using the - /// ntor or ntor3 handshakes. (`KP_ntor`) - /// - /// Accessor function. - // TODO HS derive this accessor (see above) - pub fn ipt_ntor_key(&self) -> &curve25519::PublicKey { - &self.ipt_ntor_key - } - - /// The key to be used to identify the onion service at this introduction point. - /// (`KP_hs_ipt_sid`) - /// - /// Accessor function. - // TODO HS derive this accessor (see above) - pub fn ipt_sid_key(&self) -> &HsIntroPtSessionIdKey { - &self.ipt_sid_key - } - - /// `KP_hss_ntor`, the key used to encrypt a handshake _to the onion - /// service_ when using this introduction point. - /// - /// The onion service uses a separate key of this type with each - /// introduction point as part of its strategy for preventing replay - /// attacks. - /// - /// Accessor function. - // TODO HS derive this accessor (see above) - pub fn svc_ntor_key(&self) -> &HsSvcNtorKey { - &self.svc_ntor_key - } } impl EncryptedHsDesc {