X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Frr.rs;h=c3367a642d3f38044b9226ad20e80360a4fb5a92;hb=8b836e295ec81c50907fe1f60ca43c82df792025;hp=41c0ee2df8d13ff8dcd6de0f667db01d3e9fe805;hpb=fb95ca84534249078d82f9d0b26ceb1e52427330;p=dnssec-prover diff --git a/src/rr.rs b/src/rr.rs index 41c0ee2..c3367a6 100644 --- a/src/rr.rs +++ b/src/rr.rs @@ -6,8 +6,11 @@ use alloc::vec::Vec; use alloc::string::String; use alloc::borrow::ToOwned; +use alloc::format; use core::cmp::{self, Ordering}; +use core::fmt; +use core::fmt::Write; use crate::ser::*; @@ -25,13 +28,18 @@ 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 for Name { type Error = (); fn try_from(s: String) -> Result { if s.is_empty() { return Err(()); } if *s.as_bytes().last().unwrap_or(&0) != b"."[0] { return Err(()); } if s.len() > 255 { return Err(()); } - if s.chars().any(|c| !c.is_ascii_graphic() && c != '.' && c != '-') { return Err(()); } + if s.chars().any(|c| !c.is_ascii_graphic() || c == '"') { return Err(()); } for label in s.split(".") { if label.len() > 63 { return Err(()); } } @@ -64,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 @@ -80,12 +90,28 @@ 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, 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::DName(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, @@ -93,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, @@ -106,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), @@ -118,6 +146,7 @@ impl From for RR { fn from(aaaa: AAAA) -> RR { RR::AAAA(aaaa) } } impl From for RR { fn from(ns: NS) -> RR { RR::NS(ns) } } impl From for RR { fn from(txt: Txt) -> RR { RR::Txt(txt) } } impl From for RR { fn from(cname: CName) -> RR { RR::CName(cname) } } +impl From for RR { fn from(cname: DName) -> RR { RR::DName(cname) } } impl From for RR { fn from(tlsa: TLSA) -> RR { RR::TLSA(tlsa) } } impl From for RR { fn from(dnskey: DnsKey) -> RR { RR::DnsKey(dnskey) } } impl From for RR { fn from(ds: DS) -> RR { RR::DS(ds) } } @@ -127,6 +156,7 @@ pub(crate) trait StaticRecord : Ord + Sized { // 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); fn read_from_data(name: Name, data: &[u8], wire_packet: &[u8]) -> Result; } @@ -139,12 +169,15 @@ 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); } impl 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) { RR::write_u16_len_prefixed_data(self, out) } @@ -152,6 +185,7 @@ impl Record for RR { 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) { self.write_u16_len_prefixed_data(out) } @@ -168,6 +202,8 @@ pub struct Txt { /// is an arbitrary series of bytes here. pub data: Vec, } +/// The wire type for TXT records +pub const TXT_TYPE: u16 = 16; impl PartialOrd for Txt { fn partial_cmp(&self, o: &Txt) -> Option { Some(self.name.cmp(&o.name) @@ -186,10 +222,18 @@ 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) { + 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 { - 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(()); } @@ -234,9 +278,24 @@ pub struct TLSA { /// The certificate data or hash of the certificate data itself. pub data: Vec, } +/// 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); + 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 { Ok(TLSA { name, cert_usage: read_u8(&mut data)?, selector: read_u8(&mut data)?, @@ -258,12 +317,16 @@ pub struct CName { pub name: Name, /// The canonical name. /// - /// A resolver should use this name when looking up any further records for [`Self::name`]. + /// A resolver should use this name when looking up any further records for [`self.name`]. pub canonical_name: Name, } impl StaticRecord for CName { const TYPE: u16 = 5; fn name(&self) -> &Name { &self.name } + 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 { Ok(CName { name, canonical_name: read_wire_packet_name(&mut data, wire_packet)? }) } @@ -274,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 { + Ok(DName { name, delegation_name: read_wire_packet_name(&mut data, wire_packet)? }) + } + fn write_u16_len_prefixed_data(&self, out: &mut Vec) { + 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 { @@ -291,6 +384,19 @@ pub struct DnsKey { impl StaticRecord for DnsKey { const TYPE: u16 = 48; fn name(&self) -> &Name { &self.name } + 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 { Ok(DnsKey { name, flags: read_u16(&mut data)?, protocol: read_u8(&mut data)?, @@ -326,7 +432,7 @@ impl DnsKey { #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] /// A Delegation Signer resource record which indicates that some alternative [`DnsKey`] can sign -/// for records in the zone which matches [`DS::name`]. +/// for records in the zone which matches [`self.name`]. pub struct DS { /// The name this record is at. /// @@ -349,6 +455,19 @@ pub struct DS { impl StaticRecord for DS { const TYPE: u16 = 43; fn name(&self) -> &Name { &self.name } + 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 { Ok(DS { name, key_tag: read_u16(&mut data)?, alg: read_u8(&mut data)?, @@ -375,7 +494,7 @@ pub struct RRSig { pub name: Name, /// The resource record type which this [`RRSig`] is signing. /// - /// All resources records of this type at the same name as [`Self::name`] must be signed by + /// All resources records of this type at the same name as [`self.name`] must be signed by /// this [`RRSig`]. pub ty: u16, /// The algorithm which is being used to sign. @@ -384,7 +503,7 @@ pub struct RRSig { pub alg: u8, /// The number of labels in the name of the records that this signature is signing. /// - /// If this is less than the number of labels in [`Self::name`], this signature is covering a + /// 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. @@ -399,7 +518,11 @@ pub struct RRSig { pub key_tag: u16, /// The [`DnsKey::name`] in the [`DnsKey`] which created this signature. /// - /// This must be a parent of the [`Self::name`]. + /// This must be a parent of [`self.name`]. + /// + /// [`DnsKey::name`]: Record::name + // We'd like to just link to the `DnsKey` member variable called `name`, but there doesn't + // appear to be a way to actually do that, so instead we have to link to the trait method. pub key_name: Name, /// The signature itself. pub signature: Vec, @@ -407,6 +530,19 @@ pub struct RRSig { impl StaticRecord for RRSig { const TYPE: u16 = 46; fn name(&self) -> &Name { &self.name } + 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 { Ok(RRSig { name, ty: read_u16(&mut data)?, alg: read_u8(&mut data)?, @@ -440,9 +576,14 @@ 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) + } fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result { if data.len() != 4 { return Err(()); } let mut address = [0; 4]; @@ -463,9 +604,14 @@ 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) + } fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result { if data.len() != 16 { return Err(()); } let mut address = [0; 16]; @@ -487,13 +633,16 @@ pub struct NS { /// 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`] + /// 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 { Ok(NS { name, name_server: read_wire_packet_name(&mut data, wire_packet)? }) }