aboutsummaryrefslogtreecommitdiff
path: root/src/device
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-01-29 13:52:19 +0100
committerRobin Krahl <robin.krahl@ireas.org>2020-02-03 10:22:13 +0100
commitc1f48ce6c614586042db8891d2eebf19d2212ce4 (patch)
tree9824d4552faf18a09eabb214deb87b4db950ab41 /src/device
parent4a9dab94400cb00ae1e28485ddc64d46cf27ed3c (diff)
downloadnitrokey-rs-c1f48ce6c614586042db8891d2eebf19d2212ce4.tar.gz
nitrokey-rs-c1f48ce6c614586042db8891d2eebf19d2212ce4.tar.bz2
Use NK_get_status to implement Device::get_config
libnitrokey’s NK_read_config function returns a pointer to an array that has been allocated using new[]. We would have to delete this pointer using delete[], but we only have access to free. Therefore this patch modifies the Device::get_config function to call NK_get_status instead of NK_read_config. This also makes the code more safe as we get the data as a struct instead of an array. It does not add much overhead as NK_read_config also executes the GET_STATUS command on the Nitrokey device.
Diffstat (limited to 'src/device')
-rw-r--r--src/device/mod.rs23
1 files changed, 12 insertions, 11 deletions
diff --git a/src/device/mod.rs b/src/device/mod.rs
index 403bb1f..e9ca0dc 100644
--- a/src/device/mod.rs
+++ b/src/device/mod.rs
@@ -9,7 +9,6 @@ use std::convert::{TryFrom, TryInto};
use std::ffi;
use std::fmt;
-use libc;
use nitrokey_sys;
use crate::auth::Authenticate;
@@ -18,8 +17,7 @@ use crate::error::{CommunicationError, Error};
use crate::otp::GenerateOtp;
use crate::pws::GetPasswordSafe;
use crate::util::{
- get_command_result, get_cstring, get_last_error, owned_str_from_ptr, result_from_string,
- result_or_error,
+ get_command_result, get_cstring, owned_str_from_ptr, result_from_string, result_or_error,
};
pub use pro::Pro;
@@ -386,14 +384,17 @@ pub trait Device<'a>: Authenticate<'a> + GetPasswordSafe<'a> + GenerateOtp + fmt
/// # }
/// ```
fn get_config(&self) -> Result<Config, Error> {
- let config_ptr = unsafe { nitrokey_sys::NK_read_config() };
- if config_ptr.is_null() {
- return Err(get_last_error());
- }
- let config_array_ptr = config_ptr as *const [u8; 5];
- let raw_config = unsafe { RawConfig::from(*config_array_ptr) };
- unsafe { libc::free(config_ptr as *mut libc::c_void) };
- Ok(raw_config.into())
+ let mut raw_status = nitrokey_sys::NK_status {
+ firmware_version_major: 0,
+ firmware_version_minor: 0,
+ serial_number_smart_card: 0,
+ config_numlock: 0,
+ config_capslock: 0,
+ config_scrolllock: 0,
+ otp_user_password: false,
+ };
+ get_command_result(unsafe { nitrokey_sys::NK_get_status(&mut raw_status) })?;
+ Ok(RawConfig::from(&raw_status).into())
}
/// Changes the administrator PIN.