use std::collections::BTreeMap; use clap::{App, Arg, ArgMatches, SubCommand}; use openpgp_card::{DecryptMe, Hash, KeyType, OpenPGPCard}; use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; impl Command for CommandImpl { fn name(&self) -> &str { "pgp-card-list" } fn subcommand<'a>(&self) -> App<'a, 'a> { SubCommand::with_name(self.name()).about("OpenPGP Card List subcommand") .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 mut json = BTreeMap::new(); match OpenPGPCard::list_cards() { Err(e) => return simple_error!("Failed to list OpenPGP cards: {}", e), Ok(list) => { information!("Found {} card(s)", list.len()); for (i, card) in list.iter().enumerate() { success!("Found card {}: {:?}", i, card.get_aid()); if let Ok(url) = card.get_url() { information!("URL: {}", iff!(url.is_empty(), "", &url)); } if let Ok(card_holder) = card.get_cardholder_related_data() { information!("Card holder: {:?}", card_holder); } if let Ok(supported_algo) = card.list_supported_algo() { information!("Supported algo: {:?}", supported_algo); } if let Ok(fingerprints) = card.get_fingerprints() { if let Some(a) = fingerprints.authentication() { if let Ok(algo) = card.get_algorithm_attributes(KeyType::Authentication) { information!("Authentication algo: {:?}", algo); } information!("Authentication fingerprint: {}", a); if json_output { json.insert("authentication_fingerprint", a.to_string()); } } if let Some(d) = fingerprints.decryption() { if let Ok(algo) = card.get_algorithm_attributes(KeyType::Decryption) { information!("Encryption algo: {:?}", algo); } information!("Encryption fingerprint: {}", d); if json_output { json.insert("encryption_fingerprint", d.to_string()); } } if let Some(s) = fingerprints.signature() { if let Ok(algo) = card.get_algorithm_attributes(KeyType::Signing) { information!("Signature algo: {:?}", algo); } information!("Signature fingerprint: {}", s); if json_output { json.insert("signature_fingerprint", s.to_string()); } } } } } } if json_output { println!("{}", serde_json::to_string_pretty(&json).unwrap()); } if let Ok(pass) = std::env::var("PASS") { if let Ok(list) = OpenPGPCard::list_cards() { // pw1_81 for signature // openssl dgst -sha256 -verify aa -signature sig LICENSE for card in list { match card.verify_pw1_81(&pass) { Result::Ok(user) => { let h = hex::decode("8f25018489d6fe0dec34a352314c38dc146247b7de65735790f4398a92afa84b").unwrap(); let mut hh = [0_u8; 0x20]; // for i in 0..hh.len() { // hh[i] = h[i]; // } hh.clone_from_slice(&h[..0x20]); let aa = user.signature_for_hash(Hash::SHA256(hh)); println!("////// {}", hex::encode(&aa.unwrap())); } Result::Err(_) => { failure!("error!"); } } } } if let Ok(list) = OpenPGPCard::list_cards() { // pw1_82 for encryption // PKCSv1.5 for card in list { match card.verify_pw1_82(&pass) { Result::Ok(user) => { let e = hex::decode("705d2af390174706c42de8361c4cb7a4684e06216305d6da70cab9248fa1abb9fac2b4c004187a53b9ed2b7f8ab75b20a54a12e6be672a742504b38d07b192f1f815285b6605b06a1a86d97f14ffcf41a9b3463ff3d8b2afe63e53aeb43048fef358639e5589fab8000d0805fe9145b5457cb5f7f91243b25181d1c7853b384a3bba45143a5ceeb2612463e51fbce52154e20726e6cf58f0317ebdf92613a10aec2b34224b0ffd1333c475cab473cf66aa895700e6681d20d12ddc897e0731226d4dc6bc9bd2e4aca02dba49175a35e45f9f3b56973a0381c777dac73a7eb8a04bfde45d3c19f44f740a193439da6829bf4beba90529c3017bddbd64aacbad92d3632260d5b6a7b5565fd852f17cff031fbdc2700d6acc2dedaaad307aaf7bb2de8f2b02faf84d94fa046e4ce487480918a54ca4672e1388deae8881436f454e65263a4984003495031126b4488ab27c6f4555f16bdd0fe2b9132443bbf49aa9c19864c4b1b917914e9c070a0327e52cf3b536e218a56ec97e12979e5981977e008eb9180d02da322c1a019fa535d77b67912039bc699b291d950714367d1c4a4a41edc86b1ab99d101da75a61c762fee28f8a004fb7656800623ac95d88c1978f18a1d286916cdab9c9f0f91d500cffdc8cb2853011a352bfa5f1fcd4a59720004153fb644afef083909c6a537ebc24255c1284ecf665a8cb7212672051e8b4").unwrap(); println!(":::: {}", e.len()); let x = user.decrypt(DecryptMe::RSA(&e)); println!(">>>>>> {:?}", x); } Result::Err(_) => { failure!("error!"); } } } } } Ok(None) } }