diff --git a/src/main.rs b/src/main.rs index a175ce2..de1088b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ mod digest; mod register; mod sign; mod pgp; +mod pgpcardutil; mod pgpcardlist; mod pgpcardsign; diff --git a/src/pgpcardsign.rs b/src/pgpcardsign.rs index 1a53e72..2c8721e 100644 --- a/src/pgpcardsign.rs +++ b/src/pgpcardsign.rs @@ -1,6 +1,6 @@ use clap::{ArgMatches, SubCommand, App, Arg}; use crate::cmd::{Command, CommandError}; -use openpgp_card::{OpenPGPCard, Hash, OpenPGPCardUser}; +use openpgp_card::Hash; use rust_util::XResult; use std::collections::BTreeMap; @@ -38,7 +38,7 @@ impl Command for CommandImpl { let mut json = BTreeMap::new(); if let Some(sha256) = sha256 { - let user = get_card_user_sw1_81(pass)?; + let user = crate::pgpcardutil::get_card_user_sw1_81(pass)?; let sha256_hex = opt_result!(hex::decode(sha256), "Decode sha256 failed: {}"); let sha256_hex = copy_sha256(&sha256_hex)?; let sig = user.signature_for_hash(Hash::SHA256(sha256_hex))?; @@ -52,7 +52,7 @@ impl Command for CommandImpl { } } if let Some(sha384) = sha384 { - let user = get_card_user_sw1_81(pass)?; + let user = crate::pgpcardutil::get_card_user_sw1_81(pass)?; let sha384_hex = opt_result!(hex::decode(sha384), "Decode sha384 failed: {}"); let sha384_hex = copy_sha384(&sha384_hex)?; let sig = user.signature_for_hash(Hash::SHA384(sha384_hex))?; @@ -66,7 +66,7 @@ impl Command for CommandImpl { } } if let Some(sha512) = sha512 { - let user = get_card_user_sw1_81(pass)?; + let user = crate::pgpcardutil::get_card_user_sw1_81(pass)?; let sha512_hex = opt_result!(hex::decode(sha512), "Decode sha512 failed: {}"); let sha512_hex = copy_sha512(&sha512_hex)?; let sig = user.signature_for_hash(Hash::SHA512(sha512_hex))?; @@ -106,20 +106,3 @@ macro_rules! define_copy_array { define_copy_array!(copy_sha256, 0x20); define_copy_array!(copy_sha384, 0x30); define_copy_array!(copy_sha512, 0x40); - -fn get_card_user_sw1_81(pass: &str) -> XResult { - match OpenPGPCard::list_cards() { - Ok(list) => { - // pw1_81 for signature - // openssl dgst -sha256 -verify aa -signature sig LICENSE - if list.is_empty() { - return simple_error!("Cannot find any card"); - } - match list.into_iter().next().unwrap().verify_pw1_81(pass) { - Result::Ok(user) => Ok(user), - Result::Err(_) => simple_error!("Verify pw1_81 OpenPGP card failed"), - } - } - Err(e) => simple_error!("Read OpenPGP card failed: {}", e), - } -} diff --git a/src/pgpcardutil.rs b/src/pgpcardutil.rs new file mode 100644 index 0000000..12cc831 --- /dev/null +++ b/src/pgpcardutil.rs @@ -0,0 +1,20 @@ +use rust_util::XResult; +use openpgp_card::{OpenPGPCardUser, OpenPGPCard}; + +pub fn get_card_user_sw1_81(pass: &str) -> XResult { + match OpenPGPCard::list_cards() { + Ok(list) => { + // pw1_81 for signature + // openssl dgst -sha256 -verify aa -signature sig LICENSE + if list.is_empty() { + return simple_error!("Cannot find any card"); + } + match list.into_iter().next().unwrap().verify_pw1_81(pass) { + Result::Ok(user) => Ok(user), + Result::Err(_) => simple_error!("Verify pw1_81 OpenPGP card failed"), + } + } + Err(e) => simple_error!("Read OpenPGP card failed: {}", e), + } +} +