diff --git a/Cargo.lock b/Cargo.lock index abc0022..887783a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,10 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "arrayref" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "arrayvec" version = "0.4.10" @@ -38,6 +43,44 @@ name = "bitflags" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "block-buffer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "block-buffer" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-padding 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "block-padding" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "byte-tools" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "byte-tools" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byteorder" version = "1.3.2" @@ -114,6 +157,22 @@ name = "data-encoding" version = "2.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "digest" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "digest" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "dnsbox" version = "0.1.0" @@ -134,8 +193,11 @@ dependencies = [ "data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "dnsbox-derive 0.1.0", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "gost94 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -169,6 +231,11 @@ dependencies = [ "synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "fake-simd" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "fnv" version = "1.0.6" @@ -198,6 +265,32 @@ name = "futures" version = "0.1.28" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "generic-array" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "generic-array" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "gost94" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "iovec" version = "0.1.2" @@ -309,6 +402,11 @@ dependencies = [ "libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "opaque-debug" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "owning_ref" version = "0.4.0" @@ -494,6 +592,28 @@ name = "semver-parser" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "sha-1" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "sha2" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "slab" version = "0.4.2" @@ -717,6 +837,11 @@ dependencies = [ "tokio-reactor 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "typenum" +version = "1.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-xid" version = "0.1.0" @@ -761,11 +886,17 @@ dependencies = [ ] [metadata] +"checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum arrayvec 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "92c7fb76bc8826a8b33b4ee5bb07a247a81e76764ab4d55e8f73e3a4d8808c71" "checksum autocfg 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "0e49efa51329a5fd37e7c79db4621af617cd4e3e5bc224939808d076077077bf" "checksum backtrace 0.3.32 (registry+https://github.com/rust-lang/crates.io-index)" = "18b50f5258d1a9ad8396d2d345827875de4261b158124d4c819d9b351454fae5" "checksum backtrace-sys 0.1.30 (registry+https://github.com/rust-lang/crates.io-index)" = "5b3a000b9c543553af61bc01cbfc403b04b5caa9e421033866f2e98061eb3e61" "checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd" +"checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" +"checksum block-buffer 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +"checksum block-padding 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "6d4dc3af3ee2e12f3e5d224e5e1e3d73668abbeb69e566d361f7d5563a4fdf09" +"checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" +"checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5" "checksum bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)" = "206fdffcfa2df7cbe15601ef46c813fce0965eb3286db6b56c583b814b51c81c" "checksum cc 1.0.37 (registry+https://github.com/rust-lang/crates.io-index)" = "39f75544d7bbaf57560d2168f28fd649ff9c76153874db88bdbdfd839b1a7e7d" @@ -776,13 +907,19 @@ dependencies = [ "checksum crossbeam-queue 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7c979cd6cfe72335896575c6b5688da489e420d36a27a0b9eb0c73db574b4a4b" "checksum crossbeam-utils 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f8306fcef4a7b563b76b7dd949ca48f52bc1141aa067d2ea09565f3e2652aa5c" "checksum data-encoding 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4f47ca1860a761136924ddd2422ba77b2ea54fe8cc75b9040804a0d9d32ad97" +"checksum digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90" +"checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2" "checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1" +"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum futures 0.1.28 (registry+https://github.com/rust-lang/crates.io-index)" = "45dc39533a6cae6da2b56da48edae506bb767ec07370f86f70fc062e9d435869" +"checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec" +"checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" +"checksum gost94 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "28a479e5af8bff6770b86b5dc41a5ed2e232bcd0a52fcf1af5546ed487f3b89b" "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" @@ -796,6 +933,7 @@ dependencies = [ "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" "checksum nodrop 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)" = "2f9667ddcc6cc8a43afc9b7917599d7216aa09c463919ea32c59ed6cac8bc945" "checksum num_cpus 1.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bcef43580c035376c0705c42792c294b66974abbfd2789b511784023f71f3273" +"checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" "checksum owning_ref 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49a4b8ea2179e6a2e27411d3bca09ca6dd630821cf6894c6c7c8467a8ee7ef13" "checksum parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ab41b4aed082705d1056416ae4468b6ea99d52599ecf3169b00088d43113e337" "checksum parking_lot_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94c8c7923936b28d546dfd14d4472eaf34c99b14e1c973a32b3e6d4eb04298c9" @@ -818,6 +956,8 @@ dependencies = [ "checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27" "checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403" "checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" +"checksum sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23962131a91661d643c98940b20fcaffe62d776a823247be80a48fcb8b6fce68" +"checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ab606a9c5e214920bb66c458cd7be8ef094f813f20fe77a54cc7dbfff220d4b7" "checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8" @@ -837,6 +977,7 @@ dependencies = [ "checksum tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "f2106812d500ed25a4f38235b9cae8f78a09edf43203e16e59c3b769a342a60e" "checksum tokio-udp 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "66268575b80f4a4a710ef83d087fdfeeabdce9b74c797535fbac18a2cb906e92" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" +"checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)" = "f10e386af2b13e47c89e7236a7a14a086791a2b88ebad6df9bf42040195cf770" diff --git a/lib/dnsbox-base/Cargo.toml b/lib/dnsbox-base/Cargo.toml index 0a3bd4c..0fa2c34 100644 --- a/lib/dnsbox-base/Cargo.toml +++ b/lib/dnsbox-base/Cargo.toml @@ -13,6 +13,11 @@ failure = "0.1.5" lazy_static = "1.3.0" log = "0.4" smallvec = "0.6.10" +sha-1 = { version = "0.8.1", optional = true } +sha2 = { version = "0.8.0", optional = true } +gost94 = { version = "0.7.0", optional = true } [features] no-unsafe = [] +default = ['crypto'] +crypto = ['sha-1', 'sha2', 'gost94'] diff --git a/lib/dnsbox-base/src/common_types/binary.rs b/lib/dnsbox-base/src/common_types/binary.rs index 2d60073..027fb80 100644 --- a/lib/dnsbox-base/src/common_types/binary.rs +++ b/lib/dnsbox-base/src/common_types/binary.rs @@ -33,6 +33,13 @@ lazy_static::lazy_static!{ #[derive(Clone, PartialEq, Eq, Debug)] pub struct HexShortBlob(Bytes); +impl HexShortBlob { + pub fn new(data: Vec) -> Result { + failure::ensure!(data.len() < 256, "short hex blob must be at most 255 bytes long"); + Ok(Self(data.into())) + } +} + impl DnsPacketData for HexShortBlob { fn deserialize(data: &mut Cursor) -> Result { Ok(HexShortBlob(short_blob(data)?)) @@ -80,6 +87,13 @@ impl std::ops::Deref for HexShortBlob { #[derive(Clone, PartialEq, Eq, Debug)] pub struct Base64LongBlob(Bytes); +impl Base64LongBlob { + pub fn new(data: Vec) -> Result { + failure::ensure!(data.len() < 0x1_0000, "long base64 blob must be at most 65535 bytes long"); + Ok(Self(data.into())) + } +} + impl DnsPacketData for Base64LongBlob { fn deserialize(data: &mut Cursor) -> Result { let len = u16::deserialize(data)? as usize; @@ -140,6 +154,12 @@ impl std::ops::Deref for Base64LongBlob { #[derive(Clone, PartialEq, Eq, Debug)] pub struct Base64RemainingBlob(Bytes); +impl Base64RemainingBlob { + pub fn new(data: Vec) -> Self { + Self(data.into()) + } +} + impl DnsPacketData for Base64RemainingBlob { fn deserialize(data: &mut Cursor) -> Result { Ok(Base64RemainingBlob(remaining_bytes(data))) @@ -185,6 +205,12 @@ impl std::ops::Deref for Base64RemainingBlob { #[derive(Clone, PartialEq, Eq, Debug)] pub struct HexRemainingBlob(Bytes); +impl HexRemainingBlob { + pub fn new(data: Vec) -> Self { + Self(data.into()) + } +} + impl DnsPacketData for HexRemainingBlob { fn deserialize(data: &mut Cursor) -> Result { Ok(HexRemainingBlob(remaining_bytes(data))) @@ -228,6 +254,13 @@ impl std::ops::Deref for HexRemainingBlob { #[derive(Clone, PartialEq, Eq, Debug)] pub struct HexRemainingBlobNotEmpty(Bytes); +impl HexRemainingBlobNotEmpty { + pub fn new(data: Vec) -> Result { + failure::ensure!(!data.is_empty(), "must not be empty"); + Ok(Self(data.into())) + } +} + impl DnsPacketData for HexRemainingBlobNotEmpty { fn deserialize(data: &mut Cursor) -> Result { let data = remaining_bytes(data); diff --git a/lib/dnsbox-base/src/common_types/mod.rs b/lib/dnsbox-base/src/common_types/mod.rs index f732287..881b930 100644 --- a/lib/dnsbox-base/src/common_types/mod.rs +++ b/lib/dnsbox-base/src/common_types/mod.rs @@ -13,17 +13,59 @@ mod sshfp; mod time; mod uri; -pub use self::binary::{HexShortBlob, Base64LongBlob, Base64RemainingBlob, HexRemainingBlob, HexRemainingBlobNotEmpty}; +pub use self::binary::{ + Base64LongBlob, + Base64RemainingBlob, + HexRemainingBlob, + HexRemainingBlobNotEmpty, + HexShortBlob, +}; pub use self::classes::Class; -pub use self::caa::{CaaFlags}; -pub use self::dnssec::{DnsSecAlgorithm, DnskeyFlags, DnskeyProtocol, DnsSecDigestAlgorithm, Nsec3Flags, Nsec3ParamFlags, Nsec3Algorithm}; -pub use self::eui::{EUI48Addr, EUI64Addr}; -pub use self::name::{DnsName, DnsCanonicalName, DnsCompressedName}; -pub use self::nsec::{NsecTypeBitmap, NextHashedOwnerName}; +pub use self::caa::CaaFlags; +pub use self::dnssec::{ + DnskeyFlags, + DnskeyProtocol, + DnskeyProtocolKnown, + DnsSecAlgorithm, + DnsSecAlgorithmKnown, + DnsSecDigestAlgorithm, + DnsSecDigestAlgorithmKnown, + Nsec3Algorithm, + Nsec3AlgorithmKnown, + Nsec3Flags, + Nsec3ParamFlags, +}; +pub use self::eui::{ + EUI48Addr, + EUI64Addr, +}; +pub use self::name::{ + DnsCanonicalName, + DnsCompressedName, + DnsName, +}; +pub use self::nsec::{ + NextHashedOwnerName, + NsecTypeBitmap, +}; pub use self::nxt::NxtTypeBitmap; pub use self::sig::OptionalTTL; -pub use self::sshfp::{SshFpAlgorithm, SshFpType}; -pub use self::text::{ShortText, LongText, UnquotedShortText, RemainingText}; -pub use self::time::{Time, TimeStrict, Time48}; +pub use self::sshfp::{ + SshFpAlgorithm, + SshFpAlgorithmKnown, + SshFpType, + SshFpTypeKnown, +}; +pub use self::text::{ + LongText, + RemainingText, + ShortText, + UnquotedShortText, +}; +pub use self::time::{ + Time, + Time48, + TimeStrict, +}; pub use self::types::Type; pub use self::uri::UriText; diff --git a/lib/dnsbox-base/src/crypto/mod.rs b/lib/dnsbox-base/src/crypto/mod.rs new file mode 100644 index 0000000..02c9c64 --- /dev/null +++ b/lib/dnsbox-base/src/crypto/mod.rs @@ -0,0 +1,31 @@ +use crate::common_types::DnsSecDigestAlgorithmKnown; + +pub fn sha1(data: &[u8]) -> Vec { + use sha1::Digest; + sha1::Sha1::digest(data).as_slice().to_vec() +} + +pub fn sha256(data: &[u8]) -> Vec { + use sha2::Digest; + sha2::Sha256::digest(data).as_slice().to_vec() +} + +pub fn sha384(data: &[u8]) -> Vec { + use sha2::Digest; + sha2::Sha384::digest(data).as_slice().to_vec() +} + +// gostR3411 +pub fn gost_r3411(data: &[u8]) -> Vec { + use gost94::Digest; + gost94::Gost94CryptoPro::digest(data).as_slice().to_vec() +} + +pub fn ds_hash(alg: DnsSecDigestAlgorithmKnown, data: &[u8]) -> Vec { + match alg { + DnsSecDigestAlgorithmKnown::SHA1 => sha1(data), + DnsSecDigestAlgorithmKnown::SHA256 => sha256(data), + DnsSecDigestAlgorithmKnown::GOST_R_34_11_94 => gost_r3411(data), + DnsSecDigestAlgorithmKnown::SHA384 => sha384(data), + } +} diff --git a/lib/dnsbox-base/src/lib.rs b/lib/dnsbox-base/src/lib.rs index 5799fc7..1058232 100644 --- a/lib/dnsbox-base/src/lib.rs +++ b/lib/dnsbox-base/src/lib.rs @@ -8,6 +8,8 @@ extern crate self as dnsbox_base; #[macro_use] pub mod errors; +#[cfg(feature = "crypto")] +mod crypto; pub mod common_types; pub mod ser; pub mod packet; diff --git a/lib/dnsbox-base/src/records/structs.rs b/lib/dnsbox-base/src/records/structs.rs index 5259c28..a701a9a 100644 --- a/lib/dnsbox-base/src/records/structs.rs +++ b/lib/dnsbox-base/src/records/structs.rs @@ -1,4 +1,5 @@ use crate::common_types::*; +use crate::errors::*; use std::net::{Ipv4Addr, Ipv6Addr}; use crate::ser::{packet::DnsPacketData, text::DnsTextData, RRData}; @@ -393,6 +394,27 @@ impl DNSKEY { sum.wrapping_add(sum >> 16) as u16 } + + #[cfg(feature = "crypto")] + pub fn build_ds(&self, zone: &DnsName, algs: &[DnsSecDigestAlgorithmKnown]) -> Result> { + if algs.is_empty() { return Ok(Vec::new()); } + + use crate::ser::packet::DnsPacketWriteContext; + let mut ctx = DnsPacketWriteContext::new(); + ctx.enable_canonical(); + let mut bin = Vec::new(); + zone.serialize(&mut ctx, &mut bin)?; + self.serialize(&mut ctx, &mut bin)?; + + let key_tag = self.tag(); + + Ok(algs.iter().map(|alg| DS { + key_tag, + algorithm: self.algorithm, + digest_type: (*alg).into(), + digest: HexRemainingBlob::new(crate::crypto::ds_hash(*alg, &bin)), + }).collect()) + } } #[derive(Clone, PartialEq, Eq, Debug, DnsPacketData, DnsTextData, RRData)] diff --git a/lib/dnsbox-base/src/ser/mod.rs b/lib/dnsbox-base/src/ser/mod.rs index dfb5a46..a072ee6 100644 --- a/lib/dnsbox-base/src/ser/mod.rs +++ b/lib/dnsbox-base/src/ser/mod.rs @@ -1,5 +1,7 @@ +mod rrdata; pub mod packet; pub mod text; -mod rrdata; +pub use self::packet::DnsPacketWriteContext; pub use self::rrdata::{RRDataPacket, RRDataText, RRData, StaticRRData}; +pub use self::text::DnsTextContext; diff --git a/lib/dnsbox-base/src/ser/text/mod.rs b/lib/dnsbox-base/src/ser/text/mod.rs index 9fec5c8..6eb69e5 100644 --- a/lib/dnsbox-base/src/ser/text/mod.rs +++ b/lib/dnsbox-base/src/ser/text/mod.rs @@ -139,6 +139,12 @@ impl DnsTextContext { Self::default() } + pub fn new_root_origin() -> Self { + let mut this = Self::default(); + this.set_origin(common_types::DnsName::new_root()); + this + } + pub fn zone_class(&self) -> Option { self.zone_class } @@ -190,6 +196,13 @@ impl DnsTextContext { pub fn unset_last_ttl(&mut self) -> &mut Self { self } + + pub fn parse(&self, data: &str) -> crate::errors::Result + where + T: DnsTextData, + { + parse_with(data, |data| T::dns_parse(self, data)) + } } pub trait DnsTextData {