diff --git a/tools/src/bin/opgpcard/cli.rs b/tools/src/bin/opgpcard/cli.rs index 11ba087..5eca759 100644 --- a/tools/src/bin/opgpcard/cli.rs +++ b/tools/src/bin/opgpcard/cli.rs @@ -90,13 +90,7 @@ pub enum Command { FactoryReset(commands::factory_reset::FactoryResetCommand), /// Change identity (applies only to Nitrokey Start) - SetIdentity { - #[clap(name = "card ident", short = 'c', long = "card")] - ident: String, - - #[clap(name = "identity", value_enum)] - id: SetIdentityId, - }, + SetIdentity(commands::set_identity::SetIdentityCommand), } #[derive(Parser, Debug)] @@ -307,26 +301,6 @@ impl From for openpgp_card_sequoia::types::TouchPolicy { } } -#[derive(ValueEnum, Debug, Clone)] -pub enum SetIdentityId { - #[clap(name = "0")] - Zero, - #[clap(name = "1")] - One, - #[clap(name = "2")] - Two, -} - -impl From for u8 { - fn from(id: SetIdentityId) -> Self { - match id { - SetIdentityId::Zero => 0, - SetIdentityId::One => 1, - SetIdentityId::Two => 2, - } - } -} - #[derive(ValueEnum, Debug, Clone)] #[clap(rename_all = "lower")] pub enum AdminGenerateAlgo { diff --git a/tools/src/bin/opgpcard/commands/mod.rs b/tools/src/bin/opgpcard/commands/mod.rs index 45a2364..6bbe9e7 100644 --- a/tools/src/bin/opgpcard/commands/mod.rs +++ b/tools/src/bin/opgpcard/commands/mod.rs @@ -6,6 +6,7 @@ pub mod decrypt; pub mod factory_reset; pub mod info; pub mod pubkey; +pub mod set_identity; pub mod sign; pub mod ssh; pub mod status; diff --git a/tools/src/bin/opgpcard/commands/set_identity.rs b/tools/src/bin/opgpcard/commands/set_identity.rs new file mode 100644 index 0000000..efb6cef --- /dev/null +++ b/tools/src/bin/opgpcard/commands/set_identity.rs @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: 2021-2022 Heiko Schaefer +// SPDX-FileCopyrightText: 2022 Nora Widdecke +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use anyhow::Result; +use clap::{Parser, ValueEnum}; + +use openpgp_card_sequoia::card::Card; + +use crate::util; + +#[derive(Parser, Debug)] +pub struct SetIdentityCommand { + #[clap(name = "card ident", short = 'c', long = "card")] + ident: String, + + #[clap(name = "identity", value_enum)] + id: SetIdentityId, +} + +#[derive(ValueEnum, Debug, Clone)] +pub enum SetIdentityId { + #[clap(name = "0")] + Zero, + #[clap(name = "1")] + One, + #[clap(name = "2")] + Two, +} + +impl From for u8 { + fn from(id: SetIdentityId) -> Self { + match id { + SetIdentityId::Zero => 0, + SetIdentityId::One => 1, + SetIdentityId::Two => 2, + } + } +} + +pub fn set_identity(command: SetIdentityCommand) -> Result<(), Box> { + let backend = util::open_card(&command.ident)?; + let mut card = Card::new(backend); + let mut open = card.transaction()?; + + open.set_identity(u8::from(command.id))?; + Ok(()) +} diff --git a/tools/src/bin/opgpcard/main.rs b/tools/src/bin/opgpcard/main.rs index 0c554cc..87263b3 100644 --- a/tools/src/bin/opgpcard/main.rs +++ b/tools/src/bin/opgpcard/main.rs @@ -60,8 +60,8 @@ fn main() -> Result<(), Box> { cli::Command::Pubkey(cmd) => { commands::pubkey::print_pubkey(cli.output_format, cli.output_version, cmd)?; } - cli::Command::SetIdentity { ident, id } => { - set_identity(&ident, id)?; + cli::Command::SetIdentity(cmd) => { + commands::set_identity::set_identity(cmd)?; } cli::Command::Decrypt(cmd) => { commands::decrypt::decrypt(cmd)?; @@ -548,15 +548,6 @@ fn list_cards(format: OutputFormat, output_version: OutputVersion) -> Result<()> Ok(()) } -fn set_identity(ident: &str, id: cli::SetIdentityId) -> Result<(), Box> { - let backend = util::open_card(ident)?; - let mut card = Card::new(backend); - let mut open = card.transaction()?; - - open.set_identity(u8::from(id))?; - Ok(()) -} - /// Return a card for a read operation. If `ident` is None, and exactly one card /// is plugged in, that card is returned. (We don't This fn pick_card_for_reading(ident: Option) -> Result> {