feat: piv

This commit is contained in:
2022-03-27 11:55:41 +08:00
parent 5578b57036
commit e60feacf37
2 changed files with 13 additions and 20 deletions

View File

@@ -25,7 +25,7 @@ impl Command for CommandImpl {
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 = opt_result!(YubiKey::open(), "YubiKey not found: {}");
success!("Name: {}", yk.name());
success!("Version: {}", yk.version());
success!("Serial: {}", yk.serial());

View File

@@ -1,7 +1,7 @@
use clap::{ArgMatches, SubCommand, App, Arg};
use yubikey::YubiKey;
use yubikey::piv::{SlotId, AlgorithmId};
use clap::{App, Arg, ArgMatches, SubCommand};
use rust_util::util_clap::{Command, CommandError};
use yubikey::piv::{AlgorithmId, SlotId};
use yubikey::YubiKey;
pub struct CommandImpl;
@@ -16,24 +16,17 @@ impl Command for CommandImpl {
fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError {
warning!("This feature is not complete");
let pass = sub_arg_matches.value_of("pass");
let pass = match pass {
Some(p) => p,
None => return simple_error!("Pass must be assigned"),
};
let pass_opt = sub_arg_matches.value_of("pass");
let pass = opt_value_result!(pass_opt, "Pass must be assigned");
let mut yk = YubiKey::open()?;
yk.verify_pin(pass.as_bytes())?;
let mut yk = opt_result!(YubiKey::open(), "YubiKey not found: {}");
opt_result!(yk.verify_pin(pass.as_bytes()), "YubiKey verify pin failed: {}");
let raw_in = [1_u8; 256];
let sign = yubikey::piv::sign_data(&mut yk, &raw_in, AlgorithmId::Rsa2048, SlotId::Signature);
match sign {
Ok(sign) => {
let sign_bytes = sign.as_slice();
success!("Signature HEX: {}", hex::encode(sign_bytes));
success!("Signature base64: {}", base64::encode(sign_bytes));
}
Err(e) => return simple_error!("Sign data failed: {}", e),
}
let sign_result = yubikey::piv::sign_data(&mut yk, &raw_in, AlgorithmId::Rsa2048, SlotId::Signature);
let sign = opt_result!(sign_result, "Sign data failed: {}");
let sign_bytes = sign.as_slice();
success!("Signature HEX: {}", hex::encode(sign_bytes));
success!("Signature base64: {}", base64::encode(sign_bytes));
Ok(None)
}
}