feat: update crates
This commit is contained in:
38
src/cmd_list.rs
Normal file
38
src/cmd_list.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
use rust_util::util_clap::{Command, CommandError};
|
||||
use yubikey::YubiKey;
|
||||
|
||||
pub struct CommandImpl;
|
||||
|
||||
impl Command for CommandImpl {
|
||||
fn name(&self) -> &str { "list" }
|
||||
|
||||
fn subcommand<'a>(&self) -> App<'a, 'a> {
|
||||
SubCommand::with_name(self.name()).about("YubiKey list")
|
||||
.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 yk = opt_result!(YubiKey::open(), "YubiKey not found: {}");
|
||||
|
||||
if json_output {
|
||||
let mut json = BTreeMap::<&'_ str, String>::new();
|
||||
json.insert("name", yk.name().to_string());
|
||||
json.insert("version", yk.version().to_string());
|
||||
json.insert("serial", yk.serial().0.to_string());
|
||||
|
||||
println!("{}", serde_json::to_string_pretty(&json).expect("Convert to JSON failed!"));
|
||||
} else {
|
||||
success!("Name: {}", yk.name());
|
||||
success!("Version: {}", yk.version());
|
||||
success!("Serial: {}", yk.serial().0);
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
@@ -24,8 +24,8 @@ impl Command for CommandImpl {
|
||||
let pin = opt_value_result!(pin_opt, "Pin must be assigned");
|
||||
if pin.len() < 8 { return simple_error!("Admin pin length:{}, must >= 8!", pin.len()); }
|
||||
|
||||
let mut card = crate::pgpcardutil::get_card()?;
|
||||
let mut pgp = OpenPgp::new(&mut card);
|
||||
let card = crate::pgpcardutil::get_card()?;
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
let mut trans = opt_result!(pgp.transaction(), "Open card failed: {}");
|
||||
|
||||
if sub_arg_matches.is_present("reset") {
|
||||
|
||||
@@ -38,8 +38,8 @@ impl Command for CommandImpl {
|
||||
return simple_error!("cipher or cipher-base64 must assign one");
|
||||
};
|
||||
|
||||
let mut card = crate::pgpcardutil::get_card()?;
|
||||
let mut pgp = OpenPgp::new(&mut card);
|
||||
let card = crate::pgpcardutil::get_card()?;
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
let mut trans = opt_result!(pgp.transaction(), "Open card failed: {}");
|
||||
|
||||
opt_result!(trans.verify_pw1_user(pin.as_ref()), "User pin verify failed: {}");
|
||||
|
||||
@@ -27,9 +27,9 @@ impl Command for CommandImpl {
|
||||
let cards = opt_result!(PcscBackend::cards(None), "Failed to list OpenPGP cards: {}");
|
||||
|
||||
information!("Found {} card(s)", cards.len());
|
||||
for (i, mut card) in cards.into_iter().enumerate() {
|
||||
for (i, card) in cards.into_iter().enumerate() {
|
||||
let mut json = BTreeMap::new();
|
||||
let mut pgp = OpenPgp::new(&mut card);
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
let mut trans = opt_result!(pgp.transaction(), "Open card failed: {}");
|
||||
if let Ok(application_related_data) = trans.application_related_data() {
|
||||
success!("Found card #{}: {:?}", i, application_related_data.application_id());
|
||||
|
||||
@@ -234,8 +234,8 @@ impl Command for CommandImpl {
|
||||
}
|
||||
|
||||
warning!("Force make is ON, try to write private keys to card!");
|
||||
let mut card = crate::pgpcardutil::get_card()?;
|
||||
let mut pgp = OpenPgp::new(&mut card);
|
||||
let card = crate::pgpcardutil::get_card()?;
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
let mut trans = opt_result!(pgp.transaction(), "Open card failed: {}");
|
||||
|
||||
opt_result!(trans.verify_pw3(pin.as_ref()), "Admin pin verify failed: {}");
|
||||
|
||||
@@ -78,8 +78,8 @@ impl Command for CommandImpl {
|
||||
return simple_error!("SHA256, SHA384 or SHA512 must assign at least one");
|
||||
}
|
||||
|
||||
let mut card = crate::pgpcardutil::get_card()?;
|
||||
let mut pgp = OpenPgp::new(&mut card);
|
||||
let card = crate::pgpcardutil::get_card()?;
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
let mut trans = opt_result!(pgp.transaction(), "Open card failed: {}");
|
||||
|
||||
if let Some(sha256) = sha256 {
|
||||
|
||||
@@ -6,7 +6,6 @@ use std::sync::Mutex;
|
||||
use clap::{App, Arg, ArgMatches, SubCommand};
|
||||
use openpgp_card::{KeyType, OpenPgp};
|
||||
use openpgp_card::crypto_data::{Hash, PublicKeyMaterial};
|
||||
use openpgp_card_pcsc::PcscBackend;
|
||||
use openssl::hash::MessageDigest;
|
||||
use rust_util::util_clap::{Command, CommandError};
|
||||
use rust_util::XResult;
|
||||
@@ -19,7 +18,7 @@ use crate::digest::{copy_sha256, copy_sha512};
|
||||
use crate::sshutil::{generate_ssh_string, with_sign};
|
||||
|
||||
struct SshAgent {
|
||||
card: Mutex<PcscBackend>,
|
||||
open_pgp: Mutex<OpenPgp>,
|
||||
use_sign: bool,
|
||||
pin: String,
|
||||
public_key: PublicKey,
|
||||
@@ -29,9 +28,9 @@ struct SshAgent {
|
||||
|
||||
impl SshAgent {
|
||||
fn new(pin: String, use_sign: bool) -> XResult<Self> {
|
||||
let mut card = crate::pgpcardutil::get_card()?;
|
||||
let (public_key, comment, ssh_string) = {
|
||||
let mut pgp = OpenPgp::new(&mut card);
|
||||
let card = crate::pgpcardutil::get_card()?;
|
||||
let (public_key, comment, ssh_string, open_pgp) = {
|
||||
let mut pgp = OpenPgp::new(card);
|
||||
let mut trans = opt_result!(pgp.transaction(), "Open card failed: {}");
|
||||
let serial = trans.application_related_data()
|
||||
.map(|d| d.application_id().map(|i| i.serial()))
|
||||
@@ -51,10 +50,11 @@ impl SshAgent {
|
||||
n: with_sign(n.to_vec()),
|
||||
});
|
||||
let comment = format!("pgp-card:{}:{}", iff!(use_sign, "sign", "auth"), serial);
|
||||
(public_key, comment.clone(), generate_ssh_string(e, n, &comment))
|
||||
drop(trans);
|
||||
(public_key, comment.clone(), generate_ssh_string(e, n, &comment), pgp)
|
||||
};
|
||||
Ok(Self {
|
||||
card: Mutex::new(card),
|
||||
open_pgp: Mutex::new(open_pgp),
|
||||
use_sign,
|
||||
pin,
|
||||
public_key,
|
||||
@@ -98,8 +98,8 @@ impl SshAgent {
|
||||
};
|
||||
|
||||
information!("SSH request, algorithm: {}", algorithm);
|
||||
let mut card_mut = self.card.lock().unwrap();
|
||||
let mut pgp = OpenPgp::new(&mut *card_mut);
|
||||
let mut pgp = self.open_pgp.lock().unwrap();
|
||||
// let mut pgp = OpenPgp::new(*card_mut);
|
||||
let mut trans = opt_result!(pgp.transaction(), "Open card failed: {}");
|
||||
let sig = if self.use_sign {
|
||||
debugging!("User pin verify for pw1 sign, use sign: {}", self.use_sign);
|
||||
|
||||
@@ -10,6 +10,7 @@ mod digest;
|
||||
mod rsautil;
|
||||
mod pkiutil;
|
||||
mod pgpcardutil;
|
||||
mod cmd_list;
|
||||
mod cmd_u2fregister;
|
||||
mod cmd_u2fsign;
|
||||
mod cmd_rsaencrypt;
|
||||
@@ -51,6 +52,7 @@ fn main() {
|
||||
|
||||
fn inner_main() -> CommandError {
|
||||
let commands: Vec<Box<dyn Command>> = vec![
|
||||
Box::new(cmd_list::CommandImpl),
|
||||
Box::new(cmd_chall::CommandImpl),
|
||||
Box::new(cmd_challconfig::CommandImpl),
|
||||
Box::new(cmd_rsaencrypt::CommandImpl),
|
||||
|
||||
Reference in New Issue
Block a user