feat: v1.2.1, pgp-card-make print-*-keys

This commit is contained in:
2022-04-14 23:24:38 +08:00
parent d9a5b5c831
commit ef100c2921
5 changed files with 68 additions and 6 deletions

View File

@@ -1,5 +1,6 @@
use openssl::bn::{BigNum, BigNumContext};
use openssl::rsa::Padding;
use openssl::pkey::PKey;
use openssl::rsa::{Padding, Rsa};
use rust_util::XResult;
#[derive(Debug)]
@@ -26,6 +27,31 @@ impl RsaCrt {
pub fn from(p: BigNum, q: BigNum, e: BigNum) -> XResult<RsaCrt> {
Ok(opt_result!( inner_from(p, q, e), "Calc RsaCrt failed: {}"))
}
pub fn to_public_key_pem(&self) -> XResult<String> {
Ok(crate::pkiutil::rsa_public_key_pem(
clone_big_num(&self.modulus)?.to_vec().as_slice(),
clone_big_num(&self.public_exponent)?.to_vec().as_slice(),
).1)
}
pub fn to_pem(&self) -> XResult<String> {
let private_key = opt_result!(Rsa::from_private_components(
clone_big_num(&self.modulus)?,
clone_big_num(&self.public_exponent)?,
clone_big_num(&self.private_exponent)?,
clone_big_num(&self.prime1)?,
clone_big_num(&self.prime2)?,
clone_big_num(&self.exponent1)?,
clone_big_num(&self.exponent2)?,
clone_big_num(&self.coefficient)?,
), "From private components failed: {}");
let private_pkey = opt_result!(PKey::from_rsa(private_key), "From rsa to pkey failed: {}");
// let k = private_key.private_key_to_pem_passphrase(Cipher::aes_128_gcm(), passphrase);
let private_key_pem = opt_result!(private_pkey.private_key_to_pem_pkcs8(), "Private key to pem failed: {}");
Ok(opt_result!(String::from_utf8(private_key_pem), "Pem to string failed: {}").trim().to_string())
}
}
pub fn clone_big_num(n: &BigNum) -> XResult<BigNum> {