2023-12-04 17:21:55 +00:00
|
|
|
use log::info;
|
2024-08-27 07:08:17 +00:00
|
|
|
use rcgen::{date_time_ymd, CertificateParams, DnType, KeyPair};
|
2024-08-26 18:33:25 +00:00
|
|
|
use std::sync::LazyLock;
|
2024-08-27 07:08:17 +00:00
|
|
|
use std::time::Instant;
|
|
|
|
use tokio_rustls::rustls::pki_types::{CertificateDer, PrivateKeyDer, PrivatePkcs8KeyDer};
|
2023-10-01 15:16:23 +00:00
|
|
|
|
2024-08-27 07:08:17 +00:00
|
|
|
pub static TLS_CERTIFICATE: LazyLock<(Vec<CertificateDer<'static>>, PrivateKeyDer<'static>)> = LazyLock::new(|| {
|
|
|
|
info!("Generating self-signed tls certificate");
|
2023-12-04 17:21:55 +00:00
|
|
|
|
2024-08-27 07:08:17 +00:00
|
|
|
let now = Instant::now();
|
|
|
|
let key_pair = KeyPair::generate().unwrap();
|
|
|
|
let mut cert = CertificateParams::new(vec![]).unwrap();
|
|
|
|
cert.distinguished_name = rcgen::DistinguishedName::new();
|
|
|
|
cert.distinguished_name.push(DnType::CountryName, "FR".to_string());
|
|
|
|
let el = now.elapsed();
|
|
|
|
let year = 2024 - (el.as_nanos() % 2) as i32;
|
|
|
|
let month = 1 + (el.as_nanos() % 12) as u8;
|
|
|
|
let day = 1 + (el.as_nanos() % 31) as u8;
|
|
|
|
cert.not_before = date_time_ymd(year, month, day);
|
|
|
|
|
|
|
|
let el = now.elapsed();
|
|
|
|
let year = 2024 + (el.as_nanos() % 50) as i32;
|
|
|
|
let month = 1 + (el.as_nanos() % 12) as u8;
|
|
|
|
let day = 1 + (el.as_nanos() % 31) as u8;
|
|
|
|
cert.not_after = date_time_ymd(year, month, day);
|
2023-12-04 17:21:55 +00:00
|
|
|
|
2024-08-27 07:08:17 +00:00
|
|
|
let cert = cert.self_signed(&key_pair).unwrap().der().clone();
|
|
|
|
let private_key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(key_pair.serialized_der().to_vec()));
|
2023-10-01 15:16:23 +00:00
|
|
|
|
2024-08-27 07:08:17 +00:00
|
|
|
(vec![cert], private_key)
|
2023-10-01 15:16:23 +00:00
|
|
|
});
|