diff --git a/src/base/iana/mod.rs b/src/base/iana/mod.rs index 2b73fe62..9d86b2e9 100644 --- a/src/base/iana/mod.rs +++ b/src/base/iana/mod.rs @@ -35,6 +35,7 @@ pub use self::rcode::{OptRcode, Rcode, TsigRcode}; pub use self::rtype::Rtype; pub use self::secalg::SecAlg; pub use self::svcb::SvcParamKey; +pub use self::zonemd::{ZonemdAlg, ZonemdScheme}; #[macro_use] mod macros; @@ -49,3 +50,4 @@ pub mod rcode; pub mod rtype; pub mod secalg; pub mod svcb; +pub mod zonemd; diff --git a/src/base/iana/zonemd.rs b/src/base/iana/zonemd.rs new file mode 100644 index 00000000..cd92a101 --- /dev/null +++ b/src/base/iana/zonemd.rs @@ -0,0 +1,50 @@ +//! ZONEMD IANA parameters. + +//------------ ZonemdScheme -------------------------------------------------- + +int_enum! { + /// ZONEMD schemes. + /// + /// This type selects the method by which data is collated and presented + /// as input to the hashing function for use with [ZONEMD]. + /// + /// For the currently registered values see the [IANA registration]. This + /// type is complete as of 2024-11-29. + /// + /// [ZONEMD]: ../../../rdata/zonemd/index.html + /// [IANA registration]: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#zonemd-schemes + => + ZonemdScheme, u8; + + /// Specifies that the SIMPLE scheme is used. + (SIMPLE => 1, "SIMPLE") +} + +int_enum_str_decimal!(ZonemdScheme, u8); +int_enum_zonefile_fmt_decimal!(ZonemdScheme, "scheme"); + +//------------ ZonemdAlg ----------------------------------------------------- + +int_enum! { + /// ZONEMD algorithms. + /// + /// This type selects the algorithm used to hash domain names for use with + /// the [ZONEMD]. + /// + /// For the currently registered values see the [IANA registration]. This + /// type is complete as of 2024-11-29. + /// + /// [ZONEMD]: ../../../rdata/zonemd/index.html + /// [IANA registration]: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#zonemd-hash-algorithms + => + ZonemdAlg, u8; + + /// Specifies that the SHA-384 algorithm is used. + (SHA384 => 1, "SHA384") + + /// Specifies that the SHA-512 algorithm is used. + (SHA512 => 2, "SHA512") +} + +int_enum_str_decimal!(ZonemdAlg, u8); +int_enum_zonefile_fmt_decimal!(ZonemdAlg, "hash algorithm"); diff --git a/src/rdata/zonemd.rs b/src/rdata/zonemd.rs index 66f41d40..025dae20 100644 --- a/src/rdata/zonemd.rs +++ b/src/rdata/zonemd.rs @@ -9,7 +9,7 @@ #![allow(clippy::needless_maybe_sized)] use crate::base::cmp::CanonicalOrd; -use crate::base::iana::Rtype; +use crate::base::iana::{Rtype, ZonemdAlg, ZonemdScheme}; use crate::base::rdata::{ComposeRecordData, RecordData}; use crate::base::scan::{Scan, Scanner}; use crate::base::serial::Serial; @@ -29,8 +29,8 @@ const DIGEST_MIN_LEN: usize = 12; #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct Zonemd { serial: Serial, - scheme: Scheme, - algo: Algorithm, + scheme: ZonemdScheme, + algo: ZonemdAlg, #[cfg_attr( feature = "serde", serde( @@ -54,8 +54,8 @@ impl Zonemd { /// Create a Zonemd record data from provided parameters. pub fn new( serial: Serial, - scheme: Scheme, - algo: Algorithm, + scheme: ZonemdScheme, + algo: ZonemdAlg, digest: Octs, ) -> Self { Self { @@ -72,12 +72,12 @@ impl Zonemd { } /// Get the scheme field. - pub fn scheme(&self) -> Scheme { + pub fn scheme(&self) -> ZonemdScheme { self.scheme } /// Get the hash algorithm field. - pub fn algorithm(&self) -> Algorithm { + pub fn algorithm(&self) -> ZonemdAlg { self.algo } @@ -233,20 +233,7 @@ impl> ZonefileFmt for Zonemd { p.block(|p| { p.write_token(self.serial)?; p.write_show(self.scheme)?; - p.write_comment(format_args!("scheme ({})", match self.scheme { - Scheme::Reserved => "reserved", - Scheme::Simple => "simple", - Scheme::Unassigned(_) => "unassigned", - Scheme::Private(_) => "private", - }))?; p.write_show(self.algo)?; - p.write_comment(format_args!("algorithm ({})", match self.algo { - Algorithm::Reserved => "reserved", - Algorithm::Sha384 => "SHA384", - Algorithm::Sha512 => "SHA512", - Algorithm::Unassigned(_) => "unassigned", - Algorithm::Private(_) => "private", - }))?; p.write_token(base16::encode_display(&self.digest)) }) } @@ -314,92 +301,6 @@ impl> Ord for Zonemd { } } -/// The data collation scheme. -/// -/// This enumeration wraps an 8-bit unsigned integer that identifies the -/// methods by which data is collated and presented as input to the -/// hashing function. -#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub enum Scheme { - Reserved, - Simple, - Unassigned(u8), - Private(u8), -} - -impl From for u8 { - fn from(s: Scheme) -> u8 { - match s { - Scheme::Reserved => 0, - Scheme::Simple => 1, - Scheme::Unassigned(n) => n, - Scheme::Private(n) => n, - } - } -} - -impl From for Scheme { - fn from(n: u8) -> Self { - match n { - 0 | 255 => Self::Reserved, - 1 => Self::Simple, - 2..=239 => Self::Unassigned(n), - 240..=254 => Self::Private(n), - } - } -} - -impl ZonefileFmt for Scheme { - fn fmt(&self, p: &mut impl Formatter) -> zonefile_fmt::Result { - p.write_token(u8::from(*self)) - } -} - -/// The Hash Algorithm used to construct the digest. -/// -/// This enumeration wraps an 8-bit unsigned integer that identifies -/// the cryptographic hash algorithm. -#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq, PartialOrd, Ord)] -#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -pub enum Algorithm { - Reserved, - Sha384, - Sha512, - Unassigned(u8), - Private(u8), -} - -impl From for u8 { - fn from(algo: Algorithm) -> u8 { - match algo { - Algorithm::Reserved => 0, - Algorithm::Sha384 => 1, - Algorithm::Sha512 => 2, - Algorithm::Unassigned(n) => n, - Algorithm::Private(n) => n, - } - } -} - -impl From for Algorithm { - fn from(n: u8) -> Self { - match n { - 0 | 255 => Self::Reserved, - 1 => Self::Sha384, - 2 => Self::Sha512, - 3..=239 => Self::Unassigned(n), - 240..=254 => Self::Private(n), - } - } -} - -impl ZonefileFmt for Algorithm { - fn fmt(&self, p: &mut impl Formatter) -> zonefile_fmt::Result { - p.write_token(u8::from(*self)) - } -} - #[cfg(test)] #[cfg(all(feature = "std", feature = "bytes"))] mod test { @@ -437,6 +338,7 @@ mod test { #[cfg(feature = "zonefile")] #[test] fn zonemd_parse_zonefile() { + use crate::base::iana::ZonemdAlg; use crate::base::Name; use crate::rdata::ZoneRecordData; use crate::zonefile::inplace::{Entry, Zonefile}; @@ -469,8 +371,8 @@ ns2 3600 IN AAAA 2001:db8::63 match record.into_data() { ZoneRecordData::Zonemd(rd) => { assert_eq!(2018031900, rd.serial().into_int()); - assert_eq!(Scheme::Simple, rd.scheme()); - assert_eq!(Algorithm::Sha384, rd.algorithm()); + assert_eq!(ZonemdScheme::SIMPLE, rd.scheme()); + assert_eq!(ZonemdAlg::SHA384, rd.algorithm()); } _ => panic!(), }