diff --git a/crates/daemon/keyforkd/src/middleware.rs b/crates/daemon/keyforkd/src/middleware.rs index 42c5029..6cfa585 100644 --- a/crates/daemon/keyforkd/src/middleware.rs +++ b/crates/daemon/keyforkd/src/middleware.rs @@ -11,7 +11,7 @@ pub struct BincodeLayer<'a, Request> { phantom_request: PhantomData<&'a Request>, } -impl<'a, Request> BincodeLayer<'a, Request> { +impl BincodeLayer<'_, Request> { /// Create a new [`BincodeLayer`]. pub fn new() -> Self { Self { @@ -21,7 +21,7 @@ impl<'a, Request> BincodeLayer<'a, Request> { } } -impl<'a, Request> Default for BincodeLayer<'a, Request> { +impl Default for BincodeLayer<'_, Request> { fn default() -> Self { Self::new() } diff --git a/crates/keyfork-shard/src/bin/keyfork-shard-combine-openpgp.rs b/crates/keyfork-shard/src/bin/keyfork-shard-combine-openpgp.rs index 2d3eb90..fcf559c 100644 --- a/crates/keyfork-shard/src/bin/keyfork-shard-combine-openpgp.rs +++ b/crates/keyfork-shard/src/bin/keyfork-shard-combine-openpgp.rs @@ -7,7 +7,7 @@ use std::{ process::ExitCode, }; -use keyfork_prompt::{DefaultTerminal, default_terminal}; +use keyfork_prompt::default_handler; use keyfork_shard::{openpgp::OpenPGP, Format}; type Result> = std::result::Result; @@ -32,8 +32,8 @@ fn run() -> Result<()> { _ => panic!("Usage: {program_name} [key_discovery]"), }; - let openpgp = OpenPGP::::new(); - let prompt_handler = default_terminal()?; + let openpgp = OpenPGP; + let prompt_handler = default_handler()?; let bytes = openpgp.decrypt_all_shards_to_secret(key_discovery.as_deref(), messages_file, prompt_handler)?; print!("{}", smex::encode(bytes)); diff --git a/crates/keyfork-shard/src/bin/keyfork-shard-decrypt-openpgp.rs b/crates/keyfork-shard/src/bin/keyfork-shard-decrypt-openpgp.rs index 88359cd..a06f2fe 100644 --- a/crates/keyfork-shard/src/bin/keyfork-shard-decrypt-openpgp.rs +++ b/crates/keyfork-shard/src/bin/keyfork-shard-decrypt-openpgp.rs @@ -7,7 +7,7 @@ use std::{ process::ExitCode, }; -use keyfork_prompt::{DefaultTerminal, default_terminal}; +use keyfork_prompt::default_handler; use keyfork_shard::{Format, openpgp::OpenPGP}; type Result> = std::result::Result; @@ -32,8 +32,8 @@ fn run() -> Result<()> { _ => panic!("Usage: {program_name} [key_discovery]"), }; - let openpgp = OpenPGP::::new(); - let prompt_handler = default_terminal()?; + let openpgp = OpenPGP; + let prompt_handler = default_handler()?; openpgp.decrypt_one_shard_for_transport(key_discovery.as_deref(), messages_file, prompt_handler)?; diff --git a/crates/keyfork-shard/src/bin/keyfork-shard-split-openpgp.rs b/crates/keyfork-shard/src/bin/keyfork-shard-split-openpgp.rs index a097131..82765de 100644 --- a/crates/keyfork-shard/src/bin/keyfork-shard-split-openpgp.rs +++ b/crates/keyfork-shard/src/bin/keyfork-shard-split-openpgp.rs @@ -2,7 +2,6 @@ use std::{env, path::PathBuf, process::ExitCode, str::FromStr}; -use keyfork_prompt::terminal::DefaultTerminal; use keyfork_shard::{Format, openpgp::OpenPGP}; #[derive(Clone, Debug)] @@ -51,7 +50,7 @@ fn run() -> Result<()> { smex::decode(line?)? }; - let openpgp = OpenPGP::::new(); + let openpgp = OpenPGP; openpgp.shard_and_encrypt(threshold, max, &input, key_discovery.as_path(), std::io::stdout())?; Ok(()) diff --git a/crates/keyfork-shard/src/lib.rs b/crates/keyfork-shard/src/lib.rs index 5a0963d..252ac96 100644 --- a/crates/keyfork-shard/src/lib.rs +++ b/crates/keyfork-shard/src/lib.rs @@ -3,7 +3,8 @@ use std::{ io::{stdin, stdout, Read, Write}, - sync::{Arc, Mutex}, + sync::Mutex, + rc::Rc, }; use aes_gcm::{ @@ -136,7 +137,7 @@ pub trait Format { &self, private_keys: Option, encrypted_messages: &[Self::EncryptedData], - prompt: Arc>, + prompt: Rc>>, ) -> Result<(Vec, u8), Self::Error>; /// Decrypt a single share and associated metadata from a reaable input. For the current @@ -150,7 +151,7 @@ pub trait Format { &self, private_keys: Option, encrypted_data: &[Self::EncryptedData], - prompt: Arc>, + prompt: Rc>>, ) -> Result<(Share, u8), Self::Error>; /// Decrypt multiple shares and combine them to recreate a secret. @@ -162,7 +163,7 @@ pub trait Format { &self, private_key_discovery: Option>, reader: impl Read + Send + Sync, - prompt: impl PromptHandler, + prompt: Box, ) -> Result, Box> { let private_keys = private_key_discovery .map(|p| p.discover_private_keys()) @@ -171,7 +172,7 @@ pub trait Format { let (shares, threshold) = self.decrypt_all_shards( private_keys, &encrypted_messages, - Arc::new(Mutex::new(prompt)), + Rc::new(Mutex::new(prompt)), )?; let secret = Sharks(threshold) @@ -192,9 +193,9 @@ pub trait Format { &self, private_key_discovery: Option>, reader: impl Read + Send + Sync, - prompt: impl PromptHandler, + prompt: Box, ) -> Result<(), Box> { - let prompt = Arc::new(Mutex::new(prompt)); + let prompt = Rc::new(Mutex::new(prompt)); // parse input let private_keys = private_key_discovery @@ -236,7 +237,7 @@ pub trait Format { }; let mut prompt = prompt.lock().expect(bug!(POISONED_MUTEX)); prompt_validated_wordlist::( - &mut *prompt, + &mut **prompt, QRCODE_COULDNT_READ, 3, &*validator.to_fn(), diff --git a/crates/keyfork-shard/src/openpgp.rs b/crates/keyfork-shard/src/openpgp.rs index a8f6e75..024334f 100644 --- a/crates/keyfork-shard/src/openpgp.rs +++ b/crates/keyfork-shard/src/openpgp.rs @@ -5,10 +5,10 @@ use std::{ collections::HashMap, io::{Read, Write}, - marker::PhantomData, path::Path, str::FromStr, - sync::{Arc, Mutex}, + sync::Mutex, + rc::Rc, }; use keyfork_bug::bug; @@ -186,18 +186,9 @@ impl EncryptedMessage { } /// Encoding and decoding shards using OpenPGP. -pub struct OpenPGP { - p: PhantomData

, -} +pub struct OpenPGP; -impl OpenPGP

{ - #[allow(clippy::new_without_default, missing_docs)] - pub fn new() -> Self { - Self { p: PhantomData } - } -} - -impl OpenPGP

{ +impl OpenPGP { /// Read all OpenPGP certificates in a path and return a [`Vec`] of them. /// /// Certificates are read from a file, or from files one level deep in a directory. @@ -256,7 +247,7 @@ impl OpenPGP

{ const METADATA_MESSAGE_MISSING: &str = "Metadata message was not found in parsed packets"; -impl Format for OpenPGP

{ +impl Format for OpenPGP { type Error = Error; type PublicKey = Cert; type PrivateKeyData = Vec; @@ -453,7 +444,7 @@ impl Format for OpenPGP

{ &self, private_keys: Option, encrypted_data: &[Self::EncryptedData], - prompt: Arc>, + prompt: Rc>>, ) -> std::result::Result<(Vec, u8), Self::Error> { // Be as liberal as possible when decrypting. // We don't want to invalidate someone's keys just because the old sig expired. @@ -514,7 +505,7 @@ impl Format for OpenPGP

{ &self, private_keys: Option, encrypted_data: &[Self::EncryptedData], - prompt: Arc>, + prompt: Rc>>, ) -> std::result::Result<(Share, u8), Self::Error> { let policy = NullPolicy::new(); @@ -560,22 +551,22 @@ impl Format for OpenPGP

{ } } -impl KeyDiscovery> for &Path { - fn discover_public_keys(&self) -> Result as Format>::PublicKey>> { - OpenPGP::

::discover_certs(self) +impl KeyDiscovery for &Path { + fn discover_public_keys(&self) -> Result::PublicKey>> { + OpenPGP::discover_certs(self) } - fn discover_private_keys(&self) -> Result< as Format>::PrivateKeyData> { - OpenPGP::

::discover_certs(self) + fn discover_private_keys(&self) -> Result<::PrivateKeyData> { + OpenPGP::discover_certs(self) } } -impl KeyDiscovery> for &[Cert] { - fn discover_public_keys(&self) -> Result as Format>::PublicKey>> { +impl KeyDiscovery for &[Cert] { + fn discover_public_keys(&self) -> Result::PublicKey>> { Ok(self.to_vec()) } - fn discover_private_keys(&self) -> Result< as Format>::PrivateKeyData> { + fn discover_private_keys(&self) -> Result<::PrivateKeyData> { Ok(self.to_vec()) } } @@ -637,12 +628,12 @@ fn decode_metadata_v1(buf: &[u8]) -> Result<(u8, Cert, Vec)> { // NOTE: When using single-decryptor mechanism, use this method with `threshold = 1` to return a // single message. -fn decrypt_with_manager( +fn decrypt_with_manager( threshold: u8, messages: &mut HashMap, certs: &[Cert], policy: &dyn Policy, - manager: &mut SmartcardManager

, + manager: &mut SmartcardManager, ) -> Result>> { let mut decrypted_messages = HashMap::new(); @@ -687,11 +678,11 @@ fn decrypt_with_manager( // NOTE: When using single-decryptor mechanism, only a single key should be provided in Keyring to // decrypt messages with. -fn decrypt_with_keyring( +fn decrypt_with_keyring( messages: &mut HashMap, certs: &[Cert], policy: &NullPolicy, - keyring: &mut Keyring

, + keyring: &mut Keyring, ) -> Result>, Error> { let mut decrypted_messages = HashMap::new(); @@ -721,11 +712,11 @@ fn decrypt_with_keyring( Ok(decrypted_messages) } -fn decrypt_metadata( +fn decrypt_metadata( message: &EncryptedMessage, policy: &NullPolicy, - keyring: &mut Keyring

, - manager: &mut SmartcardManager

, + keyring: &mut Keyring, + manager: &mut SmartcardManager, ) -> Result> { Ok(if keyring.is_empty() { manager.load_any_card()?; diff --git a/crates/keyfork-shard/src/openpgp/keyring.rs b/crates/keyfork-shard/src/openpgp/keyring.rs index 79263de..7657eb9 100644 --- a/crates/keyfork-shard/src/openpgp/keyring.rs +++ b/crates/keyfork-shard/src/openpgp/keyring.rs @@ -1,6 +1,6 @@ #![allow(clippy::expect_fun_call)] -use std::sync::{Arc, Mutex}; +use std::{rc::Rc, sync::Mutex}; use keyfork_bug::{bug, POISONED_MUTEX}; use keyfork_prompt::{Error as PromptError, PromptHandler}; @@ -27,14 +27,14 @@ pub enum Error { pub type Result = std::result::Result; -pub struct Keyring { +pub struct Keyring { full_certs: Vec, root: Option, - pm: Arc>, + pm: Rc>>, } -impl Keyring

{ - pub fn new(certs: impl AsRef<[Cert]>, p: Arc>) -> Result { +impl Keyring { + pub fn new(certs: impl AsRef<[Cert]>, p: Rc>>) -> Result { Ok(Self { full_certs: certs.as_ref().to_vec(), root: Default::default(), @@ -62,7 +62,7 @@ impl Keyring

{ } } -impl VerificationHelper for &mut Keyring

{ +impl VerificationHelper for &mut Keyring { fn get_certs(&mut self, ids: &[KeyHandle]) -> openpgp::Result> { Ok(ids .iter() @@ -108,7 +108,7 @@ impl VerificationHelper for &mut Keyring

{ } } -impl DecryptionHelper for &mut Keyring

{ +impl DecryptionHelper for &mut Keyring { fn decrypt( &mut self, pkesks: &[PKESK], diff --git a/crates/keyfork-shard/src/openpgp/smartcard.rs b/crates/keyfork-shard/src/openpgp/smartcard.rs index 48b4e32..bf6c835 100644 --- a/crates/keyfork-shard/src/openpgp/smartcard.rs +++ b/crates/keyfork-shard/src/openpgp/smartcard.rs @@ -2,7 +2,8 @@ use std::{ collections::{HashMap, HashSet}, - sync::{Arc, Mutex}, + rc::Rc, + sync::Mutex, }; use keyfork_bug::{bug, POISONED_MUTEX}; @@ -72,15 +73,15 @@ fn format_name(input: impl AsRef) -> String { } #[allow(clippy::module_name_repetitions)] -pub struct SmartcardManager { +pub struct SmartcardManager { current_card: Option>, root: Option, - pm: Arc>, + pm: Rc>>, pin_cache: HashMap, } -impl SmartcardManager

{ - pub fn new(p: Arc>) -> Result { +impl SmartcardManager { + pub fn new(p: Rc>>) -> Result { Ok(Self { current_card: None, root: None, @@ -174,7 +175,7 @@ impl SmartcardManager

{ } } -impl VerificationHelper for &mut SmartcardManager

{ +impl VerificationHelper for &mut SmartcardManager { fn get_certs(&mut self, ids: &[openpgp::KeyHandle]) -> openpgp::Result> { #[allow(clippy::flat_map_option)] Ok(ids @@ -218,7 +219,7 @@ impl VerificationHelper for &mut SmartcardManager

{ } } -impl DecryptionHelper for &mut SmartcardManager

{ +impl DecryptionHelper for &mut SmartcardManager { fn decrypt( &mut self, pkesks: &[PKESK], @@ -277,7 +278,7 @@ impl DecryptionHelper for &mut SmartcardManager

{ format!("Unlock card {card_id} ({cardholder_name})\n{rpea}: {attempts}\n\nPIN: ") }; let mut prompt = self.pm.lock().expect(bug!(POISONED_MUTEX)); - let temp_pin = prompt_validated_passphrase(&mut *prompt, &message, 3, &pin_validator)?; + let temp_pin = prompt_validated_passphrase(&mut **prompt, &message, 3, &pin_validator)?; let verification_status = transaction.verify_user_pin(temp_pin.as_str().trim()); match verification_status { #[allow(clippy::ignored_unit_patterns)] diff --git a/crates/keyfork/src/cli/recover.rs b/crates/keyfork/src/cli/recover.rs index fa6a604..48bc3ec 100644 --- a/crates/keyfork/src/cli/recover.rs +++ b/crates/keyfork/src/cli/recover.rs @@ -4,12 +4,11 @@ use std::path::PathBuf; use keyfork_mnemonic::{English, Mnemonic}; use keyfork_prompt::{ - default_terminal, prompt_validated_wordlist, + default_handler, prompt_validated_wordlist, validators::{ mnemonic::{MnemonicChoiceValidator, WordLength}, Validator, }, - DefaultTerminal, }; use keyfork_shard::{remote_decrypt, Format}; @@ -42,8 +41,8 @@ impl RecoverSubcommands { } => { let content = std::fs::read_to_string(shard_file)?; if content.contains("BEGIN PGP MESSAGE") { - let openpgp = keyfork_shard::openpgp::OpenPGP::::new(); - let prompt_handler = default_terminal()?; + let openpgp = keyfork_shard::openpgp::OpenPGP; + let prompt_handler = default_handler()?; // TODO: remove .clone() by making handle() consume self let seed = openpgp.decrypt_all_shards_to_secret( key_discovery.as_deref(), @@ -61,12 +60,12 @@ impl RecoverSubcommands { Ok(seed) } RecoverSubcommands::Mnemonic {} => { - let mut term = default_terminal()?; + let mut prompt_handler = default_handler()?; let validator = MnemonicChoiceValidator { word_lengths: [WordLength::Count(12), WordLength::Count(24)], }; let mnemonic = prompt_validated_wordlist::( - &mut term, + &mut *prompt_handler, "Mnemonic: ", 3, &*validator.to_fn(), diff --git a/crates/keyfork/src/cli/shard.rs b/crates/keyfork/src/cli/shard.rs index 5f9e2b4..3e3b045 100644 --- a/crates/keyfork/src/cli/shard.rs +++ b/crates/keyfork/src/cli/shard.rs @@ -1,6 +1,6 @@ use super::Keyfork; use clap::{builder::PossibleValue, Parser, Subcommand, ValueEnum}; -use keyfork_prompt::{default_terminal, DefaultTerminal}; +use keyfork_prompt::default_handler; use keyfork_shard::Format as _; use std::{ io::{stdin, stdout, Read, Write}, @@ -64,7 +64,7 @@ impl ShardExec for OpenPGP { secret: &[u8], output: &mut (impl Write + Send + Sync), ) -> Result<(), Box> { - let opgp = keyfork_shard::openpgp::OpenPGP::::new(); + let opgp = keyfork_shard::openpgp::OpenPGP; opgp.shard_and_encrypt(threshold, max, secret, key_discovery, output) } @@ -74,8 +74,8 @@ impl ShardExec for OpenPGP { input: impl Read + Send + Sync, output: &mut impl Write, ) -> Result<(), Box> { - let openpgp = keyfork_shard::openpgp::OpenPGP::::new(); - let prompt = default_terminal()?; + let openpgp = keyfork_shard::openpgp::OpenPGP; + let prompt = default_handler()?; let bytes = openpgp.decrypt_all_shards_to_secret(key_discovery, input, prompt)?; write!(output, "{}", smex::encode(bytes))?; @@ -87,8 +87,8 @@ impl ShardExec for OpenPGP { key_discovery: Option<&Path>, input: impl Read + Send + Sync, ) -> Result<(), Box> { - let openpgp = keyfork_shard::openpgp::OpenPGP::::new(); - let prompt = default_terminal()?; + let openpgp = keyfork_shard::openpgp::OpenPGP; + let prompt = default_handler()?; openpgp.decrypt_one_shard_for_transport(key_discovery, input, prompt)?; Ok(()) } diff --git a/crates/keyfork/src/cli/wizard.rs b/crates/keyfork/src/cli/wizard.rs index a15a179..d2b7be3 100644 --- a/crates/keyfork/src/cli/wizard.rs +++ b/crates/keyfork/src/cli/wizard.rs @@ -20,9 +20,10 @@ use keyfork_derive_path_data::paths; use keyfork_derive_util::DerivationIndex; use keyfork_mnemonic::Mnemonic; use keyfork_prompt::{ - default_terminal, prompt_validated_passphrase, + default_handler, + prompt_validated_passphrase, validators::{SecurePinValidator, Validator}, - DefaultTerminal, Message, PromptHandler, + Message, }; use keyfork_shard::{openpgp::OpenPGP, Format}; @@ -174,7 +175,7 @@ impl WizardSubcommands { impl GenerateShardSecret { fn handle(&self) -> Result<()> { let seed = keyfork_entropy::generate_entropy_of_const_size::<{ 256 / 8 }>()?; - let mut pm = default_terminal()?; + let mut pm = default_handler()?; let mut certs = vec![]; let mut seen_cards: HashSet = HashSet::new(); let stdout = std::io::stdout(); @@ -214,13 +215,13 @@ impl GenerateShardSecret { ))?; }; let user_pin = prompt_validated_passphrase( - &mut pm, + &mut *pm, "Please enter the new smartcard User PIN: ", 3, &user_pin_validator, )?; let admin_pin = prompt_validated_passphrase( - &mut pm, + &mut *pm, "Please enter the new smartcard Admin PIN: ", 3, &admin_pin_validator, @@ -236,7 +237,7 @@ impl GenerateShardSecret { certs.push(cert); } - let opgp = OpenPGP::::new(); + let opgp = OpenPGP; if let Some(output_file) = self.output.as_ref() { let output = File::create(output_file)?; @@ -284,8 +285,8 @@ impl BottomsUp { cert.serialize(&mut w)?; w.finalize()?; - let opgp = OpenPGP::::new(); - let certs = OpenPGP::::discover_certs(&self.key_discovery)?; + let opgp = OpenPGP; + let certs = OpenPGP::discover_certs(&self.key_discovery)?; let shardfile = File::create(&self.output_shardfile)?; opgp.shard_and_encrypt(