2
0
mirror of https://github.com/jedisct1/encrypted-dns-server synced 2024-11-12 13:10:44 +00:00
encrypted-dns-server/src/dnscrypt_certs.rs

168 lines
4.8 KiB
Rust
Raw Normal View History

2019-09-19 18:47:44 +00:00
use crate::config::*;
2019-09-02 21:02:23 +00:00
use crate::crypto::*;
2019-09-19 13:51:27 +00:00
use crate::globals::*;
2019-09-02 21:02:23 +00:00
use byteorder::{BigEndian, ByteOrder};
2019-09-19 14:40:08 +00:00
use coarsetime::Clock;
2019-09-02 21:02:23 +00:00
use std::mem;
use std::slice;
2019-09-19 13:51:27 +00:00
use std::sync::Arc;
2019-09-02 21:02:23 +00:00
2019-09-19 13:51:27 +00:00
pub const DNSCRYPT_CERTS_TTL: u32 = 86400;
pub const DNSCRYPT_CERTS_RENEWAL: u32 = 28800;
2019-09-02 21:02:23 +00:00
fn now() -> u32 {
2019-09-19 14:23:04 +00:00
Clock::now_since_epoch().as_secs() as u32
2019-09-02 21:02:23 +00:00
}
2019-09-19 18:47:44 +00:00
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
2019-09-02 21:02:23 +00:00
#[repr(C, packed)]
pub struct DNSCryptCertInner {
resolver_pk: [u8; 32],
client_magic: [u8; 8],
2019-09-02 23:10:35 +00:00
serial: [u8; 4],
2019-09-02 21:02:23 +00:00
ts_start: [u8; 4],
ts_end: [u8; 4],
}
impl DNSCryptCertInner {
pub fn as_bytes(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self as *const _ as *const u8, mem::size_of_val(self)) }
}
}
2019-09-19 18:47:44 +00:00
big_array! { BigArray; }
#[derive(Derivative, Serialize, Deserialize)]
2019-09-19 13:51:27 +00:00
#[derivative(Debug, Default, Clone)]
2019-09-02 21:02:23 +00:00
#[repr(C, packed)]
pub struct DNSCryptCert {
cert_magic: [u8; 4],
es_version: [u8; 2],
minor_version: [u8; 2],
#[derivative(Debug = "ignore", Default(value = "[0u8; 64]"))]
2019-09-19 18:47:44 +00:00
#[serde(with = "BigArray")]
2019-09-02 21:02:23 +00:00
signature: [u8; 64],
inner: DNSCryptCertInner,
}
impl DNSCryptCert {
2019-09-17 20:33:15 +00:00
pub fn new(provider_kp: &SignKeyPair, resolver_kp: &CryptKeyPair) -> Self {
2019-09-02 21:02:23 +00:00
let ts_start = now();
2019-09-19 13:51:27 +00:00
let ts_end = ts_start + DNSCRYPT_CERTS_TTL;
2019-09-02 21:02:23 +00:00
let mut dnscrypt_cert = DNSCryptCert::default();
let dnscrypt_cert_inner = &mut dnscrypt_cert.inner;
dnscrypt_cert_inner
.resolver_pk
.copy_from_slice(resolver_kp.pk.as_bytes());
dnscrypt_cert_inner
.client_magic
.copy_from_slice(&dnscrypt_cert_inner.resolver_pk[..8]);
2019-09-02 23:10:35 +00:00
BigEndian::write_u32(&mut dnscrypt_cert_inner.serial, 1);
2019-09-02 21:02:23 +00:00
BigEndian::write_u32(&mut dnscrypt_cert_inner.ts_start, ts_start);
BigEndian::write_u32(&mut dnscrypt_cert_inner.ts_end, ts_end);
BigEndian::write_u32(&mut dnscrypt_cert.cert_magic, 0x44_4e_53_43);
BigEndian::write_u16(&mut dnscrypt_cert.es_version, 2);
BigEndian::write_u16(&mut dnscrypt_cert.minor_version, 0);
dnscrypt_cert.signature.copy_from_slice(
2019-09-17 20:33:15 +00:00
provider_kp
2019-09-02 21:02:23 +00:00
.sk
.sign(dnscrypt_cert_inner.as_bytes())
.as_bytes(),
);
dnscrypt_cert
}
pub fn as_bytes(&self) -> &[u8] {
unsafe { slice::from_raw_parts(self as *const _ as *const u8, mem::size_of_val(self)) }
}
2019-09-17 20:33:15 +00:00
pub fn client_magic(&self) -> &[u8] {
&self.inner.client_magic
}
2019-09-18 11:40:05 +00:00
2019-09-19 14:23:04 +00:00
pub fn ts_end(&self) -> u32 {
BigEndian::read_u32(&self.inner.ts_end)
2019-09-18 11:40:05 +00:00
}
2019-09-17 20:33:15 +00:00
}
#[derive(Serialize, Deserialize, Debug, Clone)]
2019-09-17 20:33:15 +00:00
pub struct DNSCryptEncryptionParams {
dnscrypt_cert: DNSCryptCert,
resolver_kp: CryptKeyPair,
}
impl DNSCryptEncryptionParams {
pub fn new(provider_kp: &SignKeyPair) -> Self {
let resolver_kp = CryptKeyPair::new();
let dnscrypt_cert = DNSCryptCert::new(&provider_kp, &resolver_kp);
DNSCryptEncryptionParams {
dnscrypt_cert,
resolver_kp,
}
}
pub fn client_magic(&self) -> &[u8] {
self.dnscrypt_cert.client_magic()
}
pub fn dnscrypt_cert(&self) -> &DNSCryptCert {
&self.dnscrypt_cert
}
pub fn resolver_kp(&self) -> &CryptKeyPair {
&self.resolver_kp
}
2019-09-02 21:02:23 +00:00
}
2019-09-19 13:51:27 +00:00
pub struct DNSCryptEncryptionParamsUpdater {
globals: Arc<Globals>,
}
impl DNSCryptEncryptionParamsUpdater {
pub fn new(globals: Arc<Globals>) -> Self {
DNSCryptEncryptionParamsUpdater { globals }
}
2019-09-19 14:23:04 +00:00
fn update(&self) {
let now = now();
let mut new_params_set = vec![];
{
let params_set = self.globals.dnscrypt_encryption_params_set.read();
for params in &**params_set {
if params.dnscrypt_cert().ts_end() >= now {
new_params_set.push(params.clone());
}
}
}
let new_params = DNSCryptEncryptionParams::new(&self.globals.provider_kp);
new_params_set.push(Arc::new(new_params));
2019-09-19 18:47:44 +00:00
let state = State {
provider_kp: self.globals.provider_kp.clone(),
dnscrypt_encryption_params_set: new_params_set.iter().map(|x| (**x).clone()).collect(),
2019-09-19 18:47:44 +00:00
};
2019-09-20 00:31:31 +00:00
let state_file = self.globals.state_file.to_path_buf();
self.globals.runtime.spawn(async move {
let _ = state.async_save(state_file).await;
});
2019-09-19 14:23:04 +00:00
*self.globals.dnscrypt_encryption_params_set.write() = Arc::new(new_params_set);
}
2019-09-19 13:51:27 +00:00
pub async fn run(self) {
let mut fut_interval = tokio::timer::Interval::new_interval(
std::time::Duration::from_secs(u64::from(DNSCRYPT_CERTS_RENEWAL)),
);
2019-09-19 14:23:04 +00:00
let fut = async move {
while fut_interval.next().await.is_some() {
self.update();
2019-09-19 13:51:27 +00:00
debug!("New cert issued");
}
};
fut.await
}
}