Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improving support to CLI operations #2

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ edition = "2018"
pcsc = { version = "2.4.0" }
structopt = "0.3.21"
hex = "0.4.2"
termion = "1.5.6"
64 changes: 64 additions & 0 deletions src/apdu/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,66 @@
use crate::apdu::capdu::CryptogramType;
use crate::utils::extension::Hexadecimal;

pub mod capdu;
pub mod rapdu;

#[derive(Debug)]
pub enum Command {
Select {
application: Vec<u8>
},
GetProcessingOptions,
ReadRecord {
record: u8,
sfi: u8,
},
GenerateAC {
cryptogram_type: CryptogramType,
cdol: Option<Vec<u8>>,
},
PutData {
tag: u16,
value: Vec<u8>,
},
GetData {
tag: u16
},
PinUnblock,
}

impl Command {
pub fn from_str(str: String) -> Result<Command, String> {
let parts: Vec<&str> = str.trim().split(' ').collect();
let name = parts[0].to_lowercase();

match name.as_str() {
"select" => Ok(Command::Select {
application: parts[1].to_vec_u8()
}),
"get_processing_options" => Ok(Command::GetProcessingOptions),
"generate_ac" => {
let mut cdol = None;
if parts.len() > 2 {
cdol = Some(parts[2].to_vec_u8());
}
Ok(Command::GenerateAC {
cryptogram_type: CryptogramType::from_str(parts[1]),
cdol,
})
}
"get_data" => Ok(Command::GetData {
tag: parts[1].to_u16()
}),
"put_data" => Ok(Command::PutData {
tag: parts[1].to_u16(),
value: parts[2].to_vec_u8(),
}),
"read_record" => Ok(Command::ReadRecord {
record: parts[1].to_u8(),
sfi: parts[2].to_u8(),
}),
"pin_unblock" => Ok(Command::PinUnblock),
_ => Err(name)
}
}
}
61 changes: 0 additions & 61 deletions src/cli/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,3 @@ pub enum Mode {
input: PathBuf
},
}

#[derive(Debug)]
pub enum Command {
Select {
application: Vec<u8>
},
GetProcessingOptions,
ReadRecord {
record: u8,
sfi: u8,
},
GenerateAC {
cryptogram_type: CryptogramType,
cdol: Option<Vec<u8>>,
},
PutData {
tag: u16,
value: Vec<u8>,
},
GetData {
tag: u16
},
PinUnblock,
}

impl Command {
pub fn from_str(str: String) -> Result<Command, String> {
let parts: Vec<&str> = str.trim().split(' ').collect();
let name = parts[0].to_lowercase();

match name.as_str() {
"select" => Ok(Command::Select {
application: parts[1].to_vec_u8()
}),
"get_processing_options" => Ok(Command::GetProcessingOptions),
"generate_ac" => {
let mut cdol = None;
if parts.len() > 2 {
cdol = Some(parts[2].to_vec_u8());
}
Ok(Command::GenerateAC {
cryptogram_type: CryptogramType::from_str(parts[1]),
cdol,
})
}
"get_data" => Ok(Command::GetData {
tag: parts[1].to_u16()
}),
"put_data" => Ok(Command::PutData {
tag: parts[1].to_u16(),
value: parts[2].to_vec_u8(),
}),
"read_record" => Ok(Command::ReadRecord {
record: parts[1].to_u8(),
sfi: parts[2].to_u8(),
}),
"pin_unblock" => Ok(Command::PinUnblock),
_ => Err(name)
}
}
}
24 changes: 23 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
use std::io;
use std::io::Write;

use crate::cli::interface::Command;
use termion::color;

use crate::apdu::capdu::APDU;
use crate::apdu::Command;
use crate::apdu::rapdu::RAPDU;

mod banner;
pub mod interface;

const INPUT_COLOR: color::Rgb = color::Rgb(245, 181, 71);
const OUTPUT_COLOR: color::Rgb = color::Rgb(77, 128, 247);

pub fn announcement() {
println!("{}", banner::BANNER);
}

pub fn print_input(apdu: &RAPDU) {
println!("{red}R-APDU: {bytes:02X?}{reset}",
red = color::Fg(INPUT_COLOR),
bytes = apdu,
reset = color::Fg(color::Reset));
}

