fn _ykpiv_get_version() -> Result<Version, ErrorKind>

This commit is contained in:
Jack Grigg
2019-11-20 11:44:13 +00:00
parent 90bdda85cb
commit 31ef465571
+7 -40
View File
@@ -268,8 +268,7 @@ pub(crate) unsafe fn _ykpiv_select_application(state: &mut YubiKey) -> Result<()
// can determine how to get the serial number, which for the NEO/Yk4 // can determine how to get the serial number, which for the NEO/Yk4
// will result in another selection of the PIV applet. // will result in another selection of the PIV applet.
res = _ykpiv_get_version(state, ptr::null_mut()); if let Err(e) = _ykpiv_get_version(state) {
if let Err(e) = res.as_ref() {
if state.verbose != 0 { if state.verbose != 0 {
eprintln!("Failed to retrieve version: \'{}\'", e); eprintln!("Failed to retrieve version: \'{}\'", e);
} }
@@ -1248,27 +1247,14 @@ pub unsafe fn ykpiv_decrypt_data(
} }
/// Get the version of the PIV application installed on the YubiKey /// Get the version of the PIV application installed on the YubiKey
pub(crate) unsafe fn _ykpiv_get_version( pub(crate) unsafe fn _ykpiv_get_version(state: &mut YubiKey) -> Result<Version, ErrorKind> {
state: &mut YubiKey,
p_version: *mut Version,
) -> Result<(), ErrorKind> {
let mut data = [0u8; 261]; let mut data = [0u8; 261];
let mut recv_len = data.len() as u32; let mut recv_len = data.len() as u32;
let mut sw: i32 = 0; let mut sw: i32 = 0;
// get version from state if already from device // get version from state if already from device
if state.ver.major != 0 || state.ver.minor != 0 || state.ver.patch != 0 { if state.ver.major != 0 || state.ver.minor != 0 || state.ver.patch != 0 {
if !p_version.is_null() { return Ok(state.ver);
// TODO(tarcieri): use real references instead of pointers and memcpy!!!
#[allow(trivial_casts)]
memcpy(
p_version as *mut c_void,
&state.ver as *const Version as *const c_void,
mem::size_of::<Version>(),
);
}
return Ok(());
} }
// get version from device // get version from device
@@ -1289,40 +1275,21 @@ pub(crate) unsafe fn _ykpiv_get_version(
state.ver.minor = data[1]; state.ver.minor = data[1];
state.ver.patch = data[2]; state.ver.patch = data[2];
if !p_version.is_null() { Ok(state.ver)
// TODO(tarcieri): use real references instead of pointers and memcpy!!!
#[allow(trivial_casts)]
memcpy(
p_version as *mut c_void,
&state.ver as (*const Version) as (*const c_void),
mem::size_of::<Version>(),
);
}
Ok(())
} }
/// Get the YubiKey's PIV application version as a string /// Get the YubiKey's PIV application version as a string
pub unsafe fn ykpiv_get_version(state: &mut YubiKey) -> Result<String, ErrorKind> { pub unsafe fn ykpiv_get_version(state: &mut YubiKey) -> Result<String, ErrorKind> {
let mut ver: Version = Version { let mut res = Err(ErrorKind::GenericError);
major: 0u8,
minor: 0u8,
patch: 0u8,
};
_ykpiv_begin_transaction(state)?; _ykpiv_begin_transaction(state)?;
if _ykpiv_ensure_application_selected(state).is_ok() { if _ykpiv_ensure_application_selected(state).is_ok() {
let res = _ykpiv_get_version(state, &mut ver); res = _ykpiv_get_version(state);
if res.is_ok() {
_ykpiv_end_transaction(state);
return Ok(format!("{}.{}.{}", ver.major, ver.minor, ver.patch));
}
} }
_ykpiv_end_transaction(state); _ykpiv_end_transaction(state);
Err(ErrorKind::GenericError) res.map(|ver| format!("{}.{}.{}", ver.major, ver.minor, ver.patch))
} }
/// Get YubiKey device serial number /// Get YubiKey device serial number