trust_dns_proto/rr/rdata/openpgpkey.rs
1// Copyright 2019 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! OPENPGPKEY records for OpenPGP public keys
9use std::fmt;
10
11#[cfg(feature = "serde-config")]
12use serde::{Deserialize, Serialize};
13
14use crate::error::*;
15use crate::serialize::binary::*;
16
17/// [RFC 7929](https://tools.ietf.org/html/rfc7929#section-2.1)
18///
19/// ```text
20/// The RDATA portion of an OPENPGPKEY resource record contains a single
21/// value consisting of a Transferable Public Key formatted as specified
22/// in [RFC4880].
23/// ```
24#[cfg_attr(feature = "serde-config", derive(Deserialize, Serialize))]
25#[derive(Debug, PartialEq, Eq, Hash, Clone)]
26pub struct OPENPGPKEY {
27 public_key: Vec<u8>,
28}
29
30impl OPENPGPKEY {
31 /// Creates a new OPENPGPKEY record data.
32 ///
33 /// # Arguments
34 ///
35 /// * `public_key` - an OpenPGP Transferable Public Key. This will NOT
36 /// be checked.
37 pub fn new(public_key: Vec<u8>) -> Self {
38 Self { public_key }
39 }
40
41 /// The public key. This should be an OpenPGP Transferable Public Key,
42 /// but this is not guaranteed.
43 pub fn public_key(&self) -> &[u8] {
44 &self.public_key
45 }
46}
47
48/// Read the RData from the given decoder.
49pub fn read(decoder: &mut BinDecoder<'_>, rdata_length: Restrict<u16>) -> ProtoResult<OPENPGPKEY> {
50 let rdata_length = rdata_length.map(usize::from).unverified();
51 let public_key =
52 decoder.read_vec(rdata_length)?.unverified(/*we do not enforce a specific format*/);
53 Ok(OPENPGPKEY::new(public_key))
54}
55
56/// Write the RData using the given encoder
57pub fn emit(encoder: &mut BinEncoder<'_>, openpgpkey: &OPENPGPKEY) -> ProtoResult<()> {
58 encoder.emit_vec(openpgpkey.public_key())
59}
60
61/// Parse the RData from a set of tokens.
62///
63/// [RFC 7929](https://tools.ietf.org/html/rfc7929#section-2.3)
64///
65/// ```text
66/// 2.3. The OPENPGPKEY RDATA Presentation Format
67///
68/// The RDATA Presentation Format, as visible in Zone Files [RFC1035],
69/// consists of a single OpenPGP Transferable Public Key as defined in
70/// Section 11.1 of [RFC4880] encoded in base64 as defined in Section 4
71/// of [RFC4648].
72/// ```
73impl fmt::Display for OPENPGPKEY {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
75 f.write_str(&data_encoding::BASE64.encode(&self.public_key))
76 }
77}
78
79// TODO test