2022-05-05 15:50:44 +02:00
|
|
|
use desfire::desfire::desfire::MAX_BYTES_PER_TRANSACTION;
|
|
|
|
use desfire::desfire::Desfire;
|
|
|
|
use desfire::error::Error as DesfireError;
|
|
|
|
use desfire::iso7816_4::apduresponse::APDUResponse;
|
2022-10-05 17:28:47 +02:00
|
|
|
use rsasl::callback::SessionData;
|
2022-11-01 10:47:51 +01:00
|
|
|
use rsasl::mechanism::{Authentication, MechanismData, MechanismError, MechanismErrorKind, State, ThisProvider};
|
2022-10-05 17:28:47 +02:00
|
|
|
use rsasl::prelude::{MessageSent, SASLConfig, SASLError, SessionError};
|
2022-05-05 15:50:44 +02:00
|
|
|
use rsasl::property::AuthId;
|
2022-03-16 19:29:36 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::convert::TryFrom;
|
2022-05-05 15:50:44 +02:00
|
|
|
use std::fmt::{Debug, Display, Formatter};
|
|
|
|
use std::io::Write;
|
2022-03-16 19:29:36 +01:00
|
|
|
use std::sync::Arc;
|
2022-04-26 23:21:43 +02:00
|
|
|
|
2022-03-16 19:29:36 +01:00
|
|
|
use crate::authentication::fabfire::FabFireCardKey;
|
|
|
|
|
|
|
|
enum FabFireError {
|
|
|
|
ParseError,
|
|
|
|
SerializationError,
|
|
|
|
DeserializationError(serde_json::Error),
|
|
|
|
CardError(DesfireError),
|
|
|
|
InvalidMagic(String),
|
|
|
|
InvalidToken(String),
|
|
|
|
InvalidURN(String),
|
|
|
|
InvalidCredentials(String),
|
|
|
|
Session(SessionError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Debug for FabFireError {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
FabFireError::ParseError => write!(f, "ParseError"),
|
|
|
|
FabFireError::SerializationError => write!(f, "SerializationError"),
|
|
|
|
FabFireError::DeserializationError(e) => write!(f, "DeserializationError: {}", e),
|
|
|
|
FabFireError::CardError(err) => write!(f, "CardError: {}", err),
|
|
|
|
FabFireError::InvalidMagic(magic) => write!(f, "InvalidMagic: {}", magic),
|
|
|
|
FabFireError::InvalidToken(token) => write!(f, "InvalidToken: {}", token),
|
|
|
|
FabFireError::InvalidURN(urn) => write!(f, "InvalidURN: {}", urn),
|
2022-05-05 15:50:44 +02:00
|
|
|
FabFireError::InvalidCredentials(credentials) => {
|
|
|
|
write!(f, "InvalidCredentials: {}", credentials)
|
|
|
|
}
|
2022-03-16 19:29:36 +01:00
|
|
|
FabFireError::Session(err) => write!(f, "Session: {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for FabFireError {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
FabFireError::ParseError => write!(f, "ParseError"),
|
|
|
|
FabFireError::SerializationError => write!(f, "SerializationError"),
|
|
|
|
FabFireError::DeserializationError(e) => write!(f, "DeserializationError: {}", e),
|
|
|
|
FabFireError::CardError(err) => write!(f, "CardError: {}", err),
|
|
|
|
FabFireError::InvalidMagic(magic) => write!(f, "InvalidMagic: {}", magic),
|
|
|
|
FabFireError::InvalidToken(token) => write!(f, "InvalidToken: {}", token),
|
|
|
|
FabFireError::InvalidURN(urn) => write!(f, "InvalidURN: {}", urn),
|
2022-05-05 15:50:44 +02:00
|
|
|
FabFireError::InvalidCredentials(credentials) => {
|
|
|
|
write!(f, "InvalidCredentials: {}", credentials)
|
|
|
|
}
|
2022-03-16 19:29:36 +01:00
|
|
|
FabFireError::Session(err) => write!(f, "Session: {}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 17:28:47 +02:00
|
|
|
impl std::error::Error for FabFireError {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-16 19:29:36 +01:00
|
|
|
impl MechanismError for FabFireError {
|
|
|
|
fn kind(&self) -> MechanismErrorKind {
|
|
|
|
match self {
|
|
|
|
FabFireError::ParseError => MechanismErrorKind::Parse,
|
|
|
|
FabFireError::SerializationError => MechanismErrorKind::Protocol,
|
|
|
|
FabFireError::DeserializationError(_) => MechanismErrorKind::Parse,
|
|
|
|
FabFireError::CardError(_) => MechanismErrorKind::Protocol,
|
|
|
|
FabFireError::InvalidMagic(_) => MechanismErrorKind::Protocol,
|
|
|
|
FabFireError::InvalidToken(_) => MechanismErrorKind::Protocol,
|
|
|
|
FabFireError::InvalidURN(_) => MechanismErrorKind::Protocol,
|
|
|
|
FabFireError::InvalidCredentials(_) => MechanismErrorKind::Protocol,
|
|
|
|
FabFireError::Session(_) => MechanismErrorKind::Protocol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct CardInfo {
|
|
|
|
#[serde(rename = "UID", with = "hex")]
|
|
|
|
uid: [u8; 7],
|
|
|
|
key_old: Option<Box<[u8]>>,
|
|
|
|
key_new: Option<Box<[u8]>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct KeyInfo {
|
|
|
|
key_id: u8,
|
|
|
|
key: Box<[u8]>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AuthInfo {
|
|
|
|
rnd_a: Vec<u8>,
|
|
|
|
rnd_b: Vec<u8>,
|
|
|
|
iv: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(tag = "Cmd")]
|
|
|
|
enum CardCommand {
|
|
|
|
message {
|
|
|
|
#[serde(rename = "MssgID", skip_serializing_if = "Option::is_none")]
|
|
|
|
msg_id: Option<u32>,
|
|
|
|
#[serde(rename = "ClrTxt", skip_serializing_if = "Option::is_none")]
|
|
|
|
clr_txt: Option<String>,
|
|
|
|
#[serde(rename = "AddnTxt", skip_serializing_if = "Option::is_none")]
|
|
|
|
addn_txt: Option<String>,
|
|
|
|
},
|
|
|
|
sendPICC {
|
2022-05-05 15:50:44 +02:00
|
|
|
#[serde(
|
|
|
|
deserialize_with = "hex::deserialize",
|
|
|
|
serialize_with = "hex::serialize_upper"
|
|
|
|
)]
|
|
|
|
data: Vec<u8>,
|
2022-03-16 19:29:36 +01:00
|
|
|
},
|
|
|
|
readPICC {
|
2022-05-05 15:50:44 +02:00
|
|
|
#[serde(
|
|
|
|
deserialize_with = "hex::deserialize",
|
|
|
|
serialize_with = "hex::serialize_upper"
|
|
|
|
)]
|
|
|
|
data: Vec<u8>,
|
2022-03-16 19:29:36 +01:00
|
|
|
},
|
|
|
|
haltPICC,
|
|
|
|
Key {
|
2022-05-05 15:50:44 +02:00
|
|
|
data: String,
|
2022-03-16 19:29:36 +01:00
|
|
|
},
|
|
|
|
ConfirmUser,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum Step {
|
|
|
|
New,
|
|
|
|
SelectApp,
|
|
|
|
VerifyMagic,
|
|
|
|
GetURN,
|
|
|
|
GetToken,
|
|
|
|
Authenticate1,
|
|
|
|
Authenticate2,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FabFire {
|
|
|
|
step: Step,
|
|
|
|
card_info: Option<CardInfo>,
|
|
|
|
key_info: Option<KeyInfo>,
|
|
|
|
auth_info: Option<AuthInfo>,
|
|
|
|
app_id: u32,
|
|
|
|
local_urn: String,
|
|
|
|
desfire: Desfire,
|
|
|
|
}
|
|
|
|
|
|
|
|
const MAGIC: &'static str = "FABACCESS\0DESFIRE\01.0\0";
|
|
|
|
|
|
|
|
impl FabFire {
|
2022-10-05 17:28:47 +02:00
|
|
|
pub fn new_server(_sasl: &SASLConfig) -> Result<Box<dyn Authentication>, SASLError> {
|
2022-05-05 15:50:44 +02:00
|
|
|
Ok(Box::new(Self {
|
|
|
|
step: Step::New,
|
|
|
|
card_info: None,
|
|
|
|
key_info: None,
|
|
|
|
auth_info: None,
|
2022-05-24 23:41:42 +02:00
|
|
|
app_id: 0x464142,
|
2022-05-05 15:50:44 +02:00
|
|
|
local_urn: "urn:fabaccess:lab:innovisionlab".to_string(),
|
|
|
|
desfire: Desfire {
|
|
|
|
card: None,
|
|
|
|
session_key: None,
|
|
|
|
cbc_iv: None,
|
|
|
|
},
|
|
|
|
}))
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Authentication for FabFire {
|
2022-05-05 15:50:44 +02:00
|
|
|
fn step(
|
|
|
|
&mut self,
|
2022-11-01 10:47:51 +01:00
|
|
|
session: &mut MechanismData<'_>,
|
2022-05-05 15:50:44 +02:00
|
|
|
input: Option<&[u8]>,
|
|
|
|
writer: &mut dyn Write,
|
2022-10-05 17:28:47 +02:00
|
|
|
) -> Result<State, SessionError> {
|
2022-03-16 19:29:36 +01:00
|
|
|
match self.step {
|
|
|
|
Step::New => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::trace!("Step: New");
|
2022-03-16 19:29:36 +01:00
|
|
|
//receive card info (especially card UID) from reader
|
|
|
|
return match input {
|
2022-05-05 15:50:44 +02:00
|
|
|
None => Err(SessionError::InputDataRequired),
|
2022-03-16 19:29:36 +01:00
|
|
|
Some(cardinfo) => {
|
|
|
|
self.card_info = match serde_json::from_slice(cardinfo) {
|
|
|
|
Ok(card_info) => Some(card_info),
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Deserializing card_info failed: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::DeserializationError(e).into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
//select application
|
|
|
|
let buf = match self.desfire.select_application_cmd(self.app_id) {
|
|
|
|
Ok(buf) => match Vec::<u8>::try_from(buf) {
|
|
|
|
Ok(data) => data,
|
|
|
|
Err(e) => {
|
2022-05-05 15:50:44 +02:00
|
|
|
tracing::error!(
|
|
|
|
"Failed to convert APDUCommand to Vec<u8>: {:?}",
|
|
|
|
e
|
|
|
|
);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Failed to generate APDUCommand: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let cmd = CardCommand::sendPICC { data: buf };
|
|
|
|
return match serde_json::to_vec(&cmd) {
|
|
|
|
Ok(send_buf) => {
|
|
|
|
self.step = Step::SelectApp;
|
2022-05-05 15:50:44 +02:00
|
|
|
writer
|
|
|
|
.write_all(&send_buf)
|
|
|
|
.map_err(|e| SessionError::Io { source: e })?;
|
2022-10-05 17:28:47 +02:00
|
|
|
Ok(State::Running)
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Failed to serialize APDUCommand: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
Err(FabFireError::SerializationError.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Step::SelectApp => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::trace!("Step: SelectApp");
|
2022-03-16 19:29:36 +01:00
|
|
|
// check that we successfully selected the application
|
|
|
|
let response: CardCommand = match input {
|
2022-05-05 15:50:44 +02:00
|
|
|
None => {
|
|
|
|
return Err(SessionError::InputDataRequired);
|
|
|
|
}
|
|
|
|
Some(buf) => match serde_json::from_slice(buf)
|
|
|
|
.map_err(|e| FabFireError::DeserializationError(e))
|
|
|
|
{
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(response) => response,
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Deserializing data from card failed: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
},
|
2022-03-16 19:29:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let apdu_response = match response {
|
2022-05-05 15:50:44 +02:00
|
|
|
CardCommand::readPICC { data } => APDUResponse::new(&*data),
|
2022-03-16 19:29:36 +01:00
|
|
|
_ => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Unexpected response: {:?}", response);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
apdu_response
|
|
|
|
.check()
|
|
|
|
.map_err(|e| FabFireError::CardError(e))?;
|
2022-03-16 19:29:36 +01:00
|
|
|
|
|
|
|
// request the contents of the file containing the magic string
|
|
|
|
const MAGIC_FILE_ID: u8 = 0x01;
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
let buf = match self
|
|
|
|
.desfire
|
|
|
|
.read_data_chunk_cmd(MAGIC_FILE_ID, 0, MAGIC.len())
|
|
|
|
{
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(buf) => match Vec::<u8>::try_from(buf) {
|
|
|
|
Ok(data) => data,
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to convert APDUCommand to Vec<u8>: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
},
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to generate APDUCommand: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let cmd = CardCommand::sendPICC { data: buf };
|
|
|
|
return match serde_json::to_vec(&cmd) {
|
|
|
|
Ok(send_buf) => {
|
|
|
|
self.step = Step::VerifyMagic;
|
2022-05-05 15:50:44 +02:00
|
|
|
writer
|
|
|
|
.write_all(&send_buf)
|
|
|
|
.map_err(|e| SessionError::Io { source: e })?;
|
2022-10-05 17:28:47 +02:00
|
|
|
Ok(State::Running)
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to serialize APDUCommand: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
Err(FabFireError::SerializationError.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Step::VerifyMagic => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::trace!("Step: VerifyMagic");
|
2022-03-16 19:29:36 +01:00
|
|
|
// verify the magic string to determine that we have a valid fabfire card
|
|
|
|
let response: CardCommand = match input {
|
2022-05-05 15:50:44 +02:00
|
|
|
None => {
|
|
|
|
return Err(SessionError::InputDataRequired);
|
|
|
|
}
|
|
|
|
Some(buf) => match serde_json::from_slice(buf)
|
|
|
|
.map_err(|e| FabFireError::DeserializationError(e))
|
|
|
|
{
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(response) => response,
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Deserializing data from card failed: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
},
|
2022-03-16 19:29:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let apdu_response = match response {
|
2022-05-05 15:50:44 +02:00
|
|
|
CardCommand::readPICC { data } => APDUResponse::new(&*data),
|
2022-03-16 19:29:36 +01:00
|
|
|
_ => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Unexpected response: {:?}", response);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match apdu_response.check() {
|
|
|
|
Ok(_) => {
|
|
|
|
match apdu_response.body {
|
|
|
|
Some(data) => {
|
|
|
|
if std::str::from_utf8(data.as_slice()) != Ok(MAGIC) {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Invalid magic string");
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("No data returned from card");
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Got invalid APDUResponse: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// request the contents of the file containing the URN
|
|
|
|
const URN_FILE_ID: u8 = 0x02;
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
let buf = match self.desfire.read_data_chunk_cmd(
|
|
|
|
URN_FILE_ID,
|
|
|
|
0,
|
|
|
|
self.local_urn.as_bytes().len(),
|
|
|
|
) {
|
|
|
|
// TODO: support urn longer than 47 Bytes
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(buf) => match Vec::<u8>::try_from(buf) {
|
|
|
|
Ok(data) => data,
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to convert APDUCommand to Vec<u8>: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
},
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to generate APDUCommand: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let cmd = CardCommand::sendPICC { data: buf };
|
|
|
|
return match serde_json::to_vec(&cmd) {
|
|
|
|
Ok(send_buf) => {
|
|
|
|
self.step = Step::GetURN;
|
2022-05-05 15:50:44 +02:00
|
|
|
writer
|
|
|
|
.write_all(&send_buf)
|
|
|
|
.map_err(|e| SessionError::Io { source: e })?;
|
2022-10-05 17:28:47 +02:00
|
|
|
Ok(State::Running)
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to serialize APDUCommand: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
Err(FabFireError::SerializationError.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Step::GetURN => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::trace!("Step: GetURN");
|
2022-03-16 19:29:36 +01:00
|
|
|
// parse the urn and match it to our local urn
|
|
|
|
let response: CardCommand = match input {
|
2022-05-05 15:50:44 +02:00
|
|
|
None => {
|
|
|
|
return Err(SessionError::InputDataRequired);
|
|
|
|
}
|
|
|
|
Some(buf) => match serde_json::from_slice(buf)
|
|
|
|
.map_err(|e| FabFireError::DeserializationError(e))
|
|
|
|
{
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(response) => response,
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Deserializing data from card failed: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
},
|
2022-03-16 19:29:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let apdu_response = match response {
|
2022-05-05 15:50:44 +02:00
|
|
|
CardCommand::readPICC { data } => APDUResponse::new(&*data),
|
2022-03-16 19:29:36 +01:00
|
|
|
_ => {
|
2022-05-05 15:50:44 +02:00
|
|
|
tracing::error!("Unexpected response: {:?}", response);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match apdu_response.check() {
|
|
|
|
Ok(_) => {
|
|
|
|
match apdu_response.body {
|
|
|
|
Some(data) => {
|
|
|
|
let received_urn = String::from_utf8(data).unwrap();
|
|
|
|
if received_urn != self.local_urn {
|
2022-05-05 15:50:44 +02:00
|
|
|
tracing::error!(
|
|
|
|
"URN mismatch: {:?} != {:?}",
|
|
|
|
received_urn,
|
|
|
|
self.local_urn
|
|
|
|
);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("No data returned from card");
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2022-05-05 15:50:44 +02:00
|
|
|
tracing::error!("Got invalid APDUResponse: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// request the contents of the file containing the URN
|
|
|
|
const TOKEN_FILE_ID: u8 = 0x03;
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
let buf = match self.desfire.read_data_chunk_cmd(
|
|
|
|
TOKEN_FILE_ID,
|
|
|
|
0,
|
|
|
|
MAX_BYTES_PER_TRANSACTION,
|
|
|
|
) {
|
|
|
|
// TODO: support data longer than 47 Bytes
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(buf) => match Vec::<u8>::try_from(buf) {
|
|
|
|
Ok(data) => data,
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to convert APDUCommand to Vec<u8>: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
},
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to generate APDUCommand: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let cmd = CardCommand::sendPICC { data: buf };
|
|
|
|
return match serde_json::to_vec(&cmd) {
|
|
|
|
Ok(send_buf) => {
|
|
|
|
self.step = Step::GetToken;
|
2022-05-05 15:50:44 +02:00
|
|
|
writer
|
|
|
|
.write_all(&send_buf)
|
|
|
|
.map_err(|e| SessionError::Io { source: e })?;
|
2022-10-05 17:28:47 +02:00
|
|
|
Ok(State::Running)
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to serialize APDUCommand: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
Err(FabFireError::SerializationError.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Step::GetToken => {
|
|
|
|
// println!("Step: GetToken");
|
|
|
|
// parse the token and select the appropriate user
|
|
|
|
let response: CardCommand = match input {
|
2022-05-05 15:50:44 +02:00
|
|
|
None => {
|
|
|
|
return Err(SessionError::InputDataRequired);
|
|
|
|
}
|
|
|
|
Some(buf) => match serde_json::from_slice(buf)
|
|
|
|
.map_err(|e| FabFireError::DeserializationError(e))
|
|
|
|
{
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(response) => response,
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Deserializing data from card failed: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
},
|
2022-03-16 19:29:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let apdu_response = match response {
|
2022-05-05 15:50:44 +02:00
|
|
|
CardCommand::readPICC { data } => APDUResponse::new(&*data),
|
2022-03-16 19:29:36 +01:00
|
|
|
_ => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Unexpected response: {:?}", response);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match apdu_response.check() {
|
|
|
|
Ok(_) => {
|
|
|
|
match apdu_response.body {
|
|
|
|
Some(data) => {
|
|
|
|
let token = String::from_utf8(data).unwrap();
|
2022-10-05 17:28:47 +02:00
|
|
|
let prov =
|
|
|
|
ThisProvider::<AuthId>::with(token.trim_matches(char::from(0)));
|
|
|
|
let key = session.need_with::<FabFireCardKey, _, _>(&prov, |key| Ok(Box::from(key.as_slice())))?;
|
2022-05-05 15:50:44 +02:00
|
|
|
self.key_info = Some(KeyInfo { key_id: 0x01, key });
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
|
|
|
None => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("No data in response");
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Failed to check response: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
let buf = match self
|
|
|
|
.desfire
|
|
|
|
.authenticate_iso_aes_challenge_cmd(self.key_info.as_ref().unwrap().key_id)
|
|
|
|
{
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(buf) => match Vec::<u8>::try_from(buf) {
|
|
|
|
Ok(data) => data,
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to convert to Vec<u8>: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
},
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to create authenticate command: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let cmd = CardCommand::sendPICC { data: buf };
|
|
|
|
return match serde_json::to_vec(&cmd) {
|
|
|
|
Ok(send_buf) => {
|
|
|
|
self.step = Step::Authenticate1;
|
2022-05-05 15:50:44 +02:00
|
|
|
writer
|
|
|
|
.write_all(&send_buf)
|
|
|
|
.map_err(|e| SessionError::Io { source: e })?;
|
2022-10-05 17:28:47 +02:00
|
|
|
Ok(State::Running)
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to serialize command: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
Err(FabFireError::SerializationError.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Step::Authenticate1 => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::trace!("Step: Authenticate1");
|
2022-03-16 19:29:36 +01:00
|
|
|
let response: CardCommand = match input {
|
2022-05-05 15:50:44 +02:00
|
|
|
None => {
|
|
|
|
return Err(SessionError::InputDataRequired);
|
|
|
|
}
|
|
|
|
Some(buf) => match serde_json::from_slice(buf)
|
|
|
|
.map_err(|e| FabFireError::DeserializationError(e))
|
|
|
|
{
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(response) => response,
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Failed to deserialize response: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
},
|
2022-03-16 19:29:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let apdu_response = match response {
|
2022-05-05 15:50:44 +02:00
|
|
|
CardCommand::readPICC { data } => APDUResponse::new(&*data),
|
2022-03-16 19:29:36 +01:00
|
|
|
_ => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Unexpected response: {:?}", response);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match apdu_response.check() {
|
|
|
|
Ok(_) => {
|
|
|
|
match apdu_response.body {
|
|
|
|
Some(data) => {
|
|
|
|
let rnd_b_enc = data.as_slice();
|
|
|
|
|
|
|
|
//FIXME: This is ugly, we should find a better way to make the function testable
|
|
|
|
//TODO: Check if we need a CSPRNG here
|
|
|
|
let rnd_a: [u8; 16] = rand::random();
|
|
|
|
|
2022-05-05 15:50:44 +02:00
|
|
|
let (cmd_challenge_response, rnd_b, iv) = self
|
|
|
|
.desfire
|
|
|
|
.authenticate_iso_aes_response_cmd(
|
|
|
|
rnd_b_enc,
|
|
|
|
&*(self.key_info.as_ref().unwrap().key),
|
|
|
|
&rnd_a,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
self.auth_info = Some(AuthInfo {
|
|
|
|
rnd_a: Vec::<u8>::from(rnd_a),
|
2022-03-16 19:29:36 +01:00
|
|
|
rnd_b,
|
2022-05-05 15:50:44 +02:00
|
|
|
iv,
|
|
|
|
});
|
2022-03-16 19:29:36 +01:00
|
|
|
let buf = match Vec::<u8>::try_from(cmd_challenge_response) {
|
|
|
|
Ok(data) => data,
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to convert to Vec<u8>: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::SerializationError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let cmd = CardCommand::sendPICC { data: buf };
|
|
|
|
return match serde_json::to_vec(&cmd) {
|
|
|
|
Ok(send_buf) => {
|
|
|
|
self.step = Step::Authenticate2;
|
2022-05-05 15:50:44 +02:00
|
|
|
writer
|
|
|
|
.write_all(&send_buf)
|
|
|
|
.map_err(|e| SessionError::Io { source: e })?;
|
2022-10-05 17:28:47 +02:00
|
|
|
Ok(State::Running)
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to serialize command: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
Err(FabFireError::SerializationError.into())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
None => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Got invalid response: {:?}", apdu_response);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-03-19 06:00:21 +01:00
|
|
|
Err(e) => {
|
|
|
|
tracing::error!("Failed to check response: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Step::Authenticate2 => {
|
|
|
|
// println!("Step: Authenticate2");
|
|
|
|
let response: CardCommand = match input {
|
2022-05-05 15:50:44 +02:00
|
|
|
None => {
|
|
|
|
return Err(SessionError::InputDataRequired);
|
|
|
|
}
|
|
|
|
Some(buf) => match serde_json::from_slice(buf)
|
|
|
|
.map_err(|e| FabFireError::DeserializationError(e))
|
|
|
|
{
|
2022-03-16 19:29:36 +01:00
|
|
|
Ok(response) => response,
|
|
|
|
Err(e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Failed to deserialize response: {:?}", e);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(e.into());
|
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
},
|
2022-03-16 19:29:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
let apdu_response = match response {
|
2022-05-05 15:50:44 +02:00
|
|
|
CardCommand::readPICC { data } => APDUResponse::new(&*data),
|
2022-03-16 19:29:36 +01:00
|
|
|
_ => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Got invalid response: {:?}", response);
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match apdu_response.check() {
|
|
|
|
Ok(_) => {
|
|
|
|
match apdu_response.body {
|
2022-05-05 15:50:44 +02:00
|
|
|
Some(data) => match self.auth_info.as_ref() {
|
|
|
|
None => {
|
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
Some(auth_info) => {
|
|
|
|
if self
|
|
|
|
.desfire
|
|
|
|
.authenticate_iso_aes_verify(
|
2022-03-16 19:29:36 +01:00
|
|
|
data.as_slice(),
|
|
|
|
auth_info.rnd_a.as_slice(),
|
2022-05-05 15:50:44 +02:00
|
|
|
auth_info.rnd_b.as_slice(),
|
|
|
|
&*(self.key_info.as_ref().unwrap().key),
|
|
|
|
auth_info.iv.as_slice(),
|
|
|
|
)
|
|
|
|
.is_ok()
|
|
|
|
{
|
|
|
|
let cmd = CardCommand::message {
|
|
|
|
msg_id: Some(4),
|
|
|
|
clr_txt: None,
|
|
|
|
addn_txt: Some("".to_string()),
|
|
|
|
};
|
|
|
|
return match serde_json::to_vec(&cmd) {
|
|
|
|
Ok(send_buf) => {
|
|
|
|
self.step = Step::Authenticate1;
|
|
|
|
writer
|
|
|
|
.write_all(&send_buf)
|
|
|
|
.map_err(|e| SessionError::Io { source: e })?;
|
2022-10-05 17:28:47 +02:00
|
|
|
return Ok(State::Finished(MessageSent::Yes));
|
2022-05-05 15:50:44 +02:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
tracing::error!(
|
|
|
|
"Failed to serialize command: {:?}",
|
|
|
|
e
|
|
|
|
);
|
|
|
|
Err(FabFireError::SerializationError.into())
|
|
|
|
}
|
|
|
|
};
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
},
|
2022-03-16 19:29:36 +01:00
|
|
|
None => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("got empty response");
|
2022-03-16 19:29:36 +01:00
|
|
|
return Err(FabFireError::ParseError.into());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2022-04-26 23:21:43 +02:00
|
|
|
Err(_e) => {
|
2022-03-19 06:00:21 +01:00
|
|
|
tracing::error!("Got invalid response: {:?}", apdu_response);
|
2022-05-05 15:50:44 +02:00
|
|
|
return Err(
|
|
|
|
FabFireError::InvalidCredentials(format!("{}", apdu_response)).into(),
|
|
|
|
);
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 17:28:47 +02:00
|
|
|
return Ok(State::Finished(MessageSent::No));
|
2022-03-16 19:29:36 +01:00
|
|
|
}
|
2022-05-05 15:50:44 +02:00
|
|
|
}
|