diff --git a/src/commands/mod.rs b/src/commands/mod.rs index bdffef1c..7175734c 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -56,6 +56,7 @@ pub mod export; pub mod net; pub mod certify; pub mod link; +pub mod verify; pub mod wot; #[derive(Debug, Clone, PartialEq, Eq)] @@ -750,29 +751,6 @@ impl<'a, 'store> VerificationHelper for VHelper<'a, 'store> { } } -pub fn verify(config: Config, - input: &mut (dyn io::Read + Sync + Send), - detached: Option<&mut (dyn io::Read + Sync + Send)>, - output: &mut dyn io::Write, - signatures: usize, certs: Vec) - -> Result<()> { - let helper = VHelper::new(&config, signatures, certs); - let helper = if let Some(dsig) = detached { - let mut v = DetachedVerifierBuilder::from_reader(dsig)? - .with_policy(&config.policy, Some(config.time), helper)?; - v.verify_reader(input)?; - v.into_helper() - } else { - let mut v = VerifierBuilder::from_reader(input)? - .with_policy(&config.policy, Some(config.time), helper)?; - io::copy(&mut v, output)?; - v.into_helper() - }; - - helper.print_status(); - Ok(()) -} - pub fn split(input: &mut (dyn io::Read + Sync + Send), prefix: &str) -> Result<()> { // We (ab)use the mapping feature to create byte-accurate dumps of diff --git a/src/commands/verify.rs b/src/commands/verify.rs new file mode 100644 index 00000000..5ff3f5c7 --- /dev/null +++ b/src/commands/verify.rs @@ -0,0 +1,69 @@ +use std::io; +use std::fs::File; + +use anyhow::Context; + +use sequoia_openpgp as openpgp; +use openpgp::Cert; +use openpgp::types::KeyFlags; +use openpgp::parse::stream::DetachedVerifierBuilder; +use openpgp::parse::stream::VerifierBuilder; +use openpgp::parse::Parse; + +use crate::Config; +use crate::Result; +use crate::commands::VHelper; +use crate::load_certs; +use crate::sq_cli; + +pub fn dispatch(config: Config, command: sq_cli::verify::Command) + -> Result<()> +{ + tracer!(TRACE, "verify::dispatch"); + + let mut input = command.input.open()?; + let mut output = command.output.create_safe(config.force)?; + let mut detached = if let Some(f) = command.detached { + Some(File::open(f)?) + } else { + None + }; + let signatures = command.signatures; + // TODO ugly adaptation to load_certs' signature, fix later + let mut certs = load_certs( + command.sender_file.iter().map(|s| s.as_ref()))?; + certs.extend( + config.lookup(command.sender_certs, + Some(KeyFlags::empty().set_signing()), + true, + false) + .context("--sender-cert")?); + verify(config, &mut input, + detached.as_mut().map(|r| r as &mut (dyn io::Read + Sync + Send)), + &mut output, signatures, certs)?; + + Ok(()) +} + +pub fn verify(config: Config, + input: &mut (dyn io::Read + Sync + Send), + detached: Option<&mut (dyn io::Read + Sync + Send)>, + output: &mut dyn io::Write, + signatures: usize, certs: Vec) + -> Result<()> { + let helper = VHelper::new(&config, signatures, certs); + let helper = if let Some(dsig) = detached { + let mut v = DetachedVerifierBuilder::from_reader(dsig)? + .with_policy(&config.policy, Some(config.time), helper)?; + v.verify_reader(input)?; + v.into_helper() + } else { + let mut v = VerifierBuilder::from_reader(input)? + .with_policy(&config.policy, Some(config.time), helper)?; + io::copy(&mut v, output)?; + v.into_helper() + }; + + helper.print_status(); + Ok(()) +} diff --git a/src/sq.rs b/src/sq.rs index 2b44a0b6..79bb3fb2 100644 --- a/src/sq.rs +++ b/src/sq.rs @@ -21,7 +21,7 @@ use once_cell::unsync::OnceCell; use terminal_size::terminal_size; -use buffered_reader::{BufferedReader, Dup, File, Limitor}; +use buffered_reader::{BufferedReader, Dup, Limitor}; use sequoia_openpgp as openpgp; use openpgp::{ @@ -1053,28 +1053,8 @@ fn main() -> Result<()> { SqSubcommands::Sign(command) => { commands::sign::dispatch(config, command)? }, - SqSubcommands::Verify(command) => { - let mut input = command.input.open()?; - let mut output = command.output.create_safe(config.force)?; - let mut detached = if let Some(f) = command.detached { - Some(File::open(f)?) - } else { - None - }; - let signatures = command.signatures; - // TODO ugly adaptation to load_certs' signature, fix later - let mut certs = load_certs( - command.sender_file.iter().map(|s| s.as_ref()))?; - certs.extend( - config.lookup(command.sender_certs, - Some(KeyFlags::empty().set_signing()), - true, - false) - .context("--sender-cert")?); - commands::verify(config, &mut input, - detached.as_mut().map(|r| r as &mut (dyn io::Read + Sync + Send)), - &mut output, signatures, certs)?; + commands::verify::dispatch(config, command)? }, // TODO: Extract body to commands/armor.rs diff --git a/src/sq_cli/mod.rs b/src/sq_cli/mod.rs index b9a5d20e..bbabb5bc 100644 --- a/src/sq_cli/mod.rs +++ b/src/sq_cli/mod.rs @@ -26,7 +26,7 @@ pub mod link; mod output_versions; pub mod packet; pub mod sign; -mod verify; +pub mod verify; pub mod wkd; pub mod wot;