chore: piv, pivsign

This commit is contained in:
2022-03-27 12:04:41 +08:00
parent e60feacf37
commit f996dc7570
2 changed files with 18 additions and 9 deletions

View File

@@ -29,8 +29,6 @@ impl Command for CommandImpl {
let cipher = sub_arg_matches.value_of("cipher"); let cipher = sub_arg_matches.value_of("cipher");
let cipher_base64 = sub_arg_matches.value_of("cipher-base64"); let cipher_base64 = sub_arg_matches.value_of("cipher-base64");
let mut json = BTreeMap::<&'_ str, String>::new();
let cipher_bytes = if let Some(cipher) = cipher { let cipher_bytes = if let Some(cipher) = cipher {
opt_result!(hex::decode(cipher), "Decode cipher failed: {}") opt_result!(hex::decode(cipher), "Decode cipher failed: {}")
} else if let Some(cipher_base64) = cipher_base64 { } else if let Some(cipher_base64) = cipher_base64 {
@@ -44,18 +42,16 @@ impl Command for CommandImpl {
success!("Clear text HEX: {}", hex::encode(&text)); success!("Clear text HEX: {}", hex::encode(&text));
success!("Clear text base64: {}", base64::encode(&text)); success!("Clear text base64: {}", base64::encode(&text));
success!("Clear text UTF-8: {}", String::from_utf8_lossy(&text)); success!("Clear text UTF-8: {}", String::from_utf8_lossy(&text));
if json_output { if json_output {
let mut json = BTreeMap::<&'_ str, String>::new();
json.insert("cipher_hex", hex::encode(&cipher_bytes)); json.insert("cipher_hex", hex::encode(&cipher_bytes));
json.insert("cipher_base64", base64::encode(&cipher_bytes)); json.insert("cipher_base64", base64::encode(&cipher_bytes));
json.insert("text_hex", hex::encode(&text)); json.insert("text_hex", hex::encode(&text));
json.insert("text_utf8", String::from_utf8_lossy(&text).to_string()); json.insert("text_utf8", String::from_utf8_lossy(&text).to_string());
}
if json_output {
println!("{}", serde_json::to_string_pretty(&json).unwrap()); println!("{}", serde_json::to_string_pretty(&json).unwrap());
} }
Ok(None) Ok(None)
} }
} }

View File

@@ -1,3 +1,4 @@
use std::collections::BTreeMap;
use clap::{App, Arg, ArgMatches, SubCommand}; use clap::{App, Arg, ArgMatches, SubCommand};
use rust_util::util_clap::{Command, CommandError}; use rust_util::util_clap::{Command, CommandError};
use yubikey::piv::{AlgorithmId, SlotId}; use yubikey::piv::{AlgorithmId, SlotId};
@@ -11,10 +12,14 @@ impl Command for CommandImpl {
fn subcommand<'a>(&self) -> App<'a, 'a> { fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("PIV Sign subcommand") SubCommand::with_name(self.name()).about("PIV Sign subcommand")
.arg(Arg::with_name("pass").short("p").long("pass").takes_value(true).default_value("123456").help("OpenPGP card password")) .arg(Arg::with_name("pass").short("p").long("pass").takes_value(true).default_value("123456").help("OpenPGP card password"))
// .arg(Arg::with_name("json").long("json").help("JSON 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 json_output = sub_arg_matches.is_present("json");
if json_output {
rust_util::util_msg::set_logger_std_out(false);
}
warning!("This feature is not complete"); warning!("This feature is not complete");
let pass_opt = sub_arg_matches.value_of("pass"); let pass_opt = sub_arg_matches.value_of("pass");
let pass = opt_value_result!(pass_opt, "Pass must be assigned"); let pass = opt_value_result!(pass_opt, "Pass must be assigned");
@@ -25,8 +30,16 @@ impl Command for CommandImpl {
let sign_result = yubikey::piv::sign_data(&mut yk, &raw_in, AlgorithmId::Rsa2048, SlotId::Signature); 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 = opt_result!(sign_result, "Sign data failed: {}");
let sign_bytes = sign.as_slice(); let sign_bytes = sign.as_slice();
success!("Signature HEX: {}", hex::encode(sign_bytes));
success!("Signature base64: {}", base64::encode(sign_bytes)); if json_output {
let mut json = BTreeMap::<&'_ str, String>::new();
json.insert("sign_hex", hex::encode(&sign_bytes));
json.insert("sign_base64", base64::encode(&sign_bytes));
println!("{}", serde_json::to_string_pretty(&json).unwrap());
} else {
success!("Signature HEX: {}", hex::encode(sign_bytes));
success!("Signature base64: {}", base64::encode(sign_bytes));
}
Ok(None) Ok(None)
} }
} }