Add support for DNAME resolution
[dnssec-prover] / src / rr.rs
index 9886e3a71ceff967da990ffc8fa882d806650241..c3367a642d3f38044b9226ad20e80360a4fb5a92 100644 (file)
--- a/src/rr.rs
+++ b/src/rr.rs
@@ -9,6 +9,7 @@ use alloc::borrow::ToOwned;
 use alloc::format;
 
 use core::cmp::{self, Ordering};
+use core::fmt;
 use core::fmt::Write;
 
 use crate::ser::*;
@@ -27,6 +28,11 @@ impl core::ops::Deref for Name {
        type Target = str;
        fn deref(&self) -> &str { &self.0 }
 }
+impl fmt::Display for Name {
+       fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+               self.0.fmt(f)
+       }
+}
 impl TryFrom<String> for Name {
        type Error = ();
        fn try_from(s: String) -> Result<Name, ()> {
@@ -66,6 +72,8 @@ pub enum RR {
        TLSA(TLSA),
        /// A Canonical Name record
        CName(CName),
+       /// A Delegation Name record
+       DName(DName),
        /// A DNS (Public) Key resource record
        DnsKey(DnsKey),
        /// A Delegated Signer resource record
@@ -82,6 +90,7 @@ impl RR {
                        RR::NS(rr) => &rr.name,
                        RR::Txt(rr) => &rr.name,
                        RR::CName(rr) => &rr.name,
+                       RR::DName(rr) => &rr.name,
                        RR::TLSA(rr) => &rr.name,
                        RR::DnsKey(rr) => &rr.name,
                        RR::DS(rr) => &rr.name,
@@ -96,6 +105,7 @@ impl RR {
                        RR::NS(rr) => StaticRecord::json(rr),
                        RR::Txt(rr) => StaticRecord::json(rr),
                        RR::CName(rr) => StaticRecord::json(rr),
+                       RR::DName(rr) => StaticRecord::json(rr),
                        RR::TLSA(rr) => StaticRecord::json(rr),
                        RR::DnsKey(rr) => StaticRecord::json(rr),
                        RR::DS(rr) => StaticRecord::json(rr),
@@ -109,6 +119,7 @@ impl RR {
                        RR::NS(_) => NS::TYPE,
                        RR::Txt(_) => Txt::TYPE,
                        RR::CName(_) => CName::TYPE,
+                       RR::DName(_) => DName::TYPE,
                        RR::TLSA(_) => TLSA::TYPE,
                        RR::DnsKey(_) => DnsKey::TYPE,
                        RR::DS(_) => DS::TYPE,
@@ -122,6 +133,7 @@ impl RR {
                        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::DName(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
                        RR::TLSA(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
                        RR::DnsKey(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
                        RR::DS(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
@@ -134,6 +146,7 @@ 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<DName> for RR { fn from(cname: DName) -> RR { RR::DName(cname) } }
 impl From<TLSA> for RR { fn from(tlsa: TLSA) -> RR { RR::TLSA(tlsa) } }
 impl From<DnsKey> for RR { fn from(dnskey: DnsKey) -> RR { RR::DnsKey(dnskey) } }
 impl From<DS> for RR { fn from(ds: DS) -> RR { RR::DS(ds) } }
@@ -189,6 +202,8 @@ pub struct Txt {
        /// is an arbitrary series of bytes here.
        pub data: Vec<u8>,
 }
+/// The wire type for TXT records
+pub const TXT_TYPE: u16 = 16;
 impl PartialOrd for Txt {
        fn partial_cmp(&self, o: &Txt) -> Option<Ordering> {
                Some(self.name.cmp(&o.name)
@@ -207,7 +222,7 @@ impl PartialOrd for Txt {
        }
 }
 impl StaticRecord for Txt {
-       const TYPE: u16 = 16;
+       const TYPE: u16 = TXT_TYPE;
        fn name(&self) -> &Name { &self.name }
        fn json(&self) -> String {
                if let Ok(s) = core::str::from_utf8(&self.data) {
@@ -218,7 +233,7 @@ impl StaticRecord for Txt {
                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);
+               let mut parsed_data = Vec::with_capacity(data.len().saturating_sub(1));
                while !data.is_empty() {
                        let len = read_u8(&mut data)? as usize;
                        if data.len() < len { return Err(()); }
@@ -263,8 +278,10 @@ pub struct TLSA {
        /// The certificate data or hash of the certificate data itself.
        pub data: Vec<u8>,
 }
+/// The wire type for TLSA records
+pub const TLSA_TYPE: u16 = 52;
 impl StaticRecord for TLSA {
-       const TYPE: u16 = 52;
+       const TYPE: u16 = TLSA_TYPE;
        fn name(&self) -> &Name { &self.name }
        fn json(&self) -> String {
                let mut out = String::with_capacity(128+self.data.len()*2);
@@ -320,6 +337,36 @@ impl StaticRecord for CName {
        }
 }
 
+#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
+/// A Delegation Name resource record, referring all queries for subdomains of this name to another
+/// subtree of the DNS.
+pub struct DName {
+       /// The name this record is at.
+       pub name: Name,
+       /// The delegation name.
+       ///
+       /// A resolver should use this domain name tree when looking up any further records for
+       /// subdomains of [`self.name`].
+       pub delegation_name: Name,
+}
+impl StaticRecord for DName {
+       const TYPE: u16 = 39;
+       fn name(&self) -> &Name { &self.name }
+       fn json(&self) -> String {
+               format!("{{\"type\":\"dname\",\"name\":\"{}\",\"delegation_name\":\"{}\"}}",
+                       self.name.0, self.delegation_name.0)
+       }
+       fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
+               Ok(DName { name, delegation_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.delegation_name);
+               out.extend_from_slice(&len.to_be_bytes());
+               write_name(out, &self.delegation_name);
+       }
+}
+
+
 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
 /// A public key resource record which can be used to validate [`RRSig`]s.
 pub struct DnsKey {
@@ -529,8 +576,10 @@ pub struct A {
        /// The bytes of the IPv4 address.
        pub address: [u8; 4],
 }
+/// The wire type for A records
+pub const A_TYPE: u16 = 1;
 impl StaticRecord for A {
-       const TYPE: u16 = 1;
+       const TYPE: u16 = A_TYPE;
        fn name(&self) -> &Name { &self.name }
        fn json(&self) -> String {
                format!("{{\"type\":\"a\",\"name\":\"{}\",\"address\":{:?}}}", self.name.0, self.address)
@@ -555,8 +604,10 @@ pub struct AAAA {
        /// The bytes of the IPv6 address.
        pub address: [u8; 16],
 }
+/// The wire type for AAAA records
+pub const AAAA_TYPE: u16 = 28;
 impl StaticRecord for AAAA {
-       const TYPE: u16 = 28;
+       const TYPE: u16 = AAAA_TYPE;
        fn name(&self) -> &Name { &self.name }
        fn json(&self) -> String {
                format!("{{\"type\":\"aaaa\",\"name\":\"{}\",\"address\":{:?}}}", self.name.0, self.address)