Move the verify subcommand's dispatcher and impl to its own module

This commit is contained in:
Neal H. Walfield 2023-10-17 11:56:38 +02:00
parent d0b583f9c0
commit 602266845e
No known key found for this signature in database
GPG Key ID: 6863C9AD5B4D22D3
4 changed files with 73 additions and 46 deletions

View File

@ -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<Cert>)
-> 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

69
src/commands/verify.rs Normal file
View File

@ -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<Cert>)
-> 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(())
}

View File

@ -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

View File

@ -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;