aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-02-03 10:22:37 +0100
committerRobin Krahl <robin.krahl@ireas.org>2020-02-03 10:22:37 +0100
commitce4479b6f7a353f388aecda03cddc35389940252 (patch)
tree9824d4552faf18a09eabb214deb87b4db950ab41
parent395ace0131ef6cd655a222734f2cc8d037395a3b (diff)
parentc1f48ce6c614586042db8891d2eebf19d2212ce4 (diff)
downloadnitrokey-rs-ce4479b6f7a353f388aecda03cddc35389940252.tar.gz
nitrokey-rs-ce4479b6f7a353f388aecda03cddc35389940252.tar.bz2
Merge branch 'get_config' into next
This patch series changes the Device::get_config implementation to use libnitrokey’s NK_get_status instead of NK_read_config. This does results in the same command being sent to the Nitrokey device, but avoids a new[]/free mismatch and makes the parsing more robust.
-rw-r--r--CHANGELOG.md2
-rw-r--r--src/config.rs14
-rw-r--r--src/device/mod.rs31
3 files changed, 22 insertions, 25 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f5bb7cb..c94e170 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,8 @@ SPDX-License-Identifier: CC0-1.0
_>` to `Result<u32, _>`.
- Change the type of the field `DeviceInfo.serial_number` from
`Option<String>` to `Option<u32>`.
+- Use `NK_get_status` instead of `NK_read_config` to implement the
+ `Device::get_config` function.
# v0.5.2 (2020-01-28)
- Use `CString` to store the temporary password instead of `Vec<u8>`.
diff --git a/src/config.rs b/src/config.rs
index cb678d7..120a51b 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -83,13 +83,13 @@ impl convert::TryFrom<Config> for RawConfig {
}
}
-impl From<[u8; 5]> for RawConfig {
- fn from(data: [u8; 5]) -> Self {
- RawConfig {
- numlock: data[0],
- capslock: data[1],
- scrollock: data[2],
- user_password: data[3] != 0,
+impl From<&nitrokey_sys::NK_status> for RawConfig {
+ fn from(status: &nitrokey_sys::NK_status) -> Self {
+ Self {
+ numlock: status.config_numlock,
+ capslock: status.config_capslock,
+ scrollock: status.config_scrolllock,
+ user_password: status.otp_user_password,
}
}
}
diff --git a/src/device/mod.rs b/src/device/mod.rs
index a25ad1b..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;
@@ -191,13 +189,7 @@ impl From<nitrokey_sys::NK_status> for Status {
minor: status.firmware_version_minor,
},
serial_number: status.serial_number_smart_card,
- config: RawConfig {
- numlock: status.config_numlock,
- capslock: status.config_capslock,
- scrollock: status.config_scrolllock,
- user_password: status.otp_user_password,
- }
- .into(),
+ config: RawConfig::from(&status).into(),
}
}
}
@@ -392,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.