pub fn print_output(apdu: &APDU) {
println!("\n{green}C-APDU: {name}: {bytes:02X?}{reset}",
green = color::Fg(OUTPUT_COLOR),
name = apdu.name,
bytes = apdu.to_array(),
reset = color::Fg(color::Reset));
}

pub fn read_command() -> Option<Command> {
let mut buffer = String::new();
print!("> ");
Expand Down
5 changes: 3 additions & 2 deletions src/connection/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use pcsc::*;

use crate::apdu::capdu::APDU;
use crate::apdu::rapdu::{RAPDU, Status};
use crate::cli;

pub fn transmit(card: &Card, apdu: &APDU) -> Result<RAPDU, &'static str> {
let mut buffer = [0; MAX_BUFFER_SIZE];
println!("\nC-APDU: {}: {:02X?}", apdu.name, apdu.to_array());
cli::print_output(&apdu);
match card.transmit(&apdu.to_array(), &mut buffer) {
Ok(response) => {
let rapdu;
Expand All @@ -18,7 +19,7 @@ pub fn transmit(card: &Card, apdu: &APDU) -> Result<RAPDU, &'static str> {
else {
rapdu = RAPDU::new(Status::new(response[length - 2], response[length - 1]), &response[0..length - 2]);
}
println!("R-APDU: {:02X?}", rapdu);
cli::print_input(&rapdu);
Ok(rapdu)
}
Err(err) => {
Expand Down
18 changes: 12 additions & 6 deletions src/controller.rs → src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use structopt::StructOpt;

use crate::apdu::capdu;
use crate::apdu::capdu::{APDU, CryptogramType};
use crate::apdu::rapdu::{RAPDU, Status};
use crate::cli::interface::{Command, Emv, Mode};
use crate::connection::usb;
use crate::tlv::parser::TLV;
use apdu::capdu;
use apdu::capdu::{APDU, CryptogramType};
use apdu::rapdu::{RAPDU, Status};
use cli::interface::{Emv, Mode};
use connection::usb;
use tlv::parser::TLV;

pub mod apdu;
pub mod cli;
pub mod connection;
pub mod tlv;
pub mod utils;

fn send(card: &pcsc::Card, apdu: APDU) {
usb::transmit(card, &apdu)
Expand Down
27 changes: 11 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,10 @@ use std::process;

use structopt::StructOpt;

use crate::cli::interface::{Command, Emv, Mode};
use crate::connection::usb;

mod apdu;
mod cli;
mod connection;
mod tlv;
mod utils;
mod controller;
use emv::apdu::Command;
use emv::cli;
use emv::cli::interface::{Emv, Mode};
use emv::connection::usb;

fn main() {
let args: Emv = Emv::from_args();
Expand Down Expand Up @@ -55,26 +50,26 @@ fn run(input: PathBuf, card: pcsc::Card) {
fn execute(command: Command, card: &pcsc::Card) {
match command {
Command::Select { application } => {
controller::select_application(card, application);
emv::select_application(card, application);
}
Command::GetProcessingOptions => {
controller::get_processing_options(card);
emv::get_processing_options(card);
}
Command::GenerateAC { cryptogram_type, cdol } => {
let cdol_value = cdol.unwrap_or_else(|| { cli::read_hex_input("Input the CDOL value: ") });
controller::generate_ac(card, cryptogram_type, cdol_value);
emv::generate_ac(card, cryptogram_type, cdol_value);
}
Command::PutData { tag, value } => {
controller::put_data(card, tag, value, cli::read_hex_input("Input the MAC: "));
emv::put_data(card, tag, value, cli::read_hex_input("Input the MAC: "));
}
Command::GetData { tag } => {
controller::get_data(card, tag);
emv::get_data(card, tag);
}
Command::ReadRecord { record, sfi } => {
controller::read_record(&card, record, sfi);
emv::read_record(&card, record, sfi);
}
Command::PinUnblock => {
controller::unblock_pin(&card, cli::read_hex_input("Input the MAC: "));
emv::unblock_pin(&card, cli::read_hex_input("Input the MAC: "));
}
}
}