opgpcard: Extract ssh command into module
This commit is contained in:
parent
d05feec605
commit
660ba2d3bb
4 changed files with 59 additions and 38 deletions
|
@ -48,10 +48,7 @@ pub enum Command {
|
||||||
Info(commands::info::InfoCommand),
|
Info(commands::info::InfoCommand),
|
||||||
|
|
||||||
/// Display a card's authentication key as an SSH public key
|
/// Display a card's authentication key as an SSH public key
|
||||||
Ssh {
|
Ssh(commands::ssh::SshCommand),
|
||||||
#[clap(name = "card ident", short = 'c', long = "card")]
|
|
||||||
ident: Option<String>,
|
|
||||||
},
|
|
||||||
|
|
||||||
/// Export the key data on a card as an OpenPGP public key
|
/// Export the key data on a card as an OpenPGP public key
|
||||||
Pubkey {
|
Pubkey {
|
||||||
|
|
|
@ -3,4 +3,5 @@
|
||||||
// SPDX-License-Identifier: MIT OR Apache-2.0
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||||||
|
|
||||||
pub mod info;
|
pub mod info;
|
||||||
|
pub mod ssh;
|
||||||
pub mod status;
|
pub mod status;
|
||||||
|
|
55
tools/src/bin/opgpcard/commands/ssh.rs
Normal file
55
tools/src/bin/opgpcard/commands/ssh.rs
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
// SPDX-FileCopyrightText: 2021-2022 Heiko Schaefer <heiko@schaefer.name>
|
||||||
|
// SPDX-FileCopyrightText: 2022 Lars Wirzenius <liw@liw.fi>
|
||||||
|
// SPDX-FileCopyrightText: 2022 Nora Widdecke <mail@nora.pink>
|
||||||
|
// 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<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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(())
|
||||||
|
}
|
|
@ -57,8 +57,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
cli::Command::Info(cmd) => {
|
cli::Command::Info(cmd) => {
|
||||||
commands::info::print_info(cli.output_format, cli.output_version, cmd)?;
|
commands::info::print_info(cli.output_format, cli.output_version, cmd)?;
|
||||||
}
|
}
|
||||||
cli::Command::Ssh { ident } => {
|
cli::Command::Ssh(cmd) => {
|
||||||
print_ssh(cli.output_format, cli.output_version, ident)?;
|
commands::ssh::print_ssh(cli.output_format, cli.output_version, cmd)?;
|
||||||
}
|
}
|
||||||
cli::Command::Pubkey {
|
cli::Command::Pubkey {
|
||||||
ident,
|
ident,
|
||||||
|
@ -607,38 +607,6 @@ fn pick_card_for_reading(ident: Option<String>) -> Result<Box<dyn CardBackend +
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_ssh(
|
|
||||||
format: OutputFormat,
|
|
||||||
output_version: OutputVersion,
|
|
||||||
ident: Option<String>,
|
|
||||||
) -> 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(
|
fn print_pubkey(
|
||||||
format: OutputFormat,
|
format: OutputFormat,
|
||||||
output_version: OutputVersion,
|
output_version: OutputVersion,
|
||||||
|
|
Loading…
Reference in a new issue