use crate::wire::Error;
use std::collections::HashMap;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RetainedSecrets {
pub rs1: Vec<u8>,
pub rs2: Vec<u8>,
pub verified: bool,
pub expiration_interval: u32,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CacheEntry {
pub local_zid: [u8; 12],
pub remote_zid: [u8; 12],
pub secrets: RetainedSecrets,
}
pub trait SharedSecretStore {
fn load(&self, local_zid: [u8; 12], remote_zid: [u8; 12]) -> Result<Option<CacheEntry>, Error>;
fn store(&mut self, entry: CacheEntry) -> Result<(), Error>;
fn clear(&mut self, local_zid: [u8; 12], remote_zid: [u8; 12]) -> Result<(), Error>;
}
#[derive(Default, Clone, Debug)]
pub struct MemorySharedSecretStore {
entries: HashMap<([u8; 12], [u8; 12]), CacheEntry>,
}
impl SharedSecretStore for MemorySharedSecretStore {
fn load(&self, local_zid: [u8; 12], remote_zid: [u8; 12]) -> Result<Option<CacheEntry>, Error> {
Ok(self.entries.get(&(local_zid, remote_zid)).cloned())
}
fn store(&mut self, entry: CacheEntry) -> Result<(), Error> {
self.entries
.insert((entry.local_zid, entry.remote_zid), entry);
Ok(())
}
fn clear(&mut self, local_zid: [u8; 12], remote_zid: [u8; 12]) -> Result<(), Error> {
self.entries.remove(&(local_zid, remote_zid));
Ok(())
}
}