aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2018-05-21 22:44:22 +0000
committerRobin Krahl <robin.krahl@ireas.org>2018-05-22 00:44:45 +0200
commit57cab82dc3063cb6608aaf2ee937ad7af231c52a (patch)
treee356de0f4db205d294d9ba495fa91203bf2ddc81 /src/lib.rs
parentba716c0ba1fdcdfe824d84f04f7580a1c8501777 (diff)
downloadnitrokey-rs-57cab82dc3063cb6608aaf2ee937ad7af231c52a.tar.gz
nitrokey-rs-57cab82dc3063cb6608aaf2ee937ad7af231c52a.tar.bz2
Add support for change_admin_pin and change_user_pin
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs76
1 files changed, 75 insertions, 1 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 8ffbf5a..817ee0a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -211,7 +211,6 @@ struct RawConfig {
pub user_password: bool,
}
-#[derive(Debug)]
/// A Nitrokey device without user or admin authentication.
///
/// Use [`connect`][] or [`connect_model`][] to obtain an instance. If you
@@ -250,6 +249,7 @@ struct RawConfig {
/// [`authenticate_user`]: #method.authenticate_user
/// [`connect`]: fn.connect.html
/// [`connect_model`]: fn.connect_model.html
+#[derive(Debug)]
pub struct UnauthenticatedDevice {}
/// A Nitrokey device with user authentication.
@@ -261,6 +261,7 @@ pub struct UnauthenticatedDevice {}
/// [`authenticate_admin`]: struct.UnauthenticatedDevice#method.authenticate_admin
/// [`device`]: #method.device
/// [`UnauthenticatedDevice`]: struct.UnauthenticatedDevice.html
+#[derive(Debug)]
pub struct UserAuthenticatedDevice {
device: UnauthenticatedDevice,
temp_password: Vec<u8>,
@@ -275,6 +276,7 @@ pub struct UserAuthenticatedDevice {
/// [`authenticate_admin`]: struct.UnauthenticatedDevice#method.authenticate_admin
/// [`device`]: #method.device
/// [`UnauthenticatedDevice`]: struct.UnauthenticatedDevice.html
+#[derive(Debug)]
pub struct AdminAuthenticatedDevice {
device: UnauthenticatedDevice,
temp_password: Vec<u8>,
@@ -634,6 +636,78 @@ pub trait Device {
return result_from_string(nitrokey_sys::NK_get_totp_code(slot, 0, 0, 0));
}
}
+
+ /// Changes the administrator PIN.
+ ///
+ /// # Errors
+ ///
+ /// - [`InvalidString`][] if one of the provided passwords contains a null byte
+ /// - [`WrongPassword`][] if the current admin password is wrong
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// use nitrokey::Device;
+ /// # use nitrokey::CommandError;
+ ///
+ /// # fn try_main() -> Result<(), CommandError> {
+ /// let device = nitrokey::connect()?;
+ /// match device.change_admin_pin("12345678", "12345679") {
+ /// CommandStatus::Success => println!("Updated admin PIN."),
+ /// CommandStatus::Error(err) => println!("Failed to update admin PIN: {:?}", err),
+ /// };
+ /// # Ok(())
+ /// # }
+ /// ```
+ ///
+ /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
+ fn change_admin_pin(&self, current: &str, new: &str) -> CommandStatus {
+ let current_string = CString::new(current);
+ let new_string = CString::new(new);
+ if current_string.is_err() || new_string.is_err() {
+ return CommandStatus::Error(CommandError::InvalidString);
+ }
+ let current_string = current_string.unwrap();
+ let new_string = new_string.unwrap();
+ unsafe { CommandStatus::from(nitrokey_sys::NK_change_admin_PIN(current_string.as_ptr(), new_string.as_ptr())) }
+ }
+
+ /// Changes the user PIN.
+ ///
+ /// # Errors
+ ///
+ /// - [`InvalidString`][] if one of the provided passwords contains a null byte
+ /// - [`WrongPassword`][] if the current user password is wrong
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// use nitrokey::Device;
+ /// # use nitrokey::CommandError;
+ ///
+ /// # fn try_main() -> Result<(), CommandError> {
+ /// let device = nitrokey::connect()?;
+ /// match device.change_user_pin("123456", "123457") {
+ /// CommandStatus::Success => println!("Updated admin PIN."),
+ /// CommandStatus::Error(err) => println!("Failed to update admin PIN: {:?}", err),
+ /// };
+ /// # Ok(())
+ /// # }
+ /// ```
+ ///
+ /// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
+ /// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
+ fn change_user_pin(&self, current: &str, new: &str) -> CommandStatus {
+ let current_string = CString::new(current);
+ let new_string = CString::new(new);
+ if current_string.is_err() || new_string.is_err() {
+ return CommandStatus::Error(CommandError::InvalidString);
+ }
+ let current_string = current_string.unwrap();
+ let new_string = new_string.unwrap();
+ unsafe { CommandStatus::from(nitrokey_sys::NK_change_user_PIN(current_string.as_ptr(), new_string.as_ptr())) }
+ }
}
trait AuthenticatedDevice {