simple_dns/dns/rdata/
dnskey.rs

1use crate::{bytes_buffer::BytesBuffer, dns::WireFormat};
2use std::borrow::Cow;
3
4use super::RR;
5
6/// A DNS key record see [rfc4034](https://www.rfc-editor.org/rfc/rfc4034#section-2)
7#[derive(Debug, PartialEq, Eq, Hash, Clone)]
8pub struct DNSKEY<'a> {
9    /// The flags field contains various flags that describe the key's properties
10    pub flags: u16,
11    /// The protocol field must be set to 3 per RFC4034
12    pub protocol: u8,
13    /// The algorithm field identifies the public key's cryptographic algorithm
14    pub algorithm: u8,
15    /// The public key field contains the cryptographic key material in base64 format
16    pub public_key: Cow<'a, [u8]>,
17}
18
19impl RR for DNSKEY<'_> {
20    const TYPE_CODE: u16 = 48;
21}
22
23impl<'a> WireFormat<'a> for DNSKEY<'a> {
24    const MINIMUM_LEN: usize = 4;
25
26    fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
27    where
28        Self: Sized,
29    {
30        let flags = data.get_u16()?;
31        let protocol = data.get_u8()?;
32        let algorithm = data.get_u8()?;
33        let public_key = Cow::Borrowed(data.get_remaining());
34
35        Ok(Self {
36            flags,
37            protocol,
38            algorithm,
39            public_key,
40        })
41    }
42
43    fn write_to<T: std::io::Write>(&self, out: &mut T) -> crate::Result<()> {
44        out.write_all(&self.flags.to_be_bytes())?;
45        out.write_all(&[self.protocol])?;
46        out.write_all(&[self.algorithm])?;
47        out.write_all(&self.public_key)?;
48
49        Ok(())
50    }
51
52    fn len(&self) -> usize {
53        self.public_key.len() + Self::MINIMUM_LEN
54    }
55}
56
57impl DNSKEY<'_> {
58    /// Transforms the inner data into its owned type
59    pub fn into_owned<'b>(self) -> DNSKEY<'b> {
60        DNSKEY {
61            flags: self.flags,
62            protocol: self.protocol,
63            algorithm: self.algorithm,
64            public_key: Cow::Owned(self.public_key.into_owned()),
65        }
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use crate::{rdata::RData, ResourceRecord};
73
74    #[test]
75    fn parse_and_write_dnskey() {
76        let flags = 12345u16;
77        let protocol = 8u8;
78        let algorithm = 2u8;
79        let public_key = vec![1, 2, 3, 4, 5];
80        let rdata = DNSKEY {
81            flags,
82            protocol,
83            algorithm,
84            public_key: Cow::Owned(public_key),
85        };
86        let mut writer = Vec::new();
87        rdata.write_to(&mut writer).unwrap();
88        let rdata = DNSKEY::parse(&mut (&writer[..]).into()).unwrap();
89        assert_eq!(rdata.flags, flags);
90        assert_eq!(rdata.protocol, protocol);
91        assert_eq!(rdata.algorithm, algorithm);
92        assert_eq!(&*rdata.public_key, &[1, 2, 3, 4, 5]);
93    }
94
95    #[test]
96    fn parse_sample() -> Result<(), Box<dyn std::error::Error>> {
97        let sample_file = std::fs::read("samples/zonefile/DNSKEY.sample")?;
98
99        let sample_rdata = match ResourceRecord::parse(&mut (&sample_file[..]).into())?.rdata {
100            RData::DNSKEY(rdata) => rdata,
101            _ => unreachable!(),
102        };
103
104        assert_eq!(sample_rdata.flags, 256);
105        assert_eq!(sample_rdata.protocol, 3);
106        assert_eq!(sample_rdata.algorithm, 5);
107        assert_eq!(
108            *sample_rdata.public_key,
109            *b"\x01\x03\xd2\x2a\x6c\xa7\x7f\x35\xb8\x93\x20\x6f\xd3\x5e\x4c\x50\x6d\x83\x78\x84\x37\x09\xb9\x7e\x04\x16\x47\xe1\xbf\xf4\x3d\x8d\x64\xc6\x49\xaf\x1e\x37\x19\x73\xc9\xe8\x91\xfc\xe3\xdf\x51\x9a\x8c\x84\x0a\x63\xee\x42\xa6\xd2\xeb\xdd\xbb\x97\x03\x5d\x21\x5a\xa4\xe4\x17\xb1\xfa\x45\xfa\x11\xa9\x74\x1e\xa2\x09\x8c\x1d\xfa\x5f\xb5\xfe\xb3\x32\xfd\x4b\xc8\x15\x20\x89\xae\xf3\x6b\xa6\x44\xcc\xe2\x41\x3b\x3b\x72\xbe\x18\xcb\xef\x8d\xa2\x53\xf4\xe9\x3d\x21\x03\x86\x6d\x92\x34\xa2\xe2\x8d\xf5\x29\xa6\x7d\x54\x68\xdb\xef\xe3"
110        );
111
112        Ok(())
113    }
114}