From 660ba2d3bb4054ce364b315c8128b41cddd61bac Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Wed, 26 Oct 2022 11:31:29 +0200 Subject: [PATCH] opgpcard: Extract ssh command into module --- tools/src/bin/opgpcard/cli.rs | 5 +-- tools/src/bin/opgpcard/commands/mod.rs | 1 + tools/src/bin/opgpcard/commands/ssh.rs | 55 ++++++++++++++++++++++++++ tools/src/bin/opgpcard/main.rs | 36 +---------------- 4 files changed, 59 insertions(+), 38 deletions(-) create mode 100644 tools/src/bin/opgpcard/commands/ssh.rs diff --git a/tools/src/bin/opgpcard/cli.rs b/tools/src/bin/opgpcard/cli.rs index c22125a..248050e 100644 --- a/tools/src/bin/opgpcard/cli.rs +++ b/tools/src/bin/opgpcard/cli.rs @@ -48,10 +48,7 @@ pub enum Command { Info(commands::info::InfoCommand), /// Display a card's authentication key as an SSH public key - Ssh { - #[clap(name = "card ident", short = 'c', long = "card")] - ident: Option, - }, + Ssh(commands::ssh::SshCommand), /// Export the key data on a card as an OpenPGP public key Pubkey { diff --git a/tools/src/bin/opgpcard/commands/mod.rs b/tools/src/bin/opgpcard/commands/mod.rs index 328ce90..67deb6e 100644 --- a/tools/src/bin/opgpcard/commands/mod.rs +++ b/tools/src/bin/opgpcard/commands/mod.rs @@ -3,4 +3,5 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 pub mod info; +pub mod ssh; pub mod status; diff --git a/tools/src/bin/opgpcard/commands/ssh.rs b/tools/src/bin/opgpcard/commands/ssh.rs new file mode 100644 index 0000000..8110da7 --- /dev/null +++ b/tools/src/bin/opgpcard/commands/ssh.rs @@ -0,0 +1,55 @@ +// SPDX-FileCopyrightText: 2021-2022 Heiko Schaefer +// SPDX-FileCopyrightText: 2022 Lars Wirzenius +// SPDX-FileCopyrightText: 2022 Nora Widdecke +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use anyhow::Result; +use clap::Parser; + +use openpgp_card_sequoia::card::Card; +use openpgp_card_sequoia::types::KeyType; + +use crate::output; +use crate::pick_card_for_reading; +use crate::util; +use crate::versioned_output::{OutputBuilder, OutputFormat, OutputVersion}; + +#[derive(Parser, Debug)] +pub struct SshCommand { + #[clap(name = "card ident", short = 'c', long = "card")] + pub ident: Option, +} + +pub fn print_ssh( + format: OutputFormat, + output_version: OutputVersion, + command: SshCommand, +) -> Result<()> { + let mut output = output::Ssh::default(); + + let ident = command.ident; + + let backend = pick_card_for_reading(ident)?; + let mut card = Card::new(backend); + let mut open = card.transaction()?; + + let ident = open.application_identifier()?.ident(); + output.ident(ident.clone()); + + // Print fingerprint of authentication subkey + let fps = open.fingerprints()?; + + if let Some(fp) = fps.authentication() { + output.authentication_key_fingerprint(fp.to_string()); + } + + // Show authentication subkey as openssh public key string + if let Ok(pkm) = open.public_key(KeyType::Authentication) { + if let Ok(ssh) = util::get_ssh_pubkey_string(&pkm, ident) { + output.ssh_public_key(ssh); + } + } + + println!("{}", output.print(format, output_version)?); + Ok(()) +} diff --git a/tools/src/bin/opgpcard/main.rs b/tools/src/bin/opgpcard/main.rs index 18067f2..b06d6fe 100644 --- a/tools/src/bin/opgpcard/main.rs +++ b/tools/src/bin/opgpcard/main.rs @@ -57,8 +57,8 @@ fn main() -> Result<(), Box> { cli::Command::Info(cmd) => { commands::info::print_info(cli.output_format, cli.output_version, cmd)?; } - cli::Command::Ssh { ident } => { - print_ssh(cli.output_format, cli.output_version, ident)?; + cli::Command::Ssh(cmd) => { + commands::ssh::print_ssh(cli.output_format, cli.output_version, cmd)?; } cli::Command::Pubkey { ident, @@ -607,38 +607,6 @@ fn pick_card_for_reading(ident: Option) -> Result, -) -> Result<()> { - let mut output = output::Ssh::default(); - - let backend = pick_card_for_reading(ident)?; - let mut card = Card::new(backend); - let mut open = card.transaction()?; - - let ident = open.application_identifier()?.ident(); - output.ident(ident.clone()); - - // Print fingerprint of authentication subkey - let fps = open.fingerprints()?; - - if let Some(fp) = fps.authentication() { - output.authentication_key_fingerprint(fp.to_string()); - } - - // Show authentication subkey as openssh public key string - if let Ok(pkm) = open.public_key(KeyType::Authentication) { - if let Ok(ssh) = util::get_ssh_pubkey_string(&pkm, ident) { - output.ssh_public_key(ssh); - } - } - - println!("{}", output.print(format, output_version)?); - Ok(()) -} - fn print_pubkey( format: OutputFormat, output_version: OutputVersion,