feat: piv

This commit is contained in:
2021-07-18 11:43:10 +08:00
parent 1602edf736
commit a2a8474913

View File

@@ -15,11 +15,14 @@ impl Command for CommandImpl {
fn name(&self) -> &str { "piv" } fn name(&self) -> &str { "piv" }
fn subcommand<'a>(&self) -> App<'a, 'a> { fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("OpenPGP Card List subcommand") SubCommand::with_name(self.name()).about("PIV subcommand")
.arg(Arg::with_name("json").long("json").help("JSON output")) .arg(Arg::with_name("detail").long("detail").help("Detail output"))
.arg(Arg::with_name("show-config").long("show-config").help("Show config output"))
// .arg(Arg::with_name("json").long("json").help("JSON output"))
} }
fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError { fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
let detail_output = sub_arg_matches.is_present("detail");
let mut yk = YubiKey::open()?; let mut yk = YubiKey::open()?;
success!("Name: {}", yk.name()); success!("Name: {}", yk.name());
success!("Version: {}", yk.version()); success!("Version: {}", yk.version());
@@ -36,14 +39,19 @@ impl Command for CommandImpl {
Ok(pin_retries) => success!("PIN retries: {}",pin_retries), Ok(pin_retries) => success!("PIN retries: {}",pin_retries),
Err(e) => warning!("PIN retries: <none> {}", e), Err(e) => warning!("PIN retries: <none> {}", e),
} }
if sub_arg_matches.is_present("show-config") {
let cofnig = yk.config();
information!("Config: {:#?}", cofnig);
}
for slot in yubikey::piv::SLOTS.iter().cloned() { for slot in yubikey::piv::SLOTS.iter().cloned() {
print_cert_info(&mut yk, slot).ok(); print_cert_info(&mut yk, slot, detail_output).ok();
} }
Ok(()) Ok(())
} }
} }
fn print_cert_info(yubikey: &mut YubiKey, slot: SlotId) -> XResult<()> { fn print_cert_info(yubikey: &mut YubiKey, slot: SlotId, detail_output: bool) -> XResult<()> {
let cert = match Certificate::read(yubikey, slot) { let cert = match Certificate::read(yubikey, slot) {
Ok(c) => c, Ok(c) => c,
Err(e) => { Err(e) => {
@@ -55,18 +63,23 @@ fn print_cert_info(yubikey: &mut YubiKey, slot: SlotId) -> XResult<()> {
let buf = cert.into_buffer(); let buf = cert.into_buffer();
if !buf.is_empty() { if !buf.is_empty() {
information!("{}", "-".repeat(88));
let fingerprint_sha256 = Sha256::digest(&buf); let fingerprint_sha256 = Sha256::digest(&buf);
let slot_id: u8 = slot.into(); let slot_id: u8 = slot.into();
success!("Slot: {:?}, id: {:x}", slot, slot_id); success!("Slot: {:?}, id: {:x}", slot, slot_id);
rust_util::util_msg::when(MessageType::DEBUG, || { let cert_pem_obj = Pem {
let cert_pem_obj = Pem { tag: String::from("CERTIFICATE"),
tag: String::from("CERTIFICATE"), contents: buf.to_vec(),
contents: buf.to_vec(), };
}; if detail_output {
debugging!("{}", pem::encode(&cert_pem_obj).trim()); information!("{}", pem::encode(&cert_pem_obj).trim());
}); } else {
rust_util::util_msg::when(MessageType::DEBUG, || {
debugging!("{}", pem::encode(&cert_pem_obj).trim());
});
}
match parse_x509_certificate(&buf) { match parse_x509_certificate(&buf) {
Ok((_rem, cert)) => { Ok((_rem, cert)) => {