aboutsummaryrefslogtreecommitdiff
path: root/src/device.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/device.rs')
-rw-r--r--src/device.rs36
1 files changed, 8 insertions, 28 deletions
diff --git a/src/device.rs b/src/device.rs
index c4e83a9..63a5654 100644
--- a/src/device.rs
+++ b/src/device.rs
@@ -4,8 +4,7 @@ use libc;
use nitrokey_sys;
use otp::GenerateOtp;
use pws::GetPasswordSafe;
-use std::ffi::CString;
-use util::{get_command_result, get_last_error, result_from_string, CommandError};
+use util::{get_command_result, get_cstring, get_last_error, result_from_string, CommandError};
/// Available Nitrokey models.
#[derive(Debug, PartialEq)]
@@ -312,13 +311,8 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn change_admin_pin(&self, current: &str, new: &str) -> Result<(), CommandError> {
- let current_string = CString::new(current);
- let new_string = CString::new(new);
- if current_string.is_err() || new_string.is_err() {
- return Err(CommandError::InvalidString);
- }
- let current_string = current_string.unwrap();
- let new_string = new_string.unwrap();
+ let current_string = get_cstring(current)?;
+ let new_string = get_cstring(new)?;
unsafe {
get_command_result(nitrokey_sys::NK_change_admin_PIN(
current_string.as_ptr(),
@@ -353,13 +347,8 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn change_user_pin(&self, current: &str, new: &str) -> Result<(), CommandError> {
- let current_string = CString::new(current);
- let new_string = CString::new(new);
- if current_string.is_err() || new_string.is_err() {
- return Err(CommandError::InvalidString);
- }
- let current_string = current_string.unwrap();
- let new_string = new_string.unwrap();
+ let current_string = get_cstring(current)?;
+ let new_string = get_cstring(new)?;
unsafe {
get_command_result(nitrokey_sys::NK_change_user_PIN(
current_string.as_ptr(),
@@ -394,13 +383,8 @@ pub trait Device: Authenticate + GetPasswordSafe + GenerateOtp {
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
fn unlock_user_pin(&self, admin_pin: &str, user_pin: &str) -> Result<(), CommandError> {
- let admin_pin_string = CString::new(admin_pin);
- let user_pin_string = CString::new(user_pin);
- if admin_pin_string.is_err() || user_pin_string.is_err() {
- return Err(CommandError::InvalidString);
- }
- let admin_pin_string = admin_pin_string.unwrap();
- let user_pin_string = user_pin_string.unwrap();
+ let admin_pin_string = get_cstring(admin_pin)?;
+ let user_pin_string = get_cstring(user_pin)?;
unsafe {
get_command_result(nitrokey_sys::NK_unlock_user_password(
admin_pin_string.as_ptr(),
@@ -562,11 +546,7 @@ impl Storage {
/// [`InvalidString`]: enum.CommandError.html#variant.InvalidString
/// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword
pub fn enable_encrypted_volume(&self, user_pin: &str) -> Result<(), CommandError> {
- let user_pin = CString::new(user_pin);
- if user_pin.is_err() {
- return Err(CommandError::InvalidString);
- }
- let user_pin = user_pin.unwrap();
+ let user_pin = get_cstring(user_pin)?;
unsafe { get_command_result(nitrokey_sys::NK_unlock_encrypted_volume(user_pin.as_ptr())) }
}