use alloc::boxed::Box;
use crate::key::{Error, PrivateKey, PublicKey};
use crate::x509::{AnyPrivateKey, AnyPublicKey, Pkcs8ReadOptions};
pub fn private_key_from_pkcs8_der(der: &[u8]) -> Result<Box<dyn PrivateKey>, Error> {
Ok(AnyPrivateKey::from_pkcs8_der(der, Pkcs8ReadOptions::new())
.map_err(|_| Error::Encoding)?
.into_dyn())
}
pub fn private_key_from_pkcs8_pem(pem: &str) -> Result<Box<dyn PrivateKey>, Error> {
Ok(AnyPrivateKey::from_pkcs8_pem(pem, Pkcs8ReadOptions::new())
.map_err(|_| Error::Encoding)?
.into_dyn())
}
pub fn private_key_from_pkcs8_der_encrypted(
der: &[u8],
password: &[u8],
) -> Result<Box<dyn PrivateKey>, Error> {
Ok(
AnyPrivateKey::from_pkcs8_der(der, Pkcs8ReadOptions::new().password(password))
.map_err(|_| Error::Encoding)?
.into_dyn(),
)
}
pub fn private_key_from_pkcs8_pem_encrypted(
pem: &str,
password: &[u8],
) -> Result<Box<dyn PrivateKey>, Error> {
Ok(
AnyPrivateKey::from_pkcs8_pem(pem, Pkcs8ReadOptions::new().password(password))
.map_err(|_| Error::Encoding)?
.into_dyn(),
)
}
pub fn public_key_from_spki_der(der: &[u8]) -> Result<Box<dyn PublicKey>, Error> {
Ok(AnyPublicKey::from_spki_der(der)
.map_err(|_| Error::Encoding)?
.into_dyn())
}
pub fn public_key_from_spki_pem(pem: &str) -> Result<Box<dyn PublicKey>, Error> {
Ok(AnyPublicKey::from_spki_pem(pem)
.map_err(|_| Error::Encoding)?
.into_dyn())
}