chore: reorg code
This commit is contained in:
108
src/cmd_pgpcardsign.rs
Normal file
108
src/cmd_pgpcardsign.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
use std::collections::BTreeMap;
|
||||
use clap::{ArgMatches, SubCommand, App, Arg};
|
||||
use openpgp_card::Hash;
|
||||
use rust_util::XResult;
|
||||
use rust_util::util_clap::{Command, CommandError};
|
||||
|
||||
pub struct CommandImpl;
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> &str { "pgp-card-sign" }
|
||||
|
||||
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
||||
SubCommand::with_name(self.name()).about("OpenPGP Card 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("sha256").short("2").long("sha256").takes_value(true).help("Digest SHA256 HEX"))
|
||||
.arg(Arg::with_name("sha384").short("3").long("sha384").takes_value(true).help("Digest SHA384 HEX"))
|
||||
.arg(Arg::with_name("sha512").short("5").long("sha512").takes_value(true).help("Digest SHA512 HEX"))
|
||||
.arg(Arg::with_name("json").long("json").help("JSON output"))
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
let pass = sub_arg_matches.value_of("pass");
|
||||
let pass = match pass {
|
||||
Some(p) => p,
|
||||
None => return simple_error!("Pass must be assigned"),
|
||||
};
|
||||
let sha256 = sub_arg_matches.value_of("sha256");
|
||||
let sha384 = sub_arg_matches.value_of("sha384");
|
||||
let sha512 = sub_arg_matches.value_of("sha512");
|
||||
|
||||
if sha256.is_none() && sha384.is_none() && sha512.is_none() {
|
||||
return simple_error!("SHA256, SHA384 or SHA512 must assign one");
|
||||
}
|
||||
|
||||
let mut json = BTreeMap::new();
|
||||
if let Some(sha256) = sha256 {
|
||||
let user = crate::pgpcardutil::get_card_user_sw1_81(pass)?;
|
||||
let sha256_hex = opt_result!(hex::decode(sha256.trim()), "Decode sha256 failed: {}");
|
||||
let sha256_hex = copy_sha256(&sha256_hex)?;
|
||||
let sig = user.signature_for_hash(Hash::SHA256(sha256_hex))?;
|
||||
success!("SHA256 signature HEX: {}", hex::encode(&sig));
|
||||
success!("SHA256 signature base64: {}", base64::encode(&sig));
|
||||
if json_output {
|
||||
let mut entry = BTreeMap::new();
|
||||
entry.insert("digest", hex::encode(&sha256_hex));
|
||||
entry.insert("signature", hex::encode(&sig));
|
||||
json.insert("sha256", entry);
|
||||
}
|
||||
}
|
||||
if let Some(sha384) = sha384 {
|
||||
let user = crate::pgpcardutil::get_card_user_sw1_81(pass)?;
|
||||
let sha384_hex = opt_result!(hex::decode(sha384.trim()), "Decode sha384 failed: {}");
|
||||
let sha384_hex = copy_sha384(&sha384_hex)?;
|
||||
let sig = user.signature_for_hash(Hash::SHA384(sha384_hex))?;
|
||||
success!("SHA384 signature HEX: {}", hex::encode(&sig));
|
||||
success!("SHA384 signature base64: {}", base64::encode(&sig));
|
||||
if json_output {
|
||||
let mut entry = BTreeMap::new();
|
||||
entry.insert("digest", hex::encode(&sha384_hex));
|
||||
entry.insert("signature", hex::encode(&sig));
|
||||
json.insert("sha384", entry);
|
||||
}
|
||||
}
|
||||
if let Some(sha512) = sha512 {
|
||||
let user = crate::pgpcardutil::get_card_user_sw1_81(pass)?;
|
||||
let sha512_hex = opt_result!(hex::decode(sha512.trim()), "Decode sha512 failed: {}");
|
||||
let sha512_hex = copy_sha512(&sha512_hex)?;
|
||||
let sig = user.signature_for_hash(Hash::SHA512(sha512_hex))?;
|
||||
success!("SHA512 signature HEX: {}", hex::encode(&sig));
|
||||
success!("SHA512 signature base64: {}", base64::encode(&sig));
|
||||
if json_output {
|
||||
let mut entry = BTreeMap::new();
|
||||
entry.insert("digest", hex::encode(&sha512_hex));
|
||||
entry.insert("signature", hex::encode(&sig));
|
||||
json.insert("sha512", entry);
|
||||
}
|
||||
}
|
||||
|
||||
if json_output {
|
||||
println!("{}", serde_json::to_string_pretty(&json).unwrap());
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! define_copy_array {
|
||||
($fn_name: ident, $len: tt) => (
|
||||
fn $fn_name(in_arr: &[u8]) -> XResult<[u8; $len]> {
|
||||
if in_arr.len() != $len {
|
||||
return simple_error!("Array length is not: {}, but is: {}", $len, in_arr.len());
|
||||
}
|
||||
let mut out_arr = [0_u8; $len];
|
||||
for i in 0..$len {
|
||||
out_arr[i] = in_arr[i];
|
||||
}
|
||||
Ok(out_arr)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
define_copy_array!(copy_sha256, 0x20);
|
||||
define_copy_array!(copy_sha384, 0x30);
|
||||
define_copy_array!(copy_sha512, 0x40);
|
||||
Reference in New Issue
Block a user