diff --git a/Cargo.lock b/Cargo.lock index bedbd2b..a52aec0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -384,7 +384,7 @@ dependencies = [ [[package]] name = "card-cli" -version = "1.1.2" +version = "1.1.3" dependencies = [ "authenticator", "base64 0.13.0", diff --git a/Cargo.toml b/Cargo.toml index 35d5520..c7a84b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "card-cli" -version = "1.1.2" +version = "1.1.3" authors = ["Hatter Jiang "] edition = "2018" diff --git a/README.md b/README.md index 8341c56..3e0471e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ cargo install --git https://git.hatter.ink/hatter/card-cli.git ## encrypt & decrypt -sample public key +sample encrypt public key ``` -----BEGIN PUBLIC KEY----- MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEApUM8M+QRMUw0dIvXISFx @@ -43,7 +43,7 @@ $ cargo r -- pgp-card-decrypt -c $(cat enc.txt | xxd -ps -c 11111) sign ``` -$ cargo r -- pgp-card-sign -2 $(shasum -a 256 test.txt | awk '{print $1}') +$ cargo r -- pgp-card-sign -2 $(shasum -a 256 test.txt | awk '{print $1}') OR diff --git a/src/cmd_pgpcardlist.rs b/src/cmd_pgpcardlist.rs index 07f7cbd..202ef3a 100644 --- a/src/cmd_pgpcardlist.rs +++ b/src/cmd_pgpcardlist.rs @@ -9,6 +9,8 @@ use openssl::rsa::Rsa; use pem::Pem; use rust_util::util_clap::{Command, CommandError}; +use crate::digest::sha256_bytes; + pub struct CommandImpl; impl Command for CommandImpl { @@ -110,9 +112,11 @@ impl Command for CommandImpl { } if detail_output { if let Ok(public_key) = trans.public_key(KeyType::Authentication) { - if let Some(public_key_pem) = public_key_pem(&public_key) { + if let Some((public_key_sha256, public_key_pem)) = public_key_pem(&public_key) { + information!("Authentication public key sha256: {}", hex::encode(&public_key_sha256)); information!("Authentication public key: {}", public_key_pem.trim()); if json_output { + json.insert("authentication_public_key_sha256", hex::encode(&public_key_sha256)); json.insert("authentication_public_key_pem", public_key_pem); } } @@ -129,9 +133,11 @@ impl Command for CommandImpl { } if detail_output { if let Ok(public_key) = trans.public_key(KeyType::Decryption) { - if let Some(public_key_pem) = public_key_pem(&public_key) { + if let Some((public_key_sha256, public_key_pem)) = public_key_pem(&public_key) { + information!("Encryption public key sha256: {}", hex::encode(&public_key_sha256)); information!("Encryption public key: {}", public_key_pem.trim()); if json_output { + json.insert("encryption_public_key_sha256", hex::encode(&public_key_sha256)); json.insert("encryption_public_key_pem", public_key_pem); } } @@ -148,9 +154,11 @@ impl Command for CommandImpl { } if detail_output { if let Ok(public_key) = trans.public_key(KeyType::Signing) { - if let Some(public_key_pem) = public_key_pem(&public_key) { + if let Some((public_key_sha256, public_key_pem)) = public_key_pem(&public_key) { + information!("Signature public key sha256: {}", hex::encode(&public_key_sha256)); information!("Signature public key: {}", public_key_pem.trim()); if json_output { + json.insert("signature_public_key_sha256", hex::encode(&public_key_sha256)); json.insert("signature_public_key_pem", public_key_pem); } } @@ -169,18 +177,20 @@ impl Command for CommandImpl { } } -fn public_key_pem(public_key: &PublicKeyMaterial) -> Option { +fn public_key_pem(public_key: &PublicKeyMaterial) -> Option<(Vec, String)> { match public_key { PublicKeyMaterial::R(rsa_pub) => { let rsa_pub_key = Rsa::from_public_components( BigNum::from_slice(rsa_pub.n()).unwrap(), BigNum::from_slice(rsa_pub.v()).unwrap(), ); + let rsa_pub_key_bytes = rsa_pub_key.unwrap().public_key_to_der().unwrap(); + let rsa_pub_key_bytes_sha256 = sha256_bytes(&rsa_pub_key_bytes); let pub_key_pem_obj = Pem { tag: String::from("PUBLIC KEY"), - contents: rsa_pub_key.unwrap().public_key_to_der().unwrap(), + contents: rsa_pub_key_bytes, }; - Some(pem::encode(&pub_key_pem_obj)) + Some((rsa_pub_key_bytes_sha256, pem::encode(&pub_key_pem_obj))) } _ => { warning!("Not RSA public key: {:?}", public_key);