use zeroize::{Zeroize, Zeroizing};
use crate::{
Command, Error, KeyGeneration, StatusWord, Transport,
card_authenticator::CardAuthenticator,
channel::{CommandChannel, PlainChannel},
secure_messaging::SecureChannel,
};
const VERIFY: u8 = 0x20;
pub struct Pin(Zeroizing<String>);
impl Pin {
#[must_use]
pub fn new(pin: String) -> Self {
Self(Zeroizing::new(pin))
}
pub(crate) fn check_format(&self) -> Result<(), Error> {
if PIN_LENGTH.contains(&self.0.encode_utf16().count()) {
Ok(())
} else {
Err(Error::InvalidPinFormat)
}
}
fn encode(&self) -> Vec<u8> {
self.0
.encode_utf16()
.map(|unit| unit.to_le_bytes()[0])
.collect()
}
}
const PIN_LENGTH: std::ops::RangeInclusive<usize> = 8..=16;
const RETRIES_LEFT: u8 = 0x63;
const RETRIES_MASK: u8 = 0xF0;
const RETRIES_TAG: u8 = 0xC0;
const BLOCKED: StatusWord = StatusWord(0x6983);
const MAX_RETRIES: u8 = 3;
pub(crate) struct PinGuard {
authenticator: CardAuthenticator,
expected: Option<u8>,
}
#[derive(Clone, Copy)]
enum Reached {
BeforeVerify,
VerifySent,
Accepted,
}
impl PinGuard {
pub(crate) fn new(authenticator: CardAuthenticator) -> Self {
Self {
authenticator,
expected: None,
}
}
pub(crate) fn key_generation(&self) -> KeyGeneration {
self.authenticator.key_generation()
}
pub(crate) fn read_retries(&mut self, transport: &mut impl Transport) -> Result<u8, Error> {
let result = self.query(transport);
match result {
Ok(retries) => self.expected = Some(retries),
Err(Error::PinBlocked) => self.expected = Some(0),
Err(_) => {}
}
result
}
pub(crate) fn verified<R>(
&mut self,
transport: &mut impl Transport,
pin: Pin,
work: impl FnOnce(&mut SecureChannel<'_>) -> Result<R, Error>,
) -> Result<R, Error> {
pin.check_format()?;
let expected = match self.expected {
None => return Err(Error::PinRetriesNotRead),
Some(0) => return Err(Error::PinBlocked),
Some(expected) => expected,
};
let authenticator = &self.authenticator;
let mut reached = Reached::BeforeVerify;
let result = transport.transaction(|card| {
let mut channel = authenticator.open_pin_channel(PlainChannel::new(card))?;
match query_retries(&mut channel)? {
0 => return Err(Error::PinBlocked),
retries if retries != expected => return Err(Error::PinRetriesChanged),
_ => {}
}
reached = Reached::VerifySent;
send_verify(&mut channel, pin)?;
reached = Reached::Accepted;
work(&mut authenticator.open_user_channel(channel)?)
});
self.expected = match (reached, &result) {
(Reached::Accepted, _) => Some(MAX_RETRIES),
(_, Err(Error::PinBlocked)) => Some(0),
(_, Err(Error::WrongPin { retries_left })) => Some(*retries_left),
(Reached::VerifySent, _) | (_, Err(Error::PinRetriesChanged)) => None,
(Reached::BeforeVerify, _) => self.expected,
};
result
}
fn query(&self, transport: &mut impl Transport) -> Result<u8, Error> {
transport.transaction(|card| {
query_retries(
&mut self
.authenticator
.open_pin_channel(PlainChannel::new(card))?,
)
})
}
}
fn send_verify(channel: &mut SecureChannel<'_>, pin: Pin) -> Result<(), Error> {
let mut command = Command {
cla: 0x00,
ins: VERIFY,
p1: 0x00,
p2: 0x00,
data: pin.encode(),
le: None,
};
drop(pin);
let response = channel.transmit(&command);
command.data.zeroize();
let response = response?;
if response.status != StatusWord::SUCCESS {
return Err(refusal(response.status));
}
Ok(())
}
fn refusal(status: StatusWord) -> Error {
match retries_in(status) {
Some(retries_left) if retries_left > 0 => Error::WrongPin { retries_left },
Some(_) => Error::PinBlocked,
None if status == BLOCKED => Error::PinBlocked,
None => Error::Status(status),
}
}
fn retries_in(status: StatusWord) -> Option<u8> {
match status.0.to_be_bytes() {
[RETRIES_LEFT, sw2] if sw2 & RETRIES_MASK == RETRIES_TAG => Some(sw2 & !RETRIES_MASK),
_ => None,
}
}
fn query_retries(channel: &mut SecureChannel<'_>) -> Result<u8, Error> {
let response = channel.transmit(&Command {
cla: 0x00,
ins: VERIFY,
p1: 0x00,
p2: 0x00,
data: vec![],
le: Some(256),
})?;
match retries_in(response.status) {
Some(retries) => Ok(retries),
None if response.status == BLOCKED => Err(Error::PinBlocked),
None => Err(Error::Status(response.status)),
}
}