flex_dns/rdata/
dnskey.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/// # DNS key record
8/// This record is used to store public keys that are associated with a zone.
9#[derive(Copy, Clone, Debug, PartialEq)]
10pub struct DnsKey<'a> {
11    /// The flags field specifies various flags that control the security
12    /// related aspects of the key.
13    pub flags: u16,
14    /// The protocol field specifies the protocol for which the key is used.
15    pub protocol: u8,
16    /// The algorithm field specifies the public key's cryptographic algorithm
17    pub algorithm: u8,
18    /// The public key field holds the public key material.
19    pub public_key: Characters<'a>
20}
21
22impl<'a> RDataParse<'a> for DnsKey<'a> {
23    #[inline]
24    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
25        let flags = u16::parse(rdata, i)?;
26        let protocol = u8::parse(rdata, i)?;
27        let algorithm = u8::parse(rdata, i)?;
28        let public_key = Characters::parse(rdata, i)?;
29
30        Ok(DnsKey {
31            flags,
32            protocol,
33            algorithm,
34            public_key
35        })
36    }
37}
38
39impl<'a> WriteBytes for DnsKey<'a> {
40    #[inline]
41    fn write<
42        const PTR_STORAGE: usize,
43        const DNS_SECTION: usize,
44        B: MutBuffer + Buffer,
45    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
46        let mut bytes = 0;
47
48        bytes += self.flags.write(message)?;
49        bytes += self.protocol.write(message)?;
50        bytes += self.algorithm.write(message)?;
51        bytes += self.public_key.write(message)?;
52
53        Ok(bytes)
54    }
55}
56
57#[cfg(test)]
58mod test {
59    use crate::rdata::testutils::parse_write_test;
60
61    use super::*;
62
63    parse_write_test!(
64        8,
65        [
66            0x0f, 0x0e, // flags
67            0x5c, // protocol
68            0x8a, // algorithm
69            0x03, // public key length
70            b'w', b'w', b'w', // public key
71        ],
72        DnsKey {
73            flags: 0x0f0e,
74            protocol: 0x5c,
75            algorithm: 0x8a,
76            public_key: unsafe { Characters::new_unchecked(b"www") },
77        },
78    );
79}