use base64::Engine as _;
use hickory_proto::op::Message;
use hickory_proto::rr::rdata::tsig::{TSIG, TsigAlgorithm, make_tsig_record};
use hickory_proto::rr::{Name, RData};
use hickory_proto::serialize::binary::{BinEncodable, BinEncoder};
use hmac::{Hmac, Mac};
use sha2::Sha256;
use crate::error::{DriverError, Result};
pub const FUDGE: u16 = 300;
const SERVICE: &str = "dns";
#[derive(Clone)]
pub struct TsigKey {
name: Name,
secret: Vec<u8>,
}
impl std::fmt::Debug for TsigKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TsigKey").field("name", &self.name.to_ascii()).finish_non_exhaustive()
}
}
impl TsigKey {
pub fn hmac_sha256(name: &str, secret: &str) -> Result<Self> {
let mut name = Name::from_ascii(name).map_err(|error| {
DriverError::invalid(format!("the TSIG key name is not a domain name: {error}"))
})?;
name.set_fqdn(true);
let secret = base64::engine::general_purpose::STANDARD
.decode(secret.trim())
.map_err(|_| DriverError::invalid("the TSIG secret is not base64"))?;
if secret.len() < 16 {
return Err(DriverError::invalid("the TSIG secret is shorter than 16 bytes"));
}
Ok(Self { name: name.to_lowercase(), secret })
}
pub fn name(&self) -> String {
self.name.to_ascii()
}
fn mac(&self, data: &[u8]) -> Vec<u8> {
let mut mac =
Hmac::<Sha256>::new_from_slice(&self.secret).expect("HMAC takes a key of any length");
mac.update(data);
mac.finalize().into_bytes().to_vec()
}
pub fn sign(
&self,
wire: &[u8],
prior_mac: Option<&[u8]>,
time: u64,
) -> Result<(Vec<u8>, Vec<u8>)> {
if wire.len() < 12 {
return Err(DriverError::invalid("a DNS message is at least 12 bytes"));
}
let id = u16::from_be_bytes([wire[0], wire[1]]);
let unsigned =
TSIG::new(TsigAlgorithm::HmacSha256, time, FUDGE, Vec::new(), id, None, Vec::new());
let mut tbs = Vec::with_capacity(wire.len() + 128);
if let Some(prior) = prior_mac {
tbs.extend_from_slice(&(prior.len() as u16).to_be_bytes());
tbs.extend_from_slice(prior);
}
tbs.extend_from_slice(wire);
emit_variables(&mut tbs, &unsigned, &self.name)?;
let mac = self.mac(&tbs);
let record = make_tsig_record(self.name.clone(), unsigned.set_mac(mac.clone()));
let tail = record.to_bytes().map_err(|error| DriverError::invalid(error.to_string()))?;
let mut signed = wire.to_vec();
let additionals = u16::from_be_bytes([signed[10], signed[11]]).saturating_add(1);
signed[10..12].copy_from_slice(&additionals.to_be_bytes());
signed.extend_from_slice(&tail);
Ok((signed, mac))
}
pub fn verify(
&self,
wire: &[u8],
prior_mac: Option<&[u8]>,
first: bool,
now: u64,
) -> Result<Vec<u8>> {
let refused = |reason: &str| DriverError::Unauthorized {
service: SERVICE.into(),
reason: reason.into(),
};
let message = Message::from_vec(wire)
.map_err(|error| DriverError::protocol(SERVICE, error.to_string()))?;
let (signer, tsig) = match message.signature {
Some(record) => (record.name.clone(), record.data.clone()),
None => match message.additionals.last() {
Some(record) => match &record.data {
RData::TSIG(tsig) => (record.name.clone(), tsig.clone()),
_ => return Err(refused("the message is not signed with TSIG")),
},
None => return Err(refused("the message is not signed with TSIG")),
},
};
if signer.to_lowercase() != self.name {
return Err(refused("the message is signed with another TSIG key"));
}
let tsig = &tsig;
if tsig.algorithm != TsigAlgorithm::HmacSha256 {
return Err(refused("the message is signed with an algorithm other than hmac-sha256"));
}
if let Some(error) = &tsig.error {
return Err(refused(&format!("the peer reported TSIG error {error:?}")));
}
if now.abs_diff(tsig.time) > u64::from(tsig.fudge) {
return Err(refused("the TSIG time is outside the allowed fudge; check both clocks"));
}
let start = last_record_start(wire)
.ok_or_else(|| DriverError::protocol(SERVICE, "the signed message is truncated"))?;
let mut stripped = wire[..start].to_vec();
stripped[0..2].copy_from_slice(&tsig.oid.to_be_bytes());
let additionals = u16::from_be_bytes([stripped[10], stripped[11]]).saturating_sub(1);
stripped[10..12].copy_from_slice(&additionals.to_be_bytes());
let mut tbs = Vec::with_capacity(wire.len() + 64);
if let Some(prior) = prior_mac {
tbs.extend_from_slice(&(prior.len() as u16).to_be_bytes());
tbs.extend_from_slice(prior);
}
tbs.extend_from_slice(&stripped);
if first {
emit_variables(&mut tbs, tsig, &signer)?;
} else {
tbs.extend_from_slice(&((tsig.time >> 32) as u16).to_be_bytes());
tbs.extend_from_slice(&(tsig.time as u32).to_be_bytes());
tbs.extend_from_slice(&tsig.fudge.to_be_bytes());
}
let mut mac =
Hmac::<Sha256>::new_from_slice(&self.secret).expect("HMAC takes a key of any length");
mac.update(&tbs);
mac.verify_slice(&tsig.mac).map_err(|_| refused("the TSIG MAC does not match"))?;
Ok(tsig.mac.clone())
}
}
fn emit_variables(out: &mut Vec<u8>, tsig: &TSIG, key: &Name) -> Result<()> {
let mut variables = Vec::with_capacity(64);
let mut encoder = BinEncoder::new(&mut variables);
tsig.emit_tsig_for_mac(&mut encoder, key)
.map_err(|error| DriverError::invalid(error.to_string()))?;
out.extend_from_slice(&variables);
Ok(())
}
pub(crate) fn last_record_start(wire: &[u8]) -> Option<usize> {
let count = |at: usize| -> Option<usize> {
Some(usize::from(u16::from_be_bytes([*wire.get(at)?, *wire.get(at + 1)?])))
};
let questions = count(4)?;
let records = count(6)? + count(8)? + count(10)?;
if records == 0 {
return None;
}
let mut at = 12;
for _ in 0..questions {
at = skip_name(wire, at)? + 4;
}
let mut last = at;
for _ in 0..records {
last = at;
at = skip_name(wire, at)?;
let length = usize::from(u16::from_be_bytes([*wire.get(at + 8)?, *wire.get(at + 9)?]));
at += 10 + length;
if at > wire.len() {
return None;
}
}
Some(last)
}
fn skip_name(wire: &[u8], mut at: usize) -> Option<usize> {
loop {
let length = *wire.get(at)?;
match length {
0 => return Some(at + 1),
length if length & 0xC0 == 0xC0 => return Some(at + 2),
length => at += 1 + usize::from(length),
}
}
}
pub(crate) fn now() -> u64 {
u64::try_from(chrono::Utc::now().timestamp()).unwrap_or_default()
}