From 21e7c36a0ae14f2200e806fcf66cbe0836cc2ec0 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Tue, 25 Dec 2018 23:02:18 +0100 Subject: Add status output for Nitrokey Pro Currently, the status command fails for a Nitrokey Pro. This patch changes the command to also print basic status information for Pro devices. For the sake of consistency, the common status is always queried using the common `Device` functions, even if the Storage status includes the same information. --- nitrocli/CHANGELOG.md | 1 + nitrocli/doc/nitrocli.1 | 9 +++---- nitrocli/src/args.rs | 2 +- nitrocli/src/commands.rs | 61 ++++++++++++++++++++++++------------------------ 4 files changed, 37 insertions(+), 36 deletions(-) diff --git a/nitrocli/CHANGELOG.md b/nitrocli/CHANGELOG.md index 3ade4e3..65510df 100644 --- a/nitrocli/CHANGELOG.md +++ b/nitrocli/CHANGELOG.md @@ -7,6 +7,7 @@ Unreleased indirect dependencies - Removed the `hid`, `hidapi-sys` and `pkg-config` dependencies - Added the `otp` command for working with one-time passwords +- Made `status` command work with Nitrokey Pro devices - Enabled CI pipeline comprising code style conformance checks, linting, and building of the project - Added badges indicating pipeline status, current `crates.io` published diff --git a/nitrocli/doc/nitrocli.1 b/nitrocli/doc/nitrocli.1 index db6dde1..98f6b28 100644 --- a/nitrocli/doc/nitrocli.1 +++ b/nitrocli/doc/nitrocli.1 @@ -1,4 +1,4 @@ -.TH NITROCLI 1 2018-12-14 +.TH NITROCLI 1 2018-12-26 .SH NAME nitrocli \- access Nitrokey devices .SH SYNOPSIS @@ -19,9 +19,10 @@ The user PIN that is required to open the volume is queried using Close the encrypted volume on the Nitrokey Storage. .TP .B status -Print the status of the connected Nitrokey Storage, including the SD card -serial number, the firmware version, the encryption status, the PIN retry count -and the status of the volumes. +Print the status of the connected Nitrokey device, including the stick serial +number, SD card serial number, the firmware version and the PIN retry count. +For the Nitrokey Storage, it also includes the encryption status and the status +of the volumes. .TP .B clear Clear the passphrase cached by the \fBopen\fR command. diff --git a/nitrocli/src/args.rs b/nitrocli/src/args.rs index ab80f3d..b48acfa 100644 --- a/nitrocli/src/args.rs +++ b/nitrocli/src/args.rs @@ -211,7 +211,7 @@ fn parse(parser: &argparse::ArgumentParser<'_>, args: Vec) -> Result<()> /// Inquire the status of the nitrokey. fn status(args: Vec) -> Result<()> { let mut parser = argparse::ArgumentParser::new(); - parser.set_description("Print the status of the connected Nitrokey Storage"); + parser.set_description("Print the status of the connected Nitrokey device"); parse(&parser, args)?; commands::status() diff --git a/nitrocli/src/commands.rs b/nitrocli/src/commands.rs index c4d4598..528fa99 100644 --- a/nitrocli/src/commands.rs +++ b/nitrocli/src/commands.rs @@ -40,8 +40,11 @@ fn get_device() -> Result { /// Connect to a Nitrokey Storage device and return it. fn get_storage_device() -> Result { - nitrokey::Storage::connect() - .or_else(|_| Err(Error::Error("Nitrokey device not found".to_string()))) + nitrokey::Storage::connect().or_else(|_| { + Err(Error::Error( + "Nitrokey Storage device not found".to_string(), + )) + }) } /// Authenticate the given device using the given PIN type and operation. @@ -171,41 +174,30 @@ where .map_err(|(_data, err)| err) } -/// Pretty print the status that is common to all Nitrokey devices. -fn print_status( - model: &'static str, - smartcard_id: &str, - firmware_major: i32, - firmware_minor: i32, - user_retries: u8, - admin_retries: u8, -) { +/// Query and pretty print the status that is common to all Nitrokey devices. +fn print_status(model: &'static str, device: &nitrokey::DeviceWrapper) -> Result<()> { + let serial_number = device + .get_serial_number() + .map_err(|err| get_error("Could not query the serial number", &err))?; println!( r#"Status: model: {model} - smart card ID: {id} + serial number: 0x{id} firmware version: {fwv0}.{fwv1} user retry count: {urc} admin retry count: {arc}"#, model = model, - id = smartcard_id, - fwv0 = firmware_major, - fwv1 = firmware_minor, - urc = user_retries, - arc = admin_retries, + id = serial_number, + fwv0 = device.get_major_firmware_version(), + fwv1 = device.get_minor_firmware_version(), + urc = device.get_user_retry_count(), + arc = device.get_admin_retry_count(), ); + Ok(()) } -/// Pretty print the response of a status command for the Nitrokey Storage. +/// Pretty print the status of a Nitrokey Storage. fn print_storage_status(status: &nitrokey::StorageStatus) { - print_status( - "Storage", - &format!("{:#x}", status.serial_number_smart_card), - status.firmware_version_major as i32, - status.firmware_version_minor as i32, - status.user_retry_count, - status.admin_retry_count, - ); println!( r#" SD card ID: {id:#x} @@ -234,11 +226,18 @@ fn print_storage_status(status: &nitrokey::StorageStatus) { /// Inquire the status of the nitrokey. pub fn status() -> Result<()> { - let status = get_storage_device()? - .get_status() - .map_err(|err| get_error("Getting Storage status failed", &err))?; - - print_storage_status(&status); + let device = get_device()?; + let model = match device { + nitrokey::DeviceWrapper::Pro(_) => "Pro", + nitrokey::DeviceWrapper::Storage(_) => "Storage", + }; + print_status(model, &device)?; + if let nitrokey::DeviceWrapper::Storage(storage) = device { + let status = storage + .get_status() + .map_err(|err| get_error("Getting Storage status failed", &err))?; + print_storage_status(&status); + } Ok(()) } -- cgit v1.2.3