flex_dns/rdata/
openpgpkey.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/// # OpenPGP Key Record (OPENPGPKEY)
8/// This record is used as part of the DNS-Based Authentication of Named
9/// Entities (DANE) protocol to associate a public key with a domain name.
10/// The OPENPGPKEY record is intended to be used in conjunction with the
11/// TLSA record [RFC6698].
12#[derive(Copy, Clone, Debug, PartialEq)]
13pub struct OpenPgpKey<'a> {
14    /// flags is a bitmap of flags (see [RFC 4880](https://tools.ietf.org/html/rfc4880))
15    pub flags: u16,
16    /// algorithm is the algorithm of the public key
17    pub algorithm: u8,
18    /// public_key is the public key
19    pub public_key: Characters<'a>,
20    /// fingerprint is the fingerprint of the referenced public key
21    pub fingerprint: Characters<'a>,
22}
23
24impl<'a> RDataParse<'a> for OpenPgpKey<'a> {
25    #[inline]
26    fn parse(rdata: &RData<'a>, i: &mut usize) -> Result<Self, DnsMessageError> {
27        let flags = u16::parse(rdata, i)?;
28        let algorithm = u8::parse(rdata, i)?;
29        let public_key = Characters::parse(rdata, i)?;
30        let fingerprint = Characters::parse(rdata, i)?;
31
32        Ok(Self {
33            flags,
34            algorithm,
35            public_key,
36            fingerprint
37        })
38    }
39}
40
41impl<'a> WriteBytes for OpenPgpKey<'a> {
42    #[inline]
43    fn write<
44        const PTR_STORAGE: usize,
45        const DNS_SECTION: usize,
46        B: MutBuffer + Buffer,
47    >(&self, message: &mut DnsMessage<PTR_STORAGE, DNS_SECTION, B>) -> Result<usize, DnsMessageError> {
48        let mut bytes = 0;
49
50        bytes += self.flags.write(message)?;
51        bytes += self.algorithm.write(message)?;
52        bytes += self.public_key.write(message)?;
53        bytes += self.fingerprint.write(message)?;
54
55        Ok(bytes)
56    }
57}
58
59#[cfg(test)]
60mod test {
61    use crate::rdata::testutils::parse_write_test;
62
63    use super::*;
64
65    parse_write_test!(
66        11,
67        [
68            0x00, 0x0e, // flags
69            0x0a, // algorithm
70            0x03, b'w', b'w', b'w', // public key
71            0x03, b'w', b'w', b'w', // fingerprint
72        ],
73        OpenPgpKey {
74            flags: 0x000e,
75            algorithm: 0x0a,
76            public_key: unsafe { Characters::new_unchecked(b"www") },
77            fingerprint: unsafe { Characters::new_unchecked(b"www") },
78        },
79    );
80}