Skip to content

Commit

Permalink
Refactor: incorporated the new Rrtype and corrected some deprecated c…
Browse files Browse the repository at this point in the history
…alls
  • Loading branch information
EstudianteGenerico047 committed Jul 15, 2024
1 parent 2d5c294 commit 814faee
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/dnssec/dnssec_fetch.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<DnskeyRdata>, ClientError> {
Expand Down
5 changes: 2 additions & 3 deletions src/dnssec/dnssec_message.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

use std::str::FromStr;
use crate::domain_name::DomainName;
use crate::message::rclass::Rclass;
Expand Down Expand Up @@ -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]]);
Expand All @@ -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);

Expand Down
25 changes: 13 additions & 12 deletions src/dnssec/rrset_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool, ClientError> {
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();
Expand All @@ -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<bool, ClientError> {
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();
Expand All @@ -61,11 +62,11 @@ pub fn verify_ds(ds_record: &ResourceRecord, dnskey: &DnskeyRdata) -> Result<boo
let hashed = Sha256::digest(&dnskey_bytes);
encode(&hashed)
},
_ => 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"))
}
}

0 comments on commit 814faee

Please sign in to comment.