use crate::{
Error, Holder, Port, Response, StatusWord, certificates, channel::PlainChannel, cwa14890,
files, key_sets::ComponentRoots, pkcs15, secure_messaging::SecureMessaging,
};
const BLOCK: usize = 8;
pub fn apdu(raw: &[u8]) -> Result<Vec<u8>, Error> {
let response = Response::parse(raw)?;
let _ = response.status.to_string();
response.into_success()
}
pub fn files(fci: &[u8]) -> Result<usize, Error> {
let _ = files::Location::new(fci).and_then(|location| location.last_file());
files::fci_length(fci)
}
pub fn secure_messaging(input: &[u8]) -> Result<(Vec<u8>, StatusWord), Error> {
let Some((status, plain)) = input.split_first_chunk() else {
return Response::parse(input).map(split);
};
let plain = &plain[..plain.len() - plain.len() % BLOCK];
let mut channel = SecureMessaging::fixed();
let protected = channel.protected_response(plain, StatusWord(u16::from_be_bytes(*status)));
channel.unprotect(&Response::parse(&protected)?).map(split)
}
pub fn secure_messaging_raw(raw: &[u8]) -> Result<(Vec<u8>, StatusWord), Error> {
SecureMessaging::fixed()
.unprotect(&Response::parse(raw)?)
.map(split)
}
fn split(response: Response) -> (Vec<u8>, StatusWord) {
(response.data, response.status)
}
pub fn cwa14890(input: &[u8]) -> Result<bool, Error> {
cwa14890::check_answers(input, &ComponentRoots::embedded())
}
pub fn pkcs15(file: &[u8]) -> Result<usize, Error> {
let file = &file[..file.len().min(usize::from(u16::MAX))];
let location = files::Location::new(&[0x60, 0x04]).expect("the path is valid");
let read = files::read_sequences(
&mut PlainChannel::new(&mut StoredFile(file)),
&location,
pkcs15::MAX_OBJECTS,
)?;
for labelled in [true, false] {
assert_eq!(
decoded(file, labelled),
decoded(&read, labelled),
"the object-by-object read changed the decode"
);
}
Ok(pkcs15::entries(file, true)?.len() + pkcs15::entries(file, false)?.len())
}
type Decoded = Option<Vec<(String, Vec<u8>, Vec<u8>)>>;
fn decoded(bytes: &[u8], labelled: bool) -> Decoded {
let entries = pkcs15::entries(bytes, labelled).ok()?;
Some(
entries
.into_iter()
.map(|e| (e.label, e.id, e.path))
.collect(),
)
}
struct StoredFile<'a>(&'a [u8]);
impl Port for StoredFile<'_> {
fn transmit(&mut self, apdu: &[u8]) -> Result<Vec<u8>, Error> {
let file = self.0;
let answer = match apdu {
[_, 0xA4, ..] => {
let [high, low] = u16::try_from(file.len()).unwrap_or(u16::MAX).to_be_bytes();
vec![0x6F, 0x04, 0x81, 0x02, high, low, 0x90, 0x00]
}
[_, 0xB0, p1, p2, le] => {
let offset = usize::from(u16::from_be_bytes([*p1, *p2]));
let wanted = usize::from(*le);
match file.get(offset..) {
Some(rest) if !rest.is_empty() => {
let data = &rest[..rest.len().min(wanted)];
let status: [u8; 2] = if data.len() < wanted {
[0x62, 0x82]
} else {
[0x90, 0x00]
};
[data, &status].concat()
}
_ => vec![0x6B, 0x00],
}
}
_ => vec![0x6D, 0x00],
};
Ok(answer)
}
}
pub fn certificates(stored: &[u8]) -> Result<Vec<u8>, Error> {
certificates::stored_x509(stored.to_vec())
}
pub fn holder(der: &[u8]) -> Result<Holder, Error> {
Holder::from_der(der)
}