Add note about DoH proof building usage.
[dnssec-prover] / src / rr.rs
1 //! Resource Records are the fundamental type in the DNS - individual records mapping a name to
2 //! some data.
3 //!
4 //! This module holds structs and utilities for the Resource Records supported by this crate.
5
6 use alloc::vec::Vec;
7 use alloc::string::String;
8 use alloc::borrow::ToOwned;
9 use alloc::format;
10
11 use core::cmp::{self, Ordering};
12 use core::fmt;
13 use core::fmt::Write;
14
15 use crate::ser::*;
16
17 /// A valid domain name.
18 ///
19 /// It must end with a ".", be no longer than 255 bytes, consist of only printable ASCII
20 /// characters and each label may be no longer than 63 bytes.
21 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
22 pub struct Name(String);
23 impl Name {
24         /// Gets the underlying human-readable domain name
25         pub fn as_str(&self) -> &str { &self.0 }
26 }
27 impl core::ops::Deref for Name {
28         type Target = str;
29         fn deref(&self) -> &str { &self.0 }
30 }
31 impl fmt::Display for Name {
32         fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
33                 self.0.fmt(f)
34         }
35 }
36 impl TryFrom<String> for Name {
37         type Error = ();
38         fn try_from(s: String) -> Result<Name, ()> {
39                 if s.is_empty() { return Err(()); }
40                 if *s.as_bytes().last().unwrap_or(&0) != b"."[0] { return Err(()); }
41                 if s.len() > 255 { return Err(()); }
42                 if s.chars().any(|c| !c.is_ascii_graphic() || c == '"') { return Err(()); }
43                 for label in s.split(".") {
44                         if label.len() > 63 { return Err(()); }
45                 }
46
47                 Ok(Name(s))
48         }
49 }
50 impl TryFrom<&str> for Name {
51         type Error = ();
52         fn try_from(s: &str) -> Result<Name, ()> {
53                 Self::try_from(s.to_owned())
54         }
55 }
56
57 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
58 /// A supported Resource Record
59 ///
60 /// Note that we only currently support a handful of RR types as needed to generate and validate
61 /// TXT or TLSA record proofs.
62 pub enum RR {
63         /// An IPv4 resource record
64         A(A),
65         /// An IPv6 resource record
66         AAAA(AAAA),
67         /// A name server resource record
68         NS(NS),
69         /// A text resource record
70         Txt(Txt),
71         /// A TLS Certificate Association resource record
72         TLSA(TLSA),
73         /// A Canonical Name record
74         CName(CName),
75         /// A DNS (Public) Key resource record
76         DnsKey(DnsKey),
77         /// A Delegated Signer resource record
78         DS(DS),
79         /// A Resource Record Signature record
80         RRSig(RRSig),
81 }
82 impl RR {
83         /// Gets the name this record refers to.
84         pub fn name(&self) -> &Name {
85                 match self {
86                         RR::A(rr) => &rr.name,
87                         RR::AAAA(rr) => &rr.name,
88                         RR::NS(rr) => &rr.name,
89                         RR::Txt(rr) => &rr.name,
90                         RR::CName(rr) => &rr.name,
91                         RR::TLSA(rr) => &rr.name,
92                         RR::DnsKey(rr) => &rr.name,
93                         RR::DS(rr) => &rr.name,
94                         RR::RRSig(rr) => &rr.name,
95                 }
96         }
97         /// Gets a JSON encoding of this record
98         pub fn json(&self) -> String {
99                 match self {
100                         RR::A(rr) => StaticRecord::json(rr),
101                         RR::AAAA(rr) => StaticRecord::json(rr),
102                         RR::NS(rr) => StaticRecord::json(rr),
103                         RR::Txt(rr) => StaticRecord::json(rr),
104                         RR::CName(rr) => StaticRecord::json(rr),
105                         RR::TLSA(rr) => StaticRecord::json(rr),
106                         RR::DnsKey(rr) => StaticRecord::json(rr),
107                         RR::DS(rr) => StaticRecord::json(rr),
108                         RR::RRSig(rr) => StaticRecord::json(rr),
109                 }
110         }
111         fn ty(&self) -> u16 {
112                 match self {
113                         RR::A(_) => A::TYPE,
114                         RR::AAAA(_) => AAAA::TYPE,
115                         RR::NS(_) => NS::TYPE,
116                         RR::Txt(_) => Txt::TYPE,
117                         RR::CName(_) => CName::TYPE,
118                         RR::TLSA(_) => TLSA::TYPE,
119                         RR::DnsKey(_) => DnsKey::TYPE,
120                         RR::DS(_) => DS::TYPE,
121                         RR::RRSig(_) => RRSig::TYPE,
122                 }
123         }
124         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
125                 match self {
126                         RR::A(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
127                         RR::AAAA(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
128                         RR::NS(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
129                         RR::Txt(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
130                         RR::CName(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
131                         RR::TLSA(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
132                         RR::DnsKey(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
133                         RR::DS(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
134                         RR::RRSig(rr) => StaticRecord::write_u16_len_prefixed_data(rr, out),
135                 }
136         }
137 }
138 impl From<A> for RR { fn from(a: A) -> RR { RR::A(a) } }
139 impl From<AAAA> for RR { fn from(aaaa: AAAA) -> RR { RR::AAAA(aaaa) } }
140 impl From<NS> for RR { fn from(ns: NS) -> RR { RR::NS(ns) } }
141 impl From<Txt> for RR { fn from(txt: Txt) -> RR { RR::Txt(txt) } }
142 impl From<CName> for RR { fn from(cname: CName) -> RR { RR::CName(cname) } }
143 impl From<TLSA> for RR { fn from(tlsa: TLSA) -> RR { RR::TLSA(tlsa) } }
144 impl From<DnsKey> for RR { fn from(dnskey: DnsKey) -> RR { RR::DnsKey(dnskey) } }
145 impl From<DS> for RR { fn from(ds: DS) -> RR { RR::DS(ds) } }
146 impl From<RRSig> for RR { fn from(rrsig: RRSig) -> RR { RR::RRSig(rrsig) } }
147
148 pub(crate) trait StaticRecord : Ord + Sized {
149         // http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
150         const TYPE: u16;
151         fn name(&self) -> &Name;
152         fn json(&self) -> String;
153         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>);
154         fn read_from_data(name: Name, data: &[u8], wire_packet: &[u8]) -> Result<Self, ()>;
155 }
156 /// A trait describing a resource record (including the [`RR`] enum).
157 pub trait Record : Ord {
158         /// The resource record type, as maintained by IANA.
159         ///
160         /// Current assignments can be found at
161         /// <http://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4>
162         fn ty(&self) -> u16;
163         /// The name this record is at.
164         fn name(&self) -> &Name;
165         /// Gets a JSON encoding of this record.
166         fn json(&self) -> String;
167         /// Writes the data of this record, prefixed by a u16 length, to the given `Vec`.
168         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>);
169 }
170 impl<RR: StaticRecord> Record for RR {
171         fn ty(&self) -> u16 { RR::TYPE }
172         fn name(&self) -> &Name { RR::name(self) }
173         fn json(&self) -> String { RR::json(self) }
174         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
175                 RR::write_u16_len_prefixed_data(self, out)
176         }
177 }
178 impl Record for RR {
179         fn ty(&self) -> u16 { self.ty() }
180         fn name(&self) -> &Name { self.name() }
181         fn json(&self) -> String { self.json() }
182         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
183                 self.write_u16_len_prefixed_data(out)
184         }
185 }
186
187 #[derive(Debug, Clone, PartialEq, Eq, Ord)]
188 /// A text resource record, containing arbitrary text data
189 pub struct Txt {
190         /// The name this record is at.
191         pub name: Name,
192         /// The text record itself.
193         ///
194         /// While this is generally UTF-8-valid, there is no specific requirement that it be, and thus
195         /// is an arbitrary series of bytes here.
196         pub data: Vec<u8>,
197 }
198 /// The wire type for TXT records
199 pub const TXT_TYPE: u16 = 16;
200 impl PartialOrd for Txt {
201         fn partial_cmp(&self, o: &Txt) -> Option<Ordering> {
202                 Some(self.name.cmp(&o.name)
203                         .then_with(|| {
204                                 // Compare in wire encoding form, i.e. compare in 255-byte chunks
205                                 for i in 1..(self.data.len() / 255) + 2 {
206                                         let start = (i - 1)*255;
207                                         let self_len = cmp::min(i * 255, self.data.len());
208                                         let o_len = cmp::min(i * 255, o.data.len());
209                                         let slice_cmp = self_len.cmp(&o_len)
210                                                 .then_with(|| self.data[start..self_len].cmp(&o.data[start..o_len]));
211                                         if !slice_cmp.is_eq() { return slice_cmp; }
212                                 }
213                                 Ordering::Equal
214                         }))
215         }
216 }
217 impl StaticRecord for Txt {
218         const TYPE: u16 = TXT_TYPE;
219         fn name(&self) -> &Name { &self.name }
220         fn json(&self) -> String {
221                 if let Ok(s) = core::str::from_utf8(&self.data) {
222                         if s.chars().all(|c| !c.is_control() && c != '"') {
223                                 return format!("{{\"type\":\"txt\",\"name\":\"{}\",\"contents\":\"{}\"}}", self.name.0, s);
224                         }
225                 }
226                 format!("{{\"type\":\"txt\",\"name\":\"{}\",\"contents\":{:?}}}", self.name.0, &self.data[..])
227         }
228         fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
229                 let mut parsed_data = Vec::with_capacity(data.len().saturating_sub(1));
230                 while !data.is_empty() {
231                         let len = read_u8(&mut data)? as usize;
232                         if data.len() < len { return Err(()); }
233                         parsed_data.extend_from_slice(&data[..len]);
234                         data = &data[len..];
235                 }
236                 Ok(Txt { name, data: parsed_data })
237         }
238         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
239                 let len = (self.data.len() + (self.data.len() + 254) / 255) as u16;
240                 out.extend_from_slice(&len.to_be_bytes());
241
242                 let mut data_write = &self.data[..];
243                 out.extend_from_slice(&[data_write.len().try_into().unwrap_or(255)]);
244                 while !data_write.is_empty() {
245                         let split_pos = core::cmp::min(255, data_write.len());
246                         out.extend_from_slice(&data_write[..split_pos]);
247                         data_write = &data_write[split_pos..];
248                         if !data_write.is_empty() {
249                                 out.extend_from_slice(&[data_write.len().try_into().unwrap_or(255)]);
250                         }
251                 }
252         }
253 }
254
255 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
256 /// A TLS Certificate Association resource record containing information about the TLS certificate
257 /// which should be expected when communicating with the host at the given name.
258 ///
259 /// See <https://en.wikipedia.org/wiki/DNS-based_Authentication_of_Named_Entities#TLSA_RR> for more
260 /// info.
261 pub struct TLSA {
262         /// The name this record is at.
263         pub name: Name,
264         /// The type of constraint on the TLS certificate(s) used which should be enforced by this
265         /// record.
266         pub cert_usage: u8,
267         /// Whether to match on the full certificate, or only the public key.
268         pub selector: u8,
269         /// The type of data included which is used to match the TLS certificate(s).
270         pub data_ty: u8,
271         /// The certificate data or hash of the certificate data itself.
272         pub data: Vec<u8>,
273 }
274 /// The wire type for TLSA records
275 pub const TLSA_TYPE: u16 = 52;
276 impl StaticRecord for TLSA {
277         const TYPE: u16 = TLSA_TYPE;
278         fn name(&self) -> &Name { &self.name }
279         fn json(&self) -> String {
280                 let mut out = String::with_capacity(128+self.data.len()*2);
281                 write!(&mut out,
282                         "{{\"type\":\"tlsa\",\"name\":\"{}\",\"usage\":{},\"selector\":{},\"data_ty\":{},\"data\":\"",
283                         self.name.0, self.cert_usage, self.selector, self.data_ty
284                 ).expect("Write to a String shouldn't fail");
285                 for c in self.data.iter() {
286                         write!(&mut out, "{:02X}", c)
287                                 .expect("Write to a String shouldn't fail");
288                 }
289                 out += "\"}";
290                 out
291         }
292         fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
293                 Ok(TLSA {
294                         name, cert_usage: read_u8(&mut data)?, selector: read_u8(&mut data)?,
295                         data_ty: read_u8(&mut data)?, data: data.to_vec(),
296                 })
297         }
298         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
299                 let len = 3 + self.data.len();
300                 out.extend_from_slice(&(len as u16).to_be_bytes());
301                 out.extend_from_slice(&[self.cert_usage, self.selector, self.data_ty]);
302                 out.extend_from_slice(&self.data);
303         }
304 }
305
306 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
307 /// A Canonical Name resource record, referring all queries for this name to another name.
308 pub struct CName {
309         /// The name this record is at.
310         pub name: Name,
311         /// The canonical name.
312         ///
313         /// A resolver should use this name when looking up any further records for [`self.name`].
314         pub canonical_name: Name,
315 }
316 impl StaticRecord for CName {
317         const TYPE: u16 = 5;
318         fn name(&self) -> &Name { &self.name }
319         fn json(&self) -> String {
320                 format!("{{\"type\":\"cname\",\"name\":\"{}\",\"canonical_name\":\"{}\"}}",
321                         self.name.0, self.canonical_name.0)
322         }
323         fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
324                 Ok(CName { name, canonical_name: read_wire_packet_name(&mut data, wire_packet)? })
325         }
326         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
327                 let len: u16 = name_len(&self.canonical_name);
328                 out.extend_from_slice(&len.to_be_bytes());
329                 write_name(out, &self.canonical_name);
330         }
331 }
332
333 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
334 /// A public key resource record which can be used to validate [`RRSig`]s.
335 pub struct DnsKey {
336         /// The name this record is at.
337         pub name: Name,
338         /// Flags which constrain the usage of this public key.
339         pub flags: u16,
340         /// The protocol this key is used for (protocol `3` is DNSSEC). 
341         pub protocol: u8,
342         /// The algorithm which this public key uses to sign data.
343         pub alg: u8,
344         /// The public key itself.
345         pub pubkey: Vec<u8>,
346 }
347 impl StaticRecord for DnsKey {
348         const TYPE: u16 = 48;
349         fn name(&self) -> &Name { &self.name }
350         fn json(&self) -> String {
351                 let mut out = String::with_capacity(128+self.pubkey.len()*2);
352                 write!(&mut out,
353                         "{{\"type\":\"dnskey\",\"name\":\"{}\",\"flags\":{},\"protocol\":{},\"alg\":{},\"pubkey\":\"",
354                         self.name.0, self.flags, self.protocol, self.alg
355                 ).expect("Write to a String shouldn't fail");
356                 for c in self.pubkey.iter() {
357                         write!(&mut out, "{:02X}", c)
358                                 .expect("Write to a String shouldn't fail");
359                 }
360                 out += "\"}";
361                 out
362         }
363         fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
364                 Ok(DnsKey {
365                         name, flags: read_u16(&mut data)?, protocol: read_u8(&mut data)?,
366                         alg: read_u8(&mut data)?, pubkey: data.to_vec(),
367                 })
368         }
369         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
370                 let len = 2 + 1 + 1 + self.pubkey.len();
371                 out.extend_from_slice(&(len as u16).to_be_bytes());
372                 out.extend_from_slice(&self.flags.to_be_bytes());
373                 out.extend_from_slice(&self.protocol.to_be_bytes());
374                 out.extend_from_slice(&self.alg.to_be_bytes());
375                 out.extend_from_slice(&self.pubkey);
376         }
377 }
378 impl DnsKey {
379         /// A short (non-cryptographic) digest which can be used to refer to this [`DnsKey`].
380         pub fn key_tag(&self) -> u16 {
381                 let mut res = u32::from(self.flags);
382                 res += u32::from(self.protocol) << 8;
383                 res += u32::from(self.alg);
384                 for (idx, b) in self.pubkey.iter().enumerate() {
385                         if idx % 2 == 0 {
386                                 res += u32::from(*b) << 8;
387                         } else {
388                                 res += u32::from(*b);
389                         }
390                 }
391                 res += (res >> 16) & 0xffff;
392                 (res & 0xffff) as u16
393         }
394 }
395
396 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
397 /// A Delegation Signer resource record which indicates that some alternative [`DnsKey`] can sign
398 /// for records in the zone which matches [`self.name`].
399 pub struct DS {
400         /// The name this record is at.
401         ///
402         /// This is also the zone that a [`DnsKey`] which matches the [`Self::digest`] can sign for.
403         pub name: Name,
404         /// A short tag which describes the matching [`DnsKey`].
405         ///
406         /// This matches the [`DnsKey::key_tag`] for the [`DnsKey`] which is referred to by this
407         /// [`DS`].
408         pub key_tag: u16,
409         /// The algorithm which the [`DnsKey`] referred to by this [`DS`] uses.
410         ///
411         /// This matches the [`DnsKey::alg`] field in the referred-to [`DnsKey`].
412         pub alg: u8,
413         /// The type of digest used to hash the referred-to [`DnsKey`].
414         pub digest_type: u8,
415         /// The digest itself.
416         pub digest: Vec<u8>,
417 }
418 impl StaticRecord for DS {
419         const TYPE: u16 = 43;
420         fn name(&self) -> &Name { &self.name }
421         fn json(&self) -> String {
422                 let mut out = String::with_capacity(128+self.digest.len()*2);
423                 write!(&mut out,
424                         "{{\"type\":\"ds\",\"name\":\"{}\",\"key_tag\":{},\"alg\":{},\"digest_type\":{},\"digest\":\"",
425                         self.name.0, self.key_tag, self.alg, self.digest_type
426                 ).expect("Write to a String shouldn't fail");
427                 for c in self.digest.iter() {
428                         write!(&mut out, "{:02X}", c)
429                                 .expect("Write to a String shouldn't fail");
430                 }
431                 out += "\"}";
432                 out
433         }
434         fn read_from_data(name: Name, mut data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
435                 Ok(DS {
436                         name, key_tag: read_u16(&mut data)?, alg: read_u8(&mut data)?,
437                         digest_type: read_u8(&mut data)?, digest: data.to_vec(),
438                 })
439         }
440         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
441                 let len = 2 + 1 + 1 + self.digest.len();
442                 out.extend_from_slice(&(len as u16).to_be_bytes());
443                 out.extend_from_slice(&self.key_tag.to_be_bytes());
444                 out.extend_from_slice(&self.alg.to_be_bytes());
445                 out.extend_from_slice(&self.digest_type.to_be_bytes());
446                 out.extend_from_slice(&self.digest);
447         }
448 }
449
450 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
451 /// A Resource Record (set) Signature resource record. This contains a signature over all the
452 /// resources records of the given type at the given name.
453 pub struct RRSig {
454         /// The name this record is at.
455         ///
456         /// This is also the name of any records which this signature is covering (ignoring wildcards).
457         pub name: Name,
458         /// The resource record type which this [`RRSig`] is signing.
459         ///
460         /// All resources records of this type at the same name as [`self.name`] must be signed by
461         /// this [`RRSig`].
462         pub ty: u16,
463         /// The algorithm which is being used to sign.
464         ///
465         /// This must match the [`DnsKey::alg`] field in the [`DnsKey`] being used to sign.
466         pub alg: u8,
467         /// The number of labels in the name of the records that this signature is signing.
468         ///
469         /// If this is less than the number of labels in [`self.name`], this signature is covering a
470         /// wildcard entry.
471         pub labels: u8,
472         /// The TTL of the records which this [`RRSig`] is signing.
473         pub orig_ttl: u32,
474         /// The expiration (as a UNIX timestamp) of this signature.
475         pub expiration: u32,
476         /// The time (as a UNIX timestamp) at which this signature becomes valid.
477         pub inception: u32,
478         /// A short tag which describes the matching [`DnsKey`].
479         ///
480         /// This matches the [`DnsKey::key_tag`] for the [`DnsKey`] which created this signature.
481         pub key_tag: u16,
482         /// The [`DnsKey::name`] in the [`DnsKey`] which created this signature.
483         ///
484         /// This must be a parent of [`self.name`].
485         ///
486         /// [`DnsKey::name`]: Record::name
487         // We'd like to just link to the `DnsKey` member variable called `name`, but there doesn't
488         // appear to be a way to actually do that, so instead we have to link to the trait method.
489         pub key_name: Name,
490         /// The signature itself.
491         pub signature: Vec<u8>,
492 }
493 impl StaticRecord for RRSig {
494         const TYPE: u16 = 46;
495         fn name(&self) -> &Name { &self.name }
496         fn json(&self) -> String {
497                 let mut out = String::with_capacity(256 + self.signature.len()*2);
498                 write!(&mut out,
499                         "{{\"type\":\"ds\",\"name\":\"{}\",\"signed_record_type\":{},\"alg\":{},\"signed_labels\":{},\"orig_ttl\":{},\"expiration\"{},\"inception\":{},\"key_tag\":{},\"key_name\":\"{}\",\"signature\":\"",
500                         self.name.0, self.ty, self.alg, self.labels, self.orig_ttl, self.expiration, self.inception, self.key_tag, self.key_name.0
501                 ).expect("Write to a String shouldn't fail");
502                 for c in self.signature.iter() {
503                         write!(&mut out, "{:02X}", c)
504                                 .expect("Write to a String shouldn't fail");
505                 }
506                 out += "\"}";
507                 out
508         }
509         fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
510                 Ok(RRSig {
511                         name, ty: read_u16(&mut data)?, alg: read_u8(&mut data)?,
512                         labels: read_u8(&mut data)?, orig_ttl: read_u32(&mut data)?,
513                         expiration: read_u32(&mut data)?, inception: read_u32(&mut data)?,
514                         key_tag: read_u16(&mut data)?,
515                         key_name: read_wire_packet_name(&mut data, wire_packet)?,
516                         signature: data.to_vec(),
517                 })
518         }
519         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
520                 let len = 2 + 1 + 1 + 4*3 + 2 + name_len(&self.key_name) + self.signature.len() as u16;
521                 out.extend_from_slice(&len.to_be_bytes());
522                 out.extend_from_slice(&self.ty.to_be_bytes());
523                 out.extend_from_slice(&self.alg.to_be_bytes());
524                 out.extend_from_slice(&self.labels.to_be_bytes());
525                 out.extend_from_slice(&self.orig_ttl.to_be_bytes());
526                 out.extend_from_slice(&self.expiration.to_be_bytes());
527                 out.extend_from_slice(&self.inception.to_be_bytes());
528                 out.extend_from_slice(&self.key_tag.to_be_bytes());
529                 write_name(out, &self.key_name);
530                 out.extend_from_slice(&self.signature);
531         }
532 }
533
534 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
535 /// An IPv4 Address resource record
536 pub struct A {
537         /// The name this record is at.
538         pub name: Name,
539         /// The bytes of the IPv4 address.
540         pub address: [u8; 4],
541 }
542 /// The wire type for A records
543 pub const A_TYPE: u16 = 1;
544 impl StaticRecord for A {
545         const TYPE: u16 = A_TYPE;
546         fn name(&self) -> &Name { &self.name }
547         fn json(&self) -> String {
548                 format!("{{\"type\":\"a\",\"name\":\"{}\",\"address\":{:?}}}", self.name.0, self.address)
549         }
550         fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
551                 if data.len() != 4 { return Err(()); }
552                 let mut address = [0; 4];
553                 address.copy_from_slice(&data);
554                 Ok(A { name, address })
555         }
556         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
557                 out.extend_from_slice(&4u16.to_be_bytes());
558                 out.extend_from_slice(&self.address);
559         }
560 }
561
562 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
563 /// An IPv6 Address resource record
564 pub struct AAAA {
565         /// The name this record is at.
566         pub name: Name,
567         /// The bytes of the IPv6 address.
568         pub address: [u8; 16],
569 }
570 /// The wire type for AAAA records
571 pub const AAAA_TYPE: u16 = 28;
572 impl StaticRecord for AAAA {
573         const TYPE: u16 = AAAA_TYPE;
574         fn name(&self) -> &Name { &self.name }
575         fn json(&self) -> String {
576                 format!("{{\"type\":\"aaaa\",\"name\":\"{}\",\"address\":{:?}}}", self.name.0, self.address)
577         }
578         fn read_from_data(name: Name, data: &[u8], _wire_packet: &[u8]) -> Result<Self, ()> {
579                 if data.len() != 16 { return Err(()); }
580                 let mut address = [0; 16];
581                 address.copy_from_slice(&data);
582                 Ok(AAAA { name, address })
583         }
584         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
585                 out.extend_from_slice(&16u16.to_be_bytes());
586                 out.extend_from_slice(&self.address);
587         }
588 }
589
590 #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
591 /// A Name Server resource record, which indicates the server responsible for handling queries for
592 /// a zone.
593 pub struct NS {
594         /// The name this record is at.
595         ///
596         /// This is also the zone which the server at [`Self::name_server`] is responsible for handling
597         /// queries for.
598         pub name: Name,
599         /// The name of the server which is responsible for handling queries for the [`self.name`]
600         /// zone.
601         pub name_server: Name,
602 }
603 impl StaticRecord for NS {
604         const TYPE: u16 = 2;
605         fn name(&self) -> &Name { &self.name }
606         fn json(&self) -> String {
607                 format!("{{\"type\":\"ns\",\"name\":\"{}\",\"ns\":\"{}\"}}", self.name.0, self.name_server.0)
608         }
609         fn read_from_data(name: Name, mut data: &[u8], wire_packet: &[u8]) -> Result<Self, ()> {
610                 Ok(NS { name, name_server: read_wire_packet_name(&mut data, wire_packet)? })
611         }
612         fn write_u16_len_prefixed_data(&self, out: &mut Vec<u8>) {
613                 out.extend_from_slice(&name_len(&self.name_server).to_be_bytes());
614                 write_name(out, &self.name_server);
615         }
616 }