1
//! RSA->Ed25519 cross-certificates
2
//!
3
//! These are used in the Tor link handshake to prove that a given ed25519
4
//! key speaks for a given (deprecated) RSA identity.
5

            
6
use tor_bytes::Reader;
7
use tor_checkable::{timed::TimerangeBound, ExternallySigned};
8
use tor_llcrypto as ll;
9

            
10
use digest::Digest;
11

            
12
/// A RSA->Ed25519 cross-certificate
13
///
14
/// This kind of certificate is used in the channel handshake to prove
15
/// that the Ed25519 identity key speaks on behalf of the RSA identity key.
16
///
17
/// (There is no converse type for certifying Ed25519 identity keys with
18
/// RSA identity keys, since the RSA identity keys are too weak to trust.)
19
#[must_use]
20
pub struct RsaCrosscert {
21
    /// The key that is being certified
22
    subject_key: ll::pk::ed25519::PublicKey,
23
    /// The expiration time of this certificate, in hours since the
24
    /// unix epoch.
25
    exp_hours: u32,
26
    /// The digest of the signed part of the certificate (for checking)
27
    digest: [u8; 32],
28
    /// The (alleged) signature on the certificate.
29
    signature: Vec<u8>,
30
}
31

            
32
impl RsaCrosscert {
33
    /// Return the time at which this certificate becomes expired
34
70
    pub fn expiry(&self) -> std::time::SystemTime {
35
70
        let d = std::time::Duration::new(u64::from(self.exp_hours) * 3600, 0);
36
70
        std::time::SystemTime::UNIX_EPOCH + d
37
70
    }
38

            
39
    /// Return true if the subject key in this certificate matches `other`
40
70
    pub fn subject_key_matches(&self, other: &ll::pk::ed25519::PublicKey) -> bool {
41
70
        &self.subject_key == other
42
70
    }
43

            
44
    /// Decode a slice of bytes into an RSA crosscert.
45
84
    pub fn decode(bytes: &[u8]) -> tor_bytes::Result<UncheckedRsaCrosscert> {
46
84
        let mut r = Reader::from_slice(bytes);
47
84
        let signed_portion = r.peek(36)?; // TODO(nickm): a bit ugly.
48
84
        let subject_key = r.extract()?;
49
84
        let exp_hours = r.take_u32()?;
50
84
        let siglen = r.take_u8()?;
51
84
        let signature = r.take(siglen as usize)?.into();
52
84

            
53
84
        let mut d = ll::d::Sha256::new();
54
84
        d.update(&b"Tor TLS RSA/Ed25519 cross-certificate"[..]);
55
84
        d.update(signed_portion);
56
84
        let digest = d.finalize().into();
57
84

            
58
84
        let cc = RsaCrosscert {
59
84
            subject_key,
60
84
            exp_hours,
61
84
            digest,
62
84
            signature,
63
84
        };
64
84

            
65
84
        Ok(UncheckedRsaCrosscert(cc))
66
84
    }
67
}
68

            
69
/// An RsaCrosscert whose signature has not been checked.
70
pub struct UncheckedRsaCrosscert(RsaCrosscert);
71

            
72
impl ExternallySigned<TimerangeBound<RsaCrosscert>> for UncheckedRsaCrosscert {
73
    type Key = ll::pk::rsa::PublicKey;
74
    type KeyHint = ();
75
    type Error = tor_bytes::Error;
76

            
77
28
    fn key_is_correct(&self, _k: &Self::Key) -> Result<(), Self::KeyHint> {
78
28
        // there is no way to check except for trying to verify the signature
79
28
        Ok(())
80
28
    }
81

            
82
    fn is_well_signed(&self, k: &Self::Key) -> Result<(), Self::Error> {
83
98
        k.verify(&self.0.digest[..], &self.0.signature[..])
84
122
            .map_err(|_| {
85
52
                tor_bytes::Error::BadMessage("Invalid signature on RSA->Ed identity crosscert")
86
122
            })?;
87
70
        Ok(())
88
98
    }
89

            
90
70
    fn dangerously_assume_wellsigned(self) -> TimerangeBound<RsaCrosscert> {
91
70
        let expiration = self.0.expiry();
92
70
        TimerangeBound::new(self.0, ..expiration)
93
70
    }
94
}