From 814faeededd49f9746f4570062f5f1f969202333 Mon Sep 17 00:00:00 2001 From: Carlo Date: Sun, 14 Jul 2024 22:57:42 -0400 Subject: [PATCH] Refactor: incorporated the new Rrtype and corrected some deprecated calls --- src/dnssec/dnssec_fetch.rs | 7 +++++-- src/dnssec/dnssec_message.rs | 5 ++--- src/dnssec/rrset_signature.rs | 25 +++++++++++++------------ 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/dnssec/dnssec_fetch.rs b/src/dnssec/dnssec_fetch.rs index 24295daa..ccd090aa 100644 --- a/src/dnssec/dnssec_fetch.rs +++ b/src/dnssec/dnssec_fetch.rs @@ -1,7 +1,10 @@ -use crate::message::{DnsMessage, Rdata, ResourceRecord}; +use crate::message::DnsMessage; +use crate::message::rdata::Rdata; +use crate::message::rdata::dnskey_rdata::DnskeyRdata; +use crate::message::resource_record::ResourceRecord; use crate::dnssec::dnssec_message_processing::extract_dnssec_records; use crate::dnssec::rrset_signature::{verify_rrsig, verify_ds}; -use crate::message::rdata::DnskeyRdata; + use crate::client::client_error::ClientError; pub async fn fetch_dnskey_records(dns_response: &DnsMessage) -> Result, ClientError> { diff --git a/src/dnssec/dnssec_message.rs b/src/dnssec/dnssec_message.rs index 975cf6b8..3f1b4b0c 100644 --- a/src/dnssec/dnssec_message.rs +++ b/src/dnssec/dnssec_message.rs @@ -1,4 +1,3 @@ - use std::str::FromStr; use crate::domain_name::DomainName; use crate::message::rclass::Rclass; @@ -42,7 +41,7 @@ fn create_opt_rr(capacity: u16 ,e_rcode :u8, version: u8, do_bit: bool) -> Resou } fn read_opt_rr(opt_rr: ResourceRecord) -> String { - let requested_udp_len = Rclass::from_rclass_to_int(opt_rr.get_rclass()); + let requested_udp_len = Rclass::from(opt_rr.get_rclass()); let data = opt_rr.get_ttl().to_be_bytes(); let (e_rcode, version) = (data[0], data[1]); let z = u16::from_be_bytes([data[2], data[3]]); @@ -59,7 +58,7 @@ fn create_dns_message_with_dnssec(mut msg: DnsMessage) -> DnsMessage { // We create a opt rr with the do bit set to 1 // with NOERR as rcode and EDNS0 let rr = create_opt_rr(REQUESTED_UDP_LEN, - rcode::Rcode::from_rcode_to_int(Rcode::NOERROR), + Rcode::from(Rcode::NOERROR).into(), EDNS_VERSION, true); diff --git a/src/dnssec/rrset_signature.rs b/src/dnssec/rrset_signature.rs index ca59a2cc..d7d75447 100644 --- a/src/dnssec/rrset_signature.rs +++ b/src/dnssec/rrset_signature.rs @@ -4,29 +4,30 @@ use crypto::sha1::Sha1; use base64::encode; use crate::message::rdata::Rdata; use crate::message::rdata::dnskey_rdata::DnskeyRdata; -use crate::message::rdata::rrsig_rdata::{RRSIGRdata}; -use crate::message::resource_record::ResourceRecord; +use crate::message::rdata::rrsig_rdata::RRSIGRdata; +use crate::message::rrtype::Rrtype; +use crate::message::resource_record::{ResourceRecord, ToBytes}; use crate::client::client_error::ClientError; pub fn verify_rrsig(rrsig: &RRSIGRdata, dnskey: &DnskeyRdata, rrset: &[ResourceRecord]) -> Result { let mut rrsig_data = Vec::new(); - rrsig_data.extend_from_slice(&rrsig.get_type_covered().to_be_bytes()); + rrsig_data.extend_from_slice(&u16::from(rrsig.get_type_covered()).to_be_bytes()); rrsig_data.push(rrsig.get_algorithm()); rrsig_data.push(rrsig.get_labels()); rrsig_data.extend_from_slice(&rrsig.get_original_ttl().to_be_bytes()); rrsig_data.extend_from_slice(&rrsig.get_signature_expiration().to_be_bytes()); rrsig_data.extend_from_slice(&rrsig.get_signature_inception().to_be_bytes()); rrsig_data.extend_from_slice(&rrsig.get_key_tag().to_be_bytes()); - rrsig_data.extend_from_slice(rrsig.get_signer_name().to_bytes()?); + rrsig_data.extend_from_slice(&rrsig.get_signer_name().to_bytes());//Try? let mut rrset_sorted = rrset.to_vec(); rrset_sorted.sort_by(|a, b| a.get_name().cmp(&b.get_name())); for rr in rrset_sorted.iter() { - rrsig_data.extend_from_slice(rr.get_name().to_bytes()?); - rrsig_data.extend_from_slice(&rr.get_ttl.to_be_bytes()); + rrsig_data.extend_from_slice(&rr.get_name().to_bytes()); //Try? + rrsig_data.extend_from_slice(&rr.get_ttl().to_be_bytes()); rrsig_data.extend_from_slice(&(rr.get_rdata().to_bytes().len() as u16).to_be_bytes()); - rrsig_data.extend_from_slice(&rr.get_rdata().to_bytes()?); + rrsig_data.extend_from_slice(&rr.get_rdata().to_bytes());//Try? } let signature = rrsig.get_signature().clone(); @@ -44,13 +45,13 @@ pub fn verify_rrsig(rrsig: &RRSIGRdata, dnskey: &DnskeyRdata, rrset: &[ResourceR // RSA/SHA256 Ok(encode(&hashed) == encode(&signature)) }, - _ => Err(ClientError::new("Unknown DNSKEY algorithm")), + _ => Err(ClientError::NotImplemented("Unknown DNSKEY algorithm")), } } pub fn verify_ds(ds_record: &ResourceRecord, dnskey: &DnskeyRdata) -> Result { if let Rdata::DS(ds_rdata) = &ds_record.get_rdata() { - let dnskey_bytes = dnskey.to_bytes()?; + let dnskey_bytes = dnskey.to_bytes(); //Try? let hashed_key = match ds_rdata.algorithm { 1 => { let mut hasher = Sha1::new(); @@ -61,11 +62,11 @@ pub fn verify_ds(ds_record: &ResourceRecord, dnskey: &DnskeyRdata) -> Result return Err(ClientError::new("Unknown DS algorithm")), + _ => return Err(ClientError::NotImplemented("Unknown DS algorithm")), }; - Ok(ds_rdata.digest == hashed_key) + Ok(ds_rdata.digest == hashed_key.as_bytes()) } else { - Err(ClientError::new("Provided record is not a DS record")) + Err(ClientError::FormatError("Provided record is not a DS record")) } }