1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![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)] #![allow(clippy::uninlined_format_args)]
40#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] #![allow(clippy::collapsible_if)] #![deny(clippy::unused_async)]
47use std::{
54 sync::Arc,
55 time::{Duration, SystemTime},
56};
57
58use digest::Digest;
59use rand::CryptoRng;
60use rsa::pkcs8::{EncodePrivateKey as _, SubjectPublicKeyInfo};
61use tor_error::into_internal;
62use tor_llcrypto::{pk::rsa::KeyPair as RsaKeypair, util::rng::RngCompat};
63use 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
71const EXPECT_ID_BITS: usize = 1024;
73const EXPECT_ID_EXPONENT: u32 = 65537;
75const ID_CERT_LIFETIME_DAYS: u32 = 365;
77
78pub fn create_legacy_rsa_id_cert<Rng: CryptoRng>(
89 rng: &mut Rng,
90 now: SystemTime,
91 hostname: &str,
92 keypair: &RsaKeypair,
93) -> Result<Vec<u8>, X509CertError> {
94 use rsa::pkcs1v15::SigningKey;
95 use tor_llcrypto::d::Sha256;
96 let public = keypair.to_public_key();
97 if !public.exponent_is(EXPECT_ID_EXPONENT) {
98 return Err(X509CertError::InvalidSigningKey("Invalid exponent".into()));
99 }
100 if !public.bits() == EXPECT_ID_BITS {
101 return Err(X509CertError::InvalidSigningKey(
102 "Invalid key length".into(),
103 ));
104 }
105
106 let self_signed_profile = Profile::Manual { issuer: None };
107 let serial_number = random_serial_number(rng)?;
108 let (validity, _) = cert_validity(now, ID_CERT_LIFETIME_DAYS)?;
109 let subject: x509_cert::name::Name = format!("CN={hostname}")
111 .parse()
112 .map_err(X509CertError::InvalidHostname)?;
113 let spki = SubjectPublicKeyInfo::from_key(keypair.to_public_key().as_key().clone())?;
114
115 let signer = SigningKey::<Sha256>::new(keypair.as_key().clone());
116
117 let mut builder = CertificateBuilder::new(
118 self_signed_profile,
119 serial_number,
120 validity,
121 subject,
122 spki,
123 &signer,
124 )?;
125
126 builder.add_extension(&KeyUsage(
130 KeyUsages::KeyCertSign | KeyUsages::DigitalSignature,
131 ))?;
132
133 let cert = builder.build()?;
134
135 let mut output = Vec::new();
136 let _ignore_length: x509_cert::der::Length = cert
137 .encode_to_vec(&mut output)
138 .map_err(X509CertError::CouldNotEncode)?;
139 Ok(output)
140}
141
142#[derive(Clone, Debug)]
151#[non_exhaustive]
152pub struct TlsKeyAndCert {
153 certificates: Vec<Vec<u8>>,
157
158 private_key: rsa::RsaPrivateKey,
163
164 sha256_digest: [u8; 32],
171
172 expiration: SystemTime,
175}
176
177const TLS_CERT_LIFETIME_DAYS: u32 = 30;
179
180impl TlsKeyAndCert {
181 pub fn certificates_der(&self) -> Vec<&[u8]> {
183 self.certificates.iter().map(|der| der.as_ref()).collect()
184 }
185 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 pub fn private_key_pkcs8_der(&self) -> Result<Zeroizing<Vec<u8>>, X509CertError> {
195 Ok(self
196 .private_key
197 .to_pkcs8_der()
198 .map_err(X509CertError::CouldNotFormatPkcs8)?
199 .to_bytes())
200 }
201 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 pub fn expiration(&self) -> SystemTime {
209 self.expiration
210 }
211
212 pub fn link_cert_sha256(&self) -> &[u8; 32] {
218 &self.sha256_digest
219 }
220
221 pub fn create<Rng: CryptoRng>(
237 rng: &mut Rng,
238 now: SystemTime,
239 issuer_hostname: &str,
240 subject_hostname: &str,
241 ) -> Result<Self, X509CertError> {
242 const RSA_KEY_BITS: usize = 2048;
259 let private_key = rsa::RsaPrivateKey::new(&mut RngCompat::new(&mut *rng), RSA_KEY_BITS)
260 .map_err(into_internal!("Unable to generate RSA key"))?;
261 let public_key = private_key.to_public_key();
262
263 let issuer_private_key = p256::ecdsa::SigningKey::random(&mut RngCompat::new(&mut *rng));
267
268 let issuer = format!("CN={issuer_hostname}")
270 .parse()
271 .map_err(X509CertError::InvalidHostname)?;
272 let subject: x509_cert::name::Name = format!("CN={subject_hostname}")
273 .parse()
274 .map_err(X509CertError::InvalidHostname)?;
275
276 let self_signed_profile = Profile::Leaf {
277 issuer,
278 enable_key_agreement: true,
279 enable_key_encipherment: true,
280 include_subject_key_identifier: true,
281 };
282 let serial_number = random_serial_number(rng)?;
283 let (validity, expiration) = cert_validity(now, TLS_CERT_LIFETIME_DAYS)?;
284 let spki = SubjectPublicKeyInfo::from_key(public_key)?;
285
286 let builder = CertificateBuilder::new(
287 self_signed_profile,
288 serial_number,
289 validity,
290 subject,
291 spki,
292 &issuer_private_key,
293 )?;
294
295 let cert = builder.build::<ecdsa::der::Signature<_>>()?;
296
297 let mut certificate_der = Vec::new();
298 let _ignore_length: x509_cert::der::Length = cert
299 .encode_to_vec(&mut certificate_der)
300 .map_err(X509CertError::CouldNotEncode)?;
301
302 let sha256_digest = tor_llcrypto::d::Sha256::digest(&certificate_der).into();
303 let certificates = vec![certificate_der];
304
305 Ok(TlsKeyAndCert {
306 certificates,
307 private_key,
308 sha256_digest,
309 expiration,
310 })
311 }
312}
313
314fn cert_validity(
322 now: SystemTime,
323 lifetime_days: u32,
324) -> Result<(Validity, SystemTime), X509CertError> {
325 const ONE_DAY: Duration = Duration::new(86400, 0);
326
327 let start_of_day_containing = |when| -> Result<_, X509CertError> {
328 let dt = DateTime::from_system_time(when)
329 .map_err(into_internal!("Couldn't represent time as a DER DateTime"))?;
330 let dt = DateTime::new(dt.year(), dt.month(), dt.day(), 0, 0, 0)
331 .map_err(into_internal!("Couldn't construct DER DateTime"))?;
332 Ok(x509_cert::time::Time::GeneralTime(
333 GeneralizedTime::from_date_time(dt),
334 ))
335 };
336
337 let start_on_day = now - ONE_DAY;
338 let end_on_day = start_on_day + ONE_DAY * lifetime_days;
339
340 let validity = Validity {
341 not_before: start_of_day_containing(start_on_day)?,
342 not_after: start_of_day_containing(end_on_day)?,
343 };
344 let expiration = validity.not_after.into();
345 Ok((validity, expiration))
346}
347
348fn random_serial_number<Rng: CryptoRng>(rng: &mut Rng) -> Result<SerialNumber, X509CertError> {
350 const SER_NUMBER_LEN: usize = 16;
351 let mut buf = [0; SER_NUMBER_LEN];
352 rng.fill_bytes(&mut buf[..]);
353 Ok(SerialNumber::new(&buf[..]).map_err(into_internal!("Couldn't construct serial number!"))?)
354}
355
356#[derive(Clone, Debug, thiserror::Error)]
358#[non_exhaustive]
359pub enum X509CertError {
360 #[error("Provided signing key not valid: {0}")]
362 InvalidSigningKey(String),
363
364 #[error("Couldn't use provided key as a subject")]
366 SubjectKeyError(#[from] x509_cert::spki::Error),
367
368 #[error("Unable to set hostname when creating certificate")]
371 InvalidHostname(#[source] x509_cert::der::Error),
372
373 #[error("Unable to build certificate")]
375 CouldNotBuild(#[source] Arc<x509_cert::builder::Error>),
376
377 #[error("Unable to encode certificate")]
379 CouldNotEncode(#[source] x509_cert::der::Error),
380
381 #[error("Unable to format key as PKCS8")]
383 CouldNotFormatPkcs8(#[source] p256::pkcs8::Error),
384
385 #[error("Internal error while creating certificate")]
387 Bug(#[from] tor_error::Bug),
388}
389
390impl 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)]
397mod test {
398 #![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 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 }
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}