#[cfg(feature = "mockhsm")]
use crate::device;
use crate::{
command, connector, response,
session::{
self,
securechannel::{Mac, MAC_SIZE},
ErrorKind::ProtocolError,
},
};
#[derive(Debug)]
pub(crate) struct Message {
pub code: response::Code,
pub session_id: Option<session::Id>,
pub data: Vec<u8>,
pub mac: Option<Mac>,
}
impl Message {
pub fn parse(message: connector::Message) -> Result<Self, session::Error> {
let connector::Message(mut bytes) = message;
if bytes.len() < 3 {
fail!(
ProtocolError,
"response too short: {} (expected at least 3-bytes)",
bytes.len()
);
}
let code = response::Code::from_u8(bytes[0]).map_err(|e| err!(ProtocolError, "{}", e))?;
let mut length_bytes = [0u8; 2];
length_bytes.copy_from_slice(&bytes[1..3]);
let length = u16::from_be_bytes(length_bytes) as usize;
if length.checked_add(3).unwrap() != bytes.len() {
fail!(
ProtocolError,
"unexpected response length {} (expecting {})",
bytes.len().checked_sub(3).unwrap(),
length
);
}
bytes.drain(..3);
let session_id = if has_session_id(code) {
if bytes.is_empty() {
fail!(ProtocolError, "session ID missing");
} else {
Some(session::Id::from_u8(bytes.remove(0))?)
}
} else {
None
};
let mac = if has_rmac(code) {
if bytes.len() < MAC_SIZE {
fail!(ProtocolError, "missing R-MAC for {:?}", code,);
}
let mac_index = bytes.len() - MAC_SIZE;
Some(Mac::from_slice(&bytes.split_off(mac_index)))
} else {
None
};
Ok(Self {
code,
session_id,
data: bytes,
mac,
})
}
#[cfg(feature = "mockhsm")]
pub fn new<T>(code: response::Code, response_data: T) -> Self
where
T: Into<Vec<u8>>,
{
Self {
code,
session_id: None,
data: response_data.into(),
mac: None,
}
}
#[cfg(feature = "mockhsm")]
pub fn new_with_mac<D, M>(
code: response::Code,
session_id: session::Id,
response_data: D,
mac: M,
) -> Self
where
D: Into<Vec<u8>>,
M: Into<Mac>,
{
Self {
code,
session_id: Some(session_id),
data: response_data.into(),
mac: Some(mac.into()),
}
}
#[cfg(feature = "mockhsm")]
pub fn success<T>(command_type: command::Code, response_data: T) -> Self
where
T: Into<Vec<u8>>,
{
Self::new(response::Code::Success(command_type), response_data)
}
pub fn is_err(&self) -> bool {
match self.code {
response::Code::Success(_) => false,
_ => true,
}
}
pub fn command(&self) -> Option<command::Code> {
match self.code {
response::Code::Success(cmd) => Some(cmd),
_ => None,
}
}
pub fn len(&self) -> usize {
let mut result = self.data.len();
if self.session_id.is_some() {
result += 1;
}
if self.mac.is_some() {
result += MAC_SIZE;
}
result
}
}
#[cfg(feature = "mockhsm")]
impl From<device::ErrorKind> for Message {
fn from(kind: device::ErrorKind) -> Self {
Self::new(response::Code::MemoryError, vec![kind.to_u8()])
}
}
#[cfg(feature = "mockhsm")]
impl Into<Vec<u8>> for Message {
fn into(mut self) -> Vec<u8> {
let mut result = Vec::with_capacity(3 + self.len());
result.push(self.code.to_u8());
let length = self.len() as u16;
result.extend_from_slice(&length.to_be_bytes());
if let Some(session_id) = self.session_id {
result.push(session_id.to_u8());
}
result.append(&mut self.data);
if let Some(mac) = self.mac {
result.extend_from_slice(mac.as_slice());
}
result
}
}
fn has_session_id(code: response::Code) -> bool {
match code {
response::Code::Success(cmd_type) => match cmd_type {
command::Code::CreateSession | command::Code::SessionMessage => true,
_ => false,
},
_ => false,
}
}
fn has_rmac(code: response::Code) -> bool {
match code {
response::Code::Success(cmd_type) => match cmd_type {
command::Code::SessionMessage => true,
_ => false,
},
_ => false,
}
}