1 //! Resource Records are the fundamental type in the DNS - individual records mapping a name to
4 //! This module holds structs and utilities for the Resource Records supported by this crate.
7 use alloc::string::String;
8 use alloc::borrow::ToOwned;
12 /// A valid domain name.
14 /// It must end with a ".", be no longer than 255 bytes, consist of only printable ASCII
15 /// characters and each label may be no longer than 63 bytes.
16 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
17 pub struct Name(String);
19 /// Gets the underlying human-readable domain name
20 pub fn as_str(&self) -> &str { &self.0 }
22 impl core::ops::Deref for Name {
24 fn deref(&self) -> &str { &self.0 }
26 impl TryFrom<String> for Name {
28 fn try_from(s: String) -> Result<Name, ()> {
29 if s.is_empty() { return Err(()); }
30 if *s.as_bytes().last().unwrap_or(&0) != b"."[0] { return Err(()); }
31 if s.len() > 255 { return Err(()); }
32 if s.chars().any(|c| !c.is_ascii_graphic() && c != '.' && c != '-') { return Err(()); }
33 for label in s.split(".") {
34 if label.len() > 63 { return Err(()); }
40 impl TryFrom<&str> for Name {
42 fn try_from(s: &str) -> Result<Name, ()> {
43 Self::try_from(s.to_owned())
47 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
48 /// A supported Resource Record
50 /// Note that we only currently support a handful of RR types as needed to generate and validate
51 /// TXT or TLSA record proofs.
53 /// An IPv4 resource record
55 /// An IPv6 resource record
57 /// A name server resource record
59 /// A text resource record
61 /// A TLS Certificate Association resource record
63 /// A Canonical Name record
65 /// A DNS (Public) Key resource record
67 /// A Delegated Signer resource record
69 /// A Resource Record Signature record
73 /// Gets the name this record refers to.
74 pub fn name(&self) -> &Name {
76 RR::A(rr) => &rr.name,
77 RR::AAAA(rr) => &rr.name,
78 RR::NS(rr) => &rr.name,
79 RR::Txt(rr) => &rr.name,
80 RR::CName(rr) => &rr.name,
81 RR::TLSA(rr) => &rr.name,
82 RR::DnsKey(rr) => &rr.name,
83 RR::DS(rr) => &rr.name,
84 RR::RRSig(rr) => &rr.name,
90 RR::AAAA(_) => AAAA::TYPE,
91 RR::NS(_) => NS::TYPE,
92 RR::Txt(_) => Txt::TYPE,
93 RR::CName(_) => CName::TYPE,
94 RR::TLSA(_) => TLSA::TYPE,
95 RR::DnsKey(_) => DnsKey::TYPE,
96 RR::DS(_) => DS::TYPE,
97 RR::RRSig(_) => RRSig::TYPE,
100 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
102 RR::A(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
103 RR::AAAA(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
104 RR::NS(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
105 RR::Txt(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
106 RR::CName(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
107 RR::TLSA(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
108 RR::DnsKey(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
109 RR::DS(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
110 RR::RRSig(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
114 impl From<A> for RR { fn from(a: A) -> RR { RR::A(a) } }
115 impl From<AAAA> for RR { fn from(aaaa: AAAA) -> RR { RR::AAAA(aaaa) } }
116 impl From<NS> for RR { fn from(ns: NS) -> RR { RR::NS(ns) } }
117 impl From<Txt> for RR { fn from(txt: Txt) -> RR { RR::Txt(txt) } }
118 impl From<CName> for RR { fn from(cname: CName) -> RR { RR::CName(cname) } }
119 impl From<TLSA> for RR { fn from(tlsa: TLSA) -> RR { RR::TLSA(tlsa) } }
120 impl From<DnsKey> for RR { fn from(dnskey: DnsKey) -> RR { RR::DnsKey(dnskey) } }
121 impl From<DS> for RR { fn from(ds: DS) -> RR { RR::DS(ds) } }
122 impl From<RRSig> for RR { fn from(rrsig: RRSig) -> RR { RR::RRSig(rrsig) } }
124 pub(crate) trait StaticRecord : Ord + Sized {
125 // http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
127 fn name(&self) -> &Name;
128 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>);
129 fn read_from_data(name: Name, data: &[u8], wire_packet: &[u8]) -> Result<Self, ()>;
131 /// A trait describing a resource record (including the [`RR`] enum).
132 pub trait Record : Ord {
133 /// The resource record type, as maintained by IANA.
135 /// Current assignments can be found at
136 /// <http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4>
138 /// The name this record is at.
139 fn name(&self) -> &Name;
140 /// Writes the data of this record, prefixed by a u16 length, to the given `Vec`.
141 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>);
143 impl<RR: StaticRecord> Record for RR {
144 fn ty(&self) -> u16 { RR::TYPE }
145 fn name(&self) -> &Name { RR::name(self) }
146 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
147 RR::write_u16_len_prefixed_data(self, out)
151 fn ty(&self) -> u16 { self.ty() }
152 fn name(&self) -> &Name { self.name() }
153 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
154 self.write_u16_len_prefixed_data(out)
158 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] // TODO: ord is wrong cause need to consider len first, maybe
159 /// A text resource record, containing arbitrary text data
161 /// The name this record is at.
163 /// The text record itself.
165 /// While this is generally UTF-8-valid, there is no specific requirement that it be, and thus
166 /// is an arbitrary series of bytes here.
169 impl StaticRecord for Txt {
170 const TYPE: u16 = 16;
171 fn name(&self) -> &Name { &self.name }
172 fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
173 let mut parsed_data = Vec::with_capacity(data.len() - 1);
174 while !data.is_empty() {
175 let len = read_u8(&mut data)? as usize;
176 if data.len() < len { return Err(()); }
177 parsed_data.extend_from_slice(&data[..len]);
180 Ok(Txt { name, data: parsed_data })
182 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
183 let len = (self.data.len() + self.data.len() / 255 + 1) as u16;
184 out.extend_from_slice(&len.to_be_bytes());
186 let mut data_write = &self.data[..];
187 out.extend_from_slice(&[data_write.len().try_into().unwrap_or(255)]);
188 while !data_write.is_empty() {
189 let split_pos = core::cmp::min(255, data_write.len());
190 out.extend_from_slice(&data_write[..split_pos]);
191 data_write = &data_write[split_pos..];
192 if !data_write.is_empty() {
193 out.extend_from_slice(&[data_write.len().try_into().unwrap_or(255)]);
199 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
200 /// A TLS Certificate Association resource record containing information about the TLS certificate
201 /// which should be expected when communicating with the host at the given name.
203 /// See <https://en.wikipedia.org/wiki/DNS-based_Authentication_of_Named_Entities#TLSA_RR> for more
206 /// The name this record is at.
208 /// The type of constraint on the TLS certificate(s) used which should be enforced by this
211 /// Whether to match on the full certificate, or only the public key.
213 /// The type of data included which is used to match the TLS certificate(s).
215 /// The certificate data or hash of the certificate data itself.
218 impl StaticRecord for TLSA {
219 const TYPE: u16 = 52;
220 fn name(&self) -> &Name { &self.name }
221 fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
223 name, cert_usage: read_u8(&mut data)?, selector: read_u8(&mut data)?,
224 data_ty: read_u8(&mut data)?, data: data.to_vec(),
227 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
228 let len = 3 + self.data.len();
229 out.extend_from_slice(&(len as u16).to_be_bytes());
230 out.extend_from_slice(&[self.cert_usage, self.selector, self.data_ty]);
231 out.extend_from_slice(&self.data);
235 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
236 /// A Canonical Name resource record, referring all queries for this name to another name.
238 /// The name this record is at.
240 /// The canonical name.
242 /// A resolver should use this name when looking up any further records for [`Self::name`].
243 pub canonical_name: Name,
245 impl StaticRecord for CName {
247 fn name(&self) -> &Name { &self.name }
248 fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
249 Ok(CName { name, canonical_name: read_wire_packet_name(&mut data, wire_packet)? })
251 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
252 let len: u16 = name_len(&self.canonical_name);
253 out.extend_from_slice(&len.to_be_bytes());
254 write_name(out, &self.canonical_name);
258 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
259 /// A public key resource record which can be used to validate [`RRSig`]s.
261 /// The name this record is at.
263 /// Flags which constrain the usage of this public key.
265 /// The protocol this key is used for (protocol `3` is DNSSEC).
267 /// The algorithm which this public key uses to sign data.
269 /// The public key itself.
272 impl StaticRecord for DnsKey {
273 const TYPE: u16 = 48;
274 fn name(&self) -> &Name { &self.name }
275 fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
277 name, flags: read_u16(&mut data)?, protocol: read_u8(&mut data)?,
278 alg: read_u8(&mut data)?, pubkey: data.to_vec(),
281 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
282 let len = 2 + 1 + 1 + self.pubkey.len();
283 out.extend_from_slice(&(len as u16).to_be_bytes());
284 out.extend_from_slice(&self.flags.to_be_bytes());
285 out.extend_from_slice(&self.protocol.to_be_bytes());
286 out.extend_from_slice(&self.alg.to_be_bytes());
287 out.extend_from_slice(&self.pubkey);
291 /// A short (non-cryptographic) digest which can be used to refer to this [`DnsKey`].
292 pub fn key_tag(&self) -> u16 {
293 let mut res = u32::from(self.flags);
294 res += u32::from(self.protocol) << 8;
295 res += u32::from(self.alg);
296 for (idx, b) in self.pubkey.iter().enumerate() {
298 res += u32::from(*b) << 8;
300 res += u32::from(*b);
303 res += (res >> 16) & 0xffff;
304 (res & 0xffff) as u16
308 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
309 /// A Delegation Signer resource record which indicates that some alternative [`DnsKey`] can sign
310 /// for records in the zone which matches [`DS::name`].
312 /// The name this record is at.
314 /// This is also the zone that a [`DnsKey`] which matches the [`Self::digest`] can sign for.
316 /// A short tag which describes the matching [`DnsKey`].
318 /// This matches the [`DnsKey::key_tag`] for the [`DnsKey`] which is referred to by this
321 /// The algorithm which the [`DnsKey`] referred to by this [`DS`] uses.
323 /// This matches the [`DnsKey::alg`] field in the referred-to [`DnsKey`].
325 /// The type of digest used to hash the referred-to [`DnsKey`].
327 /// The digest itself.
330 impl StaticRecord for DS {
331 const TYPE: u16 = 43;
332 fn name(&self) -> &Name { &self.name }
333 fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
335 name, key_tag: read_u16(&mut data)?, alg: read_u8(&mut data)?,
336 digest_type: read_u8(&mut data)?, digest: data.to_vec(),
339 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
340 let len = 2 + 1 + 1 + self.digest.len();
341 out.extend_from_slice(&(len as u16).to_be_bytes());
342 out.extend_from_slice(&self.key_tag.to_be_bytes());
343 out.extend_from_slice(&self.alg.to_be_bytes());
344 out.extend_from_slice(&self.digest_type.to_be_bytes());
345 out.extend_from_slice(&self.digest);
349 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
350 /// A Resource Record (set) Signature resource record. This contains a signature over all the
351 /// resources records of the given type at the given name.
353 /// The name this record is at.
355 /// This is also the name of any records which this signature is covering (ignoring wildcards).
357 /// The resource record type which this [`RRSig`] is signing.
359 /// All resources records of this type at the same name as [`Self::name`] must be signed by
362 /// The algorithm which is being used to sign.
364 /// This must match the [`DnsKey::alg`] field in the [`DnsKey`] being used to sign.
366 /// The number of labels in the name of the records that this signature is signing.
367 // TODO: Describe this better in terms of wildcards
369 /// The TTL of the records which this [`RRSig`] is signing.
371 /// The expiration (as a UNIX timestamp) of this signature.
373 /// The time (as a UNIX timestamp) at which this signature becomes valid.
375 /// A short tag which describes the matching [`DnsKey`].
377 /// This matches the [`DnsKey::key_tag`] for the [`DnsKey`] which created this signature.
379 /// The [`DnsKey::name`] in the [`DnsKey`] which created this signature.
381 /// This must be a parent of the [`Self::name`].
383 /// The signature itself.
384 pub signature: Vec<u8>,
386 impl StaticRecord for RRSig {
387 const TYPE: u16 = 46;
388 fn name(&self) -> &Name { &self.name }
389 fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
391 name, ty: read_u16(&mut data)?, alg: read_u8(&mut data)?,
392 labels: read_u8(&mut data)?, orig_ttl: read_u32(&mut data)?,
393 expiration: read_u32(&mut data)?, inception: read_u32(&mut data)?,
394 key_tag: read_u16(&mut data)?,
395 key_name: read_wire_packet_name(&mut data, wire_packet)?,
396 signature: data.to_vec(),
399 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
400 let len = 2 + 1 + 1 + 4*3 + 2 + name_len(&self.key_name) + self.signature.len() as u16;
401 out.extend_from_slice(&len.to_be_bytes());
402 out.extend_from_slice(&self.ty.to_be_bytes());
403 out.extend_from_slice(&self.alg.to_be_bytes());
404 out.extend_from_slice(&self.labels.to_be_bytes());
405 out.extend_from_slice(&self.orig_ttl.to_be_bytes());
406 out.extend_from_slice(&self.expiration.to_be_bytes());
407 out.extend_from_slice(&self.inception.to_be_bytes());
408 out.extend_from_slice(&self.key_tag.to_be_bytes());
409 write_name(out, &self.key_name);
410 out.extend_from_slice(&self.signature);
414 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
415 /// An IPv4 Address resource record
417 /// The name this record is at.
419 /// The bytes of the IPv4 address.
420 pub address: [u8; 4],
422 impl StaticRecord for A {
424 fn name(&self) -> &Name { &self.name }
425 fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
426 if data.len() != 4 { return Err(()); }
427 let mut address = [0; 4];
428 address.copy_from_slice(&data);
429 Ok(A { name, address })
431 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
432 out.extend_from_slice(&4u16.to_be_bytes());
433 out.extend_from_slice(&self.address);
437 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
438 /// An IPv6 Address resource record
440 /// The name this record is at.
442 /// The bytes of the IPv6 address.
443 pub address: [u8; 16],
445 impl StaticRecord for AAAA {
446 const TYPE: u16 = 28;
447 fn name(&self) -> &Name { &self.name }
448 fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
449 if data.len() != 16 { return Err(()); }
450 let mut address = [0; 16];
451 address.copy_from_slice(&data);
452 Ok(AAAA { name, address })
454 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
455 out.extend_from_slice(&16u16.to_be_bytes());
456 out.extend_from_slice(&self.address);
460 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
461 /// A Name Server resource record, which indicates the server responsible for handling queries for
464 /// The name this record is at.
466 /// This is also the zone which the server at [`Self::name_server`] is responsible for handling
469 /// The name of the server which is responsible for handling queries for the [`Self::name`]
471 pub name_server: Name,
473 impl StaticRecord for NS {
475 fn name(&self) -> &Name { &self.name }
476 fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
477 Ok(NS { name, name_server: read_wire_packet_name(&mut data, wire_packet)? })
479 fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
480 out.extend_from_slice(&name_len(&self.name_server).to_be_bytes());
481 write_name(out, &self.name_server);