use crate::{
Error, Holder, Response, StatusWord, certificates, 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<Response, Error> {
let Some((status, plain)) = input.split_first_chunk() else {
return Response::parse(input);
};
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)?)
}
pub fn secure_messaging_raw(raw: &[u8]) -> Result<Response, Error> {
SecureMessaging::fixed().unprotect(&Response::parse(raw)?)
}
pub fn cwa14890(input: &[u8]) -> Result<bool, Error> {
cwa14890::check_answers(input, &ComponentRoots::embedded())
}
pub fn pkcs15(file: &[u8]) -> Result<usize, Error> {
Ok(pkcs15::entries(file, true)?.len() + pkcs15::entries(file, false)?.len())
}
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)
}