This commit is contained in:
Grimmauld 2025-02-06 12:03:02 +01:00
parent ec1d1bba0e
commit 5c01623394
No known key found for this signature in database
2 changed files with 72 additions and 81 deletions

View file

@ -1,13 +1,11 @@
/// Utilities for interacting with YubiKey OATH/TOTP functionality
extern crate pcsc;
extern crate byteorder;
/// Utilities for interacting with YubiKey OATH/TOTP functionality
extern crate pcsc;
use std::ffi::{CString};
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use std::ffi::CString;
use std::io::{Cursor, Read, Write};
use std::time::{SystemTime};
use std::time::SystemTime;
pub type DetectResult<'a> = Result<Vec<YubiKey<'a>>, pcsc::Error>;
@ -39,14 +37,14 @@ pub fn format_code(code: u32, digits: OathDigits) -> String {
} else {
code_string.split_off(code_string.len() - 6)
}
},
}
OathDigits::Eight => {
if code_string.len() <= 8 {
format!("{:0>8}", code_string)
} else {
code_string.split_off(code_string.len() - 8)
}
},
}
}
}
@ -54,33 +52,21 @@ fn to_error_response(sw1: u8, sw2: u8) -> Option<String> {
let code: usize = (sw1 as usize | sw2 as usize) << 8;
match code {
code if code == ErrorResponse::GenericError as usize => {
Some(String::from("Generic error"))
},
code if code == ErrorResponse::NoSpace as usize => {
Some(String::from("No space on device"))
},
code if code == ErrorResponse::GenericError as usize => Some(String::from("Generic error")),
code if code == ErrorResponse::NoSpace as usize => Some(String::from("No space on device")),
code if code == ErrorResponse::CommandAborted as usize => {
Some(String::from("Command was aborted"))
},
}
code if code == ErrorResponse::AuthRequired as usize => {
Some(String::from("Authentication required"))
},
code if code == ErrorResponse::WrongSyntax as usize => {
Some(String::from("Wrong syntax"))
},
}
code if code == ErrorResponse::WrongSyntax as usize => Some(String::from("Wrong syntax")),
code if code == ErrorResponse::InvalidInstruction as usize => {
Some(String::from("Invalid instruction"))
},
code if code == SuccessResponse::Okay as usize => {
None
},
sw1 if sw1 == SuccessResponse::MoreData as usize => {
None
},
_ => {
Some(String::from("Unknown error"))
},
}
code if code == SuccessResponse::Okay as usize => None,
sw1 if sw1 == SuccessResponse::MoreData as usize => None,
_ => Some(String::from("Unknown error")),
}
}
@ -235,7 +221,7 @@ impl<'a> YubiKey<'a> {
let mut card = match ctx.connect(
&CString::new(self.name).unwrap(),
pcsc::ShareMode::Shared,
pcsc::Protocols::ANY
pcsc::Protocols::ANY,
) {
Ok(card) => card,
Err(err) => return Err(format!("{}", err)),
@ -256,9 +242,17 @@ impl<'a> YubiKey<'a> {
let mut response_buf = Vec::new();
// Request OATH codes from device
let response = self.apdu(&tx, 0, Instruction::CalculateAll as u8, 0,
0x01, Some(&to_tlv(Tag::Challenge,
&time_challenge(Some(SystemTime::now())))));
let response = self.apdu(
&tx,
0,
Instruction::CalculateAll as u8,
0,
0x01,
Some(&to_tlv(
Tag::Challenge,
&time_challenge(Some(SystemTime::now())),
)),
);
// Handle errors from command
match response {
@ -275,10 +269,10 @@ impl<'a> YubiKey<'a> {
sw1 = more_resp.sw1;
sw2 = more_resp.sw2;
response_buf.extend(more_resp.buf);
},
}
Err(e) => {
return Err(format!("{}", e));
},
}
}
}
@ -287,7 +281,7 @@ impl<'a> YubiKey<'a> {
}
return Ok(self.parse_list(&response_buf).unwrap());
},
}
Err(e) => {
return Err(format!("{}", e));
}
@ -352,7 +346,7 @@ impl<'a> YubiKey<'a> {
results.push(OathCredential::new(
&String::from_utf8(name).unwrap(),
OathCode { digits, value }
OathCode { digits, value },
));
}
@ -367,7 +361,7 @@ impl<'a> YubiKey<'a> {
instruction: u8,
parameter1: u8,
parameter2: u8,
data: Option<&[u8]>
data: Option<&[u8]>,
) -> Result<ApduResponse, pcsc::Error> {
// Create a container for the transaction payload
let mut tx_buf = Vec::new();
@ -443,4 +437,3 @@ impl<'a> YubiKey<'a> {
})
}
}

View file

@ -41,7 +41,7 @@ fn main() {
Err(e) => {
println!("ERROR {}", e);
continue;
},
}
};
// Show message is node codes found
@ -54,9 +54,7 @@ fn main() {
let code = lib_ykoath::format_code(oath.code.value, oath.code.digits);
let name_clone = oath.name.clone();
let mut label_vec: Vec<&str> = name_clone.split(":").collect();
let mut code_entry_label: String = String::from(
label_vec.remove(0)
);
let mut code_entry_label: String = String::from(label_vec.remove(0));
if label_vec.len() > 0 {
code_entry_label.push_str(" (");