use super::{AuthenticationState, Card, CardIoDefault, Unauthenticated};
use crate::{Error, KeyId, Session, StatusCode, io};
pub struct Authenticated {
pub(crate) session: Session,
}
impl Authenticated {
pub fn session(&self) -> &Session {
&self.session
}
pub fn session_mut(&mut self) -> &mut Session {
&mut self.session
}
}
impl AuthenticationState for Authenticated {}
impl<'card, IoBackendT> CardIoDefault<IoBackendT> for Card<'card, IoBackendT, Authenticated>
where
IoBackendT: io::Backend,
{
async fn default_exchange_multi<'a>(
&mut self,
output: &'a mut [u8],
header: &[u8],
data: &[&[u8]],
) -> Result<(StatusCode, &'a [u8]), Error<IoBackendT::Error>> {
Ok(match &mut self.authentication.session {
Session::Aes { keying, .. } => {
io::plain_out_cmac_in(&self.card, keying, output, header, data).await?
}
Session::Des { keying, .. } => {
io::plain_out_cmac_in(&self.card, keying, output, header, data).await?
}
})
}
async fn default_exchange_multi_de_minimis<'a>(
&'a mut self,
header: &[u8],
data: &[&[u8]],
) -> Result<(StatusCode, &'a [u8]), Error<IoBackendT::Error>> {
Ok(match &mut self.authentication.session {
Session::Aes { keying, .. } => {
io::plain_out_cmac_in(&self.card, keying, &mut self.buf, header, data).await?
}
Session::Des { keying, .. } => {
io::plain_out_cmac_in(&self.card, keying, &mut self.buf, header, data).await?
}
})
}
}
impl<'card, IoBackendT> Card<'card, IoBackendT, Authenticated>
where
IoBackendT: io::Backend,
{
pub fn get_current_key_id(&self) -> KeyId {
self.authentication.session.get_key_id()
}
}
impl<'card, IoBackendT, AuthenticationStateT> Card<'card, IoBackendT, AuthenticationStateT>
where
AuthenticationStateT: AuthenticationState,
IoBackendT: io::Backend,
{
pub fn to_unauthenticated(self) -> Card<'card, IoBackendT, Unauthenticated> {
let Self {
card,
buf,
application_id,
authentication: _,
} = self;
Card::<'card, IoBackendT, Unauthenticated> {
card,
buf,
application_id,
authentication: Unauthenticated,
}
}
}