From b8bd87bd7e7f2467fd6953459dd76ab581e100e7 Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Fri, 16 Jul 2021 14:15:03 +0200 Subject: [PATCH] Remove Arc> attempts, rely on assuan::Client now being Send+Sync --- openpgp-card-sequoia/src/lib.rs | 3 +-- openpgp-card-sequoia/src/main.rs | 7 +++---- openpgp-card-sequoia/src/signer.rs | 17 +++++------------ openpgp-card/src/lib.rs | 2 +- scdc/src/lib.rs | 21 ++++++++------------- 5 files changed, 18 insertions(+), 32 deletions(-) diff --git a/openpgp-card-sequoia/src/lib.rs b/openpgp-card-sequoia/src/lib.rs index b2b20e3..9bd73f2 100644 --- a/openpgp-card-sequoia/src/lib.rs +++ b/openpgp-card-sequoia/src/lib.rs @@ -24,7 +24,6 @@ use openpgp_card::{ errors::OpenpgpCardError, CardAdmin, CardSign, CardUploadableKey, CardUser, EccKey, EccType, KeyType, PrivateKeyMaterial, RSAKey, }; -use std::sync::{Arc, Mutex}; mod decryptor; mod signer; @@ -291,7 +290,7 @@ pub fn decrypt( } pub fn sign( - ocu: Arc>, + ocu: CardSign, cert: &sequoia_openpgp::Cert, input: &mut dyn io::Read, ) -> Result { diff --git a/openpgp-card-sequoia/src/main.rs b/openpgp-card-sequoia/src/main.rs index 97f4dac..62fdd12 100644 --- a/openpgp-card-sequoia/src/main.rs +++ b/openpgp-card-sequoia/src/main.rs @@ -8,9 +8,8 @@ use anyhow::Result; use sequoia_openpgp::parse::Parse; use sequoia_openpgp::Cert; -use openpgp_card::{CardBase, KeyType}; +use openpgp_card::KeyType; use openpgp_card_scdc::ScdClient; -use std::sync::{Arc, Mutex}; // Filename of test key and test message to use: @@ -196,14 +195,14 @@ fn main() -> Result<(), Box> { // Sign match oc.verify_pw1_for_signing("123456") { - Ok(mut oc_user) => { + Ok(oc_user) => { println!("pw1 81 verify ok"); let cert = Cert::from_file(TEST_KEY_PATH)?; let text = "Hello world, I am signed."; let res = openpgp_card_sequoia::sign( - Arc::new(Mutex::new(oc_user)), + oc_user, &cert, &mut text.as_bytes(), ); diff --git a/openpgp-card-sequoia/src/signer.rs b/openpgp-card-sequoia/src/signer.rs index 8781ccb..79c4570 100644 --- a/openpgp-card-sequoia/src/signer.rs +++ b/openpgp-card-sequoia/src/signer.rs @@ -15,11 +15,10 @@ use openpgp_card::CardSign; use openpgp_card::Hash; use crate::PublicKey; -use std::sync::{Arc, Mutex}; pub(crate) struct CardSigner { /// The OpenPGP card (authenticated to allow signing operations) - ocu: Arc>, + ocu: CardSign, /// The matching public key for the card's signing key public: PublicKey, @@ -31,12 +30,12 @@ impl CardSigner { /// An Error is returned if no match between the card's signing /// key and a (sub)key of `cert` can be made. pub fn new( - cs: Arc>, + cs: CardSign, cert: &openpgp::Cert, policy: &dyn Policy, ) -> Result { // Get the fingerprint for the signing key from the card. - let fps = cs.lock().unwrap().get_fingerprints()?; + let fps = cs.get_fingerprints()?; let fp = fps.signature(); if let Some(fp) = fp { @@ -123,10 +122,7 @@ impl<'a> crypto::Signer for CardSigner { } }; - let cs = self.ocu.clone(); - let mut cs = cs.lock().unwrap(); - - let sig = cs.signature_for_hash(hash)?; + let sig = self.ocu.signature_for_hash(hash)?; let mpi = mpi::MPI::new(&sig[..]); Ok(mpi::Signature::RSA { s: mpi }) @@ -134,10 +130,7 @@ impl<'a> crypto::Signer for CardSigner { (PublicKeyAlgorithm::EdDSA, mpi::PublicKey::EdDSA { .. }) => { let hash = Hash::EdDSA(digest); - let cs = self.ocu.clone(); - let mut cs = cs.lock().unwrap(); - - let sig = cs.signature_for_hash(hash)?; + let sig = self.ocu.signature_for_hash(hash)?; let r = mpi::MPI::new(&sig[..32]); let s = mpi::MPI::new(&sig[32..]); diff --git a/openpgp-card/src/lib.rs b/openpgp-card/src/lib.rs index 6750e9a..3224d64 100644 --- a/openpgp-card/src/lib.rs +++ b/openpgp-card/src/lib.rs @@ -30,7 +30,7 @@ pub trait CardClient { fn transmit(&mut self, cmd: &[u8], buf_size: usize) -> Result>; } -pub type CardClientBox = Box; +pub type CardClientBox = Box; /// Information about the capabilities of the card. /// (feature configuration from card metadata) diff --git a/scdc/src/lib.rs b/scdc/src/lib.rs index a8119ee..06c45c4 100644 --- a/scdc/src/lib.rs +++ b/scdc/src/lib.rs @@ -5,7 +5,7 @@ use anyhow::{anyhow, Result}; use futures::StreamExt; use lazy_static::lazy_static; use sequoia_ipc::assuan::{Client, Response}; -use std::sync::{Arc, Mutex}; +use std::sync::Mutex; use tokio::runtime::Runtime; use openpgp_card::errors::OpenpgpCardError; @@ -17,7 +17,7 @@ lazy_static! { } pub struct ScdClient { - client: Arc>, + client: Client, } impl ScdClient { @@ -47,27 +47,24 @@ impl ScdClient { pub fn new(socket: &str) -> Result { let client = RT.lock().unwrap().block_on(Client::connect(socket))?; - let client = Arc::new(Mutex::new(client)); Ok(Self { client }) } /// SERIALNO --demand=D27600012401030400050000A8350000 fn select_card(&mut self, serial: &str) -> Result<()> { - let mut client = self.client.lock().unwrap(); - let send = format!("SERIALNO --demand={}\n", serial); - client.send(send)?; + self.client.send(send)?; let mut rt = RT.lock().unwrap(); - while let Some(response) = rt.block_on(client.next()) { + while let Some(response) = rt.block_on(self.client.next()) { if let Err(_) = response { return Err(anyhow!("Card not found")); } if let Ok(Response::Status { .. }) = response { // drop remaining lines - while let Some(drop) = rt.block_on(client.next()) {} + while let Some(_drop) = rt.block_on(self.client.next()) {} return Ok(()); } @@ -81,15 +78,13 @@ 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 send = format!("APDU {}\n", hex); println!("send: '{}'", send); - client.send(send)?; + self.client.send(send)?; let mut rt = RT.lock().unwrap(); - while let Some(response) = rt.block_on(client.next()) { + while let Some(response) = rt.block_on(self.client.next()) { println!("res: {:x?}", response); if let Err(_) = response { unimplemented!(); @@ -99,7 +94,7 @@ impl CardClient for ScdClient { let res = partial; // drop remaining lines - while let Some(drop) = rt.block_on(client.next()) { + while let Some(drop) = rt.block_on(self.client.next()) { println!("drop: {:x?}", drop); }