use crate::{error, EncryptedPrivateKeyInfo, Error, Result};
use alloc::{borrow::ToOwned, vec::Vec};
use core::{
convert::{TryFrom, TryInto},
fmt,
};
use der::Encodable;
use zeroize::{Zeroize, Zeroizing};
#[cfg(feature = "encryption")]
use crate::PrivateKeyDocument;
#[cfg(feature = "pem")]
use {
crate::{encrypted_private_key_info::PEM_TYPE_LABEL, pem, LineEnding},
alloc::string::String,
core::str::FromStr,
};
#[cfg(feature = "std")]
use {
super::private_key::write_secret_file,
std::{fs, path::Path},
};
#[derive(Clone, Eq, PartialEq)]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg_attr(docsrs, doc(cfg(feature = "pkcs5")))]
pub struct EncryptedPrivateKeyDocument(Zeroizing<Vec<u8>>);
impl EncryptedPrivateKeyDocument {
#[cfg(feature = "encryption")]
#[cfg_attr(docsrs, doc(cfg(feature = "encryption")))]
pub fn decrypt(&self, password: impl AsRef<[u8]>) -> Result<PrivateKeyDocument> {
self.encrypted_private_key_info().decrypt(password)
}
pub fn encrypted_private_key_info(&self) -> EncryptedPrivateKeyInfo<'_> {
EncryptedPrivateKeyInfo::try_from(self.0.as_ref())
.expect("malformed EncryptedPrivateKeyDocument")
}
pub fn from_der(bytes: &[u8]) -> Result<Self> {
bytes.try_into()
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn from_pem(s: &str) -> Result<Self> {
let (label, der_bytes) = pem::decode_vec(s.as_bytes())?;
if label != PEM_TYPE_LABEL {
return Err(pem::Error::Label.into());
}
Self::from_der(&*der_bytes)
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn to_pem(&self) -> Zeroizing<String> {
self.to_pem_with_le(LineEnding::default())
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
pub fn to_pem_with_le(&self, line_ending: LineEnding) -> Zeroizing<String> {
Zeroizing::new(
pem::encode_string(PEM_TYPE_LABEL, line_ending, &self.0)
.expect(error::PEM_ENCODING_MSG),
)
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn read_der_file(path: impl AsRef<Path>) -> Result<Self> {
fs::read(path)?.try_into()
}
#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn read_pem_file(path: impl AsRef<Path>) -> Result<Self> {
Self::from_pem(&Zeroizing::new(fs::read_to_string(path)?))
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn write_der_file(&self, path: impl AsRef<Path>) -> Result<()> {
write_secret_file(path, self.as_ref())
}
#[cfg(all(feature = "pem", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn write_pem_file(&self, path: impl AsRef<Path>) -> Result<()> {
write_secret_file(path, self.to_pem().as_bytes())
}
}
impl AsRef<[u8]> for EncryptedPrivateKeyDocument {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl From<EncryptedPrivateKeyInfo<'_>> for EncryptedPrivateKeyDocument {
fn from(key: EncryptedPrivateKeyInfo<'_>) -> EncryptedPrivateKeyDocument {
EncryptedPrivateKeyDocument::from(&key)
}
}
impl From<&EncryptedPrivateKeyInfo<'_>> for EncryptedPrivateKeyDocument {
fn from(key: &EncryptedPrivateKeyInfo<'_>) -> EncryptedPrivateKeyDocument {
key.to_vec()
.ok()
.and_then(|buf| buf.try_into().ok())
.expect(error::DER_ENCODING_MSG)
}
}
impl TryFrom<&[u8]> for EncryptedPrivateKeyDocument {
type Error = Error;
fn try_from(bytes: &[u8]) -> Result<Self> {
EncryptedPrivateKeyInfo::try_from(bytes)?;
Ok(Self(Zeroizing::new(bytes.to_owned())))
}
}
impl TryFrom<Vec<u8>> for EncryptedPrivateKeyDocument {
type Error = Error;
fn try_from(mut bytes: Vec<u8>) -> Result<Self> {
if let Err(err) = EncryptedPrivateKeyInfo::try_from(bytes.as_slice()) {
bytes.zeroize();
return Err(err);
}
Ok(Self(Zeroizing::new(bytes)))
}
}
impl fmt::Debug for EncryptedPrivateKeyDocument {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_tuple("EncryptedPrivateKeyDocument")
.field(&self.encrypted_private_key_info())
.finish()
}
}
#[cfg(feature = "pem")]
#[cfg_attr(docsrs, doc(cfg(feature = "pem")))]
impl FromStr for EncryptedPrivateKeyDocument {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
Self::from_pem(s)
}
}