summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-03 13:50:15 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-03 14:53:06 +0100
commit0a7a62c9af15b11e5dbfad1900ac89924457b272 (patch)
treee77735813562d284ce617663016cb2319f6276e2 /src
parentd60e03b46a6af75056f07394ef66ecaa35f32d77 (diff)
downloadnitrokey-rs-0a7a62c9af15b11e5dbfad1900ac89924457b272.tar.gz
nitrokey-rs-0a7a62c9af15b11e5dbfad1900ac89924457b272.tar.bz2
Add Device::factory_reset method
This patch adds the factory_reset_method to the Device trait that uses the NK_factory_reset function to perform a factory reset. The tests verify that the user and admin PIN are reset and that the OTP storage and the password safe are deleted.
Diffstat (limited to 'src')
-rw-r--r--src/device.rs36
1 files changed, 33 insertions, 3 deletions
diff --git a/src/device.rs b/src/device.rs
index 4b8cc5c..8702405 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -510,6 +510,36 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
fn lock(&self) -> Result<(), CommandError> {
unsafe { get_command_result(nitrokey_sys::NK_lock_device()) }
}
+
+ /// 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.).
+ ///
+ /// # Errors
+ ///
+ /// - [`InvalidString`][] if the provided password contains a null byte
+ /// - [`WrongPassword`][] if the admin password is wrong
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// use nitrokey::Device;
+ /// # use nitrokey::CommandError;
+ ///
+ /// # fn try_main() -> Result<(), CommandError> {
+ /// let device = nitrokey::connect()?;
+ /// match device.factory_reset("12345678") {
+ /// Ok(()) => println!("Performed a factory reset."),
+ /// Err(err) => println!("Could not perform a factory reset: {}", err),
+ /// };
+ /// # Ok(())
+ /// # }
+ /// ```
+ fn factory_reset(&self, admin_pin: &str) -> Result<(), CommandError> {
+ let admin_pin_string = get_cstring(admin_pin)?;
+ unsafe { get_command_result(nitrokey_sys::NK_factory_reset(admin_pin_string.as_ptr())) }
+ }
}
/// Connects to a Nitrokey device. This method can be used to connect to any connected device,
@@ -700,9 +730,9 @@ impl Storage {
let new_string = get_cstring(new)?;
unsafe {
get_command_result(nitrokey_sys::NK_change_update_password(
- current_string.as_ptr(),
- new_string.as_ptr(),
- ))
+ current_string.as_ptr(),
+ new_string.as_ptr(),
+ ))
}
}