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::{ExternallySigned, timed::TimerangeBound};
8
use tor_llcrypto as ll;
9

            
10
use digest::Digest;
11

            
12
use crate::{CertType, ExpiryHours};
13

            
14
#[cfg(feature = "encode")]
15
mod encode;
16
#[cfg(feature = "encode")]
17
pub use encode::EncodedRsaCrosscert;
18

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

            
39
/// Prefix appended when generating a digest for an RsaCrosscert
40
const PREFIX: &[u8] = b"Tor TLS RSA/Ed25519 cross-certificate";
41

            
42
/// Compute the SHA256 digest of `c`, prefixed with PREFIX.
43
554
fn compute_digest(c: &[u8]) -> [u8; 32] {
44
554
    let mut d = ll::d::Sha256::new();
45
554
    d.update(PREFIX);
46
554
    d.update(c);
47
554
    d.finalize().into()
48
554
}
49

            
50
impl RsaCrosscert {
51
    /// Return the time at which this certificate becomes expired
52
404
    pub fn expiry(&self) -> std::time::SystemTime {
53
404
        self.exp_hours.into()
54
404
    }
55

            
56
    /// Return a reference to the digest.
57
    pub fn digest(&self) -> &[u8; 32] {
58
        &self.digest
59
    }
60

            
61
    /// Return true if the subject key in this certificate matches `other`
62
402
    pub fn subject_key_matches(&self, other: &ll::pk::ed25519::Ed25519Identity) -> bool {
63
402
        other == &self.subject_key
64
402
    }
65

            
66
    /// Return this certificate cert type.
67
    pub fn cert_type(&self) -> CertType {
68
        CertType::RSA_ID_V_IDENTITY
69
    }
70

            
71
    /// Decode a slice of bytes into an RSA crosscert.
72
452
    pub fn decode(bytes: &[u8]) -> tor_bytes::Result<UncheckedRsaCrosscert> {
73
452
        let mut r = Reader::from_slice(bytes);
74
452
        let signed_portion = r.peek(36)?; // TODO(nickm): a bit ugly.
75
452
        let subject_key = r.extract()?;
76
452
        let exp_hours = r.extract()?;
77
452
        let siglen = r.take_u8()?;
78
452
        let signature = r.take(siglen as usize)?.into();
79

            
80
452
        let digest = compute_digest(signed_portion);
81

            
82
452
        let cc = RsaCrosscert {
83
452
            subject_key,
84
452
            exp_hours,
85
452
            digest,
86
452
            signature,
87
452
        };
88

            
89
452
        Ok(UncheckedRsaCrosscert(cc))
90
452
    }
91
}
92

            
93
/// An RsaCrosscert whose signature has not been checked.
94
pub struct UncheckedRsaCrosscert(RsaCrosscert);
95

            
96
impl ExternallySigned<TimerangeBound<RsaCrosscert>> for UncheckedRsaCrosscert {
97
    type Key = ll::pk::rsa::PublicKey;
98
    type KeyHint = ();
99
    type Error = tor_bytes::Error;
100

            
101
100
    fn key_is_correct(&self, _k: &Self::Key) -> Result<(), Self::KeyHint> {
102
        // there is no way to check except for trying to verify the signature
103
100
        Ok(())
104
100
    }
105

            
106
502
    fn is_well_signed(&self, k: &Self::Key) -> Result<(), Self::Error> {
107
502
        k.verify(&self.0.digest[..], &self.0.signature[..])
108
542
            .map_err(|_| {
109
138
                tor_bytes::Error::InvalidMessage(
110
138
                    "Invalid signature on RSA->Ed identity crosscert".into(),
111
138
                )
112
140
            })?;
113
402
        Ok(())
114
502
    }
115

            
116
402
    fn dangerously_assume_wellsigned(self) -> TimerangeBound<RsaCrosscert> {
117
402
        let expiration = self.0.expiry();
118
402
        TimerangeBound::new(self.0, ..expiration)
119
402
    }
120
}