Onion service descriptor parsing and decoding, first cut.

This branch has code that successfully parses and decrypts all the layers of an example onion service descriptor. It's part of #744 (closed).

(It's not all of it, since there are things remaining to clean up; see the "TODO HS" notes here and there.)

Assigning review to @Diziet since @eta is busy in Brussels.

Merge request reports

Loading
+41 −8
Changes for crates/tor-cert/src/lib.rs: 41 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -92,18 +92,39 @@ caret_int! {
        /// crosscert format. (Legacy)
        RSA_ID_V_IDENTITY = 0x07,

        /// For onion services: short-term signing key authenticated with
        /// blinded service identity.
        /// For onion services: short-term descriptor signing key
        /// (`KP_hs_desc_sign`), signed with blinded onion service identity
        /// (`KP_hs_blind_id`).
        HS_BLINDED_ID_V_SIGNING = 0x08,

        /// For onion services: to be documented.
        /// For onion services: Introduction point authentication key
        /// (`KP_hs_intro_tid`), signed with short term descriptor signing key
        /// (`KP_hs_desc_sign`).
        ///
        /// This one is, sadly, a bit complicated. In the original specification
        /// it was meant to be a cross-certificate, where the signature would be
        /// _on_ the descriptor signing key, _signed with_ the intro TID key.
        /// But we got it backwards in the C Tor implementation, and now, for
        /// compatibility, we are stuck doing it backwards in the future.
        ///
        /// If we find in the future that it is actually important to
        /// cross-certify these keys (as originally intended), then we should
        /// add a new certificate type, and put the new certificate in the onion
        /// service descriptor.
        HS_IP_V_SIGNING = 0x09,

        /// An ntor key converted to a ed25519 key, cross-certifying an
        /// identity key.
        NTOR_CC_IDENTITY = 0x0A,

        /// For onion services: to be documented.
        /// For onion services: Ntor encryption key (`KP_hs_intro_ntor`),
        /// converted to ed25519, signed with the descriptor signing key
        /// (`KP_hs_desc_sign`).
        ///
        /// As with [`HS_IP_V_SIGNING`](CertType::HS_IP_V_SIGNING), this
        /// certificate type is backwards.  In the original specification it was
        /// meant to be a cross certificate, with the signing and signed keys
        /// reversed.
        HS_IP_CC_SIGNING = 0x0B,
    }
}
@@ -506,11 +527,12 @@ impl tor_checkable::SelfSigned<SigCheckedCert> for UncheckedCert {
    }
}

