feat: v1.7.6, add subcommand age-address

This commit is contained in:
2023-10-06 10:46:45 +08:00
parent 78824f279b
commit 4b7743a68b
4 changed files with 107 additions and 2 deletions

52
src/cmd_ageaddress.rs Normal file
View File

@@ -0,0 +1,52 @@
use bech32::{ToBase32, Variant};
use clap::{App, ArgMatches, SubCommand};
use openpgp_card::{KeyType, OpenPgp};
use openpgp_card::algorithm::{Algo, Curve};
use openpgp_card::crypto_data::{EccType, PublicKeyMaterial};
use openpgp_card_pcsc::PcscBackend;
use rust_util::util_clap::{Command, CommandError};
const AGE_PUBLIC_KEY_PREFIX: &str = "age";
pub struct CommandImpl;
impl Command for CommandImpl {
fn name(&self) -> &str { "age-address" }
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("OpenPGP Card Encryption key to age address")
}
fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError {
let cards = opt_result!(PcscBackend::cards(None), "Failed to list OpenPGP cards: {}");
information!("Found {} card(s)", cards.len());
for (i, card) in cards.into_iter().enumerate() {
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());
}
let encryption_public_key = match trans.public_key(KeyType::Decryption) {
Ok(pub_key) => pub_key,
Err(e) => return simple_error!("Get decryption public key failed: {}", e),
};
if let PublicKeyMaterial::E(ecc_pub) = &encryption_public_key {
if let Algo::Ecc(ecc) = ecc_pub.algo() {
if let (EccType::ECDH, Curve::Cv25519) = (ecc.ecc_type(), ecc.curve()) {
let pub_key_bytes = ecc_pub.data();
let age_address = opt_result!(bech32::encode(
AGE_PUBLIC_KEY_PREFIX,
pub_key_bytes.to_base32(),
Variant::Bech32,
), "Generate age address failed: {}");
success!("Age address: {}", age_address);
return Ok(None);
}
}
}
return simple_error!("Not supported encryption key: {}", encryption_public_key);
}
Ok(None)
}
}

View File

@@ -35,6 +35,7 @@ mod cmd_pivgenerate;
mod cmd_chall;
mod cmd_challconfig;
mod cmd_sshagent;
mod cmd_ageaddress;
pub struct DefaultCommandImpl;
@@ -49,6 +50,10 @@ impl DefaultCommandImpl {
}
fn main() {
// Run with: RUST_LOG=debug, for more: https://docs.rs/env_logger/0.10.0/env_logger/
#[cfg(debug_assertions)]
env_logger::init();
match inner_main() {
Err(e) => failure_and_exit!("Run cli error: {}", e),
Ok(Some(code)) => std::process::exit(code),
@@ -81,6 +86,7 @@ fn inner_main() -> CommandError {
Box::new(cmd_u2fregister::CommandImpl),
Box::new(cmd_u2fsign::CommandImpl),
Box::new(cmd_sshagent::CommandImpl),
Box::new(cmd_ageaddress::CommandImpl),
];
let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))