From 8451a9afca7e187cf584fbfc77eda15662e80e3c Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Fri, 10 Jun 2022 13:46:36 +0200 Subject: sq: Adapt packet dump to clap3's derive style. --- sq/src/commands/dump.rs | 24 +++++++++++++----------- sq/src/sq.rs | 47 +++++++++++++---------------------------------- sq/src/sq_cli.rs | 2 +- 3 files changed, 27 insertions(+), 46 deletions(-) diff --git a/sq/src/commands/dump.rs b/sq/src/commands/dump.rs index d11bcf56..130cafd4 100644 --- a/sq/src/commands/dump.rs +++ b/sq/src/commands/dump.rs @@ -9,9 +9,11 @@ use self::openpgp::packet::prelude::*; use self::openpgp::packet::header::CTB; use self::openpgp::packet::{Header, header::BodyLength, Signature}; use self::openpgp::packet::signature::subpacket::{Subpacket, SubpacketValue}; -use self::openpgp::crypto::{SessionKey, S2K}; +use self::openpgp::crypto::S2K; use self::openpgp::parse::{map::Map, Parse, PacketParserResult}; +use crate::sq_cli::CliSessionKey; + #[derive(Debug)] pub enum Kind { Message { @@ -55,8 +57,8 @@ impl Convert> for Timestamp { #[allow(clippy::redundant_pattern_matching)] pub fn dump(input: &mut (dyn io::Read + Sync + Send), output: &mut dyn io::Write, - mpis: bool, hex: bool, sk: Option<&SessionKey>, - algo_hint: Option, + mpis: bool, hex: bool, + sk: Option<&CliSessionKey>, width: W) -> Result where W: Into> @@ -85,26 +87,26 @@ pub fn dump(input: &mut (dyn io::Read + Sync + Send), } Packet::SEIP(_) if sk.is_some() => { message_encrypted = true; - let sk = sk.as_ref().unwrap(); - let decrypted_with = if let Some(algo) = algo_hint { + let sk = sk.unwrap(); + let decrypted_with = if let Some(algo) = sk.symmetric_algo { // We know which algorithm to use, so only try decrypting // with that one. - pp.decrypt(algo, sk).is_ok().then(|| algo) + pp.decrypt(algo, &sk.session_key).is_ok().then(|| algo) } else { // We don't know which algorithm to use, // try to find one that decrypts the message. (1u8..=19) .map(SymmetricAlgorithm::from) - .find(|algo| pp.decrypt(*algo, sk).is_ok()) + .find(|algo| pp.decrypt(*algo, &sk.session_key).is_ok()) }; let mut fields = Vec::new(); - fields.push(format!("Session key: {}", hex::encode(sk))); + fields.push(format!("Session key: {}", &sk.display_sensitive())); if let Some(algo) = decrypted_with { fields.push(format!("Symmetric algo: {}", algo)); fields.push("Decryption successful".into()); } else { - if let Some(algo) = algo_hint { + if let Some(algo) = sk.symmetric_algo { fields.push(format!( "Indicated Symmetric algo: {}", algo )); @@ -126,10 +128,10 @@ pub fn dump(input: &mut (dyn io::Read + Sync + Send), unreachable!() }; - let _ = pp.decrypt(algo, sk); + let _ = pp.decrypt(algo, &sk.session_key); let mut fields = Vec::new(); - fields.push(format!("Session key: {}", hex::encode(sk))); + fields.push(format!("Session key: {}", sk.display_sensitive())); if pp.processed() { fields.push("Decryption successful".into()); } else { diff --git a/sq/src/sq.rs b/sq/src/sq.rs index 1ad1f500..918ac599 100644 --- a/sq/src/sq.rs +++ b/sq/src/sq.rs @@ -15,9 +15,8 @@ use openpgp::{ Result, }; use crate::openpgp::{armor, Cert}; -use crate::openpgp::crypto::{Password, SessionKey}; -use crate::openpgp::fmt::hex; -use crate::openpgp::types::{KeyFlags, SymmetricAlgorithm}; +use crate::openpgp::crypto::Password; +use crate::openpgp::types::KeyFlags; use crate::openpgp::packet::prelude::*; use crate::openpgp::parse::{Parse, PacketParser, PacketParserResult}; use crate::openpgp::packet::signature::subpacket::NotationData; @@ -662,24 +661,19 @@ fn main() -> Result<()> { Some(("packet", m)) => match m.subcommand() { Some(("dump", m)) => { - let mut input = open_or_stdin(m.value_of("input"))?; - let mut output = - config.create_or_stdout_unsafe(m.value_of("output"))?; - - let (session_key, algo_hint) = - if let Some(sk) = m.value_of("session-key") { - decode_session_key(sk) - .with_context(|| format!( - "Bad value passed to --session-key: {:?}", - sk - ))? - } else { - (None, None) - }; + use clap::FromArgMatches; + let command = sq_cli::PacketDumpCommand::from_arg_matches(m)?; + + let mut input = open_or_stdin(command.io.input.as_deref())?; + let mut output = config.create_or_stdout_unsafe( + command.io.output.as_deref(), + )?; + + let session_key = command.session_key; let width = term_size::dimensions_stdout().map(|(w, _)| w); commands::dump(&mut input, &mut output, - m.is_present("mpis"), m.is_present("hex"), - session_key.as_ref(), algo_hint, width)?; + command.mpis, command.hex, + session_key.as_ref(), width)?; }, Some(("decrypt", m)) => { @@ -746,21 +740,6 @@ fn main() -> Result<()> { Ok(()) } -/// Parses a session key, which may have an algorithm prefix -fn decode_session_key( - sk: &str, -) -> Result<(Option, Option)> { - if let Some((algo, sk)) = sk.split_once(':') { - let algo = SymmetricAlgorithm::from(algo.parse::()?); - let dsk = hex::decode_pretty(sk)?.into(); - Ok((Some(dsk), Some(algo))) - } else { - let dsk = hex::decode_pretty(sk)?.into(); - Ok((Some(dsk), None)) - } -} - - /// Parses the given string depicting a ISO 8601 timestamp. fn parse_iso8601(s: &str, pad_date_with: chrono::NaiveTime) -> Result> diff --git a/sq/src/sq_cli.rs b/sq/src/sq_cli.rs index 076ce430..eb7438c0 100644 --- a/sq/src/sq_cli.rs +++ b/sq/src/sq_cli.rs @@ -466,7 +466,7 @@ pub struct PacketDumpCommand { value_name = "SESSION-KEY", help = "Decrypts an encrypted message using SESSION-KEY", )] - pub session_key: Option, + pub session_key: Option, #[clap( long = "mpis", help = "Prints cryptographic artifacts", -- cgit v1.2.3