1
#![cfg_attr(docsrs, feature(doc_cfg))]
2
#![doc = include_str!("../README.md")]
3
// @@ begin lint list maintained by maint/add_warning @@
4
#![allow(renamed_and_removed_lints)] // @@REMOVE_WHEN(ci_arti_stable)
5
#![allow(unknown_lints)] // @@REMOVE_WHEN(ci_arti_nightly)
6
#![warn(missing_docs)]
7
#![warn(noop_method_call)]
8
#![warn(unreachable_pub)]
9
#![warn(clippy::all)]
10
#![deny(clippy::await_holding_lock)]
11
#![deny(clippy::cargo_common_metadata)]
12
#![deny(clippy::cast_lossless)]
13
#![deny(clippy::checked_conversions)]
14
#![warn(clippy::cognitive_complexity)]
15
#![deny(clippy::debug_assert_with_mut_call)]
16
#![deny(clippy::exhaustive_enums)]
17
#![deny(clippy::exhaustive_structs)]
18
#![deny(clippy::expl_impl_clone_on_copy)]
19
#![deny(clippy::fallible_impl_from)]
20
#![deny(clippy::implicit_clone)]
21
#![deny(clippy::large_stack_arrays)]
22
#![warn(clippy::manual_ok_or)]
23
#![deny(clippy::missing_docs_in_private_items)]
24
#![warn(clippy::needless_borrow)]
25
#![warn(clippy::needless_pass_by_value)]
26
#![warn(clippy::option_option)]
27
#![deny(clippy::print_stderr)]
28
#![deny(clippy::print_stdout)]
29
#![warn(clippy::rc_buffer)]
30
#![deny(clippy::ref_option_ref)]
31
#![warn(clippy::semicolon_if_nothing_returned)]
32
#![warn(clippy::trait_duplication_in_bounds)]
33
#![deny(clippy::unchecked_time_subtraction)]
34
#![deny(clippy::unnecessary_wraps)]
35
#![warn(clippy::unseparated_literal_suffix)]
36
#![deny(clippy::unwrap_used)]
37
#![deny(clippy::mod_module_files)]
38
#![allow(clippy::let_unit_value)] // This can reasonably be done for explicitness
39
#![allow(clippy::uninlined_format_args)]
40
#![allow(clippy::significant_drop_in_scrutinee)] // arti/-/merge_requests/588/#note_2812945
41
#![allow(clippy::result_large_err)] // temporary workaround for arti#587
42
#![allow(clippy::needless_raw_string_hashes)] // complained-about code is fine, often best
43
#![allow(clippy::needless_lifetimes)] // See arti#1765
44
#![allow(mismatched_lifetime_syntaxes)] // temporary workaround for arti#2060
45
#![allow(clippy::collapsible_if)] // See arti#2342
46
#![deny(clippy::unused_async)]
47
//! <!-- @@ end lint list maintained by maint/add_warning @@ -->
48

            
49
// This module uses the `x509-cert` crate to generate certificates;
50
// if we decide to switch, `rcgen` and `x509-certificate`
51
// seem like the likeliest options.
52

            
53
use std::{
54
    sync::Arc,
55
    time::{Duration, SystemTime},
56
};
57

            
58
use digest::Digest;
59
use rand::CryptoRng;
60
use rsa::pkcs8::{EncodePrivateKey as _, SubjectPublicKeyInfo};
61
use tor_error::into_internal;
62
use tor_llcrypto::{pk::rsa::KeyPair as RsaKeypair, util::rng::RngCompat};
63
use x509_cert::{
64
    builder::{Builder, CertificateBuilder, Profile},
65
    der::{DateTime, Encode, asn1::GeneralizedTime, zeroize::Zeroizing},
66
    ext::pkix::{KeyUsage, KeyUsages},
67
    serial_number::SerialNumber,
68
    time::Validity,
69
};
70

            
71
/// Legacy identity keys are required to have this length.
72
const EXPECT_ID_BITS: usize = 1024;
73
/// Legacy identity keys are required to have this exponent.
74
const EXPECT_ID_EXPONENT: u32 = 65537;
75
/// Lifetime of generated id certs, in days.
76
const ID_CERT_LIFETIME_DAYS: u32 = 365;
77

            
78
/// Create an X.509 certificate, for use in a CERTS cell,
79
/// self-certifying the provided RSA identity key.
80
///
81
/// The resulting certificate will be encoded in DER.
82
/// Its cert_type field should be 02 when it is sent in a CERTS cell.
83
///
84
/// The resulting certificate is quite minimal, and has no unnecessary extensions.
85
///
86
/// Returns an error on failure, or if `keypair` is not a 1024-bit RSA key
87
/// with exponent of 65537.
88
6
pub fn create_legacy_rsa_id_cert<Rng: CryptoRng>(
89
6
    rng: &mut Rng,
90
6
    now: SystemTime,
91
6
    hostname: &str,
92
6
    keypair: &RsaKeypair,
93
6
) -> Result<Vec<u8>, X509CertError> {
94
    use rsa::pkcs1v15::SigningKey;
95
    use tor_llcrypto::d::Sha256;
96
6
    let public = keypair.to_public_key();
97
6
    if !public.exponent_is(EXPECT_ID_EXPONENT) {
98
        return Err(X509CertError::InvalidSigningKey("Invalid exponent".into()));
99
6
    }
100
6
    if !public.bits() == EXPECT_ID_BITS {
101
        return Err(X509CertError::InvalidSigningKey(
102
            "Invalid key length".into(),
103
        ));
104
6
    }
105

            
106
6
    let self_signed_profile = Profile::Manual { issuer: None };
107
6
    let serial_number = random_serial_number(rng)?;
108
6
    let (validity, _) = cert_validity(now, ID_CERT_LIFETIME_DAYS)?;
109
    // NOTE: This is how C Tor builds its DNs, but that doesn't mean it's a good idea.
110
6
    let subject: x509_cert::name::Name = format!("CN={hostname}")
111
6
        .parse()
112
6
        .map_err(X509CertError::InvalidHostname)?;
113
6
    let spki = SubjectPublicKeyInfo::from_key(keypair.to_public_key().as_key().clone())?;
114

            
115
6
    let signer = SigningKey::<Sha256>::new(keypair.as_key().clone());
116

            
117
6
    let mut builder = CertificateBuilder::new(
118
6
        self_signed_profile,
119
6
        serial_number,
120
6
        validity,
121
6
        subject,
122
6
        spki,
123
6
        &signer,
124
    )?;
125

            
126
    // We do not, strictly speaking, need this extension: Tor doesn't care that it's there.
127
    // We do, however, need _some_ extension, or else we'll generate a v1 certificate,
128
    // which we don't want to do.
129
6
    builder.add_extension(&KeyUsage(
130
6
        KeyUsages::KeyCertSign | KeyUsages::DigitalSignature,
131
6
    ))?;
132

            
133
6
    let cert = builder.build()?;
134

            
135
6
    let mut output = Vec::new();
136
6
    let _ignore_length: x509_cert::der::Length = cert
137
6
        .encode_to_vec(&mut output)
138
6
        .map_err(X509CertError::CouldNotEncode)?;
139
6
    Ok(output)
140
6
}
141

            
142
/// A set of x.509 certificate information and keys for use with a TLS library.
143
///
144
/// Only relays need this: They should set these as the certificate(s) to be used
145
/// for incoming TLS connections.
146
///
147
/// This is not necessarily the most convenient form to manipulate certificates in:
148
/// rather, it is intended to provide the formats that TLS libraries generally
149
/// expect to get.
150
#[derive(Clone, Debug)]
151
#[non_exhaustive]
152
pub struct TlsKeyAndCert {
153
    /// A list of certificates in DER form.
154
    ///
155
    /// (This may contain more than one certificate, but for now only one certificate is used.)
156
    certificates: Vec<Vec<u8>>,
157

            
158
    /// A private key for use in the TLS handshake.
159
    //
160
    // Disabled:
161
    // private_key: ecdsa::SigningKey<p256::NistP256>,
162
    private_key: rsa::RsaPrivateKey,
163

            
164
    /// A SHA256 digest of the link certificate
165
    /// (the one certifying the private key's public component).
166
    ///
167
    /// This digest is the one what will be certified by the relay's
168
    /// `SIGNING_V_TLS_CERT`
169
    /// certificate.
170
    sha256_digest: [u8; 32],
171

            
172
    /// A time after which this set of link information won't be valid,
173
    /// and another should be generated.
174
    expiration: SystemTime,
175
}
176

            
177
/// What lifetime do we pick for a TLS certificate, in days?
178
const TLS_CERT_LIFETIME_DAYS: u32 = 30;
179

            
180
impl TlsKeyAndCert {
181
    /// Return the certificates as a list of DER-encoded values.
182
949
    pub fn certificates_der(&self) -> Vec<&[u8]> {
183
962
        self.certificates.iter().map(|der| der.as_ref()).collect()
184
949
    }
185
    /// Return the certificates as a concatenated list in PEM ("BEGIN CERTIFICATE") format.
186
    pub fn certificate_pem(&self) -> String {
187
        let config = pem::EncodeConfig::new().set_line_ending(pem::LineEnding::LF);
188
        self.certificates
189
            .iter()
190
            .map(|der| pem::encode_config(&pem::Pem::new("CERTIFICATE", &der[..]), config))
191
            .collect()
192
    }
193
    /// Return the private key in (unencrypted) PKCS8 DER format.
194
219
    pub fn private_key_pkcs8_der(&self) -> Result<Zeroizing<Vec<u8>>, X509CertError> {
195
219
        Ok(self
196
219
            .private_key
197
219
            .to_pkcs8_der()
198
219
            .map_err(X509CertError::CouldNotFormatPkcs8)?
199
219
            .to_bytes())
200
219
    }
201
    /// Return the private key in (unencrypted) PKCS8 PEM ("BEGIN PRIVATE KEY") format.
202
    pub fn private_key_pkcs8_pem(&self) -> Result<Zeroizing<String>, X509CertError> {
203
        self.private_key
204
            .to_pkcs8_pem(p256::pkcs8::LineEnding::LF)
205
            .map_err(X509CertError::CouldNotFormatPkcs8)
206
    }
207
    /// Return the earliest time at which any of these certificates will expire.
208
    pub fn expiration(&self) -> SystemTime {
209
        self.expiration
210
    }
211

            
212
    /// Return the SHA256 digest of the link certificate
213
    ///
214
    /// This digest is the one certified with the relay's
215
    /// `SIGNING_V_TLS_CERT`
216
    /// certificate.
217
146
    pub fn link_cert_sha256(&self) -> &[u8; 32] {
218
146
        &self.sha256_digest
219
146
    }
220

            
221
    /// Create a new TLS link key and associated certificate(s).
222
    ///
223
    /// The certificate will be valid at `now`, and for a while after.
224
    ///
225
    /// The certificate parameters and keys are chosen for reasonable security,
226
    /// approximate conformance to RFC5280, and limited fingerprinting resistance.
227
    ///
228
    /// Note: The fingerprinting resistance is quite limited.
229
    /// We will likely want to pursue these avenues for better fingerprinting resistance:
230
    ///
231
    /// - Encourage more use of TLS 1.3, where server certificates are encrypted.
232
    ///   (This prevents passive fingerprinting only.)
233
    /// - Adjust this function to make certificates look even more normal
234
    /// - Integrate with ACME-supporting certificate issuers (Letsencrypt, etc)
235
    ///   to get real certificates for Tor relays.
236
20
    pub fn create<Rng: CryptoRng>(
237
20
        rng: &mut Rng,
238
20
        now: SystemTime,
239
20
        issuer_hostname: &str,
240
20
        subject_hostname: &str,
241
20
    ) -> Result<Self, X509CertError> {
242
        // We would prefer to use p256 here, since it is the most commonly used elliptic curve
243
        // group for X.509 web certificate signing, as of this writing.
244
        //
245
        // We want to use an elliptic curve here for its higher security/performance ratio than RSA,
246
        // and for its _much_ faster key generation time.
247
        //
248
        // But unfortunately, we can't: C tor has a bug where if the subject key is not RSA,
249
        // the connection will be closed with an error:
250
        // <https://gitlab.torproject.org/tpo/core/tor/-/issues/41226>.
251
        // If this bug is fixed, then we will have to wait until all clients and servers upgrade
252
        // before we can send p256 subject keys instead.
253
        //
254
        // DISABLED:
255
        // let private_key = rsa::RsaPrivateKey::p256::ecdsa::SigningKey::random(&mut RngCompat::new(&mut *rng));
256
        // let public_key = p256::ecdsa::VerifyingKey::from(&private_key);
257

            
258
        const RSA_KEY_BITS: usize = 2048;
259
20
        let private_key = rsa::RsaPrivateKey::new(&mut RngCompat::new(&mut *rng), RSA_KEY_BITS)
260
20
            .map_err(into_internal!("Unable to generate RSA key"))?;
261
20
        let public_key = private_key.to_public_key();
262

            
263
        // Note that we'll discard this key after signing the certificate with it:
264
        // The real certification for private_key is done in the SIGNING_V_TLS_CERT
265
        // certificate.
266
20
        let issuer_private_key = p256::ecdsa::SigningKey::random(&mut RngCompat::new(&mut *rng));
267

            
268
        // NOTE: This is how C Tor builds its DNs, but that doesn't mean it's a good idea.
269
20
        let issuer = format!("CN={issuer_hostname}")
270
20
            .parse()
271
20
            .map_err(X509CertError::InvalidHostname)?;
272
20
        let subject: x509_cert::name::Name = format!("CN={subject_hostname}")
273
20
            .parse()
274
20
            .map_err(X509CertError::InvalidHostname)?;
275

            
276
20
        let self_signed_profile = Profile::Leaf {
277
20
            issuer,
278
20
            enable_key_agreement: true,
279
20
            enable_key_encipherment: true,
280
20
            include_subject_key_identifier: true,
281
20
        };
282
20
        let serial_number = random_serial_number(rng)?;
283
20
        let (validity, expiration) = cert_validity(now, TLS_CERT_LIFETIME_DAYS)?;
284
20
        let spki = SubjectPublicKeyInfo::from_key(public_key)?;
285

            
286
20
        let builder = CertificateBuilder::new(
287
20
            self_signed_profile,
288
20
            serial_number,
289
20
            validity,
290
20
            subject,
291
20
            spki,
292
20
            &issuer_private_key,
293
        )?;
294

            
295
20
        let cert = builder.build::<ecdsa::der::Signature<_>>()?;
296

            
297
20
        let mut certificate_der = Vec::new();
298
20
        let _ignore_length: x509_cert::der::Length = cert
299
20
            .encode_to_vec(&mut certificate_der)
300
20
            .map_err(X509CertError::CouldNotEncode)?;
301

            
302
20
        let sha256_digest = tor_llcrypto::d::Sha256::digest(&certificate_der).into();
303
20
        let certificates = vec![certificate_der];
304

            
305
20
        Ok(TlsKeyAndCert {
306
20
            certificates,
307
20
            private_key,
308
20
            sha256_digest,
309
20
            expiration,
310
20
        })
311
20
    }
312
}
313

            
314
/// Return a Validity that includes `now`, and lasts for `lifetime_days` additionally.
315
///
316
/// Additionally, return the time at which the certificate expires.
317
///
318
/// We ensure that our cert is valid at least a day into the past.
319
///
320
/// We obfuscate our current time a little by rounding to the nearest midnight UTC.
321
807
fn cert_validity(
322
807
    now: SystemTime,
323
807
    lifetime_days: u32,
324
807
) -> Result<(Validity, SystemTime), X509CertError> {
325
    const ONE_DAY: Duration = Duration::new(86400, 0);
326

            
327
1627
    let start_of_day_containing = |when| -> Result<_, X509CertError> {
328
1614
        let dt = DateTime::from_system_time(when)
329
1614
            .map_err(into_internal!("Couldn't represent time as a DER DateTime"))?;
330
1614
        let dt = DateTime::new(dt.year(), dt.month(), dt.day(), 0, 0, 0)
331
1614
            .map_err(into_internal!("Couldn't construct DER DateTime"))?;
332
1614
        Ok(x509_cert::time::Time::GeneralTime(
333
1614
            GeneralizedTime::from_date_time(dt),
334
1614
        ))
335
1614
    };
336

            
337
807
    let start_on_day = now - ONE_DAY;
338
807
    let end_on_day = start_on_day + ONE_DAY * lifetime_days;
339

            
340
807
    let validity = Validity {
341
807
        not_before: start_of_day_containing(start_on_day)?,
342
807
        not_after: start_of_day_containing(end_on_day)?,
343
    };
344
807
    let expiration = validity.not_after.into();
345
807
    Ok((validity, expiration))
346
807
}
347

            
348
/// Return a random serial number for use in a new certificate.
349
26
fn random_serial_number<Rng: CryptoRng>(rng: &mut Rng) -> Result<SerialNumber, X509CertError> {
350
    const SER_NUMBER_LEN: usize = 16;
351
26
    let mut buf = [0; SER_NUMBER_LEN];
352
26
    rng.fill_bytes(&mut buf[..]);
353
26
    Ok(SerialNumber::new(&buf[..]).map_err(into_internal!("Couldn't construct serial number!"))?)
354
26
}
355

            
356
/// An error that has occurred while trying to create a certificate.
357
#[derive(Clone, Debug, thiserror::Error)]
358
#[non_exhaustive]
359
pub enum X509CertError {
360
    /// We received a signing key that we can't use.
361
    #[error("Provided signing key not valid: {0}")]
362
    InvalidSigningKey(String),
363

            
364
    /// We received a subject key that we can't use.
365
    #[error("Couldn't use provided key as a subject")]
366
    SubjectKeyError(#[from] x509_cert::spki::Error),
367

            
368
    /// We received a hostname that we couldn't use:
369
    /// probably, it contained an equals sign or a comma.
370
    #[error("Unable to set hostname when creating certificate")]
371
    InvalidHostname(#[source] x509_cert::der::Error),
372

            
373
    /// We couldn't construct the certificate.
374
    #[error("Unable to build certificate")]
375
    CouldNotBuild(#[source] Arc<x509_cert::builder::Error>),
376

            
377
    /// We constructed the certificate, but couldn't encode it as DER.
378
    #[error("Unable to encode certificate")]
379
    CouldNotEncode(#[source] x509_cert::der::Error),
380

            
381
    /// We constructed a key but couldn't format it as PKCS8.
382
    #[error("Unable to format key as PKCS8")]
383
    CouldNotFormatPkcs8(#[source] p256::pkcs8::Error),
384

            
385
    /// We've encountered some kind of a bug.
386
    #[error("Internal error while creating certificate")]
387
    Bug(#[from] tor_error::Bug),
388
}
389

            
390
impl From<x509_cert::builder::Error> for X509CertError {
391
    fn from(value: x509_cert::builder::Error) -> Self {
392
        X509CertError::CouldNotBuild(Arc::new(value))
393
    }
394
}
395

            
396
#[cfg(test)]
397
mod test {
398
    // @@ begin test lint list maintained by maint/add_warning @@
399
    #![allow(clippy::bool_assert_comparison)]
400
    #![allow(clippy::clone_on_copy)]
401
    #![allow(clippy::dbg_macro)]
402
    #![allow(clippy::mixed_attributes_style)]
403
    #![allow(clippy::print_stderr)]
404
    #![allow(clippy::print_stdout)]
405
    #![allow(clippy::single_char_pattern)]
406
    #![allow(clippy::unwrap_used)]
407
    #![allow(clippy::unchecked_time_subtraction)]
408
    #![allow(clippy::useless_vec)]
409
    #![allow(clippy::needless_pass_by_value)]
410
    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
411

            
412
    use super::*;
413
    use tor_basic_utils::test_rng::testing_rng;
414
    use web_time_compat::SystemTimeExt;
415

            
416
    #[test]
417
    fn identity_cert_generation() {
418
        let mut rng = testing_rng();
419
        let keypair = RsaKeypair::generate(&mut rng).unwrap();
420
        let cert = create_legacy_rsa_id_cert(
421
            &mut rng,
422
            SystemTime::get(),
423
            "www.house-of-pancakes.example.com",
424
            &keypair,
425
        )
426
        .unwrap();
427

            
428
        let key_extracted = tor_llcrypto::util::x509_extract_rsa_subject_kludge(&cert[..]).unwrap();
429
        assert_eq!(key_extracted, keypair.to_public_key());
430

            
431
        // TODO: It would be neat to validate this certificate with an independent x509 implementation,
432
        // but afaict most of them sensibly refuse to handle RSA1024.
433
        //
434
        // I've checked the above-generated cert using `openssl verify`, but that's it.
435
    }
436

            
437
    #[test]
438
    fn tls_cert_info() {
439
        let mut rng = testing_rng();
440
        let certified = TlsKeyAndCert::create(
441
            &mut rng,
442
            SystemTime::get(),
443
            "foo.example.com",
444
            "bar.example.com",
445
        )
446
        .unwrap();
447
        dbg!(certified);
448
    }
449
}