use crate::{
Error,
channel::{CommandChannel, PlainChannel},
cwa14890, files,
key_sets::{Channel, ComponentRoots, KeyGeneration, KeySet},
secure_messaging::SecureChannel,
};
const R2_IDESP: &str = "BMP100001";
pub(crate) struct CardAuthenticator {
generation: KeyGeneration,
roots: ComponentRoots,
}
impl CardAuthenticator {
pub(crate) fn from_idesp(idesp: Option<&str>, roots: ComponentRoots) -> Self {
let generation = if idesp.is_some_and(|idesp| !idesp.is_empty() && idesp < R2_IDESP) {
KeyGeneration::First
} else {
KeyGeneration::Second
};
Self { generation, roots }
}
#[cfg(test)]
pub(crate) fn new(generation: KeyGeneration, roots: ComponentRoots) -> Self {
Self { generation, roots }
}
pub(crate) fn key_generation(&self) -> KeyGeneration {
self.generation
}
pub(crate) fn open_pin_channel<'a>(
&self,
mut card: PlainChannel<'a>,
) -> Result<SecureChannel<'a>, Error> {
files::leave_any_channel(&mut card)?;
cwa14890::open(card, self.keys(Channel::Pin), &self.roots)
}
pub(crate) fn open_user_channel<'a>(
&self,
mut pin_channel: SecureChannel<'a>,
) -> Result<SecureChannel<'a>, Error> {
pin_channel
.transmit(&files::master_file_selection())?
.into_success()?;
cwa14890::open_inside(pin_channel, self.keys(Channel::User), &self.roots)
}
fn keys(&self, channel: Channel) -> &'static KeySet {
KeySet::for_generation(self.generation, channel)
}
}
#[cfg(test)]
mod tests;