Make OpenPgp "Send + Sync"

This commit is contained in:
Heiko Schaefer 2022-02-28 11:06:00 +01:00
parent 326ba895a9
commit 421e4d155f
No known key found for this signature in database
GPG key ID: 4A849A1904CCBD7D
4 changed files with 39 additions and 21 deletions

View file

@ -41,7 +41,7 @@ pub struct TestCardData {
} }
impl TestCardData { impl TestCardData {
pub(crate) fn get_card(&self) -> Result<Box<dyn CardBackend>> { pub(crate) fn get_card(&self) -> Result<Box<dyn CardBackend + Send + Sync>> {
self.tc.open() self.tc.open()
} }
@ -92,7 +92,7 @@ pub enum TestCard {
} }
impl TestCard { impl TestCard {
pub fn open(&self) -> Result<Box<dyn CardBackend>> { pub fn open(&self) -> Result<Box<dyn CardBackend + Send + Sync>> {
match self { match self {
Self::Pcsc(ident) => { Self::Pcsc(ident) => {
// Attempt to shutdown SCD, if it is running. // Attempt to shutdown SCD, if it is running.
@ -103,7 +103,7 @@ impl TestCard {
// Make three attempts to open the card before failing // Make three attempts to open the card before failing
// (this can be useful in ShareMode::Exclusive) // (this can be useful in ShareMode::Exclusive)
let mut i = 1; let mut i = 1;
let card: Result<Box<dyn CardBackend>, Error> = loop { let card: Result<Box<dyn CardBackend + Send + Sync>, Error> = loop {
let res = PcscBackend::open_by_ident(ident, SHARE_MODE); let res = PcscBackend::open_by_ident(ident, SHARE_MODE);
if i == 3 { if i == 3 {

View file

@ -49,7 +49,10 @@ pub enum TestError {
} }
/// Run after each "upload keys", if key *was* uploaded (?) /// Run after each "upload keys", if key *was* uploaded (?)
pub fn test_decrypt(card: &mut dyn CardBackend, param: &[&str]) -> Result<TestOutput, TestError> { pub fn test_decrypt(
card: &mut (dyn CardBackend + Send + Sync),
param: &[&str],
) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
let mut pgpt = pgp.transaction()?; let mut pgpt = pgp.transaction()?;
@ -75,7 +78,10 @@ pub fn test_decrypt(card: &mut dyn CardBackend, param: &[&str]) -> Result<TestOu
} }
/// Run after each "upload keys", if key *was* uploaded (?) /// Run after each "upload keys", if key *was* uploaded (?)
pub fn test_sign(card: &mut dyn CardBackend, param: &[&str]) -> Result<TestOutput, TestError> { pub fn test_sign(
card: &mut (dyn CardBackend + Send + Sync),
param: &[&str],
) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
let mut pgpt = pgp.transaction()?; let mut pgpt = pgp.transaction()?;
@ -139,7 +145,7 @@ fn check_key_upload_algo_attrs() -> Result<()> {
} }
pub fn test_print_caps( pub fn test_print_caps(
card: &mut dyn CardBackend, card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str], _param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
@ -163,7 +169,7 @@ pub fn test_print_caps(
} }
pub fn test_print_algo_info( pub fn test_print_algo_info(
card: &mut dyn CardBackend, card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str], _param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
@ -185,7 +191,7 @@ pub fn test_print_algo_info(
} }
pub fn test_upload_keys( pub fn test_upload_keys(
card: &mut dyn CardBackend, card: &mut (dyn CardBackend + Send + Sync),
param: &[&str], param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
@ -215,7 +221,10 @@ pub fn test_upload_keys(
} }
/// Generate keys for each of the three KeyTypes /// Generate keys for each of the three KeyTypes
pub fn test_keygen(card: &mut dyn CardBackend, param: &[&str]) -> Result<TestOutput, TestError> { pub fn test_keygen(
card: &mut (dyn CardBackend + Send + Sync),
param: &[&str],
) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
let mut pgpt = pgp.transaction()?; let mut pgpt = pgp.transaction()?;
@ -257,7 +266,10 @@ pub fn test_keygen(card: &mut dyn CardBackend, param: &[&str]) -> Result<TestOut
} }
/// Construct public key based on data from the card /// Construct public key based on data from the card
pub fn test_get_pub(card: &mut dyn CardBackend, _param: &[&str]) -> Result<TestOutput, TestError> { pub fn test_get_pub(
card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str],
) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
let mut pgpt = pgp.transaction()?; let mut pgpt = pgp.transaction()?;
@ -296,7 +308,10 @@ pub fn test_get_pub(card: &mut dyn CardBackend, _param: &[&str]) -> Result<TestO
Ok(vec![]) Ok(vec![])
} }
pub fn test_reset(card: &mut dyn CardBackend, _param: &[&str]) -> Result<TestOutput, TestError> { pub fn test_reset(
card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str],
) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
let mut pgpt = pgp.transaction()?; let mut pgpt = pgp.transaction()?;
@ -310,7 +325,7 @@ pub fn test_reset(card: &mut dyn CardBackend, _param: &[&str]) -> Result<TestOut
/// Returns an empty TestOutput, throws errors for unexpected Status codes /// Returns an empty TestOutput, throws errors for unexpected Status codes
/// and for unequal field values. /// and for unequal field values.
pub fn test_set_user_data( pub fn test_set_user_data(
card: &mut dyn CardBackend, card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str], _param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
@ -347,7 +362,7 @@ pub fn test_set_user_data(
} }
pub fn test_private_data( pub fn test_private_data(
card: &mut dyn CardBackend, card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str], _param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
@ -443,7 +458,7 @@ pub fn test_private_data(
// } // }
pub fn test_pw_status( pub fn test_pw_status(
card: &mut dyn CardBackend, card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str], _param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
@ -473,7 +488,10 @@ pub fn test_pw_status(
/// Outputs: /// Outputs:
/// - verify pw3 (check) -> Status /// - verify pw3 (check) -> Status
/// - verify pw1 (check) -> Status /// - verify pw1 (check) -> Status
pub fn test_verify(card: &mut dyn CardBackend, _param: &[&str]) -> Result<TestOutput, TestError> { pub fn test_verify(
card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str],
) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
let mut pgpt = pgp.transaction()?; let mut pgpt = pgp.transaction()?;
@ -540,7 +558,7 @@ pub fn test_verify(card: &mut dyn CardBackend, _param: &[&str]) -> Result<TestOu
} }
pub fn test_change_pw( pub fn test_change_pw(
card: &mut dyn CardBackend, card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str], _param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
@ -601,7 +619,7 @@ pub fn test_change_pw(
} }
pub fn test_reset_retry_counter( pub fn test_reset_retry_counter(
card: &mut dyn CardBackend, card: &mut (dyn CardBackend + Send + Sync),
_param: &[&str], _param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut pgp = OpenPgp::new(card); let mut pgp = OpenPgp::new(card);
@ -666,7 +684,7 @@ pub fn test_reset_retry_counter(
pub fn run_test( pub fn run_test(
tc: &mut TestCardData, tc: &mut TestCardData,
t: fn(&mut (dyn CardBackend), &[&str]) -> Result<TestOutput, TestError>, t: fn(&mut (dyn CardBackend + Send + Sync), &[&str]) -> Result<TestOutput, TestError>,
param: &[&str], param: &[&str],
) -> Result<TestOutput, TestError> { ) -> Result<TestOutput, TestError> {
let mut card = tc.get_card()?; let mut card = tc.get_card()?;

View file

@ -23,11 +23,11 @@ use crate::{
/// Users of this crate can keep a long lived OpenPgp object. All operations must be performed on /// Users of this crate can keep a long lived OpenPgp object. All operations must be performed on
/// a short lived `OpenPgpTransaction`. /// a short lived `OpenPgpTransaction`.
pub struct OpenPgp<'a> { pub struct OpenPgp<'a> {
card: &'a mut dyn CardBackend, card: &'a mut (dyn CardBackend + Send + Sync),
} }
impl<'a> OpenPgp<'a> { impl<'a> OpenPgp<'a> {
pub fn new(card: &'a mut dyn CardBackend) -> Self { pub fn new(card: &'a mut (dyn CardBackend + Send + Sync)) -> Self {
Self { card } Self { card }
} }

View file

@ -154,7 +154,7 @@ fn set_identity(ident: &str, id: u8) -> Result<(), Box<dyn std::error::Error>> {
} }
fn print_status(ident: Option<String>, verbose: bool) -> Result<()> { fn print_status(ident: Option<String>, verbose: bool) -> Result<()> {
let mut card: Box<dyn CardBackend> = if let Some(ident) = ident { let mut card: Box<dyn CardBackend + Send + Sync> = if let Some(ident) = ident {
Box::new(util::open_card(&ident)?) Box::new(util::open_card(&ident)?)
} else { } else {
let mut cards = util::cards()?; let mut cards = util::cards()?;