summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2019-01-23 03:02:31 +0000
committerRobin Krahl <robin.krahl@ireas.org>2019-01-23 03:02:31 +0000
commite31009064eaaef9153ad5da3911aa0a939a050c2 (patch)
treef7469134712074a9081e09c1fb7caa85c52e6d04
parent601bc22ae18838ff56b64c15b365bcf7f93006be (diff)
downloadnitrokey-rs-e31009064eaaef9153ad5da3911aa0a939a050c2.tar.gz
nitrokey-rs-e31009064eaaef9153ad5da3911aa0a939a050c2.tar.bz2
Add temp_password_ptr method to AuthenticatedDevice
To reduce the number of casts, we introduce the temp_password_ptr method that casts the pointer received from the Vec<u8> to a c_char pointer that can be handled by libnitrokey.
-rw-r--r--src/auth.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/auth.rs b/src/auth.rs
index d1eb049..541de35 100644
--- a/src/auth.rs
+++ b/src/auth.rs
@@ -111,6 +111,8 @@ pub trait Authenticate {
trait AuthenticatedDevice<T> {
fn new(device: T, temp_password: Vec<u8>) -> Self;
+
+ fn temp_password_ptr(&self) -> *const c_char;
}
/// A Nitrokey device with user authentication.
@@ -217,20 +219,19 @@ impl<T: Device> Deref for User<T> {
impl<T: Device> GenerateOtp for User<T> {
fn get_hotp_code(&self, slot: u8) -> Result<String, Error> {
unsafe {
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
+ let temp_password_ptr = self.temp_password_ptr();
return result_from_string(nitrokey_sys::NK_get_hotp_code_PIN(slot, temp_password_ptr));
}
}
fn get_totp_code(&self, slot: u8) -> Result<String, Error> {
unsafe {
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
return result_from_string(nitrokey_sys::NK_get_totp_code_PIN(
slot,
0,
0,
0,
- temp_password_ptr,
+ self.temp_password_ptr(),
));
}
}
@@ -243,6 +244,10 @@ impl<T: Device> AuthenticatedDevice<T> for User<T> {
temp_password,
}
}
+
+ fn temp_password_ptr(&self) -> *const c_char {
+ self.temp_password.as_ptr() as *const c_char
+ }
}
impl<T: Device> Deref for Admin<T> {
@@ -297,7 +302,7 @@ impl<T: Device> Admin<T> {
raw_config.scrollock,
raw_config.user_password,
false,
- self.temp_password.as_ptr() as *const c_char,
+ self.temp_password_ptr(),
))
}
}
@@ -307,8 +312,7 @@ impl<T: Device> Admin<T> {
C: Fn(RawOtpSlotData, *const c_char) -> c_int,
{
let raw_data = RawOtpSlotData::new(data)?;
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
- get_command_result(callback(raw_data, temp_password_ptr))
+ get_command_result(callback(raw_data, self.temp_password_ptr()))
}
}
@@ -346,12 +350,12 @@ impl<T: Device> ConfigureOtp for Admin<T> {
}
fn erase_hotp_slot(&self, slot: u8) -> Result<(), Error> {
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
+ let temp_password_ptr = self.temp_password_ptr();
unsafe { get_command_result(nitrokey_sys::NK_erase_hotp_slot(slot, temp_password_ptr)) }
}
fn erase_totp_slot(&self, slot: u8) -> Result<(), Error> {
- let temp_password_ptr = self.temp_password.as_ptr() as *const c_char;
+ let temp_password_ptr = self.temp_password_ptr();
unsafe { get_command_result(nitrokey_sys::NK_erase_totp_slot(slot, temp_password_ptr)) }
}
}
@@ -363,6 +367,10 @@ impl<T: Device> AuthenticatedDevice<T> for Admin<T> {
temp_password,
}
}
+
+ fn temp_password_ptr(&self) -> *const c_char {
+ self.temp_password.as_ptr() as *const c_char
+ }
}
impl Authenticate for DeviceWrapper {