use alloc::vec::Vec;
use alloc::string::String;
use alloc::borrow::ToOwned;
+use alloc::format;
+
+use core::cmp::{self, Ordering};
+use core::fmt::Write;
use crate::ser::*;
/// Note that we only currently support a handful of RR types as needed to generate and validate
/// TXT or TLSA record proofs.
pub enum RR {
+ /// An IPv4 resource record
+ A(A),
+ /// An IPv6 resource record
+ AAAA(AAAA),
+ /// A name server resource record
+ NS(NS),
/// A text resource record
Txt(Txt),
/// A TLS Certificate Association resource record
/// Gets the name this record refers to.
pub fn name(&self) -> &Name {
match self {
+ RR::A(rr) => &rr.name,
+ RR::AAAA(rr) => &rr.name,
+ RR::NS(rr) => &rr.name,
RR::Txt(rr) => &rr.name,
RR::CName(rr) => &rr.name,
RR::TLSA(rr) => &rr.name,
RR::RRSig(rr) => &rr.name,
}
}
+ /// Gets a JSON encoding of this record
+ pub fn json(&self) -> String {
+ match self {
+ RR::A(rr) => StaticRecord::json(rr),
+ RR::AAAA(rr) => StaticRecord::json(rr),
+ RR::NS(rr) => StaticRecord::json(rr),
+ RR::Txt(rr) => StaticRecord::json(rr),
+ RR::CName(rr) => StaticRecord::json(rr),
+ RR::TLSA(rr) => StaticRecord::json(rr),
+ RR::DnsKey(rr) => StaticRecord::json(rr),
+ RR::DS(rr) => StaticRecord::json(rr),
+ RR::RRSig(rr) => StaticRecord::json(rr),
+ }
+ }
fn ty(&self) -> u16 {
match self {
+ RR::A(_) => A::TYPE,
+ RR::AAAA(_) => AAAA::TYPE,
+ RR::NS(_) => NS::TYPE,
RR::Txt(_) => Txt::TYPE,
RR::CName(_) => CName::TYPE,
RR::TLSA(_) => TLSA::TYPE,
}
fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
match self {
+ RR::A(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
+ RR::AAAA(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
+ RR::NS(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
RR::Txt(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
RR::CName(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
RR::TLSA(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
}
}
}
+impl From<A> for RR { fn from(a: A) -> RR { RR::A(a) } }
+impl From<AAAA> for RR { fn from(aaaa: AAAA) -> RR { RR::AAAA(aaaa) } }
+impl From<NS> for RR { fn from(ns: NS) -> RR { RR::NS(ns) } }
impl From<Txt> for RR { fn from(txt: Txt) -> RR { RR::Txt(txt) } }
impl From<CName> for RR { fn from(cname: CName) -> RR { RR::CName(cname) } }
impl From<TLSA> for RR { fn from(tlsa: TLSA) -> RR { RR::TLSA(tlsa) } }
// http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
const TYPE: u16;
fn name(&self) -> &Name;
+ fn json(&self) -> String;
fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>);
- fn read_from_data(name: Name, data: &[u8]) -> Result<Self, ()>;
+ fn read_from_data(name: Name, data: &[u8], wire_packet: &[u8]) -> Result<Self, ()>;
}
/// A trait describing a resource record (including the [`RR`] enum).
pub trait Record : Ord {
fn ty(&self) -> u16;
/// The name this record is at.
fn name(&self) -> &Name;
+ /// Gets a JSON encoding of this record.
+ fn json(&self) -> String;
/// Writes the data of this record, prefixed by a u16 length, to the given `Vec`.
fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>);
}
impl<RR: StaticRecord> Record for RR {
fn ty(&self) -> u16 { RR::TYPE }
fn name(&self) -> &Name { RR::name(self) }
+ fn json(&self) -> String { RR::json(self) }
fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
RR::write_u16_len_prefixed_data(self, out)
}
impl Record for RR {
fn ty(&self) -> u16 { self.ty() }
fn name(&self) -> &Name { self.name() }
+ fn json(&self) -> String { self.json() }
fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
self.write_u16_len_prefixed_data(out)
}
}
-#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] // TODO: ord is wrong cause need to consider len first, maybe
+#[derive(Debug, Clone, PartialEq, Eq, Ord)]
/// A text resource record, containing arbitrary text data
pub struct Txt {
/// The name this record is at.
/// is an arbitrary series of bytes here.
pub data: Vec<u8>,
}
+impl PartialOrd for Txt {
+ fn partial_cmp(&self, o: &Txt) -> Option<Ordering> {
+ Some(self.name.cmp(&o.name)
+ .then_with(|| {
+ // Compare in wire encoding form, i.e. compare in 255-byte chunks
+ for i in 1..(self.data.len() / 255) + 2 {
+ let start = (i - 1)*255;
+ let self_len = cmp::min(i * 255, self.data.len());
+ let o_len = cmp::min(i * 255, o.data.len());
+ let slice_cmp = self_len.cmp(&o_len)
+ .then_with(|| self.data[start..self_len].cmp(&o.data[start..o_len]));
+ if !slice_cmp.is_eq() { return slice_cmp; }
+ }
+ Ordering::Equal
+ }))
+ }
+}
impl StaticRecord for Txt {
const TYPE: u16 = 16;
fn name(&self) -> &Name { &self.name }
- fn read_from_data(name: Name, mut data: &[u8]) -> Result<Self, ()> {
+ fn json(&self) -> String {
+ if let Ok(s) = core::str::from_utf8(&self.data) {
+ if s.chars().all(|c| !c.is_control() && c != '"') {
+ return format!("{{\"type\":\"txt\",\"name\":\"{}\",\"contents\":\"{}\"}}", self.name.0, s);
+ }
+ }
+ format!("{{\"type\":\"txt\",\"name\":\"{}\",\"contents\":{:?}}}", self.name.0, &self.data[..])
+ }
+ fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
let mut parsed_data = Vec::with_capacity(data.len() - 1);
while !data.is_empty() {
let len = read_u8(&mut data)? as usize;
Ok(Txt { name, data: parsed_data })
}
fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
- let len = (self.data.len() + self.data.len() / 255 + 1) as u16;
+ let len = (self.data.len() + (self.data.len() + 254) / 255) as u16;
out.extend_from_slice(&len.to_be_bytes());
let mut data_write = &self.data[..];
impl StaticRecord for TLSA {
const TYPE: u16 = 52;
fn name(&self) -> &Name { &self.name }
- fn read_from_data(name: Name, mut data: &[u8]) -> Result<Self, ()> {
+ fn json(&self) -> String {
+ let mut out = String::with_capacity(128+self.data.len()*2);
+ write!(&mut out,
+ "{{\"type\":\"tlsa\",\"name\":\"{}\",\"usage\":{},\"selector\":{},\"data_ty\":{},\"data\":\"",
+ self.name.0, self.cert_usage, self.selector, self.data_ty
+ ).expect("Write to a String shouldn't fail");
+ for c in self.data.iter() {
+ write!(&mut out, "{:02X}", c)
+ .expect("Write to a String shouldn't fail");
+ }
+ out += "\"}";
+ out
+ }
+ fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
Ok(TLSA {
name, cert_usage: read_u8(&mut data)?, selector: read_u8(&mut data)?,
data_ty: read_u8(&mut data)?, data: data.to_vec(),
impl StaticRecord for CName {
const TYPE: u16 = 5;
fn name(&self) -> &Name { &self.name }
- fn read_from_data(name: Name, mut data: &[u8]) -> Result<Self, ()> {
- Ok(CName { name, canonical_name: read_name(&mut data)? })
+ fn json(&self) -> String {
+ format!("{{\"type\":\"cname\",\"name\":\"{}\",\"canonical_name\":\"{}\"}}",
+ self.name.0, self.canonical_name.0)
+ }
+ fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
+ Ok(CName { name, canonical_name: read_wire_packet_name(&mut data, wire_packet)? })
}
fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
let len: u16 = name_len(&self.canonical_name);
impl StaticRecord for DnsKey {
const TYPE: u16 = 48;
fn name(&self) -> &Name { &self.name }
- fn read_from_data(name: Name, mut data: &[u8]) -> Result<Self, ()> {
+ fn json(&self) -> String {
+ let mut out = String::with_capacity(128+self.pubkey.len()*2);
+ write!(&mut out,
+ "{{\"type\":\"dnskey\",\"name\":\"{}\",\"flags\":{},\"protocol\":{},\"alg\":{},\"pubkey\":\"",
+ self.name.0, self.flags, self.protocol, self.alg
+ ).expect("Write to a String shouldn't fail");
+ for c in self.pubkey.iter() {
+ write!(&mut out, "{:02X}", c)
+ .expect("Write to a String shouldn't fail");
+ }
+ out += "\"}";
+ out
+ }
+ fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
Ok(DnsKey {
name, flags: read_u16(&mut data)?, protocol: read_u8(&mut data)?,
alg: read_u8(&mut data)?, pubkey: data.to_vec(),
impl StaticRecord for DS {
const TYPE: u16 = 43;
fn name(&self) -> &Name { &self.name }
- fn read_from_data(name: Name, mut data: &[u8]) -> Result<Self, ()> {
+ fn json(&self) -> String {
+ let mut out = String::with_capacity(128+self.digest.len()*2);
+ write!(&mut out,
+ "{{\"type\":\"ds\",\"name\":\"{}\",\"key_tag\":{},\"alg\":{},\"digest_type\":{},\"digest\":\"",
+ self.name.0, self.key_tag, self.alg, self.digest_type
+ ).expect("Write to a String shouldn't fail");
+ for c in self.digest.iter() {
+ write!(&mut out, "{:02X}", c)
+ .expect("Write to a String shouldn't fail");
+ }
+ out += "\"}";
+ out
+ }
+ fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
Ok(DS {
name, key_tag: read_u16(&mut data)?, alg: read_u8(&mut data)?,
digest_type: read_u8(&mut data)?, digest: data.to_vec(),
/// This must match the [`DnsKey::alg`] field in the [`DnsKey`] being used to sign.
pub alg: u8,
/// The number of labels in the name of the records that this signature is signing.
- // TODO: Describe this better in terms of wildcards
+ ///
+ /// If this is less than the number of labels in [`Self::name`], this signature is covering a
+ /// wildcard entry.
pub labels: u8,
/// The TTL of the records which this [`RRSig`] is signing.
pub orig_ttl: u32,
impl StaticRecord for RRSig {
const TYPE: u16 = 46;
fn name(&self) -> &Name { &self.name }
- fn read_from_data(name: Name, mut data: &[u8]) -> Result<Self, ()> {
+ fn json(&self) -> String {
+ let mut out = String::with_capacity(256 + self.signature.len()*2);
+ write!(&mut out,
+ "{{\"type\":\"ds\",\"name\":\"{}\",\"signed_record_type\":{},\"alg\":{},\"signed_labels\":{},\"orig_ttl\":{},\"expiration\"{},\"inception\":{},\"key_tag\":{},\"key_name\":\"{}\",\"signature\":\"",
+ self.name.0, self.ty, self.alg, self.labels, self.orig_ttl, self.expiration, self.inception, self.key_tag, self.key_name.0
+ ).expect("Write to a String shouldn't fail");
+ for c in self.signature.iter() {
+ write!(&mut out, "{:02X}", c)
+ .expect("Write to a String shouldn't fail");
+ }
+ out += "\"}";
+ out
+ }
+ fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
Ok(RRSig {
name, ty: read_u16(&mut data)?, alg: read_u8(&mut data)?,
labels: read_u8(&mut data)?, orig_ttl: read_u32(&mut data)?,
expiration: read_u32(&mut data)?, inception: read_u32(&mut data)?,
- key_tag: read_u16(&mut data)?, key_name: read_name(&mut data)?,
+ key_tag: read_u16(&mut data)?,
+ key_name: read_wire_packet_name(&mut data, wire_packet)?,
signature: data.to_vec(),
})
}
out.extend_from_slice(&self.signature);
}
}
+
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+/// An IPv4 Address resource record
+pub struct A {
+ /// The name this record is at.
+ pub name: Name,
+ /// The bytes of the IPv4 address.
+ pub address: [u8; 4],
+}
+impl StaticRecord for A {
+ const TYPE: u16 = 1;
+ fn name(&self) -> &Name { &self.name }
+ fn json(&self) -> String {
+ format!("{{\"type\":\"a\",\"name\":\"{}\",\"address\":{:?}}}", self.name.0, self.address)
+ }
+ fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
+ if data.len() != 4 { return Err(()); }
+ let mut address = [0; 4];
+ address.copy_from_slice(&data);
+ Ok(A { name, address })
+ }
+ fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
+ out.extend_from_slice(&4u16.to_be_bytes());
+ out.extend_from_slice(&self.address);
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+/// An IPv6 Address resource record
+pub struct AAAA {
+ /// The name this record is at.
+ pub name: Name,
+ /// The bytes of the IPv6 address.
+ pub address: [u8; 16],
+}
+impl StaticRecord for AAAA {
+ const TYPE: u16 = 28;
+ fn name(&self) -> &Name { &self.name }
+ fn json(&self) -> String {
+ format!("{{\"type\":\"aaaa\",\"name\":\"{}\",\"address\":{:?}}}", self.name.0, self.address)
+ }
+ fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
+ if data.len() != 16 { return Err(()); }
+ let mut address = [0; 16];
+ address.copy_from_slice(&data);
+ Ok(AAAA { name, address })
+ }
+ fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
+ out.extend_from_slice(&16u16.to_be_bytes());
+ out.extend_from_slice(&self.address);
+ }
+}
+
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+/// A Name Server resource record, which indicates the server responsible for handling queries for
+/// a zone.
+pub struct NS {
+ /// The name this record is at.
+ ///
+ /// This is also the zone which the server at [`Self::name_server`] is responsible for handling
+ /// queries for.
+ pub name: Name,
+ /// The name of the server which is responsible for handling queries for the [`Self::name`]
+ /// zone.
+ pub name_server: Name,
+}
+impl StaticRecord for NS {
+ const TYPE: u16 = 2;
+ fn name(&self) -> &Name { &self.name }
+ fn json(&self) -> String {
+ format!("{{\"type\":\"ns\",\"name\":\"{}\",\"ns\":\"{}\"}}", self.name.0, self.name_server.0)
+ }
+ fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
+ Ok(NS { name, name_server: read_wire_packet_name(&mut data, wire_packet)? })
+ }
+ fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
+ out.extend_from_slice(&name_len(&self.name_server).to_be_bytes());
+ write_name(out, &self.name_server);
+ }
+}