From 1187e816d0322c9b54da832337f2df1907ef5c0c Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Sat, 10 Jul 2021 22:09:11 +0200 Subject: [PATCH] Cleanup --- openpgp-card-sequoia/Cargo.toml | 1 - openpgp-card-sequoia/src/main.rs | 1 - openpgp-card/src/key_upload.rs | 2 - scdc/src/lib.rs | 66 ++++++++++++-------------------- 4 files changed, 25 insertions(+), 45 deletions(-) diff --git a/openpgp-card-sequoia/Cargo.toml b/openpgp-card-sequoia/Cargo.toml index 118e899..c746ff3 100644 --- a/openpgp-card-sequoia/Cargo.toml +++ b/openpgp-card-sequoia/Cargo.toml @@ -15,7 +15,6 @@ documentation = "https://docs.rs/crate/openpgp-card-sequoia" sequoia-openpgp = "1.3" openpgp-card = { path = "../openpgp-card", version = "0.0.1" } openpgp-card-scdc = { path = "../scdc" } -tokio = "0.2" chrono = "0.4" anyhow = "1" thiserror = "1" diff --git a/openpgp-card-sequoia/src/main.rs b/openpgp-card-sequoia/src/main.rs index 0796aa0..899ee27 100644 --- a/openpgp-card-sequoia/src/main.rs +++ b/openpgp-card-sequoia/src/main.rs @@ -24,7 +24,6 @@ const TEST_ENC_MSG: &str = "example/encrypted_to_rsa4k.asc"; const SOCKET: &str = "/run/user/1000/gnupg/S.scdaemon"; -// #[tokio::main] fn main() -> Result<(), Box> { env_logger::init(); diff --git a/openpgp-card/src/key_upload.rs b/openpgp-card/src/key_upload.rs index c07fb0d..0bd733f 100644 --- a/openpgp-card/src/key_upload.rs +++ b/openpgp-card/src/key_upload.rs @@ -26,8 +26,6 @@ pub(crate) fn upload_key( key_type: KeyType, algo_list: Option, ) -> Result<(), OpenpgpCardError> { - println!("upload key"); - let (algo_cmd, key_cmd) = match key.get_key()? { PrivateKeyMaterial::R(rsa_key) => { // RSA bitsize diff --git a/scdc/src/lib.rs b/scdc/src/lib.rs index 9dd9bc9..fb15cc6 100644 --- a/scdc/src/lib.rs +++ b/scdc/src/lib.rs @@ -1,17 +1,17 @@ use anyhow::{anyhow, Result}; use futures::StreamExt; +use lazy_static::lazy_static; use sequoia_ipc::assuan::{Client, Response}; use std::sync::{Arc, Mutex}; use tokio::runtime::Runtime; -use lazy_static::lazy_static; use openpgp_card::card_app::CardApp; use openpgp_card::errors::OpenpgpCardError; use openpgp_card::{CardBase, CardCaps, CardClient}; lazy_static! { pub(crate) static ref RT: Mutex = - { Mutex::new(tokio::runtime::Runtime::new().unwrap()) }; + Mutex::new(tokio::runtime::Runtime::new().unwrap()); } pub struct ScdClient { @@ -22,20 +22,14 @@ impl ScdClient { /// Create a CardBase object that uses an scdaemon instance as its /// backend. pub fn open_scdc(socket: &str) -> Result { - println!("open_scdc"); - let card_client = ScdClient::new(socket)?; - - let ccb = Box::new(card_client) as Box; - - println!("get ard"); + let card_client_box = + Box::new(card_client) as Box; // read and cache "application related data" - let mut card_app = CardApp::new(ccb); + let mut card_app = CardApp::new(card_client_box); let ard = card_app.get_app_data()?; - println!("got ard"); - // Determine chaining/extended length support from card // metadata and cache this information in CardApp (as a // CardCaps) @@ -59,6 +53,7 @@ impl ScdClient { }; let caps = CardCaps::new(ext_support, chaining_support, max_cmd_bytes); + let card_app = card_app.set_caps(caps); Ok(CardBase::new(card_app, ard)) @@ -69,47 +64,36 @@ impl ScdClient { let client = Arc::new(Mutex::new(client)); Ok(Self { client }) } +} - async fn transmit_async(&mut self, cmd: &[u8]) -> Result> { +impl CardClient for ScdClient { + fn transmit(&mut self, cmd: &[u8], _: usize) -> Result> { let hex = hex::encode(cmd); let mut client = self.client.lock().unwrap(); - let mut res = None; + let send = format!("APDU {}\n", hex); + println!("send: '{}'", send); + client.send(send)?; - { - let send = format!("APDU {}\n", hex); - println!("send: '{}'", send); - client.send(send)?; + let mut rt = RT.lock().unwrap(); - while let Some(response) = client.next().await { - println!("res: {:?}", response); - if let Ok(Response::Data { partial }) = response { - res = Some(partial); + while let Some(response) = rt.block_on(client.next()) { + println!("res: {:x?}", response); + if let Ok(Response::Data { partial }) = response { + let res = partial; - // drop remaining lines - while let Some(_) = client.next().await {} - - break; + // drop remaining lines + while let Some(drop) = rt.block_on(client.next()) { + println!("drop: {:x?}", drop); } + + println!(); + + return Ok(res); } } - match res { - Some(s) => Ok(s), - None => Err(anyhow!("no response found")), - } - } -} - -impl CardClient for ScdClient { - fn transmit(&mut self, cmd: &[u8], _buf_size: usize) -> Result> { - let mut res = None; - - { - res = Some(RT.lock().unwrap().block_on(self.transmit_async(cmd))); - } - - res.unwrap() + Err(anyhow!("no response found")) } }