[][src]Trait nitrokey::Device

pub trait Device<'a>: Authenticate<'a> + GetPasswordSafe<'a> + GenerateOtp + Debug {
    fn into_manager(self) -> &'a mut Manager;
fn get_model(&self) -> Model;
fn get_status(&self) -> Result<Status, Error>; fn get_serial_number(&self) -> Result<String, Error> { ... }
fn get_user_retry_count(&self) -> Result<u8, Error> { ... }
fn get_admin_retry_count(&self) -> Result<u8, Error> { ... }
fn get_firmware_version(&self) -> Result<FirmwareVersion, Error> { ... }
fn get_config(&self) -> Result<Config, Error> { ... }
fn change_admin_pin(
        &mut self,
        current: &str,
        new: &str
    ) -> Result<(), Error> { ... }
fn change_user_pin(&mut self, current: &str, new: &str) -> Result<(), Error> { ... }
fn unlock_user_pin(
        &mut self,
        admin_pin: &str,
        user_pin: &str
    ) -> Result<(), Error> { ... }
fn lock(&mut self) -> Result<(), Error> { ... }
fn factory_reset(&mut self, admin_pin: &str) -> Result<(), Error> { ... }
fn build_aes_key(&mut self, admin_pin: &str) -> Result<(), Error> { ... } }

A Nitrokey device.

This trait provides the commands that can be executed without authentication and that are present on all supported Nitrokey devices.

Required methods

fn into_manager(self) -> &'a mut Manager

Returns the [Manager][] instance that has been used to connect to this device.

Example

use nitrokey::{Device, DeviceWrapper};

fn do_something(device: DeviceWrapper) {
    // reconnect to any device
    let manager = device.into_manager();
    let device = manager.connect();
    // do something with the device
    // ...
}

match nitrokey::take()?.connect() {
    Ok(device) => do_something(device),
    Err(err) => println!("Could not connect to a Nitrokey: {}", err),
}

fn get_model(&self) -> Model

Returns the model of the connected Nitrokey device.

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let device = manager.connect()?;
println!("Connected to a Nitrokey {}", device.get_model());

fn get_status(&self) -> Result<Status, Error>

Returns the status of the Nitrokey device.

This methods returns the status information common to all Nitrokey devices as a Status struct. Some models may provide more information, for example get_storage_status returns the StorageStatus struct.

Errors

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let device = manager.connect()?;
let status = device.get_status()?;
println!("Firmware version: {}", status.firmware_version);
println!("Serial number:    {:x}", status.serial_number);
Loading content...

Provided methods

fn get_serial_number(&self) -> Result<String, Error>

Returns the serial number of the Nitrokey device. The serial number is the string representation of a hex number.

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let device = manager.connect()?;
match device.get_serial_number() {
    Ok(number) => println!("serial no: {}", number),
    Err(err) => eprintln!("Could not get serial number: {}", err),
};

fn get_user_retry_count(&self) -> Result<u8, Error>

Returns the number of remaining authentication attempts for the user. The total number of available attempts is three.

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let device = manager.connect()?;
let count = device.get_user_retry_count();
match device.get_user_retry_count() {
    Ok(count) => println!("{} remaining authentication attempts (user)", count),
    Err(err) => eprintln!("Could not get user retry count: {}", err),
}

fn get_admin_retry_count(&self) -> Result<u8, Error>

Returns the number of remaining authentication attempts for the admin. The total number of available attempts is three.

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let device = manager.connect()?;
let count = device.get_admin_retry_count();
match device.get_admin_retry_count() {
    Ok(count) => println!("{} remaining authentication attempts (admin)", count),
    Err(err) => eprintln!("Could not get admin retry count: {}", err),
}

fn get_firmware_version(&self) -> Result<FirmwareVersion, Error>

Returns the firmware version.

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let device = manager.connect()?;
match device.get_firmware_version() {
    Ok(version) => println!("Firmware version: {}", version),
    Err(err) => eprintln!("Could not access firmware version: {}", err),
};

fn get_config(&self) -> Result<Config, Error>

Returns the current configuration of the Nitrokey device.

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let device = manager.connect()?;
let config = device.get_config()?;
println!("numlock binding:          {:?}", config.numlock);
println!("capslock binding:         {:?}", config.capslock);
println!("scrollock binding:        {:?}", config.scrollock);
println!("require password for OTP: {:?}", config.user_password);

fn change_admin_pin(&mut self, current: &str, new: &str) -> Result<(), Error>

Changes the administrator PIN.

Errors

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let mut device = manager.connect()?;
match device.change_admin_pin("12345678", "12345679") {
    Ok(()) => println!("Updated admin PIN."),
    Err(err) => eprintln!("Failed to update admin PIN: {}", err),
};

fn change_user_pin(&mut self, current: &str, new: &str) -> Result<(), Error>

Changes the user PIN.

Errors

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let mut device = manager.connect()?;
match device.change_user_pin("123456", "123457") {
    Ok(()) => println!("Updated admin PIN."),
    Err(err) => eprintln!("Failed to update admin PIN: {}", err),
};

fn unlock_user_pin(
    &mut self,
    admin_pin: &str,
    user_pin: &str
) -> Result<(), Error>

Unlocks the user PIN after three failed login attempts and sets it to the given value.

Errors

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let mut device = manager.connect()?;
match device.unlock_user_pin("12345678", "123456") {
    Ok(()) => println!("Unlocked user PIN."),
    Err(err) => eprintln!("Failed to unlock user PIN: {}", err),
};

fn lock(&mut self) -> Result<(), Error>

Locks the Nitrokey device.

This disables the password store if it has been unlocked. On the Nitrokey Storage, this also disables the volumes if they have been enabled.

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let mut device = manager.connect()?;
match device.lock() {
    Ok(()) => println!("Locked the Nitrokey device."),
    Err(err) => eprintln!("Could not lock the Nitrokey device: {}", err),
};

fn factory_reset(&mut self, admin_pin: &str) -> Result<(), Error>

Performs a factory reset on the Nitrokey device.

This commands performs a factory reset on the smart card (like the factory reset via gpg --card-edit) and then clears the flash memory (password safe, one-time passwords etc.). After a factory reset, build_aes_key has to be called before the password safe or the encrypted volume can be used.

Errors

  • [InvalidString][] if the provided password contains a null byte
  • [WrongPassword][] if the admin password is wrong

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let mut device = manager.connect()?;
match device.factory_reset("12345678") {
    Ok(()) => println!("Performed a factory reset."),
    Err(err) => eprintln!("Could not perform a factory reset: {}", err),
};

fn build_aes_key(&mut self, admin_pin: &str) -> Result<(), Error>

Builds a new AES key on the Nitrokey.

The AES key is used to encrypt the password safe and the encrypted volume. You may need to call this method after a factory reset, either using factory_reset or using gpg --card-edit. You can also use it to destroy the data stored in the password safe or on the encrypted volume.

Errors

  • [InvalidString][] if the provided password contains a null byte
  • [WrongPassword][] if the admin password is wrong

Example

use nitrokey::Device;

let mut manager = nitrokey::take()?;
let mut device = manager.connect()?;
match device.build_aes_key("12345678") {
    Ok(()) => println!("New AES keys have been built."),
    Err(err) => eprintln!("Could not build new AES keys: {}", err),
};
Loading content...

Implementors

impl<'a> Device<'a> for DeviceWrapper<'a>[src]

impl<'a> Device<'a> for Pro<'a>[src]

impl<'a> Device<'a> for Storage<'a>[src]

Loading content...