opgpcard: Extract set_identity command into module
This commit is contained in:
parent
d0ad41c9f5
commit
9b7e614772
4 changed files with 52 additions and 38 deletions
|
@ -90,13 +90,7 @@ pub enum Command {
|
||||||
FactoryReset(commands::factory_reset::FactoryResetCommand),
|
FactoryReset(commands::factory_reset::FactoryResetCommand),
|
||||||
|
|
||||||
/// Change identity (applies only to Nitrokey Start)
|
/// Change identity (applies only to Nitrokey Start)
|
||||||
SetIdentity {
|
SetIdentity(commands::set_identity::SetIdentityCommand),
|
||||||
#[clap(name = "card ident", short = 'c', long = "card")]
|
|
||||||
ident: String,
|
|
||||||
|
|
||||||
#[clap(name = "identity", value_enum)]
|
|
||||||
id: SetIdentityId,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
@ -307,26 +301,6 @@ impl From<TouchPolicy> 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<SetIdentityId> for u8 {
|
|
||||||
fn from(id: SetIdentityId) -> Self {
|
|
||||||
match id {
|
|
||||||
SetIdentityId::Zero => 0,
|
|
||||||
SetIdentityId::One => 1,
|
|
||||||
SetIdentityId::Two => 2,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(ValueEnum, Debug, Clone)]
|
#[derive(ValueEnum, Debug, Clone)]
|
||||||
#[clap(rename_all = "lower")]
|
#[clap(rename_all = "lower")]
|
||||||
pub enum AdminGenerateAlgo {
|
pub enum AdminGenerateAlgo {
|
||||||
|
|
|
@ -6,6 +6,7 @@ pub mod decrypt;
|
||||||
pub mod factory_reset;
|
pub mod factory_reset;
|
||||||
pub mod info;
|
pub mod info;
|
||||||
pub mod pubkey;
|
pub mod pubkey;
|
||||||
|
pub mod set_identity;
|
||||||
pub mod sign;
|
pub mod sign;
|
||||||
pub mod ssh;
|
pub mod ssh;
|
||||||
pub mod status;
|
pub mod status;
|
||||||
|
|
48
tools/src/bin/opgpcard/commands/set_identity.rs
Normal file
48
tools/src/bin/opgpcard/commands/set_identity.rs
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
// SPDX-FileCopyrightText: 2021-2022 Heiko Schaefer <heiko@schaefer.name>
|
||||||
|
// SPDX-FileCopyrightText: 2022 Nora Widdecke <mail@nora.pink>
|
||||||
|
// 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<SetIdentityId> 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<dyn std::error::Error>> {
|
||||||
|
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(())
|
||||||
|
}
|
|
@ -60,8 +60,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
cli::Command::Pubkey(cmd) => {
|
cli::Command::Pubkey(cmd) => {
|
||||||
commands::pubkey::print_pubkey(cli.output_format, cli.output_version, cmd)?;
|
commands::pubkey::print_pubkey(cli.output_format, cli.output_version, cmd)?;
|
||||||
}
|
}
|
||||||
cli::Command::SetIdentity { ident, id } => {
|
cli::Command::SetIdentity(cmd) => {
|
||||||
set_identity(&ident, id)?;
|
commands::set_identity::set_identity(cmd)?;
|
||||||
}
|
}
|
||||||
cli::Command::Decrypt(cmd) => {
|
cli::Command::Decrypt(cmd) => {
|
||||||
commands::decrypt::decrypt(cmd)?;
|
commands::decrypt::decrypt(cmd)?;
|
||||||
|
@ -548,15 +548,6 @@ fn list_cards(format: OutputFormat, output_version: OutputVersion) -> Result<()>
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_identity(ident: &str, id: cli::SetIdentityId) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
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
|
/// 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
|
/// is plugged in, that card is returned. (We don't This
|
||||||
fn pick_card_for_reading(ident: Option<String>) -> Result<Box<dyn CardBackend + Send + Sync>> {
|
fn pick_card_for_reading(ident: Option<String>) -> Result<Box<dyn CardBackend + Send + Sync>> {
|
||||||
|
|
Loading…
Reference in a new issue