simple_dns/dns/rdata/
caa.rs

1use std::borrow::Cow;
2
3use crate::{
4    bytes_buffer::BytesBuffer,
5    dns::{CharacterString, WireFormat},
6};
7
8use super::RR;
9
10/// RFC 8659: Allow domain name holders to indicate whether they are authorized to issue digital certificates for particular domain name
11/// Used as a security policy for certificate authorities
12/// This implementation does not validate the tag or value; it splits based on packet byte structure
13#[derive(Debug, PartialEq, Eq, Hash, Clone)]
14pub struct CAA<'a> {
15    /// Critical or noncritical indicator
16    pub flag: u8,
17    /// Property described in the VALUE field. One of `issue`, `issuewild`, or `iodef`
18    pub tag: CharacterString<'a>,
19    /// Value associated with property tag
20    pub value: Cow<'a, [u8]>,
21}
22
23impl RR for CAA<'_> {
24    const TYPE_CODE: u16 = 257;
25}
26
27impl CAA<'_> {
28    /// Transforms the inner data into it owned type
29    pub fn into_owned<'b>(self) -> CAA<'b> {
30        CAA {
31            flag: self.flag,
32            tag: self.tag.into_owned(),
33            value: self.value.into_owned().into(),
34        }
35    }
36}
37
38impl<'a> WireFormat<'a> for CAA<'a> {
39    const MINIMUM_LEN: usize = 1;
40
41    fn parse(data: &mut BytesBuffer<'a>) -> crate::Result<Self>
42    where
43        Self: Sized,
44    {
45        let flag = data.get_u8()?;
46        let tag = CharacterString::parse(data)?;
47        // FIXME: remove quotes if they are the first and last characters
48        let value = Cow::Borrowed(data.get_remaining());
49
50        Ok(Self { flag, tag, value })
51    }
52
53    fn write_to<T: std::io::Write>(&self, out: &mut T) -> crate::Result<()> {
54        out.write_all(&self.flag.to_be_bytes())?;
55        self.tag.write_to(out)?;
56        //FIXME: add quotes if the value is not already quoted
57        out.write_all(&self.value)?;
58        Ok(())
59    }
60
61    fn len(&self) -> usize {
62        self.tag.len() + self.value.len() + Self::MINIMUM_LEN
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use crate::{rdata::RData, Packet, ResourceRecord, CLASS};
69
70    use super::*;
71
72    #[test]
73    fn parse_and_write_caa() {
74        let caa = CAA {
75            flag: 0,
76            tag: CharacterString::new(b"issue").unwrap(),
77            value: b"\"example.org".into(),
78        };
79
80        let mut data = Vec::new();
81        assert!(caa.write_to(&mut data).is_ok());
82
83        let caa = CAA::parse(&mut (&data[..]).into());
84        assert!(caa.is_ok());
85        let caa = caa.unwrap();
86
87        assert_eq!(data.len(), caa.len());
88        assert_eq!(0, caa.flag);
89        assert_eq!("issue", caa.tag.to_string());
90        assert_eq!(b"\"example.org", &caa.value[..]);
91    }
92
93    #[test]
94    fn parse_rdata_with_multiple_caa_records() {
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}