108 lines
3.3 KiB
Rust
108 lines
3.3 KiB
Rust
|
|
use std::ffi::CString;
|
||
|
|
|
||
|
|
use pcsc::{Protocols, ReaderState, State};
|
||
|
|
|
||
|
|
use crate::{Card, ResultAPDU};
|
||
|
|
|
||
|
|
pub struct PCSCCard {
|
||
|
|
pub ctx: pcsc::Context,
|
||
|
|
pub reader: CString,
|
||
|
|
pub card: pcsc::Card,
|
||
|
|
pub buf: [u8; 0x10002],
|
||
|
|
}
|
||
|
|
|
||
|
|
impl Card for PCSCCard {
|
||
|
|
async fn transmit(
|
||
|
|
&mut self,
|
||
|
|
apdu: crate::OwnedCommandAPDU,
|
||
|
|
) -> std::io::Result<crate::ResultAPDU> {
|
||
|
|
let mut apdu_buf = vec![
|
||
|
|
apdu.class.encode().unwrap(),
|
||
|
|
apdu.instruction,
|
||
|
|
apdu.parameter[0],
|
||
|
|
apdu.parameter[1],
|
||
|
|
];
|
||
|
|
let extended =
|
||
|
|
apdu.command.len() > 0xFF || apdu.expected_length.map(|f| f > 0xFF) == Some(true);
|
||
|
|
|
||
|
|
if extended {
|
||
|
|
apdu_buf.push(0);
|
||
|
|
apdu_buf.extend_from_slice(&(apdu.command.len() as u16).to_be_bytes());
|
||
|
|
} else if !apdu.command.is_empty() {
|
||
|
|
apdu_buf.push(apdu.command.len() as u8);
|
||
|
|
}
|
||
|
|
|
||
|
|
apdu_buf.extend_from_slice(&apdu.command);
|
||
|
|
|
||
|
|
if extended {
|
||
|
|
apdu_buf.extend_from_slice(
|
||
|
|
&(apdu.expected_length.unwrap_or_default() as u16).to_be_bytes(),
|
||
|
|
);
|
||
|
|
} else if apdu.expected_length != Some(0) {
|
||
|
|
apdu_buf.push(apdu.expected_length.unwrap_or_default() as u8);
|
||
|
|
}
|
||
|
|
|
||
|
|
let ret_len = self
|
||
|
|
.card
|
||
|
|
.transmit(&apdu_buf, &mut self.buf)
|
||
|
|
.map_err(|f| std::io::Error::new(std::io::ErrorKind::BrokenPipe, f))?
|
||
|
|
.len();
|
||
|
|
|
||
|
|
let data = self.buf[..ret_len - 2].to_vec();
|
||
|
|
let sw = (self.buf[ret_len - 2] as u16) << 8 | (self.buf[ret_len - 1] as u16);
|
||
|
|
|
||
|
|
Ok(ResultAPDU { data, status: sw })
|
||
|
|
}
|
||
|
|
async fn transmit_raw(&mut self, apdu_buf: &[u8]) -> std::io::Result<crate::ResultAPDU> {
|
||
|
|
let ret_len = self
|
||
|
|
.card
|
||
|
|
.transmit(apdu_buf, &mut self.buf)
|
||
|
|
.map_err(|f| std::io::Error::new(std::io::ErrorKind::BrokenPipe, f))?
|
||
|
|
.len();
|
||
|
|
|
||
|
|
let data = self.buf[..ret_len - 2].to_vec();
|
||
|
|
let sw = (self.buf[ret_len - 2] as u16) << 8 | (self.buf[ret_len - 1] as u16);
|
||
|
|
|
||
|
|
Ok(ResultAPDU { data, status: sw })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl PCSCCard {
|
||
|
|
pub fn new() -> Self {
|
||
|
|
let ctx = pcsc::Context::establish(pcsc::Scope::User).unwrap();
|
||
|
|
let readers = ctx.list_readers_owned().unwrap();
|
||
|
|
let reader = readers.first().unwrap();
|
||
|
|
|
||
|
|
let mut rs = [ReaderState::new(reader.to_owned(), State::empty())];
|
||
|
|
loop {
|
||
|
|
ctx.get_status_change(None, &mut rs[..]).unwrap();
|
||
|
|
rs[0].sync_current_state();
|
||
|
|
|
||
|
|
if rs[0].event_state().contains(State::PRESENT) {
|
||
|
|
let card = ctx
|
||
|
|
.connect(reader, pcsc::ShareMode::Shared, Protocols::ANY)
|
||
|
|
.unwrap();
|
||
|
|
|
||
|
|
return Self {
|
||
|
|
ctx,
|
||
|
|
reader: reader.to_owned(),
|
||
|
|
card,
|
||
|
|
buf: [0; 0x10002],
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
pub fn wait_for_remove(self) {
|
||
|
|
let mut rs = [ReaderState::new(self.reader, State::empty())];
|
||
|
|
loop {
|
||
|
|
self.ctx.get_status_change(None, &mut rs[..]).unwrap();
|
||
|
|
if rs[0].event_state().contains(State::PRESENT) {
|
||
|
|
rs[0].sync_current_state();
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|