From e7b20abd6d1b06a7773aed71e1929c4ec9d950ed Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Mon, 31 Mar 2025 00:22:20 +0800 Subject: [PATCH] feat: v1.11.16 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cmd_keypair_generate.rs | 6 +++++- src/ecdsautil.rs | 10 ++++++---- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c552681..8f9a22f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -508,7 +508,7 @@ dependencies = [ [[package]] name = "card-cli" -version = "1.11.15" +version = "1.11.16" dependencies = [ "aes-gcm-stream", "authenticator 0.3.1", diff --git a/Cargo.toml b/Cargo.toml index 78e3036..f733375 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "card-cli" -version = "1.11.15" +version = "1.11.16" authors = ["Hatter Jiang "] edition = "2018" diff --git a/src/cmd_keypair_generate.rs b/src/cmd_keypair_generate.rs index d7e543c..7ecfd9f 100644 --- a/src/cmd_keypair_generate.rs +++ b/src/cmd_keypair_generate.rs @@ -3,6 +3,7 @@ use crate::{cmdutil, ecdsautil, hmacutil, util}; use clap::{App, Arg, ArgMatches, SubCommand}; use rust_util::util_clap::{Command, CommandError}; use std::collections::BTreeMap; +use crate::util::base64_encode; pub struct CommandImpl; @@ -44,7 +45,7 @@ impl Command for CommandImpl { } } - let (pkcs8_base64, secret_key_pem, public_key_pem, jwk_ec_key) = match key_type.as_str() { + let (pkcs8_base64, secret_key_pem, public_key_pem, public_key_der, jwk_ec_key) = match key_type.as_str() { "p256" => ecdsautil::generate_p256_keypair()?, "p384" => ecdsautil::generate_p384_keypair()?, _ => { @@ -59,6 +60,7 @@ impl Command for CommandImpl { } else { (pkcs8_base64, secret_key_pem) }; + let public_key_base64 = base64_encode(&public_key_der); let keychain_key_uri = if let Some(keychain_name) = keychain_name { let keychain_key_value = KeychainKeyValue { @@ -89,6 +91,7 @@ impl Command for CommandImpl { } } json.insert("public_key_pem", public_key_pem); + json.insert("public_key_base64", public_key_base64); json.insert("public_key_jwk", jwk_ec_key.to_string()); util::print_pretty_json(&json); @@ -103,6 +106,7 @@ impl Command for CommandImpl { } } information!("Public key PEM:\n{}", public_key_pem); + information!("Public key Base64:\n{}\n", public_key_base64); information!("Public key JWK:\n{}", jwk_ec_key.to_string()); } diff --git a/src/ecdsautil.rs b/src/ecdsautil.rs index 55f5ccd..f33be91 100644 --- a/src/ecdsautil.rs +++ b/src/ecdsautil.rs @@ -56,22 +56,24 @@ fn trim_point_leading_zero(p: &[u8]) -> Vec { } } -pub fn generate_p256_keypair() -> XResult<(String, String, String, JwkEcKey)> { +pub fn generate_p256_keypair() -> XResult<(String, String, String, Vec, JwkEcKey)> { let secret_key = p256::SecretKey::random(&mut rand::thread_rng()); let secret_key_der_base64 = base64_encode(secret_key.to_pkcs8_der()?.as_bytes()); let secret_key_pem = secret_key.to_pkcs8_pem(LineEnding::LF)?.to_string(); let public_key_pem = secret_key.public_key().to_public_key_pem(LineEnding::LF)?; + let public_key_der = secret_key.public_key().to_public_key_der()?.to_vec(); let jwk_ec_key = secret_key.public_key().to_jwk(); - Ok((secret_key_der_base64, secret_key_pem, public_key_pem, jwk_ec_key)) + Ok((secret_key_der_base64, secret_key_pem, public_key_pem, public_key_der, jwk_ec_key)) } -pub fn generate_p384_keypair() -> XResult<(String, String, String, JwkEcKey)> { +pub fn generate_p384_keypair() -> XResult<(String, String, String, Vec, JwkEcKey)> { let secret_key = p384::SecretKey::random(&mut rand::thread_rng()); let secret_key_der_base64 = base64_encode(secret_key.to_pkcs8_der()?.as_bytes()); let secret_key_pem = secret_key.to_pkcs8_pem(LineEnding::LF)?.to_string(); let public_key_pem = secret_key.public_key().to_public_key_pem(LineEnding::LF)?; + let public_key_der = secret_key.public_key().to_public_key_der()?.to_vec(); let jwk_ec_key = secret_key.public_key().to_jwk(); - Ok((secret_key_der_base64, secret_key_pem, public_key_pem, jwk_ec_key)) + Ok((secret_key_der_base64, secret_key_pem, public_key_pem, public_key_der, jwk_ec_key)) } macro_rules! parse_ecdsa_private_key {