From e31009064eaaef9153ad5da3911aa0a939a050c2 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Wed, 23 Jan 2019 03:02:31 +0000 Subject: 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 to a c_char pointer that can be handled by libnitrokey. --- src/auth.rs | 24 ++++++++++++++++-------- 1 file 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 { fn new(device: T, temp_password: Vec) -> Self; + + fn temp_password_ptr(&self) -> *const c_char; } /// A Nitrokey device with user authentication. @@ -217,20 +219,19 @@ impl Deref for User { impl GenerateOtp for User { fn get_hotp_code(&self, slot: u8) -> Result { 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 { 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 AuthenticatedDevice for User { temp_password, } } + + fn temp_password_ptr(&self) -> *const c_char { + self.temp_password.as_ptr() as *const c_char + } } impl Deref for Admin { @@ -297,7 +302,7 @@ impl Admin { 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 Admin { 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 ConfigureOtp for Admin { } 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 AuthenticatedDevice for Admin { temp_password, } } + + fn temp_password_ptr(&self) -> *const c_char { + self.temp_password.as_ptr() as *const c_char + } } impl Authenticate for DeviceWrapper { -- cgit v1.2.3