From c8dc5703abb4b1ac47fc000e725a0709e9fd1ec1 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 3 Apr 2022 17:03:31 +0800 Subject: [PATCH] feat: v1.1.2 make clippy happy --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cmd_chall.rs | 6 ++--- src/cmd_pgpcardadmin.rs | 4 +-- src/cmd_pgpcarddecrypt.rs | 2 +- src/cmd_pgpcardlist.rs | 56 +++++++++++++++++++++++++++++++++++++++ src/cmd_pgpcardsign.rs | 2 +- src/cmd_pivsign.rs | 2 +- 8 files changed, 66 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 86d5db3..920a28e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -336,7 +336,7 @@ dependencies = [ [[package]] name = "card-cli" -version = "1.1.1" +version = "1.1.2" dependencies = [ "authenticator", "base64 0.13.0", diff --git a/Cargo.toml b/Cargo.toml index 7964bab..00b1dff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "card-cli" -version = "1.1.1" +version = "1.1.2" authors = ["Hatter Jiang "] edition = "2018" diff --git a/src/cmd_chall.rs b/src/cmd_chall.rs index 0277ec4..4564f67 100644 --- a/src/cmd_chall.rs +++ b/src/cmd_chall.rs @@ -79,9 +79,9 @@ impl Command for CommandImpl { } else { success!("Challenge HEX: {}", hex::encode(challenge_bytes)); success!("Response HEX: {}", hex_string); - hex_sha256.map(|hex_sha256| success!("Response SHA256 HEX: {}", hex::encode(hex_sha256))); - hex_sha384.map(|hex_sha384| success!("Response SHA384 HEX: {}", hex::encode(hex_sha384))); - hex_sha512.map(|hex_sha512| success!("Response SHA512 HEX: {}", hex::encode(hex_sha512))); + if let Some(hex_sha256) = hex_sha256 { success!("Response SHA256 HEX: {}", hex::encode(hex_sha256)); } + if let Some(hex_sha384) = hex_sha384 { success!("Response SHA384 HEX: {}", hex::encode(hex_sha384)); } + if let Some(hex_sha512) = hex_sha512 { success!("Response SHA512 HEX: {}", hex::encode(hex_sha512)); } } } else { warning!("YubiKey not found"); diff --git a/src/cmd_pgpcardadmin.rs b/src/cmd_pgpcardadmin.rs index 4b76b0e..f2be6cf 100644 --- a/src/cmd_pgpcardadmin.rs +++ b/src/cmd_pgpcardadmin.rs @@ -19,7 +19,7 @@ impl Command for CommandImpl { } fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { - let pin_opt = sub_arg_matches.value_of("pass").or(sub_arg_matches.value_of("pin")); + let pin_opt = sub_arg_matches.value_of("pass").or_else(|| sub_arg_matches.value_of("pin")); let pin = opt_value_result!(pin_opt, "Pass must be assigned"); if pin.len() < 8 { return simple_error!("Admin pin length:{}, must >= 8!", pin.len()); } @@ -45,7 +45,7 @@ impl Command for CommandImpl { if let Some(lang) = sub_arg_matches.value_of("lang") { information!("Set lang to: {}", lang); let lang_bytes = lang.as_bytes(); - opt_result!(trans.set_lang(&vec![Lang::Value([lang_bytes[0], lang_bytes[1]])]), "Set lang failed: {}"); + opt_result!(trans.set_lang(&[Lang::Value([lang_bytes[0], lang_bytes[1]])]), "Set lang failed: {}"); success!("Set lang success"); } diff --git a/src/cmd_pgpcarddecrypt.rs b/src/cmd_pgpcarddecrypt.rs index 51b76bc..2d6c2eb 100644 --- a/src/cmd_pgpcarddecrypt.rs +++ b/src/cmd_pgpcarddecrypt.rs @@ -23,7 +23,7 @@ impl Command for CommandImpl { let json_output = sub_arg_matches.is_present("json"); if json_output { rust_util::util_msg::set_logger_std_out(false); } - let pin_opt = sub_arg_matches.value_of("pass").or(sub_arg_matches.value_of("pin")); + let pin_opt = sub_arg_matches.value_of("pass").or_else(|| sub_arg_matches.value_of("pin")); let pin = opt_value_result!(pin_opt, "User pin must be assigned"); if pin.len() < 6 { return simple_error!("User pin length:{}, must >= 6!", pin.len()); } diff --git a/src/cmd_pgpcardlist.rs b/src/cmd_pgpcardlist.rs index 1969e34..9ae8233 100644 --- a/src/cmd_pgpcardlist.rs +++ b/src/cmd_pgpcardlist.rs @@ -2,7 +2,11 @@ use std::collections::BTreeMap; use clap::{App, Arg, ArgMatches, SubCommand}; use openpgp_card::{KeyType, OpenPgp}; +use openpgp_card::crypto_data::PublicKeyMaterial; use openpgp_card_pcsc::PcscBackend; +use openssl::bn::BigNum; +use openssl::rsa::Rsa; +use pem::Pem; use rust_util::util_clap::{Command, CommandError}; pub struct CommandImpl; @@ -12,10 +16,12 @@ impl Command for CommandImpl { fn subcommand<'a>(&self) -> App<'a, 'a> { SubCommand::with_name(self.name()).about("OpenPGP Card List subcommand") + .arg(Arg::with_name("detail").long("detail").help("Detail output")) .arg(Arg::with_name("json").long("json").help("JSON output")) } fn run(&self, _arg_matches: &ArgMatches, sub_arg_matches: &ArgMatches) -> CommandError { + let detail_output = sub_arg_matches.is_present("detail"); let json_output = sub_arg_matches.is_present("json"); if json_output { rust_util::util_msg::set_logger_std_out(false); } @@ -99,6 +105,16 @@ impl Command for CommandImpl { if json_output { json.insert("authentication_fingerprint", a.to_string()); } + if detail_output { + if let Ok(public_key) = trans.public_key(KeyType::Authentication) { + if let Some(public_key_pem) = public_key_pem(&public_key) { + information!("Authentication public key: {}", public_key_pem.trim()); + if json_output { + json.insert("authentication_public_key_pem", public_key_pem); + } + } + } + } } if let Some(d) = fingerprints.decryption() { if let Ok(algo) = application_related_data.algorithm_attributes(KeyType::Decryption) { @@ -108,6 +124,16 @@ impl Command for CommandImpl { if json_output { json.insert("encryption_fingerprint", d.to_string()); } + if detail_output { + if let Ok(public_key) = trans.public_key(KeyType::Decryption) { + if let Some(public_key_pem) = public_key_pem(&public_key) { + information!("Encryption public key: {}", public_key_pem.trim()); + if json_output { + json.insert("encryption_public_key_pem", public_key_pem); + } + } + } + } } if let Some(s) = fingerprints.signature() { if let Ok(algo) = application_related_data.algorithm_attributes(KeyType::Signing) { @@ -117,6 +143,16 @@ impl Command for CommandImpl { if json_output { json.insert("signature_fingerprint", s.to_string()); } + if detail_output { + if let Ok(public_key) = trans.public_key(KeyType::Signing) { + if let Some(public_key_pem) = public_key_pem(&public_key) { + information!("Signature public key: {}", public_key_pem.trim()); + if json_output { + json.insert("signature_public_key_pem", public_key_pem); + } + } + } + } } } } @@ -129,3 +165,23 @@ impl Command for CommandImpl { Ok(None) } } + +fn public_key_pem(public_key: &PublicKeyMaterial) -> Option { + match public_key { + PublicKeyMaterial::R(rsa_pub) => { + let rsa_pub_key = Rsa::from_public_components( + BigNum::from_slice(rsa_pub.n()).unwrap(), + BigNum::from_slice(rsa_pub.v()).unwrap(), + ); + let pub_key_pem_obj = Pem { + tag: String::from("PUBLIC KEY"), + contents: rsa_pub_key.unwrap().public_key_to_der().unwrap(), + }; + Some(pem::encode(&pub_key_pem_obj)) + } + _ => { + warning!("Not RSA public key: {:?}", public_key); + None + } + } +} \ No newline at end of file diff --git a/src/cmd_pgpcardsign.rs b/src/cmd_pgpcardsign.rs index ec13c8a..3628cea 100644 --- a/src/cmd_pgpcardsign.rs +++ b/src/cmd_pgpcardsign.rs @@ -35,7 +35,7 @@ impl Command for CommandImpl { let json_output = sub_arg_matches.is_present("json"); if json_output { rust_util::util_msg::set_logger_std_out(false); } - let pin_opt = sub_arg_matches.value_of("pass").or(sub_arg_matches.value_of("pin")); + let pin_opt = sub_arg_matches.value_of("pass").or_else(|| sub_arg_matches.value_of("pin")); let pin = opt_value_result!(pin_opt, "User pin must be assigned"); if pin.len() < 6 { return simple_error!("User pin length:{}, must >= 6!", pin.len()); } diff --git a/src/cmd_pivsign.rs b/src/cmd_pivsign.rs index 9aff537..276e2e6 100644 --- a/src/cmd_pivsign.rs +++ b/src/cmd_pivsign.rs @@ -23,7 +23,7 @@ impl Command for CommandImpl { rust_util::util_msg::set_logger_std_out(false); } warning!("This feature is not complete"); - let pin_opt = sub_arg_matches.value_of("pass").or(sub_arg_matches.value_of("pin")); + let pin_opt = sub_arg_matches.value_of("pass").or_else(|| sub_arg_matches.value_of("pin")); let pin = opt_value_result!(pin_opt, "User pin must be assigned"); let mut yk = opt_result!(YubiKey::open(), "YubiKey not found: {}");