feat: v1.5.2, update crates, improve piv-ecsign

This commit is contained in:
2023-04-21 00:01:53 +08:00
parent e08282870e
commit a2e545e89a
3 changed files with 268 additions and 162 deletions

View File

@@ -7,6 +7,7 @@ use rust_util::util_msg;
use x509_parser::nom::AsBytes;
use yubikey::YubiKey;
use yubikey::piv::{AlgorithmId, RetiredSlotId, sign_data, SlotId};
use crate::digest::sha256;
pub struct CommandImpl;
@@ -18,6 +19,7 @@ impl Command for CommandImpl {
.arg(Arg::with_name("pin").short("p").long("pin").takes_value(true).help("PIV card user pin"))
.arg(Arg::with_name("slot").short("s").long("slot").takes_value(true).help("PIV slot, e.g. 82, 83 ..."))
.arg(Arg::with_name("hash-hex").long("hash-hex").takes_value(true).help("Hash"))
.arg(Arg::with_name("input").long("input").takes_value(true).help("Input"))
.arg(Arg::with_name("json").long("json").help("JSON output"))
}
@@ -30,7 +32,11 @@ impl Command for CommandImpl {
let pin_opt = sub_arg_matches.value_of("pin");
let slot = opt_value_result!(sub_arg_matches.value_of("slot"), "--slot must assigned, e.g. 82, 83 ...");
let hash_hex = opt_value_result!(sub_arg_matches.value_of("hash-hex"), "--hash-hex must assigned");
let hash_hex = if let Some(input) = sub_arg_matches.value_of("input") {
hex::encode(sha256(input))
} else {
opt_value_result!(sub_arg_matches.value_of("hash-hex"), "--hash-hex must assigned").to_string()
};
let mut yk = opt_result!(YubiKey::open(), "YubiKey not found: {}");
let retired_slot_id = opt_result!(RetiredSlotId::from_str(slot), "Slot not found: {}");
@@ -41,7 +47,7 @@ impl Command for CommandImpl {
opt_result!(yk.verify_pin(pin.as_bytes()), "YubiKey verify pin failed: {}");
}
let hash_bytes = opt_result!(hex::decode(hash_hex), "Parse epk failed: {}");
let hash_bytes = opt_result!(hex::decode(&hash_hex), "Parse epk failed: {}");
let signed_data = opt_result!(sign_data(&mut yk, &hash_bytes, AlgorithmId::EccP256, slot_id), "Sign piv failed: {}");