use der::{Decode, Encode, Header, SliceReader, Tag};
use crate::{Command, Error, StatusWord, channel::CommandChannel};
const MASTER_FILE_NAME: &[u8] = b"Master.File";
const MASTER_FILE_ID: [u8; 2] = [0x3F, 0x00];
const MAX_READ_CHUNK: usize = 0xDE;
const SELECT: u8 = 0xA4;
const READ_BINARY: u8 = 0xB0;
const EOF_REACHED: StatusWord = StatusWord(0x6282);
const OFFSET_OUTSIDE_EF: StatusWord = StatusWord(0x6B00);
const FCI: u8 = 0x6F;
pub(crate) const MAX_HEADER: usize = 4;
pub(crate) struct Location(Vec<[u8; 2]>);
impl Location {
pub(crate) fn new(path: &[u8]) -> Option<Self> {
let ids = path.chunks(2).map(|id| <[u8; 2]>::try_from(id).ok());
let ids: Option<Vec<[u8; 2]>> = ids.collect();
let ids = ids?
.into_iter()
.filter(|id| *id != MASTER_FILE_ID)
.collect::<Vec<_>>();
(!ids.is_empty()).then_some(Self(ids))
}
pub(crate) fn last_file(&self) -> Option<[u8; 2]> {
self.0.last().copied()
}
}
pub(crate) fn read(card: &mut dyn CommandChannel, location: &Location) -> Result<Vec<u8>, Error> {
let mut file = SelectedFile::select(card, location)?;
file.read_to(file.length)?;
Ok(file.content)
}
pub(crate) fn read_sequences(
card: &mut dyn CommandChannel,
location: &Location,
objects: usize,
) -> Result<Vec<u8>, Error> {
let mut file = SelectedFile::select(card, location)?;
let mut start = 0;
file.read_to(MAX_HEADER)?;
for _ in 0..objects {
let Some(length) = file.content.get(start..).and_then(sequence_length) else {
break;
};
start += length;
file.read_to(start + MAX_HEADER)?;
}
Ok(file.content)
}
fn sequence_length(bytes: &[u8]) -> Option<usize> {
let header = Header::decode(&mut SliceReader::new(bytes).ok()?).ok()?;
if header.tag != Tag::Sequence {
return None;
}
(header.encoded_len().ok()? + header.length)
.ok()?
.try_into()
.ok()
}
pub(crate) fn select_master_file(card: &mut dyn CommandChannel) -> Result<(), Error> {
card.transmit(&master_file_selection())?
.into_success()
.map(drop)
}
pub(crate) fn leave_any_channel(card: &mut dyn CommandChannel) -> Result<(), Error> {
match select_master_file(card) {
Ok(()) | Err(Error::Status(_)) => Ok(()),
Err(error) => Err(error),
}
}
pub(crate) fn master_file_selection() -> Command {
selection(0x04, MASTER_FILE_NAME)
}
fn selection(p1: u8, file: &[u8]) -> Command {
Command {
cla: 0x00,
ins: SELECT,
p1,
p2: 0x00,
data: file.to_vec(),
le: None,
}
}
fn select(card: &mut dyn CommandChannel, p1: u8, file: &[u8]) -> Result<usize, Error> {
let response = card.transmit(&selection(p1, file))?;
fci_length(&response.into_success()?)
}
pub(crate) fn fci_length(fci: &[u8]) -> Result<usize, Error> {
if fci.is_empty() {
return Ok(0);
}
if fci[0] != FCI || fci.len() <= 2 {
return Err(Error::Status(StatusWord::SUCCESS));
}
Ok(file_length(fci))
}
fn file_length(fci: &[u8]) -> usize {
let [_, length, body @ ..] = fci else {
return 0;
};
if usize::from(*length) != body.len() {
return 0;
}
let mut index = 0;
let mut file_length = take(body, 0x81, &mut index);
take(body, 0x81, &mut index);
take(body, 0x84, &mut index);
if body.get(index) == Some(&0x85) && body.get(index + 1) == Some(&10) {
file_length = body.get(index + 5..index + 7);
}
match file_length {
Some([high, low, ..]) => usize::from(u16::from_be_bytes([*high, *low])),
_ => 0,
}
}
fn take<'a>(body: &'a [u8], tag: u8, index: &mut usize) -> Option<&'a [u8]> {
if body.get(*index) != Some(&tag) {
return None;
}
let length = usize::from(*body.get(*index + 1)?);
let value = body.get(*index + 2..*index + 2 + length)?;
*index += 2 + length;
Some(value)
}
struct SelectedFile<'a> {
card: &'a mut dyn CommandChannel,
length: usize,
offset: usize,
content: Vec<u8>,
ended: bool,
}
impl<'a> SelectedFile<'a> {
fn select(card: &'a mut dyn CommandChannel, location: &Location) -> Result<Self, Error> {
select_master_file(card)?;
let mut length = 0;
for id in &location.0 {
length = select(card, 0x00, id)?;
}
Ok(Self {
card,
length,
offset: 0,
content: Vec::new(),
ended: false,
})
}
fn read_to(&mut self, end: usize) -> Result<(), Error> {
let end = end.min(self.length);
for offset in (self.offset..end).step_by(MAX_READ_CHUNK) {
if self.ended {
break;
}
let le = (end - offset).min(MAX_READ_CHUNK);
let [p1, p2] = u16::try_from(offset)
.map_err(|_| Error::CommandTooLong(offset))?
.to_be_bytes();
let response = self.card.transmit(&Command {
cla: 0x00,
ins: READ_BINARY,
p1,
p2,
data: vec![],
le: Some(u16::try_from(le).unwrap_or(0)),
})?;
match response.status {
OFFSET_OUTSIDE_EF => self.ended = true,
status if status.is_success() || status == EOF_REACHED => {
self.content.extend(response.data);
self.ended = status == EOF_REACHED;
}
status => return Err(Error::Status(status)),
}
self.offset = offset + le;
}
Ok(())
}
}
#[cfg(test)]
mod tests;