simple_dns/dns/rdata/
caa.rs

1use crate::{
2    bytes_buffer::BytesBuffer,
3    dns::{CharacterString, WireFormat},
4    lib::{Cow, Write},
5};
6
7use super::RR;
8
9/// RFC 8659: Allow domain name holders to indicate whether they are authorized to issue digital certificates for particular domain name
10/// Used as a security policy for certificate authorities
11/// This implementation does not validate the tag or value; it splits based on packet byte structure
12#[derive(Debug, PartialEq, Eq, Hash, Clone)]
13pub struct CAA<'a> {
14    /// Critical or noncritical indicator
15    pub flag: u8,
16    /// Property described in the VALUE field. One of `issue`, `issuewild`, or `iodef`
17    pub tag: CharacterString<'a>,
18    /// Value associated with property tag
19    pub value: Cow<'a, [u8]>,
20}
21
22impl RR for CAA<'_> {
23    const TYPE_CODE: u16 = 257;
24}
25
26impl CAA<'_> {
27    /// Transforms the inner data into it owned type
28    pub fn into_owned<'b>(self) -> CAA<'b> {
29        CAA {
30            flag: self.flag,
31            tag: self.tag.into_owned(),
32            value: self.value.into_owned().into(),
33        }
34    }
35}
36
37impl<'a> WireFormat<'a> for CAA<'a> {
38    const MINIMUM_LEN: usize = 1;
39
40    fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
41    where
42        Self: Sized,
43    {
44        let flag = data.get_u8()?;
45        let tag = CharacterString::parse(data)?;
46        // FIXME: remove quotes if they are the first and last characters
47        let value = Cow::Borrowed(data.get_remaining());
48
49        Ok(Self { flag, tag, value })
50    }
51
52    fn write_to<T: Write>(&self, out: &mut T) -> crate::Result<()> {
53        out.write_all(&self.flag.to_be_bytes())?;
54        self.tag.write_to(out)?;
55        //FIXME: add quotes if the value is not already quoted
56        out.write_all(&self.value)?;
57        Ok(())
58    }
59
60    fn len(&self) -> usize {
61        self.tag.len() + self.value.len() + Self::MINIMUM_LEN
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::lib::{ToString, Vec};
69
70    #[test]
71    fn parse_and_write_caa() {
72        let caa = CAA {
73            flag: 0,
74            tag: CharacterString::new(b"issue").unwrap(),
75            value: b"\"example.org".into(),
76        };
77
78        let mut data = Vec::new();
79        assert!(caa.write_to(&mut data).is_ok());
80
81        let caa = CAA::parse(&mut (&data[..]).into());
82        assert!(caa.is_ok());
83        let caa = caa.unwrap();
84
85        assert_eq!(data.len(), caa.len());
86        assert_eq!(0, caa.flag);
87        assert_eq!("issue", caa.tag.to_string());
88        assert_eq!(b"\"example.org", &caa.value[..]);
89    }
90
91    #[test]
92    fn parse_rdata_with_multiple_caa_records() {
93        use crate::{rdata::RData, Packet, ResourceRecord, CLASS};
94
95        let mut packet = Packet::new_query(0);
96        packet.answers.push(ResourceRecord::new(
97            "caa.xxx.com".try_into().unwrap(),
98            CLASS::IN,
99            11111,
100            crate::rdata::RData::CAA(CAA {
101                flag: 128,
102                tag: CharacterString::new(b"issuewild").unwrap(),
103                value: b"\"example.org".into(),
104            }),
105        ));
106
107        packet.answers.push(ResourceRecord::new(
108            "caa.yyy.com".try_into().unwrap(),
109            CLASS::IN,
110            11111,
111            crate::rdata::RData::CAA(CAA {
112                flag: 128,
113                tag: CharacterString::new(b"issuewild").unwrap(),
114                value: b"\"example_two.org".into(),
115            }),
116        ));
117
118        let data = packet
119            .build_bytes_vec_compressed()
120            .expect("Failed to generate packet");
121
122        let mut packet = Packet::parse(&data[..]).expect("Failed to parse packet");
123        let RData::CAA(cca_two) = packet.answers.pop().unwrap().rdata else {
124            panic!("failed to parse CAA record)")
125        };
126
127        let RData::CAA(cca_one) = packet.answers.pop().unwrap().rdata else {
128            panic!("failed to parse CAA record")
129        };
130
131        assert_eq!(b"\"example.org", &cca_one.value[..]);
132        assert_eq!(b"\"example_two.org", &cca_two.value[..]);
133    }
134}