use std::default::Default;
use crate::rr::dnssec::PublicKey;
const ROOT_ANCHOR_ORIG: &[u8] = include_bytes!("roots/19036.rsa");
const ROOT_ANCHOR_2018: &[u8] = include_bytes!("roots/20326.rsa");
#[derive(Clone)]
pub struct TrustAnchor {
pkeys: Vec<Vec<u8>>,
}
impl Default for TrustAnchor {
fn default() -> Self {
Self {
pkeys: vec![ROOT_ANCHOR_ORIG.to_owned(), ROOT_ANCHOR_2018.to_owned()],
}
}
}
impl TrustAnchor {
pub fn new() -> Self {
Self { pkeys: vec![] }
}
pub fn contains_dnskey_bytes(&self, other_key: &[u8]) -> bool {
self.pkeys.iter().any(|k| other_key == k.as_slice())
}
pub fn contains<P: PublicKey>(&self, other_key: &P) -> bool {
self.contains_dnskey_bytes(other_key.public_bytes())
}
pub fn insert_trust_anchor<P: PublicKey>(&mut self, public_key: &P) {
if !self.contains(public_key) {
self.pkeys.push(public_key.public_bytes().to_vec())
}
}
pub fn get(&self, idx: usize) -> &[u8] {
&self.pkeys[idx]
}
pub fn len(&self) -> usize {
self.pkeys.len()
}
pub fn is_empty(&self) -> bool {
self.pkeys.is_empty()
}
}
#[test]
fn test_kjqmt7v() {
let trust = TrustAnchor::default();
assert_eq!(trust.get(0), ROOT_ANCHOR_ORIG);
assert!(trust.contains_dnskey_bytes(ROOT_ANCHOR_ORIG));
}