1use tor_bytes::Reader;
7use tor_checkable::{timed::TimerangeBound, ExternallySigned};
8use tor_llcrypto as ll;
9
10use digest::Digest;
11
12#[must_use]
20pub struct RsaCrosscert {
21 subject_key: ll::pk::ed25519::Ed25519Identity,
23 exp_hours: u32,
26 digest: [u8; 32],
28 signature: Vec<u8>,
30}
31
32impl RsaCrosscert {
33 pub fn expiry(&self) -> std::time::SystemTime {
35 let d = std::time::Duration::new(u64::from(self.exp_hours) * 3600, 0);
36 std::time::SystemTime::UNIX_EPOCH + d
37 }
38
39 pub fn subject_key_matches(&self, other: &ll::pk::ed25519::Ed25519Identity) -> bool {
41 other == &self.subject_key
42 }
43
44 pub fn decode(bytes: &[u8]) -> tor_bytes::Result<UncheckedRsaCrosscert> {
46 let mut r = Reader::from_slice(bytes);
47 let signed_portion = r.peek(36)?; let subject_key = r.extract()?;
49 let exp_hours = r.take_u32()?;
50 let siglen = r.take_u8()?;
51 let signature = r.take(siglen as usize)?.into();
52
53 let mut d = ll::d::Sha256::new();
54 d.update(&b"Tor TLS RSA/Ed25519 cross-certificate"[..]);
55 d.update(signed_portion);
56 let digest = d.finalize().into();
57
58 let cc = RsaCrosscert {
59 subject_key,
60 exp_hours,
61 digest,
62 signature,
63 };
64
65 Ok(UncheckedRsaCrosscert(cc))
66 }
67}
68
69pub struct UncheckedRsaCrosscert(RsaCrosscert);
71
72impl ExternallySigned<TimerangeBound<RsaCrosscert>> for UncheckedRsaCrosscert {
73 type Key = ll::pk::rsa::PublicKey;
74 type KeyHint = ();
75 type Error = tor_bytes::Error;
76
77 fn key_is_correct(&self, _k: &Self::Key) -> Result<(), Self::KeyHint> {
78 Ok(())
80 }
81
82 fn is_well_signed(&self, k: &Self::Key) -> Result<(), Self::Error> {
83 k.verify(&self.0.digest[..], &self.0.signature[..])
84 .map_err(|_| {
85 tor_bytes::Error::InvalidMessage(
86 "Invalid signature on RSA->Ed identity crosscert".into(),
87 )
88 })?;
89 Ok(())
90 }
91
92 fn dangerously_assume_wellsigned(self) -> TimerangeBound<RsaCrosscert> {
93 let expiration = self.0.expiry();
94 TimerangeBound::new(self.0, ..expiration)
95 }
96}