Use cert designators for sq toolbox packet dump.

- See #207.
This commit is contained in:
Justus Winter 2024-10-24 13:09:16 +02:00
parent ddf6436ebd
commit db0d193527
No known key found for this signature in database
GPG Key ID: 686F55B4AB2B3386
2 changed files with 52 additions and 1 deletions

View File

@ -13,6 +13,7 @@ use crate::cli::types::ClapData;
use crate::cli::types::FileOrStdin;
use crate::cli::types::FileOrStdout;
use crate::cli::types::SessionKey;
use crate::cli::types::cert_designator::*;
#[derive(Parser, Debug)]
#[clap(
@ -60,12 +61,20 @@ To inspect encrypted messages, either supply the session key, or see \
after_help = DUMP_EXAMPLES,
)]
pub struct DumpCommand {
#[command(flatten)]
pub cert: CertDesignators<CertUserIDEmailFileArgs,
CertPrefix,
OneOptionalValue,
PacketDumpDoc>,
#[clap(
default_value_t = FileOrStdin::default(),
help = FileOrStdin::HELP_OPTIONAL,
value_name = FileOrStdin::VALUE_NAME,
conflicts_with_all = ["cert", "cert-userid", "cert-email", "cert-file"],
)]
pub input: FileOrStdin,
#[clap(
default_value_t = FileOrStdout::default(),
help = FileOrStdout::HELP_OPTIONAL,
@ -142,6 +151,26 @@ secret key file.",
};
test_examples!(sq_toolbox_packet_dump, DUMP_EXAMPLES);
/// Documentation for the cert designators for the toolbox packet dump
/// command.
pub struct PacketDumpDoc {}
impl AdditionalDocs for PacketDumpDoc {
fn help(arg: &'static str, help: &'static str) -> clap::builder::StyledStr {
match arg {
"file" =>
"Dump the packets of the cert read from PATH"
.into(),
_ => {
debug_assert!(help.starts_with("Use certificates"));
help.replace("Use certificates",
"Dump the packets of the certificate")
.into()
},
}
}
}
#[derive(Debug, Args)]
#[clap(
about = "Unwrap an encryption container",

View File

@ -8,6 +8,7 @@ use terminal_size::terminal_size;
use sequoia_openpgp as openpgp;
use openpgp::{
KeyHandle,
armor::{
Kind,
ReaderMode,
@ -20,6 +21,7 @@ use openpgp::{
PacketParserBuilder,
PacketParserResult,
},
serialize::SerializeInto,
};
use openpgp::serialize::stream::Message;
@ -44,7 +46,27 @@ pub fn dispatch(sq: Sq, command: Command)
tracer!(TRACE, "packet::dispatch");
match command.subcommand {
Subcommands::Dump(command) => {
let mut input = command.input.open()?;
let mut input = if command.cert.is_empty() {
if let Some(path) = command.input.inner() {
if ! path.exists() &&
format!("{}", command.input).parse::<KeyHandle>().is_ok() {
wprintln!("The file {} does not exist, \
did you mean \"sq toolbox packet dump \
--cert {}\"?",
path.display(), path.display());
}
}
Box::new(command.input.open()?)
as Box<dyn io::Read + Send + Sync>
} else {
let cert = sq.resolve_cert(&command.cert, 0)?.0;
let bytes = cert.as_tsk().to_vec()
.context("Serializing certificate")?;
Box::new(io::Cursor::new(bytes))
};
let output_type = command.output;
let mut output = output_type.create_unsafe(&sq)?;