chore: add pgpcardutil.rs
This commit is contained in:
@@ -7,6 +7,7 @@ mod digest;
|
|||||||
mod register;
|
mod register;
|
||||||
mod sign;
|
mod sign;
|
||||||
mod pgp;
|
mod pgp;
|
||||||
|
mod pgpcardutil;
|
||||||
mod pgpcardlist;
|
mod pgpcardlist;
|
||||||
mod pgpcardsign;
|
mod pgpcardsign;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use clap::{ArgMatches, SubCommand, App, Arg};
|
use clap::{ArgMatches, SubCommand, App, Arg};
|
||||||
use crate::cmd::{Command, CommandError};
|
use crate::cmd::{Command, CommandError};
|
||||||
use openpgp_card::{OpenPGPCard, Hash, OpenPGPCardUser};
|
use openpgp_card::Hash;
|
||||||
use rust_util::XResult;
|
use rust_util::XResult;
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ impl Command for CommandImpl {
|
|||||||
|
|
||||||
let mut json = BTreeMap::new();
|
let mut json = BTreeMap::new();
|
||||||
if let Some(sha256) = sha256 {
|
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 = opt_result!(hex::decode(sha256), "Decode sha256 failed: {}");
|
||||||
let sha256_hex = copy_sha256(&sha256_hex)?;
|
let sha256_hex = copy_sha256(&sha256_hex)?;
|
||||||
let sig = user.signature_for_hash(Hash::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 {
|
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 = opt_result!(hex::decode(sha384), "Decode sha384 failed: {}");
|
||||||
let sha384_hex = copy_sha384(&sha384_hex)?;
|
let sha384_hex = copy_sha384(&sha384_hex)?;
|
||||||
let sig = user.signature_for_hash(Hash::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 {
|
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 = opt_result!(hex::decode(sha512), "Decode sha512 failed: {}");
|
||||||
let sha512_hex = copy_sha512(&sha512_hex)?;
|
let sha512_hex = copy_sha512(&sha512_hex)?;
|
||||||
let sig = user.signature_for_hash(Hash::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_sha256, 0x20);
|
||||||
define_copy_array!(copy_sha384, 0x30);
|
define_copy_array!(copy_sha384, 0x30);
|
||||||
define_copy_array!(copy_sha512, 0x40);
|
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
20
src/pgpcardutil.rs
Normal 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user