This commit is contained in:
nora 2024-08-23 02:16:43 +02:00
parent c4bb37e570
commit 5e5182a0b6

View file

@ -11,15 +11,12 @@ use ssh_keys::{KeyEncryptionParams, PrivateKeyType};
#[derive(clap::Parser)] #[derive(clap::Parser)]
struct Args { struct Args {
#[command(subcommand)] #[command(subcommand)]
command: Subcommand, cmd: Subcommand,
} }
#[derive(clap::Subcommand)] #[derive(clap::Subcommand)]
enum Subcommand { enum Subcommand {
/// Strips PEM armor /// Get information about an SSH key
Unpem { id_file: PathBuf },
/// Extract the encrypted part of the private key
ExtractEncrypted { id_file: PathBuf },
Info { Info {
/// Decrypt the key to get more information. Will not display private information unless --show-private is used /// Decrypt the key to get more information. Will not display private information unless --show-private is used
#[arg(short, long)] #[arg(short, long)]
@ -29,6 +26,7 @@ enum Subcommand {
show_private: bool, show_private: bool,
id_file: PathBuf, id_file: PathBuf,
}, },
/// Generate a new SSH key
Generate { Generate {
#[arg(short, long = "type")] #[arg(short, long = "type")]
type_: KeyType, type_: KeyType,
@ -37,6 +35,19 @@ enum Subcommand {
#[arg(short, long)] #[arg(short, long)]
path: PathBuf, path: PathBuf,
}, },
/// Commands for debugging SSH keys
Debug {
#[command(subcommand)]
cmd: DebugCommand,
},
}
#[derive(clap::Subcommand)]
enum DebugCommand {
/// Strips PEM armor
Unpem { id_file: PathBuf },
/// Extract the encrypted part of the private key
ExtractEncrypted { id_file: PathBuf },
} }
#[derive(clap::ValueEnum, Clone)] #[derive(clap::ValueEnum, Clone)]
@ -47,15 +58,19 @@ enum KeyType {
fn main() -> eyre::Result<()> { fn main() -> eyre::Result<()> {
let args = Args::parse(); let args = Args::parse();
match args.command { match args.cmd {
Subcommand::Unpem { id_file } => { Subcommand::Debug {
cmd: DebugCommand::Unpem { id_file },
} => {
let file = std::fs::read(&id_file) let file = std::fs::read(&id_file)
.wrap_err_with(|| format!("reading file {}", id_file.display()))?; .wrap_err_with(|| format!("reading file {}", id_file.display()))?;
let raw = pem::parse(&file)?; let raw = pem::parse(&file)?;
std::io::stdout().lock().write_all(raw.contents())?; std::io::stdout().lock().write_all(raw.contents())?;
Ok(()) Ok(())
} }
Subcommand::ExtractEncrypted { id_file } => { Subcommand::Debug {
cmd: DebugCommand::ExtractEncrypted { id_file },
} => {
let file = std::fs::read(&id_file) let file = std::fs::read(&id_file)
.wrap_err_with(|| format!("reading file {}", id_file.display()))?; .wrap_err_with(|| format!("reading file {}", id_file.display()))?;
let keys = ssh_keys::EncryptedPrivateKeys::parse(&file)?; let keys = ssh_keys::EncryptedPrivateKeys::parse(&file)?;