impl tor_checkable::Timebound<Ed25519Cert> for SigCheckedCert {
impl tor_checkable::Timebound<Ed25519Cert> for Ed25519Cert {
    type Error = tor_checkable::TimeValidityError;
    fn is_valid_at(&self, t: &time::SystemTime) -> std::result::Result<(), Self::Error> {
        if self.cert.is_expired_at(*t) {
            let expiry = self.cert.expiry();

    fn is_valid_at(&self, t: &time::SystemTime) -> Result<(), Self::Error> {
        if self.is_expired_at(*t) {
            let expiry = self.expiry();
            Err(Self::Error::Expired(
                t.duration_since(expiry)
                    .expect("certificate expiry time inconsistent"),
@@ -520,6 +542,17 @@ impl tor_checkable::Timebound<Ed25519Cert> for SigCheckedCert {
        }
    }

    fn dangerously_assume_timely(self) -> Ed25519Cert {
        self
    }
}

impl tor_checkable::Timebound<Ed25519Cert> for SigCheckedCert {
    type Error = tor_checkable::TimeValidityError;
    fn is_valid_at(&self, t: &time::SystemTime) -> std::result::Result<(), Self::Error> {
        self.cert.is_valid_at(t)
    }

    fn dangerously_assume_timely(self) -> Ed25519Cert {
        self.cert
    }
+2
Changes for crates/tor-cert/semver.md: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
ADDED: Ed25519Cert implements Timebound.
+29
Changes for crates/tor-checkable/src/signed.rs: 29 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -19,6 +19,23 @@ impl<T> SignatureGated<T> {
    pub fn new(obj: T, signatures: Vec<Box<dyn ValidatableSignature>>) -> Self {
        SignatureGated { obj, signatures }
    }

    /// Consume this [`SignatureGated`], and return a new one with the same
    /// bounds, applying `f` to its protected value.
    ///
    /// The caller must ensure that `f` does not make any assumptions about the
    /// well-signedness of the protected value, or leak any of its contents in
    /// an inappropriate way.
    #[must_use]
    pub fn dangerously_map<F, U>(self, f: F) -> SignatureGated<U>
    where
        F: FnOnce(T) -> U,
    {
        SignatureGated {
            obj: f(self.obj),
            signatures: self.signatures,
        }
    }
}

impl<T> super::SelfSigned<T> for SignatureGated<T> {
@@ -88,4 +105,16 @@ mod test {
        );
        assert_eq!(sg.check_signature().unwrap(), 104_u32);
    }

    #[test]
    fn test_map() {
        let good = SignatureGated::new("hello world...", vec![Box::new(GoodSig)]);
        let good = good.dangerously_map(|s| &s[..11]);
        let s = good.check_signature().unwrap();
        assert_eq!(s, "hello world");

        let bad = SignatureGated::new("hello world...", vec![Box::new(BadSig)]);
        let still_bad = bad.dangerously_map(|s| &s[..11]);
        assert!(still_bad.check_signature().is_err());
    }
}
+31 −1
Changes for crates/tor-checkable/src/timed.rs: 31 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -79,6 +79,23 @@ impl<T> TimerangeBound<T> {
        let start = self.start.map(|t| t - d);
        Self { start, ..self }
    }
    /// Consume this [`TimerangeBound`], and return a new one with the same
    /// bounds, applying `f` to its protected value.
    ///
    /// The caller must ensure that `f` does not make any assumptions about the
    /// timeliness of the protected value, or leak any of its contents in
    /// an inappropriate way.
    #[must_use]
    pub fn dangerously_map<F, U>(self, f: F) -> TimerangeBound<U>
    where
        F: FnOnce(T) -> U,
    {
        TimerangeBound {
            obj: f(self.obj),
            start: self.start,
            end: self.end,
        }
    }

    /// Consume this TimeRangeBound, and return its underlying time bounds and
    /// object.
@@ -231,7 +248,6 @@ mod test {
        assert!(tr.check_valid_at_opt(None).is_err());
    }

    #[cfg(feature = "experimental-api")]
    #[test]
    fn test_dangerous() {
        let t1 = SystemTime::now();
@@ -245,4 +261,18 @@ mod test {
        assert_eq!(b.0, Bound::Included(t1));
        assert_eq!(b.1, Bound::Included(t2));
    }

    #[test]
    fn test_map() {
        let t1 = SystemTime::now();
        let min = Duration::from_secs(60);

        let tb = TimerangeBound::new(17_u32, t1..t1 + 5 * min);
        let tb = tb.dangerously_map(|v| v * v);
        assert!(tb.is_valid_at(&(t1 + 1 * min)).is_ok());
        assert!(tb.is_valid_at(&(t1 + 10 * min)).is_err());

        let val = tb.check_valid_at(&(t1 + 1 * min)).unwrap();
        assert_eq!(val, 289);
    }
}
+3 −1
Changes for crates/tor-hscrypto/src/lib.rs: 3 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -63,7 +63,9 @@ pub struct Subcredential([u8; 32]);
///
/// There can be gaps in this numbering. A descriptor with a higher-valued
/// revision counter supersedes one with a lower revision counter.
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
#[derive(
    Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, derive_more::From, derive_more::Into,
)]
pub struct RevisionCounter(u64);

define_bytes! {
Loading
Loading