52 lines
1.9 KiB
Rust
52 lines
1.9 KiB
Rust
use openssl::bn::BigNum;
|
|
use openssl::rsa::Rsa;
|
|
use pem::Pem;
|
|
use sequoia_openpgp::crypto::mpi::PublicKey;
|
|
use openpgp_card::crypto_data::PublicKeyMaterial;
|
|
|
|
use crate::digest::sha256_bytes;
|
|
|
|
pub fn sequoia_openpgp_public_key_pem(public_key: &PublicKey) -> Option<(Vec<u8>, String)> {
|
|
match public_key {
|
|
PublicKey::RSA { e, n } => {
|
|
let rsa_pub_key = Rsa::from_public_components(
|
|
BigNum::from_slice(n.value()).unwrap(),
|
|
BigNum::from_slice(e.value()).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_bytes,
|
|
};
|
|
Some((rsa_pub_key_bytes_sha256, pem::encode(&pub_key_pem_obj)))
|
|
}
|
|
_ => {
|
|
warning!("Not RSA public key: {:?}", public_key);
|
|
None
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn openpgp_card_public_key_pem(public_key: &PublicKeyMaterial) -> Option<(Vec<u8>, 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_bytes,
|
|
};
|
|
Some((rsa_pub_key_bytes_sha256, pem::encode(&pub_key_pem_obj)))
|
|
}
|
|
_ => {
|
|
warning!("Not RSA public key: {:?}", public_key);
|
|
None
|
|
}
|
|
}
|
|
}
|