use alloc::sync::Arc;
use super::{
Algorithm, PublicKey,
rdata::{RRSIG, SIG},
tbs::{self, TBS},
};
use crate::{
error::ProtoResult,
rr::{DNSClass, Name, Record},
serialize::binary::BinEncodable,
};
pub trait Verifier {
fn algorithm(&self) -> Algorithm;
fn key(&self) -> ProtoResult<Arc<dyn PublicKey + '_>>;
fn verify(&self, hash: &[u8], signature: &[u8]) -> ProtoResult<()> {
self.key()?.verify(hash, signature)
}
fn verify_message<M: BinEncodable>(
&self,
message: &M,
signature: &[u8],
sig0: &SIG,
) -> ProtoResult<()> {
tbs::message_tbs(message, sig0).and_then(|tbs| self.verify(tbs.as_ref(), signature))
}
fn verify_rrsig<'a>(
&self,
name: &Name,
dns_class: DNSClass,
sig: &RRSIG,
records: impl Iterator<Item = &'a Record>,
) -> ProtoResult<()> {
let rrset_tbs = TBS::from_sig(name, dns_class, sig, records)?;
self.verify(rrset_tbs.as_ref(), sig.sig())
}
}