diff --git a/src/util_pgp.rs b/src/util_pgp.rs new file mode 100644 index 0000000..6085ed0 --- /dev/null +++ b/src/util_pgp.rs @@ -0,0 +1,39 @@ +use openpgp_card::{OpenPgp, OpenPgpTransaction}; +use openpgp_card_pcsc::PcscBackend; +use rust_util::{failure, opt_result, opt_value_result, simple_error, success, warning, XResult}; + +use crate::util; + +pub fn read_and_verify_openpgp_pin(trans: &mut OpenPgpTransaction, pin: &Option) -> XResult<()> { + let pin = util::read_pin(pin); + if let Err(e) = trans.verify_pw1_user(pin.as_ref()) { + failure!("Verify user pin failed: {}", e); + return simple_error!("User pin verify failed: {}", e); + } + success!("User pin verify success!"); + Ok(()) +} + +pub fn get_openpgp() -> XResult { + let card = match get_card() { + Err(e) => { + failure!("Get PGP card failed: {}", e); + return simple_error!("Get card failed: {}", e); + } + Ok(card) => card + }; + Ok(OpenPgp::new(card)) +} + +pub fn get_card() -> XResult { + let card_list = opt_result!( + PcscBackend::cards(None), "Read OpenPGP card list failed: {}" + ); + if card_list.is_empty() { + return simple_error!("Cannot find any card"); + } + if card_list.len() > 1 { + warning!("Find {} OpenPGP cards, will use first card", card_list.len()); + } + Ok(opt_value_result!(card_list.into_iter().next(), "SHOULD NOT HAPPEN, CANNOT FIND ANY CARD")) +}