chore: add pgpcardutil.rs

This commit is contained in:
2021-07-11 10:02:30 +08:00
parent 98f4d475fb
commit 41724fd08e
3 changed files with 25 additions and 21 deletions

View File

@@ -7,6 +7,7 @@ mod digest;
mod register;
mod sign;
mod pgp;
mod pgpcardutil;
mod pgpcardlist;
mod pgpcardsign;

View File

@@ -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<OpenPGPCardUser> {
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),
}
}

20
src/pgpcardutil.rs Normal file
View File

@@ -0,0 +1,20 @@
use rust_util::XResult;
use openpgp_card::{OpenPGPCardUser, OpenPGPCard};
pub fn get_card_user_sw1_81(pass: &str) -> XResult<OpenPGPCardUser> {
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),
}
}