flex_dns/rdata/
tlsa.rs

1use crate::{Buffer, DnsMessage, DnsMessageError, MutBuffer};
2use crate::characters::Characters;
3use crate::parse::Parse;
4use crate::rdata::{RData, RDataParse};
5use crate::write::WriteBytes;
6
7/// # Transport Layer Security Authentication (TLSA) Record
8/// This record is used to associate a TLS server certificate or public key with
9/// the domain name where the record is found, thus forming a "TLSA certificate association".
10/// This record type is described in [RFC 6698](https://tools.ietf.org/html/rfc6698).
11#[derive(Copy, Clone, Debug, PartialEq)]
12pub struct Tlsa<'a> {
13    /// The usage of this TLSA record
14    pub usage: u8,
15    /// The selector of this TLSA record
16    pub selector: u8,
17    /// The matching type of this TLSA record
18    pub matching_type: u8,
19    /// The certificate association data
20    pub certificate_association_data: Characters<'a>,
21}
22
23impl<'a> RDataParse<'a> for Tlsa<'a> {
24    #[inline]
25    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
26        let usage = u8::parse(rdata, i)?;
27        let selector = u8::parse(rdata, i)?;
28        let matching_type = u8::parse(rdata, i)?;
29        let certificate_association_data = Characters::parse(rdata, i)?;
30
31        Ok(Self {
32            usage,
33            selector,
34            matching_type,
35            certificate_association_data,
36        })
37    }
38}
39
40impl<'a> WriteBytes for Tlsa<'a> {
41    #[inline]
42    fn write<
43        const PTR_STORAGE: usize,
44        const DNS_SECTION: usize,
45        B: MutBuffer + Buffer,
46    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
47        let mut bytes = 0;
48
49        bytes += self.usage.write(message)?;
50        bytes += self.selector.write(message)?;
51        bytes += self.matching_type.write(message)?;
52        bytes += self.certificate_association_data.write(message)?;
53
54        Ok(bytes)
55    }
56}
57
58#[cfg(test)]
59mod test {
60    use crate::rdata::testutils::parse_write_test;
61
62    use super::*;
63
64    parse_write_test!(
65        7,
66        [
67            0x0a, // usage
68            0x0b, // selector
69            0x0c, // matching type
70            0x03, // length of certificate association data
71            0x77, 0x77, 0x77, // certificate association data
72        ],
73        Tlsa {
74            usage: 10,
75            selector: 11,
76            matching_type: 12,
77            certificate_association_data: unsafe { Characters::new_unchecked(b"www") },
78        },
79    );
80}